3 * DHCP client library with GLib integration
5 * Copyright (C) 2009-2014 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
33 #include <arpa/inet.h>
37 #include <netpacket/packet.h>
38 #include <netinet/if_ether.h>
39 #include <net/ethernet.h>
42 #include <linux/filter.h>
50 #define DISCOVER_TIMEOUT 5
51 #define DISCOVER_RETRIES 6
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 {
106 struct _GDHCPClient {
112 uint8_t mac_address[6];
115 uint32_t requested_ip;
118 uint32_t lease_seconds;
119 ListenMode listen_mode;
122 uint8_t ack_retry_times;
128 guint listener_watch;
131 GHashTable *code_value_hash;
132 GHashTable *send_value_hash;
133 GDHCPClientEventFunc lease_available_cb;
134 gpointer lease_available_data;
135 GDHCPClientEventFunc ipv4ll_available_cb;
136 gpointer ipv4ll_available_data;
137 GDHCPClientEventFunc no_lease_cb;
138 gpointer no_lease_data;
139 GDHCPClientEventFunc lease_lost_cb;
140 gpointer lease_lost_data;
141 GDHCPClientEventFunc ipv4ll_lost_cb;
142 gpointer ipv4ll_lost_data;
143 GDHCPClientEventFunc address_conflict_cb;
144 gpointer address_conflict_data;
145 GDHCPDebugFunc debug_func;
147 GDHCPClientEventFunc information_req_cb;
148 gpointer information_req_data;
149 GDHCPClientEventFunc solicitation_cb;
150 gpointer solicitation_data;
151 GDHCPClientEventFunc advertise_cb;
152 gpointer advertise_data;
153 GDHCPClientEventFunc request_cb;
154 gpointer request_data;
155 GDHCPClientEventFunc renew_cb;
157 GDHCPClientEventFunc rebind_cb;
158 gpointer rebind_data;
159 GDHCPClientEventFunc release_cb;
160 gpointer release_data;
161 GDHCPClientEventFunc confirm_cb;
162 gpointer confirm_data;
163 GDHCPClientEventFunc decline_cb;
164 gpointer decline_data;
168 unsigned char *server_duid;
170 uint16_t status_code;
173 struct in6_addr ia_na;
174 struct in6_addr ia_ta;
178 struct timeval start_time;
180 #if defined TIZEN_EXT
181 uint32_t dhcp_lease_seconds;
182 gboolean init_reboot;
186 static inline void debug(GDHCPClient *client, const char *format, ...)
191 if (!client->debug_func)
194 va_start(ap, format);
196 if (vsnprintf(str, sizeof(str), format, ap) > 0)
197 client->debug_func(str, client->debug_data);
202 /* Initialize the packet with the proper defaults */
203 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
205 if (dhcp_client->type == G_DHCP_IPV6)
206 dhcpv6_init_header(pkt, type);
208 struct dhcp_packet *packet = pkt;
210 dhcp_init_header(packet, type);
211 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
215 static void add_request_options(GDHCPClient *dhcp_client,
216 struct dhcp_packet *packet)
221 int end = dhcp_end_option(packet->options);
223 for (list = dhcp_client->request_list; list; list = list->next) {
224 code = (uint8_t) GPOINTER_TO_INT(list->data);
226 packet->options[end + OPT_DATA + len] = code;
231 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
232 packet->options[end + OPT_LEN] = len;
233 packet->options[end + OPT_DATA + len] = DHCP_END;
240 unsigned char **ptr_buf;
243 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
246 uint8_t *option = value;
248 struct hash_params *params = user_data;
250 /* option[0][1] contains option code */
251 len = option[2] << 8 | option[3];
253 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
256 memcpy(*params->ptr_buf, option, len + 2 + 2);
257 (*params->ptr_buf) += len + 2 + 2;
260 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
261 unsigned char *buf, int max_buf,
262 unsigned char **ptr_buf)
264 struct hash_params params = {
270 if (dhcp_client->type != G_DHCP_IPV6)
273 g_hash_table_foreach(dhcp_client->send_value_hash,
274 add_dhcpv6_binary_option, ¶ms);
276 *ptr_buf = *params.ptr_buf;
279 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
283 buf[1] = code & 0xff;
287 memcpy(&buf[4], msg, len);
290 static int32_t get_time_diff(struct timeval *tv)
295 gettimeofday(&now, NULL);
297 hsec = (now.tv_sec - tv->tv_sec) * 100;
298 hsec += (now.tv_usec - tv->tv_usec) / 10000;
303 static void remove_timeouts(GDHCPClient *dhcp_client)
306 if (dhcp_client->timeout > 0)
307 g_source_remove(dhcp_client->timeout);
308 if (dhcp_client->t1_timeout > 0)
309 g_source_remove(dhcp_client->t1_timeout);
310 if (dhcp_client->t2_timeout > 0)
311 g_source_remove(dhcp_client->t2_timeout);
312 if (dhcp_client->lease_timeout > 0)
313 g_source_remove(dhcp_client->lease_timeout);
315 dhcp_client->timeout = 0;
316 dhcp_client->t1_timeout = 0;
317 dhcp_client->t2_timeout = 0;
318 dhcp_client->lease_timeout = 0;
322 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
323 struct dhcpv6_packet *packet,
324 unsigned char *buf, int max_buf,
325 unsigned char **ptr_buf)
328 uint16_t code, value;
333 if (dhcp_client->type != G_DHCP_IPV6)
336 for (list = dhcp_client->request_list; list; list = list->next) {
337 code = (uint16_t) GPOINTER_TO_INT(list->data);
341 case G_DHCPV6_CLIENTID:
342 if (!dhcp_client->duid)
345 len = 2 + 2 + dhcp_client->duid_len;
346 if ((*ptr_buf + len) > (buf + max_buf)) {
347 debug(dhcp_client, "Too long dhcpv6 message "
348 "when writing client id option");
352 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
353 dhcp_client->duid_len, dhcp_client->duid);
358 case G_DHCPV6_SERVERID:
359 if (!dhcp_client->server_duid)
362 len = 2 + 2 + dhcp_client->server_duid_len;
363 if ((*ptr_buf + len) > (buf + max_buf)) {
364 debug(dhcp_client, "Too long dhcpv6 message "
365 "when writing server id option");
369 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
370 dhcp_client->server_duid_len,
371 dhcp_client->server_duid);
376 case G_DHCPV6_RAPID_COMMIT:
378 if ((*ptr_buf + len) > (buf + max_buf)) {
379 debug(dhcp_client, "Too long dhcpv6 message "
380 "when writing rapid commit option");
384 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
392 case G_DHCPV6_ELAPSED_TIME:
393 if (!dhcp_client->retransmit) {
395 * Initial message, elapsed time is 0.
399 diff = get_time_diff(&dhcp_client->start_time);
400 if (diff < 0 || diff > 0xffff)
405 if ((*ptr_buf + len) > (buf + max_buf)) {
406 debug(dhcp_client, "Too long dhcpv6 message "
407 "when writing elapsed time option");
411 value = htons((uint16_t)diff);
412 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
413 2, (uint8_t *)&value);
418 case G_DHCPV6_DNS_SERVERS:
421 case G_DHCPV6_DOMAIN_LIST:
424 case G_DHCPV6_SNTP_SERVERS:
432 debug(dhcp_client, "option %d len %d added", code, len);
436 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
438 uint8_t *option = value;
439 struct dhcp_packet *packet = user_data;
441 dhcp_add_binary_option(packet, option);
444 static void add_send_options(GDHCPClient *dhcp_client,
445 struct dhcp_packet *packet)
447 g_hash_table_foreach(dhcp_client->send_value_hash,
448 add_binary_option, packet);
452 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
453 * represents the number of seconds elapsed from the start of
454 * attempting DHCP to satisfy some DHCP servers that allow for an
455 * "authoritative" reply before responding.
457 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
459 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
462 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
464 struct dhcp_packet packet;
466 debug(dhcp_client, "sending DHCP discover request");
468 init_packet(dhcp_client, &packet, DHCPDISCOVER);
470 packet.xid = dhcp_client->xid;
471 packet.secs = dhcp_attempt_secs(dhcp_client);
474 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
476 /* Explicitly saying that we want RFC-compliant packets helps
477 * some buggy DHCP servers to NOT send bigger packets */
478 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
480 add_request_options(dhcp_client, &packet);
482 add_send_options(dhcp_client, &packet);
485 * If we do not get a reply to DISCOVER packet, then we try with
486 * broadcast flag set. So first packet is sent without broadcast flag,
487 * first retry is with broadcast flag, second retry is without it etc.
488 * Reason is various buggy routers/AP that either eat the other or vice
489 * versa. In the receiving side we then find out what kind of packet
490 * the server can send.
492 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
493 INADDR_BROADCAST, SERVER_PORT,
494 MAC_BCAST_ADDR, dhcp_client->ifindex,
495 dhcp_client->retry_times % 2);
498 static int send_request(GDHCPClient *dhcp_client)
500 struct dhcp_packet packet;
502 debug(dhcp_client, "sending DHCP request (state %d)",
505 init_packet(dhcp_client, &packet, DHCPREQUEST);
507 packet.xid = dhcp_client->xid;
508 #if defined TIZEN_EXT
509 if (dhcp_client->init_reboot != TRUE)
511 packet.secs = dhcp_attempt_secs(dhcp_client);
513 if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
514 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
515 dhcp_client->requested_ip);
517 if (dhcp_client->state == REQUESTING)
518 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
519 dhcp_client->server_ip);
521 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
523 add_request_options(dhcp_client, &packet);
525 add_send_options(dhcp_client, &packet);
527 if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
528 packet.ciaddr = htonl(dhcp_client->requested_ip);
530 if (dhcp_client->state == RENEWING)
531 return dhcp_send_kernel_packet(&packet,
532 dhcp_client->requested_ip, CLIENT_PORT,
533 dhcp_client->server_ip, SERVER_PORT);
535 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
536 INADDR_BROADCAST, SERVER_PORT,
537 MAC_BCAST_ADDR, dhcp_client->ifindex,
538 dhcp_client->request_bcast);
541 static int send_release(GDHCPClient *dhcp_client,
542 uint32_t server, uint32_t ciaddr)
544 struct dhcp_packet packet;
547 debug(dhcp_client, "sending DHCP release request");
549 init_packet(dhcp_client, &packet, DHCPRELEASE);
550 dhcp_get_random(&rand);
552 packet.ciaddr = htonl(ciaddr);
554 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
556 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
557 server, SERVER_PORT);
560 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
561 static int switch_listening_mode(GDHCPClient *dhcp_client,
562 ListenMode listen_mode);
564 static gboolean send_probe_packet(gpointer dhcp_data)
566 GDHCPClient *dhcp_client;
569 dhcp_client = dhcp_data;
570 /* if requested_ip is not valid, pick a new address*/
571 if (dhcp_client->requested_ip == 0) {
572 debug(dhcp_client, "pick a new random address");
573 dhcp_client->requested_ip = ipv4ll_random_ip();
576 debug(dhcp_client, "sending IPV4LL probe request");
578 if (dhcp_client->retry_times == 1) {
579 dhcp_client->state = IPV4LL_PROBE;
580 switch_listening_mode(dhcp_client, L_ARP);
582 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
583 dhcp_client->requested_ip, dhcp_client->ifindex);
585 if (dhcp_client->retry_times < PROBE_NUM) {
586 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
587 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
588 timeout += PROBE_MIN*1000;
590 timeout = (ANNOUNCE_WAIT * 1000);
592 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
594 ipv4ll_probe_timeout,
600 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
601 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
603 static gboolean send_announce_packet(gpointer dhcp_data)
605 GDHCPClient *dhcp_client;
607 dhcp_client = dhcp_data;
609 debug(dhcp_client, "sending IPV4LL announce request");
611 ipv4ll_send_arp_packet(dhcp_client->mac_address,
612 dhcp_client->requested_ip,
613 dhcp_client->requested_ip,
614 dhcp_client->ifindex);
616 remove_timeouts(dhcp_client);
618 if (dhcp_client->state == IPV4LL_DEFEND) {
619 dhcp_client->timeout =
620 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
622 ipv4ll_defend_timeout,
627 dhcp_client->timeout =
628 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
630 ipv4ll_announce_timeout,
636 static void get_interface_mac_address(int index, uint8_t *mac_address)
641 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
643 perror("Open socket error");
647 memset(&ifr, 0, sizeof(ifr));
648 ifr.ifr_ifindex = index;
650 err = ioctl(sk, SIOCGIFNAME, &ifr);
652 perror("Get interface name error");
656 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
658 perror("Get mac address error");
662 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
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 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 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 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 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->request_list = NULL;
1230 dhcp_client->require_list = NULL;
1231 dhcp_client->duid = NULL;
1232 dhcp_client->duid_len = 0;
1233 dhcp_client->last_request = time(NULL);
1234 dhcp_client->expire = 0;
1235 dhcp_client->request_bcast = false;
1237 *error = G_DHCP_CLIENT_ERROR_NONE;
1242 g_free(dhcp_client->interface);
1243 g_free(dhcp_client);
1247 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1249 static int dhcp_l2_socket(int ifindex)
1252 struct sockaddr_ll sock;
1257 * I've selected not to see LL header, so BPF doesn't see it, too.
1258 * The filter may also pass non-IP and non-ARP packets, but we do
1259 * a more complete check when receiving the message in userspace.
1261 * and filter shamelessly stolen from:
1263 * http://www.flamewarmaster.de/software/dhcpclient/
1265 * There are a few other interesting ideas on that page (look under
1266 * "Motivation"). Use of netlink events is most interesting. Think
1267 * of various network servers listening for events and reconfiguring.
1268 * That would obsolete sending HUP signals and/or make use of restarts.
1270 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1273 * TODO: make conditional?
1275 static const struct sock_filter filter_instr[] = {
1277 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1278 /* L5, L1, is UDP? */
1279 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1280 /* ugly check for arp on ethernet-like and IPv4 */
1281 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1282 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1283 /* skip IP header */
1284 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1285 /* check udp source and destination ports */
1286 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1288 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1290 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1291 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1294 static const struct sock_fprog filter_prog = {
1295 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1296 /* casting const away: */
1297 .filter = (struct sock_filter *) filter_instr,
1300 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1304 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1305 /* Use only if standard ports are in use */
1306 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1307 sizeof(filter_prog));
1309 memset(&sock, 0, sizeof(sock));
1310 sock.sll_family = AF_PACKET;
1311 sock.sll_protocol = htons(ETH_P_IP);
1312 sock.sll_ifindex = ifindex;
1314 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1323 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1325 if (packet->ip.protocol != IPPROTO_UDP)
1328 if (packet->ip.version != IPVERSION)
1331 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1334 if (packet->udp.dest != htons(CLIENT_PORT))
1337 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1343 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1344 struct sockaddr_in *dst_addr)
1347 struct ip_udp_dhcp_packet packet;
1350 memset(&packet, 0, sizeof(packet));
1352 bytes = read(fd, &packet, sizeof(packet));
1356 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1359 if (bytes < ntohs(packet.ip.tot_len))
1360 /* packet is bigger than sizeof(packet), we did partial read */
1363 /* ignore any extra garbage bytes */
1364 bytes = ntohs(packet.ip.tot_len);
1366 if (!sanity_check(&packet, bytes))
1369 check = packet.ip.check;
1370 packet.ip.check = 0;
1371 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1374 /* verify UDP checksum. IP header has to be modified for this */
1375 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1376 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1377 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1378 check = packet.udp.check;
1379 packet.udp.check = 0;
1380 if (check && check != dhcp_checksum(&packet, bytes))
1383 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1384 sizeof(packet.udp)));
1386 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1389 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1391 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1394 static void ipv4ll_start(GDHCPClient *dhcp_client)
1398 remove_timeouts(dhcp_client);
1400 switch_listening_mode(dhcp_client, L_NONE);
1401 dhcp_client->retry_times = 0;
1402 dhcp_client->requested_ip = 0;
1404 dhcp_client->requested_ip = ipv4ll_random_ip();
1406 /*first wait a random delay to avoid storm of arp request on boot*/
1407 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1409 dhcp_client->retry_times++;
1410 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1417 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1420 switch_listening_mode(dhcp_client, L_NONE);
1422 remove_timeouts(dhcp_client);
1424 if (dhcp_client->listener_watch > 0) {
1425 g_source_remove(dhcp_client->listener_watch);
1426 dhcp_client->listener_watch = 0;
1429 dhcp_client->state = IPV4LL_PROBE;
1430 dhcp_client->retry_times = 0;
1431 dhcp_client->requested_ip = 0;
1433 g_free(dhcp_client->assigned_ip);
1434 dhcp_client->assigned_ip = NULL;
1437 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1440 struct ether_arp arp;
1441 uint32_t ip_requested;
1442 int source_conflict;
1443 int target_conflict;
1445 memset(&arp, 0, sizeof(arp));
1446 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1450 if (arp.arp_op != htons(ARPOP_REPLY) &&
1451 arp.arp_op != htons(ARPOP_REQUEST))
1454 ip_requested = htonl(dhcp_client->requested_ip);
1455 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1456 sizeof(ip_requested));
1458 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1459 sizeof(ip_requested));
1461 if (!source_conflict && !target_conflict)
1464 dhcp_client->conflicts++;
1466 debug(dhcp_client, "IPV4LL conflict detected");
1468 if (dhcp_client->state == IPV4LL_MONITOR) {
1469 if (!source_conflict)
1471 dhcp_client->state = IPV4LL_DEFEND;
1472 debug(dhcp_client, "DEFEND mode conflicts : %d",
1473 dhcp_client->conflicts);
1474 /*Try to defend with a single announce*/
1475 send_announce_packet(dhcp_client);
1479 if (dhcp_client->state == IPV4LL_DEFEND) {
1480 if (!source_conflict)
1482 else if (dhcp_client->ipv4ll_lost_cb)
1483 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1484 dhcp_client->ipv4ll_lost_data);
1487 ipv4ll_stop(dhcp_client);
1489 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1490 /*restart whole state machine*/
1491 dhcp_client->retry_times++;
1492 dhcp_client->timeout =
1493 g_timeout_add_full(G_PRIORITY_HIGH,
1494 ipv4ll_random_delay_ms(PROBE_WAIT),
1499 /* Here we got a lot of conflicts, RFC3927 states that we have
1500 * to wait RATE_LIMIT_INTERVAL before retrying,
1501 * but we just report failure.
1503 else if (dhcp_client->no_lease_cb)
1504 dhcp_client->no_lease_cb(dhcp_client,
1505 dhcp_client->no_lease_data);
1510 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1512 if (dhcp_client->type == G_DHCP_IPV6) {
1513 struct dhcpv6_packet *packet6 = pkt;
1519 xid = packet6->transaction_id[0] << 16 |
1520 packet6->transaction_id[1] << 8 |
1521 packet6->transaction_id[2];
1523 if (xid != dhcp_client->xid)
1526 struct dhcp_packet *packet = pkt;
1528 if (packet->xid != dhcp_client->xid)
1531 if (packet->hlen != 6)
1534 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1541 static void start_request(GDHCPClient *dhcp_client);
1543 static gboolean request_timeout(gpointer user_data)
1545 GDHCPClient *dhcp_client = user_data;
1547 #if defined TIZEN_EXT
1548 if (dhcp_client->init_reboot) {
1549 debug(dhcp_client, "DHCPREQUEST of INIT-REBOOT has failed");
1551 /* Start DHCPDISCOVERY when DHCPREQUEST of INIT-REBOOT has failed */
1552 g_dhcp_client_set_address_known(dhcp_client, FALSE);
1554 dhcp_client->retry_times = 0;
1555 dhcp_client->requested_ip = 0;
1557 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1562 debug(dhcp_client, "request timeout (retries %d)",
1563 dhcp_client->retry_times);
1565 dhcp_client->retry_times++;
1567 start_request(dhcp_client);
1572 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1573 gpointer user_data);
1575 static int switch_listening_mode(GDHCPClient *dhcp_client,
1576 ListenMode listen_mode)
1578 GIOChannel *listener_channel;
1579 int listener_sockfd;
1581 if (dhcp_client->listen_mode == listen_mode)
1584 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1585 dhcp_client->listen_mode, listen_mode);
1587 if (dhcp_client->listen_mode != L_NONE) {
1588 if (dhcp_client->listener_watch > 0)
1589 g_source_remove(dhcp_client->listener_watch);
1590 dhcp_client->listen_mode = L_NONE;
1591 dhcp_client->listener_sockfd = -1;
1592 dhcp_client->listener_watch = 0;
1595 if (listen_mode == L_NONE)
1598 if (listen_mode == L2)
1599 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1600 else if (listen_mode == L3) {
1601 if (dhcp_client->type == G_DHCP_IPV6)
1602 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1603 dhcp_client->interface,
1606 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1607 dhcp_client->interface,
1609 } else if (listen_mode == L_ARP)
1610 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1614 if (listener_sockfd < 0)
1617 listener_channel = g_io_channel_unix_new(listener_sockfd);
1618 if (!listener_channel) {
1619 /* Failed to create listener channel */
1620 close(listener_sockfd);
1624 dhcp_client->listen_mode = listen_mode;
1625 dhcp_client->listener_sockfd = listener_sockfd;
1627 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1628 dhcp_client->listener_watch =
1629 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1630 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1631 listener_event, dhcp_client,
1633 g_io_channel_unref(listener_channel);
1638 static void start_request(GDHCPClient *dhcp_client)
1640 debug(dhcp_client, "start request (retries %d)",
1641 dhcp_client->retry_times);
1643 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1644 if (dhcp_client->no_lease_cb)
1645 dhcp_client->no_lease_cb(dhcp_client,
1646 dhcp_client->no_lease_data);
1650 if (dhcp_client->retry_times == 0) {
1651 dhcp_client->state = REQUESTING;
1652 switch_listening_mode(dhcp_client, L2);
1655 send_request(dhcp_client);
1657 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1664 static uint32_t get_lease(struct dhcp_packet *packet)
1667 uint32_t lease_seconds;
1669 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1673 lease_seconds = get_be32(option);
1675 if (lease_seconds < 10)
1678 return lease_seconds;
1681 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1683 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1685 remove_timeouts(dhcp_client);
1687 dhcp_client->retry_times = retry_times;
1688 dhcp_client->requested_ip = 0;
1689 dhcp_client->state = INIT_SELECTING;
1690 switch_listening_mode(dhcp_client, L2);
1692 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1695 static gboolean start_expire(gpointer user_data)
1697 GDHCPClient *dhcp_client = user_data;
1699 debug(dhcp_client, "lease expired");
1701 /*remove all timeouts if they are set*/
1702 remove_timeouts(dhcp_client);
1704 restart_dhcp(dhcp_client, 0);
1706 /* ip need to be cleared */
1707 if (dhcp_client->lease_lost_cb)
1708 dhcp_client->lease_lost_cb(dhcp_client,
1709 dhcp_client->lease_lost_data);
1714 static gboolean continue_rebound(gpointer user_data)
1716 GDHCPClient *dhcp_client = user_data;
1719 switch_listening_mode(dhcp_client, L2);
1720 send_request(dhcp_client);
1722 if (dhcp_client->t2_timeout> 0) {
1723 g_source_remove(dhcp_client->t2_timeout);
1724 dhcp_client->t2_timeout = 0;
1727 /*recalculate remaining rebind time*/
1728 dhcp_client->T2 >>= 1;
1729 if (dhcp_client->T2 > 60) {
1730 dhcp_get_random(&rand);
1731 dhcp_client->t2_timeout =
1732 g_timeout_add_full(G_PRIORITY_HIGH,
1733 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1742 static gboolean start_rebound(gpointer user_data)
1744 GDHCPClient *dhcp_client = user_data;
1746 /*remove renew timer*/
1747 if (dhcp_client->t1_timeout > 0)
1748 g_source_remove(dhcp_client->t1_timeout);
1750 debug(dhcp_client, "start rebound");
1751 dhcp_client->state = REBINDING;
1753 /*calculate total rebind time*/
1754 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1756 /*send the first rebound and reschedule*/
1757 continue_rebound(user_data);
1762 static gboolean continue_renew (gpointer user_data)
1764 GDHCPClient *dhcp_client = user_data;
1767 switch_listening_mode(dhcp_client, L3);
1768 send_request(dhcp_client);
1770 if (dhcp_client->t1_timeout > 0)
1771 g_source_remove(dhcp_client->t1_timeout);
1773 dhcp_client->t1_timeout = 0;
1775 dhcp_client->T1 >>= 1;
1777 if (dhcp_client->T1 > 60) {
1778 dhcp_get_random(&rand);
1779 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1780 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1788 static gboolean start_renew(gpointer user_data)
1790 GDHCPClient *dhcp_client = user_data;
1792 debug(dhcp_client, "start renew");
1793 dhcp_client->state = RENEWING;
1795 /*calculate total renew period*/
1796 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1798 /*send first renew and reschedule for half the remaining time.*/
1799 continue_renew(user_data);
1804 static void start_bound(GDHCPClient *dhcp_client)
1806 debug(dhcp_client, "start bound");
1808 dhcp_client->state = BOUND;
1810 remove_timeouts(dhcp_client);
1813 *TODO: T1 and T2 should be set through options instead of
1814 * defaults as they are here.
1817 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1818 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1819 dhcp_client->expire = dhcp_client->lease_seconds;
1821 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1823 start_renew, dhcp_client,
1826 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1828 start_rebound, dhcp_client,
1831 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1832 dhcp_client->expire,
1833 start_expire, dhcp_client,
1837 static gboolean restart_dhcp_timeout(gpointer user_data)
1839 GDHCPClient *dhcp_client = user_data;
1841 debug(dhcp_client, "restart DHCP timeout");
1843 if (dhcp_client->state == REBOOTING) {
1844 g_free(dhcp_client->last_address);
1845 dhcp_client->last_address = NULL;
1846 restart_dhcp(dhcp_client, 0);
1848 dhcp_client->ack_retry_times++;
1849 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1854 static char *get_ip(uint32_t ip)
1856 struct in_addr addr;
1860 return g_strdup(inet_ntoa(addr));
1863 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1871 if (type == OPTION_STRING)
1872 return g_list_append(list, g_strdup(value));
1874 while ((pos = strchr(pos, ' '))) {
1877 list = g_list_append(list, g_strdup(value));
1882 list = g_list_append(list, g_strdup(value));
1887 static inline uint32_t get_uint32(unsigned char *value)
1889 return value[0] << 24 | value[1] << 16 |
1890 value[2] << 8 | value[3];
1893 static inline uint16_t get_uint16(unsigned char *value)
1895 return value[0] << 8 | value[1];
1898 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1899 struct in6_addr *addr,
1900 unsigned char prefixlen, uint32_t preferred,
1903 GDHCPIAPrefix *ia_prefix;
1905 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1909 if (dhcp_client->debug_func) {
1910 char addr_str[INET6_ADDRSTRLEN + 1];
1911 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1912 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1913 addr_str, prefixlen, preferred, valid);
1916 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1917 ia_prefix->prefixlen = prefixlen;
1918 ia_prefix->preferred = preferred;
1919 ia_prefix->valid = valid;
1920 ia_prefix->expire = time(NULL) + valid;
1922 return g_list_prepend(list, ia_prefix);
1925 static GList *get_addresses(GDHCPClient *dhcp_client,
1927 unsigned char *value,
1931 struct in6_addr addr;
1932 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1933 uint16_t option_len, option_code, st = 0, max_len;
1934 int addr_count = 0, prefix_count = 0, i, pos;
1935 unsigned char prefixlen;
1936 unsigned int shortest_valid = 0;
1940 if (!value || len < 4)
1943 iaid = get_uint32(&value[0]);
1944 if (dhcp_client->iaid != iaid)
1947 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
1948 T1 = get_uint32(&value[4]);
1949 T2 = get_uint32(&value[8]);
1952 /* IA_NA: RFC 3315, 22.4 */
1953 /* IA_PD: RFC 3633, ch 9 */
1963 max_len = len - pos;
1965 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
1967 /* We have more sub-options in this packet. */
1969 option = dhcpv6_get_sub_option(&value[pos], max_len,
1970 &option_code, &option_len);
1972 debug(dhcp_client, "pos %d option %p code %d len %d",
1973 pos, option, option_code, option_len);
1981 switch (option_code) {
1982 case G_DHCPV6_IAADDR:
1984 memcpy(&addr, &option[0], sizeof(addr));
1986 preferred = get_uint32(&option[i]);
1988 valid = get_uint32(&option[i]);
1993 case G_DHCPV6_STATUS_CODE:
1994 st = get_uint16(&option[0]);
1995 debug(dhcp_client, "error code %d", st);
1996 if (option_len > 2) {
1997 str = g_strndup((gchar *)&option[2],
1999 debug(dhcp_client, "error text: %s", str);
2006 case G_DHCPV6_IA_PREFIX:
2008 preferred = get_uint32(&option[i]);
2010 valid = get_uint32(&option[i]);
2012 prefixlen = option[i];
2014 memcpy(&addr, &option[i], sizeof(addr));
2016 if (preferred < valid) {
2017 /* RFC 3633, ch 10 */
2018 list = add_prefix(dhcp_client, list, &addr,
2019 prefixlen, preferred, valid);
2020 if (shortest_valid > valid)
2021 shortest_valid = valid;
2027 pos += 2 + 2 + option_len;
2029 } while (pos < len);
2031 if (addr_count > 0 && st == 0) {
2032 /* We only support one address atm */
2033 char addr_str[INET6_ADDRSTRLEN + 1];
2035 if (preferred > valid)
2036 /* RFC 3315, 22.6 */
2039 dhcp_client->T1 = T1;
2040 dhcp_client->T2 = T2;
2042 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2043 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2044 addr_count, addr_str, T1, T2);
2046 list = g_list_append(list, g_strdup(addr_str));
2048 if (code == G_DHCPV6_IA_NA)
2049 memcpy(&dhcp_client->ia_na, &addr,
2050 sizeof(struct in6_addr));
2052 memcpy(&dhcp_client->ia_ta, &addr,
2053 sizeof(struct in6_addr));
2055 if (valid != dhcp_client->expire)
2056 dhcp_client->expire = valid;
2059 if (prefix_count > 0 && list) {
2061 * This means we have a list of prefixes to delegate.
2063 list = g_list_reverse(list);
2065 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2066 prefix_count, T1, T2);
2068 dhcp_client->T1 = T1;
2069 dhcp_client->T2 = T2;
2071 dhcp_client->expire = shortest_valid;
2074 if (status && *status != 0)
2075 debug(dhcp_client, "status %d", *status);
2080 static GList *get_domains(int maxlen, unsigned char *value)
2086 char dns_name[NS_MAXDNAME + 1];
2088 if (!value || maxlen < 3)
2091 while (pos < maxlen) {
2092 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2094 c = (unsigned char *)dns_name;
2101 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2102 pos += (char *)c - dns_name + 1;
2105 return g_list_reverse(list);
2108 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2110 unsigned char *value,
2121 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2122 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2125 "%s server list length (%d) is invalid",
2126 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2130 for (i = 0; i < len; i += 16) {
2132 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2136 if (!inet_ntop(AF_INET6, &value[i], str,
2140 list = g_list_append(list, str);
2144 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2145 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2146 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2147 list = get_addresses(dhcp_client, code, len, value, status);
2150 case G_DHCPV6_DOMAIN_LIST:
2151 list = get_domains(len, value);
2161 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2162 struct dhcpv6_packet *packet,
2163 uint16_t pkt_len, uint16_t *status)
2165 GList *list, *value_list;
2168 uint16_t option_len;
2170 for (list = dhcp_client->request_list; list; list = list->next) {
2171 code = (uint16_t) GPOINTER_TO_INT(list->data);
2173 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2176 g_hash_table_remove(dhcp_client->code_value_hash,
2177 GINT_TO_POINTER((int) code));
2181 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2182 option_len, option, status);
2184 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2185 option_len, value_list);
2188 g_hash_table_remove(dhcp_client->code_value_hash,
2189 GINT_TO_POINTER((int) code));
2191 g_hash_table_insert(dhcp_client->code_value_hash,
2192 GINT_TO_POINTER((int) code), value_list);
2196 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2198 GDHCPOptionType type;
2199 GList *list, *value_list;
2204 for (list = dhcp_client->request_list; list; list = list->next) {
2205 code = (uint8_t) GPOINTER_TO_INT(list->data);
2207 option = dhcp_get_option(packet, code);
2209 g_hash_table_remove(dhcp_client->code_value_hash,
2210 GINT_TO_POINTER((int) code));
2214 type = dhcp_get_code_type(code);
2216 option_value = malloc_option_value_string(option, type);
2218 g_hash_table_remove(dhcp_client->code_value_hash,
2219 GINT_TO_POINTER((int) code));
2221 value_list = get_option_value_list(option_value, type);
2223 g_free(option_value);
2226 g_hash_table_remove(dhcp_client->code_value_hash,
2227 GINT_TO_POINTER((int) code));
2229 g_hash_table_insert(dhcp_client->code_value_hash,
2230 GINT_TO_POINTER((int) code), value_list);
2234 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2237 GDHCPClient *dhcp_client = user_data;
2238 struct sockaddr_in dst_addr = { 0 };
2239 struct dhcp_packet packet;
2240 struct dhcpv6_packet *packet6 = NULL;
2241 uint8_t *message_type = NULL, *client_id = NULL, *option,
2243 uint16_t option_len = 0, status = 0;
2246 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2247 uint16_t pkt_len = 0;
2251 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2252 dhcp_client->listener_watch = 0;
2256 if (dhcp_client->listen_mode == L_NONE)
2261 dhcp_client->status_code = 0;
2263 if (dhcp_client->listen_mode == L2) {
2264 re = dhcp_recv_l2_packet(&packet,
2265 dhcp_client->listener_sockfd,
2268 } else if (dhcp_client->listen_mode == L3) {
2269 if (dhcp_client->type == G_DHCP_IPV6) {
2270 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2271 dhcp_client->listener_sockfd);
2276 xid = packet6->transaction_id[0] << 16 |
2277 packet6->transaction_id[1] << 8 |
2278 packet6->transaction_id[2];
2280 re = dhcp_recv_l3_packet(&packet,
2281 dhcp_client->listener_sockfd);
2284 } else if (dhcp_client->listen_mode == L_ARP) {
2285 ipv4ll_recv_arp_packet(dhcp_client);
2293 if (!check_package_owner(dhcp_client, pkt))
2296 if (dhcp_client->type == G_DHCP_IPV6) {
2301 client_id = dhcpv6_get_option(packet6, pkt_len,
2302 G_DHCPV6_CLIENTID, &option_len, &count);
2304 if (!client_id || count == 0 || option_len == 0 ||
2305 memcmp(dhcp_client->duid, client_id,
2306 dhcp_client->duid_len) != 0) {
2308 "client duid error, discarding msg %p/%d/%d",
2309 client_id, option_len, count);
2313 option = dhcpv6_get_option(packet6, pkt_len,
2314 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2315 if (option != 0 && option_len > 0) {
2316 status = option[0]<<8 | option[1];
2318 debug(dhcp_client, "error code %d", status);
2319 if (option_len > 2) {
2320 gchar *txt = g_strndup(
2321 (gchar *)&option[2],
2323 debug(dhcp_client, "error text: %s",
2328 dhcp_client->status_code = status;
2331 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2336 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2337 "(current state %d)", ntohl(xid), dhcp_client->state);
2339 switch (dhcp_client->state) {
2340 case INIT_SELECTING:
2341 if (*message_type != DHCPOFFER)
2344 remove_timeouts(dhcp_client);
2345 dhcp_client->timeout = 0;
2346 dhcp_client->retry_times = 0;
2348 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2349 dhcp_client->server_ip = get_be32(option);
2350 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2352 dhcp_client->state = REQUESTING;
2354 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2355 dhcp_client->request_bcast = true;
2357 dhcp_client->request_bcast = false;
2359 debug(dhcp_client, "init ip %s -> %sadding broadcast flag",
2360 inet_ntoa(dst_addr.sin_addr),
2361 dhcp_client->request_bcast ? "" : "not ");
2363 start_request(dhcp_client);
2367 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2368 dhcp_client->request_bcast = true;
2370 dhcp_client->request_bcast = false;
2372 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2373 inet_ntoa(dst_addr.sin_addr),
2374 dhcp_client->request_bcast ? "" : "not ");
2379 if (*message_type == DHCPACK) {
2380 dhcp_client->retry_times = 0;
2382 remove_timeouts(dhcp_client);
2384 dhcp_client->lease_seconds = get_lease(&packet);
2386 #if defined TIZEN_EXT
2387 dhcp_client->dhcp_lease_seconds = dhcp_client->lease_seconds;
2390 get_request(dhcp_client, &packet);
2392 switch_listening_mode(dhcp_client, L_NONE);
2394 g_free(dhcp_client->assigned_ip);
2395 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2397 if (dhcp_client->state == REBOOTING) {
2398 option = dhcp_get_option(&packet,
2400 dhcp_client->server_ip = get_be32(option);
2403 /* Address should be set up here */
2404 if (dhcp_client->lease_available_cb)
2405 dhcp_client->lease_available_cb(dhcp_client,
2406 dhcp_client->lease_available_data);
2408 start_bound(dhcp_client);
2409 } else if (*message_type == DHCPNAK) {
2410 dhcp_client->retry_times = 0;
2412 remove_timeouts(dhcp_client);
2414 #if defined TIZEN_EXT
2415 if (dhcp_client->init_reboot) {
2416 g_dhcp_client_set_address_known(dhcp_client, FALSE);
2417 dhcp_client->timeout = g_idle_add_full(
2419 restart_dhcp_timeout,
2426 dhcp_client->timeout = g_timeout_add_seconds_full(
2428 restart_dhcp_timeout,
2435 if (dhcp_client->type != G_DHCP_IPV6)
2438 if (packet6->message != DHCPV6_REPLY &&
2439 packet6->message != DHCPV6_ADVERTISE)
2443 server_id = dhcpv6_get_option(packet6, pkt_len,
2444 G_DHCPV6_SERVERID, &option_len, &count);
2445 if (!server_id || count != 1 || option_len == 0) {
2446 /* RFC 3315, 15.10 */
2448 "server duid error, discarding msg %p/%d/%d",
2449 server_id, option_len, count);
2452 dhcp_client->server_duid = g_try_malloc(option_len);
2453 if (!dhcp_client->server_duid)
2455 memcpy(dhcp_client->server_duid, server_id, option_len);
2456 dhcp_client->server_duid_len = option_len;
2458 if (packet6->message == DHCPV6_REPLY) {
2459 uint8_t *rapid_commit;
2462 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2463 G_DHCPV6_RAPID_COMMIT,
2464 &option_len, &count);
2465 if (!rapid_commit || option_len != 0 ||
2467 /* RFC 3315, 17.1.4 */
2471 switch_listening_mode(dhcp_client, L_NONE);
2473 if (dhcp_client->status_code == 0)
2474 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2475 &dhcp_client->status_code);
2477 if (packet6->message == DHCPV6_ADVERTISE) {
2478 if (dhcp_client->advertise_cb)
2479 dhcp_client->advertise_cb(dhcp_client,
2480 dhcp_client->advertise_data);
2484 if (dhcp_client->solicitation_cb) {
2486 * The dhcp_client might not be valid after the
2487 * callback call so just return immediately.
2489 dhcp_client->solicitation_cb(dhcp_client,
2490 dhcp_client->solicitation_data);
2495 if (dhcp_client->type != G_DHCP_IPV6)
2498 server_id = dhcpv6_get_option(packet6, pkt_len,
2499 G_DHCPV6_SERVERID, &option_len, &count);
2500 if (!dhcp_client->server_duid && server_id &&
2503 * If we do not have server duid yet, then get it now.
2504 * Prefix delegation renew support needs it.
2506 dhcp_client->server_duid = g_try_malloc(option_len);
2507 if (!dhcp_client->server_duid)
2509 memcpy(dhcp_client->server_duid, server_id, option_len);
2510 dhcp_client->server_duid_len = option_len;
2513 case INFORMATION_REQ:
2519 if (dhcp_client->type != G_DHCP_IPV6)
2522 if (packet6->message != DHCPV6_REPLY)
2527 server_id = dhcpv6_get_option(packet6, pkt_len,
2528 G_DHCPV6_SERVERID, &option_len, &count);
2529 if (!server_id || count != 1 || option_len == 0 ||
2530 (dhcp_client->server_duid_len > 0 &&
2531 memcmp(dhcp_client->server_duid, server_id,
2532 dhcp_client->server_duid_len) != 0)) {
2533 /* RFC 3315, 15.10 */
2535 "server duid error, discarding msg %p/%d/%d",
2536 server_id, option_len, count);
2540 switch_listening_mode(dhcp_client, L_NONE);
2542 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2543 &dhcp_client->status_code);
2545 if (dhcp_client->information_req_cb) {
2547 * The dhcp_client might not be valid after the
2548 * callback call so just return immediately.
2550 dhcp_client->information_req_cb(dhcp_client,
2551 dhcp_client->information_req_data);
2554 if (dhcp_client->request_cb) {
2555 dhcp_client->request_cb(dhcp_client,
2556 dhcp_client->request_data);
2559 if (dhcp_client->renew_cb) {
2560 dhcp_client->renew_cb(dhcp_client,
2561 dhcp_client->renew_data);
2564 if (dhcp_client->rebind_cb) {
2565 dhcp_client->rebind_cb(dhcp_client,
2566 dhcp_client->rebind_data);
2569 if (dhcp_client->release_cb) {
2570 dhcp_client->release_cb(dhcp_client,
2571 dhcp_client->release_data);
2574 if (dhcp_client->decline_cb) {
2575 dhcp_client->decline_cb(dhcp_client,
2576 dhcp_client->decline_data);
2579 if (dhcp_client->confirm_cb) {
2581 server_id = dhcpv6_get_option(packet6, pkt_len,
2582 G_DHCPV6_SERVERID, &option_len,
2584 if (!server_id || count != 1 ||
2586 /* RFC 3315, 15.10 */
2588 "confirm server duid error, "
2589 "discarding msg %p/%d/%d",
2590 server_id, option_len, count);
2593 dhcp_client->server_duid = g_try_malloc(option_len);
2594 if (!dhcp_client->server_duid)
2596 memcpy(dhcp_client->server_duid, server_id, option_len);
2597 dhcp_client->server_duid_len = option_len;
2599 dhcp_client->confirm_cb(dhcp_client,
2600 dhcp_client->confirm_data);
2608 debug(dhcp_client, "processed DHCP packet (new state %d)",
2609 dhcp_client->state);
2614 static gboolean discover_timeout(gpointer user_data)
2616 GDHCPClient *dhcp_client = user_data;
2618 dhcp_client->retry_times++;
2621 * We do not send the REQUESTED IP option if we are retrying because
2622 * if the server is non-authoritative it will ignore the request if the
2623 * option is present.
2625 g_dhcp_client_start(dhcp_client, NULL);
2630 static gboolean reboot_timeout(gpointer user_data)
2632 GDHCPClient *dhcp_client = user_data;
2633 dhcp_client->retry_times = 0;
2634 dhcp_client->requested_ip = 0;
2635 dhcp_client->state = INIT_SELECTING;
2637 * We do not send the REQUESTED IP option because the server didn't
2638 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2641 g_dhcp_client_start(dhcp_client, NULL);
2646 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2648 GDHCPClient *dhcp_client = dhcp_data;
2650 debug(dhcp_client, "back to MONITOR mode");
2652 dhcp_client->conflicts = 0;
2653 dhcp_client->state = IPV4LL_MONITOR;
2658 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2660 GDHCPClient *dhcp_client = dhcp_data;
2663 #if defined TIZEN_EXT
2668 debug(dhcp_client, "request timeout (retries %d)",
2669 dhcp_client->retry_times);
2671 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2672 dhcp_client->retry_times++;
2673 send_announce_packet(dhcp_client);
2677 ip = htonl(dhcp_client->requested_ip);
2678 debug(dhcp_client, "switching to monitor mode");
2679 dhcp_client->state = IPV4LL_MONITOR;
2680 dhcp_client->assigned_ip = get_ip(ip);
2682 if (dhcp_client->ipv4ll_available_cb)
2683 dhcp_client->ipv4ll_available_cb(dhcp_client,
2684 dhcp_client->ipv4ll_available_data);
2685 dhcp_client->conflicts = 0;
2686 dhcp_client->timeout = 0;
2691 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2694 GDHCPClient *dhcp_client = dhcp_data;
2696 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2697 dhcp_client->retry_times);
2699 if (dhcp_client->retry_times == PROBE_NUM) {
2700 dhcp_client->state = IPV4LL_ANNOUNCE;
2701 dhcp_client->retry_times = 0;
2703 dhcp_client->retry_times++;
2704 send_announce_packet(dhcp_client);
2707 dhcp_client->retry_times++;
2708 send_probe_packet(dhcp_client);
2713 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2719 #if defined TIZEN_EXT
2720 int discover_retry = 0;
2724 remove_timeouts(dhcp_client);
2726 if (dhcp_client->type == G_DHCP_IPV6) {
2727 if (dhcp_client->information_req_cb) {
2728 dhcp_client->state = INFORMATION_REQ;
2729 re = switch_listening_mode(dhcp_client, L3);
2731 switch_listening_mode(dhcp_client, L_NONE);
2732 dhcp_client->state = 0;
2735 send_information_req(dhcp_client);
2737 } else if (dhcp_client->solicitation_cb) {
2738 dhcp_client->state = SOLICITATION;
2739 re = switch_listening_mode(dhcp_client, L3);
2741 switch_listening_mode(dhcp_client, L_NONE);
2742 dhcp_client->state = 0;
2745 send_solicitation(dhcp_client);
2747 } else if (dhcp_client->request_cb) {
2748 dhcp_client->state = REQUEST;
2749 re = switch_listening_mode(dhcp_client, L3);
2751 switch_listening_mode(dhcp_client, L_NONE);
2752 dhcp_client->state = 0;
2755 send_dhcpv6_request(dhcp_client);
2757 } else if (dhcp_client->confirm_cb) {
2758 dhcp_client->state = CONFIRM;
2759 re = switch_listening_mode(dhcp_client, L3);
2761 switch_listening_mode(dhcp_client, L_NONE);
2762 dhcp_client->state = 0;
2765 send_dhcpv6_confirm(dhcp_client);
2767 } else if (dhcp_client->renew_cb) {
2768 dhcp_client->state = RENEW;
2769 re = switch_listening_mode(dhcp_client, L3);
2771 switch_listening_mode(dhcp_client, L_NONE);
2772 dhcp_client->state = 0;
2775 send_dhcpv6_renew(dhcp_client);
2777 } else if (dhcp_client->rebind_cb) {
2778 dhcp_client->state = REBIND;
2779 re = switch_listening_mode(dhcp_client, L3);
2781 switch_listening_mode(dhcp_client, L_NONE);
2782 dhcp_client->state = 0;
2785 send_dhcpv6_rebind(dhcp_client);
2787 } else if (dhcp_client->release_cb) {
2788 dhcp_client->state = RENEW;
2789 re = switch_listening_mode(dhcp_client, L3);
2791 switch_listening_mode(dhcp_client, L_NONE);
2792 dhcp_client->state = 0;
2795 send_dhcpv6_release(dhcp_client);
2796 } else if (dhcp_client->decline_cb) {
2797 dhcp_client->state = DECLINE;
2798 re = switch_listening_mode(dhcp_client, L3);
2800 switch_listening_mode(dhcp_client, L_NONE);
2801 dhcp_client->state = 0;
2804 send_dhcpv6_decline(dhcp_client);
2810 #if defined TIZEN_EXT
2811 if (g_ascii_strncasecmp(dhcp_client->interface, "wlan", 4)
2813 discover_retry = DISCOVER_RETRIES_WIFI;
2814 timeout = DISCOVER_TIMEOUT_WIFI;
2816 discover_retry = DISCOVER_RETRIES;
2817 timeout = DISCOVER_TIMEOUT;
2820 debug(dhcp_client, "[DHCPC] Discover retry/total : [%d]/[%d] timeout [%d]",
2821 dhcp_client->retry_times, discover_retry, timeout);
2825 if (dhcp_client->type == G_DHCP_IPV4LL) {
2826 dhcp_client->state = INIT_SELECTING;
2827 ipv4ll_start(dhcp_client);
2831 #if defined TIZEN_EXT
2832 if (dhcp_client->retry_times == discover_retry) {
2834 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2836 if (dhcp_client->no_lease_cb)
2837 dhcp_client->no_lease_cb(dhcp_client,
2838 dhcp_client->no_lease_data);
2839 dhcp_client->retry_times = 0;
2843 if (dhcp_client->retry_times == 0) {
2844 g_free(dhcp_client->assigned_ip);
2845 dhcp_client->assigned_ip = NULL;
2847 dhcp_client->state = INIT_SELECTING;
2848 re = switch_listening_mode(dhcp_client, L2);
2852 dhcp_get_random(&rand);
2853 dhcp_client->xid = rand;
2854 dhcp_client->start = time(NULL);
2857 if (!last_address) {
2860 addr = ntohl(inet_addr(last_address));
2861 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2864 } else if (dhcp_client->last_address != last_address) {
2865 g_free(dhcp_client->last_address);
2866 dhcp_client->last_address = g_strdup(last_address);
2870 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2871 debug(dhcp_client, "DHCP client start with state init_reboot");
2872 dhcp_client->requested_ip = addr;
2873 dhcp_client->state = REBOOTING;
2874 send_request(dhcp_client);
2876 dhcp_client->timeout = g_timeout_add_seconds_full(
2878 #if defined TIZEN_EXT
2888 send_discover(dhcp_client, addr);
2890 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2891 #if defined TIZEN_EXT
2902 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2904 switch_listening_mode(dhcp_client, L_NONE);
2906 if (dhcp_client->state == BOUND ||
2907 dhcp_client->state == RENEWING ||
2908 dhcp_client->state == REBINDING)
2909 send_release(dhcp_client, dhcp_client->server_ip,
2910 dhcp_client->requested_ip);
2912 remove_timeouts(dhcp_client);
2914 if (dhcp_client->listener_watch > 0) {
2915 g_source_remove(dhcp_client->listener_watch);
2916 dhcp_client->listener_watch = 0;
2919 dhcp_client->retry_times = 0;
2920 dhcp_client->ack_retry_times = 0;
2922 dhcp_client->requested_ip = 0;
2923 dhcp_client->state = RELEASED;
2924 dhcp_client->lease_seconds = 0;
2925 dhcp_client->request_bcast = false;
2928 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2929 unsigned char option_code)
2931 return g_hash_table_lookup(dhcp_client->code_value_hash,
2932 GINT_TO_POINTER((int) option_code));
2935 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2936 GDHCPClientEvent event,
2937 GDHCPClientEventFunc func,
2941 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2942 dhcp_client->lease_available_cb = func;
2943 dhcp_client->lease_available_data = data;
2945 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2946 if (dhcp_client->type == G_DHCP_IPV6)
2948 dhcp_client->ipv4ll_available_cb = func;
2949 dhcp_client->ipv4ll_available_data = data;
2951 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2952 dhcp_client->no_lease_cb = func;
2953 dhcp_client->no_lease_data = data;
2955 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2956 dhcp_client->lease_lost_cb = func;
2957 dhcp_client->lease_lost_data = data;
2959 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2960 if (dhcp_client->type == G_DHCP_IPV6)
2962 dhcp_client->ipv4ll_lost_cb = func;
2963 dhcp_client->ipv4ll_lost_data = data;
2965 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2966 dhcp_client->address_conflict_cb = func;
2967 dhcp_client->address_conflict_data = data;
2969 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2970 if (dhcp_client->type != G_DHCP_IPV6)
2972 dhcp_client->information_req_cb = func;
2973 dhcp_client->information_req_data = data;
2975 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2976 if (dhcp_client->type != G_DHCP_IPV6)
2978 dhcp_client->solicitation_cb = func;
2979 dhcp_client->solicitation_data = data;
2981 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2982 if (dhcp_client->type != G_DHCP_IPV6)
2984 dhcp_client->advertise_cb = func;
2985 dhcp_client->advertise_data = data;
2987 case G_DHCP_CLIENT_EVENT_REQUEST:
2988 if (dhcp_client->type != G_DHCP_IPV6)
2990 dhcp_client->request_cb = func;
2991 dhcp_client->request_data = data;
2993 case G_DHCP_CLIENT_EVENT_RENEW:
2994 if (dhcp_client->type != G_DHCP_IPV6)
2996 dhcp_client->renew_cb = func;
2997 dhcp_client->renew_data = data;
2999 case G_DHCP_CLIENT_EVENT_REBIND:
3000 if (dhcp_client->type != G_DHCP_IPV6)
3002 dhcp_client->rebind_cb = func;
3003 dhcp_client->rebind_data = data;
3005 case G_DHCP_CLIENT_EVENT_RELEASE:
3006 if (dhcp_client->type != G_DHCP_IPV6)
3008 dhcp_client->release_cb = func;
3009 dhcp_client->release_data = data;
3011 case G_DHCP_CLIENT_EVENT_CONFIRM:
3012 if (dhcp_client->type != G_DHCP_IPV6)
3014 dhcp_client->confirm_cb = func;
3015 dhcp_client->confirm_data = data;
3017 case G_DHCP_CLIENT_EVENT_DECLINE:
3018 if (dhcp_client->type != G_DHCP_IPV6)
3020 dhcp_client->decline_cb = func;
3021 dhcp_client->decline_data = data;
3026 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
3028 return dhcp_client->ifindex;
3031 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
3036 #if defined TIZEN_EXT
3037 return get_ip(htonl(dhcp_client->server_ip));
3039 return get_ip(dhcp_client->server_ip);
3043 #if defined TIZEN_EXT
3044 int g_dhcp_client_get_dhcp_lease_duration(GDHCPClient *dhcp_client)
3046 return dhcp_client->dhcp_lease_seconds;
3050 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
3052 return g_strdup(dhcp_client->assigned_ip);
3055 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
3057 GList *option = NULL;
3059 if (dhcp_client->type == G_DHCP_IPV6)
3062 switch (dhcp_client->state) {
3064 case IPV4LL_MONITOR:
3065 return g_strdup("255.255.0.0");
3069 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3071 return g_strdup(option->data);
3072 case INIT_SELECTING:
3077 case IPV4LL_ANNOUNCE:
3078 case INFORMATION_REQ:
3091 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3092 unsigned int option_code)
3094 if (!g_list_find(dhcp_client->request_list,
3095 GINT_TO_POINTER((int)option_code)))
3096 dhcp_client->request_list = g_list_prepend(
3097 dhcp_client->request_list,
3098 (GINT_TO_POINTER((int) option_code)));
3100 return G_DHCP_CLIENT_ERROR_NONE;
3103 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3105 g_list_free(dhcp_client->request_list);
3106 dhcp_client->request_list = NULL;
3109 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3111 g_hash_table_remove_all(dhcp_client->send_value_hash);
3114 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3118 storage = g_try_malloc(size + OPT_DATA);
3122 storage[OPT_CODE] = code;
3123 storage[OPT_LEN] = size;
3124 memcpy(&storage[OPT_DATA], data, size);
3129 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3132 return alloc_dhcp_option(code, data, MIN(size, 255));
3135 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3137 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3140 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3142 const unsigned maclen = 6;
3143 const unsigned idlen = maclen + 1;
3144 const uint8_t option_code = G_DHCP_CLIENT_ID;
3145 uint8_t idbuf[idlen];
3146 uint8_t *data_option;
3148 idbuf[0] = ARPHRD_ETHER;
3150 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3152 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3154 return G_DHCP_CLIENT_ERROR_NOMEM;
3156 g_hash_table_insert(dhcp_client->send_value_hash,
3157 GINT_TO_POINTER((int) option_code), data_option);
3159 return G_DHCP_CLIENT_ERROR_NONE;
3162 /* Now only support send hostname and vendor class ID */
3163 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3164 unsigned char option_code, const char *option_value)
3166 uint8_t *binary_option;
3168 if ((option_code == G_DHCP_HOST_NAME ||
3169 option_code == G_DHCP_VENDOR_CLASS_ID) && option_value) {
3170 binary_option = alloc_dhcp_string_option(option_code,
3173 return G_DHCP_CLIENT_ERROR_NOMEM;
3175 g_hash_table_insert(dhcp_client->send_value_hash,
3176 GINT_TO_POINTER((int) option_code), binary_option);
3179 return G_DHCP_CLIENT_ERROR_NONE;
3182 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3187 storage = g_malloc(2 + 2 + len);
3191 storage[0] = code >> 8;
3192 storage[1] = code & 0xff;
3193 storage[2] = len >> 8;
3194 storage[3] = len & 0xff;
3195 memcpy(storage + 2 + 2, option, len);
3200 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3202 return g_hash_table_remove(dhcp_client->send_value_hash,
3203 GINT_TO_POINTER((int)code));
3206 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3207 uint16_t option_code,
3208 uint8_t *option_value,
3209 uint16_t option_len)
3212 uint8_t *binary_option;
3214 debug(dhcp_client, "setting option %d to %p len %d",
3215 option_code, option_value, option_len);
3217 binary_option = alloc_dhcpv6_option(option_code, option_value,
3220 g_hash_table_insert(dhcp_client->send_value_hash,
3221 GINT_TO_POINTER((int) option_code),
3226 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3228 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3231 dhcp_client->last_request = time(NULL);
3234 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3236 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3239 return dhcp_client->status_code;
3242 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3247 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3252 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3257 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3260 g_dhcp_client_stop(dhcp_client);
3262 g_free(dhcp_client->interface);
3263 g_free(dhcp_client->assigned_ip);
3264 g_free(dhcp_client->last_address);
3265 g_free(dhcp_client->duid);
3266 g_free(dhcp_client->server_duid);
3268 g_list_free(dhcp_client->request_list);
3269 g_list_free(dhcp_client->require_list);
3271 g_hash_table_destroy(dhcp_client->code_value_hash);
3272 g_hash_table_destroy(dhcp_client->send_value_hash);
3274 g_free(dhcp_client);
3275 #if defined TIZEN_EXT
3280 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3281 GDHCPDebugFunc func, gpointer user_data)
3286 dhcp_client->debug_func = func;
3287 dhcp_client->debug_data = user_data;
3290 static GDHCPIAPrefix *copy_prefix(gpointer data)
3292 GDHCPIAPrefix *copy, *prefix = data;
3294 copy = g_try_new(GDHCPIAPrefix, 1);
3298 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3303 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3305 GSList *copy = NULL;
3308 for (list = prefixes; list; list = list->next)
3309 copy = g_slist_prepend(copy, copy_prefix(list->data));
3314 #if defined TIZEN_EXT
3315 void g_dhcp_client_set_address_known(GDHCPClient *dhcp_client, gboolean known)
3317 /* DHCPREQUEST during INIT-REBOOT state (rfc2131)
3318 * 4.4.3 Initialization with known network address
3319 * 4.3.2 DHCPREQUEST generated during INIT-REBOOT state
3321 debug(dhcp_client, "known network address (%d)", known);
3323 if (dhcp_client->init_reboot == known)
3326 dhcp_client->init_reboot = known;