blob: 7fca184abeb84d41495faeff985666679e5221bc [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 Vlasenko9cdfd142007-11-22 01:00:00 +000022static int timeout; /* = 0. Must be signed */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000023static uint32_t requested_ip; /* = 0 */
Denis Vlasenko74c9d232007-01-18 15:42:00 +000024static uint32_t server_addr;
Denis Vlasenkofbd29182007-04-07 01:05:47 +000025static int sockfd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000026
27#define LISTEN_NONE 0
28#define LISTEN_KERNEL 1
29#define LISTEN_RAW 2
Denis Vlasenkofbd29182007-04-07 01:05:47 +000030static smallint listen_mode;
31
32static smallint state;
Mike Frysinger7031f622006-05-08 03:20:50 +000033
Denis Vlasenkodeabacd2007-09-30 17:55:43 +000034/* struct client_config_t client_config is in bb_common_bufsiz1 */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000035
Mike Frysinger7031f622006-05-08 03:20:50 +000036
Mike Frysinger7031f622006-05-08 03:20:50 +000037/* just a little helper */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000038static void change_listen_mode(int new_mode)
Mike Frysinger7031f622006-05-08 03:20:50 +000039{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000040 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000041 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Denis Vlasenkofbd29182007-04-07 01:05:47 +000042 if (sockfd >= 0) {
43 close(sockfd);
44 sockfd = -1;
45 }
Mike Frysinger7031f622006-05-08 03:20:50 +000046 listen_mode = new_mode;
47}
48
49
50/* perform a renew */
51static void perform_renew(void)
52{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000053 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000054 switch (state) {
55 case BOUND:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000056 change_listen_mode(LISTEN_KERNEL);
Mike Frysinger7031f622006-05-08 03:20:50 +000057 case RENEWING:
58 case REBINDING:
59 state = RENEW_REQUESTED;
60 break;
61 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000062 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000063 case REQUESTING:
64 case RELEASED:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000065 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +000066 state = INIT_SELECTING;
67 break;
68 case INIT_SELECTING:
69 break;
70 }
Mike Frysinger7031f622006-05-08 03:20:50 +000071}
72
73
74/* perform a release */
75static void perform_release(void)
76{
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000077 char buffer[sizeof("255.255.255.255")];
Mike Frysinger7031f622006-05-08 03:20:50 +000078 struct in_addr temp_addr;
79
80 /* send release packet */
81 if (state == BOUND || state == RENEWING || state == REBINDING) {
82 temp_addr.s_addr = server_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000083 strcpy(buffer, inet_ntoa(temp_addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000084 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000085 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000086 inet_ntoa(temp_addr), buffer);
87 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +000088 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000089 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000090 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +000091
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000092 change_listen_mode(LISTEN_NONE);
Mike Frysinger7031f622006-05-08 03:20:50 +000093 state = RELEASED;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000094 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +000095}
96
97
98static void client_background(void)
99{
Denis Vlasenko80edead2007-08-02 22:31:05 +0000100#if !BB_MMU
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000101 bb_error_msg("cannot background in uclinux (yet)");
102/* ... mainly because udhcpc calls client_background()
103 * in _the _middle _of _udhcpc _run_, not at the start!
104 * If that will be properly disabled for NOMMU, client_background()
105 * will work on NOMMU too */
106#else
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000107 bb_daemonize(0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000108 logmode &= ~LOGMODE_STDIO;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000109 /* rewrite pidfile, as our pid is different now */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000110 write_pidfile(client_config.pidfile);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000111#endif
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000112 /* Do not fork again. */
113 client_config.foreground = 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000114 client_config.background_if_no_lease = 0;
115}
116
117
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000118static 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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000131int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000132int udhcpc_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000133{
134 uint8_t *temp, *message;
Paul Fox49cce2b2007-11-07 16:01:28 +0000135 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_A, *str_t;
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000136 USE_FEATURE_UDHCP_PORT(char *str_P;)
Denis Vlasenko19183682007-12-10 07:03:38 +0000137 llist_t *list_O = NULL;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000138#if ENABLE_FEATURE_UDHCPC_ARPING
Denis Vlasenko223bc972007-11-22 00:58:49 +0000139 char *str_W;
140#endif
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000141 int tryagain_timeout = 20;
142 int discover_timeout = 3;
143 int discover_retries = 3;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000144 uint32_t xid = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000145 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000146 int packet_num;
147 /* t1, t2... what a wonderful names... */
148 unsigned t1 = t1; /* for gcc */
149 unsigned t2 = t2;
150 unsigned timestamp_got_lease = timestamp_got_lease;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000151 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000152 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000153 int retval;
154 int len;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000155 struct timeval tv;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000156 struct in_addr temp_addr;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000157 struct dhcpMessage packet;
158 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000159
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000160 enum {
161 OPT_c = 1 << 0,
162 OPT_C = 1 << 1,
163 OPT_V = 1 << 2,
164 OPT_f = 1 << 3,
165 OPT_b = 1 << 4,
166 OPT_H = 1 << 5,
167 OPT_h = 1 << 6,
168 OPT_F = 1 << 7,
169 OPT_i = 1 << 8,
170 OPT_n = 1 << 9,
171 OPT_p = 1 << 10,
172 OPT_q = 1 << 11,
173 OPT_R = 1 << 12,
174 OPT_r = 1 << 13,
175 OPT_s = 1 << 14,
176 OPT_T = 1 << 15,
177 OPT_t = 1 << 16,
178 OPT_v = 1 << 17,
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000179 OPT_S = 1 << 18,
Paul Fox49cce2b2007-11-07 16:01:28 +0000180 OPT_A = 1 << 19,
Denis Vlasenko223bc972007-11-22 00:58:49 +0000181#if ENABLE_FEATURE_UDHCPC_ARPING
182 OPT_a = 1 << 20,
183 OPT_W = 1 << 21,
184#endif
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000185 OPT_P = 1 << 22,
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000186 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000187#if ENABLE_GETOPT_LONG
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000188 static const char udhcpc_longopts[] ALIGN1 =
Denis Vlasenko19183682007-12-10 07:03:38 +0000189 "clientid\0" Required_argument "c"
190 "clientid-none\0" No_argument "C"
191 "vendorclass\0" Required_argument "V"
192 "foreground\0" No_argument "f"
193 "background\0" No_argument "b"
194 "hostname\0" Required_argument "H"
195 "fqdn\0" Required_argument "F"
196 "interface\0" Required_argument "i"
197 "now\0" No_argument "n"
198 "pidfile\0" Required_argument "p"
199 "quit\0" No_argument "q"
200 "release\0" No_argument "R"
201 "request\0" Required_argument "r"
202 "script\0" Required_argument "s"
203 "timeout\0" Required_argument "T"
204 "version\0" No_argument "v"
205 "retries\0" Required_argument "t"
206 "tryagain\0" Required_argument "A"
207 "syslog\0" No_argument "S"
Denis Vlasenko223bc972007-11-22 00:58:49 +0000208#if ENABLE_FEATURE_UDHCPC_ARPING
Denis Vlasenko19183682007-12-10 07:03:38 +0000209 "arping\0" No_argument "a"
Denis Vlasenko223bc972007-11-22 00:58:49 +0000210#endif
Denis Vlasenko19183682007-12-10 07:03:38 +0000211 "request-option\0" Required_argument "O"
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000212#if ENABLE_FEATURE_UDHCP_PORT
213 "client-port\0" Required_argument "P"
214#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000215 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000216#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000217 /* Default options. */
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000218#if ENABLE_FEATURE_UDHCP_PORT
219 SERVER_PORT = 67;
220 CLIENT_PORT = 68;
221#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000222 client_config.interface = "eth0";
223 client_config.script = DEFAULT_SCRIPT;
Mike Frysinger7031f622006-05-08 03:20:50 +0000224
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000225 /* Parse command line */
Denis Vlasenko19183682007-12-10 07:03:38 +0000226 opt_complementary = "c--C:C--c:O::"; // Cc: mutually exclusive; O: list
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000227#if ENABLE_GETOPT_LONG
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000228 applet_long_options = udhcpc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000229#endif
Denis Vlasenko223bc972007-11-22 00:58:49 +0000230 opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
231 USE_FEATURE_UDHCPC_ARPING("aW:")
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000232 USE_FEATURE_UDHCP_PORT("P:")
Denis Vlasenko19183682007-12-10 07:03:38 +0000233 "O:"
234 , &str_c, &str_V, &str_h, &str_h, &str_F
235 , &client_config.interface, &client_config.pidfile, &str_r
236 , &client_config.script, &str_T, &str_t, &str_A
Denis Vlasenko223bc972007-11-22 00:58:49 +0000237 USE_FEATURE_UDHCPC_ARPING(, &str_W)
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000238 USE_FEATURE_UDHCP_PORT(, &str_P)
Denis Vlasenko19183682007-12-10 07:03:38 +0000239 , &list_O
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000240 );
241
242 if (opt & OPT_c)
243 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000244 //if (opt & OPT_C)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000245 if (opt & OPT_V)
246 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
247 if (opt & OPT_f)
248 client_config.foreground = 1;
249 if (opt & OPT_b)
250 client_config.background_if_no_lease = 1;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000251 if (opt & (OPT_h|OPT_H))
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000252 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
253 if (opt & OPT_F) {
254 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
255 /* Flags: 0000NEOS
256 S: 1 => Client requests Server to update A RR in DNS as well as PTR
257 O: 1 => Server indicates to client that DNS has been updated regardless
258 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
259 N: 1 => Client requests Server to not update DNS
260 */
261 client_config.fqdn[OPT_DATA + 0] = 0x1;
262 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
263 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
264 }
265 // if (opt & OPT_i) client_config.interface = ...
266 if (opt & OPT_n)
267 client_config.abort_if_no_lease = 1;
268 // if (opt & OPT_p) client_config.pidfile = ...
269 if (opt & OPT_q)
270 client_config.quit_after_lease = 1;
271 if (opt & OPT_R)
272 client_config.release_on_quit = 1;
273 if (opt & OPT_r)
274 requested_ip = inet_addr(str_r);
275 // if (opt & OPT_s) client_config.script = ...
276 if (opt & OPT_T)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000277 discover_timeout = xatoi_u(str_T);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000278 if (opt & OPT_t)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000279 discover_retries = xatoi_u(str_t);
Paul Fox49cce2b2007-11-07 16:01:28 +0000280 if (opt & OPT_A)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000281 tryagain_timeout = xatoi_u(str_A);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000282 if (opt & OPT_v) {
Denis Vlasenkodef88982007-10-07 17:06:01 +0000283 puts("version "BB_VER);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000284 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000285 }
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000286 if (opt & OPT_S) {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000287 openlog(applet_name, LOG_PID, LOG_LOCAL0);
288 logmode |= LOGMODE_SYSLOG;
289 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000290#if ENABLE_FEATURE_UDHCP_PORT
291 if (opt & OPT_P) {
292 CLIENT_PORT = xatou16(str_P);
293 SERVER_PORT = CLIENT_PORT - 1;
294 }
295#endif
Denis Vlasenko19183682007-12-10 07:03:38 +0000296 while (list_O) {
297 int n = index_in_strings(dhcp_option_strings, list_O->data);
298 if (n < 0)
299 bb_error_msg_and_die("unknown option '%s'", list_O->data);
300 n = dhcp_options[n].code;
301 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
302 list_O = list_O->link;
303 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000304
305 if (read_interface(client_config.interface, &client_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +0000306 NULL, client_config.arp))
Mike Frysinger7031f622006-05-08 03:20:50 +0000307 return 1;
308
Denis Vlasenko80edead2007-08-02 22:31:05 +0000309 /* Make sure fd 0,1,2 are open */
310 bb_sanitize_stdio();
311 /* Equivalent of doing a fflush after every \n */
312 setlinebuf(stdout);
313
314 /* Create pidfile */
315 write_pidfile(client_config.pidfile);
316 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
317
318 /* Goes to stdout and possibly syslog */
Denis Vlasenkodef88982007-10-07 17:06:01 +0000319 bb_info_msg("%s (v"BB_VER") started", applet_name);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000320
Mike Frysinger7031f622006-05-08 03:20:50 +0000321 /* if not set, and not suppressed, setup the default client ID */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000322 if (!client_config.clientid && !(opt & OPT_C)) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000323 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000324 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000325 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000326 }
327
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000328 if (!client_config.vendorclass)
329 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000330
331 /* setup the signal pipe */
332 udhcp_sp_setup();
333
334 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000335 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000336 change_listen_mode(LISTEN_RAW);
Denis Vlasenkob539c842007-11-29 08:17:45 +0000337 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000338
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000339 /* Main event loop. select() waits on signal pipe and possibly
340 * on sockfd.
341 * "continue" statements in code below jump to the top of the loop.
342 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000343 for (;;) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000344 tv.tv_sec = timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000345 tv.tv_usec = 0;
346
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000347 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000348 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000349 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000350 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000351 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000352 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000353 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000354
Denis Vlasenko80edead2007-08-02 22:31:05 +0000355 retval = 0; /* If we already timed out, fall through, else... */
Mike Frysinger7031f622006-05-08 03:20:50 +0000356 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000357 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000358 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000359 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000360
Denis Vlasenko80edead2007-08-02 22:31:05 +0000361 if (retval < 0) {
362 /* EINTR? signal was caught, don't panic */
363 if (errno != EINTR) {
364 /* Else: an error occured, panic! */
365 bb_perror_msg_and_die("select");
366 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000367 continue;
368 }
369
370 /* If timeout dropped to zero, time to become active:
371 * resend discover/renew/whatever
372 */
373 if (retval == 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000374 switch (state) {
375 case INIT_SELECTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000376 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000377 if (packet_num == 0)
378 xid = random_xid();
379
380 /* send discover packet */
381 send_discover(xid, requested_ip); /* broadcast */
382
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000383 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000384 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000385 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000386 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000387 udhcp_run_script(NULL, "leasefail");
388 if (client_config.background_if_no_lease) {
389 bb_info_msg("No lease, forking to background");
390 client_background();
391 } else if (client_config.abort_if_no_lease) {
392 bb_info_msg("No lease, failing");
393 retval = 1;
394 goto ret;
395 }
396 /* wait to try again */
397 timeout = tryagain_timeout;
398 packet_num = 0;
399 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000400 case RENEW_REQUESTED:
401 case REQUESTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000402 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000403 /* send request packet */
404 if (state == RENEW_REQUESTED)
405 send_renew(xid, server_addr, requested_ip); /* unicast */
406 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
407
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000408 timeout = ((packet_num == 2) ? 10 : 2);
Mike Frysinger7031f622006-05-08 03:20:50 +0000409 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000410 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000411 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000412 /* timed out, go back to init state */
413 if (state == RENEW_REQUESTED)
414 udhcp_run_script(NULL, "deconfig");
415 change_listen_mode(LISTEN_RAW);
416 state = INIT_SELECTING;
417 timeout = 0;
418 packet_num = 0;
419 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000420 case BOUND:
421 /* Lease is starting to run out, time to enter renewing state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000422 change_listen_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000423 DEBUG("Entering renew state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000424 state = RENEWING;
Mike Frysinger7031f622006-05-08 03:20:50 +0000425 /* fall right through */
426 case RENEWING:
427 /* Either set a new T1, or enter REBINDING state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000428 if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000429 /* send a request packet */
430 send_renew(xid, server_addr, requested_ip); /* unicast */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000431 t1 += (t2 - t1) / 2;
432 timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease);
433 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000434 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000435 /* Timed out, enter rebinding state */
436 DEBUG("Entering rebinding state");
437 state = REBINDING;
438 timeout = (t2 - t1);
439 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000440 case REBINDING:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000441 /* Lease is *really* about to run out,
442 * try to find DHCP server using broadcast */
443 if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000444 /* send a request packet */
445 send_renew(xid, 0, requested_ip); /* broadcast */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000446 t2 += (lease_seconds - t2) / 2;
447 timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease);
448 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000449 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000450 /* Timed out, enter init state */
451 bb_info_msg("Lease lost, entering init state");
452 udhcp_run_script(NULL, "deconfig");
453 change_listen_mode(LISTEN_RAW);
454 state = INIT_SELECTING;
455 timeout = 0;
456 packet_num = 0;
457 continue;
458 /* case RELEASED: */
Mike Frysinger7031f622006-05-08 03:20:50 +0000459 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000460 /* yah, I know, *you* say it would never happen */
461 timeout = INT_MAX;
462 continue; /* back to main loop */
463 }
464
465 /* select() didn't timeout, something did happen. */
466 /* Is is a packet? */
467 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
468 /* A packet is ready, read it */
Mike Frysinger7031f622006-05-08 03:20:50 +0000469
470 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000471 len = udhcp_recv_packet(&packet, sockfd);
Denis Vlasenkob76b9a42008-01-25 22:46:34 +0000472 else
473 len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000474
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000475 if (len == -1) { /* error is severe, reopen socket */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000476 DEBUG("error on read, %s, reopening socket", strerror(errno));
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000477 change_listen_mode(listen_mode); /* just close and reopen */
Mike Frysinger7031f622006-05-08 03:20:50 +0000478 }
479 if (len < 0) continue;
480
481 if (packet.xid != xid) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000482 DEBUG("Ignoring XID %x (our xid is %x)",
483 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000484 continue;
485 }
486
487 /* Ignore packets that aren't for us */
488 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000489 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000490 continue;
491 }
492
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000493 message = get_option(&packet, DHCP_MESSAGE_TYPE);
494 if (message == NULL) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000495 bb_error_msg("cannot get message type from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000496 continue;
497 }
498
499 switch (state) {
500 case INIT_SELECTING:
501 /* Must be a DHCPOFFER to one of our xid's */
502 if (*message == DHCPOFFER) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000503 /* TODO: why we don't just fetch server's IP from IP header? */
Denis Vlasenko239369b2006-09-07 17:05:44 +0000504 temp = get_option(&packet, DHCP_SERVER_ID);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000505 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000506 bb_error_msg("no server ID in message");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000507 continue;
508 /* still selecting - this server looks bad */
Mike Frysinger7031f622006-05-08 03:20:50 +0000509 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000510 /* can be misaligned, thus memcpy */
511 memcpy(&server_addr, temp, 4);
512 xid = packet.xid;
513 requested_ip = packet.yiaddr;
514
515 /* enter requesting state */
516 state = REQUESTING;
517 timeout = 0;
518 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000519 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000520 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000521 case RENEW_REQUESTED:
522 case REQUESTING:
523 case RENEWING:
524 case REBINDING:
525 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000526 temp = get_option(&packet, DHCP_LEASE_TIME);
527 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000528 bb_error_msg("no lease time with ACK, using 1 hour lease");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000529 lease_seconds = 60 * 60;
Mike Frysinger7031f622006-05-08 03:20:50 +0000530 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000531 /* can be misaligned, thus memcpy */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000532 memcpy(&lease_seconds, temp, 4);
533 lease_seconds = ntohl(lease_seconds);
Mike Frysinger7031f622006-05-08 03:20:50 +0000534 }
Denis Vlasenko223bc972007-11-22 00:58:49 +0000535#if ENABLE_FEATURE_UDHCPC_ARPING
536 if (opt & OPT_a) {
537 if (!arpping(packet.yiaddr,
538 (uint32_t) 0,
539 client_config.arp,
540 client_config.interface)
541 ) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000542 bb_info_msg("offered address is in use "
543 "(got ARP reply), declining");
Denis Vlasenkoca9635b2008-01-25 19:27:08 +0000544 send_decline(xid, server_addr, packet.yiaddr);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000545
546 if (state != REQUESTING)
547 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000548 change_listen_mode(LISTEN_RAW);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000549 state = INIT_SELECTING;
550 requested_ip = 0;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000551 timeout = tryagain_timeout;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000552 packet_num = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000553 continue; /* back to main loop */
Denis Vlasenko223bc972007-11-22 00:58:49 +0000554 }
555 }
556#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000557 /* enter bound state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000558 t1 = lease_seconds / 2;
Mike Frysinger7031f622006-05-08 03:20:50 +0000559
560 /* little fixed point for n * .875 */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000561 t2 = (lease_seconds * 7) >> 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000562 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000563 bb_info_msg("Lease of %s obtained, lease time %u",
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000564 inet_ntoa(temp_addr), (unsigned)lease_seconds);
565 timestamp_got_lease = monotonic_sec();
566 timeout = t1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000567 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000568 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000569 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
570
571 state = BOUND;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000572 change_listen_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000573 if (client_config.quit_after_lease) {
574 if (client_config.release_on_quit)
575 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000576 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000577 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000578 if (!client_config.foreground)
579 client_background();
580
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000581 continue; /* back to main loop */
582 }
583 if (*message == DHCPNAK) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000584 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000585 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000586 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000587 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000588 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000589 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +0000590 sleep(3); /* avoid excessive network traffic */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000591 state = INIT_SELECTING;
592 requested_ip = 0;
593 timeout = 0;
594 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000595 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000596 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000597 /* case BOUND, RELEASED: - ignore all packets */
598 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000599 continue; /* back to main loop */
600 }
601
602 /* select() didn't timeout, something did happen.
603 * But it wasn't a packet. It's a signal pipe then. */
604 {
Denis Vlasenko80edead2007-08-02 22:31:05 +0000605 int signo = udhcp_sp_read(&rfds);
606 switch (signo) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000607 case SIGUSR1:
608 perform_renew();
Denis Vlasenkob539c842007-11-29 08:17:45 +0000609 /* start things over */
610 packet_num = 0;
611 /* Kill any timeouts because the user wants this to hurry along */
612 timeout = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000613 break;
614 case SIGUSR2:
615 perform_release();
616 break;
617 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000618 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000619 if (client_config.release_on_quit)
620 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000621 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000622 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000623 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000624 } /* for (;;) - main loop ends */
625
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000626 ret0:
627 retval = 0;
628 ret:
Denis Vlasenkobb23c062007-08-15 20:05:37 +0000629 /*if (client_config.pidfile) - remove_pidfile has it's own check */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000630 remove_pidfile(client_config.pidfile);
631 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000632}