blob: 636061818199d382fb8d6ab34060407e7a31eb49 [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 */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000010#include <syslog.h>
Denis Vlasenko1caca342007-08-02 10:14:29 +000011/* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
12#define WANT_PIDFILE 1
Mike Frysinger7031f622006-05-08 03:20:50 +000013#include "common.h"
14#include "dhcpd.h"
15#include "dhcpc.h"
16#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000017
Mike Frysinger7031f622006-05-08 03:20:50 +000018
Denis Vlasenkofbd29182007-04-07 01:05:47 +000019static int sockfd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000020
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020021#define LISTEN_NONE 0
Mike Frysinger7031f622006-05-08 03:20:50 +000022#define LISTEN_KERNEL 1
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020023#define LISTEN_RAW 2
Denis Vlasenkofbd29182007-04-07 01:05:47 +000024static smallint listen_mode;
25
Denys Vlasenko13ca4b12009-07-19 04:07:21 +020026/* initial state: (re)start DHCP negotiation */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000027#define INIT_SELECTING 0
Denys Vlasenko13ca4b12009-07-19 04:07:21 +020028/* discover was sent, DHCPOFFER reply received */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000029#define REQUESTING 1
Denys Vlasenko13ca4b12009-07-19 04:07:21 +020030/* select/renew was sent, DHCPACK reply received */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000031#define BOUND 2
Denys Vlasenko7d6a7912009-07-19 04:24:23 +020032/* half of lease passed, want to renew it by sending unicast renew requests */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000033#define RENEWING 3
Denys Vlasenko13ca4b12009-07-19 04:07:21 +020034/* renew requests were not answered, lease is almost over, send broadcast renew */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000035#define REBINDING 4
Denys Vlasenko13ca4b12009-07-19 04:07:21 +020036/* manually requested renew (SIGUSR1) */
37#define RENEW_REQUESTED 5
38/* release, possibly manually requested (SIGUSR2) */
39#define RELEASED 6
Denis Vlasenkofbd29182007-04-07 01:05:47 +000040static smallint state;
Mike Frysinger7031f622006-05-08 03:20:50 +000041
Denis Vlasenkodeabacd2007-09-30 17:55:43 +000042/* struct client_config_t client_config is in bb_common_bufsiz1 */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000043
Mike Frysinger7031f622006-05-08 03:20:50 +000044
Mike Frysinger7031f622006-05-08 03:20:50 +000045/* just a little helper */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000046static void change_listen_mode(int new_mode)
Mike Frysinger7031f622006-05-08 03:20:50 +000047{
Denys Vlasenko4945ed32009-06-17 11:58:11 +020048 log1("Entering listen mode: %s",
Denys Vlasenko13ca4b12009-07-19 04:07:21 +020049 new_mode != LISTEN_NONE
50 ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw")
51 : "none"
52 );
Denis Vlasenko517413f2008-12-10 11:16:47 +000053
54 listen_mode = new_mode;
Denis Vlasenkofbd29182007-04-07 01:05:47 +000055 if (sockfd >= 0) {
56 close(sockfd);
57 sockfd = -1;
58 }
Denis Vlasenko517413f2008-12-10 11:16:47 +000059 if (new_mode == LISTEN_KERNEL)
60 sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
61 else if (new_mode != LISTEN_NONE)
62 sockfd = udhcp_raw_socket(client_config.ifindex);
63 /* else LISTEN_NONE: sockfd stay closed */
Mike Frysinger7031f622006-05-08 03:20:50 +000064}
65
66
67/* perform a renew */
68static void perform_renew(void)
69{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000070 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000071 switch (state) {
72 case BOUND:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000073 change_listen_mode(LISTEN_KERNEL);
Mike Frysinger7031f622006-05-08 03:20:50 +000074 case RENEWING:
75 case REBINDING:
76 state = RENEW_REQUESTED;
77 break;
78 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000079 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000080 case REQUESTING:
81 case RELEASED:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000082 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +000083 state = INIT_SELECTING;
84 break;
85 case INIT_SELECTING:
86 break;
87 }
Mike Frysinger7031f622006-05-08 03:20:50 +000088}
89
90
91/* perform a release */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000092static void perform_release(uint32_t requested_ip, uint32_t server_addr)
Mike Frysinger7031f622006-05-08 03:20:50 +000093{
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000094 char buffer[sizeof("255.255.255.255")];
Mike Frysinger7031f622006-05-08 03:20:50 +000095 struct in_addr temp_addr;
96
97 /* send release packet */
98 if (state == BOUND || state == RENEWING || state == REBINDING) {
99 temp_addr.s_addr = server_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000100 strcpy(buffer, inet_ntoa(temp_addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000101 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000102 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 inet_ntoa(temp_addr), buffer);
104 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +0000105 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +0000106 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000107 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000108
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000109 change_listen_mode(LISTEN_NONE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000110 state = RELEASED;
Mike Frysinger7031f622006-05-08 03:20:50 +0000111}
112
113
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000114#if BB_MMU
Mike Frysinger7031f622006-05-08 03:20:50 +0000115static void client_background(void)
116{
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000117 bb_daemonize(0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000118 logmode &= ~LOGMODE_STDIO;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000119 /* rewrite pidfile, as our pid is different now */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000120 write_pidfile(client_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +0000121}
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000122#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000123
124
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000125static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
126{
127 uint8_t *storage;
128 int len = strlen(str);
129 if (len > 255) len = 255;
130 storage = xzalloc(len + extra + OPT_DATA);
131 storage[OPT_CODE] = code;
132 storage[OPT_LEN] = len + extra;
133 memcpy(storage + extra + OPT_DATA, str, len);
134 return storage;
135}
136
137
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000138int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000139int udhcpc_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000140{
141 uint8_t *temp, *message;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000142 char *str_c, *str_V, *str_h, *str_F, *str_r;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000143 IF_FEATURE_UDHCP_PORT(char *str_P;)
Denis Vlasenko19183682007-12-10 07:03:38 +0000144 llist_t *list_O = NULL;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000145 int tryagain_timeout = 20;
146 int discover_timeout = 3;
147 int discover_retries = 3;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000148 uint32_t server_addr = server_addr; /* for compiler */
149 uint32_t requested_ip = 0;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000150 uint32_t xid = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000151 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000152 int packet_num;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000153 int timeout; /* must be signed */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000154 unsigned already_waited_sec;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000155 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000156 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000157 int retval;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000158 struct timeval tv;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200159 struct dhcp_packet packet;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000160 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000161
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200162#if ENABLE_LONG_OPTS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000163 static const char udhcpc_longopts[] ALIGN1 =
Denis Vlasenko19183682007-12-10 07:03:38 +0000164 "clientid\0" Required_argument "c"
165 "clientid-none\0" No_argument "C"
166 "vendorclass\0" Required_argument "V"
Denis Vlasenko19183682007-12-10 07:03:38 +0000167 "hostname\0" Required_argument "H"
168 "fqdn\0" Required_argument "F"
169 "interface\0" Required_argument "i"
170 "now\0" No_argument "n"
171 "pidfile\0" Required_argument "p"
172 "quit\0" No_argument "q"
173 "release\0" No_argument "R"
174 "request\0" Required_argument "r"
175 "script\0" Required_argument "s"
176 "timeout\0" Required_argument "T"
177 "version\0" No_argument "v"
178 "retries\0" Required_argument "t"
179 "tryagain\0" Required_argument "A"
180 "syslog\0" No_argument "S"
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000181 "request-option\0" Required_argument "O"
182 "no-default-options\0" No_argument "o"
183 "foreground\0" No_argument "f"
184 "background\0" No_argument "b"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000185 IF_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a")
186 IF_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000187 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000188#endif
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000189 enum {
190 OPT_c = 1 << 0,
191 OPT_C = 1 << 1,
192 OPT_V = 1 << 2,
193 OPT_H = 1 << 3,
194 OPT_h = 1 << 4,
195 OPT_F = 1 << 5,
Denis Vlasenkof716a6d2008-06-15 09:49:21 +0000196 OPT_i = 1 << 6,
197 OPT_n = 1 << 7,
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000198 OPT_p = 1 << 8,
199 OPT_q = 1 << 9,
200 OPT_R = 1 << 10,
201 OPT_r = 1 << 11,
202 OPT_s = 1 << 12,
203 OPT_T = 1 << 13,
204 OPT_t = 1 << 14,
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200205 OPT_S = 1 << 15,
206 OPT_A = 1 << 16,
207 OPT_O = 1 << 17,
208 OPT_o = 1 << 18,
209 OPT_f = 1 << 19,
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000210/* The rest has variable bit positions, need to be clever */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200211 OPTBIT_f = 19,
212 USE_FOR_MMU( OPTBIT_b,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000213 IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,)
214 IF_FEATURE_UDHCP_PORT( OPTBIT_P,)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200215 USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000216 IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,)
217 IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,)
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000218 };
219
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000220 /* Default options. */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000221 IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
222 IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000223 client_config.interface = "eth0";
224 client_config.script = DEFAULT_SCRIPT;
Mike Frysinger7031f622006-05-08 03:20:50 +0000225
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000226 /* Parse command line */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000227 /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200228 opt_complementary = "c--C:C--c:O::T+:t+:A+"
229#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
230 ":vv"
231#endif
232 ;
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200233 IF_LONG_OPTS(applet_long_options = udhcpc_longopts;)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200234 opt = getopt32(argv, "c:CV:H:h:F:i:np:qRr:s:T:t:SA:O:of"
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000235 USE_FOR_MMU("b")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000236 IF_FEATURE_UDHCPC_ARPING("a")
237 IF_FEATURE_UDHCP_PORT("P:")
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200238 "v"
Denis Vlasenko19183682007-12-10 07:03:38 +0000239 , &str_c, &str_V, &str_h, &str_h, &str_F
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000240 , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
241 , &client_config.script /* s */
242 , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
Denis Vlasenko19183682007-12-10 07:03:38 +0000243 , &list_O
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000244 IF_FEATURE_UDHCP_PORT(, &str_P)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200245#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
246 , &dhcp_verbose
247#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000248 );
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000249 if (opt & OPT_c)
250 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000251 if (opt & OPT_V)
252 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000253 if (opt & (OPT_h|OPT_H))
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000254 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
255 if (opt & OPT_F) {
256 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
257 /* Flags: 0000NEOS
258 S: 1 => Client requests Server to update A RR in DNS as well as PTR
259 O: 1 => Server indicates to client that DNS has been updated regardless
Denis Vlasenko873b8952008-06-15 05:40:08 +0000260 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 +0000261 N: 1 => Client requests Server to not update DNS
262 */
263 client_config.fqdn[OPT_DATA + 0] = 0x1;
264 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
265 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
266 }
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000267 if (opt & OPT_r)
268 requested_ip = inet_addr(str_r);
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000269#if ENABLE_FEATURE_UDHCP_PORT
270 if (opt & OPT_P) {
271 CLIENT_PORT = xatou16(str_P);
272 SERVER_PORT = CLIENT_PORT - 1;
273 }
274#endif
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000275 if (opt & OPT_o)
276 client_config.no_default_options = 1;
Denis Vlasenko19183682007-12-10 07:03:38 +0000277 while (list_O) {
Denis Vlasenko18c93022008-08-27 22:29:43 +0000278 char *optstr = llist_pop(&list_O);
279 int n = index_in_strings(dhcp_option_strings, optstr);
Denis Vlasenko19183682007-12-10 07:03:38 +0000280 if (n < 0)
Denis Vlasenko18c93022008-08-27 22:29:43 +0000281 bb_error_msg_and_die("unknown option '%s'", optstr);
Denis Vlasenko19183682007-12-10 07:03:38 +0000282 n = dhcp_options[n].code;
283 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
Denis Vlasenko19183682007-12-10 07:03:38 +0000284 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000285
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200286 if (udhcp_read_interface(client_config.interface,
287 &client_config.ifindex,
288 NULL,
289 client_config.client_mac)
290 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000291 return 1;
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200292 }
293
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000294#if !BB_MMU
295 /* on NOMMU reexec (i.e., background) early */
296 if (!(opt & OPT_f)) {
297 bb_daemonize_or_rexec(0 /* flags */, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000298 logmode = LOGMODE_NONE;
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000299 }
300#endif
301 if (opt & OPT_S) {
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000302 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000303 logmode |= LOGMODE_SYSLOG;
304 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000305
Denis Vlasenko80edead2007-08-02 22:31:05 +0000306 /* Make sure fd 0,1,2 are open */
307 bb_sanitize_stdio();
308 /* Equivalent of doing a fflush after every \n */
309 setlinebuf(stdout);
310
311 /* Create pidfile */
312 write_pidfile(client_config.pidfile);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000313
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000314 /* Goes to stdout (unless NOMMU) and possibly syslog */
Denis Vlasenkodef88982007-10-07 17:06:01 +0000315 bb_info_msg("%s (v"BB_VER") started", applet_name);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000316
Denys Vlasenko4248c332009-06-26 23:23:16 +0200317 /* If not set, and not suppressed, set up the default client ID */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000318 if (!client_config.clientid && !(opt & OPT_C)) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000319 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000320 client_config.clientid[OPT_DATA] = 1;
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200321 memcpy(client_config.clientid + OPT_DATA+1, client_config.client_mac, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000322 }
323
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000324 if (!client_config.vendorclass)
325 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000326
Denys Vlasenko4248c332009-06-26 23:23:16 +0200327 /* Set up the signal pipe */
Mike Frysinger7031f622006-05-08 03:20:50 +0000328 udhcp_sp_setup();
329
330 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000331 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000332 change_listen_mode(LISTEN_RAW);
Denis Vlasenkob539c842007-11-29 08:17:45 +0000333 packet_num = 0;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000334 timeout = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000335 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000336
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000337 /* Main event loop. select() waits on signal pipe and possibly
338 * on sockfd.
339 * "continue" statements in code below jump to the top of the loop.
340 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000341 for (;;) {
Denis Vlasenkoec64a572009-01-14 00:28:03 +0000342 /* silence "uninitialized!" warning */
343 unsigned timestamp_before_wait = timestamp_before_wait;
Mike Frysinger7031f622006-05-08 03:20:50 +0000344
Denis Vlasenko517413f2008-12-10 11:16:47 +0000345 //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode);
346
347 /* Was opening raw or udp socket here
348 * if (listen_mode != LISTEN_NONE && sockfd < 0),
349 * but on fast network renew responses return faster
350 * than we open sockets. Thus this code is moved
351 * to change_listen_mode(). Thus we open listen socket
352 * BEFORE we send renew request (see "case BOUND:"). */
353
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000354 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000355
Denis Vlasenko19903f02008-05-21 07:03:03 +0000356 tv.tv_sec = timeout - already_waited_sec;
Denis Vlasenkob2342912008-05-21 07:02:16 +0000357 tv.tv_usec = 0;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000358 retval = 0; /* If we already timed out, fall through, else... */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200359 if ((int)tv.tv_sec > 0) {
Denis Vlasenkob2342912008-05-21 07:02:16 +0000360 timestamp_before_wait = (unsigned)monotonic_sec();
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200361 log1("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000362 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenkob2342912008-05-21 07:02:16 +0000363 if (retval < 0) {
364 /* EINTR? A signal was caught, don't panic */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200365 if (errno == EINTR) {
366 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
Denis Vlasenkob2342912008-05-21 07:02:16 +0000367 continue;
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200368 }
Denis Vlasenko80edead2007-08-02 22:31:05 +0000369 /* Else: an error occured, panic! */
370 bb_perror_msg_and_die("select");
371 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000372 }
373
374 /* If timeout dropped to zero, time to become active:
375 * resend discover/renew/whatever
376 */
377 if (retval == 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000378 /* We will restart the wait in any case */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000379 already_waited_sec = 0;
380
Mike Frysinger7031f622006-05-08 03:20:50 +0000381 switch (state) {
382 case INIT_SELECTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000383 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000384 if (packet_num == 0)
385 xid = random_xid();
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200386 /* broadcast */
387 send_discover(xid, requested_ip);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000388 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000389 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000390 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000391 }
Denis Vlasenkocdb0b652008-09-26 09:34:15 +0000392 leasefail:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000393 udhcp_run_script(NULL, "leasefail");
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000394#if BB_MMU /* -b is not supported on NOMMU */
395 if (opt & OPT_b) { /* background if no lease */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000396 bb_info_msg("No lease, forking to background");
397 client_background();
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000398 /* do not background again! */
399 opt = ((opt & ~OPT_b) | OPT_f);
400 } else
401#endif
402 if (opt & OPT_n) { /* abort if no lease */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000403 bb_info_msg("No lease, failing");
404 retval = 1;
405 goto ret;
406 }
Denis Vlasenko6de89942008-05-21 07:05:06 +0000407 /* wait before trying again */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000408 timeout = tryagain_timeout;
409 packet_num = 0;
410 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000411 case REQUESTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000412 if (packet_num < discover_retries) {
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200413 /* send broadcast select packet */
414 send_select(xid, server_addr, requested_ip);
Denis Vlasenko6de89942008-05-21 07:05:06 +0000415 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000416 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000417 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000418 }
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200419 /* Timed out, go back to init state.
420 * "discover...select...discover..." loops
Denis Vlasenko7d9399e2008-09-26 22:21:03 +0000421 * were seen in the wild. Treat them similarly
Denis Vlasenkocdb0b652008-09-26 09:34:15 +0000422 * to "no response to discover" case */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200423 change_listen_mode(LISTEN_RAW);
Denis Vlasenko7d9399e2008-09-26 22:21:03 +0000424 state = INIT_SELECTING;
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200425 goto leasefail;
Mike Frysinger7031f622006-05-08 03:20:50 +0000426 case BOUND:
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200427 /* 1/2 lease passed, enter renewing state */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200428 state = RENEWING;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000429 change_listen_mode(LISTEN_KERNEL);
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200430 log1("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000431 /* fall right through */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200432 case RENEW_REQUESTED: /* manual (SIGUSR1) renew */
433 case_RENEW_REQUESTED:
Mike Frysinger7031f622006-05-08 03:20:50 +0000434 case RENEWING:
Denis Vlasenko19903f02008-05-21 07:03:03 +0000435 if (timeout > 60) {
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200436 /* send an unicast renew request */
437 /* Sometimes observed to fail (EADDRNOTAVAIL) to bind
438 * a new UDP socket for sending inside send_renew.
439 * I hazard to guess existing listening socket
440 * is somehow conflicting with it, but why is it
441 * not deterministic then?! Strange.
Denys Vlasenko753a3ce2009-07-19 04:27:10 +0200442 * Anyway, it does recover by eventually failing through
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200443 * into INIT_SELECTING state.
444 */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200445 send_renew(xid, server_addr, requested_ip);
Denis Vlasenko19903f02008-05-21 07:03:03 +0000446 timeout >>= 1;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000447 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000448 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000449 /* Timed out, enter rebinding state */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200450 log1("Entering rebinding state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000451 state = REBINDING;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000452 /* fall right through */
Mike Frysinger7031f622006-05-08 03:20:50 +0000453 case REBINDING:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000454 /* Lease is *really* about to run out,
455 * try to find DHCP server using broadcast */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000456 if (timeout > 0) {
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200457 /* send a broadcast renew request */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200458 send_renew(xid, 0 /*INADDR_ANY*/, requested_ip);
Denis Vlasenko19903f02008-05-21 07:03:03 +0000459 timeout >>= 1;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000460 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000461 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000462 /* Timed out, enter init state */
463 bb_info_msg("Lease lost, entering init state");
464 udhcp_run_script(NULL, "deconfig");
465 change_listen_mode(LISTEN_RAW);
466 state = INIT_SELECTING;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000467 /*timeout = 0; - already is */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000468 packet_num = 0;
469 continue;
470 /* case RELEASED: */
Mike Frysinger7031f622006-05-08 03:20:50 +0000471 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000472 /* yah, I know, *you* say it would never happen */
473 timeout = INT_MAX;
474 continue; /* back to main loop */
Denys Vlasenko4248c332009-06-26 23:23:16 +0200475 } /* if select timed out */
476
477 /* select() didn't timeout, something happened */
478
479 /* Is it a signal? */
480 /* note: udhcp_sp_read checks FD_ISSET before reading */
481 switch (udhcp_sp_read(&rfds)) {
482 case SIGUSR1:
483 perform_renew();
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200484 if (state == RENEW_REQUESTED)
485 goto case_RENEW_REQUESTED;
Denys Vlasenko4248c332009-06-26 23:23:16 +0200486 /* Start things over */
487 packet_num = 0;
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200488 /* Kill any timeouts, user wants this to hurry along */
Denys Vlasenko4248c332009-06-26 23:23:16 +0200489 timeout = 0;
490 continue;
491 case SIGUSR2:
492 perform_release(requested_ip, server_addr);
493 timeout = INT_MAX;
494 continue;
495 case SIGTERM:
496 bb_info_msg("Received SIGTERM");
497 if (opt & OPT_R) /* release on quit */
498 perform_release(requested_ip, server_addr);
499 goto ret0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000500 }
501
Denis Vlasenko739e30f2008-09-26 23:45:20 +0000502 /* Is it a packet? */
Denys Vlasenko4248c332009-06-26 23:23:16 +0200503 if (listen_mode == LISTEN_NONE || !FD_ISSET(sockfd, &rfds))
504 continue; /* no */
Mike Frysinger7031f622006-05-08 03:20:50 +0000505
Denys Vlasenko4248c332009-06-26 23:23:16 +0200506 {
507 int len;
508
509 /* A packet is ready, read it */
Mike Frysinger7031f622006-05-08 03:20:50 +0000510 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenko6de89942008-05-21 07:05:06 +0000511 len = udhcp_recv_kernel_packet(&packet, sockfd);
Denis Vlasenkob76b9a42008-01-25 22:46:34 +0000512 else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000513 len = udhcp_recv_raw_packet(&packet, sockfd);
Denys Vlasenko4248c332009-06-26 23:23:16 +0200514 if (len == -1) {
515 /* Error is severe, reopen socket */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200516 bb_info_msg("Read error: %s, reopening socket", strerror(errno));
Denis Vlasenko6de89942008-05-21 07:05:06 +0000517 sleep(discover_timeout); /* 3 seconds by default */
518 change_listen_mode(listen_mode); /* just close and reopen */
519 }
Denis Vlasenkob2342912008-05-21 07:02:16 +0000520 /* If this packet will turn out to be unrelated/bogus,
521 * we will go back and wait for next one.
522 * Be sure timeout is properly decreased. */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000523 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000524 if (len < 0)
525 continue;
Denys Vlasenko4248c332009-06-26 23:23:16 +0200526 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000527
Denys Vlasenko4248c332009-06-26 23:23:16 +0200528 if (packet.xid != xid) {
529 log1("xid %x (our is %x), ignoring packet",
530 (unsigned)packet.xid, (unsigned)xid);
531 continue;
532 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000533
Denys Vlasenko4248c332009-06-26 23:23:16 +0200534 /* Ignore packets that aren't for us */
535 if (packet.hlen != 6
536 || memcmp(packet.chaddr, client_config.client_mac, 6)
537 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200538//FIXME: need to also check that last 10 bytes are zero
Denys Vlasenko4248c332009-06-26 23:23:16 +0200539 log1("chaddr does not match, ignoring packet"); // log2?
540 continue;
541 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000542
Denys Vlasenko4248c332009-06-26 23:23:16 +0200543 message = get_option(&packet, DHCP_MESSAGE_TYPE);
544 if (message == NULL) {
545 bb_error_msg("no message type option, ignoring packet");
546 continue;
547 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000548
Denys Vlasenko4248c332009-06-26 23:23:16 +0200549 switch (state) {
550 case INIT_SELECTING:
551 /* Must be a DHCPOFFER to one of our xid's */
552 if (*message == DHCPOFFER) {
553 /* TODO: why we don't just fetch server's IP from IP header? */
554 temp = get_option(&packet, DHCP_SERVER_ID);
555 if (!temp) {
556 bb_error_msg("no server ID in message");
557 continue;
558 /* still selecting - this server looks bad */
Mike Frysinger7031f622006-05-08 03:20:50 +0000559 }
Denys Vlasenko4248c332009-06-26 23:23:16 +0200560 /* it IS unaligned sometimes, don't "optimize" */
561 move_from_unaligned32(server_addr, temp);
562 xid = packet.xid;
563 requested_ip = packet.yiaddr;
564
565 /* enter requesting state */
566 state = REQUESTING;
567 timeout = 0;
568 packet_num = 0;
569 already_waited_sec = 0;
570 }
571 continue;
Denys Vlasenko4248c332009-06-26 23:23:16 +0200572 case REQUESTING:
573 case RENEWING:
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200574 case RENEW_REQUESTED:
Denys Vlasenko4248c332009-06-26 23:23:16 +0200575 case REBINDING:
576 if (*message == DHCPACK) {
577 temp = get_option(&packet, DHCP_LEASE_TIME);
578 if (!temp) {
579 bb_error_msg("no lease time with ACK, using 1 hour lease");
580 lease_seconds = 60 * 60;
581 } else {
582 /* it IS unaligned sometimes, don't "optimize" */
583 move_from_unaligned32(lease_seconds, temp);
584 lease_seconds = ntohl(lease_seconds);
585 lease_seconds &= 0x0fffffff; /* paranoia: must not be prone to overflows */
586 if (lease_seconds < 10) /* and not too small */
587 lease_seconds = 10;
588 }
Denis Vlasenko223bc972007-11-22 00:58:49 +0000589#if ENABLE_FEATURE_UDHCPC_ARPING
Denys Vlasenko4248c332009-06-26 23:23:16 +0200590 if (opt & OPT_a) {
Denis Vlasenko739e30f2008-09-26 23:45:20 +0000591/* RFC 2131 3.1 paragraph 5:
592 * "The client receives the DHCPACK message with configuration
593 * parameters. The client SHOULD perform a final check on the
594 * parameters (e.g., ARP for allocated network address), and notes
595 * the duration of the lease specified in the DHCPACK message. At this
596 * point, the client is configured. If the client detects that the
597 * address is already in use (e.g., through the use of ARP),
598 * the client MUST send a DHCPDECLINE message to the server and restarts
599 * the configuration process..." */
Denys Vlasenko4248c332009-06-26 23:23:16 +0200600 if (!arpping(packet.yiaddr,
601 NULL,
602 (uint32_t) 0,
603 client_config.client_mac,
604 client_config.interface)
605 ) {
606 bb_info_msg("Offered address is in use "
607 "(got ARP reply), declining");
608 send_decline(xid, server_addr, packet.yiaddr);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000609
Denys Vlasenko4248c332009-06-26 23:23:16 +0200610 if (state != REQUESTING)
611 udhcp_run_script(NULL, "deconfig");
612 change_listen_mode(LISTEN_RAW);
613 state = INIT_SELECTING;
614 requested_ip = 0;
615 timeout = tryagain_timeout;
616 packet_num = 0;
617 already_waited_sec = 0;
618 continue; /* back to main loop */
Denis Vlasenko223bc972007-11-22 00:58:49 +0000619 }
Denys Vlasenko4248c332009-06-26 23:23:16 +0200620 }
Denis Vlasenko223bc972007-11-22 00:58:49 +0000621#endif
Denys Vlasenko4248c332009-06-26 23:23:16 +0200622 /* enter bound state */
623 timeout = lease_seconds / 2;
624 {
625 struct in_addr temp_addr;
626 temp_addr.s_addr = packet.yiaddr;
627 bb_info_msg("Lease of %s obtained, lease time %u",
628 inet_ntoa(temp_addr), (unsigned)lease_seconds);
629 }
630 requested_ip = packet.yiaddr;
631 udhcp_run_script(&packet,
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200632 ((state >= RENEWING && state <= RENEW_REQUESTED)
633 ? "renew" : "bound")
634 );
Mike Frysinger7031f622006-05-08 03:20:50 +0000635
Denys Vlasenko4248c332009-06-26 23:23:16 +0200636 state = BOUND;
637 change_listen_mode(LISTEN_NONE);
638 if (opt & OPT_q) { /* quit after lease */
639 if (opt & OPT_R) /* release on quit */
640 perform_release(requested_ip, server_addr);
641 goto ret0;
642 }
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000643#if BB_MMU /* NOMMU case backgrounded earlier */
Denys Vlasenko4248c332009-06-26 23:23:16 +0200644 if (!(opt & OPT_f)) {
645 client_background();
646 /* do not background again! */
647 opt = ((opt & ~OPT_b) | OPT_f);
648 }
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000649#endif
Denys Vlasenko4248c332009-06-26 23:23:16 +0200650 already_waited_sec = 0;
651 continue; /* back to main loop */
Mike Frysinger7031f622006-05-08 03:20:50 +0000652 }
Denys Vlasenko4248c332009-06-26 23:23:16 +0200653 if (*message == DHCPNAK) {
654 /* return to init state */
655 bb_info_msg("Received DHCP NAK");
656 udhcp_run_script(&packet, "nak");
657 if (state != REQUESTING)
658 udhcp_run_script(NULL, "deconfig");
659 change_listen_mode(LISTEN_RAW);
660 sleep(3); /* avoid excessive network traffic */
661 state = INIT_SELECTING;
662 requested_ip = 0;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000663 timeout = 0;
Denys Vlasenko4248c332009-06-26 23:23:16 +0200664 packet_num = 0;
665 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000666 }
Denys Vlasenko4248c332009-06-26 23:23:16 +0200667 continue;
668 /* case BOUND: - ignore all packets */
669 /* case RELEASED: - ignore all packets */
Mike Frysinger7031f622006-05-08 03:20:50 +0000670 }
Denys Vlasenko4248c332009-06-26 23:23:16 +0200671 /* back to main loop */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000672 } /* for (;;) - main loop ends */
673
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000674 ret0:
675 retval = 0;
676 ret:
Denis Vlasenkob2342912008-05-21 07:02:16 +0000677 /*if (client_config.pidfile) - remove_pidfile has its own check */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000678 remove_pidfile(client_config.pidfile);
679 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000680}