5 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
20 #include <sys/ioctl.h>
28 #include "clientpacket.h"
29 #include "clientsocket.h"
31 #include "signalpipe.h"
34 static unsigned long requested_ip; /* = 0 */
35 static unsigned long server_addr;
36 static unsigned long timeout;
37 static int packet_num; /* = 0 */
41 #define LISTEN_KERNEL 1
43 static int listen_mode;
45 struct client_config_t client_config = {
46 /* Default options. */
47 .abort_if_no_lease = 0,
49 .quit_after_lease = 0,
50 .background_if_no_lease = 0,
53 .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 " -T, --timeout=seconds Try to get the lease for the amount of\n"
87 " seconds (default: 3)\n"
88 " -t, --retries=NUM Send up to NUM request packets\n"
89 " -v, --version Display version\n"
94 #define show_usage bb_show_usage
95 extern void show_usage(void) ATTRIBUTE_NORETURN;
99 /* just a little helper */
100 static void change_mode(int new_mode)
102 DEBUG(LOG_INFO, "entering %s listen mode",
103 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
104 if (fd >= 0) close(fd);
106 listen_mode = new_mode;
110 /* perform a renew */
111 static void perform_renew(void)
113 LOG(LOG_INFO, "Performing a DHCP renew");
116 change_mode(LISTEN_KERNEL);
119 state = RENEW_REQUESTED;
121 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
122 udhcp_run_script(NULL, "deconfig");
125 change_mode(LISTEN_RAW);
126 state = INIT_SELECTING;
132 /* start things over */
135 /* Kill any timeouts because the user wants this to hurry along */
140 /* perform a release */
141 static void perform_release(void)
144 struct in_addr temp_addr;
146 /* send release packet */
147 if (state == BOUND || state == RENEWING || state == REBINDING) {
148 temp_addr.s_addr = server_addr;
149 sprintf(buffer, "%s", inet_ntoa(temp_addr));
150 temp_addr.s_addr = requested_ip;
151 LOG(LOG_INFO, "Unicasting a release of %s to %s",
152 inet_ntoa(temp_addr), buffer);
153 send_release(server_addr, requested_ip); /* unicast */
154 udhcp_run_script(NULL, "deconfig");
156 LOG(LOG_INFO, "Entering released state");
158 change_mode(LISTEN_NONE);
160 timeout = 0x7fffffff;
164 static void client_background(void)
166 udhcp_background(client_config.pidfile);
167 client_config.foreground = 1; /* Do not fork again. */
168 client_config.background_if_no_lease = 0;
172 #ifdef COMBINED_BINARY
173 int udhcpc_main(int argc, char *argv[])
175 int main(int argc, char *argv[])
178 uint8_t *temp, *message;
179 unsigned long t1 = 0, t2 = 0, xid = 0;
180 unsigned long start = 0, lease;
185 struct dhcpMessage packet;
186 struct in_addr temp_addr;
192 static const struct option arg_options[] = {
193 {"clientid", required_argument, 0, 'c'},
194 {"clientid-none", no_argument, 0, 'C'},
195 {"vendorclass", required_argument, 0, 'V'},
196 {"foreground", no_argument, 0, 'f'},
197 {"background", no_argument, 0, 'b'},
198 {"hostname", required_argument, 0, 'H'},
199 {"hostname", required_argument, 0, 'h'},
200 {"fqdn", required_argument, 0, 'F'},
201 {"interface", required_argument, 0, 'i'},
202 {"now", no_argument, 0, 'n'},
203 {"pidfile", required_argument, 0, 'p'},
204 {"quit", no_argument, 0, 'q'},
205 {"request", required_argument, 0, 'r'},
206 {"script", required_argument, 0, 's'},
207 {"timeout", required_argument, 0, 'T'},
208 {"version", no_argument, 0, 'v'},
209 {"retries", required_argument, 0, 't'},
215 int option_index = 0;
216 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:T:t:v", arg_options, &option_index);
221 if (no_clientid) show_usage();
222 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
223 free(client_config.clientid);
224 client_config.clientid = xmalloc(len + 2);
225 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
226 client_config.clientid[OPT_LEN] = len;
227 client_config.clientid[OPT_DATA] = '\0';
228 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
231 if (client_config.clientid) show_usage();
235 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
236 free(client_config.vendorclass);
237 client_config.vendorclass = xmalloc(len + 2);
238 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
239 client_config.vendorclass[OPT_LEN] = len;
240 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
243 client_config.foreground = 1;
246 client_config.background_if_no_lease = 1;
250 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
251 free(client_config.hostname);
252 client_config.hostname = xmalloc(len + 2);
253 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
254 client_config.hostname[OPT_LEN] = len;
255 strncpy((char*)client_config.hostname + 2, optarg, len);
258 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
259 free(client_config.fqdn);
260 client_config.fqdn = xmalloc(len + 5);
261 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
262 client_config.fqdn[OPT_LEN] = len + 3;
264 S: 1 => Client requests Server to update A RR in DNS as well as PTR
265 O: 1 => Server indicates to client that DNS has been updated regardless
266 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
267 N: 1 => Client requests Server to not update DNS
269 client_config.fqdn[OPT_LEN + 1] = 0x1;
270 client_config.fqdn[OPT_LEN + 2] = 0;
271 client_config.fqdn[OPT_LEN + 3] = 0;
272 strncpy((char*)client_config.fqdn + 5, optarg, len);
275 client_config.interface = optarg;
278 client_config.abort_if_no_lease = 1;
281 client_config.pidfile = optarg;
284 client_config.quit_after_lease = 1;
287 requested_ip = inet_addr(optarg);
290 client_config.script = optarg;
293 client_config.timeout = atoi(optarg);
296 client_config.retries = atoi(optarg);
299 printf("version %s\n\n", BB_VER);
307 /* Start the log, sanitize fd's, and write a pid file */
308 udhcp_start_log_and_pid("udhcpc", client_config.pidfile);
310 if (read_interface(client_config.interface, &client_config.ifindex,
311 NULL, client_config.arp) < 0)
314 /* if not set, and not suppressed, setup the default client ID */
315 if (!client_config.clientid && !no_clientid) {
316 client_config.clientid = xmalloc(6 + 3);
317 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
318 client_config.clientid[OPT_LEN] = 7;
319 client_config.clientid[OPT_DATA] = 1;
320 memcpy(client_config.clientid + 3, client_config.arp, 6);
323 if (!client_config.vendorclass) {
324 client_config.vendorclass = xmalloc(sizeof("udhcp "BB_VER) + 2);
325 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
326 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "BB_VER) - 1;
327 client_config.vendorclass[OPT_DATA] = 1;
328 memcpy(&client_config.vendorclass[OPT_DATA],
329 "udhcp "BB_VER, sizeof("udhcp "BB_VER) - 1);
333 /* setup the signal pipe */
336 state = INIT_SELECTING;
337 udhcp_run_script(NULL, "deconfig");
338 change_mode(LISTEN_RAW);
342 tv.tv_sec = timeout - uptime();
345 if (listen_mode != LISTEN_NONE && fd < 0) {
346 if (listen_mode == LISTEN_KERNEL)
347 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
349 fd = raw_socket(client_config.ifindex);
351 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
355 max_fd = udhcp_sp_fd_set(&rfds, fd);
358 DEBUG(LOG_INFO, "Waiting on select...");
359 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
360 } else retval = 0; /* If we already timed out, fall through */
364 /* timeout dropped to zero */
367 if (packet_num < client_config.retries) {
371 /* send discover packet */
372 send_discover(xid, requested_ip); /* broadcast */
374 timeout = now + client_config.timeout;
377 udhcp_run_script(NULL, "leasefail");
378 if (client_config.background_if_no_lease) {
379 LOG(LOG_INFO, "No lease, forking to background.");
381 } else if (client_config.abort_if_no_lease) {
382 LOG(LOG_INFO, "No lease, failing.");
385 /* wait to try again */
390 case RENEW_REQUESTED:
392 if (packet_num < client_config.retries) {
393 /* send request packet */
394 if (state == RENEW_REQUESTED)
395 send_renew(xid, server_addr, requested_ip); /* unicast */
396 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
398 timeout = now + ((packet_num == 2) ? 10 : 2);
401 /* timed out, go back to init state */
402 if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
403 state = INIT_SELECTING;
406 change_mode(LISTEN_RAW);
410 /* Lease is starting to run out, time to enter renewing state */
412 change_mode(LISTEN_KERNEL);
413 DEBUG(LOG_INFO, "Entering renew state");
414 /* fall right through */
416 /* Either set a new T1, or enter REBINDING state */
417 if ((t2 - t1) <= (lease / 14400 + 1)) {
418 /* timed out, enter rebinding state */
420 timeout = now + (t2 - t1);
421 DEBUG(LOG_INFO, "Entering rebinding state");
423 /* send a request packet */
424 send_renew(xid, server_addr, requested_ip); /* unicast */
426 t1 = (t2 - t1) / 2 + t1;
427 timeout = t1 + start;
431 /* Either set a new T2, or enter INIT state */
432 if ((lease - t2) <= (lease / 14400 + 1)) {
433 /* timed out, enter init state */
434 state = INIT_SELECTING;
435 LOG(LOG_INFO, "Lease lost, entering init state");
436 udhcp_run_script(NULL, "deconfig");
439 change_mode(LISTEN_RAW);
441 /* send a request packet */
442 send_renew(xid, 0, requested_ip); /* broadcast */
444 t2 = (lease - t2) / 2 + t2;
445 timeout = t2 + start;
449 /* yah, I know, *you* say it would never happen */
450 timeout = 0x7fffffff;
453 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
454 /* a packet is ready, read it */
456 if (listen_mode == LISTEN_KERNEL)
457 len = udhcp_get_packet(&packet, fd);
458 else len = get_raw_packet(&packet, fd);
460 if (len == -1 && errno != EINTR) {
461 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
462 change_mode(listen_mode); /* just close and reopen */
464 if (len < 0) continue;
466 if (packet.xid != xid) {
467 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
468 (unsigned long) packet.xid, xid);
472 /* Ignore packets that aren't for us */
473 if (memcmp(packet.chaddr, client_config.arp, 6)) {
474 DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
478 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
479 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
485 /* Must be a DHCPOFFER to one of our xid's */
486 if (*message == DHCPOFFER) {
487 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
488 memcpy(&server_addr, temp, 4);
490 requested_ip = packet.yiaddr;
492 /* enter requesting state */
497 DEBUG(LOG_ERR, "No server ID in message");
501 case RENEW_REQUESTED:
505 if (*message == DHCPACK) {
506 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
507 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
510 memcpy(&lease, temp, 4);
511 lease = ntohl(lease);
514 /* enter bound state */
517 /* little fixed point for n * .875 */
518 t2 = (lease * 0x7) >> 3;
519 temp_addr.s_addr = packet.yiaddr;
520 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
521 inet_ntoa(temp_addr), lease);
523 timeout = t1 + start;
524 requested_ip = packet.yiaddr;
525 udhcp_run_script(&packet,
526 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
529 change_mode(LISTEN_NONE);
530 if (client_config.quit_after_lease)
532 if (!client_config.foreground)
535 } else if (*message == DHCPNAK) {
536 /* return to init state */
537 LOG(LOG_INFO, "Received DHCP NAK");
538 udhcp_run_script(&packet, "nak");
539 if (state != REQUESTING)
540 udhcp_run_script(NULL, "deconfig");
541 state = INIT_SELECTING;
545 change_mode(LISTEN_RAW);
546 sleep(3); /* avoid excessive network traffic */
549 /* case BOUND, RELEASED: - ignore all packets */
551 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
560 LOG(LOG_INFO, "Received SIGTERM");
563 } else if (retval == -1 && errno == EINTR) {
564 /* a signal was caught */
566 /* An error occured */
567 DEBUG(LOG_ERR, "Error on select");