blob: efe208814ce595846ea0b03886d47147755584d4 [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 Vlasenko80edead2007-08-02 22:31:05 +0000112#if !BB_MMU
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000113 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 */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000122 write_pidfile(client_config.pidfile);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000123#endif
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000124 /* Do not fork again. */
125 client_config.foreground = 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000126 client_config.background_if_no_lease = 0;
127}
128
129
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000130static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
131{
132 uint8_t *storage;
133 int len = strlen(str);
134 if (len > 255) len = 255;
135 storage = xzalloc(len + extra + OPT_DATA);
136 storage[OPT_CODE] = code;
137 storage[OPT_LEN] = len + extra;
138 memcpy(storage + extra + OPT_DATA, str, len);
139 return storage;
140}
141
142
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000143int udhcpc_main(int argc, char **argv);
144int udhcpc_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000145{
146 uint8_t *temp, *message;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000147 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000148 uint32_t xid = 0;
149 uint32_t lease = 0; /* can be given as 32-bit quantity */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000150 unsigned t1 = 0, t2 = 0; /* what a wonderful names */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000151 unsigned start = 0;
152 unsigned now;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000153 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000154 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000155 int retval;
156 int len;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000157 struct timeval tv;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000158 struct in_addr temp_addr;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000159 struct dhcpMessage packet;
160 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000161
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000162 enum {
163 OPT_c = 1 << 0,
164 OPT_C = 1 << 1,
165 OPT_V = 1 << 2,
166 OPT_f = 1 << 3,
167 OPT_b = 1 << 4,
168 OPT_H = 1 << 5,
169 OPT_h = 1 << 6,
170 OPT_F = 1 << 7,
171 OPT_i = 1 << 8,
172 OPT_n = 1 << 9,
173 OPT_p = 1 << 10,
174 OPT_q = 1 << 11,
175 OPT_R = 1 << 12,
176 OPT_r = 1 << 13,
177 OPT_s = 1 << 14,
178 OPT_T = 1 << 15,
179 OPT_t = 1 << 16,
180 OPT_v = 1 << 17,
181 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000182#if ENABLE_GETOPT_LONG
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000183 static const char udhcpc_longopts[] =
184 "clientid\0" Required_argument "c"
185 "clientid-none\0" No_argument "C"
186 "vendorclass\0" Required_argument "V"
187 "foreground\0" No_argument "f"
188 "background\0" No_argument "b"
189 "hostname\0" Required_argument "H"
190 "hostname\0" Required_argument "h"
191 "fqdn\0" Required_argument "F"
192 "interface\0" Required_argument "i"
193 "now\0" No_argument "n"
194 "pidfile\0" Required_argument "p"
195 "quit\0" No_argument "q"
196 "release\0" No_argument "R"
197 "request\0" Required_argument "r"
198 "script\0" Required_argument "s"
199 "timeout\0" Required_argument "T"
200 "version\0" No_argument "v"
201 "retries\0" Required_argument "t"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000202 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000203#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000204 /* Default options. */
205 client_config.interface = "eth0";
206 client_config.script = DEFAULT_SCRIPT;
207 client_config.retries = 3;
208 client_config.timeout = 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000209
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000210 /* Parse command line */
Denis Vlasenko09196572007-07-21 13:27:44 +0000211 opt_complementary = "c--C:C--c" // mutually exclusive
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000212 ":hH:Hh"; // -h and -H are the same
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000213#if ENABLE_GETOPT_LONG
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000214 applet_long_options = udhcpc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000215#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000216 opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
217 &str_c, &str_V, &str_h, &str_h, &str_F,
218 &client_config.interface, &client_config.pidfile, &str_r,
219 &client_config.script, &str_T, &str_t
220 );
221
222 if (opt & OPT_c)
223 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000224 //if (opt & OPT_C)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000225 if (opt & OPT_V)
226 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
227 if (opt & OPT_f)
228 client_config.foreground = 1;
229 if (opt & OPT_b)
230 client_config.background_if_no_lease = 1;
231 if (opt & OPT_h)
232 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
233 if (opt & OPT_F) {
234 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
235 /* Flags: 0000NEOS
236 S: 1 => Client requests Server to update A RR in DNS as well as PTR
237 O: 1 => Server indicates to client that DNS has been updated regardless
238 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
239 N: 1 => Client requests Server to not update DNS
240 */
241 client_config.fqdn[OPT_DATA + 0] = 0x1;
242 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
243 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
244 }
245 // if (opt & OPT_i) client_config.interface = ...
246 if (opt & OPT_n)
247 client_config.abort_if_no_lease = 1;
248 // if (opt & OPT_p) client_config.pidfile = ...
249 if (opt & OPT_q)
250 client_config.quit_after_lease = 1;
251 if (opt & OPT_R)
252 client_config.release_on_quit = 1;
253 if (opt & OPT_r)
254 requested_ip = inet_addr(str_r);
255 // if (opt & OPT_s) client_config.script = ...
256 if (opt & OPT_T)
257 client_config.timeout = xatoi_u(str_T);
258 if (opt & OPT_t)
259 client_config.retries = xatoi_u(str_t);
260 if (opt & OPT_v) {
Denis Vlasenko80edead2007-08-02 22:31:05 +0000261 printf("version %s\n", BB_VER);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000262 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000263 }
264
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000265 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
266 openlog(applet_name, LOG_PID, LOG_LOCAL0);
267 logmode |= LOGMODE_SYSLOG;
268 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000269
270 if (read_interface(client_config.interface, &client_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +0000271 NULL, client_config.arp))
Mike Frysinger7031f622006-05-08 03:20:50 +0000272 return 1;
273
Denis Vlasenko80edead2007-08-02 22:31:05 +0000274 /* Make sure fd 0,1,2 are open */
275 bb_sanitize_stdio();
276 /* Equivalent of doing a fflush after every \n */
277 setlinebuf(stdout);
278
279 /* Create pidfile */
280 write_pidfile(client_config.pidfile);
281 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
282
283 /* Goes to stdout and possibly syslog */
284 bb_info_msg("%s (v%s) started", applet_name, BB_VER);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000285
Mike Frysinger7031f622006-05-08 03:20:50 +0000286 /* if not set, and not suppressed, setup the default client ID */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000287 if (!client_config.clientid && !(opt & OPT_C)) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000288 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000289 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000290 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000291 }
292
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000293 if (!client_config.vendorclass)
294 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000295
296 /* setup the signal pipe */
297 udhcp_sp_setup();
298
299 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000300 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000301 change_mode(LISTEN_RAW);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000302 tv.tv_sec = 0;
303 goto jump_in;
Mike Frysinger7031f622006-05-08 03:20:50 +0000304
305 for (;;) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000306 tv.tv_sec = timeout - monotonic_sec();
Denis Vlasenko80edead2007-08-02 22:31:05 +0000307 jump_in:
Mike Frysinger7031f622006-05-08 03:20:50 +0000308 tv.tv_usec = 0;
309
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000310 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000311 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000312 sockfd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000313 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000314 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000315 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000316 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000317
Denis Vlasenko80edead2007-08-02 22:31:05 +0000318 retval = 0; /* If we already timed out, fall through, else... */
Mike Frysinger7031f622006-05-08 03:20:50 +0000319 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000320 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000321 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000322 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000323
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000324 now = monotonic_sec();
Denis Vlasenko80edead2007-08-02 22:31:05 +0000325 if (retval < 0) {
326 /* EINTR? signal was caught, don't panic */
327 if (errno != EINTR) {
328 /* Else: an error occured, panic! */
329 bb_perror_msg_and_die("select");
330 }
331 } else if (retval == 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000332 /* timeout dropped to zero */
333 switch (state) {
334 case INIT_SELECTING:
335 if (packet_num < client_config.retries) {
336 if (packet_num == 0)
337 xid = random_xid();
338
339 /* send discover packet */
340 send_discover(xid, requested_ip); /* broadcast */
341
342 timeout = now + client_config.timeout;
343 packet_num++;
344 } else {
Rob Landley3f785612006-05-28 01:06:36 +0000345 udhcp_run_script(NULL, "leasefail");
Mike Frysinger7031f622006-05-08 03:20:50 +0000346 if (client_config.background_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000347 bb_info_msg("No lease, forking to background");
Mike Frysinger7031f622006-05-08 03:20:50 +0000348 client_background();
349 } else if (client_config.abort_if_no_lease) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000350 bb_info_msg("No lease, failing");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000351 retval = 1;
352 goto ret;
Mike Frysinger7031f622006-05-08 03:20:50 +0000353 }
354 /* wait to try again */
355 packet_num = 0;
356 timeout = now + 60;
357 }
358 break;
359 case RENEW_REQUESTED:
360 case REQUESTING:
361 if (packet_num < client_config.retries) {
362 /* send request packet */
363 if (state == RENEW_REQUESTED)
364 send_renew(xid, server_addr, requested_ip); /* unicast */
365 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
366
367 timeout = now + ((packet_num == 2) ? 10 : 2);
368 packet_num++;
369 } else {
370 /* timed out, go back to init state */
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000371 if (state == RENEW_REQUESTED)
372 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000373 state = INIT_SELECTING;
374 timeout = now;
375 packet_num = 0;
376 change_mode(LISTEN_RAW);
377 }
378 break;
379 case BOUND:
380 /* Lease is starting to run out, time to enter renewing state */
381 state = RENEWING;
382 change_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000383 DEBUG("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000384 /* fall right through */
385 case RENEWING:
386 /* Either set a new T1, or enter REBINDING state */
387 if ((t2 - t1) <= (lease / 14400 + 1)) {
388 /* timed out, enter rebinding state */
389 state = REBINDING;
390 timeout = now + (t2 - t1);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000391 DEBUG("Entering rebinding state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000392 } else {
393 /* send a request packet */
394 send_renew(xid, server_addr, requested_ip); /* unicast */
Mike Frysinger7031f622006-05-08 03:20:50 +0000395 t1 = (t2 - t1) / 2 + t1;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000396 timeout = start + t1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000397 }
398 break;
399 case REBINDING:
400 /* Either set a new T2, or enter INIT state */
401 if ((lease - t2) <= (lease / 14400 + 1)) {
402 /* timed out, enter init state */
403 state = INIT_SELECTING;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000404 bb_info_msg("Lease lost, entering init state");
Rob Landley3f785612006-05-28 01:06:36 +0000405 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000406 timeout = now;
407 packet_num = 0;
408 change_mode(LISTEN_RAW);
409 } else {
410 /* send a request packet */
411 send_renew(xid, 0, requested_ip); /* broadcast */
Mike Frysinger7031f622006-05-08 03:20:50 +0000412 t2 = (lease - t2) / 2 + t2;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000413 timeout = start + t2;
Mike Frysinger7031f622006-05-08 03:20:50 +0000414 }
415 break;
416 case RELEASED:
417 /* yah, I know, *you* say it would never happen */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000418 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +0000419 break;
420 }
Denis Vlasenko80edead2007-08-02 22:31:05 +0000421 } else if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000422 /* a packet is ready, read it */
423
424 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000425 len = udhcp_get_packet(&packet, sockfd);
426 else len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000427
428 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000429 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000430 change_mode(listen_mode); /* just close and reopen */
431 }
432 if (len < 0) continue;
433
434 if (packet.xid != xid) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000435 DEBUG("Ignoring XID %x (our xid is %x)",
436 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000437 continue;
438 }
439
440 /* Ignore packets that aren't for us */
441 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000442 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000443 continue;
444 }
445
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000446 message = get_option(&packet, DHCP_MESSAGE_TYPE);
447 if (message == NULL) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000448 bb_error_msg("cannot get option from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000449 continue;
450 }
451
452 switch (state) {
453 case INIT_SELECTING:
454 /* Must be a DHCPOFFER to one of our xid's */
455 if (*message == DHCPOFFER) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000456 temp = get_option(&packet, DHCP_SERVER_ID);
457 if (temp) {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000458 /* can be misaligned, thus memcpy */
459 memcpy(&server_addr, temp, 4);
Mike Frysinger7031f622006-05-08 03:20:50 +0000460 xid = packet.xid;
461 requested_ip = packet.yiaddr;
462
463 /* enter requesting state */
464 state = REQUESTING;
465 timeout = now;
466 packet_num = 0;
467 } else {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000468 bb_error_msg("no server ID in message");
Mike Frysinger7031f622006-05-08 03:20:50 +0000469 }
470 }
471 break;
472 case RENEW_REQUESTED:
473 case REQUESTING:
474 case RENEWING:
475 case REBINDING:
476 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000477 temp = get_option(&packet, DHCP_LEASE_TIME);
478 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000479 bb_error_msg("no lease time with ACK, using 1 hour lease");
Mike Frysinger7031f622006-05-08 03:20:50 +0000480 lease = 60 * 60;
481 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000482 /* can be misaligned, thus memcpy */
483 memcpy(&lease, temp, 4);
484 lease = ntohl(lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000485 }
486
487 /* enter bound state */
488 t1 = lease / 2;
489
490 /* little fixed point for n * .875 */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000491 t2 = (lease * 7) >> 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000492 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000493 bb_info_msg("Lease of %s obtained, lease time %u",
494 inet_ntoa(temp_addr), (unsigned)lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000495 start = now;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000496 timeout = start + t1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000497 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000498 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000499 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
500
501 state = BOUND;
502 change_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000503 if (client_config.quit_after_lease) {
504 if (client_config.release_on_quit)
505 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000506 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000507 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000508 if (!client_config.foreground)
509 client_background();
510
511 } else if (*message == DHCPNAK) {
512 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000513 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000514 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000515 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000516 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000517 state = INIT_SELECTING;
518 timeout = now;
519 requested_ip = 0;
520 packet_num = 0;
521 change_mode(LISTEN_RAW);
522 sleep(3); /* avoid excessive network traffic */
523 }
524 break;
525 /* case BOUND, RELEASED: - ignore all packets */
526 }
Denis Vlasenko80edead2007-08-02 22:31:05 +0000527 } else {
528 int signo = udhcp_sp_read(&rfds);
529 switch (signo) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000530 case SIGUSR1:
531 perform_renew();
532 break;
533 case SIGUSR2:
534 perform_release();
535 break;
536 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000537 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000538 if (client_config.release_on_quit)
539 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000540 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000541 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000542 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000543 } /* for (;;) */
544 ret0:
545 retval = 0;
546 ret:
547 if (client_config.pidfile)
548 remove_pidfile(client_config.pidfile);
549 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000550}