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, htons(ETH_P_IP));
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);
1674 /* paranoia: must not be prone to overflows */
1675 lease_seconds &= 0x0fffffff;
1676 if (lease_seconds < 10)
1679 return lease_seconds;
1682 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1684 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1686 remove_timeouts(dhcp_client);
1688 dhcp_client->retry_times = retry_times;
1689 dhcp_client->requested_ip = 0;
1690 dhcp_client->state = INIT_SELECTING;
1691 switch_listening_mode(dhcp_client, L2);
1693 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1696 static gboolean start_expire(gpointer user_data)
1698 GDHCPClient *dhcp_client = user_data;
1700 debug(dhcp_client, "lease expired");
1702 /*remove all timeouts if they are set*/
1703 remove_timeouts(dhcp_client);
1705 restart_dhcp(dhcp_client, 0);
1707 /* ip need to be cleared */
1708 if (dhcp_client->lease_lost_cb)
1709 dhcp_client->lease_lost_cb(dhcp_client,
1710 dhcp_client->lease_lost_data);
1715 static gboolean continue_rebound(gpointer user_data)
1717 GDHCPClient *dhcp_client = user_data;
1720 switch_listening_mode(dhcp_client, L2);
1721 send_request(dhcp_client);
1723 if (dhcp_client->t2_timeout> 0)
1724 g_source_remove(dhcp_client->t2_timeout);
1726 /*recalculate remaining rebind time*/
1727 dhcp_client->T2 >>= 1;
1728 if (dhcp_client->T2 > 60) {
1729 dhcp_get_random(&rand);
1730 dhcp_client->t2_timeout =
1731 g_timeout_add_full(G_PRIORITY_HIGH,
1732 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1741 static gboolean start_rebound(gpointer user_data)
1743 GDHCPClient *dhcp_client = user_data;
1745 /*remove renew timer*/
1746 if (dhcp_client->t1_timeout > 0)
1747 g_source_remove(dhcp_client->t1_timeout);
1749 debug(dhcp_client, "start rebound");
1750 dhcp_client->state = REBINDING;
1752 /*calculate total rebind time*/
1753 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1755 /*send the first rebound and reschedule*/
1756 continue_rebound(user_data);
1761 static gboolean continue_renew (gpointer user_data)
1763 GDHCPClient *dhcp_client = user_data;
1766 switch_listening_mode(dhcp_client, L3);
1767 send_request(dhcp_client);
1769 if (dhcp_client->t1_timeout > 0)
1770 g_source_remove(dhcp_client->t1_timeout);
1772 dhcp_client->t1_timeout = 0;
1774 dhcp_client->T1 >>= 1;
1776 if (dhcp_client->T1 > 60) {
1777 dhcp_get_random(&rand);
1778 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1779 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1787 static gboolean start_renew(gpointer user_data)
1789 GDHCPClient *dhcp_client = user_data;
1791 debug(dhcp_client, "start renew");
1792 dhcp_client->state = RENEWING;
1794 /*calculate total renew period*/
1795 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1797 /*send first renew and reschedule for half the remaining time.*/
1798 continue_renew(user_data);
1803 static void start_bound(GDHCPClient *dhcp_client)
1805 debug(dhcp_client, "start bound");
1807 dhcp_client->state = BOUND;
1809 remove_timeouts(dhcp_client);
1812 *TODO: T1 and T2 should be set through options instead of
1813 * defaults as they are here.
1816 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1817 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1818 dhcp_client->expire = dhcp_client->lease_seconds;
1820 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1822 start_renew, dhcp_client,
1825 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1827 start_rebound, dhcp_client,
1830 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1831 dhcp_client->expire,
1832 start_expire, dhcp_client,
1836 static gboolean restart_dhcp_timeout(gpointer user_data)
1838 GDHCPClient *dhcp_client = user_data;
1840 debug(dhcp_client, "restart DHCP timeout");
1842 if (dhcp_client->state == REBOOTING) {
1843 g_free(dhcp_client->last_address);
1844 dhcp_client->last_address = NULL;
1845 restart_dhcp(dhcp_client, 0);
1847 dhcp_client->ack_retry_times++;
1848 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1853 static char *get_ip(uint32_t ip)
1855 struct in_addr addr;
1859 return g_strdup(inet_ntoa(addr));
1862 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1870 if (type == OPTION_STRING)
1871 return g_list_append(list, g_strdup(value));
1873 while ((pos = strchr(pos, ' '))) {
1876 list = g_list_append(list, g_strdup(value));
1881 list = g_list_append(list, g_strdup(value));
1886 static inline uint32_t get_uint32(unsigned char *value)
1888 return value[0] << 24 | value[1] << 16 |
1889 value[2] << 8 | value[3];
1892 static inline uint16_t get_uint16(unsigned char *value)
1894 return value[0] << 8 | value[1];
1897 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1898 struct in6_addr *addr,
1899 unsigned char prefixlen, uint32_t preferred,
1902 GDHCPIAPrefix *ia_prefix;
1904 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1908 if (dhcp_client->debug_func) {
1909 char addr_str[INET6_ADDRSTRLEN + 1];
1910 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1911 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1912 addr_str, prefixlen, preferred, valid);
1915 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1916 ia_prefix->prefixlen = prefixlen;
1917 ia_prefix->preferred = preferred;
1918 ia_prefix->valid = valid;
1919 ia_prefix->expire = time(NULL) + valid;
1921 return g_list_prepend(list, ia_prefix);
1924 static GList *get_addresses(GDHCPClient *dhcp_client,
1926 unsigned char *value,
1930 struct in6_addr addr;
1931 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1932 uint16_t option_len, option_code, st = 0, max_len;
1933 int addr_count = 0, prefix_count = 0, i, pos;
1934 unsigned char prefixlen;
1935 unsigned int shortest_valid = 0;
1939 if (!value || len < 4)
1942 iaid = get_uint32(&value[0]);
1943 if (dhcp_client->iaid != iaid)
1946 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
1947 T1 = get_uint32(&value[4]);
1948 T2 = get_uint32(&value[8]);
1951 /* IA_NA: RFC 3315, 22.4 */
1952 /* IA_PD: RFC 3633, ch 9 */
1962 max_len = len - pos;
1964 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
1966 /* We have more sub-options in this packet. */
1968 option = dhcpv6_get_sub_option(&value[pos], max_len,
1969 &option_code, &option_len);
1971 debug(dhcp_client, "pos %d option %p code %d len %d",
1972 pos, option, option_code, option_len);
1980 switch (option_code) {
1981 case G_DHCPV6_IAADDR:
1983 memcpy(&addr, &option[0], sizeof(addr));
1985 preferred = get_uint32(&option[i]);
1987 valid = get_uint32(&option[i]);
1992 case G_DHCPV6_STATUS_CODE:
1993 st = get_uint16(&option[0]);
1994 debug(dhcp_client, "error code %d", st);
1995 if (option_len > 2) {
1996 str = g_strndup((gchar *)&option[2],
1998 debug(dhcp_client, "error text: %s", str);
2005 case G_DHCPV6_IA_PREFIX:
2007 preferred = get_uint32(&option[i]);
2009 valid = get_uint32(&option[i]);
2011 prefixlen = option[i];
2013 memcpy(&addr, &option[i], sizeof(addr));
2015 if (preferred < valid) {
2016 /* RFC 3633, ch 10 */
2017 list = add_prefix(dhcp_client, list, &addr,
2018 prefixlen, preferred, valid);
2019 if (shortest_valid > valid)
2020 shortest_valid = valid;
2026 pos += 2 + 2 + option_len;
2028 } while (pos < len);
2030 if (addr_count > 0 && st == 0) {
2031 /* We only support one address atm */
2032 char addr_str[INET6_ADDRSTRLEN + 1];
2034 if (preferred > valid)
2035 /* RFC 3315, 22.6 */
2038 dhcp_client->T1 = T1;
2039 dhcp_client->T2 = T2;
2041 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2042 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2043 addr_count, addr_str, T1, T2);
2045 list = g_list_append(list, g_strdup(addr_str));
2047 if (code == G_DHCPV6_IA_NA)
2048 memcpy(&dhcp_client->ia_na, &addr,
2049 sizeof(struct in6_addr));
2051 memcpy(&dhcp_client->ia_ta, &addr,
2052 sizeof(struct in6_addr));
2054 if (valid != dhcp_client->expire)
2055 dhcp_client->expire = valid;
2058 if (prefix_count > 0 && list) {
2060 * This means we have a list of prefixes to delegate.
2062 list = g_list_reverse(list);
2064 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2065 prefix_count, T1, T2);
2067 dhcp_client->T1 = T1;
2068 dhcp_client->T2 = T2;
2070 dhcp_client->expire = shortest_valid;
2073 if (status && *status != 0)
2074 debug(dhcp_client, "status %d", *status);
2079 static GList *get_domains(int maxlen, unsigned char *value)
2085 char dns_name[NS_MAXDNAME + 1];
2087 if (!value || maxlen < 3)
2090 while (pos < maxlen) {
2091 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2093 c = (unsigned char *)dns_name;
2100 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2101 pos += (char *)c - dns_name + 1;
2104 return g_list_reverse(list);
2107 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2109 unsigned char *value,
2120 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2121 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2124 "%s server list length (%d) is invalid",
2125 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2129 for (i = 0; i < len; i += 16) {
2131 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2135 if (!inet_ntop(AF_INET6, &value[i], str,
2139 list = g_list_append(list, str);
2143 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2144 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2145 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2146 list = get_addresses(dhcp_client, code, len, value, status);
2149 case G_DHCPV6_DOMAIN_LIST:
2150 list = get_domains(len, value);
2160 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2161 struct dhcpv6_packet *packet,
2162 uint16_t pkt_len, uint16_t *status)
2164 GList *list, *value_list;
2167 uint16_t option_len;
2169 for (list = dhcp_client->request_list; list; list = list->next) {
2170 code = (uint16_t) GPOINTER_TO_INT(list->data);
2172 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2175 g_hash_table_remove(dhcp_client->code_value_hash,
2176 GINT_TO_POINTER((int) code));
2180 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2181 option_len, option, status);
2183 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2184 option_len, value_list);
2187 g_hash_table_remove(dhcp_client->code_value_hash,
2188 GINT_TO_POINTER((int) code));
2190 g_hash_table_insert(dhcp_client->code_value_hash,
2191 GINT_TO_POINTER((int) code), value_list);
2195 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2197 GDHCPOptionType type;
2198 GList *list, *value_list;
2203 for (list = dhcp_client->request_list; list; list = list->next) {
2204 code = (uint8_t) GPOINTER_TO_INT(list->data);
2206 option = dhcp_get_option(packet, code);
2208 g_hash_table_remove(dhcp_client->code_value_hash,
2209 GINT_TO_POINTER((int) code));
2213 type = dhcp_get_code_type(code);
2215 option_value = malloc_option_value_string(option, type);
2217 g_hash_table_remove(dhcp_client->code_value_hash,
2218 GINT_TO_POINTER((int) code));
2220 value_list = get_option_value_list(option_value, type);
2222 g_free(option_value);
2225 g_hash_table_remove(dhcp_client->code_value_hash,
2226 GINT_TO_POINTER((int) code));
2228 g_hash_table_insert(dhcp_client->code_value_hash,
2229 GINT_TO_POINTER((int) code), value_list);
2233 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2236 GDHCPClient *dhcp_client = user_data;
2237 struct sockaddr_in dst_addr = { 0 };
2238 struct dhcp_packet packet;
2239 struct dhcpv6_packet *packet6 = NULL;
2240 uint8_t *message_type = NULL, *client_id = NULL, *option,
2242 uint16_t option_len = 0, status = 0;
2245 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2246 uint16_t pkt_len = 0;
2250 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2251 dhcp_client->listener_watch = 0;
2255 if (dhcp_client->listen_mode == L_NONE)
2260 dhcp_client->status_code = 0;
2262 if (dhcp_client->listen_mode == L2) {
2263 re = dhcp_recv_l2_packet(&packet,
2264 dhcp_client->listener_sockfd,
2267 } else if (dhcp_client->listen_mode == L3) {
2268 if (dhcp_client->type == G_DHCP_IPV6) {
2269 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2270 dhcp_client->listener_sockfd);
2273 xid = packet6->transaction_id[0] << 16 |
2274 packet6->transaction_id[1] << 8 |
2275 packet6->transaction_id[2];
2277 re = dhcp_recv_l3_packet(&packet,
2278 dhcp_client->listener_sockfd);
2281 } else if (dhcp_client->listen_mode == L_ARP) {
2282 ipv4ll_recv_arp_packet(dhcp_client);
2290 if (!check_package_owner(dhcp_client, pkt))
2293 if (dhcp_client->type == G_DHCP_IPV6) {
2298 client_id = dhcpv6_get_option(packet6, pkt_len,
2299 G_DHCPV6_CLIENTID, &option_len, &count);
2301 if (!client_id || count == 0 || option_len == 0 ||
2302 memcmp(dhcp_client->duid, client_id,
2303 dhcp_client->duid_len) != 0) {
2305 "client duid error, discarding msg %p/%d/%d",
2306 client_id, option_len, count);
2310 option = dhcpv6_get_option(packet6, pkt_len,
2311 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2312 if (option != 0 && option_len > 0) {
2313 status = option[0]<<8 | option[1];
2315 debug(dhcp_client, "error code %d", status);
2316 if (option_len > 2) {
2317 gchar *txt = g_strndup(
2318 (gchar *)&option[2],
2320 debug(dhcp_client, "error text: %s",
2325 dhcp_client->status_code = status;
2328 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2333 if (!message_type && !client_id)
2334 /* No message type / client id option, ignore package */
2337 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2338 "(current state %d)", ntohl(xid), dhcp_client->state);
2340 switch (dhcp_client->state) {
2341 case INIT_SELECTING:
2342 if (*message_type != DHCPOFFER)
2345 remove_timeouts(dhcp_client);
2346 dhcp_client->timeout = 0;
2347 dhcp_client->retry_times = 0;
2349 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2350 dhcp_client->server_ip = get_be32(option);
2351 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2353 dhcp_client->state = REQUESTING;
2355 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2356 dhcp_client->request_bcast = true;
2358 dhcp_client->request_bcast = false;
2360 debug(dhcp_client, "init ip %s -> %sadding broadcast flag",
2361 inet_ntoa(dst_addr.sin_addr),
2362 dhcp_client->request_bcast ? "" : "not ");
2364 start_request(dhcp_client);
2368 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2369 dhcp_client->request_bcast = true;
2371 dhcp_client->request_bcast = false;
2373 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2374 inet_ntoa(dst_addr.sin_addr),
2375 dhcp_client->request_bcast ? "" : "not ");
2380 if (*message_type == DHCPACK) {
2381 dhcp_client->retry_times = 0;
2383 remove_timeouts(dhcp_client);
2385 dhcp_client->lease_seconds = get_lease(&packet);
2387 #if defined TIZEN_EXT
2388 dhcp_client->dhcp_lease_seconds = dhcp_client->lease_seconds;
2391 get_request(dhcp_client, &packet);
2393 switch_listening_mode(dhcp_client, L_NONE);
2395 g_free(dhcp_client->assigned_ip);
2396 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2398 if (dhcp_client->state == REBOOTING) {
2399 option = dhcp_get_option(&packet,
2401 dhcp_client->server_ip = get_be32(option);
2404 /* Address should be set up here */
2405 if (dhcp_client->lease_available_cb)
2406 dhcp_client->lease_available_cb(dhcp_client,
2407 dhcp_client->lease_available_data);
2409 start_bound(dhcp_client);
2410 } else if (*message_type == DHCPNAK) {
2411 dhcp_client->retry_times = 0;
2413 remove_timeouts(dhcp_client);
2415 #if defined TIZEN_EXT
2416 if (dhcp_client->init_reboot) {
2417 g_dhcp_client_set_address_known(dhcp_client, FALSE);
2418 dhcp_client->timeout = g_idle_add_full(
2420 restart_dhcp_timeout,
2427 dhcp_client->timeout = g_timeout_add_seconds_full(
2429 restart_dhcp_timeout,
2436 if (dhcp_client->type != G_DHCP_IPV6)
2439 if (packet6->message != DHCPV6_REPLY &&
2440 packet6->message != DHCPV6_ADVERTISE)
2444 server_id = dhcpv6_get_option(packet6, pkt_len,
2445 G_DHCPV6_SERVERID, &option_len, &count);
2446 if (!server_id || count != 1 || option_len == 0) {
2447 /* RFC 3315, 15.10 */
2449 "server duid error, discarding msg %p/%d/%d",
2450 server_id, option_len, count);
2453 dhcp_client->server_duid = g_try_malloc(option_len);
2454 if (!dhcp_client->server_duid)
2456 memcpy(dhcp_client->server_duid, server_id, option_len);
2457 dhcp_client->server_duid_len = option_len;
2459 if (packet6->message == DHCPV6_REPLY) {
2460 uint8_t *rapid_commit;
2463 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2464 G_DHCPV6_RAPID_COMMIT,
2465 &option_len, &count);
2466 if (!rapid_commit || option_len != 0 ||
2468 /* RFC 3315, 17.1.4 */
2472 switch_listening_mode(dhcp_client, L_NONE);
2474 if (dhcp_client->status_code == 0)
2475 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2476 &dhcp_client->status_code);
2478 if (packet6->message == DHCPV6_ADVERTISE) {
2479 if (dhcp_client->advertise_cb)
2480 dhcp_client->advertise_cb(dhcp_client,
2481 dhcp_client->advertise_data);
2485 if (dhcp_client->solicitation_cb) {
2487 * The dhcp_client might not be valid after the
2488 * callback call so just return immediately.
2490 dhcp_client->solicitation_cb(dhcp_client,
2491 dhcp_client->solicitation_data);
2496 if (dhcp_client->type != G_DHCP_IPV6)
2499 server_id = dhcpv6_get_option(packet6, pkt_len,
2500 G_DHCPV6_SERVERID, &option_len, &count);
2501 if (!dhcp_client->server_duid && server_id &&
2504 * If we do not have server duid yet, then get it now.
2505 * Prefix delegation renew support needs it.
2507 dhcp_client->server_duid = g_try_malloc(option_len);
2508 if (!dhcp_client->server_duid)
2510 memcpy(dhcp_client->server_duid, server_id, option_len);
2511 dhcp_client->server_duid_len = option_len;
2514 case INFORMATION_REQ:
2520 if (dhcp_client->type != G_DHCP_IPV6)
2523 if (packet6->message != DHCPV6_REPLY)
2528 server_id = dhcpv6_get_option(packet6, pkt_len,
2529 G_DHCPV6_SERVERID, &option_len, &count);
2530 if (!server_id || count != 1 || option_len == 0 ||
2531 (dhcp_client->server_duid_len > 0 &&
2532 memcmp(dhcp_client->server_duid, server_id,
2533 dhcp_client->server_duid_len) != 0)) {
2534 /* RFC 3315, 15.10 */
2536 "server duid error, discarding msg %p/%d/%d",
2537 server_id, option_len, count);
2541 switch_listening_mode(dhcp_client, L_NONE);
2543 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2544 &dhcp_client->status_code);
2546 if (dhcp_client->information_req_cb) {
2548 * The dhcp_client might not be valid after the
2549 * callback call so just return immediately.
2551 dhcp_client->information_req_cb(dhcp_client,
2552 dhcp_client->information_req_data);
2555 if (dhcp_client->request_cb) {
2556 dhcp_client->request_cb(dhcp_client,
2557 dhcp_client->request_data);
2560 if (dhcp_client->renew_cb) {
2561 dhcp_client->renew_cb(dhcp_client,
2562 dhcp_client->renew_data);
2565 if (dhcp_client->rebind_cb) {
2566 dhcp_client->rebind_cb(dhcp_client,
2567 dhcp_client->rebind_data);
2570 if (dhcp_client->release_cb) {
2571 dhcp_client->release_cb(dhcp_client,
2572 dhcp_client->release_data);
2575 if (dhcp_client->decline_cb) {
2576 dhcp_client->decline_cb(dhcp_client,
2577 dhcp_client->decline_data);
2580 if (dhcp_client->confirm_cb) {
2582 server_id = dhcpv6_get_option(packet6, pkt_len,
2583 G_DHCPV6_SERVERID, &option_len,
2585 if (!server_id || count != 1 ||
2587 /* RFC 3315, 15.10 */
2589 "confirm server duid error, "
2590 "discarding msg %p/%d/%d",
2591 server_id, option_len, count);
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;
2600 dhcp_client->confirm_cb(dhcp_client,
2601 dhcp_client->confirm_data);
2609 debug(dhcp_client, "processed DHCP packet (new state %d)",
2610 dhcp_client->state);
2615 static gboolean discover_timeout(gpointer user_data)
2617 GDHCPClient *dhcp_client = user_data;
2619 dhcp_client->retry_times++;
2622 * We do not send the REQUESTED IP option if we are retrying because
2623 * if the server is non-authoritative it will ignore the request if the
2624 * option is present.
2626 g_dhcp_client_start(dhcp_client, NULL);
2631 static gboolean reboot_timeout(gpointer user_data)
2633 GDHCPClient *dhcp_client = user_data;
2634 dhcp_client->retry_times = 0;
2635 dhcp_client->requested_ip = 0;
2636 dhcp_client->state = INIT_SELECTING;
2638 * We do not send the REQUESTED IP option because the server didn't
2639 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2642 g_dhcp_client_start(dhcp_client, NULL);
2647 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2649 GDHCPClient *dhcp_client = dhcp_data;
2651 debug(dhcp_client, "back to MONITOR mode");
2653 dhcp_client->conflicts = 0;
2654 dhcp_client->state = IPV4LL_MONITOR;
2659 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2661 GDHCPClient *dhcp_client = dhcp_data;
2664 #if defined TIZEN_EXT
2669 debug(dhcp_client, "request timeout (retries %d)",
2670 dhcp_client->retry_times);
2672 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2673 dhcp_client->retry_times++;
2674 send_announce_packet(dhcp_client);
2678 ip = htonl(dhcp_client->requested_ip);
2679 debug(dhcp_client, "switching to monitor mode");
2680 dhcp_client->state = IPV4LL_MONITOR;
2681 dhcp_client->assigned_ip = get_ip(ip);
2683 if (dhcp_client->ipv4ll_available_cb)
2684 dhcp_client->ipv4ll_available_cb(dhcp_client,
2685 dhcp_client->ipv4ll_available_data);
2686 dhcp_client->conflicts = 0;
2687 dhcp_client->timeout = 0;
2692 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2695 GDHCPClient *dhcp_client = dhcp_data;
2697 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2698 dhcp_client->retry_times);
2700 if (dhcp_client->retry_times == PROBE_NUM) {
2701 dhcp_client->state = IPV4LL_ANNOUNCE;
2702 dhcp_client->retry_times = 0;
2704 dhcp_client->retry_times++;
2705 send_announce_packet(dhcp_client);
2708 dhcp_client->retry_times++;
2709 send_probe_packet(dhcp_client);
2714 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2720 #if defined TIZEN_EXT
2721 int discover_retry = 0;
2725 remove_timeouts(dhcp_client);
2727 if (dhcp_client->type == G_DHCP_IPV6) {
2728 if (dhcp_client->information_req_cb) {
2729 dhcp_client->state = INFORMATION_REQ;
2730 re = switch_listening_mode(dhcp_client, L3);
2732 switch_listening_mode(dhcp_client, L_NONE);
2733 dhcp_client->state = 0;
2736 send_information_req(dhcp_client);
2738 } else if (dhcp_client->solicitation_cb) {
2739 dhcp_client->state = SOLICITATION;
2740 re = switch_listening_mode(dhcp_client, L3);
2742 switch_listening_mode(dhcp_client, L_NONE);
2743 dhcp_client->state = 0;
2746 send_solicitation(dhcp_client);
2748 } else if (dhcp_client->request_cb) {
2749 dhcp_client->state = REQUEST;
2750 re = switch_listening_mode(dhcp_client, L3);
2752 switch_listening_mode(dhcp_client, L_NONE);
2753 dhcp_client->state = 0;
2756 send_dhcpv6_request(dhcp_client);
2758 } else if (dhcp_client->confirm_cb) {
2759 dhcp_client->state = CONFIRM;
2760 re = switch_listening_mode(dhcp_client, L3);
2762 switch_listening_mode(dhcp_client, L_NONE);
2763 dhcp_client->state = 0;
2766 send_dhcpv6_confirm(dhcp_client);
2768 } else if (dhcp_client->renew_cb) {
2769 dhcp_client->state = RENEW;
2770 re = switch_listening_mode(dhcp_client, L3);
2772 switch_listening_mode(dhcp_client, L_NONE);
2773 dhcp_client->state = 0;
2776 send_dhcpv6_renew(dhcp_client);
2778 } else if (dhcp_client->rebind_cb) {
2779 dhcp_client->state = REBIND;
2780 re = switch_listening_mode(dhcp_client, L3);
2782 switch_listening_mode(dhcp_client, L_NONE);
2783 dhcp_client->state = 0;
2786 send_dhcpv6_rebind(dhcp_client);
2788 } else if (dhcp_client->release_cb) {
2789 dhcp_client->state = RENEW;
2790 re = switch_listening_mode(dhcp_client, L3);
2792 switch_listening_mode(dhcp_client, L_NONE);
2793 dhcp_client->state = 0;
2796 send_dhcpv6_release(dhcp_client);
2797 } else if (dhcp_client->decline_cb) {
2798 dhcp_client->state = DECLINE;
2799 re = switch_listening_mode(dhcp_client, L3);
2801 switch_listening_mode(dhcp_client, L_NONE);
2802 dhcp_client->state = 0;
2805 send_dhcpv6_decline(dhcp_client);
2811 #if defined TIZEN_EXT
2812 if (g_ascii_strncasecmp(dhcp_client->interface, "wlan", 4)
2814 discover_retry = DISCOVER_RETRIES_WIFI;
2815 timeout = DISCOVER_TIMEOUT_WIFI;
2817 discover_retry = DISCOVER_RETRIES;
2818 timeout = DISCOVER_TIMEOUT;
2821 debug(dhcp_client, "[DHCPC] Discover retry/total : [%d]/[%d] timeout [%d]",
2822 dhcp_client->retry_times, discover_retry, timeout);
2826 if (dhcp_client->type == G_DHCP_IPV4LL) {
2827 dhcp_client->state = INIT_SELECTING;
2828 ipv4ll_start(dhcp_client);
2832 #if defined TIZEN_EXT
2833 if (dhcp_client->retry_times == discover_retry) {
2835 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2837 if (dhcp_client->no_lease_cb)
2838 dhcp_client->no_lease_cb(dhcp_client,
2839 dhcp_client->no_lease_data);
2840 dhcp_client->retry_times = 0;
2844 if (dhcp_client->retry_times == 0) {
2845 g_free(dhcp_client->assigned_ip);
2846 dhcp_client->assigned_ip = NULL;
2848 dhcp_client->state = INIT_SELECTING;
2849 re = switch_listening_mode(dhcp_client, L2);
2853 dhcp_get_random(&rand);
2854 dhcp_client->xid = rand;
2855 dhcp_client->start = time(NULL);
2858 if (!last_address) {
2861 addr = ntohl(inet_addr(last_address));
2862 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2865 } else if (dhcp_client->last_address != last_address) {
2866 g_free(dhcp_client->last_address);
2867 dhcp_client->last_address = g_strdup(last_address);
2871 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2872 debug(dhcp_client, "DHCP client start with state init_reboot");
2873 dhcp_client->requested_ip = addr;
2874 dhcp_client->state = REBOOTING;
2875 send_request(dhcp_client);
2877 dhcp_client->timeout = g_timeout_add_seconds_full(
2879 #if defined TIZEN_EXT
2889 send_discover(dhcp_client, addr);
2891 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2892 #if defined TIZEN_EXT
2903 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2905 switch_listening_mode(dhcp_client, L_NONE);
2907 if (dhcp_client->state == BOUND ||
2908 dhcp_client->state == RENEWING ||
2909 dhcp_client->state == REBINDING)
2910 send_release(dhcp_client, dhcp_client->server_ip,
2911 dhcp_client->requested_ip);
2913 remove_timeouts(dhcp_client);
2915 if (dhcp_client->listener_watch > 0) {
2916 g_source_remove(dhcp_client->listener_watch);
2917 dhcp_client->listener_watch = 0;
2920 dhcp_client->retry_times = 0;
2921 dhcp_client->ack_retry_times = 0;
2923 dhcp_client->requested_ip = 0;
2924 dhcp_client->state = RELEASED;
2925 dhcp_client->lease_seconds = 0;
2926 dhcp_client->request_bcast = false;
2929 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2930 unsigned char option_code)
2932 return g_hash_table_lookup(dhcp_client->code_value_hash,
2933 GINT_TO_POINTER((int) option_code));
2936 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2937 GDHCPClientEvent event,
2938 GDHCPClientEventFunc func,
2942 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2943 dhcp_client->lease_available_cb = func;
2944 dhcp_client->lease_available_data = data;
2946 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2947 if (dhcp_client->type == G_DHCP_IPV6)
2949 dhcp_client->ipv4ll_available_cb = func;
2950 dhcp_client->ipv4ll_available_data = data;
2952 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2953 dhcp_client->no_lease_cb = func;
2954 dhcp_client->no_lease_data = data;
2956 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2957 dhcp_client->lease_lost_cb = func;
2958 dhcp_client->lease_lost_data = data;
2960 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2961 if (dhcp_client->type == G_DHCP_IPV6)
2963 dhcp_client->ipv4ll_lost_cb = func;
2964 dhcp_client->ipv4ll_lost_data = data;
2966 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2967 dhcp_client->address_conflict_cb = func;
2968 dhcp_client->address_conflict_data = data;
2970 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2971 if (dhcp_client->type != G_DHCP_IPV6)
2973 dhcp_client->information_req_cb = func;
2974 dhcp_client->information_req_data = data;
2976 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2977 if (dhcp_client->type != G_DHCP_IPV6)
2979 dhcp_client->solicitation_cb = func;
2980 dhcp_client->solicitation_data = data;
2982 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2983 if (dhcp_client->type != G_DHCP_IPV6)
2985 dhcp_client->advertise_cb = func;
2986 dhcp_client->advertise_data = data;
2988 case G_DHCP_CLIENT_EVENT_REQUEST:
2989 if (dhcp_client->type != G_DHCP_IPV6)
2991 dhcp_client->request_cb = func;
2992 dhcp_client->request_data = data;
2994 case G_DHCP_CLIENT_EVENT_RENEW:
2995 if (dhcp_client->type != G_DHCP_IPV6)
2997 dhcp_client->renew_cb = func;
2998 dhcp_client->renew_data = data;
3000 case G_DHCP_CLIENT_EVENT_REBIND:
3001 if (dhcp_client->type != G_DHCP_IPV6)
3003 dhcp_client->rebind_cb = func;
3004 dhcp_client->rebind_data = data;
3006 case G_DHCP_CLIENT_EVENT_RELEASE:
3007 if (dhcp_client->type != G_DHCP_IPV6)
3009 dhcp_client->release_cb = func;
3010 dhcp_client->release_data = data;
3012 case G_DHCP_CLIENT_EVENT_CONFIRM:
3013 if (dhcp_client->type != G_DHCP_IPV6)
3015 dhcp_client->confirm_cb = func;
3016 dhcp_client->confirm_data = data;
3018 case G_DHCP_CLIENT_EVENT_DECLINE:
3019 if (dhcp_client->type != G_DHCP_IPV6)
3021 dhcp_client->decline_cb = func;
3022 dhcp_client->decline_data = data;
3027 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
3029 return dhcp_client->ifindex;
3032 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
3037 #if defined TIZEN_EXT
3038 return get_ip(htonl(dhcp_client->server_ip));
3040 return get_ip(dhcp_client->server_ip);
3044 #if defined TIZEN_EXT
3045 int g_dhcp_client_get_dhcp_lease_duration(GDHCPClient *dhcp_client)
3047 return dhcp_client->dhcp_lease_seconds;
3051 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
3053 return g_strdup(dhcp_client->assigned_ip);
3056 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
3058 GList *option = NULL;
3060 if (dhcp_client->type == G_DHCP_IPV6)
3063 switch (dhcp_client->state) {
3065 case IPV4LL_MONITOR:
3066 return g_strdup("255.255.0.0");
3070 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3072 return g_strdup(option->data);
3073 case INIT_SELECTING:
3078 case IPV4LL_ANNOUNCE:
3079 case INFORMATION_REQ:
3092 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3093 unsigned int option_code)
3095 if (!g_list_find(dhcp_client->request_list,
3096 GINT_TO_POINTER((int)option_code)))
3097 dhcp_client->request_list = g_list_prepend(
3098 dhcp_client->request_list,
3099 (GINT_TO_POINTER((int) option_code)));
3101 return G_DHCP_CLIENT_ERROR_NONE;
3104 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3106 g_list_free(dhcp_client->request_list);
3107 dhcp_client->request_list = NULL;
3110 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3112 g_hash_table_remove_all(dhcp_client->send_value_hash);
3115 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3119 storage = g_try_malloc(size + OPT_DATA);
3123 storage[OPT_CODE] = code;
3124 storage[OPT_LEN] = size;
3125 memcpy(&storage[OPT_DATA], data, size);
3130 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3133 return alloc_dhcp_option(code, data, MIN(size, 255));
3136 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3138 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3141 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3143 const unsigned maclen = 6;
3144 const unsigned idlen = maclen + 1;
3145 const uint8_t option_code = G_DHCP_CLIENT_ID;
3146 uint8_t idbuf[idlen];
3147 uint8_t *data_option;
3149 idbuf[0] = ARPHRD_ETHER;
3151 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3153 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3155 return G_DHCP_CLIENT_ERROR_NOMEM;
3157 g_hash_table_insert(dhcp_client->send_value_hash,
3158 GINT_TO_POINTER((int) option_code), data_option);
3160 return G_DHCP_CLIENT_ERROR_NONE;
3163 /* Now only support send hostname */
3164 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3165 unsigned char option_code, const char *option_value)
3167 uint8_t *binary_option;
3169 if (option_code == G_DHCP_HOST_NAME && 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;