5 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 * 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"
33 #include "signalpipe.h"
36 static unsigned long requested_ip; /* = 0 */
37 static unsigned long server_addr;
38 static unsigned long timeout;
39 static int packet_num; /* = 0 */
43 #define LISTEN_KERNEL 1
45 static int listen_mode;
47 struct client_config_t client_config = {
48 /* Default options. */
49 .abort_if_no_lease = 0,
51 .quit_after_lease = 0,
52 .background_if_no_lease = 0,
55 .script = DEFAULT_SCRIPT,
62 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
66 static void ATTRIBUTE_NORETURN show_usage(void)
69 "Usage: udhcpc [OPTIONS]\n\n"
70 " -c, --clientid=CLIENTID Set client identifier - type is first char\n"
71 " -C, --clientid-none Suppress default client identifier\n"
72 " -V, --vendorclass=CLASSID Set vendor class identifier\n"
73 " -H, --hostname=HOSTNAME Client hostname\n"
75 " -F, --fqdn=FQDN Client fully qualified domain name\n"
76 " -f, --foreground Do not fork after getting lease\n"
77 " -b, --background Fork to background if lease cannot be\n"
78 " immediately negotiated.\n"
79 " -i, --interface=INTERFACE Interface to use (default: eth0)\n"
80 " -n, --now Exit with failure if lease cannot be\n"
81 " immediately negotiated.\n"
82 " -p, --pidfile=file Store process ID of daemon in file\n"
83 " -q, --quit Quit after obtaining lease\n"
84 " -r, --request=IP IP address to request (default: none)\n"
85 " -s, --script=file Run file at dhcp events (default:\n"
86 " " DEFAULT_SCRIPT ")\n"
87 " -v, --version Display version\n"
92 #define show_usage bb_show_usage
93 extern void show_usage(void) ATTRIBUTE_NORETURN;
97 /* just a little helper */
98 static void change_mode(int new_mode)
100 DEBUG(LOG_INFO, "entering %s listen mode",
101 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
102 if (fd >= 0) close(fd);
104 listen_mode = new_mode;
108 /* perform a renew */
109 static void perform_renew(void)
111 LOG(LOG_INFO, "Performing a DHCP renew");
114 change_mode(LISTEN_KERNEL);
117 state = RENEW_REQUESTED;
119 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
120 run_script(NULL, "deconfig");
123 change_mode(LISTEN_RAW);
124 state = INIT_SELECTING;
130 /* start things over */
133 /* Kill any timeouts because the user wants this to hurry along */
138 /* perform a release */
139 static void perform_release(void)
142 struct in_addr temp_addr;
144 /* send release packet */
145 if (state == BOUND || state == RENEWING || state == REBINDING) {
146 temp_addr.s_addr = server_addr;
147 sprintf(buffer, "%s", inet_ntoa(temp_addr));
148 temp_addr.s_addr = requested_ip;
149 LOG(LOG_INFO, "Unicasting a release of %s to %s",
150 inet_ntoa(temp_addr), buffer);
151 send_release(server_addr, requested_ip); /* unicast */
152 run_script(NULL, "deconfig");
154 LOG(LOG_INFO, "Entering released state");
156 change_mode(LISTEN_NONE);
158 timeout = 0x7fffffff;
162 static void client_background(void)
164 background(client_config.pidfile);
165 client_config.foreground = 1; /* Do not fork again. */
166 client_config.background_if_no_lease = 0;
170 #ifdef COMBINED_BINARY
171 int udhcpc_main(int argc, char *argv[])
173 int main(int argc, char *argv[])
176 uint8_t *temp, *message;
177 unsigned long t1 = 0, t2 = 0, xid = 0;
178 unsigned long start = 0, lease;
183 struct dhcpMessage packet;
184 struct in_addr temp_addr;
190 static const struct option arg_options[] = {
191 {"clientid", required_argument, 0, 'c'},
192 {"clientid-none", no_argument, 0, 'C'},
193 {"vendorclass", required_argument, 0, 'V'},
194 {"foreground", no_argument, 0, 'f'},
195 {"background", no_argument, 0, 'b'},
196 {"hostname", required_argument, 0, 'H'},
197 {"hostname", required_argument, 0, 'h'},
198 {"fqdn", required_argument, 0, 'F'},
199 {"interface", required_argument, 0, 'i'},
200 {"now", no_argument, 0, 'n'},
201 {"pidfile", required_argument, 0, 'p'},
202 {"quit", no_argument, 0, 'q'},
203 {"request", required_argument, 0, 'r'},
204 {"script", required_argument, 0, 's'},
205 {"version", no_argument, 0, 'v'},
206 {"retries", required_argument, 0, 't'},
212 int option_index = 0;
213 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:t:v", arg_options, &option_index);
218 if (no_clientid) show_usage();
219 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
220 free(client_config.clientid);
221 client_config.clientid = xmalloc(len + 2);
222 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
223 client_config.clientid[OPT_LEN] = len;
224 client_config.clientid[OPT_DATA] = '\0';
225 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
228 if (client_config.clientid) show_usage();
232 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
233 free(client_config.vendorclass);
234 client_config.vendorclass = xmalloc(len + 2);
235 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
236 client_config.vendorclass[OPT_LEN] = len;
237 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
240 client_config.foreground = 1;
243 client_config.background_if_no_lease = 1;
247 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
248 free(client_config.hostname);
249 client_config.hostname = xmalloc(len + 2);
250 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
251 client_config.hostname[OPT_LEN] = len;
252 strncpy((char*)client_config.hostname + 2, optarg, len);
255 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
256 free(client_config.fqdn);
257 client_config.fqdn = xmalloc(len + 5);
258 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
259 client_config.fqdn[OPT_LEN] = len + 3;
261 S: 1 => Client requests Server to update A RR in DNS as well as PTR
262 O: 1 => Server indicates to client that DNS has been updated regardless
263 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
264 N: 1 => Client requests Server to not update DNS
266 client_config.fqdn[OPT_LEN + 1] = 0x1;
267 client_config.fqdn[OPT_LEN + 2] = 0;
268 client_config.fqdn[OPT_LEN + 3] = 0;
269 strncpy((char*)client_config.fqdn + 5, optarg, len);
272 client_config.interface = optarg;
275 client_config.abort_if_no_lease = 1;
278 client_config.pidfile = optarg;
281 client_config.quit_after_lease = 1;
284 requested_ip = inet_addr(optarg);
287 client_config.script = optarg;
290 client_config.retries = atoi(optarg);
293 printf("udhcpcd, version %s\n\n", VERSION);
301 /* Start the log, sanitize fd's, and write a pid file */
302 start_log_and_pid("udhcpc", client_config.pidfile);
304 if (read_interface(client_config.interface, &client_config.ifindex,
305 NULL, client_config.arp) < 0)
308 /* if not set, and not suppressed, setup the default client ID */
309 if (!client_config.clientid && !no_clientid) {
310 client_config.clientid = xmalloc(6 + 3);
311 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
312 client_config.clientid[OPT_LEN] = 7;
313 client_config.clientid[OPT_DATA] = 1;
314 memcpy(client_config.clientid + 3, client_config.arp, 6);
317 if (!client_config.vendorclass) {
318 client_config.vendorclass = xmalloc(sizeof("udhcp "VERSION) + 2);
319 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
320 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "VERSION) - 1;
321 client_config.vendorclass[OPT_DATA] = 1;
322 memcpy(&client_config.vendorclass[OPT_DATA],
323 "udhcp "VERSION, sizeof("udhcp "VERSION) - 1);
327 /* setup the signal pipe */
330 state = INIT_SELECTING;
331 run_script(NULL, "deconfig");
332 change_mode(LISTEN_RAW);
336 tv.tv_sec = timeout - uptime();
339 if (listen_mode != LISTEN_NONE && fd < 0) {
340 if (listen_mode == LISTEN_KERNEL)
341 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
343 fd = raw_socket(client_config.ifindex);
345 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
349 max_fd = udhcp_sp_fd_set(&rfds, fd);
352 DEBUG(LOG_INFO, "Waiting on select...");
353 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
354 } else retval = 0; /* If we already timed out, fall through */
358 /* timeout dropped to zero */
361 if (packet_num < client_config.retries) {
365 /* send discover packet */
366 send_discover(xid, requested_ip); /* broadcast */
368 timeout = now + ((packet_num == 2) ? 4 : 2);
371 run_script(NULL, "leasefail");
372 if (client_config.background_if_no_lease) {
373 LOG(LOG_INFO, "No lease, forking to background.");
375 } else if (client_config.abort_if_no_lease) {
376 LOG(LOG_INFO, "No lease, failing.");
379 /* wait to try again */
384 case RENEW_REQUESTED:
386 if (packet_num < client_config.retries) {
387 /* send request packet */
388 if (state == RENEW_REQUESTED)
389 send_renew(xid, server_addr, requested_ip); /* unicast */
390 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
392 timeout = now + ((packet_num == 2) ? 10 : 2);
395 /* timed out, go back to init state */
396 if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
397 state = INIT_SELECTING;
400 change_mode(LISTEN_RAW);
404 /* Lease is starting to run out, time to enter renewing state */
406 change_mode(LISTEN_KERNEL);
407 DEBUG(LOG_INFO, "Entering renew state");
408 /* fall right through */
410 /* Either set a new T1, or enter REBINDING state */
411 if ((t2 - t1) <= (lease / 14400 + 1)) {
412 /* timed out, enter rebinding state */
414 timeout = now + (t2 - t1);
415 DEBUG(LOG_INFO, "Entering rebinding state");
417 /* send a request packet */
418 send_renew(xid, server_addr, requested_ip); /* unicast */
420 t1 = (t2 - t1) / 2 + t1;
421 timeout = t1 + start;
425 /* Either set a new T2, or enter INIT state */
426 if ((lease - t2) <= (lease / 14400 + 1)) {
427 /* timed out, enter init state */
428 state = INIT_SELECTING;
429 LOG(LOG_INFO, "Lease lost, entering init state");
430 run_script(NULL, "deconfig");
433 change_mode(LISTEN_RAW);
435 /* send a request packet */
436 send_renew(xid, 0, requested_ip); /* broadcast */
438 t2 = (lease - t2) / 2 + t2;
439 timeout = t2 + start;
443 /* yah, I know, *you* say it would never happen */
444 timeout = 0x7fffffff;
447 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
448 /* a packet is ready, read it */
450 if (listen_mode == LISTEN_KERNEL)
451 len = get_packet(&packet, fd);
452 else len = get_raw_packet(&packet, fd);
454 if (len == -1 && errno != EINTR) {
455 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
456 change_mode(listen_mode); /* just close and reopen */
458 if (len < 0) continue;
460 if (packet.xid != xid) {
461 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
462 (unsigned long) packet.xid, xid);
465 /* Ignore packets that aren't for us */
466 if (memcmp(packet.chaddr, client_config.arp, 6)) {
467 DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
471 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
472 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
478 /* Must be a DHCPOFFER to one of our xid's */
479 if (*message == DHCPOFFER) {
480 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
481 memcpy(&server_addr, temp, 4);
483 requested_ip = packet.yiaddr;
485 /* enter requesting state */
490 DEBUG(LOG_ERR, "No server ID in message");
494 case RENEW_REQUESTED:
498 if (*message == DHCPACK) {
499 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
500 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
503 memcpy(&lease, temp, 4);
504 lease = ntohl(lease);
507 /* enter bound state */
510 /* little fixed point for n * .875 */
511 t2 = (lease * 0x7) >> 3;
512 temp_addr.s_addr = packet.yiaddr;
513 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
514 inet_ntoa(temp_addr), lease);
516 timeout = t1 + start;
517 requested_ip = packet.yiaddr;
519 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
522 change_mode(LISTEN_NONE);
523 if (client_config.quit_after_lease)
525 if (!client_config.foreground)
528 } else if (*message == DHCPNAK) {
529 /* return to init state */
530 LOG(LOG_INFO, "Received DHCP NAK");
531 run_script(&packet, "nak");
532 if (state != REQUESTING)
533 run_script(NULL, "deconfig");
534 state = INIT_SELECTING;
538 change_mode(LISTEN_RAW);
539 sleep(3); /* avoid excessive network traffic */
542 /* case BOUND, RELEASED: - ignore all packets */
544 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
553 LOG(LOG_INFO, "Received SIGTERM");
556 } else if (retval == -1 && errno == EINTR) {
557 /* a signal was caught */
559 /* An error occured */
560 DEBUG(LOG_ERR, "Error on select");