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 gboolean init_reboot;
185 static inline void debug(GDHCPClient *client, const char *format, ...)
190 if (!client->debug_func)
193 va_start(ap, format);
195 if (vsnprintf(str, sizeof(str), format, ap) > 0)
196 client->debug_func(str, client->debug_data);
201 /* Initialize the packet with the proper defaults */
202 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
204 if (dhcp_client->type == G_DHCP_IPV6)
205 dhcpv6_init_header(pkt, type);
207 struct dhcp_packet *packet = pkt;
209 dhcp_init_header(packet, type);
210 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
214 static void add_request_options(GDHCPClient *dhcp_client,
215 struct dhcp_packet *packet)
220 int end = dhcp_end_option(packet->options);
222 for (list = dhcp_client->request_list; list; list = list->next) {
223 code = (uint8_t) GPOINTER_TO_INT(list->data);
225 packet->options[end + OPT_DATA + len] = code;
230 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
231 packet->options[end + OPT_LEN] = len;
232 packet->options[end + OPT_DATA + len] = DHCP_END;
239 unsigned char **ptr_buf;
242 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
245 uint8_t *option = value;
247 struct hash_params *params = user_data;
249 /* option[0][1] contains option code */
250 len = option[2] << 8 | option[3];
252 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
255 memcpy(*params->ptr_buf, option, len + 2 + 2);
256 (*params->ptr_buf) += len + 2 + 2;
259 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
260 unsigned char *buf, int max_buf,
261 unsigned char **ptr_buf)
263 struct hash_params params = {
269 if (dhcp_client->type != G_DHCP_IPV6)
272 g_hash_table_foreach(dhcp_client->send_value_hash,
273 add_dhcpv6_binary_option, ¶ms);
275 *ptr_buf = *params.ptr_buf;
278 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
282 buf[1] = code & 0xff;
286 memcpy(&buf[4], msg, len);
289 static int32_t get_time_diff(struct timeval *tv)
294 gettimeofday(&now, NULL);
296 hsec = (now.tv_sec - tv->tv_sec) * 100;
297 hsec += (now.tv_usec - tv->tv_usec) / 10000;
302 static void remove_timeouts(GDHCPClient *dhcp_client)
305 if (dhcp_client->timeout > 0)
306 g_source_remove(dhcp_client->timeout);
307 if (dhcp_client->t1_timeout > 0)
308 g_source_remove(dhcp_client->t1_timeout);
309 if (dhcp_client->t2_timeout > 0)
310 g_source_remove(dhcp_client->t2_timeout);
311 if (dhcp_client->lease_timeout > 0)
312 g_source_remove(dhcp_client->lease_timeout);
314 dhcp_client->timeout = 0;
315 dhcp_client->t1_timeout = 0;
316 dhcp_client->t2_timeout = 0;
317 dhcp_client->lease_timeout = 0;
321 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
322 struct dhcpv6_packet *packet,
323 unsigned char *buf, int max_buf,
324 unsigned char **ptr_buf)
327 uint16_t code, value;
332 if (dhcp_client->type != G_DHCP_IPV6)
335 for (list = dhcp_client->request_list; list; list = list->next) {
336 code = (uint16_t) GPOINTER_TO_INT(list->data);
340 case G_DHCPV6_CLIENTID:
341 if (!dhcp_client->duid)
344 len = 2 + 2 + dhcp_client->duid_len;
345 if ((*ptr_buf + len) > (buf + max_buf)) {
346 debug(dhcp_client, "Too long dhcpv6 message "
347 "when writing client id option");
351 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
352 dhcp_client->duid_len, dhcp_client->duid);
357 case G_DHCPV6_SERVERID:
358 if (!dhcp_client->server_duid)
361 len = 2 + 2 + dhcp_client->server_duid_len;
362 if ((*ptr_buf + len) > (buf + max_buf)) {
363 debug(dhcp_client, "Too long dhcpv6 message "
364 "when writing server id option");
368 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
369 dhcp_client->server_duid_len,
370 dhcp_client->server_duid);
375 case G_DHCPV6_RAPID_COMMIT:
377 if ((*ptr_buf + len) > (buf + max_buf)) {
378 debug(dhcp_client, "Too long dhcpv6 message "
379 "when writing rapid commit option");
383 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
391 case G_DHCPV6_ELAPSED_TIME:
392 if (!dhcp_client->retransmit) {
394 * Initial message, elapsed time is 0.
398 diff = get_time_diff(&dhcp_client->start_time);
399 if (diff < 0 || diff > 0xffff)
404 if ((*ptr_buf + len) > (buf + max_buf)) {
405 debug(dhcp_client, "Too long dhcpv6 message "
406 "when writing elapsed time option");
410 value = htons((uint16_t)diff);
411 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
412 2, (uint8_t *)&value);
417 case G_DHCPV6_DNS_SERVERS:
420 case G_DHCPV6_DOMAIN_LIST:
423 case G_DHCPV6_SNTP_SERVERS:
431 debug(dhcp_client, "option %d len %d added", code, len);
435 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
437 uint8_t *option = value;
438 struct dhcp_packet *packet = user_data;
440 dhcp_add_binary_option(packet, option);
443 static void add_send_options(GDHCPClient *dhcp_client,
444 struct dhcp_packet *packet)
446 g_hash_table_foreach(dhcp_client->send_value_hash,
447 add_binary_option, packet);
451 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
452 * represents the number of seconds elapsed from the start of
453 * attempting DHCP to satisfy some DHCP servers that allow for an
454 * "authoritative" reply before responding.
456 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
458 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
461 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
463 struct dhcp_packet packet;
465 debug(dhcp_client, "sending DHCP discover request");
467 init_packet(dhcp_client, &packet, DHCPDISCOVER);
469 packet.xid = dhcp_client->xid;
470 packet.secs = dhcp_attempt_secs(dhcp_client);
473 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
475 /* Explicitly saying that we want RFC-compliant packets helps
476 * some buggy DHCP servers to NOT send bigger packets */
477 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
479 add_request_options(dhcp_client, &packet);
481 add_send_options(dhcp_client, &packet);
484 * If we do not get a reply to DISCOVER packet, then we try with
485 * broadcast flag set. So first packet is sent without broadcast flag,
486 * first retry is with broadcast flag, second retry is without it etc.
487 * Reason is various buggy routers/AP that either eat the other or vice
488 * versa. In the receiving side we then find out what kind of packet
489 * the server can send.
491 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
492 INADDR_BROADCAST, SERVER_PORT,
493 MAC_BCAST_ADDR, dhcp_client->ifindex,
494 dhcp_client->retry_times % 2);
497 static int send_request(GDHCPClient *dhcp_client)
499 struct dhcp_packet packet;
501 debug(dhcp_client, "sending DHCP request (state %d)",
504 init_packet(dhcp_client, &packet, DHCPREQUEST);
506 packet.xid = dhcp_client->xid;
507 #if defined TIZEN_EXT
508 if (dhcp_client->init_reboot != TRUE)
510 packet.secs = dhcp_attempt_secs(dhcp_client);
512 if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
513 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
514 dhcp_client->requested_ip);
516 if (dhcp_client->state == REQUESTING)
517 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
518 dhcp_client->server_ip);
520 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
522 add_request_options(dhcp_client, &packet);
524 add_send_options(dhcp_client, &packet);
526 if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
527 packet.ciaddr = htonl(dhcp_client->requested_ip);
529 if (dhcp_client->state == RENEWING)
530 return dhcp_send_kernel_packet(&packet,
531 dhcp_client->requested_ip, CLIENT_PORT,
532 dhcp_client->server_ip, SERVER_PORT);
534 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
535 INADDR_BROADCAST, SERVER_PORT,
536 MAC_BCAST_ADDR, dhcp_client->ifindex,
537 dhcp_client->request_bcast);
540 static int send_release(GDHCPClient *dhcp_client,
541 uint32_t server, uint32_t ciaddr)
543 struct dhcp_packet packet;
546 debug(dhcp_client, "sending DHCP release request");
548 init_packet(dhcp_client, &packet, DHCPRELEASE);
549 dhcp_get_random(&rand);
551 packet.ciaddr = htonl(ciaddr);
553 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
555 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
556 server, SERVER_PORT);
559 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
560 static int switch_listening_mode(GDHCPClient *dhcp_client,
561 ListenMode listen_mode);
563 static gboolean send_probe_packet(gpointer dhcp_data)
565 GDHCPClient *dhcp_client;
568 dhcp_client = dhcp_data;
569 /* if requested_ip is not valid, pick a new address*/
570 if (dhcp_client->requested_ip == 0) {
571 debug(dhcp_client, "pick a new random address");
572 dhcp_client->requested_ip = ipv4ll_random_ip();
575 debug(dhcp_client, "sending IPV4LL probe request");
577 if (dhcp_client->retry_times == 1) {
578 dhcp_client->state = IPV4LL_PROBE;
579 switch_listening_mode(dhcp_client, L_ARP);
581 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
582 dhcp_client->requested_ip, dhcp_client->ifindex);
584 if (dhcp_client->retry_times < PROBE_NUM) {
585 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
586 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
587 timeout += PROBE_MIN*1000;
589 timeout = (ANNOUNCE_WAIT * 1000);
591 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
593 ipv4ll_probe_timeout,
599 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
600 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
602 static gboolean send_announce_packet(gpointer dhcp_data)
604 GDHCPClient *dhcp_client;
606 dhcp_client = dhcp_data;
608 debug(dhcp_client, "sending IPV4LL announce request");
610 ipv4ll_send_arp_packet(dhcp_client->mac_address,
611 dhcp_client->requested_ip,
612 dhcp_client->requested_ip,
613 dhcp_client->ifindex);
615 remove_timeouts(dhcp_client);
617 if (dhcp_client->state == IPV4LL_DEFEND) {
618 dhcp_client->timeout =
619 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
621 ipv4ll_defend_timeout,
626 dhcp_client->timeout =
627 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
629 ipv4ll_announce_timeout,
635 static void get_interface_mac_address(int index, uint8_t *mac_address)
640 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
642 perror("Open socket error");
646 memset(&ifr, 0, sizeof(ifr));
647 ifr.ifr_ifindex = index;
649 err = ioctl(sk, SIOCGIFNAME, &ifr);
651 perror("Get interface name error");
655 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
657 perror("Get mac address error");
661 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
667 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
672 dhcp_client->retransmit = true;
675 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
680 dhcp_client->retransmit = false;
683 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
684 unsigned char **duid, int *duid_len)
689 case G_DHCPV6_DUID_LLT:
690 *duid_len = 2 + 2 + 4 + ETH_ALEN;
691 *duid = g_try_malloc(*duid_len);
697 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
700 duid_time = time(NULL) - DUID_TIME_EPOCH;
701 (*duid)[4] = duid_time >> 24;
702 (*duid)[5] = duid_time >> 16;
703 (*duid)[6] = duid_time >> 8;
704 (*duid)[7] = duid_time & 0xff;
706 case G_DHCPV6_DUID_EN:
708 case G_DHCPV6_DUID_LL:
709 *duid_len = 2 + 2 + ETH_ALEN;
710 *duid = g_try_malloc(*duid_len);
716 get_interface_mac_address(index, &(*duid)[2 + 2]);
725 static gchar *convert_to_hex(unsigned char *buf, int len)
727 gchar *ret = g_try_malloc(len * 2 + 1);
730 for (i = 0; ret && i < len; i++)
731 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
736 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
739 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
742 g_free(dhcp_client->duid);
744 dhcp_client->duid = duid;
745 dhcp_client->duid_len = duid_len;
747 if (dhcp_client->debug_func) {
748 gchar *hex = convert_to_hex(duid, duid_len);
749 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
756 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
757 uint32_t *T2, GSList *prefixes)
759 uint8_t options[1452];
760 unsigned int max_buf = sizeof(options);
761 int len, count = g_slist_length(prefixes);
763 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
766 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
768 memset(options, 0, sizeof(options));
770 options[0] = dhcp_client->iaid >> 24;
771 options[1] = dhcp_client->iaid >> 16;
772 options[2] = dhcp_client->iaid >> 8;
773 options[3] = dhcp_client->iaid;
776 uint32_t t = htonl(*T1);
777 memcpy(&options[4], &t, 4);
781 uint32_t t = htonl(*T2);
782 memcpy(&options[8], &t, 4);
790 for (list = prefixes; list; list = list->next) {
791 GDHCPIAPrefix *prefix = list->data;
792 uint8_t sub_option[4+4+1+16];
794 if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
796 "Too long dhcpv6 message "
797 "when writing IA prefix option");
801 memset(&sub_option, 0, sizeof(sub_option));
803 /* preferred and validity time are left zero */
805 sub_option[8] = prefix->prefixlen;
806 memcpy(&sub_option[9], &prefix->prefix, 16);
808 copy_option(&options[len], G_DHCPV6_IA_PREFIX,
809 sizeof(sub_option), sub_option);
810 len += 2 + 2 + sizeof(sub_option);
814 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
820 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
822 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
825 return dhcp_client->iaid;
828 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
830 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
833 dhcp_client->iaid = iaid;
836 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
841 get_interface_mac_address(index, buf);
843 memcpy(iaid, &buf[2], 4);
844 dhcp_client->iaid = iaid[0] << 24 |
845 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
848 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
849 uint32_t *T1, uint32_t *T2,
853 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
857 *T1 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
861 *T2 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
865 *started = dhcp_client->last_request;
868 *expire = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
869 dhcp_client->last_request + dhcp_client->expire;
874 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
878 buf[1] = G_DHCPV6_IAADDR;
881 memcpy(&buf[4], &dhcp_client->ia_na, 16);
882 memset(&buf[20], 0, 4); /* preferred */
883 memset(&buf[24], 0, 4); /* valid */
887 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
890 struct in6_addr addr;
892 if (inet_pton(AF_INET6, address, &addr) != 1)
896 buf[1] = G_DHCPV6_IAADDR;
899 memcpy(&buf[4], &addr, 16);
900 memset(&buf[20], 0, 4); /* preferred */
901 memset(&buf[24], 0, 4); /* valid */
905 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
909 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
911 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
921 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
922 int code, uint32_t *T1, uint32_t *T2,
923 bool add_iaaddr, const char *ia_na)
925 if (code == G_DHCPV6_IA_TA) {
926 uint8_t ia_options[4];
928 put_iaid(dhcp_client, index, ia_options);
930 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
931 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
932 ia_options, sizeof(ia_options));
934 } else if (code == G_DHCPV6_IA_NA) {
935 struct in6_addr addr;
937 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
940 * If caller has specified the IPv6 address it wishes to
941 * to use (ia_na != NULL and address is valid), then send
942 * the address to server.
943 * If caller did not specify the address (ia_na == NULL) and
944 * if the current address is not set, then we should not send
945 * the address sub-option.
947 if (add_iaaddr && ((!ia_na &&
948 !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
950 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
951 #define IAADDR_LEN (16+4+4)
952 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
955 memcpy(&dhcp_client->ia_na, &addr,
956 sizeof(struct in6_addr));
958 put_iaid(dhcp_client, index, ia_options);
961 ia_options[4] = *T1 >> 24;
962 ia_options[5] = *T1 >> 16;
963 ia_options[6] = *T1 >> 8;
966 memset(&ia_options[4], 0x00, 4);
969 ia_options[8] = *T2 >> 24;
970 ia_options[9] = *T2 >> 16;
971 ia_options[10] = *T2 >> 8;
972 ia_options[11] = *T2;
974 memset(&ia_options[8], 0x00, 4);
976 create_iaaddr(dhcp_client, &ia_options[12],
979 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
980 ia_options, sizeof(ia_options));
982 uint8_t ia_options[4+4+4];
984 put_iaid(dhcp_client, index, ia_options);
986 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
987 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
989 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
990 ia_options, sizeof(ia_options));
999 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
1000 int code, uint32_t *T1, uint32_t *T2,
1004 uint8_t *ia_options, *pos;
1005 int len, count, total_len;
1007 count = g_slist_length(addresses);
1011 g_dhcp_client_set_request(dhcp_client, code);
1013 if (code == G_DHCPV6_IA_TA)
1015 else if (code == G_DHCPV6_IA_NA)
1016 len = 4 + 4 + 4; /* IAID + T1 + T2 */
1020 total_len = len + count * (2 + 2 + 16 + 4 + 4);
1021 ia_options = g_try_malloc0(total_len);
1025 put_iaid(dhcp_client, index, ia_options);
1027 pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
1029 for (list = addresses; list; list = list->next) {
1030 pos = append_iaaddr(dhcp_client, pos, list->data);
1035 if (code == G_DHCPV6_IA_NA) {
1037 ia_options[4] = *T1 >> 24;
1038 ia_options[5] = *T1 >> 16;
1039 ia_options[6] = *T1 >> 8;
1040 ia_options[7] = *T1;
1042 memset(&ia_options[4], 0x00, 4);
1045 ia_options[8] = *T2 >> 24;
1046 ia_options[9] = *T2 >> 16;
1047 ia_options[10] = *T2 >> 8;
1048 ia_options[11] = *T2;
1050 memset(&ia_options[8], 0x00, 4);
1053 g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1060 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1063 int i, j, len = sizeof(uint16_t) * args;
1066 values = g_try_malloc(len);
1071 for (i = 0, j = 0; i < args; i++) {
1072 uint16_t value = va_arg(va, int);
1073 values[j++] = value >> 8;
1074 values[j++] = value & 0xff;
1078 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1085 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1087 struct dhcpv6_packet *packet;
1088 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1092 memset(buf, 0, sizeof(buf));
1093 packet = (struct dhcpv6_packet *)&buf[0];
1094 ptr = buf + sizeof(struct dhcpv6_packet);
1096 init_packet(dhcp_client, packet, type);
1098 if (!dhcp_client->retransmit) {
1099 dhcp_client->xid = packet->transaction_id[0] << 16 |
1100 packet->transaction_id[1] << 8 |
1101 packet->transaction_id[2];
1102 gettimeofday(&dhcp_client->start_time, NULL);
1104 packet->transaction_id[0] = dhcp_client->xid >> 16;
1105 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1106 packet->transaction_id[2] = dhcp_client->xid;
1109 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1111 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1114 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1116 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1118 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1120 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1122 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1126 static int send_solicitation(GDHCPClient *dhcp_client)
1128 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1131 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1133 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1136 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1138 return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1141 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1143 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1146 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1148 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1151 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1153 return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1156 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1158 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1161 static int send_information_req(GDHCPClient *dhcp_client)
1163 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1167 static void remove_value(gpointer data, gpointer user_data)
1173 static void remove_option_value(gpointer data)
1175 GList *option_value = data;
1177 g_list_foreach(option_value, remove_value, NULL);
1178 g_list_free(option_value);
1181 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1182 int ifindex, GDHCPClientError *error)
1184 GDHCPClient *dhcp_client;
1187 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1191 dhcp_client = g_try_new0(GDHCPClient, 1);
1193 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1197 dhcp_client->interface = get_interface_name(ifindex);
1198 if (!dhcp_client->interface) {
1199 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1203 if (!interface_is_up(ifindex)) {
1204 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1208 get_interface_mac_address(ifindex, dhcp_client->mac_address);
1210 dhcp_client->listener_sockfd = -1;
1211 dhcp_client->listen_mode = L_NONE;
1212 dhcp_client->ref_count = 1;
1213 dhcp_client->type = type;
1214 dhcp_client->ifindex = ifindex;
1215 dhcp_client->lease_available_cb = NULL;
1216 dhcp_client->ipv4ll_available_cb = NULL;
1217 dhcp_client->no_lease_cb = NULL;
1218 dhcp_client->lease_lost_cb = NULL;
1219 dhcp_client->ipv4ll_lost_cb = NULL;
1220 dhcp_client->address_conflict_cb = NULL;
1221 dhcp_client->listener_watch = 0;
1222 dhcp_client->retry_times = 0;
1223 dhcp_client->ack_retry_times = 0;
1224 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1225 g_direct_equal, NULL, remove_option_value);
1226 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1227 g_direct_equal, NULL, g_free);
1228 dhcp_client->request_list = NULL;
1229 dhcp_client->require_list = NULL;
1230 dhcp_client->duid = NULL;
1231 dhcp_client->duid_len = 0;
1232 dhcp_client->last_request = time(NULL);
1233 dhcp_client->expire = 0;
1234 dhcp_client->request_bcast = false;
1236 *error = G_DHCP_CLIENT_ERROR_NONE;
1241 g_free(dhcp_client->interface);
1242 g_free(dhcp_client);
1246 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1248 static int dhcp_l2_socket(int ifindex)
1251 struct sockaddr_ll sock;
1256 * I've selected not to see LL header, so BPF doesn't see it, too.
1257 * The filter may also pass non-IP and non-ARP packets, but we do
1258 * a more complete check when receiving the message in userspace.
1260 * and filter shamelessly stolen from:
1262 * http://www.flamewarmaster.de/software/dhcpclient/
1264 * There are a few other interesting ideas on that page (look under
1265 * "Motivation"). Use of netlink events is most interesting. Think
1266 * of various network servers listening for events and reconfiguring.
1267 * That would obsolete sending HUP signals and/or make use of restarts.
1269 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1272 * TODO: make conditional?
1274 static const struct sock_filter filter_instr[] = {
1276 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1277 /* L5, L1, is UDP? */
1278 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1279 /* ugly check for arp on ethernet-like and IPv4 */
1280 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1281 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1282 /* skip IP header */
1283 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1284 /* check udp source and destination ports */
1285 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1287 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1289 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1290 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1293 static const struct sock_fprog filter_prog = {
1294 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1295 /* casting const away: */
1296 .filter = (struct sock_filter *) filter_instr,
1299 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
1303 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1304 /* Use only if standard ports are in use */
1305 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1306 sizeof(filter_prog));
1308 memset(&sock, 0, sizeof(sock));
1309 sock.sll_family = AF_PACKET;
1310 sock.sll_protocol = htons(ETH_P_IP);
1311 sock.sll_ifindex = ifindex;
1313 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1322 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1324 if (packet->ip.protocol != IPPROTO_UDP)
1327 if (packet->ip.version != IPVERSION)
1330 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1333 if (packet->udp.dest != htons(CLIENT_PORT))
1336 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1342 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1343 struct sockaddr_in *dst_addr)
1346 struct ip_udp_dhcp_packet packet;
1349 memset(&packet, 0, sizeof(packet));
1351 bytes = read(fd, &packet, sizeof(packet));
1355 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1358 if (bytes < ntohs(packet.ip.tot_len))
1359 /* packet is bigger than sizeof(packet), we did partial read */
1362 /* ignore any extra garbage bytes */
1363 bytes = ntohs(packet.ip.tot_len);
1365 if (!sanity_check(&packet, bytes))
1368 check = packet.ip.check;
1369 packet.ip.check = 0;
1370 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1373 /* verify UDP checksum. IP header has to be modified for this */
1374 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1375 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1376 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1377 check = packet.udp.check;
1378 packet.udp.check = 0;
1379 if (check && check != dhcp_checksum(&packet, bytes))
1382 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1383 sizeof(packet.udp)));
1385 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1388 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1390 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1393 static void ipv4ll_start(GDHCPClient *dhcp_client)
1397 remove_timeouts(dhcp_client);
1399 switch_listening_mode(dhcp_client, L_NONE);
1400 dhcp_client->retry_times = 0;
1401 dhcp_client->requested_ip = 0;
1403 dhcp_client->requested_ip = ipv4ll_random_ip();
1405 /*first wait a random delay to avoid storm of arp request on boot*/
1406 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1408 dhcp_client->retry_times++;
1409 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1416 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1419 switch_listening_mode(dhcp_client, L_NONE);
1421 remove_timeouts(dhcp_client);
1423 if (dhcp_client->listener_watch > 0) {
1424 g_source_remove(dhcp_client->listener_watch);
1425 dhcp_client->listener_watch = 0;
1428 dhcp_client->state = IPV4LL_PROBE;
1429 dhcp_client->retry_times = 0;
1430 dhcp_client->requested_ip = 0;
1432 g_free(dhcp_client->assigned_ip);
1433 dhcp_client->assigned_ip = NULL;
1436 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1439 struct ether_arp arp;
1440 uint32_t ip_requested;
1441 int source_conflict;
1442 int target_conflict;
1444 memset(&arp, 0, sizeof(arp));
1445 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1449 if (arp.arp_op != htons(ARPOP_REPLY) &&
1450 arp.arp_op != htons(ARPOP_REQUEST))
1453 ip_requested = htonl(dhcp_client->requested_ip);
1454 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1455 sizeof(ip_requested));
1457 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1458 sizeof(ip_requested));
1460 if (!source_conflict && !target_conflict)
1463 dhcp_client->conflicts++;
1465 debug(dhcp_client, "IPV4LL conflict detected");
1467 if (dhcp_client->state == IPV4LL_MONITOR) {
1468 if (!source_conflict)
1470 dhcp_client->state = IPV4LL_DEFEND;
1471 debug(dhcp_client, "DEFEND mode conflicts : %d",
1472 dhcp_client->conflicts);
1473 /*Try to defend with a single announce*/
1474 send_announce_packet(dhcp_client);
1478 if (dhcp_client->state == IPV4LL_DEFEND) {
1479 if (!source_conflict)
1481 else if (dhcp_client->ipv4ll_lost_cb)
1482 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1483 dhcp_client->ipv4ll_lost_data);
1486 ipv4ll_stop(dhcp_client);
1488 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1489 /*restart whole state machine*/
1490 dhcp_client->retry_times++;
1491 dhcp_client->timeout =
1492 g_timeout_add_full(G_PRIORITY_HIGH,
1493 ipv4ll_random_delay_ms(PROBE_WAIT),
1498 /* Here we got a lot of conflicts, RFC3927 states that we have
1499 * to wait RATE_LIMIT_INTERVAL before retrying,
1500 * but we just report failure.
1502 else if (dhcp_client->no_lease_cb)
1503 dhcp_client->no_lease_cb(dhcp_client,
1504 dhcp_client->no_lease_data);
1509 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1511 if (dhcp_client->type == G_DHCP_IPV6) {
1512 struct dhcpv6_packet *packet6 = pkt;
1518 xid = packet6->transaction_id[0] << 16 |
1519 packet6->transaction_id[1] << 8 |
1520 packet6->transaction_id[2];
1522 if (xid != dhcp_client->xid)
1525 struct dhcp_packet *packet = pkt;
1527 if (packet->xid != dhcp_client->xid)
1530 if (packet->hlen != 6)
1533 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1540 static void start_request(GDHCPClient *dhcp_client);
1542 static gboolean request_timeout(gpointer user_data)
1544 GDHCPClient *dhcp_client = user_data;
1546 #if defined TIZEN_EXT
1547 if (dhcp_client->init_reboot) {
1548 debug(dhcp_client, "DHCPREQUEST of INIT-REBOOT has failed");
1550 /* Start DHCPDISCOVERY when DHCPREQUEST of INIT-REBOOT has failed */
1551 g_dhcp_client_set_address_known(dhcp_client, FALSE);
1553 dhcp_client->retry_times = 0;
1554 dhcp_client->requested_ip = 0;
1556 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1561 debug(dhcp_client, "request timeout (retries %d)",
1562 dhcp_client->retry_times);
1564 dhcp_client->retry_times++;
1566 start_request(dhcp_client);
1571 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1572 gpointer user_data);
1574 static int switch_listening_mode(GDHCPClient *dhcp_client,
1575 ListenMode listen_mode)
1577 GIOChannel *listener_channel;
1578 int listener_sockfd;
1580 if (dhcp_client->listen_mode == listen_mode)
1583 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1584 dhcp_client->listen_mode, listen_mode);
1586 if (dhcp_client->listen_mode != L_NONE) {
1587 if (dhcp_client->listener_watch > 0)
1588 g_source_remove(dhcp_client->listener_watch);
1589 dhcp_client->listen_mode = L_NONE;
1590 dhcp_client->listener_sockfd = -1;
1591 dhcp_client->listener_watch = 0;
1594 if (listen_mode == L_NONE)
1597 if (listen_mode == L2)
1598 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1599 else if (listen_mode == L3) {
1600 if (dhcp_client->type == G_DHCP_IPV6)
1601 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1602 dhcp_client->interface,
1605 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1606 dhcp_client->interface,
1608 } else if (listen_mode == L_ARP)
1609 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1613 if (listener_sockfd < 0)
1616 listener_channel = g_io_channel_unix_new(listener_sockfd);
1617 if (!listener_channel) {
1618 /* Failed to create listener channel */
1619 close(listener_sockfd);
1623 dhcp_client->listen_mode = listen_mode;
1624 dhcp_client->listener_sockfd = listener_sockfd;
1626 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1627 dhcp_client->listener_watch =
1628 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1629 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1630 listener_event, dhcp_client,
1632 g_io_channel_unref(listener_channel);
1637 static void start_request(GDHCPClient *dhcp_client)
1639 debug(dhcp_client, "start request (retries %d)",
1640 dhcp_client->retry_times);
1642 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1643 if (dhcp_client->no_lease_cb)
1644 dhcp_client->no_lease_cb(dhcp_client,
1645 dhcp_client->no_lease_data);
1649 if (dhcp_client->retry_times == 0) {
1650 dhcp_client->state = REQUESTING;
1651 switch_listening_mode(dhcp_client, L2);
1654 send_request(dhcp_client);
1656 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1663 static uint32_t get_lease(struct dhcp_packet *packet)
1666 uint32_t lease_seconds;
1668 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1672 lease_seconds = get_be32(option);
1673 /* paranoia: must not be prone to overflows */
1674 lease_seconds &= 0x0fffffff;
1675 if (lease_seconds < 10)
1678 return lease_seconds;
1681 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1683 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1685 remove_timeouts(dhcp_client);
1687 dhcp_client->retry_times = retry_times;
1688 dhcp_client->requested_ip = 0;
1689 dhcp_client->state = INIT_SELECTING;
1690 switch_listening_mode(dhcp_client, L2);
1692 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1695 static gboolean start_expire(gpointer user_data)
1697 GDHCPClient *dhcp_client = user_data;
1699 debug(dhcp_client, "lease expired");
1701 /*remove all timeouts if they are set*/
1702 remove_timeouts(dhcp_client);
1704 restart_dhcp(dhcp_client, 0);
1706 /* ip need to be cleared */
1707 if (dhcp_client->lease_lost_cb)
1708 dhcp_client->lease_lost_cb(dhcp_client,
1709 dhcp_client->lease_lost_data);
1714 static gboolean continue_rebound(gpointer user_data)
1716 GDHCPClient *dhcp_client = user_data;
1719 switch_listening_mode(dhcp_client, L2);
1720 send_request(dhcp_client);
1722 if (dhcp_client->t2_timeout> 0)
1723 g_source_remove(dhcp_client->t2_timeout);
1725 /*recalculate remaining rebind time*/
1726 dhcp_client->T2 >>= 1;
1727 if (dhcp_client->T2 > 60) {
1728 dhcp_get_random(&rand);
1729 dhcp_client->t2_timeout =
1730 g_timeout_add_full(G_PRIORITY_HIGH,
1731 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1740 static gboolean start_rebound(gpointer user_data)
1742 GDHCPClient *dhcp_client = user_data;
1744 /*remove renew timer*/
1745 if (dhcp_client->t1_timeout > 0)
1746 g_source_remove(dhcp_client->t1_timeout);
1748 debug(dhcp_client, "start rebound");
1749 dhcp_client->state = REBINDING;
1751 /*calculate total rebind time*/
1752 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1754 /*send the first rebound and reschedule*/
1755 continue_rebound(user_data);
1760 static gboolean continue_renew (gpointer user_data)
1762 GDHCPClient *dhcp_client = user_data;
1765 switch_listening_mode(dhcp_client, L3);
1766 send_request(dhcp_client);
1768 if (dhcp_client->t1_timeout > 0)
1769 g_source_remove(dhcp_client->t1_timeout);
1771 dhcp_client->t1_timeout = 0;
1773 dhcp_client->T1 >>= 1;
1775 if (dhcp_client->T1 > 60) {
1776 dhcp_get_random(&rand);
1777 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1778 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1786 static gboolean start_renew(gpointer user_data)
1788 GDHCPClient *dhcp_client = user_data;
1790 debug(dhcp_client, "start renew");
1791 dhcp_client->state = RENEWING;
1793 /*calculate total renew period*/
1794 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1796 /*send first renew and reschedule for half the remaining time.*/
1797 continue_renew(user_data);
1802 static void start_bound(GDHCPClient *dhcp_client)
1804 debug(dhcp_client, "start bound");
1806 dhcp_client->state = BOUND;
1808 remove_timeouts(dhcp_client);
1811 *TODO: T1 and T2 should be set through options instead of
1812 * defaults as they are here.
1815 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1816 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1817 dhcp_client->expire = dhcp_client->lease_seconds;
1819 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1821 start_renew, dhcp_client,
1824 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1826 start_rebound, dhcp_client,
1829 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1830 dhcp_client->expire,
1831 start_expire, dhcp_client,
1835 static gboolean restart_dhcp_timeout(gpointer user_data)
1837 GDHCPClient *dhcp_client = user_data;
1839 debug(dhcp_client, "restart DHCP timeout");
1841 if (dhcp_client->state == REBOOTING) {
1842 g_free(dhcp_client->last_address);
1843 dhcp_client->last_address = NULL;
1844 restart_dhcp(dhcp_client, 0);
1846 dhcp_client->ack_retry_times++;
1847 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1852 static char *get_ip(uint32_t ip)
1854 struct in_addr addr;
1858 return g_strdup(inet_ntoa(addr));
1861 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1869 if (type == OPTION_STRING)
1870 return g_list_append(list, g_strdup(value));
1872 while ((pos = strchr(pos, ' '))) {
1875 list = g_list_append(list, g_strdup(value));
1880 list = g_list_append(list, g_strdup(value));
1885 static inline uint32_t get_uint32(unsigned char *value)
1887 return value[0] << 24 | value[1] << 16 |
1888 value[2] << 8 | value[3];
1891 static inline uint16_t get_uint16(unsigned char *value)
1893 return value[0] << 8 | value[1];
1896 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1897 struct in6_addr *addr,
1898 unsigned char prefixlen, uint32_t preferred,
1901 GDHCPIAPrefix *ia_prefix;
1903 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1907 if (dhcp_client->debug_func) {
1908 char addr_str[INET6_ADDRSTRLEN + 1];
1909 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1910 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1911 addr_str, prefixlen, preferred, valid);
1914 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1915 ia_prefix->prefixlen = prefixlen;
1916 ia_prefix->preferred = preferred;
1917 ia_prefix->valid = valid;
1918 ia_prefix->expire = time(NULL) + valid;
1920 return g_list_prepend(list, ia_prefix);
1923 static GList *get_addresses(GDHCPClient *dhcp_client,
1925 unsigned char *value,
1929 struct in6_addr addr;
1930 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1931 uint16_t option_len, option_code, st = 0, max_len;
1932 int addr_count = 0, prefix_count = 0, i, pos;
1933 unsigned char prefixlen;
1934 unsigned int shortest_valid = 0;
1938 if (!value || len < 4)
1941 iaid = get_uint32(&value[0]);
1942 if (dhcp_client->iaid != iaid)
1945 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
1946 T1 = get_uint32(&value[4]);
1947 T2 = get_uint32(&value[8]);
1950 /* IA_NA: RFC 3315, 22.4 */
1951 /* IA_PD: RFC 3633, ch 9 */
1961 max_len = len - pos;
1963 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
1965 /* We have more sub-options in this packet. */
1967 option = dhcpv6_get_sub_option(&value[pos], max_len,
1968 &option_code, &option_len);
1970 debug(dhcp_client, "pos %d option %p code %d len %d",
1971 pos, option, option_code, option_len);
1979 switch (option_code) {
1980 case G_DHCPV6_IAADDR:
1982 memcpy(&addr, &option[0], sizeof(addr));
1984 preferred = get_uint32(&option[i]);
1986 valid = get_uint32(&option[i]);
1991 case G_DHCPV6_STATUS_CODE:
1992 st = get_uint16(&option[0]);
1993 debug(dhcp_client, "error code %d", st);
1994 if (option_len > 2) {
1995 str = g_strndup((gchar *)&option[2],
1997 debug(dhcp_client, "error text: %s", str);
2004 case G_DHCPV6_IA_PREFIX:
2006 preferred = get_uint32(&option[i]);
2008 valid = get_uint32(&option[i]);
2010 prefixlen = option[i];
2012 memcpy(&addr, &option[i], sizeof(addr));
2014 if (preferred < valid) {
2015 /* RFC 3633, ch 10 */
2016 list = add_prefix(dhcp_client, list, &addr,
2017 prefixlen, preferred, valid);
2018 if (shortest_valid > valid)
2019 shortest_valid = valid;
2025 pos += 2 + 2 + option_len;
2027 } while (pos < len);
2029 if (addr_count > 0 && st == 0) {
2030 /* We only support one address atm */
2031 char addr_str[INET6_ADDRSTRLEN + 1];
2033 if (preferred > valid)
2034 /* RFC 3315, 22.6 */
2037 dhcp_client->T1 = T1;
2038 dhcp_client->T2 = T2;
2040 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2041 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2042 addr_count, addr_str, T1, T2);
2044 list = g_list_append(list, g_strdup(addr_str));
2046 if (code == G_DHCPV6_IA_NA)
2047 memcpy(&dhcp_client->ia_na, &addr,
2048 sizeof(struct in6_addr));
2050 memcpy(&dhcp_client->ia_ta, &addr,
2051 sizeof(struct in6_addr));
2053 if (valid != dhcp_client->expire)
2054 dhcp_client->expire = valid;
2057 if (prefix_count > 0 && list) {
2059 * This means we have a list of prefixes to delegate.
2061 list = g_list_reverse(list);
2063 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2064 prefix_count, T1, T2);
2066 dhcp_client->T1 = T1;
2067 dhcp_client->T2 = T2;
2069 dhcp_client->expire = shortest_valid;
2072 if (status && *status != 0)
2073 debug(dhcp_client, "status %d", *status);
2078 static GList *get_domains(int maxlen, unsigned char *value)
2084 char dns_name[NS_MAXDNAME + 1];
2086 if (!value || maxlen < 3)
2089 while (pos < maxlen) {
2090 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2092 c = (unsigned char *)dns_name;
2099 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2100 pos += (char *)c - dns_name + 1;
2103 return g_list_reverse(list);
2106 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2108 unsigned char *value,
2119 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2120 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2123 "%s server list length (%d) is invalid",
2124 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2128 for (i = 0; i < len; i += 16) {
2130 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2134 if (!inet_ntop(AF_INET6, &value[i], str,
2138 list = g_list_append(list, str);
2142 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2143 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2144 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2145 list = get_addresses(dhcp_client, code, len, value, status);
2148 case G_DHCPV6_DOMAIN_LIST:
2149 list = get_domains(len, value);
2159 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2160 struct dhcpv6_packet *packet,
2161 uint16_t pkt_len, uint16_t *status)
2163 GList *list, *value_list;
2166 uint16_t option_len;
2168 for (list = dhcp_client->request_list; list; list = list->next) {
2169 code = (uint16_t) GPOINTER_TO_INT(list->data);
2171 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2174 g_hash_table_remove(dhcp_client->code_value_hash,
2175 GINT_TO_POINTER((int) code));
2179 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2180 option_len, option, status);
2182 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2183 option_len, value_list);
2186 g_hash_table_remove(dhcp_client->code_value_hash,
2187 GINT_TO_POINTER((int) code));
2189 g_hash_table_insert(dhcp_client->code_value_hash,
2190 GINT_TO_POINTER((int) code), value_list);
2194 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2196 GDHCPOptionType type;
2197 GList *list, *value_list;
2202 for (list = dhcp_client->request_list; list; list = list->next) {
2203 code = (uint8_t) GPOINTER_TO_INT(list->data);
2205 option = dhcp_get_option(packet, code);
2207 g_hash_table_remove(dhcp_client->code_value_hash,
2208 GINT_TO_POINTER((int) code));
2212 type = dhcp_get_code_type(code);
2214 option_value = malloc_option_value_string(option, type);
2216 g_hash_table_remove(dhcp_client->code_value_hash,
2217 GINT_TO_POINTER((int) code));
2219 value_list = get_option_value_list(option_value, type);
2221 g_free(option_value);
2224 g_hash_table_remove(dhcp_client->code_value_hash,
2225 GINT_TO_POINTER((int) code));
2227 g_hash_table_insert(dhcp_client->code_value_hash,
2228 GINT_TO_POINTER((int) code), value_list);
2232 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2235 GDHCPClient *dhcp_client = user_data;
2236 struct sockaddr_in dst_addr = { 0 };
2237 struct dhcp_packet packet;
2238 struct dhcpv6_packet *packet6 = NULL;
2239 uint8_t *message_type = NULL, *client_id = NULL, *option,
2241 uint16_t option_len = 0, status = 0;
2244 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2245 uint16_t pkt_len = 0;
2249 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2250 dhcp_client->listener_watch = 0;
2254 if (dhcp_client->listen_mode == L_NONE)
2259 dhcp_client->status_code = 0;
2261 if (dhcp_client->listen_mode == L2) {
2262 re = dhcp_recv_l2_packet(&packet,
2263 dhcp_client->listener_sockfd,
2266 } else if (dhcp_client->listen_mode == L3) {
2267 if (dhcp_client->type == G_DHCP_IPV6) {
2268 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2269 dhcp_client->listener_sockfd);
2272 xid = packet6->transaction_id[0] << 16 |
2273 packet6->transaction_id[1] << 8 |
2274 packet6->transaction_id[2];
2276 re = dhcp_recv_l3_packet(&packet,
2277 dhcp_client->listener_sockfd);
2280 } else if (dhcp_client->listen_mode == L_ARP) {
2281 ipv4ll_recv_arp_packet(dhcp_client);
2289 if (!check_package_owner(dhcp_client, pkt))
2292 if (dhcp_client->type == G_DHCP_IPV6) {
2297 client_id = dhcpv6_get_option(packet6, pkt_len,
2298 G_DHCPV6_CLIENTID, &option_len, &count);
2300 if (!client_id || count == 0 || option_len == 0 ||
2301 memcmp(dhcp_client->duid, client_id,
2302 dhcp_client->duid_len) != 0) {
2304 "client duid error, discarding msg %p/%d/%d",
2305 client_id, option_len, count);
2309 option = dhcpv6_get_option(packet6, pkt_len,
2310 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2311 if (option != 0 && option_len > 0) {
2312 status = option[0]<<8 | option[1];
2314 debug(dhcp_client, "error code %d", status);
2315 if (option_len > 2) {
2316 gchar *txt = g_strndup(
2317 (gchar *)&option[2],
2319 debug(dhcp_client, "error text: %s",
2324 dhcp_client->status_code = status;
2327 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2332 if (!message_type && !client_id)
2333 /* No message type / client id option, ignore package */
2336 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2337 "(current state %d)", ntohl(xid), dhcp_client->state);
2339 switch (dhcp_client->state) {
2340 case INIT_SELECTING:
2341 if (*message_type != DHCPOFFER)
2344 remove_timeouts(dhcp_client);
2345 dhcp_client->timeout = 0;
2346 dhcp_client->retry_times = 0;
2348 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2349 dhcp_client->server_ip = get_be32(option);
2350 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2352 dhcp_client->state = REQUESTING;
2354 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2355 dhcp_client->request_bcast = true;
2357 dhcp_client->request_bcast = false;
2359 debug(dhcp_client, "init ip %s -> %sadding broadcast flag",
2360 inet_ntoa(dst_addr.sin_addr),
2361 dhcp_client->request_bcast ? "" : "not ");
2363 start_request(dhcp_client);
2367 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2368 dhcp_client->request_bcast = true;
2370 dhcp_client->request_bcast = false;
2372 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2373 inet_ntoa(dst_addr.sin_addr),
2374 dhcp_client->request_bcast ? "" : "not ");
2379 if (*message_type == DHCPACK) {
2380 dhcp_client->retry_times = 0;
2382 remove_timeouts(dhcp_client);
2384 dhcp_client->lease_seconds = get_lease(&packet);
2386 get_request(dhcp_client, &packet);
2388 switch_listening_mode(dhcp_client, L_NONE);
2390 g_free(dhcp_client->assigned_ip);
2391 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2393 if (dhcp_client->state == REBOOTING) {
2394 option = dhcp_get_option(&packet,
2396 dhcp_client->server_ip = get_be32(option);
2399 /* Address should be set up here */
2400 if (dhcp_client->lease_available_cb)
2401 dhcp_client->lease_available_cb(dhcp_client,
2402 dhcp_client->lease_available_data);
2404 start_bound(dhcp_client);
2405 } else if (*message_type == DHCPNAK) {
2406 dhcp_client->retry_times = 0;
2408 remove_timeouts(dhcp_client);
2410 #if defined TIZEN_EXT
2411 if (dhcp_client->init_reboot) {
2412 g_dhcp_client_set_address_known(dhcp_client, FALSE);
2413 dhcp_client->timeout = g_idle_add_full(
2415 restart_dhcp_timeout,
2422 dhcp_client->timeout = g_timeout_add_seconds_full(
2424 restart_dhcp_timeout,
2431 if (dhcp_client->type != G_DHCP_IPV6)
2434 if (packet6->message != DHCPV6_REPLY &&
2435 packet6->message != DHCPV6_ADVERTISE)
2439 server_id = dhcpv6_get_option(packet6, pkt_len,
2440 G_DHCPV6_SERVERID, &option_len, &count);
2441 if (!server_id || count != 1 || option_len == 0) {
2442 /* RFC 3315, 15.10 */
2444 "server duid error, discarding msg %p/%d/%d",
2445 server_id, option_len, count);
2448 dhcp_client->server_duid = g_try_malloc(option_len);
2449 if (!dhcp_client->server_duid)
2451 memcpy(dhcp_client->server_duid, server_id, option_len);
2452 dhcp_client->server_duid_len = option_len;
2454 if (packet6->message == DHCPV6_REPLY) {
2455 uint8_t *rapid_commit;
2458 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2459 G_DHCPV6_RAPID_COMMIT,
2460 &option_len, &count);
2461 if (!rapid_commit || option_len != 0 ||
2463 /* RFC 3315, 17.1.4 */
2467 switch_listening_mode(dhcp_client, L_NONE);
2469 if (dhcp_client->status_code == 0)
2470 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2471 &dhcp_client->status_code);
2473 if (packet6->message == DHCPV6_ADVERTISE) {
2474 if (dhcp_client->advertise_cb)
2475 dhcp_client->advertise_cb(dhcp_client,
2476 dhcp_client->advertise_data);
2480 if (dhcp_client->solicitation_cb) {
2482 * The dhcp_client might not be valid after the
2483 * callback call so just return immediately.
2485 dhcp_client->solicitation_cb(dhcp_client,
2486 dhcp_client->solicitation_data);
2491 if (dhcp_client->type != G_DHCP_IPV6)
2494 server_id = dhcpv6_get_option(packet6, pkt_len,
2495 G_DHCPV6_SERVERID, &option_len, &count);
2496 if (!dhcp_client->server_duid && server_id &&
2499 * If we do not have server duid yet, then get it now.
2500 * Prefix delegation renew support needs it.
2502 dhcp_client->server_duid = g_try_malloc(option_len);
2503 if (!dhcp_client->server_duid)
2505 memcpy(dhcp_client->server_duid, server_id, option_len);
2506 dhcp_client->server_duid_len = option_len;
2509 case INFORMATION_REQ:
2515 if (dhcp_client->type != G_DHCP_IPV6)
2518 if (packet6->message != DHCPV6_REPLY)
2523 server_id = dhcpv6_get_option(packet6, pkt_len,
2524 G_DHCPV6_SERVERID, &option_len, &count);
2525 if (!server_id || count != 1 || option_len == 0 ||
2526 (dhcp_client->server_duid_len > 0 &&
2527 memcmp(dhcp_client->server_duid, server_id,
2528 dhcp_client->server_duid_len) != 0)) {
2529 /* RFC 3315, 15.10 */
2531 "server duid error, discarding msg %p/%d/%d",
2532 server_id, option_len, count);
2536 switch_listening_mode(dhcp_client, L_NONE);
2538 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2539 &dhcp_client->status_code);
2541 if (dhcp_client->information_req_cb) {
2543 * The dhcp_client might not be valid after the
2544 * callback call so just return immediately.
2546 dhcp_client->information_req_cb(dhcp_client,
2547 dhcp_client->information_req_data);
2550 if (dhcp_client->request_cb) {
2551 dhcp_client->request_cb(dhcp_client,
2552 dhcp_client->request_data);
2555 if (dhcp_client->renew_cb) {
2556 dhcp_client->renew_cb(dhcp_client,
2557 dhcp_client->renew_data);
2560 if (dhcp_client->rebind_cb) {
2561 dhcp_client->rebind_cb(dhcp_client,
2562 dhcp_client->rebind_data);
2565 if (dhcp_client->release_cb) {
2566 dhcp_client->release_cb(dhcp_client,
2567 dhcp_client->release_data);
2570 if (dhcp_client->decline_cb) {
2571 dhcp_client->decline_cb(dhcp_client,
2572 dhcp_client->decline_data);
2575 if (dhcp_client->confirm_cb) {
2577 server_id = dhcpv6_get_option(packet6, pkt_len,
2578 G_DHCPV6_SERVERID, &option_len,
2580 if (!server_id || count != 1 ||
2582 /* RFC 3315, 15.10 */
2584 "confirm server duid error, "
2585 "discarding msg %p/%d/%d",
2586 server_id, option_len, count);
2589 dhcp_client->server_duid = g_try_malloc(option_len);
2590 if (!dhcp_client->server_duid)
2592 memcpy(dhcp_client->server_duid, server_id, option_len);
2593 dhcp_client->server_duid_len = option_len;
2595 dhcp_client->confirm_cb(dhcp_client,
2596 dhcp_client->confirm_data);
2604 debug(dhcp_client, "processed DHCP packet (new state %d)",
2605 dhcp_client->state);
2610 static gboolean discover_timeout(gpointer user_data)
2612 GDHCPClient *dhcp_client = user_data;
2614 dhcp_client->retry_times++;
2617 * We do not send the REQUESTED IP option if we are retrying because
2618 * if the server is non-authoritative it will ignore the request if the
2619 * option is present.
2621 g_dhcp_client_start(dhcp_client, NULL);
2626 static gboolean reboot_timeout(gpointer user_data)
2628 GDHCPClient *dhcp_client = user_data;
2629 dhcp_client->retry_times = 0;
2630 dhcp_client->requested_ip = 0;
2631 dhcp_client->state = INIT_SELECTING;
2633 * We do not send the REQUESTED IP option because the server didn't
2634 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2637 g_dhcp_client_start(dhcp_client, NULL);
2642 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2644 GDHCPClient *dhcp_client = dhcp_data;
2646 debug(dhcp_client, "back to MONITOR mode");
2648 dhcp_client->conflicts = 0;
2649 dhcp_client->state = IPV4LL_MONITOR;
2654 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2656 GDHCPClient *dhcp_client = dhcp_data;
2659 #if defined TIZEN_EXT
2664 debug(dhcp_client, "request timeout (retries %d)",
2665 dhcp_client->retry_times);
2667 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2668 dhcp_client->retry_times++;
2669 send_announce_packet(dhcp_client);
2673 ip = htonl(dhcp_client->requested_ip);
2674 debug(dhcp_client, "switching to monitor mode");
2675 dhcp_client->state = IPV4LL_MONITOR;
2676 dhcp_client->assigned_ip = get_ip(ip);
2678 if (dhcp_client->ipv4ll_available_cb)
2679 dhcp_client->ipv4ll_available_cb(dhcp_client,
2680 dhcp_client->ipv4ll_available_data);
2681 dhcp_client->conflicts = 0;
2682 dhcp_client->timeout = 0;
2687 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2690 GDHCPClient *dhcp_client = dhcp_data;
2692 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2693 dhcp_client->retry_times);
2695 if (dhcp_client->retry_times == PROBE_NUM) {
2696 dhcp_client->state = IPV4LL_ANNOUNCE;
2697 dhcp_client->retry_times = 0;
2699 dhcp_client->retry_times++;
2700 send_announce_packet(dhcp_client);
2703 dhcp_client->retry_times++;
2704 send_probe_packet(dhcp_client);
2709 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2715 #if defined TIZEN_EXT
2716 int discover_retry = 0;
2720 remove_timeouts(dhcp_client);
2722 if (dhcp_client->type == G_DHCP_IPV6) {
2723 if (dhcp_client->information_req_cb) {
2724 dhcp_client->state = INFORMATION_REQ;
2725 re = switch_listening_mode(dhcp_client, L3);
2727 switch_listening_mode(dhcp_client, L_NONE);
2728 dhcp_client->state = 0;
2731 send_information_req(dhcp_client);
2733 } else if (dhcp_client->solicitation_cb) {
2734 dhcp_client->state = SOLICITATION;
2735 re = switch_listening_mode(dhcp_client, L3);
2737 switch_listening_mode(dhcp_client, L_NONE);
2738 dhcp_client->state = 0;
2741 send_solicitation(dhcp_client);
2743 } else if (dhcp_client->request_cb) {
2744 dhcp_client->state = REQUEST;
2745 re = switch_listening_mode(dhcp_client, L3);
2747 switch_listening_mode(dhcp_client, L_NONE);
2748 dhcp_client->state = 0;
2751 send_dhcpv6_request(dhcp_client);
2753 } else if (dhcp_client->confirm_cb) {
2754 dhcp_client->state = CONFIRM;
2755 re = switch_listening_mode(dhcp_client, L3);
2757 switch_listening_mode(dhcp_client, L_NONE);
2758 dhcp_client->state = 0;
2761 send_dhcpv6_confirm(dhcp_client);
2763 } else if (dhcp_client->renew_cb) {
2764 dhcp_client->state = RENEW;
2765 re = switch_listening_mode(dhcp_client, L3);
2767 switch_listening_mode(dhcp_client, L_NONE);
2768 dhcp_client->state = 0;
2771 send_dhcpv6_renew(dhcp_client);
2773 } else if (dhcp_client->rebind_cb) {
2774 dhcp_client->state = REBIND;
2775 re = switch_listening_mode(dhcp_client, L3);
2777 switch_listening_mode(dhcp_client, L_NONE);
2778 dhcp_client->state = 0;
2781 send_dhcpv6_rebind(dhcp_client);
2783 } else if (dhcp_client->release_cb) {
2784 dhcp_client->state = RENEW;
2785 re = switch_listening_mode(dhcp_client, L3);
2787 switch_listening_mode(dhcp_client, L_NONE);
2788 dhcp_client->state = 0;
2791 send_dhcpv6_release(dhcp_client);
2792 } else if (dhcp_client->decline_cb) {
2793 dhcp_client->state = DECLINE;
2794 re = switch_listening_mode(dhcp_client, L3);
2796 switch_listening_mode(dhcp_client, L_NONE);
2797 dhcp_client->state = 0;
2800 send_dhcpv6_decline(dhcp_client);
2806 #if defined TIZEN_EXT
2807 if (g_ascii_strncasecmp(dhcp_client->interface, "wlan", 4)
2809 discover_retry = DISCOVER_RETRIES_WIFI;
2810 timeout = DISCOVER_TIMEOUT_WIFI;
2812 discover_retry = DISCOVER_RETRIES;
2813 timeout = DISCOVER_TIMEOUT;
2816 debug(dhcp_client, "[DHCPC] Discover retry/total : [%d]/[%d] timeout [%d]",
2817 dhcp_client->retry_times, discover_retry, timeout);
2821 if (dhcp_client->type == G_DHCP_IPV4LL) {
2822 dhcp_client->state = INIT_SELECTING;
2823 ipv4ll_start(dhcp_client);
2827 #if defined TIZEN_EXT
2828 if (dhcp_client->retry_times == discover_retry) {
2830 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2832 if (dhcp_client->no_lease_cb)
2833 dhcp_client->no_lease_cb(dhcp_client,
2834 dhcp_client->no_lease_data);
2835 dhcp_client->retry_times = 0;
2839 if (dhcp_client->retry_times == 0) {
2840 g_free(dhcp_client->assigned_ip);
2841 dhcp_client->assigned_ip = NULL;
2843 dhcp_client->state = INIT_SELECTING;
2844 re = switch_listening_mode(dhcp_client, L2);
2848 dhcp_get_random(&rand);
2849 dhcp_client->xid = rand;
2850 dhcp_client->start = time(NULL);
2853 if (!last_address) {
2856 addr = ntohl(inet_addr(last_address));
2857 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2860 } else if (dhcp_client->last_address != last_address) {
2861 g_free(dhcp_client->last_address);
2862 dhcp_client->last_address = g_strdup(last_address);
2866 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2867 debug(dhcp_client, "DHCP client start with state init_reboot");
2868 dhcp_client->requested_ip = addr;
2869 dhcp_client->state = REBOOTING;
2870 send_request(dhcp_client);
2872 dhcp_client->timeout = g_timeout_add_seconds_full(
2874 #if defined TIZEN_EXT
2884 send_discover(dhcp_client, addr);
2886 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2887 #if defined TIZEN_EXT
2898 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2900 switch_listening_mode(dhcp_client, L_NONE);
2902 if (dhcp_client->state == BOUND ||
2903 dhcp_client->state == RENEWING ||
2904 dhcp_client->state == REBINDING)
2905 send_release(dhcp_client, dhcp_client->server_ip,
2906 dhcp_client->requested_ip);
2908 remove_timeouts(dhcp_client);
2910 if (dhcp_client->listener_watch > 0) {
2911 g_source_remove(dhcp_client->listener_watch);
2912 dhcp_client->listener_watch = 0;
2915 dhcp_client->retry_times = 0;
2916 dhcp_client->ack_retry_times = 0;
2918 dhcp_client->requested_ip = 0;
2919 dhcp_client->state = RELEASED;
2920 dhcp_client->lease_seconds = 0;
2921 dhcp_client->request_bcast = false;
2924 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2925 unsigned char option_code)
2927 return g_hash_table_lookup(dhcp_client->code_value_hash,
2928 GINT_TO_POINTER((int) option_code));
2931 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2932 GDHCPClientEvent event,
2933 GDHCPClientEventFunc func,
2937 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2938 dhcp_client->lease_available_cb = func;
2939 dhcp_client->lease_available_data = data;
2941 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2942 if (dhcp_client->type == G_DHCP_IPV6)
2944 dhcp_client->ipv4ll_available_cb = func;
2945 dhcp_client->ipv4ll_available_data = data;
2947 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2948 dhcp_client->no_lease_cb = func;
2949 dhcp_client->no_lease_data = data;
2951 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2952 dhcp_client->lease_lost_cb = func;
2953 dhcp_client->lease_lost_data = data;
2955 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2956 if (dhcp_client->type == G_DHCP_IPV6)
2958 dhcp_client->ipv4ll_lost_cb = func;
2959 dhcp_client->ipv4ll_lost_data = data;
2961 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2962 dhcp_client->address_conflict_cb = func;
2963 dhcp_client->address_conflict_data = data;
2965 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2966 if (dhcp_client->type != G_DHCP_IPV6)
2968 dhcp_client->information_req_cb = func;
2969 dhcp_client->information_req_data = data;
2971 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2972 if (dhcp_client->type != G_DHCP_IPV6)
2974 dhcp_client->solicitation_cb = func;
2975 dhcp_client->solicitation_data = data;
2977 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2978 if (dhcp_client->type != G_DHCP_IPV6)
2980 dhcp_client->advertise_cb = func;
2981 dhcp_client->advertise_data = data;
2983 case G_DHCP_CLIENT_EVENT_REQUEST:
2984 if (dhcp_client->type != G_DHCP_IPV6)
2986 dhcp_client->request_cb = func;
2987 dhcp_client->request_data = data;
2989 case G_DHCP_CLIENT_EVENT_RENEW:
2990 if (dhcp_client->type != G_DHCP_IPV6)
2992 dhcp_client->renew_cb = func;
2993 dhcp_client->renew_data = data;
2995 case G_DHCP_CLIENT_EVENT_REBIND:
2996 if (dhcp_client->type != G_DHCP_IPV6)
2998 dhcp_client->rebind_cb = func;
2999 dhcp_client->rebind_data = data;
3001 case G_DHCP_CLIENT_EVENT_RELEASE:
3002 if (dhcp_client->type != G_DHCP_IPV6)
3004 dhcp_client->release_cb = func;
3005 dhcp_client->release_data = data;
3007 case G_DHCP_CLIENT_EVENT_CONFIRM:
3008 if (dhcp_client->type != G_DHCP_IPV6)
3010 dhcp_client->confirm_cb = func;
3011 dhcp_client->confirm_data = data;
3013 case G_DHCP_CLIENT_EVENT_DECLINE:
3014 if (dhcp_client->type != G_DHCP_IPV6)
3016 dhcp_client->decline_cb = func;
3017 dhcp_client->decline_data = data;
3022 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
3024 return dhcp_client->ifindex;
3027 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
3032 #if defined TIZEN_EXT
3033 return get_ip(htonl(dhcp_client->server_ip));
3035 return get_ip(dhcp_client->server_ip);
3039 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
3041 return g_strdup(dhcp_client->assigned_ip);
3044 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
3046 GList *option = NULL;
3048 if (dhcp_client->type == G_DHCP_IPV6)
3051 switch (dhcp_client->state) {
3053 case IPV4LL_MONITOR:
3054 return g_strdup("255.255.0.0");
3058 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3060 return g_strdup(option->data);
3061 case INIT_SELECTING:
3066 case IPV4LL_ANNOUNCE:
3067 case INFORMATION_REQ:
3080 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3081 unsigned int option_code)
3083 if (!g_list_find(dhcp_client->request_list,
3084 GINT_TO_POINTER((int)option_code)))
3085 dhcp_client->request_list = g_list_prepend(
3086 dhcp_client->request_list,
3087 (GINT_TO_POINTER((int) option_code)));
3089 return G_DHCP_CLIENT_ERROR_NONE;
3092 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3094 g_list_free(dhcp_client->request_list);
3095 dhcp_client->request_list = NULL;
3098 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3100 g_hash_table_remove_all(dhcp_client->send_value_hash);
3103 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3107 storage = g_try_malloc(size + OPT_DATA);
3111 storage[OPT_CODE] = code;
3112 storage[OPT_LEN] = size;
3113 memcpy(&storage[OPT_DATA], data, size);
3118 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3121 return alloc_dhcp_option(code, data, MIN(size, 255));
3124 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3126 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3129 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3131 const unsigned maclen = 6;
3132 const unsigned idlen = maclen + 1;
3133 const uint8_t option_code = G_DHCP_CLIENT_ID;
3134 uint8_t idbuf[idlen];
3135 uint8_t *data_option;
3137 idbuf[0] = ARPHRD_ETHER;
3139 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3141 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3143 return G_DHCP_CLIENT_ERROR_NOMEM;
3145 g_hash_table_insert(dhcp_client->send_value_hash,
3146 GINT_TO_POINTER((int) option_code), data_option);
3148 return G_DHCP_CLIENT_ERROR_NONE;
3151 /* Now only support send hostname */
3152 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3153 unsigned char option_code, const char *option_value)
3155 uint8_t *binary_option;
3157 if (option_code == G_DHCP_HOST_NAME && option_value) {
3158 binary_option = alloc_dhcp_string_option(option_code,
3161 return G_DHCP_CLIENT_ERROR_NOMEM;
3163 g_hash_table_insert(dhcp_client->send_value_hash,
3164 GINT_TO_POINTER((int) option_code), binary_option);
3167 return G_DHCP_CLIENT_ERROR_NONE;
3170 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3175 storage = g_malloc(2 + 2 + len);
3179 storage[0] = code >> 8;
3180 storage[1] = code & 0xff;
3181 storage[2] = len >> 8;
3182 storage[3] = len & 0xff;
3183 memcpy(storage + 2 + 2, option, len);
3188 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3190 return g_hash_table_remove(dhcp_client->send_value_hash,
3191 GINT_TO_POINTER((int)code));
3194 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3195 uint16_t option_code,
3196 uint8_t *option_value,
3197 uint16_t option_len)
3200 uint8_t *binary_option;
3202 debug(dhcp_client, "setting option %d to %p len %d",
3203 option_code, option_value, option_len);
3205 binary_option = alloc_dhcpv6_option(option_code, option_value,
3208 g_hash_table_insert(dhcp_client->send_value_hash,
3209 GINT_TO_POINTER((int) option_code),
3214 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3216 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3219 dhcp_client->last_request = time(NULL);
3222 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3224 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3227 return dhcp_client->status_code;
3230 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3235 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3240 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3245 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3248 g_dhcp_client_stop(dhcp_client);
3250 g_free(dhcp_client->interface);
3251 g_free(dhcp_client->assigned_ip);
3252 g_free(dhcp_client->last_address);
3253 g_free(dhcp_client->duid);
3254 g_free(dhcp_client->server_duid);
3256 g_list_free(dhcp_client->request_list);
3257 g_list_free(dhcp_client->require_list);
3259 g_hash_table_destroy(dhcp_client->code_value_hash);
3260 g_hash_table_destroy(dhcp_client->send_value_hash);
3262 g_free(dhcp_client);
3263 #if defined TIZEN_EXT
3268 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3269 GDHCPDebugFunc func, gpointer user_data)
3274 dhcp_client->debug_func = func;
3275 dhcp_client->debug_data = user_data;
3278 static GDHCPIAPrefix *copy_prefix(gpointer data)
3280 GDHCPIAPrefix *copy, *prefix = data;
3282 copy = g_try_new(GDHCPIAPrefix, 1);
3286 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3291 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3293 GSList *copy = NULL;
3296 for (list = prefixes; list; list = list->next)
3297 copy = g_slist_prepend(copy, copy_prefix(list->data));
3302 #if defined TIZEN_EXT
3303 void g_dhcp_client_set_address_known(GDHCPClient *dhcp_client, gboolean known)
3305 /* DHCPREQUEST during INIT-REBOOT state (rfc2131)
3306 * 4.4.3 Initialization with known network address
3307 * 4.3.2 DHCPREQUEST generated during INIT-REBOOT state
3309 debug(dhcp_client, "known network address (%d)", known);
3311 if (dhcp_client->init_reboot == known)
3314 dhcp_client->init_reboot = known;