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.
20 /* Something is definitely wrong here. IPv4 addresses
21 * in variables of type long?? BTW, we use inet_ntoa()
22 * in the code. Manpage says that struct in_addr has a member of type long (!)
23 * which holds IPv4 address, and the struct is passed by value (!!)
25 static unsigned long requested_ip; /* = 0 */
26 static unsigned long server_addr;
27 static unsigned long timeout;
28 static int packet_num; /* = 0 */
32 #define LISTEN_KERNEL 1
34 static int listen_mode;
36 struct client_config_t client_config;
39 /* just a little helper */
40 static void change_mode(int new_mode)
42 DEBUG("entering %s listen mode",
43 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
44 if (fd >= 0) close(fd);
46 listen_mode = new_mode;
51 static void perform_renew(void)
53 bb_info_msg("Performing a DHCP renew");
56 change_mode(LISTEN_KERNEL);
59 state = RENEW_REQUESTED;
61 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
62 udhcp_run_script(NULL, "deconfig");
65 change_mode(LISTEN_RAW);
66 state = INIT_SELECTING;
72 /* start things over */
75 /* Kill any timeouts because the user wants this to hurry along */
80 /* perform a release */
81 static void perform_release(void)
84 struct in_addr temp_addr;
86 /* send release packet */
87 if (state == BOUND || state == RENEWING || state == REBINDING) {
88 temp_addr.s_addr = server_addr;
89 sprintf(buffer, "%s", inet_ntoa(temp_addr));
90 temp_addr.s_addr = requested_ip;
91 bb_info_msg("Unicasting a release of %s to %s",
92 inet_ntoa(temp_addr), buffer);
93 send_release(server_addr, requested_ip); /* unicast */
94 udhcp_run_script(NULL, "deconfig");
96 bb_info_msg("Entering released state");
98 change_mode(LISTEN_NONE);
100 timeout = 0x7fffffff;
104 static void client_background(void)
106 udhcp_background(client_config.pidfile);
107 client_config.foreground = 1; /* Do not fork again. */
108 client_config.background_if_no_lease = 0;
112 static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
115 int len = strlen(str);
116 if (len > 255) len = 255;
117 storage = xzalloc(len + extra + OPT_DATA);
118 storage[OPT_CODE] = code;
119 storage[OPT_LEN] = len + extra;
120 memcpy(storage + extra + OPT_DATA, str, len);
125 int udhcpc_main(int argc, char *argv[])
127 uint8_t *temp, *message;
128 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
129 unsigned long t1 = 0, t2 = 0, xid = 0;
130 unsigned long start = 0, lease = 0;
140 struct dhcpMessage packet;
141 struct in_addr temp_addr;
163 static const struct option arg_options[] = {
164 { "clientid", required_argument, 0, 'c' },
165 { "clientid-none", no_argument, 0, 'C' },
166 { "vendorclass", required_argument, 0, 'V' },
167 { "foreground", no_argument, 0, 'f' },
168 { "background", no_argument, 0, 'b' },
169 { "hostname", required_argument, 0, 'H' },
170 { "hostname", required_argument, 0, 'h' },
171 { "fqdn", required_argument, 0, 'F' },
172 { "interface", required_argument, 0, 'i' },
173 { "now", no_argument, 0, 'n' },
174 { "pidfile", required_argument, 0, 'p' },
175 { "quit", no_argument, 0, 'q' },
176 { "release", no_argument, 0, 'R' },
177 { "request", required_argument, 0, 'r' },
178 { "script", required_argument, 0, 's' },
179 { "timeout", required_argument, 0, 'T' },
180 { "version", no_argument, 0, 'v' },
181 { "retries", required_argument, 0, 't' },
185 /* Default options. */
186 client_config.interface = "eth0";
187 client_config.script = DEFAULT_SCRIPT;
188 client_config.retries = 3;
189 client_config.timeout = 3;
191 /* Parse command line */
192 opt_complementary = "?:c--C:C--c" // mutually exclusive
193 ":hH:Hh"; // -h and -H are the same
194 applet_long_options = arg_options;
195 opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
196 &str_c, &str_V, &str_h, &str_h, &str_F,
197 &client_config.interface, &client_config.pidfile, &str_r,
198 &client_config.script, &str_T, &str_t
202 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
206 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
208 client_config.foreground = 1;
210 client_config.background_if_no_lease = 1;
212 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
214 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
216 S: 1 => Client requests Server to update A RR in DNS as well as PTR
217 O: 1 => Server indicates to client that DNS has been updated regardless
218 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
219 N: 1 => Client requests Server to not update DNS
221 client_config.fqdn[OPT_DATA + 0] = 0x1;
222 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
223 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
225 // if (opt & OPT_i) client_config.interface = ...
227 client_config.abort_if_no_lease = 1;
228 // if (opt & OPT_p) client_config.pidfile = ...
230 client_config.quit_after_lease = 1;
232 client_config.release_on_quit = 1;
234 requested_ip = inet_addr(str_r);
235 // if (opt & OPT_s) client_config.script = ...
237 client_config.timeout = xatoi_u(str_T);
239 client_config.retries = xatoi_u(str_t);
241 printf("version %s\n\n", BB_VER);
245 /* Start the log, sanitize fd's, and write a pid file */
246 udhcp_start_log_and_pid(client_config.pidfile);
248 if (read_interface(client_config.interface, &client_config.ifindex,
249 NULL, client_config.arp) < 0)
252 /* if not set, and not suppressed, setup the default client ID */
253 if (!client_config.clientid && !no_clientid) {
254 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
255 client_config.clientid[OPT_DATA] = 1;
256 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
259 if (!client_config.vendorclass)
260 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
262 /* setup the signal pipe */
265 state = INIT_SELECTING;
266 udhcp_run_script(NULL, "deconfig");
267 change_mode(LISTEN_RAW);
270 tv.tv_sec = timeout - uptime();
273 if (listen_mode != LISTEN_NONE && fd < 0) {
274 if (listen_mode == LISTEN_KERNEL)
275 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
277 fd = raw_socket(client_config.ifindex);
279 bb_perror_msg("FATAL: cannot listen on socket");
283 max_fd = udhcp_sp_fd_set(&rfds, fd);
286 DEBUG("Waiting on select...");
287 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
288 } else retval = 0; /* If we already timed out, fall through */
292 /* timeout dropped to zero */
295 if (packet_num < client_config.retries) {
299 /* send discover packet */
300 send_discover(xid, requested_ip); /* broadcast */
302 timeout = now + client_config.timeout;
305 udhcp_run_script(NULL, "leasefail");
306 if (client_config.background_if_no_lease) {
307 bb_info_msg("No lease, forking to background");
309 } else if (client_config.abort_if_no_lease) {
310 bb_info_msg("No lease, failing");
313 /* wait to try again */
318 case RENEW_REQUESTED:
320 if (packet_num < client_config.retries) {
321 /* send request packet */
322 if (state == RENEW_REQUESTED)
323 send_renew(xid, server_addr, requested_ip); /* unicast */
324 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
326 timeout = now + ((packet_num == 2) ? 10 : 2);
329 /* timed out, go back to init state */
330 if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
331 state = INIT_SELECTING;
334 change_mode(LISTEN_RAW);
338 /* Lease is starting to run out, time to enter renewing state */
340 change_mode(LISTEN_KERNEL);
341 DEBUG("Entering renew state");
342 /* fall right through */
344 /* Either set a new T1, or enter REBINDING state */
345 if ((t2 - t1) <= (lease / 14400 + 1)) {
346 /* timed out, enter rebinding state */
348 timeout = now + (t2 - t1);
349 DEBUG("Entering rebinding state");
351 /* send a request packet */
352 send_renew(xid, server_addr, requested_ip); /* unicast */
354 t1 = (t2 - t1) / 2 + t1;
355 timeout = t1 + start;
359 /* Either set a new T2, or enter INIT state */
360 if ((lease - t2) <= (lease / 14400 + 1)) {
361 /* timed out, enter init state */
362 state = INIT_SELECTING;
363 bb_info_msg("Lease lost, entering init state");
364 udhcp_run_script(NULL, "deconfig");
367 change_mode(LISTEN_RAW);
369 /* send a request packet */
370 send_renew(xid, 0, requested_ip); /* broadcast */
372 t2 = (lease - t2) / 2 + t2;
373 timeout = t2 + start;
377 /* yah, I know, *you* say it would never happen */
378 timeout = 0x7fffffff;
381 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
382 /* a packet is ready, read it */
384 if (listen_mode == LISTEN_KERNEL)
385 len = udhcp_get_packet(&packet, fd);
386 else len = get_raw_packet(&packet, fd);
388 if (len == -1 && errno != EINTR) {
389 DEBUG("error on read, %s, reopening socket", strerror(errno));
390 change_mode(listen_mode); /* just close and reopen */
392 if (len < 0) continue;
394 if (packet.xid != xid) {
395 DEBUG("Ignoring XID %lx (our xid is %lx)",
396 (unsigned long) packet.xid, xid);
400 /* Ignore packets that aren't for us */
401 if (memcmp(packet.chaddr, client_config.arp, 6)) {
402 DEBUG("Packet does not have our chaddr - ignoring");
406 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
407 bb_error_msg("cannot get option from packet - ignoring");
413 /* Must be a DHCPOFFER to one of our xid's */
414 if (*message == DHCPOFFER) {
415 temp = get_option(&packet, DHCP_SERVER_ID);
417 server_addr = *(uint32_t*)temp;
419 requested_ip = packet.yiaddr;
421 /* enter requesting state */
426 bb_error_msg("no server ID in message");
430 case RENEW_REQUESTED:
434 if (*message == DHCPACK) {
435 temp = get_option(&packet, DHCP_LEASE_TIME);
437 bb_error_msg("no lease time with ACK, using 1 hour lease");
440 lease = ntohl(*(uint32_t*)temp);
443 /* enter bound state */
446 /* little fixed point for n * .875 */
447 t2 = (lease * 0x7) >> 3;
448 temp_addr.s_addr = packet.yiaddr;
449 bb_info_msg("Lease of %s obtained, lease time %ld",
450 inet_ntoa(temp_addr), lease);
452 timeout = t1 + start;
453 requested_ip = packet.yiaddr;
454 udhcp_run_script(&packet,
455 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
458 change_mode(LISTEN_NONE);
459 if (client_config.quit_after_lease) {
460 if (client_config.release_on_quit)
464 if (!client_config.foreground)
467 } else if (*message == DHCPNAK) {
468 /* return to init state */
469 bb_info_msg("Received DHCP NAK");
470 udhcp_run_script(&packet, "nak");
471 if (state != REQUESTING)
472 udhcp_run_script(NULL, "deconfig");
473 state = INIT_SELECTING;
477 change_mode(LISTEN_RAW);
478 sleep(3); /* avoid excessive network traffic */
481 /* case BOUND, RELEASED: - ignore all packets */
483 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
492 bb_info_msg("Received SIGTERM");
493 if (client_config.release_on_quit)
497 } else if (retval == -1 && errno == EINTR) {
498 /* a signal was caught */
500 /* An error occured */
501 bb_perror_msg("select");