blob: eed9d9a01b5cd54b1bfff3baa07f6c9077d4a8c9 [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
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020023#define LISTEN_NONE 0
Mike Frysinger7031f622006-05-08 03:20:50 +000024#define LISTEN_KERNEL 1
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020025#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{
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020044 log1("entering %s listen mode",
Mike Frysinger7031f622006-05-08 03:20:50 +000045 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Denis Vlasenko517413f2008-12-10 11:16:47 +000046
47 listen_mode = new_mode;
Denis Vlasenkofbd29182007-04-07 01:05:47 +000048 if (sockfd >= 0) {
49 close(sockfd);
50 sockfd = -1;
51 }
Denis Vlasenko517413f2008-12-10 11:16:47 +000052 if (new_mode == LISTEN_KERNEL)
53 sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
54 else if (new_mode != LISTEN_NONE)
55 sockfd = udhcp_raw_socket(client_config.ifindex);
56 /* else LISTEN_NONE: sockfd stay closed */
Mike Frysinger7031f622006-05-08 03:20:50 +000057}
58
59
60/* perform a renew */
61static void perform_renew(void)
62{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000063 bb_info_msg("Performing a DHCP renew");
Mike Frysinger7031f622006-05-08 03:20:50 +000064 switch (state) {
65 case BOUND:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000066 change_listen_mode(LISTEN_KERNEL);
Mike Frysinger7031f622006-05-08 03:20:50 +000067 case RENEWING:
68 case REBINDING:
69 state = RENEW_REQUESTED;
70 break;
71 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
Rob Landley3f785612006-05-28 01:06:36 +000072 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000073 case REQUESTING:
74 case RELEASED:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +000075 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +000076 state = INIT_SELECTING;
77 break;
78 case INIT_SELECTING:
79 break;
80 }
Mike Frysinger7031f622006-05-08 03:20:50 +000081}
82
83
84/* perform a release */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +000085static void perform_release(uint32_t requested_ip, uint32_t server_addr)
Mike Frysinger7031f622006-05-08 03:20:50 +000086{
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000087 char buffer[sizeof("255.255.255.255")];
Mike Frysinger7031f622006-05-08 03:20:50 +000088 struct in_addr temp_addr;
89
90 /* send release packet */
91 if (state == BOUND || state == RENEWING || state == REBINDING) {
92 temp_addr.s_addr = server_addr;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000093 strcpy(buffer, inet_ntoa(temp_addr));
Mike Frysinger7031f622006-05-08 03:20:50 +000094 temp_addr.s_addr = requested_ip;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000095 bb_info_msg("Unicasting a release of %s to %s",
Mike Frysinger7031f622006-05-08 03:20:50 +000096 inet_ntoa(temp_addr), buffer);
97 send_release(server_addr, requested_ip); /* unicast */
Rob Landley3f785612006-05-28 01:06:36 +000098 udhcp_run_script(NULL, "deconfig");
Mike Frysinger7031f622006-05-08 03:20:50 +000099 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000100 bb_info_msg("Entering released state");
Mike Frysinger7031f622006-05-08 03:20:50 +0000101
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000102 change_listen_mode(LISTEN_NONE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 state = RELEASED;
Mike Frysinger7031f622006-05-08 03:20:50 +0000104}
105
106
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000107#if BB_MMU
Mike Frysinger7031f622006-05-08 03:20:50 +0000108static void client_background(void)
109{
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000110 bb_daemonize(0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000111 logmode &= ~LOGMODE_STDIO;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000112 /* rewrite pidfile, as our pid is different now */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000113 write_pidfile(client_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +0000114}
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000115#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000116
117
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000118static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
119{
120 uint8_t *storage;
121 int len = strlen(str);
122 if (len > 255) len = 255;
123 storage = xzalloc(len + extra + OPT_DATA);
124 storage[OPT_CODE] = code;
125 storage[OPT_LEN] = len + extra;
126 memcpy(storage + extra + OPT_DATA, str, len);
127 return storage;
128}
129
130
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000131int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000132int udhcpc_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000133{
134 uint8_t *temp, *message;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000135 char *str_c, *str_V, *str_h, *str_F, *str_r;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000136 IF_FEATURE_UDHCP_PORT(char *str_P;)
Denis Vlasenko19183682007-12-10 07:03:38 +0000137 llist_t *list_O = NULL;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000138 int tryagain_timeout = 20;
139 int discover_timeout = 3;
140 int discover_retries = 3;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000141 uint32_t server_addr = server_addr; /* for compiler */
142 uint32_t requested_ip = 0;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000143 uint32_t xid = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000144 uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000145 int packet_num;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000146 int timeout; /* must be signed */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000147 unsigned already_waited_sec;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000148 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000149 int max_fd;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000150 int retval;
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000151 struct timeval tv;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000152 struct dhcpMessage packet;
153 fd_set rfds;
Mike Frysinger7031f622006-05-08 03:20:50 +0000154
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000155#if ENABLE_GETOPT_LONG
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000156 static const char udhcpc_longopts[] ALIGN1 =
Denis Vlasenko19183682007-12-10 07:03:38 +0000157 "clientid\0" Required_argument "c"
158 "clientid-none\0" No_argument "C"
159 "vendorclass\0" Required_argument "V"
Denis Vlasenko19183682007-12-10 07:03:38 +0000160 "hostname\0" Required_argument "H"
161 "fqdn\0" Required_argument "F"
162 "interface\0" Required_argument "i"
163 "now\0" No_argument "n"
164 "pidfile\0" Required_argument "p"
165 "quit\0" No_argument "q"
166 "release\0" No_argument "R"
167 "request\0" Required_argument "r"
168 "script\0" Required_argument "s"
169 "timeout\0" Required_argument "T"
170 "version\0" No_argument "v"
171 "retries\0" Required_argument "t"
172 "tryagain\0" Required_argument "A"
173 "syslog\0" No_argument "S"
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000174 "request-option\0" Required_argument "O"
175 "no-default-options\0" No_argument "o"
176 "foreground\0" No_argument "f"
177 "background\0" No_argument "b"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000178 IF_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a")
179 IF_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000180 ;
Denis Vlasenkoc61852a2006-11-29 11:09:43 +0000181#endif
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000182 enum {
183 OPT_c = 1 << 0,
184 OPT_C = 1 << 1,
185 OPT_V = 1 << 2,
186 OPT_H = 1 << 3,
187 OPT_h = 1 << 4,
188 OPT_F = 1 << 5,
Denis Vlasenkof716a6d2008-06-15 09:49:21 +0000189 OPT_i = 1 << 6,
190 OPT_n = 1 << 7,
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000191 OPT_p = 1 << 8,
192 OPT_q = 1 << 9,
193 OPT_R = 1 << 10,
194 OPT_r = 1 << 11,
195 OPT_s = 1 << 12,
196 OPT_T = 1 << 13,
197 OPT_t = 1 << 14,
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200198 OPT_S = 1 << 15,
199 OPT_A = 1 << 16,
200 OPT_O = 1 << 17,
201 OPT_o = 1 << 18,
202 OPT_f = 1 << 19,
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000203/* The rest has variable bit positions, need to be clever */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200204 OPTBIT_f = 19,
205 USE_FOR_MMU( OPTBIT_b,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000206 IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,)
207 IF_FEATURE_UDHCP_PORT( OPTBIT_P,)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200208 USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000209 IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,)
210 IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,)
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000211 };
212
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000213 /* Default options. */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000214 IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
215 IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000216 client_config.interface = "eth0";
217 client_config.script = DEFAULT_SCRIPT;
Mike Frysinger7031f622006-05-08 03:20:50 +0000218
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000219 /* Parse command line */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000220 /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200221 opt_complementary = "c--C:C--c:O::T+:t+:A+"
222#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
223 ":vv"
224#endif
225 ;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000226 IF_GETOPT_LONG(applet_long_options = udhcpc_longopts;)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200227 opt = getopt32(argv, "c:CV:H:h:F:i:np:qRr:s:T:t:SA:O:of"
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000228 USE_FOR_MMU("b")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000229 IF_FEATURE_UDHCPC_ARPING("a")
230 IF_FEATURE_UDHCP_PORT("P:")
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200231 "v"
Denis Vlasenko19183682007-12-10 07:03:38 +0000232 , &str_c, &str_V, &str_h, &str_h, &str_F
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000233 , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
234 , &client_config.script /* s */
235 , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
Denis Vlasenko19183682007-12-10 07:03:38 +0000236 , &list_O
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000237 IF_FEATURE_UDHCP_PORT(, &str_P)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200238#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
239 , &dhcp_verbose
240#endif
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000241 );
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000242 if (opt & OPT_c)
243 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000244 if (opt & OPT_V)
245 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000246 if (opt & (OPT_h|OPT_H))
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000247 client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
248 if (opt & OPT_F) {
249 client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
250 /* Flags: 0000NEOS
251 S: 1 => Client requests Server to update A RR in DNS as well as PTR
252 O: 1 => Server indicates to client that DNS has been updated regardless
Denis Vlasenko873b8952008-06-15 05:40:08 +0000253 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 +0000254 N: 1 => Client requests Server to not update DNS
255 */
256 client_config.fqdn[OPT_DATA + 0] = 0x1;
257 /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
258 /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
259 }
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000260 if (opt & OPT_r)
261 requested_ip = inet_addr(str_r);
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000262#if ENABLE_FEATURE_UDHCP_PORT
263 if (opt & OPT_P) {
264 CLIENT_PORT = xatou16(str_P);
265 SERVER_PORT = CLIENT_PORT - 1;
266 }
267#endif
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000268 if (opt & OPT_o)
269 client_config.no_default_options = 1;
Denis Vlasenko19183682007-12-10 07:03:38 +0000270 while (list_O) {
Denis Vlasenko18c93022008-08-27 22:29:43 +0000271 char *optstr = llist_pop(&list_O);
272 int n = index_in_strings(dhcp_option_strings, optstr);
Denis Vlasenko19183682007-12-10 07:03:38 +0000273 if (n < 0)
Denis Vlasenko18c93022008-08-27 22:29:43 +0000274 bb_error_msg_and_die("unknown option '%s'", optstr);
Denis Vlasenko19183682007-12-10 07:03:38 +0000275 n = dhcp_options[n].code;
276 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
Denis Vlasenko19183682007-12-10 07:03:38 +0000277 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000278
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200279 if (udhcp_read_interface(client_config.interface,
280 &client_config.ifindex,
281 NULL,
282 client_config.client_mac)
283 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000284 return 1;
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200285 }
286
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000287#if !BB_MMU
288 /* on NOMMU reexec (i.e., background) early */
289 if (!(opt & OPT_f)) {
290 bb_daemonize_or_rexec(0 /* flags */, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000291 logmode = LOGMODE_NONE;
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000292 }
293#endif
294 if (opt & OPT_S) {
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000295 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000296 logmode |= LOGMODE_SYSLOG;
297 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000298
Denis Vlasenko80edead2007-08-02 22:31:05 +0000299 /* Make sure fd 0,1,2 are open */
300 bb_sanitize_stdio();
301 /* Equivalent of doing a fflush after every \n */
302 setlinebuf(stdout);
303
304 /* Create pidfile */
305 write_pidfile(client_config.pidfile);
Denis Vlasenko80edead2007-08-02 22:31:05 +0000306
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000307 /* Goes to stdout (unless NOMMU) and possibly syslog */
Denis Vlasenkodef88982007-10-07 17:06:01 +0000308 bb_info_msg("%s (v"BB_VER") started", applet_name);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000309
Mike Frysinger7031f622006-05-08 03:20:50 +0000310 /* if not set, and not suppressed, setup the default client ID */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000311 if (!client_config.clientid && !(opt & OPT_C)) {
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000312 client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
Mike Frysinger7031f622006-05-08 03:20:50 +0000313 client_config.clientid[OPT_DATA] = 1;
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200314 memcpy(client_config.clientid + OPT_DATA+1, client_config.client_mac, 6);
Mike Frysinger7031f622006-05-08 03:20:50 +0000315 }
316
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000317 if (!client_config.vendorclass)
318 client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000319
320 /* setup the signal pipe */
321 udhcp_sp_setup();
322
323 state = INIT_SELECTING;
Rob Landley3f785612006-05-28 01:06:36 +0000324 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000325 change_listen_mode(LISTEN_RAW);
Denis Vlasenkob539c842007-11-29 08:17:45 +0000326 packet_num = 0;
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000327 timeout = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000328 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000329
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000330 /* Main event loop. select() waits on signal pipe and possibly
331 * on sockfd.
332 * "continue" statements in code below jump to the top of the loop.
333 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000334 for (;;) {
Denis Vlasenkoec64a572009-01-14 00:28:03 +0000335 /* silence "uninitialized!" warning */
336 unsigned timestamp_before_wait = timestamp_before_wait;
Mike Frysinger7031f622006-05-08 03:20:50 +0000337
Denis Vlasenko517413f2008-12-10 11:16:47 +0000338 //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode);
339
340 /* Was opening raw or udp socket here
341 * if (listen_mode != LISTEN_NONE && sockfd < 0),
342 * but on fast network renew responses return faster
343 * than we open sockets. Thus this code is moved
344 * to change_listen_mode(). Thus we open listen socket
345 * BEFORE we send renew request (see "case BOUND:"). */
346
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000347 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000348
Denis Vlasenko19903f02008-05-21 07:03:03 +0000349 tv.tv_sec = timeout - already_waited_sec;
Denis Vlasenkob2342912008-05-21 07:02:16 +0000350 tv.tv_usec = 0;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000351 retval = 0; /* If we already timed out, fall through, else... */
Mike Frysinger7031f622006-05-08 03:20:50 +0000352 if (tv.tv_sec > 0) {
Denis Vlasenkob2342912008-05-21 07:02:16 +0000353 timestamp_before_wait = (unsigned)monotonic_sec();
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200354 log1("Waiting on select...");
Mike Frysinger7031f622006-05-08 03:20:50 +0000355 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
Denis Vlasenkob2342912008-05-21 07:02:16 +0000356 if (retval < 0) {
357 /* EINTR? A signal was caught, don't panic */
358 if (errno == EINTR)
359 continue;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000360 /* Else: an error occured, panic! */
361 bb_perror_msg_and_die("select");
362 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000363 }
364
365 /* If timeout dropped to zero, time to become active:
366 * resend discover/renew/whatever
367 */
368 if (retval == 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000369 /* We will restart the wait in any case */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000370 already_waited_sec = 0;
371
Mike Frysinger7031f622006-05-08 03:20:50 +0000372 switch (state) {
373 case INIT_SELECTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000374 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000375 if (packet_num == 0)
376 xid = random_xid();
377
Mike Frysinger7031f622006-05-08 03:20:50 +0000378 send_discover(xid, requested_ip); /* broadcast */
379
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000380 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000381 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000382 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000383 }
Denis Vlasenkocdb0b652008-09-26 09:34:15 +0000384 leasefail:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000385 udhcp_run_script(NULL, "leasefail");
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000386#if BB_MMU /* -b is not supported on NOMMU */
387 if (opt & OPT_b) { /* background if no lease */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000388 bb_info_msg("No lease, forking to background");
389 client_background();
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000390 /* do not background again! */
391 opt = ((opt & ~OPT_b) | OPT_f);
392 } else
393#endif
394 if (opt & OPT_n) { /* abort if no lease */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000395 bb_info_msg("No lease, failing");
396 retval = 1;
397 goto ret;
398 }
Denis Vlasenko6de89942008-05-21 07:05:06 +0000399 /* wait before trying again */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000400 timeout = tryagain_timeout;
401 packet_num = 0;
402 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000403 case RENEW_REQUESTED:
404 case REQUESTING:
Denis Vlasenko223bc972007-11-22 00:58:49 +0000405 if (packet_num < discover_retries) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000406 /* send request packet */
Denis Vlasenko6de89942008-05-21 07:05:06 +0000407 if (state == RENEW_REQUESTED) /* unicast */
408 send_renew(xid, server_addr, requested_ip);
409 else /* broadcast */
Denis Vlasenkoc321b512008-09-26 16:29:12 +0000410 send_select(xid, server_addr, requested_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000411
Denis Vlasenko6de89942008-05-21 07:05:06 +0000412 timeout = discover_timeout;
Mike Frysinger7031f622006-05-08 03:20:50 +0000413 packet_num++;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000414 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000415 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000416 /* timed out, go back to init state */
417 if (state == RENEW_REQUESTED)
418 udhcp_run_script(NULL, "deconfig");
419 change_listen_mode(LISTEN_RAW);
Denis Vlasenkocdb0b652008-09-26 09:34:15 +0000420 /* "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 */
Denis Vlasenko7d9399e2008-09-26 22:21:03 +0000423 if (state == REQUESTING) {
424 state = INIT_SELECTING;
Denis Vlasenkocdb0b652008-09-26 09:34:15 +0000425 goto leasefail;
Denis Vlasenko7d9399e2008-09-26 22:21:03 +0000426 }
427 state = INIT_SELECTING;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000428 timeout = 0;
429 packet_num = 0;
430 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000431 case BOUND:
Denis Vlasenko19903f02008-05-21 07:03:03 +0000432 /* Half of the lease passed, time to enter renewing state */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000433 change_listen_mode(LISTEN_KERNEL);
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200434 log1("Entering renew state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000435 state = RENEWING;
Mike Frysinger7031f622006-05-08 03:20:50 +0000436 /* fall right through */
437 case RENEWING:
Denis Vlasenko19903f02008-05-21 07:03:03 +0000438 if (timeout > 60) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000439 /* send a request packet */
440 send_renew(xid, server_addr, requested_ip); /* unicast */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000441 timeout >>= 1;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000442 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000443 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000444 /* Timed out, enter rebinding state */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200445 log1("Entering rebinding state");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000446 state = REBINDING;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000447 /* fall right through */
Mike Frysinger7031f622006-05-08 03:20:50 +0000448 case REBINDING:
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000449 /* Lease is *really* about to run out,
450 * try to find DHCP server using broadcast */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000451 if (timeout > 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000452 /* send a request packet */
Denis Vlasenkoec64a572009-01-14 00:28:03 +0000453 send_renew(xid, 0 /*INADDR_ANY*/, requested_ip); /* broadcast */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000454 timeout >>= 1;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000455 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000456 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000457 /* Timed out, enter init state */
458 bb_info_msg("Lease lost, entering init state");
459 udhcp_run_script(NULL, "deconfig");
460 change_listen_mode(LISTEN_RAW);
461 state = INIT_SELECTING;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000462 /*timeout = 0; - already is */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000463 packet_num = 0;
464 continue;
465 /* case RELEASED: */
Mike Frysinger7031f622006-05-08 03:20:50 +0000466 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000467 /* yah, I know, *you* say it would never happen */
468 timeout = INT_MAX;
469 continue; /* back to main loop */
470 }
471
472 /* select() didn't timeout, something did happen. */
Denis Vlasenko739e30f2008-09-26 23:45:20 +0000473 /* Is it a packet? */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000474 if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000475 int len;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000476 /* A packet is ready, read it */
Mike Frysinger7031f622006-05-08 03:20:50 +0000477
478 if (listen_mode == LISTEN_KERNEL)
Denis Vlasenko6de89942008-05-21 07:05:06 +0000479 len = udhcp_recv_kernel_packet(&packet, sockfd);
Denis Vlasenkob76b9a42008-01-25 22:46:34 +0000480 else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000481 len = udhcp_recv_raw_packet(&packet, sockfd);
482 if (len == -1) { /* error is severe, reopen socket */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200483 bb_info_msg("Read error: %s, reopening socket", strerror(errno));
Denis Vlasenko6de89942008-05-21 07:05:06 +0000484 sleep(discover_timeout); /* 3 seconds by default */
485 change_listen_mode(listen_mode); /* just close and reopen */
486 }
Denis Vlasenkob2342912008-05-21 07:02:16 +0000487 /* If this packet will turn out to be unrelated/bogus,
488 * we will go back and wait for next one.
489 * Be sure timeout is properly decreased. */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000490 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000491 if (len < 0)
492 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000493
494 if (packet.xid != xid) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200495 log1("xid %x (our is %x), ignoring packet",
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000496 (unsigned)packet.xid, (unsigned)xid);
Mike Frysinger7031f622006-05-08 03:20:50 +0000497 continue;
498 }
499
500 /* Ignore packets that aren't for us */
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200501 if (memcmp(packet.chaddr, client_config.client_mac, 6)) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200502//FIXME: need to also check that last 10 bytes are zero
503 log1("chaddr does not match, ignoring packet"); // log2?
Mike Frysinger7031f622006-05-08 03:20:50 +0000504 continue;
505 }
506
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000507 message = get_option(&packet, DHCP_MESSAGE_TYPE);
508 if (message == NULL) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200509 bb_error_msg("no message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000510 continue;
511 }
512
513 switch (state) {
514 case INIT_SELECTING:
515 /* Must be a DHCPOFFER to one of our xid's */
516 if (*message == DHCPOFFER) {
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000517 /* TODO: why we don't just fetch server's IP from IP header? */
Denis Vlasenko239369b2006-09-07 17:05:44 +0000518 temp = get_option(&packet, DHCP_SERVER_ID);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000519 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000520 bb_error_msg("no server ID in message");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000521 continue;
522 /* still selecting - this server looks bad */
Mike Frysinger7031f622006-05-08 03:20:50 +0000523 }
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000524 /* it IS unaligned sometimes, don't "optimize" */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000525 move_from_unaligned32(server_addr, temp);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000526 xid = packet.xid;
527 requested_ip = packet.yiaddr;
528
529 /* enter requesting state */
530 state = REQUESTING;
531 timeout = 0;
532 packet_num = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000533 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000534 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000535 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000536 case RENEW_REQUESTED:
537 case REQUESTING:
538 case RENEWING:
539 case REBINDING:
540 if (*message == DHCPACK) {
Denis Vlasenko239369b2006-09-07 17:05:44 +0000541 temp = get_option(&packet, DHCP_LEASE_TIME);
542 if (!temp) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000543 bb_error_msg("no lease time with ACK, using 1 hour lease");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000544 lease_seconds = 60 * 60;
Mike Frysinger7031f622006-05-08 03:20:50 +0000545 } else {
Denis Vlasenko739e30f2008-09-26 23:45:20 +0000546 /* it IS unaligned sometimes, don't "optimize" */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000547 move_from_unaligned32(lease_seconds, temp);
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000548 lease_seconds = ntohl(lease_seconds);
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000549 lease_seconds &= 0x0fffffff; /* paranoia: must not be prone to overflows */
550 if (lease_seconds < 10) /* and not too small */
551 lease_seconds = 10;
Mike Frysinger7031f622006-05-08 03:20:50 +0000552 }
Denis Vlasenko223bc972007-11-22 00:58:49 +0000553#if ENABLE_FEATURE_UDHCPC_ARPING
554 if (opt & OPT_a) {
Denis Vlasenko739e30f2008-09-26 23:45:20 +0000555/* RFC 2131 3.1 paragraph 5:
556 * "The client receives the DHCPACK message with configuration
557 * parameters. The client SHOULD perform a final check on the
558 * parameters (e.g., ARP for allocated network address), and notes
559 * the duration of the lease specified in the DHCPACK message. At this
560 * point, the client is configured. If the client detects that the
561 * address is already in use (e.g., through the use of ARP),
562 * the client MUST send a DHCPDECLINE message to the server and restarts
563 * the configuration process..." */
Denis Vlasenko223bc972007-11-22 00:58:49 +0000564 if (!arpping(packet.yiaddr,
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200565 NULL,
566 (uint32_t) 0,
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200567 client_config.client_mac,
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200568 client_config.interface)
Denis Vlasenko223bc972007-11-22 00:58:49 +0000569 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200570 bb_info_msg("Offered address is in use "
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000571 "(got ARP reply), declining");
Denis Vlasenkoca9635b2008-01-25 19:27:08 +0000572 send_decline(xid, server_addr, packet.yiaddr);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000573
574 if (state != REQUESTING)
575 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000576 change_listen_mode(LISTEN_RAW);
Denis Vlasenko223bc972007-11-22 00:58:49 +0000577 state = INIT_SELECTING;
578 requested_ip = 0;
Denis Vlasenko68af8e72007-11-22 21:41:14 +0000579 timeout = tryagain_timeout;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000580 packet_num = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000581 already_waited_sec = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000582 continue; /* back to main loop */
Denis Vlasenko223bc972007-11-22 00:58:49 +0000583 }
584 }
585#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000586 /* enter bound state */
Denis Vlasenko19903f02008-05-21 07:03:03 +0000587 timeout = lease_seconds / 2;
Denis Vlasenko6de89942008-05-21 07:05:06 +0000588 {
589 struct in_addr temp_addr;
590 temp_addr.s_addr = packet.yiaddr;
591 bb_info_msg("Lease of %s obtained, lease time %u",
592 inet_ntoa(temp_addr), (unsigned)lease_seconds);
593 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000594 requested_ip = packet.yiaddr;
Rob Landley3f785612006-05-28 01:06:36 +0000595 udhcp_run_script(&packet,
Denis Vlasenko739e30f2008-09-26 23:45:20 +0000596 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
Mike Frysinger7031f622006-05-08 03:20:50 +0000597
598 state = BOUND;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000599 change_listen_mode(LISTEN_NONE);
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000600 if (opt & OPT_q) { /* quit after lease */
601 if (opt & OPT_R) /* release on quit */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000602 perform_release(requested_ip, server_addr);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000603 goto ret0;
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000604 }
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000605#if BB_MMU /* NOMMU case backgrounded earlier */
606 if (!(opt & OPT_f)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000607 client_background();
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000608 /* do not background again! */
609 opt = ((opt & ~OPT_b) | OPT_f);
610 }
611#endif
Denis Vlasenko19903f02008-05-21 07:03:03 +0000612 already_waited_sec = 0;
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000613 continue; /* back to main loop */
614 }
615 if (*message == DHCPNAK) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000616 /* return to init state */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000617 bb_info_msg("Received DHCP NAK");
Rob Landley3f785612006-05-28 01:06:36 +0000618 udhcp_run_script(&packet, "nak");
Mike Frysinger7031f622006-05-08 03:20:50 +0000619 if (state != REQUESTING)
Rob Landley3f785612006-05-28 01:06:36 +0000620 udhcp_run_script(NULL, "deconfig");
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000621 change_listen_mode(LISTEN_RAW);
Mike Frysinger7031f622006-05-08 03:20:50 +0000622 sleep(3); /* avoid excessive network traffic */
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000623 state = INIT_SELECTING;
624 requested_ip = 0;
625 timeout = 0;
626 packet_num = 0;
Denis Vlasenko19903f02008-05-21 07:03:03 +0000627 already_waited_sec = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000628 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000629 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000630 /* case BOUND, RELEASED: - ignore all packets */
631 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000632 continue; /* back to main loop */
633 }
634
635 /* select() didn't timeout, something did happen.
636 * But it wasn't a packet. It's a signal pipe then. */
637 {
Denis Vlasenko80edead2007-08-02 22:31:05 +0000638 int signo = udhcp_sp_read(&rfds);
639 switch (signo) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000640 case SIGUSR1:
641 perform_renew();
Denis Vlasenkob539c842007-11-29 08:17:45 +0000642 /* start things over */
643 packet_num = 0;
644 /* Kill any timeouts because the user wants this to hurry along */
645 timeout = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000646 break;
647 case SIGUSR2:
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000648 perform_release(requested_ip, server_addr);
649 timeout = INT_MAX;
Mike Frysinger7031f622006-05-08 03:20:50 +0000650 break;
651 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000652 bb_info_msg("Received SIGTERM");
Denis Vlasenko21765fa2008-06-13 20:44:05 +0000653 if (opt & OPT_R) /* release on quit */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000654 perform_release(requested_ip, server_addr);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000655 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000656 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000657 }
Denis Vlasenko9cdfd142007-11-22 01:00:00 +0000658 } /* for (;;) - main loop ends */
659
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000660 ret0:
661 retval = 0;
662 ret:
Denis Vlasenkob2342912008-05-21 07:02:16 +0000663 /*if (client_config.pidfile) - remove_pidfile has its own check */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000664 remove_pidfile(client_config.pidfile);
665 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000666}