blob: 4612c4100ce92f6bed252ea618ebe1323fbdf2e7 [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
Russ Dill61fb4892002-10-14 21:41:28 +000022#include <sys/time.h>
Russ Dill61fb4892002-10-14 21:41:28 +000023#include <sys/file.h>
24#include <unistd.h>
25#include <getopt.h>
26#include <stdlib.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <signal.h>
31#include <time.h>
32#include <string.h>
33#include <sys/ioctl.h>
34#include <net/if.h>
35#include <errno.h>
36
37#include "dhcpd.h"
38#include "dhcpc.h"
39#include "options.h"
40#include "clientpacket.h"
Russ Dill61fb4892002-10-14 21:41:28 +000041#include "script.h"
42#include "socket.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000043#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000044
45static int state;
46static unsigned long requested_ip; /* = 0 */
47static unsigned long server_addr;
48static unsigned long timeout;
49static int packet_num; /* = 0 */
Russ Dill6393d692003-01-21 22:39:34 +000050static int fd = -1;
Russ Dill61fb4892002-10-14 21:41:28 +000051
52#define LISTEN_NONE 0
53#define LISTEN_KERNEL 1
54#define LISTEN_RAW 2
55static int listen_mode;
56
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000057#ifdef CONFIG_INSTALL_NO_USR
58#define DEFAULT_SCRIPT "/share/udhcpc/default.script"
59#else
Russ Dill61fb4892002-10-14 21:41:28 +000060#define DEFAULT_SCRIPT "/usr/share/udhcpc/default.script"
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000061#endif
Russ Dill61fb4892002-10-14 21:41:28 +000062
63struct client_config_t client_config = {
64 /* Default options. */
65 abort_if_no_lease: 0,
66 foreground: 0,
67 quit_after_lease: 0,
68 background_if_no_lease: 0,
69 interface: "eth0",
70 pidfile: NULL,
71 script: DEFAULT_SCRIPT,
72 clientid: NULL,
73 hostname: NULL,
74 ifindex: 0,
75 arp: "\0\0\0\0\0\0", /* appease gcc-3.0 */
76};
77
Russ Dill61fb4892002-10-14 21:41:28 +000078/* just a little helper */
79static void change_mode(int new_mode)
80{
81 DEBUG(LOG_INFO, "entering %s listen mode",
82 new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
Russ Dill6393d692003-01-21 22:39:34 +000083 if (fd >= 0) close(fd);
Russ Dill61fb4892002-10-14 21:41:28 +000084 fd = -1;
85 listen_mode = new_mode;
86}
87
88
89/* perform a renew */
90static void perform_renew(void)
91{
92 LOG(LOG_INFO, "Performing a DHCP renew");
93 switch (state) {
Russ Dill61fb4892002-10-14 21:41:28 +000094 case BOUND:
Russ Dill61fb4892002-10-14 21:41:28 +000095 change_mode(LISTEN_KERNEL);
Russ Dillf5ecd432002-10-31 19:21:27 +000096 case RENEWING:
97 case REBINDING:
Russ Dill61fb4892002-10-14 21:41:28 +000098 state = RENEW_REQUESTED;
99 break;
Russ Dillf5ecd432002-10-31 19:21:27 +0000100 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
101 run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000102 case REQUESTING:
103 case RELEASED:
104 change_mode(LISTEN_RAW);
105 state = INIT_SELECTING;
106 break;
107 case INIT_SELECTING:
108 }
109
110 /* start things over */
111 packet_num = 0;
112
113 /* Kill any timeouts because the user wants this to hurry along */
114 timeout = 0;
115}
116
117
118/* perform a release */
119static void perform_release(void)
120{
121 char buffer[16];
122 struct in_addr temp_addr;
123
124 /* send release packet */
125 if (state == BOUND || state == RENEWING || state == REBINDING) {
126 temp_addr.s_addr = server_addr;
127 sprintf(buffer, "%s", inet_ntoa(temp_addr));
128 temp_addr.s_addr = requested_ip;
129 LOG(LOG_INFO, "Unicasting a release of %s to %s",
130 inet_ntoa(temp_addr), buffer);
131 send_release(server_addr, requested_ip); /* unicast */
132 run_script(NULL, "deconfig");
133 }
134 LOG(LOG_INFO, "Entering released state");
135
136 change_mode(LISTEN_NONE);
137 state = RELEASED;
138 timeout = 0x7fffffff;
139}
140
141
Glenn L McGrath24833432003-06-10 17:22:49 +0000142static void client_background(void)
Russ Dill61fb4892002-10-14 21:41:28 +0000143{
Glenn L McGrath24833432003-06-10 17:22:49 +0000144 background(client_config.pidfile);
Russ Dill61fb4892002-10-14 21:41:28 +0000145 client_config.foreground = 1; /* Do not fork again. */
Russ Dill6393d692003-01-21 22:39:34 +0000146 client_config.background_if_no_lease = 0;
Russ Dill61fb4892002-10-14 21:41:28 +0000147}
148
149
Russ Dill61fb4892002-10-14 21:41:28 +0000150int udhcpc_main(int argc, char *argv[])
Russ Dill61fb4892002-10-14 21:41:28 +0000151{
152 unsigned char *temp, *message;
153 unsigned long t1 = 0, t2 = 0, xid = 0;
154 unsigned long start = 0, lease;
155 fd_set rfds;
156 int retval;
157 struct timeval tv;
158 int c, len;
159 struct dhcpMessage packet;
160 struct in_addr temp_addr;
Russ Dill61fb4892002-10-14 21:41:28 +0000161 time_t now;
162 int max_fd;
163 int sig;
164
Glenn L McGrath24833432003-06-10 17:22:49 +0000165 static const struct option arg_options[] = {
Russ Dill61fb4892002-10-14 21:41:28 +0000166 {"clientid", required_argument, 0, 'c'},
167 {"foreground", no_argument, 0, 'f'},
168 {"background", no_argument, 0, 'b'},
169 {"hostname", required_argument, 0, 'H'},
170 {"hostname", required_argument, 0, 'h'},
171 {"interface", required_argument, 0, 'i'},
172 {"now", no_argument, 0, 'n'},
173 {"pidfile", required_argument, 0, 'p'},
174 {"quit", no_argument, 0, 'q'},
175 {"request", required_argument, 0, 'r'},
176 {"script", required_argument, 0, 's'},
177 {"version", no_argument, 0, 'v'},
Russ Dill61fb4892002-10-14 21:41:28 +0000178 {0, 0, 0, 0}
179 };
180
181 /* get options */
182 while (1) {
183 int option_index = 0;
184 c = getopt_long(argc, argv, "c:fbH:h:i:np:qr:s:v", arg_options, &option_index);
185 if (c == -1) break;
186
187 switch (c) {
188 case 'c':
189 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Russ Dill1eb7a172002-12-11 21:12:45 +0000190 if (client_config.clientid) free(client_config.clientid);
Russ Dill61fb4892002-10-14 21:41:28 +0000191 client_config.clientid = xmalloc(len + 2);
192 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
193 client_config.clientid[OPT_LEN] = len;
194 client_config.clientid[OPT_DATA] = '\0';
195 strncpy(client_config.clientid + OPT_DATA, optarg, len);
196 break;
197 case 'f':
198 client_config.foreground = 1;
199 break;
200 case 'b':
201 client_config.background_if_no_lease = 1;
202 break;
203 case 'h':
204 case 'H':
205 len = strlen(optarg) > 255 ? 255 : strlen(optarg);
Russ Dill1eb7a172002-12-11 21:12:45 +0000206 if (client_config.hostname) free(client_config.hostname);
Russ Dill61fb4892002-10-14 21:41:28 +0000207 client_config.hostname = xmalloc(len + 2);
208 client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
209 client_config.hostname[OPT_LEN] = len;
210 strncpy(client_config.hostname + 2, optarg, len);
211 break;
212 case 'i':
213 client_config.interface = optarg;
214 break;
215 case 'n':
216 client_config.abort_if_no_lease = 1;
217 break;
218 case 'p':
219 client_config.pidfile = optarg;
220 break;
221 case 'q':
222 client_config.quit_after_lease = 1;
223 break;
224 case 'r':
225 requested_ip = inet_addr(optarg);
226 break;
227 case 's':
228 client_config.script = optarg;
229 break;
230 case 'v':
Glenn L McGrath24833432003-06-10 17:22:49 +0000231 bb_error_msg("version %s\n", VERSION);
232 return(0);
Russ Dill61fb4892002-10-14 21:41:28 +0000233 break;
234 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000235 bb_show_usage();
Russ Dill61fb4892002-10-14 21:41:28 +0000236 }
237 }
238
Glenn L McGrath24833432003-06-10 17:22:49 +0000239 start_log("client");
Russ Dill61fb4892002-10-14 21:41:28 +0000240 if (read_interface(client_config.interface, &client_config.ifindex,
241 NULL, client_config.arp) < 0)
Glenn L McGrath24833432003-06-10 17:22:49 +0000242 return(1);
Russ Dill61fb4892002-10-14 21:41:28 +0000243
244 if (!client_config.clientid) {
245 client_config.clientid = xmalloc(6 + 3);
246 client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
247 client_config.clientid[OPT_LEN] = 7;
248 client_config.clientid[OPT_DATA] = 1;
249 memcpy(client_config.clientid + 3, client_config.arp, 6);
250 }
251
252 /* setup signal handlers */
Glenn L McGrath24833432003-06-10 17:22:49 +0000253 udhcp_set_signal_pipe(SIGUSR2);
Russ Dill61fb4892002-10-14 21:41:28 +0000254
255 state = INIT_SELECTING;
256 run_script(NULL, "deconfig");
257 change_mode(LISTEN_RAW);
258
259 for (;;) {
260
261 tv.tv_sec = timeout - time(0);
262 tv.tv_usec = 0;
263 FD_ZERO(&rfds);
264
265 if (listen_mode != LISTEN_NONE && fd < 0) {
266 if (listen_mode == LISTEN_KERNEL)
267 fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
268 else
269 fd = raw_socket(client_config.ifindex);
270 if (fd < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000271 LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
272 return(0);
Russ Dill61fb4892002-10-14 21:41:28 +0000273 }
274 }
275 if (fd >= 0) FD_SET(fd, &rfds);
Glenn L McGrath24833432003-06-10 17:22:49 +0000276 FD_SET(udhcp_signal_pipe[0], &rfds);
Russ Dill61fb4892002-10-14 21:41:28 +0000277
278 if (tv.tv_sec > 0) {
279 DEBUG(LOG_INFO, "Waiting on select...\n");
Glenn L McGrath24833432003-06-10 17:22:49 +0000280 max_fd = udhcp_signal_pipe[0] > fd ? udhcp_signal_pipe[0] : fd;
Russ Dill61fb4892002-10-14 21:41:28 +0000281 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
282 } else retval = 0; /* If we already timed out, fall through */
283
284 now = time(0);
285 if (retval == 0) {
286 /* timeout dropped to zero */
287 switch (state) {
288 case INIT_SELECTING:
289 if (packet_num < 3) {
290 if (packet_num == 0)
291 xid = random_xid();
292
293 /* send discover packet */
294 send_discover(xid, requested_ip); /* broadcast */
295
296 timeout = now + ((packet_num == 2) ? 4 : 2);
297 packet_num++;
298 } else {
299 if (client_config.background_if_no_lease) {
300 LOG(LOG_INFO, "No lease, forking to background.");
Glenn L McGrath24833432003-06-10 17:22:49 +0000301 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000302 } else if (client_config.abort_if_no_lease) {
303 LOG(LOG_INFO, "No lease, failing.");
Glenn L McGrath24833432003-06-10 17:22:49 +0000304 return(1);
Russ Dill61fb4892002-10-14 21:41:28 +0000305 }
306 /* wait to try again */
307 packet_num = 0;
308 timeout = now + 60;
309 }
310 break;
311 case RENEW_REQUESTED:
312 case REQUESTING:
313 if (packet_num < 3) {
314 /* send request packet */
315 if (state == RENEW_REQUESTED)
316 send_renew(xid, server_addr, requested_ip); /* unicast */
317 else send_selecting(xid, server_addr, requested_ip); /* broadcast */
318
319 timeout = now + ((packet_num == 2) ? 10 : 2);
320 packet_num++;
321 } else {
322 /* timed out, go back to init state */
Russ Dillf5ecd432002-10-31 19:21:27 +0000323 if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
Russ Dill61fb4892002-10-14 21:41:28 +0000324 state = INIT_SELECTING;
325 timeout = now;
326 packet_num = 0;
327 change_mode(LISTEN_RAW);
328 }
329 break;
330 case BOUND:
331 /* Lease is starting to run out, time to enter renewing state */
332 state = RENEWING;
333 change_mode(LISTEN_KERNEL);
334 DEBUG(LOG_INFO, "Entering renew state");
335 /* fall right through */
336 case RENEWING:
337 /* Either set a new T1, or enter REBINDING state */
338 if ((t2 - t1) <= (lease / 14400 + 1)) {
339 /* timed out, enter rebinding state */
340 state = REBINDING;
341 timeout = now + (t2 - t1);
342 DEBUG(LOG_INFO, "Entering rebinding state");
343 } else {
344 /* send a request packet */
345 send_renew(xid, server_addr, requested_ip); /* unicast */
346
347 t1 = (t2 - t1) / 2 + t1;
348 timeout = t1 + start;
349 }
350 break;
351 case REBINDING:
352 /* Either set a new T2, or enter INIT state */
353 if ((lease - t2) <= (lease / 14400 + 1)) {
354 /* timed out, enter init state */
355 state = INIT_SELECTING;
356 LOG(LOG_INFO, "Lease lost, entering init state");
357 run_script(NULL, "deconfig");
358 timeout = now;
359 packet_num = 0;
360 change_mode(LISTEN_RAW);
361 } else {
362 /* send a request packet */
363 send_renew(xid, 0, requested_ip); /* broadcast */
364
365 t2 = (lease - t2) / 2 + t2;
366 timeout = t2 + start;
367 }
368 break;
369 case RELEASED:
370 /* yah, I know, *you* say it would never happen */
371 timeout = 0x7fffffff;
372 break;
373 }
374 } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
375 /* a packet is ready, read it */
376
377 if (listen_mode == LISTEN_KERNEL)
378 len = get_packet(&packet, fd);
379 else len = get_raw_packet(&packet, fd);
380
381 if (len == -1 && errno != EINTR) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000382 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
Russ Dill61fb4892002-10-14 21:41:28 +0000383 change_mode(listen_mode); /* just close and reopen */
384 }
385 if (len < 0) continue;
386
387 if (packet.xid != xid) {
388 DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
389 (unsigned long) packet.xid, xid);
390 continue;
391 }
392
393 if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
394 DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
395 continue;
396 }
397
398 switch (state) {
399 case INIT_SELECTING:
400 /* Must be a DHCPOFFER to one of our xid's */
401 if (*message == DHCPOFFER) {
402 if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
403 memcpy(&server_addr, temp, 4);
404 xid = packet.xid;
405 requested_ip = packet.yiaddr;
406
407 /* enter requesting state */
408 state = REQUESTING;
409 timeout = now;
410 packet_num = 0;
411 } else {
412 DEBUG(LOG_ERR, "No server ID in message");
413 }
414 }
415 break;
416 case RENEW_REQUESTED:
417 case REQUESTING:
418 case RENEWING:
419 case REBINDING:
420 if (*message == DHCPACK) {
421 if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
422 LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
423 lease = 60 * 60;
424 } else {
425 memcpy(&lease, temp, 4);
426 lease = ntohl(lease);
427 }
428
429 /* enter bound state */
430 t1 = lease / 2;
431
432 /* little fixed point for n * .875 */
433 t2 = (lease * 0x7) >> 3;
434 temp_addr.s_addr = packet.yiaddr;
435 LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
436 inet_ntoa(temp_addr), lease);
437 start = now;
438 timeout = t1 + start;
439 requested_ip = packet.yiaddr;
440 run_script(&packet,
441 ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
442
443 state = BOUND;
444 change_mode(LISTEN_NONE);
445 if (client_config.quit_after_lease)
Glenn L McGrath24833432003-06-10 17:22:49 +0000446 return(0);
Russ Dill61fb4892002-10-14 21:41:28 +0000447 if (!client_config.foreground)
Glenn L McGrath24833432003-06-10 17:22:49 +0000448 client_background();
Russ Dill61fb4892002-10-14 21:41:28 +0000449
450 } else if (*message == DHCPNAK) {
451 /* return to init state */
452 LOG(LOG_INFO, "Received DHCP NAK");
453 run_script(&packet, "nak");
454 if (state != REQUESTING)
455 run_script(NULL, "deconfig");
456 state = INIT_SELECTING;
457 timeout = now;
458 requested_ip = 0;
459 packet_num = 0;
460 change_mode(LISTEN_RAW);
461 sleep(3); /* avoid excessive network traffic */
462 }
463 break;
464 /* case BOUND, RELEASED: - ignore all packets */
465 }
Glenn L McGrath24833432003-06-10 17:22:49 +0000466 } else if (retval > 0 && FD_ISSET(udhcp_signal_pipe[0], &rfds)) {
467 if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0) {
468 DEBUG(LOG_ERR, "Could not read signal: %m");
Russ Dill61fb4892002-10-14 21:41:28 +0000469 continue; /* probably just EINTR */
470 }
471 switch (sig) {
472 case SIGUSR1:
473 perform_renew();
474 break;
475 case SIGUSR2:
476 perform_release();
477 break;
478 case SIGTERM:
479 LOG(LOG_INFO, "Received SIGTERM");
Glenn L McGrath24833432003-06-10 17:22:49 +0000480 return(0);
Russ Dill61fb4892002-10-14 21:41:28 +0000481 }
482 } else if (retval == -1 && errno == EINTR) {
483 /* a signal was caught */
484 } else {
485 /* An error occured */
486 DEBUG(LOG_ERR, "Error on select");
487 }
488
489 }
490 return 0;
491}