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.
20 /* Something is definitely wrong here. IPv4 addresses
21 * in variables of type long?? BTW, we use inet_ntoa()
22 * in the code. Manpage says that struct in_addr has a member of type long (!)
23 * which holds IPv4 address, and the struct is passed by value (!!)
25 static unsigned timeout;
26 static uint32_t requested_ip; /* = 0 */
27 static uint32_t server_addr;
28 static int packet_num; /* = 0 */
29 static int sockfd = -1;
32 #define LISTEN_KERNEL 1
34 static smallint listen_mode;
36 static smallint state;
38 struct client_config_t client_config;
41 /* just a little helper */
42 static void change_mode(int new_mode)
44 DEBUG("entering %s listen mode",
45 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
50 listen_mode = new_mode;
55 static void perform_renew(void)
57 bb_info_msg("Performing a DHCP renew");
60 change_mode(LISTEN_KERNEL);
63 state = RENEW_REQUESTED;
65 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
66 udhcp_run_script(NULL, "deconfig");
69 change_mode(LISTEN_RAW);
70 state = INIT_SELECTING;
76 /* start things over */
79 /* Kill any timeouts because the user wants this to hurry along */
84 /* perform a release */
85 static void perform_release(void)
87 char buffer[sizeof("255.255.255.255")];
88 struct in_addr temp_addr;
90 /* send release packet */
91 if (state == BOUND || state == RENEWING || state == REBINDING) {
92 temp_addr.s_addr = server_addr;
93 strcpy(buffer, inet_ntoa(temp_addr));
94 temp_addr.s_addr = requested_ip;
95 bb_info_msg("Unicasting a release of %s to %s",
96 inet_ntoa(temp_addr), buffer);
97 send_release(server_addr, requested_ip); /* unicast */
98 udhcp_run_script(NULL, "deconfig");
100 bb_info_msg("Entering released state");
102 change_mode(LISTEN_NONE);
108 static void client_background(void)
111 bb_error_msg("cannot background in uclinux (yet)");
112 /* ... mainly because udhcpc calls client_background()
113 * in _the _middle _of _udhcpc _run_, not at the start!
114 * If that will be properly disabled for NOMMU, client_background()
115 * will work on NOMMU too */
118 logmode &= ~LOGMODE_STDIO;
119 /* rewrite pidfile, as our pid is different now */
120 if (client_config.pidfile)
121 write_pidfile(client_config.pidfile);
123 /* Do not fork again. */
124 client_config.foreground = 1;
125 client_config.background_if_no_lease = 0;
129 static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
132 int len = strlen(str);
133 if (len > 255) len = 255;
134 storage = xzalloc(len + extra + OPT_DATA);
135 storage[OPT_CODE] = code;
136 storage[OPT_LEN] = len + extra;
137 memcpy(storage + extra + OPT_DATA, str, len);
142 int udhcpc_main(int argc, char **argv);
143 int udhcpc_main(int argc, char **argv)
145 uint8_t *temp, *message;
146 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
148 uint32_t lease = 0; /* can be given as 32-bit quantity */
149 unsigned t1 = 0, t2 = 0;
160 struct dhcpMessage packet;
161 struct in_addr temp_addr;
183 #if ENABLE_GETOPT_LONG
184 static const struct option arg_options[] = {
185 { "clientid", required_argument, 0, 'c' },
186 { "clientid-none", no_argument, 0, 'C' },
187 { "vendorclass", required_argument, 0, 'V' },
188 { "foreground", no_argument, 0, 'f' },
189 { "background", no_argument, 0, 'b' },
190 { "hostname", required_argument, 0, 'H' },
191 { "hostname", required_argument, 0, 'h' },
192 { "fqdn", required_argument, 0, 'F' },
193 { "interface", required_argument, 0, 'i' },
194 { "now", no_argument, 0, 'n' },
195 { "pidfile", required_argument, 0, 'p' },
196 { "quit", no_argument, 0, 'q' },
197 { "release", no_argument, 0, 'R' },
198 { "request", required_argument, 0, 'r' },
199 { "script", required_argument, 0, 's' },
200 { "timeout", required_argument, 0, 'T' },
201 { "version", no_argument, 0, 'v' },
202 { "retries", required_argument, 0, 't' },
206 /* Default options. */
207 client_config.interface = "eth0";
208 client_config.script = DEFAULT_SCRIPT;
209 client_config.retries = 3;
210 client_config.timeout = 3;
212 /* Parse command line */
213 opt_complementary = "c--C:C--c" // mutually exclusive
214 ":hH:Hh"; // -h and -H are the same
215 #if ENABLE_GETOPT_LONG
216 applet_long_options = arg_options;
218 opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
219 &str_c, &str_V, &str_h, &str_h, &str_F,
220 &client_config.interface, &client_config.pidfile, &str_r,
221 &client_config.script, &str_T, &str_t
225 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
229 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
231 client_config.foreground = 1;
233 client_config.background_if_no_lease = 1;
235 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
237 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
239 S: 1 => Client requests Server to update A RR in DNS as well as PTR
240 O: 1 => Server indicates to client that DNS has been updated regardless
241 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
242 N: 1 => Client requests Server to not update DNS
244 client_config.fqdn[OPT_DATA + 0] = 0x1;
245 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
246 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
248 // if (opt & OPT_i) client_config.interface = ...
250 client_config.abort_if_no_lease = 1;
251 // if (opt & OPT_p) client_config.pidfile = ...
253 client_config.quit_after_lease = 1;
255 client_config.release_on_quit = 1;
257 requested_ip = inet_addr(str_r);
258 // if (opt & OPT_s) client_config.script = ...
260 client_config.timeout = xatoi_u(str_T);
262 client_config.retries = xatoi_u(str_t);
264 printf("version %s\n\n", BB_VER);
268 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
269 openlog(applet_name, LOG_PID, LOG_LOCAL0);
270 logmode |= LOGMODE_SYSLOG;
273 if (read_interface(client_config.interface, &client_config.ifindex,
274 NULL, client_config.arp) < 0)
277 /* Sanitize fd's and write pidfile */
278 udhcp_make_pidfile(client_config.pidfile);
280 /* if not set, and not suppressed, setup the default client ID */
281 if (!client_config.clientid && !no_clientid) {
282 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
283 client_config.clientid[OPT_DATA] = 1;
284 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
287 if (!client_config.vendorclass)
288 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
290 /* setup the signal pipe */
293 state = INIT_SELECTING;
294 udhcp_run_script(NULL, "deconfig");
295 change_mode(LISTEN_RAW);
298 tv.tv_sec = timeout - monotonic_sec();
301 if (listen_mode != LISTEN_NONE && sockfd < 0) {
302 if (listen_mode == LISTEN_KERNEL)
303 sockfd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
305 sockfd = raw_socket(client_config.ifindex);
307 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
310 DEBUG("Waiting on select...");
311 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
312 } else retval = 0; /* If we already timed out, fall through */
314 now = monotonic_sec();
316 /* timeout dropped to zero */
319 if (packet_num < client_config.retries) {
323 /* send discover packet */
324 send_discover(xid, requested_ip); /* broadcast */
326 timeout = now + client_config.timeout;
329 udhcp_run_script(NULL, "leasefail");
330 if (client_config.background_if_no_lease) {
331 bb_info_msg("No lease, forking to background");
333 } else if (client_config.abort_if_no_lease) {
334 bb_info_msg("No lease, failing");
338 /* wait to try again */
343 case RENEW_REQUESTED:
345 if (packet_num < client_config.retries) {
346 /* send request packet */
347 if (state == RENEW_REQUESTED)
348 send_renew(xid, server_addr, requested_ip); /* unicast */
349 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
351 timeout = now + ((packet_num == 2) ? 10 : 2);
354 /* timed out, go back to init state */
355 if (state == RENEW_REQUESTED)
356 udhcp_run_script(NULL, "deconfig");
357 state = INIT_SELECTING;
360 change_mode(LISTEN_RAW);
364 /* Lease is starting to run out, time to enter renewing state */
366 change_mode(LISTEN_KERNEL);
367 DEBUG("Entering renew state");
368 /* fall right through */
370 /* Either set a new T1, or enter REBINDING state */
371 if ((t2 - t1) <= (lease / 14400 + 1)) {
372 /* timed out, enter rebinding state */
374 timeout = now + (t2 - t1);
375 DEBUG("Entering rebinding state");
377 /* send a request packet */
378 send_renew(xid, server_addr, requested_ip); /* unicast */
380 t1 = (t2 - t1) / 2 + t1;
381 timeout = t1 + start;
385 /* Either set a new T2, or enter INIT state */
386 if ((lease - t2) <= (lease / 14400 + 1)) {
387 /* timed out, enter init state */
388 state = INIT_SELECTING;
389 bb_info_msg("Lease lost, entering init state");
390 udhcp_run_script(NULL, "deconfig");
393 change_mode(LISTEN_RAW);
395 /* send a request packet */
396 send_renew(xid, 0, requested_ip); /* broadcast */
398 t2 = (lease - t2) / 2 + t2;
399 timeout = t2 + start;
403 /* yah, I know, *you* say it would never happen */
407 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
408 /* a packet is ready, read it */
410 if (listen_mode == LISTEN_KERNEL)
411 len = udhcp_get_packet(&packet, sockfd);
412 else len = get_raw_packet(&packet, sockfd);
414 if (len == -1 && errno != EINTR) {
415 DEBUG("error on read, %s, reopening socket", strerror(errno));
416 change_mode(listen_mode); /* just close and reopen */
418 if (len < 0) continue;
420 if (packet.xid != xid) {
421 DEBUG("Ignoring XID %x (our xid is %x)",
422 (unsigned)packet.xid, (unsigned)xid);
426 /* Ignore packets that aren't for us */
427 if (memcmp(packet.chaddr, client_config.arp, 6)) {
428 DEBUG("Packet does not have our chaddr - ignoring");
432 message = get_option(&packet, DHCP_MESSAGE_TYPE);
433 if (message == NULL) {
434 bb_error_msg("cannot get option from packet - ignoring");
440 /* Must be a DHCPOFFER to one of our xid's */
441 if (*message == DHCPOFFER) {
442 temp = get_option(&packet, DHCP_SERVER_ID);
444 /* can be misaligned, thus memcpy */
445 memcpy(&server_addr, temp, 4);
447 requested_ip = packet.yiaddr;
449 /* enter requesting state */
454 bb_error_msg("no server ID in message");
458 case RENEW_REQUESTED:
462 if (*message == DHCPACK) {
463 temp = get_option(&packet, DHCP_LEASE_TIME);
465 bb_error_msg("no lease time with ACK, using 1 hour lease");
468 /* can be misaligned, thus memcpy */
469 memcpy(&lease, temp, 4);
470 lease = ntohl(lease);
473 /* enter bound state */
476 /* little fixed point for n * .875 */
477 t2 = (lease * 7) >> 3;
478 temp_addr.s_addr = packet.yiaddr;
479 bb_info_msg("Lease of %s obtained, lease time %u",
480 inet_ntoa(temp_addr), (unsigned)lease);
482 timeout = t1 + start;
483 requested_ip = packet.yiaddr;
484 udhcp_run_script(&packet,
485 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
488 change_mode(LISTEN_NONE);
489 if (client_config.quit_after_lease) {
490 if (client_config.release_on_quit)
494 if (!client_config.foreground)
497 } else if (*message == DHCPNAK) {
498 /* return to init state */
499 bb_info_msg("Received DHCP NAK");
500 udhcp_run_script(&packet, "nak");
501 if (state != REQUESTING)
502 udhcp_run_script(NULL, "deconfig");
503 state = INIT_SELECTING;
507 change_mode(LISTEN_RAW);
508 sleep(3); /* avoid excessive network traffic */
511 /* case BOUND, RELEASED: - ignore all packets */
513 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
522 bb_info_msg("Received SIGTERM");
523 if (client_config.release_on_quit)
527 } else if (retval == -1 && errno == EINTR) {
528 /* a signal was caught */
530 /* An error occured */
531 bb_perror_msg("select");
537 if (client_config.pidfile)
538 remove_pidfile(client_config.pidfile);