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.
14 /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
15 #define WANT_PIDFILE 1
22 /* Something is definitely wrong here. IPv4 addresses
23 * in variables of type long?? BTW, we use inet_ntoa()
24 * in the code. Manpage says that struct in_addr has a member of type long (!)
25 * which holds IPv4 address, and the struct is passed by value (!!)
27 static unsigned timeout;
28 static uint32_t requested_ip; /* = 0 */
29 static uint32_t server_addr;
30 static int packet_num; /* = 0 */
31 static int sockfd = -1;
34 #define LISTEN_KERNEL 1
36 static smallint listen_mode;
38 static smallint state;
40 struct client_config_t client_config;
43 /* just a little helper */
44 static void change_mode(int new_mode)
46 DEBUG("entering %s listen mode",
47 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
52 listen_mode = new_mode;
57 static void perform_renew(void)
59 bb_info_msg("Performing a DHCP renew");
62 change_mode(LISTEN_KERNEL);
65 state = RENEW_REQUESTED;
67 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
68 udhcp_run_script(NULL, "deconfig");
71 change_mode(LISTEN_RAW);
72 state = INIT_SELECTING;
78 /* start things over */
81 /* Kill any timeouts because the user wants this to hurry along */
86 /* perform a release */
87 static void perform_release(void)
89 char buffer[sizeof("255.255.255.255")];
90 struct in_addr temp_addr;
92 /* send release packet */
93 if (state == BOUND || state == RENEWING || state == REBINDING) {
94 temp_addr.s_addr = server_addr;
95 strcpy(buffer, inet_ntoa(temp_addr));
96 temp_addr.s_addr = requested_ip;
97 bb_info_msg("Unicasting a release of %s to %s",
98 inet_ntoa(temp_addr), buffer);
99 send_release(server_addr, requested_ip); /* unicast */
100 udhcp_run_script(NULL, "deconfig");
102 bb_info_msg("Entering released state");
104 change_mode(LISTEN_NONE);
110 static void client_background(void)
113 bb_error_msg("cannot background in uclinux (yet)");
114 /* ... mainly because udhcpc calls client_background()
115 * in _the _middle _of _udhcpc _run_, not at the start!
116 * If that will be properly disabled for NOMMU, client_background()
117 * will work on NOMMU too */
120 logmode &= ~LOGMODE_STDIO;
121 /* rewrite pidfile, as our pid is different now */
122 if (client_config.pidfile)
123 write_pidfile(client_config.pidfile);
125 /* Do not fork again. */
126 client_config.foreground = 1;
127 client_config.background_if_no_lease = 0;
131 static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
134 int len = strlen(str);
135 if (len > 255) len = 255;
136 storage = xzalloc(len + extra + OPT_DATA);
137 storage[OPT_CODE] = code;
138 storage[OPT_LEN] = len + extra;
139 memcpy(storage + extra + OPT_DATA, str, len);
144 int udhcpc_main(int argc, char **argv);
145 int udhcpc_main(int argc, char **argv)
147 uint8_t *temp, *message;
148 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
150 uint32_t lease = 0; /* can be given as 32-bit quantity */
151 unsigned t1 = 0, t2 = 0;
162 struct dhcpMessage packet;
163 struct in_addr temp_addr;
185 #if ENABLE_GETOPT_LONG
186 static const char udhcpc_longopts[] =
187 "clientid\0" Required_argument "c"
188 "clientid-none\0" No_argument "C"
189 "vendorclass\0" Required_argument "V"
190 "foreground\0" No_argument "f"
191 "background\0" No_argument "b"
192 "hostname\0" Required_argument "H"
193 "hostname\0" Required_argument "h"
194 "fqdn\0" Required_argument "F"
195 "interface\0" Required_argument "i"
196 "now\0" No_argument "n"
197 "pidfile\0" Required_argument "p"
198 "quit\0" No_argument "q"
199 "release\0" No_argument "R"
200 "request\0" Required_argument "r"
201 "script\0" Required_argument "s"
202 "timeout\0" Required_argument "T"
203 "version\0" No_argument "v"
204 "retries\0" Required_argument "t"
207 /* Default options. */
208 client_config.interface = "eth0";
209 client_config.script = DEFAULT_SCRIPT;
210 client_config.retries = 3;
211 client_config.timeout = 3;
213 /* Parse command line */
214 opt_complementary = "c--C:C--c" // mutually exclusive
215 ":hH:Hh"; // -h and -H are the same
216 #if ENABLE_GETOPT_LONG
217 applet_long_options = udhcpc_longopts;
219 opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
220 &str_c, &str_V, &str_h, &str_h, &str_F,
221 &client_config.interface, &client_config.pidfile, &str_r,
222 &client_config.script, &str_T, &str_t
226 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
230 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
232 client_config.foreground = 1;
234 client_config.background_if_no_lease = 1;
236 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
238 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
240 S: 1 => Client requests Server to update A RR in DNS as well as PTR
241 O: 1 => Server indicates to client that DNS has been updated regardless
242 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
243 N: 1 => Client requests Server to not update DNS
245 client_config.fqdn[OPT_DATA + 0] = 0x1;
246 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
247 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
249 // if (opt & OPT_i) client_config.interface = ...
251 client_config.abort_if_no_lease = 1;
252 // if (opt & OPT_p) client_config.pidfile = ...
254 client_config.quit_after_lease = 1;
256 client_config.release_on_quit = 1;
258 requested_ip = inet_addr(str_r);
259 // if (opt & OPT_s) client_config.script = ...
261 client_config.timeout = xatoi_u(str_T);
263 client_config.retries = xatoi_u(str_t);
265 printf("version %s\n\n", BB_VER);
269 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
270 openlog(applet_name, LOG_PID, LOG_LOCAL0);
271 logmode |= LOGMODE_SYSLOG;
274 if (read_interface(client_config.interface, &client_config.ifindex,
275 NULL, client_config.arp) < 0)
278 /* Sanitize fd's and write pidfile */
279 udhcp_make_pidfile(client_config.pidfile);
281 /* if not set, and not suppressed, setup the default client ID */
282 if (!client_config.clientid && !no_clientid) {
283 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
284 client_config.clientid[OPT_DATA] = 1;
285 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
288 if (!client_config.vendorclass)
289 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
291 /* setup the signal pipe */
294 state = INIT_SELECTING;
295 udhcp_run_script(NULL, "deconfig");
296 change_mode(LISTEN_RAW);
299 tv.tv_sec = timeout - monotonic_sec();
302 if (listen_mode != LISTEN_NONE && sockfd < 0) {
303 if (listen_mode == LISTEN_KERNEL)
304 sockfd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
306 sockfd = raw_socket(client_config.ifindex);
308 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
311 DEBUG("Waiting on select...");
312 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
313 } else retval = 0; /* If we already timed out, fall through */
315 now = monotonic_sec();
317 /* timeout dropped to zero */
320 if (packet_num < client_config.retries) {
324 /* send discover packet */
325 send_discover(xid, requested_ip); /* broadcast */
327 timeout = now + client_config.timeout;
330 udhcp_run_script(NULL, "leasefail");
331 if (client_config.background_if_no_lease) {
332 bb_info_msg("No lease, forking to background");
334 } else if (client_config.abort_if_no_lease) {
335 bb_info_msg("No lease, failing");
339 /* wait to try again */
344 case RENEW_REQUESTED:
346 if (packet_num < client_config.retries) {
347 /* send request packet */
348 if (state == RENEW_REQUESTED)
349 send_renew(xid, server_addr, requested_ip); /* unicast */
350 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
352 timeout = now + ((packet_num == 2) ? 10 : 2);
355 /* timed out, go back to init state */
356 if (state == RENEW_REQUESTED)
357 udhcp_run_script(NULL, "deconfig");
358 state = INIT_SELECTING;
361 change_mode(LISTEN_RAW);
365 /* Lease is starting to run out, time to enter renewing state */
367 change_mode(LISTEN_KERNEL);
368 DEBUG("Entering renew state");
369 /* fall right through */
371 /* Either set a new T1, or enter REBINDING state */
372 if ((t2 - t1) <= (lease / 14400 + 1)) {
373 /* timed out, enter rebinding state */
375 timeout = now + (t2 - t1);
376 DEBUG("Entering rebinding state");
378 /* send a request packet */
379 send_renew(xid, server_addr, requested_ip); /* unicast */
381 t1 = (t2 - t1) / 2 + t1;
382 timeout = t1 + start;
386 /* Either set a new T2, or enter INIT state */
387 if ((lease - t2) <= (lease / 14400 + 1)) {
388 /* timed out, enter init state */
389 state = INIT_SELECTING;
390 bb_info_msg("Lease lost, entering init state");
391 udhcp_run_script(NULL, "deconfig");
394 change_mode(LISTEN_RAW);
396 /* send a request packet */
397 send_renew(xid, 0, requested_ip); /* broadcast */
399 t2 = (lease - t2) / 2 + t2;
400 timeout = t2 + start;
404 /* yah, I know, *you* say it would never happen */
408 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
409 /* a packet is ready, read it */
411 if (listen_mode == LISTEN_KERNEL)
412 len = udhcp_get_packet(&packet, sockfd);
413 else len = get_raw_packet(&packet, sockfd);
415 if (len == -1 && errno != EINTR) {
416 DEBUG("error on read, %s, reopening socket", strerror(errno));
417 change_mode(listen_mode); /* just close and reopen */
419 if (len < 0) continue;
421 if (packet.xid != xid) {
422 DEBUG("Ignoring XID %x (our xid is %x)",
423 (unsigned)packet.xid, (unsigned)xid);
427 /* Ignore packets that aren't for us */
428 if (memcmp(packet.chaddr, client_config.arp, 6)) {
429 DEBUG("Packet does not have our chaddr - ignoring");
433 message = get_option(&packet, DHCP_MESSAGE_TYPE);
434 if (message == NULL) {
435 bb_error_msg("cannot get option from packet - ignoring");
441 /* Must be a DHCPOFFER to one of our xid's */
442 if (*message == DHCPOFFER) {
443 temp = get_option(&packet, DHCP_SERVER_ID);
445 /* can be misaligned, thus memcpy */
446 memcpy(&server_addr, temp, 4);
448 requested_ip = packet.yiaddr;
450 /* enter requesting state */
455 bb_error_msg("no server ID in message");
459 case RENEW_REQUESTED:
463 if (*message == DHCPACK) {
464 temp = get_option(&packet, DHCP_LEASE_TIME);
466 bb_error_msg("no lease time with ACK, using 1 hour lease");
469 /* can be misaligned, thus memcpy */
470 memcpy(&lease, temp, 4);
471 lease = ntohl(lease);
474 /* enter bound state */
477 /* little fixed point for n * .875 */
478 t2 = (lease * 7) >> 3;
479 temp_addr.s_addr = packet.yiaddr;
480 bb_info_msg("Lease of %s obtained, lease time %u",
481 inet_ntoa(temp_addr), (unsigned)lease);
483 timeout = t1 + start;
484 requested_ip = packet.yiaddr;
485 udhcp_run_script(&packet,
486 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
489 change_mode(LISTEN_NONE);
490 if (client_config.quit_after_lease) {
491 if (client_config.release_on_quit)
495 if (!client_config.foreground)
498 } else if (*message == DHCPNAK) {
499 /* return to init state */
500 bb_info_msg("Received DHCP NAK");
501 udhcp_run_script(&packet, "nak");
502 if (state != REQUESTING)
503 udhcp_run_script(NULL, "deconfig");
504 state = INIT_SELECTING;
508 change_mode(LISTEN_RAW);
509 sleep(3); /* avoid excessive network traffic */
512 /* case BOUND, RELEASED: - ignore all packets */
514 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
523 bb_info_msg("Received SIGTERM");
524 if (client_config.release_on_quit)
528 } else if (retval == -1 && errno == EINTR) {
529 /* a signal was caught */
531 /* An error occured */
532 bb_perror_msg("select");
538 if (client_config.pidfile)
539 remove_pidfile(client_config.pidfile);