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,
61 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
65 static void ATTRIBUTE_NORETURN show_usage(void)
68 "Usage: udhcpc [OPTIONS]\n\n"
69 " -c, --clientid=CLIENTID Set client identifier - type is first char\n"
70 " -C, --clientid-none Suppress default client identifier\n"
71 " -V, --vendorclass=CLASSID Set vendor class identifier\n"
72 " -H, --hostname=HOSTNAME Client hostname\n"
74 " -F, --fqdn=FQDN Client fully qualified domain name\n"
75 " -f, --foreground Do not fork after getting lease\n"
76 " -b, --background Fork to background if lease cannot be\n"
77 " immediately negotiated.\n"
78 " -i, --interface=INTERFACE Interface to use (default: eth0)\n"
79 " -n, --now Exit with failure if lease cannot be\n"
80 " immediately negotiated.\n"
81 " -p, --pidfile=file Store process ID of daemon in file\n"
82 " -q, --quit Quit after obtaining lease\n"
83 " -r, --request=IP IP address to request (default: none)\n"
84 " -s, --script=file Run file at dhcp events (default:\n"
85 " " DEFAULT_SCRIPT ")\n"
86 " -v, --version Display version\n"
91 #define show_usage bb_show_usage
92 extern void show_usage(void) ATTRIBUTE_NORETURN;
96 /* just a little helper */
97 static void change_mode(int new_mode)
99 DEBUG(LOG_INFO, "entering %s listen mode",
100 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
101 if (fd >= 0) close(fd);
103 listen_mode = new_mode;
107 /* perform a renew */
108 static void perform_renew(void)
110 LOG(LOG_INFO, "Performing a DHCP renew");
113 change_mode(LISTEN_KERNEL);
116 state = RENEW_REQUESTED;
118 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
119 run_script(NULL, "deconfig");
122 change_mode(LISTEN_RAW);
123 state = INIT_SELECTING;
129 /* start things over */
132 /* Kill any timeouts because the user wants this to hurry along */
137 /* perform a release */
138 static void perform_release(void)
141 struct in_addr temp_addr;
143 /* send release packet */
144 if (state == BOUND || state == RENEWING || state == REBINDING) {
145 temp_addr.s_addr = server_addr;
146 sprintf(buffer, "%s", inet_ntoa(temp_addr));
147 temp_addr.s_addr = requested_ip;
148 LOG(LOG_INFO, "Unicasting a release of %s to %s",
149 inet_ntoa(temp_addr), buffer);
150 send_release(server_addr, requested_ip); /* unicast */
151 run_script(NULL, "deconfig");
153 LOG(LOG_INFO, "Entering released state");
155 change_mode(LISTEN_NONE);
157 timeout = 0x7fffffff;
161 static void client_background(void)
163 background(client_config.pidfile);
164 client_config.foreground = 1; /* Do not fork again. */
165 client_config.background_if_no_lease = 0;
169 #ifdef COMBINED_BINARY
170 int udhcpc_main(int argc, char *argv[])
172 int main(int argc, char *argv[])
175 uint8_t *temp, *message;
176 unsigned long t1 = 0, t2 = 0, xid = 0;
177 unsigned long start = 0, lease;
182 struct dhcpMessage packet;
183 struct in_addr temp_addr;
189 static const struct option arg_options[] = {
190 {"clientid", required_argument, 0, 'c'},
191 {"clientid-none", no_argument, 0, 'C'},
192 {"vendorclass", required_argument, 0, 'V'},
193 {"foreground", no_argument, 0, 'f'},
194 {"background", no_argument, 0, 'b'},
195 {"hostname", required_argument, 0, 'H'},
196 {"hostname", required_argument, 0, 'h'},
197 {"fqdn", required_argument, 0, 'F'},
198 {"interface", required_argument, 0, 'i'},
199 {"now", no_argument, 0, 'n'},
200 {"pidfile", required_argument, 0, 'p'},
201 {"quit", no_argument, 0, 'q'},
202 {"request", required_argument, 0, 'r'},
203 {"script", required_argument, 0, 's'},
204 {"version", no_argument, 0, 'v'},
210 int option_index = 0;
211 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:v", arg_options, &option_index);
216 if (no_clientid) show_usage();
217 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
218 if (client_config.clientid) free(client_config.clientid);
219 client_config.clientid = xmalloc(len + 2);
220 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
221 client_config.clientid[OPT_LEN] = len;
222 client_config.clientid[OPT_DATA] = '\0';
223 strncpy(client_config.clientid + OPT_DATA, optarg, len);
226 if (client_config.clientid) show_usage();
230 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
231 if (client_config.vendorclass) free(client_config.vendorclass);
232 client_config.vendorclass = xmalloc(len + 2);
233 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
234 client_config.vendorclass[OPT_LEN] = len;
235 strncpy(client_config.vendorclass + OPT_DATA, optarg, len);
238 client_config.foreground = 1;
241 client_config.background_if_no_lease = 1;
245 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
246 if (client_config.hostname) free(client_config.hostname);
247 client_config.hostname = xmalloc(len + 2);
248 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
249 client_config.hostname[OPT_LEN] = len;
250 strncpy(client_config.hostname + 2, optarg, len);
253 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
254 if (client_config.fqdn) free(client_config.fqdn);
255 client_config.fqdn = xmalloc(len + 5);
256 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
257 client_config.fqdn[OPT_LEN] = len + 3;
259 S: 1 => Client requests Server to update A RR in DNS as well as PTR
260 O: 1 => Server indicates to client that DNS has been updated regardless
261 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
262 N: 1 => Client requests Server to not update DNS
264 client_config.fqdn[OPT_LEN + 1] = 0x1;
265 client_config.fqdn[OPT_LEN + 2] = 0;
266 client_config.fqdn[OPT_LEN + 3] = 0;
267 strncpy(client_config.fqdn + 5, optarg, len);
270 client_config.interface = optarg;
273 client_config.abort_if_no_lease = 1;
276 client_config.pidfile = optarg;
279 client_config.quit_after_lease = 1;
282 requested_ip = inet_addr(optarg);
285 client_config.script = optarg;
288 printf("udhcpcd, version %s\n\n", VERSION);
296 /* Start the log, sanitize fd's, and write a pid file */
297 start_log_and_pid("udhcpc", client_config.pidfile);
299 if (read_interface(client_config.interface, &client_config.ifindex,
300 NULL, client_config.arp) < 0)
303 /* if not set, and not suppressed, setup the default client ID */
304 if (!client_config.clientid && !no_clientid) {
305 client_config.clientid = xmalloc(6 + 3);
306 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
307 client_config.clientid[OPT_LEN] = 7;
308 client_config.clientid[OPT_DATA] = 1;
309 memcpy(client_config.clientid + 3, client_config.arp, 6);
312 if (!client_config.vendorclass) {
313 client_config.vendorclass = xmalloc(sizeof("udhcp "VERSION) + 2);
314 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
315 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "VERSION) - 1;
316 client_config.vendorclass[OPT_DATA] = 1;
317 memcpy(&client_config.vendorclass[OPT_DATA],
318 "udhcp "VERSION, sizeof("udhcp "VERSION) - 1);
322 /* setup the signal pipe */
325 state = INIT_SELECTING;
326 run_script(NULL, "deconfig");
327 change_mode(LISTEN_RAW);
331 tv.tv_sec = timeout - uptime();
334 if (listen_mode != LISTEN_NONE && fd < 0) {
335 if (listen_mode == LISTEN_KERNEL)
336 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
338 fd = raw_socket(client_config.ifindex);
340 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
344 max_fd = udhcp_sp_fd_set(&rfds, fd);
347 DEBUG(LOG_INFO, "Waiting on select...");
348 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
349 } else retval = 0; /* If we already timed out, fall through */
353 /* timeout dropped to zero */
356 if (packet_num < 3) {
360 /* send discover packet */
361 send_discover(xid, requested_ip); /* broadcast */
363 timeout = now + ((packet_num == 2) ? 4 : 2);
366 run_script(NULL, "leasefail");
367 if (client_config.background_if_no_lease) {
368 LOG(LOG_INFO, "No lease, forking to background.");
370 } else if (client_config.abort_if_no_lease) {
371 LOG(LOG_INFO, "No lease, failing.");
374 /* wait to try again */
379 case RENEW_REQUESTED:
381 if (packet_num < 3) {
382 /* send request packet */
383 if (state == RENEW_REQUESTED)
384 send_renew(xid, server_addr, requested_ip); /* unicast */
385 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
387 timeout = now + ((packet_num == 2) ? 10 : 2);
390 /* timed out, go back to init state */
391 if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
392 state = INIT_SELECTING;
395 change_mode(LISTEN_RAW);
399 /* Lease is starting to run out, time to enter renewing state */
401 change_mode(LISTEN_KERNEL);
402 DEBUG(LOG_INFO, "Entering renew state");
403 /* fall right through */
405 /* Either set a new T1, or enter REBINDING state */
406 if ((t2 - t1) <= (lease / 14400 + 1)) {
407 /* timed out, enter rebinding state */
409 timeout = now + (t2 - t1);
410 DEBUG(LOG_INFO, "Entering rebinding state");
412 /* send a request packet */
413 send_renew(xid, server_addr, requested_ip); /* unicast */
415 t1 = (t2 - t1) / 2 + t1;
416 timeout = t1 + start;
420 /* Either set a new T2, or enter INIT state */
421 if ((lease - t2) <= (lease / 14400 + 1)) {
422 /* timed out, enter init state */
423 state = INIT_SELECTING;
424 LOG(LOG_INFO, "Lease lost, entering init state");
425 run_script(NULL, "deconfig");
428 change_mode(LISTEN_RAW);
430 /* send a request packet */
431 send_renew(xid, 0, requested_ip); /* broadcast */
433 t2 = (lease - t2) / 2 + t2;
434 timeout = t2 + start;
438 /* yah, I know, *you* say it would never happen */
439 timeout = 0x7fffffff;
442 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
443 /* a packet is ready, read it */
445 if (listen_mode == LISTEN_KERNEL)
446 len = get_packet(&packet, fd);
447 else len = get_raw_packet(&packet, fd);
449 if (len == -1 && errno != EINTR) {
450 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
451 change_mode(listen_mode); /* just close and reopen */
453 if (len < 0) continue;
455 if (packet.xid != xid) {
456 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
457 (unsigned long) packet.xid, xid);
460 /* Ignore packets that aren't for us */
461 if (memcmp(packet.chaddr, client_config.arp, 6)) {
462 DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
466 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
467 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
473 /* Must be a DHCPOFFER to one of our xid's */
474 if (*message == DHCPOFFER) {
475 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
476 memcpy(&server_addr, temp, 4);
478 requested_ip = packet.yiaddr;
480 /* enter requesting state */
485 DEBUG(LOG_ERR, "No server ID in message");
489 case RENEW_REQUESTED:
493 if (*message == DHCPACK) {
494 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
495 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
498 memcpy(&lease, temp, 4);
499 lease = ntohl(lease);
502 /* enter bound state */
505 /* little fixed point for n * .875 */
506 t2 = (lease * 0x7) >> 3;
507 temp_addr.s_addr = packet.yiaddr;
508 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
509 inet_ntoa(temp_addr), lease);
511 timeout = t1 + start;
512 requested_ip = packet.yiaddr;
514 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
517 change_mode(LISTEN_NONE);
518 if (client_config.quit_after_lease)
520 if (!client_config.foreground)
523 } else if (*message == DHCPNAK) {
524 /* return to init state */
525 LOG(LOG_INFO, "Received DHCP NAK");
526 run_script(&packet, "nak");
527 if (state != REQUESTING)
528 run_script(NULL, "deconfig");
529 state = INIT_SELECTING;
533 change_mode(LISTEN_RAW);
534 sleep(3); /* avoid excessive network traffic */
537 /* case BOUND, RELEASED: - ignore all packets */
539 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
548 LOG(LOG_INFO, "Received SIGTERM");
551 } else if (retval == -1 && errno == EINTR) {
552 /* a signal was caught */
554 /* An error occured */
555 DEBUG(LOG_ERR, "Error on select");