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
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
36 #include <netpacket/packet.h>
37 #include <netinet/if_ether.h>
38 #include <net/ethernet.h>
41 #include <linux/filter.h>
45 #include "../src/connman.h"
46 #include "../src/shared/arp.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 {
92 uint8_t mac_address[6];
95 uint32_t requested_ip;
98 uint32_t lease_seconds;
99 ListenMode listen_mode;
102 uint8_t ack_retry_times;
108 guint listener_watch;
111 GHashTable *code_value_hash;
112 GHashTable *send_value_hash;
113 GHashTable *secs_bcast_hash;
114 GDHCPClientEventFunc lease_available_cb;
115 gpointer lease_available_data;
116 GDHCPClientEventFunc ipv4ll_available_cb;
117 gpointer ipv4ll_available_data;
118 GDHCPClientEventFunc no_lease_cb;
119 gpointer no_lease_data;
120 GDHCPClientEventFunc lease_lost_cb;
121 gpointer lease_lost_data;
122 GDHCPClientEventFunc ipv4ll_lost_cb;
123 gpointer ipv4ll_lost_data;
124 GDHCPClientEventFunc address_conflict_cb;
125 gpointer address_conflict_data;
126 GDHCPDebugFunc debug_func;
128 GDHCPClientEventFunc information_req_cb;
129 gpointer information_req_data;
130 GDHCPClientEventFunc solicitation_cb;
131 gpointer solicitation_data;
132 GDHCPClientEventFunc advertise_cb;
133 gpointer advertise_data;
134 GDHCPClientEventFunc request_cb;
135 gpointer request_data;
136 GDHCPClientEventFunc renew_cb;
138 GDHCPClientEventFunc rebind_cb;
139 gpointer rebind_data;
140 GDHCPClientEventFunc release_cb;
141 gpointer release_data;
142 GDHCPClientEventFunc confirm_cb;
143 gpointer confirm_data;
144 GDHCPClientEventFunc decline_cb;
145 gpointer decline_data;
149 unsigned char *server_duid;
151 uint16_t status_code;
154 struct in6_addr ia_na;
155 struct in6_addr ia_ta;
159 struct timeval start_time;
163 static inline void debug(GDHCPClient *client, const char *format, ...)
168 if (!client->debug_func)
171 va_start(ap, format);
173 if (vsnprintf(str, sizeof(str), format, ap) > 0)
174 client->debug_func(str, client->debug_data);
179 /* Initialize the packet with the proper defaults */
180 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
182 if (dhcp_client->type == G_DHCP_IPV6)
183 dhcpv6_init_header(pkt, type);
185 struct dhcp_packet *packet = pkt;
187 dhcp_init_header(packet, type);
188 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
192 static void add_request_options(GDHCPClient *dhcp_client,
193 struct dhcp_packet *packet)
198 int end = dhcp_end_option(packet->options);
200 for (list = dhcp_client->request_list; list; list = list->next) {
201 code = (uint8_t) GPOINTER_TO_INT(list->data);
203 packet->options[end + OPT_DATA + len] = code;
208 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
209 packet->options[end + OPT_LEN] = len;
210 packet->options[end + OPT_DATA + len] = DHCP_END;
217 unsigned char **ptr_buf;
220 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
223 uint8_t *option = value;
225 struct hash_params *params = user_data;
227 /* option[0][1] contains option code */
228 len = option[2] << 8 | option[3];
230 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
233 memcpy(*params->ptr_buf, option, len + 2 + 2);
234 (*params->ptr_buf) += len + 2 + 2;
237 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
238 unsigned char *buf, int max_buf,
239 unsigned char **ptr_buf)
241 struct hash_params params = {
247 if (dhcp_client->type != G_DHCP_IPV6)
250 g_hash_table_foreach(dhcp_client->send_value_hash,
251 add_dhcpv6_binary_option, ¶ms);
253 *ptr_buf = *params.ptr_buf;
256 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
260 buf[1] = code & 0xff;
264 memcpy(&buf[4], msg, len);
267 static int32_t get_time_diff(struct timeval *tv)
272 gettimeofday(&now, NULL);
274 hsec = (now.tv_sec - tv->tv_sec) * 100;
275 hsec += (now.tv_usec - tv->tv_usec) / 10000;
280 static void remove_timeouts(GDHCPClient *dhcp_client)
283 if (dhcp_client->timeout > 0)
284 g_source_remove(dhcp_client->timeout);
285 if (dhcp_client->t1_timeout > 0)
286 g_source_remove(dhcp_client->t1_timeout);
287 if (dhcp_client->t2_timeout > 0)
288 g_source_remove(dhcp_client->t2_timeout);
289 if (dhcp_client->lease_timeout > 0)
290 g_source_remove(dhcp_client->lease_timeout);
292 dhcp_client->timeout = 0;
293 dhcp_client->t1_timeout = 0;
294 dhcp_client->t2_timeout = 0;
295 dhcp_client->lease_timeout = 0;
299 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
300 struct dhcpv6_packet *packet,
301 unsigned char *buf, int max_buf,
302 unsigned char **ptr_buf)
305 uint16_t code, value;
310 if (dhcp_client->type != G_DHCP_IPV6)
313 for (list = dhcp_client->request_list; list; list = list->next) {
314 code = (uint16_t) GPOINTER_TO_INT(list->data);
318 case G_DHCPV6_CLIENTID:
319 if (!dhcp_client->duid)
322 len = 2 + 2 + dhcp_client->duid_len;
323 if ((*ptr_buf + len) > (buf + max_buf)) {
324 debug(dhcp_client, "Too long dhcpv6 message "
325 "when writing client id option");
329 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
330 dhcp_client->duid_len, dhcp_client->duid);
335 case G_DHCPV6_SERVERID:
336 if (!dhcp_client->server_duid)
339 len = 2 + 2 + dhcp_client->server_duid_len;
340 if ((*ptr_buf + len) > (buf + max_buf)) {
341 debug(dhcp_client, "Too long dhcpv6 message "
342 "when writing server id option");
346 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
347 dhcp_client->server_duid_len,
348 dhcp_client->server_duid);
353 case G_DHCPV6_RAPID_COMMIT:
355 if ((*ptr_buf + len) > (buf + max_buf)) {
356 debug(dhcp_client, "Too long dhcpv6 message "
357 "when writing rapid commit option");
361 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
369 case G_DHCPV6_ELAPSED_TIME:
370 if (!dhcp_client->retransmit) {
372 * Initial message, elapsed time is 0.
376 diff = get_time_diff(&dhcp_client->start_time);
377 if (diff < 0 || diff > 0xffff)
382 if ((*ptr_buf + len) > (buf + max_buf)) {
383 debug(dhcp_client, "Too long dhcpv6 message "
384 "when writing elapsed time option");
388 value = htons((uint16_t)diff);
389 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
390 2, (uint8_t *)&value);
395 case G_DHCPV6_DNS_SERVERS:
398 case G_DHCPV6_DOMAIN_LIST:
401 case G_DHCPV6_SNTP_SERVERS:
409 debug(dhcp_client, "option %d len %d added", code, len);
413 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
415 uint8_t *option = value;
416 struct dhcp_packet *packet = user_data;
418 dhcp_add_binary_option(packet, option);
421 static void add_send_options(GDHCPClient *dhcp_client,
422 struct dhcp_packet *packet)
424 g_hash_table_foreach(dhcp_client->send_value_hash,
425 add_binary_option, packet);
429 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
430 * represents the number of seconds elapsed from the start of
431 * attempting DHCP to satisfy some DHCP servers that allow for an
432 * "authoritative" reply before responding.
434 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
436 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
439 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
441 struct dhcp_packet packet;
443 debug(dhcp_client, "sending DHCP discover request");
445 init_packet(dhcp_client, &packet, DHCPDISCOVER);
447 packet.xid = dhcp_client->xid;
448 packet.secs = dhcp_attempt_secs(dhcp_client);
451 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
453 /* Explicitly saying that we want RFC-compliant packets helps
454 * some buggy DHCP servers to NOT send bigger packets */
455 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
457 add_request_options(dhcp_client, &packet);
459 add_send_options(dhcp_client, &packet);
462 * If we do not get a reply to DISCOVER packet, then we try with
463 * broadcast flag set. So first packet is sent without broadcast flag,
464 * first retry is with broadcast flag, second retry is without it etc.
465 * Reason is various buggy routers/AP that either eat the other or vice
466 * versa. In the receiving side we then find out what kind of packet
467 * the server can send.
469 dhcp_client->request_bcast = dhcp_client->retry_times % 2;
471 if (dhcp_client->request_bcast)
472 g_hash_table_add(dhcp_client->secs_bcast_hash,
473 GINT_TO_POINTER(packet.secs));
475 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
476 INADDR_BROADCAST, SERVER_PORT,
477 MAC_BCAST_ADDR, dhcp_client->ifindex,
478 dhcp_client->request_bcast);
481 int g_dhcp_client_decline(GDHCPClient *dhcp_client, uint32_t requested)
483 struct dhcp_packet packet;
485 dhcp_client->state = DECLINED;
486 dhcp_client->retry_times = 0;
488 debug(dhcp_client, "sending DHCP decline");
490 init_packet(dhcp_client, &packet, DHCPDECLINE);
492 packet.xid = dhcp_client->xid;
493 packet.secs = dhcp_attempt_secs(dhcp_client);
496 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
498 add_send_options(dhcp_client, &packet);
500 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
501 INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
502 dhcp_client->ifindex, true);
505 static int send_request(GDHCPClient *dhcp_client)
507 struct dhcp_packet packet;
509 debug(dhcp_client, "sending DHCP request (state %d)",
512 init_packet(dhcp_client, &packet, DHCPREQUEST);
514 packet.xid = dhcp_client->xid;
515 packet.secs = dhcp_attempt_secs(dhcp_client);
517 if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
518 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
519 dhcp_client->requested_ip);
521 if (dhcp_client->state == REQUESTING)
522 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
523 dhcp_client->server_ip);
525 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
527 add_request_options(dhcp_client, &packet);
529 add_send_options(dhcp_client, &packet);
531 if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
532 packet.ciaddr = htonl(dhcp_client->requested_ip);
534 if (dhcp_client->state == RENEWING)
535 return dhcp_send_kernel_packet(&packet,
536 dhcp_client->requested_ip, CLIENT_PORT,
537 dhcp_client->server_ip, SERVER_PORT,
538 dhcp_client->interface);
540 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
541 INADDR_BROADCAST, SERVER_PORT,
542 MAC_BCAST_ADDR, dhcp_client->ifindex,
543 dhcp_client->request_bcast);
546 static int send_release(GDHCPClient *dhcp_client,
547 uint32_t server, uint32_t ciaddr)
549 struct dhcp_packet packet;
552 debug(dhcp_client, "sending DHCP release request");
554 init_packet(dhcp_client, &packet, DHCPRELEASE);
555 __connman_util_get_random(&rand);
557 packet.ciaddr = htonl(ciaddr);
559 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
561 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
563 dhcp_client->interface);
566 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
567 static int switch_listening_mode(GDHCPClient *dhcp_client,
568 ListenMode listen_mode);
570 static gboolean send_probe_packet(gpointer dhcp_data)
572 GDHCPClient *dhcp_client;
575 dhcp_client = dhcp_data;
576 /* if requested_ip is not valid, pick a new address*/
577 if (dhcp_client->requested_ip == 0) {
578 debug(dhcp_client, "pick a new random address");
579 dhcp_client->requested_ip = arp_random_ip();
582 debug(dhcp_client, "sending IPV4LL probe request");
584 if (dhcp_client->retry_times == 1) {
585 dhcp_client->state = IPV4LL_PROBE;
586 switch_listening_mode(dhcp_client, L_ARP);
588 arp_send_packet(dhcp_client->mac_address, 0,
589 dhcp_client->requested_ip, dhcp_client->ifindex);
591 if (dhcp_client->retry_times < PROBE_NUM) {
592 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
593 timeout = __connman_util_random_delay_ms(PROBE_MAX-PROBE_MIN);
594 timeout += PROBE_MIN*1000;
596 timeout = (ANNOUNCE_WAIT * 1000);
598 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
600 ipv4ll_probe_timeout,
606 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
607 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
609 static gboolean send_announce_packet(gpointer dhcp_data)
611 GDHCPClient *dhcp_client;
613 dhcp_client = dhcp_data;
615 debug(dhcp_client, "sending IPV4LL announce request");
617 arp_send_packet(dhcp_client->mac_address,
618 dhcp_client->requested_ip,
619 dhcp_client->requested_ip,
620 dhcp_client->ifindex);
622 remove_timeouts(dhcp_client);
624 if (dhcp_client->state == IPV4LL_DEFEND) {
625 dhcp_client->timeout =
626 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
628 ipv4ll_defend_timeout,
633 dhcp_client->timeout =
634 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
636 ipv4ll_announce_timeout,
642 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
647 dhcp_client->retransmit = true;
650 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
655 dhcp_client->retransmit = false;
658 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
659 unsigned char **duid, int *duid_len)
664 case G_DHCPV6_DUID_LLT:
665 *duid_len = 2 + 2 + 4 + ETH_ALEN;
666 *duid = g_try_malloc(*duid_len);
672 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
675 duid_time = time(NULL) - DUID_TIME_EPOCH;
676 (*duid)[4] = duid_time >> 24;
677 (*duid)[5] = duid_time >> 16;
678 (*duid)[6] = duid_time >> 8;
679 (*duid)[7] = duid_time & 0xff;
681 case G_DHCPV6_DUID_EN:
683 case G_DHCPV6_DUID_LL:
684 *duid_len = 2 + 2 + ETH_ALEN;
685 *duid = g_try_malloc(*duid_len);
691 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2]);
700 static gchar *convert_to_hex(unsigned char *buf, int len)
702 gchar *ret = g_try_malloc(len * 2 + 1);
705 for (i = 0; ret && i < len; i++)
706 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
711 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
714 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
717 g_free(dhcp_client->duid);
719 dhcp_client->duid = duid;
720 dhcp_client->duid_len = duid_len;
722 if (dhcp_client->debug_func) {
723 gchar *hex = convert_to_hex(duid, duid_len);
724 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
731 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
732 uint32_t *T2, GSList *prefixes)
734 uint8_t options[1452];
735 unsigned int max_buf = sizeof(options);
736 int len, count = g_slist_length(prefixes);
738 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
741 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
743 memset(options, 0, sizeof(options));
745 options[0] = dhcp_client->iaid >> 24;
746 options[1] = dhcp_client->iaid >> 16;
747 options[2] = dhcp_client->iaid >> 8;
748 options[3] = dhcp_client->iaid;
751 uint32_t t = htonl(*T1);
752 memcpy(&options[4], &t, 4);
756 uint32_t t = htonl(*T2);
757 memcpy(&options[8], &t, 4);
765 for (list = prefixes; list; list = list->next) {
766 GDHCPIAPrefix *prefix = list->data;
767 uint8_t sub_option[4+4+1+16];
769 if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
771 "Too long dhcpv6 message "
772 "when writing IA prefix option");
776 memset(&sub_option, 0, sizeof(sub_option));
778 /* preferred and validity time are left zero */
780 sub_option[8] = prefix->prefixlen;
781 memcpy(&sub_option[9], &prefix->prefix, 16);
783 copy_option(&options[len], G_DHCPV6_IA_PREFIX,
784 sizeof(sub_option), sub_option);
785 len += 2 + 2 + sizeof(sub_option);
789 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
795 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
797 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
800 return dhcp_client->iaid;
803 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
805 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
808 dhcp_client->iaid = iaid;
811 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
816 __connman_inet_get_interface_mac_address(index, buf);
818 memcpy(iaid, &buf[2], 4);
819 dhcp_client->iaid = iaid[0] << 24 |
820 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
823 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
824 uint32_t *T1, uint32_t *T2,
828 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
832 *T1 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
836 *T2 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
840 *started = dhcp_client->last_request;
843 *expire = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
844 dhcp_client->last_request + dhcp_client->expire;
849 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
853 buf[1] = G_DHCPV6_IAADDR;
856 memcpy(&buf[4], &dhcp_client->ia_na, 16);
857 memset(&buf[20], 0, 4); /* preferred */
858 memset(&buf[24], 0, 4); /* valid */
862 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
865 struct in6_addr addr;
867 if (inet_pton(AF_INET6, address, &addr) != 1)
871 buf[1] = G_DHCPV6_IAADDR;
874 memcpy(&buf[4], &addr, 16);
875 memset(&buf[20], 0, 4); /* preferred */
876 memset(&buf[24], 0, 4); /* valid */
880 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
884 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
886 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
896 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
897 int code, uint32_t *T1, uint32_t *T2,
898 bool add_iaaddr, const char *ia_na)
900 if (code == G_DHCPV6_IA_TA) {
901 uint8_t ia_options[4];
903 put_iaid(dhcp_client, index, ia_options);
905 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
906 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
907 ia_options, sizeof(ia_options));
909 } else if (code == G_DHCPV6_IA_NA) {
910 struct in6_addr addr;
912 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
915 * If caller has specified the IPv6 address it wishes to
916 * to use (ia_na != NULL and address is valid), then send
917 * the address to server.
918 * If caller did not specify the address (ia_na == NULL) and
919 * if the current address is not set, then we should not send
920 * the address sub-option.
922 if (add_iaaddr && ((!ia_na &&
923 !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
925 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
926 #define IAADDR_LEN (16+4+4)
927 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
930 memcpy(&dhcp_client->ia_na, &addr,
931 sizeof(struct in6_addr));
933 put_iaid(dhcp_client, index, ia_options);
936 ia_options[4] = *T1 >> 24;
937 ia_options[5] = *T1 >> 16;
938 ia_options[6] = *T1 >> 8;
941 memset(&ia_options[4], 0x00, 4);
944 ia_options[8] = *T2 >> 24;
945 ia_options[9] = *T2 >> 16;
946 ia_options[10] = *T2 >> 8;
947 ia_options[11] = *T2;
949 memset(&ia_options[8], 0x00, 4);
951 create_iaaddr(dhcp_client, &ia_options[12],
954 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
955 ia_options, sizeof(ia_options));
957 uint8_t ia_options[4+4+4];
959 put_iaid(dhcp_client, index, ia_options);
961 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
962 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
964 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
965 ia_options, sizeof(ia_options));
974 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
975 int code, uint32_t *T1, uint32_t *T2,
979 uint8_t *ia_options, *pos;
980 int len, count, total_len;
982 count = g_slist_length(addresses);
986 g_dhcp_client_set_request(dhcp_client, code);
988 if (code == G_DHCPV6_IA_TA)
990 else if (code == G_DHCPV6_IA_NA)
991 len = 4 + 4 + 4; /* IAID + T1 + T2 */
995 total_len = len + count * (2 + 2 + 16 + 4 + 4);
996 ia_options = g_try_malloc0(total_len);
1000 put_iaid(dhcp_client, index, ia_options);
1002 pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
1004 for (list = addresses; list; list = list->next) {
1005 pos = append_iaaddr(dhcp_client, pos, list->data);
1010 if (code == G_DHCPV6_IA_NA) {
1012 ia_options[4] = *T1 >> 24;
1013 ia_options[5] = *T1 >> 16;
1014 ia_options[6] = *T1 >> 8;
1015 ia_options[7] = *T1;
1017 memset(&ia_options[4], 0x00, 4);
1020 ia_options[8] = *T2 >> 24;
1021 ia_options[9] = *T2 >> 16;
1022 ia_options[10] = *T2 >> 8;
1023 ia_options[11] = *T2;
1025 memset(&ia_options[8], 0x00, 4);
1028 g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1035 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1038 int i, j, len = sizeof(uint16_t) * args;
1041 values = g_try_malloc(len);
1046 for (i = 0, j = 0; i < args; i++) {
1047 uint16_t value = va_arg(va, int);
1048 values[j++] = value >> 8;
1049 values[j++] = value & 0xff;
1053 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1060 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1062 struct dhcpv6_packet *packet;
1063 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1067 memset(buf, 0, sizeof(buf));
1068 packet = (struct dhcpv6_packet *)&buf[0];
1069 ptr = buf + sizeof(struct dhcpv6_packet);
1071 init_packet(dhcp_client, packet, type);
1073 if (!dhcp_client->retransmit) {
1074 dhcp_client->xid = packet->transaction_id[0] << 16 |
1075 packet->transaction_id[1] << 8 |
1076 packet->transaction_id[2];
1077 gettimeofday(&dhcp_client->start_time, NULL);
1079 packet->transaction_id[0] = dhcp_client->xid >> 16;
1080 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1081 packet->transaction_id[2] = dhcp_client->xid;
1084 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1086 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1089 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1091 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1093 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1095 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1097 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1101 static int send_solicitation(GDHCPClient *dhcp_client)
1103 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1106 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1108 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1111 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1113 return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1116 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1118 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1121 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1123 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1126 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1128 return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1131 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1133 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1136 static int send_information_req(GDHCPClient *dhcp_client)
1138 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1142 static void remove_value(gpointer data, gpointer user_data)
1148 static void remove_option_value(gpointer data)
1150 GList *option_value = data;
1152 g_list_foreach(option_value, remove_value, NULL);
1153 g_list_free(option_value);
1156 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1157 int ifindex, GDHCPClientError *error)
1159 GDHCPClient *dhcp_client;
1162 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1166 dhcp_client = g_try_new0(GDHCPClient, 1);
1168 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1172 dhcp_client->interface = get_interface_name(ifindex);
1173 if (!dhcp_client->interface) {
1174 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1178 if (!interface_is_up(ifindex)) {
1179 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1183 __connman_inet_get_interface_mac_address(ifindex, dhcp_client->mac_address);
1185 dhcp_client->listener_sockfd = -1;
1186 dhcp_client->listen_mode = L_NONE;
1187 dhcp_client->ref_count = 1;
1188 dhcp_client->type = type;
1189 dhcp_client->ifindex = ifindex;
1190 dhcp_client->lease_available_cb = NULL;
1191 dhcp_client->ipv4ll_available_cb = NULL;
1192 dhcp_client->no_lease_cb = NULL;
1193 dhcp_client->lease_lost_cb = NULL;
1194 dhcp_client->ipv4ll_lost_cb = NULL;
1195 dhcp_client->address_conflict_cb = NULL;
1196 dhcp_client->listener_watch = 0;
1197 dhcp_client->retry_times = 0;
1198 dhcp_client->ack_retry_times = 0;
1199 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1200 g_direct_equal, NULL, remove_option_value);
1201 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1202 g_direct_equal, NULL, g_free);
1203 dhcp_client->secs_bcast_hash = g_hash_table_new(g_direct_hash,
1205 dhcp_client->request_list = NULL;
1206 dhcp_client->require_list = NULL;
1207 dhcp_client->duid = NULL;
1208 dhcp_client->duid_len = 0;
1209 dhcp_client->last_request = time(NULL);
1210 dhcp_client->expire = 0;
1211 dhcp_client->request_bcast = false;
1213 *error = G_DHCP_CLIENT_ERROR_NONE;
1218 g_free(dhcp_client->interface);
1219 g_free(dhcp_client);
1223 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1225 static int dhcp_l2_socket(int ifindex)
1228 struct sockaddr_ll sock;
1233 * I've selected not to see LL header, so BPF doesn't see it, too.
1234 * The filter may also pass non-IP and non-ARP packets, but we do
1235 * a more complete check when receiving the message in userspace.
1237 * and filter shamelessly stolen from:
1239 * http://www.flamewarmaster.de/software/dhcpclient/
1241 * There are a few other interesting ideas on that page (look under
1242 * "Motivation"). Use of netlink events is most interesting. Think
1243 * of various network servers listening for events and reconfiguring.
1244 * That would obsolete sending HUP signals and/or make use of restarts.
1246 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1249 * TODO: make conditional?
1251 static const struct sock_filter filter_instr[] = {
1253 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1254 /* L5, L1, is UDP? */
1255 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1256 /* ugly check for arp on ethernet-like and IPv4 */
1257 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1258 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1259 /* skip IP header */
1260 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1261 /* check udp source and destination ports */
1262 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1264 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1266 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1267 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1270 static const struct sock_fprog filter_prog = {
1271 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1272 /* casting const away: */
1273 .filter = (struct sock_filter *) filter_instr,
1276 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1280 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1281 /* Use only if standard ports are in use */
1282 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1283 sizeof(filter_prog));
1285 memset(&sock, 0, sizeof(sock));
1286 sock.sll_family = AF_PACKET;
1287 sock.sll_protocol = htons(ETH_P_IP);
1288 sock.sll_ifindex = ifindex;
1290 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1299 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1301 if (packet->ip.protocol != IPPROTO_UDP)
1304 if (packet->ip.version != IPVERSION)
1307 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1310 if (packet->udp.dest != htons(CLIENT_PORT))
1313 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1319 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1320 struct sockaddr_in *dst_addr)
1323 struct ip_udp_dhcp_packet packet;
1326 memset(&packet, 0, sizeof(packet));
1328 bytes = read(fd, &packet, sizeof(packet));
1332 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1335 if (bytes < ntohs(packet.ip.tot_len))
1336 /* packet is bigger than sizeof(packet), we did partial read */
1339 /* ignore any extra garbage bytes */
1340 bytes = ntohs(packet.ip.tot_len);
1342 if (!sanity_check(&packet, bytes))
1345 check = packet.ip.check;
1346 packet.ip.check = 0;
1347 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1350 /* verify UDP checksum. IP header has to be modified for this */
1351 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1352 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1353 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1354 check = packet.udp.check;
1355 packet.udp.check = 0;
1356 if (check && check != dhcp_checksum(&packet, bytes))
1359 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1360 sizeof(packet.udp)));
1362 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1365 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1367 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1370 static void ipv4ll_start(GDHCPClient *dhcp_client)
1374 remove_timeouts(dhcp_client);
1376 switch_listening_mode(dhcp_client, L_NONE);
1377 dhcp_client->retry_times = 0;
1378 dhcp_client->requested_ip = 0;
1380 dhcp_client->requested_ip = arp_random_ip();
1382 /*first wait a random delay to avoid storm of arp request on boot*/
1383 timeout = __connman_util_random_delay_ms(PROBE_WAIT);
1385 dhcp_client->retry_times++;
1386 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1393 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1396 switch_listening_mode(dhcp_client, L_NONE);
1398 remove_timeouts(dhcp_client);
1400 if (dhcp_client->listener_watch > 0) {
1401 g_source_remove(dhcp_client->listener_watch);
1402 dhcp_client->listener_watch = 0;
1405 dhcp_client->state = IPV4LL_PROBE;
1406 dhcp_client->retry_times = 0;
1407 dhcp_client->requested_ip = 0;
1409 g_free(dhcp_client->assigned_ip);
1410 dhcp_client->assigned_ip = NULL;
1413 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1416 struct ether_arp arp;
1417 uint32_t ip_requested;
1418 int source_conflict;
1419 int target_conflict;
1422 memset(&arp, 0, sizeof(arp));
1423 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1427 if (arp.arp_op != htons(ARPOP_REPLY) &&
1428 arp.arp_op != htons(ARPOP_REQUEST))
1431 if (memcmp(arp.arp_sha, dhcp_client->mac_address, ETH_ALEN) == 0)
1434 ip_requested = htonl(dhcp_client->requested_ip);
1435 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1436 sizeof(ip_requested));
1438 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1439 sizeof(ip_requested));
1441 if (!source_conflict && !target_conflict)
1444 dhcp_client->conflicts++;
1446 debug(dhcp_client, "IPV4LL conflict detected");
1448 if (dhcp_client->state == IPV4LL_MONITOR) {
1449 if (!source_conflict)
1451 dhcp_client->state = IPV4LL_DEFEND;
1452 debug(dhcp_client, "DEFEND mode conflicts : %d",
1453 dhcp_client->conflicts);
1454 /*Try to defend with a single announce*/
1455 send_announce_packet(dhcp_client);
1459 if (dhcp_client->state == IPV4LL_DEFEND) {
1460 if (!source_conflict)
1462 else if (dhcp_client->ipv4ll_lost_cb)
1463 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1464 dhcp_client->ipv4ll_lost_data);
1467 ipv4ll_stop(dhcp_client);
1469 /* If we got a lot of conflicts, RFC3927 states that we have
1470 * to wait RATE_LIMIT_INTERVAL before retrying,
1472 if (dhcp_client->conflicts < MAX_CONFLICTS)
1473 timeout_ms = __connman_util_random_delay_ms(PROBE_WAIT);
1475 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
1476 dhcp_client->retry_times++;
1477 dhcp_client->timeout =
1478 g_timeout_add_full(G_PRIORITY_HIGH,
1487 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1489 if (dhcp_client->type == G_DHCP_IPV6) {
1490 struct dhcpv6_packet *packet6 = pkt;
1496 xid = packet6->transaction_id[0] << 16 |
1497 packet6->transaction_id[1] << 8 |
1498 packet6->transaction_id[2];
1500 if (xid != dhcp_client->xid)
1503 struct dhcp_packet *packet = pkt;
1505 if (packet->xid != dhcp_client->xid)
1508 if (packet->hlen != 6)
1511 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1518 static void start_request(GDHCPClient *dhcp_client);
1520 static gboolean request_timeout(gpointer user_data)
1522 GDHCPClient *dhcp_client = user_data;
1524 debug(dhcp_client, "request timeout (retries %d)",
1525 dhcp_client->retry_times);
1527 dhcp_client->retry_times++;
1529 start_request(dhcp_client);
1534 static void listener_watch_destroy(gpointer user_data)
1536 GDHCPClient *dhcp_client = user_data;
1537 g_dhcp_client_unref(dhcp_client);
1540 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1541 gpointer user_data);
1543 static int switch_listening_mode(GDHCPClient *dhcp_client,
1544 ListenMode listen_mode)
1546 GIOChannel *listener_channel;
1547 int listener_sockfd;
1549 if (dhcp_client->listen_mode == listen_mode)
1552 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1553 dhcp_client->listen_mode, listen_mode);
1555 if (dhcp_client->listen_mode != L_NONE) {
1556 if (dhcp_client->listener_watch > 0)
1557 g_source_remove(dhcp_client->listener_watch);
1558 dhcp_client->listen_mode = L_NONE;
1559 dhcp_client->listener_sockfd = -1;
1560 dhcp_client->listener_watch = 0;
1563 if (listen_mode == L_NONE)
1566 if (listen_mode == L2)
1567 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1568 else if (listen_mode == L3) {
1569 if (dhcp_client->type == G_DHCP_IPV6)
1570 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1571 dhcp_client->interface,
1574 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1575 dhcp_client->interface,
1577 } else if (listen_mode == L_ARP)
1578 listener_sockfd = arp_socket(dhcp_client->ifindex);
1582 if (listener_sockfd < 0)
1585 listener_channel = g_io_channel_unix_new(listener_sockfd);
1586 if (!listener_channel) {
1587 /* Failed to create listener channel */
1588 close(listener_sockfd);
1592 dhcp_client->listen_mode = listen_mode;
1593 dhcp_client->listener_sockfd = listener_sockfd;
1595 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1596 dhcp_client->listener_watch =
1597 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1598 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1599 listener_event, g_dhcp_client_ref(dhcp_client),
1600 listener_watch_destroy);
1601 g_io_channel_unref(listener_channel);
1606 static void start_request(GDHCPClient *dhcp_client)
1608 debug(dhcp_client, "start request (retries %d)",
1609 dhcp_client->retry_times);
1611 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1612 if (dhcp_client->no_lease_cb)
1613 dhcp_client->no_lease_cb(dhcp_client,
1614 dhcp_client->no_lease_data);
1618 if (dhcp_client->retry_times == 0) {
1619 dhcp_client->state = REQUESTING;
1620 switch_listening_mode(dhcp_client, L2);
1623 send_request(dhcp_client);
1625 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1632 static uint32_t get_lease(struct dhcp_packet *packet)
1635 uint32_t lease_seconds;
1637 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1641 lease_seconds = get_be32(option);
1643 if (lease_seconds < 10)
1646 return lease_seconds;
1649 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1651 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1653 remove_timeouts(dhcp_client);
1655 dhcp_client->retry_times = retry_times;
1656 dhcp_client->requested_ip = 0;
1657 dhcp_client->state = INIT_SELECTING;
1658 switch_listening_mode(dhcp_client, L2);
1660 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1663 static gboolean start_expire(gpointer user_data)
1665 GDHCPClient *dhcp_client = user_data;
1667 debug(dhcp_client, "lease expired");
1669 /*remove all timeouts if they are set*/
1670 remove_timeouts(dhcp_client);
1672 restart_dhcp(dhcp_client, 0);
1674 /* ip need to be cleared */
1675 if (dhcp_client->lease_lost_cb)
1676 dhcp_client->lease_lost_cb(dhcp_client,
1677 dhcp_client->lease_lost_data);
1682 static gboolean continue_rebound(gpointer user_data)
1684 GDHCPClient *dhcp_client = user_data;
1687 switch_listening_mode(dhcp_client, L2);
1688 send_request(dhcp_client);
1690 if (dhcp_client->t2_timeout> 0) {
1691 g_source_remove(dhcp_client->t2_timeout);
1692 dhcp_client->t2_timeout = 0;
1695 /*recalculate remaining rebind time*/
1696 dhcp_client->T2 >>= 1;
1697 if (dhcp_client->T2 > 60) {
1698 __connman_util_get_random(&rand);
1699 dhcp_client->t2_timeout =
1700 g_timeout_add_full(G_PRIORITY_HIGH,
1701 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1710 static gboolean start_rebound(gpointer user_data)
1712 GDHCPClient *dhcp_client = user_data;
1714 /*remove renew timer*/
1715 if (dhcp_client->t1_timeout > 0)
1716 g_source_remove(dhcp_client->t1_timeout);
1718 debug(dhcp_client, "start rebound");
1719 dhcp_client->state = REBINDING;
1721 /*calculate total rebind time*/
1722 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1724 /*send the first rebound and reschedule*/
1725 continue_rebound(user_data);
1730 static gboolean continue_renew (gpointer user_data)
1732 GDHCPClient *dhcp_client = user_data;
1735 switch_listening_mode(dhcp_client, L3);
1736 send_request(dhcp_client);
1738 if (dhcp_client->t1_timeout > 0)
1739 g_source_remove(dhcp_client->t1_timeout);
1741 dhcp_client->t1_timeout = 0;
1743 dhcp_client->T1 >>= 1;
1745 if (dhcp_client->T1 > 60) {
1746 __connman_util_get_random(&rand);
1747 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1748 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1756 static gboolean start_renew(gpointer user_data)
1758 GDHCPClient *dhcp_client = user_data;
1760 debug(dhcp_client, "start renew");
1761 dhcp_client->state = RENEWING;
1763 /*calculate total renew period*/
1764 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1766 /*send first renew and reschedule for half the remaining time.*/
1767 continue_renew(user_data);
1772 static void start_bound(GDHCPClient *dhcp_client)
1774 debug(dhcp_client, "start bound");
1776 dhcp_client->state = BOUND;
1778 remove_timeouts(dhcp_client);
1781 *TODO: T1 and T2 should be set through options instead of
1782 * defaults as they are here.
1785 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1786 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1787 dhcp_client->expire = dhcp_client->lease_seconds;
1789 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1791 start_renew, dhcp_client,
1794 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1796 start_rebound, dhcp_client,
1799 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1800 dhcp_client->expire,
1801 start_expire, dhcp_client,
1805 static gboolean restart_dhcp_timeout(gpointer user_data)
1807 GDHCPClient *dhcp_client = user_data;
1809 debug(dhcp_client, "restart DHCP timeout");
1811 if (dhcp_client->state == REBOOTING) {
1812 g_free(dhcp_client->last_address);
1813 dhcp_client->last_address = NULL;
1814 restart_dhcp(dhcp_client, 0);
1816 dhcp_client->ack_retry_times++;
1817 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1822 static char *get_ip(uint32_t ip)
1824 struct in_addr addr;
1828 return g_strdup(inet_ntoa(addr));
1831 /* get a rough idea of how long an option will be */
1832 static const uint8_t len_of_option_as_string[] = {
1833 [OPTION_IP] = sizeof("255.255.255.255 "),
1834 [OPTION_STRING] = 1,
1835 [OPTION_U8] = sizeof("255 "),
1836 [OPTION_U16] = sizeof("65535 "),
1837 [OPTION_U32] = sizeof("4294967295 "),
1840 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1842 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1845 /* Create "opt_value1 option_value2 ..." string */
1846 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1848 unsigned upper_length;
1852 len = option[OPT_LEN - OPT_DATA];
1853 type &= OPTION_TYPE_MASK;
1854 optlen = dhcp_option_lengths[type];
1857 upper_length = len_of_option_as_string[type] *
1858 ((unsigned)len / (unsigned)optlen);
1859 dest = ret = g_malloc(upper_length + 1);
1863 while (len >= optlen) {
1866 dest += sprint_nip(dest, "", option);
1869 uint16_t val_u16 = get_be16(option);
1870 dest += sprintf(dest, "%u", val_u16);
1874 uint32_t val_u32 = get_be32(option);
1875 dest += sprintf(dest, "%u", val_u32);
1879 memcpy(dest, option, len);
1896 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1904 if (type == OPTION_STRING)
1905 return g_list_append(list, g_strdup(value));
1907 while ((pos = strchr(pos, ' '))) {
1910 list = g_list_append(list, g_strdup(value));
1915 list = g_list_append(list, g_strdup(value));
1920 static inline uint32_t get_uint32(unsigned char *value)
1922 return value[0] << 24 | value[1] << 16 |
1923 value[2] << 8 | value[3];
1926 static inline uint16_t get_uint16(unsigned char *value)
1928 return value[0] << 8 | value[1];
1931 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1932 struct in6_addr *addr,
1933 unsigned char prefixlen, uint32_t preferred,
1936 GDHCPIAPrefix *ia_prefix;
1938 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1942 if (dhcp_client->debug_func) {
1943 char addr_str[INET6_ADDRSTRLEN + 1];
1944 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1945 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1946 addr_str, prefixlen, preferred, valid);
1949 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1950 ia_prefix->prefixlen = prefixlen;
1951 ia_prefix->preferred = preferred;
1952 ia_prefix->valid = valid;
1953 ia_prefix->expire = time(NULL) + valid;
1955 return g_list_prepend(list, ia_prefix);
1958 static GList *get_addresses(GDHCPClient *dhcp_client,
1960 unsigned char *value,
1964 struct in6_addr addr;
1965 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1966 uint16_t option_len, option_code, st = 0, max_len;
1967 int addr_count = 0, prefix_count = 0, i, pos;
1968 unsigned char prefixlen;
1969 unsigned int shortest_valid = 0;
1973 if (!value || len < 4)
1976 iaid = get_uint32(&value[0]);
1977 if (dhcp_client->iaid != iaid)
1980 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
1981 T1 = get_uint32(&value[4]);
1982 T2 = get_uint32(&value[8]);
1985 /* IA_NA: RFC 3315, 22.4 */
1986 /* IA_PD: RFC 3633, ch 9 */
1996 max_len = len - pos;
1998 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
2000 /* We have more sub-options in this packet. */
2002 option = dhcpv6_get_sub_option(&value[pos], max_len,
2003 &option_code, &option_len);
2005 debug(dhcp_client, "pos %d option %p code %d len %d",
2006 pos, option, option_code, option_len);
2014 switch (option_code) {
2015 case G_DHCPV6_IAADDR:
2017 memcpy(&addr, &option[0], sizeof(addr));
2019 preferred = get_uint32(&option[i]);
2021 valid = get_uint32(&option[i]);
2026 case G_DHCPV6_STATUS_CODE:
2027 st = get_uint16(&option[0]);
2028 debug(dhcp_client, "error code %d", st);
2029 if (option_len > 2) {
2030 str = g_strndup((gchar *)&option[2],
2032 debug(dhcp_client, "error text: %s", str);
2039 case G_DHCPV6_IA_PREFIX:
2041 preferred = get_uint32(&option[i]);
2043 valid = get_uint32(&option[i]);
2045 prefixlen = option[i];
2047 memcpy(&addr, &option[i], sizeof(addr));
2049 if (preferred < valid) {
2050 /* RFC 3633, ch 10 */
2051 list = add_prefix(dhcp_client, list, &addr,
2052 prefixlen, preferred, valid);
2053 if (shortest_valid > valid)
2054 shortest_valid = valid;
2060 pos += 2 + 2 + option_len;
2062 } while (pos < len);
2064 if (addr_count > 0 && st == 0) {
2065 /* We only support one address atm */
2066 char addr_str[INET6_ADDRSTRLEN + 1];
2068 if (preferred > valid)
2069 /* RFC 3315, 22.6 */
2072 dhcp_client->T1 = T1;
2073 dhcp_client->T2 = T2;
2075 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2076 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2077 addr_count, addr_str, T1, T2);
2079 list = g_list_append(list, g_strdup(addr_str));
2081 if (code == G_DHCPV6_IA_NA)
2082 memcpy(&dhcp_client->ia_na, &addr,
2083 sizeof(struct in6_addr));
2085 memcpy(&dhcp_client->ia_ta, &addr,
2086 sizeof(struct in6_addr));
2088 if (valid != dhcp_client->expire)
2089 dhcp_client->expire = valid;
2092 if (prefix_count > 0 && list) {
2094 * This means we have a list of prefixes to delegate.
2096 list = g_list_reverse(list);
2098 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2099 prefix_count, T1, T2);
2101 dhcp_client->T1 = T1;
2102 dhcp_client->T2 = T2;
2104 dhcp_client->expire = shortest_valid;
2107 if (status && *status != 0)
2108 debug(dhcp_client, "status %d", *status);
2113 static GList *get_domains(int maxlen, unsigned char *value)
2119 char dns_name[NS_MAXDNAME + 1];
2121 if (!value || maxlen < 3)
2124 while (pos < maxlen) {
2125 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2127 c = (unsigned char *)dns_name;
2134 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2135 pos += (char *)c - dns_name + 1;
2138 return g_list_reverse(list);
2141 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2143 unsigned char *value,
2154 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2155 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2158 "%s server list length (%d) is invalid",
2159 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2163 for (i = 0; i < len; i += 16) {
2165 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2169 if (!inet_ntop(AF_INET6, &value[i], str,
2173 list = g_list_append(list, str);
2177 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2178 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2179 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2180 list = get_addresses(dhcp_client, code, len, value, status);
2183 case G_DHCPV6_DOMAIN_LIST:
2184 list = get_domains(len, value);
2194 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2195 struct dhcpv6_packet *packet,
2196 uint16_t pkt_len, uint16_t *status)
2198 GList *list, *value_list;
2201 uint16_t option_len;
2203 for (list = dhcp_client->request_list; list; list = list->next) {
2204 code = (uint16_t) GPOINTER_TO_INT(list->data);
2206 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2209 g_hash_table_remove(dhcp_client->code_value_hash,
2210 GINT_TO_POINTER((int) code));
2214 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2215 option_len, option, status);
2217 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2218 option_len, value_list);
2221 g_hash_table_remove(dhcp_client->code_value_hash,
2222 GINT_TO_POINTER((int) code));
2224 g_hash_table_insert(dhcp_client->code_value_hash,
2225 GINT_TO_POINTER((int) code), value_list);
2229 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2231 GDHCPOptionType type;
2232 GList *list, *value_list;
2237 for (list = dhcp_client->request_list; list; list = list->next) {
2238 code = (uint8_t) GPOINTER_TO_INT(list->data);
2240 option = dhcp_get_option(packet, code);
2242 g_hash_table_remove(dhcp_client->code_value_hash,
2243 GINT_TO_POINTER((int) code));
2247 type = dhcp_get_code_type(code);
2249 option_value = malloc_option_value_string(option, type);
2251 g_hash_table_remove(dhcp_client->code_value_hash,
2252 GINT_TO_POINTER((int) code));
2254 value_list = get_option_value_list(option_value, type);
2256 g_free(option_value);
2259 g_hash_table_remove(dhcp_client->code_value_hash,
2260 GINT_TO_POINTER((int) code));
2262 g_hash_table_insert(dhcp_client->code_value_hash,
2263 GINT_TO_POINTER((int) code), value_list);
2267 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2270 GDHCPClient *dhcp_client = user_data;
2271 struct sockaddr_in dst_addr = { 0 };
2272 struct dhcp_packet packet;
2273 struct dhcpv6_packet *packet6 = NULL;
2274 uint8_t *message_type = NULL, *client_id = NULL, *option,
2276 uint16_t option_len = 0, status = 0;
2279 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2280 uint16_t pkt_len = 0;
2284 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2285 dhcp_client->listener_watch = 0;
2289 if (dhcp_client->listen_mode == L_NONE)
2294 dhcp_client->status_code = 0;
2296 if (dhcp_client->listen_mode == L2) {
2297 re = dhcp_recv_l2_packet(&packet,
2298 dhcp_client->listener_sockfd,
2301 } else if (dhcp_client->listen_mode == L3) {
2302 if (dhcp_client->type == G_DHCP_IPV6) {
2303 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2304 dhcp_client->listener_sockfd);
2309 xid = packet6->transaction_id[0] << 16 |
2310 packet6->transaction_id[1] << 8 |
2311 packet6->transaction_id[2];
2313 re = dhcp_recv_l3_packet(&packet,
2314 dhcp_client->listener_sockfd);
2317 } else if (dhcp_client->listen_mode == L_ARP) {
2318 ipv4ll_recv_arp_packet(dhcp_client);
2326 if (!check_package_owner(dhcp_client, pkt))
2329 if (dhcp_client->type == G_DHCP_IPV6) {
2334 client_id = dhcpv6_get_option(packet6, pkt_len,
2335 G_DHCPV6_CLIENTID, &option_len, &count);
2337 if (!client_id || count == 0 || option_len == 0 ||
2338 memcmp(dhcp_client->duid, client_id,
2339 dhcp_client->duid_len) != 0) {
2341 "client duid error, discarding msg %p/%d/%d",
2342 client_id, option_len, count);
2346 option = dhcpv6_get_option(packet6, pkt_len,
2347 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2348 if (option != 0 && option_len > 0) {
2349 status = option[0]<<8 | option[1];
2351 debug(dhcp_client, "error code %d", status);
2352 if (option_len > 2) {
2353 gchar *txt = g_strndup(
2354 (gchar *)&option[2],
2356 debug(dhcp_client, "error text: %s",
2361 dhcp_client->status_code = status;
2364 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2369 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2370 "(current state %d)", ntohl(xid), dhcp_client->state);
2372 switch (dhcp_client->state) {
2373 case INIT_SELECTING:
2374 if (*message_type != DHCPOFFER)
2377 remove_timeouts(dhcp_client);
2378 dhcp_client->timeout = 0;
2379 dhcp_client->retry_times = 0;
2381 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2382 dhcp_client->server_ip = get_be32(option);
2383 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2385 dhcp_client->state = REQUESTING;
2390 * If unicasting is not possible, the message MAY be
2391 * sent as an IP broadcast using an IP broadcast address
2392 * (preferably 0xffffffff) as the IP destination address
2393 * and the link-layer broadcast address as the link-layer
2394 * destination address.
2396 * For interoperability reasons, if the response is an IP
2397 * broadcast, let's reuse broadcast flag from DHCPDISCOVER
2398 * to which the server has responded. Some servers are picky
2401 dhcp_client->request_bcast =
2402 dst_addr.sin_addr.s_addr == INADDR_BROADCAST &&
2403 g_hash_table_contains(dhcp_client->secs_bcast_hash,
2404 GINT_TO_POINTER(packet.secs));
2406 debug(dhcp_client, "init ip %s secs %hu -> broadcast flag %s",
2407 inet_ntoa(dst_addr.sin_addr), packet.secs,
2408 dhcp_client->request_bcast ? "on" : "off");
2410 start_request(dhcp_client);
2414 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2415 dhcp_client->request_bcast = true;
2417 dhcp_client->request_bcast = false;
2419 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2420 inet_ntoa(dst_addr.sin_addr),
2421 dhcp_client->request_bcast ? "" : "not ");
2426 if (*message_type == DHCPACK) {
2427 dhcp_client->retry_times = 0;
2429 remove_timeouts(dhcp_client);
2431 dhcp_client->lease_seconds = get_lease(&packet);
2433 get_request(dhcp_client, &packet);
2435 switch_listening_mode(dhcp_client, L_NONE);
2437 g_free(dhcp_client->assigned_ip);
2438 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2440 if (dhcp_client->state == REBOOTING) {
2441 option = dhcp_get_option(&packet,
2443 dhcp_client->server_ip = get_be32(option);
2446 /* Address should be set up here */
2447 if (dhcp_client->lease_available_cb)
2448 dhcp_client->lease_available_cb(dhcp_client,
2449 dhcp_client->lease_available_data);
2451 start_bound(dhcp_client);
2452 } else if (*message_type == DHCPNAK) {
2453 dhcp_client->retry_times = 0;
2455 remove_timeouts(dhcp_client);
2457 dhcp_client->timeout = g_timeout_add_seconds_full(
2459 restart_dhcp_timeout,
2466 if (dhcp_client->type != G_DHCP_IPV6)
2469 if (packet6->message != DHCPV6_REPLY &&
2470 packet6->message != DHCPV6_ADVERTISE)
2474 server_id = dhcpv6_get_option(packet6, pkt_len,
2475 G_DHCPV6_SERVERID, &option_len, &count);
2476 if (!server_id || count != 1 || option_len == 0) {
2477 /* RFC 3315, 15.10 */
2479 "server duid error, discarding msg %p/%d/%d",
2480 server_id, option_len, count);
2483 dhcp_client->server_duid = g_try_malloc(option_len);
2484 if (!dhcp_client->server_duid)
2486 memcpy(dhcp_client->server_duid, server_id, option_len);
2487 dhcp_client->server_duid_len = option_len;
2489 if (packet6->message == DHCPV6_REPLY) {
2490 uint8_t *rapid_commit;
2493 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2494 G_DHCPV6_RAPID_COMMIT,
2495 &option_len, &count);
2496 if (!rapid_commit || option_len != 0 ||
2498 /* RFC 3315, 17.1.4 */
2502 switch_listening_mode(dhcp_client, L_NONE);
2504 if (dhcp_client->status_code == 0)
2505 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2506 &dhcp_client->status_code);
2508 if (packet6->message == DHCPV6_ADVERTISE) {
2509 if (dhcp_client->advertise_cb)
2510 dhcp_client->advertise_cb(dhcp_client,
2511 dhcp_client->advertise_data);
2515 if (dhcp_client->solicitation_cb) {
2517 * The dhcp_client might not be valid after the
2518 * callback call so just return immediately.
2520 dhcp_client->solicitation_cb(dhcp_client,
2521 dhcp_client->solicitation_data);
2526 if (dhcp_client->type != G_DHCP_IPV6)
2529 server_id = dhcpv6_get_option(packet6, pkt_len,
2530 G_DHCPV6_SERVERID, &option_len, &count);
2531 if (!dhcp_client->server_duid && server_id &&
2534 * If we do not have server duid yet, then get it now.
2535 * Prefix delegation renew support needs it.
2537 dhcp_client->server_duid = g_try_malloc(option_len);
2538 if (!dhcp_client->server_duid)
2540 memcpy(dhcp_client->server_duid, server_id, option_len);
2541 dhcp_client->server_duid_len = option_len;
2544 case INFORMATION_REQ:
2550 if (dhcp_client->type != G_DHCP_IPV6)
2553 if (packet6->message != DHCPV6_REPLY)
2558 server_id = dhcpv6_get_option(packet6, pkt_len,
2559 G_DHCPV6_SERVERID, &option_len, &count);
2560 if (!server_id || count != 1 || option_len == 0 ||
2561 (dhcp_client->server_duid_len > 0 &&
2562 memcmp(dhcp_client->server_duid, server_id,
2563 dhcp_client->server_duid_len) != 0)) {
2564 /* RFC 3315, 15.10 */
2566 "server duid error, discarding msg %p/%d/%d",
2567 server_id, option_len, count);
2571 switch_listening_mode(dhcp_client, L_NONE);
2573 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2574 &dhcp_client->status_code);
2576 if (dhcp_client->information_req_cb) {
2578 * The dhcp_client might not be valid after the
2579 * callback call so just return immediately.
2581 dhcp_client->information_req_cb(dhcp_client,
2582 dhcp_client->information_req_data);
2585 if (dhcp_client->request_cb) {
2586 dhcp_client->request_cb(dhcp_client,
2587 dhcp_client->request_data);
2590 if (dhcp_client->renew_cb) {
2591 dhcp_client->renew_cb(dhcp_client,
2592 dhcp_client->renew_data);
2595 if (dhcp_client->rebind_cb) {
2596 dhcp_client->rebind_cb(dhcp_client,
2597 dhcp_client->rebind_data);
2600 if (dhcp_client->release_cb) {
2601 dhcp_client->release_cb(dhcp_client,
2602 dhcp_client->release_data);
2605 if (dhcp_client->decline_cb) {
2606 dhcp_client->decline_cb(dhcp_client,
2607 dhcp_client->decline_data);
2610 if (dhcp_client->confirm_cb) {
2612 server_id = dhcpv6_get_option(packet6, pkt_len,
2613 G_DHCPV6_SERVERID, &option_len,
2615 if (!server_id || count != 1 ||
2617 /* RFC 3315, 15.10 */
2619 "confirm server duid error, "
2620 "discarding msg %p/%d/%d",
2621 server_id, option_len, count);
2624 dhcp_client->server_duid = g_try_malloc(option_len);
2625 if (!dhcp_client->server_duid)
2627 memcpy(dhcp_client->server_duid, server_id, option_len);
2628 dhcp_client->server_duid_len = option_len;
2630 dhcp_client->confirm_cb(dhcp_client,
2631 dhcp_client->confirm_data);
2639 debug(dhcp_client, "processed DHCP packet (new state %d)",
2640 dhcp_client->state);
2645 static gboolean discover_timeout(gpointer user_data)
2647 GDHCPClient *dhcp_client = user_data;
2649 dhcp_client->retry_times++;
2652 * We do not send the REQUESTED IP option if we are retrying because
2653 * if the server is non-authoritative it will ignore the request if the
2654 * option is present.
2656 g_dhcp_client_start(dhcp_client, NULL);
2661 static gboolean reboot_timeout(gpointer user_data)
2663 GDHCPClient *dhcp_client = user_data;
2664 dhcp_client->retry_times = 0;
2665 dhcp_client->requested_ip = 0;
2666 dhcp_client->state = INIT_SELECTING;
2668 * We do not send the REQUESTED IP option because the server didn't
2669 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2672 g_dhcp_client_start(dhcp_client, NULL);
2677 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2679 GDHCPClient *dhcp_client = dhcp_data;
2681 debug(dhcp_client, "back to MONITOR mode");
2683 dhcp_client->conflicts = 0;
2684 dhcp_client->state = IPV4LL_MONITOR;
2689 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2691 GDHCPClient *dhcp_client = dhcp_data;
2694 debug(dhcp_client, "request timeout (retries %d)",
2695 dhcp_client->retry_times);
2697 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2698 dhcp_client->retry_times++;
2699 send_announce_packet(dhcp_client);
2703 ip = htonl(dhcp_client->requested_ip);
2704 debug(dhcp_client, "switching to monitor mode");
2705 dhcp_client->state = IPV4LL_MONITOR;
2706 dhcp_client->assigned_ip = get_ip(ip);
2708 if (dhcp_client->ipv4ll_available_cb)
2709 dhcp_client->ipv4ll_available_cb(dhcp_client,
2710 dhcp_client->ipv4ll_available_data);
2711 dhcp_client->conflicts = 0;
2712 dhcp_client->timeout = 0;
2717 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2720 GDHCPClient *dhcp_client = dhcp_data;
2722 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2723 dhcp_client->retry_times);
2725 if (dhcp_client->retry_times == PROBE_NUM) {
2726 dhcp_client->state = IPV4LL_ANNOUNCE;
2727 dhcp_client->retry_times = 0;
2729 dhcp_client->retry_times++;
2730 send_announce_packet(dhcp_client);
2733 dhcp_client->retry_times++;
2734 send_probe_packet(dhcp_client);
2739 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2744 ClientState oldstate = dhcp_client->state;
2746 remove_timeouts(dhcp_client);
2748 if (dhcp_client->type == G_DHCP_IPV6) {
2749 if (dhcp_client->information_req_cb) {
2750 dhcp_client->state = INFORMATION_REQ;
2751 re = switch_listening_mode(dhcp_client, L3);
2753 switch_listening_mode(dhcp_client, L_NONE);
2754 dhcp_client->state = 0;
2757 send_information_req(dhcp_client);
2759 } else if (dhcp_client->solicitation_cb) {
2760 dhcp_client->state = SOLICITATION;
2761 re = switch_listening_mode(dhcp_client, L3);
2763 switch_listening_mode(dhcp_client, L_NONE);
2764 dhcp_client->state = 0;
2767 send_solicitation(dhcp_client);
2769 } else if (dhcp_client->request_cb) {
2770 dhcp_client->state = REQUEST;
2771 re = switch_listening_mode(dhcp_client, L3);
2773 switch_listening_mode(dhcp_client, L_NONE);
2774 dhcp_client->state = 0;
2777 send_dhcpv6_request(dhcp_client);
2779 } else if (dhcp_client->confirm_cb) {
2780 dhcp_client->state = CONFIRM;
2781 re = switch_listening_mode(dhcp_client, L3);
2783 switch_listening_mode(dhcp_client, L_NONE);
2784 dhcp_client->state = 0;
2787 send_dhcpv6_confirm(dhcp_client);
2789 } else if (dhcp_client->renew_cb) {
2790 dhcp_client->state = RENEW;
2791 re = switch_listening_mode(dhcp_client, L3);
2793 switch_listening_mode(dhcp_client, L_NONE);
2794 dhcp_client->state = 0;
2797 send_dhcpv6_renew(dhcp_client);
2799 } else if (dhcp_client->rebind_cb) {
2800 dhcp_client->state = REBIND;
2801 re = switch_listening_mode(dhcp_client, L3);
2803 switch_listening_mode(dhcp_client, L_NONE);
2804 dhcp_client->state = 0;
2807 send_dhcpv6_rebind(dhcp_client);
2809 } else if (dhcp_client->release_cb) {
2810 dhcp_client->state = RENEW;
2811 re = switch_listening_mode(dhcp_client, L3);
2813 switch_listening_mode(dhcp_client, L_NONE);
2814 dhcp_client->state = 0;
2817 send_dhcpv6_release(dhcp_client);
2818 } else if (dhcp_client->decline_cb) {
2819 dhcp_client->state = DECLINE;
2820 re = switch_listening_mode(dhcp_client, L3);
2822 switch_listening_mode(dhcp_client, L_NONE);
2823 dhcp_client->state = 0;
2826 send_dhcpv6_decline(dhcp_client);
2832 if (dhcp_client->type == G_DHCP_IPV4LL) {
2833 dhcp_client->state = INIT_SELECTING;
2834 ipv4ll_start(dhcp_client);
2838 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2839 if (dhcp_client->no_lease_cb)
2840 dhcp_client->no_lease_cb(dhcp_client,
2841 dhcp_client->no_lease_data);
2842 dhcp_client->retry_times = 0;
2846 if (dhcp_client->retry_times == 0) {
2847 g_free(dhcp_client->assigned_ip);
2848 dhcp_client->assigned_ip = NULL;
2850 dhcp_client->state = INIT_SELECTING;
2851 re = switch_listening_mode(dhcp_client, L2);
2855 __connman_util_get_random(&rand);
2856 dhcp_client->xid = rand;
2857 dhcp_client->start = time(NULL);
2858 g_hash_table_remove_all(dhcp_client->secs_bcast_hash);
2861 if (!last_address || oldstate == DECLINED) {
2864 addr = ntohl(inet_addr(last_address));
2865 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2868 } else if (dhcp_client->last_address != last_address) {
2869 g_free(dhcp_client->last_address);
2870 dhcp_client->last_address = g_strdup(last_address);
2874 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2875 debug(dhcp_client, "DHCP client start with state init_reboot");
2876 dhcp_client->requested_ip = addr;
2877 dhcp_client->state = REBOOTING;
2878 send_request(dhcp_client);
2880 dhcp_client->timeout = g_timeout_add_seconds_full(
2888 send_discover(dhcp_client, addr);
2890 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2898 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2900 switch_listening_mode(dhcp_client, L_NONE);
2902 if (dhcp_client->state == BOUND ||
2903 dhcp_client->state == RENEWING ||
2904 dhcp_client->state == REBINDING)
2905 send_release(dhcp_client, dhcp_client->server_ip,
2906 dhcp_client->requested_ip);
2908 remove_timeouts(dhcp_client);
2910 if (dhcp_client->listener_watch > 0) {
2911 g_source_remove(dhcp_client->listener_watch);
2912 dhcp_client->listener_watch = 0;
2915 dhcp_client->retry_times = 0;
2916 dhcp_client->ack_retry_times = 0;
2918 dhcp_client->requested_ip = 0;
2919 dhcp_client->state = RELEASED;
2920 dhcp_client->lease_seconds = 0;
2921 dhcp_client->request_bcast = false;
2924 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2925 unsigned char option_code)
2927 return g_hash_table_lookup(dhcp_client->code_value_hash,
2928 GINT_TO_POINTER((int) option_code));
2931 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2932 GDHCPClientEvent event,
2933 GDHCPClientEventFunc func,
2937 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2938 dhcp_client->lease_available_cb = func;
2939 dhcp_client->lease_available_data = data;
2941 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2942 if (dhcp_client->type == G_DHCP_IPV6)
2944 dhcp_client->ipv4ll_available_cb = func;
2945 dhcp_client->ipv4ll_available_data = data;
2947 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2948 dhcp_client->no_lease_cb = func;
2949 dhcp_client->no_lease_data = data;
2951 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2952 dhcp_client->lease_lost_cb = func;
2953 dhcp_client->lease_lost_data = data;
2955 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2956 if (dhcp_client->type == G_DHCP_IPV6)
2958 dhcp_client->ipv4ll_lost_cb = func;
2959 dhcp_client->ipv4ll_lost_data = data;
2961 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2962 dhcp_client->address_conflict_cb = func;
2963 dhcp_client->address_conflict_data = data;
2965 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2966 if (dhcp_client->type != G_DHCP_IPV6)
2968 dhcp_client->information_req_cb = func;
2969 dhcp_client->information_req_data = data;
2971 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2972 if (dhcp_client->type != G_DHCP_IPV6)
2974 dhcp_client->solicitation_cb = func;
2975 dhcp_client->solicitation_data = data;
2977 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2978 if (dhcp_client->type != G_DHCP_IPV6)
2980 dhcp_client->advertise_cb = func;
2981 dhcp_client->advertise_data = data;
2983 case G_DHCP_CLIENT_EVENT_REQUEST:
2984 if (dhcp_client->type != G_DHCP_IPV6)
2986 dhcp_client->request_cb = func;
2987 dhcp_client->request_data = data;
2989 case G_DHCP_CLIENT_EVENT_RENEW:
2990 if (dhcp_client->type != G_DHCP_IPV6)
2992 dhcp_client->renew_cb = func;
2993 dhcp_client->renew_data = data;
2995 case G_DHCP_CLIENT_EVENT_REBIND:
2996 if (dhcp_client->type != G_DHCP_IPV6)
2998 dhcp_client->rebind_cb = func;
2999 dhcp_client->rebind_data = data;
3001 case G_DHCP_CLIENT_EVENT_RELEASE:
3002 if (dhcp_client->type != G_DHCP_IPV6)
3004 dhcp_client->release_cb = func;
3005 dhcp_client->release_data = data;
3007 case G_DHCP_CLIENT_EVENT_CONFIRM:
3008 if (dhcp_client->type != G_DHCP_IPV6)
3010 dhcp_client->confirm_cb = func;
3011 dhcp_client->confirm_data = data;
3013 case G_DHCP_CLIENT_EVENT_DECLINE:
3014 if (dhcp_client->type != G_DHCP_IPV6)
3016 dhcp_client->decline_cb = func;
3017 dhcp_client->decline_data = data;
3022 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
3024 return dhcp_client->ifindex;
3027 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
3032 return get_ip(dhcp_client->server_ip);
3035 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
3037 return g_strdup(dhcp_client->assigned_ip);
3040 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
3042 GList *option = NULL;
3044 if (dhcp_client->type == G_DHCP_IPV6)
3047 switch (dhcp_client->state) {
3049 case IPV4LL_MONITOR:
3050 return g_strdup("255.255.0.0");
3054 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3056 return g_strdup(option->data);
3057 case INIT_SELECTING:
3063 case IPV4LL_ANNOUNCE:
3064 case INFORMATION_REQ:
3077 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3078 unsigned int option_code)
3080 if (!g_list_find(dhcp_client->request_list,
3081 GINT_TO_POINTER((int)option_code)))
3082 dhcp_client->request_list = g_list_prepend(
3083 dhcp_client->request_list,
3084 (GINT_TO_POINTER((int) option_code)));
3086 return G_DHCP_CLIENT_ERROR_NONE;
3089 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3091 g_list_free(dhcp_client->request_list);
3092 dhcp_client->request_list = NULL;
3095 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3097 g_hash_table_remove_all(dhcp_client->send_value_hash);
3100 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3104 storage = g_try_malloc(size + OPT_DATA);
3108 storage[OPT_CODE] = code;
3109 storage[OPT_LEN] = size;
3110 memcpy(&storage[OPT_DATA], data, size);
3115 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3118 return alloc_dhcp_option(code, data, MIN(size, 255));
3121 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3123 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3126 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3128 const unsigned maclen = 6;
3129 const unsigned idlen = maclen + 1;
3130 const uint8_t option_code = G_DHCP_CLIENT_ID;
3131 uint8_t idbuf[idlen];
3132 uint8_t *data_option;
3134 idbuf[0] = ARPHRD_ETHER;
3136 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3138 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3140 return G_DHCP_CLIENT_ERROR_NOMEM;
3142 g_hash_table_insert(dhcp_client->send_value_hash,
3143 GINT_TO_POINTER((int) option_code), data_option);
3145 return G_DHCP_CLIENT_ERROR_NONE;
3148 /* Now only support send hostname and vendor class ID */
3149 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3150 unsigned char option_code, const char *option_value)
3152 uint8_t *binary_option;
3154 if ((option_code == G_DHCP_HOST_NAME ||
3155 option_code == G_DHCP_VENDOR_CLASS_ID) && option_value) {
3156 binary_option = alloc_dhcp_string_option(option_code,
3159 return G_DHCP_CLIENT_ERROR_NOMEM;
3161 g_hash_table_insert(dhcp_client->send_value_hash,
3162 GINT_TO_POINTER((int) option_code), binary_option);
3165 return G_DHCP_CLIENT_ERROR_NONE;
3168 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3173 storage = g_malloc(2 + 2 + len);
3177 storage[0] = code >> 8;
3178 storage[1] = code & 0xff;
3179 storage[2] = len >> 8;
3180 storage[3] = len & 0xff;
3181 memcpy(storage + 2 + 2, option, len);
3186 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3188 return g_hash_table_remove(dhcp_client->send_value_hash,
3189 GINT_TO_POINTER((int)code));
3192 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3193 uint16_t option_code,
3194 uint8_t *option_value,
3195 uint16_t option_len)
3198 uint8_t *binary_option;
3200 debug(dhcp_client, "setting option %d to %p len %d",
3201 option_code, option_value, option_len);
3203 binary_option = alloc_dhcpv6_option(option_code, option_value,
3206 g_hash_table_insert(dhcp_client->send_value_hash,
3207 GINT_TO_POINTER((int) option_code),
3212 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3214 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3217 dhcp_client->last_request = time(NULL);
3220 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3222 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3225 return dhcp_client->status_code;
3228 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3233 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3238 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3243 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3246 g_dhcp_client_stop(dhcp_client);
3248 g_free(dhcp_client->interface);
3249 g_free(dhcp_client->assigned_ip);
3250 g_free(dhcp_client->last_address);
3251 g_free(dhcp_client->duid);
3252 g_free(dhcp_client->server_duid);
3254 g_list_free(dhcp_client->request_list);
3255 g_list_free(dhcp_client->require_list);
3257 g_hash_table_destroy(dhcp_client->code_value_hash);
3258 g_hash_table_destroy(dhcp_client->send_value_hash);
3259 g_hash_table_destroy(dhcp_client->secs_bcast_hash);
3261 g_free(dhcp_client);
3264 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3265 GDHCPDebugFunc func, gpointer user_data)
3270 dhcp_client->debug_func = func;
3271 dhcp_client->debug_data = user_data;
3274 static GDHCPIAPrefix *copy_prefix(gpointer data)
3276 GDHCPIAPrefix *copy, *prefix = data;
3278 copy = g_try_new(GDHCPIAPrefix, 1);
3282 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3287 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3289 GSList *copy = NULL;
3292 for (list = prefixes; list; list = list->next)
3293 copy = g_slist_prepend(copy, copy_prefix(list->data));