blob: 3e2cd121779212c8651bc96621983b668be117e7 [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;
Denys Vlasenko87fa2162010-03-20 18:06:23 +0100128 int len = strnlen(str, 255);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000129 storage = xzalloc(len + extra + OPT_DATA);
130 storage[OPT_CODE] = code;
131 storage[OPT_LEN] = len + extra;
132 memcpy(storage + extra + OPT_DATA, str, len);
133 return storage;
134}
135
136
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000137int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000138int udhcpc_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000139{
140 uint8_t *temp, *message;
Denys Vlasenko87fa2162010-03-20 18:06:23 +0100141 const char *str_c, *str_V, *str_h, *str_F, *str_r;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000142 IF_FEATURE_UDHCP_PORT(char *str_P;)
Denis Vlasenko19183682007-12-10 07:03:38 +0000143 llist_t *list_O = NULL;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000144 int tryagain_timeout = 20;
145 int discover_timeout = 3;
146 int discover_retries = 3;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000147 uint32_t server_addr = server_addr; /* for compiler */
148 uint32_t requested_ip = 0;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000149 uint32_t xid = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000150 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000151 int packet_num;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000152 int timeout; /* must be signed */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000153 unsigned already_waited_sec;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000154 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000155 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000156 int retval;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000157 struct timeval tv;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200158 struct dhcp_packet packet;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000159 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000160
Denys Vlasenkof3b92d32009-06-19 12:10:38 +0200161#if ENABLE_LONG_OPTS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000162 static const char udhcpc_longopts[] ALIGN1 =
Denis Vlasenko19183682007-12-10 07:03:38 +0000163 "clientid\0" Required_argument "c"
164 "clientid-none\0" No_argument "C"
165 "vendorclass\0" Required_argument "V"
Denis Vlasenko19183682007-12-10 07:03:38 +0000166 "hostname\0" Required_argument "H"
167 "fqdn\0" Required_argument "F"
168 "interface\0" Required_argument "i"
169 "now\0" No_argument "n"
170 "pidfile\0" Required_argument "p"
171 "quit\0" No_argument "q"
172 "release\0" No_argument "R"
173 "request\0" Required_argument "r"
174 "script\0" Required_argument "s"
175 "timeout\0" Required_argument "T"
176 "version\0" No_argument "v"
177 "retries\0" Required_argument "t"
178 "tryagain\0" Required_argument "A"
179 "syslog\0" No_argument "S"
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000180 "request-option\0" Required_argument "O"
181 "no-default-options\0" No_argument "o"
182 "foreground\0" No_argument "f"
183 "background\0" No_argument "b"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000184 IF_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a")
185 IF_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000186 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000187#endif
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000188 enum {
189 OPT_c = 1 << 0,
190 OPT_C = 1 << 1,
191 OPT_V = 1 << 2,
192 OPT_H = 1 << 3,
193 OPT_h = 1 << 4,
194 OPT_F = 1 << 5,
Denis Vlasenkof716a6d2008-06-15 09:49:21 +0000195 OPT_i = 1 << 6,
196 OPT_n = 1 << 7,
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000197 OPT_p = 1 << 8,
198 OPT_q = 1 << 9,
199 OPT_R = 1 << 10,
200 OPT_r = 1 << 11,
201 OPT_s = 1 << 12,
202 OPT_T = 1 << 13,
203 OPT_t = 1 << 14,
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200204 OPT_S = 1 << 15,
205 OPT_A = 1 << 16,
206 OPT_O = 1 << 17,
207 OPT_o = 1 << 18,
208 OPT_f = 1 << 19,
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000209/* The rest has variable bit positions, need to be clever */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200210 OPTBIT_f = 19,
211 USE_FOR_MMU( OPTBIT_b,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000212 IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,)
213 IF_FEATURE_UDHCP_PORT( OPTBIT_P,)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200214 USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000215 IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,)
216 IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,)
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000217 };
218
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000219 /* Default options. */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000220 IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
221 IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000222 client_config.interface = "eth0";
223 client_config.script = DEFAULT_SCRIPT;
Denys Vlasenko87fa2162010-03-20 18:06:23 +0100224 str_V = "udhcp "BB_VER;
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 Vlasenko68af8e72007-11-22 21:41:14 +0000249 if (opt & (OPT_h|OPT_H))
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000250 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
251 if (opt & OPT_F) {
Denys Vlasenko87fa2162010-03-20 18:06:23 +0100252 /* FQDN option format: [0x51][len][flags][0][0]<fqdn> */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000253 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
Denys Vlasenko87fa2162010-03-20 18:06:23 +0100254 /* Flag bits: 0000NEOS
255 * S: 1 = Client requests server to update A RR in DNS as well as PTR
256 * O: 1 = Server indicates to client that DNS has been updated regardless
257 * E: 1 = Name is in DNS format, i.e. <4>host<6>domain<3>com<0>,
258 * not "host.domain.com". Format 0 is obsolete.
259 * N: 1 = Client requests server to not update DNS (S must be 0 then)
260 * Two [0] bytes which follow are deprecated and must be 0.
261 */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000262 client_config.fqdn[OPT_DATA + 0] = 0x1;
Denys Vlasenko87fa2162010-03-20 18:06:23 +0100263 /*client_config.fqdn[OPT_DATA + 1] = 0; - xzalloc did it */
264 /*client_config.fqdn[OPT_DATA + 2] = 0; */
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000265 }
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000266 if (opt & OPT_r)
267 requested_ip = inet_addr(str_r);
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000268#if ENABLE_FEATURE_UDHCP_PORT
269 if (opt & OPT_P) {
270 CLIENT_PORT = xatou16(str_P);
271 SERVER_PORT = CLIENT_PORT - 1;
272 }
273#endif
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000274 if (opt & OPT_o)
275 client_config.no_default_options = 1;
Denis Vlasenko19183682007-12-10 07:03:38 +0000276 while (list_O) {
Denis Vlasenko18c93022008-08-27 22:29:43 +0000277 char *optstr = llist_pop(&list_O);
278 int n = index_in_strings(dhcp_option_strings, optstr);
Denis Vlasenko19183682007-12-10 07:03:38 +0000279 if (n < 0)
Denis Vlasenko18c93022008-08-27 22:29:43 +0000280 bb_error_msg_and_die("unknown option '%s'", optstr);
Denis Vlasenko19183682007-12-10 07:03:38 +0000281 n = dhcp_options[n].code;
282 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
Denis Vlasenko19183682007-12-10 07:03:38 +0000283 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000284
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200285 if (udhcp_read_interface(client_config.interface,
286 &client_config.ifindex,
287 NULL,
288 client_config.client_mac)
289 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000290 return 1;
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200291 }
292
Denys Vlasenko87fa2162010-03-20 18:06:23 +0100293 if (opt & OPT_c) {
294 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
295 } else if (!(opt & OPT_C)) {
296 /* not set and not suppressed, set the default client ID */
297 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
298 client_config.clientid[OPT_DATA] = 1; /* type: ethernet */
299 memcpy(client_config.clientid + OPT_DATA+1, client_config.client_mac, 6);
300 }
301 if (str_V[0] != '\0')
302 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000303#if !BB_MMU
304 /* on NOMMU reexec (i.e., background) early */
305 if (!(opt & OPT_f)) {
306 bb_daemonize_or_rexec(0 /* flags */, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000307 logmode = LOGMODE_NONE;
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000308 }
309#endif
310 if (opt & OPT_S) {
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000311 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000312 logmode |= LOGMODE_SYSLOG;
313 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000314
Denis Vlasenko80edead2007-08-02 22:31:05 +0000315 /* Make sure fd 0,1,2 are open */
316 bb_sanitize_stdio();
317 /* Equivalent of doing a fflush after every \n */
318 setlinebuf(stdout);
319
320 /* Create pidfile */
321 write_pidfile(client_config.pidfile);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000322
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000323 /* Goes to stdout (unless NOMMU) and possibly syslog */
Denis Vlasenkodef88982007-10-07 17:06:01 +0000324 bb_info_msg("%s (v"BB_VER") started", applet_name);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000325
Denys Vlasenko4248c332009-06-26 23:23:16 +0200326 /* Set up the signal pipe */
Mike Frysinger7031f622006-05-08 03:20:50 +0000327 udhcp_sp_setup();
328
329 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000330 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000331 change_listen_mode(LISTEN_RAW);
Denis Vlasenkob539c842007-11-29 08:17:45 +0000332 packet_num = 0;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000333 timeout = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000334 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000335
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000336 /* Main event loop. select() waits on signal pipe and possibly
337 * on sockfd.
338 * "continue" statements in code below jump to the top of the loop.
339 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000340 for (;;) {
Denis Vlasenkoec64a572009-01-14 00:28:03 +0000341 /* silence "uninitialized!" warning */
342 unsigned timestamp_before_wait = timestamp_before_wait;
Mike Frysinger7031f622006-05-08 03:20:50 +0000343
Denis Vlasenko517413f2008-12-10 11:16:47 +0000344 //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode);
345
346 /* Was opening raw or udp socket here
347 * if (listen_mode != LISTEN_NONE && sockfd < 0),
348 * but on fast network renew responses return faster
349 * than we open sockets. Thus this code is moved
350 * to change_listen_mode(). Thus we open listen socket
351 * BEFORE we send renew request (see "case BOUND:"). */
352
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000353 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000354
Denis Vlasenko19903f02008-05-21 07:03:03 +0000355 tv.tv_sec = timeout - already_waited_sec;
Denis Vlasenkob2342912008-05-21 07:02:16 +0000356 tv.tv_usec = 0;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000357 retval = 0; /* If we already timed out, fall through, else... */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200358 if ((int)tv.tv_sec > 0) {
Denis Vlasenkob2342912008-05-21 07:02:16 +0000359 timestamp_before_wait = (unsigned)monotonic_sec();
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200360 log1("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000361 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenkob2342912008-05-21 07:02:16 +0000362 if (retval < 0) {
363 /* EINTR? A signal was caught, don't panic */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200364 if (errno == EINTR) {
365 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
Denis Vlasenkob2342912008-05-21 07:02:16 +0000366 continue;
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200367 }
Denis Vlasenko80edead2007-08-02 22:31:05 +0000368 /* Else: an error occured, panic! */
369 bb_perror_msg_and_die("select");
370 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000371 }
372
373 /* If timeout dropped to zero, time to become active:
374 * resend discover/renew/whatever
375 */
376 if (retval == 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000377 /* We will restart the wait in any case */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000378 already_waited_sec = 0;
379
Mike Frysinger7031f622006-05-08 03:20:50 +0000380 switch (state) {
381 case INIT_SELECTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000382 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000383 if (packet_num == 0)
384 xid = random_xid();
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200385 /* broadcast */
386 send_discover(xid, requested_ip);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000387 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000388 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000389 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000390 }
Denis Vlasenkocdb0b652008-09-26 09:34:15 +0000391 leasefail:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000392 udhcp_run_script(NULL, "leasefail");
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000393#if BB_MMU /* -b is not supported on NOMMU */
394 if (opt & OPT_b) { /* background if no lease */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000395 bb_info_msg("No lease, forking to background");
396 client_background();
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000397 /* do not background again! */
398 opt = ((opt & ~OPT_b) | OPT_f);
399 } else
400#endif
401 if (opt & OPT_n) { /* abort if no lease */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000402 bb_info_msg("No lease, failing");
403 retval = 1;
404 goto ret;
405 }
Denis Vlasenko6de89942008-05-21 07:05:06 +0000406 /* wait before trying again */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000407 timeout = tryagain_timeout;
408 packet_num = 0;
409 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000410 case REQUESTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000411 if (packet_num < discover_retries) {
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200412 /* send broadcast select packet */
413 send_select(xid, server_addr, requested_ip);
Denis Vlasenko6de89942008-05-21 07:05:06 +0000414 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000415 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000416 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000417 }
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200418 /* Timed out, go back to init state.
419 * "discover...select...discover..." loops
Denis Vlasenko7d9399e2008-09-26 22:21:03 +0000420 * were seen in the wild. Treat them similarly
Denis Vlasenkocdb0b652008-09-26 09:34:15 +0000421 * to "no response to discover" case */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200422 change_listen_mode(LISTEN_RAW);
Denis Vlasenko7d9399e2008-09-26 22:21:03 +0000423 state = INIT_SELECTING;
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200424 goto leasefail;
Mike Frysinger7031f622006-05-08 03:20:50 +0000425 case BOUND:
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200426 /* 1/2 lease passed, enter renewing state */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200427 state = RENEWING;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000428 change_listen_mode(LISTEN_KERNEL);
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200429 log1("Entering renew state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000430 /* fall right through */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200431 case RENEW_REQUESTED: /* manual (SIGUSR1) renew */
432 case_RENEW_REQUESTED:
Mike Frysinger7031f622006-05-08 03:20:50 +0000433 case RENEWING:
Denis Vlasenko19903f02008-05-21 07:03:03 +0000434 if (timeout > 60) {
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200435 /* send an unicast renew request */
436 /* Sometimes observed to fail (EADDRNOTAVAIL) to bind
437 * a new UDP socket for sending inside send_renew.
438 * I hazard to guess existing listening socket
439 * is somehow conflicting with it, but why is it
440 * not deterministic then?! Strange.
Denys Vlasenko753a3ce2009-07-19 04:27:10 +0200441 * Anyway, it does recover by eventually failing through
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200442 * into INIT_SELECTING state.
443 */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200444 send_renew(xid, server_addr, requested_ip);
Denis Vlasenko19903f02008-05-21 07:03:03 +0000445 timeout >>= 1;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000446 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000447 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000448 /* Timed out, enter rebinding state */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200449 log1("Entering rebinding state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000450 state = REBINDING;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000451 /* fall right through */
Mike Frysinger7031f622006-05-08 03:20:50 +0000452 case REBINDING:
Denys Vlasenko219757f2009-10-08 23:05:46 +0200453 /* Switch to bcast receive */
454 change_listen_mode(LISTEN_RAW);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000455 /* Lease is *really* about to run out,
456 * try to find DHCP server using broadcast */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000457 if (timeout > 0) {
Denys Vlasenko7d6a7912009-07-19 04:24:23 +0200458 /* send a broadcast renew request */
Denys Vlasenko13ca4b12009-07-19 04:07:21 +0200459 send_renew(xid, 0 /*INADDR_ANY*/, requested_ip);
Denis Vlasenko19903f02008-05-21 07:03:03 +0000460 timeout >>= 1;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000461 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000462 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000463 /* Timed out, enter init state */
464 bb_info_msg("Lease lost, entering init state");
465 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000466 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;
Denys Vlasenko4abfc262009-07-19 04:35:16 +0200631 udhcp_run_script(&packet, state == REQUESTING ? "bound" : "renew");
Mike Frysinger7031f622006-05-08 03:20:50 +0000632
Denys Vlasenko4248c332009-06-26 23:23:16 +0200633 state = BOUND;
634 change_listen_mode(LISTEN_NONE);
635 if (opt & OPT_q) { /* quit after lease */
636 if (opt & OPT_R) /* release on quit */
637 perform_release(requested_ip, server_addr);
638 goto ret0;
639 }
Justin Maggardaa369e02009-08-13 01:26:17 +0200640 /* future renew failures should not exit (JM) */
641 opt &= ~OPT_n;
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000642#if BB_MMU /* NOMMU case backgrounded earlier */
Denys Vlasenko4248c332009-06-26 23:23:16 +0200643 if (!(opt & OPT_f)) {
644 client_background();
645 /* do not background again! */
646 opt = ((opt & ~OPT_b) | OPT_f);
647 }
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000648#endif
Denys Vlasenko4248c332009-06-26 23:23:16 +0200649 already_waited_sec = 0;
650 continue; /* back to main loop */
Mike Frysinger7031f622006-05-08 03:20:50 +0000651 }
Denys Vlasenko4248c332009-06-26 23:23:16 +0200652 if (*message == DHCPNAK) {
653 /* return to init state */
654 bb_info_msg("Received DHCP NAK");
655 udhcp_run_script(&packet, "nak");
656 if (state != REQUESTING)
657 udhcp_run_script(NULL, "deconfig");
658 change_listen_mode(LISTEN_RAW);
659 sleep(3); /* avoid excessive network traffic */
660 state = INIT_SELECTING;
661 requested_ip = 0;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000662 timeout = 0;
Denys Vlasenko4248c332009-06-26 23:23:16 +0200663 packet_num = 0;
664 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000665 }
Denys Vlasenko4248c332009-06-26 23:23:16 +0200666 continue;
667 /* case BOUND: - ignore all packets */
668 /* case RELEASED: - ignore all packets */
Mike Frysinger7031f622006-05-08 03:20:50 +0000669 }
Denys Vlasenko4248c332009-06-26 23:23:16 +0200670 /* back to main loop */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000671 } /* for (;;) - main loop ends */
672
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000673 ret0:
674 retval = 0;
675 ret:
Denis Vlasenkob2342912008-05-21 07:02:16 +0000676 /*if (client_config.pidfile) - remove_pidfile has its own check */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000677 remove_pidfile(client_config.pidfile);
678 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000679}