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 #if ENABLE_FEATURE_UDHCPC_ARPING
139 int tryagain_timeout = 20;
140 int discover_timeout = 3;
141 int discover_retries = 3;
143 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
145 /* t1, t2... what a wonderful names... */
146 unsigned t1 = t1; /* for gcc */
148 unsigned timestamp_got_lease = timestamp_got_lease;
154 struct in_addr temp_addr;
155 struct dhcpMessage packet;
179 #if ENABLE_FEATURE_UDHCPC_ARPING
184 #if ENABLE_GETOPT_LONG
185 static const char udhcpc_longopts[] ALIGN1 =
186 "clientid\0" Required_argument "c"
187 "clientid-none\0" No_argument "C"
188 "vendorclass\0" Required_argument "V"
189 "foreground\0" No_argument "f"
190 "background\0" No_argument "b"
191 "hostname\0" Required_argument "H"
192 "fqdn\0" Required_argument "F"
193 "interface\0" Required_argument "i"
194 "now\0" No_argument "n"
195 "pidfile\0" Required_argument "p"
196 "quit\0" No_argument "q"
197 "release\0" No_argument "R"
198 "request\0" Required_argument "r"
199 "script\0" Required_argument "s"
200 "timeout\0" Required_argument "T"
201 "version\0" No_argument "v"
202 "retries\0" Required_argument "t"
203 "tryagain\0" Required_argument "A"
204 "syslog\0" No_argument "S"
205 #if ENABLE_FEATURE_UDHCPC_ARPING
206 "arping\0" No_argument "a"
210 /* Default options. */
211 client_config.interface = "eth0";
212 client_config.script = DEFAULT_SCRIPT;
214 /* Parse command line */
215 opt_complementary = "c--C:C--c"; // mutually exclusive
216 #if ENABLE_GETOPT_LONG
217 applet_long_options = udhcpc_longopts;
219 opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
220 USE_FEATURE_UDHCPC_ARPING("aW:")
221 , &str_c, &str_V, &str_h, &str_h, &str_F,
222 &client_config.interface, &client_config.pidfile, &str_r,
223 &client_config.script, &str_T, &str_t, &str_A
224 USE_FEATURE_UDHCPC_ARPING(, &str_W)
228 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
231 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
233 client_config.foreground = 1;
235 client_config.background_if_no_lease = 1;
236 if (opt & (OPT_h|OPT_H))
237 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
239 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
241 S: 1 => Client requests Server to update A RR in DNS as well as PTR
242 O: 1 => Server indicates to client that DNS has been updated regardless
243 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
244 N: 1 => Client requests Server to not update DNS
246 client_config.fqdn[OPT_DATA + 0] = 0x1;
247 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
248 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
250 // if (opt & OPT_i) client_config.interface = ...
252 client_config.abort_if_no_lease = 1;
253 // if (opt & OPT_p) client_config.pidfile = ...
255 client_config.quit_after_lease = 1;
257 client_config.release_on_quit = 1;
259 requested_ip = inet_addr(str_r);
260 // if (opt & OPT_s) client_config.script = ...
262 discover_timeout = xatoi_u(str_T);
264 discover_retries = xatoi_u(str_t);
266 tryagain_timeout = xatoi_u(str_A);
268 puts("version "BB_VER);
273 openlog(applet_name, LOG_PID, LOG_LOCAL0);
274 logmode |= LOGMODE_SYSLOG;
277 if (read_interface(client_config.interface, &client_config.ifindex,
278 NULL, client_config.arp))
281 /* Make sure fd 0,1,2 are open */
283 /* Equivalent of doing a fflush after every \n */
287 write_pidfile(client_config.pidfile);
288 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
290 /* Goes to stdout and possibly syslog */
291 bb_info_msg("%s (v"BB_VER") started", applet_name);
293 /* if not set, and not suppressed, setup the default client ID */
294 if (!client_config.clientid && !(opt & OPT_C)) {
295 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
296 client_config.clientid[OPT_DATA] = 1;
297 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
300 if (!client_config.vendorclass)
301 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
303 /* setup the signal pipe */
306 state = INIT_SELECTING;
307 udhcp_run_script(NULL, "deconfig");
308 change_listen_mode(LISTEN_RAW);
311 /* Main event loop. select() waits on signal pipe and possibly
313 * "continue" statements in code below jump to the top of the loop.
319 if (listen_mode != LISTEN_NONE && sockfd < 0) {
320 if (listen_mode == LISTEN_KERNEL)
321 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
323 sockfd = raw_socket(client_config.ifindex);
325 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
327 retval = 0; /* If we already timed out, fall through, else... */
329 DEBUG("Waiting on select...");
330 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
334 /* EINTR? signal was caught, don't panic */
335 if (errno != EINTR) {
336 /* Else: an error occured, panic! */
337 bb_perror_msg_and_die("select");
342 /* If timeout dropped to zero, time to become active:
343 * resend discover/renew/whatever
348 if (packet_num < discover_retries) {
352 /* send discover packet */
353 send_discover(xid, requested_ip); /* broadcast */
355 timeout = discover_timeout;
359 udhcp_run_script(NULL, "leasefail");
360 if (client_config.background_if_no_lease) {
361 bb_info_msg("No lease, forking to background");
363 } else if (client_config.abort_if_no_lease) {
364 bb_info_msg("No lease, failing");
368 /* wait to try again */
369 timeout = tryagain_timeout;
372 case RENEW_REQUESTED:
374 if (packet_num < discover_retries) {
375 /* send request packet */
376 if (state == RENEW_REQUESTED)
377 send_renew(xid, server_addr, requested_ip); /* unicast */
378 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
380 timeout = ((packet_num == 2) ? 10 : 2);
384 /* timed out, go back to init state */
385 if (state == RENEW_REQUESTED)
386 udhcp_run_script(NULL, "deconfig");
387 change_listen_mode(LISTEN_RAW);
388 state = INIT_SELECTING;
393 /* Lease is starting to run out, time to enter renewing state */
394 change_listen_mode(LISTEN_KERNEL);
395 DEBUG("Entering renew state");
397 /* fall right through */
399 /* Either set a new T1, or enter REBINDING state */
400 if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
401 /* send a request packet */
402 send_renew(xid, server_addr, requested_ip); /* unicast */
404 timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease);
407 /* Timed out, enter rebinding state */
408 DEBUG("Entering rebinding state");
413 /* Lease is *really* about to run out,
414 * try to find DHCP server using broadcast */
415 if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
416 /* send a request packet */
417 send_renew(xid, 0, requested_ip); /* broadcast */
418 t2 += (lease_seconds - t2) / 2;
419 timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease);
422 /* Timed out, enter init state */
423 bb_info_msg("Lease lost, entering init state");
424 udhcp_run_script(NULL, "deconfig");
425 change_listen_mode(LISTEN_RAW);
426 state = INIT_SELECTING;
432 /* yah, I know, *you* say it would never happen */
434 continue; /* back to main loop */
437 /* select() didn't timeout, something did happen. */
438 /* Is is a packet? */
439 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
440 /* A packet is ready, read it */
442 if (listen_mode == LISTEN_KERNEL)
443 len = udhcp_get_packet(&packet, sockfd);
444 else len = get_raw_packet(&packet, sockfd);
446 if (len == -1 && errno != EINTR) {
447 DEBUG("error on read, %s, reopening socket", strerror(errno));
448 change_listen_mode(listen_mode); /* just close and reopen */
450 if (len < 0) continue;
452 if (packet.xid != xid) {
453 DEBUG("Ignoring XID %x (our xid is %x)",
454 (unsigned)packet.xid, (unsigned)xid);
458 /* Ignore packets that aren't for us */
459 if (memcmp(packet.chaddr, client_config.arp, 6)) {
460 DEBUG("Packet does not have our chaddr - ignoring");
464 message = get_option(&packet, DHCP_MESSAGE_TYPE);
465 if (message == NULL) {
466 bb_error_msg("cannot get message type from packet - ignoring");
472 /* Must be a DHCPOFFER to one of our xid's */
473 if (*message == DHCPOFFER) {
474 /* TODO: why we don't just fetch server's IP from IP header? */
475 temp = get_option(&packet, DHCP_SERVER_ID);
477 bb_error_msg("no server ID in message");
479 /* still selecting - this server looks bad */
481 /* can be misaligned, thus memcpy */
482 memcpy(&server_addr, temp, 4);
484 requested_ip = packet.yiaddr;
486 /* enter requesting state */
492 case RENEW_REQUESTED:
496 if (*message == DHCPACK) {
497 temp = get_option(&packet, DHCP_LEASE_TIME);
499 bb_error_msg("no lease time with ACK, using 1 hour lease");
500 lease_seconds = 60 * 60;
502 /* can be misaligned, thus memcpy */
503 memcpy(&lease_seconds, temp, 4);
504 lease_seconds = ntohl(lease_seconds);
506 #if ENABLE_FEATURE_UDHCPC_ARPING
508 if (!arpping(packet.yiaddr,
511 client_config.interface)
513 bb_info_msg("offered address is in use "
514 "(got ARP reply), declining");
515 send_decline(xid, server_addr);
517 if (state != REQUESTING)
518 udhcp_run_script(NULL, "deconfig");
519 change_listen_mode(LISTEN_RAW);
520 state = INIT_SELECTING;
522 timeout = tryagain_timeout;
524 continue; /* back to main loop */
528 /* enter bound state */
529 t1 = lease_seconds / 2;
531 /* little fixed point for n * .875 */
532 t2 = (lease_seconds * 7) >> 3;
533 temp_addr.s_addr = packet.yiaddr;
534 bb_info_msg("Lease of %s obtained, lease time %u",
535 inet_ntoa(temp_addr), (unsigned)lease_seconds);
536 timestamp_got_lease = monotonic_sec();
538 requested_ip = packet.yiaddr;
539 udhcp_run_script(&packet,
540 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
543 change_listen_mode(LISTEN_NONE);
544 if (client_config.quit_after_lease) {
545 if (client_config.release_on_quit)
549 if (!client_config.foreground)
552 continue; /* back to main loop */
554 if (*message == DHCPNAK) {
555 /* return to init state */
556 bb_info_msg("Received DHCP NAK");
557 udhcp_run_script(&packet, "nak");
558 if (state != REQUESTING)
559 udhcp_run_script(NULL, "deconfig");
560 change_listen_mode(LISTEN_RAW);
561 sleep(3); /* avoid excessive network traffic */
562 state = INIT_SELECTING;
568 /* case BOUND, RELEASED: - ignore all packets */
570 continue; /* back to main loop */
573 /* select() didn't timeout, something did happen.
574 * But it wasn't a packet. It's a signal pipe then. */
576 int signo = udhcp_sp_read(&rfds);
580 /* start things over */
582 /* Kill any timeouts because the user wants this to hurry along */
589 bb_info_msg("Received SIGTERM");
590 if (client_config.release_on_quit)
595 } /* for (;;) - main loop ends */
600 /*if (client_config.pidfile) - remove_pidfile has it's own check */
601 remove_pidfile(client_config.pidfile);