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 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 /* t1, t2... what a wonderful names... */
147 unsigned t1 = t1; /* for gcc */
149 unsigned timestamp_got_lease = timestamp_got_lease;
155 struct in_addr temp_addr;
156 struct dhcpMessage packet;
180 #if ENABLE_FEATURE_UDHCPC_ARPING
185 #if ENABLE_GETOPT_LONG
186 static const char udhcpc_longopts[] ALIGN1 =
187 "clientid\0" Required_argument "c"
188 "clientid-none\0" No_argument "C"
189 "vendorclass\0" Required_argument "V"
190 "foreground\0" No_argument "f"
191 "background\0" No_argument "b"
192 "hostname\0" Required_argument "H"
193 "fqdn\0" Required_argument "F"
194 "interface\0" Required_argument "i"
195 "now\0" No_argument "n"
196 "pidfile\0" Required_argument "p"
197 "quit\0" No_argument "q"
198 "release\0" No_argument "R"
199 "request\0" Required_argument "r"
200 "script\0" Required_argument "s"
201 "timeout\0" Required_argument "T"
202 "version\0" No_argument "v"
203 "retries\0" Required_argument "t"
204 "tryagain\0" Required_argument "A"
205 "syslog\0" No_argument "S"
206 #if ENABLE_FEATURE_UDHCPC_ARPING
207 "arping\0" No_argument "a"
209 "request-option\0" Required_argument "O"
212 /* Default options. */
213 client_config.interface = "eth0";
214 client_config.script = DEFAULT_SCRIPT;
216 /* Parse command line */
217 opt_complementary = "c--C:C--c:O::"; // Cc: mutually exclusive; O: list
218 #if ENABLE_GETOPT_LONG
219 applet_long_options = udhcpc_longopts;
221 opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
222 USE_FEATURE_UDHCPC_ARPING("aW:")
224 , &str_c, &str_V, &str_h, &str_h, &str_F
225 , &client_config.interface, &client_config.pidfile, &str_r
226 , &client_config.script, &str_T, &str_t, &str_A
227 USE_FEATURE_UDHCPC_ARPING(, &str_W)
232 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
235 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
237 client_config.foreground = 1;
239 client_config.background_if_no_lease = 1;
240 if (opt & (OPT_h|OPT_H))
241 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
243 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
245 S: 1 => Client requests Server to update A RR in DNS as well as PTR
246 O: 1 => Server indicates to client that DNS has been updated regardless
247 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
248 N: 1 => Client requests Server to not update DNS
250 client_config.fqdn[OPT_DATA + 0] = 0x1;
251 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
252 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
254 // if (opt & OPT_i) client_config.interface = ...
256 client_config.abort_if_no_lease = 1;
257 // if (opt & OPT_p) client_config.pidfile = ...
259 client_config.quit_after_lease = 1;
261 client_config.release_on_quit = 1;
263 requested_ip = inet_addr(str_r);
264 // if (opt & OPT_s) client_config.script = ...
266 discover_timeout = xatoi_u(str_T);
268 discover_retries = xatoi_u(str_t);
270 tryagain_timeout = xatoi_u(str_A);
272 puts("version "BB_VER);
276 openlog(applet_name, LOG_PID, LOG_LOCAL0);
277 logmode |= LOGMODE_SYSLOG;
280 int n = index_in_strings(dhcp_option_strings, list_O->data);
282 bb_error_msg_and_die("unknown option '%s'", list_O->data);
283 n = dhcp_options[n].code;
284 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
285 list_O = list_O->link;
288 if (read_interface(client_config.interface, &client_config.ifindex,
289 NULL, client_config.arp))
292 /* Make sure fd 0,1,2 are open */
294 /* Equivalent of doing a fflush after every \n */
298 write_pidfile(client_config.pidfile);
299 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
301 /* Goes to stdout and possibly syslog */
302 bb_info_msg("%s (v"BB_VER") started", applet_name);
304 /* if not set, and not suppressed, setup the default client ID */
305 if (!client_config.clientid && !(opt & OPT_C)) {
306 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
307 client_config.clientid[OPT_DATA] = 1;
308 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
311 if (!client_config.vendorclass)
312 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
314 /* setup the signal pipe */
317 state = INIT_SELECTING;
318 udhcp_run_script(NULL, "deconfig");
319 change_listen_mode(LISTEN_RAW);
322 /* Main event loop. select() waits on signal pipe and possibly
324 * "continue" statements in code below jump to the top of the loop.
330 if (listen_mode != LISTEN_NONE && sockfd < 0) {
331 if (listen_mode == LISTEN_KERNEL)
332 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
334 sockfd = raw_socket(client_config.ifindex);
336 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
338 retval = 0; /* If we already timed out, fall through, else... */
340 DEBUG("Waiting on select...");
341 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
345 /* EINTR? signal was caught, don't panic */
346 if (errno != EINTR) {
347 /* Else: an error occured, panic! */
348 bb_perror_msg_and_die("select");
353 /* If timeout dropped to zero, time to become active:
354 * resend discover/renew/whatever
359 if (packet_num < discover_retries) {
363 /* send discover packet */
364 send_discover(xid, requested_ip); /* broadcast */
366 timeout = discover_timeout;
370 udhcp_run_script(NULL, "leasefail");
371 if (client_config.background_if_no_lease) {
372 bb_info_msg("No lease, forking to background");
374 } else if (client_config.abort_if_no_lease) {
375 bb_info_msg("No lease, failing");
379 /* wait to try again */
380 timeout = tryagain_timeout;
383 case RENEW_REQUESTED:
385 if (packet_num < discover_retries) {
386 /* send request packet */
387 if (state == RENEW_REQUESTED)
388 send_renew(xid, server_addr, requested_ip); /* unicast */
389 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
391 timeout = ((packet_num == 2) ? 10 : 2);
395 /* timed out, go back to init state */
396 if (state == RENEW_REQUESTED)
397 udhcp_run_script(NULL, "deconfig");
398 change_listen_mode(LISTEN_RAW);
399 state = INIT_SELECTING;
404 /* Lease is starting to run out, time to enter renewing state */
405 change_listen_mode(LISTEN_KERNEL);
406 DEBUG("Entering renew state");
408 /* fall right through */
410 /* Either set a new T1, or enter REBINDING state */
411 if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
412 /* send a request packet */
413 send_renew(xid, server_addr, requested_ip); /* unicast */
415 timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease);
418 /* Timed out, enter rebinding state */
419 DEBUG("Entering rebinding state");
424 /* Lease is *really* about to run out,
425 * try to find DHCP server using broadcast */
426 if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
427 /* send a request packet */
428 send_renew(xid, 0, requested_ip); /* broadcast */
429 t2 += (lease_seconds - t2) / 2;
430 timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease);
433 /* Timed out, enter init state */
434 bb_info_msg("Lease lost, entering init state");
435 udhcp_run_script(NULL, "deconfig");
436 change_listen_mode(LISTEN_RAW);
437 state = INIT_SELECTING;
443 /* yah, I know, *you* say it would never happen */
445 continue; /* back to main loop */
448 /* select() didn't timeout, something did happen. */
449 /* Is is a packet? */
450 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
451 /* A packet is ready, read it */
453 if (listen_mode == LISTEN_KERNEL)
454 len = udhcp_get_packet(&packet, sockfd);
455 else len = get_raw_packet(&packet, sockfd);
457 if (len == -1 && errno != EINTR) {
458 DEBUG("error on read, %s, reopening socket", strerror(errno));
459 change_listen_mode(listen_mode); /* just close and reopen */
461 if (len < 0) continue;
463 if (packet.xid != xid) {
464 DEBUG("Ignoring XID %x (our xid is %x)",
465 (unsigned)packet.xid, (unsigned)xid);
469 /* Ignore packets that aren't for us */
470 if (memcmp(packet.chaddr, client_config.arp, 6)) {
471 DEBUG("Packet does not have our chaddr - ignoring");
475 message = get_option(&packet, DHCP_MESSAGE_TYPE);
476 if (message == NULL) {
477 bb_error_msg("cannot get message type from packet - ignoring");
483 /* Must be a DHCPOFFER to one of our xid's */
484 if (*message == DHCPOFFER) {
485 /* TODO: why we don't just fetch server's IP from IP header? */
486 temp = get_option(&packet, DHCP_SERVER_ID);
488 bb_error_msg("no server ID in message");
490 /* still selecting - this server looks bad */
492 /* can be misaligned, thus memcpy */
493 memcpy(&server_addr, temp, 4);
495 requested_ip = packet.yiaddr;
497 /* enter requesting state */
503 case RENEW_REQUESTED:
507 if (*message == DHCPACK) {
508 temp = get_option(&packet, DHCP_LEASE_TIME);
510 bb_error_msg("no lease time with ACK, using 1 hour lease");
511 lease_seconds = 60 * 60;
513 /* can be misaligned, thus memcpy */
514 memcpy(&lease_seconds, temp, 4);
515 lease_seconds = ntohl(lease_seconds);
517 #if ENABLE_FEATURE_UDHCPC_ARPING
519 if (!arpping(packet.yiaddr,
522 client_config.interface)
524 bb_info_msg("offered address is in use "
525 "(got ARP reply), declining");
526 send_decline(xid, server_addr);
528 if (state != REQUESTING)
529 udhcp_run_script(NULL, "deconfig");
530 change_listen_mode(LISTEN_RAW);
531 state = INIT_SELECTING;
533 timeout = tryagain_timeout;
535 continue; /* back to main loop */
539 /* enter bound state */
540 t1 = lease_seconds / 2;
542 /* little fixed point for n * .875 */
543 t2 = (lease_seconds * 7) >> 3;
544 temp_addr.s_addr = packet.yiaddr;
545 bb_info_msg("Lease of %s obtained, lease time %u",
546 inet_ntoa(temp_addr), (unsigned)lease_seconds);
547 timestamp_got_lease = monotonic_sec();
549 requested_ip = packet.yiaddr;
550 udhcp_run_script(&packet,
551 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
554 change_listen_mode(LISTEN_NONE);
555 if (client_config.quit_after_lease) {
556 if (client_config.release_on_quit)
560 if (!client_config.foreground)
563 continue; /* back to main loop */
565 if (*message == DHCPNAK) {
566 /* return to init state */
567 bb_info_msg("Received DHCP NAK");
568 udhcp_run_script(&packet, "nak");
569 if (state != REQUESTING)
570 udhcp_run_script(NULL, "deconfig");
571 change_listen_mode(LISTEN_RAW);
572 sleep(3); /* avoid excessive network traffic */
573 state = INIT_SELECTING;
579 /* case BOUND, RELEASED: - ignore all packets */
581 continue; /* back to main loop */
584 /* select() didn't timeout, something did happen.
585 * But it wasn't a packet. It's a signal pipe then. */
587 int signo = udhcp_sp_read(&rfds);
591 /* start things over */
593 /* Kill any timeouts because the user wants this to hurry along */
600 bb_info_msg("Received SIGTERM");
601 if (client_config.release_on_quit)
606 } /* for (;;) - main loop ends */
611 /*if (client_config.pidfile) - remove_pidfile has it's own check */
612 remove_pidfile(client_config.pidfile);