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
56 #define REQUEST_TIMEOUT 5
58 #define REQUEST_RETRIES 3
61 #define DISCOVER_TIMEOUT_WIFI 1
62 #define DISCOVER_RETRIES_WIFI 10
63 static int dhcp_discover_timeout = DISCOVER_TIMEOUT_WIFI;
64 static int dhcp_discover_max_retry = DISCOVER_RETRIES_WIFI;
66 void set_dhcp_discover_timeout(int timeout_value)
68 dhcp_discover_timeout = timeout_value;
71 void set_dhcp_discover_retry_count(int retry_count)
73 dhcp_discover_max_retry = retry_count;
77 typedef enum _listen_mode {
84 typedef enum _dhcp_client_state {
107 struct _GDHCPClient {
113 uint8_t mac_address[6];
116 uint32_t requested_ip;
119 uint32_t lease_seconds;
120 ListenMode listen_mode;
123 uint8_t ack_retry_times;
129 guint listener_watch;
132 GHashTable *code_value_hash;
133 GHashTable *send_value_hash;
134 GHashTable *secs_bcast_hash;
135 GDHCPClientEventFunc lease_available_cb;
136 gpointer lease_available_data;
137 GDHCPClientEventFunc ipv4ll_available_cb;
138 gpointer ipv4ll_available_data;
139 GDHCPClientEventFunc no_lease_cb;
140 gpointer no_lease_data;
141 GDHCPClientEventFunc lease_lost_cb;
142 gpointer lease_lost_data;
143 GDHCPClientEventFunc ipv4ll_lost_cb;
144 gpointer ipv4ll_lost_data;
145 GDHCPClientEventFunc address_conflict_cb;
146 gpointer address_conflict_data;
147 GDHCPDebugFunc debug_func;
149 GDHCPClientEventFunc information_req_cb;
150 gpointer information_req_data;
151 GDHCPClientEventFunc solicitation_cb;
152 gpointer solicitation_data;
153 GDHCPClientEventFunc advertise_cb;
154 gpointer advertise_data;
155 GDHCPClientEventFunc request_cb;
156 gpointer request_data;
157 GDHCPClientEventFunc renew_cb;
159 GDHCPClientEventFunc rebind_cb;
160 gpointer rebind_data;
161 GDHCPClientEventFunc release_cb;
162 gpointer release_data;
163 GDHCPClientEventFunc confirm_cb;
164 gpointer confirm_data;
165 GDHCPClientEventFunc decline_cb;
166 gpointer decline_data;
170 unsigned char *server_duid;
172 uint16_t status_code;
175 struct in6_addr ia_na;
176 struct in6_addr ia_ta;
180 struct timeval start_time;
182 #if defined TIZEN_EXT
183 uint32_t dhcp_lease_seconds;
184 gboolean init_reboot;
188 static inline void debug(GDHCPClient *client, const char *format, ...)
193 if (!client->debug_func)
196 va_start(ap, format);
198 if (vsnprintf(str, sizeof(str), format, ap) > 0)
199 client->debug_func(str, client->debug_data);
204 /* Initialize the packet with the proper defaults */
205 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
207 if (dhcp_client->type == G_DHCP_IPV6)
208 dhcpv6_init_header(pkt, type);
210 struct dhcp_packet *packet = pkt;
212 dhcp_init_header(packet, type);
213 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
217 static void add_request_options(GDHCPClient *dhcp_client,
218 struct dhcp_packet *packet)
223 int end = dhcp_end_option(packet->options);
225 for (list = dhcp_client->request_list; list; list = list->next) {
226 code = (uint8_t) GPOINTER_TO_INT(list->data);
228 packet->options[end + OPT_DATA + len] = code;
233 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
234 packet->options[end + OPT_LEN] = len;
235 packet->options[end + OPT_DATA + len] = DHCP_END;
242 unsigned char **ptr_buf;
245 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
248 uint8_t *option = value;
250 struct hash_params *params = user_data;
252 /* option[0][1] contains option code */
253 len = option[2] << 8 | option[3];
255 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
258 memcpy(*params->ptr_buf, option, len + 2 + 2);
259 (*params->ptr_buf) += len + 2 + 2;
262 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
263 unsigned char *buf, int max_buf,
264 unsigned char **ptr_buf)
266 struct hash_params params = {
272 if (dhcp_client->type != G_DHCP_IPV6)
275 g_hash_table_foreach(dhcp_client->send_value_hash,
276 add_dhcpv6_binary_option, ¶ms);
278 *ptr_buf = *params.ptr_buf;
281 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
285 buf[1] = code & 0xff;
289 memcpy(&buf[4], msg, len);
292 static int32_t get_time_diff(struct timeval *tv)
297 gettimeofday(&now, NULL);
299 hsec = (now.tv_sec - tv->tv_sec) * 100;
300 hsec += (now.tv_usec - tv->tv_usec) / 10000;
305 static void remove_timeouts(GDHCPClient *dhcp_client)
308 if (dhcp_client->timeout > 0)
309 g_source_remove(dhcp_client->timeout);
310 if (dhcp_client->t1_timeout > 0)
311 g_source_remove(dhcp_client->t1_timeout);
312 if (dhcp_client->t2_timeout > 0)
313 g_source_remove(dhcp_client->t2_timeout);
314 if (dhcp_client->lease_timeout > 0)
315 g_source_remove(dhcp_client->lease_timeout);
317 dhcp_client->timeout = 0;
318 dhcp_client->t1_timeout = 0;
319 dhcp_client->t2_timeout = 0;
320 dhcp_client->lease_timeout = 0;
324 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
325 struct dhcpv6_packet *packet,
326 unsigned char *buf, int max_buf,
327 unsigned char **ptr_buf)
330 uint16_t code, value;
335 if (dhcp_client->type != G_DHCP_IPV6)
338 for (list = dhcp_client->request_list; list; list = list->next) {
339 code = (uint16_t) GPOINTER_TO_INT(list->data);
343 case G_DHCPV6_CLIENTID:
344 if (!dhcp_client->duid)
347 len = 2 + 2 + dhcp_client->duid_len;
348 if ((*ptr_buf + len) > (buf + max_buf)) {
349 debug(dhcp_client, "Too long dhcpv6 message "
350 "when writing client id option");
354 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
355 dhcp_client->duid_len, dhcp_client->duid);
360 case G_DHCPV6_SERVERID:
361 if (!dhcp_client->server_duid)
364 len = 2 + 2 + dhcp_client->server_duid_len;
365 if ((*ptr_buf + len) > (buf + max_buf)) {
366 debug(dhcp_client, "Too long dhcpv6 message "
367 "when writing server id option");
371 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
372 dhcp_client->server_duid_len,
373 dhcp_client->server_duid);
378 case G_DHCPV6_RAPID_COMMIT:
380 if ((*ptr_buf + len) > (buf + max_buf)) {
381 debug(dhcp_client, "Too long dhcpv6 message "
382 "when writing rapid commit option");
386 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
394 case G_DHCPV6_ELAPSED_TIME:
395 if (!dhcp_client->retransmit) {
397 * Initial message, elapsed time is 0.
401 diff = get_time_diff(&dhcp_client->start_time);
402 if (diff < 0 || diff > 0xffff)
407 if ((*ptr_buf + len) > (buf + max_buf)) {
408 debug(dhcp_client, "Too long dhcpv6 message "
409 "when writing elapsed time option");
413 value = htons((uint16_t)diff);
414 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
415 2, (uint8_t *)&value);
420 case G_DHCPV6_DNS_SERVERS:
423 case G_DHCPV6_DOMAIN_LIST:
426 case G_DHCPV6_SNTP_SERVERS:
434 debug(dhcp_client, "option %d len %d added", code, len);
438 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
440 uint8_t *option = value;
441 struct dhcp_packet *packet = user_data;
443 dhcp_add_binary_option(packet, option);
446 static void add_send_options(GDHCPClient *dhcp_client,
447 struct dhcp_packet *packet)
449 g_hash_table_foreach(dhcp_client->send_value_hash,
450 add_binary_option, packet);
454 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
455 * represents the number of seconds elapsed from the start of
456 * attempting DHCP to satisfy some DHCP servers that allow for an
457 * "authoritative" reply before responding.
459 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
461 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
464 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
466 struct dhcp_packet packet;
468 debug(dhcp_client, "sending DHCP discover request");
470 init_packet(dhcp_client, &packet, DHCPDISCOVER);
472 packet.xid = dhcp_client->xid;
473 packet.secs = dhcp_attempt_secs(dhcp_client);
476 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
478 /* Explicitly saying that we want RFC-compliant packets helps
479 * some buggy DHCP servers to NOT send bigger packets */
480 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
482 add_request_options(dhcp_client, &packet);
484 add_send_options(dhcp_client, &packet);
487 * If we do not get a reply to DISCOVER packet, then we try with
488 * broadcast flag set. So first packet is sent without broadcast flag,
489 * first retry is with broadcast flag, second retry is without it etc.
490 * Reason is various buggy routers/AP that either eat the other or vice
491 * versa. In the receiving side we then find out what kind of packet
492 * the server can send.
494 dhcp_client->request_bcast = dhcp_client->retry_times % 2;
496 if (dhcp_client->request_bcast)
497 g_hash_table_add(dhcp_client->secs_bcast_hash,
498 GINT_TO_POINTER(packet.secs));
500 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
501 INADDR_BROADCAST, SERVER_PORT,
502 MAC_BCAST_ADDR, dhcp_client->ifindex,
503 dhcp_client->request_bcast);
506 int g_dhcp_client_decline(GDHCPClient *dhcp_client, uint32_t requested)
508 struct dhcp_packet packet;
510 dhcp_client->state = DECLINED;
511 dhcp_client->retry_times = 0;
513 debug(dhcp_client, "sending DHCP decline");
515 init_packet(dhcp_client, &packet, DHCPDECLINE);
517 packet.xid = dhcp_client->xid;
518 packet.secs = dhcp_attempt_secs(dhcp_client);
521 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
523 add_send_options(dhcp_client, &packet);
525 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
526 INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
527 dhcp_client->ifindex, true);
530 static int send_request(GDHCPClient *dhcp_client)
532 struct dhcp_packet packet;
534 debug(dhcp_client, "sending DHCP request (state %d)",
537 init_packet(dhcp_client, &packet, DHCPREQUEST);
539 packet.xid = dhcp_client->xid;
540 #if defined TIZEN_EXT
541 if (dhcp_client->init_reboot != TRUE)
543 packet.secs = dhcp_attempt_secs(dhcp_client);
545 if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
546 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
547 dhcp_client->requested_ip);
549 if (dhcp_client->state == REQUESTING)
550 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
551 dhcp_client->server_ip);
553 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
555 add_request_options(dhcp_client, &packet);
557 add_send_options(dhcp_client, &packet);
559 if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
560 packet.ciaddr = htonl(dhcp_client->requested_ip);
562 if (dhcp_client->state == RENEWING)
563 return dhcp_send_kernel_packet(&packet,
564 dhcp_client->requested_ip, CLIENT_PORT,
565 dhcp_client->server_ip, SERVER_PORT,
566 dhcp_client->interface);
568 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
569 INADDR_BROADCAST, SERVER_PORT,
570 MAC_BCAST_ADDR, dhcp_client->ifindex,
571 dhcp_client->request_bcast);
574 static int send_release(GDHCPClient *dhcp_client,
575 uint32_t server, uint32_t ciaddr)
577 struct dhcp_packet packet;
580 debug(dhcp_client, "sending DHCP release request");
582 init_packet(dhcp_client, &packet, DHCPRELEASE);
583 __connman_util_get_random(&rand);
585 packet.ciaddr = htonl(ciaddr);
587 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
589 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
591 dhcp_client->interface);
594 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
595 static int switch_listening_mode(GDHCPClient *dhcp_client,
596 ListenMode listen_mode);
598 static gboolean send_probe_packet(gpointer dhcp_data)
600 GDHCPClient *dhcp_client;
603 dhcp_client = dhcp_data;
604 /* if requested_ip is not valid, pick a new address*/
605 if (dhcp_client->requested_ip == 0) {
606 debug(dhcp_client, "pick a new random address");
607 dhcp_client->requested_ip = arp_random_ip();
610 debug(dhcp_client, "sending IPV4LL probe request");
612 if (dhcp_client->retry_times == 1) {
613 dhcp_client->state = IPV4LL_PROBE;
614 switch_listening_mode(dhcp_client, L_ARP);
616 arp_send_packet(dhcp_client->mac_address, 0,
617 dhcp_client->requested_ip, dhcp_client->ifindex);
619 if (dhcp_client->retry_times < PROBE_NUM) {
620 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
621 timeout = __connman_util_random_delay_ms(PROBE_MAX-PROBE_MIN);
622 timeout += PROBE_MIN*1000;
624 timeout = (ANNOUNCE_WAIT * 1000);
626 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
628 ipv4ll_probe_timeout,
634 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
635 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
637 static gboolean send_announce_packet(gpointer dhcp_data)
639 GDHCPClient *dhcp_client;
641 dhcp_client = dhcp_data;
643 debug(dhcp_client, "sending IPV4LL announce request");
645 arp_send_packet(dhcp_client->mac_address,
646 dhcp_client->requested_ip,
647 dhcp_client->requested_ip,
648 dhcp_client->ifindex);
650 remove_timeouts(dhcp_client);
652 if (dhcp_client->state == IPV4LL_DEFEND) {
653 dhcp_client->timeout =
654 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
656 ipv4ll_defend_timeout,
661 dhcp_client->timeout =
662 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
664 ipv4ll_announce_timeout,
670 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
675 dhcp_client->retransmit = true;
678 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
683 dhcp_client->retransmit = false;
686 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
687 unsigned char **duid, int *duid_len)
692 case G_DHCPV6_DUID_LLT:
693 *duid_len = 2 + 2 + 4 + ETH_ALEN;
694 *duid = g_try_malloc(*duid_len);
700 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
703 duid_time = time(NULL) - DUID_TIME_EPOCH;
704 (*duid)[4] = duid_time >> 24;
705 (*duid)[5] = duid_time >> 16;
706 (*duid)[6] = duid_time >> 8;
707 (*duid)[7] = duid_time & 0xff;
709 case G_DHCPV6_DUID_EN:
711 case G_DHCPV6_DUID_LL:
712 *duid_len = 2 + 2 + ETH_ALEN;
713 *duid = g_try_malloc(*duid_len);
719 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2]);
728 static gchar *convert_to_hex(unsigned char *buf, int len)
730 gchar *ret = g_try_malloc(len * 2 + 1);
733 for (i = 0; ret && i < len; i++)
734 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
739 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
742 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
745 g_free(dhcp_client->duid);
747 dhcp_client->duid = duid;
748 dhcp_client->duid_len = duid_len;
750 if (dhcp_client->debug_func) {
751 gchar *hex = convert_to_hex(duid, duid_len);
752 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
759 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
760 uint32_t *T2, GSList *prefixes)
762 uint8_t options[1452];
763 unsigned int max_buf = sizeof(options);
764 int len, count = g_slist_length(prefixes);
766 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
769 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
771 memset(options, 0, sizeof(options));
773 options[0] = dhcp_client->iaid >> 24;
774 options[1] = dhcp_client->iaid >> 16;
775 options[2] = dhcp_client->iaid >> 8;
776 options[3] = dhcp_client->iaid;
779 uint32_t t = htonl(*T1);
780 memcpy(&options[4], &t, 4);
784 uint32_t t = htonl(*T2);
785 memcpy(&options[8], &t, 4);
793 for (list = prefixes; list; list = list->next) {
794 GDHCPIAPrefix *prefix = list->data;
795 uint8_t sub_option[4+4+1+16];
797 if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
799 "Too long dhcpv6 message "
800 "when writing IA prefix option");
804 memset(&sub_option, 0, sizeof(sub_option));
806 /* preferred and validity time are left zero */
808 sub_option[8] = prefix->prefixlen;
809 memcpy(&sub_option[9], &prefix->prefix, 16);
811 copy_option(&options[len], G_DHCPV6_IA_PREFIX,
812 sizeof(sub_option), sub_option);
813 len += 2 + 2 + sizeof(sub_option);
817 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
823 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
825 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
828 return dhcp_client->iaid;
831 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
833 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
836 dhcp_client->iaid = iaid;
839 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
844 __connman_inet_get_interface_mac_address(index, buf);
846 memcpy(iaid, &buf[2], 4);
847 dhcp_client->iaid = iaid[0] << 24 |
848 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
851 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
852 uint32_t *T1, uint32_t *T2,
856 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
860 *T1 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
864 *T2 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
868 *started = dhcp_client->last_request;
871 *expire = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
872 dhcp_client->last_request + dhcp_client->expire;
877 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
881 buf[1] = G_DHCPV6_IAADDR;
884 memcpy(&buf[4], &dhcp_client->ia_na, 16);
885 memset(&buf[20], 0, 4); /* preferred */
886 memset(&buf[24], 0, 4); /* valid */
890 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
893 struct in6_addr addr;
895 if (inet_pton(AF_INET6, address, &addr) != 1)
899 buf[1] = G_DHCPV6_IAADDR;
902 memcpy(&buf[4], &addr, 16);
903 memset(&buf[20], 0, 4); /* preferred */
904 memset(&buf[24], 0, 4); /* valid */
908 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
912 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
914 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
924 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
925 int code, uint32_t *T1, uint32_t *T2,
926 bool add_iaaddr, const char *ia_na)
928 if (code == G_DHCPV6_IA_TA) {
929 uint8_t ia_options[4];
931 put_iaid(dhcp_client, index, ia_options);
933 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
934 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
935 ia_options, sizeof(ia_options));
937 } else if (code == G_DHCPV6_IA_NA) {
938 struct in6_addr addr;
940 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
943 * If caller has specified the IPv6 address it wishes to
944 * to use (ia_na != NULL and address is valid), then send
945 * the address to server.
946 * If caller did not specify the address (ia_na == NULL) and
947 * if the current address is not set, then we should not send
948 * the address sub-option.
950 if (add_iaaddr && ((!ia_na &&
951 !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
953 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
954 #define IAADDR_LEN (16+4+4)
955 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
958 memcpy(&dhcp_client->ia_na, &addr,
959 sizeof(struct in6_addr));
961 put_iaid(dhcp_client, index, ia_options);
964 ia_options[4] = *T1 >> 24;
965 ia_options[5] = *T1 >> 16;
966 ia_options[6] = *T1 >> 8;
969 memset(&ia_options[4], 0x00, 4);
972 ia_options[8] = *T2 >> 24;
973 ia_options[9] = *T2 >> 16;
974 ia_options[10] = *T2 >> 8;
975 ia_options[11] = *T2;
977 memset(&ia_options[8], 0x00, 4);
979 create_iaaddr(dhcp_client, &ia_options[12],
982 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
983 ia_options, sizeof(ia_options));
985 uint8_t ia_options[4+4+4];
987 put_iaid(dhcp_client, index, ia_options);
989 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
990 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
992 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
993 ia_options, sizeof(ia_options));
1002 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
1003 int code, uint32_t *T1, uint32_t *T2,
1007 uint8_t *ia_options, *pos;
1008 int len, count, total_len;
1010 count = g_slist_length(addresses);
1014 g_dhcp_client_set_request(dhcp_client, code);
1016 if (code == G_DHCPV6_IA_TA)
1018 else if (code == G_DHCPV6_IA_NA)
1019 len = 4 + 4 + 4; /* IAID + T1 + T2 */
1023 total_len = len + count * (2 + 2 + 16 + 4 + 4);
1024 ia_options = g_try_malloc0(total_len);
1028 put_iaid(dhcp_client, index, ia_options);
1030 pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
1032 for (list = addresses; list; list = list->next) {
1033 pos = append_iaaddr(dhcp_client, pos, list->data);
1038 if (code == G_DHCPV6_IA_NA) {
1040 ia_options[4] = *T1 >> 24;
1041 ia_options[5] = *T1 >> 16;
1042 ia_options[6] = *T1 >> 8;
1043 ia_options[7] = *T1;
1045 memset(&ia_options[4], 0x00, 4);
1048 ia_options[8] = *T2 >> 24;
1049 ia_options[9] = *T2 >> 16;
1050 ia_options[10] = *T2 >> 8;
1051 ia_options[11] = *T2;
1053 memset(&ia_options[8], 0x00, 4);
1056 g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1063 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1066 int i, j, len = sizeof(uint16_t) * args;
1069 values = g_try_malloc(len);
1074 for (i = 0, j = 0; i < args; i++) {
1075 uint16_t value = va_arg(va, int);
1076 values[j++] = value >> 8;
1077 values[j++] = value & 0xff;
1081 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1088 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1090 struct dhcpv6_packet *packet;
1091 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1095 memset(buf, 0, sizeof(buf));
1096 packet = (struct dhcpv6_packet *)&buf[0];
1097 ptr = buf + sizeof(struct dhcpv6_packet);
1099 init_packet(dhcp_client, packet, type);
1101 if (!dhcp_client->retransmit) {
1102 dhcp_client->xid = packet->transaction_id[0] << 16 |
1103 packet->transaction_id[1] << 8 |
1104 packet->transaction_id[2];
1105 gettimeofday(&dhcp_client->start_time, NULL);
1107 packet->transaction_id[0] = dhcp_client->xid >> 16;
1108 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1109 packet->transaction_id[2] = dhcp_client->xid;
1112 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1114 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1117 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1119 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1121 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1123 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1125 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1129 static int send_solicitation(GDHCPClient *dhcp_client)
1131 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1134 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1136 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1139 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1141 return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1144 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1146 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1149 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1151 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1154 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1156 return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1159 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1161 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1164 static int send_information_req(GDHCPClient *dhcp_client)
1166 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1170 static void remove_value(gpointer data, gpointer user_data)
1176 static void remove_option_value(gpointer data)
1178 GList *option_value = data;
1180 g_list_foreach(option_value, remove_value, NULL);
1181 g_list_free(option_value);
1184 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1185 int ifindex, GDHCPClientError *error)
1187 GDHCPClient *dhcp_client;
1190 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1194 dhcp_client = g_try_new0(GDHCPClient, 1);
1196 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1200 dhcp_client->interface = get_interface_name(ifindex);
1201 if (!dhcp_client->interface) {
1202 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1206 if (!interface_is_up(ifindex)) {
1207 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1211 __connman_inet_get_interface_mac_address(ifindex, dhcp_client->mac_address);
1213 dhcp_client->listener_sockfd = -1;
1214 dhcp_client->listen_mode = L_NONE;
1215 dhcp_client->ref_count = 1;
1216 dhcp_client->type = type;
1217 dhcp_client->ifindex = ifindex;
1218 dhcp_client->lease_available_cb = NULL;
1219 dhcp_client->ipv4ll_available_cb = NULL;
1220 dhcp_client->no_lease_cb = NULL;
1221 dhcp_client->lease_lost_cb = NULL;
1222 dhcp_client->ipv4ll_lost_cb = NULL;
1223 dhcp_client->address_conflict_cb = NULL;
1224 dhcp_client->listener_watch = 0;
1225 dhcp_client->retry_times = 0;
1226 dhcp_client->ack_retry_times = 0;
1227 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1228 g_direct_equal, NULL, remove_option_value);
1229 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1230 g_direct_equal, NULL, g_free);
1231 dhcp_client->secs_bcast_hash = g_hash_table_new(g_direct_hash,
1233 dhcp_client->request_list = NULL;
1234 dhcp_client->require_list = NULL;
1235 dhcp_client->duid = NULL;
1236 dhcp_client->duid_len = 0;
1237 dhcp_client->last_request = time(NULL);
1238 dhcp_client->expire = 0;
1239 dhcp_client->request_bcast = false;
1241 *error = G_DHCP_CLIENT_ERROR_NONE;
1246 g_free(dhcp_client->interface);
1247 g_free(dhcp_client);
1251 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1253 static int dhcp_l2_socket(int ifindex)
1256 struct sockaddr_ll sock;
1261 * I've selected not to see LL header, so BPF doesn't see it, too.
1262 * The filter may also pass non-IP and non-ARP packets, but we do
1263 * a more complete check when receiving the message in userspace.
1265 * and filter shamelessly stolen from:
1267 * http://www.flamewarmaster.de/software/dhcpclient/
1269 * There are a few other interesting ideas on that page (look under
1270 * "Motivation"). Use of netlink events is most interesting. Think
1271 * of various network servers listening for events and reconfiguring.
1272 * That would obsolete sending HUP signals and/or make use of restarts.
1274 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1277 * TODO: make conditional?
1279 static const struct sock_filter filter_instr[] = {
1281 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1282 /* L5, L1, is UDP? */
1283 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1284 /* ugly check for arp on ethernet-like and IPv4 */
1285 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1286 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1287 /* skip IP header */
1288 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1289 /* check udp source and destination ports */
1290 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1292 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1294 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1295 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1298 static const struct sock_fprog filter_prog = {
1299 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1300 /* casting const away: */
1301 .filter = (struct sock_filter *) filter_instr,
1304 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1308 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1309 /* Use only if standard ports are in use */
1310 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1311 sizeof(filter_prog));
1313 memset(&sock, 0, sizeof(sock));
1314 sock.sll_family = AF_PACKET;
1315 sock.sll_protocol = htons(ETH_P_IP);
1316 sock.sll_ifindex = ifindex;
1318 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1327 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1329 if (packet->ip.protocol != IPPROTO_UDP)
1332 if (packet->ip.version != IPVERSION)
1335 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1338 if (packet->udp.dest != htons(CLIENT_PORT))
1341 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1347 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1348 struct sockaddr_in *dst_addr)
1351 struct ip_udp_dhcp_packet packet;
1354 memset(&packet, 0, sizeof(packet));
1356 bytes = read(fd, &packet, sizeof(packet));
1360 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1363 if (bytes < ntohs(packet.ip.tot_len))
1364 /* packet is bigger than sizeof(packet), we did partial read */
1367 /* ignore any extra garbage bytes */
1368 bytes = ntohs(packet.ip.tot_len);
1370 if (!sanity_check(&packet, bytes))
1373 check = packet.ip.check;
1374 packet.ip.check = 0;
1375 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1378 /* verify UDP checksum. IP header has to be modified for this */
1379 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1380 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1381 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1382 check = packet.udp.check;
1383 packet.udp.check = 0;
1384 if (check && check != dhcp_checksum(&packet, bytes))
1387 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1388 sizeof(packet.udp)));
1390 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1393 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1395 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1398 static void ipv4ll_start(GDHCPClient *dhcp_client)
1402 remove_timeouts(dhcp_client);
1404 switch_listening_mode(dhcp_client, L_NONE);
1405 dhcp_client->retry_times = 0;
1406 dhcp_client->requested_ip = 0;
1408 dhcp_client->requested_ip = arp_random_ip();
1410 /*first wait a random delay to avoid storm of arp request on boot*/
1411 timeout = __connman_util_random_delay_ms(PROBE_WAIT);
1413 dhcp_client->retry_times++;
1414 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1421 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1424 switch_listening_mode(dhcp_client, L_NONE);
1426 remove_timeouts(dhcp_client);
1428 if (dhcp_client->listener_watch > 0) {
1429 g_source_remove(dhcp_client->listener_watch);
1430 dhcp_client->listener_watch = 0;
1433 dhcp_client->state = IPV4LL_PROBE;
1434 dhcp_client->retry_times = 0;
1435 dhcp_client->requested_ip = 0;
1437 g_free(dhcp_client->assigned_ip);
1438 dhcp_client->assigned_ip = NULL;
1441 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1444 struct ether_arp arp;
1445 uint32_t ip_requested;
1446 int source_conflict;
1447 int target_conflict;
1450 memset(&arp, 0, sizeof(arp));
1451 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1455 if (arp.arp_op != htons(ARPOP_REPLY) &&
1456 arp.arp_op != htons(ARPOP_REQUEST))
1459 if (memcmp(arp.arp_sha, dhcp_client->mac_address, ETH_ALEN) == 0)
1462 ip_requested = htonl(dhcp_client->requested_ip);
1463 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1464 sizeof(ip_requested));
1466 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1467 sizeof(ip_requested));
1469 if (!source_conflict && !target_conflict)
1472 dhcp_client->conflicts++;
1474 debug(dhcp_client, "IPV4LL conflict detected");
1476 if (dhcp_client->state == IPV4LL_MONITOR) {
1477 if (!source_conflict)
1479 dhcp_client->state = IPV4LL_DEFEND;
1480 debug(dhcp_client, "DEFEND mode conflicts : %d",
1481 dhcp_client->conflicts);
1482 /*Try to defend with a single announce*/
1483 send_announce_packet(dhcp_client);
1487 if (dhcp_client->state == IPV4LL_DEFEND) {
1488 if (!source_conflict)
1490 else if (dhcp_client->ipv4ll_lost_cb)
1491 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1492 dhcp_client->ipv4ll_lost_data);
1495 ipv4ll_stop(dhcp_client);
1497 /* If we got a lot of conflicts, RFC3927 states that we have
1498 * to wait RATE_LIMIT_INTERVAL before retrying,
1500 if (dhcp_client->conflicts < MAX_CONFLICTS)
1501 timeout_ms = __connman_util_random_delay_ms(PROBE_WAIT);
1503 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
1504 dhcp_client->retry_times++;
1505 dhcp_client->timeout =
1506 g_timeout_add_full(G_PRIORITY_HIGH,
1515 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1517 if (dhcp_client->type == G_DHCP_IPV6) {
1518 struct dhcpv6_packet *packet6 = pkt;
1524 xid = packet6->transaction_id[0] << 16 |
1525 packet6->transaction_id[1] << 8 |
1526 packet6->transaction_id[2];
1528 if (xid != dhcp_client->xid)
1531 struct dhcp_packet *packet = pkt;
1533 if (packet->xid != dhcp_client->xid)
1536 if (packet->hlen != 6)
1539 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1546 static void start_request(GDHCPClient *dhcp_client);
1548 static gboolean request_timeout(gpointer user_data)
1550 GDHCPClient *dhcp_client = user_data;
1552 #if defined TIZEN_EXT
1553 if (dhcp_client->init_reboot) {
1554 debug(dhcp_client, "DHCPREQUEST of INIT-REBOOT has failed");
1556 /* Start DHCPDISCOVERY when DHCPREQUEST of INIT-REBOOT has failed */
1557 g_dhcp_client_set_address_known(dhcp_client, FALSE);
1559 dhcp_client->retry_times = 0;
1560 dhcp_client->requested_ip = 0;
1562 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1567 debug(dhcp_client, "request timeout (retries %d)",
1568 dhcp_client->retry_times);
1570 dhcp_client->retry_times++;
1572 start_request(dhcp_client);
1577 static void listener_watch_destroy(gpointer user_data)
1579 GDHCPClient *dhcp_client = user_data;
1580 g_dhcp_client_unref(dhcp_client);
1583 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1584 gpointer user_data);
1586 static int switch_listening_mode(GDHCPClient *dhcp_client,
1587 ListenMode listen_mode)
1589 GIOChannel *listener_channel;
1590 int listener_sockfd;
1592 if (dhcp_client->listen_mode == listen_mode)
1595 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1596 dhcp_client->listen_mode, listen_mode);
1598 if (dhcp_client->listen_mode != L_NONE) {
1599 if (dhcp_client->listener_watch > 0)
1600 g_source_remove(dhcp_client->listener_watch);
1601 dhcp_client->listen_mode = L_NONE;
1602 dhcp_client->listener_sockfd = -1;
1603 dhcp_client->listener_watch = 0;
1606 if (listen_mode == L_NONE)
1609 if (listen_mode == L2)
1610 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1611 else if (listen_mode == L3) {
1612 if (dhcp_client->type == G_DHCP_IPV6)
1613 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1614 dhcp_client->interface,
1617 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1618 dhcp_client->interface,
1620 } else if (listen_mode == L_ARP)
1621 listener_sockfd = arp_socket(dhcp_client->ifindex);
1625 if (listener_sockfd < 0)
1628 listener_channel = g_io_channel_unix_new(listener_sockfd);
1629 if (!listener_channel) {
1630 /* Failed to create listener channel */
1631 close(listener_sockfd);
1635 dhcp_client->listen_mode = listen_mode;
1636 dhcp_client->listener_sockfd = listener_sockfd;
1638 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1639 dhcp_client->listener_watch =
1640 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1641 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1642 listener_event, g_dhcp_client_ref(dhcp_client),
1643 listener_watch_destroy);
1644 g_io_channel_unref(listener_channel);
1649 static void start_request(GDHCPClient *dhcp_client)
1651 debug(dhcp_client, "start request (retries %d)",
1652 dhcp_client->retry_times);
1654 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1655 if (dhcp_client->no_lease_cb)
1656 dhcp_client->no_lease_cb(dhcp_client,
1657 dhcp_client->no_lease_data);
1661 if (dhcp_client->retry_times == 0) {
1662 dhcp_client->state = REQUESTING;
1663 switch_listening_mode(dhcp_client, L2);
1666 send_request(dhcp_client);
1668 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1675 static uint32_t get_lease(struct dhcp_packet *packet)
1678 uint32_t lease_seconds;
1680 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1684 lease_seconds = get_be32(option);
1686 if (lease_seconds < 10)
1689 return lease_seconds;
1692 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1694 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1696 remove_timeouts(dhcp_client);
1698 dhcp_client->retry_times = retry_times;
1699 dhcp_client->requested_ip = 0;
1700 dhcp_client->state = INIT_SELECTING;
1701 switch_listening_mode(dhcp_client, L2);
1703 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1706 static gboolean start_expire(gpointer user_data)
1708 GDHCPClient *dhcp_client = user_data;
1710 debug(dhcp_client, "lease expired");
1712 /*remove all timeouts if they are set*/
1713 remove_timeouts(dhcp_client);
1715 restart_dhcp(dhcp_client, 0);
1717 /* ip need to be cleared */
1718 if (dhcp_client->lease_lost_cb)
1719 dhcp_client->lease_lost_cb(dhcp_client,
1720 dhcp_client->lease_lost_data);
1725 static gboolean continue_rebound(gpointer user_data)
1727 GDHCPClient *dhcp_client = user_data;
1730 switch_listening_mode(dhcp_client, L2);
1731 send_request(dhcp_client);
1733 if (dhcp_client->t2_timeout> 0) {
1734 g_source_remove(dhcp_client->t2_timeout);
1735 dhcp_client->t2_timeout = 0;
1738 /*recalculate remaining rebind time*/
1739 dhcp_client->T2 >>= 1;
1740 if (dhcp_client->T2 > 60) {
1741 __connman_util_get_random(&rand);
1742 dhcp_client->t2_timeout =
1743 g_timeout_add_full(G_PRIORITY_HIGH,
1744 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1753 static gboolean start_rebound(gpointer user_data)
1755 GDHCPClient *dhcp_client = user_data;
1757 /*remove renew timer*/
1758 if (dhcp_client->t1_timeout > 0)
1759 g_source_remove(dhcp_client->t1_timeout);
1761 debug(dhcp_client, "start rebound");
1762 dhcp_client->state = REBINDING;
1764 /*calculate total rebind time*/
1765 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1767 /*send the first rebound and reschedule*/
1768 continue_rebound(user_data);
1773 static gboolean continue_renew (gpointer user_data)
1775 GDHCPClient *dhcp_client = user_data;
1778 switch_listening_mode(dhcp_client, L3);
1779 send_request(dhcp_client);
1781 if (dhcp_client->t1_timeout > 0)
1782 g_source_remove(dhcp_client->t1_timeout);
1784 dhcp_client->t1_timeout = 0;
1786 dhcp_client->T1 >>= 1;
1788 if (dhcp_client->T1 > 60) {
1789 __connman_util_get_random(&rand);
1790 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1791 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1799 static gboolean start_renew(gpointer user_data)
1801 GDHCPClient *dhcp_client = user_data;
1803 debug(dhcp_client, "start renew");
1804 dhcp_client->state = RENEWING;
1806 /*calculate total renew period*/
1807 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1809 /*send first renew and reschedule for half the remaining time.*/
1810 continue_renew(user_data);
1815 static void start_bound(GDHCPClient *dhcp_client)
1817 debug(dhcp_client, "start bound");
1819 dhcp_client->state = BOUND;
1821 remove_timeouts(dhcp_client);
1824 *TODO: T1 and T2 should be set through options instead of
1825 * defaults as they are here.
1828 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1829 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1830 dhcp_client->expire = dhcp_client->lease_seconds;
1832 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1834 start_renew, dhcp_client,
1837 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1839 start_rebound, dhcp_client,
1842 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1843 dhcp_client->expire,
1844 start_expire, dhcp_client,
1848 static gboolean restart_dhcp_timeout(gpointer user_data)
1850 GDHCPClient *dhcp_client = user_data;
1852 debug(dhcp_client, "restart DHCP timeout");
1854 if (dhcp_client->state == REBOOTING) {
1855 g_free(dhcp_client->last_address);
1856 dhcp_client->last_address = NULL;
1857 restart_dhcp(dhcp_client, 0);
1859 dhcp_client->ack_retry_times++;
1860 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1865 static char *get_ip(uint32_t ip)
1867 struct in_addr addr;
1871 return g_strdup(inet_ntoa(addr));
1874 /* get a rough idea of how long an option will be */
1875 static const uint8_t len_of_option_as_string[] = {
1876 [OPTION_IP] = sizeof("255.255.255.255 "),
1877 [OPTION_STRING] = 1,
1878 [OPTION_U8] = sizeof("255 "),
1879 [OPTION_U16] = sizeof("65535 "),
1880 [OPTION_U32] = sizeof("4294967295 "),
1883 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1885 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1888 /* Create "opt_value1 option_value2 ..." string */
1889 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1891 unsigned upper_length;
1895 len = option[OPT_LEN - OPT_DATA];
1896 type &= OPTION_TYPE_MASK;
1897 optlen = dhcp_option_lengths[type];
1900 upper_length = len_of_option_as_string[type] *
1901 ((unsigned)len / (unsigned)optlen);
1902 dest = ret = g_malloc(upper_length + 1);
1906 while (len >= optlen) {
1909 dest += sprint_nip(dest, "", option);
1912 uint16_t val_u16 = get_be16(option);
1913 dest += sprintf(dest, "%u", val_u16);
1917 uint32_t val_u32 = get_be32(option);
1918 dest += sprintf(dest, "%u", val_u32);
1922 memcpy(dest, option, len);
1939 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1947 if (type == OPTION_STRING)
1948 return g_list_append(list, g_strdup(value));
1950 while ((pos = strchr(pos, ' '))) {
1953 list = g_list_append(list, g_strdup(value));
1958 list = g_list_append(list, g_strdup(value));
1963 static inline uint32_t get_uint32(unsigned char *value)
1965 return value[0] << 24 | value[1] << 16 |
1966 value[2] << 8 | value[3];
1969 static inline uint16_t get_uint16(unsigned char *value)
1971 return value[0] << 8 | value[1];
1974 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1975 struct in6_addr *addr,
1976 unsigned char prefixlen, uint32_t preferred,
1979 GDHCPIAPrefix *ia_prefix;
1981 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1985 if (dhcp_client->debug_func) {
1986 char addr_str[INET6_ADDRSTRLEN + 1];
1987 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1988 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1989 addr_str, prefixlen, preferred, valid);
1992 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1993 ia_prefix->prefixlen = prefixlen;
1994 ia_prefix->preferred = preferred;
1995 ia_prefix->valid = valid;
1996 ia_prefix->expire = time(NULL) + valid;
1998 return g_list_prepend(list, ia_prefix);
2001 static GList *get_addresses(GDHCPClient *dhcp_client,
2003 unsigned char *value,
2007 struct in6_addr addr;
2008 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
2009 uint16_t option_len, option_code, st = 0, max_len;
2010 int addr_count = 0, prefix_count = 0, i, pos;
2011 unsigned char prefixlen;
2012 unsigned int shortest_valid = 0;
2016 if (!value || len < 4)
2019 iaid = get_uint32(&value[0]);
2020 if (dhcp_client->iaid != iaid)
2023 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
2024 T1 = get_uint32(&value[4]);
2025 T2 = get_uint32(&value[8]);
2028 /* IA_NA: RFC 3315, 22.4 */
2029 /* IA_PD: RFC 3633, ch 9 */
2039 max_len = len - pos;
2041 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
2043 /* We have more sub-options in this packet. */
2045 option = dhcpv6_get_sub_option(&value[pos], max_len,
2046 &option_code, &option_len);
2048 debug(dhcp_client, "pos %d option %p code %d len %d",
2049 pos, option, option_code, option_len);
2057 switch (option_code) {
2058 case G_DHCPV6_IAADDR:
2060 memcpy(&addr, &option[0], sizeof(addr));
2062 preferred = get_uint32(&option[i]);
2064 valid = get_uint32(&option[i]);
2069 case G_DHCPV6_STATUS_CODE:
2070 st = get_uint16(&option[0]);
2071 debug(dhcp_client, "error code %d", st);
2072 if (option_len > 2) {
2073 str = g_strndup((gchar *)&option[2],
2075 debug(dhcp_client, "error text: %s", str);
2082 case G_DHCPV6_IA_PREFIX:
2084 preferred = get_uint32(&option[i]);
2086 valid = get_uint32(&option[i]);
2088 prefixlen = option[i];
2090 memcpy(&addr, &option[i], sizeof(addr));
2092 if (preferred < valid) {
2093 /* RFC 3633, ch 10 */
2094 list = add_prefix(dhcp_client, list, &addr,
2095 prefixlen, preferred, valid);
2096 if (shortest_valid > valid)
2097 shortest_valid = valid;
2103 pos += 2 + 2 + option_len;
2105 } while (pos < len);
2107 if (addr_count > 0 && st == 0) {
2108 /* We only support one address atm */
2109 char addr_str[INET6_ADDRSTRLEN + 1];
2111 if (preferred > valid)
2112 /* RFC 3315, 22.6 */
2115 dhcp_client->T1 = T1;
2116 dhcp_client->T2 = T2;
2118 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2119 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2120 addr_count, addr_str, T1, T2);
2122 list = g_list_append(list, g_strdup(addr_str));
2124 if (code == G_DHCPV6_IA_NA)
2125 memcpy(&dhcp_client->ia_na, &addr,
2126 sizeof(struct in6_addr));
2128 memcpy(&dhcp_client->ia_ta, &addr,
2129 sizeof(struct in6_addr));
2131 if (valid != dhcp_client->expire)
2132 dhcp_client->expire = valid;
2135 if (prefix_count > 0 && list) {
2137 * This means we have a list of prefixes to delegate.
2139 list = g_list_reverse(list);
2141 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2142 prefix_count, T1, T2);
2144 dhcp_client->T1 = T1;
2145 dhcp_client->T2 = T2;
2147 dhcp_client->expire = shortest_valid;
2150 if (status && *status != 0)
2151 debug(dhcp_client, "status %d", *status);
2156 static GList *get_domains(int maxlen, unsigned char *value)
2162 char dns_name[NS_MAXDNAME + 1];
2164 if (!value || maxlen < 3)
2167 while (pos < maxlen) {
2168 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2170 c = (unsigned char *)dns_name;
2177 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2178 pos += (char *)c - dns_name + 1;
2181 return g_list_reverse(list);
2184 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2186 unsigned char *value,
2197 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2198 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2201 "%s server list length (%d) is invalid",
2202 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2206 for (i = 0; i < len; i += 16) {
2208 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2212 if (!inet_ntop(AF_INET6, &value[i], str,
2216 list = g_list_append(list, str);
2220 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2221 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2222 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2223 list = get_addresses(dhcp_client, code, len, value, status);
2226 case G_DHCPV6_DOMAIN_LIST:
2227 list = get_domains(len, value);
2237 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2238 struct dhcpv6_packet *packet,
2239 uint16_t pkt_len, uint16_t *status)
2241 GList *list, *value_list;
2244 uint16_t option_len;
2246 for (list = dhcp_client->request_list; list; list = list->next) {
2247 code = (uint16_t) GPOINTER_TO_INT(list->data);
2249 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2252 g_hash_table_remove(dhcp_client->code_value_hash,
2253 GINT_TO_POINTER((int) code));
2257 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2258 option_len, option, status);
2260 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2261 option_len, value_list);
2264 g_hash_table_remove(dhcp_client->code_value_hash,
2265 GINT_TO_POINTER((int) code));
2267 g_hash_table_insert(dhcp_client->code_value_hash,
2268 GINT_TO_POINTER((int) code), value_list);
2272 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2274 GDHCPOptionType type;
2275 GList *list, *value_list;
2280 for (list = dhcp_client->request_list; list; list = list->next) {
2281 code = (uint8_t) GPOINTER_TO_INT(list->data);
2283 option = dhcp_get_option(packet, code);
2285 g_hash_table_remove(dhcp_client->code_value_hash,
2286 GINT_TO_POINTER((int) code));
2290 type = dhcp_get_code_type(code);
2292 option_value = malloc_option_value_string(option, type);
2294 g_hash_table_remove(dhcp_client->code_value_hash,
2295 GINT_TO_POINTER((int) code));
2297 value_list = get_option_value_list(option_value, type);
2299 g_free(option_value);
2302 g_hash_table_remove(dhcp_client->code_value_hash,
2303 GINT_TO_POINTER((int) code));
2305 g_hash_table_insert(dhcp_client->code_value_hash,
2306 GINT_TO_POINTER((int) code), value_list);
2310 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2313 GDHCPClient *dhcp_client = user_data;
2314 struct sockaddr_in dst_addr = { 0 };
2315 struct dhcp_packet packet;
2316 struct dhcpv6_packet *packet6 = NULL;
2317 uint8_t *message_type = NULL, *client_id = NULL, *option,
2319 uint16_t option_len = 0, status = 0;
2322 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2323 uint16_t pkt_len = 0;
2327 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2328 dhcp_client->listener_watch = 0;
2329 #if defined TIZEN_EXT
2330 /* re-register event listener when socket failed */
2331 int retry_count = 0;
2333 while (retry_count++ < GIO_SOCKET_RETRY_COUNT && ret < 0)
2334 ret = switch_listening_mode(dhcp_client,
2336 #endif /* defined TIZEN_EXT */
2340 if (dhcp_client->listen_mode == L_NONE)
2345 dhcp_client->status_code = 0;
2347 if (dhcp_client->listen_mode == L2) {
2348 re = dhcp_recv_l2_packet(&packet,
2349 dhcp_client->listener_sockfd,
2352 } else if (dhcp_client->listen_mode == L3) {
2353 if (dhcp_client->type == G_DHCP_IPV6) {
2354 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2355 dhcp_client->listener_sockfd);
2360 xid = packet6->transaction_id[0] << 16 |
2361 packet6->transaction_id[1] << 8 |
2362 packet6->transaction_id[2];
2364 re = dhcp_recv_l3_packet(&packet,
2365 dhcp_client->listener_sockfd);
2368 } else if (dhcp_client->listen_mode == L_ARP) {
2369 ipv4ll_recv_arp_packet(dhcp_client);
2377 if (!check_package_owner(dhcp_client, pkt))
2380 if (dhcp_client->type == G_DHCP_IPV6) {
2385 client_id = dhcpv6_get_option(packet6, pkt_len,
2386 G_DHCPV6_CLIENTID, &option_len, &count);
2388 if (!client_id || count == 0 || option_len == 0 ||
2389 memcmp(dhcp_client->duid, client_id,
2390 dhcp_client->duid_len) != 0) {
2392 "client duid error, discarding msg %p/%d/%d",
2393 client_id, option_len, count);
2397 option = dhcpv6_get_option(packet6, pkt_len,
2398 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2399 if (option != 0 && option_len > 0) {
2400 status = option[0]<<8 | option[1];
2402 debug(dhcp_client, "error code %d", status);
2403 if (option_len > 2) {
2404 gchar *txt = g_strndup(
2405 (gchar *)&option[2],
2407 debug(dhcp_client, "error text: %s",
2412 dhcp_client->status_code = status;
2415 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2420 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2421 "(current state %d)", ntohl(xid), dhcp_client->state);
2423 switch (dhcp_client->state) {
2424 case INIT_SELECTING:
2425 if (*message_type != DHCPOFFER)
2428 remove_timeouts(dhcp_client);
2429 dhcp_client->timeout = 0;
2430 dhcp_client->retry_times = 0;
2432 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2433 dhcp_client->server_ip = get_be32(option);
2434 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2436 dhcp_client->state = REQUESTING;
2441 * If unicasting is not possible, the message MAY be
2442 * sent as an IP broadcast using an IP broadcast address
2443 * (preferably 0xffffffff) as the IP destination address
2444 * and the link-layer broadcast address as the link-layer
2445 * destination address.
2447 * For interoperability reasons, if the response is an IP
2448 * broadcast, let's reuse broadcast flag from DHCPDISCOVER
2449 * to which the server has responded. Some servers are picky
2452 dhcp_client->request_bcast =
2453 dst_addr.sin_addr.s_addr == INADDR_BROADCAST &&
2454 g_hash_table_contains(dhcp_client->secs_bcast_hash,
2455 GINT_TO_POINTER(packet.secs));
2457 debug(dhcp_client, "init ip %s secs %hu -> broadcast flag %s",
2458 inet_ntoa(dst_addr.sin_addr), packet.secs,
2459 dhcp_client->request_bcast ? "on" : "off");
2461 start_request(dhcp_client);
2465 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2466 dhcp_client->request_bcast = true;
2468 dhcp_client->request_bcast = false;
2470 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2471 inet_ntoa(dst_addr.sin_addr),
2472 dhcp_client->request_bcast ? "" : "not ");
2477 if (*message_type == DHCPACK) {
2478 dhcp_client->retry_times = 0;
2480 remove_timeouts(dhcp_client);
2482 dhcp_client->lease_seconds = get_lease(&packet);
2484 #if defined TIZEN_EXT
2485 dhcp_client->dhcp_lease_seconds = dhcp_client->lease_seconds;
2488 get_request(dhcp_client, &packet);
2490 switch_listening_mode(dhcp_client, L_NONE);
2492 g_free(dhcp_client->assigned_ip);
2493 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2495 if (dhcp_client->state == REBOOTING) {
2496 option = dhcp_get_option(&packet,
2498 dhcp_client->server_ip = get_be32(option);
2501 /* Address should be set up here */
2502 if (dhcp_client->lease_available_cb)
2503 dhcp_client->lease_available_cb(dhcp_client,
2504 dhcp_client->lease_available_data);
2506 start_bound(dhcp_client);
2507 } else if (*message_type == DHCPNAK) {
2508 dhcp_client->retry_times = 0;
2510 remove_timeouts(dhcp_client);
2512 #if defined TIZEN_EXT
2513 if (dhcp_client->init_reboot) {
2514 g_dhcp_client_set_address_known(dhcp_client, FALSE);
2515 dhcp_client->timeout = g_idle_add_full(
2517 restart_dhcp_timeout,
2524 dhcp_client->timeout = g_timeout_add_seconds_full(
2526 restart_dhcp_timeout,
2533 if (dhcp_client->type != G_DHCP_IPV6)
2536 if (packet6->message != DHCPV6_REPLY &&
2537 packet6->message != DHCPV6_ADVERTISE)
2541 server_id = dhcpv6_get_option(packet6, pkt_len,
2542 G_DHCPV6_SERVERID, &option_len, &count);
2543 if (!server_id || count != 1 || option_len == 0) {
2544 /* RFC 3315, 15.10 */
2546 "server duid error, discarding msg %p/%d/%d",
2547 server_id, option_len, count);
2550 dhcp_client->server_duid = g_try_malloc(option_len);
2551 if (!dhcp_client->server_duid)
2553 memcpy(dhcp_client->server_duid, server_id, option_len);
2554 dhcp_client->server_duid_len = option_len;
2556 if (packet6->message == DHCPV6_REPLY) {
2557 uint8_t *rapid_commit;
2560 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2561 G_DHCPV6_RAPID_COMMIT,
2562 &option_len, &count);
2563 if (!rapid_commit || option_len != 0 ||
2565 /* RFC 3315, 17.1.4 */
2569 switch_listening_mode(dhcp_client, L_NONE);
2571 if (dhcp_client->status_code == 0)
2572 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2573 &dhcp_client->status_code);
2575 if (packet6->message == DHCPV6_ADVERTISE) {
2576 if (dhcp_client->advertise_cb)
2577 dhcp_client->advertise_cb(dhcp_client,
2578 dhcp_client->advertise_data);
2582 if (dhcp_client->solicitation_cb) {
2584 * The dhcp_client might not be valid after the
2585 * callback call so just return immediately.
2587 dhcp_client->solicitation_cb(dhcp_client,
2588 dhcp_client->solicitation_data);
2593 if (dhcp_client->type != G_DHCP_IPV6)
2596 server_id = dhcpv6_get_option(packet6, pkt_len,
2597 G_DHCPV6_SERVERID, &option_len, &count);
2598 if (!dhcp_client->server_duid && server_id &&
2601 * If we do not have server duid yet, then get it now.
2602 * Prefix delegation renew support needs it.
2604 dhcp_client->server_duid = g_try_malloc(option_len);
2605 if (!dhcp_client->server_duid)
2607 memcpy(dhcp_client->server_duid, server_id, option_len);
2608 dhcp_client->server_duid_len = option_len;
2611 case INFORMATION_REQ:
2617 if (dhcp_client->type != G_DHCP_IPV6)
2620 if (packet6->message != DHCPV6_REPLY)
2625 server_id = dhcpv6_get_option(packet6, pkt_len,
2626 G_DHCPV6_SERVERID, &option_len, &count);
2627 if (!server_id || count != 1 || option_len == 0 ||
2628 (dhcp_client->server_duid_len > 0 &&
2629 memcmp(dhcp_client->server_duid, server_id,
2630 dhcp_client->server_duid_len) != 0)) {
2631 /* RFC 3315, 15.10 */
2633 "server duid error, discarding msg %p/%d/%d",
2634 server_id, option_len, count);
2638 switch_listening_mode(dhcp_client, L_NONE);
2640 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2641 &dhcp_client->status_code);
2643 if (dhcp_client->information_req_cb) {
2645 * The dhcp_client might not be valid after the
2646 * callback call so just return immediately.
2648 dhcp_client->information_req_cb(dhcp_client,
2649 dhcp_client->information_req_data);
2652 if (dhcp_client->request_cb) {
2653 dhcp_client->request_cb(dhcp_client,
2654 dhcp_client->request_data);
2657 if (dhcp_client->renew_cb) {
2658 dhcp_client->renew_cb(dhcp_client,
2659 dhcp_client->renew_data);
2662 if (dhcp_client->rebind_cb) {
2663 dhcp_client->rebind_cb(dhcp_client,
2664 dhcp_client->rebind_data);
2667 if (dhcp_client->release_cb) {
2668 dhcp_client->release_cb(dhcp_client,
2669 dhcp_client->release_data);
2672 if (dhcp_client->decline_cb) {
2673 dhcp_client->decline_cb(dhcp_client,
2674 dhcp_client->decline_data);
2677 if (dhcp_client->confirm_cb) {
2679 server_id = dhcpv6_get_option(packet6, pkt_len,
2680 G_DHCPV6_SERVERID, &option_len,
2682 if (!server_id || count != 1 ||
2684 /* RFC 3315, 15.10 */
2686 "confirm server duid error, "
2687 "discarding msg %p/%d/%d",
2688 server_id, option_len, count);
2691 dhcp_client->server_duid = g_try_malloc(option_len);
2692 if (!dhcp_client->server_duid)
2694 memcpy(dhcp_client->server_duid, server_id, option_len);
2695 dhcp_client->server_duid_len = option_len;
2697 dhcp_client->confirm_cb(dhcp_client,
2698 dhcp_client->confirm_data);
2706 debug(dhcp_client, "processed DHCP packet (new state %d)",
2707 dhcp_client->state);
2712 static gboolean discover_timeout(gpointer user_data)
2714 GDHCPClient *dhcp_client = user_data;
2716 dhcp_client->retry_times++;
2719 * We do not send the REQUESTED IP option if we are retrying because
2720 * if the server is non-authoritative it will ignore the request if the
2721 * option is present.
2723 g_dhcp_client_start(dhcp_client, NULL);
2728 static gboolean reboot_timeout(gpointer user_data)
2730 GDHCPClient *dhcp_client = user_data;
2731 dhcp_client->retry_times = 0;
2732 dhcp_client->requested_ip = 0;
2733 dhcp_client->state = INIT_SELECTING;
2735 * We do not send the REQUESTED IP option because the server didn't
2736 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2739 g_dhcp_client_start(dhcp_client, NULL);
2744 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2746 GDHCPClient *dhcp_client = dhcp_data;
2748 debug(dhcp_client, "back to MONITOR mode");
2750 dhcp_client->conflicts = 0;
2751 dhcp_client->state = IPV4LL_MONITOR;
2756 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2758 GDHCPClient *dhcp_client = dhcp_data;
2761 #if defined TIZEN_EXT
2766 debug(dhcp_client, "request timeout (retries %d)",
2767 dhcp_client->retry_times);
2769 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2770 dhcp_client->retry_times++;
2771 send_announce_packet(dhcp_client);
2775 ip = htonl(dhcp_client->requested_ip);
2776 debug(dhcp_client, "switching to monitor mode");
2777 dhcp_client->state = IPV4LL_MONITOR;
2778 dhcp_client->assigned_ip = get_ip(ip);
2780 if (dhcp_client->ipv4ll_available_cb)
2781 dhcp_client->ipv4ll_available_cb(dhcp_client,
2782 dhcp_client->ipv4ll_available_data);
2783 dhcp_client->conflicts = 0;
2784 dhcp_client->timeout = 0;
2789 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2792 GDHCPClient *dhcp_client = dhcp_data;
2794 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2795 dhcp_client->retry_times);
2797 if (dhcp_client->retry_times == PROBE_NUM) {
2798 dhcp_client->state = IPV4LL_ANNOUNCE;
2799 dhcp_client->retry_times = 0;
2801 dhcp_client->retry_times++;
2802 send_announce_packet(dhcp_client);
2805 dhcp_client->retry_times++;
2806 send_probe_packet(dhcp_client);
2811 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2816 ClientState oldstate = dhcp_client->state;
2818 #if defined TIZEN_EXT
2819 int discover_retry = 0;
2823 remove_timeouts(dhcp_client);
2825 if (dhcp_client->type == G_DHCP_IPV6) {
2826 if (dhcp_client->information_req_cb) {
2827 dhcp_client->state = INFORMATION_REQ;
2828 re = switch_listening_mode(dhcp_client, L3);
2830 switch_listening_mode(dhcp_client, L_NONE);
2831 dhcp_client->state = 0;
2834 send_information_req(dhcp_client);
2836 } else if (dhcp_client->solicitation_cb) {
2837 dhcp_client->state = SOLICITATION;
2838 re = switch_listening_mode(dhcp_client, L3);
2840 switch_listening_mode(dhcp_client, L_NONE);
2841 dhcp_client->state = 0;
2844 send_solicitation(dhcp_client);
2846 } else if (dhcp_client->request_cb) {
2847 dhcp_client->state = REQUEST;
2848 re = switch_listening_mode(dhcp_client, L3);
2850 switch_listening_mode(dhcp_client, L_NONE);
2851 dhcp_client->state = 0;
2854 send_dhcpv6_request(dhcp_client);
2856 } else if (dhcp_client->confirm_cb) {
2857 dhcp_client->state = CONFIRM;
2858 re = switch_listening_mode(dhcp_client, L3);
2860 switch_listening_mode(dhcp_client, L_NONE);
2861 dhcp_client->state = 0;
2864 send_dhcpv6_confirm(dhcp_client);
2866 } else if (dhcp_client->renew_cb) {
2867 dhcp_client->state = RENEW;
2868 re = switch_listening_mode(dhcp_client, L3);
2870 switch_listening_mode(dhcp_client, L_NONE);
2871 dhcp_client->state = 0;
2874 send_dhcpv6_renew(dhcp_client);
2876 } else if (dhcp_client->rebind_cb) {
2877 dhcp_client->state = REBIND;
2878 re = switch_listening_mode(dhcp_client, L3);
2880 switch_listening_mode(dhcp_client, L_NONE);
2881 dhcp_client->state = 0;
2884 send_dhcpv6_rebind(dhcp_client);
2886 } else if (dhcp_client->release_cb) {
2887 dhcp_client->state = RENEW;
2888 re = switch_listening_mode(dhcp_client, L3);
2890 switch_listening_mode(dhcp_client, L_NONE);
2891 dhcp_client->state = 0;
2894 send_dhcpv6_release(dhcp_client);
2895 } else if (dhcp_client->decline_cb) {
2896 dhcp_client->state = DECLINE;
2897 re = switch_listening_mode(dhcp_client, L3);
2899 switch_listening_mode(dhcp_client, L_NONE);
2900 dhcp_client->state = 0;
2903 send_dhcpv6_decline(dhcp_client);
2909 #if defined TIZEN_EXT
2910 if (g_ascii_strncasecmp(dhcp_client->interface, "wlan", 4)
2912 discover_retry = DISCOVER_RETRIES_WIFI;
2913 timeout = DISCOVER_TIMEOUT_WIFI;
2915 discover_retry = DISCOVER_RETRIES;
2916 timeout = DISCOVER_TIMEOUT;
2919 debug(dhcp_client, "[DHCPC] Discover retry/total : [%d]/[%d] timeout [%d]",
2920 dhcp_client->retry_times, discover_retry, timeout);
2924 if (dhcp_client->type == G_DHCP_IPV4LL) {
2925 dhcp_client->state = INIT_SELECTING;
2926 ipv4ll_start(dhcp_client);
2930 #if defined TIZEN_EXT
2931 if (dhcp_client->retry_times == discover_retry) {
2933 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2935 if (dhcp_client->no_lease_cb)
2936 dhcp_client->no_lease_cb(dhcp_client,
2937 dhcp_client->no_lease_data);
2938 dhcp_client->retry_times = 0;
2942 if (dhcp_client->retry_times == 0) {
2943 g_free(dhcp_client->assigned_ip);
2944 dhcp_client->assigned_ip = NULL;
2946 dhcp_client->state = INIT_SELECTING;
2947 re = switch_listening_mode(dhcp_client, L2);
2951 __connman_util_get_random(&rand);
2952 dhcp_client->xid = rand;
2953 dhcp_client->start = time(NULL);
2954 g_hash_table_remove_all(dhcp_client->secs_bcast_hash);
2957 if (!last_address || oldstate == DECLINED) {
2960 addr = ntohl(inet_addr(last_address));
2961 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2964 } else if (dhcp_client->last_address != last_address) {
2965 g_free(dhcp_client->last_address);
2966 dhcp_client->last_address = g_strdup(last_address);
2970 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2971 debug(dhcp_client, "DHCP client start with state init_reboot");
2972 dhcp_client->requested_ip = addr;
2973 dhcp_client->state = REBOOTING;
2974 send_request(dhcp_client);
2976 dhcp_client->timeout = g_timeout_add_seconds_full(
2978 #if defined TIZEN_EXT
2988 send_discover(dhcp_client, addr);
2990 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2991 #if defined TIZEN_EXT
3002 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
3004 switch_listening_mode(dhcp_client, L_NONE);
3006 if (dhcp_client->state == BOUND ||
3007 dhcp_client->state == RENEWING ||
3008 dhcp_client->state == REBINDING)
3009 send_release(dhcp_client, dhcp_client->server_ip,
3010 dhcp_client->requested_ip);
3012 remove_timeouts(dhcp_client);
3014 if (dhcp_client->listener_watch > 0) {
3015 g_source_remove(dhcp_client->listener_watch);
3016 dhcp_client->listener_watch = 0;
3019 dhcp_client->retry_times = 0;
3020 dhcp_client->ack_retry_times = 0;
3022 dhcp_client->requested_ip = 0;
3023 dhcp_client->state = RELEASED;
3024 dhcp_client->lease_seconds = 0;
3025 dhcp_client->request_bcast = false;
3028 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
3029 unsigned char option_code)
3031 return g_hash_table_lookup(dhcp_client->code_value_hash,
3032 GINT_TO_POINTER((int) option_code));
3035 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
3036 GDHCPClientEvent event,
3037 GDHCPClientEventFunc func,
3041 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
3042 dhcp_client->lease_available_cb = func;
3043 dhcp_client->lease_available_data = data;
3045 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
3046 if (dhcp_client->type == G_DHCP_IPV6)
3048 dhcp_client->ipv4ll_available_cb = func;
3049 dhcp_client->ipv4ll_available_data = data;
3051 case G_DHCP_CLIENT_EVENT_NO_LEASE:
3052 dhcp_client->no_lease_cb = func;
3053 dhcp_client->no_lease_data = data;
3055 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
3056 dhcp_client->lease_lost_cb = func;
3057 dhcp_client->lease_lost_data = data;
3059 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
3060 if (dhcp_client->type == G_DHCP_IPV6)
3062 dhcp_client->ipv4ll_lost_cb = func;
3063 dhcp_client->ipv4ll_lost_data = data;
3065 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
3066 dhcp_client->address_conflict_cb = func;
3067 dhcp_client->address_conflict_data = data;
3069 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
3070 if (dhcp_client->type != G_DHCP_IPV6)
3072 dhcp_client->information_req_cb = func;
3073 dhcp_client->information_req_data = data;
3075 case G_DHCP_CLIENT_EVENT_SOLICITATION:
3076 if (dhcp_client->type != G_DHCP_IPV6)
3078 dhcp_client->solicitation_cb = func;
3079 dhcp_client->solicitation_data = data;
3081 case G_DHCP_CLIENT_EVENT_ADVERTISE:
3082 if (dhcp_client->type != G_DHCP_IPV6)
3084 dhcp_client->advertise_cb = func;
3085 dhcp_client->advertise_data = data;
3087 case G_DHCP_CLIENT_EVENT_REQUEST:
3088 if (dhcp_client->type != G_DHCP_IPV6)
3090 dhcp_client->request_cb = func;
3091 dhcp_client->request_data = data;
3093 case G_DHCP_CLIENT_EVENT_RENEW:
3094 if (dhcp_client->type != G_DHCP_IPV6)
3096 dhcp_client->renew_cb = func;
3097 dhcp_client->renew_data = data;
3099 case G_DHCP_CLIENT_EVENT_REBIND:
3100 if (dhcp_client->type != G_DHCP_IPV6)
3102 dhcp_client->rebind_cb = func;
3103 dhcp_client->rebind_data = data;
3105 case G_DHCP_CLIENT_EVENT_RELEASE:
3106 if (dhcp_client->type != G_DHCP_IPV6)
3108 dhcp_client->release_cb = func;
3109 dhcp_client->release_data = data;
3111 case G_DHCP_CLIENT_EVENT_CONFIRM:
3112 if (dhcp_client->type != G_DHCP_IPV6)
3114 dhcp_client->confirm_cb = func;
3115 dhcp_client->confirm_data = data;
3117 case G_DHCP_CLIENT_EVENT_DECLINE:
3118 if (dhcp_client->type != G_DHCP_IPV6)
3120 dhcp_client->decline_cb = func;
3121 dhcp_client->decline_data = data;
3126 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
3128 return dhcp_client->ifindex;
3131 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
3136 #if defined TIZEN_EXT
3137 return get_ip(htonl(dhcp_client->server_ip));
3139 return get_ip(dhcp_client->server_ip);
3143 #if defined TIZEN_EXT
3144 int g_dhcp_client_get_dhcp_lease_duration(GDHCPClient *dhcp_client)
3146 return dhcp_client->dhcp_lease_seconds;
3150 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
3152 return g_strdup(dhcp_client->assigned_ip);
3155 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
3157 GList *option = NULL;
3159 if (dhcp_client->type == G_DHCP_IPV6)
3162 switch (dhcp_client->state) {
3164 case IPV4LL_MONITOR:
3165 return g_strdup("255.255.0.0");
3169 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3171 return g_strdup(option->data);
3172 case INIT_SELECTING:
3178 case IPV4LL_ANNOUNCE:
3179 case INFORMATION_REQ:
3192 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3193 unsigned int option_code)
3195 if (!g_list_find(dhcp_client->request_list,
3196 GINT_TO_POINTER((int)option_code)))
3197 dhcp_client->request_list = g_list_prepend(
3198 dhcp_client->request_list,
3199 (GINT_TO_POINTER((int) option_code)));
3201 return G_DHCP_CLIENT_ERROR_NONE;
3204 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3206 g_list_free(dhcp_client->request_list);
3207 dhcp_client->request_list = NULL;
3210 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3212 g_hash_table_remove_all(dhcp_client->send_value_hash);
3215 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3219 storage = g_try_malloc(size + OPT_DATA);
3223 storage[OPT_CODE] = code;
3224 storage[OPT_LEN] = size;
3225 memcpy(&storage[OPT_DATA], data, size);
3230 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3233 return alloc_dhcp_option(code, data, MIN(size, 255));
3236 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3238 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3241 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3243 const unsigned maclen = 6;
3244 const unsigned idlen = maclen + 1;
3245 const uint8_t option_code = G_DHCP_CLIENT_ID;
3246 uint8_t idbuf[idlen];
3247 uint8_t *data_option;
3249 idbuf[0] = ARPHRD_ETHER;
3251 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3253 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3255 return G_DHCP_CLIENT_ERROR_NOMEM;
3257 g_hash_table_insert(dhcp_client->send_value_hash,
3258 GINT_TO_POINTER((int) option_code), data_option);
3260 return G_DHCP_CLIENT_ERROR_NONE;
3263 /* Now only support send hostname and vendor class ID */
3264 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3265 unsigned char option_code, const char *option_value)
3267 uint8_t *binary_option;
3269 if ((option_code == G_DHCP_HOST_NAME ||
3270 option_code == G_DHCP_VENDOR_CLASS_ID) && option_value) {
3271 binary_option = alloc_dhcp_string_option(option_code,
3274 return G_DHCP_CLIENT_ERROR_NOMEM;
3276 g_hash_table_insert(dhcp_client->send_value_hash,
3277 GINT_TO_POINTER((int) option_code), binary_option);
3280 return G_DHCP_CLIENT_ERROR_NONE;
3283 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3288 storage = g_malloc(2 + 2 + len);
3292 storage[0] = code >> 8;
3293 storage[1] = code & 0xff;
3294 storage[2] = len >> 8;
3295 storage[3] = len & 0xff;
3296 memcpy(storage + 2 + 2, option, len);
3301 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3303 return g_hash_table_remove(dhcp_client->send_value_hash,
3304 GINT_TO_POINTER((int)code));
3307 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3308 uint16_t option_code,
3309 uint8_t *option_value,
3310 uint16_t option_len)
3313 uint8_t *binary_option;
3315 debug(dhcp_client, "setting option %d to %p len %d",
3316 option_code, option_value, option_len);
3318 binary_option = alloc_dhcpv6_option(option_code, option_value,
3321 g_hash_table_insert(dhcp_client->send_value_hash,
3322 GINT_TO_POINTER((int) option_code),
3327 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3329 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3332 dhcp_client->last_request = time(NULL);
3335 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3337 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3340 return dhcp_client->status_code;
3343 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3348 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3353 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3358 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3361 g_dhcp_client_stop(dhcp_client);
3363 g_free(dhcp_client->interface);
3364 g_free(dhcp_client->assigned_ip);
3365 g_free(dhcp_client->last_address);
3366 g_free(dhcp_client->duid);
3367 g_free(dhcp_client->server_duid);
3369 g_list_free(dhcp_client->request_list);
3370 g_list_free(dhcp_client->require_list);
3372 g_hash_table_destroy(dhcp_client->code_value_hash);
3373 g_hash_table_destroy(dhcp_client->send_value_hash);
3374 g_hash_table_destroy(dhcp_client->secs_bcast_hash);
3376 g_free(dhcp_client);
3377 #if defined TIZEN_EXT
3382 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3383 GDHCPDebugFunc func, gpointer user_data)
3388 dhcp_client->debug_func = func;
3389 dhcp_client->debug_data = user_data;
3392 static GDHCPIAPrefix *copy_prefix(gpointer data)
3394 GDHCPIAPrefix *copy, *prefix = data;
3396 copy = g_try_new(GDHCPIAPrefix, 1);
3400 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3405 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3407 GSList *copy = NULL;
3410 for (list = prefixes; list; list = list->next)
3411 copy = g_slist_prepend(copy, copy_prefix(list->data));
3416 #if defined TIZEN_EXT
3417 void g_dhcp_client_set_address_known(GDHCPClient *dhcp_client, gboolean known)
3419 /* DHCPREQUEST during INIT-REBOOT state (rfc2131)
3420 * 4.4.3 Initialization with known network address
3421 * 4.3.2 DHCPREQUEST generated during INIT-REBOOT state
3423 debug(dhcp_client, "known network address (%d)", known);
3425 if (dhcp_client->init_reboot == known)
3428 dhcp_client->init_reboot = known;