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,
63 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
67 static void ATTRIBUTE_NORETURN show_usage(void)
70 "Usage: udhcpc [OPTIONS]\n\n"
71 " -c, --clientid=CLIENTID Set client identifier - type is first char\n"
72 " -C, --clientid-none Suppress default client identifier\n"
73 " -V, --vendorclass=CLASSID Set vendor class identifier\n"
74 " -H, --hostname=HOSTNAME Client hostname\n"
76 " -F, --fqdn=FQDN Client fully qualified domain name\n"
77 " -f, --foreground Do not fork after getting lease\n"
78 " -b, --background Fork to background if lease cannot be\n"
79 " immediately negotiated.\n"
80 " -i, --interface=INTERFACE Interface to use (default: eth0)\n"
81 " -n, --now Exit with failure if lease cannot be\n"
82 " immediately negotiated.\n"
83 " -p, --pidfile=file Store process ID of daemon in file\n"
84 " -q, --quit Quit after obtaining lease\n"
85 " -r, --request=IP IP address to request (default: none)\n"
86 " -s, --script=file Run file at dhcp events (default:\n"
87 " " DEFAULT_SCRIPT ")\n"
88 " -T, --timeout=seconds Try to get the lease for the amount of\n"
89 " seconds (default: 3)\n"
90 " -t, --retries=NUM Send up to NUM request packets\n"
91 " -v, --version Display version\n"
96 #define show_usage bb_show_usage
97 extern void show_usage(void) ATTRIBUTE_NORETURN;
101 /* just a little helper */
102 static void change_mode(int new_mode)
104 DEBUG(LOG_INFO, "entering %s listen mode",
105 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
106 if (fd >= 0) close(fd);
108 listen_mode = new_mode;
112 /* perform a renew */
113 static void perform_renew(void)
115 LOG(LOG_INFO, "Performing a DHCP renew");
118 change_mode(LISTEN_KERNEL);
121 state = RENEW_REQUESTED;
123 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
124 run_script(NULL, "deconfig");
127 change_mode(LISTEN_RAW);
128 state = INIT_SELECTING;
134 /* start things over */
137 /* Kill any timeouts because the user wants this to hurry along */
142 /* perform a release */
143 static void perform_release(void)
146 struct in_addr temp_addr;
148 /* send release packet */
149 if (state == BOUND || state == RENEWING || state == REBINDING) {
150 temp_addr.s_addr = server_addr;
151 sprintf(buffer, "%s", inet_ntoa(temp_addr));
152 temp_addr.s_addr = requested_ip;
153 LOG(LOG_INFO, "Unicasting a release of %s to %s",
154 inet_ntoa(temp_addr), buffer);
155 send_release(server_addr, requested_ip); /* unicast */
156 run_script(NULL, "deconfig");
158 LOG(LOG_INFO, "Entering released state");
160 change_mode(LISTEN_NONE);
162 timeout = 0x7fffffff;
166 static void client_background(void)
168 background(client_config.pidfile);
169 client_config.foreground = 1; /* Do not fork again. */
170 client_config.background_if_no_lease = 0;
174 #ifdef COMBINED_BINARY
175 int udhcpc_main(int argc, char *argv[])
177 int main(int argc, char *argv[])
180 uint8_t *temp, *message;
181 unsigned long t1 = 0, t2 = 0, xid = 0;
182 unsigned long start = 0, lease;
187 struct dhcpMessage packet;
188 struct in_addr temp_addr;
194 static const struct option arg_options[] = {
195 {"clientid", required_argument, 0, 'c'},
196 {"clientid-none", no_argument, 0, 'C'},
197 {"vendorclass", required_argument, 0, 'V'},
198 {"foreground", no_argument, 0, 'f'},
199 {"background", no_argument, 0, 'b'},
200 {"hostname", required_argument, 0, 'H'},
201 {"hostname", required_argument, 0, 'h'},
202 {"fqdn", required_argument, 0, 'F'},
203 {"interface", required_argument, 0, 'i'},
204 {"now", no_argument, 0, 'n'},
205 {"pidfile", required_argument, 0, 'p'},
206 {"quit", no_argument, 0, 'q'},
207 {"request", required_argument, 0, 'r'},
208 {"script", required_argument, 0, 's'},
209 {"timeout", required_argument, 0, 'T'},
210 {"version", no_argument, 0, 'v'},
211 {"retries", required_argument, 0, 't'},
217 int option_index = 0;
218 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:T:t:v", arg_options, &option_index);
223 if (no_clientid) show_usage();
224 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
225 free(client_config.clientid);
226 client_config.clientid = xmalloc(len + 2);
227 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
228 client_config.clientid[OPT_LEN] = len;
229 client_config.clientid[OPT_DATA] = '\0';
230 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
233 if (client_config.clientid) show_usage();
237 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
238 free(client_config.vendorclass);
239 client_config.vendorclass = xmalloc(len + 2);
240 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
241 client_config.vendorclass[OPT_LEN] = len;
242 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
245 client_config.foreground = 1;
248 client_config.background_if_no_lease = 1;
252 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
253 free(client_config.hostname);
254 client_config.hostname = xmalloc(len + 2);
255 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
256 client_config.hostname[OPT_LEN] = len;
257 strncpy((char*)client_config.hostname + 2, optarg, len);
260 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
261 free(client_config.fqdn);
262 client_config.fqdn = xmalloc(len + 5);
263 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
264 client_config.fqdn[OPT_LEN] = len + 3;
266 S: 1 => Client requests Server to update A RR in DNS as well as PTR
267 O: 1 => Server indicates to client that DNS has been updated regardless
268 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
269 N: 1 => Client requests Server to not update DNS
271 client_config.fqdn[OPT_LEN + 1] = 0x1;
272 client_config.fqdn[OPT_LEN + 2] = 0;
273 client_config.fqdn[OPT_LEN + 3] = 0;
274 strncpy((char*)client_config.fqdn + 5, optarg, len);
277 client_config.interface = optarg;
280 client_config.abort_if_no_lease = 1;
283 client_config.pidfile = optarg;
286 client_config.quit_after_lease = 1;
289 requested_ip = inet_addr(optarg);
292 client_config.script = optarg;
295 client_config.timeout = atoi(optarg);
298 client_config.retries = atoi(optarg);
301 printf("udhcpcd, version %s\n\n", VERSION);
309 /* Start the log, sanitize fd's, and write a pid file */
310 start_log_and_pid("udhcpc", client_config.pidfile);
312 if (read_interface(client_config.interface, &client_config.ifindex,
313 NULL, client_config.arp) < 0)
316 /* if not set, and not suppressed, setup the default client ID */
317 if (!client_config.clientid && !no_clientid) {
318 client_config.clientid = xmalloc(6 + 3);
319 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
320 client_config.clientid[OPT_LEN] = 7;
321 client_config.clientid[OPT_DATA] = 1;
322 memcpy(client_config.clientid + 3, client_config.arp, 6);
325 if (!client_config.vendorclass) {
326 client_config.vendorclass = xmalloc(sizeof("udhcp "VERSION) + 2);
327 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
328 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "VERSION) - 1;
329 client_config.vendorclass[OPT_DATA] = 1;
330 memcpy(&client_config.vendorclass[OPT_DATA],
331 "udhcp "VERSION, sizeof("udhcp "VERSION) - 1);
335 /* setup the signal pipe */
338 state = INIT_SELECTING;
339 run_script(NULL, "deconfig");
340 change_mode(LISTEN_RAW);
344 tv.tv_sec = timeout - uptime();
347 if (listen_mode != LISTEN_NONE && fd < 0) {
348 if (listen_mode == LISTEN_KERNEL)
349 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
351 fd = raw_socket(client_config.ifindex);
353 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
357 max_fd = udhcp_sp_fd_set(&rfds, fd);
360 DEBUG(LOG_INFO, "Waiting on select...");
361 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
362 } else retval = 0; /* If we already timed out, fall through */
366 /* timeout dropped to zero */
369 if (packet_num < client_config.retries) {
373 /* send discover packet */
374 send_discover(xid, requested_ip); /* broadcast */
376 timeout = now + client_config.timeout;
379 run_script(NULL, "leasefail");
380 if (client_config.background_if_no_lease) {
381 LOG(LOG_INFO, "No lease, forking to background.");
383 } else if (client_config.abort_if_no_lease) {
384 LOG(LOG_INFO, "No lease, failing.");
387 /* wait to try again */
392 case RENEW_REQUESTED:
394 if (packet_num < client_config.retries) {
395 /* send request packet */
396 if (state == RENEW_REQUESTED)
397 send_renew(xid, server_addr, requested_ip); /* unicast */
398 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
400 timeout = now + ((packet_num == 2) ? 10 : 2);
403 /* timed out, go back to init state */
404 if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
405 state = INIT_SELECTING;
408 change_mode(LISTEN_RAW);
412 /* Lease is starting to run out, time to enter renewing state */
414 change_mode(LISTEN_KERNEL);
415 DEBUG(LOG_INFO, "Entering renew state");
416 /* fall right through */
418 /* Either set a new T1, or enter REBINDING state */
419 if ((t2 - t1) <= (lease / 14400 + 1)) {
420 /* timed out, enter rebinding state */
422 timeout = now + (t2 - t1);
423 DEBUG(LOG_INFO, "Entering rebinding state");
425 /* send a request packet */
426 send_renew(xid, server_addr, requested_ip); /* unicast */
428 t1 = (t2 - t1) / 2 + t1;
429 timeout = t1 + start;
433 /* Either set a new T2, or enter INIT state */
434 if ((lease - t2) <= (lease / 14400 + 1)) {
435 /* timed out, enter init state */
436 state = INIT_SELECTING;
437 LOG(LOG_INFO, "Lease lost, entering init state");
438 run_script(NULL, "deconfig");
441 change_mode(LISTEN_RAW);
443 /* send a request packet */
444 send_renew(xid, 0, requested_ip); /* broadcast */
446 t2 = (lease - t2) / 2 + t2;
447 timeout = t2 + start;
451 /* yah, I know, *you* say it would never happen */
452 timeout = 0x7fffffff;
455 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
456 /* a packet is ready, read it */
458 if (listen_mode == LISTEN_KERNEL)
459 len = get_packet(&packet, fd);
460 else len = get_raw_packet(&packet, fd);
462 if (len == -1 && errno != EINTR) {
463 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
464 change_mode(listen_mode); /* just close and reopen */
466 if (len < 0) continue;
468 if (packet.xid != xid) {
469 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
470 (unsigned long) packet.xid, xid);
474 /* Ignore packets that aren't for us */
475 if (memcmp(packet.chaddr, client_config.arp, 6)) {
476 DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
480 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
481 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
487 /* Must be a DHCPOFFER to one of our xid's */
488 if (*message == DHCPOFFER) {
489 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
490 memcpy(&server_addr, temp, 4);
492 requested_ip = packet.yiaddr;
494 /* enter requesting state */
499 DEBUG(LOG_ERR, "No server ID in message");
503 case RENEW_REQUESTED:
507 if (*message == DHCPACK) {
508 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
509 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
512 memcpy(&lease, temp, 4);
513 lease = ntohl(lease);
516 /* enter bound state */
519 /* little fixed point for n * .875 */
520 t2 = (lease * 0x7) >> 3;
521 temp_addr.s_addr = packet.yiaddr;
522 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
523 inet_ntoa(temp_addr), lease);
525 timeout = t1 + start;
526 requested_ip = packet.yiaddr;
528 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
531 change_mode(LISTEN_NONE);
532 if (client_config.quit_after_lease)
534 if (!client_config.foreground)
537 } else if (*message == DHCPNAK) {
538 /* return to init state */
539 LOG(LOG_INFO, "Received DHCP NAK");
540 run_script(&packet, "nak");
541 if (state != REQUESTING)
542 run_script(NULL, "deconfig");
543 state = INIT_SELECTING;
547 change_mode(LISTEN_RAW);
548 sleep(3); /* avoid excessive network traffic */
551 /* case BOUND, RELEASED: - ignore all packets */
553 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
562 LOG(LOG_INFO, "Received SIGTERM");
565 } else if (retval == -1 && errno == EINTR) {
566 /* a signal was caught */
568 /* An error occured */
569 DEBUG(LOG_ERR, "Error on select");