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