blob: 0c85eca830c0e944bc2bed4ea33389bda985dbb5 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001/* dhcpc.c
2 *
3 * udhcp DHCP client
4 *
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
6 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Russ Dill61fb4892002-10-14 21:41:28 +00008 */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00009
Russ Dill61fb4892002-10-14 21:41:28 +000010#include <sys/time.h>
Russ Dill61fb4892002-10-14 21:41:28 +000011#include <sys/file.h>
12#include <unistd.h>
13#include <getopt.h>
14#include <stdlib.h>
15#include <sys/socket.h>
16#include <netinet/in.h>
17#include <arpa/inet.h>
18#include <signal.h>
19#include <time.h>
20#include <string.h>
21#include <sys/ioctl.h>
22#include <net/if.h>
23#include <errno.h>
24
"Vladimir N. Oleynik"b6284092005-09-23 11:25:29 +000025#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000026#include "dhcpd.h"
27#include "dhcpc.h"
28#include "options.h"
29#include "clientpacket.h"
Russ Dill4e864a32003-12-18 22:25:38 +000030#include "clientsocket.h"
Russ Dill61fb4892002-10-14 21:41:28 +000031#include "script.h"
32#include "socket.h"
Russ Dill4e864a32003-12-18 22:25:38 +000033#include "signalpipe.h"
Russ Dill61fb4892002-10-14 21:41:28 +000034
35static int state;
36static unsigned long requested_ip; /* = 0 */
37static unsigned long server_addr;
38static unsigned long timeout;
39static int packet_num; /* = 0 */
Russ Dill6393d692003-01-21 22:39:34 +000040static int fd = -1;
Russ Dill61fb4892002-10-14 21:41:28 +000041
42#define LISTEN_NONE 0
43#define LISTEN_KERNEL 1
44#define LISTEN_RAW 2
45static int listen_mode;
46
Russ Dill61fb4892002-10-14 21:41:28 +000047struct client_config_t client_config = {
48 /* Default options. */
Mike Frysinger3cc01a82005-04-16 04:17:39 +000049 .abort_if_no_lease = 0,
50 .foreground = 0,
51 .quit_after_lease = 0,
52 .background_if_no_lease = 0,
53 .interface = "eth0",
54 .pidfile = NULL,
55 .script = DEFAULT_SCRIPT,
56 .clientid = NULL,
Paul Fox28de9512005-09-22 18:59:13 +000057 .vendorclass = NULL,
Mike Frysinger3cc01a82005-04-16 04:17:39 +000058 .hostname = NULL,
59 .fqdn = NULL,
60 .ifindex = 0,
Rob Landleya2841e62006-02-20 23:26:38 +000061 .retries = 3,
Eric Andersen28a6afe2006-03-20 17:37:00 +000062 .timeout = 3,
Mike Frysinger3cc01a82005-04-16 04:17:39 +000063 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
Russ Dill61fb4892002-10-14 21:41:28 +000064};
65
Russ Dill4b77aca2003-12-16 02:28:20 +000066#ifndef IN_BUSYBOX
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000067static void ATTRIBUTE_NORETURN show_usage(void)
Russ Dill4b77aca2003-12-16 02:28:20 +000068{
Russ Dill4e864a32003-12-18 22:25:38 +000069 printf(
Russ Dill4b77aca2003-12-16 02:28:20 +000070"Usage: udhcpc [OPTIONS]\n\n"
Paul Fox28de9512005-09-22 18:59:13 +000071" -c, --clientid=CLIENTID Set client identifier - type is first char\n"
Paul Foxa39bba32005-08-01 14:31:13 +000072" -C, --clientid-none Suppress default client identifier\n"
Paul Fox28de9512005-09-22 18:59:13 +000073" -V, --vendorclass=CLASSID Set vendor class identifier\n"
Russ Dill4b77aca2003-12-16 02:28:20 +000074" -H, --hostname=HOSTNAME Client hostname\n"
75" -h Alias for -H\n"
Mike Frysingerd8248532004-12-06 14:59:45 +000076" -F, --fqdn=FQDN Client fully qualified domain name\n"
Russ Dill4b77aca2003-12-16 02:28:20 +000077" -f, --foreground Do not fork after getting lease\n"
78" -b, --background Fork to background if lease cannot be\n"
79" immediately negotiated.\n"
80" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
81" -n, --now Exit with failure if lease cannot be\n"
82" immediately negotiated.\n"
83" -p, --pidfile=file Store process ID of daemon in file\n"
84" -q, --quit Quit after obtaining lease\n"
85" -r, --request=IP IP address to request (default: none)\n"
86" -s, --script=file Run file at dhcp events (default:\n"
87" " DEFAULT_SCRIPT ")\n"
Eric Andersen28a6afe2006-03-20 17:37:00 +000088" -T, --timeout=seconds Try to get the lease for the amount of\n"
89" seconds (default: 3)\n"
Russ Dill4b77aca2003-12-16 02:28:20 +000090" -v, --version Display version\n"
Russ Dill4e864a32003-12-18 22:25:38 +000091 );
92 exit(0);
Russ Dill4b77aca2003-12-16 02:28:20 +000093}
94#else
95#define show_usage bb_show_usage
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000096extern void show_usage(void) ATTRIBUTE_NORETURN;
Russ Dill4b77aca2003-12-16 02:28:20 +000097#endif
98
99
Russ Dill61fb4892002-10-14 21:41:28 +0000100/* just a little helper */
101static void change_mode(int new_mode)
102{
103 DEBUG(LOG_INFO, "entering %s listen mode",
104 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Russ Dill6393d692003-01-21 22:39:34 +0000105 if (fd >= 0) close(fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000106 fd = -1;
107 listen_mode = new_mode;
108}
109
110
111/* perform a renew */
112static void perform_renew(void)
113{
114 LOG(LOG_INFO, "Performing a DHCP renew");
115 switch (state) {
Russ Dill61fb4892002-10-14 21:41:28 +0000116 case BOUND:
Russ Dill61fb4892002-10-14 21:41:28 +0000117 change_mode(LISTEN_KERNEL);
Russ Dillf5ecd432002-10-31 19:21:27 +0000118 case RENEWING:
119 case REBINDING:
Russ Dill61fb4892002-10-14 21:41:28 +0000120 state = RENEW_REQUESTED;
121 break;
Russ Dillf5ecd432002-10-31 19:21:27 +0000122 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
123 run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000124 case REQUESTING:
125 case RELEASED:
126 change_mode(LISTEN_RAW);
127 state = INIT_SELECTING;
128 break;
129 case INIT_SELECTING:
Eric Andersen1b2e7c32003-06-20 09:20:28 +0000130 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000131 }
132
133 /* start things over */
134 packet_num = 0;
135
136 /* Kill any timeouts because the user wants this to hurry along */
137 timeout = 0;
138}
139
140
141/* perform a release */
142static void perform_release(void)
143{
144 char buffer[16];
145 struct in_addr temp_addr;
146
147 /* send release packet */
148 if (state == BOUND || state == RENEWING || state == REBINDING) {
149 temp_addr.s_addr = server_addr;
150 sprintf(buffer, "%s", inet_ntoa(temp_addr));
151 temp_addr.s_addr = requested_ip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000152 LOG(LOG_INFO, "Unicasting a release of %s to %s",
Russ Dill61fb4892002-10-14 21:41:28 +0000153 inet_ntoa(temp_addr), buffer);
154 send_release(server_addr, requested_ip); /* unicast */
155 run_script(NULL, "deconfig");
156 }
157 LOG(LOG_INFO, "Entering released state");
158
159 change_mode(LISTEN_NONE);
160 state = RELEASED;
161 timeout = 0x7fffffff;
162}
163
164
Glenn L McGrath24833432003-06-10 17:22:49 +0000165static void client_background(void)
Russ Dill61fb4892002-10-14 21:41:28 +0000166{
Glenn L McGrath24833432003-06-10 17:22:49 +0000167 background(client_config.pidfile);
Russ Dill61fb4892002-10-14 21:41:28 +0000168 client_config.foreground = 1; /* Do not fork again. */
Russ Dill6393d692003-01-21 22:39:34 +0000169 client_config.background_if_no_lease = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000170}
171
172
Russ Dill4e864a32003-12-18 22:25:38 +0000173#ifdef COMBINED_BINARY
Russ Dill61fb4892002-10-14 21:41:28 +0000174int udhcpc_main(int argc, char *argv[])
Russ Dill4e864a32003-12-18 22:25:38 +0000175#else
176int main(int argc, char *argv[])
177#endif
Russ Dill61fb4892002-10-14 21:41:28 +0000178{
Eric Andersenad953732004-01-30 23:45:53 +0000179 uint8_t *temp, *message;
Russ Dill61fb4892002-10-14 21:41:28 +0000180 unsigned long t1 = 0, t2 = 0, xid = 0;
181 unsigned long start = 0, lease;
182 fd_set rfds;
183 int retval;
184 struct timeval tv;
185 int c, len;
186 struct dhcpMessage packet;
187 struct in_addr temp_addr;
Russ Dill309c7b72004-05-19 08:29:05 +0000188 long now;
Russ Dill61fb4892002-10-14 21:41:28 +0000189 int max_fd;
190 int sig;
Paul Foxa39bba32005-08-01 14:31:13 +0000191 int no_clientid = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000192
Glenn L McGrath24833432003-06-10 17:22:49 +0000193 static const struct option arg_options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +0000194 {"clientid", required_argument, 0, 'c'},
Paul Foxa39bba32005-08-01 14:31:13 +0000195 {"clientid-none", no_argument, 0, 'C'},
Paul Fox28de9512005-09-22 18:59:13 +0000196 {"vendorclass", required_argument, 0, 'V'},
Russ Dill61fb4892002-10-14 21:41:28 +0000197 {"foreground", no_argument, 0, 'f'},
198 {"background", no_argument, 0, 'b'},
199 {"hostname", required_argument, 0, 'H'},
Mike Frysingerd8248532004-12-06 14:59:45 +0000200 {"hostname", required_argument, 0, 'h'},
201 {"fqdn", required_argument, 0, 'F'},
Russ Dill61fb4892002-10-14 21:41:28 +0000202 {"interface", required_argument, 0, 'i'},
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000203 {"now", no_argument, 0, 'n'},
Russ Dill61fb4892002-10-14 21:41:28 +0000204 {"pidfile", required_argument, 0, 'p'},
205 {"quit", no_argument, 0, 'q'},
206 {"request", required_argument, 0, 'r'},
207 {"script", required_argument, 0, 's'},
Eric Andersen28a6afe2006-03-20 17:37:00 +0000208 {"timeout", required_argument, 0, 'T'},
Russ Dill61fb4892002-10-14 21:41:28 +0000209 {"version", no_argument, 0, 'v'},
Rob Landleya2841e62006-02-20 23:26:38 +0000210 {"retries", required_argument, 0, 't'},
Russ Dill61fb4892002-10-14 21:41:28 +0000211 {0, 0, 0, 0}
212 };
213
214 /* get options */
215 while (1) {
216 int option_index = 0;
Eric Andersen28a6afe2006-03-20 17:37:00 +0000217 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:T:t:v", arg_options, &option_index);
Russ Dill61fb4892002-10-14 21:41:28 +0000218 if (c == -1) break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000219
Russ Dill61fb4892002-10-14 21:41:28 +0000220 switch (c) {
221 case 'c':
Paul Foxa39bba32005-08-01 14:31:13 +0000222 if (no_clientid) show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +0000223 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Rob Landleye7c43b62006-03-01 16:39:45 +0000224 free(client_config.clientid);
Russ Dill61fb4892002-10-14 21:41:28 +0000225 client_config.clientid = xmalloc(len + 2);
226 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
227 client_config.clientid[OPT_LEN] = len;
228 client_config.clientid[OPT_DATA] = '\0';
Eric Andersena68ea1c2006-01-30 22:48:39 +0000229 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
Russ Dill61fb4892002-10-14 21:41:28 +0000230 break;
Paul Foxa39bba32005-08-01 14:31:13 +0000231 case 'C':
232 if (client_config.clientid) show_usage();
233 no_clientid = 1;
234 break;
Paul Fox28de9512005-09-22 18:59:13 +0000235 case 'V':
236 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Rob Landleye7c43b62006-03-01 16:39:45 +0000237 free(client_config.vendorclass);
Paul Fox28de9512005-09-22 18:59:13 +0000238 client_config.vendorclass = xmalloc(len + 2);
239 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
240 client_config.vendorclass[OPT_LEN] = len;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000241 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
Paul Fox28de9512005-09-22 18:59:13 +0000242 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000243 case 'f':
244 client_config.foreground = 1;
245 break;
246 case 'b':
247 client_config.background_if_no_lease = 1;
248 break;
249 case 'h':
250 case 'H':
251 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Rob Landleye7c43b62006-03-01 16:39:45 +0000252 free(client_config.hostname);
Russ Dill61fb4892002-10-14 21:41:28 +0000253 client_config.hostname = xmalloc(len + 2);
254 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
255 client_config.hostname[OPT_LEN] = len;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000256 strncpy((char*)client_config.hostname + 2, optarg, len);
Russ Dill61fb4892002-10-14 21:41:28 +0000257 break;
Mike Frysingerd8248532004-12-06 14:59:45 +0000258 case 'F':
259 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Rob Landleye7c43b62006-03-01 16:39:45 +0000260 free(client_config.fqdn);
Mike Frysingerd8248532004-12-06 14:59:45 +0000261 client_config.fqdn = xmalloc(len + 5);
262 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
263 client_config.fqdn[OPT_LEN] = len + 3;
264 /* Flags: 0000NEOS
265 S: 1 => Client requests Server to update A RR in DNS as well as PTR
266 O: 1 => Server indicates to client that DNS has been updated regardless
267 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
268 N: 1 => Client requests Server to not update DNS
269 */
270 client_config.fqdn[OPT_LEN + 1] = 0x1;
271 client_config.fqdn[OPT_LEN + 2] = 0;
272 client_config.fqdn[OPT_LEN + 3] = 0;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000273 strncpy((char*)client_config.fqdn + 5, optarg, len);
Mike Frysingerd8248532004-12-06 14:59:45 +0000274 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000275 case 'i':
276 client_config.interface = optarg;
277 break;
278 case 'n':
279 client_config.abort_if_no_lease = 1;
280 break;
281 case 'p':
282 client_config.pidfile = optarg;
283 break;
284 case 'q':
285 client_config.quit_after_lease = 1;
286 break;
287 case 'r':
288 requested_ip = inet_addr(optarg);
289 break;
290 case 's':
291 client_config.script = optarg;
292 break;
Eric Andersen28a6afe2006-03-20 17:37:00 +0000293 case 'T':
294 client_config.timeout = atoi(optarg);
295 break;
Rob Landleya2841e62006-02-20 23:26:38 +0000296 case 't':
297 client_config.retries = atoi(optarg);
298 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000299 case 'v':
Russ Dill4e864a32003-12-18 22:25:38 +0000300 printf("udhcpcd, version %s\n\n", VERSION);
301 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000302 break;
303 default:
Russ Dill4b77aca2003-12-16 02:28:20 +0000304 show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +0000305 }
306 }
307
Russ Dill4e864a32003-12-18 22:25:38 +0000308 /* Start the log, sanitize fd's, and write a pid file */
309 start_log_and_pid("udhcpc", client_config.pidfile);
310
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000311 if (read_interface(client_config.interface, &client_config.ifindex,
Russ Dill61fb4892002-10-14 21:41:28 +0000312 NULL, client_config.arp) < 0)
Russ Dill4e864a32003-12-18 22:25:38 +0000313 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000314
Paul Foxa39bba32005-08-01 14:31:13 +0000315 /* if not set, and not suppressed, setup the default client ID */
316 if (!client_config.clientid && !no_clientid) {
Russ Dill61fb4892002-10-14 21:41:28 +0000317 client_config.clientid = xmalloc(6 + 3);
318 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
319 client_config.clientid[OPT_LEN] = 7;
320 client_config.clientid[OPT_DATA] = 1;
321 memcpy(client_config.clientid + 3, client_config.arp, 6);
322 }
323
Paul Fox28de9512005-09-22 18:59:13 +0000324 if (!client_config.vendorclass) {
325 client_config.vendorclass = xmalloc(sizeof("udhcp "VERSION) + 2);
326 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
327 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "VERSION) - 1;
328 client_config.vendorclass[OPT_DATA] = 1;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000329 memcpy(&client_config.vendorclass[OPT_DATA],
Paul Fox28de9512005-09-22 18:59:13 +0000330 "udhcp "VERSION, sizeof("udhcp "VERSION) - 1);
331 }
332
333
Russ Dill4e864a32003-12-18 22:25:38 +0000334 /* setup the signal pipe */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000335 udhcp_sp_setup();
336
Russ Dill61fb4892002-10-14 21:41:28 +0000337 state = INIT_SELECTING;
338 run_script(NULL, "deconfig");
339 change_mode(LISTEN_RAW);
340
341 for (;;) {
342
Rob Landley918f2ab2005-05-04 02:15:23 +0000343 tv.tv_sec = timeout - uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000344 tv.tv_usec = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000345
346 if (listen_mode != LISTEN_NONE && fd < 0) {
347 if (listen_mode == LISTEN_KERNEL)
348 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
349 else
350 fd = raw_socket(client_config.ifindex);
351 if (fd < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000352 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
Russ Dill4e864a32003-12-18 22:25:38 +0000353 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000354 }
355 }
Russ Dill4e864a32003-12-18 22:25:38 +0000356 max_fd = udhcp_sp_fd_set(&rfds, fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000357
358 if (tv.tv_sec > 0) {
Glenn L McGrath8ce8f9b2003-08-29 15:19:44 +0000359 DEBUG(LOG_INFO, "Waiting on select...");
Russ Dill61fb4892002-10-14 21:41:28 +0000360 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
361 } else retval = 0; /* If we already timed out, fall through */
362
Rob Landley918f2ab2005-05-04 02:15:23 +0000363 now = uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000364 if (retval == 0) {
365 /* timeout dropped to zero */
366 switch (state) {
367 case INIT_SELECTING:
Rob Landleya2841e62006-02-20 23:26:38 +0000368 if (packet_num < client_config.retries) {
Russ Dill61fb4892002-10-14 21:41:28 +0000369 if (packet_num == 0)
370 xid = random_xid();
371
372 /* send discover packet */
373 send_discover(xid, requested_ip); /* broadcast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000374
Eric Andersen28a6afe2006-03-20 17:37:00 +0000375 timeout = now + client_config.timeout;
Russ Dill61fb4892002-10-14 21:41:28 +0000376 packet_num++;
377 } else {
Glenn L McGrathd9461f82003-09-01 04:08:36 +0000378 run_script(NULL, "leasefail");
Russ Dill61fb4892002-10-14 21:41:28 +0000379 if (client_config.background_if_no_lease) {
380 LOG(LOG_INFO, "No lease, forking to background.");
Glenn L McGrath24833432003-06-10 17:22:49 +0000381 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000382 } else if (client_config.abort_if_no_lease) {
383 LOG(LOG_INFO, "No lease, failing.");
Russ Dill4e864a32003-12-18 22:25:38 +0000384 return 1;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000385 }
Russ Dill61fb4892002-10-14 21:41:28 +0000386 /* wait to try again */
387 packet_num = 0;
388 timeout = now + 60;
389 }
390 break;
391 case RENEW_REQUESTED:
392 case REQUESTING:
Rob Landleya2841e62006-02-20 23:26:38 +0000393 if (packet_num < client_config.retries) {
Russ Dill61fb4892002-10-14 21:41:28 +0000394 /* send request packet */
395 if (state == RENEW_REQUESTED)
396 send_renew(xid, server_addr, requested_ip); /* unicast */
397 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000398
Russ Dill61fb4892002-10-14 21:41:28 +0000399 timeout = now + ((packet_num == 2) ? 10 : 2);
400 packet_num++;
401 } else {
402 /* timed out, go back to init state */
Russ Dillf5ecd432002-10-31 19:21:27 +0000403 if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000404 state = INIT_SELECTING;
405 timeout = now;
406 packet_num = 0;
407 change_mode(LISTEN_RAW);
408 }
409 break;
410 case BOUND:
411 /* Lease is starting to run out, time to enter renewing state */
412 state = RENEWING;
413 change_mode(LISTEN_KERNEL);
414 DEBUG(LOG_INFO, "Entering renew state");
415 /* fall right through */
416 case RENEWING:
417 /* Either set a new T1, or enter REBINDING state */
418 if ((t2 - t1) <= (lease / 14400 + 1)) {
419 /* timed out, enter rebinding state */
420 state = REBINDING;
421 timeout = now + (t2 - t1);
422 DEBUG(LOG_INFO, "Entering rebinding state");
423 } else {
424 /* send a request packet */
425 send_renew(xid, server_addr, requested_ip); /* unicast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000426
Russ Dill61fb4892002-10-14 21:41:28 +0000427 t1 = (t2 - t1) / 2 + t1;
428 timeout = t1 + start;
429 }
430 break;
431 case REBINDING:
432 /* Either set a new T2, or enter INIT state */
433 if ((lease - t2) <= (lease / 14400 + 1)) {
434 /* timed out, enter init state */
435 state = INIT_SELECTING;
436 LOG(LOG_INFO, "Lease lost, entering init state");
437 run_script(NULL, "deconfig");
438 timeout = now;
439 packet_num = 0;
440 change_mode(LISTEN_RAW);
441 } else {
442 /* send a request packet */
443 send_renew(xid, 0, requested_ip); /* broadcast */
444
445 t2 = (lease - t2) / 2 + t2;
446 timeout = t2 + start;
447 }
448 break;
449 case RELEASED:
450 /* yah, I know, *you* say it would never happen */
451 timeout = 0x7fffffff;
452 break;
453 }
454 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
455 /* a packet is ready, read it */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000456
Russ Dill61fb4892002-10-14 21:41:28 +0000457 if (listen_mode == LISTEN_KERNEL)
458 len = get_packet(&packet, fd);
459 else len = get_raw_packet(&packet, fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000460
Russ Dill61fb4892002-10-14 21:41:28 +0000461 if (len == -1 && errno != EINTR) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000462 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
Russ Dill61fb4892002-10-14 21:41:28 +0000463 change_mode(listen_mode); /* just close and reopen */
464 }
465 if (len < 0) continue;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000466
Russ Dill61fb4892002-10-14 21:41:28 +0000467 if (packet.xid != xid) {
468 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
469 (unsigned long) packet.xid, xid);
470 continue;
471 }
Rob Landley0b1ff5a2005-05-26 05:25:12 +0000472 /* Ignore packets that aren't for us */
Paul Fox01f67982005-07-20 19:13:21 +0000473 if (memcmp(packet.chaddr, client_config.arp, 6)) {
474 DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
475 continue;
476 }
477
Russ Dill61fb4892002-10-14 21:41:28 +0000478 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
479 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
480 continue;
481 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000482
Russ Dill61fb4892002-10-14 21:41:28 +0000483 switch (state) {
484 case INIT_SELECTING:
485 /* Must be a DHCPOFFER to one of our xid's */
486 if (*message == DHCPOFFER) {
487 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
488 memcpy(&server_addr, temp, 4);
489 xid = packet.xid;
490 requested_ip = packet.yiaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000491
Russ Dill61fb4892002-10-14 21:41:28 +0000492 /* enter requesting state */
493 state = REQUESTING;
494 timeout = now;
495 packet_num = 0;
496 } else {
497 DEBUG(LOG_ERR, "No server ID in message");
498 }
499 }
500 break;
501 case RENEW_REQUESTED:
502 case REQUESTING:
503 case RENEWING:
504 case REBINDING:
505 if (*message == DHCPACK) {
506 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
507 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
508 lease = 60 * 60;
509 } else {
510 memcpy(&lease, temp, 4);
511 lease = ntohl(lease);
512 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000513
Russ Dill61fb4892002-10-14 21:41:28 +0000514 /* enter bound state */
515 t1 = lease / 2;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000516
Russ Dill61fb4892002-10-14 21:41:28 +0000517 /* little fixed point for n * .875 */
518 t2 = (lease * 0x7) >> 3;
519 temp_addr.s_addr = packet.yiaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000520 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
Russ Dill61fb4892002-10-14 21:41:28 +0000521 inet_ntoa(temp_addr), lease);
522 start = now;
523 timeout = t1 + start;
524 requested_ip = packet.yiaddr;
525 run_script(&packet,
526 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
527
528 state = BOUND;
529 change_mode(LISTEN_NONE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000530 if (client_config.quit_after_lease)
Russ Dill4e864a32003-12-18 22:25:38 +0000531 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000532 if (!client_config.foreground)
Glenn L McGrath24833432003-06-10 17:22:49 +0000533 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000534
535 } else if (*message == DHCPNAK) {
536 /* return to init state */
537 LOG(LOG_INFO, "Received DHCP NAK");
538 run_script(&packet, "nak");
539 if (state != REQUESTING)
540 run_script(NULL, "deconfig");
541 state = INIT_SELECTING;
542 timeout = now;
543 requested_ip = 0;
544 packet_num = 0;
545 change_mode(LISTEN_RAW);
546 sleep(3); /* avoid excessive network traffic */
547 }
548 break;
549 /* case BOUND, RELEASED: - ignore all packets */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000550 }
Russ Dill4e864a32003-12-18 22:25:38 +0000551 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
Russ Dill61fb4892002-10-14 21:41:28 +0000552 switch (sig) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000553 case SIGUSR1:
Russ Dill61fb4892002-10-14 21:41:28 +0000554 perform_renew();
555 break;
556 case SIGUSR2:
557 perform_release();
558 break;
559 case SIGTERM:
560 LOG(LOG_INFO, "Received SIGTERM");
Russ Dill4e864a32003-12-18 22:25:38 +0000561 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000562 }
563 } else if (retval == -1 && errno == EINTR) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000564 /* a signal was caught */
Russ Dill61fb4892002-10-14 21:41:28 +0000565 } else {
566 /* An error occured */
567 DEBUG(LOG_ERR, "Error on select");
568 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000569
Russ Dill61fb4892002-10-14 21:41:28 +0000570 }
571 return 0;
572}