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.
14 /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
15 #define WANT_PIDFILE 1
22 static int timeout; /* = 0. Must be signed */
23 static uint32_t requested_ip; /* = 0 */
24 static uint32_t server_addr;
25 static int sockfd = -1;
28 #define LISTEN_KERNEL 1
30 static smallint listen_mode;
32 static smallint state;
34 /* struct client_config_t client_config is in bb_common_bufsiz1 */
37 /* just a little helper */
38 static void change_listen_mode(int new_mode)
40 DEBUG("entering %s listen mode",
41 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
46 listen_mode = new_mode;
51 static void perform_renew(void)
53 bb_info_msg("Performing a DHCP renew");
56 change_listen_mode(LISTEN_KERNEL);
59 state = RENEW_REQUESTED;
61 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
62 udhcp_run_script(NULL, "deconfig");
65 change_listen_mode(LISTEN_RAW);
66 state = INIT_SELECTING;
74 /* perform a release */
75 static void perform_release(void)
77 char buffer[sizeof("255.255.255.255")];
78 struct in_addr temp_addr;
80 /* send release packet */
81 if (state == BOUND || state == RENEWING || state == REBINDING) {
82 temp_addr.s_addr = server_addr;
83 strcpy(buffer, inet_ntoa(temp_addr));
84 temp_addr.s_addr = requested_ip;
85 bb_info_msg("Unicasting a release of %s to %s",
86 inet_ntoa(temp_addr), buffer);
87 send_release(server_addr, requested_ip); /* unicast */
88 udhcp_run_script(NULL, "deconfig");
90 bb_info_msg("Entering released state");
92 change_listen_mode(LISTEN_NONE);
98 static void client_background(void)
101 bb_error_msg("cannot background in uclinux (yet)");
102 /* ... mainly because udhcpc calls client_background()
103 * in _the _middle _of _udhcpc _run_, not at the start!
104 * If that will be properly disabled for NOMMU, client_background()
105 * will work on NOMMU too */
108 logmode &= ~LOGMODE_STDIO;
109 /* rewrite pidfile, as our pid is different now */
110 write_pidfile(client_config.pidfile);
112 /* Do not fork again. */
113 client_config.foreground = 1;
114 client_config.background_if_no_lease = 0;
118 static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
121 int len = strlen(str);
122 if (len > 255) len = 255;
123 storage = xzalloc(len + extra + OPT_DATA);
124 storage[OPT_CODE] = code;
125 storage[OPT_LEN] = len + extra;
126 memcpy(storage + extra + OPT_DATA, str, len);
131 int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
132 int udhcpc_main(int argc, char **argv)
134 uint8_t *temp, *message;
135 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_A, *str_t;
136 USE_FEATURE_UDHCP_PORT(char *str_P;)
137 llist_t *list_O = NULL;
138 #if ENABLE_FEATURE_UDHCPC_ARPING
141 int tryagain_timeout = 20;
142 int discover_timeout = 3;
143 int discover_retries = 3;
145 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
147 /* t1, t2... what a wonderful names... */
148 unsigned t1 = t1; /* for gcc */
150 unsigned timestamp_got_lease = timestamp_got_lease;
156 struct in_addr temp_addr;
157 struct dhcpMessage packet;
181 #if ENABLE_FEATURE_UDHCPC_ARPING
187 #if ENABLE_GETOPT_LONG
188 static const char udhcpc_longopts[] ALIGN1 =
189 "clientid\0" Required_argument "c"
190 "clientid-none\0" No_argument "C"
191 "vendorclass\0" Required_argument "V"
192 "foreground\0" No_argument "f"
193 "background\0" No_argument "b"
194 "hostname\0" Required_argument "H"
195 "fqdn\0" Required_argument "F"
196 "interface\0" Required_argument "i"
197 "now\0" No_argument "n"
198 "pidfile\0" Required_argument "p"
199 "quit\0" No_argument "q"
200 "release\0" No_argument "R"
201 "request\0" Required_argument "r"
202 "script\0" Required_argument "s"
203 "timeout\0" Required_argument "T"
204 "version\0" No_argument "v"
205 "retries\0" Required_argument "t"
206 "tryagain\0" Required_argument "A"
207 "syslog\0" No_argument "S"
208 #if ENABLE_FEATURE_UDHCPC_ARPING
209 "arping\0" No_argument "a"
211 "request-option\0" Required_argument "O"
212 #if ENABLE_FEATURE_UDHCP_PORT
213 "client-port\0" Required_argument "P"
217 /* Default options. */
218 #if ENABLE_FEATURE_UDHCP_PORT
222 client_config.interface = "eth0";
223 client_config.script = DEFAULT_SCRIPT;
225 /* Parse command line */
226 opt_complementary = "c--C:C--c:O::"; // Cc: mutually exclusive; O: list
227 #if ENABLE_GETOPT_LONG
228 applet_long_options = udhcpc_longopts;
230 opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
231 USE_FEATURE_UDHCPC_ARPING("aW:")
232 USE_FEATURE_UDHCP_PORT("P:")
234 , &str_c, &str_V, &str_h, &str_h, &str_F
235 , &client_config.interface, &client_config.pidfile, &str_r
236 , &client_config.script, &str_T, &str_t, &str_A
237 USE_FEATURE_UDHCPC_ARPING(, &str_W)
238 USE_FEATURE_UDHCP_PORT(, &str_P)
243 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
246 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
248 client_config.foreground = 1;
250 client_config.background_if_no_lease = 1;
251 if (opt & (OPT_h|OPT_H))
252 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
254 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
256 S: 1 => Client requests Server to update A RR in DNS as well as PTR
257 O: 1 => Server indicates to client that DNS has been updated regardless
258 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
259 N: 1 => Client requests Server to not update DNS
261 client_config.fqdn[OPT_DATA + 0] = 0x1;
262 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
263 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
265 // if (opt & OPT_i) client_config.interface = ...
267 client_config.abort_if_no_lease = 1;
268 // if (opt & OPT_p) client_config.pidfile = ...
270 client_config.quit_after_lease = 1;
272 client_config.release_on_quit = 1;
274 requested_ip = inet_addr(str_r);
275 // if (opt & OPT_s) client_config.script = ...
277 discover_timeout = xatoi_u(str_T);
279 discover_retries = xatoi_u(str_t);
281 tryagain_timeout = xatoi_u(str_A);
283 puts("version "BB_VER);
287 openlog(applet_name, LOG_PID, LOG_LOCAL0);
288 logmode |= LOGMODE_SYSLOG;
290 #if ENABLE_FEATURE_UDHCP_PORT
292 CLIENT_PORT = xatou16(str_P);
293 SERVER_PORT = CLIENT_PORT - 1;
297 int n = index_in_strings(dhcp_option_strings, list_O->data);
299 bb_error_msg_and_die("unknown option '%s'", list_O->data);
300 n = dhcp_options[n].code;
301 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
302 list_O = list_O->link;
305 if (read_interface(client_config.interface, &client_config.ifindex,
306 NULL, client_config.arp))
309 /* Make sure fd 0,1,2 are open */
311 /* Equivalent of doing a fflush after every \n */
315 write_pidfile(client_config.pidfile);
316 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
318 /* Goes to stdout and possibly syslog */
319 bb_info_msg("%s (v"BB_VER") started", applet_name);
321 /* if not set, and not suppressed, setup the default client ID */
322 if (!client_config.clientid && !(opt & OPT_C)) {
323 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
324 client_config.clientid[OPT_DATA] = 1;
325 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
328 if (!client_config.vendorclass)
329 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
331 /* setup the signal pipe */
334 state = INIT_SELECTING;
335 udhcp_run_script(NULL, "deconfig");
336 change_listen_mode(LISTEN_RAW);
339 /* Main event loop. select() waits on signal pipe and possibly
341 * "continue" statements in code below jump to the top of the loop.
347 if (listen_mode != LISTEN_NONE && sockfd < 0) {
348 if (listen_mode == LISTEN_KERNEL)
349 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
351 sockfd = raw_socket(client_config.ifindex);
353 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
355 retval = 0; /* If we already timed out, fall through, else... */
357 DEBUG("Waiting on select...");
358 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
362 /* EINTR? signal was caught, don't panic */
363 if (errno != EINTR) {
364 /* Else: an error occured, panic! */
365 bb_perror_msg_and_die("select");
370 /* If timeout dropped to zero, time to become active:
371 * resend discover/renew/whatever
376 if (packet_num < discover_retries) {
380 /* send discover packet */
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 to try again */
397 timeout = tryagain_timeout;
400 case RENEW_REQUESTED:
402 if (packet_num < discover_retries) {
403 /* send request packet */
404 if (state == RENEW_REQUESTED)
405 send_renew(xid, server_addr, requested_ip); /* unicast */
406 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
408 timeout = ((packet_num == 2) ? 10 : 2);
412 /* timed out, go back to init state */
413 if (state == RENEW_REQUESTED)
414 udhcp_run_script(NULL, "deconfig");
415 change_listen_mode(LISTEN_RAW);
416 state = INIT_SELECTING;
421 /* Lease is starting to run out, time to enter renewing state */
422 change_listen_mode(LISTEN_KERNEL);
423 DEBUG("Entering renew state");
425 /* fall right through */
427 /* Either set a new T1, or enter REBINDING state */
428 if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
429 /* send a request packet */
430 send_renew(xid, server_addr, requested_ip); /* unicast */
432 timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease);
435 /* Timed out, enter rebinding state */
436 DEBUG("Entering rebinding state");
441 /* Lease is *really* about to run out,
442 * try to find DHCP server using broadcast */
443 if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
444 /* send a request packet */
445 send_renew(xid, 0, requested_ip); /* broadcast */
446 t2 += (lease_seconds - t2) / 2;
447 timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease);
450 /* Timed out, enter init state */
451 bb_info_msg("Lease lost, entering init state");
452 udhcp_run_script(NULL, "deconfig");
453 change_listen_mode(LISTEN_RAW);
454 state = INIT_SELECTING;
460 /* yah, I know, *you* say it would never happen */
462 continue; /* back to main loop */
465 /* select() didn't timeout, something did happen. */
466 /* Is is a packet? */
467 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
468 /* A packet is ready, read it */
470 if (listen_mode == LISTEN_KERNEL)
471 len = udhcp_recv_packet(&packet, sockfd);
473 len = get_raw_packet(&packet, sockfd);
475 if (len == -1) { /* error is severe, reopen socket */
476 DEBUG("error on read, %s, reopening socket", strerror(errno));
477 change_listen_mode(listen_mode); /* just close and reopen */
479 if (len < 0) continue;
481 if (packet.xid != xid) {
482 DEBUG("Ignoring XID %x (our xid is %x)",
483 (unsigned)packet.xid, (unsigned)xid);
487 /* Ignore packets that aren't for us */
488 if (memcmp(packet.chaddr, client_config.arp, 6)) {
489 DEBUG("Packet does not have our chaddr - ignoring");
493 message = get_option(&packet, DHCP_MESSAGE_TYPE);
494 if (message == NULL) {
495 bb_error_msg("cannot get message type from packet - ignoring");
501 /* Must be a DHCPOFFER to one of our xid's */
502 if (*message == DHCPOFFER) {
503 /* TODO: why we don't just fetch server's IP from IP header? */
504 temp = get_option(&packet, DHCP_SERVER_ID);
506 bb_error_msg("no server ID in message");
508 /* still selecting - this server looks bad */
510 /* can be misaligned, thus memcpy */
511 memcpy(&server_addr, temp, 4);
513 requested_ip = packet.yiaddr;
515 /* enter requesting state */
521 case RENEW_REQUESTED:
525 if (*message == DHCPACK) {
526 temp = get_option(&packet, DHCP_LEASE_TIME);
528 bb_error_msg("no lease time with ACK, using 1 hour lease");
529 lease_seconds = 60 * 60;
531 /* can be misaligned, thus memcpy */
532 memcpy(&lease_seconds, temp, 4);
533 lease_seconds = ntohl(lease_seconds);
535 #if ENABLE_FEATURE_UDHCPC_ARPING
537 if (!arpping(packet.yiaddr,
540 client_config.interface)
542 bb_info_msg("offered address is in use "
543 "(got ARP reply), declining");
544 send_decline(xid, server_addr, packet.yiaddr);
546 if (state != REQUESTING)
547 udhcp_run_script(NULL, "deconfig");
548 change_listen_mode(LISTEN_RAW);
549 state = INIT_SELECTING;
551 timeout = tryagain_timeout;
553 continue; /* back to main loop */
557 /* enter bound state */
558 t1 = lease_seconds / 2;
560 /* little fixed point for n * .875 */
561 t2 = (lease_seconds * 7) >> 3;
562 temp_addr.s_addr = packet.yiaddr;
563 bb_info_msg("Lease of %s obtained, lease time %u",
564 inet_ntoa(temp_addr), (unsigned)lease_seconds);
565 timestamp_got_lease = monotonic_sec();
567 requested_ip = packet.yiaddr;
568 udhcp_run_script(&packet,
569 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
572 change_listen_mode(LISTEN_NONE);
573 if (client_config.quit_after_lease) {
574 if (client_config.release_on_quit)
578 if (!client_config.foreground)
581 continue; /* back to main loop */
583 if (*message == DHCPNAK) {
584 /* return to init state */
585 bb_info_msg("Received DHCP NAK");
586 udhcp_run_script(&packet, "nak");
587 if (state != REQUESTING)
588 udhcp_run_script(NULL, "deconfig");
589 change_listen_mode(LISTEN_RAW);
590 sleep(3); /* avoid excessive network traffic */
591 state = INIT_SELECTING;
597 /* case BOUND, RELEASED: - ignore all packets */
599 continue; /* back to main loop */
602 /* select() didn't timeout, something did happen.
603 * But it wasn't a packet. It's a signal pipe then. */
605 int signo = udhcp_sp_read(&rfds);
609 /* start things over */
611 /* Kill any timeouts because the user wants this to hurry along */
618 bb_info_msg("Received SIGTERM");
619 if (client_config.release_on_quit)
624 } /* for (;;) - main loop ends */
629 /*if (client_config.pidfile) - remove_pidfile has it's own check */
630 remove_pidfile(client_config.pidfile);