blob: 4bb90c2daf4ac2578d84e615bdc0312c8c8c99f9 [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
Denis Vlasenko1caca342007-08-02 10:14:29 +000014/* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
15#define WANT_PIDFILE 1
Mike Frysinger7031f622006-05-08 03:20:50 +000016#include "common.h"
17#include "dhcpd.h"
18#include "dhcpc.h"
19#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000020
Mike Frysinger7031f622006-05-08 03:20:50 +000021
Denis Vlasenko239369b2006-09-07 17:05:44 +000022/* 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 (!!)
26 */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000027static unsigned timeout;
28static uint32_t requested_ip; /* = 0 */
Denis Vlasenko74c9d232007-01-18 15:42:00 +000029static uint32_t server_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000030static int packet_num; /* = 0 */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000031static int sockfd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000032
33#define LISTEN_NONE 0
34#define LISTEN_KERNEL 1
35#define LISTEN_RAW 2
Denis Vlasenkofbd29182007-04-07 01:05:47 +000036static smallint listen_mode;
37
38static smallint state;
Mike Frysinger7031f622006-05-08 03:20:50 +000039
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000040struct client_config_t client_config;
41
Mike Frysinger7031f622006-05-08 03:20:50 +000042
Mike Frysinger7031f622006-05-08 03:20:50 +000043/* just a little helper */
44static void change_mode(int new_mode)
45{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000046 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000047 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Denis Vlasenkofbd29182007-04-07 01:05:47 +000048 if (sockfd >= 0) {
49 close(sockfd);
50 sockfd = -1;
51 }
Mike Frysinger7031f622006-05-08 03:20:50 +000052 listen_mode = new_mode;
53}
54
55
56/* perform a renew */
57static void perform_renew(void)
58{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000059 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000060 switch (state) {
61 case BOUND:
62 change_mode(LISTEN_KERNEL);
63 case RENEWING:
64 case REBINDING:
65 state = RENEW_REQUESTED;
66 break;
67 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000068 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000069 case REQUESTING:
70 case RELEASED:
71 change_mode(LISTEN_RAW);
72 state = INIT_SELECTING;
73 break;
74 case INIT_SELECTING:
75 break;
76 }
77
78 /* start things over */
79 packet_num = 0;
80
81 /* Kill any timeouts because the user wants this to hurry along */
82 timeout = 0;
83}
84
85
86/* perform a release */
87static void perform_release(void)
88{
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000089 char buffer[sizeof("255.255.255.255")];
Mike Frysinger7031f622006-05-08 03:20:50 +000090 struct in_addr temp_addr;
91
92 /* send release packet */
93 if (state == BOUND || state == RENEWING || state == REBINDING) {
94 temp_addr.s_addr = server_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000095 strcpy(buffer, inet_ntoa(temp_addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000096 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000097 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000098 inet_ntoa(temp_addr), buffer);
99 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +0000100 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000101 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000102 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000103
104 change_mode(LISTEN_NONE);
105 state = RELEASED;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000106 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +0000107}
108
109
110static void client_background(void)
111{
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000112#ifdef __uClinux__
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 */
118#else
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000119 bb_daemonize(0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000120 logmode &= ~LOGMODE_STDIO;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000121 /* rewrite pidfile, as our pid is different now */
122 if (client_config.pidfile)
123 write_pidfile(client_config.pidfile);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000124#endif
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000125 /* Do not fork again. */
126 client_config.foreground = 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000127 client_config.background_if_no_lease = 0;
128}
129
130
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000131static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
132{
133 uint8_t *storage;
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);
140 return storage;
141}
142
143
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000144int udhcpc_main(int argc, char **argv);
145int udhcpc_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000146{
147 uint8_t *temp, *message;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000148 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000149 uint32_t xid = 0;
150 uint32_t lease = 0; /* can be given as 32-bit quantity */
151 unsigned t1 = 0, t2 = 0;
152 unsigned start = 0;
153 unsigned now;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000154 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000155 int max_fd;
156 int sig;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000157 int retval;
158 int len;
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 int no_clientid = 0;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000160 fd_set rfds;
161 struct timeval tv;
162 struct dhcpMessage packet;
163 struct in_addr temp_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000164
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000165 enum {
166 OPT_c = 1 << 0,
167 OPT_C = 1 << 1,
168 OPT_V = 1 << 2,
169 OPT_f = 1 << 3,
170 OPT_b = 1 << 4,
171 OPT_H = 1 << 5,
172 OPT_h = 1 << 6,
173 OPT_F = 1 << 7,
174 OPT_i = 1 << 8,
175 OPT_n = 1 << 9,
176 OPT_p = 1 << 10,
177 OPT_q = 1 << 11,
178 OPT_R = 1 << 12,
179 OPT_r = 1 << 13,
180 OPT_s = 1 << 14,
181 OPT_T = 1 << 15,
182 OPT_t = 1 << 16,
183 OPT_v = 1 << 17,
184 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000185#if ENABLE_GETOPT_LONG
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000186 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"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000205 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000206#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000207 /* Default options. */
208 client_config.interface = "eth0";
209 client_config.script = DEFAULT_SCRIPT;
210 client_config.retries = 3;
211 client_config.timeout = 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000212
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000213 /* Parse command line */
Denis Vlasenko09196572007-07-21 13:27:44 +0000214 opt_complementary = "c--C:C--c" // mutually exclusive
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000215 ":hH:Hh"; // -h and -H are the same
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000216#if ENABLE_GETOPT_LONG
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000217 applet_long_options = udhcpc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000218#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000219 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
223 );
224
225 if (opt & OPT_c)
226 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
227 if (opt & OPT_C)
228 no_clientid = 1;
229 if (opt & OPT_V)
230 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
231 if (opt & OPT_f)
232 client_config.foreground = 1;
233 if (opt & OPT_b)
234 client_config.background_if_no_lease = 1;
235 if (opt & OPT_h)
236 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
237 if (opt & OPT_F) {
238 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
239 /* Flags: 0000NEOS
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
244 */
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 */
248 }
249 // if (opt & OPT_i) client_config.interface = ...
250 if (opt & OPT_n)
251 client_config.abort_if_no_lease = 1;
252 // if (opt & OPT_p) client_config.pidfile = ...
253 if (opt & OPT_q)
254 client_config.quit_after_lease = 1;
255 if (opt & OPT_R)
256 client_config.release_on_quit = 1;
257 if (opt & OPT_r)
258 requested_ip = inet_addr(str_r);
259 // if (opt & OPT_s) client_config.script = ...
260 if (opt & OPT_T)
261 client_config.timeout = xatoi_u(str_T);
262 if (opt & OPT_t)
263 client_config.retries = xatoi_u(str_t);
264 if (opt & OPT_v) {
265 printf("version %s\n\n", BB_VER);
266 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000267 }
268
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000269 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
270 openlog(applet_name, LOG_PID, LOG_LOCAL0);
271 logmode |= LOGMODE_SYSLOG;
272 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000273
274 if (read_interface(client_config.interface, &client_config.ifindex,
275 NULL, client_config.arp) < 0)
276 return 1;
277
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000278 /* Sanitize fd's and write pidfile */
279 udhcp_make_pidfile(client_config.pidfile);
280
Mike Frysinger7031f622006-05-08 03:20:50 +0000281 /* if not set, and not suppressed, setup the default client ID */
282 if (!client_config.clientid && !no_clientid) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000283 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000284 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000285 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000286 }
287
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000288 if (!client_config.vendorclass)
289 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000290
291 /* setup the signal pipe */
292 udhcp_sp_setup();
293
294 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000295 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000296 change_mode(LISTEN_RAW);
297
298 for (;;) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000299 tv.tv_sec = timeout - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000300 tv.tv_usec = 0;
301
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000302 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000303 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000304 sockfd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000305 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000306 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000307 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000308 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000309
310 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000311 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000312 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
313 } else retval = 0; /* If we already timed out, fall through */
314
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000315 now = monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000316 if (retval == 0) {
317 /* timeout dropped to zero */
318 switch (state) {
319 case INIT_SELECTING:
320 if (packet_num < client_config.retries) {
321 if (packet_num == 0)
322 xid = random_xid();
323
324 /* send discover packet */
325 send_discover(xid, requested_ip); /* broadcast */
326
327 timeout = now + client_config.timeout;
328 packet_num++;
329 } else {
Rob Landley3f785612006-05-28 01:06:36 +0000330 udhcp_run_script(NULL, "leasefail");
Mike Frysinger7031f622006-05-08 03:20:50 +0000331 if (client_config.background_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000332 bb_info_msg("No lease, forking to background");
Mike Frysinger7031f622006-05-08 03:20:50 +0000333 client_background();
334 } else if (client_config.abort_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000335 bb_info_msg("No lease, failing");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000336 retval = 1;
337 goto ret;
Mike Frysinger7031f622006-05-08 03:20:50 +0000338 }
339 /* wait to try again */
340 packet_num = 0;
341 timeout = now + 60;
342 }
343 break;
344 case RENEW_REQUESTED:
345 case REQUESTING:
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 */
351
352 timeout = now + ((packet_num == 2) ? 10 : 2);
353 packet_num++;
354 } else {
355 /* timed out, go back to init state */
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000356 if (state == RENEW_REQUESTED)
357 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000358 state = INIT_SELECTING;
359 timeout = now;
360 packet_num = 0;
361 change_mode(LISTEN_RAW);
362 }
363 break;
364 case BOUND:
365 /* Lease is starting to run out, time to enter renewing state */
366 state = RENEWING;
367 change_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000368 DEBUG("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000369 /* fall right through */
370 case RENEWING:
371 /* Either set a new T1, or enter REBINDING state */
372 if ((t2 - t1) <= (lease / 14400 + 1)) {
373 /* timed out, enter rebinding state */
374 state = REBINDING;
375 timeout = now + (t2 - t1);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000376 DEBUG("Entering rebinding state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000377 } else {
378 /* send a request packet */
379 send_renew(xid, server_addr, requested_ip); /* unicast */
380
381 t1 = (t2 - t1) / 2 + t1;
382 timeout = t1 + start;
383 }
384 break;
385 case REBINDING:
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;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000390 bb_info_msg("Lease lost, entering init state");
Rob Landley3f785612006-05-28 01:06:36 +0000391 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000392 timeout = now;
393 packet_num = 0;
394 change_mode(LISTEN_RAW);
395 } else {
396 /* send a request packet */
397 send_renew(xid, 0, requested_ip); /* broadcast */
398
399 t2 = (lease - t2) / 2 + t2;
400 timeout = t2 + start;
401 }
402 break;
403 case RELEASED:
404 /* yah, I know, *you* say it would never happen */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000405 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +0000406 break;
407 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000408 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000409 /* a packet is ready, read it */
410
411 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000412 len = udhcp_get_packet(&packet, sockfd);
413 else len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000414
415 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000416 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000417 change_mode(listen_mode); /* just close and reopen */
418 }
419 if (len < 0) continue;
420
421 if (packet.xid != xid) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000422 DEBUG("Ignoring XID %x (our xid is %x)",
423 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000424 continue;
425 }
426
427 /* Ignore packets that aren't for us */
428 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000429 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000430 continue;
431 }
432
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000433 message = get_option(&packet, DHCP_MESSAGE_TYPE);
434 if (message == NULL) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000435 bb_error_msg("cannot get option from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000436 continue;
437 }
438
439 switch (state) {
440 case INIT_SELECTING:
441 /* Must be a DHCPOFFER to one of our xid's */
442 if (*message == DHCPOFFER) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000443 temp = get_option(&packet, DHCP_SERVER_ID);
444 if (temp) {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000445 /* can be misaligned, thus memcpy */
446 memcpy(&server_addr, temp, 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000447 xid = packet.xid;
448 requested_ip = packet.yiaddr;
449
450 /* enter requesting state */
451 state = REQUESTING;
452 timeout = now;
453 packet_num = 0;
454 } else {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000455 bb_error_msg("no server ID in message");
Mike Frysinger7031f622006-05-08 03:20:50 +0000456 }
457 }
458 break;
459 case RENEW_REQUESTED:
460 case REQUESTING:
461 case RENEWING:
462 case REBINDING:
463 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000464 temp = get_option(&packet, DHCP_LEASE_TIME);
465 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000466 bb_error_msg("no lease time with ACK, using 1 hour lease");
Mike Frysinger7031f622006-05-08 03:20:50 +0000467 lease = 60 * 60;
468 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000469 /* can be misaligned, thus memcpy */
470 memcpy(&lease, temp, 4);
471 lease = ntohl(lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000472 }
473
474 /* enter bound state */
475 t1 = lease / 2;
476
477 /* little fixed point for n * .875 */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000478 t2 = (lease * 7) >> 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000479 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000480 bb_info_msg("Lease of %s obtained, lease time %u",
481 inet_ntoa(temp_addr), (unsigned)lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000482 start = now;
483 timeout = t1 + start;
484 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000485 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000486 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
487
488 state = BOUND;
489 change_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000490 if (client_config.quit_after_lease) {
491 if (client_config.release_on_quit)
492 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000493 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000494 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000495 if (!client_config.foreground)
496 client_background();
497
498 } else if (*message == DHCPNAK) {
499 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000500 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000501 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000502 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000503 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000504 state = INIT_SELECTING;
505 timeout = now;
506 requested_ip = 0;
507 packet_num = 0;
508 change_mode(LISTEN_RAW);
509 sleep(3); /* avoid excessive network traffic */
510 }
511 break;
512 /* case BOUND, RELEASED: - ignore all packets */
513 }
514 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
515 switch (sig) {
516 case SIGUSR1:
517 perform_renew();
518 break;
519 case SIGUSR2:
520 perform_release();
521 break;
522 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000523 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000524 if (client_config.release_on_quit)
525 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000526 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000527 }
528 } else if (retval == -1 && errno == EINTR) {
529 /* a signal was caught */
530 } else {
531 /* An error occured */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000532 bb_perror_msg("select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000533 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000534 } /* for (;;) */
535 ret0:
536 retval = 0;
537 ret:
538 if (client_config.pidfile)
539 remove_pidfile(client_config.pidfile);
540 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000541}