Annotation of /trunk/mkinitrd-magellan/busybox/networking/udhcp/dhcpc.c
Parent Directory | Revision Log
Revision 984 -
(hide annotations)
(download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 20082 byte(s)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 20082 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* 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 | niro | 816 | #include <syslog.h> |
11 | /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */ | ||
12 | #define WANT_PIDFILE 1 | ||
13 | niro | 532 | #include "common.h" |
14 | #include "dhcpd.h" | ||
15 | #include "dhcpc.h" | ||
16 | #include "options.h" | ||
17 | |||
18 | |||
19 | niro | 816 | static int sockfd = -1; |
20 | niro | 532 | |
21 | niro | 984 | #define LISTEN_NONE 0 |
22 | niro | 532 | #define LISTEN_KERNEL 1 |
23 | niro | 984 | #define LISTEN_RAW 2 |
24 | niro | 816 | static smallint listen_mode; |
25 | niro | 532 | |
26 | niro | 984 | /* initial state: (re)start DHCP negotiation */ |
27 | niro | 816 | #define INIT_SELECTING 0 |
28 | niro | 984 | /* discover was sent, DHCPOFFER reply received */ |
29 | niro | 816 | #define REQUESTING 1 |
30 | niro | 984 | /* select/renew was sent, DHCPACK reply received */ |
31 | niro | 816 | #define BOUND 2 |
32 | niro | 984 | /* half of lease passed, want to renew it by sending unicast renew requests */ |
33 | niro | 816 | #define RENEWING 3 |
34 | niro | 984 | /* renew requests were not answered, lease is almost over, send broadcast renew */ |
35 | niro | 816 | #define REBINDING 4 |
36 | niro | 984 | /* manually requested renew (SIGUSR1) */ |
37 | #define RENEW_REQUESTED 5 | ||
38 | /* release, possibly manually requested (SIGUSR2) */ | ||
39 | #define RELEASED 6 | ||
40 | niro | 816 | static smallint state; |
41 | niro | 532 | |
42 | niro | 816 | /* struct client_config_t client_config is in bb_common_bufsiz1 */ |
43 | niro | 532 | |
44 | niro | 816 | |
45 | niro | 532 | /* just a little helper */ |
46 | niro | 816 | static void change_listen_mode(int new_mode) |
47 | niro | 532 | { |
48 | niro | 984 | log1("Entering listen mode: %s", |
49 | new_mode != LISTEN_NONE | ||
50 | ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw") | ||
51 | : "none" | ||
52 | ); | ||
53 | |||
54 | listen_mode = new_mode; | ||
55 | niro | 816 | if (sockfd >= 0) { |
56 | close(sockfd); | ||
57 | sockfd = -1; | ||
58 | } | ||
59 | niro | 984 | if (new_mode == LISTEN_KERNEL) |
60 | sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface); | ||
61 | else if (new_mode != LISTEN_NONE) | ||
62 | sockfd = udhcp_raw_socket(client_config.ifindex); | ||
63 | /* else LISTEN_NONE: sockfd stay closed */ | ||
64 | niro | 532 | } |
65 | |||
66 | |||
67 | /* perform a renew */ | ||
68 | static void perform_renew(void) | ||
69 | { | ||
70 | bb_info_msg("Performing a DHCP renew"); | ||
71 | switch (state) { | ||
72 | case BOUND: | ||
73 | niro | 816 | change_listen_mode(LISTEN_KERNEL); |
74 | niro | 532 | case RENEWING: |
75 | case REBINDING: | ||
76 | state = RENEW_REQUESTED; | ||
77 | break; | ||
78 | case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ | ||
79 | udhcp_run_script(NULL, "deconfig"); | ||
80 | case REQUESTING: | ||
81 | case RELEASED: | ||
82 | niro | 816 | change_listen_mode(LISTEN_RAW); |
83 | niro | 532 | state = INIT_SELECTING; |
84 | break; | ||
85 | case INIT_SELECTING: | ||
86 | break; | ||
87 | } | ||
88 | } | ||
89 | |||
90 | |||
91 | /* perform a release */ | ||
92 | niro | 816 | static void perform_release(uint32_t requested_ip, uint32_t server_addr) |
93 | niro | 532 | { |
94 | niro | 816 | char buffer[sizeof("255.255.255.255")]; |
95 | niro | 532 | struct in_addr temp_addr; |
96 | |||
97 | /* send release packet */ | ||
98 | if (state == BOUND || state == RENEWING || state == REBINDING) { | ||
99 | temp_addr.s_addr = server_addr; | ||
100 | niro | 816 | strcpy(buffer, inet_ntoa(temp_addr)); |
101 | niro | 532 | temp_addr.s_addr = requested_ip; |
102 | bb_info_msg("Unicasting a release of %s to %s", | ||
103 | inet_ntoa(temp_addr), buffer); | ||
104 | send_release(server_addr, requested_ip); /* unicast */ | ||
105 | udhcp_run_script(NULL, "deconfig"); | ||
106 | } | ||
107 | bb_info_msg("Entering released state"); | ||
108 | |||
109 | niro | 816 | change_listen_mode(LISTEN_NONE); |
110 | niro | 532 | state = RELEASED; |
111 | } | ||
112 | |||
113 | |||
114 | niro | 816 | #if BB_MMU |
115 | niro | 532 | static void client_background(void) |
116 | { | ||
117 | niro | 816 | bb_daemonize(0); |
118 | logmode &= ~LOGMODE_STDIO; | ||
119 | /* rewrite pidfile, as our pid is different now */ | ||
120 | write_pidfile(client_config.pidfile); | ||
121 | niro | 532 | } |
122 | niro | 816 | #endif |
123 | niro | 532 | |
124 | |||
125 | static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) | ||
126 | { | ||
127 | uint8_t *storage; | ||
128 | int len = strlen(str); | ||
129 | if (len > 255) len = 255; | ||
130 | storage = xzalloc(len + extra + OPT_DATA); | ||
131 | storage[OPT_CODE] = code; | ||
132 | storage[OPT_LEN] = len + extra; | ||
133 | memcpy(storage + extra + OPT_DATA, str, len); | ||
134 | return storage; | ||
135 | } | ||
136 | |||
137 | |||
138 | niro | 816 | int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
139 | int udhcpc_main(int argc UNUSED_PARAM, char **argv) | ||
140 | niro | 532 | { |
141 | uint8_t *temp, *message; | ||
142 | niro | 816 | char *str_c, *str_V, *str_h, *str_F, *str_r; |
143 | niro | 984 | IF_FEATURE_UDHCP_PORT(char *str_P;) |
144 | niro | 816 | llist_t *list_O = NULL; |
145 | int tryagain_timeout = 20; | ||
146 | int discover_timeout = 3; | ||
147 | int discover_retries = 3; | ||
148 | uint32_t server_addr = server_addr; /* for compiler */ | ||
149 | uint32_t requested_ip = 0; | ||
150 | uint32_t xid = 0; | ||
151 | uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */ | ||
152 | int packet_num; | ||
153 | int timeout; /* must be signed */ | ||
154 | unsigned already_waited_sec; | ||
155 | niro | 532 | unsigned opt; |
156 | int max_fd; | ||
157 | int retval; | ||
158 | struct timeval tv; | ||
159 | niro | 984 | struct dhcp_packet packet; |
160 | niro | 816 | fd_set rfds; |
161 | niro | 532 | |
162 | niro | 984 | #if ENABLE_LONG_OPTS |
163 | niro | 816 | static const char udhcpc_longopts[] ALIGN1 = |
164 | "clientid\0" Required_argument "c" | ||
165 | "clientid-none\0" No_argument "C" | ||
166 | "vendorclass\0" Required_argument "V" | ||
167 | "hostname\0" Required_argument "H" | ||
168 | "fqdn\0" Required_argument "F" | ||
169 | "interface\0" Required_argument "i" | ||
170 | "now\0" No_argument "n" | ||
171 | "pidfile\0" Required_argument "p" | ||
172 | "quit\0" No_argument "q" | ||
173 | "release\0" No_argument "R" | ||
174 | "request\0" Required_argument "r" | ||
175 | "script\0" Required_argument "s" | ||
176 | "timeout\0" Required_argument "T" | ||
177 | "version\0" No_argument "v" | ||
178 | "retries\0" Required_argument "t" | ||
179 | "tryagain\0" Required_argument "A" | ||
180 | "syslog\0" No_argument "S" | ||
181 | "request-option\0" Required_argument "O" | ||
182 | "no-default-options\0" No_argument "o" | ||
183 | "foreground\0" No_argument "f" | ||
184 | "background\0" No_argument "b" | ||
185 | niro | 984 | IF_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a") |
186 | IF_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P") | ||
187 | niro | 816 | ; |
188 | #endif | ||
189 | niro | 532 | enum { |
190 | OPT_c = 1 << 0, | ||
191 | OPT_C = 1 << 1, | ||
192 | OPT_V = 1 << 2, | ||
193 | niro | 816 | OPT_H = 1 << 3, |
194 | OPT_h = 1 << 4, | ||
195 | OPT_F = 1 << 5, | ||
196 | OPT_i = 1 << 6, | ||
197 | OPT_n = 1 << 7, | ||
198 | OPT_p = 1 << 8, | ||
199 | OPT_q = 1 << 9, | ||
200 | OPT_R = 1 << 10, | ||
201 | OPT_r = 1 << 11, | ||
202 | OPT_s = 1 << 12, | ||
203 | OPT_T = 1 << 13, | ||
204 | OPT_t = 1 << 14, | ||
205 | niro | 984 | OPT_S = 1 << 15, |
206 | OPT_A = 1 << 16, | ||
207 | OPT_O = 1 << 17, | ||
208 | OPT_o = 1 << 18, | ||
209 | OPT_f = 1 << 19, | ||
210 | niro | 816 | /* The rest has variable bit positions, need to be clever */ |
211 | niro | 984 | OPTBIT_f = 19, |
212 | USE_FOR_MMU( OPTBIT_b,) | ||
213 | IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,) | ||
214 | IF_FEATURE_UDHCP_PORT( OPTBIT_P,) | ||
215 | USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,) | ||
216 | IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,) | ||
217 | IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,) | ||
218 | niro | 532 | }; |
219 | niro | 816 | |
220 | niro | 532 | /* Default options. */ |
221 | niro | 984 | IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) |
222 | IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) | ||
223 | niro | 532 | client_config.interface = "eth0"; |
224 | client_config.script = DEFAULT_SCRIPT; | ||
225 | |||
226 | /* Parse command line */ | ||
227 | niro | 816 | /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */ |
228 | niro | 984 | opt_complementary = "c--C:C--c:O::T+:t+:A+" |
229 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 | ||
230 | ":vv" | ||
231 | #endif | ||
232 | ; | ||
233 | IF_LONG_OPTS(applet_long_options = udhcpc_longopts;) | ||
234 | opt = getopt32(argv, "c:CV:H:h:F:i:np:qRr:s:T:t:SA:O:of" | ||
235 | niro | 816 | USE_FOR_MMU("b") |
236 | niro | 984 | IF_FEATURE_UDHCPC_ARPING("a") |
237 | IF_FEATURE_UDHCP_PORT("P:") | ||
238 | "v" | ||
239 | niro | 816 | , &str_c, &str_V, &str_h, &str_h, &str_F |
240 | , &client_config.interface, &client_config.pidfile, &str_r /* i,p */ | ||
241 | , &client_config.script /* s */ | ||
242 | , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */ | ||
243 | , &list_O | ||
244 | niro | 984 | IF_FEATURE_UDHCP_PORT(, &str_P) |
245 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 | ||
246 | , &dhcp_verbose | ||
247 | #endif | ||
248 | niro | 532 | ); |
249 | if (opt & OPT_c) | ||
250 | client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0); | ||
251 | if (opt & OPT_V) | ||
252 | client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0); | ||
253 | niro | 816 | if (opt & (OPT_h|OPT_H)) |
254 | niro | 532 | client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0); |
255 | if (opt & OPT_F) { | ||
256 | client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3); | ||
257 | /* Flags: 0000NEOS | ||
258 | S: 1 => Client requests Server to update A RR in DNS as well as PTR | ||
259 | O: 1 => Server indicates to client that DNS has been updated regardless | ||
260 | niro | 816 | E: 1 => Name data is DNS format, i.e. <4>host<6>domain<3>com<0> not "host.domain.com" |
261 | niro | 532 | N: 1 => Client requests Server to not update DNS |
262 | */ | ||
263 | client_config.fqdn[OPT_DATA + 0] = 0x1; | ||
264 | /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */ | ||
265 | /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */ | ||
266 | } | ||
267 | if (opt & OPT_r) | ||
268 | requested_ip = inet_addr(str_r); | ||
269 | niro | 816 | #if ENABLE_FEATURE_UDHCP_PORT |
270 | if (opt & OPT_P) { | ||
271 | CLIENT_PORT = xatou16(str_P); | ||
272 | SERVER_PORT = CLIENT_PORT - 1; | ||
273 | } | ||
274 | #endif | ||
275 | if (opt & OPT_o) | ||
276 | client_config.no_default_options = 1; | ||
277 | while (list_O) { | ||
278 | char *optstr = llist_pop(&list_O); | ||
279 | int n = index_in_strings(dhcp_option_strings, optstr); | ||
280 | if (n < 0) | ||
281 | bb_error_msg_and_die("unknown option '%s'", optstr); | ||
282 | n = dhcp_options[n].code; | ||
283 | client_config.opt_mask[n >> 3] |= 1 << (n & 7); | ||
284 | } | ||
285 | niro | 532 | |
286 | niro | 984 | if (udhcp_read_interface(client_config.interface, |
287 | &client_config.ifindex, | ||
288 | NULL, | ||
289 | client_config.client_mac) | ||
290 | ) { | ||
291 | niro | 532 | return 1; |
292 | niro | 984 | } |
293 | |||
294 | niro | 816 | #if !BB_MMU |
295 | /* on NOMMU reexec (i.e., background) early */ | ||
296 | if (!(opt & OPT_f)) { | ||
297 | bb_daemonize_or_rexec(0 /* flags */, argv); | ||
298 | niro | 984 | logmode = LOGMODE_NONE; |
299 | niro | 816 | } |
300 | #endif | ||
301 | if (opt & OPT_S) { | ||
302 | niro | 984 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
303 | niro | 816 | logmode |= LOGMODE_SYSLOG; |
304 | } | ||
305 | niro | 532 | |
306 | niro | 816 | /* Make sure fd 0,1,2 are open */ |
307 | bb_sanitize_stdio(); | ||
308 | /* Equivalent of doing a fflush after every \n */ | ||
309 | setlinebuf(stdout); | ||
310 | |||
311 | /* Create pidfile */ | ||
312 | write_pidfile(client_config.pidfile); | ||
313 | |||
314 | /* Goes to stdout (unless NOMMU) and possibly syslog */ | ||
315 | bb_info_msg("%s (v"BB_VER") started", applet_name); | ||
316 | |||
317 | niro | 984 | /* If not set, and not suppressed, set up the default client ID */ |
318 | niro | 816 | if (!client_config.clientid && !(opt & OPT_C)) { |
319 | niro | 532 | client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7); |
320 | client_config.clientid[OPT_DATA] = 1; | ||
321 | niro | 984 | memcpy(client_config.clientid + OPT_DATA+1, client_config.client_mac, 6); |
322 | niro | 532 | } |
323 | |||
324 | if (!client_config.vendorclass) | ||
325 | client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0); | ||
326 | |||
327 | niro | 984 | /* Set up the signal pipe */ |
328 | niro | 532 | udhcp_sp_setup(); |
329 | |||
330 | state = INIT_SELECTING; | ||
331 | udhcp_run_script(NULL, "deconfig"); | ||
332 | niro | 816 | change_listen_mode(LISTEN_RAW); |
333 | packet_num = 0; | ||
334 | timeout = 0; | ||
335 | already_waited_sec = 0; | ||
336 | niro | 532 | |
337 | niro | 816 | /* Main event loop. select() waits on signal pipe and possibly |
338 | * on sockfd. | ||
339 | * "continue" statements in code below jump to the top of the loop. | ||
340 | */ | ||
341 | niro | 532 | for (;;) { |
342 | niro | 984 | /* silence "uninitialized!" warning */ |
343 | unsigned timestamp_before_wait = timestamp_before_wait; | ||
344 | niro | 532 | |
345 | niro | 984 | //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode); |
346 | |||
347 | /* Was opening raw or udp socket here | ||
348 | * if (listen_mode != LISTEN_NONE && sockfd < 0), | ||
349 | * but on fast network renew responses return faster | ||
350 | * than we open sockets. Thus this code is moved | ||
351 | * to change_listen_mode(). Thus we open listen socket | ||
352 | * BEFORE we send renew request (see "case BOUND:"). */ | ||
353 | |||
354 | niro | 816 | max_fd = udhcp_sp_fd_set(&rfds, sockfd); |
355 | niro | 532 | |
356 | niro | 816 | tv.tv_sec = timeout - already_waited_sec; |
357 | tv.tv_usec = 0; | ||
358 | retval = 0; /* If we already timed out, fall through, else... */ | ||
359 | niro | 984 | if ((int)tv.tv_sec > 0) { |
360 | niro | 816 | timestamp_before_wait = (unsigned)monotonic_sec(); |
361 | niro | 984 | log1("Waiting on select..."); |
362 | niro | 532 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); |
363 | niro | 816 | if (retval < 0) { |
364 | /* EINTR? A signal was caught, don't panic */ | ||
365 | niro | 984 | if (errno == EINTR) { |
366 | already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait; | ||
367 | niro | 816 | continue; |
368 | niro | 984 | } |
369 | niro | 816 | /* Else: an error occured, panic! */ |
370 | bb_perror_msg_and_die("select"); | ||
371 | } | ||
372 | } | ||
373 | niro | 532 | |
374 | niro | 816 | /* If timeout dropped to zero, time to become active: |
375 | * resend discover/renew/whatever | ||
376 | */ | ||
377 | niro | 532 | if (retval == 0) { |
378 | niro | 816 | /* We will restart the wait in any case */ |
379 | already_waited_sec = 0; | ||
380 | |||
381 | niro | 532 | switch (state) { |
382 | case INIT_SELECTING: | ||
383 | niro | 816 | if (packet_num < discover_retries) { |
384 | niro | 532 | if (packet_num == 0) |
385 | xid = random_xid(); | ||
386 | niro | 984 | /* broadcast */ |
387 | send_discover(xid, requested_ip); | ||
388 | niro | 816 | timeout = discover_timeout; |
389 | niro | 532 | packet_num++; |
390 | niro | 816 | continue; |
391 | niro | 532 | } |
392 | niro | 816 | leasefail: |
393 | udhcp_run_script(NULL, "leasefail"); | ||
394 | #if BB_MMU /* -b is not supported on NOMMU */ | ||
395 | if (opt & OPT_b) { /* background if no lease */ | ||
396 | bb_info_msg("No lease, forking to background"); | ||
397 | client_background(); | ||
398 | /* do not background again! */ | ||
399 | opt = ((opt & ~OPT_b) | OPT_f); | ||
400 | } else | ||
401 | #endif | ||
402 | if (opt & OPT_n) { /* abort if no lease */ | ||
403 | bb_info_msg("No lease, failing"); | ||
404 | retval = 1; | ||
405 | goto ret; | ||
406 | } | ||
407 | /* wait before trying again */ | ||
408 | timeout = tryagain_timeout; | ||
409 | packet_num = 0; | ||
410 | continue; | ||
411 | niro | 532 | case REQUESTING: |
412 | niro | 816 | if (packet_num < discover_retries) { |
413 | niro | 984 | /* send broadcast select packet */ |
414 | send_select(xid, server_addr, requested_ip); | ||
415 | niro | 816 | timeout = discover_timeout; |
416 | niro | 532 | packet_num++; |
417 | niro | 816 | continue; |
418 | } | ||
419 | niro | 984 | /* Timed out, go back to init state. |
420 | * "discover...select...discover..." loops | ||
421 | niro | 816 | * were seen in the wild. Treat them similarly |
422 | * to "no response to discover" case */ | ||
423 | niro | 984 | change_listen_mode(LISTEN_RAW); |
424 | niro | 816 | state = INIT_SELECTING; |
425 | niro | 984 | goto leasefail; |
426 | niro | 532 | case BOUND: |
427 | niro | 984 | /* 1/2 lease passed, enter renewing state */ |
428 | state = RENEWING; | ||
429 | niro | 816 | change_listen_mode(LISTEN_KERNEL); |
430 | niro | 984 | log1("Entering renew state"); |
431 | niro | 532 | /* fall right through */ |
432 | niro | 984 | case RENEW_REQUESTED: /* manual (SIGUSR1) renew */ |
433 | case_RENEW_REQUESTED: | ||
434 | niro | 532 | case RENEWING: |
435 | niro | 816 | if (timeout > 60) { |
436 | niro | 984 | /* send an unicast renew request */ |
437 | /* Sometimes observed to fail (EADDRNOTAVAIL) to bind | ||
438 | * a new UDP socket for sending inside send_renew. | ||
439 | * I hazard to guess existing listening socket | ||
440 | * is somehow conflicting with it, but why is it | ||
441 | * not deterministic then?! Strange. | ||
442 | * Anyway, it does recover by eventually failing through | ||
443 | * into INIT_SELECTING state. | ||
444 | */ | ||
445 | send_renew(xid, server_addr, requested_ip); | ||
446 | niro | 816 | timeout >>= 1; |
447 | continue; | ||
448 | niro | 532 | } |
449 | niro | 816 | /* Timed out, enter rebinding state */ |
450 | niro | 984 | log1("Entering rebinding state"); |
451 | niro | 816 | state = REBINDING; |
452 | /* fall right through */ | ||
453 | niro | 532 | case REBINDING: |
454 | niro | 984 | /* Switch to bcast receive */ |
455 | change_listen_mode(LISTEN_RAW); | ||
456 | niro | 816 | /* Lease is *really* about to run out, |
457 | * try to find DHCP server using broadcast */ | ||
458 | if (timeout > 0) { | ||
459 | niro | 984 | /* send a broadcast renew request */ |
460 | send_renew(xid, 0 /*INADDR_ANY*/, requested_ip); | ||
461 | niro | 816 | timeout >>= 1; |
462 | continue; | ||
463 | niro | 532 | } |
464 | niro | 816 | /* Timed out, enter init state */ |
465 | bb_info_msg("Lease lost, entering init state"); | ||
466 | udhcp_run_script(NULL, "deconfig"); | ||
467 | state = INIT_SELECTING; | ||
468 | /*timeout = 0; - already is */ | ||
469 | packet_num = 0; | ||
470 | continue; | ||
471 | /* case RELEASED: */ | ||
472 | niro | 532 | } |
473 | niro | 816 | /* yah, I know, *you* say it would never happen */ |
474 | timeout = INT_MAX; | ||
475 | continue; /* back to main loop */ | ||
476 | niro | 984 | } /* if select timed out */ |
477 | |||
478 | /* select() didn't timeout, something happened */ | ||
479 | |||
480 | /* Is it a signal? */ | ||
481 | /* note: udhcp_sp_read checks FD_ISSET before reading */ | ||
482 | switch (udhcp_sp_read(&rfds)) { | ||
483 | case SIGUSR1: | ||
484 | perform_renew(); | ||
485 | if (state == RENEW_REQUESTED) | ||
486 | goto case_RENEW_REQUESTED; | ||
487 | /* Start things over */ | ||
488 | packet_num = 0; | ||
489 | /* Kill any timeouts, user wants this to hurry along */ | ||
490 | timeout = 0; | ||
491 | continue; | ||
492 | case SIGUSR2: | ||
493 | perform_release(requested_ip, server_addr); | ||
494 | timeout = INT_MAX; | ||
495 | continue; | ||
496 | case SIGTERM: | ||
497 | bb_info_msg("Received SIGTERM"); | ||
498 | if (opt & OPT_R) /* release on quit */ | ||
499 | perform_release(requested_ip, server_addr); | ||
500 | goto ret0; | ||
501 | niro | 816 | } |
502 | niro | 532 | |
503 | niro | 816 | /* Is it a packet? */ |
504 | niro | 984 | if (listen_mode == LISTEN_NONE || !FD_ISSET(sockfd, &rfds)) |
505 | continue; /* no */ | ||
506 | |||
507 | { | ||
508 | niro | 816 | int len; |
509 | niro | 984 | |
510 | niro | 816 | /* A packet is ready, read it */ |
511 | niro | 532 | if (listen_mode == LISTEN_KERNEL) |
512 | niro | 816 | len = udhcp_recv_kernel_packet(&packet, sockfd); |
513 | else | ||
514 | len = udhcp_recv_raw_packet(&packet, sockfd); | ||
515 | niro | 984 | if (len == -1) { |
516 | /* Error is severe, reopen socket */ | ||
517 | bb_info_msg("Read error: %s, reopening socket", strerror(errno)); | ||
518 | niro | 816 | sleep(discover_timeout); /* 3 seconds by default */ |
519 | change_listen_mode(listen_mode); /* just close and reopen */ | ||
520 | niro | 532 | } |
521 | niro | 816 | /* If this packet will turn out to be unrelated/bogus, |
522 | * we will go back and wait for next one. | ||
523 | * Be sure timeout is properly decreased. */ | ||
524 | already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait; | ||
525 | if (len < 0) | ||
526 | continue; | ||
527 | niro | 984 | } |
528 | niro | 532 | |
529 | niro | 984 | if (packet.xid != xid) { |
530 | log1("xid %x (our is %x), ignoring packet", | ||
531 | (unsigned)packet.xid, (unsigned)xid); | ||
532 | continue; | ||
533 | } | ||
534 | niro | 532 | |
535 | niro | 984 | /* Ignore packets that aren't for us */ |
536 | if (packet.hlen != 6 | ||
537 | || memcmp(packet.chaddr, client_config.client_mac, 6) | ||
538 | ) { | ||
539 | //FIXME: need to also check that last 10 bytes are zero | ||
540 | log1("chaddr does not match, ignoring packet"); // log2? | ||
541 | continue; | ||
542 | } | ||
543 | niro | 532 | |
544 | niro | 984 | message = get_option(&packet, DHCP_MESSAGE_TYPE); |
545 | if (message == NULL) { | ||
546 | bb_error_msg("no message type option, ignoring packet"); | ||
547 | continue; | ||
548 | } | ||
549 | |||
550 | switch (state) { | ||
551 | case INIT_SELECTING: | ||
552 | /* Must be a DHCPOFFER to one of our xid's */ | ||
553 | if (*message == DHCPOFFER) { | ||
554 | /* TODO: why we don't just fetch server's IP from IP header? */ | ||
555 | temp = get_option(&packet, DHCP_SERVER_ID); | ||
556 | if (!temp) { | ||
557 | bb_error_msg("no server ID in message"); | ||
558 | continue; | ||
559 | /* still selecting - this server looks bad */ | ||
560 | } | ||
561 | /* it IS unaligned sometimes, don't "optimize" */ | ||
562 | move_from_unaligned32(server_addr, temp); | ||
563 | xid = packet.xid; | ||
564 | requested_ip = packet.yiaddr; | ||
565 | |||
566 | /* enter requesting state */ | ||
567 | state = REQUESTING; | ||
568 | timeout = 0; | ||
569 | packet_num = 0; | ||
570 | already_waited_sec = 0; | ||
571 | niro | 532 | } |
572 | niro | 984 | continue; |
573 | case REQUESTING: | ||
574 | case RENEWING: | ||
575 | case RENEW_REQUESTED: | ||
576 | case REBINDING: | ||
577 | if (*message == DHCPACK) { | ||
578 | temp = get_option(&packet, DHCP_LEASE_TIME); | ||
579 | if (!temp) { | ||
580 | bb_error_msg("no lease time with ACK, using 1 hour lease"); | ||
581 | lease_seconds = 60 * 60; | ||
582 | } else { | ||
583 | niro | 816 | /* it IS unaligned sometimes, don't "optimize" */ |
584 | niro | 984 | move_from_unaligned32(lease_seconds, temp); |
585 | lease_seconds = ntohl(lease_seconds); | ||
586 | lease_seconds &= 0x0fffffff; /* paranoia: must not be prone to overflows */ | ||
587 | if (lease_seconds < 10) /* and not too small */ | ||
588 | lease_seconds = 10; | ||
589 | niro | 532 | } |
590 | niro | 816 | #if ENABLE_FEATURE_UDHCPC_ARPING |
591 | niro | 984 | if (opt & OPT_a) { |
592 | niro | 816 | /* RFC 2131 3.1 paragraph 5: |
593 | * "The client receives the DHCPACK message with configuration | ||
594 | * parameters. The client SHOULD perform a final check on the | ||
595 | * parameters (e.g., ARP for allocated network address), and notes | ||
596 | * the duration of the lease specified in the DHCPACK message. At this | ||
597 | * point, the client is configured. If the client detects that the | ||
598 | * address is already in use (e.g., through the use of ARP), | ||
599 | * the client MUST send a DHCPDECLINE message to the server and restarts | ||
600 | * the configuration process..." */ | ||
601 | niro | 984 | if (!arpping(packet.yiaddr, |
602 | NULL, | ||
603 | (uint32_t) 0, | ||
604 | client_config.client_mac, | ||
605 | client_config.interface) | ||
606 | ) { | ||
607 | bb_info_msg("Offered address is in use " | ||
608 | "(got ARP reply), declining"); | ||
609 | send_decline(xid, server_addr, packet.yiaddr); | ||
610 | niro | 532 | |
611 | niro | 984 | if (state != REQUESTING) |
612 | udhcp_run_script(NULL, "deconfig"); | ||
613 | change_listen_mode(LISTEN_RAW); | ||
614 | state = INIT_SELECTING; | ||
615 | requested_ip = 0; | ||
616 | timeout = tryagain_timeout; | ||
617 | packet_num = 0; | ||
618 | already_waited_sec = 0; | ||
619 | continue; /* back to main loop */ | ||
620 | niro | 816 | } |
621 | niro | 984 | } |
622 | niro | 816 | #endif |
623 | niro | 984 | /* enter bound state */ |
624 | timeout = lease_seconds / 2; | ||
625 | { | ||
626 | struct in_addr temp_addr; | ||
627 | temp_addr.s_addr = packet.yiaddr; | ||
628 | bb_info_msg("Lease of %s obtained, lease time %u", | ||
629 | inet_ntoa(temp_addr), (unsigned)lease_seconds); | ||
630 | } | ||
631 | requested_ip = packet.yiaddr; | ||
632 | udhcp_run_script(&packet, state == REQUESTING ? "bound" : "renew"); | ||
633 | niro | 532 | |
634 | niro | 984 | state = BOUND; |
635 | change_listen_mode(LISTEN_NONE); | ||
636 | if (opt & OPT_q) { /* quit after lease */ | ||
637 | if (opt & OPT_R) /* release on quit */ | ||
638 | perform_release(requested_ip, server_addr); | ||
639 | goto ret0; | ||
640 | } | ||
641 | /* future renew failures should not exit (JM) */ | ||
642 | opt &= ~OPT_n; | ||
643 | niro | 816 | #if BB_MMU /* NOMMU case backgrounded earlier */ |
644 | niro | 984 | if (!(opt & OPT_f)) { |
645 | client_background(); | ||
646 | /* do not background again! */ | ||
647 | opt = ((opt & ~OPT_b) | OPT_f); | ||
648 | } | ||
649 | niro | 816 | #endif |
650 | niro | 984 | already_waited_sec = 0; |
651 | continue; /* back to main loop */ | ||
652 | niro | 532 | } |
653 | niro | 984 | if (*message == DHCPNAK) { |
654 | /* return to init state */ | ||
655 | bb_info_msg("Received DHCP NAK"); | ||
656 | udhcp_run_script(&packet, "nak"); | ||
657 | if (state != REQUESTING) | ||
658 | udhcp_run_script(NULL, "deconfig"); | ||
659 | change_listen_mode(LISTEN_RAW); | ||
660 | sleep(3); /* avoid excessive network traffic */ | ||
661 | state = INIT_SELECTING; | ||
662 | requested_ip = 0; | ||
663 | timeout = 0; | ||
664 | niro | 816 | packet_num = 0; |
665 | niro | 984 | already_waited_sec = 0; |
666 | niro | 532 | } |
667 | niro | 984 | continue; |
668 | /* case BOUND: - ignore all packets */ | ||
669 | /* case RELEASED: - ignore all packets */ | ||
670 | niro | 532 | } |
671 | niro | 984 | /* back to main loop */ |
672 | niro | 816 | } /* for (;;) - main loop ends */ |
673 | niro | 532 | |
674 | niro | 816 | ret0: |
675 | retval = 0; | ||
676 | ret: | ||
677 | /*if (client_config.pidfile) - remove_pidfile has its own check */ | ||
678 | remove_pidfile(client_config.pidfile); | ||
679 | return retval; | ||
680 | niro | 532 | } |