1 /* vi: set sw=4 ts=4: */
6 * Russ Dill <Russ.Dill@asu.edu> July 2001
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
13 /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
14 #define WANT_PIDFILE 1
21 static int timeout; /* = 0. Must be signed */
22 static uint32_t requested_ip; /* = 0 */
23 static uint32_t server_addr;
24 static int sockfd = -1;
27 #define LISTEN_KERNEL 1
29 static smallint listen_mode;
31 static smallint state;
33 /* struct client_config_t client_config is in bb_common_bufsiz1 */
36 /* just a little helper */
37 static void change_listen_mode(int new_mode)
39 DEBUG("entering %s listen mode",
40 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
45 listen_mode = new_mode;
50 static void perform_renew(void)
52 bb_info_msg("Performing a DHCP renew");
55 change_listen_mode(LISTEN_KERNEL);
58 state = RENEW_REQUESTED;
60 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
61 udhcp_run_script(NULL, "deconfig");
64 change_listen_mode(LISTEN_RAW);
65 state = INIT_SELECTING;
73 /* perform a release */
74 static void perform_release(void)
76 char buffer[sizeof("255.255.255.255")];
77 struct in_addr temp_addr;
79 /* send release packet */
80 if (state == BOUND || state == RENEWING || state == REBINDING) {
81 temp_addr.s_addr = server_addr;
82 strcpy(buffer, inet_ntoa(temp_addr));
83 temp_addr.s_addr = requested_ip;
84 bb_info_msg("Unicasting a release of %s to %s",
85 inet_ntoa(temp_addr), buffer);
86 send_release(server_addr, requested_ip); /* unicast */
87 udhcp_run_script(NULL, "deconfig");
89 bb_info_msg("Entering released state");
91 change_listen_mode(LISTEN_NONE);
97 static void client_background(void)
100 bb_error_msg("cannot background in uclinux (yet)");
101 /* ... mainly because udhcpc calls client_background()
102 * in _the _middle _of _udhcpc _run_, not at the start!
103 * If that will be properly disabled for NOMMU, client_background()
104 * will work on NOMMU too */
107 logmode &= ~LOGMODE_STDIO;
108 /* rewrite pidfile, as our pid is different now */
109 write_pidfile(client_config.pidfile);
111 /* Do not fork again. */
112 client_config.foreground = 1;
113 client_config.background_if_no_lease = 0;
117 static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
120 int len = strlen(str);
121 if (len > 255) len = 255;
122 storage = xzalloc(len + extra + OPT_DATA);
123 storage[OPT_CODE] = code;
124 storage[OPT_LEN] = len + extra;
125 memcpy(storage + extra + OPT_DATA, str, len);
130 int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
131 int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
133 uint8_t *temp, *message;
134 char *str_c, *str_V, *str_h, *str_F, *str_r;
135 USE_FEATURE_UDHCP_PORT(char *str_P;)
136 llist_t *list_O = NULL;
137 #if ENABLE_FEATURE_UDHCPC_ARPING
140 int tryagain_timeout = 20;
141 int discover_timeout = 3;
142 int discover_retries = 3;
144 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
146 unsigned already_waited_sec;
151 struct dhcpMessage packet;
175 #if ENABLE_FEATURE_UDHCPC_ARPING
182 #if ENABLE_GETOPT_LONG
183 static const char udhcpc_longopts[] ALIGN1 =
184 "clientid\0" Required_argument "c"
185 "clientid-none\0" No_argument "C"
186 "vendorclass\0" Required_argument "V"
187 "foreground\0" No_argument "f"
188 "background\0" No_argument "b"
189 "hostname\0" Required_argument "H"
190 "fqdn\0" Required_argument "F"
191 "interface\0" Required_argument "i"
192 "now\0" No_argument "n"
193 "pidfile\0" Required_argument "p"
194 "quit\0" No_argument "q"
195 "release\0" No_argument "R"
196 "request\0" Required_argument "r"
197 "script\0" Required_argument "s"
198 "timeout\0" Required_argument "T"
199 "version\0" No_argument "v"
200 "retries\0" Required_argument "t"
201 "tryagain\0" Required_argument "A"
202 "syslog\0" No_argument "S"
203 #if ENABLE_FEATURE_UDHCPC_ARPING
204 "arping\0" No_argument "a"
206 "request-option\0" Required_argument "O"
207 #if ENABLE_FEATURE_UDHCP_PORT
208 "client-port\0" Required_argument "P"
210 "no-default-options\0" No_argument "o"
213 /* Default options. */
214 #if ENABLE_FEATURE_UDHCP_PORT
218 client_config.interface = "eth0";
219 client_config.script = DEFAULT_SCRIPT;
221 /* Parse command line */
222 /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */
223 opt_complementary = "c--C:C--c:O::T+:t+:A+";
224 #if ENABLE_GETOPT_LONG
225 applet_long_options = udhcpc_longopts;
227 opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
228 USE_FEATURE_UDHCPC_ARPING("aW:")
229 USE_FEATURE_UDHCP_PORT("P:")
231 , &str_c, &str_V, &str_h, &str_h, &str_F
232 , &client_config.interface, &client_config.pidfile, &str_r
233 , &client_config.script
234 , &discover_timeout, &discover_retries, &tryagain_timeout
235 USE_FEATURE_UDHCPC_ARPING(, &str_W)
236 USE_FEATURE_UDHCP_PORT(, &str_P)
241 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
244 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
246 client_config.foreground = 1;
248 client_config.background_if_no_lease = 1;
249 if (opt & (OPT_h|OPT_H))
250 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
252 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
254 S: 1 => Client requests Server to update A RR in DNS as well as PTR
255 O: 1 => Server indicates to client that DNS has been updated regardless
256 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
257 N: 1 => Client requests Server to not update DNS
259 client_config.fqdn[OPT_DATA + 0] = 0x1;
260 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
261 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
263 // if (opt & OPT_i) client_config.interface = ...
265 client_config.abort_if_no_lease = 1;
266 // if (opt & OPT_p) client_config.pidfile = ...
268 client_config.quit_after_lease = 1;
270 client_config.release_on_quit = 1;
272 requested_ip = inet_addr(str_r);
273 // if (opt & OPT_s) client_config.script = ...
274 // if (opt & OPT_T) discover_timeout = xatoi_u(str_T);
275 // if (opt & OPT_t) discover_retries = xatoi_u(str_t);
276 // if (opt & OPT_A) tryagain_timeout = xatoi_u(str_A);
278 puts("version "BB_VER);
282 openlog(applet_name, LOG_PID, LOG_LOCAL0);
283 logmode |= LOGMODE_SYSLOG;
285 #if ENABLE_FEATURE_UDHCP_PORT
287 CLIENT_PORT = xatou16(str_P);
288 SERVER_PORT = CLIENT_PORT - 1;
292 client_config.no_default_options = 1;
294 int n = index_in_strings(dhcp_option_strings, list_O->data);
296 bb_error_msg_and_die("unknown option '%s'", list_O->data);
297 n = dhcp_options[n].code;
298 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
299 list_O = list_O->link;
302 if (read_interface(client_config.interface, &client_config.ifindex,
303 NULL, client_config.arp))
306 /* Make sure fd 0,1,2 are open */
308 /* Equivalent of doing a fflush after every \n */
312 write_pidfile(client_config.pidfile);
313 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
315 /* Goes to stdout and possibly syslog */
316 bb_info_msg("%s (v"BB_VER") started", applet_name);
318 /* if not set, and not suppressed, setup the default client ID */
319 if (!client_config.clientid && !(opt & OPT_C)) {
320 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
321 client_config.clientid[OPT_DATA] = 1;
322 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
325 if (!client_config.vendorclass)
326 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
328 /* setup the signal pipe */
331 state = INIT_SELECTING;
332 udhcp_run_script(NULL, "deconfig");
333 change_listen_mode(LISTEN_RAW);
335 already_waited_sec = 0;
337 /* Main event loop. select() waits on signal pipe and possibly
339 * "continue" statements in code below jump to the top of the loop.
342 unsigned timestamp_before_wait;
344 if (listen_mode != LISTEN_NONE && sockfd < 0) {
345 if (listen_mode == LISTEN_KERNEL)
346 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
348 sockfd = raw_socket(client_config.ifindex);
350 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
352 tv.tv_sec = timeout - already_waited_sec;
354 retval = 0; /* If we already timed out, fall through, else... */
356 timestamp_before_wait = (unsigned)monotonic_sec();
357 DEBUG("Waiting on select...");
358 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
360 /* EINTR? A signal was caught, don't panic */
363 /* Else: an error occured, panic! */
364 bb_perror_msg_and_die("select");
368 /* If timeout dropped to zero, time to become active:
369 * resend discover/renew/whatever
372 /* We will restart wait afresh in any case */
373 already_waited_sec = 0;
377 if (packet_num < discover_retries) {
381 send_discover(xid, requested_ip); /* broadcast */
383 timeout = discover_timeout;
387 udhcp_run_script(NULL, "leasefail");
388 if (client_config.background_if_no_lease) {
389 bb_info_msg("No lease, forking to background");
391 } else if (client_config.abort_if_no_lease) {
392 bb_info_msg("No lease, failing");
396 /* wait before trying again */
397 timeout = tryagain_timeout;
400 case RENEW_REQUESTED:
402 if (packet_num < discover_retries) {
403 /* send request packet */
404 if (state == RENEW_REQUESTED) /* unicast */
405 send_renew(xid, server_addr, requested_ip);
407 send_selecting(xid, server_addr, requested_ip);
409 timeout = discover_timeout;
413 /* timed out, go back to init state */
414 if (state == RENEW_REQUESTED)
415 udhcp_run_script(NULL, "deconfig");
416 change_listen_mode(LISTEN_RAW);
417 state = INIT_SELECTING;
422 /* Half of the lease passed, time to enter renewing state */
423 change_listen_mode(LISTEN_KERNEL);
424 DEBUG("Entering renew state");
426 /* fall right through */
429 /* send a request packet */
430 send_renew(xid, server_addr, requested_ip); /* unicast */
434 /* Timed out, enter rebinding state */
435 DEBUG("Entering rebinding state");
437 /* fall right through */
439 /* Lease is *really* about to run out,
440 * try to find DHCP server using broadcast */
442 /* send a request packet */
443 send_renew(xid, 0, requested_ip); /* broadcast */
447 /* Timed out, enter init state */
448 bb_info_msg("Lease lost, entering init state");
449 udhcp_run_script(NULL, "deconfig");
450 change_listen_mode(LISTEN_RAW);
451 state = INIT_SELECTING;
452 /*timeout = 0; - already is */
457 /* yah, I know, *you* say it would never happen */
459 continue; /* back to main loop */
462 /* select() didn't timeout, something did happen. */
463 /* Is is a packet? */
464 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
466 /* A packet is ready, read it */
468 if (listen_mode == LISTEN_KERNEL)
469 len = udhcp_recv_kernel_packet(&packet, sockfd);
471 len = udhcp_recv_raw_packet(&packet, sockfd);
472 if (len == -1) { /* error is severe, reopen socket */
473 DEBUG("error on read, %s, reopening socket", strerror(errno));
474 sleep(discover_timeout); /* 3 seconds by default */
475 change_listen_mode(listen_mode); /* just close and reopen */
477 /* If this packet will turn out to be unrelated/bogus,
478 * we will go back and wait for next one.
479 * Be sure timeout is properly decreased. */
480 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
484 if (packet.xid != xid) {
485 DEBUG("Ignoring XID %x (our xid is %x)",
486 (unsigned)packet.xid, (unsigned)xid);
490 /* Ignore packets that aren't for us */
491 if (memcmp(packet.chaddr, client_config.arp, 6)) {
492 DEBUG("Packet does not have our chaddr - ignoring");
496 message = get_option(&packet, DHCP_MESSAGE_TYPE);
497 if (message == NULL) {
498 bb_error_msg("cannot get message type from packet - ignoring");
504 /* Must be a DHCPOFFER to one of our xid's */
505 if (*message == DHCPOFFER) {
506 /* TODO: why we don't just fetch server's IP from IP header? */
507 temp = get_option(&packet, DHCP_SERVER_ID);
509 bb_error_msg("no server ID in message");
511 /* still selecting - this server looks bad */
513 /* can be misaligned, thus memcpy */
514 memcpy(&server_addr, temp, 4);
516 requested_ip = packet.yiaddr;
518 /* enter requesting state */
522 already_waited_sec = 0;
525 case RENEW_REQUESTED:
529 if (*message == DHCPACK) {
530 temp = get_option(&packet, DHCP_LEASE_TIME);
532 bb_error_msg("no lease time with ACK, using 1 hour lease");
533 lease_seconds = 60 * 60;
535 /* can be misaligned, thus memcpy */
536 memcpy(&lease_seconds, temp, 4);
537 lease_seconds = ntohl(lease_seconds);
538 lease_seconds &= 0x0fffffff; /* paranoia: must not be negative */
540 #if ENABLE_FEATURE_UDHCPC_ARPING
542 if (!arpping(packet.yiaddr,
545 client_config.interface)
547 bb_info_msg("offered address is in use "
548 "(got ARP reply), declining");
549 send_decline(xid, server_addr, packet.yiaddr);
551 if (state != REQUESTING)
552 udhcp_run_script(NULL, "deconfig");
553 change_listen_mode(LISTEN_RAW);
554 state = INIT_SELECTING;
556 timeout = tryagain_timeout;
558 already_waited_sec = 0;
559 continue; /* back to main loop */
563 /* enter bound state */
564 timeout = lease_seconds / 2;
566 struct in_addr temp_addr;
567 temp_addr.s_addr = packet.yiaddr;
568 bb_info_msg("Lease of %s obtained, lease time %u",
569 inet_ntoa(temp_addr), (unsigned)lease_seconds);
571 requested_ip = packet.yiaddr;
572 udhcp_run_script(&packet,
573 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
576 change_listen_mode(LISTEN_NONE);
577 if (client_config.quit_after_lease) {
578 if (client_config.release_on_quit)
582 if (!client_config.foreground)
585 already_waited_sec = 0;
586 continue; /* back to main loop */
588 if (*message == DHCPNAK) {
589 /* return to init state */
590 bb_info_msg("Received DHCP NAK");
591 udhcp_run_script(&packet, "nak");
592 if (state != REQUESTING)
593 udhcp_run_script(NULL, "deconfig");
594 change_listen_mode(LISTEN_RAW);
595 sleep(3); /* avoid excessive network traffic */
596 state = INIT_SELECTING;
600 already_waited_sec = 0;
603 /* case BOUND, RELEASED: - ignore all packets */
605 continue; /* back to main loop */
608 /* select() didn't timeout, something did happen.
609 * But it wasn't a packet. It's a signal pipe then. */
611 int signo = udhcp_sp_read(&rfds);
615 /* start things over */
617 /* Kill any timeouts because the user wants this to hurry along */
624 bb_info_msg("Received SIGTERM");
625 if (client_config.release_on_quit)
630 } /* for (;;) - main loop ends */
635 /*if (client_config.pidfile) - remove_pidfile has its own check */
636 remove_pidfile(client_config.pidfile);