新闻  |   论坛  |   博客  |   在线研讨会
Busybox udhcpc:自动识别ip配置以及流程解析
电子禅石 | 2023-05-06 13:46:20    阅读:26296   发布文章

udhcpc来自于Busybox,基于DHCP服务,达到自动配置IP的功能。

下面分别介绍如何配置udhcpc,以及udhcpc是如何达到自动配置IP功能的。

1. 配置udhcpc1.1 配置kernel支持DHCP

进入Networking -> Networking options -> TCP/IP networking,选择IP: DHCP support。


1.2 配置udhcpc

进入Networking Utilities -> udhcpc (DHCP client)。



1.3 启动udhcpc

在/etc/init.d/rcS中启动udhcpc。

 #
 # To config network
 #
udhcpc
1.4 udhcpc使用说明

复制代码

BusyBox v1.27.2 (2020-08-25 12:39:58 CST) multi-call binary.

Usage: udhcpc [-fbqRB] [-a[MSEC]] [-t N] [-T SEC] [-A SEC/-n]
        [-i IFACE] [-s PROG] [-p PIDFILE]
        [-oC] [-r IP] [-V VENDOR] [-F NAME] [-x OPT:VAL]... [-O OPT]...        -i,--interface IFACE    Interface to use (default eth0)        -s,--script PROG        Run PROG at DHCP events (default /usr/share/udhcpc/default.script)        -p,--pidfile FILE       Create pidfile        -B,--broadcast          Request broadcast replies        -t,--retries N          Send up to N discover packets (default 3)        -T,--timeout SEC        Pause between packets (default 3)        -A,--tryagain SEC       Wait if lease is not obtained (default 20)        -n,--now                Exit if lease is not obtained        -q,--quit               Exit after obtaining lease        -R,--release            Release IP on exit        -f,--foreground         Run in foreground        -b,--background         Background if lease is not obtained        -S,--syslog             Log to syslog too        -a[MSEC],--arping[=MSEC] Validate offered address with ARP ping        -r,--request IP         Request this IP address        -o,--no-default-options Don't request any options (unless -O is given)
        -O,--request-option OPT Request option OPT from server (cumulative)        -x OPT:VAL              Include option OPT in sent packets (cumulative)
                                Examples of string, numeric, and hex byte opts:                                -x hostname:bbox - option 12
                                -x lease:3600 - option 51 (lease time)                                -x 0x3d:0100BEEFC0FFEE - option 61 (client id)        -F,--fqdn NAME          Ask server to update DNS mapping for NAME        -V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')        -C,--clientid-none      Don't send MAC as client identifierSignals:
        USR1    Renew lease
        USR2    Release lease

复制代码

2. 流程解析

整个udhcpc的框架是可执行文件udhcpc、脚本文件/usr/share/udhcpc/default.script、DNS配置文件/etc/resolv.conf。

入口是udhcpc,然后udhcpc调用shell脚本default.script中的deconfig/leasefail/bound/renew/nak等选项,resolv.conf存放DNS配置文件。

2.1 Kernel DHCP相关

DHCP在内核中是IP Autocofig dispatcher一部分,入口在ip_auto_config()。

内核负责将udhcpc发送的DHCP请求通过对应的网络设备发送出去,并将接受到的数据解析返回给udhcpc。

复制代码

static int __init ip_auto_config(void)
{...    if (ic_myaddr == NONE ||...
        ic_first_dev->next) {
#ifdef IPCONFIG_DYNAMIC        if (ic_dynamic() < 0) {
            ic_close_devs();...        }#else /* !DYNAMIC */...#endif /* IPCONFIG_DYNAMIC */
    } else {...
    }...
}static int __init ic_dynamic(void)
{...#ifdef IPCONFIG_BOOTP    if (do_bootp)
        ic_bootp_init();------------------------初始化DHCP/BOOTP相关,主要是注册处理从服务器接收到的DHCP/BOOTP回复。#endif#ifdef IPCONFIG_RARP    if (do_rarp)
        ic_rarp_init();#endif...    for (;;) {
#ifdef IPCONFIG_BOOTP        if (do_bootp && (d->able & IC_BOOTP))
            ic_bootp_send_if(d, jiffies - start_jiffies);---发送DHCP/BOOTP请求到d网络设备。#endif...        if (!d->next) {
            jiff = jiffies + timeout;            while (time_before(jiffies, jiff) && !ic_got_reply)
                schedule_timeout_uninterruptible(1);
        }
#ifdef IPCONFIG_DHCP        /* DHCP isn't done until we get a DHCPACK. */
        if ((ic_got_reply & IC_BOOTP) &&
            (ic_proto_enabled & IC_USE_DHCP) &&
            ic_dhcp_msgtype != DHCPACK) {
            ic_got_reply = 0;            /* continue on device that got the reply */
            d = ic_dev;
            pr_cont(",");            continue;
        }#endif /* IPCONFIG_DHCP */...
    }...
}

复制代码

 

ic_bootp_send_if()通过d指定的网络设备发送socket DHCP/BOOTP请求。

复制代码

/*
 *  Send DHCP/BOOTP request to single interface. */static void __init ic_bootp_send_if(struct ic_device *d, unsigned long jiffies_diff)
{    struct net_device *dev = d->dev;    struct sk_buff *skb;    struct bootp_pkt *b;    struct iphdr *h;    int hlen = LL_RESERVED_SPACE(dev);    int tlen = dev->needed_tailroom;    /* Allocate packet */
    skb = alloc_skb(sizeof(struct bootp_pkt) + hlen + tlen + 15,
            GFP_KERNEL);--------------------------分配skb,下面逐渐填充内容。    if (!skb)        return;
    skb_reserve(skb, hlen);
    b = (struct bootp_pkt *) skb_put(skb, sizeof(struct bootp_pkt));
    memset(b, 0, sizeof(struct bootp_pkt));    /* Construct IP header */
    skb_reset_network_header(skb);
    h = ip_hdr(skb);
    h->version = 4;
    h->ihl = 5;
    h->tot_len = htons(sizeof(struct bootp_pkt));
    h->frag_off = htons(IP_DF);
    h->ttl = 64;
    h->protocol = IPPROTO_UDP;
    h->daddr = htonl(INADDR_BROADCAST);
    h->check = ip_fast_csum((unsigned char *) h, h->ihl);    /* Construct UDP header */
    b->udph.source = htons(68);
    b->udph.dest = htons(67);
    b->udph.len = htons(sizeof(struct bootp_pkt) - sizeof(struct iphdr));    /* UDP checksum not calculated -- explicitly allowed in BOOTP RFC */

    /* Construct DHCP/BOOTP header */
    b->op = BOOTP_REQUEST;    if (dev->type < 256) /* check for false types */
        b->htype = dev->type;    else if (dev->type == ARPHRD_FDDI)
        b->htype = ARPHRD_ETHER;    else {
        pr_warn("Unknown ARP type 0x%04x for device %s\n", dev->type,
            dev->name);
        b->htype = dev->type; /* can cause undefined behavior */
    }    /* server_ip and your_ip address are both already zero per RFC2131 */
    b->hlen = dev->addr_len;
    memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
    b->secs = htons(jiffies_diff / HZ);
    b->xid = d->xid;    /* add DHCP options or BOOTP extensions */#ifdef IPCONFIG_DHCP    if (ic_proto_enabled & IC_USE_DHCP)
        ic_dhcp_init_options(b->exten, d);---------构建DHCP附加内容。    else#endif
        ic_bootp_init_ext(b->exten);    /* Chain packet down the line... */
    skb->dev = dev;
    skb->protocol = htons(ETH_P_IP);    if (dev_hard_header(skb, dev, ntohs(skb->protocol),
                dev->broadcast, dev->dev_addr, skb->len) < 0) {
        kfree_skb(skb);
        printk("E");        return;
    }    if (dev_queue_xmit(skb) < 0)-------------------发送skb。
        printk("E");
}

复制代码

 

2.2 udhcpc解析

和DHCP服务交互的状态如下:

复制代码

/* initial state: (re)start DHCP negotiation */#define INIT_SELECTING  0/* discover was sent, DHCPOFFER reply received */#define REQUESTING      1/* select/renew was sent, DHCPACK reply received */#define BOUND           2/* half of lease passed, want to renew it by sending unicast renew requests */#define RENEWING        3/* renew requests were not answered, lease is almost over, send broadcast renew */#define REBINDING       4/* manually requested renew (SIGUSR1) */#define RENEW_REQUESTED 5/* release, possibly manually requested (SIGUSR2) */#define RELEASED        6

复制代码

udhcpc遵循DHCP协议和DHCP服务器进行交互,通过状态机来解析从DHCP服务器获取的packet。

复制代码

 udhcpc_main( argc UNUSED_PARAM,  ** (udhcp_read_interface(client_config.& =...     pollfd pfds[== timeout -= 
         (tv > == poll(pfds, , tv < INT_MAX/ ? tv *  (retval < 
                 (errno ==+= (unsigned)monotonic_sec() -
         (retval ==              (udhcp_read_interface(client_config.& ret0; =  (!discover_retries || packet_num < (packet_num == ==++ BB_MMU /* -b is not supported on NOMMU */                 (opt & OPT_b) { = ((opt & ~OPT_b) |
                 (opt & OPT_n) { = ==  (packet_num < =++=== ; 
             RENEW_REQUESTED:  (timeout > >>= =
            
                 (timeout >  >>= == ; 
                = =; 

        
         (listen_mode == LISTEN_NONE || !pfds[; 
             (listen_mode === udhcp_recv_kernel_packet(&= udhcp_recv_raw_packet(& (len == -+= (unsigned)monotonic_sec() - (len <  (packet.xid !=
         (packet.hlen != 
         || memcmp(packet.chaddr, client_config.client_mac, ) != 
            log1(); 
            = udhcp_get_option(& (message ==
             (*message ==*= = udhcp_get_option(& (!=== = =  (*message ==*= udhcp_get_option(& (!=  * =
                     (lease_seconds < =  (lease_seconds >  / =  / 
                ===&packet, state == REQUESTING ?  : = (unsigned)monotonic_sec() -= lease_seconds /  ((unsigned)timeout <= already_waited_sec = = (opt & OPT_q) { 
                    &= ~ BB_MMU /* NOMMU case backgrounded earlier */                 (!(opt &= ((opt & ~OPT_b) |
                
                

                ;  (*message ==
                 (server_addr != *= udhcp_get_option(& (!
                             (svid !=, &packet,  (state !=); == ; = = = = 
         (opt & OPT_R) =

复制代码

udhcp_run_script()是udhcpc和default.script的接口,通过udhcp_run_script()即可调用其中一部分脚本。

复制代码

/* Call a script with a par file and env vars */static void udhcp_run_script(struct dhcp_packet *packet, const char *name)
{    char **envp, **curr;    char *argv[3];

    envp = fill_envp(packet);----------将options以及packet中的内容导出到shell环境变量中,下面执行shell的过程中使用。    /* call script */
    log1("executing %s %s", client_config.script, name);
    argv[0] = (char*) client_config.script;
    argv[1] = (char*) name;
    argv[2] = NULL;
    spawn_and_wait(argv);    for (curr = envp; *curr; curr++) {
        log2(" %s", *curr);
        bb_unsetenv_and_free(*curr);
    }
    free(envp);
}/* put all the parameters into the environment */static char **fill_envp(struct dhcp_packet *packet)
{    int envc;    int i;    char **envp, **curr;    const char *opt_name;
    uint8_t *temp;
    uint8_t overload = 0;#define BITMAP unsigned#define BBITS (sizeof(BITMAP) * 8)#define BMASK(i) (1 << (i & (sizeof(BITMAP) * 8 - 1)))#define FOUND_OPTS(i) (found_opts[(unsigned)i / BBITS])
    BITMAP found_opts[256 / BBITS];

    memset(found_opts, 0, sizeof(found_opts));    /* We need 6 elements for:
     * "interface=IFACE"
     * "ip=N.N.N.N" from packet->yiaddr
     * "siaddr=IP" from packet->siaddr_nip (unless 0)
     * "boot_file=FILE" from packet->file (unless overloaded)
     * "sname=SERVER_HOSTNAME" from packet->sname (unless overloaded)
     * terminating NULL     */
    envc = 6;    /* +1 element for each option, +2 for subnet option: */
    if (packet) {        /* note: do not search for "pad" (0) and "end" (255) options *///TODO: change logic to scan packet _once_
        for (i = 1; i < 255; i++) {
            temp = udhcp_get_option(packet, i);            if (temp) {                if (i == DHCP_OPTION_OVERLOAD)
                    overload |= *temp;                else if (i == DHCP_SUBNET)
                    envc++; /* for $mask */
                envc++;                /*if (i != DHCP_MESSAGE_TYPE)*/
                FOUND_OPTS(i) |= BMASK(i);
            }
        }
    }
    curr = envp = xzalloc(sizeof(envp[0]) * envc);    *curr = xasprintf("interface=%s", client_config.interface);
    putenv(*curr++);    if (!packet)        return envp;    /* Most important one: yiaddr as $ip */
    *curr = xmalloc(sizeof("ip=255.255.255.255"));
    sprint_nip(*curr, "ip=", (uint8_t *) &packet->yiaddr);
    putenv(*curr++);    if (packet->siaddr_nip) {        /* IP address of next server to use in bootstrap */
        *curr = xmalloc(sizeof("siaddr=255.255.255.255"));
        sprint_nip(*curr, "siaddr=", (uint8_t *) &packet->siaddr_nip);
        putenv(*curr++);
    }    if (!(overload & FILE_FIELD) && packet->file[0]) {        /* watch out for invalid packets */
        *curr = xasprintf("boot_file=%."DHCP_PKT_FILE_LEN_STR"s", packet->file);
        putenv(*curr++);
    }    if (!(overload & SNAME_FIELD) && packet->sname[0]) {        /* watch out for invalid packets */
        *curr = xasprintf("sname=%."DHCP_PKT_SNAME_LEN_STR"s", packet->sname);
        putenv(*curr++);
    }    /* Export known DHCP options */
    opt_name = dhcp_option_strings;
    i = 0;    while (*opt_name) {
        uint8_t code = dhcp_optflags[i].code;
        BITMAP *found_ptr = &FOUND_OPTS(code);
        BITMAP found_mask = BMASK(code);        if (!(*found_ptr & found_mask))            goto next;        *found_ptr &= ~found_mask; /* leave only unknown options */
        temp = udhcp_get_option(packet, code);        *curr = xmalloc_optname_optval(temp, &dhcp_optflags[i], opt_name);---将optname和value导出到shell环境变量中。
        putenv(*curr++);        if (code == DHCP_SUBNET) {            /* Subnet option: make things like "$ip/$mask" possible */
            uint32_t subnet;
            move_from_unaligned32(subnet, temp);            *curr = xasprintf("mask=%u", mton(subnet));
            putenv(*curr++);
        }
 next:
        opt_name += strlen(opt_name) + 1;
        i++;
    }...    return envp;
}

复制代码

 2.3 default.script脚本解析

default.script是udhcpc的延伸,方便灵活的实现udhcpc的功能。

复制代码

#!/bin/sh

# udhcpc script edited by Tim Riker <Tim@Rikers.org>[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1RESOLV_CONF="/etc/resolv.conf"[ -e $RESOLV_CONF ] || touch $RESOLV_CONF
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"[ -n "$subnet" ] && NETMASK="netmask $subnet"case "$1" in    deconfig)        /sbin/ifconfig $interface up        /sbin/ifconfig $interface 0.0.0.0--------------------$interface是udhcpc指定的,首先up启动设备;然后配置一个默认0.0.0.0地址。

        # drop info from this interface
        # resolv.conf may be a symlink to /tmp/, so take care
        TMPFILE=$(mktemp)
        grep -vE "# $interface\$" $RESOLV_CONF > $TMPFILE
        cat $TMPFILE > $RESOLV_CONF
        rm -f $TMPFILE        if [ -x /usr/sbin/avahi-autoipd ]; then            /usr/sbin/avahi-autoipd -k $interface
        fi
        ;;    leasefail|nak)        if [ -x /usr/sbin/avahi-autoipd ]; then            /usr/sbin/avahi-autoipd -wD $interface --no-chroot
        fi
        ;;    renew|bound)        if [ -x /usr/sbin/avahi-autoipd ]; then            /usr/sbin/avahi-autoipd -k $interface
        fi        /sbin/ifconfig $interface $ip $BROADCAST $NETMASK-----------根据udhcpc获取的interface/ip/BROADCASR/NETMASK,通过ifconfig配置到interface的设备。        if [ -n "$router" ] ; then
            echo "deleting routers"
            while route del default gw 0.0.0.0 dev $interface 2> /dev/null; do
                :
            done            for i in $router ; do
                route add default gw $i dev $interface--------------router不为空的情况下,先删除默认路由,重新建立路由。
            done
        fi

        # drop info from this interface
        # resolv.conf may be a symlink to /tmp/, so take care
        TMPFILE=$(mktemp)
        grep -vE "# $interface\$" $RESOLV_CONF > $TMPFILE
        cat $TMPFILE > $RESOLV_CONF
        rm -f $TMPFILE

        # prefer rfc3359 domain search list (option 119) if available        if [ -n "$search" ]; then
            search_list=$search
        elif [ -n "$domain" ]; then
            search_list=$domain
        fi

        [ -n "$search_list" ] &&
            echo "search $search_list # $interface" >> $RESOLV_CONF        for i in $dns ; do
            echo adding dns $i
            echo "nameserver $i # $interface" >> $RESOLV_CONF--------更新dns信息到/etc/resolv.conf中。
        done
        ;;
esac

HOOK_DIR="$0.d"for hook in "${HOOK_DIR}/"*; do
    [ -f "${hook}" -a -x "${hook}" ] || continue
    "${hook}" "${@}"done

exit 0

复制代码

 2.4 运行实例log 

 运行log如下:

复制代码

[2020-08-26 19:16:08] udhcpc: started, v1.27.2[2020-08-26 19:16:08] udhcpc: sending discover-------------------------------------发送DHCPDISCOVER到DHCP服务器,服务器会返回一个可用IP地址。[2020-08-26 19:16:10] udhcpc: sending select for 192.168.33.184--------------------发送DHCPREQUEST到DHCP服务器,是对DPCPOFFER的响应。[2020-08-26 19:16:10] udhcpc: lease of 192.168.33.184 obtained, lease time 86400---此时已经经过DHCP服务器分配IP,IP地址为192.168.33.184。然后调用default.script的bound或renew。[2020-08-26 19:16:10] deleting routers---------------------------------------------default.script中删除路由。
[2020-08-26 19:16:10] adding dns 192.168.33.1--------------------------------------配置DNS。

复制代码

3. DHCP协议

参考:《Dynamic Host Configuration Protocol

其中《3.1 Client-server interaction - allocating a network address》介绍了如何分配一个IP地址的流程。

复制代码

   Message         Use   -------         ---

   DHCPDISCOVER -  Client broadcast to locate available servers.

   DHCPOFFER    -  Server to client in response to DHCPDISCOVER with
                   offer of configuration parameters.

   DHCPREQUEST  -  Client message to servers either (a) requesting
                   offered parameters from one server and implicitly
                   declining offers from all others, (b) confirming
                   correctness of previously allocated address after,
                   e.g., system reboot, or (c) extending the lease on a
                   particular network address.

   DHCPACK      -  Server to client with configuration parameters,
                   including committed network address.

   DHCPNAK      -  Server to client indicating client's notion of network
                   address is incorrect (e.g., client has moved to new
                   subnet) or client's lease as expired
   DHCPDECLINE  -  Client to server indicating network address is already                   in use.

   DHCPRELEASE  -  Client to server relinquishing network address and
                   cancelling remaining lease.

   DHCPINFORM   -  Client to server, asking only for local configuration
                   parameters; client already has externally configured
                   network address.

                          Table 2:  DHCP messages

                Server          Client          Server
            (not selected)                    (selected)

                  v               v               v                  |               |               |
                  |     Begins initialization     |
                  |               |               |
                  | _____________/|\____________  |
                  |/DHCPDISCOVER | DHCPDISCOVER  \|
                  |               |               |
              Determines          |          Determines
             configuration        |         configuration                  |               |               |
                  |\             |  ____________/ |
                  | \________    | /DHCPOFFER     |
                  | DHCPOFFER\   |/               |
                  |           \  |                |
                  |       Collects replies        |
                  |             \|                |
                  |     Selects configuration     |
                  |               |               |
                  | _____________/|\____________  |
                  |/ DHCPREQUEST  |  DHCPREQUEST\ |
                  |               |               |
                  |               |     Commits configuration                  |               |               |
                  |               | _____________/|
                  |               |/ DHCPACK      |
                  |               |               |
                  |    Initialization complete    |
                  |               |               |
                  .               .               .
                  .               .               .                  |               |               |
                  |      Graceful shutdown        |
                  |               |               |
                  |               |\ ____________ |
                  |               | DHCPRELEASE  \|
                  |               |               |
                  |               |        Discards lease                  |               |               |
                  v               v               v
     Figure 3: Timeline diagram of messages exchanged between DHCP
               client and servers when allocating a new network address

复制代码

 

4. 小结

整个自动配置IP功能,需要Kernel支持DHCP、udhcpc、default.script。

在rcS中启动udhcpc,默认使用default.script脚本已达到自动配置ip、route等。

联系方式:arnoldlu@qq.com


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
属于自己的技术积累分享,成为嵌入式系统研发高手。
推荐文章
最近访客