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 long requested_ip; /* = 0 */
26 static unsigned long server_addr;
27 static unsigned long timeout;
28 static int packet_num; /* = 0 */
32 #define LISTEN_KERNEL 1
34 static int listen_mode;
36 struct client_config_t client_config = {
37 /* Default options. */
38 .abort_if_no_lease = 0,
40 .quit_after_lease = 0,
42 .background_if_no_lease = 0,
45 .script = DEFAULT_SCRIPT,
53 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
56 /* just a little helper */
57 static void change_mode(int new_mode)
59 DEBUG("entering %s listen mode",
60 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
61 if (fd >= 0) close(fd);
63 listen_mode = new_mode;
68 static void perform_renew(void)
70 bb_info_msg("Performing a DHCP renew");
73 change_mode(LISTEN_KERNEL);
76 state = RENEW_REQUESTED;
78 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
79 udhcp_run_script(NULL, "deconfig");
82 change_mode(LISTEN_RAW);
83 state = INIT_SELECTING;
89 /* start things over */
92 /* Kill any timeouts because the user wants this to hurry along */
97 /* perform a release */
98 static void perform_release(void)
101 struct in_addr temp_addr;
103 /* send release packet */
104 if (state == BOUND || state == RENEWING || state == REBINDING) {
105 temp_addr.s_addr = server_addr;
106 sprintf(buffer, "%s", inet_ntoa(temp_addr));
107 temp_addr.s_addr = requested_ip;
108 bb_info_msg("Unicasting a release of %s to %s",
109 inet_ntoa(temp_addr), buffer);
110 send_release(server_addr, requested_ip); /* unicast */
111 udhcp_run_script(NULL, "deconfig");
113 bb_info_msg("Entering released state");
115 change_mode(LISTEN_NONE);
117 timeout = 0x7fffffff;
121 static void client_background(void)
123 udhcp_background(client_config.pidfile);
124 client_config.foreground = 1; /* Do not fork again. */
125 client_config.background_if_no_lease = 0;
129 int udhcpc_main(int argc, char *argv[])
131 uint8_t *temp, *message;
132 unsigned long t1 = 0, t2 = 0, xid = 0;
133 unsigned long start = 0, lease = 0;
138 struct dhcpMessage packet;
139 struct in_addr temp_addr;
145 static const struct option arg_options[] = {
146 {"clientid", required_argument, 0, 'c'},
147 {"clientid-none", no_argument, 0, 'C'},
148 {"vendorclass", required_argument, 0, 'V'},
149 {"foreground", no_argument, 0, 'f'},
150 {"background", no_argument, 0, 'b'},
151 {"hostname", required_argument, 0, 'H'},
152 {"hostname", required_argument, 0, 'h'},
153 {"fqdn", required_argument, 0, 'F'},
154 {"interface", required_argument, 0, 'i'},
155 {"now", no_argument, 0, 'n'},
156 {"pidfile", required_argument, 0, 'p'},
157 {"quit", no_argument, 0, 'q'},
158 {"release", no_argument, 0, 'R'},
159 {"request", required_argument, 0, 'r'},
160 {"script", required_argument, 0, 's'},
161 {"timeout", required_argument, 0, 'T'},
162 {"version", no_argument, 0, 'v'},
163 {"retries", required_argument, 0, 't'},
169 int option_index = 0;
170 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v", arg_options, &option_index);
175 if (no_clientid) bb_show_usage();
176 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
177 free(client_config.clientid);
178 client_config.clientid = xmalloc(len + 2);
179 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
180 client_config.clientid[OPT_LEN] = len;
181 client_config.clientid[OPT_DATA] = '\0';
182 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
185 if (client_config.clientid) bb_show_usage();
189 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
190 free(client_config.vendorclass);
191 client_config.vendorclass = xmalloc(len + 2);
192 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
193 client_config.vendorclass[OPT_LEN] = len;
194 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
197 client_config.foreground = 1;
200 client_config.background_if_no_lease = 1;
204 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
205 free(client_config.hostname);
206 client_config.hostname = xmalloc(len + 2);
207 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
208 client_config.hostname[OPT_LEN] = len;
209 strncpy((char*)client_config.hostname + 2, optarg, len);
212 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
213 free(client_config.fqdn);
214 client_config.fqdn = xmalloc(len + 5);
215 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
216 client_config.fqdn[OPT_LEN] = len + 3;
218 S: 1 => Client requests Server to update A RR in DNS as well as PTR
219 O: 1 => Server indicates to client that DNS has been updated regardless
220 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
221 N: 1 => Client requests Server to not update DNS
223 client_config.fqdn[OPT_LEN + 1] = 0x1;
224 client_config.fqdn[OPT_LEN + 2] = 0;
225 client_config.fqdn[OPT_LEN + 3] = 0;
226 strncpy((char*)client_config.fqdn + 5, optarg, len);
229 client_config.interface = optarg;
232 client_config.abort_if_no_lease = 1;
235 client_config.pidfile = optarg;
238 client_config.quit_after_lease = 1;
241 client_config.release_on_quit = 1;
244 requested_ip = inet_addr(optarg);
247 client_config.script = optarg;
250 client_config.timeout = xatoi_u(optarg);
253 client_config.retries = xatoi_u(optarg);
256 printf("version %s\n\n", BB_VER);
264 /* Start the log, sanitize fd's, and write a pid file */
265 udhcp_start_log_and_pid(client_config.pidfile);
267 if (read_interface(client_config.interface, &client_config.ifindex,
268 NULL, client_config.arp) < 0)
271 /* if not set, and not suppressed, setup the default client ID */
272 if (!client_config.clientid && !no_clientid) {
273 client_config.clientid = xmalloc(6 + 3);
274 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
275 client_config.clientid[OPT_LEN] = 7;
276 client_config.clientid[OPT_DATA] = 1;
277 memcpy(client_config.clientid + 3, client_config.arp, 6);
280 if (!client_config.vendorclass) {
281 client_config.vendorclass = xmalloc(sizeof("udhcp "BB_VER) + 2);
282 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
283 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "BB_VER) - 1;
284 client_config.vendorclass[OPT_DATA] = 1;
285 memcpy(&client_config.vendorclass[OPT_DATA],
286 "udhcp "BB_VER, sizeof("udhcp "BB_VER) - 1);
290 /* setup the signal pipe */
293 state = INIT_SELECTING;
294 udhcp_run_script(NULL, "deconfig");
295 change_mode(LISTEN_RAW);
299 tv.tv_sec = timeout - uptime();
302 if (listen_mode != LISTEN_NONE && fd < 0) {
303 if (listen_mode == LISTEN_KERNEL)
304 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
306 fd = raw_socket(client_config.ifindex);
308 bb_perror_msg("FATAL: cannot listen on socket");
312 max_fd = udhcp_sp_fd_set(&rfds, fd);
315 DEBUG("Waiting on select...");
316 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
317 } else retval = 0; /* If we already timed out, fall through */
321 /* timeout dropped to zero */
324 if (packet_num < client_config.retries) {
328 /* send discover packet */
329 send_discover(xid, requested_ip); /* broadcast */
331 timeout = now + client_config.timeout;
334 udhcp_run_script(NULL, "leasefail");
335 if (client_config.background_if_no_lease) {
336 bb_info_msg("No lease, forking to background");
338 } else if (client_config.abort_if_no_lease) {
339 bb_info_msg("No lease, failing");
342 /* wait to try again */
347 case RENEW_REQUESTED:
349 if (packet_num < client_config.retries) {
350 /* send request packet */
351 if (state == RENEW_REQUESTED)
352 send_renew(xid, server_addr, requested_ip); /* unicast */
353 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
355 timeout = now + ((packet_num == 2) ? 10 : 2);
358 /* timed out, go back to init state */
359 if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
360 state = INIT_SELECTING;
363 change_mode(LISTEN_RAW);
367 /* Lease is starting to run out, time to enter renewing state */
369 change_mode(LISTEN_KERNEL);
370 DEBUG("Entering renew state");
371 /* fall right through */
373 /* Either set a new T1, or enter REBINDING state */
374 if ((t2 - t1) <= (lease / 14400 + 1)) {
375 /* timed out, enter rebinding state */
377 timeout = now + (t2 - t1);
378 DEBUG("Entering rebinding state");
380 /* send a request packet */
381 send_renew(xid, server_addr, requested_ip); /* unicast */
383 t1 = (t2 - t1) / 2 + t1;
384 timeout = t1 + start;
388 /* Either set a new T2, or enter INIT state */
389 if ((lease - t2) <= (lease / 14400 + 1)) {
390 /* timed out, enter init state */
391 state = INIT_SELECTING;
392 bb_info_msg("Lease lost, entering init state");
393 udhcp_run_script(NULL, "deconfig");
396 change_mode(LISTEN_RAW);
398 /* send a request packet */
399 send_renew(xid, 0, requested_ip); /* broadcast */
401 t2 = (lease - t2) / 2 + t2;
402 timeout = t2 + start;
406 /* yah, I know, *you* say it would never happen */
407 timeout = 0x7fffffff;
410 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
411 /* a packet is ready, read it */
413 if (listen_mode == LISTEN_KERNEL)
414 len = udhcp_get_packet(&packet, fd);
415 else len = get_raw_packet(&packet, fd);
417 if (len == -1 && errno != EINTR) {
418 DEBUG("error on read, %s, reopening socket", strerror(errno));
419 change_mode(listen_mode); /* just close and reopen */
421 if (len < 0) continue;
423 if (packet.xid != xid) {
424 DEBUG("Ignoring XID %lx (our xid is %lx)",
425 (unsigned long) packet.xid, xid);
429 /* Ignore packets that aren't for us */
430 if (memcmp(packet.chaddr, client_config.arp, 6)) {
431 DEBUG("Packet does not have our chaddr - ignoring");
435 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
436 bb_error_msg("cannot get option from packet - ignoring");
442 /* Must be a DHCPOFFER to one of our xid's */
443 if (*message == DHCPOFFER) {
444 temp = get_option(&packet, DHCP_SERVER_ID);
446 server_addr = *(uint32_t*)temp;
448 requested_ip = packet.yiaddr;
450 /* enter requesting state */
455 bb_error_msg("no server ID in message");
459 case RENEW_REQUESTED:
463 if (*message == DHCPACK) {
464 temp = get_option(&packet, DHCP_LEASE_TIME);
466 bb_error_msg("no lease time with ACK, using 1 hour lease");
469 lease = ntohl(*(uint32_t*)temp);
472 /* enter bound state */
475 /* little fixed point for n * .875 */
476 t2 = (lease * 0x7) >> 3;
477 temp_addr.s_addr = packet.yiaddr;
478 bb_info_msg("Lease of %s obtained, lease time %ld",
479 inet_ntoa(temp_addr), lease);
481 timeout = t1 + start;
482 requested_ip = packet.yiaddr;
483 udhcp_run_script(&packet,
484 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
487 change_mode(LISTEN_NONE);
488 if (client_config.quit_after_lease) {
489 if (client_config.release_on_quit)
493 if (!client_config.foreground)
496 } else if (*message == DHCPNAK) {
497 /* return to init state */
498 bb_info_msg("Received DHCP NAK");
499 udhcp_run_script(&packet, "nak");
500 if (state != REQUESTING)
501 udhcp_run_script(NULL, "deconfig");
502 state = INIT_SELECTING;
506 change_mode(LISTEN_RAW);
507 sleep(3); /* avoid excessive network traffic */
510 /* case BOUND, RELEASED: - ignore all packets */
512 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
521 bb_info_msg("Received SIGTERM");
522 if (client_config.release_on_quit)
526 } else if (retval == -1 && errno == EINTR) {
527 /* a signal was caught */
529 /* An error occured */
530 bb_perror_msg("select");