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
54 #define REQUEST_TIMEOUT 1
55 #define REQUEST_RETRIES 14
57 #define REQUEST_TIMEOUT 5
58 #define REQUEST_RETRIES 3
62 #define DISCOVER_TIMEOUT_WIFI 1
63 #define DISCOVER_RETRIES_WIFI 10
64 static int dhcp_discover_timeout = DISCOVER_TIMEOUT_WIFI;
65 static int dhcp_discover_max_retry = DISCOVER_RETRIES_WIFI;
67 void set_dhcp_discover_timeout(int timeout_value)
69 dhcp_discover_timeout = timeout_value;
72 void set_dhcp_discover_retry_count(int retry_count)
74 dhcp_discover_max_retry = retry_count;
78 typedef enum _listen_mode {
85 typedef enum _dhcp_client_state {
108 struct _GDHCPClient {
114 uint8_t mac_address[6];
117 uint32_t requested_ip;
120 uint32_t lease_seconds;
121 ListenMode listen_mode;
124 uint8_t ack_retry_times;
130 guint listener_watch;
133 GHashTable *code_value_hash;
134 GHashTable *send_value_hash;
135 GHashTable *secs_bcast_hash;
136 GDHCPClientEventFunc lease_available_cb;
137 gpointer lease_available_data;
138 GDHCPClientEventFunc ipv4ll_available_cb;
139 gpointer ipv4ll_available_data;
140 GDHCPClientEventFunc no_lease_cb;
141 gpointer no_lease_data;
142 GDHCPClientEventFunc lease_lost_cb;
143 gpointer lease_lost_data;
144 GDHCPClientEventFunc ipv4ll_lost_cb;
145 gpointer ipv4ll_lost_data;
146 GDHCPClientEventFunc address_conflict_cb;
147 gpointer address_conflict_data;
148 GDHCPDebugFunc debug_func;
150 GDHCPClientEventFunc information_req_cb;
151 gpointer information_req_data;
152 GDHCPClientEventFunc solicitation_cb;
153 gpointer solicitation_data;
154 GDHCPClientEventFunc advertise_cb;
155 gpointer advertise_data;
156 GDHCPClientEventFunc request_cb;
157 gpointer request_data;
158 GDHCPClientEventFunc renew_cb;
160 GDHCPClientEventFunc rebind_cb;
161 gpointer rebind_data;
162 GDHCPClientEventFunc release_cb;
163 gpointer release_data;
164 GDHCPClientEventFunc confirm_cb;
165 gpointer confirm_data;
166 GDHCPClientEventFunc decline_cb;
167 gpointer decline_data;
171 unsigned char *server_duid;
173 uint16_t status_code;
176 struct in6_addr ia_na;
177 struct in6_addr ia_ta;
181 struct timeval start_time;
183 #if defined TIZEN_EXT
184 uint32_t dhcp_lease_seconds;
185 gboolean init_reboot;
189 static inline void debug(GDHCPClient *client, const char *format, ...)
194 if (!client->debug_func)
197 va_start(ap, format);
199 if (vsnprintf(str, sizeof(str), format, ap) > 0)
200 client->debug_func(str, client->debug_data);
205 /* Initialize the packet with the proper defaults */
206 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
208 if (dhcp_client->type == G_DHCP_IPV6)
209 dhcpv6_init_header(pkt, type);
211 struct dhcp_packet *packet = pkt;
213 dhcp_init_header(packet, type);
214 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
218 static void add_request_options(GDHCPClient *dhcp_client,
219 struct dhcp_packet *packet)
224 int end = dhcp_end_option(packet->options);
226 for (list = dhcp_client->request_list; list; list = list->next) {
227 code = (uint8_t) GPOINTER_TO_INT(list->data);
229 packet->options[end + OPT_DATA + len] = code;
234 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
235 packet->options[end + OPT_LEN] = len;
236 packet->options[end + OPT_DATA + len] = DHCP_END;
243 unsigned char **ptr_buf;
246 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
249 uint8_t *option = value;
251 struct hash_params *params = user_data;
253 /* option[0][1] contains option code */
254 len = option[2] << 8 | option[3];
256 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
259 memcpy(*params->ptr_buf, option, len + 2 + 2);
260 (*params->ptr_buf) += len + 2 + 2;
263 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
264 unsigned char *buf, int max_buf,
265 unsigned char **ptr_buf)
267 struct hash_params params = {
273 if (dhcp_client->type != G_DHCP_IPV6)
276 g_hash_table_foreach(dhcp_client->send_value_hash,
277 add_dhcpv6_binary_option, ¶ms);
279 *ptr_buf = *params.ptr_buf;
282 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
286 buf[1] = code & 0xff;
290 memcpy(&buf[4], msg, len);
293 static int32_t get_time_diff(struct timeval *tv)
298 gettimeofday(&now, NULL);
300 hsec = (now.tv_sec - tv->tv_sec) * 100;
301 hsec += (now.tv_usec - tv->tv_usec) / 10000;
306 static void remove_timeouts(GDHCPClient *dhcp_client)
309 if (dhcp_client->timeout > 0)
310 g_source_remove(dhcp_client->timeout);
311 if (dhcp_client->t1_timeout > 0)
312 g_source_remove(dhcp_client->t1_timeout);
313 if (dhcp_client->t2_timeout > 0)
314 g_source_remove(dhcp_client->t2_timeout);
315 if (dhcp_client->lease_timeout > 0)
316 g_source_remove(dhcp_client->lease_timeout);
318 dhcp_client->timeout = 0;
319 dhcp_client->t1_timeout = 0;
320 dhcp_client->t2_timeout = 0;
321 dhcp_client->lease_timeout = 0;
325 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
326 struct dhcpv6_packet *packet,
327 unsigned char *buf, int max_buf,
328 unsigned char **ptr_buf)
331 uint16_t code, value;
336 if (dhcp_client->type != G_DHCP_IPV6)
339 for (list = dhcp_client->request_list; list; list = list->next) {
340 code = (uint16_t) GPOINTER_TO_INT(list->data);
344 case G_DHCPV6_CLIENTID:
345 if (!dhcp_client->duid)
348 len = 2 + 2 + dhcp_client->duid_len;
349 if ((*ptr_buf + len) > (buf + max_buf)) {
350 debug(dhcp_client, "Too long dhcpv6 message "
351 "when writing client id option");
355 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
356 dhcp_client->duid_len, dhcp_client->duid);
361 case G_DHCPV6_SERVERID:
362 if (!dhcp_client->server_duid)
365 len = 2 + 2 + dhcp_client->server_duid_len;
366 if ((*ptr_buf + len) > (buf + max_buf)) {
367 debug(dhcp_client, "Too long dhcpv6 message "
368 "when writing server id option");
372 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
373 dhcp_client->server_duid_len,
374 dhcp_client->server_duid);
379 case G_DHCPV6_RAPID_COMMIT:
381 if ((*ptr_buf + len) > (buf + max_buf)) {
382 debug(dhcp_client, "Too long dhcpv6 message "
383 "when writing rapid commit option");
387 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
395 case G_DHCPV6_ELAPSED_TIME:
396 if (!dhcp_client->retransmit) {
398 * Initial message, elapsed time is 0.
402 diff = get_time_diff(&dhcp_client->start_time);
403 if (diff < 0 || diff > 0xffff)
408 if ((*ptr_buf + len) > (buf + max_buf)) {
409 debug(dhcp_client, "Too long dhcpv6 message "
410 "when writing elapsed time option");
414 value = htons((uint16_t)diff);
415 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
416 2, (uint8_t *)&value);
421 case G_DHCPV6_DNS_SERVERS:
424 case G_DHCPV6_DOMAIN_LIST:
427 case G_DHCPV6_SNTP_SERVERS:
435 debug(dhcp_client, "option %d len %d added", code, len);
439 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
441 uint8_t *option = value;
442 struct dhcp_packet *packet = user_data;
444 dhcp_add_binary_option(packet, option);
447 static void add_send_options(GDHCPClient *dhcp_client,
448 struct dhcp_packet *packet)
450 g_hash_table_foreach(dhcp_client->send_value_hash,
451 add_binary_option, packet);
455 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
456 * represents the number of seconds elapsed from the start of
457 * attempting DHCP to satisfy some DHCP servers that allow for an
458 * "authoritative" reply before responding.
460 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
462 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
465 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
467 struct dhcp_packet packet;
469 debug(dhcp_client, "sending DHCP discover request");
471 init_packet(dhcp_client, &packet, DHCPDISCOVER);
473 packet.xid = dhcp_client->xid;
474 packet.secs = dhcp_attempt_secs(dhcp_client);
477 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
479 /* Explicitly saying that we want RFC-compliant packets helps
480 * some buggy DHCP servers to NOT send bigger packets */
481 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
483 add_request_options(dhcp_client, &packet);
485 add_send_options(dhcp_client, &packet);
488 * If we do not get a reply to DISCOVER packet, then we try with
489 * broadcast flag set. So first packet is sent without broadcast flag,
490 * first retry is with broadcast flag, second retry is without it etc.
491 * Reason is various buggy routers/AP that either eat the other or vice
492 * versa. In the receiving side we then find out what kind of packet
493 * the server can send.
495 dhcp_client->request_bcast = dhcp_client->retry_times % 2;
497 if (dhcp_client->request_bcast)
498 g_hash_table_add(dhcp_client->secs_bcast_hash,
499 GINT_TO_POINTER(packet.secs));
501 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
502 INADDR_BROADCAST, SERVER_PORT,
503 MAC_BCAST_ADDR, dhcp_client->ifindex,
504 dhcp_client->request_bcast);
507 int g_dhcp_client_decline(GDHCPClient *dhcp_client, uint32_t requested)
509 struct dhcp_packet packet;
511 dhcp_client->state = DECLINED;
512 dhcp_client->retry_times = 0;
514 debug(dhcp_client, "sending DHCP decline");
516 init_packet(dhcp_client, &packet, DHCPDECLINE);
518 packet.xid = dhcp_client->xid;
519 packet.secs = dhcp_attempt_secs(dhcp_client);
522 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
524 add_send_options(dhcp_client, &packet);
526 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
527 INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
528 dhcp_client->ifindex, true);
531 static int send_request(GDHCPClient *dhcp_client)
533 struct dhcp_packet packet;
535 debug(dhcp_client, "sending DHCP request (state %d)",
538 init_packet(dhcp_client, &packet, DHCPREQUEST);
540 packet.xid = dhcp_client->xid;
541 #if defined TIZEN_EXT
542 if (dhcp_client->init_reboot != TRUE)
544 packet.secs = dhcp_attempt_secs(dhcp_client);
546 if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
547 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
548 dhcp_client->requested_ip);
550 if (dhcp_client->state == REQUESTING)
551 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
552 dhcp_client->server_ip);
554 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
556 add_request_options(dhcp_client, &packet);
558 add_send_options(dhcp_client, &packet);
560 if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
561 packet.ciaddr = htonl(dhcp_client->requested_ip);
563 if (dhcp_client->state == RENEWING)
564 return dhcp_send_kernel_packet(&packet,
565 dhcp_client->requested_ip, CLIENT_PORT,
566 dhcp_client->server_ip, SERVER_PORT,
567 dhcp_client->interface);
569 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
570 INADDR_BROADCAST, SERVER_PORT,
571 MAC_BCAST_ADDR, dhcp_client->ifindex,
572 dhcp_client->request_bcast);
575 static int send_release(GDHCPClient *dhcp_client,
576 uint32_t server, uint32_t ciaddr)
578 struct dhcp_packet packet;
581 debug(dhcp_client, "sending DHCP release request");
583 init_packet(dhcp_client, &packet, DHCPRELEASE);
584 __connman_util_get_random(&rand);
586 packet.ciaddr = htonl(ciaddr);
588 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
590 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
592 dhcp_client->interface);
595 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
596 static int switch_listening_mode(GDHCPClient *dhcp_client,
597 ListenMode listen_mode);
599 static gboolean send_probe_packet(gpointer dhcp_data)
601 GDHCPClient *dhcp_client;
604 dhcp_client = dhcp_data;
605 /* if requested_ip is not valid, pick a new address*/
606 if (dhcp_client->requested_ip == 0) {
607 debug(dhcp_client, "pick a new random address");
608 dhcp_client->requested_ip = arp_random_ip();
611 debug(dhcp_client, "sending IPV4LL probe request");
613 if (dhcp_client->retry_times == 1) {
614 dhcp_client->state = IPV4LL_PROBE;
615 switch_listening_mode(dhcp_client, L_ARP);
617 arp_send_packet(dhcp_client->mac_address, 0,
618 dhcp_client->requested_ip, dhcp_client->ifindex);
620 if (dhcp_client->retry_times < PROBE_NUM) {
621 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
622 timeout = __connman_util_random_delay_ms(PROBE_MAX-PROBE_MIN);
623 timeout += PROBE_MIN*1000;
625 timeout = (ANNOUNCE_WAIT * 1000);
627 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
629 ipv4ll_probe_timeout,
635 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
636 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
638 static gboolean send_announce_packet(gpointer dhcp_data)
640 GDHCPClient *dhcp_client;
642 dhcp_client = dhcp_data;
644 debug(dhcp_client, "sending IPV4LL announce request");
646 arp_send_packet(dhcp_client->mac_address,
647 dhcp_client->requested_ip,
648 dhcp_client->requested_ip,
649 dhcp_client->ifindex);
651 remove_timeouts(dhcp_client);
653 if (dhcp_client->state == IPV4LL_DEFEND) {
654 dhcp_client->timeout =
655 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
657 ipv4ll_defend_timeout,
662 dhcp_client->timeout =
663 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
665 ipv4ll_announce_timeout,
671 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
676 dhcp_client->retransmit = true;
679 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
684 dhcp_client->retransmit = false;
687 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
688 unsigned char **duid, int *duid_len)
693 case G_DHCPV6_DUID_LLT:
694 *duid_len = 2 + 2 + 4 + ETH_ALEN;
695 *duid = g_try_malloc(*duid_len);
701 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
704 duid_time = time(NULL) - DUID_TIME_EPOCH;
705 (*duid)[4] = duid_time >> 24;
706 (*duid)[5] = duid_time >> 16;
707 (*duid)[6] = duid_time >> 8;
708 (*duid)[7] = duid_time & 0xff;
710 case G_DHCPV6_DUID_EN:
712 case G_DHCPV6_DUID_LL:
713 *duid_len = 2 + 2 + ETH_ALEN;
714 *duid = g_try_malloc(*duid_len);
720 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2]);
729 static gchar *convert_to_hex(unsigned char *buf, int len)
731 gchar *ret = g_try_malloc(len * 2 + 1);
734 for (i = 0; ret && i < len; i++)
735 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
740 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
743 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
746 g_free(dhcp_client->duid);
748 dhcp_client->duid = duid;
749 dhcp_client->duid_len = duid_len;
751 if (dhcp_client->debug_func) {
752 gchar *hex = convert_to_hex(duid, duid_len);
753 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
760 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
761 uint32_t *T2, GSList *prefixes)
763 uint8_t options[1452];
764 unsigned int max_buf = sizeof(options);
765 int len, count = g_slist_length(prefixes);
767 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
770 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
772 memset(options, 0, sizeof(options));
774 options[0] = dhcp_client->iaid >> 24;
775 options[1] = dhcp_client->iaid >> 16;
776 options[2] = dhcp_client->iaid >> 8;
777 options[3] = dhcp_client->iaid;
780 uint32_t t = htonl(*T1);
781 memcpy(&options[4], &t, 4);
785 uint32_t t = htonl(*T2);
786 memcpy(&options[8], &t, 4);
794 for (list = prefixes; list; list = list->next) {
795 GDHCPIAPrefix *prefix = list->data;
796 uint8_t sub_option[4+4+1+16];
798 if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
800 "Too long dhcpv6 message "
801 "when writing IA prefix option");
805 memset(&sub_option, 0, sizeof(sub_option));
807 /* preferred and validity time are left zero */
809 sub_option[8] = prefix->prefixlen;
810 memcpy(&sub_option[9], &prefix->prefix, 16);
812 copy_option(&options[len], G_DHCPV6_IA_PREFIX,
813 sizeof(sub_option), sub_option);
814 len += 2 + 2 + sizeof(sub_option);
818 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
824 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
826 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
829 return dhcp_client->iaid;
832 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
834 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
837 dhcp_client->iaid = iaid;
840 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
845 __connman_inet_get_interface_mac_address(index, buf);
847 memcpy(iaid, &buf[2], 4);
848 dhcp_client->iaid = iaid[0] << 24 |
849 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
852 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
853 uint32_t *T1, uint32_t *T2,
857 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
861 *T1 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
865 *T2 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
869 *started = dhcp_client->last_request;
872 *expire = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
873 dhcp_client->last_request + dhcp_client->expire;
878 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
882 buf[1] = G_DHCPV6_IAADDR;
885 memcpy(&buf[4], &dhcp_client->ia_na, 16);
886 memset(&buf[20], 0, 4); /* preferred */
887 memset(&buf[24], 0, 4); /* valid */
891 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
894 struct in6_addr addr;
896 if (inet_pton(AF_INET6, address, &addr) != 1)
900 buf[1] = G_DHCPV6_IAADDR;
903 memcpy(&buf[4], &addr, 16);
904 memset(&buf[20], 0, 4); /* preferred */
905 memset(&buf[24], 0, 4); /* valid */
909 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
913 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
915 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
925 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
926 int code, uint32_t *T1, uint32_t *T2,
927 bool add_iaaddr, const char *ia_na)
929 if (code == G_DHCPV6_IA_TA) {
930 uint8_t ia_options[4];
932 put_iaid(dhcp_client, index, ia_options);
934 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
935 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
936 ia_options, sizeof(ia_options));
938 } else if (code == G_DHCPV6_IA_NA) {
939 struct in6_addr addr;
941 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
944 * If caller has specified the IPv6 address it wishes to
945 * to use (ia_na != NULL and address is valid), then send
946 * the address to server.
947 * If caller did not specify the address (ia_na == NULL) and
948 * if the current address is not set, then we should not send
949 * the address sub-option.
951 if (add_iaaddr && ((!ia_na &&
952 !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
954 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
955 #define IAADDR_LEN (16+4+4)
956 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
959 memcpy(&dhcp_client->ia_na, &addr,
960 sizeof(struct in6_addr));
962 put_iaid(dhcp_client, index, ia_options);
965 ia_options[4] = *T1 >> 24;
966 ia_options[5] = *T1 >> 16;
967 ia_options[6] = *T1 >> 8;
970 memset(&ia_options[4], 0x00, 4);
973 ia_options[8] = *T2 >> 24;
974 ia_options[9] = *T2 >> 16;
975 ia_options[10] = *T2 >> 8;
976 ia_options[11] = *T2;
978 memset(&ia_options[8], 0x00, 4);
980 create_iaaddr(dhcp_client, &ia_options[12],
983 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
984 ia_options, sizeof(ia_options));
986 uint8_t ia_options[4+4+4];
988 put_iaid(dhcp_client, index, ia_options);
990 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
991 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
993 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
994 ia_options, sizeof(ia_options));
1003 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
1004 int code, uint32_t *T1, uint32_t *T2,
1008 uint8_t *ia_options, *pos;
1009 int len, count, total_len;
1011 count = g_slist_length(addresses);
1015 g_dhcp_client_set_request(dhcp_client, code);
1017 if (code == G_DHCPV6_IA_TA)
1019 else if (code == G_DHCPV6_IA_NA)
1020 len = 4 + 4 + 4; /* IAID + T1 + T2 */
1024 total_len = len + count * (2 + 2 + 16 + 4 + 4);
1025 ia_options = g_try_malloc0(total_len);
1029 put_iaid(dhcp_client, index, ia_options);
1031 pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
1033 for (list = addresses; list; list = list->next) {
1034 pos = append_iaaddr(dhcp_client, pos, list->data);
1039 if (code == G_DHCPV6_IA_NA) {
1041 ia_options[4] = *T1 >> 24;
1042 ia_options[5] = *T1 >> 16;
1043 ia_options[6] = *T1 >> 8;
1044 ia_options[7] = *T1;
1046 memset(&ia_options[4], 0x00, 4);
1049 ia_options[8] = *T2 >> 24;
1050 ia_options[9] = *T2 >> 16;
1051 ia_options[10] = *T2 >> 8;
1052 ia_options[11] = *T2;
1054 memset(&ia_options[8], 0x00, 4);
1057 g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1064 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1067 int i, j, len = sizeof(uint16_t) * args;
1070 values = g_try_malloc(len);
1075 for (i = 0, j = 0; i < args; i++) {
1076 uint16_t value = va_arg(va, int);
1077 values[j++] = value >> 8;
1078 values[j++] = value & 0xff;
1082 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1089 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1091 struct dhcpv6_packet *packet;
1092 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1096 memset(buf, 0, sizeof(buf));
1097 packet = (struct dhcpv6_packet *)&buf[0];
1098 ptr = buf + sizeof(struct dhcpv6_packet);
1100 init_packet(dhcp_client, packet, type);
1102 if (!dhcp_client->retransmit) {
1103 dhcp_client->xid = packet->transaction_id[0] << 16 |
1104 packet->transaction_id[1] << 8 |
1105 packet->transaction_id[2];
1106 gettimeofday(&dhcp_client->start_time, NULL);
1108 packet->transaction_id[0] = dhcp_client->xid >> 16;
1109 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1110 packet->transaction_id[2] = dhcp_client->xid;
1113 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1115 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1118 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1120 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1122 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1124 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1126 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1130 static int send_solicitation(GDHCPClient *dhcp_client)
1132 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1135 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1137 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1140 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1142 return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1145 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1147 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1150 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1152 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1155 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1157 return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1160 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1162 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1165 static int send_information_req(GDHCPClient *dhcp_client)
1167 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1171 static void remove_value(gpointer data, gpointer user_data)
1177 static void remove_option_value(gpointer data)
1179 GList *option_value = data;
1181 g_list_foreach(option_value, remove_value, NULL);
1182 g_list_free(option_value);
1185 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1186 int ifindex, GDHCPClientError *error)
1188 GDHCPClient *dhcp_client;
1191 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1195 dhcp_client = g_try_new0(GDHCPClient, 1);
1197 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1201 dhcp_client->interface = get_interface_name(ifindex);
1202 if (!dhcp_client->interface) {
1203 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1207 if (!interface_is_up(ifindex)) {
1208 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1212 __connman_inet_get_interface_mac_address(ifindex, dhcp_client->mac_address);
1214 dhcp_client->listener_sockfd = -1;
1215 dhcp_client->listen_mode = L_NONE;
1216 dhcp_client->ref_count = 1;
1217 dhcp_client->type = type;
1218 dhcp_client->ifindex = ifindex;
1219 dhcp_client->lease_available_cb = NULL;
1220 dhcp_client->ipv4ll_available_cb = NULL;
1221 dhcp_client->no_lease_cb = NULL;
1222 dhcp_client->lease_lost_cb = NULL;
1223 dhcp_client->ipv4ll_lost_cb = NULL;
1224 dhcp_client->address_conflict_cb = NULL;
1225 dhcp_client->listener_watch = 0;
1226 dhcp_client->retry_times = 0;
1227 dhcp_client->ack_retry_times = 0;
1228 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1229 g_direct_equal, NULL, remove_option_value);
1230 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1231 g_direct_equal, NULL, g_free);
1232 dhcp_client->secs_bcast_hash = g_hash_table_new(g_direct_hash,
1234 dhcp_client->request_list = NULL;
1235 dhcp_client->require_list = NULL;
1236 dhcp_client->duid = NULL;
1237 dhcp_client->duid_len = 0;
1238 dhcp_client->last_request = time(NULL);
1239 dhcp_client->expire = 0;
1240 dhcp_client->request_bcast = false;
1242 *error = G_DHCP_CLIENT_ERROR_NONE;
1247 g_free(dhcp_client->interface);
1248 g_free(dhcp_client);
1252 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1254 static int dhcp_l2_socket(int ifindex)
1257 struct sockaddr_ll sock;
1262 * I've selected not to see LL header, so BPF doesn't see it, too.
1263 * The filter may also pass non-IP and non-ARP packets, but we do
1264 * a more complete check when receiving the message in userspace.
1266 * and filter shamelessly stolen from:
1268 * http://www.flamewarmaster.de/software/dhcpclient/
1270 * There are a few other interesting ideas on that page (look under
1271 * "Motivation"). Use of netlink events is most interesting. Think
1272 * of various network servers listening for events and reconfiguring.
1273 * That would obsolete sending HUP signals and/or make use of restarts.
1275 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1278 * TODO: make conditional?
1280 static const struct sock_filter filter_instr[] = {
1282 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1283 /* L5, L1, is UDP? */
1284 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1285 /* ugly check for arp on ethernet-like and IPv4 */
1286 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1287 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1288 /* skip IP header */
1289 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1290 /* check udp source and destination ports */
1291 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1293 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1295 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1296 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1299 static const struct sock_fprog filter_prog = {
1300 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1301 /* casting const away: */
1302 .filter = (struct sock_filter *) filter_instr,
1305 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1309 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1310 /* Use only if standard ports are in use */
1311 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1312 sizeof(filter_prog));
1314 memset(&sock, 0, sizeof(sock));
1315 sock.sll_family = AF_PACKET;
1316 sock.sll_protocol = htons(ETH_P_IP);
1317 sock.sll_ifindex = ifindex;
1319 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1328 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1330 if (packet->ip.protocol != IPPROTO_UDP)
1333 if (packet->ip.version != IPVERSION)
1336 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1339 if (packet->udp.dest != htons(CLIENT_PORT))
1342 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1348 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1349 struct sockaddr_in *dst_addr)
1352 struct ip_udp_dhcp_packet packet;
1355 memset(&packet, 0, sizeof(packet));
1357 bytes = read(fd, &packet, sizeof(packet));
1361 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1364 if (bytes < ntohs(packet.ip.tot_len))
1365 /* packet is bigger than sizeof(packet), we did partial read */
1368 /* ignore any extra garbage bytes */
1369 bytes = ntohs(packet.ip.tot_len);
1371 if (!sanity_check(&packet, bytes))
1374 check = packet.ip.check;
1375 packet.ip.check = 0;
1376 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1379 /* verify UDP checksum. IP header has to be modified for this */
1380 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1381 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1382 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1383 check = packet.udp.check;
1384 packet.udp.check = 0;
1385 if (check && check != dhcp_checksum(&packet, bytes))
1388 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1389 sizeof(packet.udp)));
1391 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1394 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1396 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1399 static void ipv4ll_start(GDHCPClient *dhcp_client)
1403 remove_timeouts(dhcp_client);
1405 switch_listening_mode(dhcp_client, L_NONE);
1406 dhcp_client->retry_times = 0;
1407 dhcp_client->requested_ip = 0;
1409 dhcp_client->requested_ip = arp_random_ip();
1411 /*first wait a random delay to avoid storm of arp request on boot*/
1412 timeout = __connman_util_random_delay_ms(PROBE_WAIT);
1414 dhcp_client->retry_times++;
1415 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1422 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1425 switch_listening_mode(dhcp_client, L_NONE);
1427 remove_timeouts(dhcp_client);
1429 if (dhcp_client->listener_watch > 0) {
1430 g_source_remove(dhcp_client->listener_watch);
1431 dhcp_client->listener_watch = 0;
1434 dhcp_client->state = IPV4LL_PROBE;
1435 dhcp_client->retry_times = 0;
1436 dhcp_client->requested_ip = 0;
1438 g_free(dhcp_client->assigned_ip);
1439 dhcp_client->assigned_ip = NULL;
1442 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1445 struct ether_arp arp;
1446 uint32_t ip_requested;
1447 int source_conflict;
1448 int target_conflict;
1451 memset(&arp, 0, sizeof(arp));
1452 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1456 if (arp.arp_op != htons(ARPOP_REPLY) &&
1457 arp.arp_op != htons(ARPOP_REQUEST))
1460 if (memcmp(arp.arp_sha, dhcp_client->mac_address, ETH_ALEN) == 0)
1463 ip_requested = htonl(dhcp_client->requested_ip);
1464 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1465 sizeof(ip_requested));
1467 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1468 sizeof(ip_requested));
1470 if (!source_conflict && !target_conflict)
1473 dhcp_client->conflicts++;
1475 debug(dhcp_client, "IPV4LL conflict detected");
1477 if (dhcp_client->state == IPV4LL_MONITOR) {
1478 if (!source_conflict)
1480 dhcp_client->state = IPV4LL_DEFEND;
1481 debug(dhcp_client, "DEFEND mode conflicts : %d",
1482 dhcp_client->conflicts);
1483 /*Try to defend with a single announce*/
1484 send_announce_packet(dhcp_client);
1488 if (dhcp_client->state == IPV4LL_DEFEND) {
1489 if (!source_conflict)
1491 else if (dhcp_client->ipv4ll_lost_cb)
1492 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1493 dhcp_client->ipv4ll_lost_data);
1496 ipv4ll_stop(dhcp_client);
1498 /* If we got a lot of conflicts, RFC3927 states that we have
1499 * to wait RATE_LIMIT_INTERVAL before retrying,
1501 if (dhcp_client->conflicts < MAX_CONFLICTS)
1502 timeout_ms = __connman_util_random_delay_ms(PROBE_WAIT);
1504 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
1505 dhcp_client->retry_times++;
1506 dhcp_client->timeout =
1507 g_timeout_add_full(G_PRIORITY_HIGH,
1516 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1518 if (dhcp_client->type == G_DHCP_IPV6) {
1519 struct dhcpv6_packet *packet6 = pkt;
1525 xid = packet6->transaction_id[0] << 16 |
1526 packet6->transaction_id[1] << 8 |
1527 packet6->transaction_id[2];
1529 if (xid != dhcp_client->xid)
1532 struct dhcp_packet *packet = pkt;
1534 if (packet->xid != dhcp_client->xid)
1537 if (packet->hlen != 6)
1540 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1547 static void start_request(GDHCPClient *dhcp_client);
1549 static gboolean request_timeout(gpointer user_data)
1551 GDHCPClient *dhcp_client = user_data;
1553 #if defined TIZEN_EXT
1554 if (dhcp_client->init_reboot) {
1555 debug(dhcp_client, "DHCPREQUEST of INIT-REBOOT has failed");
1557 /* Start DHCPDISCOVERY when DHCPREQUEST of INIT-REBOOT has failed */
1558 g_dhcp_client_set_address_known(dhcp_client, FALSE);
1560 dhcp_client->retry_times = 0;
1561 dhcp_client->requested_ip = 0;
1563 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1568 debug(dhcp_client, "request timeout (retries %d)",
1569 dhcp_client->retry_times);
1571 dhcp_client->retry_times++;
1573 start_request(dhcp_client);
1578 static void listener_watch_destroy(gpointer user_data)
1580 GDHCPClient *dhcp_client = user_data;
1581 g_dhcp_client_unref(dhcp_client);
1584 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1585 gpointer user_data);
1587 static int switch_listening_mode(GDHCPClient *dhcp_client,
1588 ListenMode listen_mode)
1590 GIOChannel *listener_channel;
1591 int listener_sockfd;
1593 if (dhcp_client->listen_mode == listen_mode)
1596 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1597 dhcp_client->listen_mode, listen_mode);
1599 if (dhcp_client->listen_mode != L_NONE) {
1600 if (dhcp_client->listener_watch > 0)
1601 g_source_remove(dhcp_client->listener_watch);
1602 dhcp_client->listen_mode = L_NONE;
1603 dhcp_client->listener_sockfd = -1;
1604 dhcp_client->listener_watch = 0;
1607 if (listen_mode == L_NONE)
1610 if (listen_mode == L2)
1611 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1612 else if (listen_mode == L3) {
1613 if (dhcp_client->type == G_DHCP_IPV6)
1614 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1615 dhcp_client->interface,
1618 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1619 dhcp_client->interface,
1621 } else if (listen_mode == L_ARP)
1622 listener_sockfd = arp_socket(dhcp_client->ifindex);
1626 if (listener_sockfd < 0)
1629 listener_channel = g_io_channel_unix_new(listener_sockfd);
1630 if (!listener_channel) {
1631 /* Failed to create listener channel */
1632 close(listener_sockfd);
1636 dhcp_client->listen_mode = listen_mode;
1637 dhcp_client->listener_sockfd = listener_sockfd;
1639 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1640 dhcp_client->listener_watch =
1641 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1642 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1643 listener_event, g_dhcp_client_ref(dhcp_client),
1644 listener_watch_destroy);
1645 g_io_channel_unref(listener_channel);
1650 static void start_request(GDHCPClient *dhcp_client)
1652 debug(dhcp_client, "start request (retries %d)",
1653 dhcp_client->retry_times);
1655 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1656 if (dhcp_client->no_lease_cb)
1657 dhcp_client->no_lease_cb(dhcp_client,
1658 dhcp_client->no_lease_data);
1662 if (dhcp_client->retry_times == 0) {
1663 dhcp_client->state = REQUESTING;
1664 switch_listening_mode(dhcp_client, L2);
1667 send_request(dhcp_client);
1669 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1676 static uint32_t get_lease(struct dhcp_packet *packet, uint16_t packet_len)
1679 uint32_t lease_seconds;
1681 option = dhcp_get_option(packet, packet_len, DHCP_LEASE_TIME);
1685 lease_seconds = get_be32(option);
1687 if (lease_seconds < 10)
1690 return lease_seconds;
1693 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1695 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1697 remove_timeouts(dhcp_client);
1699 dhcp_client->retry_times = retry_times;
1700 dhcp_client->requested_ip = 0;
1701 dhcp_client->state = INIT_SELECTING;
1702 switch_listening_mode(dhcp_client, L2);
1704 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1707 static gboolean start_expire(gpointer user_data)
1709 GDHCPClient *dhcp_client = user_data;
1711 debug(dhcp_client, "lease expired");
1713 /*remove all timeouts if they are set*/
1714 remove_timeouts(dhcp_client);
1716 restart_dhcp(dhcp_client, 0);
1718 /* ip need to be cleared */
1719 if (dhcp_client->lease_lost_cb)
1720 dhcp_client->lease_lost_cb(dhcp_client,
1721 dhcp_client->lease_lost_data);
1726 static gboolean continue_rebound(gpointer user_data)
1728 GDHCPClient *dhcp_client = user_data;
1731 switch_listening_mode(dhcp_client, L2);
1732 send_request(dhcp_client);
1734 if (dhcp_client->t2_timeout> 0) {
1735 g_source_remove(dhcp_client->t2_timeout);
1736 dhcp_client->t2_timeout = 0;
1739 /*recalculate remaining rebind time*/
1740 dhcp_client->T2 >>= 1;
1741 if (dhcp_client->T2 > 60) {
1742 __connman_util_get_random(&rand);
1743 dhcp_client->t2_timeout =
1744 g_timeout_add_full(G_PRIORITY_HIGH,
1745 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1754 static gboolean start_rebound(gpointer user_data)
1756 GDHCPClient *dhcp_client = user_data;
1758 /*remove renew timer*/
1759 if (dhcp_client->t1_timeout > 0)
1760 g_source_remove(dhcp_client->t1_timeout);
1762 debug(dhcp_client, "start rebound");
1763 dhcp_client->state = REBINDING;
1765 /*calculate total rebind time*/
1766 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1768 /*send the first rebound and reschedule*/
1769 continue_rebound(user_data);
1774 static gboolean continue_renew (gpointer user_data)
1776 GDHCPClient *dhcp_client = user_data;
1779 switch_listening_mode(dhcp_client, L3);
1780 send_request(dhcp_client);
1782 if (dhcp_client->t1_timeout > 0)
1783 g_source_remove(dhcp_client->t1_timeout);
1785 dhcp_client->t1_timeout = 0;
1787 dhcp_client->T1 >>= 1;
1789 if (dhcp_client->T1 > 60) {
1790 __connman_util_get_random(&rand);
1791 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1792 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1800 static gboolean start_renew(gpointer user_data)
1802 GDHCPClient *dhcp_client = user_data;
1804 debug(dhcp_client, "start renew");
1805 dhcp_client->state = RENEWING;
1807 /*calculate total renew period*/
1808 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1810 /*send first renew and reschedule for half the remaining time.*/
1811 continue_renew(user_data);
1816 static void start_bound(GDHCPClient *dhcp_client)
1818 debug(dhcp_client, "start bound");
1820 dhcp_client->state = BOUND;
1822 remove_timeouts(dhcp_client);
1825 *TODO: T1 and T2 should be set through options instead of
1826 * defaults as they are here.
1829 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1830 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1831 dhcp_client->expire = dhcp_client->lease_seconds;
1833 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1835 start_renew, dhcp_client,
1838 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1840 start_rebound, dhcp_client,
1843 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1844 dhcp_client->expire,
1845 start_expire, dhcp_client,
1849 static gboolean restart_dhcp_timeout(gpointer user_data)
1851 GDHCPClient *dhcp_client = user_data;
1853 debug(dhcp_client, "restart DHCP timeout");
1855 if (dhcp_client->state == REBOOTING) {
1856 g_free(dhcp_client->last_address);
1857 dhcp_client->last_address = NULL;
1858 restart_dhcp(dhcp_client, 0);
1860 dhcp_client->ack_retry_times++;
1861 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1866 static char *get_ip(uint32_t ip)
1868 struct in_addr addr;
1872 return g_strdup(inet_ntoa(addr));
1875 /* get a rough idea of how long an option will be */
1876 static const uint8_t len_of_option_as_string[] = {
1877 [OPTION_IP] = sizeof("255.255.255.255 "),
1878 [OPTION_STRING] = 1,
1879 [OPTION_U8] = sizeof("255 "),
1880 [OPTION_U16] = sizeof("65535 "),
1881 [OPTION_U32] = sizeof("4294967295 "),
1884 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1886 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1889 /* Create "opt_value1 option_value2 ..." string */
1890 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1892 unsigned upper_length;
1896 len = option[OPT_LEN - OPT_DATA];
1897 type &= OPTION_TYPE_MASK;
1898 optlen = dhcp_option_lengths[type];
1901 upper_length = len_of_option_as_string[type] *
1902 ((unsigned)len / (unsigned)optlen);
1903 dest = ret = g_malloc(upper_length + 1);
1907 while (len >= optlen) {
1910 dest += sprint_nip(dest, "", option);
1913 uint16_t val_u16 = get_be16(option);
1914 dest += sprintf(dest, "%u", val_u16);
1918 uint32_t val_u32 = get_be32(option);
1919 dest += sprintf(dest, "%u", val_u32);
1923 memcpy(dest, option, len);
1940 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1948 if (type == OPTION_STRING)
1949 return g_list_append(list, g_strdup(value));
1951 while ((pos = strchr(pos, ' '))) {
1954 list = g_list_append(list, g_strdup(value));
1959 list = g_list_append(list, g_strdup(value));
1964 static inline uint32_t get_uint32(unsigned char *value)
1966 return value[0] << 24 | value[1] << 16 |
1967 value[2] << 8 | value[3];
1970 static inline uint16_t get_uint16(unsigned char *value)
1972 return value[0] << 8 | value[1];
1975 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1976 struct in6_addr *addr,
1977 unsigned char prefixlen, uint32_t preferred,
1980 GDHCPIAPrefix *ia_prefix;
1982 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1986 if (dhcp_client->debug_func) {
1987 char addr_str[INET6_ADDRSTRLEN + 1];
1988 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1989 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1990 addr_str, prefixlen, preferred, valid);
1993 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1994 ia_prefix->prefixlen = prefixlen;
1995 ia_prefix->preferred = preferred;
1996 ia_prefix->valid = valid;
1997 ia_prefix->expire = time(NULL) + valid;
1999 return g_list_prepend(list, ia_prefix);
2002 static GList *get_addresses(GDHCPClient *dhcp_client,
2004 unsigned char *value,
2008 struct in6_addr addr;
2009 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
2010 uint16_t option_len, option_code, st = 0, max_len;
2011 int addr_count = 0, prefix_count = 0, i, pos;
2012 unsigned char prefixlen;
2013 unsigned int shortest_valid = 0;
2017 if (!value || len < 4)
2020 iaid = get_uint32(&value[0]);
2021 if (dhcp_client->iaid != iaid)
2024 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
2025 T1 = get_uint32(&value[4]);
2026 T2 = get_uint32(&value[8]);
2029 /* IA_NA: RFC 3315, 22.4 */
2030 /* IA_PD: RFC 3633, ch 9 */
2040 max_len = len - pos;
2042 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
2044 /* We have more sub-options in this packet. */
2046 option = dhcpv6_get_sub_option(&value[pos], max_len,
2047 &option_code, &option_len);
2049 debug(dhcp_client, "pos %d option %p code %d len %d",
2050 pos, option, option_code, option_len);
2058 switch (option_code) {
2059 case G_DHCPV6_IAADDR:
2061 memcpy(&addr, &option[0], sizeof(addr));
2063 preferred = get_uint32(&option[i]);
2065 valid = get_uint32(&option[i]);
2070 case G_DHCPV6_STATUS_CODE:
2071 st = get_uint16(&option[0]);
2072 debug(dhcp_client, "error code %d", st);
2073 if (option_len > 2) {
2074 str = g_strndup((gchar *)&option[2],
2076 debug(dhcp_client, "error text: %s", str);
2083 case G_DHCPV6_IA_PREFIX:
2085 preferred = get_uint32(&option[i]);
2087 valid = get_uint32(&option[i]);
2089 prefixlen = option[i];
2091 memcpy(&addr, &option[i], sizeof(addr));
2093 if (preferred < valid) {
2094 /* RFC 3633, ch 10 */
2095 list = add_prefix(dhcp_client, list, &addr,
2096 prefixlen, preferred, valid);
2097 if (shortest_valid > valid)
2098 shortest_valid = valid;
2104 pos += 2 + 2 + option_len;
2106 } while (pos < len);
2108 if (addr_count > 0 && st == 0) {
2109 /* We only support one address atm */
2110 char addr_str[INET6_ADDRSTRLEN + 1];
2112 if (preferred > valid)
2113 /* RFC 3315, 22.6 */
2116 dhcp_client->T1 = T1;
2117 dhcp_client->T2 = T2;
2119 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2120 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2121 addr_count, addr_str, T1, T2);
2123 list = g_list_append(list, g_strdup(addr_str));
2125 if (code == G_DHCPV6_IA_NA)
2126 memcpy(&dhcp_client->ia_na, &addr,
2127 sizeof(struct in6_addr));
2129 memcpy(&dhcp_client->ia_ta, &addr,
2130 sizeof(struct in6_addr));
2132 if (valid != dhcp_client->expire)
2133 dhcp_client->expire = valid;
2136 if (prefix_count > 0 && list) {
2138 * This means we have a list of prefixes to delegate.
2140 list = g_list_reverse(list);
2142 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2143 prefix_count, T1, T2);
2145 dhcp_client->T1 = T1;
2146 dhcp_client->T2 = T2;
2148 dhcp_client->expire = shortest_valid;
2151 if (status && *status != 0)
2152 debug(dhcp_client, "status %d", *status);
2157 static GList *get_domains(int maxlen, unsigned char *value)
2163 char dns_name[NS_MAXDNAME + 1];
2165 if (!value || maxlen < 3)
2168 while (pos < maxlen) {
2169 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2171 c = (unsigned char *)dns_name;
2178 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2179 pos += (char *)c - dns_name + 1;
2182 return g_list_reverse(list);
2185 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2187 unsigned char *value,
2198 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2199 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2202 "%s server list length (%d) is invalid",
2203 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2207 for (i = 0; i < len; i += 16) {
2209 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2213 if (!inet_ntop(AF_INET6, &value[i], str,
2217 list = g_list_append(list, str);
2221 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2222 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2223 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2224 list = get_addresses(dhcp_client, code, len, value, status);
2227 case G_DHCPV6_DOMAIN_LIST:
2228 list = get_domains(len, value);
2238 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2239 struct dhcpv6_packet *packet,
2240 uint16_t pkt_len, uint16_t *status)
2242 GList *list, *value_list;
2245 uint16_t option_len;
2247 for (list = dhcp_client->request_list; list; list = list->next) {
2248 code = (uint16_t) GPOINTER_TO_INT(list->data);
2250 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2253 g_hash_table_remove(dhcp_client->code_value_hash,
2254 GINT_TO_POINTER((int) code));
2258 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2259 option_len, option, status);
2261 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2262 option_len, value_list);
2265 g_hash_table_remove(dhcp_client->code_value_hash,
2266 GINT_TO_POINTER((int) code));
2268 g_hash_table_insert(dhcp_client->code_value_hash,
2269 GINT_TO_POINTER((int) code), value_list);
2273 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet,
2274 uint16_t packet_len)
2276 GDHCPOptionType type;
2277 GList *list, *value_list;
2282 for (list = dhcp_client->request_list; list; list = list->next) {
2283 code = (uint8_t) GPOINTER_TO_INT(list->data);
2285 option = dhcp_get_option(packet, packet_len, code);
2287 g_hash_table_remove(dhcp_client->code_value_hash,
2288 GINT_TO_POINTER((int) code));
2292 type = dhcp_get_code_type(code);
2294 option_value = malloc_option_value_string(option, type);
2296 g_hash_table_remove(dhcp_client->code_value_hash,
2297 GINT_TO_POINTER((int) code));
2299 value_list = get_option_value_list(option_value, type);
2301 g_free(option_value);
2304 g_hash_table_remove(dhcp_client->code_value_hash,
2305 GINT_TO_POINTER((int) code));
2307 g_hash_table_insert(dhcp_client->code_value_hash,
2308 GINT_TO_POINTER((int) code), value_list);
2312 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2315 GDHCPClient *dhcp_client = user_data;
2316 struct sockaddr_in dst_addr = { 0 };
2317 struct dhcp_packet packet = { 0 };
2318 struct dhcpv6_packet *packet6 = NULL;
2319 uint8_t *message_type = NULL, *client_id = NULL, *option,
2321 uint16_t option_len = 0, status = 0;
2324 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2325 uint16_t pkt_len = 0;
2329 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2330 dhcp_client->listener_watch = 0;
2331 #if defined TIZEN_EXT
2332 /* re-register event listener when socket failed */
2333 int retry_count = 0;
2335 while (retry_count++ < GIO_SOCKET_RETRY_COUNT && ret < 0)
2336 ret = switch_listening_mode(dhcp_client,
2338 #endif /* defined TIZEN_EXT */
2342 if (dhcp_client->listen_mode == L_NONE)
2347 dhcp_client->status_code = 0;
2349 if (dhcp_client->listen_mode == L2) {
2350 re = dhcp_recv_l2_packet(&packet,
2351 dhcp_client->listener_sockfd,
2353 pkt_len = (uint16_t)(unsigned int)re;
2355 } else if (dhcp_client->listen_mode == L3) {
2356 if (dhcp_client->type == G_DHCP_IPV6) {
2357 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2358 dhcp_client->listener_sockfd);
2363 xid = packet6->transaction_id[0] << 16 |
2364 packet6->transaction_id[1] << 8 |
2365 packet6->transaction_id[2];
2367 re = dhcp_recv_l3_packet(&packet,
2368 dhcp_client->listener_sockfd);
2371 } else if (dhcp_client->listen_mode == L_ARP) {
2372 ipv4ll_recv_arp_packet(dhcp_client);
2380 if (!check_package_owner(dhcp_client, pkt))
2383 if (dhcp_client->type == G_DHCP_IPV6) {
2388 client_id = dhcpv6_get_option(packet6, pkt_len,
2389 G_DHCPV6_CLIENTID, &option_len, &count);
2391 if (!client_id || count == 0 || option_len == 0 ||
2392 memcmp(dhcp_client->duid, client_id,
2393 dhcp_client->duid_len) != 0) {
2395 "client duid error, discarding msg %p/%d/%d",
2396 client_id, option_len, count);
2400 option = dhcpv6_get_option(packet6, pkt_len,
2401 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2402 if (option != 0 && option_len > 0) {
2403 status = option[0]<<8 | option[1];
2405 debug(dhcp_client, "error code %d", status);
2406 if (option_len > 2) {
2407 gchar *txt = g_strndup(
2408 (gchar *)&option[2],
2410 debug(dhcp_client, "error text: %s",
2415 dhcp_client->status_code = status;
2418 message_type = dhcp_get_option(&packet, pkt_len, DHCP_MESSAGE_TYPE);
2423 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2424 "(current state %d)", ntohl(xid), dhcp_client->state);
2426 switch (dhcp_client->state) {
2427 case INIT_SELECTING:
2428 if (*message_type != DHCPOFFER)
2431 remove_timeouts(dhcp_client);
2432 dhcp_client->timeout = 0;
2433 dhcp_client->retry_times = 0;
2435 option = dhcp_get_option(&packet, pkt_len, DHCP_SERVER_ID);
2439 dhcp_client->server_ip = get_be32(option);
2440 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2442 dhcp_client->state = REQUESTING;
2447 * If unicasting is not possible, the message MAY be
2448 * sent as an IP broadcast using an IP broadcast address
2449 * (preferably 0xffffffff) as the IP destination address
2450 * and the link-layer broadcast address as the link-layer
2451 * destination address.
2453 * For interoperability reasons, if the response is an IP
2454 * broadcast, let's reuse broadcast flag from DHCPDISCOVER
2455 * to which the server has responded. Some servers are picky
2458 dhcp_client->request_bcast =
2459 dst_addr.sin_addr.s_addr == INADDR_BROADCAST &&
2460 g_hash_table_contains(dhcp_client->secs_bcast_hash,
2461 GINT_TO_POINTER(packet.secs));
2463 debug(dhcp_client, "init ip %s secs %hu -> broadcast flag %s",
2464 inet_ntoa(dst_addr.sin_addr), packet.secs,
2465 dhcp_client->request_bcast ? "on" : "off");
2467 start_request(dhcp_client);
2471 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2472 dhcp_client->request_bcast = true;
2474 dhcp_client->request_bcast = false;
2476 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2477 inet_ntoa(dst_addr.sin_addr),
2478 dhcp_client->request_bcast ? "" : "not ");
2483 if (*message_type == DHCPACK) {
2484 dhcp_client->retry_times = 0;
2486 remove_timeouts(dhcp_client);
2488 dhcp_client->lease_seconds = get_lease(&packet, pkt_len);
2490 #if defined TIZEN_EXT
2491 dhcp_client->dhcp_lease_seconds = dhcp_client->lease_seconds;
2494 get_request(dhcp_client, &packet, pkt_len);
2496 switch_listening_mode(dhcp_client, L_NONE);
2498 g_free(dhcp_client->assigned_ip);
2499 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2501 if (dhcp_client->state == REBOOTING) {
2502 option = dhcp_get_option(&packet, pkt_len,
2506 dhcp_client->server_ip = get_be32(option);
2509 /* Address should be set up here */
2510 if (dhcp_client->lease_available_cb)
2511 dhcp_client->lease_available_cb(dhcp_client,
2512 dhcp_client->lease_available_data);
2514 start_bound(dhcp_client);
2515 } else if (*message_type == DHCPNAK) {
2516 dhcp_client->retry_times = 0;
2518 remove_timeouts(dhcp_client);
2520 #if defined TIZEN_EXT
2521 if (dhcp_client->init_reboot) {
2522 g_dhcp_client_set_address_known(dhcp_client, FALSE);
2523 dhcp_client->timeout = g_idle_add_full(
2525 restart_dhcp_timeout,
2532 dhcp_client->timeout = g_timeout_add_seconds_full(
2534 restart_dhcp_timeout,
2541 if (dhcp_client->type != G_DHCP_IPV6)
2544 if (packet6->message != DHCPV6_REPLY &&
2545 packet6->message != DHCPV6_ADVERTISE)
2549 server_id = dhcpv6_get_option(packet6, pkt_len,
2550 G_DHCPV6_SERVERID, &option_len, &count);
2551 if (!server_id || count != 1 || option_len == 0) {
2552 /* RFC 3315, 15.10 */
2554 "server duid error, discarding msg %p/%d/%d",
2555 server_id, option_len, count);
2558 dhcp_client->server_duid = g_try_malloc(option_len);
2559 if (!dhcp_client->server_duid)
2561 memcpy(dhcp_client->server_duid, server_id, option_len);
2562 dhcp_client->server_duid_len = option_len;
2564 if (packet6->message == DHCPV6_REPLY) {
2565 uint8_t *rapid_commit;
2568 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2569 G_DHCPV6_RAPID_COMMIT,
2570 &option_len, &count);
2571 if (!rapid_commit || option_len != 0 ||
2573 /* RFC 3315, 17.1.4 */
2577 switch_listening_mode(dhcp_client, L_NONE);
2579 if (dhcp_client->status_code == 0)
2580 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2581 &dhcp_client->status_code);
2583 if (packet6->message == DHCPV6_ADVERTISE) {
2584 if (dhcp_client->advertise_cb)
2585 dhcp_client->advertise_cb(dhcp_client,
2586 dhcp_client->advertise_data);
2590 if (dhcp_client->solicitation_cb) {
2592 * The dhcp_client might not be valid after the
2593 * callback call so just return immediately.
2595 dhcp_client->solicitation_cb(dhcp_client,
2596 dhcp_client->solicitation_data);
2601 if (dhcp_client->type != G_DHCP_IPV6)
2604 server_id = dhcpv6_get_option(packet6, pkt_len,
2605 G_DHCPV6_SERVERID, &option_len, &count);
2606 if (!dhcp_client->server_duid && server_id &&
2609 * If we do not have server duid yet, then get it now.
2610 * Prefix delegation renew support needs it.
2612 dhcp_client->server_duid = g_try_malloc(option_len);
2613 if (!dhcp_client->server_duid)
2615 memcpy(dhcp_client->server_duid, server_id, option_len);
2616 dhcp_client->server_duid_len = option_len;
2619 case INFORMATION_REQ:
2625 if (dhcp_client->type != G_DHCP_IPV6)
2628 if (packet6->message != DHCPV6_REPLY)
2633 server_id = dhcpv6_get_option(packet6, pkt_len,
2634 G_DHCPV6_SERVERID, &option_len, &count);
2635 if (!server_id || count != 1 || option_len == 0 ||
2636 (dhcp_client->server_duid_len > 0 &&
2637 memcmp(dhcp_client->server_duid, server_id,
2638 dhcp_client->server_duid_len) != 0)) {
2639 /* RFC 3315, 15.10 */
2641 "server duid error, discarding msg %p/%d/%d",
2642 server_id, option_len, count);
2646 switch_listening_mode(dhcp_client, L_NONE);
2648 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2649 &dhcp_client->status_code);
2651 if (dhcp_client->information_req_cb) {
2653 * The dhcp_client might not be valid after the
2654 * callback call so just return immediately.
2656 dhcp_client->information_req_cb(dhcp_client,
2657 dhcp_client->information_req_data);
2660 if (dhcp_client->request_cb) {
2661 dhcp_client->request_cb(dhcp_client,
2662 dhcp_client->request_data);
2665 if (dhcp_client->renew_cb) {
2666 dhcp_client->renew_cb(dhcp_client,
2667 dhcp_client->renew_data);
2670 if (dhcp_client->rebind_cb) {
2671 dhcp_client->rebind_cb(dhcp_client,
2672 dhcp_client->rebind_data);
2675 if (dhcp_client->release_cb) {
2676 dhcp_client->release_cb(dhcp_client,
2677 dhcp_client->release_data);
2680 if (dhcp_client->decline_cb) {
2681 dhcp_client->decline_cb(dhcp_client,
2682 dhcp_client->decline_data);
2685 if (dhcp_client->confirm_cb) {
2687 server_id = dhcpv6_get_option(packet6, pkt_len,
2688 G_DHCPV6_SERVERID, &option_len,
2690 if (!server_id || count != 1 ||
2692 /* RFC 3315, 15.10 */
2694 "confirm server duid error, "
2695 "discarding msg %p/%d/%d",
2696 server_id, option_len, count);
2699 dhcp_client->server_duid = g_try_malloc(option_len);
2700 if (!dhcp_client->server_duid)
2702 memcpy(dhcp_client->server_duid, server_id, option_len);
2703 dhcp_client->server_duid_len = option_len;
2705 dhcp_client->confirm_cb(dhcp_client,
2706 dhcp_client->confirm_data);
2714 debug(dhcp_client, "processed DHCP packet (new state %d)",
2715 dhcp_client->state);
2720 static gboolean discover_timeout(gpointer user_data)
2722 GDHCPClient *dhcp_client = user_data;
2724 dhcp_client->retry_times++;
2727 * We do not send the REQUESTED IP option if we are retrying because
2728 * if the server is non-authoritative it will ignore the request if the
2729 * option is present.
2731 g_dhcp_client_start(dhcp_client, NULL);
2736 static gboolean reboot_timeout(gpointer user_data)
2738 GDHCPClient *dhcp_client = user_data;
2739 dhcp_client->retry_times = 0;
2740 dhcp_client->requested_ip = 0;
2741 dhcp_client->state = INIT_SELECTING;
2743 * We do not send the REQUESTED IP option because the server didn't
2744 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2747 g_dhcp_client_start(dhcp_client, NULL);
2752 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2754 GDHCPClient *dhcp_client = dhcp_data;
2756 debug(dhcp_client, "back to MONITOR mode");
2758 dhcp_client->conflicts = 0;
2759 dhcp_client->state = IPV4LL_MONITOR;
2764 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2766 GDHCPClient *dhcp_client = dhcp_data;
2769 #if defined TIZEN_EXT
2774 debug(dhcp_client, "request timeout (retries %d)",
2775 dhcp_client->retry_times);
2777 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2778 dhcp_client->retry_times++;
2779 send_announce_packet(dhcp_client);
2783 ip = htonl(dhcp_client->requested_ip);
2784 debug(dhcp_client, "switching to monitor mode");
2785 dhcp_client->state = IPV4LL_MONITOR;
2786 dhcp_client->assigned_ip = get_ip(ip);
2788 if (dhcp_client->ipv4ll_available_cb)
2789 dhcp_client->ipv4ll_available_cb(dhcp_client,
2790 dhcp_client->ipv4ll_available_data);
2791 dhcp_client->conflicts = 0;
2792 dhcp_client->timeout = 0;
2797 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2800 GDHCPClient *dhcp_client = dhcp_data;
2802 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2803 dhcp_client->retry_times);
2805 if (dhcp_client->retry_times == PROBE_NUM) {
2806 dhcp_client->state = IPV4LL_ANNOUNCE;
2807 dhcp_client->retry_times = 0;
2809 dhcp_client->retry_times++;
2810 send_announce_packet(dhcp_client);
2813 dhcp_client->retry_times++;
2814 send_probe_packet(dhcp_client);
2819 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2824 ClientState oldstate = dhcp_client->state;
2826 #if defined TIZEN_EXT
2827 int discover_retry = 0;
2831 remove_timeouts(dhcp_client);
2833 if (dhcp_client->type == G_DHCP_IPV6) {
2834 if (dhcp_client->information_req_cb) {
2835 dhcp_client->state = INFORMATION_REQ;
2836 re = switch_listening_mode(dhcp_client, L3);
2838 switch_listening_mode(dhcp_client, L_NONE);
2839 dhcp_client->state = 0;
2842 send_information_req(dhcp_client);
2844 } else if (dhcp_client->solicitation_cb) {
2845 dhcp_client->state = SOLICITATION;
2846 re = switch_listening_mode(dhcp_client, L3);
2848 switch_listening_mode(dhcp_client, L_NONE);
2849 dhcp_client->state = 0;
2852 send_solicitation(dhcp_client);
2854 } else if (dhcp_client->request_cb) {
2855 dhcp_client->state = REQUEST;
2856 re = switch_listening_mode(dhcp_client, L3);
2858 switch_listening_mode(dhcp_client, L_NONE);
2859 dhcp_client->state = 0;
2862 send_dhcpv6_request(dhcp_client);
2864 } else if (dhcp_client->confirm_cb) {
2865 dhcp_client->state = CONFIRM;
2866 re = switch_listening_mode(dhcp_client, L3);
2868 switch_listening_mode(dhcp_client, L_NONE);
2869 dhcp_client->state = 0;
2872 send_dhcpv6_confirm(dhcp_client);
2874 } else if (dhcp_client->renew_cb) {
2875 dhcp_client->state = RENEW;
2876 re = switch_listening_mode(dhcp_client, L3);
2878 switch_listening_mode(dhcp_client, L_NONE);
2879 dhcp_client->state = 0;
2882 send_dhcpv6_renew(dhcp_client);
2884 } else if (dhcp_client->rebind_cb) {
2885 dhcp_client->state = REBIND;
2886 re = switch_listening_mode(dhcp_client, L3);
2888 switch_listening_mode(dhcp_client, L_NONE);
2889 dhcp_client->state = 0;
2892 send_dhcpv6_rebind(dhcp_client);
2894 } else if (dhcp_client->release_cb) {
2895 dhcp_client->state = RENEW;
2896 re = switch_listening_mode(dhcp_client, L3);
2898 switch_listening_mode(dhcp_client, L_NONE);
2899 dhcp_client->state = 0;
2902 send_dhcpv6_release(dhcp_client);
2903 } else if (dhcp_client->decline_cb) {
2904 dhcp_client->state = DECLINE;
2905 re = switch_listening_mode(dhcp_client, L3);
2907 switch_listening_mode(dhcp_client, L_NONE);
2908 dhcp_client->state = 0;
2911 send_dhcpv6_decline(dhcp_client);
2917 #if defined TIZEN_EXT
2918 if (g_ascii_strncasecmp(dhcp_client->interface, "wlan", 4)
2920 discover_retry = DISCOVER_RETRIES_WIFI;
2921 timeout = DISCOVER_TIMEOUT_WIFI;
2923 discover_retry = DISCOVER_RETRIES;
2924 timeout = DISCOVER_TIMEOUT;
2927 debug(dhcp_client, "[DHCPC] Discover retry/total : [%d]/[%d] timeout [%d]",
2928 dhcp_client->retry_times, discover_retry, timeout);
2932 if (dhcp_client->type == G_DHCP_IPV4LL) {
2933 dhcp_client->state = INIT_SELECTING;
2934 ipv4ll_start(dhcp_client);
2938 #if defined TIZEN_EXT
2939 if (dhcp_client->retry_times == discover_retry) {
2941 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2943 if (dhcp_client->no_lease_cb)
2944 dhcp_client->no_lease_cb(dhcp_client,
2945 dhcp_client->no_lease_data);
2946 dhcp_client->retry_times = 0;
2950 if (dhcp_client->retry_times == 0) {
2951 g_free(dhcp_client->assigned_ip);
2952 dhcp_client->assigned_ip = NULL;
2954 dhcp_client->state = INIT_SELECTING;
2955 re = switch_listening_mode(dhcp_client, L2);
2959 __connman_util_get_random(&rand);
2960 dhcp_client->xid = rand;
2961 dhcp_client->start = time(NULL);
2962 g_hash_table_remove_all(dhcp_client->secs_bcast_hash);
2965 if (!last_address || oldstate == DECLINED) {
2968 addr = ntohl(inet_addr(last_address));
2969 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2972 } else if (dhcp_client->last_address != last_address) {
2973 g_free(dhcp_client->last_address);
2974 dhcp_client->last_address = g_strdup(last_address);
2978 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2979 debug(dhcp_client, "DHCP client start with state init_reboot");
2980 dhcp_client->requested_ip = addr;
2981 dhcp_client->state = REBOOTING;
2982 send_request(dhcp_client);
2984 dhcp_client->timeout = g_timeout_add_seconds_full(
2986 #if defined TIZEN_EXT
2996 send_discover(dhcp_client, addr);
2998 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2999 #if defined TIZEN_EXT
3010 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
3012 switch_listening_mode(dhcp_client, L_NONE);
3014 if (dhcp_client->state == BOUND ||
3015 dhcp_client->state == RENEWING ||
3016 dhcp_client->state == REBINDING)
3017 send_release(dhcp_client, dhcp_client->server_ip,
3018 dhcp_client->requested_ip);
3020 remove_timeouts(dhcp_client);
3022 if (dhcp_client->listener_watch > 0) {
3023 g_source_remove(dhcp_client->listener_watch);
3024 dhcp_client->listener_watch = 0;
3027 dhcp_client->retry_times = 0;
3028 dhcp_client->ack_retry_times = 0;
3030 dhcp_client->requested_ip = 0;
3031 dhcp_client->state = RELEASED;
3032 dhcp_client->lease_seconds = 0;
3033 dhcp_client->request_bcast = false;
3036 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
3037 unsigned char option_code)
3039 return g_hash_table_lookup(dhcp_client->code_value_hash,
3040 GINT_TO_POINTER((int) option_code));
3043 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
3044 GDHCPClientEvent event,
3045 GDHCPClientEventFunc func,
3049 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
3050 dhcp_client->lease_available_cb = func;
3051 dhcp_client->lease_available_data = data;
3053 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
3054 if (dhcp_client->type == G_DHCP_IPV6)
3056 dhcp_client->ipv4ll_available_cb = func;
3057 dhcp_client->ipv4ll_available_data = data;
3059 case G_DHCP_CLIENT_EVENT_NO_LEASE:
3060 dhcp_client->no_lease_cb = func;
3061 dhcp_client->no_lease_data = data;
3063 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
3064 dhcp_client->lease_lost_cb = func;
3065 dhcp_client->lease_lost_data = data;
3067 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
3068 if (dhcp_client->type == G_DHCP_IPV6)
3070 dhcp_client->ipv4ll_lost_cb = func;
3071 dhcp_client->ipv4ll_lost_data = data;
3073 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
3074 dhcp_client->address_conflict_cb = func;
3075 dhcp_client->address_conflict_data = data;
3077 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
3078 if (dhcp_client->type != G_DHCP_IPV6)
3080 dhcp_client->information_req_cb = func;
3081 dhcp_client->information_req_data = data;
3083 case G_DHCP_CLIENT_EVENT_SOLICITATION:
3084 if (dhcp_client->type != G_DHCP_IPV6)
3086 dhcp_client->solicitation_cb = func;
3087 dhcp_client->solicitation_data = data;
3089 case G_DHCP_CLIENT_EVENT_ADVERTISE:
3090 if (dhcp_client->type != G_DHCP_IPV6)
3092 dhcp_client->advertise_cb = func;
3093 dhcp_client->advertise_data = data;
3095 case G_DHCP_CLIENT_EVENT_REQUEST:
3096 if (dhcp_client->type != G_DHCP_IPV6)
3098 dhcp_client->request_cb = func;
3099 dhcp_client->request_data = data;
3101 case G_DHCP_CLIENT_EVENT_RENEW:
3102 if (dhcp_client->type != G_DHCP_IPV6)
3104 dhcp_client->renew_cb = func;
3105 dhcp_client->renew_data = data;
3107 case G_DHCP_CLIENT_EVENT_REBIND:
3108 if (dhcp_client->type != G_DHCP_IPV6)
3110 dhcp_client->rebind_cb = func;
3111 dhcp_client->rebind_data = data;
3113 case G_DHCP_CLIENT_EVENT_RELEASE:
3114 if (dhcp_client->type != G_DHCP_IPV6)
3116 dhcp_client->release_cb = func;
3117 dhcp_client->release_data = data;
3119 case G_DHCP_CLIENT_EVENT_CONFIRM:
3120 if (dhcp_client->type != G_DHCP_IPV6)
3122 dhcp_client->confirm_cb = func;
3123 dhcp_client->confirm_data = data;
3125 case G_DHCP_CLIENT_EVENT_DECLINE:
3126 if (dhcp_client->type != G_DHCP_IPV6)
3128 dhcp_client->decline_cb = func;
3129 dhcp_client->decline_data = data;
3134 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
3136 return dhcp_client->ifindex;
3139 #if defined TIZEN_EXT
3140 char *g_dhcp_client_get_interface(GDHCPClient *dhcp_client)
3142 return dhcp_client->interface;
3146 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
3151 #if defined TIZEN_EXT
3152 return get_ip(htonl(dhcp_client->server_ip));
3154 return get_ip(dhcp_client->server_ip);
3158 #if defined TIZEN_EXT
3159 int g_dhcp_client_get_dhcp_lease_duration(GDHCPClient *dhcp_client)
3161 return dhcp_client->dhcp_lease_seconds;
3165 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
3167 return g_strdup(dhcp_client->assigned_ip);
3170 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
3172 GList *option = NULL;
3174 if (dhcp_client->type == G_DHCP_IPV6)
3177 switch (dhcp_client->state) {
3179 case IPV4LL_MONITOR:
3180 return g_strdup("255.255.0.0");
3184 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3186 return g_strdup(option->data);
3187 case INIT_SELECTING:
3193 case IPV4LL_ANNOUNCE:
3194 case INFORMATION_REQ:
3207 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3208 unsigned int option_code)
3210 if (!g_list_find(dhcp_client->request_list,
3211 GINT_TO_POINTER((int)option_code)))
3212 dhcp_client->request_list = g_list_prepend(
3213 dhcp_client->request_list,
3214 (GINT_TO_POINTER((int) option_code)));
3216 return G_DHCP_CLIENT_ERROR_NONE;
3219 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3221 g_list_free(dhcp_client->request_list);
3222 dhcp_client->request_list = NULL;
3225 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3227 g_hash_table_remove_all(dhcp_client->send_value_hash);
3230 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3234 storage = g_try_malloc(size + OPT_DATA);
3238 storage[OPT_CODE] = code;
3239 storage[OPT_LEN] = size;
3240 memcpy(&storage[OPT_DATA], data, size);
3245 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3248 return alloc_dhcp_option(code, data, MIN(size, 255));
3251 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3253 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3256 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3258 const unsigned maclen = 6;
3259 const unsigned idlen = maclen + 1;
3260 const uint8_t option_code = G_DHCP_CLIENT_ID;
3261 uint8_t idbuf[idlen];
3262 uint8_t *data_option;
3264 idbuf[0] = ARPHRD_ETHER;
3266 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3268 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3270 return G_DHCP_CLIENT_ERROR_NOMEM;
3272 g_hash_table_insert(dhcp_client->send_value_hash,
3273 GINT_TO_POINTER((int) option_code), data_option);
3275 return G_DHCP_CLIENT_ERROR_NONE;
3278 /* Now only support send hostname and vendor class ID */
3279 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3280 unsigned char option_code, const char *option_value)
3282 uint8_t *binary_option;
3284 if ((option_code == G_DHCP_HOST_NAME ||
3285 option_code == G_DHCP_VENDOR_CLASS_ID) && option_value) {
3286 binary_option = alloc_dhcp_string_option(option_code,
3289 return G_DHCP_CLIENT_ERROR_NOMEM;
3291 g_hash_table_insert(dhcp_client->send_value_hash,
3292 GINT_TO_POINTER((int) option_code), binary_option);
3295 return G_DHCP_CLIENT_ERROR_NONE;
3298 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3303 storage = g_malloc(2 + 2 + len);
3307 storage[0] = code >> 8;
3308 storage[1] = code & 0xff;
3309 storage[2] = len >> 8;
3310 storage[3] = len & 0xff;
3311 memcpy(storage + 2 + 2, option, len);
3316 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3318 return g_hash_table_remove(dhcp_client->send_value_hash,
3319 GINT_TO_POINTER((int)code));
3322 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3323 uint16_t option_code,
3324 uint8_t *option_value,
3325 uint16_t option_len)
3328 uint8_t *binary_option;
3330 debug(dhcp_client, "setting option %d to %p len %d",
3331 option_code, option_value, option_len);
3333 binary_option = alloc_dhcpv6_option(option_code, option_value,
3336 g_hash_table_insert(dhcp_client->send_value_hash,
3337 GINT_TO_POINTER((int) option_code),
3342 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3344 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3347 dhcp_client->last_request = time(NULL);
3350 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3352 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3355 return dhcp_client->status_code;
3358 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3363 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3368 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3373 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3376 g_dhcp_client_stop(dhcp_client);
3378 g_free(dhcp_client->interface);
3379 g_free(dhcp_client->assigned_ip);
3380 g_free(dhcp_client->last_address);
3381 g_free(dhcp_client->duid);
3382 g_free(dhcp_client->server_duid);
3384 g_list_free(dhcp_client->request_list);
3385 g_list_free(dhcp_client->require_list);
3387 g_hash_table_destroy(dhcp_client->code_value_hash);
3388 g_hash_table_destroy(dhcp_client->send_value_hash);
3389 g_hash_table_destroy(dhcp_client->secs_bcast_hash);
3391 #if defined TIZEN_EXT
3392 dhcp_client->interface = NULL;
3393 dhcp_client->assigned_ip = NULL;
3394 dhcp_client->last_address = NULL;
3395 dhcp_client->duid = NULL;
3396 dhcp_client->server_duid = NULL;
3399 g_free(dhcp_client);
3402 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3403 GDHCPDebugFunc func, gpointer user_data)
3408 dhcp_client->debug_func = func;
3409 dhcp_client->debug_data = user_data;
3412 static GDHCPIAPrefix *copy_prefix(gpointer data)
3414 GDHCPIAPrefix *copy, *prefix = data;
3416 copy = g_try_new(GDHCPIAPrefix, 1);
3420 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3425 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3427 GSList *copy = NULL;
3430 for (list = prefixes; list; list = list->next)
3431 copy = g_slist_prepend(copy, copy_prefix(list->data));
3436 #if defined TIZEN_EXT
3437 void g_dhcp_client_set_address_known(GDHCPClient *dhcp_client, gboolean known)
3439 /* DHCPREQUEST during INIT-REBOOT state (rfc2131)
3440 * 4.4.3 Initialization with known network address
3441 * 4.3.2 DHCPREQUEST generated during INIT-REBOOT state
3443 debug(dhcp_client, "known network address (%d)", known);
3445 if (dhcp_client->init_reboot == known)
3448 dhcp_client->init_reboot = known;