dhcpc.c 18 KB

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