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);
567 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
568 INADDR_BROADCAST, SERVER_PORT,
569 MAC_BCAST_ADDR, dhcp_client->ifindex,
570 dhcp_client->request_bcast);
573 static int send_release(GDHCPClient *dhcp_client,
574 uint32_t server, uint32_t ciaddr)
576 struct dhcp_packet packet;
579 debug(dhcp_client, "sending DHCP release request");
581 init_packet(dhcp_client, &packet, DHCPRELEASE);
582 __connman_util_get_random(&rand);
584 packet.ciaddr = htonl(ciaddr);
586 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
588 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
589 server, SERVER_PORT);
592 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
593 static int switch_listening_mode(GDHCPClient *dhcp_client,
594 ListenMode listen_mode);
596 static gboolean send_probe_packet(gpointer dhcp_data)
598 GDHCPClient *dhcp_client;
601 dhcp_client = dhcp_data;
602 /* if requested_ip is not valid, pick a new address*/
603 if (dhcp_client->requested_ip == 0) {
604 debug(dhcp_client, "pick a new random address");
605 dhcp_client->requested_ip = arp_random_ip();
608 debug(dhcp_client, "sending IPV4LL probe request");
610 if (dhcp_client->retry_times == 1) {
611 dhcp_client->state = IPV4LL_PROBE;
612 switch_listening_mode(dhcp_client, L_ARP);
614 arp_send_packet(dhcp_client->mac_address, 0,
615 dhcp_client->requested_ip, dhcp_client->ifindex);
617 if (dhcp_client->retry_times < PROBE_NUM) {
618 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
619 timeout = __connman_util_random_delay_ms(PROBE_MAX-PROBE_MIN);
620 timeout += PROBE_MIN*1000;
622 timeout = (ANNOUNCE_WAIT * 1000);
624 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
626 ipv4ll_probe_timeout,
632 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
633 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
635 static gboolean send_announce_packet(gpointer dhcp_data)
637 GDHCPClient *dhcp_client;
639 dhcp_client = dhcp_data;
641 debug(dhcp_client, "sending IPV4LL announce request");
643 arp_send_packet(dhcp_client->mac_address,
644 dhcp_client->requested_ip,
645 dhcp_client->requested_ip,
646 dhcp_client->ifindex);
648 remove_timeouts(dhcp_client);
650 if (dhcp_client->state == IPV4LL_DEFEND) {
651 dhcp_client->timeout =
652 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
654 ipv4ll_defend_timeout,
659 dhcp_client->timeout =
660 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
662 ipv4ll_announce_timeout,
668 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
673 dhcp_client->retransmit = true;
676 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
681 dhcp_client->retransmit = false;
684 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
685 unsigned char **duid, int *duid_len)
690 case G_DHCPV6_DUID_LLT:
691 *duid_len = 2 + 2 + 4 + ETH_ALEN;
692 *duid = g_try_malloc(*duid_len);
698 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
701 duid_time = time(NULL) - DUID_TIME_EPOCH;
702 (*duid)[4] = duid_time >> 24;
703 (*duid)[5] = duid_time >> 16;
704 (*duid)[6] = duid_time >> 8;
705 (*duid)[7] = duid_time & 0xff;
707 case G_DHCPV6_DUID_EN:
709 case G_DHCPV6_DUID_LL:
710 *duid_len = 2 + 2 + ETH_ALEN;
711 *duid = g_try_malloc(*duid_len);
717 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2]);
726 static gchar *convert_to_hex(unsigned char *buf, int len)
728 gchar *ret = g_try_malloc(len * 2 + 1);
731 for (i = 0; ret && i < len; i++)
732 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
737 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
740 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
743 g_free(dhcp_client->duid);
745 dhcp_client->duid = duid;
746 dhcp_client->duid_len = duid_len;
748 if (dhcp_client->debug_func) {
749 gchar *hex = convert_to_hex(duid, duid_len);
750 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
757 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
758 uint32_t *T2, GSList *prefixes)
760 uint8_t options[1452];
761 unsigned int max_buf = sizeof(options);
762 int len, count = g_slist_length(prefixes);
764 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
767 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
769 memset(options, 0, sizeof(options));
771 options[0] = dhcp_client->iaid >> 24;
772 options[1] = dhcp_client->iaid >> 16;
773 options[2] = dhcp_client->iaid >> 8;
774 options[3] = dhcp_client->iaid;
777 uint32_t t = htonl(*T1);
778 memcpy(&options[4], &t, 4);
782 uint32_t t = htonl(*T2);
783 memcpy(&options[8], &t, 4);
791 for (list = prefixes; list; list = list->next) {
792 GDHCPIAPrefix *prefix = list->data;
793 uint8_t sub_option[4+4+1+16];
795 if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
797 "Too long dhcpv6 message "
798 "when writing IA prefix option");
802 memset(&sub_option, 0, sizeof(sub_option));
804 /* preferred and validity time are left zero */
806 sub_option[8] = prefix->prefixlen;
807 memcpy(&sub_option[9], &prefix->prefix, 16);
809 copy_option(&options[len], G_DHCPV6_IA_PREFIX,
810 sizeof(sub_option), sub_option);
811 len += 2 + 2 + sizeof(sub_option);
815 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
821 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
823 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
826 return dhcp_client->iaid;
829 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
831 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
834 dhcp_client->iaid = iaid;
837 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
842 __connman_inet_get_interface_mac_address(index, buf);
844 memcpy(iaid, &buf[2], 4);
845 dhcp_client->iaid = iaid[0] << 24 |
846 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
849 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
850 uint32_t *T1, uint32_t *T2,
854 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
858 *T1 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
862 *T2 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
866 *started = dhcp_client->last_request;
869 *expire = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
870 dhcp_client->last_request + dhcp_client->expire;
875 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
879 buf[1] = G_DHCPV6_IAADDR;
882 memcpy(&buf[4], &dhcp_client->ia_na, 16);
883 memset(&buf[20], 0, 4); /* preferred */
884 memset(&buf[24], 0, 4); /* valid */
888 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
891 struct in6_addr addr;
893 if (inet_pton(AF_INET6, address, &addr) != 1)
897 buf[1] = G_DHCPV6_IAADDR;
900 memcpy(&buf[4], &addr, 16);
901 memset(&buf[20], 0, 4); /* preferred */
902 memset(&buf[24], 0, 4); /* valid */
906 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
910 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
912 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
922 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
923 int code, uint32_t *T1, uint32_t *T2,
924 bool add_iaaddr, const char *ia_na)
926 if (code == G_DHCPV6_IA_TA) {
927 uint8_t ia_options[4];
929 put_iaid(dhcp_client, index, ia_options);
931 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
932 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
933 ia_options, sizeof(ia_options));
935 } else if (code == G_DHCPV6_IA_NA) {
936 struct in6_addr addr;
938 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
941 * If caller has specified the IPv6 address it wishes to
942 * to use (ia_na != NULL and address is valid), then send
943 * the address to server.
944 * If caller did not specify the address (ia_na == NULL) and
945 * if the current address is not set, then we should not send
946 * the address sub-option.
948 if (add_iaaddr && ((!ia_na &&
949 !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
951 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
952 #define IAADDR_LEN (16+4+4)
953 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
956 memcpy(&dhcp_client->ia_na, &addr,
957 sizeof(struct in6_addr));
959 put_iaid(dhcp_client, index, ia_options);
962 ia_options[4] = *T1 >> 24;
963 ia_options[5] = *T1 >> 16;
964 ia_options[6] = *T1 >> 8;
967 memset(&ia_options[4], 0x00, 4);
970 ia_options[8] = *T2 >> 24;
971 ia_options[9] = *T2 >> 16;
972 ia_options[10] = *T2 >> 8;
973 ia_options[11] = *T2;
975 memset(&ia_options[8], 0x00, 4);
977 create_iaaddr(dhcp_client, &ia_options[12],
980 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
981 ia_options, sizeof(ia_options));
983 uint8_t ia_options[4+4+4];
985 put_iaid(dhcp_client, index, ia_options);
987 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
988 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
990 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
991 ia_options, sizeof(ia_options));
1000 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
1001 int code, uint32_t *T1, uint32_t *T2,
1005 uint8_t *ia_options, *pos;
1006 int len, count, total_len;
1008 count = g_slist_length(addresses);
1012 g_dhcp_client_set_request(dhcp_client, code);
1014 if (code == G_DHCPV6_IA_TA)
1016 else if (code == G_DHCPV6_IA_NA)
1017 len = 4 + 4 + 4; /* IAID + T1 + T2 */
1021 total_len = len + count * (2 + 2 + 16 + 4 + 4);
1022 ia_options = g_try_malloc0(total_len);
1026 put_iaid(dhcp_client, index, ia_options);
1028 pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
1030 for (list = addresses; list; list = list->next) {
1031 pos = append_iaaddr(dhcp_client, pos, list->data);
1036 if (code == G_DHCPV6_IA_NA) {
1038 ia_options[4] = *T1 >> 24;
1039 ia_options[5] = *T1 >> 16;
1040 ia_options[6] = *T1 >> 8;
1041 ia_options[7] = *T1;
1043 memset(&ia_options[4], 0x00, 4);
1046 ia_options[8] = *T2 >> 24;
1047 ia_options[9] = *T2 >> 16;
1048 ia_options[10] = *T2 >> 8;
1049 ia_options[11] = *T2;
1051 memset(&ia_options[8], 0x00, 4);
1054 g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1061 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1064 int i, j, len = sizeof(uint16_t) * args;
1067 values = g_try_malloc(len);
1072 for (i = 0, j = 0; i < args; i++) {
1073 uint16_t value = va_arg(va, int);
1074 values[j++] = value >> 8;
1075 values[j++] = value & 0xff;
1079 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1086 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1088 struct dhcpv6_packet *packet;
1089 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1093 memset(buf, 0, sizeof(buf));
1094 packet = (struct dhcpv6_packet *)&buf[0];
1095 ptr = buf + sizeof(struct dhcpv6_packet);
1097 init_packet(dhcp_client, packet, type);
1099 if (!dhcp_client->retransmit) {
1100 dhcp_client->xid = packet->transaction_id[0] << 16 |
1101 packet->transaction_id[1] << 8 |
1102 packet->transaction_id[2];
1103 gettimeofday(&dhcp_client->start_time, NULL);
1105 packet->transaction_id[0] = dhcp_client->xid >> 16;
1106 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1107 packet->transaction_id[2] = dhcp_client->xid;
1110 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1112 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1115 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1117 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1119 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1121 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1123 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1127 static int send_solicitation(GDHCPClient *dhcp_client)
1129 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1132 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1134 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1137 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1139 return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1142 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1144 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1147 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1149 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1152 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1154 return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1157 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1159 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1162 static int send_information_req(GDHCPClient *dhcp_client)
1164 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1168 static void remove_value(gpointer data, gpointer user_data)
1174 static void remove_option_value(gpointer data)
1176 GList *option_value = data;
1178 g_list_foreach(option_value, remove_value, NULL);
1179 g_list_free(option_value);
1182 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1183 int ifindex, GDHCPClientError *error)
1185 GDHCPClient *dhcp_client;
1188 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1192 dhcp_client = g_try_new0(GDHCPClient, 1);
1194 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1198 dhcp_client->interface = get_interface_name(ifindex);
1199 if (!dhcp_client->interface) {
1200 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1204 if (!interface_is_up(ifindex)) {
1205 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1209 __connman_inet_get_interface_mac_address(ifindex, dhcp_client->mac_address);
1211 dhcp_client->listener_sockfd = -1;
1212 dhcp_client->listen_mode = L_NONE;
1213 dhcp_client->ref_count = 1;
1214 dhcp_client->type = type;
1215 dhcp_client->ifindex = ifindex;
1216 dhcp_client->lease_available_cb = NULL;
1217 dhcp_client->ipv4ll_available_cb = NULL;
1218 dhcp_client->no_lease_cb = NULL;
1219 dhcp_client->lease_lost_cb = NULL;
1220 dhcp_client->ipv4ll_lost_cb = NULL;
1221 dhcp_client->address_conflict_cb = NULL;
1222 dhcp_client->listener_watch = 0;
1223 dhcp_client->retry_times = 0;
1224 dhcp_client->ack_retry_times = 0;
1225 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1226 g_direct_equal, NULL, remove_option_value);
1227 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1228 g_direct_equal, NULL, g_free);
1229 dhcp_client->secs_bcast_hash = g_hash_table_new(g_direct_hash,
1231 dhcp_client->request_list = NULL;
1232 dhcp_client->require_list = NULL;
1233 dhcp_client->duid = NULL;
1234 dhcp_client->duid_len = 0;
1235 dhcp_client->last_request = time(NULL);
1236 dhcp_client->expire = 0;
1237 dhcp_client->request_bcast = false;
1239 *error = G_DHCP_CLIENT_ERROR_NONE;
1244 g_free(dhcp_client->interface);
1245 g_free(dhcp_client);
1249 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1251 static int dhcp_l2_socket(int ifindex)
1254 struct sockaddr_ll sock;
1259 * I've selected not to see LL header, so BPF doesn't see it, too.
1260 * The filter may also pass non-IP and non-ARP packets, but we do
1261 * a more complete check when receiving the message in userspace.
1263 * and filter shamelessly stolen from:
1265 * http://www.flamewarmaster.de/software/dhcpclient/
1267 * There are a few other interesting ideas on that page (look under
1268 * "Motivation"). Use of netlink events is most interesting. Think
1269 * of various network servers listening for events and reconfiguring.
1270 * That would obsolete sending HUP signals and/or make use of restarts.
1272 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1275 * TODO: make conditional?
1277 static const struct sock_filter filter_instr[] = {
1279 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1280 /* L5, L1, is UDP? */
1281 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1282 /* ugly check for arp on ethernet-like and IPv4 */
1283 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1284 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1285 /* skip IP header */
1286 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1287 /* check udp source and destination ports */
1288 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1290 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1292 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1293 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1296 static const struct sock_fprog filter_prog = {
1297 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1298 /* casting const away: */
1299 .filter = (struct sock_filter *) filter_instr,
1302 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1306 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1307 /* Use only if standard ports are in use */
1308 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1309 sizeof(filter_prog));
1311 memset(&sock, 0, sizeof(sock));
1312 sock.sll_family = AF_PACKET;
1313 sock.sll_protocol = htons(ETH_P_IP);
1314 sock.sll_ifindex = ifindex;
1316 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1325 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1327 if (packet->ip.protocol != IPPROTO_UDP)
1330 if (packet->ip.version != IPVERSION)
1333 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1336 if (packet->udp.dest != htons(CLIENT_PORT))
1339 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1345 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1346 struct sockaddr_in *dst_addr)
1349 struct ip_udp_dhcp_packet packet;
1352 memset(&packet, 0, sizeof(packet));
1354 bytes = read(fd, &packet, sizeof(packet));
1358 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1361 if (bytes < ntohs(packet.ip.tot_len))
1362 /* packet is bigger than sizeof(packet), we did partial read */
1365 /* ignore any extra garbage bytes */
1366 bytes = ntohs(packet.ip.tot_len);
1368 if (!sanity_check(&packet, bytes))
1371 check = packet.ip.check;
1372 packet.ip.check = 0;
1373 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1376 /* verify UDP checksum. IP header has to be modified for this */
1377 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1378 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1379 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1380 check = packet.udp.check;
1381 packet.udp.check = 0;
1382 if (check && check != dhcp_checksum(&packet, bytes))
1385 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1386 sizeof(packet.udp)));
1388 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1391 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1393 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1396 static void ipv4ll_start(GDHCPClient *dhcp_client)
1400 remove_timeouts(dhcp_client);
1402 switch_listening_mode(dhcp_client, L_NONE);
1403 dhcp_client->retry_times = 0;
1404 dhcp_client->requested_ip = 0;
1406 dhcp_client->requested_ip = arp_random_ip();
1408 /*first wait a random delay to avoid storm of arp request on boot*/
1409 timeout = __connman_util_random_delay_ms(PROBE_WAIT);
1411 dhcp_client->retry_times++;
1412 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1419 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1422 switch_listening_mode(dhcp_client, L_NONE);
1424 remove_timeouts(dhcp_client);
1426 if (dhcp_client->listener_watch > 0) {
1427 g_source_remove(dhcp_client->listener_watch);
1428 dhcp_client->listener_watch = 0;
1431 dhcp_client->state = IPV4LL_PROBE;
1432 dhcp_client->retry_times = 0;
1433 dhcp_client->requested_ip = 0;
1435 g_free(dhcp_client->assigned_ip);
1436 dhcp_client->assigned_ip = NULL;
1439 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1442 struct ether_arp arp;
1443 uint32_t ip_requested;
1444 int source_conflict;
1445 int target_conflict;
1448 memset(&arp, 0, sizeof(arp));
1449 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1453 if (arp.arp_op != htons(ARPOP_REPLY) &&
1454 arp.arp_op != htons(ARPOP_REQUEST))
1457 if (memcmp(arp.arp_sha, dhcp_client->mac_address, ETH_ALEN) == 0)
1460 ip_requested = htonl(dhcp_client->requested_ip);
1461 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1462 sizeof(ip_requested));
1464 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1465 sizeof(ip_requested));
1467 if (!source_conflict && !target_conflict)
1470 dhcp_client->conflicts++;
1472 debug(dhcp_client, "IPV4LL conflict detected");
1474 if (dhcp_client->state == IPV4LL_MONITOR) {
1475 if (!source_conflict)
1477 dhcp_client->state = IPV4LL_DEFEND;
1478 debug(dhcp_client, "DEFEND mode conflicts : %d",
1479 dhcp_client->conflicts);
1480 /*Try to defend with a single announce*/
1481 send_announce_packet(dhcp_client);
1485 if (dhcp_client->state == IPV4LL_DEFEND) {
1486 if (!source_conflict)
1488 else if (dhcp_client->ipv4ll_lost_cb)
1489 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1490 dhcp_client->ipv4ll_lost_data);
1493 ipv4ll_stop(dhcp_client);
1495 /* If we got a lot of conflicts, RFC3927 states that we have
1496 * to wait RATE_LIMIT_INTERVAL before retrying,
1498 if (dhcp_client->conflicts < MAX_CONFLICTS)
1499 timeout_ms = __connman_util_random_delay_ms(PROBE_WAIT);
1501 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
1502 dhcp_client->retry_times++;
1503 dhcp_client->timeout =
1504 g_timeout_add_full(G_PRIORITY_HIGH,
1513 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1515 if (dhcp_client->type == G_DHCP_IPV6) {
1516 struct dhcpv6_packet *packet6 = pkt;
1522 xid = packet6->transaction_id[0] << 16 |
1523 packet6->transaction_id[1] << 8 |
1524 packet6->transaction_id[2];
1526 if (xid != dhcp_client->xid)
1529 struct dhcp_packet *packet = pkt;
1531 if (packet->xid != dhcp_client->xid)
1534 if (packet->hlen != 6)
1537 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1544 static void start_request(GDHCPClient *dhcp_client);
1546 static gboolean request_timeout(gpointer user_data)
1548 GDHCPClient *dhcp_client = user_data;
1550 #if defined TIZEN_EXT
1551 if (dhcp_client->init_reboot) {
1552 debug(dhcp_client, "DHCPREQUEST of INIT-REBOOT has failed");
1554 /* Start DHCPDISCOVERY when DHCPREQUEST of INIT-REBOOT has failed */
1555 g_dhcp_client_set_address_known(dhcp_client, FALSE);
1557 dhcp_client->retry_times = 0;
1558 dhcp_client->requested_ip = 0;
1560 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1565 debug(dhcp_client, "request timeout (retries %d)",
1566 dhcp_client->retry_times);
1568 dhcp_client->retry_times++;
1570 start_request(dhcp_client);
1575 static void listener_watch_destroy(gpointer user_data)
1577 GDHCPClient *dhcp_client = user_data;
1578 g_dhcp_client_unref(dhcp_client);
1581 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1582 gpointer user_data);
1584 static int switch_listening_mode(GDHCPClient *dhcp_client,
1585 ListenMode listen_mode)
1587 GIOChannel *listener_channel;
1588 int listener_sockfd;
1590 if (dhcp_client->listen_mode == listen_mode)
1593 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1594 dhcp_client->listen_mode, listen_mode);
1596 if (dhcp_client->listen_mode != L_NONE) {
1597 if (dhcp_client->listener_watch > 0)
1598 g_source_remove(dhcp_client->listener_watch);
1599 dhcp_client->listen_mode = L_NONE;
1600 dhcp_client->listener_sockfd = -1;
1601 dhcp_client->listener_watch = 0;
1604 if (listen_mode == L_NONE)
1607 if (listen_mode == L2)
1608 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1609 else if (listen_mode == L3) {
1610 if (dhcp_client->type == G_DHCP_IPV6)
1611 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1612 dhcp_client->interface,
1615 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1616 dhcp_client->interface,
1618 } else if (listen_mode == L_ARP)
1619 listener_sockfd = arp_socket(dhcp_client->ifindex);
1623 if (listener_sockfd < 0)
1626 listener_channel = g_io_channel_unix_new(listener_sockfd);
1627 if (!listener_channel) {
1628 /* Failed to create listener channel */
1629 close(listener_sockfd);
1633 dhcp_client->listen_mode = listen_mode;
1634 dhcp_client->listener_sockfd = listener_sockfd;
1636 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1637 dhcp_client->listener_watch =
1638 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1639 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1640 listener_event, g_dhcp_client_ref(dhcp_client),
1641 listener_watch_destroy);
1642 g_io_channel_unref(listener_channel);
1647 static void start_request(GDHCPClient *dhcp_client)
1649 debug(dhcp_client, "start request (retries %d)",
1650 dhcp_client->retry_times);
1652 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1653 if (dhcp_client->no_lease_cb)
1654 dhcp_client->no_lease_cb(dhcp_client,
1655 dhcp_client->no_lease_data);
1659 if (dhcp_client->retry_times == 0) {
1660 dhcp_client->state = REQUESTING;
1661 switch_listening_mode(dhcp_client, L2);
1664 send_request(dhcp_client);
1666 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1673 static uint32_t get_lease(struct dhcp_packet *packet)
1676 uint32_t lease_seconds;
1678 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1682 lease_seconds = get_be32(option);
1684 if (lease_seconds < 10)
1687 return lease_seconds;
1690 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1692 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1694 remove_timeouts(dhcp_client);
1696 dhcp_client->retry_times = retry_times;
1697 dhcp_client->requested_ip = 0;
1698 dhcp_client->state = INIT_SELECTING;
1699 switch_listening_mode(dhcp_client, L2);
1701 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1704 static gboolean start_expire(gpointer user_data)
1706 GDHCPClient *dhcp_client = user_data;
1708 debug(dhcp_client, "lease expired");
1710 /*remove all timeouts if they are set*/
1711 remove_timeouts(dhcp_client);
1713 restart_dhcp(dhcp_client, 0);
1715 /* ip need to be cleared */
1716 if (dhcp_client->lease_lost_cb)
1717 dhcp_client->lease_lost_cb(dhcp_client,
1718 dhcp_client->lease_lost_data);
1723 static gboolean continue_rebound(gpointer user_data)
1725 GDHCPClient *dhcp_client = user_data;
1728 switch_listening_mode(dhcp_client, L2);
1729 send_request(dhcp_client);
1731 if (dhcp_client->t2_timeout> 0) {
1732 g_source_remove(dhcp_client->t2_timeout);
1733 dhcp_client->t2_timeout = 0;
1736 /*recalculate remaining rebind time*/
1737 dhcp_client->T2 >>= 1;
1738 if (dhcp_client->T2 > 60) {
1739 __connman_util_get_random(&rand);
1740 dhcp_client->t2_timeout =
1741 g_timeout_add_full(G_PRIORITY_HIGH,
1742 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1751 static gboolean start_rebound(gpointer user_data)
1753 GDHCPClient *dhcp_client = user_data;
1755 /*remove renew timer*/
1756 if (dhcp_client->t1_timeout > 0)
1757 g_source_remove(dhcp_client->t1_timeout);
1759 debug(dhcp_client, "start rebound");
1760 dhcp_client->state = REBINDING;
1762 /*calculate total rebind time*/
1763 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1765 /*send the first rebound and reschedule*/
1766 continue_rebound(user_data);
1771 static gboolean continue_renew (gpointer user_data)
1773 GDHCPClient *dhcp_client = user_data;
1776 switch_listening_mode(dhcp_client, L3);
1777 send_request(dhcp_client);
1779 if (dhcp_client->t1_timeout > 0)
1780 g_source_remove(dhcp_client->t1_timeout);
1782 dhcp_client->t1_timeout = 0;
1784 dhcp_client->T1 >>= 1;
1786 if (dhcp_client->T1 > 60) {
1787 __connman_util_get_random(&rand);
1788 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1789 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1797 static gboolean start_renew(gpointer user_data)
1799 GDHCPClient *dhcp_client = user_data;
1801 debug(dhcp_client, "start renew");
1802 dhcp_client->state = RENEWING;
1804 /*calculate total renew period*/
1805 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1807 /*send first renew and reschedule for half the remaining time.*/
1808 continue_renew(user_data);
1813 static void start_bound(GDHCPClient *dhcp_client)
1815 debug(dhcp_client, "start bound");
1817 dhcp_client->state = BOUND;
1819 remove_timeouts(dhcp_client);
1822 *TODO: T1 and T2 should be set through options instead of
1823 * defaults as they are here.
1826 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1827 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1828 dhcp_client->expire = dhcp_client->lease_seconds;
1830 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1832 start_renew, dhcp_client,
1835 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1837 start_rebound, dhcp_client,
1840 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1841 dhcp_client->expire,
1842 start_expire, dhcp_client,
1846 static gboolean restart_dhcp_timeout(gpointer user_data)
1848 GDHCPClient *dhcp_client = user_data;
1850 debug(dhcp_client, "restart DHCP timeout");
1852 if (dhcp_client->state == REBOOTING) {
1853 g_free(dhcp_client->last_address);
1854 dhcp_client->last_address = NULL;
1855 restart_dhcp(dhcp_client, 0);
1857 dhcp_client->ack_retry_times++;
1858 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1863 static char *get_ip(uint32_t ip)
1865 struct in_addr addr;
1869 return g_strdup(inet_ntoa(addr));
1872 /* get a rough idea of how long an option will be */
1873 static const uint8_t len_of_option_as_string[] = {
1874 [OPTION_IP] = sizeof("255.255.255.255 "),
1875 [OPTION_STRING] = 1,
1876 [OPTION_U8] = sizeof("255 "),
1877 [OPTION_U16] = sizeof("65535 "),
1878 [OPTION_U32] = sizeof("4294967295 "),
1881 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1883 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1886 /* Create "opt_value1 option_value2 ..." string */
1887 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1889 unsigned upper_length;
1893 len = option[OPT_LEN - OPT_DATA];
1894 type &= OPTION_TYPE_MASK;
1895 optlen = dhcp_option_lengths[type];
1898 upper_length = len_of_option_as_string[type] *
1899 ((unsigned)len / (unsigned)optlen);
1900 dest = ret = g_malloc(upper_length + 1);
1904 while (len >= optlen) {
1907 dest += sprint_nip(dest, "", option);
1910 uint16_t val_u16 = get_be16(option);
1911 dest += sprintf(dest, "%u", val_u16);
1915 uint32_t val_u32 = get_be32(option);
1916 dest += sprintf(dest, "%u", val_u32);
1920 memcpy(dest, option, len);
1937 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1945 if (type == OPTION_STRING)
1946 return g_list_append(list, g_strdup(value));
1948 while ((pos = strchr(pos, ' '))) {
1951 list = g_list_append(list, g_strdup(value));
1956 list = g_list_append(list, g_strdup(value));
1961 static inline uint32_t get_uint32(unsigned char *value)
1963 return value[0] << 24 | value[1] << 16 |
1964 value[2] << 8 | value[3];
1967 static inline uint16_t get_uint16(unsigned char *value)
1969 return value[0] << 8 | value[1];
1972 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1973 struct in6_addr *addr,
1974 unsigned char prefixlen, uint32_t preferred,
1977 GDHCPIAPrefix *ia_prefix;
1979 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1983 if (dhcp_client->debug_func) {
1984 char addr_str[INET6_ADDRSTRLEN + 1];
1985 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1986 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1987 addr_str, prefixlen, preferred, valid);
1990 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1991 ia_prefix->prefixlen = prefixlen;
1992 ia_prefix->preferred = preferred;
1993 ia_prefix->valid = valid;
1994 ia_prefix->expire = time(NULL) + valid;
1996 return g_list_prepend(list, ia_prefix);
1999 static GList *get_addresses(GDHCPClient *dhcp_client,
2001 unsigned char *value,
2005 struct in6_addr addr;
2006 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
2007 uint16_t option_len, option_code, st = 0, max_len;
2008 int addr_count = 0, prefix_count = 0, i, pos;
2009 unsigned char prefixlen;
2010 unsigned int shortest_valid = 0;
2014 if (!value || len < 4)
2017 iaid = get_uint32(&value[0]);
2018 if (dhcp_client->iaid != iaid)
2021 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
2022 T1 = get_uint32(&value[4]);
2023 T2 = get_uint32(&value[8]);
2026 /* IA_NA: RFC 3315, 22.4 */
2027 /* IA_PD: RFC 3633, ch 9 */
2037 max_len = len - pos;
2039 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
2041 /* We have more sub-options in this packet. */
2043 option = dhcpv6_get_sub_option(&value[pos], max_len,
2044 &option_code, &option_len);
2046 debug(dhcp_client, "pos %d option %p code %d len %d",
2047 pos, option, option_code, option_len);
2055 switch (option_code) {
2056 case G_DHCPV6_IAADDR:
2058 memcpy(&addr, &option[0], sizeof(addr));
2060 preferred = get_uint32(&option[i]);
2062 valid = get_uint32(&option[i]);
2067 case G_DHCPV6_STATUS_CODE:
2068 st = get_uint16(&option[0]);
2069 debug(dhcp_client, "error code %d", st);
2070 if (option_len > 2) {
2071 str = g_strndup((gchar *)&option[2],
2073 debug(dhcp_client, "error text: %s", str);
2080 case G_DHCPV6_IA_PREFIX:
2082 preferred = get_uint32(&option[i]);
2084 valid = get_uint32(&option[i]);
2086 prefixlen = option[i];
2088 memcpy(&addr, &option[i], sizeof(addr));
2090 if (preferred < valid) {
2091 /* RFC 3633, ch 10 */
2092 list = add_prefix(dhcp_client, list, &addr,
2093 prefixlen, preferred, valid);
2094 if (shortest_valid > valid)
2095 shortest_valid = valid;
2101 pos += 2 + 2 + option_len;
2103 } while (pos < len);
2105 if (addr_count > 0 && st == 0) {
2106 /* We only support one address atm */
2107 char addr_str[INET6_ADDRSTRLEN + 1];
2109 if (preferred > valid)
2110 /* RFC 3315, 22.6 */
2113 dhcp_client->T1 = T1;
2114 dhcp_client->T2 = T2;
2116 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2117 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2118 addr_count, addr_str, T1, T2);
2120 list = g_list_append(list, g_strdup(addr_str));
2122 if (code == G_DHCPV6_IA_NA)
2123 memcpy(&dhcp_client->ia_na, &addr,
2124 sizeof(struct in6_addr));
2126 memcpy(&dhcp_client->ia_ta, &addr,
2127 sizeof(struct in6_addr));
2129 if (valid != dhcp_client->expire)
2130 dhcp_client->expire = valid;
2133 if (prefix_count > 0 && list) {
2135 * This means we have a list of prefixes to delegate.
2137 list = g_list_reverse(list);
2139 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2140 prefix_count, T1, T2);
2142 dhcp_client->T1 = T1;
2143 dhcp_client->T2 = T2;
2145 dhcp_client->expire = shortest_valid;
2148 if (status && *status != 0)
2149 debug(dhcp_client, "status %d", *status);
2154 static GList *get_domains(int maxlen, unsigned char *value)
2160 char dns_name[NS_MAXDNAME + 1];
2162 if (!value || maxlen < 3)
2165 while (pos < maxlen) {
2166 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2168 c = (unsigned char *)dns_name;
2175 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2176 pos += (char *)c - dns_name + 1;
2179 return g_list_reverse(list);
2182 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2184 unsigned char *value,
2195 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2196 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2199 "%s server list length (%d) is invalid",
2200 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2204 for (i = 0; i < len; i += 16) {
2206 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2210 if (!inet_ntop(AF_INET6, &value[i], str,
2214 list = g_list_append(list, str);
2218 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2219 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2220 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2221 list = get_addresses(dhcp_client, code, len, value, status);
2224 case G_DHCPV6_DOMAIN_LIST:
2225 list = get_domains(len, value);
2235 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2236 struct dhcpv6_packet *packet,
2237 uint16_t pkt_len, uint16_t *status)
2239 GList *list, *value_list;
2242 uint16_t option_len;
2244 for (list = dhcp_client->request_list; list; list = list->next) {
2245 code = (uint16_t) GPOINTER_TO_INT(list->data);
2247 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2250 g_hash_table_remove(dhcp_client->code_value_hash,
2251 GINT_TO_POINTER((int) code));
2255 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2256 option_len, option, status);
2258 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2259 option_len, value_list);
2262 g_hash_table_remove(dhcp_client->code_value_hash,
2263 GINT_TO_POINTER((int) code));
2265 g_hash_table_insert(dhcp_client->code_value_hash,
2266 GINT_TO_POINTER((int) code), value_list);
2270 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2272 GDHCPOptionType type;
2273 GList *list, *value_list;
2278 for (list = dhcp_client->request_list; list; list = list->next) {
2279 code = (uint8_t) GPOINTER_TO_INT(list->data);
2281 option = dhcp_get_option(packet, code);
2283 g_hash_table_remove(dhcp_client->code_value_hash,
2284 GINT_TO_POINTER((int) code));
2288 type = dhcp_get_code_type(code);
2290 option_value = malloc_option_value_string(option, type);
2292 g_hash_table_remove(dhcp_client->code_value_hash,
2293 GINT_TO_POINTER((int) code));
2295 value_list = get_option_value_list(option_value, type);
2297 g_free(option_value);
2300 g_hash_table_remove(dhcp_client->code_value_hash,
2301 GINT_TO_POINTER((int) code));
2303 g_hash_table_insert(dhcp_client->code_value_hash,
2304 GINT_TO_POINTER((int) code), value_list);
2308 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2311 GDHCPClient *dhcp_client = user_data;
2312 struct sockaddr_in dst_addr = { 0 };
2313 struct dhcp_packet packet;
2314 struct dhcpv6_packet *packet6 = NULL;
2315 uint8_t *message_type = NULL, *client_id = NULL, *option,
2317 uint16_t option_len = 0, status = 0;
2320 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2321 uint16_t pkt_len = 0;
2325 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2326 dhcp_client->listener_watch = 0;
2330 if (dhcp_client->listen_mode == L_NONE)
2335 dhcp_client->status_code = 0;
2337 if (dhcp_client->listen_mode == L2) {
2338 re = dhcp_recv_l2_packet(&packet,
2339 dhcp_client->listener_sockfd,
2342 } else if (dhcp_client->listen_mode == L3) {
2343 if (dhcp_client->type == G_DHCP_IPV6) {
2344 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2345 dhcp_client->listener_sockfd);
2350 xid = packet6->transaction_id[0] << 16 |
2351 packet6->transaction_id[1] << 8 |
2352 packet6->transaction_id[2];
2354 re = dhcp_recv_l3_packet(&packet,
2355 dhcp_client->listener_sockfd);
2358 } else if (dhcp_client->listen_mode == L_ARP) {
2359 ipv4ll_recv_arp_packet(dhcp_client);
2367 if (!check_package_owner(dhcp_client, pkt))
2370 if (dhcp_client->type == G_DHCP_IPV6) {
2375 client_id = dhcpv6_get_option(packet6, pkt_len,
2376 G_DHCPV6_CLIENTID, &option_len, &count);
2378 if (!client_id || count == 0 || option_len == 0 ||
2379 memcmp(dhcp_client->duid, client_id,
2380 dhcp_client->duid_len) != 0) {
2382 "client duid error, discarding msg %p/%d/%d",
2383 client_id, option_len, count);
2387 option = dhcpv6_get_option(packet6, pkt_len,
2388 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2389 if (option != 0 && option_len > 0) {
2390 status = option[0]<<8 | option[1];
2392 debug(dhcp_client, "error code %d", status);
2393 if (option_len > 2) {
2394 gchar *txt = g_strndup(
2395 (gchar *)&option[2],
2397 debug(dhcp_client, "error text: %s",
2402 dhcp_client->status_code = status;
2405 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2410 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2411 "(current state %d)", ntohl(xid), dhcp_client->state);
2413 switch (dhcp_client->state) {
2414 case INIT_SELECTING:
2415 if (*message_type != DHCPOFFER)
2418 remove_timeouts(dhcp_client);
2419 dhcp_client->timeout = 0;
2420 dhcp_client->retry_times = 0;
2422 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2423 dhcp_client->server_ip = get_be32(option);
2424 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2426 dhcp_client->state = REQUESTING;
2431 * If unicasting is not possible, the message MAY be
2432 * sent as an IP broadcast using an IP broadcast address
2433 * (preferably 0xffffffff) as the IP destination address
2434 * and the link-layer broadcast address as the link-layer
2435 * destination address.
2437 * For interoperability reasons, if the response is an IP
2438 * broadcast, let's reuse broadcast flag from DHCPDISCOVER
2439 * to which the server has responded. Some servers are picky
2442 dhcp_client->request_bcast =
2443 dst_addr.sin_addr.s_addr == INADDR_BROADCAST &&
2444 g_hash_table_contains(dhcp_client->secs_bcast_hash,
2445 GINT_TO_POINTER(packet.secs));
2447 debug(dhcp_client, "init ip %s secs %hu -> broadcast flag %s",
2448 inet_ntoa(dst_addr.sin_addr), packet.secs,
2449 dhcp_client->request_bcast ? "on" : "off");
2451 start_request(dhcp_client);
2455 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2456 dhcp_client->request_bcast = true;
2458 dhcp_client->request_bcast = false;
2460 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2461 inet_ntoa(dst_addr.sin_addr),
2462 dhcp_client->request_bcast ? "" : "not ");
2467 if (*message_type == DHCPACK) {
2468 dhcp_client->retry_times = 0;
2470 remove_timeouts(dhcp_client);
2472 dhcp_client->lease_seconds = get_lease(&packet);
2474 #if defined TIZEN_EXT
2475 dhcp_client->dhcp_lease_seconds = dhcp_client->lease_seconds;
2478 get_request(dhcp_client, &packet);
2480 switch_listening_mode(dhcp_client, L_NONE);
2482 g_free(dhcp_client->assigned_ip);
2483 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2485 if (dhcp_client->state == REBOOTING) {
2486 option = dhcp_get_option(&packet,
2488 dhcp_client->server_ip = get_be32(option);
2491 /* Address should be set up here */
2492 if (dhcp_client->lease_available_cb)
2493 dhcp_client->lease_available_cb(dhcp_client,
2494 dhcp_client->lease_available_data);
2496 start_bound(dhcp_client);
2497 } else if (*message_type == DHCPNAK) {
2498 dhcp_client->retry_times = 0;
2500 remove_timeouts(dhcp_client);
2502 #if defined TIZEN_EXT
2503 if (dhcp_client->init_reboot) {
2504 g_dhcp_client_set_address_known(dhcp_client, FALSE);
2505 dhcp_client->timeout = g_idle_add_full(
2507 restart_dhcp_timeout,
2514 dhcp_client->timeout = g_timeout_add_seconds_full(
2516 restart_dhcp_timeout,
2523 if (dhcp_client->type != G_DHCP_IPV6)
2526 if (packet6->message != DHCPV6_REPLY &&
2527 packet6->message != DHCPV6_ADVERTISE)
2531 server_id = dhcpv6_get_option(packet6, pkt_len,
2532 G_DHCPV6_SERVERID, &option_len, &count);
2533 if (!server_id || count != 1 || option_len == 0) {
2534 /* RFC 3315, 15.10 */
2536 "server duid error, discarding msg %p/%d/%d",
2537 server_id, option_len, count);
2540 dhcp_client->server_duid = g_try_malloc(option_len);
2541 if (!dhcp_client->server_duid)
2543 memcpy(dhcp_client->server_duid, server_id, option_len);
2544 dhcp_client->server_duid_len = option_len;
2546 if (packet6->message == DHCPV6_REPLY) {
2547 uint8_t *rapid_commit;
2550 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2551 G_DHCPV6_RAPID_COMMIT,
2552 &option_len, &count);
2553 if (!rapid_commit || option_len != 0 ||
2555 /* RFC 3315, 17.1.4 */
2559 switch_listening_mode(dhcp_client, L_NONE);
2561 if (dhcp_client->status_code == 0)
2562 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2563 &dhcp_client->status_code);
2565 if (packet6->message == DHCPV6_ADVERTISE) {
2566 if (dhcp_client->advertise_cb)
2567 dhcp_client->advertise_cb(dhcp_client,
2568 dhcp_client->advertise_data);
2572 if (dhcp_client->solicitation_cb) {
2574 * The dhcp_client might not be valid after the
2575 * callback call so just return immediately.
2577 dhcp_client->solicitation_cb(dhcp_client,
2578 dhcp_client->solicitation_data);
2583 if (dhcp_client->type != G_DHCP_IPV6)
2586 server_id = dhcpv6_get_option(packet6, pkt_len,
2587 G_DHCPV6_SERVERID, &option_len, &count);
2588 if (!dhcp_client->server_duid && server_id &&
2591 * If we do not have server duid yet, then get it now.
2592 * Prefix delegation renew support needs it.
2594 dhcp_client->server_duid = g_try_malloc(option_len);
2595 if (!dhcp_client->server_duid)
2597 memcpy(dhcp_client->server_duid, server_id, option_len);
2598 dhcp_client->server_duid_len = option_len;
2601 case INFORMATION_REQ:
2607 if (dhcp_client->type != G_DHCP_IPV6)
2610 if (packet6->message != DHCPV6_REPLY)
2615 server_id = dhcpv6_get_option(packet6, pkt_len,
2616 G_DHCPV6_SERVERID, &option_len, &count);
2617 if (!server_id || count != 1 || option_len == 0 ||
2618 (dhcp_client->server_duid_len > 0 &&
2619 memcmp(dhcp_client->server_duid, server_id,
2620 dhcp_client->server_duid_len) != 0)) {
2621 /* RFC 3315, 15.10 */
2623 "server duid error, discarding msg %p/%d/%d",
2624 server_id, option_len, count);
2628 switch_listening_mode(dhcp_client, L_NONE);
2630 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2631 &dhcp_client->status_code);
2633 if (dhcp_client->information_req_cb) {
2635 * The dhcp_client might not be valid after the
2636 * callback call so just return immediately.
2638 dhcp_client->information_req_cb(dhcp_client,
2639 dhcp_client->information_req_data);
2642 if (dhcp_client->request_cb) {
2643 dhcp_client->request_cb(dhcp_client,
2644 dhcp_client->request_data);
2647 if (dhcp_client->renew_cb) {
2648 dhcp_client->renew_cb(dhcp_client,
2649 dhcp_client->renew_data);
2652 if (dhcp_client->rebind_cb) {
2653 dhcp_client->rebind_cb(dhcp_client,
2654 dhcp_client->rebind_data);
2657 if (dhcp_client->release_cb) {
2658 dhcp_client->release_cb(dhcp_client,
2659 dhcp_client->release_data);
2662 if (dhcp_client->decline_cb) {
2663 dhcp_client->decline_cb(dhcp_client,
2664 dhcp_client->decline_data);
2667 if (dhcp_client->confirm_cb) {
2669 server_id = dhcpv6_get_option(packet6, pkt_len,
2670 G_DHCPV6_SERVERID, &option_len,
2672 if (!server_id || count != 1 ||
2674 /* RFC 3315, 15.10 */
2676 "confirm server duid error, "
2677 "discarding msg %p/%d/%d",
2678 server_id, option_len, count);
2681 dhcp_client->server_duid = g_try_malloc(option_len);
2682 if (!dhcp_client->server_duid)
2684 memcpy(dhcp_client->server_duid, server_id, option_len);
2685 dhcp_client->server_duid_len = option_len;
2687 dhcp_client->confirm_cb(dhcp_client,
2688 dhcp_client->confirm_data);
2696 debug(dhcp_client, "processed DHCP packet (new state %d)",
2697 dhcp_client->state);
2702 static gboolean discover_timeout(gpointer user_data)
2704 GDHCPClient *dhcp_client = user_data;
2706 dhcp_client->retry_times++;
2709 * We do not send the REQUESTED IP option if we are retrying because
2710 * if the server is non-authoritative it will ignore the request if the
2711 * option is present.
2713 g_dhcp_client_start(dhcp_client, NULL);
2718 static gboolean reboot_timeout(gpointer user_data)
2720 GDHCPClient *dhcp_client = user_data;
2721 dhcp_client->retry_times = 0;
2722 dhcp_client->requested_ip = 0;
2723 dhcp_client->state = INIT_SELECTING;
2725 * We do not send the REQUESTED IP option because the server didn't
2726 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2729 g_dhcp_client_start(dhcp_client, NULL);
2734 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2736 GDHCPClient *dhcp_client = dhcp_data;
2738 debug(dhcp_client, "back to MONITOR mode");
2740 dhcp_client->conflicts = 0;
2741 dhcp_client->state = IPV4LL_MONITOR;
2746 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2748 GDHCPClient *dhcp_client = dhcp_data;
2751 #if defined TIZEN_EXT
2756 debug(dhcp_client, "request timeout (retries %d)",
2757 dhcp_client->retry_times);
2759 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2760 dhcp_client->retry_times++;
2761 send_announce_packet(dhcp_client);
2765 ip = htonl(dhcp_client->requested_ip);
2766 debug(dhcp_client, "switching to monitor mode");
2767 dhcp_client->state = IPV4LL_MONITOR;
2768 dhcp_client->assigned_ip = get_ip(ip);
2770 if (dhcp_client->ipv4ll_available_cb)
2771 dhcp_client->ipv4ll_available_cb(dhcp_client,
2772 dhcp_client->ipv4ll_available_data);
2773 dhcp_client->conflicts = 0;
2774 dhcp_client->timeout = 0;
2779 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2782 GDHCPClient *dhcp_client = dhcp_data;
2784 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2785 dhcp_client->retry_times);
2787 if (dhcp_client->retry_times == PROBE_NUM) {
2788 dhcp_client->state = IPV4LL_ANNOUNCE;
2789 dhcp_client->retry_times = 0;
2791 dhcp_client->retry_times++;
2792 send_announce_packet(dhcp_client);
2795 dhcp_client->retry_times++;
2796 send_probe_packet(dhcp_client);
2801 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2806 ClientState oldstate = dhcp_client->state;
2808 #if defined TIZEN_EXT
2809 int discover_retry = 0;
2813 remove_timeouts(dhcp_client);
2815 if (dhcp_client->type == G_DHCP_IPV6) {
2816 if (dhcp_client->information_req_cb) {
2817 dhcp_client->state = INFORMATION_REQ;
2818 re = switch_listening_mode(dhcp_client, L3);
2820 switch_listening_mode(dhcp_client, L_NONE);
2821 dhcp_client->state = 0;
2824 send_information_req(dhcp_client);
2826 } else if (dhcp_client->solicitation_cb) {
2827 dhcp_client->state = SOLICITATION;
2828 re = switch_listening_mode(dhcp_client, L3);
2830 switch_listening_mode(dhcp_client, L_NONE);
2831 dhcp_client->state = 0;
2834 send_solicitation(dhcp_client);
2836 } else if (dhcp_client->request_cb) {
2837 dhcp_client->state = REQUEST;
2838 re = switch_listening_mode(dhcp_client, L3);
2840 switch_listening_mode(dhcp_client, L_NONE);
2841 dhcp_client->state = 0;
2844 send_dhcpv6_request(dhcp_client);
2846 } else if (dhcp_client->confirm_cb) {
2847 dhcp_client->state = CONFIRM;
2848 re = switch_listening_mode(dhcp_client, L3);
2850 switch_listening_mode(dhcp_client, L_NONE);
2851 dhcp_client->state = 0;
2854 send_dhcpv6_confirm(dhcp_client);
2856 } else if (dhcp_client->renew_cb) {
2857 dhcp_client->state = RENEW;
2858 re = switch_listening_mode(dhcp_client, L3);
2860 switch_listening_mode(dhcp_client, L_NONE);
2861 dhcp_client->state = 0;
2864 send_dhcpv6_renew(dhcp_client);
2866 } else if (dhcp_client->rebind_cb) {
2867 dhcp_client->state = REBIND;
2868 re = switch_listening_mode(dhcp_client, L3);
2870 switch_listening_mode(dhcp_client, L_NONE);
2871 dhcp_client->state = 0;
2874 send_dhcpv6_rebind(dhcp_client);
2876 } else if (dhcp_client->release_cb) {
2877 dhcp_client->state = RENEW;
2878 re = switch_listening_mode(dhcp_client, L3);
2880 switch_listening_mode(dhcp_client, L_NONE);
2881 dhcp_client->state = 0;
2884 send_dhcpv6_release(dhcp_client);
2885 } else if (dhcp_client->decline_cb) {
2886 dhcp_client->state = DECLINE;
2887 re = switch_listening_mode(dhcp_client, L3);
2889 switch_listening_mode(dhcp_client, L_NONE);
2890 dhcp_client->state = 0;
2893 send_dhcpv6_decline(dhcp_client);
2899 #if defined TIZEN_EXT
2900 if (g_ascii_strncasecmp(dhcp_client->interface, "wlan", 4)
2902 discover_retry = DISCOVER_RETRIES_WIFI;
2903 timeout = DISCOVER_TIMEOUT_WIFI;
2905 discover_retry = DISCOVER_RETRIES;
2906 timeout = DISCOVER_TIMEOUT;
2909 debug(dhcp_client, "[DHCPC] Discover retry/total : [%d]/[%d] timeout [%d]",
2910 dhcp_client->retry_times, discover_retry, timeout);
2914 if (dhcp_client->type == G_DHCP_IPV4LL) {
2915 dhcp_client->state = INIT_SELECTING;
2916 ipv4ll_start(dhcp_client);
2920 #if defined TIZEN_EXT
2921 if (dhcp_client->retry_times == discover_retry) {
2923 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2925 if (dhcp_client->no_lease_cb)
2926 dhcp_client->no_lease_cb(dhcp_client,
2927 dhcp_client->no_lease_data);
2928 dhcp_client->retry_times = 0;
2932 if (dhcp_client->retry_times == 0) {
2933 g_free(dhcp_client->assigned_ip);
2934 dhcp_client->assigned_ip = NULL;
2936 dhcp_client->state = INIT_SELECTING;
2937 re = switch_listening_mode(dhcp_client, L2);
2941 __connman_util_get_random(&rand);
2942 dhcp_client->xid = rand;
2943 dhcp_client->start = time(NULL);
2944 g_hash_table_remove_all(dhcp_client->secs_bcast_hash);
2947 if (!last_address || oldstate == DECLINED) {
2950 addr = ntohl(inet_addr(last_address));
2951 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2954 } else if (dhcp_client->last_address != last_address) {
2955 g_free(dhcp_client->last_address);
2956 dhcp_client->last_address = g_strdup(last_address);
2960 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2961 debug(dhcp_client, "DHCP client start with state init_reboot");
2962 dhcp_client->requested_ip = addr;
2963 dhcp_client->state = REBOOTING;
2964 send_request(dhcp_client);
2966 dhcp_client->timeout = g_timeout_add_seconds_full(
2968 #if defined TIZEN_EXT
2978 send_discover(dhcp_client, addr);
2980 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2981 #if defined TIZEN_EXT
2992 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2994 switch_listening_mode(dhcp_client, L_NONE);
2996 if (dhcp_client->state == BOUND ||
2997 dhcp_client->state == RENEWING ||
2998 dhcp_client->state == REBINDING)
2999 send_release(dhcp_client, dhcp_client->server_ip,
3000 dhcp_client->requested_ip);
3002 remove_timeouts(dhcp_client);
3004 if (dhcp_client->listener_watch > 0) {
3005 g_source_remove(dhcp_client->listener_watch);
3006 dhcp_client->listener_watch = 0;
3009 dhcp_client->retry_times = 0;
3010 dhcp_client->ack_retry_times = 0;
3012 dhcp_client->requested_ip = 0;
3013 dhcp_client->state = RELEASED;
3014 dhcp_client->lease_seconds = 0;
3015 dhcp_client->request_bcast = false;
3018 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
3019 unsigned char option_code)
3021 return g_hash_table_lookup(dhcp_client->code_value_hash,
3022 GINT_TO_POINTER((int) option_code));
3025 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
3026 GDHCPClientEvent event,
3027 GDHCPClientEventFunc func,
3031 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
3032 dhcp_client->lease_available_cb = func;
3033 dhcp_client->lease_available_data = data;
3035 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
3036 if (dhcp_client->type == G_DHCP_IPV6)
3038 dhcp_client->ipv4ll_available_cb = func;
3039 dhcp_client->ipv4ll_available_data = data;
3041 case G_DHCP_CLIENT_EVENT_NO_LEASE:
3042 dhcp_client->no_lease_cb = func;
3043 dhcp_client->no_lease_data = data;
3045 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
3046 dhcp_client->lease_lost_cb = func;
3047 dhcp_client->lease_lost_data = data;
3049 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
3050 if (dhcp_client->type == G_DHCP_IPV6)
3052 dhcp_client->ipv4ll_lost_cb = func;
3053 dhcp_client->ipv4ll_lost_data = data;
3055 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
3056 dhcp_client->address_conflict_cb = func;
3057 dhcp_client->address_conflict_data = data;
3059 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
3060 if (dhcp_client->type != G_DHCP_IPV6)
3062 dhcp_client->information_req_cb = func;
3063 dhcp_client->information_req_data = data;
3065 case G_DHCP_CLIENT_EVENT_SOLICITATION:
3066 if (dhcp_client->type != G_DHCP_IPV6)
3068 dhcp_client->solicitation_cb = func;
3069 dhcp_client->solicitation_data = data;
3071 case G_DHCP_CLIENT_EVENT_ADVERTISE:
3072 if (dhcp_client->type != G_DHCP_IPV6)
3074 dhcp_client->advertise_cb = func;
3075 dhcp_client->advertise_data = data;
3077 case G_DHCP_CLIENT_EVENT_REQUEST:
3078 if (dhcp_client->type != G_DHCP_IPV6)
3080 dhcp_client->request_cb = func;
3081 dhcp_client->request_data = data;
3083 case G_DHCP_CLIENT_EVENT_RENEW:
3084 if (dhcp_client->type != G_DHCP_IPV6)
3086 dhcp_client->renew_cb = func;
3087 dhcp_client->renew_data = data;
3089 case G_DHCP_CLIENT_EVENT_REBIND:
3090 if (dhcp_client->type != G_DHCP_IPV6)
3092 dhcp_client->rebind_cb = func;
3093 dhcp_client->rebind_data = data;
3095 case G_DHCP_CLIENT_EVENT_RELEASE:
3096 if (dhcp_client->type != G_DHCP_IPV6)
3098 dhcp_client->release_cb = func;
3099 dhcp_client->release_data = data;
3101 case G_DHCP_CLIENT_EVENT_CONFIRM:
3102 if (dhcp_client->type != G_DHCP_IPV6)
3104 dhcp_client->confirm_cb = func;
3105 dhcp_client->confirm_data = data;
3107 case G_DHCP_CLIENT_EVENT_DECLINE:
3108 if (dhcp_client->type != G_DHCP_IPV6)
3110 dhcp_client->decline_cb = func;
3111 dhcp_client->decline_data = data;
3116 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
3118 return dhcp_client->ifindex;
3121 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
3126 #if defined TIZEN_EXT
3127 return get_ip(htonl(dhcp_client->server_ip));
3129 return get_ip(dhcp_client->server_ip);
3133 #if defined TIZEN_EXT
3134 int g_dhcp_client_get_dhcp_lease_duration(GDHCPClient *dhcp_client)
3136 return dhcp_client->dhcp_lease_seconds;
3140 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
3142 return g_strdup(dhcp_client->assigned_ip);
3145 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
3147 GList *option = NULL;
3149 if (dhcp_client->type == G_DHCP_IPV6)
3152 switch (dhcp_client->state) {
3154 case IPV4LL_MONITOR:
3155 return g_strdup("255.255.0.0");
3159 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3161 return g_strdup(option->data);
3162 case INIT_SELECTING:
3168 case IPV4LL_ANNOUNCE:
3169 case INFORMATION_REQ:
3182 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3183 unsigned int option_code)
3185 if (!g_list_find(dhcp_client->request_list,
3186 GINT_TO_POINTER((int)option_code)))
3187 dhcp_client->request_list = g_list_prepend(
3188 dhcp_client->request_list,
3189 (GINT_TO_POINTER((int) option_code)));
3191 return G_DHCP_CLIENT_ERROR_NONE;
3194 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3196 g_list_free(dhcp_client->request_list);
3197 dhcp_client->request_list = NULL;
3200 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3202 g_hash_table_remove_all(dhcp_client->send_value_hash);
3205 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3209 storage = g_try_malloc(size + OPT_DATA);
3213 storage[OPT_CODE] = code;
3214 storage[OPT_LEN] = size;
3215 memcpy(&storage[OPT_DATA], data, size);
3220 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3223 return alloc_dhcp_option(code, data, MIN(size, 255));
3226 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3228 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3231 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3233 const unsigned maclen = 6;
3234 const unsigned idlen = maclen + 1;
3235 const uint8_t option_code = G_DHCP_CLIENT_ID;
3236 uint8_t idbuf[idlen];
3237 uint8_t *data_option;
3239 idbuf[0] = ARPHRD_ETHER;
3241 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3243 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3245 return G_DHCP_CLIENT_ERROR_NOMEM;
3247 g_hash_table_insert(dhcp_client->send_value_hash,
3248 GINT_TO_POINTER((int) option_code), data_option);
3250 return G_DHCP_CLIENT_ERROR_NONE;
3253 /* Now only support send hostname and vendor class ID */
3254 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3255 unsigned char option_code, const char *option_value)
3257 uint8_t *binary_option;
3259 if ((option_code == G_DHCP_HOST_NAME ||
3260 option_code == G_DHCP_VENDOR_CLASS_ID) && option_value) {
3261 binary_option = alloc_dhcp_string_option(option_code,
3264 return G_DHCP_CLIENT_ERROR_NOMEM;
3266 g_hash_table_insert(dhcp_client->send_value_hash,
3267 GINT_TO_POINTER((int) option_code), binary_option);
3270 return G_DHCP_CLIENT_ERROR_NONE;
3273 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3278 storage = g_malloc(2 + 2 + len);
3282 storage[0] = code >> 8;
3283 storage[1] = code & 0xff;
3284 storage[2] = len >> 8;
3285 storage[3] = len & 0xff;
3286 memcpy(storage + 2 + 2, option, len);
3291 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3293 return g_hash_table_remove(dhcp_client->send_value_hash,
3294 GINT_TO_POINTER((int)code));
3297 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3298 uint16_t option_code,
3299 uint8_t *option_value,
3300 uint16_t option_len)
3303 uint8_t *binary_option;
3305 debug(dhcp_client, "setting option %d to %p len %d",
3306 option_code, option_value, option_len);
3308 binary_option = alloc_dhcpv6_option(option_code, option_value,
3311 g_hash_table_insert(dhcp_client->send_value_hash,
3312 GINT_TO_POINTER((int) option_code),
3317 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3319 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3322 dhcp_client->last_request = time(NULL);
3325 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3327 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3330 return dhcp_client->status_code;
3333 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3338 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3343 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3348 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3351 g_dhcp_client_stop(dhcp_client);
3353 g_free(dhcp_client->interface);
3354 g_free(dhcp_client->assigned_ip);
3355 g_free(dhcp_client->last_address);
3356 g_free(dhcp_client->duid);
3357 g_free(dhcp_client->server_duid);
3359 g_list_free(dhcp_client->request_list);
3360 g_list_free(dhcp_client->require_list);
3362 g_hash_table_destroy(dhcp_client->code_value_hash);
3363 g_hash_table_destroy(dhcp_client->send_value_hash);
3364 g_hash_table_destroy(dhcp_client->secs_bcast_hash);
3366 g_free(dhcp_client);
3367 #if defined TIZEN_EXT
3372 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3373 GDHCPDebugFunc func, gpointer user_data)
3378 dhcp_client->debug_func = func;
3379 dhcp_client->debug_data = user_data;
3382 static GDHCPIAPrefix *copy_prefix(gpointer data)
3384 GDHCPIAPrefix *copy, *prefix = data;
3386 copy = g_try_new(GDHCPIAPrefix, 1);
3390 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3395 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3397 GSList *copy = NULL;
3400 for (list = prefixes; list; list = list->next)
3401 copy = g_slist_prepend(copy, copy_prefix(list->data));
3406 #if defined TIZEN_EXT
3407 void g_dhcp_client_set_address_known(GDHCPClient *dhcp_client, gboolean known)
3409 /* DHCPREQUEST during INIT-REBOOT state (rfc2131)
3410 * 4.4.3 Initialization with known network address
3411 * 4.3.2 DHCPREQUEST generated during INIT-REBOOT state
3413 debug(dhcp_client, "known network address (%d)", known);
3415 if (dhcp_client->init_reboot == known)
3418 dhcp_client->init_reboot = known;