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 /* Something is definitely wrong here. IPv4 addresses
23 * in variables of type long?? BTW, we use inet_ntoa()
24 * in the code. Manpage says that struct in_addr has a member of type long (!)
25 * which holds IPv4 address, and the struct is passed by value (!!)
27 static int timeout; /* = 0. Must be signed */
28 static uint32_t requested_ip; /* = 0 */
29 static uint32_t server_addr;
30 static int packet_num; /* = 0 */
31 static int sockfd = -1;
34 #define LISTEN_KERNEL 1
36 static smallint listen_mode;
38 static smallint state;
40 /* struct client_config_t client_config is in bb_common_bufsiz1 */
43 /* just a little helper */
44 static void change_listen_mode(int new_mode)
46 DEBUG("entering %s listen mode",
47 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
52 listen_mode = new_mode;
57 static void perform_renew(void)
59 bb_info_msg("Performing a DHCP renew");
62 change_listen_mode(LISTEN_KERNEL);
65 state = RENEW_REQUESTED;
67 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
68 udhcp_run_script(NULL, "deconfig");
71 change_listen_mode(LISTEN_RAW);
72 state = INIT_SELECTING;
78 /* start things over */
81 /* Kill any timeouts because the user wants this to hurry along */
86 /* perform a release */
87 static void perform_release(void)
89 char buffer[sizeof("255.255.255.255")];
90 struct in_addr temp_addr;
92 /* send release packet */
93 if (state == BOUND || state == RENEWING || state == REBINDING) {
94 temp_addr.s_addr = server_addr;
95 strcpy(buffer, inet_ntoa(temp_addr));
96 temp_addr.s_addr = requested_ip;
97 bb_info_msg("Unicasting a release of %s to %s",
98 inet_ntoa(temp_addr), buffer);
99 send_release(server_addr, requested_ip); /* unicast */
100 udhcp_run_script(NULL, "deconfig");
102 bb_info_msg("Entering released state");
104 change_listen_mode(LISTEN_NONE);
110 static void client_background(void)
113 bb_error_msg("cannot background in uclinux (yet)");
114 /* ... mainly because udhcpc calls client_background()
115 * in _the _middle _of _udhcpc _run_, not at the start!
116 * If that will be properly disabled for NOMMU, client_background()
117 * will work on NOMMU too */
120 logmode &= ~LOGMODE_STDIO;
121 /* rewrite pidfile, as our pid is different now */
122 write_pidfile(client_config.pidfile);
124 /* Do not fork again. */
125 client_config.foreground = 1;
126 client_config.background_if_no_lease = 0;
130 static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
133 int len = strlen(str);
134 if (len > 255) len = 255;
135 storage = xzalloc(len + extra + OPT_DATA);
136 storage[OPT_CODE] = code;
137 storage[OPT_LEN] = len + extra;
138 memcpy(storage + extra + OPT_DATA, str, len);
143 int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
144 int udhcpc_main(int argc, char **argv)
146 uint8_t *temp, *message;
147 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_A, *str_t;
148 #if ENABLE_FEATURE_UDHCPC_ARPING
151 int tryagain_timeout = 20;
152 int discover_timeout = 3;
153 int discover_retries = 3;
155 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
156 unsigned t1 = 0, t2 = 0; /* what a wonderful names */
157 unsigned timestamp_got_lease = 0; /* for gcc */
163 struct in_addr temp_addr;
164 struct dhcpMessage packet;
188 #if ENABLE_FEATURE_UDHCPC_ARPING
193 #if ENABLE_GETOPT_LONG
194 static const char udhcpc_longopts[] ALIGN1 =
195 "clientid\0" Required_argument "c"
196 "clientid-none\0" No_argument "C"
197 "vendorclass\0" Required_argument "V"
198 "foreground\0" No_argument "f"
199 "background\0" No_argument "b"
200 "hostname\0" Required_argument "H"
201 "fqdn\0" Required_argument "F"
202 "interface\0" Required_argument "i"
203 "now\0" No_argument "n"
204 "pidfile\0" Required_argument "p"
205 "quit\0" No_argument "q"
206 "release\0" No_argument "R"
207 "request\0" Required_argument "r"
208 "script\0" Required_argument "s"
209 "timeout\0" Required_argument "T"
210 "version\0" No_argument "v"
211 "retries\0" Required_argument "t"
212 "tryagain\0" Required_argument "A"
213 "syslog\0" No_argument "S"
214 #if ENABLE_FEATURE_UDHCPC_ARPING
215 "arping\0" No_argument "a"
219 /* Default options. */
220 client_config.interface = "eth0";
221 client_config.script = DEFAULT_SCRIPT;
223 /* Parse command line */
224 opt_complementary = "c--C:C--c"; // mutually exclusive
225 #if ENABLE_GETOPT_LONG
226 applet_long_options = udhcpc_longopts;
228 opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
229 USE_FEATURE_UDHCPC_ARPING("aW:")
230 , &str_c, &str_V, &str_h, &str_h, &str_F,
231 &client_config.interface, &client_config.pidfile, &str_r,
232 &client_config.script, &str_T, &str_t, &str_A
233 USE_FEATURE_UDHCPC_ARPING(, &str_W)
237 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
240 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
242 client_config.foreground = 1;
244 client_config.background_if_no_lease = 1;
245 if (opt & (OPT_h|OPT_H))
246 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
248 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
250 S: 1 => Client requests Server to update A RR in DNS as well as PTR
251 O: 1 => Server indicates to client that DNS has been updated regardless
252 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
253 N: 1 => Client requests Server to not update DNS
255 client_config.fqdn[OPT_DATA + 0] = 0x1;
256 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
257 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
259 // if (opt & OPT_i) client_config.interface = ...
261 client_config.abort_if_no_lease = 1;
262 // if (opt & OPT_p) client_config.pidfile = ...
264 client_config.quit_after_lease = 1;
266 client_config.release_on_quit = 1;
268 requested_ip = inet_addr(str_r);
269 // if (opt & OPT_s) client_config.script = ...
271 discover_timeout = xatoi_u(str_T);
273 discover_retries = xatoi_u(str_t);
275 tryagain_timeout = xatoi_u(str_A);
277 puts("version "BB_VER);
282 openlog(applet_name, LOG_PID, LOG_LOCAL0);
283 logmode |= LOGMODE_SYSLOG;
286 if (read_interface(client_config.interface, &client_config.ifindex,
287 NULL, client_config.arp))
290 /* Make sure fd 0,1,2 are open */
292 /* Equivalent of doing a fflush after every \n */
296 write_pidfile(client_config.pidfile);
297 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
299 /* Goes to stdout and possibly syslog */
300 bb_info_msg("%s (v"BB_VER") started", applet_name);
302 /* if not set, and not suppressed, setup the default client ID */
303 if (!client_config.clientid && !(opt & OPT_C)) {
304 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
305 client_config.clientid[OPT_DATA] = 1;
306 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
309 if (!client_config.vendorclass)
310 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
312 /* setup the signal pipe */
315 state = INIT_SELECTING;
316 udhcp_run_script(NULL, "deconfig");
317 change_listen_mode(LISTEN_RAW);
319 /* Main event loop. select() waits on signal pipe and possibly
321 * "continue" statements in code below jump to the top of the loop.
327 if (listen_mode != LISTEN_NONE && sockfd < 0) {
328 if (listen_mode == LISTEN_KERNEL)
329 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
331 sockfd = raw_socket(client_config.ifindex);
333 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
335 retval = 0; /* If we already timed out, fall through, else... */
337 DEBUG("Waiting on select...");
338 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
342 /* EINTR? signal was caught, don't panic */
343 if (errno != EINTR) {
344 /* Else: an error occured, panic! */
345 bb_perror_msg_and_die("select");
350 /* If timeout dropped to zero, time to become active:
351 * resend discover/renew/whatever
356 if (packet_num < discover_retries) {
360 /* send discover packet */
361 send_discover(xid, requested_ip); /* broadcast */
363 timeout = discover_timeout;
367 udhcp_run_script(NULL, "leasefail");
368 if (client_config.background_if_no_lease) {
369 bb_info_msg("No lease, forking to background");
371 } else if (client_config.abort_if_no_lease) {
372 bb_info_msg("No lease, failing");
376 /* wait to try again */
377 timeout = tryagain_timeout;
380 case RENEW_REQUESTED:
382 if (packet_num < discover_retries) {
383 /* send request packet */
384 if (state == RENEW_REQUESTED)
385 send_renew(xid, server_addr, requested_ip); /* unicast */
386 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
388 timeout = ((packet_num == 2) ? 10 : 2);
392 /* timed out, go back to init state */
393 if (state == RENEW_REQUESTED)
394 udhcp_run_script(NULL, "deconfig");
395 change_listen_mode(LISTEN_RAW);
396 state = INIT_SELECTING;
401 /* Lease is starting to run out, time to enter renewing state */
402 change_listen_mode(LISTEN_KERNEL);
403 DEBUG("Entering renew state");
405 /* fall right through */
407 /* Either set a new T1, or enter REBINDING state */
408 if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
409 /* send a request packet */
410 send_renew(xid, server_addr, requested_ip); /* unicast */
412 timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease);
415 /* Timed out, enter rebinding state */
416 DEBUG("Entering rebinding state");
421 /* Lease is *really* about to run out,
422 * try to find DHCP server using broadcast */
423 if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
424 /* send a request packet */
425 send_renew(xid, 0, requested_ip); /* broadcast */
426 t2 += (lease_seconds - t2) / 2;
427 timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease);
430 /* Timed out, enter init state */
431 bb_info_msg("Lease lost, entering init state");
432 udhcp_run_script(NULL, "deconfig");
433 change_listen_mode(LISTEN_RAW);
434 state = INIT_SELECTING;
440 /* yah, I know, *you* say it would never happen */
442 continue; /* back to main loop */
445 /* select() didn't timeout, something did happen. */
446 /* Is is a packet? */
447 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
448 /* A packet is ready, read it */
450 if (listen_mode == LISTEN_KERNEL)
451 len = udhcp_get_packet(&packet, sockfd);
452 else len = get_raw_packet(&packet, sockfd);
454 if (len == -1 && errno != EINTR) {
455 DEBUG("error on read, %s, reopening socket", strerror(errno));
456 change_listen_mode(listen_mode); /* just close and reopen */
458 if (len < 0) continue;
460 if (packet.xid != xid) {
461 DEBUG("Ignoring XID %x (our xid is %x)",
462 (unsigned)packet.xid, (unsigned)xid);
466 /* Ignore packets that aren't for us */
467 if (memcmp(packet.chaddr, client_config.arp, 6)) {
468 DEBUG("Packet does not have our chaddr - ignoring");
472 message = get_option(&packet, DHCP_MESSAGE_TYPE);
473 if (message == NULL) {
474 bb_error_msg("cannot get message type from packet - ignoring");
480 /* Must be a DHCPOFFER to one of our xid's */
481 if (*message == DHCPOFFER) {
482 /* TODO: why we don't just fetch server's IP from IP header? */
483 temp = get_option(&packet, DHCP_SERVER_ID);
485 bb_error_msg("no server ID in message");
487 /* still selecting - this server looks bad */
489 /* can be misaligned, thus memcpy */
490 memcpy(&server_addr, temp, 4);
492 requested_ip = packet.yiaddr;
494 /* enter requesting state */
500 case RENEW_REQUESTED:
504 if (*message == DHCPACK) {
505 temp = get_option(&packet, DHCP_LEASE_TIME);
507 bb_error_msg("no lease time with ACK, using 1 hour lease");
508 lease_seconds = 60 * 60;
510 /* can be misaligned, thus memcpy */
511 memcpy(&lease_seconds, temp, 4);
512 lease_seconds = ntohl(lease_seconds);
514 #if ENABLE_FEATURE_UDHCPC_ARPING
516 if (!arpping(packet.yiaddr,
519 client_config.interface)
521 bb_info_msg("offered address is in use "
522 "(got ARP reply), declining");
523 send_decline(xid, server_addr);
525 if (state != REQUESTING)
526 udhcp_run_script(NULL, "deconfig");
527 change_listen_mode(LISTEN_RAW);
528 state = INIT_SELECTING;
530 timeout = tryagain_timeout;
532 continue; /* back to main loop */
536 /* enter bound state */
537 t1 = lease_seconds / 2;
539 /* little fixed point for n * .875 */
540 t2 = (lease_seconds * 7) >> 3;
541 temp_addr.s_addr = packet.yiaddr;
542 bb_info_msg("Lease of %s obtained, lease time %u",
543 inet_ntoa(temp_addr), (unsigned)lease_seconds);
544 timestamp_got_lease = monotonic_sec();
546 requested_ip = packet.yiaddr;
547 udhcp_run_script(&packet,
548 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
551 change_listen_mode(LISTEN_NONE);
552 if (client_config.quit_after_lease) {
553 if (client_config.release_on_quit)
557 if (!client_config.foreground)
560 continue; /* back to main loop */
562 if (*message == DHCPNAK) {
563 /* return to init state */
564 bb_info_msg("Received DHCP NAK");
565 udhcp_run_script(&packet, "nak");
566 if (state != REQUESTING)
567 udhcp_run_script(NULL, "deconfig");
568 change_listen_mode(LISTEN_RAW);
569 sleep(3); /* avoid excessive network traffic */
570 state = INIT_SELECTING;
576 /* case BOUND, RELEASED: - ignore all packets */
578 continue; /* back to main loop */
581 /* select() didn't timeout, something did happen.
582 * But it wasn't a packet. It's a signal pipe then. */
584 int signo = udhcp_sp_read(&rfds);
593 bb_info_msg("Received SIGTERM");
594 if (client_config.release_on_quit)
599 } /* for (;;) - main loop ends */
604 /*if (client_config.pidfile) - remove_pidfile has it's own check */
605 remove_pidfile(client_config.pidfile);