blob: eeef435de18b2861531646496c272b9264db9d84 [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
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000011#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000012
Denis Vlasenko1caca342007-08-02 10:14:29 +000013/* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
14#define WANT_PIDFILE 1
Mike Frysinger7031f622006-05-08 03:20:50 +000015#include "common.h"
16#include "dhcpd.h"
17#include "dhcpc.h"
18#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000019
Mike Frysinger7031f622006-05-08 03:20:50 +000020
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000021static int timeout; /* = 0. Must be signed */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000022static uint32_t requested_ip; /* = 0 */
Denis Vlasenko74c9d232007-01-18 15:42:00 +000023static uint32_t server_addr;
Denis Vlasenkofbd29182007-04-07 01:05:47 +000024static int sockfd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000025
26#define LISTEN_NONE 0
27#define LISTEN_KERNEL 1
28#define LISTEN_RAW 2
Denis Vlasenkofbd29182007-04-07 01:05:47 +000029static smallint listen_mode;
30
31static smallint state;
Mike Frysinger7031f622006-05-08 03:20:50 +000032
Denis Vlasenkodeabacd2007-09-30 17:55:43 +000033/* struct client_config_t client_config is in bb_common_bufsiz1 */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000034
Mike Frysinger7031f622006-05-08 03:20:50 +000035
Mike Frysinger7031f622006-05-08 03:20:50 +000036/* just a little helper */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000037static void change_listen_mode(int new_mode)
Mike Frysinger7031f622006-05-08 03:20:50 +000038{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000039 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000040 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Denis Vlasenkofbd29182007-04-07 01:05:47 +000041 if (sockfd >= 0) {
42 close(sockfd);
43 sockfd = -1;
44 }
Mike Frysinger7031f622006-05-08 03:20:50 +000045 listen_mode = new_mode;
46}
47
48
49/* perform a renew */
50static void perform_renew(void)
51{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000052 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000053 switch (state) {
54 case BOUND:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000055 change_listen_mode(LISTEN_KERNEL);
Mike Frysinger7031f622006-05-08 03:20:50 +000056 case RENEWING:
57 case REBINDING:
58 state = RENEW_REQUESTED;
59 break;
60 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000061 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000062 case REQUESTING:
63 case RELEASED:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000064 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +000065 state = INIT_SELECTING;
66 break;
67 case INIT_SELECTING:
68 break;
69 }
Mike Frysinger7031f622006-05-08 03:20:50 +000070}
71
72
73/* perform a release */
74static void perform_release(void)
75{
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000076 char buffer[sizeof("255.255.255.255")];
Mike Frysinger7031f622006-05-08 03:20:50 +000077 struct in_addr temp_addr;
78
79 /* send release packet */
80 if (state == BOUND || state == RENEWING || state == REBINDING) {
81 temp_addr.s_addr = server_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000082 strcpy(buffer, inet_ntoa(temp_addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000083 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000084 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000085 inet_ntoa(temp_addr), buffer);
86 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +000087 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000088 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000089 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +000090
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000091 change_listen_mode(LISTEN_NONE);
Mike Frysinger7031f622006-05-08 03:20:50 +000092 state = RELEASED;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000093 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +000094}
95
96
97static void client_background(void)
98{
Denis Vlasenko80edead2007-08-02 22:31:05 +000099#if !BB_MMU
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000100 bb_error_msg("cannot background in uclinux (yet)");
101/* ... mainly because udhcpc calls client_background()
102 * in _the _middle _of _udhcpc _run_, not at the start!
103 * If that will be properly disabled for NOMMU, client_background()
104 * will work on NOMMU too */
105#else
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000106 bb_daemonize(0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000107 logmode &= ~LOGMODE_STDIO;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000108 /* rewrite pidfile, as our pid is different now */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000109 write_pidfile(client_config.pidfile);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000110#endif
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000111 /* Do not fork again. */
112 client_config.foreground = 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000113 client_config.background_if_no_lease = 0;
114}
115
116
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000117static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
118{
119 uint8_t *storage;
120 int len = strlen(str);
121 if (len > 255) len = 255;
122 storage = xzalloc(len + extra + OPT_DATA);
123 storage[OPT_CODE] = code;
124 storage[OPT_LEN] = len + extra;
125 memcpy(storage + extra + OPT_DATA, str, len);
126 return storage;
127}
128
129
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000130int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +0000131int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000132{
133 uint8_t *temp, *message;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000134 char *str_c, *str_V, *str_h, *str_F, *str_r;
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000135 USE_FEATURE_UDHCP_PORT(char *str_P;)
Denis Vlasenko19183682007-12-10 07:03:38 +0000136 llist_t *list_O = NULL;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000137#if ENABLE_FEATURE_UDHCPC_ARPING
Denis Vlasenko223bc972007-11-22 00:58:49 +0000138 char *str_W;
139#endif
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000140 int tryagain_timeout = 20;
141 int discover_timeout = 3;
142 int discover_retries = 3;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000143 uint32_t xid = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000144 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000145 int packet_num;
146 /* t1, t2... what a wonderful names... */
147 unsigned t1 = t1; /* for gcc */
148 unsigned t2 = t2;
149 unsigned timestamp_got_lease = timestamp_got_lease;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000150 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000151 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000152 int retval;
153 int len;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000154 struct timeval tv;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000155 struct in_addr temp_addr;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000156 struct dhcpMessage packet;
157 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000158
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000159 enum {
160 OPT_c = 1 << 0,
161 OPT_C = 1 << 1,
162 OPT_V = 1 << 2,
163 OPT_f = 1 << 3,
164 OPT_b = 1 << 4,
165 OPT_H = 1 << 5,
166 OPT_h = 1 << 6,
167 OPT_F = 1 << 7,
168 OPT_i = 1 << 8,
169 OPT_n = 1 << 9,
170 OPT_p = 1 << 10,
171 OPT_q = 1 << 11,
172 OPT_R = 1 << 12,
173 OPT_r = 1 << 13,
174 OPT_s = 1 << 14,
175 OPT_T = 1 << 15,
176 OPT_t = 1 << 16,
177 OPT_v = 1 << 17,
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000178 OPT_S = 1 << 18,
Paul Fox49cce2b2007-11-07 16:01:28 +0000179 OPT_A = 1 << 19,
Denis Vlasenko223bc972007-11-22 00:58:49 +0000180#if ENABLE_FEATURE_UDHCPC_ARPING
181 OPT_a = 1 << 20,
182 OPT_W = 1 << 21,
183#endif
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000184 OPT_P = 1 << 22,
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000185 OPT_o = 1 << 23,
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 Vlasenko2e4c3c42008-04-02 13:04:19 +0000215 "no-default-options\0" No_argument "o"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000216 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000217#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000218 /* Default options. */
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000219#if ENABLE_FEATURE_UDHCP_PORT
220 SERVER_PORT = 67;
221 CLIENT_PORT = 68;
222#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000223 client_config.interface = "eth0";
224 client_config.script = DEFAULT_SCRIPT;
Mike Frysinger7031f622006-05-08 03:20:50 +0000225
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000226 /* Parse command line */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000227 /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */
228 opt_complementary = "c--C:C--c:O::T+:t+:A+";
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000229#if ENABLE_GETOPT_LONG
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000230 applet_long_options = udhcpc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000231#endif
Denis Vlasenko223bc972007-11-22 00:58:49 +0000232 opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
233 USE_FEATURE_UDHCPC_ARPING("aW:")
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000234 USE_FEATURE_UDHCP_PORT("P:")
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000235 "O:o"
Denis Vlasenko19183682007-12-10 07:03:38 +0000236 , &str_c, &str_V, &str_h, &str_h, &str_F
237 , &client_config.interface, &client_config.pidfile, &str_r
Denis Vlasenko1d426652008-03-17 09:09:09 +0000238 , &client_config.script
239 , &discover_timeout, &discover_retries, &tryagain_timeout
Denis Vlasenko223bc972007-11-22 00:58:49 +0000240 USE_FEATURE_UDHCPC_ARPING(, &str_W)
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000241 USE_FEATURE_UDHCP_PORT(, &str_P)
Denis Vlasenko19183682007-12-10 07:03:38 +0000242 , &list_O
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000243 );
244
245 if (opt & OPT_c)
246 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000247 //if (opt & OPT_C)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000248 if (opt & OPT_V)
249 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
250 if (opt & OPT_f)
251 client_config.foreground = 1;
252 if (opt & OPT_b)
253 client_config.background_if_no_lease = 1;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000254 if (opt & (OPT_h|OPT_H))
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000255 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
256 if (opt & OPT_F) {
257 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
258 /* Flags: 0000NEOS
259 S: 1 => Client requests Server to update A RR in DNS as well as PTR
260 O: 1 => Server indicates to client that DNS has been updated regardless
261 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
262 N: 1 => Client requests Server to not update DNS
263 */
264 client_config.fqdn[OPT_DATA + 0] = 0x1;
265 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
266 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
267 }
268 // if (opt & OPT_i) client_config.interface = ...
269 if (opt & OPT_n)
270 client_config.abort_if_no_lease = 1;
271 // if (opt & OPT_p) client_config.pidfile = ...
272 if (opt & OPT_q)
273 client_config.quit_after_lease = 1;
274 if (opt & OPT_R)
275 client_config.release_on_quit = 1;
276 if (opt & OPT_r)
277 requested_ip = inet_addr(str_r);
278 // if (opt & OPT_s) client_config.script = ...
Denis Vlasenko1d426652008-03-17 09:09:09 +0000279 // if (opt & OPT_T) discover_timeout = xatoi_u(str_T);
280 // if (opt & OPT_t) discover_retries = xatoi_u(str_t);
281 // if (opt & OPT_A) 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 Vlasenko2e4c3c42008-04-02 13:04:19 +0000296 if (opt & OPT_o)
297 client_config.no_default_options = 1;
Denis Vlasenko19183682007-12-10 07:03:38 +0000298 while (list_O) {
299 int n = index_in_strings(dhcp_option_strings, list_O->data);
300 if (n < 0)
301 bb_error_msg_and_die("unknown option '%s'", list_O->data);
302 n = dhcp_options[n].code;
303 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
304 list_O = list_O->link;
305 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000306
307 if (read_interface(client_config.interface, &client_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +0000308 NULL, client_config.arp))
Mike Frysinger7031f622006-05-08 03:20:50 +0000309 return 1;
310
Denis Vlasenko80edead2007-08-02 22:31:05 +0000311 /* Make sure fd 0,1,2 are open */
312 bb_sanitize_stdio();
313 /* Equivalent of doing a fflush after every \n */
314 setlinebuf(stdout);
315
316 /* Create pidfile */
317 write_pidfile(client_config.pidfile);
318 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
319
320 /* Goes to stdout and possibly syslog */
Denis Vlasenkodef88982007-10-07 17:06:01 +0000321 bb_info_msg("%s (v"BB_VER") started", applet_name);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000322
Mike Frysinger7031f622006-05-08 03:20:50 +0000323 /* if not set, and not suppressed, setup the default client ID */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000324 if (!client_config.clientid && !(opt & OPT_C)) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000325 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000326 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000327 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000328 }
329
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000330 if (!client_config.vendorclass)
331 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000332
333 /* setup the signal pipe */
334 udhcp_sp_setup();
335
336 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000337 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000338 change_listen_mode(LISTEN_RAW);
Denis Vlasenkob539c842007-11-29 08:17:45 +0000339 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000340
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000341 /* Main event loop. select() waits on signal pipe and possibly
342 * on sockfd.
343 * "continue" statements in code below jump to the top of the loop.
344 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000345 for (;;) {
Denis Vlasenkob2342912008-05-21 07:02:16 +0000346 unsigned timestamp_before_wait;
Mike Frysinger7031f622006-05-08 03:20:50 +0000347
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000348 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000349 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000350 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000351 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000352 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000353 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000354 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000355
Denis Vlasenkob2342912008-05-21 07:02:16 +0000356 tv.tv_sec = timeout;
357 tv.tv_usec = 0;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000358 retval = 0; /* If we already timed out, fall through, else... */
Mike Frysinger7031f622006-05-08 03:20:50 +0000359 if (tv.tv_sec > 0) {
Denis Vlasenkob2342912008-05-21 07:02:16 +0000360 timestamp_before_wait = (unsigned)monotonic_sec();
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000361 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000362 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenkob2342912008-05-21 07:02:16 +0000363 if (retval < 0) {
364 /* EINTR? A signal was caught, don't panic */
365 if (errno == EINTR)
366 continue;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000367 /* Else: an error occured, panic! */
368 bb_perror_msg_and_die("select");
369 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000370 }
371
372 /* If timeout dropped to zero, time to become active:
373 * resend discover/renew/whatever
374 */
375 if (retval == 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000376 switch (state) {
377 case INIT_SELECTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000378 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000379 if (packet_num == 0)
380 xid = random_xid();
381
382 /* send discover packet */
383 send_discover(xid, requested_ip); /* broadcast */
384
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000385 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000386 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000387 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000388 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000389 udhcp_run_script(NULL, "leasefail");
390 if (client_config.background_if_no_lease) {
391 bb_info_msg("No lease, forking to background");
392 client_background();
393 } else if (client_config.abort_if_no_lease) {
394 bb_info_msg("No lease, failing");
395 retval = 1;
396 goto ret;
397 }
398 /* wait to try again */
399 timeout = tryagain_timeout;
400 packet_num = 0;
401 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000402 case RENEW_REQUESTED:
403 case REQUESTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000404 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000405 /* send request packet */
406 if (state == RENEW_REQUESTED)
407 send_renew(xid, server_addr, requested_ip); /* unicast */
408 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
409
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000410 timeout = ((packet_num == 2) ? 10 : 2);
Mike Frysinger7031f622006-05-08 03:20:50 +0000411 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000412 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000413 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000414 /* timed out, go back to init state */
415 if (state == RENEW_REQUESTED)
416 udhcp_run_script(NULL, "deconfig");
417 change_listen_mode(LISTEN_RAW);
418 state = INIT_SELECTING;
419 timeout = 0;
420 packet_num = 0;
421 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000422 case BOUND:
423 /* Lease is starting to run out, time to enter renewing state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000424 change_listen_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000425 DEBUG("Entering renew state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000426 state = RENEWING;
Mike Frysinger7031f622006-05-08 03:20:50 +0000427 /* fall right through */
428 case RENEWING:
429 /* Either set a new T1, or enter REBINDING state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000430 if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000431 /* send a request packet */
432 send_renew(xid, server_addr, requested_ip); /* unicast */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000433 t1 += (t2 - t1) / 2;
Denis Vlasenkob2342912008-05-21 07:02:16 +0000434 timeout = t1 - ((unsigned)monotonic_sec() - timestamp_got_lease);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000435 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000436 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000437 /* Timed out, enter rebinding state */
438 DEBUG("Entering rebinding state");
439 state = REBINDING;
440 timeout = (t2 - t1);
441 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000442 case REBINDING:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000443 /* Lease is *really* about to run out,
444 * try to find DHCP server using broadcast */
445 if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000446 /* send a request packet */
447 send_renew(xid, 0, requested_ip); /* broadcast */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000448 t2 += (lease_seconds - t2) / 2;
Denis Vlasenkob2342912008-05-21 07:02:16 +0000449 timeout = t2 - ((unsigned)monotonic_sec() - timestamp_got_lease);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000450 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000451 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000452 /* Timed out, enter init state */
453 bb_info_msg("Lease lost, entering init state");
454 udhcp_run_script(NULL, "deconfig");
455 change_listen_mode(LISTEN_RAW);
456 state = INIT_SELECTING;
457 timeout = 0;
458 packet_num = 0;
459 continue;
460 /* case RELEASED: */
Mike Frysinger7031f622006-05-08 03:20:50 +0000461 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000462 /* yah, I know, *you* say it would never happen */
463 timeout = INT_MAX;
464 continue; /* back to main loop */
465 }
466
467 /* select() didn't timeout, something did happen. */
468 /* Is is a packet? */
469 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
470 /* A packet is ready, read it */
Mike Frysinger7031f622006-05-08 03:20:50 +0000471
472 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000473 len = udhcp_recv_packet(&packet, sockfd);
Denis Vlasenkob76b9a42008-01-25 22:46:34 +0000474 else
475 len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000476
Denis Vlasenkob2342912008-05-21 07:02:16 +0000477 /* If this packet will turn out to be unrelated/bogus,
478 * we will go back and wait for next one.
479 * Be sure timeout is properly decreased. */
480 timeout -= (unsigned)monotonic_sec() - timestamp_before_wait;
481
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000482 if (len == -1) { /* error is severe, reopen socket */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000483 DEBUG("error on read, %s, reopening socket", strerror(errno));
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000484 change_listen_mode(listen_mode); /* just close and reopen */
Mike Frysinger7031f622006-05-08 03:20:50 +0000485 }
486 if (len < 0) continue;
487
488 if (packet.xid != xid) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000489 DEBUG("Ignoring XID %x (our xid is %x)",
490 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000491 continue;
492 }
493
494 /* Ignore packets that aren't for us */
495 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000496 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000497 continue;
498 }
499
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000500 message = get_option(&packet, DHCP_MESSAGE_TYPE);
501 if (message == NULL) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000502 bb_error_msg("cannot get message type from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000503 continue;
504 }
505
506 switch (state) {
507 case INIT_SELECTING:
508 /* Must be a DHCPOFFER to one of our xid's */
509 if (*message == DHCPOFFER) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000510 /* TODO: why we don't just fetch server's IP from IP header? */
Denis Vlasenko239369b2006-09-07 17:05:44 +0000511 temp = get_option(&packet, DHCP_SERVER_ID);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000512 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000513 bb_error_msg("no server ID in message");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000514 continue;
515 /* still selecting - this server looks bad */
Mike Frysinger7031f622006-05-08 03:20:50 +0000516 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000517 /* can be misaligned, thus memcpy */
518 memcpy(&server_addr, temp, 4);
519 xid = packet.xid;
520 requested_ip = packet.yiaddr;
521
522 /* enter requesting state */
523 state = REQUESTING;
524 timeout = 0;
525 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000526 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000527 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000528 case RENEW_REQUESTED:
529 case REQUESTING:
530 case RENEWING:
531 case REBINDING:
532 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000533 temp = get_option(&packet, DHCP_LEASE_TIME);
534 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000535 bb_error_msg("no lease time with ACK, using 1 hour lease");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000536 lease_seconds = 60 * 60;
Mike Frysinger7031f622006-05-08 03:20:50 +0000537 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000538 /* can be misaligned, thus memcpy */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000539 memcpy(&lease_seconds, temp, 4);
540 lease_seconds = ntohl(lease_seconds);
Mike Frysinger7031f622006-05-08 03:20:50 +0000541 }
Denis Vlasenko223bc972007-11-22 00:58:49 +0000542#if ENABLE_FEATURE_UDHCPC_ARPING
543 if (opt & OPT_a) {
544 if (!arpping(packet.yiaddr,
545 (uint32_t) 0,
546 client_config.arp,
547 client_config.interface)
548 ) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000549 bb_info_msg("offered address is in use "
550 "(got ARP reply), declining");
Denis Vlasenkoca9635b2008-01-25 19:27:08 +0000551 send_decline(xid, server_addr, packet.yiaddr);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000552
553 if (state != REQUESTING)
554 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000555 change_listen_mode(LISTEN_RAW);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000556 state = INIT_SELECTING;
557 requested_ip = 0;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000558 timeout = tryagain_timeout;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000559 packet_num = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000560 continue; /* back to main loop */
Denis Vlasenko223bc972007-11-22 00:58:49 +0000561 }
562 }
563#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000564 /* enter bound state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000565 t1 = lease_seconds / 2;
Mike Frysinger7031f622006-05-08 03:20:50 +0000566
567 /* little fixed point for n * .875 */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000568 t2 = (lease_seconds * 7) >> 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000569 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000570 bb_info_msg("Lease of %s obtained, lease time %u",
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000571 inet_ntoa(temp_addr), (unsigned)lease_seconds);
572 timestamp_got_lease = monotonic_sec();
573 timeout = t1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000574 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000575 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000576 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
577
578 state = BOUND;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000579 change_listen_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000580 if (client_config.quit_after_lease) {
581 if (client_config.release_on_quit)
582 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000583 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000584 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000585 if (!client_config.foreground)
586 client_background();
587
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000588 continue; /* back to main loop */
589 }
590 if (*message == DHCPNAK) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000591 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000592 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000593 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000594 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000595 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000596 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +0000597 sleep(3); /* avoid excessive network traffic */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000598 state = INIT_SELECTING;
599 requested_ip = 0;
600 timeout = 0;
601 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000602 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000603 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000604 /* case BOUND, RELEASED: - ignore all packets */
605 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000606 continue; /* back to main loop */
607 }
608
609 /* select() didn't timeout, something did happen.
610 * But it wasn't a packet. It's a signal pipe then. */
611 {
Denis Vlasenko80edead2007-08-02 22:31:05 +0000612 int signo = udhcp_sp_read(&rfds);
613 switch (signo) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000614 case SIGUSR1:
615 perform_renew();
Denis Vlasenkob539c842007-11-29 08:17:45 +0000616 /* start things over */
617 packet_num = 0;
618 /* Kill any timeouts because the user wants this to hurry along */
619 timeout = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000620 break;
621 case SIGUSR2:
622 perform_release();
623 break;
624 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000625 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000626 if (client_config.release_on_quit)
627 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000628 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000629 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000630 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000631 } /* for (;;) - main loop ends */
632
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000633 ret0:
634 retval = 0;
635 ret:
Denis Vlasenkob2342912008-05-21 07:02:16 +0000636 /*if (client_config.pidfile) - remove_pidfile has its own check */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000637 remove_pidfile(client_config.pidfile);
638 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000639}