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.
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
21 #include <sys/ioctl.h>
29 #include "clientpacket.h"
30 #include "clientsocket.h"
32 #include "signalpipe.h"
35 /* Something is definitely wrong here. IPv4 addresses
36 * in variables of type long?? BTW, we use inet_ntoa()
37 * in the code. Manpage says that struct in_addr has a member of type long (!)
38 * which holds IPv4 address, and the struct is passed by value (!!)
40 static unsigned long requested_ip; /* = 0 */
41 static unsigned long server_addr;
42 static unsigned long timeout;
43 static int packet_num; /* = 0 */
47 #define LISTEN_KERNEL 1
49 static int listen_mode;
51 struct client_config_t client_config = {
52 /* Default options. */
53 .abort_if_no_lease = 0,
55 .quit_after_lease = 0,
56 .background_if_no_lease = 0,
59 .script = DEFAULT_SCRIPT,
67 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
70 /* just a little helper */
71 static void change_mode(int new_mode)
73 DEBUG("entering %s listen mode",
74 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
75 if (fd >= 0) close(fd);
77 listen_mode = new_mode;
82 static void perform_renew(void)
84 bb_info_msg("Performing a DHCP renew");
87 change_mode(LISTEN_KERNEL);
90 state = RENEW_REQUESTED;
92 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
93 udhcp_run_script(NULL, "deconfig");
96 change_mode(LISTEN_RAW);
97 state = INIT_SELECTING;
103 /* start things over */
106 /* Kill any timeouts because the user wants this to hurry along */
111 /* perform a release */
112 static void perform_release(void)
115 struct in_addr temp_addr;
117 /* send release packet */
118 if (state == BOUND || state == RENEWING || state == REBINDING) {
119 temp_addr.s_addr = server_addr;
120 sprintf(buffer, "%s", inet_ntoa(temp_addr));
121 temp_addr.s_addr = requested_ip;
122 bb_info_msg("Unicasting a release of %s to %s",
123 inet_ntoa(temp_addr), buffer);
124 send_release(server_addr, requested_ip); /* unicast */
125 udhcp_run_script(NULL, "deconfig");
127 bb_info_msg("Entering released state");
129 change_mode(LISTEN_NONE);
131 timeout = 0x7fffffff;
135 static void client_background(void)
137 udhcp_background(client_config.pidfile);
138 client_config.foreground = 1; /* Do not fork again. */
139 client_config.background_if_no_lease = 0;
143 int udhcpc_main(int argc, char *argv[])
145 uint8_t *temp, *message;
146 unsigned long t1 = 0, t2 = 0, xid = 0;
147 unsigned long start = 0, lease = 0;
152 struct dhcpMessage packet;
153 struct in_addr temp_addr;
159 static const struct option arg_options[] = {
160 {"clientid", required_argument, 0, 'c'},
161 {"clientid-none", no_argument, 0, 'C'},
162 {"vendorclass", required_argument, 0, 'V'},
163 {"foreground", no_argument, 0, 'f'},
164 {"background", no_argument, 0, 'b'},
165 {"hostname", required_argument, 0, 'H'},
166 {"hostname", required_argument, 0, 'h'},
167 {"fqdn", required_argument, 0, 'F'},
168 {"interface", required_argument, 0, 'i'},
169 {"now", no_argument, 0, 'n'},
170 {"pidfile", required_argument, 0, 'p'},
171 {"quit", no_argument, 0, 'q'},
172 {"request", required_argument, 0, 'r'},
173 {"script", required_argument, 0, 's'},
174 {"timeout", required_argument, 0, 'T'},
175 {"version", no_argument, 0, 'v'},
176 {"retries", required_argument, 0, 't'},
182 int option_index = 0;
183 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:T:t:v", arg_options, &option_index);
188 if (no_clientid) bb_show_usage();
189 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
190 free(client_config.clientid);
191 client_config.clientid = xmalloc(len + 2);
192 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
193 client_config.clientid[OPT_LEN] = len;
194 client_config.clientid[OPT_DATA] = '\0';
195 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
198 if (client_config.clientid) bb_show_usage();
202 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
203 free(client_config.vendorclass);
204 client_config.vendorclass = xmalloc(len + 2);
205 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
206 client_config.vendorclass[OPT_LEN] = len;
207 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
210 client_config.foreground = 1;
213 client_config.background_if_no_lease = 1;
217 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
218 free(client_config.hostname);
219 client_config.hostname = xmalloc(len + 2);
220 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
221 client_config.hostname[OPT_LEN] = len;
222 strncpy((char*)client_config.hostname + 2, optarg, len);
225 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
226 free(client_config.fqdn);
227 client_config.fqdn = xmalloc(len + 5);
228 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
229 client_config.fqdn[OPT_LEN] = len + 3;
231 S: 1 => Client requests Server to update A RR in DNS as well as PTR
232 O: 1 => Server indicates to client that DNS has been updated regardless
233 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
234 N: 1 => Client requests Server to not update DNS
236 client_config.fqdn[OPT_LEN + 1] = 0x1;
237 client_config.fqdn[OPT_LEN + 2] = 0;
238 client_config.fqdn[OPT_LEN + 3] = 0;
239 strncpy((char*)client_config.fqdn + 5, optarg, len);
242 client_config.interface = optarg;
245 client_config.abort_if_no_lease = 1;
248 client_config.pidfile = optarg;
251 client_config.quit_after_lease = 1;
254 requested_ip = inet_addr(optarg);
257 client_config.script = optarg;
260 client_config.timeout = atoi(optarg);
263 client_config.retries = atoi(optarg);
266 printf("version %s\n\n", BB_VER);
274 /* Start the log, sanitize fd's, and write a pid file */
275 udhcp_start_log_and_pid(client_config.pidfile);
277 if (read_interface(client_config.interface, &client_config.ifindex,
278 NULL, client_config.arp) < 0)
281 /* if not set, and not suppressed, setup the default client ID */
282 if (!client_config.clientid && !no_clientid) {
283 client_config.clientid = xmalloc(6 + 3);
284 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
285 client_config.clientid[OPT_LEN] = 7;
286 client_config.clientid[OPT_DATA] = 1;
287 memcpy(client_config.clientid + 3, client_config.arp, 6);
290 if (!client_config.vendorclass) {
291 client_config.vendorclass = xmalloc(sizeof("udhcp "BB_VER) + 2);
292 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
293 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "BB_VER) - 1;
294 client_config.vendorclass[OPT_DATA] = 1;
295 memcpy(&client_config.vendorclass[OPT_DATA],
296 "udhcp "BB_VER, sizeof("udhcp "BB_VER) - 1);
300 /* setup the signal pipe */
303 state = INIT_SELECTING;
304 udhcp_run_script(NULL, "deconfig");
305 change_mode(LISTEN_RAW);
309 tv.tv_sec = timeout - uptime();
312 if (listen_mode != LISTEN_NONE && fd < 0) {
313 if (listen_mode == LISTEN_KERNEL)
314 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
316 fd = raw_socket(client_config.ifindex);
318 bb_perror_msg("FATAL: couldn't listen on socket");
322 max_fd = udhcp_sp_fd_set(&rfds, fd);
325 DEBUG("Waiting on select...");
326 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
327 } else retval = 0; /* If we already timed out, fall through */
331 /* timeout dropped to zero */
334 if (packet_num < client_config.retries) {
338 /* send discover packet */
339 send_discover(xid, requested_ip); /* broadcast */
341 timeout = now + client_config.timeout;
344 udhcp_run_script(NULL, "leasefail");
345 if (client_config.background_if_no_lease) {
346 bb_info_msg("No lease, forking to background");
348 } else if (client_config.abort_if_no_lease) {
349 bb_info_msg("No lease, failing");
352 /* wait to try again */
357 case RENEW_REQUESTED:
359 if (packet_num < client_config.retries) {
360 /* send request packet */
361 if (state == RENEW_REQUESTED)
362 send_renew(xid, server_addr, requested_ip); /* unicast */
363 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
365 timeout = now + ((packet_num == 2) ? 10 : 2);
368 /* timed out, go back to init state */
369 if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
370 state = INIT_SELECTING;
373 change_mode(LISTEN_RAW);
377 /* Lease is starting to run out, time to enter renewing state */
379 change_mode(LISTEN_KERNEL);
380 DEBUG("Entering renew state");
381 /* fall right through */
383 /* Either set a new T1, or enter REBINDING state */
384 if ((t2 - t1) <= (lease / 14400 + 1)) {
385 /* timed out, enter rebinding state */
387 timeout = now + (t2 - t1);
388 DEBUG("Entering rebinding state");
390 /* send a request packet */
391 send_renew(xid, server_addr, requested_ip); /* unicast */
393 t1 = (t2 - t1) / 2 + t1;
394 timeout = t1 + start;
398 /* Either set a new T2, or enter INIT state */
399 if ((lease - t2) <= (lease / 14400 + 1)) {
400 /* timed out, enter init state */
401 state = INIT_SELECTING;
402 bb_info_msg("Lease lost, entering init state");
403 udhcp_run_script(NULL, "deconfig");
406 change_mode(LISTEN_RAW);
408 /* send a request packet */
409 send_renew(xid, 0, requested_ip); /* broadcast */
411 t2 = (lease - t2) / 2 + t2;
412 timeout = t2 + start;
416 /* yah, I know, *you* say it would never happen */
417 timeout = 0x7fffffff;
420 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
421 /* a packet is ready, read it */
423 if (listen_mode == LISTEN_KERNEL)
424 len = udhcp_get_packet(&packet, fd);
425 else len = get_raw_packet(&packet, fd);
427 if (len == -1 && errno != EINTR) {
428 DEBUG("error on read, %s, reopening socket", strerror(errno));
429 change_mode(listen_mode); /* just close and reopen */
431 if (len < 0) continue;
433 if (packet.xid != xid) {
434 DEBUG("Ignoring XID %lx (our xid is %lx)",
435 (unsigned long) packet.xid, xid);
439 /* Ignore packets that aren't for us */
440 if (memcmp(packet.chaddr, client_config.arp, 6)) {
441 DEBUG("Packet does not have our chaddr - ignoring");
445 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
446 bb_error_msg("Couldnt get option from packet - ignoring");
452 /* Must be a DHCPOFFER to one of our xid's */
453 if (*message == DHCPOFFER) {
454 temp = get_option(&packet, DHCP_SERVER_ID);
456 server_addr = *(uint32_t*)temp;
458 requested_ip = packet.yiaddr;
460 /* enter requesting state */
465 bb_error_msg("No server ID in message");
469 case RENEW_REQUESTED:
473 if (*message == DHCPACK) {
474 temp = get_option(&packet, DHCP_LEASE_TIME);
476 bb_error_msg("No lease time with ACK, using 1 hour lease");
479 lease = ntohl(*(uint32_t*)temp);
482 /* enter bound state */
485 /* little fixed point for n * .875 */
486 t2 = (lease * 0x7) >> 3;
487 temp_addr.s_addr = packet.yiaddr;
488 bb_info_msg("Lease of %s obtained, lease time %ld",
489 inet_ntoa(temp_addr), lease);
491 timeout = t1 + start;
492 requested_ip = packet.yiaddr;
493 udhcp_run_script(&packet,
494 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
497 change_mode(LISTEN_NONE);
498 if (client_config.quit_after_lease)
500 if (!client_config.foreground)
503 } else if (*message == DHCPNAK) {
504 /* return to init state */
505 bb_info_msg("Received DHCP NAK");
506 udhcp_run_script(&packet, "nak");
507 if (state != REQUESTING)
508 udhcp_run_script(NULL, "deconfig");
509 state = INIT_SELECTING;
513 change_mode(LISTEN_RAW);
514 sleep(3); /* avoid excessive network traffic */
517 /* case BOUND, RELEASED: - ignore all packets */
519 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
528 bb_info_msg("Received SIGTERM");
531 } else if (retval == -1 && errno == EINTR) {
532 /* a signal was caught */
534 /* An error occured */
535 bb_perror_msg("select");