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
53 #define REQUEST_TIMEOUT 5
54 #define REQUEST_RETRIES 3
56 typedef enum _listen_mode {
63 typedef enum _dhcp_client_state {
91 uint8_t mac_address[6];
94 uint32_t requested_ip;
97 uint32_t lease_seconds;
98 ListenMode listen_mode;
101 uint8_t ack_retry_times;
107 guint listener_watch;
110 GHashTable *code_value_hash;
111 GHashTable *send_value_hash;
112 GDHCPClientEventFunc lease_available_cb;
113 gpointer lease_available_data;
114 GDHCPClientEventFunc ipv4ll_available_cb;
115 gpointer ipv4ll_available_data;
116 GDHCPClientEventFunc no_lease_cb;
117 gpointer no_lease_data;
118 GDHCPClientEventFunc lease_lost_cb;
119 gpointer lease_lost_data;
120 GDHCPClientEventFunc ipv4ll_lost_cb;
121 gpointer ipv4ll_lost_data;
122 GDHCPClientEventFunc address_conflict_cb;
123 gpointer address_conflict_data;
124 GDHCPDebugFunc debug_func;
126 GDHCPClientEventFunc information_req_cb;
127 gpointer information_req_data;
128 GDHCPClientEventFunc solicitation_cb;
129 gpointer solicitation_data;
130 GDHCPClientEventFunc advertise_cb;
131 gpointer advertise_data;
132 GDHCPClientEventFunc request_cb;
133 gpointer request_data;
134 GDHCPClientEventFunc renew_cb;
136 GDHCPClientEventFunc rebind_cb;
137 gpointer rebind_data;
138 GDHCPClientEventFunc release_cb;
139 gpointer release_data;
140 GDHCPClientEventFunc confirm_cb;
141 gpointer confirm_data;
142 GDHCPClientEventFunc decline_cb;
143 gpointer decline_data;
147 unsigned char *server_duid;
149 uint16_t status_code;
152 struct in6_addr ia_na;
153 struct in6_addr ia_ta;
157 struct timeval start_time;
159 #if defined TIZEN_EXT
160 gboolean init_reboot;
164 static inline void debug(GDHCPClient *client, const char *format, ...)
169 if (!client->debug_func)
172 va_start(ap, format);
174 if (vsnprintf(str, sizeof(str), format, ap) > 0)
175 client->debug_func(str, client->debug_data);
180 /* Initialize the packet with the proper defaults */
181 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
183 if (dhcp_client->type == G_DHCP_IPV6)
184 dhcpv6_init_header(pkt, type);
186 struct dhcp_packet *packet = pkt;
188 dhcp_init_header(packet, type);
189 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
193 static void add_request_options(GDHCPClient *dhcp_client,
194 struct dhcp_packet *packet)
199 int end = dhcp_end_option(packet->options);
201 for (list = dhcp_client->request_list; list; list = list->next) {
202 code = (uint8_t) GPOINTER_TO_INT(list->data);
204 packet->options[end + OPT_DATA + len] = code;
209 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
210 packet->options[end + OPT_LEN] = len;
211 packet->options[end + OPT_DATA + len] = DHCP_END;
218 unsigned char **ptr_buf;
221 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
224 uint8_t *option = value;
226 struct hash_params *params = user_data;
228 /* option[0][1] contains option code */
229 len = option[2] << 8 | option[3];
231 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
234 memcpy(*params->ptr_buf, option, len + 2 + 2);
235 (*params->ptr_buf) += len + 2 + 2;
238 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
239 unsigned char *buf, int max_buf,
240 unsigned char **ptr_buf)
242 struct hash_params params = {
248 if (dhcp_client->type != G_DHCP_IPV6)
251 g_hash_table_foreach(dhcp_client->send_value_hash,
252 add_dhcpv6_binary_option, ¶ms);
254 *ptr_buf = *params.ptr_buf;
257 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
261 buf[1] = code & 0xff;
265 memcpy(&buf[4], msg, len);
268 static int32_t get_time_diff(struct timeval *tv)
273 gettimeofday(&now, NULL);
275 hsec = (now.tv_sec - tv->tv_sec) * 100;
276 hsec += (now.tv_usec - tv->tv_usec) / 10000;
281 static void remove_timeouts(GDHCPClient *dhcp_client)
284 if (dhcp_client->timeout > 0)
285 g_source_remove(dhcp_client->timeout);
286 if (dhcp_client->t1_timeout > 0)
287 g_source_remove(dhcp_client->t1_timeout);
288 if (dhcp_client->t2_timeout > 0)
289 g_source_remove(dhcp_client->t2_timeout);
290 if (dhcp_client->lease_timeout > 0)
291 g_source_remove(dhcp_client->lease_timeout);
293 dhcp_client->timeout = 0;
294 dhcp_client->t1_timeout = 0;
295 dhcp_client->t2_timeout = 0;
296 dhcp_client->lease_timeout = 0;
300 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
301 struct dhcpv6_packet *packet,
302 unsigned char *buf, int max_buf,
303 unsigned char **ptr_buf)
306 uint16_t code, value;
311 if (dhcp_client->type != G_DHCP_IPV6)
314 for (list = dhcp_client->request_list; list; list = list->next) {
315 code = (uint16_t) GPOINTER_TO_INT(list->data);
319 case G_DHCPV6_CLIENTID:
320 if (!dhcp_client->duid)
323 len = 2 + 2 + dhcp_client->duid_len;
324 if ((*ptr_buf + len) > (buf + max_buf)) {
325 debug(dhcp_client, "Too long dhcpv6 message "
326 "when writing client id option");
330 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
331 dhcp_client->duid_len, dhcp_client->duid);
336 case G_DHCPV6_SERVERID:
337 if (!dhcp_client->server_duid)
340 len = 2 + 2 + dhcp_client->server_duid_len;
341 if ((*ptr_buf + len) > (buf + max_buf)) {
342 debug(dhcp_client, "Too long dhcpv6 message "
343 "when writing server id option");
347 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
348 dhcp_client->server_duid_len,
349 dhcp_client->server_duid);
354 case G_DHCPV6_RAPID_COMMIT:
356 if ((*ptr_buf + len) > (buf + max_buf)) {
357 debug(dhcp_client, "Too long dhcpv6 message "
358 "when writing rapid commit option");
362 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
370 case G_DHCPV6_ELAPSED_TIME:
371 if (!dhcp_client->retransmit) {
373 * Initial message, elapsed time is 0.
377 diff = get_time_diff(&dhcp_client->start_time);
378 if (diff < 0 || diff > 0xffff)
383 if ((*ptr_buf + len) > (buf + max_buf)) {
384 debug(dhcp_client, "Too long dhcpv6 message "
385 "when writing elapsed time option");
389 value = htons((uint16_t)diff);
390 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
391 2, (uint8_t *)&value);
396 case G_DHCPV6_DNS_SERVERS:
399 case G_DHCPV6_DOMAIN_LIST:
402 case G_DHCPV6_SNTP_SERVERS:
410 debug(dhcp_client, "option %d len %d added", code, len);
414 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
416 uint8_t *option = value;
417 struct dhcp_packet *packet = user_data;
419 dhcp_add_binary_option(packet, option);
422 static void add_send_options(GDHCPClient *dhcp_client,
423 struct dhcp_packet *packet)
425 g_hash_table_foreach(dhcp_client->send_value_hash,
426 add_binary_option, packet);
430 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
431 * represents the number of seconds elapsed from the start of
432 * attempting DHCP to satisfy some DHCP servers that allow for an
433 * "authoritative" reply before responding.
435 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
437 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
440 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
442 struct dhcp_packet packet;
444 debug(dhcp_client, "sending DHCP discover request");
446 init_packet(dhcp_client, &packet, DHCPDISCOVER);
448 packet.xid = dhcp_client->xid;
449 packet.secs = dhcp_attempt_secs(dhcp_client);
452 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
454 /* Explicitly saying that we want RFC-compliant packets helps
455 * some buggy DHCP servers to NOT send bigger packets */
456 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
458 add_request_options(dhcp_client, &packet);
460 add_send_options(dhcp_client, &packet);
463 * If we do not get a reply to DISCOVER packet, then we try with
464 * broadcast flag set. So first packet is sent without broadcast flag,
465 * first retry is with broadcast flag, second retry is without it etc.
466 * Reason is various buggy routers/AP that either eat the other or vice
467 * versa. In the receiving side we then find out what kind of packet
468 * the server can send.
470 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
471 INADDR_BROADCAST, SERVER_PORT,
472 MAC_BCAST_ADDR, dhcp_client->ifindex,
473 dhcp_client->retry_times % 2);
476 static int send_request(GDHCPClient *dhcp_client)
478 struct dhcp_packet packet;
480 debug(dhcp_client, "sending DHCP request (state %d)",
483 init_packet(dhcp_client, &packet, DHCPREQUEST);
485 packet.xid = dhcp_client->xid;
486 #if defined TIZEN_EXT
487 if (dhcp_client->init_reboot != TRUE)
489 packet.secs = dhcp_attempt_secs(dhcp_client);
491 if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
492 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
493 dhcp_client->requested_ip);
495 if (dhcp_client->state == REQUESTING)
496 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
497 dhcp_client->server_ip);
499 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
501 add_request_options(dhcp_client, &packet);
503 add_send_options(dhcp_client, &packet);
505 if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
506 packet.ciaddr = htonl(dhcp_client->requested_ip);
508 if (dhcp_client->state == RENEWING)
509 return dhcp_send_kernel_packet(&packet,
510 dhcp_client->requested_ip, CLIENT_PORT,
511 dhcp_client->server_ip, SERVER_PORT);
513 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
514 INADDR_BROADCAST, SERVER_PORT,
515 MAC_BCAST_ADDR, dhcp_client->ifindex,
516 dhcp_client->request_bcast);
519 static int send_release(GDHCPClient *dhcp_client,
520 uint32_t server, uint32_t ciaddr)
522 struct dhcp_packet packet;
525 debug(dhcp_client, "sending DHCP release request");
527 init_packet(dhcp_client, &packet, DHCPRELEASE);
528 dhcp_get_random(&rand);
530 packet.ciaddr = htonl(ciaddr);
532 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
534 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
535 server, SERVER_PORT);
538 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
539 static int switch_listening_mode(GDHCPClient *dhcp_client,
540 ListenMode listen_mode);
542 static gboolean send_probe_packet(gpointer dhcp_data)
544 GDHCPClient *dhcp_client;
547 dhcp_client = dhcp_data;
548 /* if requested_ip is not valid, pick a new address*/
549 if (dhcp_client->requested_ip == 0) {
550 debug(dhcp_client, "pick a new random address");
551 dhcp_client->requested_ip = ipv4ll_random_ip();
554 debug(dhcp_client, "sending IPV4LL probe request");
556 if (dhcp_client->retry_times == 1) {
557 dhcp_client->state = IPV4LL_PROBE;
558 switch_listening_mode(dhcp_client, L_ARP);
560 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
561 dhcp_client->requested_ip, dhcp_client->ifindex);
563 if (dhcp_client->retry_times < PROBE_NUM) {
564 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
565 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
566 timeout += PROBE_MIN*1000;
568 timeout = (ANNOUNCE_WAIT * 1000);
570 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
572 ipv4ll_probe_timeout,
578 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
579 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
581 static gboolean send_announce_packet(gpointer dhcp_data)
583 GDHCPClient *dhcp_client;
585 dhcp_client = dhcp_data;
587 debug(dhcp_client, "sending IPV4LL announce request");
589 ipv4ll_send_arp_packet(dhcp_client->mac_address,
590 dhcp_client->requested_ip,
591 dhcp_client->requested_ip,
592 dhcp_client->ifindex);
594 remove_timeouts(dhcp_client);
596 if (dhcp_client->state == IPV4LL_DEFEND) {
597 dhcp_client->timeout =
598 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
600 ipv4ll_defend_timeout,
605 dhcp_client->timeout =
606 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
608 ipv4ll_announce_timeout,
614 static void get_interface_mac_address(int index, uint8_t *mac_address)
619 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
621 perror("Open socket error");
625 memset(&ifr, 0, sizeof(ifr));
626 ifr.ifr_ifindex = index;
628 err = ioctl(sk, SIOCGIFNAME, &ifr);
630 perror("Get interface name error");
634 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
636 perror("Get mac address error");
640 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
646 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
651 dhcp_client->retransmit = true;
654 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
659 dhcp_client->retransmit = false;
662 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
663 unsigned char **duid, int *duid_len)
668 case G_DHCPV6_DUID_LLT:
669 *duid_len = 2 + 2 + 4 + ETH_ALEN;
670 *duid = g_try_malloc(*duid_len);
676 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
679 duid_time = time(NULL) - DUID_TIME_EPOCH;
680 (*duid)[4] = duid_time >> 24;
681 (*duid)[5] = duid_time >> 16;
682 (*duid)[6] = duid_time >> 8;
683 (*duid)[7] = duid_time & 0xff;
685 case G_DHCPV6_DUID_EN:
687 case G_DHCPV6_DUID_LL:
688 *duid_len = 2 + 2 + ETH_ALEN;
689 *duid = g_try_malloc(*duid_len);
695 get_interface_mac_address(index, &(*duid)[2 + 2]);
704 static gchar *convert_to_hex(unsigned char *buf, int len)
706 gchar *ret = g_try_malloc(len * 2 + 1);
709 for (i = 0; ret && i < len; i++)
710 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
715 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
718 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
721 g_free(dhcp_client->duid);
723 dhcp_client->duid = duid;
724 dhcp_client->duid_len = duid_len;
726 if (dhcp_client->debug_func) {
727 gchar *hex = convert_to_hex(duid, duid_len);
728 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
735 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
736 uint32_t *T2, GSList *prefixes)
738 uint8_t options[1452];
739 unsigned int max_buf = sizeof(options);
740 int len, count = g_slist_length(prefixes);
742 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
745 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
747 memset(options, 0, sizeof(options));
749 options[0] = dhcp_client->iaid >> 24;
750 options[1] = dhcp_client->iaid >> 16;
751 options[2] = dhcp_client->iaid >> 8;
752 options[3] = dhcp_client->iaid;
755 uint32_t t = htonl(*T1);
756 memcpy(&options[4], &t, 4);
760 uint32_t t = htonl(*T2);
761 memcpy(&options[8], &t, 4);
769 for (list = prefixes; list; list = list->next) {
770 GDHCPIAPrefix *prefix = list->data;
771 uint8_t sub_option[4+4+1+16];
773 if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
775 "Too long dhcpv6 message "
776 "when writing IA prefix option");
780 memset(&sub_option, 0, sizeof(sub_option));
782 /* preferred and validity time are left zero */
784 sub_option[8] = prefix->prefixlen;
785 memcpy(&sub_option[9], &prefix->prefix, 16);
787 copy_option(&options[len], G_DHCPV6_IA_PREFIX,
788 sizeof(sub_option), sub_option);
789 len += 2 + 2 + sizeof(sub_option);
793 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
799 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
801 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
804 return dhcp_client->iaid;
807 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
809 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
812 dhcp_client->iaid = iaid;
815 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
820 get_interface_mac_address(index, buf);
822 memcpy(iaid, &buf[2], 4);
823 dhcp_client->iaid = iaid[0] << 24 |
824 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
827 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
828 uint32_t *T1, uint32_t *T2,
832 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
836 *T1 = dhcp_client->T1;
839 *T2 = dhcp_client->T2;
842 *started = dhcp_client->last_request;
845 *expire = dhcp_client->last_request + dhcp_client->expire;
850 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
854 buf[1] = G_DHCPV6_IAADDR;
857 memcpy(&buf[4], &dhcp_client->ia_na, 16);
858 memset(&buf[20], 0, 4); /* preferred */
859 memset(&buf[24], 0, 4); /* valid */
863 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
866 struct in6_addr addr;
868 if (inet_pton(AF_INET6, address, &addr) != 1)
872 buf[1] = G_DHCPV6_IAADDR;
875 memcpy(&buf[4], &addr, 16);
876 memset(&buf[20], 0, 4); /* preferred */
877 memset(&buf[24], 0, 4); /* valid */
881 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
885 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
887 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
897 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
898 int code, uint32_t *T1, uint32_t *T2,
899 bool add_iaaddr, const char *ia_na)
901 if (code == G_DHCPV6_IA_TA) {
902 uint8_t ia_options[4];
904 put_iaid(dhcp_client, index, ia_options);
906 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
907 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
908 ia_options, sizeof(ia_options));
910 } else if (code == G_DHCPV6_IA_NA) {
911 struct in6_addr addr;
913 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
916 * If caller has specified the IPv6 address it wishes to
917 * to use (ia_na != NULL and address is valid), then send
918 * the address to server.
919 * If caller did not specify the address (ia_na == NULL) and
920 * if the current address is not set, then we should not send
921 * the address sub-option.
923 if (add_iaaddr && ((!ia_na &&
924 !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
926 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
927 #define IAADDR_LEN (16+4+4)
928 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
931 memcpy(&dhcp_client->ia_na, &addr,
932 sizeof(struct in6_addr));
934 put_iaid(dhcp_client, index, ia_options);
937 ia_options[4] = *T1 >> 24;
938 ia_options[5] = *T1 >> 16;
939 ia_options[6] = *T1 >> 8;
942 memset(&ia_options[4], 0x00, 4);
945 ia_options[8] = *T2 >> 24;
946 ia_options[9] = *T2 >> 16;
947 ia_options[10] = *T2 >> 8;
948 ia_options[11] = *T2;
950 memset(&ia_options[8], 0x00, 4);
952 create_iaaddr(dhcp_client, &ia_options[12],
955 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
956 ia_options, sizeof(ia_options));
958 uint8_t ia_options[4+4+4];
960 put_iaid(dhcp_client, index, ia_options);
962 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
963 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
965 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
966 ia_options, sizeof(ia_options));
975 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
976 int code, uint32_t *T1, uint32_t *T2,
980 uint8_t *ia_options, *pos;
981 int len, count, total_len;
983 count = g_slist_length(addresses);
987 g_dhcp_client_set_request(dhcp_client, code);
989 if (code == G_DHCPV6_IA_TA)
991 else if (code == G_DHCPV6_IA_NA)
992 len = 4 + 4 + 4; /* IAID + T1 + T2 */
996 total_len = len + count * (2 + 2 + 16 + 4 + 4);
997 ia_options = g_try_malloc0(total_len);
1001 put_iaid(dhcp_client, index, ia_options);
1003 pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
1005 for (list = addresses; list; list = list->next) {
1006 pos = append_iaaddr(dhcp_client, pos, list->data);
1011 if (code == G_DHCPV6_IA_NA) {
1013 ia_options[4] = *T1 >> 24;
1014 ia_options[5] = *T1 >> 16;
1015 ia_options[6] = *T1 >> 8;
1016 ia_options[7] = *T1;
1018 memset(&ia_options[4], 0x00, 4);
1021 ia_options[8] = *T2 >> 24;
1022 ia_options[9] = *T2 >> 16;
1023 ia_options[10] = *T2 >> 8;
1024 ia_options[11] = *T2;
1026 memset(&ia_options[8], 0x00, 4);
1029 g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1036 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1039 int i, j, len = sizeof(uint16_t) * args;
1042 values = g_try_malloc(len);
1047 for (i = 0, j = 0; i < args; i++) {
1048 uint16_t value = va_arg(va, int);
1049 values[j++] = value >> 8;
1050 values[j++] = value & 0xff;
1054 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1061 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1063 struct dhcpv6_packet *packet;
1064 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1068 memset(buf, 0, sizeof(buf));
1069 packet = (struct dhcpv6_packet *)&buf[0];
1070 ptr = buf + sizeof(struct dhcpv6_packet);
1072 init_packet(dhcp_client, packet, type);
1074 if (!dhcp_client->retransmit) {
1075 dhcp_client->xid = packet->transaction_id[0] << 16 |
1076 packet->transaction_id[1] << 8 |
1077 packet->transaction_id[2];
1078 gettimeofday(&dhcp_client->start_time, NULL);
1080 packet->transaction_id[0] = dhcp_client->xid >> 16;
1081 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1082 packet->transaction_id[2] = dhcp_client->xid;
1085 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1087 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1090 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1092 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1094 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1096 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1098 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1102 static int send_solicitation(GDHCPClient *dhcp_client)
1104 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1107 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1109 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1112 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1114 return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1117 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1119 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1122 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1124 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1127 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1129 return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1132 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1134 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1137 static int send_information_req(GDHCPClient *dhcp_client)
1139 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1143 static void remove_value(gpointer data, gpointer user_data)
1149 static void remove_option_value(gpointer data)
1151 GList *option_value = data;
1153 g_list_foreach(option_value, remove_value, NULL);
1154 g_list_free(option_value);
1157 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1158 int ifindex, GDHCPClientError *error)
1160 GDHCPClient *dhcp_client;
1163 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1167 dhcp_client = g_try_new0(GDHCPClient, 1);
1169 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1173 dhcp_client->interface = get_interface_name(ifindex);
1174 if (!dhcp_client->interface) {
1175 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1179 if (!interface_is_up(ifindex)) {
1180 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1184 get_interface_mac_address(ifindex, dhcp_client->mac_address);
1186 dhcp_client->listener_sockfd = -1;
1187 dhcp_client->listen_mode = L_NONE;
1188 dhcp_client->ref_count = 1;
1189 dhcp_client->type = type;
1190 dhcp_client->ifindex = ifindex;
1191 dhcp_client->lease_available_cb = NULL;
1192 dhcp_client->ipv4ll_available_cb = NULL;
1193 dhcp_client->no_lease_cb = NULL;
1194 dhcp_client->lease_lost_cb = NULL;
1195 dhcp_client->ipv4ll_lost_cb = NULL;
1196 dhcp_client->address_conflict_cb = NULL;
1197 dhcp_client->listener_watch = 0;
1198 dhcp_client->retry_times = 0;
1199 dhcp_client->ack_retry_times = 0;
1200 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1201 g_direct_equal, NULL, remove_option_value);
1202 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1203 g_direct_equal, NULL, g_free);
1204 dhcp_client->request_list = NULL;
1205 dhcp_client->require_list = NULL;
1206 dhcp_client->duid = NULL;
1207 dhcp_client->duid_len = 0;
1208 dhcp_client->last_request = time(NULL);
1209 dhcp_client->expire = 0;
1210 dhcp_client->request_bcast = false;
1212 *error = G_DHCP_CLIENT_ERROR_NONE;
1217 g_free(dhcp_client->interface);
1218 g_free(dhcp_client);
1222 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1224 static int dhcp_l2_socket(int ifindex)
1227 struct sockaddr_ll sock;
1232 * I've selected not to see LL header, so BPF doesn't see it, too.
1233 * The filter may also pass non-IP and non-ARP packets, but we do
1234 * a more complete check when receiving the message in userspace.
1236 * and filter shamelessly stolen from:
1238 * http://www.flamewarmaster.de/software/dhcpclient/
1240 * There are a few other interesting ideas on that page (look under
1241 * "Motivation"). Use of netlink events is most interesting. Think
1242 * of various network servers listening for events and reconfiguring.
1243 * That would obsolete sending HUP signals and/or make use of restarts.
1245 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1248 * TODO: make conditional?
1250 static const struct sock_filter filter_instr[] = {
1252 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1253 /* L5, L1, is UDP? */
1254 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1255 /* ugly check for arp on ethernet-like and IPv4 */
1256 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1257 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1258 /* skip IP header */
1259 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1260 /* check udp source and destination ports */
1261 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1263 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1265 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1266 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1269 static const struct sock_fprog filter_prog = {
1270 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1271 /* casting const away: */
1272 .filter = (struct sock_filter *) filter_instr,
1275 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
1279 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1280 /* Use only if standard ports are in use */
1281 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1282 sizeof(filter_prog));
1284 memset(&sock, 0, sizeof(sock));
1285 sock.sll_family = AF_PACKET;
1286 sock.sll_protocol = htons(ETH_P_IP);
1287 sock.sll_ifindex = ifindex;
1289 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1298 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1300 if (packet->ip.protocol != IPPROTO_UDP)
1303 if (packet->ip.version != IPVERSION)
1306 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1309 if (packet->udp.dest != htons(CLIENT_PORT))
1312 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1318 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1319 struct sockaddr_in *dst_addr)
1322 struct ip_udp_dhcp_packet packet;
1325 memset(&packet, 0, sizeof(packet));
1327 bytes = read(fd, &packet, sizeof(packet));
1331 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1334 if (bytes < ntohs(packet.ip.tot_len))
1335 /* packet is bigger than sizeof(packet), we did partial read */
1338 /* ignore any extra garbage bytes */
1339 bytes = ntohs(packet.ip.tot_len);
1341 if (!sanity_check(&packet, bytes))
1344 check = packet.ip.check;
1345 packet.ip.check = 0;
1346 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1349 /* verify UDP checksum. IP header has to be modified for this */
1350 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1351 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1352 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1353 check = packet.udp.check;
1354 packet.udp.check = 0;
1355 if (check && check != dhcp_checksum(&packet, bytes))
1358 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1359 sizeof(packet.udp)));
1361 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1364 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1366 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1369 static void ipv4ll_start(GDHCPClient *dhcp_client)
1373 remove_timeouts(dhcp_client);
1375 switch_listening_mode(dhcp_client, L_NONE);
1376 dhcp_client->retry_times = 0;
1377 dhcp_client->requested_ip = 0;
1379 dhcp_client->requested_ip = ipv4ll_random_ip();
1381 /*first wait a random delay to avoid storm of arp request on boot*/
1382 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1384 dhcp_client->retry_times++;
1385 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1392 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1395 switch_listening_mode(dhcp_client, L_NONE);
1397 remove_timeouts(dhcp_client);
1399 if (dhcp_client->listener_watch > 0) {
1400 g_source_remove(dhcp_client->listener_watch);
1401 dhcp_client->listener_watch = 0;
1404 dhcp_client->state = IPV4LL_PROBE;
1405 dhcp_client->retry_times = 0;
1406 dhcp_client->requested_ip = 0;
1408 g_free(dhcp_client->assigned_ip);
1409 dhcp_client->assigned_ip = NULL;
1412 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1415 struct ether_arp arp;
1416 uint32_t ip_requested;
1417 int source_conflict;
1418 int target_conflict;
1420 memset(&arp, 0, sizeof(arp));
1421 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1425 if (arp.arp_op != htons(ARPOP_REPLY) &&
1426 arp.arp_op != htons(ARPOP_REQUEST))
1429 ip_requested = htonl(dhcp_client->requested_ip);
1430 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1431 sizeof(ip_requested));
1433 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1434 sizeof(ip_requested));
1436 if (!source_conflict && !target_conflict)
1439 dhcp_client->conflicts++;
1441 debug(dhcp_client, "IPV4LL conflict detected");
1443 if (dhcp_client->state == IPV4LL_MONITOR) {
1444 if (!source_conflict)
1446 dhcp_client->state = IPV4LL_DEFEND;
1447 debug(dhcp_client, "DEFEND mode conflicts : %d",
1448 dhcp_client->conflicts);
1449 /*Try to defend with a single announce*/
1450 send_announce_packet(dhcp_client);
1454 if (dhcp_client->state == IPV4LL_DEFEND) {
1455 if (!source_conflict)
1457 else if (dhcp_client->ipv4ll_lost_cb)
1458 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1459 dhcp_client->ipv4ll_lost_data);
1462 ipv4ll_stop(dhcp_client);
1464 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1465 /*restart whole state machine*/
1466 dhcp_client->retry_times++;
1467 dhcp_client->timeout =
1468 g_timeout_add_full(G_PRIORITY_HIGH,
1469 ipv4ll_random_delay_ms(PROBE_WAIT),
1474 /* Here we got a lot of conflicts, RFC3927 states that we have
1475 * to wait RATE_LIMIT_INTERVAL before retrying,
1476 * but we just report failure.
1478 else if (dhcp_client->no_lease_cb)
1479 dhcp_client->no_lease_cb(dhcp_client,
1480 dhcp_client->no_lease_data);
1485 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1487 if (dhcp_client->type == G_DHCP_IPV6) {
1488 struct dhcpv6_packet *packet6 = pkt;
1494 xid = packet6->transaction_id[0] << 16 |
1495 packet6->transaction_id[1] << 8 |
1496 packet6->transaction_id[2];
1498 if (xid != dhcp_client->xid)
1501 struct dhcp_packet *packet = pkt;
1503 if (packet->xid != dhcp_client->xid)
1506 if (packet->hlen != 6)
1509 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1516 static void start_request(GDHCPClient *dhcp_client);
1518 static gboolean request_timeout(gpointer user_data)
1520 GDHCPClient *dhcp_client = user_data;
1522 #if defined TIZEN_EXT
1523 if (dhcp_client->init_reboot) {
1524 debug(dhcp_client, "DHCPREQUEST of INIT-REBOOT has failed");
1526 /* Start DHCPDISCOVERY when DHCPREQUEST of INIT-REBOOT has failed */
1527 g_dhcp_client_set_address_known(dhcp_client, FALSE);
1529 dhcp_client->retry_times = 0;
1530 dhcp_client->requested_ip = 0;
1532 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1537 debug(dhcp_client, "request timeout (retries %d)",
1538 dhcp_client->retry_times);
1540 dhcp_client->retry_times++;
1542 start_request(dhcp_client);
1547 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1548 gpointer user_data);
1550 static int switch_listening_mode(GDHCPClient *dhcp_client,
1551 ListenMode listen_mode)
1553 GIOChannel *listener_channel;
1554 int listener_sockfd;
1556 if (dhcp_client->listen_mode == listen_mode)
1559 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1560 dhcp_client->listen_mode, listen_mode);
1562 if (dhcp_client->listen_mode != L_NONE) {
1563 if (dhcp_client->listener_watch > 0)
1564 g_source_remove(dhcp_client->listener_watch);
1565 dhcp_client->listen_mode = L_NONE;
1566 dhcp_client->listener_sockfd = -1;
1567 dhcp_client->listener_watch = 0;
1570 if (listen_mode == L_NONE)
1573 if (listen_mode == L2)
1574 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1575 else if (listen_mode == L3) {
1576 if (dhcp_client->type == G_DHCP_IPV6)
1577 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1578 dhcp_client->interface,
1581 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1582 dhcp_client->interface,
1584 } else if (listen_mode == L_ARP)
1585 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1589 if (listener_sockfd < 0)
1592 listener_channel = g_io_channel_unix_new(listener_sockfd);
1593 if (!listener_channel) {
1594 /* Failed to create listener channel */
1595 close(listener_sockfd);
1599 dhcp_client->listen_mode = listen_mode;
1600 dhcp_client->listener_sockfd = listener_sockfd;
1602 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1603 dhcp_client->listener_watch =
1604 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1605 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1606 listener_event, dhcp_client,
1608 g_io_channel_unref(listener_channel);
1613 static void start_request(GDHCPClient *dhcp_client)
1615 debug(dhcp_client, "start request (retries %d)",
1616 dhcp_client->retry_times);
1618 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1619 if (dhcp_client->no_lease_cb)
1620 dhcp_client->no_lease_cb(dhcp_client,
1621 dhcp_client->no_lease_data);
1625 if (dhcp_client->retry_times == 0) {
1626 dhcp_client->state = REQUESTING;
1627 switch_listening_mode(dhcp_client, L2);
1630 send_request(dhcp_client);
1632 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1639 static uint32_t get_lease(struct dhcp_packet *packet)
1642 uint32_t lease_seconds;
1644 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1648 lease_seconds = get_be32(option);
1649 /* paranoia: must not be prone to overflows */
1650 lease_seconds &= 0x0fffffff;
1651 if (lease_seconds < 10)
1654 return lease_seconds;
1657 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1659 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1661 remove_timeouts(dhcp_client);
1663 dhcp_client->retry_times = retry_times;
1664 dhcp_client->requested_ip = 0;
1665 dhcp_client->state = INIT_SELECTING;
1666 switch_listening_mode(dhcp_client, L2);
1668 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1671 static gboolean start_expire(gpointer user_data)
1673 GDHCPClient *dhcp_client = user_data;
1675 debug(dhcp_client, "lease expired");
1677 /*remove all timeouts if they are set*/
1678 remove_timeouts(dhcp_client);
1680 restart_dhcp(dhcp_client, 0);
1682 /* ip need to be cleared */
1683 if (dhcp_client->lease_lost_cb)
1684 dhcp_client->lease_lost_cb(dhcp_client,
1685 dhcp_client->lease_lost_data);
1690 static gboolean continue_rebound(gpointer user_data)
1692 GDHCPClient *dhcp_client = user_data;
1695 switch_listening_mode(dhcp_client, L2);
1696 send_request(dhcp_client);
1698 if (dhcp_client->t2_timeout> 0)
1699 g_source_remove(dhcp_client->t2_timeout);
1701 /*recalculate remaining rebind time*/
1702 dhcp_client->T2 >>= 1;
1703 if (dhcp_client->T2 > 60) {
1704 dhcp_get_random(&rand);
1705 dhcp_client->t2_timeout =
1706 g_timeout_add_full(G_PRIORITY_HIGH,
1707 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1716 static gboolean start_rebound(gpointer user_data)
1718 GDHCPClient *dhcp_client = user_data;
1720 /*remove renew timer*/
1721 if (dhcp_client->t1_timeout > 0)
1722 g_source_remove(dhcp_client->t1_timeout);
1724 debug(dhcp_client, "start rebound");
1725 dhcp_client->state = REBINDING;
1727 /*calculate total rebind time*/
1728 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1730 /*send the first rebound and reschedule*/
1731 continue_rebound(user_data);
1736 static gboolean continue_renew (gpointer user_data)
1738 GDHCPClient *dhcp_client = user_data;
1741 switch_listening_mode(dhcp_client, L3);
1742 send_request(dhcp_client);
1744 if (dhcp_client->t1_timeout > 0)
1745 g_source_remove(dhcp_client->t1_timeout);
1747 dhcp_client->t1_timeout = 0;
1749 dhcp_client->T1 >>= 1;
1751 if (dhcp_client->T1 > 60) {
1752 dhcp_get_random(&rand);
1753 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1754 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1762 static gboolean start_renew(gpointer user_data)
1764 GDHCPClient *dhcp_client = user_data;
1766 debug(dhcp_client, "start renew");
1767 dhcp_client->state = RENEWING;
1769 /*calculate total renew period*/
1770 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1772 /*send first renew and reschedule for half the remaining time.*/
1773 continue_renew(user_data);
1778 static void start_bound(GDHCPClient *dhcp_client)
1780 debug(dhcp_client, "start bound");
1782 dhcp_client->state = BOUND;
1784 remove_timeouts(dhcp_client);
1787 *TODO: T1 and T2 should be set through options instead of
1788 * defaults as they are here.
1791 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1792 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1793 dhcp_client->expire = dhcp_client->lease_seconds;
1795 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1797 start_renew, dhcp_client,
1800 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1802 start_rebound, dhcp_client,
1805 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1806 dhcp_client->expire,
1807 start_expire, dhcp_client,
1811 static gboolean restart_dhcp_timeout(gpointer user_data)
1813 GDHCPClient *dhcp_client = user_data;
1815 debug(dhcp_client, "restart DHCP timeout");
1817 if (dhcp_client->state == REBOOTING) {
1818 g_free(dhcp_client->last_address);
1819 dhcp_client->last_address = NULL;
1820 restart_dhcp(dhcp_client, 0);
1822 dhcp_client->ack_retry_times++;
1823 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1828 static char *get_ip(uint32_t ip)
1830 struct in_addr addr;
1834 return g_strdup(inet_ntoa(addr));
1837 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1845 if (type == OPTION_STRING)
1846 return g_list_append(list, g_strdup(value));
1848 while ((pos = strchr(pos, ' '))) {
1851 list = g_list_append(list, g_strdup(value));
1856 list = g_list_append(list, g_strdup(value));
1861 static inline uint32_t get_uint32(unsigned char *value)
1863 return value[0] << 24 | value[1] << 16 |
1864 value[2] << 8 | value[3];
1867 static inline uint16_t get_uint16(unsigned char *value)
1869 return value[0] << 8 | value[1];
1872 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1873 struct in6_addr *addr,
1874 unsigned char prefixlen, uint32_t preferred,
1877 GDHCPIAPrefix *ia_prefix;
1879 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1883 if (dhcp_client->debug_func) {
1884 char addr_str[INET6_ADDRSTRLEN + 1];
1885 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1886 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1887 addr_str, prefixlen, preferred, valid);
1890 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1891 ia_prefix->prefixlen = prefixlen;
1892 ia_prefix->preferred = preferred;
1893 ia_prefix->valid = valid;
1894 ia_prefix->expire = time(NULL) + valid;
1896 return g_list_prepend(list, ia_prefix);
1899 static GList *get_addresses(GDHCPClient *dhcp_client,
1901 unsigned char *value,
1905 struct in6_addr addr;
1906 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1907 uint16_t option_len, option_code, st = 0, max_len;
1908 int addr_count = 0, prefix_count = 0, i, pos;
1909 unsigned char prefixlen;
1910 unsigned int shortest_valid = 0;
1914 if (!value || len < 4)
1917 iaid = get_uint32(&value[0]);
1918 if (dhcp_client->iaid != iaid)
1921 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
1922 T1 = get_uint32(&value[4]);
1923 T2 = get_uint32(&value[8]);
1926 /* IA_NA: RFC 3315, 22.4 */
1927 /* IA_PD: RFC 3633, ch 9 */
1937 max_len = len - pos;
1939 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
1941 /* We have more sub-options in this packet. */
1943 option = dhcpv6_get_sub_option(&value[pos], max_len,
1944 &option_code, &option_len);
1946 debug(dhcp_client, "pos %d option %p code %d len %d",
1947 pos, option, option_code, option_len);
1955 switch (option_code) {
1956 case G_DHCPV6_IAADDR:
1958 memcpy(&addr, &option[0], sizeof(addr));
1960 preferred = get_uint32(&option[i]);
1962 valid = get_uint32(&option[i]);
1967 case G_DHCPV6_STATUS_CODE:
1968 st = get_uint16(&option[0]);
1969 debug(dhcp_client, "error code %d", st);
1970 if (option_len > 2) {
1971 str = g_strndup((gchar *)&option[2],
1973 debug(dhcp_client, "error text: %s", str);
1980 case G_DHCPV6_IA_PREFIX:
1982 preferred = get_uint32(&option[i]);
1984 valid = get_uint32(&option[i]);
1986 prefixlen = option[i];
1988 memcpy(&addr, &option[i], sizeof(addr));
1990 if (preferred < valid) {
1991 /* RFC 3633, ch 10 */
1992 list = add_prefix(dhcp_client, list, &addr,
1993 prefixlen, preferred, valid);
1994 if (shortest_valid > valid)
1995 shortest_valid = valid;
2001 pos += 2 + 2 + option_len;
2003 } while (pos < len);
2005 if (addr_count > 0 && st == 0) {
2006 /* We only support one address atm */
2007 char addr_str[INET6_ADDRSTRLEN + 1];
2009 if (preferred > valid)
2010 /* RFC 3315, 22.6 */
2013 dhcp_client->T1 = T1;
2014 dhcp_client->T2 = T2;
2016 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2017 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2018 addr_count, addr_str, T1, T2);
2020 list = g_list_append(list, g_strdup(addr_str));
2022 if (code == G_DHCPV6_IA_NA)
2023 memcpy(&dhcp_client->ia_na, &addr,
2024 sizeof(struct in6_addr));
2026 memcpy(&dhcp_client->ia_ta, &addr,
2027 sizeof(struct in6_addr));
2029 if (valid != dhcp_client->expire)
2030 dhcp_client->expire = valid;
2033 if (prefix_count > 0 && list) {
2035 * This means we have a list of prefixes to delegate.
2037 list = g_list_reverse(list);
2039 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2040 prefix_count, T1, T2);
2042 dhcp_client->T1 = T1;
2043 dhcp_client->T2 = T2;
2045 dhcp_client->expire = shortest_valid;
2048 if (status && *status != 0)
2049 debug(dhcp_client, "status %d", *status);
2054 static GList *get_domains(int maxlen, unsigned char *value)
2060 char dns_name[NS_MAXDNAME + 1];
2062 if (!value || maxlen < 3)
2065 while (pos < maxlen) {
2066 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2068 c = (unsigned char *)dns_name;
2075 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2076 pos += (char *)c - dns_name + 1;
2079 return g_list_reverse(list);
2082 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2084 unsigned char *value,
2095 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2096 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2099 "%s server list length (%d) is invalid",
2100 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2104 for (i = 0; i < len; i += 16) {
2106 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2110 if (!inet_ntop(AF_INET6, &value[i], str,
2114 list = g_list_append(list, str);
2118 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2119 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2120 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2121 list = get_addresses(dhcp_client, code, len, value, status);
2124 case G_DHCPV6_DOMAIN_LIST:
2125 list = get_domains(len, value);
2135 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2136 struct dhcpv6_packet *packet,
2137 uint16_t pkt_len, uint16_t *status)
2139 GList *list, *value_list;
2142 uint16_t option_len;
2144 for (list = dhcp_client->request_list; list; list = list->next) {
2145 code = (uint16_t) GPOINTER_TO_INT(list->data);
2147 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2150 g_hash_table_remove(dhcp_client->code_value_hash,
2151 GINT_TO_POINTER((int) code));
2155 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2156 option_len, option, status);
2158 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2159 option_len, value_list);
2162 g_hash_table_remove(dhcp_client->code_value_hash,
2163 GINT_TO_POINTER((int) code));
2165 g_hash_table_insert(dhcp_client->code_value_hash,
2166 GINT_TO_POINTER((int) code), value_list);
2170 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2172 GDHCPOptionType type;
2173 GList *list, *value_list;
2178 for (list = dhcp_client->request_list; list; list = list->next) {
2179 code = (uint8_t) GPOINTER_TO_INT(list->data);
2181 option = dhcp_get_option(packet, code);
2183 g_hash_table_remove(dhcp_client->code_value_hash,
2184 GINT_TO_POINTER((int) code));
2188 type = dhcp_get_code_type(code);
2190 option_value = malloc_option_value_string(option, type);
2192 g_hash_table_remove(dhcp_client->code_value_hash,
2193 GINT_TO_POINTER((int) code));
2195 value_list = get_option_value_list(option_value, type);
2197 g_free(option_value);
2200 g_hash_table_remove(dhcp_client->code_value_hash,
2201 GINT_TO_POINTER((int) code));
2203 g_hash_table_insert(dhcp_client->code_value_hash,
2204 GINT_TO_POINTER((int) code), value_list);
2208 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2211 GDHCPClient *dhcp_client = user_data;
2212 struct sockaddr_in dst_addr = { 0 };
2213 struct dhcp_packet packet;
2214 struct dhcpv6_packet *packet6 = NULL;
2215 uint8_t *message_type = NULL, *client_id = NULL, *option,
2217 uint16_t option_len = 0, status = 0;
2220 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2221 uint16_t pkt_len = 0;
2225 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2226 dhcp_client->listener_watch = 0;
2230 if (dhcp_client->listen_mode == L_NONE)
2235 dhcp_client->status_code = 0;
2237 if (dhcp_client->listen_mode == L2) {
2238 re = dhcp_recv_l2_packet(&packet,
2239 dhcp_client->listener_sockfd,
2242 } else if (dhcp_client->listen_mode == L3) {
2243 if (dhcp_client->type == G_DHCP_IPV6) {
2244 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2245 dhcp_client->listener_sockfd);
2248 xid = packet6->transaction_id[0] << 16 |
2249 packet6->transaction_id[1] << 8 |
2250 packet6->transaction_id[2];
2252 re = dhcp_recv_l3_packet(&packet,
2253 dhcp_client->listener_sockfd);
2256 } else if (dhcp_client->listen_mode == L_ARP) {
2257 ipv4ll_recv_arp_packet(dhcp_client);
2265 if (!check_package_owner(dhcp_client, pkt))
2268 if (dhcp_client->type == G_DHCP_IPV6) {
2273 client_id = dhcpv6_get_option(packet6, pkt_len,
2274 G_DHCPV6_CLIENTID, &option_len, &count);
2276 if (!client_id || count == 0 || option_len == 0 ||
2277 memcmp(dhcp_client->duid, client_id,
2278 dhcp_client->duid_len) != 0) {
2280 "client duid error, discarding msg %p/%d/%d",
2281 client_id, option_len, count);
2285 option = dhcpv6_get_option(packet6, pkt_len,
2286 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2287 if (option != 0 && option_len > 0) {
2288 status = option[0]<<8 | option[1];
2290 debug(dhcp_client, "error code %d", status);
2291 if (option_len > 2) {
2292 gchar *txt = g_strndup(
2293 (gchar *)&option[2],
2295 debug(dhcp_client, "error text: %s",
2300 dhcp_client->status_code = status;
2303 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2308 if (!message_type && !client_id)
2309 /* No message type / client id option, ignore package */
2312 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2313 "(current state %d)", ntohl(xid), dhcp_client->state);
2315 switch (dhcp_client->state) {
2316 case INIT_SELECTING:
2317 if (*message_type != DHCPOFFER)
2320 remove_timeouts(dhcp_client);
2321 dhcp_client->timeout = 0;
2322 dhcp_client->retry_times = 0;
2324 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2325 dhcp_client->server_ip = get_be32(option);
2326 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2328 dhcp_client->state = REQUESTING;
2330 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2331 dhcp_client->request_bcast = true;
2333 dhcp_client->request_bcast = false;
2335 debug(dhcp_client, "init ip %s -> %sadding broadcast flag",
2336 inet_ntoa(dst_addr.sin_addr),
2337 dhcp_client->request_bcast ? "" : "not ");
2339 start_request(dhcp_client);
2343 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2344 dhcp_client->request_bcast = true;
2346 dhcp_client->request_bcast = false;
2348 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2349 inet_ntoa(dst_addr.sin_addr),
2350 dhcp_client->request_bcast ? "" : "not ");
2355 if (*message_type == DHCPACK) {
2356 dhcp_client->retry_times = 0;
2358 remove_timeouts(dhcp_client);
2360 dhcp_client->lease_seconds = get_lease(&packet);
2362 get_request(dhcp_client, &packet);
2364 switch_listening_mode(dhcp_client, L_NONE);
2366 g_free(dhcp_client->assigned_ip);
2367 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2369 if (dhcp_client->state == REBOOTING) {
2370 option = dhcp_get_option(&packet,
2372 dhcp_client->server_ip = get_be32(option);
2375 /* Address should be set up here */
2376 if (dhcp_client->lease_available_cb)
2377 dhcp_client->lease_available_cb(dhcp_client,
2378 dhcp_client->lease_available_data);
2380 start_bound(dhcp_client);
2381 } else if (*message_type == DHCPNAK) {
2382 dhcp_client->retry_times = 0;
2384 remove_timeouts(dhcp_client);
2386 #if defined TIZEN_EXT
2387 if (dhcp_client->init_reboot) {
2388 g_dhcp_client_set_address_known(dhcp_client, FALSE);
2389 dhcp_client->timeout = g_idle_add_full(
2391 restart_dhcp_timeout,
2398 dhcp_client->timeout = g_timeout_add_seconds_full(
2400 restart_dhcp_timeout,
2407 if (dhcp_client->type != G_DHCP_IPV6)
2410 if (packet6->message != DHCPV6_REPLY &&
2411 packet6->message != DHCPV6_ADVERTISE)
2415 server_id = dhcpv6_get_option(packet6, pkt_len,
2416 G_DHCPV6_SERVERID, &option_len, &count);
2417 if (!server_id || count != 1 || option_len == 0) {
2418 /* RFC 3315, 15.10 */
2420 "server duid error, discarding msg %p/%d/%d",
2421 server_id, option_len, count);
2424 dhcp_client->server_duid = g_try_malloc(option_len);
2425 if (!dhcp_client->server_duid)
2427 memcpy(dhcp_client->server_duid, server_id, option_len);
2428 dhcp_client->server_duid_len = option_len;
2430 if (packet6->message == DHCPV6_REPLY) {
2431 uint8_t *rapid_commit;
2434 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2435 G_DHCPV6_RAPID_COMMIT,
2436 &option_len, &count);
2437 if (!rapid_commit || option_len != 0 ||
2439 /* RFC 3315, 17.1.4 */
2443 switch_listening_mode(dhcp_client, L_NONE);
2445 if (dhcp_client->status_code == 0)
2446 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2447 &dhcp_client->status_code);
2449 if (packet6->message == DHCPV6_ADVERTISE) {
2450 if (dhcp_client->advertise_cb)
2451 dhcp_client->advertise_cb(dhcp_client,
2452 dhcp_client->advertise_data);
2456 if (dhcp_client->solicitation_cb) {
2458 * The dhcp_client might not be valid after the
2459 * callback call so just return immediately.
2461 dhcp_client->solicitation_cb(dhcp_client,
2462 dhcp_client->solicitation_data);
2467 if (dhcp_client->type != G_DHCP_IPV6)
2470 server_id = dhcpv6_get_option(packet6, pkt_len,
2471 G_DHCPV6_SERVERID, &option_len, &count);
2472 if (!dhcp_client->server_duid && server_id &&
2475 * If we do not have server duid yet, then get it now.
2476 * Prefix delegation renew support needs it.
2478 dhcp_client->server_duid = g_try_malloc(option_len);
2479 if (!dhcp_client->server_duid)
2481 memcpy(dhcp_client->server_duid, server_id, option_len);
2482 dhcp_client->server_duid_len = option_len;
2485 case INFORMATION_REQ:
2491 if (dhcp_client->type != G_DHCP_IPV6)
2494 if (packet6->message != DHCPV6_REPLY)
2499 server_id = dhcpv6_get_option(packet6, pkt_len,
2500 G_DHCPV6_SERVERID, &option_len, &count);
2501 if (!server_id || count != 1 || option_len == 0 ||
2502 (dhcp_client->server_duid_len > 0 &&
2503 memcmp(dhcp_client->server_duid, server_id,
2504 dhcp_client->server_duid_len) != 0)) {
2505 /* RFC 3315, 15.10 */
2507 "server duid error, discarding msg %p/%d/%d",
2508 server_id, option_len, count);
2512 switch_listening_mode(dhcp_client, L_NONE);
2514 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2515 &dhcp_client->status_code);
2517 if (dhcp_client->information_req_cb) {
2519 * The dhcp_client might not be valid after the
2520 * callback call so just return immediately.
2522 dhcp_client->information_req_cb(dhcp_client,
2523 dhcp_client->information_req_data);
2526 if (dhcp_client->request_cb) {
2527 dhcp_client->request_cb(dhcp_client,
2528 dhcp_client->request_data);
2531 if (dhcp_client->renew_cb) {
2532 dhcp_client->renew_cb(dhcp_client,
2533 dhcp_client->renew_data);
2536 if (dhcp_client->rebind_cb) {
2537 dhcp_client->rebind_cb(dhcp_client,
2538 dhcp_client->rebind_data);
2541 if (dhcp_client->release_cb) {
2542 dhcp_client->release_cb(dhcp_client,
2543 dhcp_client->release_data);
2546 if (dhcp_client->decline_cb) {
2547 dhcp_client->decline_cb(dhcp_client,
2548 dhcp_client->decline_data);
2551 if (dhcp_client->confirm_cb) {
2553 server_id = dhcpv6_get_option(packet6, pkt_len,
2554 G_DHCPV6_SERVERID, &option_len,
2556 if (!server_id || count != 1 ||
2558 /* RFC 3315, 15.10 */
2560 "confirm server duid error, "
2561 "discarding msg %p/%d/%d",
2562 server_id, option_len, count);
2565 dhcp_client->server_duid = g_try_malloc(option_len);
2566 if (!dhcp_client->server_duid)
2568 memcpy(dhcp_client->server_duid, server_id, option_len);
2569 dhcp_client->server_duid_len = option_len;
2571 dhcp_client->confirm_cb(dhcp_client,
2572 dhcp_client->confirm_data);
2580 debug(dhcp_client, "processed DHCP packet (new state %d)",
2581 dhcp_client->state);
2586 static gboolean discover_timeout(gpointer user_data)
2588 GDHCPClient *dhcp_client = user_data;
2590 dhcp_client->retry_times++;
2593 * We do not send the REQUESTED IP option if we are retrying because
2594 * if the server is non-authoritative it will ignore the request if the
2595 * option is present.
2597 g_dhcp_client_start(dhcp_client, NULL);
2602 static gboolean reboot_timeout(gpointer user_data)
2604 GDHCPClient *dhcp_client = user_data;
2605 dhcp_client->retry_times = 0;
2606 dhcp_client->requested_ip = 0;
2607 dhcp_client->state = INIT_SELECTING;
2609 * We do not send the REQUESTED IP option because the server didn't
2610 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2613 g_dhcp_client_start(dhcp_client, NULL);
2618 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2620 GDHCPClient *dhcp_client = dhcp_data;
2622 debug(dhcp_client, "back to MONITOR mode");
2624 dhcp_client->conflicts = 0;
2625 dhcp_client->state = IPV4LL_MONITOR;
2630 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2632 GDHCPClient *dhcp_client = dhcp_data;
2635 #if defined TIZEN_EXT
2640 debug(dhcp_client, "request timeout (retries %d)",
2641 dhcp_client->retry_times);
2643 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2644 dhcp_client->retry_times++;
2645 send_announce_packet(dhcp_client);
2649 ip = htonl(dhcp_client->requested_ip);
2650 debug(dhcp_client, "switching to monitor mode");
2651 dhcp_client->state = IPV4LL_MONITOR;
2652 dhcp_client->assigned_ip = get_ip(ip);
2654 if (dhcp_client->ipv4ll_available_cb)
2655 dhcp_client->ipv4ll_available_cb(dhcp_client,
2656 dhcp_client->ipv4ll_available_data);
2657 dhcp_client->conflicts = 0;
2658 dhcp_client->timeout = 0;
2663 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2666 GDHCPClient *dhcp_client = dhcp_data;
2668 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2669 dhcp_client->retry_times);
2671 if (dhcp_client->retry_times == PROBE_NUM) {
2672 dhcp_client->state = IPV4LL_ANNOUNCE;
2673 dhcp_client->retry_times = 0;
2675 dhcp_client->retry_times++;
2676 send_announce_packet(dhcp_client);
2679 dhcp_client->retry_times++;
2680 send_probe_packet(dhcp_client);
2685 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2691 remove_timeouts(dhcp_client);
2693 if (dhcp_client->type == G_DHCP_IPV6) {
2694 if (dhcp_client->information_req_cb) {
2695 dhcp_client->state = INFORMATION_REQ;
2696 re = switch_listening_mode(dhcp_client, L3);
2698 switch_listening_mode(dhcp_client, L_NONE);
2699 dhcp_client->state = 0;
2702 send_information_req(dhcp_client);
2704 } else if (dhcp_client->solicitation_cb) {
2705 dhcp_client->state = SOLICITATION;
2706 re = switch_listening_mode(dhcp_client, L3);
2708 switch_listening_mode(dhcp_client, L_NONE);
2709 dhcp_client->state = 0;
2712 send_solicitation(dhcp_client);
2714 } else if (dhcp_client->request_cb) {
2715 dhcp_client->state = REQUEST;
2716 re = switch_listening_mode(dhcp_client, L3);
2718 switch_listening_mode(dhcp_client, L_NONE);
2719 dhcp_client->state = 0;
2722 send_dhcpv6_request(dhcp_client);
2724 } else if (dhcp_client->confirm_cb) {
2725 dhcp_client->state = CONFIRM;
2726 re = switch_listening_mode(dhcp_client, L3);
2728 switch_listening_mode(dhcp_client, L_NONE);
2729 dhcp_client->state = 0;
2732 send_dhcpv6_confirm(dhcp_client);
2734 } else if (dhcp_client->renew_cb) {
2735 dhcp_client->state = RENEW;
2736 re = switch_listening_mode(dhcp_client, L3);
2738 switch_listening_mode(dhcp_client, L_NONE);
2739 dhcp_client->state = 0;
2742 send_dhcpv6_renew(dhcp_client);
2744 } else if (dhcp_client->rebind_cb) {
2745 dhcp_client->state = REBIND;
2746 re = switch_listening_mode(dhcp_client, L3);
2748 switch_listening_mode(dhcp_client, L_NONE);
2749 dhcp_client->state = 0;
2752 send_dhcpv6_rebind(dhcp_client);
2754 } else if (dhcp_client->release_cb) {
2755 dhcp_client->state = RENEW;
2756 re = switch_listening_mode(dhcp_client, L3);
2758 switch_listening_mode(dhcp_client, L_NONE);
2759 dhcp_client->state = 0;
2762 send_dhcpv6_release(dhcp_client);
2763 } else if (dhcp_client->decline_cb) {
2764 dhcp_client->state = DECLINE;
2765 re = switch_listening_mode(dhcp_client, L3);
2767 switch_listening_mode(dhcp_client, L_NONE);
2768 dhcp_client->state = 0;
2771 send_dhcpv6_decline(dhcp_client);
2777 if (dhcp_client->type == G_DHCP_IPV4LL) {
2778 dhcp_client->state = INIT_SELECTING;
2779 ipv4ll_start(dhcp_client);
2783 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2784 if (dhcp_client->no_lease_cb)
2785 dhcp_client->no_lease_cb(dhcp_client,
2786 dhcp_client->no_lease_data);
2787 dhcp_client->retry_times = 0;
2791 if (dhcp_client->retry_times == 0) {
2792 g_free(dhcp_client->assigned_ip);
2793 dhcp_client->assigned_ip = NULL;
2795 dhcp_client->state = INIT_SELECTING;
2796 re = switch_listening_mode(dhcp_client, L2);
2800 dhcp_get_random(&rand);
2801 dhcp_client->xid = rand;
2802 dhcp_client->start = time(NULL);
2805 if (!last_address) {
2808 addr = ntohl(inet_addr(last_address));
2809 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2812 } else if (dhcp_client->last_address != last_address) {
2813 g_free(dhcp_client->last_address);
2814 dhcp_client->last_address = g_strdup(last_address);
2818 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2819 debug(dhcp_client, "DHCP client start with state init_reboot");
2820 dhcp_client->requested_ip = addr;
2821 dhcp_client->state = REBOOTING;
2822 send_request(dhcp_client);
2824 dhcp_client->timeout = g_timeout_add_seconds_full(
2832 send_discover(dhcp_client, addr);
2834 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2842 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2844 switch_listening_mode(dhcp_client, L_NONE);
2846 if (dhcp_client->state == BOUND ||
2847 dhcp_client->state == RENEWING ||
2848 dhcp_client->state == REBINDING)
2849 send_release(dhcp_client, dhcp_client->server_ip,
2850 dhcp_client->requested_ip);
2852 remove_timeouts(dhcp_client);
2854 if (dhcp_client->listener_watch > 0) {
2855 g_source_remove(dhcp_client->listener_watch);
2856 dhcp_client->listener_watch = 0;
2859 dhcp_client->retry_times = 0;
2860 dhcp_client->ack_retry_times = 0;
2862 dhcp_client->requested_ip = 0;
2863 dhcp_client->state = RELEASED;
2864 dhcp_client->lease_seconds = 0;
2865 dhcp_client->request_bcast = false;
2868 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2869 unsigned char option_code)
2871 return g_hash_table_lookup(dhcp_client->code_value_hash,
2872 GINT_TO_POINTER((int) option_code));
2875 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2876 GDHCPClientEvent event,
2877 GDHCPClientEventFunc func,
2881 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2882 dhcp_client->lease_available_cb = func;
2883 dhcp_client->lease_available_data = data;
2885 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2886 if (dhcp_client->type == G_DHCP_IPV6)
2888 dhcp_client->ipv4ll_available_cb = func;
2889 dhcp_client->ipv4ll_available_data = data;
2891 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2892 dhcp_client->no_lease_cb = func;
2893 dhcp_client->no_lease_data = data;
2895 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2896 dhcp_client->lease_lost_cb = func;
2897 dhcp_client->lease_lost_data = data;
2899 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2900 if (dhcp_client->type == G_DHCP_IPV6)
2902 dhcp_client->ipv4ll_lost_cb = func;
2903 dhcp_client->ipv4ll_lost_data = data;
2905 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2906 dhcp_client->address_conflict_cb = func;
2907 dhcp_client->address_conflict_data = data;
2909 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2910 if (dhcp_client->type != G_DHCP_IPV6)
2912 dhcp_client->information_req_cb = func;
2913 dhcp_client->information_req_data = data;
2915 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2916 if (dhcp_client->type != G_DHCP_IPV6)
2918 dhcp_client->solicitation_cb = func;
2919 dhcp_client->solicitation_data = data;
2921 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2922 if (dhcp_client->type != G_DHCP_IPV6)
2924 dhcp_client->advertise_cb = func;
2925 dhcp_client->advertise_data = data;
2927 case G_DHCP_CLIENT_EVENT_REQUEST:
2928 if (dhcp_client->type != G_DHCP_IPV6)
2930 dhcp_client->request_cb = func;
2931 dhcp_client->request_data = data;
2933 case G_DHCP_CLIENT_EVENT_RENEW:
2934 if (dhcp_client->type != G_DHCP_IPV6)
2936 dhcp_client->renew_cb = func;
2937 dhcp_client->renew_data = data;
2939 case G_DHCP_CLIENT_EVENT_REBIND:
2940 if (dhcp_client->type != G_DHCP_IPV6)
2942 dhcp_client->rebind_cb = func;
2943 dhcp_client->rebind_data = data;
2945 case G_DHCP_CLIENT_EVENT_RELEASE:
2946 if (dhcp_client->type != G_DHCP_IPV6)
2948 dhcp_client->release_cb = func;
2949 dhcp_client->release_data = data;
2951 case G_DHCP_CLIENT_EVENT_CONFIRM:
2952 if (dhcp_client->type != G_DHCP_IPV6)
2954 dhcp_client->confirm_cb = func;
2955 dhcp_client->confirm_data = data;
2957 case G_DHCP_CLIENT_EVENT_DECLINE:
2958 if (dhcp_client->type != G_DHCP_IPV6)
2960 dhcp_client->decline_cb = func;
2961 dhcp_client->decline_data = data;
2966 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2968 return dhcp_client->ifindex;
2971 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
2976 return get_ip(dhcp_client->server_ip);
2979 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2981 return g_strdup(dhcp_client->assigned_ip);
2984 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2986 GList *option = NULL;
2988 if (dhcp_client->type == G_DHCP_IPV6)
2991 switch (dhcp_client->state) {
2993 case IPV4LL_MONITOR:
2994 return g_strdup("255.255.0.0");
2998 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3000 return g_strdup(option->data);
3001 case INIT_SELECTING:
3006 case IPV4LL_ANNOUNCE:
3007 case INFORMATION_REQ:
3020 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3021 unsigned int option_code)
3023 if (!g_list_find(dhcp_client->request_list,
3024 GINT_TO_POINTER((int)option_code)))
3025 dhcp_client->request_list = g_list_prepend(
3026 dhcp_client->request_list,
3027 (GINT_TO_POINTER((int) option_code)));
3029 return G_DHCP_CLIENT_ERROR_NONE;
3032 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3034 g_list_free(dhcp_client->request_list);
3035 dhcp_client->request_list = NULL;
3038 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3040 g_hash_table_remove_all(dhcp_client->send_value_hash);
3043 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3047 storage = g_try_malloc(size + OPT_DATA);
3051 storage[OPT_CODE] = code;
3052 storage[OPT_LEN] = size;
3053 memcpy(&storage[OPT_DATA], data, size);
3058 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3061 return alloc_dhcp_option(code, data, MIN(size, 255));
3064 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3066 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3069 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3071 const unsigned maclen = 6;
3072 const unsigned idlen = maclen + 1;
3073 const uint8_t option_code = G_DHCP_CLIENT_ID;
3074 uint8_t idbuf[idlen];
3075 uint8_t *data_option;
3077 idbuf[0] = ARPHRD_ETHER;
3079 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3081 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3083 return G_DHCP_CLIENT_ERROR_NOMEM;
3085 g_hash_table_insert(dhcp_client->send_value_hash,
3086 GINT_TO_POINTER((int) option_code), data_option);
3088 return G_DHCP_CLIENT_ERROR_NONE;
3091 /* Now only support send hostname */
3092 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3093 unsigned char option_code, const char *option_value)
3095 uint8_t *binary_option;
3097 if (option_code == G_DHCP_HOST_NAME && option_value) {
3098 binary_option = alloc_dhcp_string_option(option_code,
3101 return G_DHCP_CLIENT_ERROR_NOMEM;
3103 g_hash_table_insert(dhcp_client->send_value_hash,
3104 GINT_TO_POINTER((int) option_code), binary_option);
3107 return G_DHCP_CLIENT_ERROR_NONE;
3110 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3115 storage = g_malloc(2 + 2 + len);
3119 storage[0] = code >> 8;
3120 storage[1] = code & 0xff;
3121 storage[2] = len >> 8;
3122 storage[3] = len & 0xff;
3123 memcpy(storage + 2 + 2, option, len);
3128 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3130 return g_hash_table_remove(dhcp_client->send_value_hash,
3131 GINT_TO_POINTER((int)code));
3134 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3135 uint16_t option_code,
3136 uint8_t *option_value,
3137 uint16_t option_len)
3140 uint8_t *binary_option;
3142 debug(dhcp_client, "setting option %d to %p len %d",
3143 option_code, option_value, option_len);
3145 binary_option = alloc_dhcpv6_option(option_code, option_value,
3148 g_hash_table_insert(dhcp_client->send_value_hash,
3149 GINT_TO_POINTER((int) option_code),
3154 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3156 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3159 dhcp_client->last_request = time(NULL);
3162 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3164 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3167 return dhcp_client->status_code;
3170 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3175 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3180 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3185 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3188 g_dhcp_client_stop(dhcp_client);
3190 g_free(dhcp_client->interface);
3191 g_free(dhcp_client->assigned_ip);
3192 g_free(dhcp_client->last_address);
3193 g_free(dhcp_client->duid);
3194 g_free(dhcp_client->server_duid);
3196 g_list_free(dhcp_client->request_list);
3197 g_list_free(dhcp_client->require_list);
3199 g_hash_table_destroy(dhcp_client->code_value_hash);
3200 g_hash_table_destroy(dhcp_client->send_value_hash);
3202 g_free(dhcp_client);
3203 #if defined TIZEN_EXT
3208 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3209 GDHCPDebugFunc func, gpointer user_data)
3214 dhcp_client->debug_func = func;
3215 dhcp_client->debug_data = user_data;
3218 static GDHCPIAPrefix *copy_prefix(gpointer data)
3220 GDHCPIAPrefix *copy, *prefix = data;
3222 copy = g_try_new(GDHCPIAPrefix, 1);
3226 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3231 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3233 GSList *copy = NULL;
3236 for (list = prefixes; list; list = list->next)
3237 copy = g_slist_prepend(copy, copy_prefix(list->data));
3242 #if defined TIZEN_EXT
3243 void g_dhcp_client_set_address_known(GDHCPClient *dhcp_client, gboolean known)
3245 /* DHCPREQUEST during INIT-REBOOT state (rfc2131)
3246 * 4.4.3 Initialization with known network address
3247 * 4.3.2 DHCPREQUEST generated during INIT-REBOOT state
3249 debug(dhcp_client, "known network address (%d)", known);
3251 if (dhcp_client->init_reboot == known)
3254 dhcp_client->init_reboot = known;