blob: 8568ac1e75aac39d563186577bbe3c2e9784aa59 [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,
61 .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
Russ Dill61fb4892002-10-14 21:41:28 +000062};
63
Russ Dill4b77aca2003-12-16 02:28:20 +000064#ifndef IN_BUSYBOX
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000065static void ATTRIBUTE_NORETURN show_usage(void)
Russ Dill4b77aca2003-12-16 02:28:20 +000066{
Russ Dill4e864a32003-12-18 22:25:38 +000067 printf(
Russ Dill4b77aca2003-12-16 02:28:20 +000068"Usage: udhcpc [OPTIONS]\n\n"
Paul Fox28de9512005-09-22 18:59:13 +000069" -c, --clientid=CLIENTID Set client identifier - type is first char\n"
Paul Foxa39bba32005-08-01 14:31:13 +000070" -C, --clientid-none Suppress default client identifier\n"
Paul Fox28de9512005-09-22 18:59:13 +000071" -V, --vendorclass=CLASSID Set vendor class identifier\n"
Russ Dill4b77aca2003-12-16 02:28:20 +000072" -H, --hostname=HOSTNAME Client hostname\n"
73" -h Alias for -H\n"
Mike Frysingerd8248532004-12-06 14:59:45 +000074" -F, --fqdn=FQDN Client fully qualified domain name\n"
Russ Dill4b77aca2003-12-16 02:28:20 +000075" -f, --foreground Do not fork after getting lease\n"
76" -b, --background Fork to background if lease cannot be\n"
77" immediately negotiated.\n"
78" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
79" -n, --now Exit with failure if lease cannot be\n"
80" immediately negotiated.\n"
81" -p, --pidfile=file Store process ID of daemon in file\n"
82" -q, --quit Quit after obtaining lease\n"
83" -r, --request=IP IP address to request (default: none)\n"
84" -s, --script=file Run file at dhcp events (default:\n"
85" " DEFAULT_SCRIPT ")\n"
86" -v, --version Display version\n"
Russ Dill4e864a32003-12-18 22:25:38 +000087 );
88 exit(0);
Russ Dill4b77aca2003-12-16 02:28:20 +000089}
90#else
91#define show_usage bb_show_usage
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000092extern void show_usage(void) ATTRIBUTE_NORETURN;
Russ Dill4b77aca2003-12-16 02:28:20 +000093#endif
94
95
Russ Dill61fb4892002-10-14 21:41:28 +000096/* just a little helper */
97static void change_mode(int new_mode)
98{
99 DEBUG(LOG_INFO, "entering %s listen mode",
100 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Russ Dill6393d692003-01-21 22:39:34 +0000101 if (fd >= 0) close(fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000102 fd = -1;
103 listen_mode = new_mode;
104}
105
106
107/* perform a renew */
108static void perform_renew(void)
109{
110 LOG(LOG_INFO, "Performing a DHCP renew");
111 switch (state) {
Russ Dill61fb4892002-10-14 21:41:28 +0000112 case BOUND:
Russ Dill61fb4892002-10-14 21:41:28 +0000113 change_mode(LISTEN_KERNEL);
Russ Dillf5ecd432002-10-31 19:21:27 +0000114 case RENEWING:
115 case REBINDING:
Russ Dill61fb4892002-10-14 21:41:28 +0000116 state = RENEW_REQUESTED;
117 break;
Russ Dillf5ecd432002-10-31 19:21:27 +0000118 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
119 run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000120 case REQUESTING:
121 case RELEASED:
122 change_mode(LISTEN_RAW);
123 state = INIT_SELECTING;
124 break;
125 case INIT_SELECTING:
Eric Andersen1b2e7c32003-06-20 09:20:28 +0000126 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000127 }
128
129 /* start things over */
130 packet_num = 0;
131
132 /* Kill any timeouts because the user wants this to hurry along */
133 timeout = 0;
134}
135
136
137/* perform a release */
138static void perform_release(void)
139{
140 char buffer[16];
141 struct in_addr temp_addr;
142
143 /* send release packet */
144 if (state == BOUND || state == RENEWING || state == REBINDING) {
145 temp_addr.s_addr = server_addr;
146 sprintf(buffer, "%s", inet_ntoa(temp_addr));
147 temp_addr.s_addr = requested_ip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000148 LOG(LOG_INFO, "Unicasting a release of %s to %s",
Russ Dill61fb4892002-10-14 21:41:28 +0000149 inet_ntoa(temp_addr), buffer);
150 send_release(server_addr, requested_ip); /* unicast */
151 run_script(NULL, "deconfig");
152 }
153 LOG(LOG_INFO, "Entering released state");
154
155 change_mode(LISTEN_NONE);
156 state = RELEASED;
157 timeout = 0x7fffffff;
158}
159
160
Glenn L McGrath24833432003-06-10 17:22:49 +0000161static void client_background(void)
Russ Dill61fb4892002-10-14 21:41:28 +0000162{
Glenn L McGrath24833432003-06-10 17:22:49 +0000163 background(client_config.pidfile);
Russ Dill61fb4892002-10-14 21:41:28 +0000164 client_config.foreground = 1; /* Do not fork again. */
Russ Dill6393d692003-01-21 22:39:34 +0000165 client_config.background_if_no_lease = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000166}
167
168
Russ Dill4e864a32003-12-18 22:25:38 +0000169#ifdef COMBINED_BINARY
Russ Dill61fb4892002-10-14 21:41:28 +0000170int udhcpc_main(int argc, char *argv[])
Russ Dill4e864a32003-12-18 22:25:38 +0000171#else
172int main(int argc, char *argv[])
173#endif
Russ Dill61fb4892002-10-14 21:41:28 +0000174{
Eric Andersenad953732004-01-30 23:45:53 +0000175 uint8_t *temp, *message;
Russ Dill61fb4892002-10-14 21:41:28 +0000176 unsigned long t1 = 0, t2 = 0, xid = 0;
177 unsigned long start = 0, lease;
178 fd_set rfds;
179 int retval;
180 struct timeval tv;
181 int c, len;
182 struct dhcpMessage packet;
183 struct in_addr temp_addr;
Russ Dill309c7b72004-05-19 08:29:05 +0000184 long now;
Russ Dill61fb4892002-10-14 21:41:28 +0000185 int max_fd;
186 int sig;
Paul Foxa39bba32005-08-01 14:31:13 +0000187 int no_clientid = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000188
Glenn L McGrath24833432003-06-10 17:22:49 +0000189 static const struct option arg_options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +0000190 {"clientid", required_argument, 0, 'c'},
Paul Foxa39bba32005-08-01 14:31:13 +0000191 {"clientid-none", no_argument, 0, 'C'},
Paul Fox28de9512005-09-22 18:59:13 +0000192 {"vendorclass", required_argument, 0, 'V'},
Russ Dill61fb4892002-10-14 21:41:28 +0000193 {"foreground", no_argument, 0, 'f'},
194 {"background", no_argument, 0, 'b'},
195 {"hostname", required_argument, 0, 'H'},
Mike Frysingerd8248532004-12-06 14:59:45 +0000196 {"hostname", required_argument, 0, 'h'},
197 {"fqdn", required_argument, 0, 'F'},
Russ Dill61fb4892002-10-14 21:41:28 +0000198 {"interface", required_argument, 0, 'i'},
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000199 {"now", no_argument, 0, 'n'},
Russ Dill61fb4892002-10-14 21:41:28 +0000200 {"pidfile", required_argument, 0, 'p'},
201 {"quit", no_argument, 0, 'q'},
202 {"request", required_argument, 0, 'r'},
203 {"script", required_argument, 0, 's'},
204 {"version", no_argument, 0, 'v'},
Russ Dill61fb4892002-10-14 21:41:28 +0000205 {0, 0, 0, 0}
206 };
207
208 /* get options */
209 while (1) {
210 int option_index = 0;
Paul Fox28de9512005-09-22 18:59:13 +0000211 c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:v", arg_options, &option_index);
Russ Dill61fb4892002-10-14 21:41:28 +0000212 if (c == -1) break;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000213
Russ Dill61fb4892002-10-14 21:41:28 +0000214 switch (c) {
215 case 'c':
Paul Foxa39bba32005-08-01 14:31:13 +0000216 if (no_clientid) show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +0000217 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Russ Dill1eb7a172002-12-11 21:12:45 +0000218 if (client_config.clientid) free(client_config.clientid);
Russ Dill61fb4892002-10-14 21:41:28 +0000219 client_config.clientid = xmalloc(len + 2);
220 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
221 client_config.clientid[OPT_LEN] = len;
222 client_config.clientid[OPT_DATA] = '\0';
Eric Andersena68ea1c2006-01-30 22:48:39 +0000223 strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
Russ Dill61fb4892002-10-14 21:41:28 +0000224 break;
Paul Foxa39bba32005-08-01 14:31:13 +0000225 case 'C':
226 if (client_config.clientid) show_usage();
227 no_clientid = 1;
228 break;
Paul Fox28de9512005-09-22 18:59:13 +0000229 case 'V':
230 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
231 if (client_config.vendorclass) free(client_config.vendorclass);
232 client_config.vendorclass = xmalloc(len + 2);
233 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
234 client_config.vendorclass[OPT_LEN] = len;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000235 strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
Paul Fox28de9512005-09-22 18:59:13 +0000236 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000237 case 'f':
238 client_config.foreground = 1;
239 break;
240 case 'b':
241 client_config.background_if_no_lease = 1;
242 break;
243 case 'h':
244 case 'H':
245 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Russ Dill1eb7a172002-12-11 21:12:45 +0000246 if (client_config.hostname) free(client_config.hostname);
Russ Dill61fb4892002-10-14 21:41:28 +0000247 client_config.hostname = xmalloc(len + 2);
248 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
249 client_config.hostname[OPT_LEN] = len;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000250 strncpy((char*)client_config.hostname + 2, optarg, len);
Russ Dill61fb4892002-10-14 21:41:28 +0000251 break;
Mike Frysingerd8248532004-12-06 14:59:45 +0000252 case 'F':
253 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
254 if (client_config.fqdn) free(client_config.fqdn);
255 client_config.fqdn = xmalloc(len + 5);
256 client_config.fqdn[OPT_CODE] = DHCP_FQDN;
257 client_config.fqdn[OPT_LEN] = len + 3;
258 /* Flags: 0000NEOS
259 S: 1 => Client requests Server to update A RR in DNS as well as PTR
260 O: 1 => Server indicates to client that DNS has been updated regardless
261 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
262 N: 1 => Client requests Server to not update DNS
263 */
264 client_config.fqdn[OPT_LEN + 1] = 0x1;
265 client_config.fqdn[OPT_LEN + 2] = 0;
266 client_config.fqdn[OPT_LEN + 3] = 0;
Eric Andersena68ea1c2006-01-30 22:48:39 +0000267 strncpy((char*)client_config.fqdn + 5, optarg, len);
Mike Frysingerd8248532004-12-06 14:59:45 +0000268 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000269 case 'i':
270 client_config.interface = optarg;
271 break;
272 case 'n':
273 client_config.abort_if_no_lease = 1;
274 break;
275 case 'p':
276 client_config.pidfile = optarg;
277 break;
278 case 'q':
279 client_config.quit_after_lease = 1;
280 break;
281 case 'r':
282 requested_ip = inet_addr(optarg);
283 break;
284 case 's':
285 client_config.script = optarg;
286 break;
287 case 'v':
Russ Dill4e864a32003-12-18 22:25:38 +0000288 printf("udhcpcd, version %s\n\n", VERSION);
289 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000290 break;
291 default:
Russ Dill4b77aca2003-12-16 02:28:20 +0000292 show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +0000293 }
294 }
295
Russ Dill4e864a32003-12-18 22:25:38 +0000296 /* Start the log, sanitize fd's, and write a pid file */
297 start_log_and_pid("udhcpc", client_config.pidfile);
298
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000299 if (read_interface(client_config.interface, &client_config.ifindex,
Russ Dill61fb4892002-10-14 21:41:28 +0000300 NULL, client_config.arp) < 0)
Russ Dill4e864a32003-12-18 22:25:38 +0000301 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000302
Paul Foxa39bba32005-08-01 14:31:13 +0000303 /* if not set, and not suppressed, setup the default client ID */
304 if (!client_config.clientid && !no_clientid) {
Russ Dill61fb4892002-10-14 21:41:28 +0000305 client_config.clientid = xmalloc(6 + 3);
306 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
307 client_config.clientid[OPT_LEN] = 7;
308 client_config.clientid[OPT_DATA] = 1;
309 memcpy(client_config.clientid + 3, client_config.arp, 6);
310 }
311
Paul Fox28de9512005-09-22 18:59:13 +0000312 if (!client_config.vendorclass) {
313 client_config.vendorclass = xmalloc(sizeof("udhcp "VERSION) + 2);
314 client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
315 client_config.vendorclass[OPT_LEN] = sizeof("udhcp "VERSION) - 1;
316 client_config.vendorclass[OPT_DATA] = 1;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000317 memcpy(&client_config.vendorclass[OPT_DATA],
Paul Fox28de9512005-09-22 18:59:13 +0000318 "udhcp "VERSION, sizeof("udhcp "VERSION) - 1);
319 }
320
321
Russ Dill4e864a32003-12-18 22:25:38 +0000322 /* setup the signal pipe */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000323 udhcp_sp_setup();
324
Russ Dill61fb4892002-10-14 21:41:28 +0000325 state = INIT_SELECTING;
326 run_script(NULL, "deconfig");
327 change_mode(LISTEN_RAW);
328
329 for (;;) {
330
Rob Landley918f2ab2005-05-04 02:15:23 +0000331 tv.tv_sec = timeout - uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000332 tv.tv_usec = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000333
334 if (listen_mode != LISTEN_NONE && fd < 0) {
335 if (listen_mode == LISTEN_KERNEL)
336 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
337 else
338 fd = raw_socket(client_config.ifindex);
339 if (fd < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000340 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
Russ Dill4e864a32003-12-18 22:25:38 +0000341 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000342 }
343 }
Russ Dill4e864a32003-12-18 22:25:38 +0000344 max_fd = udhcp_sp_fd_set(&rfds, fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000345
346 if (tv.tv_sec > 0) {
Glenn L McGrath8ce8f9b2003-08-29 15:19:44 +0000347 DEBUG(LOG_INFO, "Waiting on select...");
Russ Dill61fb4892002-10-14 21:41:28 +0000348 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
349 } else retval = 0; /* If we already timed out, fall through */
350
Rob Landley918f2ab2005-05-04 02:15:23 +0000351 now = uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000352 if (retval == 0) {
353 /* timeout dropped to zero */
354 switch (state) {
355 case INIT_SELECTING:
356 if (packet_num < 3) {
357 if (packet_num == 0)
358 xid = random_xid();
359
360 /* send discover packet */
361 send_discover(xid, requested_ip); /* broadcast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000362
Russ Dill61fb4892002-10-14 21:41:28 +0000363 timeout = now + ((packet_num == 2) ? 4 : 2);
364 packet_num++;
365 } else {
Glenn L McGrathd9461f82003-09-01 04:08:36 +0000366 run_script(NULL, "leasefail");
Russ Dill61fb4892002-10-14 21:41:28 +0000367 if (client_config.background_if_no_lease) {
368 LOG(LOG_INFO, "No lease, forking to background.");
Glenn L McGrath24833432003-06-10 17:22:49 +0000369 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000370 } else if (client_config.abort_if_no_lease) {
371 LOG(LOG_INFO, "No lease, failing.");
Russ Dill4e864a32003-12-18 22:25:38 +0000372 return 1;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000373 }
Russ Dill61fb4892002-10-14 21:41:28 +0000374 /* wait to try again */
375 packet_num = 0;
376 timeout = now + 60;
377 }
378 break;
379 case RENEW_REQUESTED:
380 case REQUESTING:
381 if (packet_num < 3) {
382 /* send request packet */
383 if (state == RENEW_REQUESTED)
384 send_renew(xid, server_addr, requested_ip); /* unicast */
385 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000386
Russ Dill61fb4892002-10-14 21:41:28 +0000387 timeout = now + ((packet_num == 2) ? 10 : 2);
388 packet_num++;
389 } else {
390 /* timed out, go back to init state */
Russ Dillf5ecd432002-10-31 19:21:27 +0000391 if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000392 state = INIT_SELECTING;
393 timeout = now;
394 packet_num = 0;
395 change_mode(LISTEN_RAW);
396 }
397 break;
398 case BOUND:
399 /* Lease is starting to run out, time to enter renewing state */
400 state = RENEWING;
401 change_mode(LISTEN_KERNEL);
402 DEBUG(LOG_INFO, "Entering renew state");
403 /* fall right through */
404 case RENEWING:
405 /* Either set a new T1, or enter REBINDING state */
406 if ((t2 - t1) <= (lease / 14400 + 1)) {
407 /* timed out, enter rebinding state */
408 state = REBINDING;
409 timeout = now + (t2 - t1);
410 DEBUG(LOG_INFO, "Entering rebinding state");
411 } else {
412 /* send a request packet */
413 send_renew(xid, server_addr, requested_ip); /* unicast */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000414
Russ Dill61fb4892002-10-14 21:41:28 +0000415 t1 = (t2 - t1) / 2 + t1;
416 timeout = t1 + start;
417 }
418 break;
419 case REBINDING:
420 /* Either set a new T2, or enter INIT state */
421 if ((lease - t2) <= (lease / 14400 + 1)) {
422 /* timed out, enter init state */
423 state = INIT_SELECTING;
424 LOG(LOG_INFO, "Lease lost, entering init state");
425 run_script(NULL, "deconfig");
426 timeout = now;
427 packet_num = 0;
428 change_mode(LISTEN_RAW);
429 } else {
430 /* send a request packet */
431 send_renew(xid, 0, requested_ip); /* broadcast */
432
433 t2 = (lease - t2) / 2 + t2;
434 timeout = t2 + start;
435 }
436 break;
437 case RELEASED:
438 /* yah, I know, *you* say it would never happen */
439 timeout = 0x7fffffff;
440 break;
441 }
442 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
443 /* a packet is ready, read it */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000444
Russ Dill61fb4892002-10-14 21:41:28 +0000445 if (listen_mode == LISTEN_KERNEL)
446 len = get_packet(&packet, fd);
447 else len = get_raw_packet(&packet, fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000448
Russ Dill61fb4892002-10-14 21:41:28 +0000449 if (len == -1 && errno != EINTR) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000450 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
Russ Dill61fb4892002-10-14 21:41:28 +0000451 change_mode(listen_mode); /* just close and reopen */
452 }
453 if (len < 0) continue;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000454
Russ Dill61fb4892002-10-14 21:41:28 +0000455 if (packet.xid != xid) {
456 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
457 (unsigned long) packet.xid, xid);
458 continue;
459 }
Rob Landley0b1ff5a2005-05-26 05:25:12 +0000460 /* Ignore packets that aren't for us */
Paul Fox01f67982005-07-20 19:13:21 +0000461 if (memcmp(packet.chaddr, client_config.arp, 6)) {
462 DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
463 continue;
464 }
465
Russ Dill61fb4892002-10-14 21:41:28 +0000466 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
467 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
468 continue;
469 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000470
Russ Dill61fb4892002-10-14 21:41:28 +0000471 switch (state) {
472 case INIT_SELECTING:
473 /* Must be a DHCPOFFER to one of our xid's */
474 if (*message == DHCPOFFER) {
475 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
476 memcpy(&server_addr, temp, 4);
477 xid = packet.xid;
478 requested_ip = packet.yiaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000479
Russ Dill61fb4892002-10-14 21:41:28 +0000480 /* enter requesting state */
481 state = REQUESTING;
482 timeout = now;
483 packet_num = 0;
484 } else {
485 DEBUG(LOG_ERR, "No server ID in message");
486 }
487 }
488 break;
489 case RENEW_REQUESTED:
490 case REQUESTING:
491 case RENEWING:
492 case REBINDING:
493 if (*message == DHCPACK) {
494 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
495 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
496 lease = 60 * 60;
497 } else {
498 memcpy(&lease, temp, 4);
499 lease = ntohl(lease);
500 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000501
Russ Dill61fb4892002-10-14 21:41:28 +0000502 /* enter bound state */
503 t1 = lease / 2;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000504
Russ Dill61fb4892002-10-14 21:41:28 +0000505 /* little fixed point for n * .875 */
506 t2 = (lease * 0x7) >> 3;
507 temp_addr.s_addr = packet.yiaddr;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000508 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
Russ Dill61fb4892002-10-14 21:41:28 +0000509 inet_ntoa(temp_addr), lease);
510 start = now;
511 timeout = t1 + start;
512 requested_ip = packet.yiaddr;
513 run_script(&packet,
514 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
515
516 state = BOUND;
517 change_mode(LISTEN_NONE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000518 if (client_config.quit_after_lease)
Russ Dill4e864a32003-12-18 22:25:38 +0000519 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000520 if (!client_config.foreground)
Glenn L McGrath24833432003-06-10 17:22:49 +0000521 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000522
523 } else if (*message == DHCPNAK) {
524 /* return to init state */
525 LOG(LOG_INFO, "Received DHCP NAK");
526 run_script(&packet, "nak");
527 if (state != REQUESTING)
528 run_script(NULL, "deconfig");
529 state = INIT_SELECTING;
530 timeout = now;
531 requested_ip = 0;
532 packet_num = 0;
533 change_mode(LISTEN_RAW);
534 sleep(3); /* avoid excessive network traffic */
535 }
536 break;
537 /* case BOUND, RELEASED: - ignore all packets */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000538 }
Russ Dill4e864a32003-12-18 22:25:38 +0000539 } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
Russ Dill61fb4892002-10-14 21:41:28 +0000540 switch (sig) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000541 case SIGUSR1:
Russ Dill61fb4892002-10-14 21:41:28 +0000542 perform_renew();
543 break;
544 case SIGUSR2:
545 perform_release();
546 break;
547 case SIGTERM:
548 LOG(LOG_INFO, "Received SIGTERM");
Russ Dill4e864a32003-12-18 22:25:38 +0000549 return 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000550 }
551 } else if (retval == -1 && errno == EINTR) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000552 /* a signal was caught */
Russ Dill61fb4892002-10-14 21:41:28 +0000553 } else {
554 /* An error occured */
555 DEBUG(LOG_ERR, "Error on select");
556 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000557
Russ Dill61fb4892002-10-14 21:41:28 +0000558 }
559 return 0;
560}