blob: 6909e8489d4339ad5ea57a205dbd151907881beb [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/* dhcpc.c
3 *
4 * udhcp DHCP client
5 *
6 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 *
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9 */
10
Mike Frysinger7031f622006-05-08 03:20:50 +000011#include <getopt.h>
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000012#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000013
14#include "common.h"
15#include "dhcpd.h"
16#include "dhcpc.h"
17#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000018
Mike Frysinger7031f622006-05-08 03:20:50 +000019
Denis Vlasenko239369b2006-09-07 17:05:44 +000020/* 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 (!!)
24 */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000025static unsigned timeout;
26static uint32_t requested_ip; /* = 0 */
Denis Vlasenko74c9d232007-01-18 15:42:00 +000027static uint32_t server_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000028static int packet_num; /* = 0 */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000029static int sockfd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000030
31#define LISTEN_NONE 0
32#define LISTEN_KERNEL 1
33#define LISTEN_RAW 2
Denis Vlasenkofbd29182007-04-07 01:05:47 +000034static smallint listen_mode;
35
36static smallint state;
Mike Frysinger7031f622006-05-08 03:20:50 +000037
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000038struct client_config_t client_config;
39
Mike Frysinger7031f622006-05-08 03:20:50 +000040
Mike Frysinger7031f622006-05-08 03:20:50 +000041/* just a little helper */
42static void change_mode(int new_mode)
43{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000044 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000045 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Denis Vlasenkofbd29182007-04-07 01:05:47 +000046 if (sockfd >= 0) {
47 close(sockfd);
48 sockfd = -1;
49 }
Mike Frysinger7031f622006-05-08 03:20:50 +000050 listen_mode = new_mode;
51}
52
53
54/* perform a renew */
55static void perform_renew(void)
56{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000057 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000058 switch (state) {
59 case BOUND:
60 change_mode(LISTEN_KERNEL);
61 case RENEWING:
62 case REBINDING:
63 state = RENEW_REQUESTED;
64 break;
65 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000066 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000067 case REQUESTING:
68 case RELEASED:
69 change_mode(LISTEN_RAW);
70 state = INIT_SELECTING;
71 break;
72 case INIT_SELECTING:
73 break;
74 }
75
76 /* start things over */
77 packet_num = 0;
78
79 /* Kill any timeouts because the user wants this to hurry along */
80 timeout = 0;
81}
82
83
84/* perform a release */
85static void perform_release(void)
86{
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000087 char buffer[sizeof("255.255.255.255")];
Mike Frysinger7031f622006-05-08 03:20:50 +000088 struct in_addr temp_addr;
89
90 /* send release packet */
91 if (state == BOUND || state == RENEWING || state == REBINDING) {
92 temp_addr.s_addr = server_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000093 strcpy(buffer, inet_ntoa(temp_addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000094 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000095 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000096 inet_ntoa(temp_addr), buffer);
97 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +000098 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000099 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000100 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000101
102 change_mode(LISTEN_NONE);
103 state = RELEASED;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000104 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +0000105}
106
107
108static void client_background(void)
109{
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000110#ifdef __uClinux__
111 bb_error_msg("cannot background in uclinux (yet)");
112/* ... mainly because udhcpc calls client_background()
113 * in _the _middle _of _udhcpc _run_, not at the start!
114 * If that will be properly disabled for NOMMU, client_background()
115 * will work on NOMMU too */
116#else
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000117 bb_daemonize(0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000118 logmode &= ~LOGMODE_STDIO;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000119 /* rewrite pidfile, as our pid is different now */
120 if (client_config.pidfile)
121 write_pidfile(client_config.pidfile);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000122#endif
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000123 /* Do not fork again. */
124 client_config.foreground = 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000125 client_config.background_if_no_lease = 0;
126}
127
128
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000129static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
130{
131 uint8_t *storage;
132 int len = strlen(str);
133 if (len > 255) len = 255;
134 storage = xzalloc(len + extra + OPT_DATA);
135 storage[OPT_CODE] = code;
136 storage[OPT_LEN] = len + extra;
137 memcpy(storage + extra + OPT_DATA, str, len);
138 return storage;
139}
140
141
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000142int udhcpc_main(int argc, char **argv);
143int udhcpc_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000144{
145 uint8_t *temp, *message;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000146 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000147 uint32_t xid = 0;
148 uint32_t lease = 0; /* can be given as 32-bit quantity */
149 unsigned t1 = 0, t2 = 0;
150 unsigned start = 0;
151 unsigned now;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000152 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000153 int max_fd;
154 int sig;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000155 int retval;
156 int len;
Mike Frysinger7031f622006-05-08 03:20:50 +0000157 int no_clientid = 0;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000158 fd_set rfds;
159 struct timeval tv;
160 struct dhcpMessage packet;
161 struct in_addr temp_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000162
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000163 enum {
164 OPT_c = 1 << 0,
165 OPT_C = 1 << 1,
166 OPT_V = 1 << 2,
167 OPT_f = 1 << 3,
168 OPT_b = 1 << 4,
169 OPT_H = 1 << 5,
170 OPT_h = 1 << 6,
171 OPT_F = 1 << 7,
172 OPT_i = 1 << 8,
173 OPT_n = 1 << 9,
174 OPT_p = 1 << 10,
175 OPT_q = 1 << 11,
176 OPT_R = 1 << 12,
177 OPT_r = 1 << 13,
178 OPT_s = 1 << 14,
179 OPT_T = 1 << 15,
180 OPT_t = 1 << 16,
181 OPT_v = 1 << 17,
182 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000183#if ENABLE_GETOPT_LONG
Mike Frysinger7031f622006-05-08 03:20:50 +0000184 static const struct option arg_options[] = {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000185 { "clientid", required_argument, 0, 'c' },
186 { "clientid-none", no_argument, 0, 'C' },
187 { "vendorclass", required_argument, 0, 'V' },
188 { "foreground", no_argument, 0, 'f' },
189 { "background", no_argument, 0, 'b' },
190 { "hostname", required_argument, 0, 'H' },
191 { "hostname", required_argument, 0, 'h' },
192 { "fqdn", required_argument, 0, 'F' },
193 { "interface", required_argument, 0, 'i' },
194 { "now", no_argument, 0, 'n' },
195 { "pidfile", required_argument, 0, 'p' },
196 { "quit", no_argument, 0, 'q' },
197 { "release", no_argument, 0, 'R' },
198 { "request", required_argument, 0, 'r' },
199 { "script", required_argument, 0, 's' },
200 { "timeout", required_argument, 0, 'T' },
201 { "version", no_argument, 0, 'v' },
202 { "retries", required_argument, 0, 't' },
203 { 0, 0, 0, 0 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000204 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000205#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000206 /* Default options. */
207 client_config.interface = "eth0";
208 client_config.script = DEFAULT_SCRIPT;
209 client_config.retries = 3;
210 client_config.timeout = 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000211
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000212 /* Parse command line */
213 opt_complementary = "?:c--C:C--c" // mutually exclusive
214 ":hH:Hh"; // -h and -H are the same
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000215#if ENABLE_GETOPT_LONG
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000216 applet_long_options = arg_options;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000217#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000218 opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
219 &str_c, &str_V, &str_h, &str_h, &str_F,
220 &client_config.interface, &client_config.pidfile, &str_r,
221 &client_config.script, &str_T, &str_t
222 );
223
224 if (opt & OPT_c)
225 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
226 if (opt & OPT_C)
227 no_clientid = 1;
228 if (opt & OPT_V)
229 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
230 if (opt & OPT_f)
231 client_config.foreground = 1;
232 if (opt & OPT_b)
233 client_config.background_if_no_lease = 1;
234 if (opt & OPT_h)
235 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
236 if (opt & OPT_F) {
237 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
238 /* Flags: 0000NEOS
239 S: 1 => Client requests Server to update A RR in DNS as well as PTR
240 O: 1 => Server indicates to client that DNS has been updated regardless
241 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
242 N: 1 => Client requests Server to not update DNS
243 */
244 client_config.fqdn[OPT_DATA + 0] = 0x1;
245 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
246 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
247 }
248 // if (opt & OPT_i) client_config.interface = ...
249 if (opt & OPT_n)
250 client_config.abort_if_no_lease = 1;
251 // if (opt & OPT_p) client_config.pidfile = ...
252 if (opt & OPT_q)
253 client_config.quit_after_lease = 1;
254 if (opt & OPT_R)
255 client_config.release_on_quit = 1;
256 if (opt & OPT_r)
257 requested_ip = inet_addr(str_r);
258 // if (opt & OPT_s) client_config.script = ...
259 if (opt & OPT_T)
260 client_config.timeout = xatoi_u(str_T);
261 if (opt & OPT_t)
262 client_config.retries = xatoi_u(str_t);
263 if (opt & OPT_v) {
264 printf("version %s\n\n", BB_VER);
265 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000266 }
267
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000268 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
269 openlog(applet_name, LOG_PID, LOG_LOCAL0);
270 logmode |= LOGMODE_SYSLOG;
271 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000272
273 if (read_interface(client_config.interface, &client_config.ifindex,
274 NULL, client_config.arp) < 0)
275 return 1;
276
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000277 /* Sanitize fd's and write pidfile */
278 udhcp_make_pidfile(client_config.pidfile);
279
Mike Frysinger7031f622006-05-08 03:20:50 +0000280 /* if not set, and not suppressed, setup the default client ID */
281 if (!client_config.clientid && !no_clientid) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000282 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000283 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000284 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000285 }
286
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000287 if (!client_config.vendorclass)
288 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000289
290 /* setup the signal pipe */
291 udhcp_sp_setup();
292
293 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000294 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000295 change_mode(LISTEN_RAW);
296
297 for (;;) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000298 tv.tv_sec = timeout - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000299 tv.tv_usec = 0;
300
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000301 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000302 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000303 sockfd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000304 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000305 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000306 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000307 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000308
309 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000310 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000311 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
312 } else retval = 0; /* If we already timed out, fall through */
313
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000314 now = monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000315 if (retval == 0) {
316 /* timeout dropped to zero */
317 switch (state) {
318 case INIT_SELECTING:
319 if (packet_num < client_config.retries) {
320 if (packet_num == 0)
321 xid = random_xid();
322
323 /* send discover packet */
324 send_discover(xid, requested_ip); /* broadcast */
325
326 timeout = now + client_config.timeout;
327 packet_num++;
328 } else {
Rob Landley3f785612006-05-28 01:06:36 +0000329 udhcp_run_script(NULL, "leasefail");
Mike Frysinger7031f622006-05-08 03:20:50 +0000330 if (client_config.background_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000331 bb_info_msg("No lease, forking to background");
Mike Frysinger7031f622006-05-08 03:20:50 +0000332 client_background();
333 } else if (client_config.abort_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000334 bb_info_msg("No lease, failing");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000335 retval = 1;
336 goto ret;
Mike Frysinger7031f622006-05-08 03:20:50 +0000337 }
338 /* wait to try again */
339 packet_num = 0;
340 timeout = now + 60;
341 }
342 break;
343 case RENEW_REQUESTED:
344 case REQUESTING:
345 if (packet_num < client_config.retries) {
346 /* send request packet */
347 if (state == RENEW_REQUESTED)
348 send_renew(xid, server_addr, requested_ip); /* unicast */
349 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
350
351 timeout = now + ((packet_num == 2) ? 10 : 2);
352 packet_num++;
353 } else {
354 /* timed out, go back to init state */
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000355 if (state == RENEW_REQUESTED)
356 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000357 state = INIT_SELECTING;
358 timeout = now;
359 packet_num = 0;
360 change_mode(LISTEN_RAW);
361 }
362 break;
363 case BOUND:
364 /* Lease is starting to run out, time to enter renewing state */
365 state = RENEWING;
366 change_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000367 DEBUG("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000368 /* fall right through */
369 case RENEWING:
370 /* Either set a new T1, or enter REBINDING state */
371 if ((t2 - t1) <= (lease / 14400 + 1)) {
372 /* timed out, enter rebinding state */
373 state = REBINDING;
374 timeout = now + (t2 - t1);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000375 DEBUG("Entering rebinding state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000376 } else {
377 /* send a request packet */
378 send_renew(xid, server_addr, requested_ip); /* unicast */
379
380 t1 = (t2 - t1) / 2 + t1;
381 timeout = t1 + start;
382 }
383 break;
384 case REBINDING:
385 /* Either set a new T2, or enter INIT state */
386 if ((lease - t2) <= (lease / 14400 + 1)) {
387 /* timed out, enter init state */
388 state = INIT_SELECTING;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000389 bb_info_msg("Lease lost, entering init state");
Rob Landley3f785612006-05-28 01:06:36 +0000390 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000391 timeout = now;
392 packet_num = 0;
393 change_mode(LISTEN_RAW);
394 } else {
395 /* send a request packet */
396 send_renew(xid, 0, requested_ip); /* broadcast */
397
398 t2 = (lease - t2) / 2 + t2;
399 timeout = t2 + start;
400 }
401 break;
402 case RELEASED:
403 /* yah, I know, *you* say it would never happen */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000404 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +0000405 break;
406 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000407 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000408 /* a packet is ready, read it */
409
410 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000411 len = udhcp_get_packet(&packet, sockfd);
412 else len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000413
414 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000415 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000416 change_mode(listen_mode); /* just close and reopen */
417 }
418 if (len < 0) continue;
419
420 if (packet.xid != xid) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000421 DEBUG("Ignoring XID %x (our xid is %x)",
422 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000423 continue;
424 }
425
426 /* Ignore packets that aren't for us */
427 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000428 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000429 continue;
430 }
431
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000432 message = get_option(&packet, DHCP_MESSAGE_TYPE);
433 if (message == NULL) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000434 bb_error_msg("cannot get option from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000435 continue;
436 }
437
438 switch (state) {
439 case INIT_SELECTING:
440 /* Must be a DHCPOFFER to one of our xid's */
441 if (*message == DHCPOFFER) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000442 temp = get_option(&packet, DHCP_SERVER_ID);
443 if (temp) {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000444 /* can be misaligned, thus memcpy */
445 memcpy(&server_addr, temp, 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000446 xid = packet.xid;
447 requested_ip = packet.yiaddr;
448
449 /* enter requesting state */
450 state = REQUESTING;
451 timeout = now;
452 packet_num = 0;
453 } else {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000454 bb_error_msg("no server ID in message");
Mike Frysinger7031f622006-05-08 03:20:50 +0000455 }
456 }
457 break;
458 case RENEW_REQUESTED:
459 case REQUESTING:
460 case RENEWING:
461 case REBINDING:
462 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000463 temp = get_option(&packet, DHCP_LEASE_TIME);
464 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000465 bb_error_msg("no lease time with ACK, using 1 hour lease");
Mike Frysinger7031f622006-05-08 03:20:50 +0000466 lease = 60 * 60;
467 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000468 /* can be misaligned, thus memcpy */
469 memcpy(&lease, temp, 4);
470 lease = ntohl(lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000471 }
472
473 /* enter bound state */
474 t1 = lease / 2;
475
476 /* little fixed point for n * .875 */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000477 t2 = (lease * 7) >> 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000478 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000479 bb_info_msg("Lease of %s obtained, lease time %u",
480 inet_ntoa(temp_addr), (unsigned)lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000481 start = now;
482 timeout = t1 + start;
483 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000484 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000485 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
486
487 state = BOUND;
488 change_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000489 if (client_config.quit_after_lease) {
490 if (client_config.release_on_quit)
491 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000492 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000493 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000494 if (!client_config.foreground)
495 client_background();
496
497 } else if (*message == DHCPNAK) {
498 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000499 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000500 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000501 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000502 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000503 state = INIT_SELECTING;
504 timeout = now;
505 requested_ip = 0;
506 packet_num = 0;
507 change_mode(LISTEN_RAW);
508 sleep(3); /* avoid excessive network traffic */
509 }
510 break;
511 /* case BOUND, RELEASED: - ignore all packets */
512 }
513 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
514 switch (sig) {
515 case SIGUSR1:
516 perform_renew();
517 break;
518 case SIGUSR2:
519 perform_release();
520 break;
521 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000522 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000523 if (client_config.release_on_quit)
524 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000525 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000526 }
527 } else if (retval == -1 && errno == EINTR) {
528 /* a signal was caught */
529 } else {
530 /* An error occured */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000531 bb_perror_msg("select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000532 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000533 } /* for (;;) */
534 ret0:
535 retval = 0;
536 ret:
537 if (client_config.pidfile)
538 remove_pidfile(client_config.pidfile);
539 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000540}