1 /* vi: set sw=4 ts=4: */
6 * WARNING: THIS CODE IS INCOMPLETE. IT IS NOWHERE NEAR
7 * TO BE READY FOR PRODUCTION USE.
9 * Copyright (C) 2011 Denys Vlasenko.
11 * Licensed under GPLv2, see file LICENSE in this source tree.
14 //config:config UDHCPC6
15 //config: bool "udhcp client for DHCPv6 (udhcpc6)"
16 //config: default n # not yet ready
18 //config: udhcpc6 is a DHCPv6 client
20 //applet:IF_UDHCPC6(APPLET(udhcpc6, BB_DIR_USR_BIN, BB_SUID_DROP))
22 //kbuild:lib-$(CONFIG_UDHCPC6) += d6_dhcpc.o d6_packet.o d6_socket.o common.o
26 /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
27 #define WANT_PIDFILE 1
31 #include "d6_common.h"
33 #include <netinet/if_ether.h>
34 #include <netpacket/packet.h>
35 #include <linux/filter.h>
37 /* "struct client_config_t client_config" is in bb_common_bufsiz1 */
41 static const char udhcpc_longopts[] ALIGN1 =
42 "interface\0" Required_argument "i"
43 "now\0" No_argument "n"
44 "pidfile\0" Required_argument "p"
45 "quit\0" No_argument "q"
46 "release\0" No_argument "R"
47 "request\0" Required_argument "r"
48 "script\0" Required_argument "s"
49 "timeout\0" Required_argument "T"
50 "version\0" No_argument "v"
51 "retries\0" Required_argument "t"
52 "tryagain\0" Required_argument "A"
53 "syslog\0" No_argument "S"
54 "request-option\0" Required_argument "O"
55 "no-default-options\0" No_argument "o"
56 "foreground\0" No_argument "f"
57 "background\0" No_argument "b"
58 "broadcast\0" No_argument "B"
59 /// IF_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a")
60 IF_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P")
63 /* Must match getopt32 option string order */
81 /* The rest has variable bit positions, need to be clever */
83 USE_FOR_MMU( OPTBIT_b,)
84 ///IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,)
85 IF_FEATURE_UDHCP_PORT( OPTBIT_P,)
86 USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,)
87 ///IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,)
88 IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,)
92 /*** Utility functions ***/
94 static void *d6_find_option(uint8_t *option, uint8_t *option_end, unsigned code)
96 /* "length minus 4" */
97 int len_m4 = option_end - option - 4;
99 /* Next option's len is too big? */
100 if (option[2] > len_m4)
101 return NULL; /* yes. bogus packet! */
102 /* So far we treat any opts with code >255
103 * or len >255 as bogus, and stop at once.
104 * This simplifies big-endian handling.
106 if (option[1] != 0 || option[3] != 0)
108 /* Option seems to be valid */
109 /* Does its code match? */
110 if (option[0] == code)
111 return option; /* yes! */
112 option += option[2] + 4;
113 len_m4 -= option[2] + 4;
118 static void *d6_copy_option(uint8_t *option, uint8_t *option_end, unsigned code)
120 uint8_t *opt = d6_find_option(option, option_end, code);
123 return memcpy(xmalloc(opt[2] + 4), opt, opt[2] + 4);
126 static void *d6_store_blob(void *dst, const void *src, unsigned len)
128 memcpy(dst, src, len);
133 /*** Script execution code ***/
135 /* put all the parameters into the environment */
136 static char **fill_envp(struct d6_packet *packet
143 #define BITMAP unsigned
144 #define BBITS (sizeof(BITMAP) * 8)
145 #define BMASK(i) (1 << (i & (sizeof(BITMAP) * 8 - 1)))
146 #define FOUND_OPTS(i) (found_opts[(unsigned)i / BBITS])
147 ///BITMAP found_opts[256 / BBITS];
149 ///memset(found_opts, 0, sizeof(found_opts));
151 /* We need 2 elements for:
157 curr = envp = xzalloc(sizeof(envp[0]) * envc);
159 *curr = xasprintf("interface=%s", client_config.interface);
165 /* Call a script with a par file and env vars */
166 static void d6_run_script(struct d6_packet *packet, const char *name)
171 envp = fill_envp(packet);
174 log1("Executing %s %s", client_config.script, name);
175 argv[0] = (char*) client_config.script;
176 argv[1] = (char*) name;
178 spawn_and_wait(argv);
180 for (curr = envp; *curr; curr++) {
182 bb_unsetenv_and_free(*curr);
188 /*** Sending/receiving packets ***/
190 static ALWAYS_INLINE uint32_t random_xid(void)
192 uint32_t t = rand() & htonl(0x00ffffff);
196 /* Initialize the packet with the proper defaults */
197 static uint8_t *init_d6_packet(struct d6_packet *packet, char type, uint32_t xid)
199 struct d6_option *clientid;
201 memset(packet, 0, sizeof(*packet));
203 packet->d6_xid32 = xid;
204 packet->d6_msg_type = type;
206 clientid = (void*)client_config.clientid;
207 return d6_store_blob(packet->d6_options, clientid, clientid->len + 2+2);
210 static uint8_t *add_d6_client_options(uint8_t *ptr)
216 /* Add a "param req" option with the list of options we'd like to have
217 * from stubborn DHCP servers. Pull the data from the struct in common.c.
218 * No bounds checking because it goes towards the head of the packet. */
221 /* Add -x options if any */
225 static int d6_mcast_from_client_config_ifindex(struct d6_packet *packet, uint8_t *end)
227 static const uint8_t FF02__1_2[16] = {
228 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02,
232 return d6_send_raw_packet(
233 packet, (end - (uint8_t*) packet),
234 /*src*/ NULL, CLIENT_PORT,
235 /*dst*/ (struct in6_addr*)FF02__1_2, SERVER_PORT, MAC_BCAST_ADDR,
236 client_config.ifindex
240 /* Milticast a DHCPv6 Solicit packet to the network, with an optionally requested IP.
242 * RFC 3315 17.1.1. Creation of Solicit Messages
244 * The client MUST include a Client Identifier option to identify itself
245 * to the server. The client includes IA options for any IAs to which
246 * it wants the server to assign addresses. The client MAY include
247 * addresses in the IAs as a hint to the server about addresses for
248 * which the client has a preference. ...
250 * The client uses IA_NA options to request the assignment of non-
251 * temporary addresses and uses IA_TA options to request the assignment
252 * of temporary addresses. Either IA_NA or IA_TA options, or a
253 * combination of both, can be included in DHCP messages.
255 * The client SHOULD include an Option Request option (see section 22.7)
256 * to indicate the options the client is interested in receiving. The
257 * client MAY additionally include instances of those options that are
258 * identified in the Option Request option, with data values as hints to
259 * the server about parameter values the client would like to have
262 * The client includes a Reconfigure Accept option (see section 22.20)
263 * if the client is willing to accept Reconfigure messages from the
265 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266 | OPTION_CLIENTID | option-len |
267 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270 . (variable length) .
272 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
275 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
276 | OPTION_IA_NA | option-len |
277 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
279 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
281 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
283 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
287 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
290 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
291 | OPTION_IAADDR | option-len |
292 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
297 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
298 | preferred-lifetime |
299 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
301 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
305 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
308 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
309 | OPTION_ORO | option-len |
310 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311 | requested-option-code-1 | requested-option-code-2 |
312 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
314 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
317 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
318 | OPTION_RECONF_ACCEPT | 0 |
319 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
321 /* NOINLINE: limit stack usage in caller */
322 static NOINLINE int send_d6_discover(uint32_t xid, struct in6_addr *requested_ipv6)
324 struct d6_packet packet;
328 /* Fill in: msg type, client id */
329 opt_ptr = init_d6_packet(&packet, D6_MSG_SOLICIT, xid);
331 /* Create new IA_NA, optionally with included IAADDR with requested IP */
332 free(client6_data.ia_na);
333 len = requested_ipv6 ? 2+2+4+4+4 + 2+2+16+4+4 : 2+2+4+4+4;
334 client6_data.ia_na = xzalloc(len);
335 client6_data.ia_na->code = D6_OPT_IA_NA;
336 client6_data.ia_na->len = len - 4;
337 *(uint32_t*)client6_data.ia_na->data = rand(); /* IAID */
338 if (requested_ipv6) {
339 struct d6_option *iaaddr = (void*)(client6_data.ia_na->data + 4+4+4);
340 iaaddr->code = D6_OPT_IAADDR;
341 iaaddr->len = 16+4+4;
342 memcpy(iaaddr->data, requested_ipv6, 16);
344 opt_ptr = d6_store_blob(opt_ptr, client6_data.ia_na, len);
347 * "param req" option according to -O, options specified with -x
349 opt_ptr = add_d6_client_options(opt_ptr);
351 bb_info_msg("Sending discover...");
352 return d6_mcast_from_client_config_ifindex(&packet, opt_ptr);
355 /* Multicast a DHCPv6 request message
357 * RFC 3315 18.1.1. Creation and Transmission of Request Messages
359 * The client uses a Request message to populate IAs with addresses and
360 * obtain other configuration information. The client includes one or
361 * more IA options in the Request message. The server then returns
362 * addresses and other information about the IAs to the client in IA
363 * options in a Reply message.
365 * The client generates a transaction ID and inserts this value in the
366 * "transaction-id" field.
368 * The client places the identifier of the destination server in a
369 * Server Identifier option.
371 * The client MUST include a Client Identifier option to identify itself
372 * to the server. The client adds any other appropriate options,
373 * including one or more IA options (if the client is requesting that
374 * the server assign it some network addresses).
376 * The client MUST include an Option Request option (see section 22.7)
377 * to indicate the options the client is interested in receiving. The
378 * client MAY include options with data values as hints to the server
379 * about parameter values the client would like to have returned.
381 * The client includes a Reconfigure Accept option (see section 22.20)
382 * indicating whether or not the client is willing to accept Reconfigure
383 * messages from the server.
385 /* NOINLINE: limit stack usage in caller */
386 static NOINLINE int send_d6_select(uint32_t xid)
388 struct d6_packet packet;
391 /* Fill in: msg type, client id */
392 opt_ptr = init_d6_packet(&packet, D6_MSG_REQUEST, xid);
395 opt_ptr = d6_store_blob(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
396 /* IA NA (contains requested IP) */
397 opt_ptr = d6_store_blob(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
400 * "param req" option according to -O, options specified with -x
402 opt_ptr = add_d6_client_options(opt_ptr);
404 bb_info_msg("Sending select...");
405 return d6_mcast_from_client_config_ifindex(&packet, opt_ptr);
408 /* Unicast or broadcast a DHCP renew message
410 * RFC 3315 18.1.3. Creation and Transmission of Renew Messages
412 * To extend the valid and preferred lifetimes for the addresses
413 * associated with an IA, the client sends a Renew message to the server
414 * from which the client obtained the addresses in the IA containing an
415 * IA option for the IA. The client includes IA Address options in the
416 * IA option for the addresses associated with the IA. The server
417 * determines new lifetimes for the addresses in the IA according to the
418 * administrative configuration of the server. The server may also add
419 * new addresses to the IA. The server may remove addresses from the IA
420 * by setting the preferred and valid lifetimes of those addresses to
423 * The server controls the time at which the client contacts the server
424 * to extend the lifetimes on assigned addresses through the T1 and T2
425 * parameters assigned to an IA.
427 * At time T1 for an IA, the client initiates a Renew/Reply message
428 * exchange to extend the lifetimes on any addresses in the IA. The
429 * client includes an IA option with all addresses currently assigned to
430 * the IA in its Renew message.
432 * If T1 or T2 is set to 0 by the server (for an IA_NA) or there are no
433 * T1 or T2 times (for an IA_TA), the client may send a Renew or Rebind
434 * message, respectively, at the client's discretion.
436 * The client sets the "msg-type" field to RENEW. The client generates
437 * a transaction ID and inserts this value in the "transaction-id"
440 * The client places the identifier of the destination server in a
441 * Server Identifier option.
443 * The client MUST include a Client Identifier option to identify itself
444 * to the server. The client adds any appropriate options, including
445 * one or more IA options. The client MUST include the list of
446 * addresses the client currently has associated with the IAs in the
449 * The client MUST include an Option Request option (see section 22.7)
450 * to indicate the options the client is interested in receiving. The
451 * client MAY include options with data values as hints to the server
452 * about parameter values the client would like to have returned.
454 /* NOINLINE: limit stack usage in caller */
455 static NOINLINE int send_d6_renew(uint32_t xid, struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
457 struct d6_packet packet;
460 /* Fill in: msg type, client id */
461 opt_ptr = init_d6_packet(&packet, DHCPREQUEST, xid);
464 opt_ptr = d6_store_blob(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
465 /* IA NA (contains requested IP) */
466 opt_ptr = d6_store_blob(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
469 * "param req" option according to -O, options specified with -x
471 opt_ptr = add_d6_client_options(opt_ptr);
473 bb_info_msg("Sending renew...");
475 return d6_send_kernel_packet(
476 &packet, (opt_ptr - (uint8_t*) &packet),
477 our_cur_ipv6, CLIENT_PORT,
478 server_ipv6, SERVER_PORT
480 return d6_mcast_from_client_config_ifindex(&packet, opt_ptr);
483 /* Unicast a DHCP release message */
484 static int send_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
486 struct d6_packet packet;
489 /* Fill in: msg type, client id */
490 opt_ptr = init_d6_packet(&packet, D6_MSG_RELEASE, random_xid());
492 opt_ptr = d6_store_blob(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
493 /* IA NA (contains our current IP) */
494 opt_ptr = d6_store_blob(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
496 bb_info_msg("Sending release...");
497 return d6_send_kernel_packet(
498 &packet, (opt_ptr - (uint8_t*) &packet),
499 our_cur_ipv6, CLIENT_PORT,
500 server_ipv6, SERVER_PORT
504 /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
505 /* NOINLINE: limit stack usage in caller */
506 static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6
508 , struct d6_packet *d6_pkt, int fd)
511 struct ip6_udp_d6_packet packet;
513 bytes = safe_read(fd, &packet, sizeof(packet));
515 log1("Packet read error, ignoring");
516 /* NB: possible down interface, etc. Caller should pause. */
517 return bytes; /* returns -1 */
520 if (bytes < (int) (sizeof(packet.ip6) + sizeof(packet.udp))) {
521 log1("Packet is too short, ignoring");
525 if (bytes < sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen)) {
526 /* packet is bigger than sizeof(packet), we did partial read */
527 log1("Oversized packet, ignoring");
531 /* ignore any extra garbage bytes */
532 bytes = sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen);
534 /* make sure its the right packet for us, and that it passes sanity checks */
535 if (packet.ip6.ip6_nxt != IPPROTO_UDP
536 || (packet.ip6.ip6_vfc >> 4) != 6
537 || packet.udp.dest != htons(CLIENT_PORT)
538 /* || bytes > (int) sizeof(packet) - can't happen */
539 || packet.udp.len != packet.ip6.ip6_plen
541 log1("Unrelated/bogus packet, ignoring");
545 //How to do this for ipv6?
546 // /* verify UDP checksum. IP header has to be modified for this */
547 // memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
548 // /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
549 // packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
550 // check = packet.udp.check;
551 // packet.udp.check = 0;
552 // if (check && check != inet_cksum((uint16_t *)&packet, bytes)) {
553 // log1("Packet with bad UDP checksum received, ignoring");
557 log1("Received a packet");
558 d6_dump_packet(&packet.data);
560 bytes -= sizeof(packet.ip6) + sizeof(packet.udp);
561 memcpy(d6_pkt, &packet.data, bytes);
568 static int sockfd = -1;
570 #define LISTEN_NONE 0
571 #define LISTEN_KERNEL 1
573 static smallint listen_mode;
575 /* initial state: (re)start DHCP negotiation */
576 #define INIT_SELECTING 0
577 /* discover was sent, DHCPOFFER reply received */
579 /* select/renew was sent, DHCPACK reply received */
581 /* half of lease passed, want to renew it by sending unicast renew requests */
583 /* renew requests were not answered, lease is almost over, send broadcast renew */
585 /* manually requested renew (SIGUSR1) */
586 #define RENEW_REQUESTED 5
587 /* release, possibly manually requested (SIGUSR2) */
589 static smallint state;
591 static int d6_raw_socket(int ifindex)
594 struct sockaddr_ll sock;
599 * I've selected not to see LL header, so BPF doesn't see it, too.
600 * The filter may also pass non-IP and non-ARP packets, but we do
601 * a more complete check when receiving the message in userspace.
603 * and filter shamelessly stolen from:
605 * http://www.flamewarmaster.de/software/dhcpclient/
607 * There are a few other interesting ideas on that page (look under
608 * "Motivation"). Use of netlink events is most interesting. Think
609 * of various network servers listening for events and reconfiguring.
610 * That would obsolete sending HUP signals and/or make use of restarts.
612 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
615 * TODO: make conditional?
618 static const struct sock_filter filter_instr[] = {
619 /* load 9th byte (protocol) */
620 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
621 /* jump to L1 if it is IPPROTO_UDP, else to L4 */
622 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6),
623 /* L1: load halfword from offset 6 (flags and frag offset) */
624 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
625 /* jump to L4 if any bits in frag offset field are set, else to L2 */
626 BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0),
627 /* L2: skip IP header (load index reg with header len) */
628 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0),
629 /* load udp destination port from halfword[header_len + 2] */
630 BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2),
631 /* jump to L3 if udp dport is CLIENT_PORT, else to L4 */
632 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1),
633 /* L3: accept packet */
634 BPF_STMT(BPF_RET|BPF_K, 0xffffffff),
635 /* L4: discard packet */
636 BPF_STMT(BPF_RET|BPF_K, 0),
638 static const struct sock_fprog filter_prog = {
639 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
640 /* casting const away: */
641 .filter = (struct sock_filter *) filter_instr,
645 log1("Opening raw socket on ifindex %d", ifindex); //log2?
647 fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
648 log1("Got raw socket fd %d", fd); //log2?
650 sock.sll_family = AF_PACKET;
651 sock.sll_protocol = htons(ETH_P_IPV6);
652 sock.sll_ifindex = ifindex;
653 xbind(fd, (struct sockaddr *) &sock, sizeof(sock));
656 if (CLIENT_PORT == 68) {
657 /* Use only if standard port is in use */
658 /* Ignoring error (kernel may lack support for this) */
659 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
660 sizeof(filter_prog)) >= 0)
661 log1("Attached filter to raw socket fd %d", fd); // log?
665 log1("Created raw socket");
670 static void change_listen_mode(int new_mode)
672 log1("Entering listen mode: %s",
673 new_mode != LISTEN_NONE
674 ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw")
678 listen_mode = new_mode;
683 if (new_mode == LISTEN_KERNEL)
684 sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
685 else if (new_mode != LISTEN_NONE)
686 sockfd = d6_raw_socket(client_config.ifindex);
687 /* else LISTEN_NONE: sockfd stays closed */
690 /* Called only on SIGUSR1 */
691 static void perform_renew(void)
693 bb_info_msg("Performing a DHCP renew");
696 change_listen_mode(LISTEN_KERNEL);
699 state = RENEW_REQUESTED;
701 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
702 d6_run_script(NULL, "deconfig");
705 change_listen_mode(LISTEN_RAW);
706 state = INIT_SELECTING;
713 static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
715 /* send release packet */
716 if (state == BOUND || state == RENEWING || state == REBINDING) {
717 bb_info_msg("Unicasting a release");
718 send_d6_release(server_ipv6, our_cur_ipv6); /* unicast */
719 d6_run_script(NULL, "deconfig");
721 bb_info_msg("Entering released state");
723 change_listen_mode(LISTEN_NONE);
727 ///static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
729 /// uint8_t *storage;
730 /// int len = strnlen(str, 255);
731 /// storage = xzalloc(len + extra + OPT_DATA);
732 /// storage[OPT_CODE] = code;
733 /// storage[OPT_LEN] = len + extra;
734 /// memcpy(storage + extra + OPT_DATA, str, len);
739 static void client_background(void)
742 logmode &= ~LOGMODE_STDIO;
743 /* rewrite pidfile, as our pid is different now */
744 write_pidfile(client_config.pidfile);
748 //usage:#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
749 //usage:# define IF_UDHCP_VERBOSE(...) __VA_ARGS__
751 //usage:# define IF_UDHCP_VERBOSE(...)
753 //usage:#define udhcpc6_trivial_usage
754 //usage: "[-fbnq"IF_UDHCP_VERBOSE("v")"oRB] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]\n"
755 //usage: " [-x OPT:VAL]... [-O OPT]..." IF_FEATURE_UDHCP_PORT(" [-P N]")
756 //usage:#define udhcpc6_full_usage "\n"
757 //usage: IF_LONG_OPTS(
758 //usage: "\n -i,--interface IFACE Interface to use (default eth0)"
759 //usage: "\n -p,--pidfile FILE Create pidfile"
760 //usage: "\n -s,--script PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")"
761 //usage: "\n -B,--broadcast Request broadcast replies"
762 //usage: "\n -t,--retries N Send up to N discover packets"
763 //usage: "\n -T,--timeout N Pause between packets (default 3 seconds)"
764 //usage: "\n -A,--tryagain N Wait N seconds after failure (default 20)"
765 //usage: "\n -f,--foreground Run in foreground"
766 //usage: USE_FOR_MMU(
767 //usage: "\n -b,--background Background if lease is not obtained"
769 //usage: "\n -n,--now Exit if lease is not obtained"
770 //usage: "\n -q,--quit Exit after obtaining lease"
771 //usage: "\n -R,--release Release IP on exit"
772 //usage: "\n -S,--syslog Log to syslog too"
773 //usage: IF_FEATURE_UDHCP_PORT(
774 //usage: "\n -P,--client-port N Use port N (default 68)"
776 ////usage: IF_FEATURE_UDHCPC_ARPING(
777 ////usage: "\n -a,--arping Use arping to validate offered address"
779 //usage: "\n -O,--request-option OPT Request option OPT from server (cumulative)"
780 //usage: "\n -o,--no-default-options Don't request any options (unless -O is given)"
781 //usage: "\n -r,--request IP Request this IP address"
782 //usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)"
783 //usage: "\n Examples of string, numeric, and hex byte opts:"
784 //usage: "\n -x hostname:bbox - option 12"
785 //usage: "\n -x lease:3600 - option 51 (lease time)"
786 //usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)"
787 //usage: IF_UDHCP_VERBOSE(
788 //usage: "\n -v Verbose"
791 //usage: IF_NOT_LONG_OPTS(
792 //usage: "\n -i IFACE Interface to use (default eth0)"
793 //usage: "\n -p FILE Create pidfile"
794 //usage: "\n -s PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")"
795 //usage: "\n -B Request broadcast replies"
796 //usage: "\n -t N Send up to N discover packets"
797 //usage: "\n -T N Pause between packets (default 3 seconds)"
798 //usage: "\n -A N Wait N seconds (default 20) after failure"
799 //usage: "\n -f Run in foreground"
800 //usage: USE_FOR_MMU(
801 //usage: "\n -b Background if lease is not obtained"
803 //usage: "\n -n Exit if lease is not obtained"
804 //usage: "\n -q Exit after obtaining lease"
805 //usage: "\n -R Release IP on exit"
806 //usage: "\n -S Log to syslog too"
807 //usage: IF_FEATURE_UDHCP_PORT(
808 //usage: "\n -P N Use port N (default 68)"
810 ////usage: IF_FEATURE_UDHCPC_ARPING(
811 ////usage: "\n -a Use arping to validate offered address"
813 //usage: "\n -O OPT Request option OPT from server (cumulative)"
814 //usage: "\n -o Don't request any options (unless -O is given)"
815 //usage: "\n -r IP Request this IP address"
816 //usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)"
817 //usage: "\n Examples of string, numeric, and hex byte opts:"
818 //usage: "\n -x hostname:bbox - option 12"
819 //usage: "\n -x lease:3600 - option 51 (lease time)"
820 //usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)"
821 //usage: IF_UDHCP_VERBOSE(
822 //usage: "\n -v Verbose"
825 //usage: "\nSignals:"
826 //usage: "\n USR1 Renew lease"
827 //usage: "\n USR2 Release lease"
830 int udhcpc6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
831 int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
834 IF_FEATURE_UDHCP_PORT(char *str_P;)
835 void *clientid_mac_ptr;
836 llist_t *list_O = NULL;
837 llist_t *list_x = NULL;
838 int tryagain_timeout = 20;
839 int discover_timeout = 3;
840 int discover_retries = 3;
841 struct in6_addr srv6_buf;
842 struct in6_addr ipv6_buf;
843 struct in6_addr *requested_ipv6;
846 int timeout; /* must be signed */
847 unsigned already_waited_sec;
853 /* Default options */
854 IF_FEATURE_UDHCP_PORT(SERVER_PORT = 547;)
855 IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 546;)
856 client_config.interface = "eth0";
857 client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT;
859 /* Parse command line */
860 /* O,x: list; -T,-t,-A take numeric param */
861 opt_complementary = "O::x::T+:t+:A+" IF_UDHCP_VERBOSE(":vv") ;
862 IF_LONG_OPTS(applet_long_options = udhcpc_longopts;)
863 opt = getopt32(argv, "i:np:qRr:s:T:t:SA:O:ox:fB"
865 ///IF_FEATURE_UDHCPC_ARPING("a")
866 IF_FEATURE_UDHCP_PORT("P:")
868 , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
869 , &client_config.script /* s */
870 , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
873 IF_FEATURE_UDHCP_PORT(, &str_P)
874 IF_UDHCP_VERBOSE(, &dhcp_verbose)
876 requested_ipv6 = NULL;
878 if (inet_pton(AF_INET6, str_r, &ipv6_buf) <= 0)
879 bb_error_msg_and_die("bad IPv6 address '%s'", str_r);
880 requested_ipv6 = &ipv6_buf;
882 #if ENABLE_FEATURE_UDHCP_PORT
884 CLIENT_PORT = xatou16(str_P);
885 SERVER_PORT = CLIENT_PORT - 1;
889 client_config.no_default_options = 1;
891 char *optstr = llist_pop(&list_O);
892 unsigned n = bb_strtou(optstr, NULL, 0);
893 if (errno || n > 254) {
894 n = udhcp_option_idx(optstr);
895 n = dhcp_optflags[n].code;
897 client_config.opt_mask[n >> 3] |= 1 << (n & 7);
900 char *optstr = llist_pop(&list_x);
901 char *colon = strchr(optstr, ':');
904 /* now it looks similar to udhcpd's config file line:
905 * "optname optval", using the common routine: */
906 udhcp_str2optset(optstr, &client_config.options);
909 if (udhcp_read_interface(client_config.interface,
910 &client_config.ifindex,
912 client_config.client_mac)
917 /* Create client ID based on mac, set clientid_mac_ptr */
919 struct d6_option *clientid;
920 clientid = xzalloc(2+2+2+2+6);
921 clientid->code = D6_OPT_CLIENTID;
922 clientid->len = 2+2+6;
923 clientid->data[0] = 3; /* DUID-LL */
924 clientid->data[2] = 1; /* ethernet */
925 clientid_mac_ptr = clientid->data + 2+2;
926 memcpy(clientid_mac_ptr, client_config.client_mac, 6);
927 client_config.clientid = (void*)clientid;
931 /* on NOMMU reexec (i.e., background) early */
932 if (!(opt & OPT_f)) {
933 bb_daemonize_or_rexec(0 /* flags */, argv);
934 logmode = LOGMODE_NONE;
938 openlog(applet_name, LOG_PID, LOG_DAEMON);
939 logmode |= LOGMODE_SYSLOG;
942 /* Make sure fd 0,1,2 are open */
944 /* Equivalent of doing a fflush after every \n */
947 write_pidfile(client_config.pidfile);
948 /* Goes to stdout (unless NOMMU) and possibly syslog */
949 bb_info_msg("%s (v"BB_VER") started", applet_name);
950 /* Set up the signal pipe */
952 /* We want random_xid to be random... */
953 srand(monotonic_us());
955 state = INIT_SELECTING;
956 d6_run_script(NULL, "deconfig");
957 change_listen_mode(LISTEN_RAW);
960 already_waited_sec = 0;
962 /* Main event loop. select() waits on signal pipe and possibly
964 * "continue" statements in code below jump to the top of the loop.
968 struct d6_packet packet;
970 /* silence "uninitialized!" warning */
971 unsigned timestamp_before_wait = timestamp_before_wait;
973 //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode);
975 /* Was opening raw or udp socket here
976 * if (listen_mode != LISTEN_NONE && sockfd < 0),
977 * but on fast network renew responses return faster
978 * than we open sockets. Thus this code is moved
979 * to change_listen_mode(). Thus we open listen socket
980 * BEFORE we send renew request (see "case BOUND:"). */
982 max_fd = udhcp_sp_fd_set(&rfds, sockfd);
984 tv.tv_sec = timeout - already_waited_sec;
987 /* If we already timed out, fall through with retval = 0, else... */
988 if ((int)tv.tv_sec > 0) {
989 timestamp_before_wait = (unsigned)monotonic_sec();
990 log1("Waiting on select...");
991 retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
993 /* EINTR? A signal was caught, don't panic */
994 if (errno == EINTR) {
995 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
998 /* Else: an error occured, panic! */
999 bb_perror_msg_and_die("select");
1003 /* If timeout dropped to zero, time to become active:
1004 * resend discover/renew/whatever
1007 /* When running on a bridge, the ifindex may have changed
1008 * (e.g. if member interfaces were added/removed
1009 * or if the status of the bridge changed).
1010 * Refresh ifindex and client_mac:
1012 if (udhcp_read_interface(client_config.interface,
1013 &client_config.ifindex,
1015 client_config.client_mac)
1017 goto ret0; /* iface is gone? */
1019 memcpy(clientid_mac_ptr, client_config.client_mac, 6);
1021 /* We will restart the wait in any case */
1022 already_waited_sec = 0;
1025 case INIT_SELECTING:
1026 if (packet_num < discover_retries) {
1027 if (packet_num == 0)
1030 send_d6_discover(xid, requested_ipv6);
1031 timeout = discover_timeout;
1036 d6_run_script(NULL, "leasefail");
1037 #if BB_MMU /* -b is not supported on NOMMU */
1038 if (opt & OPT_b) { /* background if no lease */
1039 bb_info_msg("No lease, forking to background");
1040 client_background();
1041 /* do not background again! */
1042 opt = ((opt & ~OPT_b) | OPT_f);
1045 if (opt & OPT_n) { /* abort if no lease */
1046 bb_info_msg("No lease, failing");
1050 /* wait before trying again */
1051 timeout = tryagain_timeout;
1055 if (packet_num < discover_retries) {
1056 /* send multicast select packet */
1057 send_d6_select(xid);
1058 timeout = discover_timeout;
1062 /* Timed out, go back to init state.
1063 * "discover...select...discover..." loops
1064 * were seen in the wild. Treat them similarly
1065 * to "no response to discover" case */
1066 change_listen_mode(LISTEN_RAW);
1067 state = INIT_SELECTING;
1070 /* 1/2 lease passed, enter renewing state */
1072 client_config.first_secs = 0; /* make secs field count from 0 */
1073 change_listen_mode(LISTEN_KERNEL);
1074 log1("Entering renew state");
1075 /* fall right through */
1076 case RENEW_REQUESTED: /* manual (SIGUSR1) renew */
1077 case_RENEW_REQUESTED:
1080 /* send an unicast renew request */
1081 /* Sometimes observed to fail (EADDRNOTAVAIL) to bind
1082 * a new UDP socket for sending inside send_renew.
1083 * I hazard to guess existing listening socket
1084 * is somehow conflicting with it, but why is it
1085 * not deterministic then?! Strange.
1086 * Anyway, it does recover by eventually failing through
1087 * into INIT_SELECTING state.
1089 send_d6_renew(xid, &srv6_buf, requested_ipv6);
1093 /* Timed out, enter rebinding state */
1094 log1("Entering rebinding state");
1096 /* fall right through */
1098 /* Switch to bcast receive */
1099 change_listen_mode(LISTEN_RAW);
1100 /* Lease is *really* about to run out,
1101 * try to find DHCP server using broadcast */
1103 /* send a broadcast renew request */
1104 send_d6_renew(xid, /*server_ipv6:*/ NULL, requested_ipv6);
1108 /* Timed out, enter init state */
1109 bb_info_msg("Lease lost, entering init state");
1110 d6_run_script(NULL, "deconfig");
1111 state = INIT_SELECTING;
1112 client_config.first_secs = 0; /* make secs field count from 0 */
1113 /*timeout = 0; - already is */
1116 /* case RELEASED: */
1118 /* yah, I know, *you* say it would never happen */
1120 continue; /* back to main loop */
1121 } /* if select timed out */
1123 /* select() didn't timeout, something happened */
1125 /* Is it a signal? */
1126 /* note: udhcp_sp_read checks FD_ISSET before reading */
1127 switch (udhcp_sp_read(&rfds)) {
1129 client_config.first_secs = 0; /* make secs field count from 0 */
1130 already_waited_sec = 0;
1132 if (state == RENEW_REQUESTED) {
1133 /* We might be either on the same network
1134 * (in which case renew might work),
1135 * or we might be on a completely different one
1136 * (in which case renew won't ever succeed).
1137 * For the second case, must make sure timeout
1138 * is not too big, or else we can send
1139 * futile renew requests for hours.
1140 * (Ab)use -A TIMEOUT value (usually 20 sec)
1141 * as a cap on the timeout.
1143 if (timeout > tryagain_timeout)
1144 timeout = tryagain_timeout;
1145 goto case_RENEW_REQUESTED;
1147 /* Start things over */
1149 /* Kill any timeouts, user wants this to hurry along */
1153 perform_d6_release(&srv6_buf, requested_ipv6);
1157 bb_info_msg("Received SIGTERM");
1161 /* Is it a packet? */
1162 if (listen_mode == LISTEN_NONE || !FD_ISSET(sockfd, &rfds))
1168 /* A packet is ready, read it */
1169 if (listen_mode == LISTEN_KERNEL)
1170 len = d6_recv_kernel_packet(&srv6_buf, &packet, sockfd);
1172 len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd);
1174 /* Error is severe, reopen socket */
1175 bb_info_msg("Read error: %s, reopening socket", strerror(errno));
1176 sleep(discover_timeout); /* 3 seconds by default */
1177 change_listen_mode(listen_mode); /* just close and reopen */
1179 /* If this packet will turn out to be unrelated/bogus,
1180 * we will go back and wait for next one.
1181 * Be sure timeout is properly decreased. */
1182 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
1185 packet_end = (uint8_t*)&packet + len;
1188 if ((packet.d6_xid32 & htonl(0x00ffffff)) != xid) {
1189 log1("xid %x (our is %x), ignoring packet",
1190 (unsigned)(packet.d6_xid32 & htonl(0x00ffffff)), (unsigned)xid);
1195 case INIT_SELECTING:
1196 if (packet.d6_msg_type == D6_MSG_ADVERTISE)
1198 /* DHCPv6 has "Rapid Commit", when instead of Advertise,
1199 * server sends Reply right away.
1200 * Fall through to check for this case.
1204 case RENEW_REQUESTED:
1206 if (packet.d6_msg_type == D6_MSG_REPLY) {
1207 uint32_t lease_seconds;
1208 struct d6_option *option, *iaaddr;
1210 option = d6_find_option(packet.d6_options, packet_end, D6_OPT_STATUS_CODE);
1211 if (option && option->data[4] != 0) {
1212 /* return to init state */
1213 bb_info_msg("Received DHCP NAK (%u)", option->data[4]);
1214 d6_run_script(&packet, "nak");
1215 if (state != REQUESTING)
1216 d6_run_script(NULL, "deconfig");
1217 change_listen_mode(LISTEN_RAW);
1218 sleep(3); /* avoid excessive network traffic */
1219 state = INIT_SELECTING;
1220 client_config.first_secs = 0; /* make secs field count from 0 */
1221 requested_ipv6 = NULL;
1224 already_waited_sec = 0;
1227 option = d6_copy_option(packet.d6_options, packet_end, D6_OPT_SERVERID);
1229 bb_error_msg("no server ID, ignoring packet");
1231 /* still selecting - this server looks bad */
1233 //Note: we do not bother comparing server IDs in Advertise and Reply msgs.
1234 //server_id variable is used solely for creation of proper server_id option
1235 //in outgoing packets. (why DHCPv6 even introduced it is a mystery).
1236 free(client6_data.server_id);
1237 client6_data.server_id = option;
1238 if (packet.d6_msg_type == D6_MSG_ADVERTISE) {
1239 /* enter requesting state */
1243 already_waited_sec = 0;
1246 /* It's a D6_MSG_REPLY */
1248 * RFC 3315 18.1.8. Receipt of Reply Messages
1250 * Upon the receipt of a valid Reply message in response to a Solicit
1251 * (with a Rapid Commit option), Request, Confirm, Renew, Rebind or
1252 * Information-request message, the client extracts the configuration
1253 * information contained in the Reply. The client MAY choose to report
1254 * any status code or message from the status code option in the Reply
1257 * The client SHOULD perform duplicate address detection [17] on each of
1258 * the addresses in any IAs it receives in the Reply message before
1259 * using that address for traffic. If any of the addresses are found to
1260 * be in use on the link, the client sends a Decline message to the
1261 * server as described in section 18.1.7.
1263 * If the Reply was received in response to a Solicit (with a Rapid
1264 * Commit option), Request, Renew or Rebind message, the client updates
1265 * the information it has recorded about IAs from the IA options
1266 * contained in the Reply message:
1268 * - Record T1 and T2 times.
1270 * - Add any new addresses in the IA option to the IA as recorded by
1273 * - Update lifetimes for any addresses in the IA option that the
1274 * client already has recorded in the IA.
1276 * - Discard any addresses from the IA, as recorded by the client, that
1277 * have a valid lifetime of 0 in the IA Address option.
1279 * - Leave unchanged any information about addresses the client has
1280 * recorded in the IA but that were not included in the IA from the
1283 * Management of the specific configuration information is detailed in
1284 * the definition of each option in section 22.
1286 * If the client receives a Reply message with a Status Code containing
1287 * UnspecFail, the server is indicating that it was unable to process
1288 * the message due to an unspecified failure condition. If the client
1289 * retransmits the original message to the same server to retry the
1290 * desired operation, the client MUST limit the rate at which it
1291 * retransmits the message and limit the duration of the time during
1292 * which it retransmits the message.
1294 * When the client receives a Reply message with a Status Code option
1295 * with the value UseMulticast, the client records the receipt of the
1296 * message and sends subsequent messages to the server through the
1297 * interface on which the message was received using multicast. The
1298 * client resends the original message using multicast.
1300 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1301 * | OPTION_IA_NA | option-len |
1302 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1303 * | IAID (4 octets) |
1304 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1306 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1308 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1312 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1315 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1316 * | OPTION_IAADDR | option-len |
1317 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1322 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1323 * | preferred-lifetime |
1324 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1325 * | valid-lifetime |
1326 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1328 * . IAaddr-options .
1330 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1332 free(client6_data.ia_na);
1333 client6_data.ia_na = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_NA);
1334 if (!client6_data.ia_na) {
1335 bb_error_msg("no lease time, ignoring packet");
1338 if (client6_data.ia_na->len < (4 + 4 + 4) + (2 + 2 + 16 + 4 + 4)) {
1339 bb_error_msg("IA_NA option is too short:%d bytes", client6_data.ia_na->len);
1342 iaaddr = d6_find_option(client6_data.ia_na->data,
1343 client6_data.ia_na->data + client6_data.ia_na->len,
1347 bb_error_msg("no lease time, ignoring packet");
1350 if (iaaddr->len < (16 + 4 + 4)) {
1351 bb_error_msg("IAADDR option is too short:%d bytes", iaaddr->len);
1354 /* Note: the address is sufficiently aligned for cast:
1355 * we _copied_ IA-NA, and copy is always well-aligned.
1357 requested_ipv6 = (struct in6_addr*) iaaddr->data;
1358 move_from_unaligned32(lease_seconds, iaaddr->data + 16 + 4);
1359 lease_seconds = ntohl(lease_seconds);
1360 /* paranoia: must not be too small and not prone to overflows */
1361 if (lease_seconds < 0x10)
1362 lease_seconds = 0x10;
1363 /// TODO: check for 0 lease time?
1364 if (lease_seconds >= 0x10000000)
1365 lease_seconds = 0x0fffffff;
1366 /* enter bound state */
1367 timeout = lease_seconds / 2;
1368 bb_info_msg("Lease obtained, lease time %u",
1369 /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds);
1370 d6_run_script(&packet, state == REQUESTING ? "bound" : "renew");
1373 change_listen_mode(LISTEN_NONE);
1374 if (opt & OPT_q) { /* quit after lease */
1377 /* future renew failures should not exit (JM) */
1379 #if BB_MMU /* NOMMU case backgrounded earlier */
1380 if (!(opt & OPT_f)) {
1381 client_background();
1382 /* do not background again! */
1383 opt = ((opt & ~OPT_b) | OPT_f);
1386 already_waited_sec = 0;
1387 continue; /* back to main loop */
1390 /* case BOUND: - ignore all packets */
1391 /* case RELEASED: - ignore all packets */
1393 /* back to main loop */
1394 } /* for (;;) - main loop ends */
1397 if (opt & OPT_R) /* release on quit */
1398 perform_d6_release(&srv6_buf, requested_ipv6);
1401 /*if (client_config.pidfile) - remove_pidfile has its own check */
1402 remove_pidfile(client_config.pidfile);