blob: 234ecfd135f8f7a75d32a2b912d39a31bb59ee5c [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 Vlasenko239369b2006-09-07 17:05:44 +000022/* Something is definitely wrong here. IPv4 addresses
23 * in variables of type long?? BTW, we use inet_ntoa()
24 * in the code. Manpage says that struct in_addr has a member of type long (!)
25 * which holds IPv4 address, and the struct is passed by value (!!)
26 */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000027static int timeout; /* = 0. Must be signed */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000028static uint32_t requested_ip; /* = 0 */
Denis Vlasenko74c9d232007-01-18 15:42:00 +000029static uint32_t server_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000030static int packet_num; /* = 0 */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000031static int sockfd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000032
33#define LISTEN_NONE 0
34#define LISTEN_KERNEL 1
35#define LISTEN_RAW 2
Denis Vlasenkofbd29182007-04-07 01:05:47 +000036static smallint listen_mode;
37
38static smallint state;
Mike Frysinger7031f622006-05-08 03:20:50 +000039
Denis Vlasenkodeabacd2007-09-30 17:55:43 +000040/* struct client_config_t client_config is in bb_common_bufsiz1 */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000041
Mike Frysinger7031f622006-05-08 03:20:50 +000042
Mike Frysinger7031f622006-05-08 03:20:50 +000043/* just a little helper */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000044static void change_listen_mode(int new_mode)
Mike Frysinger7031f622006-05-08 03:20:50 +000045{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000046 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000047 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Denis Vlasenkofbd29182007-04-07 01:05:47 +000048 if (sockfd >= 0) {
49 close(sockfd);
50 sockfd = -1;
51 }
Mike Frysinger7031f622006-05-08 03:20:50 +000052 listen_mode = new_mode;
53}
54
55
56/* perform a renew */
57static void perform_renew(void)
58{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000059 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000060 switch (state) {
61 case BOUND:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000062 change_listen_mode(LISTEN_KERNEL);
Mike Frysinger7031f622006-05-08 03:20:50 +000063 case RENEWING:
64 case REBINDING:
65 state = RENEW_REQUESTED;
66 break;
67 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000068 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000069 case REQUESTING:
70 case RELEASED:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000071 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +000072 state = INIT_SELECTING;
73 break;
74 case INIT_SELECTING:
75 break;
76 }
77
78 /* start things over */
79 packet_num = 0;
80
81 /* Kill any timeouts because the user wants this to hurry along */
82 timeout = 0;
83}
84
85
86/* perform a release */
87static void perform_release(void)
88{
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000089 char buffer[sizeof("255.255.255.255")];
Mike Frysinger7031f622006-05-08 03:20:50 +000090 struct in_addr temp_addr;
91
92 /* send release packet */
93 if (state == BOUND || state == RENEWING || state == REBINDING) {
94 temp_addr.s_addr = server_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000095 strcpy(buffer, inet_ntoa(temp_addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000096 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000097 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000098 inet_ntoa(temp_addr), buffer);
99 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +0000100 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000101 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000102 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000103
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000104 change_listen_mode(LISTEN_NONE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000105 state = RELEASED;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000106 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +0000107}
108
109
110static void client_background(void)
111{
Denis Vlasenko80edead2007-08-02 22:31:05 +0000112#if !BB_MMU
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000113 bb_error_msg("cannot background in uclinux (yet)");
114/* ... mainly because udhcpc calls client_background()
115 * in _the _middle _of _udhcpc _run_, not at the start!
116 * If that will be properly disabled for NOMMU, client_background()
117 * will work on NOMMU too */
118#else
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000119 bb_daemonize(0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000120 logmode &= ~LOGMODE_STDIO;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000121 /* rewrite pidfile, as our pid is different now */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000122 write_pidfile(client_config.pidfile);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000123#endif
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000124 /* Do not fork again. */
125 client_config.foreground = 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000126 client_config.background_if_no_lease = 0;
127}
128
129
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000130static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
131{
132 uint8_t *storage;
133 int len = strlen(str);
134 if (len > 255) len = 255;
135 storage = xzalloc(len + extra + OPT_DATA);
136 storage[OPT_CODE] = code;
137 storage[OPT_LEN] = len + extra;
138 memcpy(storage + extra + OPT_DATA, str, len);
139 return storage;
140}
141
142
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000143int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000144int udhcpc_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000145{
146 uint8_t *temp, *message;
Paul Fox49cce2b2007-11-07 16:01:28 +0000147 char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_A, *str_t;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000148 int tryagain_timeout = 60;
149 int discover_timeout = 3;
150 int discover_retries = 3;
151#if ENABLE_FEATURE_UDHCPC_ARPING
152 int decline_wait = 10;
153 char *str_W;
154#endif
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000155 uint32_t xid = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000156 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000157 unsigned t1 = 0, t2 = 0; /* what a wonderful names */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000158 unsigned timestamp_got_lease = 0; /* for gcc */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000159 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000160 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000161 int retval;
162 int len;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000163 struct timeval tv;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000164 struct in_addr temp_addr;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000165 struct dhcpMessage packet;
166 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000167
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000168 enum {
169 OPT_c = 1 << 0,
170 OPT_C = 1 << 1,
171 OPT_V = 1 << 2,
172 OPT_f = 1 << 3,
173 OPT_b = 1 << 4,
174 OPT_H = 1 << 5,
175 OPT_h = 1 << 6,
176 OPT_F = 1 << 7,
177 OPT_i = 1 << 8,
178 OPT_n = 1 << 9,
179 OPT_p = 1 << 10,
180 OPT_q = 1 << 11,
181 OPT_R = 1 << 12,
182 OPT_r = 1 << 13,
183 OPT_s = 1 << 14,
184 OPT_T = 1 << 15,
185 OPT_t = 1 << 16,
186 OPT_v = 1 << 17,
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000187 OPT_S = 1 << 18,
Paul Fox49cce2b2007-11-07 16:01:28 +0000188 OPT_A = 1 << 19,
Denis Vlasenko223bc972007-11-22 00:58:49 +0000189#if ENABLE_FEATURE_UDHCPC_ARPING
190 OPT_a = 1 << 20,
191 OPT_W = 1 << 21,
192#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000193 };
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000194#if ENABLE_GETOPT_LONG
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000195 static const char udhcpc_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000196 "clientid\0" Required_argument "c"
197 "clientid-none\0" No_argument "C"
198 "vendorclass\0" Required_argument "V"
199 "foreground\0" No_argument "f"
200 "background\0" No_argument "b"
201 "hostname\0" Required_argument "H"
202 "hostname\0" Required_argument "h"
203 "fqdn\0" Required_argument "F"
204 "interface\0" Required_argument "i"
205 "now\0" No_argument "n"
206 "pidfile\0" Required_argument "p"
207 "quit\0" No_argument "q"
208 "release\0" No_argument "R"
209 "request\0" Required_argument "r"
210 "script\0" Required_argument "s"
211 "timeout\0" Required_argument "T"
212 "version\0" No_argument "v"
213 "retries\0" Required_argument "t"
Paul Fox49cce2b2007-11-07 16:01:28 +0000214 "tryagain\0" Required_argument "A"
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000215 "syslog\0" No_argument "S"
Denis Vlasenko223bc972007-11-22 00:58:49 +0000216#if ENABLE_FEATURE_UDHCPC_ARPING
217 "arping\0" No_argument "a"
218 "wait\0" Required_argument "W"
219#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000220 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000221#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000222 /* Default options. */
223 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 Vlasenko09196572007-07-21 13:27:44 +0000227 opt_complementary = "c--C:C--c" // mutually exclusive
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000228 ":hH:Hh"; // -h and -H are the same
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:")
234 , &str_c, &str_V, &str_h, &str_h, &str_F,
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000235 &client_config.interface, &client_config.pidfile, &str_r,
Paul Fox49cce2b2007-11-07 16:01:28 +0000236 &client_config.script, &str_T, &str_t, &str_A
Denis Vlasenko223bc972007-11-22 00:58:49 +0000237 USE_FEATURE_UDHCPC_ARPING(, &str_W)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000238 );
239
240 if (opt & OPT_c)
241 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000242 //if (opt & OPT_C)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000243 if (opt & OPT_V)
244 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
245 if (opt & OPT_f)
246 client_config.foreground = 1;
247 if (opt & OPT_b)
248 client_config.background_if_no_lease = 1;
249 if (opt & OPT_h)
250 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
251 if (opt & OPT_F) {
252 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
253 /* Flags: 0000NEOS
254 S: 1 => Client requests Server to update A RR in DNS as well as PTR
255 O: 1 => Server indicates to client that DNS has been updated regardless
256 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
257 N: 1 => Client requests Server to not update DNS
258 */
259 client_config.fqdn[OPT_DATA + 0] = 0x1;
260 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
261 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
262 }
263 // if (opt & OPT_i) client_config.interface = ...
264 if (opt & OPT_n)
265 client_config.abort_if_no_lease = 1;
266 // if (opt & OPT_p) client_config.pidfile = ...
267 if (opt & OPT_q)
268 client_config.quit_after_lease = 1;
269 if (opt & OPT_R)
270 client_config.release_on_quit = 1;
271 if (opt & OPT_r)
272 requested_ip = inet_addr(str_r);
273 // if (opt & OPT_s) client_config.script = ...
274 if (opt & OPT_T)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000275 discover_timeout = xatoi_u(str_T);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000276 if (opt & OPT_t)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000277 discover_retries = xatoi_u(str_t);
Paul Fox49cce2b2007-11-07 16:01:28 +0000278 if (opt & OPT_A)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000279 tryagain_timeout = xatoi_u(str_A);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000280 if (opt & OPT_v) {
Denis Vlasenkodef88982007-10-07 17:06:01 +0000281 puts("version "BB_VER);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000282 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000283 }
284
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000285 if (opt & OPT_S) {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000286 openlog(applet_name, LOG_PID, LOG_LOCAL0);
287 logmode |= LOGMODE_SYSLOG;
288 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000289
Denis Vlasenko223bc972007-11-22 00:58:49 +0000290#if ENABLE_FEATURE_UDHCPC_ARPING
291 if (opt & OPT_W)
292 decline_wait = xatou_range(str_W, 10, INT_MAX);
293#endif
294
Mike Frysinger7031f622006-05-08 03:20:50 +0000295 if (read_interface(client_config.interface, &client_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +0000296 NULL, client_config.arp))
Mike Frysinger7031f622006-05-08 03:20:50 +0000297 return 1;
298
Denis Vlasenko80edead2007-08-02 22:31:05 +0000299 /* Make sure fd 0,1,2 are open */
300 bb_sanitize_stdio();
301 /* Equivalent of doing a fflush after every \n */
302 setlinebuf(stdout);
303
304 /* Create pidfile */
305 write_pidfile(client_config.pidfile);
306 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
307
308 /* Goes to stdout and possibly syslog */
Denis Vlasenkodef88982007-10-07 17:06:01 +0000309 bb_info_msg("%s (v"BB_VER") started", applet_name);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000310
Mike Frysinger7031f622006-05-08 03:20:50 +0000311 /* if not set, and not suppressed, setup the default client ID */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000312 if (!client_config.clientid && !(opt & OPT_C)) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000313 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000314 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000315 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000316 }
317
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000318 if (!client_config.vendorclass)
319 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000320
321 /* setup the signal pipe */
322 udhcp_sp_setup();
323
324 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000325 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000326 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +0000327
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000328 /* Main event loop. select() waits on signal pipe and possibly
329 * on sockfd.
330 * "continue" statements in code below jump to the top of the loop.
331 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000332 for (;;) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000333 tv.tv_sec = timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000334 tv.tv_usec = 0;
335
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000336 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000337 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000338 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000339 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000340 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000341 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000342 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000343
Denis Vlasenko80edead2007-08-02 22:31:05 +0000344 retval = 0; /* If we already timed out, fall through, else... */
Mike Frysinger7031f622006-05-08 03:20:50 +0000345 if (tv.tv_sec > 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000346 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000347 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000348 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000349
Denis Vlasenko80edead2007-08-02 22:31:05 +0000350 if (retval < 0) {
351 /* EINTR? signal was caught, don't panic */
352 if (errno != EINTR) {
353 /* Else: an error occured, panic! */
354 bb_perror_msg_and_die("select");
355 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000356 continue;
357 }
358
359 /* If timeout dropped to zero, time to become active:
360 * resend discover/renew/whatever
361 */
362 if (retval == 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000363 switch (state) {
364 case INIT_SELECTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000365 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000366 if (packet_num == 0)
367 xid = random_xid();
368
369 /* send discover packet */
370 send_discover(xid, requested_ip); /* broadcast */
371
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000372 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000373 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000374 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000375 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000376 udhcp_run_script(NULL, "leasefail");
377 if (client_config.background_if_no_lease) {
378 bb_info_msg("No lease, forking to background");
379 client_background();
380 } else if (client_config.abort_if_no_lease) {
381 bb_info_msg("No lease, failing");
382 retval = 1;
383 goto ret;
384 }
385 /* wait to try again */
386 timeout = tryagain_timeout;
387 packet_num = 0;
388 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000389 case RENEW_REQUESTED:
390 case REQUESTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000391 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000392 /* send request packet */
393 if (state == RENEW_REQUESTED)
394 send_renew(xid, server_addr, requested_ip); /* unicast */
395 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
396
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000397 timeout = ((packet_num == 2) ? 10 : 2);
Mike Frysinger7031f622006-05-08 03:20:50 +0000398 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000399 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000400 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000401 /* timed out, go back to init state */
402 if (state == RENEW_REQUESTED)
403 udhcp_run_script(NULL, "deconfig");
404 change_listen_mode(LISTEN_RAW);
405 state = INIT_SELECTING;
406 timeout = 0;
407 packet_num = 0;
408 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000409 case BOUND:
410 /* Lease is starting to run out, time to enter renewing state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000411 change_listen_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000412 DEBUG("Entering renew state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000413 state = RENEWING;
Mike Frysinger7031f622006-05-08 03:20:50 +0000414 /* fall right through */
415 case RENEWING:
416 /* Either set a new T1, or enter REBINDING state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000417 if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000418 /* send a request packet */
419 send_renew(xid, server_addr, requested_ip); /* unicast */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000420 t1 += (t2 - t1) / 2;
421 timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease);
422 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000423 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000424 /* Timed out, enter rebinding state */
425 DEBUG("Entering rebinding state");
426 state = REBINDING;
427 timeout = (t2 - t1);
428 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000429 case REBINDING:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000430 /* Lease is *really* about to run out,
431 * try to find DHCP server using broadcast */
432 if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000433 /* send a request packet */
434 send_renew(xid, 0, requested_ip); /* broadcast */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000435 t2 += (lease_seconds - t2) / 2;
436 timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease);
437 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000438 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000439 /* Timed out, enter init state */
440 bb_info_msg("Lease lost, entering init state");
441 udhcp_run_script(NULL, "deconfig");
442 change_listen_mode(LISTEN_RAW);
443 state = INIT_SELECTING;
444 timeout = 0;
445 packet_num = 0;
446 continue;
447 /* case RELEASED: */
Mike Frysinger7031f622006-05-08 03:20:50 +0000448 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000449 /* yah, I know, *you* say it would never happen */
450 timeout = INT_MAX;
451 continue; /* back to main loop */
452 }
453
454 /* select() didn't timeout, something did happen. */
455 /* Is is a packet? */
456 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
457 /* A packet is ready, read it */
Mike Frysinger7031f622006-05-08 03:20:50 +0000458
459 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000460 len = udhcp_get_packet(&packet, sockfd);
461 else len = get_raw_packet(&packet, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000462
463 if (len == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000464 DEBUG("error on read, %s, reopening socket", strerror(errno));
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000465 change_listen_mode(listen_mode); /* just close and reopen */
Mike Frysinger7031f622006-05-08 03:20:50 +0000466 }
467 if (len < 0) continue;
468
469 if (packet.xid != xid) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000470 DEBUG("Ignoring XID %x (our xid is %x)",
471 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000472 continue;
473 }
474
475 /* Ignore packets that aren't for us */
476 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000477 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000478 continue;
479 }
480
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000481 message = get_option(&packet, DHCP_MESSAGE_TYPE);
482 if (message == NULL) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000483 bb_error_msg("cannot get message type from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000484 continue;
485 }
486
487 switch (state) {
488 case INIT_SELECTING:
489 /* Must be a DHCPOFFER to one of our xid's */
490 if (*message == DHCPOFFER) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000491 /* TODO: why we don't just fetch server's IP from IP header? */
Denis Vlasenko239369b2006-09-07 17:05:44 +0000492 temp = get_option(&packet, DHCP_SERVER_ID);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000493 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000494 bb_error_msg("no server ID in message");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000495 continue;
496 /* still selecting - this server looks bad */
Mike Frysinger7031f622006-05-08 03:20:50 +0000497 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000498 /* can be misaligned, thus memcpy */
499 memcpy(&server_addr, temp, 4);
500 xid = packet.xid;
501 requested_ip = packet.yiaddr;
502
503 /* enter requesting state */
504 state = REQUESTING;
505 timeout = 0;
506 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000507 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000508 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000509 case RENEW_REQUESTED:
510 case REQUESTING:
511 case RENEWING:
512 case REBINDING:
513 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000514 temp = get_option(&packet, DHCP_LEASE_TIME);
515 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000516 bb_error_msg("no lease time with ACK, using 1 hour lease");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000517 lease_seconds = 60 * 60;
Mike Frysinger7031f622006-05-08 03:20:50 +0000518 } else {
Denis Vlasenko74c9d232007-01-18 15:42:00 +0000519 /* can be misaligned, thus memcpy */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000520 memcpy(&lease_seconds, temp, 4);
521 lease_seconds = ntohl(lease_seconds);
Mike Frysinger7031f622006-05-08 03:20:50 +0000522 }
Denis Vlasenko223bc972007-11-22 00:58:49 +0000523#if ENABLE_FEATURE_UDHCPC_ARPING
524 if (opt & OPT_a) {
525 if (!arpping(packet.yiaddr,
526 (uint32_t) 0,
527 client_config.arp,
528 client_config.interface)
529 ) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000530 bb_info_msg("offered address is in use "
531 "(got ARP reply), declining");
Denis Vlasenko223bc972007-11-22 00:58:49 +0000532 send_decline(xid, server_addr);
533
534 if (state != REQUESTING)
535 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000536 change_listen_mode(LISTEN_RAW);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000537 state = INIT_SELECTING;
538 requested_ip = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000539 timeout = decline_wait;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000540 packet_num = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000541 continue; /* back to main loop */
Denis Vlasenko223bc972007-11-22 00:58:49 +0000542 }
543 }
544#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000545 /* enter bound state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000546 t1 = lease_seconds / 2;
Mike Frysinger7031f622006-05-08 03:20:50 +0000547
548 /* little fixed point for n * .875 */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000549 t2 = (lease_seconds * 7) >> 3;
Mike Frysinger7031f622006-05-08 03:20:50 +0000550 temp_addr.s_addr = packet.yiaddr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000551 bb_info_msg("Lease of %s obtained, lease time %u",
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000552 inet_ntoa(temp_addr), (unsigned)lease_seconds);
553 timestamp_got_lease = monotonic_sec();
554 timeout = t1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000555 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000556 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000557 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
558
559 state = BOUND;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000560 change_listen_mode(LISTEN_NONE);
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000561 if (client_config.quit_after_lease) {
562 if (client_config.release_on_quit)
563 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000564 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000565 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000566 if (!client_config.foreground)
567 client_background();
568
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000569 continue; /* back to main loop */
570 }
571 if (*message == DHCPNAK) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000572 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000573 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000574 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000575 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000576 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000577 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +0000578 sleep(3); /* avoid excessive network traffic */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000579 state = INIT_SELECTING;
580 requested_ip = 0;
581 timeout = 0;
582 packet_num = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000583 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000584 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000585 /* case BOUND, RELEASED: - ignore all packets */
586 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000587 continue; /* back to main loop */
588 }
589
590 /* select() didn't timeout, something did happen.
591 * But it wasn't a packet. It's a signal pipe then. */
592 {
Denis Vlasenko80edead2007-08-02 22:31:05 +0000593 int signo = udhcp_sp_read(&rfds);
594 switch (signo) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000595 case SIGUSR1:
596 perform_renew();
597 break;
598 case SIGUSR2:
599 perform_release();
600 break;
601 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000602 bb_info_msg("Received SIGTERM");
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000603 if (client_config.release_on_quit)
604 perform_release();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000605 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000606 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000607 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000608 } /* for (;;) - main loop ends */
609
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000610 ret0:
611 retval = 0;
612 ret:
Denis Vlasenkobb23c062007-08-15 20:05:37 +0000613 /*if (client_config.pidfile) - remove_pidfile has it's own check */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000614 remove_pidfile(client_config.pidfile);
615 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000616}