3 * DHCP client library with GLib integration
5 * Copyright (C) 2009-2014 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
33 #include <arpa/inet.h>
37 #include <netpacket/packet.h>
38 #include <netinet/if_ether.h>
39 #include <net/ethernet.h>
42 #include <linux/filter.h>
50 #define DISCOVER_TIMEOUT 5
51 #define DISCOVER_RETRIES 6
53 #define REQUEST_TIMEOUT 5
54 #define REQUEST_RETRIES 3
56 typedef enum _listen_mode {
63 typedef enum _dhcp_client_state {
91 uint8_t mac_address[6];
94 uint32_t requested_ip;
97 uint32_t lease_seconds;
98 ListenMode listen_mode;
101 uint8_t ack_retry_times;
107 guint listener_watch;
110 GHashTable *code_value_hash;
111 GHashTable *send_value_hash;
112 GDHCPClientEventFunc lease_available_cb;
113 gpointer lease_available_data;
114 GDHCPClientEventFunc ipv4ll_available_cb;
115 gpointer ipv4ll_available_data;
116 GDHCPClientEventFunc no_lease_cb;
117 gpointer no_lease_data;
118 GDHCPClientEventFunc lease_lost_cb;
119 gpointer lease_lost_data;
120 GDHCPClientEventFunc ipv4ll_lost_cb;
121 gpointer ipv4ll_lost_data;
122 GDHCPClientEventFunc address_conflict_cb;
123 gpointer address_conflict_data;
124 GDHCPDebugFunc debug_func;
126 GDHCPClientEventFunc information_req_cb;
127 gpointer information_req_data;
128 GDHCPClientEventFunc solicitation_cb;
129 gpointer solicitation_data;
130 GDHCPClientEventFunc advertise_cb;
131 gpointer advertise_data;
132 GDHCPClientEventFunc request_cb;
133 gpointer request_data;
134 GDHCPClientEventFunc renew_cb;
136 GDHCPClientEventFunc rebind_cb;
137 gpointer rebind_data;
138 GDHCPClientEventFunc release_cb;
139 gpointer release_data;
140 GDHCPClientEventFunc confirm_cb;
141 gpointer confirm_data;
142 GDHCPClientEventFunc decline_cb;
143 gpointer decline_data;
147 unsigned char *server_duid;
149 uint16_t status_code;
152 struct in6_addr ia_na;
153 struct in6_addr ia_ta;
157 struct timeval start_time;
161 static inline void debug(GDHCPClient *client, const char *format, ...)
166 if (!client->debug_func)
169 va_start(ap, format);
171 if (vsnprintf(str, sizeof(str), format, ap) > 0)
172 client->debug_func(str, client->debug_data);
177 /* Initialize the packet with the proper defaults */
178 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
180 if (dhcp_client->type == G_DHCP_IPV6)
181 dhcpv6_init_header(pkt, type);
183 struct dhcp_packet *packet = pkt;
185 dhcp_init_header(packet, type);
186 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
190 static void add_request_options(GDHCPClient *dhcp_client,
191 struct dhcp_packet *packet)
196 int end = dhcp_end_option(packet->options);
198 for (list = dhcp_client->request_list; list; list = list->next) {
199 code = (uint8_t) GPOINTER_TO_INT(list->data);
201 packet->options[end + OPT_DATA + len] = code;
206 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
207 packet->options[end + OPT_LEN] = len;
208 packet->options[end + OPT_DATA + len] = DHCP_END;
215 unsigned char **ptr_buf;
218 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
221 uint8_t *option = value;
223 struct hash_params *params = user_data;
225 /* option[0][1] contains option code */
226 len = option[2] << 8 | option[3];
228 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
231 memcpy(*params->ptr_buf, option, len + 2 + 2);
232 (*params->ptr_buf) += len + 2 + 2;
235 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
236 unsigned char *buf, int max_buf,
237 unsigned char **ptr_buf)
239 struct hash_params params = {
245 if (dhcp_client->type != G_DHCP_IPV6)
248 g_hash_table_foreach(dhcp_client->send_value_hash,
249 add_dhcpv6_binary_option, ¶ms);
251 *ptr_buf = *params.ptr_buf;
254 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
258 buf[1] = code & 0xff;
262 memcpy(&buf[4], msg, len);
265 static int32_t get_time_diff(struct timeval *tv)
270 gettimeofday(&now, NULL);
272 hsec = (now.tv_sec - tv->tv_sec) * 100;
273 hsec += (now.tv_usec - tv->tv_usec) / 10000;
278 static void remove_timeouts(GDHCPClient *dhcp_client)
281 if (dhcp_client->timeout > 0)
282 g_source_remove(dhcp_client->timeout);
283 if (dhcp_client->t1_timeout > 0)
284 g_source_remove(dhcp_client->t1_timeout);
285 if (dhcp_client->t2_timeout > 0)
286 g_source_remove(dhcp_client->t2_timeout);
287 if (dhcp_client->lease_timeout > 0)
288 g_source_remove(dhcp_client->lease_timeout);
290 dhcp_client->timeout = 0;
291 dhcp_client->t1_timeout = 0;
292 dhcp_client->t2_timeout = 0;
293 dhcp_client->lease_timeout = 0;
297 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
298 struct dhcpv6_packet *packet,
299 unsigned char *buf, int max_buf,
300 unsigned char **ptr_buf)
303 uint16_t code, value;
308 if (dhcp_client->type != G_DHCP_IPV6)
311 for (list = dhcp_client->request_list; list; list = list->next) {
312 code = (uint16_t) GPOINTER_TO_INT(list->data);
316 case G_DHCPV6_CLIENTID:
317 if (!dhcp_client->duid)
320 len = 2 + 2 + dhcp_client->duid_len;
321 if ((*ptr_buf + len) > (buf + max_buf)) {
322 debug(dhcp_client, "Too long dhcpv6 message "
323 "when writing client id option");
327 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
328 dhcp_client->duid_len, dhcp_client->duid);
333 case G_DHCPV6_SERVERID:
334 if (!dhcp_client->server_duid)
337 len = 2 + 2 + dhcp_client->server_duid_len;
338 if ((*ptr_buf + len) > (buf + max_buf)) {
339 debug(dhcp_client, "Too long dhcpv6 message "
340 "when writing server id option");
344 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
345 dhcp_client->server_duid_len,
346 dhcp_client->server_duid);
351 case G_DHCPV6_RAPID_COMMIT:
353 if ((*ptr_buf + len) > (buf + max_buf)) {
354 debug(dhcp_client, "Too long dhcpv6 message "
355 "when writing rapid commit option");
359 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
367 case G_DHCPV6_ELAPSED_TIME:
368 if (!dhcp_client->retransmit) {
370 * Initial message, elapsed time is 0.
374 diff = get_time_diff(&dhcp_client->start_time);
375 if (diff < 0 || diff > 0xffff)
380 if ((*ptr_buf + len) > (buf + max_buf)) {
381 debug(dhcp_client, "Too long dhcpv6 message "
382 "when writing elapsed time option");
386 value = htons((uint16_t)diff);
387 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
388 2, (uint8_t *)&value);
393 case G_DHCPV6_DNS_SERVERS:
396 case G_DHCPV6_DOMAIN_LIST:
399 case G_DHCPV6_SNTP_SERVERS:
407 debug(dhcp_client, "option %d len %d added", code, len);
411 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
413 uint8_t *option = value;
414 struct dhcp_packet *packet = user_data;
416 dhcp_add_binary_option(packet, option);
419 static void add_send_options(GDHCPClient *dhcp_client,
420 struct dhcp_packet *packet)
422 g_hash_table_foreach(dhcp_client->send_value_hash,
423 add_binary_option, packet);
427 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
428 * represents the number of seconds elapsed from the start of
429 * attempting DHCP to satisfy some DHCP servers that allow for an
430 * "authoritative" reply before responding.
432 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
434 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
437 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
439 struct dhcp_packet packet;
441 debug(dhcp_client, "sending DHCP discover request");
443 init_packet(dhcp_client, &packet, DHCPDISCOVER);
445 packet.xid = dhcp_client->xid;
446 packet.secs = dhcp_attempt_secs(dhcp_client);
449 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
451 /* Explicitly saying that we want RFC-compliant packets helps
452 * some buggy DHCP servers to NOT send bigger packets */
453 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
455 add_request_options(dhcp_client, &packet);
457 add_send_options(dhcp_client, &packet);
460 * If we do not get a reply to DISCOVER packet, then we try with
461 * broadcast flag set. So first packet is sent without broadcast flag,
462 * first retry is with broadcast flag, second retry is without it etc.
463 * Reason is various buggy routers/AP that either eat the other or vice
464 * versa. In the receiving side we then find out what kind of packet
465 * the server can send.
467 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
468 INADDR_BROADCAST, SERVER_PORT,
469 MAC_BCAST_ADDR, dhcp_client->ifindex,
470 dhcp_client->retry_times % 2);
473 static int send_request(GDHCPClient *dhcp_client)
475 struct dhcp_packet packet;
477 debug(dhcp_client, "sending DHCP request (state %d)",
480 init_packet(dhcp_client, &packet, DHCPREQUEST);
482 packet.xid = dhcp_client->xid;
483 packet.secs = dhcp_attempt_secs(dhcp_client);
485 if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
486 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
487 dhcp_client->requested_ip);
489 if (dhcp_client->state == REQUESTING)
490 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
491 dhcp_client->server_ip);
493 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
495 add_request_options(dhcp_client, &packet);
497 add_send_options(dhcp_client, &packet);
499 if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
500 packet.ciaddr = htonl(dhcp_client->requested_ip);
502 if (dhcp_client->state == RENEWING)
503 return dhcp_send_kernel_packet(&packet,
504 dhcp_client->requested_ip, CLIENT_PORT,
505 dhcp_client->server_ip, SERVER_PORT);
507 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
508 INADDR_BROADCAST, SERVER_PORT,
509 MAC_BCAST_ADDR, dhcp_client->ifindex,
510 dhcp_client->request_bcast);
513 static int send_release(GDHCPClient *dhcp_client,
514 uint32_t server, uint32_t ciaddr)
516 struct dhcp_packet packet;
518 debug(dhcp_client, "sending DHCP release request");
520 init_packet(dhcp_client, &packet, DHCPRELEASE);
522 packet.ciaddr = htonl(ciaddr);
524 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
526 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
527 server, SERVER_PORT);
530 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
531 static int switch_listening_mode(GDHCPClient *dhcp_client,
532 ListenMode listen_mode);
534 static gboolean send_probe_packet(gpointer dhcp_data)
536 GDHCPClient *dhcp_client;
539 dhcp_client = dhcp_data;
540 /* if requested_ip is not valid, pick a new address*/
541 if (dhcp_client->requested_ip == 0) {
542 debug(dhcp_client, "pick a new random address");
543 dhcp_client->requested_ip = ipv4ll_random_ip(0);
546 debug(dhcp_client, "sending IPV4LL probe request");
548 if (dhcp_client->retry_times == 1) {
549 dhcp_client->state = IPV4LL_PROBE;
550 switch_listening_mode(dhcp_client, L_ARP);
552 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
553 dhcp_client->requested_ip, dhcp_client->ifindex);
555 if (dhcp_client->retry_times < PROBE_NUM) {
556 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
557 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
558 timeout += PROBE_MIN*1000;
560 timeout = (ANNOUNCE_WAIT * 1000);
562 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
564 ipv4ll_probe_timeout,
570 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
571 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
573 static gboolean send_announce_packet(gpointer dhcp_data)
575 GDHCPClient *dhcp_client;
577 dhcp_client = dhcp_data;
579 debug(dhcp_client, "sending IPV4LL announce request");
581 ipv4ll_send_arp_packet(dhcp_client->mac_address,
582 dhcp_client->requested_ip,
583 dhcp_client->requested_ip,
584 dhcp_client->ifindex);
586 remove_timeouts(dhcp_client);
588 if (dhcp_client->state == IPV4LL_DEFEND) {
589 dhcp_client->timeout =
590 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
592 ipv4ll_defend_timeout,
597 dhcp_client->timeout =
598 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
600 ipv4ll_announce_timeout,
606 static void get_interface_mac_address(int index, uint8_t *mac_address)
611 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
613 perror("Open socket error");
617 memset(&ifr, 0, sizeof(ifr));
618 ifr.ifr_ifindex = index;
620 err = ioctl(sk, SIOCGIFNAME, &ifr);
622 perror("Get interface name error");
626 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
628 perror("Get mac address error");
632 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
638 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
643 dhcp_client->retransmit = true;
646 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
651 dhcp_client->retransmit = false;
654 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
655 unsigned char **duid, int *duid_len)
660 case G_DHCPV6_DUID_LLT:
661 *duid_len = 2 + 2 + 4 + ETH_ALEN;
662 *duid = g_try_malloc(*duid_len);
668 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
671 duid_time = time(NULL) - DUID_TIME_EPOCH;
672 (*duid)[4] = duid_time >> 24;
673 (*duid)[5] = duid_time >> 16;
674 (*duid)[6] = duid_time >> 8;
675 (*duid)[7] = duid_time & 0xff;
677 case G_DHCPV6_DUID_EN:
679 case G_DHCPV6_DUID_LL:
680 *duid_len = 2 + 2 + ETH_ALEN;
681 *duid = g_try_malloc(*duid_len);
687 get_interface_mac_address(index, &(*duid)[2 + 2]);
696 static gchar *convert_to_hex(unsigned char *buf, int len)
698 gchar *ret = g_try_malloc(len * 2 + 1);
701 for (i = 0; ret && i < len; i++)
702 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
707 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
710 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
713 g_free(dhcp_client->duid);
715 dhcp_client->duid = duid;
716 dhcp_client->duid_len = duid_len;
718 if (dhcp_client->debug_func) {
719 gchar *hex = convert_to_hex(duid, duid_len);
720 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
727 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
728 uint32_t *T2, GSList *prefixes)
730 uint8_t options[1452];
731 unsigned int max_buf = sizeof(options);
732 int len, count = g_slist_length(prefixes);
734 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
737 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
739 memset(options, 0, sizeof(options));
741 options[0] = dhcp_client->iaid >> 24;
742 options[1] = dhcp_client->iaid >> 16;
743 options[2] = dhcp_client->iaid >> 8;
744 options[3] = dhcp_client->iaid;
747 uint32_t t = htonl(*T1);
748 memcpy(&options[4], &t, 4);
752 uint32_t t = htonl(*T2);
753 memcpy(&options[8], &t, 4);
761 for (list = prefixes; list; list = list->next) {
762 GDHCPIAPrefix *prefix = list->data;
763 uint8_t sub_option[4+4+1+16];
765 if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
767 "Too long dhcpv6 message "
768 "when writing IA prefix option");
772 memset(&sub_option, 0, sizeof(sub_option));
774 /* preferred and validity time are left zero */
776 sub_option[8] = prefix->prefixlen;
777 memcpy(&sub_option[9], &prefix->prefix, 16);
779 copy_option(&options[len], G_DHCPV6_IA_PREFIX,
780 sizeof(sub_option), sub_option);
781 len += 2 + 2 + sizeof(sub_option);
785 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
791 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
793 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
796 return dhcp_client->iaid;
799 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
801 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
804 dhcp_client->iaid = iaid;
807 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
812 get_interface_mac_address(index, buf);
814 memcpy(iaid, &buf[2], 4);
815 dhcp_client->iaid = iaid[0] << 24 |
816 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
819 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
820 uint32_t *T1, uint32_t *T2,
824 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
828 *T1 = dhcp_client->T1;
831 *T2 = dhcp_client->T2;
834 *started = dhcp_client->last_request;
837 *expire = dhcp_client->last_request + dhcp_client->expire;
842 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
846 buf[1] = G_DHCPV6_IAADDR;
849 memcpy(&buf[4], &dhcp_client->ia_na, 16);
850 memset(&buf[20], 0, 4); /* preferred */
851 memset(&buf[24], 0, 4); /* valid */
855 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
858 struct in6_addr addr;
860 if (inet_pton(AF_INET6, address, &addr) != 1)
864 buf[1] = G_DHCPV6_IAADDR;
867 memcpy(&buf[4], &addr, 16);
868 memset(&buf[20], 0, 4); /* preferred */
869 memset(&buf[24], 0, 4); /* valid */
873 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
877 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
879 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
889 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
890 int code, uint32_t *T1, uint32_t *T2,
891 bool add_iaaddr, const char *ia_na)
893 if (code == G_DHCPV6_IA_TA) {
894 uint8_t ia_options[4];
896 put_iaid(dhcp_client, index, ia_options);
898 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
899 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
900 ia_options, sizeof(ia_options));
902 } else if (code == G_DHCPV6_IA_NA) {
903 struct in6_addr addr;
905 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
908 * If caller has specified the IPv6 address it wishes to
909 * to use (ia_na != NULL and address is valid), then send
910 * the address to server.
911 * If caller did not specify the address (ia_na == NULL) and
912 * if the current address is not set, then we should not send
913 * the address sub-option.
915 if (add_iaaddr && ((!ia_na &&
916 !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
918 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
919 #define IAADDR_LEN (16+4+4)
920 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
923 memcpy(&dhcp_client->ia_na, &addr,
924 sizeof(struct in6_addr));
926 put_iaid(dhcp_client, index, ia_options);
929 ia_options[4] = *T1 >> 24;
930 ia_options[5] = *T1 >> 16;
931 ia_options[6] = *T1 >> 8;
934 memset(&ia_options[4], 0x00, 4);
937 ia_options[8] = *T2 >> 24;
938 ia_options[9] = *T2 >> 16;
939 ia_options[10] = *T2 >> 8;
940 ia_options[11] = *T2;
942 memset(&ia_options[8], 0x00, 4);
944 create_iaaddr(dhcp_client, &ia_options[12],
947 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
948 ia_options, sizeof(ia_options));
950 uint8_t ia_options[4+4+4];
952 put_iaid(dhcp_client, index, ia_options);
954 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
955 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
957 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
958 ia_options, sizeof(ia_options));
967 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
968 int code, uint32_t *T1, uint32_t *T2,
972 uint8_t *ia_options, *pos;
973 int len, count, total_len;
975 count = g_slist_length(addresses);
979 g_dhcp_client_set_request(dhcp_client, code);
981 if (code == G_DHCPV6_IA_TA)
983 else if (code == G_DHCPV6_IA_NA)
984 len = 4 + 4 + 4; /* IAID + T1 + T2 */
988 total_len = len + count * (2 + 2 + 16 + 4 + 4);
989 ia_options = g_try_malloc0(total_len);
993 put_iaid(dhcp_client, index, ia_options);
995 pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
997 for (list = addresses; list; list = list->next) {
998 pos = append_iaaddr(dhcp_client, pos, list->data);
1003 if (code == G_DHCPV6_IA_NA) {
1005 ia_options[4] = *T1 >> 24;
1006 ia_options[5] = *T1 >> 16;
1007 ia_options[6] = *T1 >> 8;
1008 ia_options[7] = *T1;
1010 memset(&ia_options[4], 0x00, 4);
1013 ia_options[8] = *T2 >> 24;
1014 ia_options[9] = *T2 >> 16;
1015 ia_options[10] = *T2 >> 8;
1016 ia_options[11] = *T2;
1018 memset(&ia_options[8], 0x00, 4);
1021 g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1028 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1031 int i, j, len = sizeof(uint16_t) * args;
1034 values = g_try_malloc(len);
1039 for (i = 0, j = 0; i < args; i++) {
1040 uint16_t value = va_arg(va, int);
1041 values[j++] = value >> 8;
1042 values[j++] = value & 0xff;
1046 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1053 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1055 struct dhcpv6_packet *packet;
1056 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1060 memset(buf, 0, sizeof(buf));
1061 packet = (struct dhcpv6_packet *)&buf[0];
1062 ptr = buf + sizeof(struct dhcpv6_packet);
1064 init_packet(dhcp_client, packet, type);
1066 if (!dhcp_client->retransmit) {
1067 dhcp_client->xid = packet->transaction_id[0] << 16 |
1068 packet->transaction_id[1] << 8 |
1069 packet->transaction_id[2];
1070 gettimeofday(&dhcp_client->start_time, NULL);
1072 packet->transaction_id[0] = dhcp_client->xid >> 16;
1073 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1074 packet->transaction_id[2] = dhcp_client->xid;
1077 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1079 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1082 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1084 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1086 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1088 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1090 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1094 static int send_solicitation(GDHCPClient *dhcp_client)
1096 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1099 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1101 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1104 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1106 return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1109 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1111 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1114 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1116 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1119 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1121 return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1124 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1126 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1129 static int send_information_req(GDHCPClient *dhcp_client)
1131 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1135 static void remove_value(gpointer data, gpointer user_data)
1141 static void remove_option_value(gpointer data)
1143 GList *option_value = data;
1145 g_list_foreach(option_value, remove_value, NULL);
1146 g_list_free(option_value);
1149 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1150 int ifindex, GDHCPClientError *error)
1152 GDHCPClient *dhcp_client;
1155 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1159 dhcp_client = g_try_new0(GDHCPClient, 1);
1161 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1165 dhcp_client->interface = get_interface_name(ifindex);
1166 if (!dhcp_client->interface) {
1167 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1171 if (!interface_is_up(ifindex)) {
1172 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1176 get_interface_mac_address(ifindex, dhcp_client->mac_address);
1178 dhcp_client->listener_sockfd = -1;
1179 dhcp_client->listen_mode = L_NONE;
1180 dhcp_client->ref_count = 1;
1181 dhcp_client->type = type;
1182 dhcp_client->ifindex = ifindex;
1183 dhcp_client->lease_available_cb = NULL;
1184 dhcp_client->ipv4ll_available_cb = NULL;
1185 dhcp_client->no_lease_cb = NULL;
1186 dhcp_client->lease_lost_cb = NULL;
1187 dhcp_client->ipv4ll_lost_cb = NULL;
1188 dhcp_client->address_conflict_cb = NULL;
1189 dhcp_client->listener_watch = 0;
1190 dhcp_client->retry_times = 0;
1191 dhcp_client->ack_retry_times = 0;
1192 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1193 g_direct_equal, NULL, remove_option_value);
1194 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1195 g_direct_equal, NULL, g_free);
1196 dhcp_client->request_list = NULL;
1197 dhcp_client->require_list = NULL;
1198 dhcp_client->duid = NULL;
1199 dhcp_client->duid_len = 0;
1200 dhcp_client->last_request = time(NULL);
1201 dhcp_client->expire = 0;
1202 dhcp_client->request_bcast = false;
1204 *error = G_DHCP_CLIENT_ERROR_NONE;
1209 g_free(dhcp_client->interface);
1210 g_free(dhcp_client);
1214 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1216 static int dhcp_l2_socket(int ifindex)
1219 struct sockaddr_ll sock;
1224 * I've selected not to see LL header, so BPF doesn't see it, too.
1225 * The filter may also pass non-IP and non-ARP packets, but we do
1226 * a more complete check when receiving the message in userspace.
1228 * and filter shamelessly stolen from:
1230 * http://www.flamewarmaster.de/software/dhcpclient/
1232 * There are a few other interesting ideas on that page (look under
1233 * "Motivation"). Use of netlink events is most interesting. Think
1234 * of various network servers listening for events and reconfiguring.
1235 * That would obsolete sending HUP signals and/or make use of restarts.
1237 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1240 * TODO: make conditional?
1242 static const struct sock_filter filter_instr[] = {
1244 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1245 /* L5, L1, is UDP? */
1246 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1247 /* ugly check for arp on ethernet-like and IPv4 */
1248 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1249 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1250 /* skip IP header */
1251 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1252 /* check udp source and destination ports */
1253 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1255 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1257 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1258 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1261 static const struct sock_fprog filter_prog = {
1262 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1263 /* casting const away: */
1264 .filter = (struct sock_filter *) filter_instr,
1267 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
1271 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1272 /* Use only if standard ports are in use */
1273 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1274 sizeof(filter_prog));
1276 memset(&sock, 0, sizeof(sock));
1277 sock.sll_family = AF_PACKET;
1278 sock.sll_protocol = htons(ETH_P_IP);
1279 sock.sll_ifindex = ifindex;
1281 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1290 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1292 if (packet->ip.protocol != IPPROTO_UDP)
1295 if (packet->ip.version != IPVERSION)
1298 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1301 if (packet->udp.dest != htons(CLIENT_PORT))
1304 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1310 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1311 struct sockaddr_in *dst_addr)
1314 struct ip_udp_dhcp_packet packet;
1317 memset(&packet, 0, sizeof(packet));
1319 bytes = read(fd, &packet, sizeof(packet));
1323 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1326 if (bytes < ntohs(packet.ip.tot_len))
1327 /* packet is bigger than sizeof(packet), we did partial read */
1330 /* ignore any extra garbage bytes */
1331 bytes = ntohs(packet.ip.tot_len);
1333 if (!sanity_check(&packet, bytes))
1336 check = packet.ip.check;
1337 packet.ip.check = 0;
1338 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1341 /* verify UDP checksum. IP header has to be modified for this */
1342 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1343 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1344 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1345 check = packet.udp.check;
1346 packet.udp.check = 0;
1347 if (check && check != dhcp_checksum(&packet, bytes))
1350 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1351 sizeof(packet.udp)));
1353 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1356 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1358 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1361 static void ipv4ll_start(GDHCPClient *dhcp_client)
1366 remove_timeouts(dhcp_client);
1368 switch_listening_mode(dhcp_client, L_NONE);
1369 dhcp_client->retry_times = 0;
1370 dhcp_client->requested_ip = 0;
1372 /*try to start with a based mac address ip*/
1373 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1374 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1376 /*first wait a random delay to avoid storm of arp request on boot*/
1377 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1379 dhcp_client->retry_times++;
1380 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1387 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1390 switch_listening_mode(dhcp_client, L_NONE);
1392 remove_timeouts(dhcp_client);
1394 if (dhcp_client->listener_watch > 0) {
1395 g_source_remove(dhcp_client->listener_watch);
1396 dhcp_client->listener_watch = 0;
1399 dhcp_client->state = IPV4LL_PROBE;
1400 dhcp_client->retry_times = 0;
1401 dhcp_client->requested_ip = 0;
1403 g_free(dhcp_client->assigned_ip);
1404 dhcp_client->assigned_ip = NULL;
1407 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1410 struct ether_arp arp;
1411 uint32_t ip_requested;
1412 int source_conflict;
1413 int target_conflict;
1415 memset(&arp, 0, sizeof(arp));
1416 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1420 if (arp.arp_op != htons(ARPOP_REPLY) &&
1421 arp.arp_op != htons(ARPOP_REQUEST))
1424 ip_requested = htonl(dhcp_client->requested_ip);
1425 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1426 sizeof(ip_requested));
1428 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1429 sizeof(ip_requested));
1431 if (!source_conflict && !target_conflict)
1434 dhcp_client->conflicts++;
1436 debug(dhcp_client, "IPV4LL conflict detected");
1438 if (dhcp_client->state == IPV4LL_MONITOR) {
1439 if (!source_conflict)
1441 dhcp_client->state = IPV4LL_DEFEND;
1442 debug(dhcp_client, "DEFEND mode conflicts : %d",
1443 dhcp_client->conflicts);
1444 /*Try to defend with a single announce*/
1445 send_announce_packet(dhcp_client);
1449 if (dhcp_client->state == IPV4LL_DEFEND) {
1450 if (!source_conflict)
1452 else if (dhcp_client->ipv4ll_lost_cb)
1453 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1454 dhcp_client->ipv4ll_lost_data);
1457 ipv4ll_stop(dhcp_client);
1459 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1460 /*restart whole state machine*/
1461 dhcp_client->retry_times++;
1462 dhcp_client->timeout =
1463 g_timeout_add_full(G_PRIORITY_HIGH,
1464 ipv4ll_random_delay_ms(PROBE_WAIT),
1469 /* Here we got a lot of conflicts, RFC3927 states that we have
1470 * to wait RATE_LIMIT_INTERVAL before retrying,
1471 * but we just report failure.
1473 else if (dhcp_client->no_lease_cb)
1474 dhcp_client->no_lease_cb(dhcp_client,
1475 dhcp_client->no_lease_data);
1480 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1482 if (dhcp_client->type == G_DHCP_IPV6) {
1483 struct dhcpv6_packet *packet6 = pkt;
1489 xid = packet6->transaction_id[0] << 16 |
1490 packet6->transaction_id[1] << 8 |
1491 packet6->transaction_id[2];
1493 if (xid != dhcp_client->xid)
1496 struct dhcp_packet *packet = pkt;
1498 if (packet->xid != dhcp_client->xid)
1501 if (packet->hlen != 6)
1504 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1511 static void start_request(GDHCPClient *dhcp_client);
1513 static gboolean request_timeout(gpointer user_data)
1515 GDHCPClient *dhcp_client = user_data;
1517 debug(dhcp_client, "request timeout (retries %d)",
1518 dhcp_client->retry_times);
1520 dhcp_client->retry_times++;
1522 start_request(dhcp_client);
1527 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1528 gpointer user_data);
1530 static int switch_listening_mode(GDHCPClient *dhcp_client,
1531 ListenMode listen_mode)
1533 GIOChannel *listener_channel;
1534 int listener_sockfd;
1536 if (dhcp_client->listen_mode == listen_mode)
1539 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1540 dhcp_client->listen_mode, listen_mode);
1542 if (dhcp_client->listen_mode != L_NONE) {
1543 if (dhcp_client->listener_watch > 0)
1544 g_source_remove(dhcp_client->listener_watch);
1545 dhcp_client->listen_mode = L_NONE;
1546 dhcp_client->listener_sockfd = -1;
1547 dhcp_client->listener_watch = 0;
1550 if (listen_mode == L_NONE)
1553 if (listen_mode == L2)
1554 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1555 else if (listen_mode == L3) {
1556 if (dhcp_client->type == G_DHCP_IPV6)
1557 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1558 dhcp_client->interface,
1561 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1562 dhcp_client->interface,
1564 } else if (listen_mode == L_ARP)
1565 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1569 if (listener_sockfd < 0)
1572 listener_channel = g_io_channel_unix_new(listener_sockfd);
1573 if (!listener_channel) {
1574 /* Failed to create listener channel */
1575 close(listener_sockfd);
1579 dhcp_client->listen_mode = listen_mode;
1580 dhcp_client->listener_sockfd = listener_sockfd;
1582 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1583 dhcp_client->listener_watch =
1584 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1585 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1586 listener_event, dhcp_client,
1588 g_io_channel_unref(listener_channel);
1593 static void start_request(GDHCPClient *dhcp_client)
1595 debug(dhcp_client, "start request (retries %d)",
1596 dhcp_client->retry_times);
1598 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1599 if (dhcp_client->no_lease_cb)
1600 dhcp_client->no_lease_cb(dhcp_client,
1601 dhcp_client->no_lease_data);
1605 if (dhcp_client->retry_times == 0) {
1606 dhcp_client->state = REQUESTING;
1607 switch_listening_mode(dhcp_client, L2);
1610 send_request(dhcp_client);
1612 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1619 static uint32_t get_lease(struct dhcp_packet *packet)
1622 uint32_t lease_seconds;
1624 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1628 lease_seconds = get_be32(option);
1629 /* paranoia: must not be prone to overflows */
1630 lease_seconds &= 0x0fffffff;
1631 if (lease_seconds < 10)
1634 return lease_seconds;
1637 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1639 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1641 remove_timeouts(dhcp_client);
1643 dhcp_client->retry_times = retry_times;
1644 dhcp_client->requested_ip = 0;
1645 dhcp_client->state = INIT_SELECTING;
1646 switch_listening_mode(dhcp_client, L2);
1648 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1651 static gboolean start_expire(gpointer user_data)
1653 GDHCPClient *dhcp_client = user_data;
1655 debug(dhcp_client, "lease expired");
1657 /*remove all timeouts if they are set*/
1658 remove_timeouts(dhcp_client);
1660 restart_dhcp(dhcp_client, 0);
1662 /* ip need to be cleared */
1663 if (dhcp_client->lease_lost_cb)
1664 dhcp_client->lease_lost_cb(dhcp_client,
1665 dhcp_client->lease_lost_data);
1670 static gboolean continue_rebound(gpointer user_data)
1672 GDHCPClient *dhcp_client = user_data;
1674 switch_listening_mode(dhcp_client, L2);
1675 send_request(dhcp_client);
1677 if (dhcp_client->t2_timeout> 0)
1678 g_source_remove(dhcp_client->t2_timeout);
1680 /*recalculate remaining rebind time*/
1681 dhcp_client->T2 >>= 1;
1682 if (dhcp_client->T2 > 60) {
1683 dhcp_client->t2_timeout =
1684 g_timeout_add_full(G_PRIORITY_HIGH,
1685 dhcp_client->T2 * 1000 + (rand() % 2000) - 1000,
1694 static gboolean start_rebound(gpointer user_data)
1696 GDHCPClient *dhcp_client = user_data;
1698 /*remove renew timer*/
1699 if (dhcp_client->t1_timeout > 0)
1700 g_source_remove(dhcp_client->t1_timeout);
1702 debug(dhcp_client, "start rebound");
1703 dhcp_client->state = REBINDING;
1705 /*calculate total rebind time*/
1706 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1708 /*send the first rebound and reschedule*/
1709 continue_rebound(user_data);
1714 static gboolean continue_renew (gpointer user_data)
1716 GDHCPClient *dhcp_client = user_data;
1718 switch_listening_mode(dhcp_client, L3);
1719 send_request(dhcp_client);
1721 if (dhcp_client->t1_timeout > 0)
1722 g_source_remove(dhcp_client->t1_timeout);
1724 dhcp_client->T1 >>= 1;
1726 if (dhcp_client->T1 > 60) {
1727 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1728 dhcp_client->T1 * 1000 + (rand() % 2000) - 1000,
1736 static gboolean start_renew(gpointer user_data)
1738 GDHCPClient *dhcp_client = user_data;
1740 debug(dhcp_client, "start renew");
1741 dhcp_client->state = RENEWING;
1743 /*calculate total renew period*/
1744 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1746 /*send first renew and reschedule for half the remaining time.*/
1747 continue_renew(user_data);
1752 static void start_bound(GDHCPClient *dhcp_client)
1754 debug(dhcp_client, "start bound");
1756 dhcp_client->state = BOUND;
1758 remove_timeouts(dhcp_client);
1761 *TODO: T1 and T2 should be set through options instead of
1762 * defaults as they are here.
1765 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1766 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1767 dhcp_client->expire = dhcp_client->lease_seconds;
1769 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1771 start_renew, dhcp_client,
1774 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1776 start_rebound, dhcp_client,
1779 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1780 dhcp_client->expire,
1781 start_expire, dhcp_client,
1785 static gboolean restart_dhcp_timeout(gpointer user_data)
1787 GDHCPClient *dhcp_client = user_data;
1789 debug(dhcp_client, "restart DHCP timeout");
1791 if (dhcp_client->state == REBOOTING) {
1792 g_free(dhcp_client->last_address);
1793 dhcp_client->last_address = NULL;
1794 restart_dhcp(dhcp_client, 0);
1796 dhcp_client->ack_retry_times++;
1797 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1802 static char *get_ip(uint32_t ip)
1804 struct in_addr addr;
1808 return g_strdup(inet_ntoa(addr));
1811 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1819 if (type == OPTION_STRING)
1820 return g_list_append(list, g_strdup(value));
1822 while ((pos = strchr(pos, ' '))) {
1825 list = g_list_append(list, g_strdup(value));
1830 list = g_list_append(list, g_strdup(value));
1835 static inline uint32_t get_uint32(unsigned char *value)
1837 return value[0] << 24 | value[1] << 16 |
1838 value[2] << 8 | value[3];
1841 static inline uint16_t get_uint16(unsigned char *value)
1843 return value[0] << 8 | value[1];
1846 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1847 struct in6_addr *addr,
1848 unsigned char prefixlen, uint32_t preferred,
1851 GDHCPIAPrefix *ia_prefix;
1853 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1857 if (dhcp_client->debug_func) {
1858 char addr_str[INET6_ADDRSTRLEN + 1];
1859 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1860 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1861 addr_str, prefixlen, preferred, valid);
1864 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1865 ia_prefix->prefixlen = prefixlen;
1866 ia_prefix->preferred = preferred;
1867 ia_prefix->valid = valid;
1868 ia_prefix->expire = time(NULL) + valid;
1870 return g_list_prepend(list, ia_prefix);
1873 static GList *get_addresses(GDHCPClient *dhcp_client,
1875 unsigned char *value,
1879 struct in6_addr addr;
1880 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1881 uint16_t option_len, option_code, st = 0, max_len;
1882 int addr_count = 0, prefix_count = 0, i, pos;
1883 unsigned char prefixlen;
1884 unsigned int shortest_valid = 0;
1888 if (!value || len < 4)
1891 iaid = get_uint32(&value[0]);
1892 if (dhcp_client->iaid != iaid)
1895 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
1896 T1 = get_uint32(&value[4]);
1897 T2 = get_uint32(&value[8]);
1900 /* IA_NA: RFC 3315, 22.4 */
1901 /* IA_PD: RFC 3633, ch 9 */
1911 max_len = len - pos;
1913 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
1915 /* We have more sub-options in this packet. */
1917 option = dhcpv6_get_sub_option(&value[pos], max_len,
1918 &option_code, &option_len);
1920 debug(dhcp_client, "pos %d option %p code %d len %d",
1921 pos, option, option_code, option_len);
1929 switch (option_code) {
1930 case G_DHCPV6_IAADDR:
1932 memcpy(&addr, &option[0], sizeof(addr));
1934 preferred = get_uint32(&option[i]);
1936 valid = get_uint32(&option[i]);
1941 case G_DHCPV6_STATUS_CODE:
1942 st = get_uint16(&option[0]);
1943 debug(dhcp_client, "error code %d", st);
1944 if (option_len > 2) {
1945 str = g_strndup((gchar *)&option[2],
1947 debug(dhcp_client, "error text: %s", str);
1954 case G_DHCPV6_IA_PREFIX:
1956 preferred = get_uint32(&option[i]);
1958 valid = get_uint32(&option[i]);
1960 prefixlen = option[i];
1962 memcpy(&addr, &option[i], sizeof(addr));
1964 if (preferred < valid) {
1965 /* RFC 3633, ch 10 */
1966 list = add_prefix(dhcp_client, list, &addr,
1967 prefixlen, preferred, valid);
1968 if (shortest_valid > valid)
1969 shortest_valid = valid;
1975 pos += 2 + 2 + option_len;
1977 } while (pos < len);
1979 if (addr_count > 0 && st == 0) {
1980 /* We only support one address atm */
1981 char addr_str[INET6_ADDRSTRLEN + 1];
1983 if (preferred > valid)
1984 /* RFC 3315, 22.6 */
1987 dhcp_client->T1 = T1;
1988 dhcp_client->T2 = T2;
1990 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
1991 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
1992 addr_count, addr_str, T1, T2);
1994 list = g_list_append(list, g_strdup(addr_str));
1996 if (code == G_DHCPV6_IA_NA)
1997 memcpy(&dhcp_client->ia_na, &addr,
1998 sizeof(struct in6_addr));
2000 memcpy(&dhcp_client->ia_ta, &addr,
2001 sizeof(struct in6_addr));
2003 if (valid != dhcp_client->expire)
2004 dhcp_client->expire = valid;
2007 if (prefix_count > 0 && list) {
2009 * This means we have a list of prefixes to delegate.
2011 list = g_list_reverse(list);
2013 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2014 prefix_count, T1, T2);
2016 dhcp_client->T1 = T1;
2017 dhcp_client->T2 = T2;
2019 dhcp_client->expire = shortest_valid;
2022 if (status && *status != 0)
2023 debug(dhcp_client, "status %d", *status);
2028 static GList *get_domains(int maxlen, unsigned char *value)
2034 char dns_name[NS_MAXDNAME + 1];
2036 if (!value || maxlen < 3)
2039 while (pos < maxlen) {
2040 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2042 c = (unsigned char *)dns_name;
2049 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2050 pos += (char *)c - dns_name + 1;
2053 return g_list_reverse(list);
2056 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2058 unsigned char *value,
2069 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2070 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2073 "%s server list length (%d) is invalid",
2074 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2078 for (i = 0; i < len; i += 16) {
2080 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2084 if (!inet_ntop(AF_INET6, &value[i], str,
2088 list = g_list_append(list, str);
2092 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2093 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2094 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2095 list = get_addresses(dhcp_client, code, len, value, status);
2098 case G_DHCPV6_DOMAIN_LIST:
2099 list = get_domains(len, value);
2109 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2110 struct dhcpv6_packet *packet,
2111 uint16_t pkt_len, uint16_t *status)
2113 GList *list, *value_list;
2116 uint16_t option_len;
2118 for (list = dhcp_client->request_list; list; list = list->next) {
2119 code = (uint16_t) GPOINTER_TO_INT(list->data);
2121 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2124 g_hash_table_remove(dhcp_client->code_value_hash,
2125 GINT_TO_POINTER((int) code));
2129 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2130 option_len, option, status);
2132 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2133 option_len, value_list);
2136 g_hash_table_remove(dhcp_client->code_value_hash,
2137 GINT_TO_POINTER((int) code));
2139 g_hash_table_insert(dhcp_client->code_value_hash,
2140 GINT_TO_POINTER((int) code), value_list);
2144 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2146 GDHCPOptionType type;
2147 GList *list, *value_list;
2152 for (list = dhcp_client->request_list; list; list = list->next) {
2153 code = (uint8_t) GPOINTER_TO_INT(list->data);
2155 option = dhcp_get_option(packet, code);
2157 g_hash_table_remove(dhcp_client->code_value_hash,
2158 GINT_TO_POINTER((int) code));
2162 type = dhcp_get_code_type(code);
2164 option_value = malloc_option_value_string(option, type);
2166 g_hash_table_remove(dhcp_client->code_value_hash,
2167 GINT_TO_POINTER((int) code));
2169 value_list = get_option_value_list(option_value, type);
2171 g_free(option_value);
2174 g_hash_table_remove(dhcp_client->code_value_hash,
2175 GINT_TO_POINTER((int) code));
2177 g_hash_table_insert(dhcp_client->code_value_hash,
2178 GINT_TO_POINTER((int) code), value_list);
2182 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2185 GDHCPClient *dhcp_client = user_data;
2186 struct sockaddr_in dst_addr = { 0 };
2187 struct dhcp_packet packet;
2188 struct dhcpv6_packet *packet6 = NULL;
2189 uint8_t *message_type = NULL, *client_id = NULL, *option,
2191 uint16_t option_len = 0, status = 0;
2194 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2195 uint16_t pkt_len = 0;
2199 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2200 dhcp_client->listener_watch = 0;
2204 if (dhcp_client->listen_mode == L_NONE)
2209 dhcp_client->status_code = 0;
2211 if (dhcp_client->listen_mode == L2) {
2212 re = dhcp_recv_l2_packet(&packet,
2213 dhcp_client->listener_sockfd,
2216 } else if (dhcp_client->listen_mode == L3) {
2217 if (dhcp_client->type == G_DHCP_IPV6) {
2218 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2219 dhcp_client->listener_sockfd);
2222 xid = packet6->transaction_id[0] << 16 |
2223 packet6->transaction_id[1] << 8 |
2224 packet6->transaction_id[2];
2226 re = dhcp_recv_l3_packet(&packet,
2227 dhcp_client->listener_sockfd);
2230 } else if (dhcp_client->listen_mode == L_ARP) {
2231 ipv4ll_recv_arp_packet(dhcp_client);
2239 if (!check_package_owner(dhcp_client, pkt))
2242 if (dhcp_client->type == G_DHCP_IPV6) {
2247 client_id = dhcpv6_get_option(packet6, pkt_len,
2248 G_DHCPV6_CLIENTID, &option_len, &count);
2250 if (!client_id || count == 0 || option_len == 0 ||
2251 memcmp(dhcp_client->duid, client_id,
2252 dhcp_client->duid_len) != 0) {
2254 "client duid error, discarding msg %p/%d/%d",
2255 client_id, option_len, count);
2259 option = dhcpv6_get_option(packet6, pkt_len,
2260 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2261 if (option != 0 && option_len > 0) {
2262 status = option[0]<<8 | option[1];
2264 debug(dhcp_client, "error code %d", status);
2265 if (option_len > 2) {
2266 gchar *txt = g_strndup(
2267 (gchar *)&option[2],
2269 debug(dhcp_client, "error text: %s",
2274 dhcp_client->status_code = status;
2277 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2282 if (!message_type && !client_id)
2283 /* No message type / client id option, ignore package */
2286 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2287 "(current state %d)", ntohl(xid), dhcp_client->state);
2289 switch (dhcp_client->state) {
2290 case INIT_SELECTING:
2291 if (*message_type != DHCPOFFER)
2294 remove_timeouts(dhcp_client);
2295 dhcp_client->timeout = 0;
2296 dhcp_client->retry_times = 0;
2298 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2299 dhcp_client->server_ip = get_be32(option);
2300 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2302 dhcp_client->state = REQUESTING;
2304 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2305 dhcp_client->request_bcast = true;
2307 dhcp_client->request_bcast = false;
2309 debug(dhcp_client, "init ip %s -> %sadding broadcast flag",
2310 inet_ntoa(dst_addr.sin_addr),
2311 dhcp_client->request_bcast ? "" : "not ");
2313 start_request(dhcp_client);
2317 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2318 dhcp_client->request_bcast = true;
2320 dhcp_client->request_bcast = false;
2322 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2323 inet_ntoa(dst_addr.sin_addr),
2324 dhcp_client->request_bcast ? "" : "not ");
2329 if (*message_type == DHCPACK) {
2330 dhcp_client->retry_times = 0;
2332 remove_timeouts(dhcp_client);
2334 dhcp_client->lease_seconds = get_lease(&packet);
2336 get_request(dhcp_client, &packet);
2338 switch_listening_mode(dhcp_client, L_NONE);
2340 g_free(dhcp_client->assigned_ip);
2341 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2343 if (dhcp_client->state == REBOOTING) {
2344 option = dhcp_get_option(&packet,
2346 dhcp_client->server_ip = get_be32(option);
2349 /* Address should be set up here */
2350 if (dhcp_client->lease_available_cb)
2351 dhcp_client->lease_available_cb(dhcp_client,
2352 dhcp_client->lease_available_data);
2354 start_bound(dhcp_client);
2355 } else if (*message_type == DHCPNAK) {
2356 dhcp_client->retry_times = 0;
2358 remove_timeouts(dhcp_client);
2360 dhcp_client->timeout = g_timeout_add_seconds_full(
2362 restart_dhcp_timeout,
2369 if (dhcp_client->type != G_DHCP_IPV6)
2372 if (packet6->message != DHCPV6_REPLY &&
2373 packet6->message != DHCPV6_ADVERTISE)
2377 server_id = dhcpv6_get_option(packet6, pkt_len,
2378 G_DHCPV6_SERVERID, &option_len, &count);
2379 if (!server_id || count != 1 || option_len == 0) {
2380 /* RFC 3315, 15.10 */
2382 "server duid error, discarding msg %p/%d/%d",
2383 server_id, option_len, count);
2386 dhcp_client->server_duid = g_try_malloc(option_len);
2387 if (!dhcp_client->server_duid)
2389 memcpy(dhcp_client->server_duid, server_id, option_len);
2390 dhcp_client->server_duid_len = option_len;
2392 if (packet6->message == DHCPV6_REPLY) {
2393 uint8_t *rapid_commit;
2396 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2397 G_DHCPV6_RAPID_COMMIT,
2398 &option_len, &count);
2399 if (!rapid_commit || option_len == 0 ||
2401 /* RFC 3315, 17.1.4 */
2405 switch_listening_mode(dhcp_client, L_NONE);
2407 if (dhcp_client->status_code == 0)
2408 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2409 &dhcp_client->status_code);
2411 if (packet6->message == DHCPV6_ADVERTISE) {
2412 if (dhcp_client->advertise_cb)
2413 dhcp_client->advertise_cb(dhcp_client,
2414 dhcp_client->advertise_data);
2418 if (dhcp_client->solicitation_cb) {
2420 * The dhcp_client might not be valid after the
2421 * callback call so just return immediately.
2423 dhcp_client->solicitation_cb(dhcp_client,
2424 dhcp_client->solicitation_data);
2429 if (dhcp_client->type != G_DHCP_IPV6)
2432 server_id = dhcpv6_get_option(packet6, pkt_len,
2433 G_DHCPV6_SERVERID, &option_len, &count);
2434 if (!dhcp_client->server_duid && server_id &&
2437 * If we do not have server duid yet, then get it now.
2438 * Prefix delegation renew support needs it.
2440 dhcp_client->server_duid = g_try_malloc(option_len);
2441 if (!dhcp_client->server_duid)
2443 memcpy(dhcp_client->server_duid, server_id, option_len);
2444 dhcp_client->server_duid_len = option_len;
2447 case INFORMATION_REQ:
2453 if (dhcp_client->type != G_DHCP_IPV6)
2456 if (packet6->message != DHCPV6_REPLY)
2461 server_id = dhcpv6_get_option(packet6, pkt_len,
2462 G_DHCPV6_SERVERID, &option_len, &count);
2463 if (!server_id || count != 1 || option_len == 0 ||
2464 (dhcp_client->server_duid_len > 0 &&
2465 memcmp(dhcp_client->server_duid, server_id,
2466 dhcp_client->server_duid_len) != 0)) {
2467 /* RFC 3315, 15.10 */
2469 "server duid error, discarding msg %p/%d/%d",
2470 server_id, option_len, count);
2474 switch_listening_mode(dhcp_client, L_NONE);
2476 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2477 &dhcp_client->status_code);
2479 if (dhcp_client->information_req_cb) {
2481 * The dhcp_client might not be valid after the
2482 * callback call so just return immediately.
2484 dhcp_client->information_req_cb(dhcp_client,
2485 dhcp_client->information_req_data);
2488 if (dhcp_client->request_cb) {
2489 dhcp_client->request_cb(dhcp_client,
2490 dhcp_client->request_data);
2493 if (dhcp_client->renew_cb) {
2494 dhcp_client->renew_cb(dhcp_client,
2495 dhcp_client->renew_data);
2498 if (dhcp_client->rebind_cb) {
2499 dhcp_client->rebind_cb(dhcp_client,
2500 dhcp_client->rebind_data);
2503 if (dhcp_client->release_cb) {
2504 dhcp_client->release_cb(dhcp_client,
2505 dhcp_client->release_data);
2508 if (dhcp_client->decline_cb) {
2509 dhcp_client->decline_cb(dhcp_client,
2510 dhcp_client->decline_data);
2513 if (dhcp_client->confirm_cb) {
2515 server_id = dhcpv6_get_option(packet6, pkt_len,
2516 G_DHCPV6_SERVERID, &option_len,
2518 if (!server_id || count != 1 ||
2520 /* RFC 3315, 15.10 */
2522 "confirm server duid error, "
2523 "discarding msg %p/%d/%d",
2524 server_id, option_len, count);
2527 dhcp_client->server_duid = g_try_malloc(option_len);
2528 if (!dhcp_client->server_duid)
2530 memcpy(dhcp_client->server_duid, server_id, option_len);
2531 dhcp_client->server_duid_len = option_len;
2533 dhcp_client->confirm_cb(dhcp_client,
2534 dhcp_client->confirm_data);
2542 debug(dhcp_client, "processed DHCP packet (new state %d)",
2543 dhcp_client->state);
2548 static gboolean discover_timeout(gpointer user_data)
2550 GDHCPClient *dhcp_client = user_data;
2552 dhcp_client->retry_times++;
2555 * We do not send the REQUESTED IP option if we are retrying because
2556 * if the server is non-authoritative it will ignore the request if the
2557 * option is present.
2559 g_dhcp_client_start(dhcp_client, NULL);
2564 static gboolean reboot_timeout(gpointer user_data)
2566 GDHCPClient *dhcp_client = user_data;
2567 dhcp_client->retry_times = 0;
2568 dhcp_client->requested_ip = 0;
2569 dhcp_client->state = INIT_SELECTING;
2571 * We do not send the REQUESTED IP option because the server didn't
2572 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2575 g_dhcp_client_start(dhcp_client, NULL);
2580 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2582 GDHCPClient *dhcp_client = dhcp_data;
2584 debug(dhcp_client, "back to MONITOR mode");
2586 dhcp_client->conflicts = 0;
2587 dhcp_client->state = IPV4LL_MONITOR;
2592 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2594 GDHCPClient *dhcp_client = dhcp_data;
2597 debug(dhcp_client, "request timeout (retries %d)",
2598 dhcp_client->retry_times);
2600 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2601 dhcp_client->retry_times++;
2602 send_announce_packet(dhcp_client);
2606 ip = htonl(dhcp_client->requested_ip);
2607 debug(dhcp_client, "switching to monitor mode");
2608 dhcp_client->state = IPV4LL_MONITOR;
2609 dhcp_client->assigned_ip = get_ip(ip);
2611 if (dhcp_client->ipv4ll_available_cb)
2612 dhcp_client->ipv4ll_available_cb(dhcp_client,
2613 dhcp_client->ipv4ll_available_data);
2614 dhcp_client->conflicts = 0;
2619 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2622 GDHCPClient *dhcp_client = dhcp_data;
2624 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2625 dhcp_client->retry_times);
2627 if (dhcp_client->retry_times == PROBE_NUM) {
2628 dhcp_client->state = IPV4LL_ANNOUNCE;
2629 dhcp_client->retry_times = 0;
2631 dhcp_client->retry_times++;
2632 send_announce_packet(dhcp_client);
2635 dhcp_client->retry_times++;
2636 send_probe_packet(dhcp_client);
2641 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2646 if (dhcp_client->type == G_DHCP_IPV6) {
2647 if (dhcp_client->information_req_cb) {
2648 dhcp_client->state = INFORMATION_REQ;
2649 re = switch_listening_mode(dhcp_client, L3);
2651 switch_listening_mode(dhcp_client, L_NONE);
2652 dhcp_client->state = 0;
2655 send_information_req(dhcp_client);
2657 } else if (dhcp_client->solicitation_cb) {
2658 dhcp_client->state = SOLICITATION;
2659 re = switch_listening_mode(dhcp_client, L3);
2661 switch_listening_mode(dhcp_client, L_NONE);
2662 dhcp_client->state = 0;
2665 send_solicitation(dhcp_client);
2667 } else if (dhcp_client->request_cb) {
2668 dhcp_client->state = REQUEST;
2669 re = switch_listening_mode(dhcp_client, L3);
2671 switch_listening_mode(dhcp_client, L_NONE);
2672 dhcp_client->state = 0;
2675 send_dhcpv6_request(dhcp_client);
2677 } else if (dhcp_client->confirm_cb) {
2678 dhcp_client->state = CONFIRM;
2679 re = switch_listening_mode(dhcp_client, L3);
2681 switch_listening_mode(dhcp_client, L_NONE);
2682 dhcp_client->state = 0;
2685 send_dhcpv6_confirm(dhcp_client);
2687 } else if (dhcp_client->renew_cb) {
2688 dhcp_client->state = RENEW;
2689 re = switch_listening_mode(dhcp_client, L3);
2691 switch_listening_mode(dhcp_client, L_NONE);
2692 dhcp_client->state = 0;
2695 send_dhcpv6_renew(dhcp_client);
2697 } else if (dhcp_client->rebind_cb) {
2698 dhcp_client->state = REBIND;
2699 re = switch_listening_mode(dhcp_client, L3);
2701 switch_listening_mode(dhcp_client, L_NONE);
2702 dhcp_client->state = 0;
2705 send_dhcpv6_rebind(dhcp_client);
2707 } else if (dhcp_client->release_cb) {
2708 dhcp_client->state = RENEW;
2709 re = switch_listening_mode(dhcp_client, L3);
2711 switch_listening_mode(dhcp_client, L_NONE);
2712 dhcp_client->state = 0;
2715 send_dhcpv6_release(dhcp_client);
2716 } else if (dhcp_client->decline_cb) {
2717 dhcp_client->state = DECLINE;
2718 re = switch_listening_mode(dhcp_client, L3);
2720 switch_listening_mode(dhcp_client, L_NONE);
2721 dhcp_client->state = 0;
2724 send_dhcpv6_decline(dhcp_client);
2730 if (dhcp_client->type == G_DHCP_IPV4LL) {
2731 dhcp_client->state = INIT_SELECTING;
2732 ipv4ll_start(dhcp_client);
2736 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2737 if (dhcp_client->no_lease_cb)
2738 dhcp_client->no_lease_cb(dhcp_client,
2739 dhcp_client->no_lease_data);
2740 dhcp_client->retry_times = 0;
2744 if (dhcp_client->retry_times == 0) {
2745 g_free(dhcp_client->assigned_ip);
2746 dhcp_client->assigned_ip = NULL;
2748 dhcp_client->state = INIT_SELECTING;
2749 re = switch_listening_mode(dhcp_client, L2);
2753 dhcp_client->xid = rand();
2754 dhcp_client->start = time(NULL);
2757 if (!last_address) {
2760 addr = ntohl(inet_addr(last_address));
2761 if (addr == 0xFFFFFFFF) {
2764 g_free(dhcp_client->last_address);
2765 dhcp_client->last_address = g_strdup(last_address);
2769 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2770 debug(dhcp_client, "DHCP client start with state init_reboot");
2771 dhcp_client->requested_ip = addr;
2772 dhcp_client->state = REBOOTING;
2773 send_request(dhcp_client);
2775 dhcp_client->timeout = g_timeout_add_seconds_full(
2783 send_discover(dhcp_client, addr);
2785 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2793 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2795 switch_listening_mode(dhcp_client, L_NONE);
2797 if (dhcp_client->state == BOUND ||
2798 dhcp_client->state == RENEWING ||
2799 dhcp_client->state == REBINDING)
2800 send_release(dhcp_client, dhcp_client->server_ip,
2801 dhcp_client->requested_ip);
2803 remove_timeouts(dhcp_client);
2805 if (dhcp_client->listener_watch > 0) {
2806 g_source_remove(dhcp_client->listener_watch);
2807 dhcp_client->listener_watch = 0;
2810 dhcp_client->retry_times = 0;
2811 dhcp_client->ack_retry_times = 0;
2813 dhcp_client->requested_ip = 0;
2814 dhcp_client->state = RELEASED;
2815 dhcp_client->lease_seconds = 0;
2816 dhcp_client->request_bcast = false;
2819 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2820 unsigned char option_code)
2822 return g_hash_table_lookup(dhcp_client->code_value_hash,
2823 GINT_TO_POINTER((int) option_code));
2826 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2827 GDHCPClientEvent event,
2828 GDHCPClientEventFunc func,
2832 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2833 dhcp_client->lease_available_cb = func;
2834 dhcp_client->lease_available_data = data;
2836 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2837 if (dhcp_client->type == G_DHCP_IPV6)
2839 dhcp_client->ipv4ll_available_cb = func;
2840 dhcp_client->ipv4ll_available_data = data;
2842 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2843 dhcp_client->no_lease_cb = func;
2844 dhcp_client->no_lease_data = data;
2846 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2847 dhcp_client->lease_lost_cb = func;
2848 dhcp_client->lease_lost_data = data;
2850 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2851 if (dhcp_client->type == G_DHCP_IPV6)
2853 dhcp_client->ipv4ll_lost_cb = func;
2854 dhcp_client->ipv4ll_lost_data = data;
2856 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2857 dhcp_client->address_conflict_cb = func;
2858 dhcp_client->address_conflict_data = data;
2860 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2861 if (dhcp_client->type != G_DHCP_IPV6)
2863 dhcp_client->information_req_cb = func;
2864 dhcp_client->information_req_data = data;
2866 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2867 if (dhcp_client->type != G_DHCP_IPV6)
2869 dhcp_client->solicitation_cb = func;
2870 dhcp_client->solicitation_data = data;
2872 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2873 if (dhcp_client->type != G_DHCP_IPV6)
2875 dhcp_client->advertise_cb = func;
2876 dhcp_client->advertise_data = data;
2878 case G_DHCP_CLIENT_EVENT_REQUEST:
2879 if (dhcp_client->type != G_DHCP_IPV6)
2881 dhcp_client->request_cb = func;
2882 dhcp_client->request_data = data;
2884 case G_DHCP_CLIENT_EVENT_RENEW:
2885 if (dhcp_client->type != G_DHCP_IPV6)
2887 dhcp_client->renew_cb = func;
2888 dhcp_client->renew_data = data;
2890 case G_DHCP_CLIENT_EVENT_REBIND:
2891 if (dhcp_client->type != G_DHCP_IPV6)
2893 dhcp_client->rebind_cb = func;
2894 dhcp_client->rebind_data = data;
2896 case G_DHCP_CLIENT_EVENT_RELEASE:
2897 if (dhcp_client->type != G_DHCP_IPV6)
2899 dhcp_client->release_cb = func;
2900 dhcp_client->release_data = data;
2902 case G_DHCP_CLIENT_EVENT_CONFIRM:
2903 if (dhcp_client->type != G_DHCP_IPV6)
2905 dhcp_client->confirm_cb = func;
2906 dhcp_client->confirm_data = data;
2908 case G_DHCP_CLIENT_EVENT_DECLINE:
2909 if (dhcp_client->type != G_DHCP_IPV6)
2911 dhcp_client->decline_cb = func;
2912 dhcp_client->decline_data = data;
2917 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2919 return dhcp_client->ifindex;
2922 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2924 return g_strdup(dhcp_client->assigned_ip);
2927 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2929 GList *option = NULL;
2931 if (dhcp_client->type == G_DHCP_IPV6)
2934 switch (dhcp_client->state) {
2936 case IPV4LL_MONITOR:
2937 return g_strdup("255.255.0.0");
2941 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2943 return g_strdup(option->data);
2944 case INIT_SELECTING:
2949 case IPV4LL_ANNOUNCE:
2950 case INFORMATION_REQ:
2963 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2964 unsigned int option_code)
2966 if (!g_list_find(dhcp_client->request_list,
2967 GINT_TO_POINTER((int)option_code)))
2968 dhcp_client->request_list = g_list_prepend(
2969 dhcp_client->request_list,
2970 (GINT_TO_POINTER((int) option_code)));
2972 return G_DHCP_CLIENT_ERROR_NONE;
2975 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2977 g_list_free(dhcp_client->request_list);
2978 dhcp_client->request_list = NULL;
2981 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2983 g_hash_table_remove_all(dhcp_client->send_value_hash);
2986 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
2990 storage = g_try_malloc(size + OPT_DATA);
2994 storage[OPT_CODE] = code;
2995 storage[OPT_LEN] = size;
2996 memcpy(&storage[OPT_DATA], data, size);
3001 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3004 return alloc_dhcp_option(code, data, MIN(size, 255));
3007 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3009 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3012 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3014 const unsigned maclen = 6;
3015 const unsigned idlen = maclen + 1;
3016 const uint8_t option_code = G_DHCP_CLIENT_ID;
3017 uint8_t idbuf[idlen];
3018 uint8_t *data_option;
3020 idbuf[0] = ARPHRD_ETHER;
3022 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3024 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3026 return G_DHCP_CLIENT_ERROR_NOMEM;
3028 g_hash_table_insert(dhcp_client->send_value_hash,
3029 GINT_TO_POINTER((int) option_code), data_option);
3031 return G_DHCP_CLIENT_ERROR_NONE;
3034 /* Now only support send hostname */
3035 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3036 unsigned char option_code, const char *option_value)
3038 uint8_t *binary_option;
3040 if (option_code == G_DHCP_HOST_NAME && option_value) {
3041 binary_option = alloc_dhcp_string_option(option_code,
3044 return G_DHCP_CLIENT_ERROR_NOMEM;
3046 g_hash_table_insert(dhcp_client->send_value_hash,
3047 GINT_TO_POINTER((int) option_code), binary_option);
3050 return G_DHCP_CLIENT_ERROR_NONE;
3053 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3058 storage = g_malloc(2 + 2 + len);
3062 storage[0] = code >> 8;
3063 storage[1] = code & 0xff;
3064 storage[2] = len >> 8;
3065 storage[3] = len & 0xff;
3066 memcpy(storage + 2 + 2, option, len);
3071 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3073 return g_hash_table_remove(dhcp_client->send_value_hash,
3074 GINT_TO_POINTER((int)code));
3077 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3078 uint16_t option_code,
3079 uint8_t *option_value,
3080 uint16_t option_len)
3083 uint8_t *binary_option;
3085 debug(dhcp_client, "setting option %d to %p len %d",
3086 option_code, option_value, option_len);
3088 binary_option = alloc_dhcpv6_option(option_code, option_value,
3091 g_hash_table_insert(dhcp_client->send_value_hash,
3092 GINT_TO_POINTER((int) option_code),
3097 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3099 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3102 dhcp_client->last_request = time(NULL);
3105 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3107 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3110 return dhcp_client->status_code;
3113 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3118 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3123 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3128 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3131 g_dhcp_client_stop(dhcp_client);
3133 g_free(dhcp_client->interface);
3134 g_free(dhcp_client->assigned_ip);
3135 g_free(dhcp_client->last_address);
3136 g_free(dhcp_client->duid);
3137 g_free(dhcp_client->server_duid);
3139 g_list_free(dhcp_client->request_list);
3140 g_list_free(dhcp_client->require_list);
3142 g_hash_table_destroy(dhcp_client->code_value_hash);
3143 g_hash_table_destroy(dhcp_client->send_value_hash);
3145 g_free(dhcp_client);
3148 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3149 GDHCPDebugFunc func, gpointer user_data)
3154 dhcp_client->debug_func = func;
3155 dhcp_client->debug_data = user_data;
3158 static GDHCPIAPrefix *copy_prefix(gpointer data)
3160 GDHCPIAPrefix *copy, *prefix = data;
3162 copy = g_try_new(GDHCPIAPrefix, 1);
3166 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3171 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3173 GSList *copy = NULL;
3176 for (list = prefixes; list; list = list->next)
3177 copy = g_slist_prepend(copy, copy_prefix(list->data));