blob: d8077f7e8c709e1777e55d14a59c15f7fd8a49fa [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 Vlasenko223bc972007-11-22 00:58:49 +0000136#if ENABLE_FEATURE_UDHCPC_ARPING
Denis Vlasenko223bc972007-11-22 00:58:49 +0000137 char *str_W;
138#endif
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000139 int tryagain_timeout = 20;
140 int discover_timeout = 3;
141 int discover_retries = 3;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000142 uint32_t xid = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000143 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000144 int packet_num;
145 /* t1, t2... what a wonderful names... */
146 unsigned t1 = t1; /* for gcc */
147 unsigned t2 = t2;
148 unsigned timestamp_got_lease = timestamp_got_lease;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000149 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000150 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000151 int retval;
152 int len;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000153 struct timeval tv;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000154 struct in_addr temp_addr;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000155 struct dhcpMessage packet;
156 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000157
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000158 enum {
159 OPT_c = 1 << 0,
160 OPT_C = 1 << 1,
161 OPT_V = 1 << 2,
162 OPT_f = 1 << 3,
163 OPT_b = 1 << 4,
164 OPT_H = 1 << 5,
165 OPT_h = 1 << 6,
166 OPT_F = 1 << 7,
167 OPT_i = 1 << 8,
168 OPT_n = 1 << 9,
169 OPT_p = 1 << 10,
170 OPT_q = 1 << 11,
171 OPT_R = 1 << 12,
172 OPT_r = 1 << 13,
173 OPT_s = 1 << 14,
174 OPT_T = 1 << 15,
175 OPT_t = 1 << 16,
176 OPT_v = 1 << 17,
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000177 OPT_S = 1 << 18,
Paul Fox49cce2b2007-11-07 16:01:28 +0000178 OPT_A = 1 << 19,
Denis Vlasenko223bc972007-11-22 00:58:49 +0000179#if ENABLE_FEATURE_UDHCPC_ARPING
180 OPT_a = 1 << 20,
181 OPT_W = 1 << 21,
182#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000183 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000184#if ENABLE_GETOPT_LONG
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000185 static const char udhcpc_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000186 "clientid\0" Required_argument "c"
187 "clientid-none\0" No_argument "C"
188 "vendorclass\0" Required_argument "V"
189 "foreground\0" No_argument "f"
190 "background\0" No_argument "b"
191 "hostname\0" Required_argument "H"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000192 "fqdn\0" Required_argument "F"
193 "interface\0" Required_argument "i"
194 "now\0" No_argument "n"
195 "pidfile\0" Required_argument "p"
196 "quit\0" No_argument "q"
197 "release\0" No_argument "R"
198 "request\0" Required_argument "r"
199 "script\0" Required_argument "s"
200 "timeout\0" Required_argument "T"
201 "version\0" No_argument "v"
202 "retries\0" Required_argument "t"
Paul Fox49cce2b2007-11-07 16:01:28 +0000203 "tryagain\0" Required_argument "A"
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000204 "syslog\0" No_argument "S"
Denis Vlasenko223bc972007-11-22 00:58:49 +0000205#if ENABLE_FEATURE_UDHCPC_ARPING
206 "arping\0" No_argument "a"
Denis Vlasenko223bc972007-11-22 00:58:49 +0000207#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000208 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000209#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000210 /* Default options. */
211 client_config.interface = "eth0";
212 client_config.script = DEFAULT_SCRIPT;
Mike Frysinger7031f622006-05-08 03:20:50 +0000213
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000214 /* Parse command line */
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000215 opt_complementary = "c--C:C--c"; // mutually exclusive
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000216#if ENABLE_GETOPT_LONG
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000217 applet_long_options = udhcpc_longopts;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000218#endif
Denis Vlasenko223bc972007-11-22 00:58:49 +0000219 opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
220 USE_FEATURE_UDHCPC_ARPING("aW:")
221 , &str_c, &str_V, &str_h, &str_h, &str_F,
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000222 &client_config.interface, &client_config.pidfile, &str_r,
Paul Fox49cce2b2007-11-07 16:01:28 +0000223 &client_config.script, &str_T, &str_t, &str_A
Denis Vlasenko223bc972007-11-22 00:58:49 +0000224 USE_FEATURE_UDHCPC_ARPING(, &str_W)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000225 );
226
227 if (opt & OPT_c)
228 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000229 //if (opt & OPT_C)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000230 if (opt & OPT_V)
231 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
232 if (opt & OPT_f)
233 client_config.foreground = 1;
234 if (opt & OPT_b)
235 client_config.background_if_no_lease = 1;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000236 if (opt & (OPT_h|OPT_H))
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000237 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
238 if (opt & OPT_F) {
239 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
240 /* Flags: 0000NEOS
241 S: 1 => Client requests Server to update A RR in DNS as well as PTR
242 O: 1 => Server indicates to client that DNS has been updated regardless
243 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
244 N: 1 => Client requests Server to not update DNS
245 */
246 client_config.fqdn[OPT_DATA + 0] = 0x1;
247 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
248 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
249 }
250 // if (opt & OPT_i) client_config.interface = ...
251 if (opt & OPT_n)
252 client_config.abort_if_no_lease = 1;
253 // if (opt & OPT_p) client_config.pidfile = ...
254 if (opt & OPT_q)
255 client_config.quit_after_lease = 1;
256 if (opt & OPT_R)
257 client_config.release_on_quit = 1;
258 if (opt & OPT_r)
259 requested_ip = inet_addr(str_r);
260 // if (opt & OPT_s) client_config.script = ...
261 if (opt & OPT_T)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000262 discover_timeout = xatoi_u(str_T);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000263 if (opt & OPT_t)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000264 discover_retries = xatoi_u(str_t);
Paul Fox49cce2b2007-11-07 16:01:28 +0000265 if (opt & OPT_A)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000266 tryagain_timeout = xatoi_u(str_A);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000267 if (opt & OPT_v) {
Denis Vlasenkodef88982007-10-07 17:06:01 +0000268 puts("version "BB_VER);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000269 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000270 }
271
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000272 if (opt & OPT_S) {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000273 openlog(applet_name, LOG_PID, LOG_LOCAL0);
274 logmode |= LOGMODE_SYSLOG;
275 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000276
277 if (read_interface(client_config.interface, &client_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +0000278 NULL, client_config.arp))
Mike Frysinger7031f622006-05-08 03:20:50 +0000279 return 1;
280
Denis Vlasenko80edead2007-08-02 22:31:05 +0000281 /* Make sure fd 0,1,2 are open */
282 bb_sanitize_stdio();
283 /* Equivalent of doing a fflush after every \n */
284 setlinebuf(stdout);
285
286 /* Create pidfile */
287 write_pidfile(client_config.pidfile);
288 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
289
290 /* Goes to stdout and possibly syslog */
Denis Vlasenkodef88982007-10-07 17:06:01 +0000291 bb_info_msg("%s (v"BB_VER") started", applet_name);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000292
Mike Frysinger7031f622006-05-08 03:20:50 +0000293 /* if not set, and not suppressed, setup the default client ID */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000294 if (!client_config.clientid && !(opt & OPT_C)) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000295 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000296 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000297 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000298 }
299
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000300 if (!client_config.vendorclass)
301 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000302
303 /* setup the signal pipe */
304 udhcp_sp_setup();
305
306 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000307 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000308 change_listen_mode(LISTEN_RAW);
Denis Vlasenkob539c842007-11-29 08:17:45 +0000309 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000310
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000311 /* Main event loop. select() waits on signal pipe and possibly
312 * on sockfd.
313 * "continue" statements in code below jump to the top of the loop.
314 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000315 for (;;) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000316 tv.tv_sec = timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000317 tv.tv_usec = 0;
318
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000319 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000320 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000321 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000322 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000323 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000324 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000325 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000326
Denis Vlasenko80edead2007-08-02 22:31:05 +0000327 retval = 0; /* If we already timed out, fall through, else... */
Mike Frysinger7031f622006-05-08 03:20:50 +0000328 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000329 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000330 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000331 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000332
Denis Vlasenko80edead2007-08-02 22:31:05 +0000333 if (retval < 0) {
334 /* EINTR? signal was caught, don't panic */
335 if (errno != EINTR) {
336 /* Else: an error occured, panic! */
337 bb_perror_msg_and_die("select");
338 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000339 continue;
340 }
341
342 /* If timeout dropped to zero, time to become active:
343 * resend discover/renew/whatever
344 */
345 if (retval == 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000346 switch (state) {
347 case INIT_SELECTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000348 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000349 if (packet_num == 0)
350 xid = random_xid();
351
352 /* send discover packet */
353 send_discover(xid, requested_ip); /* broadcast */
354
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000355 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000356 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000357 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000358 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000359 udhcp_run_script(NULL, "leasefail");
360 if (client_config.background_if_no_lease) {
361 bb_info_msg("No lease, forking to background");
362 client_background();
363 } else if (client_config.abort_if_no_lease) {
364 bb_info_msg("No lease, failing");
365 retval = 1;
366 goto ret;
367 }
368 /* wait to try again */
369 timeout = tryagain_timeout;
370 packet_num = 0;
371 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000372 case RENEW_REQUESTED:
373 case REQUESTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000374 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000375 /* send request packet */
376 if (state == RENEW_REQUESTED)
377 send_renew(xid, server_addr, requested_ip); /* unicast */
378 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
379
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000380 timeout = ((packet_num == 2) ? 10 : 2);
Mike Frysinger7031f622006-05-08 03:20:50 +0000381 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000382 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000383 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000384 /* timed out, go back to init state */
385 if (state == RENEW_REQUESTED)
386 udhcp_run_script(NULL, "deconfig");
387 change_listen_mode(LISTEN_RAW);
388 state = INIT_SELECTING;
389 timeout = 0;
390 packet_num = 0;
391 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000392 case BOUND:
393 /* Lease is starting to run out, time to enter renewing state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000394 change_listen_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000395 DEBUG("Entering renew state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000396 state = RENEWING;
Mike Frysinger7031f622006-05-08 03:20:50 +0000397 /* fall right through */
398 case RENEWING:
399 /* Either set a new T1, or enter REBINDING state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000400 if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000401 /* send a request packet */
402 send_renew(xid, server_addr, requested_ip); /* unicast */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000403 t1 += (t2 - t1) / 2;
404 timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease);
405 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000406 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000407 /* Timed out, enter rebinding state */
408 DEBUG("Entering rebinding state");
409 state = REBINDING;
410 timeout = (t2 - t1);
411 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000412 case REBINDING:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000413 /* Lease is *really* about to run out,
414 * try to find DHCP server using broadcast */
415 if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000416 /* send a request packet */
417 send_renew(xid, 0, requested_ip); /* broadcast */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000418 t2 += (lease_seconds - t2) / 2;
419 timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease);
420 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000421 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000422 /* Timed out, enter init state */
423 bb_info_msg("Lease lost, entering init state");
424 udhcp_run_script(NULL, "deconfig");
425 change_listen_mode(LISTEN_RAW);
426 state = INIT_SELECTING;
427 timeout = 0;
428 packet_num = 0;
429 continue;
430 /* case RELEASED: */
Mike Frysinger7031f622006-05-08 03:20:50 +0000431 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000432 /* yah, I know, *you* say it would never happen */
433 timeout = INT_MAX;
434 continue; /* back to main loop */
435 }
436
437 /* select() didn't timeout, something did happen. */
438 /* Is is a packet? */
439 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
440 /* A packet is ready, read it */
Mike Frysinger7031f622006-05-08 03:20:50 +0000441
442 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000443 len = udhcp_get_packet(&packet, sockfd);
444 else len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000445
446 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000447 DEBUG("error on read, %s, reopening socket", strerror(errno));
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000448 change_listen_mode(listen_mode); /* just close and reopen */
Mike Frysinger7031f622006-05-08 03:20:50 +0000449 }
450 if (len < 0) continue;
451
452 if (packet.xid != xid) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000453 DEBUG("Ignoring XID %x (our xid is %x)",
454 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000455 continue;
456 }
457
458 /* Ignore packets that aren't for us */
459 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000460 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000461 continue;
462 }
463
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000464 message = get_option(&packet, DHCP_MESSAGE_TYPE);
465 if (message == NULL) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000466 bb_error_msg("cannot get message type from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000467 continue;
468 }
469
470 switch (state) {
471 case INIT_SELECTING:
472 /* Must be a DHCPOFFER to one of our xid's */
473 if (*message == DHCPOFFER) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000474 /* TODO: why we don't just fetch server's IP from IP header? */
Denis Vlasenko239369b2006-09-07 17:05:44 +0000475 temp = get_option(&packet, DHCP_SERVER_ID);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000476 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000477 bb_error_msg("no server ID in message");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000478 continue;
479 /* still selecting - this server looks bad */
Mike Frysinger7031f622006-05-08 03:20:50 +0000480 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000481 /* can be misaligned, thus memcpy */
482 memcpy(&server_addr, temp, 4);
483 xid = packet.xid;
484 requested_ip = packet.yiaddr;
485
486 /* enter requesting state */
487 state = REQUESTING;
488 timeout = 0;
489 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000490 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000491 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000492 case RENEW_REQUESTED:
493 case REQUESTING:
494 case RENEWING:
495 case REBINDING:
496 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000497 temp = get_option(&packet, DHCP_LEASE_TIME);
498 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000499 bb_error_msg("no lease time with ACK, using 1 hour lease");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000500 lease_seconds = 60 * 60;
Mike Frysinger7031f622006-05-08 03:20:50 +0000501 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000502 /* can be misaligned, thus memcpy */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000503 memcpy(&lease_seconds, temp, 4);
504 lease_seconds = ntohl(lease_seconds);
Mike Frysinger7031f622006-05-08 03:20:50 +0000505 }
Denis Vlasenko223bc972007-11-22 00:58:49 +0000506#if ENABLE_FEATURE_UDHCPC_ARPING
507 if (opt & OPT_a) {
508 if (!arpping(packet.yiaddr,
509 (uint32_t) 0,
510 client_config.arp,
511 client_config.interface)
512 ) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000513 bb_info_msg("offered address is in use "
514 "(got ARP reply), declining");
Denis Vlasenko223bc972007-11-22 00:58:49 +0000515 send_decline(xid, server_addr);
516
517 if (state != REQUESTING)
518 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000519 change_listen_mode(LISTEN_RAW);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000520 state = INIT_SELECTING;
521 requested_ip = 0;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000522 timeout = tryagain_timeout;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000523 packet_num = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000524 continue; /* back to main loop */
Denis Vlasenko223bc972007-11-22 00:58:49 +0000525 }
526 }
527#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000528 /* enter bound state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000529 t1 = lease_seconds / 2;
Mike Frysinger7031f622006-05-08 03:20:50 +0000530
531 /* little fixed point for n * .875 */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000532 t2 = (lease_seconds * 7) >> 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000533 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000534 bb_info_msg("Lease of %s obtained, lease time %u",
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000535 inet_ntoa(temp_addr), (unsigned)lease_seconds);
536 timestamp_got_lease = monotonic_sec();
537 timeout = t1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000538 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000539 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000540 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
541
542 state = BOUND;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000543 change_listen_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000544 if (client_config.quit_after_lease) {
545 if (client_config.release_on_quit)
546 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000547 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000548 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000549 if (!client_config.foreground)
550 client_background();
551
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000552 continue; /* back to main loop */
553 }
554 if (*message == DHCPNAK) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000555 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000556 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000557 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000558 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000559 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000560 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +0000561 sleep(3); /* avoid excessive network traffic */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000562 state = INIT_SELECTING;
563 requested_ip = 0;
564 timeout = 0;
565 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000566 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000567 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000568 /* case BOUND, RELEASED: - ignore all packets */
569 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000570 continue; /* back to main loop */
571 }
572
573 /* select() didn't timeout, something did happen.
574 * But it wasn't a packet. It's a signal pipe then. */
575 {
Denis Vlasenko80edead2007-08-02 22:31:05 +0000576 int signo = udhcp_sp_read(&rfds);
577 switch (signo) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000578 case SIGUSR1:
579 perform_renew();
Denis Vlasenkob539c842007-11-29 08:17:45 +0000580 /* start things over */
581 packet_num = 0;
582 /* Kill any timeouts because the user wants this to hurry along */
583 timeout = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000584 break;
585 case SIGUSR2:
586 perform_release();
587 break;
588 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000589 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000590 if (client_config.release_on_quit)
591 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000592 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000593 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000594 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000595 } /* for (;;) - main loop ends */
596
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000597 ret0:
598 retval = 0;
599 ret:
Denis Vlasenkobb23c062007-08-15 20:05:37 +0000600 /*if (client_config.pidfile) - remove_pidfile has it's own check */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000601 remove_pidfile(client_config.pidfile);
602 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000603}