blob: 92ebe36769b809f4b382ea9280074a3cf21e3ced [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 Vlasenkofbd29182007-04-07 01:05:47 +000021static int sockfd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000022
23#define LISTEN_NONE 0
24#define LISTEN_KERNEL 1
25#define LISTEN_RAW 2
Denis Vlasenkofbd29182007-04-07 01:05:47 +000026static smallint listen_mode;
27
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000028#define INIT_SELECTING 0
29#define REQUESTING 1
30#define BOUND 2
31#define RENEWING 3
32#define REBINDING 4
33#define INIT_REBOOT 5
34#define RENEW_REQUESTED 6
35#define RELEASED 7
Denis Vlasenkofbd29182007-04-07 01:05:47 +000036static smallint state;
Mike Frysinger7031f622006-05-08 03:20:50 +000037
Denis Vlasenkodeabacd2007-09-30 17:55:43 +000038/* struct client_config_t client_config is in bb_common_bufsiz1 */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000039
Mike Frysinger7031f622006-05-08 03:20:50 +000040
Mike Frysinger7031f622006-05-08 03:20:50 +000041/* just a little helper */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000042static void change_listen_mode(int new_mode)
Mike Frysinger7031f622006-05-08 03:20:50 +000043{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000044 DEBUG("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000045 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Denis Vlasenkofbd29182007-04-07 01:05:47 +000046 if (sockfd >= 0) {
47 close(sockfd);
48 sockfd = -1;
49 }
Mike Frysinger7031f622006-05-08 03:20:50 +000050 listen_mode = new_mode;
51}
52
53
54/* perform a renew */
55static void perform_renew(void)
56{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000057 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000058 switch (state) {
59 case BOUND:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000060 change_listen_mode(LISTEN_KERNEL);
Mike Frysinger7031f622006-05-08 03:20:50 +000061 case RENEWING:
62 case REBINDING:
63 state = RENEW_REQUESTED;
64 break;
65 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000066 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000067 case REQUESTING:
68 case RELEASED:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000069 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +000070 state = INIT_SELECTING;
71 break;
72 case INIT_SELECTING:
73 break;
74 }
Mike Frysinger7031f622006-05-08 03:20:50 +000075}
76
77
78/* perform a release */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000079static void perform_release(uint32_t requested_ip, uint32_t server_addr)
Mike Frysinger7031f622006-05-08 03:20:50 +000080{
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000081 char buffer[sizeof("255.255.255.255")];
Mike Frysinger7031f622006-05-08 03:20:50 +000082 struct in_addr temp_addr;
83
84 /* send release packet */
85 if (state == BOUND || state == RENEWING || state == REBINDING) {
86 temp_addr.s_addr = server_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000087 strcpy(buffer, inet_ntoa(temp_addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000088 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000089 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000090 inet_ntoa(temp_addr), buffer);
91 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +000092 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000093 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000094 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +000095
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000096 change_listen_mode(LISTEN_NONE);
Mike Frysinger7031f622006-05-08 03:20:50 +000097 state = RELEASED;
Mike Frysinger7031f622006-05-08 03:20:50 +000098}
99
100
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000101#if BB_MMU
Mike Frysinger7031f622006-05-08 03:20:50 +0000102static void client_background(void)
103{
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000104 bb_daemonize(0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000105 logmode &= ~LOGMODE_STDIO;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000106 /* rewrite pidfile, as our pid is different now */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000107 write_pidfile(client_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +0000108}
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000109#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000110
111
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000112static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
113{
114 uint8_t *storage;
115 int len = strlen(str);
116 if (len > 255) len = 255;
117 storage = xzalloc(len + extra + OPT_DATA);
118 storage[OPT_CODE] = code;
119 storage[OPT_LEN] = len + extra;
120 memcpy(storage + extra + OPT_DATA, str, len);
121 return storage;
122}
123
124
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000125int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000126int udhcpc_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000127{
128 uint8_t *temp, *message;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000129 char *str_c, *str_V, *str_h, *str_F, *str_r;
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000130 USE_FEATURE_UDHCP_PORT(char *str_P;)
Denis Vlasenko19183682007-12-10 07:03:38 +0000131 llist_t *list_O = NULL;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000132 int tryagain_timeout = 20;
133 int discover_timeout = 3;
134 int discover_retries = 3;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000135 uint32_t server_addr = server_addr; /* for compiler */
136 uint32_t requested_ip = 0;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000137 uint32_t xid = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000138 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000139 int packet_num;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000140 int timeout; /* must be signed */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000141 unsigned already_waited_sec;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000142 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000143 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000144 int retval;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000145 struct timeval tv;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000146 struct dhcpMessage packet;
147 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000148
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000149#if ENABLE_GETOPT_LONG
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000150 static const char udhcpc_longopts[] ALIGN1 =
Denis Vlasenko19183682007-12-10 07:03:38 +0000151 "clientid\0" Required_argument "c"
152 "clientid-none\0" No_argument "C"
153 "vendorclass\0" Required_argument "V"
Denis Vlasenko19183682007-12-10 07:03:38 +0000154 "hostname\0" Required_argument "H"
155 "fqdn\0" Required_argument "F"
156 "interface\0" Required_argument "i"
157 "now\0" No_argument "n"
158 "pidfile\0" Required_argument "p"
159 "quit\0" No_argument "q"
160 "release\0" No_argument "R"
161 "request\0" Required_argument "r"
162 "script\0" Required_argument "s"
163 "timeout\0" Required_argument "T"
164 "version\0" No_argument "v"
165 "retries\0" Required_argument "t"
166 "tryagain\0" Required_argument "A"
167 "syslog\0" No_argument "S"
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000168 "request-option\0" Required_argument "O"
169 "no-default-options\0" No_argument "o"
170 "foreground\0" No_argument "f"
171 "background\0" No_argument "b"
Denis Vlasenko873b8952008-06-15 05:40:08 +0000172 USE_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a")
173 USE_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000174 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000175#endif
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000176 enum {
177 OPT_c = 1 << 0,
178 OPT_C = 1 << 1,
179 OPT_V = 1 << 2,
180 OPT_H = 1 << 3,
181 OPT_h = 1 << 4,
182 OPT_F = 1 << 5,
Denis Vlasenkof716a6d2008-06-15 09:49:21 +0000183 OPT_i = 1 << 6,
184 OPT_n = 1 << 7,
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000185 OPT_p = 1 << 8,
186 OPT_q = 1 << 9,
187 OPT_R = 1 << 10,
188 OPT_r = 1 << 11,
189 OPT_s = 1 << 12,
190 OPT_T = 1 << 13,
191 OPT_t = 1 << 14,
192 OPT_v = 1 << 15,
193 OPT_S = 1 << 16,
194 OPT_A = 1 << 17,
195 OPT_O = 1 << 18,
196 OPT_o = 1 << 19,
197 OPT_f = 1 << 20,
198/* The rest has variable bit positions, need to be clever */
199 OPTBIT_f = 20,
Denis Vlasenko873b8952008-06-15 05:40:08 +0000200 USE_FOR_MMU( OPTBIT_b,)
201 USE_FEATURE_UDHCPC_ARPING(OPTBIT_a,)
202 USE_FEATURE_UDHCP_PORT( OPTBIT_P,)
203 USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,)
204 USE_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,)
205 USE_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,)
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000206 };
207
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000208 /* Default options. */
Denis Vlasenko873b8952008-06-15 05:40:08 +0000209 USE_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
210 USE_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000211 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 Vlasenko1d426652008-03-17 09:09:09 +0000215 /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */
216 opt_complementary = "c--C:C--c:O::T+:t+:A+";
Denis Vlasenko873b8952008-06-15 05:40:08 +0000217 USE_GETOPT_LONG(applet_long_options = udhcpc_longopts;)
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000218 opt = getopt32(argv, "c:CV:H:h:F:i:np:qRr:s:T:t:vSA:O:of"
219 USE_FOR_MMU("b")
220 USE_FEATURE_UDHCPC_ARPING("a")
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000221 USE_FEATURE_UDHCP_PORT("P:")
Denis Vlasenko19183682007-12-10 07:03:38 +0000222 , &str_c, &str_V, &str_h, &str_h, &str_F
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000223 , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
224 , &client_config.script /* s */
225 , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
Denis Vlasenko19183682007-12-10 07:03:38 +0000226 , &list_O
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000227 USE_FEATURE_UDHCP_PORT(, &str_P)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000228 );
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000229 if (opt & OPT_c)
230 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000231 if (opt & OPT_V)
232 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000233 if (opt & (OPT_h|OPT_H))
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000234 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
235 if (opt & OPT_F) {
236 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
237 /* Flags: 0000NEOS
238 S: 1 => Client requests Server to update A RR in DNS as well as PTR
239 O: 1 => Server indicates to client that DNS has been updated regardless
Denis Vlasenko873b8952008-06-15 05:40:08 +0000240 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<3>com<0> not "host.domain.com"
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000241 N: 1 => Client requests Server to not update DNS
242 */
243 client_config.fqdn[OPT_DATA + 0] = 0x1;
244 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
245 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
246 }
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000247 if (opt & OPT_r)
248 requested_ip = inet_addr(str_r);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000249 if (opt & OPT_v) {
Denis Vlasenkodef88982007-10-07 17:06:01 +0000250 puts("version "BB_VER);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000251 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000252 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000253#if ENABLE_FEATURE_UDHCP_PORT
254 if (opt & OPT_P) {
255 CLIENT_PORT = xatou16(str_P);
256 SERVER_PORT = CLIENT_PORT - 1;
257 }
258#endif
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000259 if (opt & OPT_o)
260 client_config.no_default_options = 1;
Denis Vlasenko19183682007-12-10 07:03:38 +0000261 while (list_O) {
Denis Vlasenko18c93022008-08-27 22:29:43 +0000262 char *optstr = llist_pop(&list_O);
263 int n = index_in_strings(dhcp_option_strings, optstr);
Denis Vlasenko19183682007-12-10 07:03:38 +0000264 if (n < 0)
Denis Vlasenko18c93022008-08-27 22:29:43 +0000265 bb_error_msg_and_die("unknown option '%s'", optstr);
Denis Vlasenko19183682007-12-10 07:03:38 +0000266 n = dhcp_options[n].code;
267 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
Denis Vlasenko19183682007-12-10 07:03:38 +0000268 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000269
270 if (read_interface(client_config.interface, &client_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +0000271 NULL, client_config.arp))
Mike Frysinger7031f622006-05-08 03:20:50 +0000272 return 1;
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000273#if !BB_MMU
274 /* on NOMMU reexec (i.e., background) early */
275 if (!(opt & OPT_f)) {
276 bb_daemonize_or_rexec(0 /* flags */, argv);
277 logmode = 0;
278 }
279#endif
280 if (opt & OPT_S) {
281 openlog(applet_name, LOG_PID, LOG_LOCAL0);
282 logmode |= LOGMODE_SYSLOG;
283 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000284
Denis Vlasenko80edead2007-08-02 22:31:05 +0000285 /* Make sure fd 0,1,2 are open */
286 bb_sanitize_stdio();
287 /* Equivalent of doing a fflush after every \n */
288 setlinebuf(stdout);
289
290 /* Create pidfile */
291 write_pidfile(client_config.pidfile);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000292
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000293 /* Goes to stdout (unless NOMMU) and possibly syslog */
Denis Vlasenkodef88982007-10-07 17:06:01 +0000294 bb_info_msg("%s (v"BB_VER") started", applet_name);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000295
Mike Frysinger7031f622006-05-08 03:20:50 +0000296 /* if not set, and not suppressed, setup the default client ID */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000297 if (!client_config.clientid && !(opt & OPT_C)) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000298 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000299 client_config.clientid[OPT_DATA] = 1;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000300 memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000301 }
302
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000303 if (!client_config.vendorclass)
304 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000305
306 /* setup the signal pipe */
307 udhcp_sp_setup();
308
309 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000310 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000311 change_listen_mode(LISTEN_RAW);
Denis Vlasenkob539c842007-11-29 08:17:45 +0000312 packet_num = 0;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000313 timeout = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000314 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000315
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000316 /* Main event loop. select() waits on signal pipe and possibly
317 * on sockfd.
318 * "continue" statements in code below jump to the top of the loop.
319 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000320 for (;;) {
Denis Vlasenkob2342912008-05-21 07:02:16 +0000321 unsigned timestamp_before_wait;
Mike Frysinger7031f622006-05-08 03:20:50 +0000322
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000323 if (listen_mode != LISTEN_NONE && sockfd < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000324 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000325 sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
Mike Frysinger7031f622006-05-08 03:20:50 +0000326 else
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000327 sockfd = raw_socket(client_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +0000328 }
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000329 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000330
Denis Vlasenko19903f02008-05-21 07:03:03 +0000331 tv.tv_sec = timeout - already_waited_sec;
Denis Vlasenkob2342912008-05-21 07:02:16 +0000332 tv.tv_usec = 0;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000333 retval = 0; /* If we already timed out, fall through, else... */
Mike Frysinger7031f622006-05-08 03:20:50 +0000334 if (tv.tv_sec > 0) {
Denis Vlasenkob2342912008-05-21 07:02:16 +0000335 timestamp_before_wait = (unsigned)monotonic_sec();
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000336 DEBUG("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000337 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenkob2342912008-05-21 07:02:16 +0000338 if (retval < 0) {
339 /* EINTR? A signal was caught, don't panic */
340 if (errno == EINTR)
341 continue;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000342 /* Else: an error occured, panic! */
343 bb_perror_msg_and_die("select");
344 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000345 }
346
347 /* If timeout dropped to zero, time to become active:
348 * resend discover/renew/whatever
349 */
350 if (retval == 0) {
Denis Vlasenko19903f02008-05-21 07:03:03 +0000351 /* We will restart wait afresh in any case */
352 already_waited_sec = 0;
353
Mike Frysinger7031f622006-05-08 03:20:50 +0000354 switch (state) {
355 case INIT_SELECTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000356 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000357 if (packet_num == 0)
358 xid = random_xid();
359
Mike Frysinger7031f622006-05-08 03:20:50 +0000360 send_discover(xid, requested_ip); /* broadcast */
361
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000362 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000363 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000364 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000365 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000366 udhcp_run_script(NULL, "leasefail");
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000367#if BB_MMU /* -b is not supported on NOMMU */
368 if (opt & OPT_b) { /* background if no lease */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000369 bb_info_msg("No lease, forking to background");
370 client_background();
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000371 /* do not background again! */
372 opt = ((opt & ~OPT_b) | OPT_f);
373 } else
374#endif
375 if (opt & OPT_n) { /* abort if no lease */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000376 bb_info_msg("No lease, failing");
377 retval = 1;
378 goto ret;
379 }
Denis Vlasenko6de89942008-05-21 07:05:06 +0000380 /* wait before trying again */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000381 timeout = tryagain_timeout;
382 packet_num = 0;
383 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000384 case RENEW_REQUESTED:
385 case REQUESTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000386 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000387 /* send request packet */
Denis Vlasenko6de89942008-05-21 07:05:06 +0000388 if (state == RENEW_REQUESTED) /* unicast */
389 send_renew(xid, server_addr, requested_ip);
390 else /* broadcast */
391 send_selecting(xid, server_addr, requested_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000392
Denis Vlasenko6de89942008-05-21 07:05:06 +0000393 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000394 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000395 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000396 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000397 /* timed out, go back to init state */
398 if (state == RENEW_REQUESTED)
399 udhcp_run_script(NULL, "deconfig");
400 change_listen_mode(LISTEN_RAW);
401 state = INIT_SELECTING;
402 timeout = 0;
403 packet_num = 0;
404 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000405 case BOUND:
Denis Vlasenko19903f02008-05-21 07:03:03 +0000406 /* Half of the lease passed, time to enter renewing state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000407 change_listen_mode(LISTEN_KERNEL);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000408 DEBUG("Entering renew state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000409 state = RENEWING;
Mike Frysinger7031f622006-05-08 03:20:50 +0000410 /* fall right through */
411 case RENEWING:
Denis Vlasenko19903f02008-05-21 07:03:03 +0000412 if (timeout > 60) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000413 /* send a request packet */
414 send_renew(xid, server_addr, requested_ip); /* unicast */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000415 timeout >>= 1;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000416 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000417 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000418 /* Timed out, enter rebinding state */
419 DEBUG("Entering rebinding state");
420 state = REBINDING;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000421 /* fall right through */
Mike Frysinger7031f622006-05-08 03:20:50 +0000422 case REBINDING:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000423 /* Lease is *really* about to run out,
424 * try to find DHCP server using broadcast */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000425 if (timeout > 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000426 /* send a request packet */
427 send_renew(xid, 0, requested_ip); /* broadcast */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000428 timeout >>= 1;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000429 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000430 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000431 /* Timed out, enter init state */
432 bb_info_msg("Lease lost, entering init state");
433 udhcp_run_script(NULL, "deconfig");
434 change_listen_mode(LISTEN_RAW);
435 state = INIT_SELECTING;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000436 /*timeout = 0; - already is */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000437 packet_num = 0;
438 continue;
439 /* case RELEASED: */
Mike Frysinger7031f622006-05-08 03:20:50 +0000440 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000441 /* yah, I know, *you* say it would never happen */
442 timeout = INT_MAX;
443 continue; /* back to main loop */
444 }
445
446 /* select() didn't timeout, something did happen. */
447 /* Is is a packet? */
448 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000449 int len;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000450 /* A packet is ready, read it */
Mike Frysinger7031f622006-05-08 03:20:50 +0000451
452 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenko6de89942008-05-21 07:05:06 +0000453 len = udhcp_recv_kernel_packet(&packet, sockfd);
Denis Vlasenkob76b9a42008-01-25 22:46:34 +0000454 else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000455 len = udhcp_recv_raw_packet(&packet, sockfd);
456 if (len == -1) { /* error is severe, reopen socket */
457 DEBUG("error on read, %s, reopening socket", strerror(errno));
458 sleep(discover_timeout); /* 3 seconds by default */
459 change_listen_mode(listen_mode); /* just close and reopen */
460 }
Denis Vlasenkob2342912008-05-21 07:02:16 +0000461 /* If this packet will turn out to be unrelated/bogus,
462 * we will go back and wait for next one.
463 * Be sure timeout is properly decreased. */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000464 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000465 if (len < 0)
466 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000467
468 if (packet.xid != xid) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000469 DEBUG("Ignoring XID %x (our xid is %x)",
470 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000471 continue;
472 }
473
474 /* Ignore packets that aren't for us */
475 if (memcmp(packet.chaddr, client_config.arp, 6)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000476 DEBUG("Packet does not have our chaddr - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000477 continue;
478 }
479
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000480 message = get_option(&packet, DHCP_MESSAGE_TYPE);
481 if (message == NULL) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000482 bb_error_msg("cannot get message type from packet - ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000483 continue;
484 }
485
486 switch (state) {
487 case INIT_SELECTING:
488 /* Must be a DHCPOFFER to one of our xid's */
489 if (*message == DHCPOFFER) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000490 /* TODO: why we don't just fetch server's IP from IP header? */
Denis Vlasenko239369b2006-09-07 17:05:44 +0000491 temp = get_option(&packet, DHCP_SERVER_ID);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000492 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000493 bb_error_msg("no server ID in message");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000494 continue;
495 /* still selecting - this server looks bad */
Mike Frysinger7031f622006-05-08 03:20:50 +0000496 }
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000497 /* it IS unaligned sometimes, don't "optimize" */
498 server_addr = get_unaligned_u32p((uint32_t*)temp);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000499 xid = packet.xid;
500 requested_ip = packet.yiaddr;
501
502 /* enter requesting state */
503 state = REQUESTING;
504 timeout = 0;
505 packet_num = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000506 already_waited_sec = 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);
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000522 lease_seconds &= 0x0fffffff; /* paranoia: must not be prone to overflows */
523 if (lease_seconds < 10) /* and not too small */
524 lease_seconds = 10;
Mike Frysinger7031f622006-05-08 03:20:50 +0000525 }
Denis Vlasenko223bc972007-11-22 00:58:49 +0000526#if ENABLE_FEATURE_UDHCPC_ARPING
527 if (opt & OPT_a) {
528 if (!arpping(packet.yiaddr,
529 (uint32_t) 0,
530 client_config.arp,
531 client_config.interface)
532 ) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000533 bb_info_msg("offered address is in use "
534 "(got ARP reply), declining");
Denis Vlasenkoca9635b2008-01-25 19:27:08 +0000535 send_decline(xid, server_addr, packet.yiaddr);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000536
537 if (state != REQUESTING)
538 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000539 change_listen_mode(LISTEN_RAW);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000540 state = INIT_SELECTING;
541 requested_ip = 0;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000542 timeout = tryagain_timeout;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000543 packet_num = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000544 already_waited_sec = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000545 continue; /* back to main loop */
Denis Vlasenko223bc972007-11-22 00:58:49 +0000546 }
547 }
548#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000549 /* enter bound state */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000550 timeout = lease_seconds / 2;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000551 {
552 struct in_addr temp_addr;
553 temp_addr.s_addr = packet.yiaddr;
554 bb_info_msg("Lease of %s obtained, lease time %u",
555 inet_ntoa(temp_addr), (unsigned)lease_seconds);
556 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000557 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000558 udhcp_run_script(&packet,
Mike Frysinger7031f622006-05-08 03:20:50 +0000559 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
560
561 state = BOUND;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000562 change_listen_mode(LISTEN_NONE);
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000563 if (opt & OPT_q) { /* quit after lease */
564 if (opt & OPT_R) /* release on quit */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000565 perform_release(requested_ip, server_addr);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000566 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000567 }
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000568#if BB_MMU /* NOMMU case backgrounded earlier */
569 if (!(opt & OPT_f)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000570 client_background();
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000571 /* do not background again! */
572 opt = ((opt & ~OPT_b) | OPT_f);
573 }
574#endif
Denis Vlasenko19903f02008-05-21 07:03:03 +0000575 already_waited_sec = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000576 continue; /* back to main loop */
577 }
578 if (*message == DHCPNAK) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000579 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000580 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000581 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000582 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000583 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000584 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +0000585 sleep(3); /* avoid excessive network traffic */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000586 state = INIT_SELECTING;
587 requested_ip = 0;
588 timeout = 0;
589 packet_num = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000590 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000591 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000592 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000593 /* case BOUND, RELEASED: - ignore all packets */
594 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000595 continue; /* back to main loop */
596 }
597
598 /* select() didn't timeout, something did happen.
599 * But it wasn't a packet. It's a signal pipe then. */
600 {
Denis Vlasenko80edead2007-08-02 22:31:05 +0000601 int signo = udhcp_sp_read(&rfds);
602 switch (signo) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000603 case SIGUSR1:
604 perform_renew();
Denis Vlasenkob539c842007-11-29 08:17:45 +0000605 /* start things over */
606 packet_num = 0;
607 /* Kill any timeouts because the user wants this to hurry along */
608 timeout = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000609 break;
610 case SIGUSR2:
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000611 perform_release(requested_ip, server_addr);
612 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +0000613 break;
614 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000615 bb_info_msg("Received SIGTERM");
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000616 if (opt & OPT_R) /* release on quit */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000617 perform_release(requested_ip, server_addr);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000618 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000619 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000620 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000621 } /* for (;;) - main loop ends */
622
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000623 ret0:
624 retval = 0;
625 ret:
Denis Vlasenkob2342912008-05-21 07:02:16 +0000626 /*if (client_config.pidfile) - remove_pidfile has its own check */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000627 remove_pidfile(client_config.pidfile);
628 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000629}