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 static unsigned long requested_ip; /* = 0 */
36 static unsigned long server_addr;
37 static unsigned long timeout;
38 static int packet_num; /* = 0 */
42 #define LISTEN_KERNEL 1
44 static int listen_mode;
46 struct client_config_t client_config = {
47 /* Default options. */
48 .abort_if_no_lease = 0,
50 .quit_after_lease = 0,
51 .background_if_no_lease = 0,
54 .script = DEFAULT_SCRIPT,
62 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
65 /* just a little helper */
66 static void change_mode(int new_mode)
68 DEBUG(LOG_INFO, "entering %s listen mode",
69 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
70 if (fd >= 0) close(fd);
72 listen_mode = new_mode;
77 static void perform_renew(void)
79 LOG(LOG_INFO, "Performing a DHCP renew");
82 change_mode(LISTEN_KERNEL);
85 state = RENEW_REQUESTED;
87 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
88 udhcp_run_script(NULL, "deconfig");
91 change_mode(LISTEN_RAW);
92 state = INIT_SELECTING;
98 /* start things over */
101 /* Kill any timeouts because the user wants this to hurry along */
106 /* perform a release */
107 static void perform_release(void)
110 struct in_addr temp_addr;
112 /* send release packet */
113 if (state == BOUND || state == RENEWING || state == REBINDING) {
114 temp_addr.s_addr = server_addr;
115 sprintf(buffer, "%s", inet_ntoa(temp_addr));
116 temp_addr.s_addr = requested_ip;
117 LOG(LOG_INFO, "Unicasting a release of %s to %s",
118 inet_ntoa(temp_addr), buffer);
119 send_release(server_addr, requested_ip); /* unicast */
120 udhcp_run_script(NULL, "deconfig");
122 LOG(LOG_INFO, "Entering released state");
124 change_mode(LISTEN_NONE);
126 timeout = 0x7fffffff;
130 static void client_background(void)
132 udhcp_background(client_config.pidfile);
133 client_config.foreground = 1; /* Do not fork again. */
134 client_config.background_if_no_lease = 0;
138 int udhcpc_main(int argc, char *argv[])
140 uint8_t *temp, *message;
141 unsigned long t1 = 0, t2 = 0, xid = 0;
142 unsigned long start = 0, lease;
147 struct dhcpMessage packet;
148 struct in_addr temp_addr;
154 static const struct option arg_options[] = {
155 {"clientid", required_argument, 0, 'c'},
156 {"clientid-none", no_argument, 0, 'C'},
157 {"vendorclass", required_argument, 0, 'V'},
158 {"foreground", no_argument, 0, 'f'},
159 {"background", no_argument, 0, 'b'},
160 {"hostname", required_argument, 0, 'H'},
161 {"hostname", required_argument, 0, 'h'},
162 {"fqdn", required_argument, 0, 'F'},
163 {"interface", required_argument, 0, 'i'},
164 {"now", no_argument, 0, 'n'},
165 {"pidfile", required_argument, 0, 'p'},
166 {"quit", no_argument, 0, 'q'},
167 {"request", required_argument, 0, 'r'},
168 {"script", required_argument, 0, 's'},
169 {"timeout", required_argument, 0, 'T'},
170 {"version", no_argument, 0, 'v'},
171 {"retries", required_argument, 0, 't'},
177 int option_index = 0;
178 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:T:t:v", arg_options, &option_index);
183 if (no_clientid) bb_show_usage();
184 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
185 free(client_config.clientid);
186 client_config.clientid = xmalloc(len + 2);
187 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
188 client_config.clientid[OPT_LEN] = len;
189 client_config.clientid[OPT_DATA] = '\0';
190 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
193 if (client_config.clientid) bb_show_usage();
197 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
198 free(client_config.vendorclass);
199 client_config.vendorclass = xmalloc(len + 2);
200 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
201 client_config.vendorclass[OPT_LEN] = len;
202 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
205 client_config.foreground = 1;
208 client_config.background_if_no_lease = 1;
212 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
213 free(client_config.hostname);
214 client_config.hostname = xmalloc(len + 2);
215 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
216 client_config.hostname[OPT_LEN] = len;
217 strncpy((char*)client_config.hostname + 2, optarg, len);
220 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
221 free(client_config.fqdn);
222 client_config.fqdn = xmalloc(len + 5);
223 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
224 client_config.fqdn[OPT_LEN] = len + 3;
226 S: 1 => Client requests Server to update A RR in DNS as well as PTR
227 O: 1 => Server indicates to client that DNS has been updated regardless
228 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
229 N: 1 => Client requests Server to not update DNS
231 client_config.fqdn[OPT_LEN + 1] = 0x1;
232 client_config.fqdn[OPT_LEN + 2] = 0;
233 client_config.fqdn[OPT_LEN + 3] = 0;
234 strncpy((char*)client_config.fqdn + 5, optarg, len);
237 client_config.interface = optarg;
240 client_config.abort_if_no_lease = 1;
243 client_config.pidfile = optarg;
246 client_config.quit_after_lease = 1;
249 requested_ip = inet_addr(optarg);
252 client_config.script = optarg;
255 client_config.timeout = atoi(optarg);
258 client_config.retries = atoi(optarg);
261 printf("version %s\n\n", BB_VER);
269 /* Start the log, sanitize fd's, and write a pid file */
270 udhcp_start_log_and_pid("udhcpc", client_config.pidfile);
272 if (read_interface(client_config.interface, &client_config.ifindex,
273 NULL, client_config.arp) < 0)
276 /* if not set, and not suppressed, setup the default client ID */
277 if (!client_config.clientid && !no_clientid) {
278 client_config.clientid = xmalloc(6 + 3);
279 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
280 client_config.clientid[OPT_LEN] = 7;
281 client_config.clientid[OPT_DATA] = 1;
282 memcpy(client_config.clientid + 3, client_config.arp, 6);
285 if (!client_config.vendorclass) {
286 client_config.vendorclass = xmalloc(sizeof("udhcp "BB_VER) + 2);
287 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
288 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "BB_VER) - 1;
289 client_config.vendorclass[OPT_DATA] = 1;
290 memcpy(&client_config.vendorclass[OPT_DATA],
291 "udhcp "BB_VER, sizeof("udhcp "BB_VER) - 1);
295 /* setup the signal pipe */
298 state = INIT_SELECTING;
299 udhcp_run_script(NULL, "deconfig");
300 change_mode(LISTEN_RAW);
304 tv.tv_sec = timeout - uptime();
307 if (listen_mode != LISTEN_NONE && fd < 0) {
308 if (listen_mode == LISTEN_KERNEL)
309 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
311 fd = raw_socket(client_config.ifindex);
313 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
317 max_fd = udhcp_sp_fd_set(&rfds, fd);
320 DEBUG(LOG_INFO, "Waiting on select...");
321 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
322 } else retval = 0; /* If we already timed out, fall through */
326 /* timeout dropped to zero */
329 if (packet_num < client_config.retries) {
333 /* send discover packet */
334 send_discover(xid, requested_ip); /* broadcast */
336 timeout = now + client_config.timeout;
339 udhcp_run_script(NULL, "leasefail");
340 if (client_config.background_if_no_lease) {
341 LOG(LOG_INFO, "No lease, forking to background.");
343 } else if (client_config.abort_if_no_lease) {
344 LOG(LOG_INFO, "No lease, failing.");
347 /* wait to try again */
352 case RENEW_REQUESTED:
354 if (packet_num < client_config.retries) {
355 /* send request packet */
356 if (state == RENEW_REQUESTED)
357 send_renew(xid, server_addr, requested_ip); /* unicast */
358 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
360 timeout = now + ((packet_num == 2) ? 10 : 2);
363 /* timed out, go back to init state */
364 if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
365 state = INIT_SELECTING;
368 change_mode(LISTEN_RAW);
372 /* Lease is starting to run out, time to enter renewing state */
374 change_mode(LISTEN_KERNEL);
375 DEBUG(LOG_INFO, "Entering renew state");
376 /* fall right through */
378 /* Either set a new T1, or enter REBINDING state */
379 if ((t2 - t1) <= (lease / 14400 + 1)) {
380 /* timed out, enter rebinding state */
382 timeout = now + (t2 - t1);
383 DEBUG(LOG_INFO, "Entering rebinding state");
385 /* send a request packet */
386 send_renew(xid, server_addr, requested_ip); /* unicast */
388 t1 = (t2 - t1) / 2 + t1;
389 timeout = t1 + start;
393 /* Either set a new T2, or enter INIT state */
394 if ((lease - t2) <= (lease / 14400 + 1)) {
395 /* timed out, enter init state */
396 state = INIT_SELECTING;
397 LOG(LOG_INFO, "Lease lost, entering init state");
398 udhcp_run_script(NULL, "deconfig");
401 change_mode(LISTEN_RAW);
403 /* send a request packet */
404 send_renew(xid, 0, requested_ip); /* broadcast */
406 t2 = (lease - t2) / 2 + t2;
407 timeout = t2 + start;
411 /* yah, I know, *you* say it would never happen */
412 timeout = 0x7fffffff;
415 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
416 /* a packet is ready, read it */
418 if (listen_mode == LISTEN_KERNEL)
419 len = udhcp_get_packet(&packet, fd);
420 else len = get_raw_packet(&packet, fd);
422 if (len == -1 && errno != EINTR) {
423 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
424 change_mode(listen_mode); /* just close and reopen */
426 if (len < 0) continue;
428 if (packet.xid != xid) {
429 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
430 (unsigned long) packet.xid, xid);
434 /* Ignore packets that aren't for us */
435 if (memcmp(packet.chaddr, client_config.arp, 6)) {
436 DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
440 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
441 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
447 /* Must be a DHCPOFFER to one of our xid's */
448 if (*message == DHCPOFFER) {
449 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
450 memcpy(&server_addr, temp, 4);
452 requested_ip = packet.yiaddr;
454 /* enter requesting state */
459 DEBUG(LOG_ERR, "No server ID in message");
463 case RENEW_REQUESTED:
467 if (*message == DHCPACK) {
468 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
469 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
472 memcpy(&lease, temp, 4);
473 lease = ntohl(lease);
476 /* enter bound state */
479 /* little fixed point for n * .875 */
480 t2 = (lease * 0x7) >> 3;
481 temp_addr.s_addr = packet.yiaddr;
482 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
483 inet_ntoa(temp_addr), lease);
485 timeout = t1 + start;
486 requested_ip = packet.yiaddr;
487 udhcp_run_script(&packet,
488 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
491 change_mode(LISTEN_NONE);
492 if (client_config.quit_after_lease)
494 if (!client_config.foreground)
497 } else if (*message == DHCPNAK) {
498 /* return to init state */
499 LOG(LOG_INFO, "Received DHCP NAK");
500 udhcp_run_script(&packet, "nak");
501 if (state != REQUESTING)
502 udhcp_run_script(NULL, "deconfig");
503 state = INIT_SELECTING;
507 change_mode(LISTEN_RAW);
508 sleep(3); /* avoid excessive network traffic */
511 /* case BOUND, RELEASED: - ignore all packets */
513 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
522 LOG(LOG_INFO, "Received SIGTERM");
525 } else if (retval == -1 && errno == EINTR) {
526 /* a signal was caught */
528 /* An error occured */
529 DEBUG(LOG_ERR, "Error on select");