5 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
20 #include <sys/ioctl.h>
28 #include "clientpacket.h"
29 #include "clientsocket.h"
31 #include "signalpipe.h"
35 static unsigned long requested_ip
; /* = 0 */
36 static unsigned long server_addr
;
37 static unsigned long timeout
;
38 static int packet_num
; /* = 0 */
42 #define LISTEN_KERNEL 1
44 static int listen_mode
;
46 struct client_config_t client_config
= {
47 /* Default options. */
48 .abort_if_no_lease
= 0,
50 .quit_after_lease
= 0,
51 .background_if_no_lease
= 0,
54 .script
= DEFAULT_SCRIPT
,
62 .arp
= "\0\0\0\0\0\0", /* appease gcc-3.0 */
66 /* just a little helper */
67 static void change_mode(int new_mode
)
69 DEBUG(LOG_INFO
, "entering %s listen mode",
70 new_mode
? (new_mode
== 1 ? "kernel" : "raw") : "none");
71 if (fd
>= 0) close(fd
);
73 listen_mode
= new_mode
;
78 static void perform_renew(void)
80 LOG(LOG_INFO
, "Performing a DHCP renew");
83 change_mode(LISTEN_KERNEL
);
86 state
= RENEW_REQUESTED
;
88 case RENEW_REQUESTED
: /* impatient are we? fine, square 1 */
89 udhcp_run_script(NULL
, "deconfig");
92 change_mode(LISTEN_RAW
);
93 state
= INIT_SELECTING
;
99 /* start things over */
102 /* Kill any timeouts because the user wants this to hurry along */
107 /* perform a release */
108 static void perform_release(void)
111 struct in_addr temp_addr
;
113 /* send release packet */
114 if (state
== BOUND
|| state
== RENEWING
|| state
== REBINDING
) {
115 temp_addr
.s_addr
= server_addr
;
116 sprintf(buffer
, "%s", inet_ntoa(temp_addr
));
117 temp_addr
.s_addr
= requested_ip
;
118 LOG(LOG_INFO
, "Unicasting a release of %s to %s",
119 inet_ntoa(temp_addr
), buffer
);
120 send_release(server_addr
, requested_ip
); /* unicast */
121 udhcp_run_script(NULL
, "deconfig");
123 LOG(LOG_INFO
, "Entering released state");
125 change_mode(LISTEN_NONE
);
127 timeout
= 0x7fffffff;
131 static void client_background(void)
133 udhcp_background(client_config
.pidfile
);
134 client_config
.foreground
= 1; /* Do not fork again. */
135 client_config
.background_if_no_lease
= 0;
139 int udhcpc_main(int argc
, char *argv
[])
141 uint8_t *temp
, *message
;
142 unsigned long t1
= 0, t2
= 0, xid
= 0;
143 unsigned long start
= 0, lease
;
148 struct dhcpMessage packet
;
149 struct in_addr temp_addr
;
155 static const struct option arg_options
[] = {
156 {"clientid", required_argument
, 0, 'c'},
157 {"clientid-none", no_argument
, 0, 'C'},
158 {"vendorclass", required_argument
, 0, 'V'},
159 {"foreground", no_argument
, 0, 'f'},
160 {"background", no_argument
, 0, 'b'},
161 {"hostname", required_argument
, 0, 'H'},
162 {"hostname", required_argument
, 0, 'h'},
163 {"fqdn", required_argument
, 0, 'F'},
164 {"interface", required_argument
, 0, 'i'},
165 {"laniface", required_argument
, 0, 'l'},
166 {"now", no_argument
, 0, 'n'},
167 {"pidfile", required_argument
, 0, 'p'},
168 {"quit", no_argument
, 0, 'q'},
169 {"request", required_argument
, 0, 'r'},
170 {"script", required_argument
, 0, 's'},
171 {"timeout", required_argument
, 0, 'T'},
172 {"version", no_argument
, 0, 'v'},
173 {"retries", required_argument
, 0, 't'},
179 int option_index
= 0;
180 c
= getopt_long(argc
, argv
, "c:CV:fbH:h:F:i:np:qr:s:T:t:vl:mQ", arg_options
, &option_index
);
185 client_config
.lan_interface
= optarg
;
191 if (no_clientid
) bb_show_usage();
192 len
= strlen(optarg
) > 255 ? 255 : strlen(optarg
);
193 free(client_config
.clientid
);
194 client_config
.clientid
= xmalloc(len
+ 2);
195 client_config
.clientid
[OPT_CODE
] = DHCP_CLIENT_ID
;
196 client_config
.clientid
[OPT_LEN
] = len
;
197 client_config
.clientid
[OPT_DATA
] = '\0';
198 strncpy((char*)client_config
.clientid
+ OPT_DATA
, optarg
, len
);
201 if (client_config
.clientid
) bb_show_usage();
205 len
= strlen(optarg
) > 255 ? 255 : strlen(optarg
);
206 free(client_config
.vendorclass
);
207 client_config
.vendorclass
= xmalloc(len
+ 2);
208 client_config
.vendorclass
[OPT_CODE
] = DHCP_VENDOR
;
209 client_config
.vendorclass
[OPT_LEN
] = len
;
210 strncpy((char*)client_config
.vendorclass
+ OPT_DATA
, optarg
, len
);
213 client_config
.foreground
= 1;
216 client_config
.background_if_no_lease
= 1;
220 len
= strlen(optarg
) > 255 ? 255 : strlen(optarg
);
221 free(client_config
.hostname
);
222 client_config
.hostname
= xmalloc(len
+ 2);
223 client_config
.hostname
[OPT_CODE
] = DHCP_HOST_NAME
;
224 client_config
.hostname
[OPT_LEN
] = len
;
225 strncpy((char*)client_config
.hostname
+ 2, optarg
, len
);
228 len
= strlen(optarg
) > 255 ? 255 : strlen(optarg
);
229 free(client_config
.fqdn
);
230 client_config
.fqdn
= xmalloc(len
+ 5);
231 client_config
.fqdn
[OPT_CODE
] = DHCP_FQDN
;
232 client_config
.fqdn
[OPT_LEN
] = len
+ 3;
234 S: 1 => Client requests Server to update A RR in DNS as well as PTR
235 O: 1 => Server indicates to client that DNS has been updated regardless
236 E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
237 N: 1 => Client requests Server to not update DNS
239 client_config
.fqdn
[OPT_LEN
+ 1] = 0x1;
240 client_config
.fqdn
[OPT_LEN
+ 2] = 0;
241 client_config
.fqdn
[OPT_LEN
+ 3] = 0;
242 strncpy((char*)client_config
.fqdn
+ 5, optarg
, len
);
245 client_config
.interface
= optarg
;
248 client_config
.abort_if_no_lease
= 1;
251 client_config
.pidfile
= optarg
;
254 client_config
.quit_after_lease
= 1;
257 requested_ip
= inet_addr(optarg
);
260 client_config
.script
= optarg
;
263 client_config
.timeout
= atoi(optarg
);
266 client_config
.retries
= atoi(optarg
);
269 printf("version %s\n\n", BB_VER
);
273 setlogmask(LOG_UPTO(LOG_ERR
));
280 /* Start the log, sanitize fd's, and write a pid file */
281 #ifdef CONFIG_FEATURE_UDHCP_DEBUG
282 udhcp_start_log_and_pid("udhcpc_debug", client_config
.pidfile
);
284 udhcp_start_log_and_pid("udhcpc", client_config
.pidfile
);
287 if (read_interface(client_config
.interface
, &client_config
.ifindex
,
288 NULL
, client_config
.arp
) < 0)
291 /* if not set, and not suppressed, setup the default client ID */
292 if (!client_config
.clientid
&& !no_clientid
) {
293 client_config
.clientid
= xmalloc(6 + 3);
294 client_config
.clientid
[OPT_CODE
] = DHCP_CLIENT_ID
;
295 client_config
.clientid
[OPT_LEN
] = 7;
296 client_config
.clientid
[OPT_DATA
] = 1;
297 memcpy(client_config
.clientid
+ 3, client_config
.arp
, 6);
300 if (!client_config
.vendorclass
) {
301 client_config
.vendorclass
= xmalloc(sizeof("udhcp "BB_VER
) + 2);
302 client_config
.vendorclass
[OPT_CODE
] = DHCP_VENDOR
;
303 client_config
.vendorclass
[OPT_LEN
] = sizeof("udhcp "BB_VER
) - 1;
304 client_config
.vendorclass
[OPT_DATA
] = 1;
305 memcpy(&client_config
.vendorclass
[OPT_DATA
],
306 "udhcp "BB_VER
, sizeof("udhcp "BB_VER
) - 1);
310 /* setup the signal pipe */
314 printf("DEBUG %s\n", __TIME__
);
316 server_addr
= inet_addr("192.168.5.5");
317 requested_ip
= inet_addr("192.168.5.3");
323 state
= INIT_SELECTING
;
324 udhcp_run_script(NULL
, "deconfig");
325 change_mode(LISTEN_RAW
);
329 tv
.tv_sec
= timeout
- uptime();
332 if (listen_mode
!= LISTEN_NONE
&& fd
< 0) {
333 if (listen_mode
== LISTEN_KERNEL
)
334 fd
= listen_socket(INADDR_ANY
, CLIENT_PORT
, client_config
.interface
);
336 fd
= raw_socket(client_config
.ifindex
);
338 LOG(LOG_ERR
, "FATAL: couldn't listen on socket, %m");
342 max_fd
= udhcp_sp_fd_set(&rfds
, fd
);
345 DEBUG(LOG_INFO
, "Waiting on select...");
346 retval
= select(max_fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
347 } else retval
= 0; /* If we already timed out, fall through */
351 DEBUG(LOG_DEBUG
, "T/O: state=%d packet_num=%d", state
, packet_num
);
353 /* timeout dropped to zero */
356 if (packet_num
< client_config
.retries
) {
360 /* send discover packet */
361 send_discover(xid
, requested_ip
); /* broadcast */
363 timeout
= now
+ client_config
.timeout
;
366 udhcp_run_script(NULL
, "leasefail");
367 if (client_config
.background_if_no_lease
) {
368 LOG(LOG_INFO
, "No lease, forking to background.");
370 } else if (client_config
.abort_if_no_lease
) {
371 LOG(LOG_INFO
, "No lease, failing.");
374 /* wait to try again */
376 timeout
= now
+ 20; // reduced from 60 sec
379 case RENEW_REQUESTED
:
381 if (packet_num
< client_config
.retries
) {
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 */
387 timeout
= now
+ ((packet_num
== 2) ? 10 : 2);
390 /* timed out, go back to init state */
391 if (state
== RENEW_REQUESTED
) udhcp_run_script(NULL
, "deconfig");
392 state
= INIT_SELECTING
;
395 change_mode(LISTEN_RAW
);
399 /* Lease is starting to run out, time to enter renewing state */
401 //change_mode(LISTEN_KERNEL);
402 change_mode(LISTEN_RAW
); // Some DHCP server send broadcast ACK after they receive a REQUEST during RENEWING state, by honor
403 DEBUG(LOG_INFO
, "Entering renew state");
404 /* fall right through */
406 /* Either set a new T1, or enter REBINDING state */
407 if ((t2
- t1
) <= (lease
/ 14400 + 1)) {
408 /* timed out, enter rebinding state */
410 timeout
= now
+ (t2
- t1
);
411 LOG(LOG_INFO
, "Entering rebinding state");
413 /* send a request packet */
414 send_renew(xid
, server_addr
, requested_ip
); /* unicast */
416 t1
= (t2
- t1
) / 2 + t1
;
417 timeout
= t1
+ start
;
421 /* Either set a new T2, or enter INIT state */
422 if ((lease
- t2
) <= (lease
/ 14400 + 1)) {
423 /* timed out, enter init state */
424 state
= INIT_SELECTING
;
425 LOG(LOG_INFO
, "Lease lost, entering init state");
426 udhcp_run_script(NULL
, "deconfig");
429 change_mode(LISTEN_RAW
);
431 /* send a request packet */
432 send_renew(xid
, 0, requested_ip
); /* broadcast */
434 t2
= (lease
- t2
) / 2 + t2
;
435 timeout
= t2
+ start
;
439 /* yah, I know, *you* say it would never happen */
440 timeout
= 0x7fffffff;
443 } else if (retval
> 0 && listen_mode
!= LISTEN_NONE
&& FD_ISSET(fd
, &rfds
)) {
444 /* a packet is ready, read it */
445 DEBUG(LOG_DEBUG
, "R: state=%d", state
);
447 if (listen_mode
== LISTEN_KERNEL
)
448 len
= udhcp_get_packet(&packet
, fd
);
449 else len
= get_raw_packet(&packet
, fd
);
451 if (len
== -1 && errno
!= EINTR
) {
452 DEBUG(LOG_INFO
, "error on read, %m, reopening socket");
453 change_mode(listen_mode
); /* just close and reopen */
455 if (len
< 0) continue;
457 if (packet
.xid
!= xid
) {
458 DEBUG(LOG_INFO
, "Ignoring XID %lx (our xid is %lx)",
459 (unsigned long) packet
.xid
, xid
);
463 /* Ignore packets that aren't for us */
464 if (memcmp(packet
.chaddr
, client_config
.arp
, 6)) {
465 DEBUG(LOG_INFO
, "packet does not have our chaddr -- ignoring");
469 if ((message
= get_option(&packet
, DHCP_MESSAGE_TYPE
)) == NULL
) {
470 DEBUG(LOG_ERR
, "couldnt get option from packet -- ignoring");
474 DEBUG(LOG_DEBUG
, "R: message=%d", *message
);
477 /* Must be a DHCPOFFER to one of our xid's */
478 if (*message
== DHCPOFFER
) {
479 if ((temp
= get_option(&packet
, DHCP_SERVER_ID
))) {
480 memcpy(&server_addr
, temp
, 4);
482 requested_ip
= packet
.yiaddr
;
485 // if the wan ip address exists in our lan, decline
489 unsigned char lan_mac
[6];
491 if ((client_config
.lan_interface
!= NULL
) &&
492 (read_interface(client_config
.lan_interface
, &ifindex
, &lan_ip
, lan_mac
) >= 0)) {
493 if (arpping(requested_ip
, lan_ip
, lan_mac
, client_config
.lan_interface
) == 0) {
494 temp_addr
.s_addr
= requested_ip
;
495 LOG(LOG_ERR
, "Offered IP address %s exists in the network. Declining.", inet_ntoa(temp_addr
));
496 send_decline(xid
, requested_ip
);
502 /* enter requesting state */
507 DEBUG(LOG_ERR
, "No server ID in message");
511 case RENEW_REQUESTED
:
515 if (*message
== DHCPACK
) {
516 if (!(temp
= get_option(&packet
, DHCP_LEASE_TIME
))) {
517 LOG(LOG_ERR
, "No lease time with ACK, using 1 hour lease");
520 memcpy(&lease
, temp
, 4);
521 lease
= ntohl(lease
);
524 /* enter bound state */
527 /* little fixed point for n * .875 */
528 t2
= (lease
* 0x7) >> 3;
529 temp_addr
.s_addr
= packet
.yiaddr
;
530 LOG(LOG_INFO
, "Lease of %s obtained, lease time %ld",
531 inet_ntoa(temp_addr
), lease
);
533 timeout
= t1
+ start
;
534 requested_ip
= packet
.yiaddr
;
535 udhcp_run_script(&packet
,
536 ((state
== RENEWING
|| state
== REBINDING
) ? "renew" : "bound"));
539 change_mode(LISTEN_NONE
);
540 if (client_config
.quit_after_lease
)
542 if (!client_config
.foreground
)
545 } else if (*message
== DHCPNAK
) {
546 /* return to init state */
547 LOG(LOG_INFO
, "Received DHCP NAK");
548 udhcp_run_script(&packet
, "nak");
549 if (state
!= REQUESTING
)
550 udhcp_run_script(NULL
, "deconfig");
551 state
= INIT_SELECTING
;
555 change_mode(LISTEN_RAW
);
556 sleep(3); /* avoid excessive network traffic */
559 /* case BOUND, RELEASED: - ignore all packets */
561 } else if (retval
> 0 && (sig
= udhcp_sp_read(&rfds
))) {
570 LOG(LOG_INFO
, "Received SIGTERM");
573 } else if (retval
== -1 && errno
== EINTR
) {
574 /* a signal was caught */
576 /* An error occured */
577 DEBUG(LOG_ERR
, "Error on select");