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;
161 static inline void debug(GDHCPClient *client, const char *format, ...)
166 if (!client->debug_func)
169 va_start(ap, format);
171 if (vsnprintf(str, sizeof(str), format, ap) > 0)
172 client->debug_func(str, client->debug_data);
177 /* Initialize the packet with the proper defaults */
178 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
180 if (dhcp_client->type == G_DHCP_IPV6)
181 dhcpv6_init_header(pkt, type);
183 struct dhcp_packet *packet = pkt;
185 dhcp_init_header(packet, type);
186 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
190 static void add_request_options(GDHCPClient *dhcp_client,
191 struct dhcp_packet *packet)
196 int end = dhcp_end_option(packet->options);
198 for (list = dhcp_client->request_list; list; list = list->next) {
199 code = (uint8_t) GPOINTER_TO_INT(list->data);
201 packet->options[end + OPT_DATA + len] = code;
206 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
207 packet->options[end + OPT_LEN] = len;
208 packet->options[end + OPT_DATA + len] = DHCP_END;
215 unsigned char **ptr_buf;
218 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
221 uint8_t *option = value;
223 struct hash_params *params = user_data;
225 /* option[0][1] contains option code */
226 len = option[2] << 8 | option[3];
228 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
231 memcpy(*params->ptr_buf, option, len + 2 + 2);
232 (*params->ptr_buf) += len + 2 + 2;
235 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
236 unsigned char *buf, int max_buf,
237 unsigned char **ptr_buf)
239 struct hash_params params = {
245 if (dhcp_client->type != G_DHCP_IPV6)
248 g_hash_table_foreach(dhcp_client->send_value_hash,
249 add_dhcpv6_binary_option, ¶ms);
251 *ptr_buf = *params.ptr_buf;
254 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
258 buf[1] = code & 0xff;
262 memcpy(&buf[4], msg, len);
265 static int32_t get_time_diff(struct timeval *tv)
270 gettimeofday(&now, NULL);
272 hsec = (now.tv_sec - tv->tv_sec) * 100;
273 hsec += (now.tv_usec - tv->tv_usec) / 10000;
278 static void remove_timeouts(GDHCPClient *dhcp_client)
281 if (dhcp_client->timeout > 0)
282 g_source_remove(dhcp_client->timeout);
283 if (dhcp_client->t1_timeout > 0)
284 g_source_remove(dhcp_client->t1_timeout);
285 if (dhcp_client->t2_timeout > 0)
286 g_source_remove(dhcp_client->t2_timeout);
287 if (dhcp_client->lease_timeout > 0)
288 g_source_remove(dhcp_client->lease_timeout);
290 dhcp_client->timeout = 0;
291 dhcp_client->t1_timeout = 0;
292 dhcp_client->t2_timeout = 0;
293 dhcp_client->lease_timeout = 0;
297 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
298 struct dhcpv6_packet *packet,
299 unsigned char *buf, int max_buf,
300 unsigned char **ptr_buf)
303 uint16_t code, value;
308 if (dhcp_client->type != G_DHCP_IPV6)
311 for (list = dhcp_client->request_list; list; list = list->next) {
312 code = (uint16_t) GPOINTER_TO_INT(list->data);
316 case G_DHCPV6_CLIENTID:
317 if (!dhcp_client->duid)
320 len = 2 + 2 + dhcp_client->duid_len;
321 if ((*ptr_buf + len) > (buf + max_buf)) {
322 debug(dhcp_client, "Too long dhcpv6 message "
323 "when writing client id option");
327 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
328 dhcp_client->duid_len, dhcp_client->duid);
333 case G_DHCPV6_SERVERID:
334 if (!dhcp_client->server_duid)
337 len = 2 + 2 + dhcp_client->server_duid_len;
338 if ((*ptr_buf + len) > (buf + max_buf)) {
339 debug(dhcp_client, "Too long dhcpv6 message "
340 "when writing server id option");
344 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
345 dhcp_client->server_duid_len,
346 dhcp_client->server_duid);
351 case G_DHCPV6_RAPID_COMMIT:
353 if ((*ptr_buf + len) > (buf + max_buf)) {
354 debug(dhcp_client, "Too long dhcpv6 message "
355 "when writing rapid commit option");
359 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
367 case G_DHCPV6_ELAPSED_TIME:
368 if (!dhcp_client->retransmit) {
370 * Initial message, elapsed time is 0.
374 diff = get_time_diff(&dhcp_client->start_time);
375 if (diff < 0 || diff > 0xffff)
380 if ((*ptr_buf + len) > (buf + max_buf)) {
381 debug(dhcp_client, "Too long dhcpv6 message "
382 "when writing elapsed time option");
386 value = htons((uint16_t)diff);
387 copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
388 2, (uint8_t *)&value);
393 case G_DHCPV6_DNS_SERVERS:
396 case G_DHCPV6_DOMAIN_LIST:
399 case G_DHCPV6_SNTP_SERVERS:
407 debug(dhcp_client, "option %d len %d added", code, len);
411 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
413 uint8_t *option = value;
414 struct dhcp_packet *packet = user_data;
416 dhcp_add_binary_option(packet, option);
419 static void add_send_options(GDHCPClient *dhcp_client,
420 struct dhcp_packet *packet)
422 g_hash_table_foreach(dhcp_client->send_value_hash,
423 add_binary_option, packet);
427 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
428 * represents the number of seconds elapsed from the start of
429 * attempting DHCP to satisfy some DHCP servers that allow for an
430 * "authoritative" reply before responding.
432 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
434 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
437 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
439 struct dhcp_packet packet;
441 debug(dhcp_client, "sending DHCP discover request");
443 init_packet(dhcp_client, &packet, DHCPDISCOVER);
445 packet.xid = dhcp_client->xid;
446 packet.secs = dhcp_attempt_secs(dhcp_client);
449 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
451 /* Explicitly saying that we want RFC-compliant packets helps
452 * some buggy DHCP servers to NOT send bigger packets */
453 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
455 add_request_options(dhcp_client, &packet);
457 add_send_options(dhcp_client, &packet);
460 * If we do not get a reply to DISCOVER packet, then we try with
461 * broadcast flag set. So first packet is sent without broadcast flag,
462 * first retry is with broadcast flag, second retry is without it etc.
463 * Reason is various buggy routers/AP that either eat the other or vice
464 * versa. In the receiving side we then find out what kind of packet
465 * the server can send.
467 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
468 INADDR_BROADCAST, SERVER_PORT,
469 MAC_BCAST_ADDR, dhcp_client->ifindex,
470 dhcp_client->retry_times % 2);
473 static int send_request(GDHCPClient *dhcp_client)
475 struct dhcp_packet packet;
477 debug(dhcp_client, "sending DHCP request (state %d)",
480 init_packet(dhcp_client, &packet, DHCPREQUEST);
482 packet.xid = dhcp_client->xid;
483 packet.secs = dhcp_attempt_secs(dhcp_client);
485 if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
486 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
487 dhcp_client->requested_ip);
489 if (dhcp_client->state == REQUESTING)
490 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
491 dhcp_client->server_ip);
493 dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
495 add_request_options(dhcp_client, &packet);
497 add_send_options(dhcp_client, &packet);
499 if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
500 packet.ciaddr = htonl(dhcp_client->requested_ip);
502 if (dhcp_client->state == RENEWING)
503 return dhcp_send_kernel_packet(&packet,
504 dhcp_client->requested_ip, CLIENT_PORT,
505 dhcp_client->server_ip, SERVER_PORT);
507 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
508 INADDR_BROADCAST, SERVER_PORT,
509 MAC_BCAST_ADDR, dhcp_client->ifindex,
510 dhcp_client->request_bcast);
513 static int send_release(GDHCPClient *dhcp_client,
514 uint32_t server, uint32_t ciaddr)
516 struct dhcp_packet packet;
519 debug(dhcp_client, "sending DHCP release request");
521 init_packet(dhcp_client, &packet, DHCPRELEASE);
522 dhcp_get_random(&rand);
524 packet.ciaddr = htonl(ciaddr);
526 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
528 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
529 server, SERVER_PORT);
532 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
533 static int switch_listening_mode(GDHCPClient *dhcp_client,
534 ListenMode listen_mode);
536 static gboolean send_probe_packet(gpointer dhcp_data)
538 GDHCPClient *dhcp_client;
541 dhcp_client = dhcp_data;
542 /* if requested_ip is not valid, pick a new address*/
543 if (dhcp_client->requested_ip == 0) {
544 debug(dhcp_client, "pick a new random address");
545 dhcp_client->requested_ip = ipv4ll_random_ip();
548 debug(dhcp_client, "sending IPV4LL probe request");
550 if (dhcp_client->retry_times == 1) {
551 dhcp_client->state = IPV4LL_PROBE;
552 switch_listening_mode(dhcp_client, L_ARP);
554 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
555 dhcp_client->requested_ip, dhcp_client->ifindex);
557 if (dhcp_client->retry_times < PROBE_NUM) {
558 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
559 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
560 timeout += PROBE_MIN*1000;
562 timeout = (ANNOUNCE_WAIT * 1000);
564 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
566 ipv4ll_probe_timeout,
572 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
573 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
575 static gboolean send_announce_packet(gpointer dhcp_data)
577 GDHCPClient *dhcp_client;
579 dhcp_client = dhcp_data;
581 debug(dhcp_client, "sending IPV4LL announce request");
583 ipv4ll_send_arp_packet(dhcp_client->mac_address,
584 dhcp_client->requested_ip,
585 dhcp_client->requested_ip,
586 dhcp_client->ifindex);
588 remove_timeouts(dhcp_client);
590 if (dhcp_client->state == IPV4LL_DEFEND) {
591 dhcp_client->timeout =
592 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
594 ipv4ll_defend_timeout,
599 dhcp_client->timeout =
600 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
602 ipv4ll_announce_timeout,
608 static void get_interface_mac_address(int index, uint8_t *mac_address)
613 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
615 perror("Open socket error");
619 memset(&ifr, 0, sizeof(ifr));
620 ifr.ifr_ifindex = index;
622 err = ioctl(sk, SIOCGIFNAME, &ifr);
624 perror("Get interface name error");
628 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
630 perror("Get mac address error");
634 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
640 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
645 dhcp_client->retransmit = true;
648 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
653 dhcp_client->retransmit = false;
656 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
657 unsigned char **duid, int *duid_len)
662 case G_DHCPV6_DUID_LLT:
663 *duid_len = 2 + 2 + 4 + ETH_ALEN;
664 *duid = g_try_malloc(*duid_len);
670 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
673 duid_time = time(NULL) - DUID_TIME_EPOCH;
674 (*duid)[4] = duid_time >> 24;
675 (*duid)[5] = duid_time >> 16;
676 (*duid)[6] = duid_time >> 8;
677 (*duid)[7] = duid_time & 0xff;
679 case G_DHCPV6_DUID_EN:
681 case G_DHCPV6_DUID_LL:
682 *duid_len = 2 + 2 + ETH_ALEN;
683 *duid = g_try_malloc(*duid_len);
689 get_interface_mac_address(index, &(*duid)[2 + 2]);
698 static gchar *convert_to_hex(unsigned char *buf, int len)
700 gchar *ret = g_try_malloc(len * 2 + 1);
703 for (i = 0; ret && i < len; i++)
704 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
709 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
712 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
715 g_free(dhcp_client->duid);
717 dhcp_client->duid = duid;
718 dhcp_client->duid_len = duid_len;
720 if (dhcp_client->debug_func) {
721 gchar *hex = convert_to_hex(duid, duid_len);
722 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
729 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
730 uint32_t *T2, GSList *prefixes)
732 uint8_t options[1452];
733 unsigned int max_buf = sizeof(options);
734 int len, count = g_slist_length(prefixes);
736 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
739 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
741 memset(options, 0, sizeof(options));
743 options[0] = dhcp_client->iaid >> 24;
744 options[1] = dhcp_client->iaid >> 16;
745 options[2] = dhcp_client->iaid >> 8;
746 options[3] = dhcp_client->iaid;
749 uint32_t t = htonl(*T1);
750 memcpy(&options[4], &t, 4);
754 uint32_t t = htonl(*T2);
755 memcpy(&options[8], &t, 4);
763 for (list = prefixes; list; list = list->next) {
764 GDHCPIAPrefix *prefix = list->data;
765 uint8_t sub_option[4+4+1+16];
767 if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
769 "Too long dhcpv6 message "
770 "when writing IA prefix option");
774 memset(&sub_option, 0, sizeof(sub_option));
776 /* preferred and validity time are left zero */
778 sub_option[8] = prefix->prefixlen;
779 memcpy(&sub_option[9], &prefix->prefix, 16);
781 copy_option(&options[len], G_DHCPV6_IA_PREFIX,
782 sizeof(sub_option), sub_option);
783 len += 2 + 2 + sizeof(sub_option);
787 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
793 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
795 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
798 return dhcp_client->iaid;
801 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
803 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
806 dhcp_client->iaid = iaid;
809 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
814 get_interface_mac_address(index, buf);
816 memcpy(iaid, &buf[2], 4);
817 dhcp_client->iaid = iaid[0] << 24 |
818 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
821 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
822 uint32_t *T1, uint32_t *T2,
826 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
830 *T1 = dhcp_client->T1;
833 *T2 = dhcp_client->T2;
836 *started = dhcp_client->last_request;
839 *expire = dhcp_client->last_request + dhcp_client->expire;
844 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
848 buf[1] = G_DHCPV6_IAADDR;
851 memcpy(&buf[4], &dhcp_client->ia_na, 16);
852 memset(&buf[20], 0, 4); /* preferred */
853 memset(&buf[24], 0, 4); /* valid */
857 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
860 struct in6_addr addr;
862 if (inet_pton(AF_INET6, address, &addr) != 1)
866 buf[1] = G_DHCPV6_IAADDR;
869 memcpy(&buf[4], &addr, 16);
870 memset(&buf[20], 0, 4); /* preferred */
871 memset(&buf[24], 0, 4); /* valid */
875 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
879 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
881 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
891 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
892 int code, uint32_t *T1, uint32_t *T2,
893 bool add_iaaddr, const char *ia_na)
895 if (code == G_DHCPV6_IA_TA) {
896 uint8_t ia_options[4];
898 put_iaid(dhcp_client, index, ia_options);
900 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
901 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
902 ia_options, sizeof(ia_options));
904 } else if (code == G_DHCPV6_IA_NA) {
905 struct in6_addr addr;
907 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
910 * If caller has specified the IPv6 address it wishes to
911 * to use (ia_na != NULL and address is valid), then send
912 * the address to server.
913 * If caller did not specify the address (ia_na == NULL) and
914 * if the current address is not set, then we should not send
915 * the address sub-option.
917 if (add_iaaddr && ((!ia_na &&
918 !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
920 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
921 #define IAADDR_LEN (16+4+4)
922 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
925 memcpy(&dhcp_client->ia_na, &addr,
926 sizeof(struct in6_addr));
928 put_iaid(dhcp_client, index, ia_options);
931 ia_options[4] = *T1 >> 24;
932 ia_options[5] = *T1 >> 16;
933 ia_options[6] = *T1 >> 8;
936 memset(&ia_options[4], 0x00, 4);
939 ia_options[8] = *T2 >> 24;
940 ia_options[9] = *T2 >> 16;
941 ia_options[10] = *T2 >> 8;
942 ia_options[11] = *T2;
944 memset(&ia_options[8], 0x00, 4);
946 create_iaaddr(dhcp_client, &ia_options[12],
949 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
950 ia_options, sizeof(ia_options));
952 uint8_t ia_options[4+4+4];
954 put_iaid(dhcp_client, index, ia_options);
956 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
957 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
959 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
960 ia_options, sizeof(ia_options));
969 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
970 int code, uint32_t *T1, uint32_t *T2,
974 uint8_t *ia_options, *pos;
975 int len, count, total_len;
977 count = g_slist_length(addresses);
981 g_dhcp_client_set_request(dhcp_client, code);
983 if (code == G_DHCPV6_IA_TA)
985 else if (code == G_DHCPV6_IA_NA)
986 len = 4 + 4 + 4; /* IAID + T1 + T2 */
990 total_len = len + count * (2 + 2 + 16 + 4 + 4);
991 ia_options = g_try_malloc0(total_len);
995 put_iaid(dhcp_client, index, ia_options);
997 pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
999 for (list = addresses; list; list = list->next) {
1000 pos = append_iaaddr(dhcp_client, pos, list->data);
1005 if (code == G_DHCPV6_IA_NA) {
1007 ia_options[4] = *T1 >> 24;
1008 ia_options[5] = *T1 >> 16;
1009 ia_options[6] = *T1 >> 8;
1010 ia_options[7] = *T1;
1012 memset(&ia_options[4], 0x00, 4);
1015 ia_options[8] = *T2 >> 24;
1016 ia_options[9] = *T2 >> 16;
1017 ia_options[10] = *T2 >> 8;
1018 ia_options[11] = *T2;
1020 memset(&ia_options[8], 0x00, 4);
1023 g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1030 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1033 int i, j, len = sizeof(uint16_t) * args;
1036 values = g_try_malloc(len);
1041 for (i = 0, j = 0; i < args; i++) {
1042 uint16_t value = va_arg(va, int);
1043 values[j++] = value >> 8;
1044 values[j++] = value & 0xff;
1048 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1055 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1057 struct dhcpv6_packet *packet;
1058 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1062 memset(buf, 0, sizeof(buf));
1063 packet = (struct dhcpv6_packet *)&buf[0];
1064 ptr = buf + sizeof(struct dhcpv6_packet);
1066 init_packet(dhcp_client, packet, type);
1068 if (!dhcp_client->retransmit) {
1069 dhcp_client->xid = packet->transaction_id[0] << 16 |
1070 packet->transaction_id[1] << 8 |
1071 packet->transaction_id[2];
1072 gettimeofday(&dhcp_client->start_time, NULL);
1074 packet->transaction_id[0] = dhcp_client->xid >> 16;
1075 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1076 packet->transaction_id[2] = dhcp_client->xid;
1079 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1081 debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1084 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1086 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1088 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1090 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1092 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1096 static int send_solicitation(GDHCPClient *dhcp_client)
1098 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1101 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1103 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1106 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1108 return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1111 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1113 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1116 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1118 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1121 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1123 return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1126 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1128 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1131 static int send_information_req(GDHCPClient *dhcp_client)
1133 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1137 static void remove_value(gpointer data, gpointer user_data)
1143 static void remove_option_value(gpointer data)
1145 GList *option_value = data;
1147 g_list_foreach(option_value, remove_value, NULL);
1148 g_list_free(option_value);
1151 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1152 int ifindex, GDHCPClientError *error)
1154 GDHCPClient *dhcp_client;
1157 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1161 dhcp_client = g_try_new0(GDHCPClient, 1);
1163 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1167 dhcp_client->interface = get_interface_name(ifindex);
1168 if (!dhcp_client->interface) {
1169 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1173 if (!interface_is_up(ifindex)) {
1174 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1178 get_interface_mac_address(ifindex, dhcp_client->mac_address);
1180 dhcp_client->listener_sockfd = -1;
1181 dhcp_client->listen_mode = L_NONE;
1182 dhcp_client->ref_count = 1;
1183 dhcp_client->type = type;
1184 dhcp_client->ifindex = ifindex;
1185 dhcp_client->lease_available_cb = NULL;
1186 dhcp_client->ipv4ll_available_cb = NULL;
1187 dhcp_client->no_lease_cb = NULL;
1188 dhcp_client->lease_lost_cb = NULL;
1189 dhcp_client->ipv4ll_lost_cb = NULL;
1190 dhcp_client->address_conflict_cb = NULL;
1191 dhcp_client->listener_watch = 0;
1192 dhcp_client->retry_times = 0;
1193 dhcp_client->ack_retry_times = 0;
1194 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1195 g_direct_equal, NULL, remove_option_value);
1196 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1197 g_direct_equal, NULL, g_free);
1198 dhcp_client->request_list = NULL;
1199 dhcp_client->require_list = NULL;
1200 dhcp_client->duid = NULL;
1201 dhcp_client->duid_len = 0;
1202 dhcp_client->last_request = time(NULL);
1203 dhcp_client->expire = 0;
1204 dhcp_client->request_bcast = false;
1206 *error = G_DHCP_CLIENT_ERROR_NONE;
1211 g_free(dhcp_client->interface);
1212 g_free(dhcp_client);
1216 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
1218 static int dhcp_l2_socket(int ifindex)
1221 struct sockaddr_ll sock;
1226 * I've selected not to see LL header, so BPF doesn't see it, too.
1227 * The filter may also pass non-IP and non-ARP packets, but we do
1228 * a more complete check when receiving the message in userspace.
1230 * and filter shamelessly stolen from:
1232 * http://www.flamewarmaster.de/software/dhcpclient/
1234 * There are a few other interesting ideas on that page (look under
1235 * "Motivation"). Use of netlink events is most interesting. Think
1236 * of various network servers listening for events and reconfiguring.
1237 * That would obsolete sending HUP signals and/or make use of restarts.
1239 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1242 * TODO: make conditional?
1244 static const struct sock_filter filter_instr[] = {
1246 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1247 /* L5, L1, is UDP? */
1248 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1249 /* ugly check for arp on ethernet-like and IPv4 */
1250 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1251 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1252 /* skip IP header */
1253 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1254 /* check udp source and destination ports */
1255 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1257 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1259 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1260 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1263 static const struct sock_fprog filter_prog = {
1264 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1265 /* casting const away: */
1266 .filter = (struct sock_filter *) filter_instr,
1269 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
1273 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1274 /* Use only if standard ports are in use */
1275 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1276 sizeof(filter_prog));
1278 memset(&sock, 0, sizeof(sock));
1279 sock.sll_family = AF_PACKET;
1280 sock.sll_protocol = htons(ETH_P_IP);
1281 sock.sll_ifindex = ifindex;
1283 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1292 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1294 if (packet->ip.protocol != IPPROTO_UDP)
1297 if (packet->ip.version != IPVERSION)
1300 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1303 if (packet->udp.dest != htons(CLIENT_PORT))
1306 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1312 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1313 struct sockaddr_in *dst_addr)
1316 struct ip_udp_dhcp_packet packet;
1319 memset(&packet, 0, sizeof(packet));
1321 bytes = read(fd, &packet, sizeof(packet));
1325 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1328 if (bytes < ntohs(packet.ip.tot_len))
1329 /* packet is bigger than sizeof(packet), we did partial read */
1332 /* ignore any extra garbage bytes */
1333 bytes = ntohs(packet.ip.tot_len);
1335 if (!sanity_check(&packet, bytes))
1338 check = packet.ip.check;
1339 packet.ip.check = 0;
1340 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1343 /* verify UDP checksum. IP header has to be modified for this */
1344 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1345 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1346 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1347 check = packet.udp.check;
1348 packet.udp.check = 0;
1349 if (check && check != dhcp_checksum(&packet, bytes))
1352 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1353 sizeof(packet.udp)));
1355 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1358 dst_addr->sin_addr.s_addr = packet.ip.daddr;
1360 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1363 static void ipv4ll_start(GDHCPClient *dhcp_client)
1367 remove_timeouts(dhcp_client);
1369 switch_listening_mode(dhcp_client, L_NONE);
1370 dhcp_client->retry_times = 0;
1371 dhcp_client->requested_ip = 0;
1373 dhcp_client->requested_ip = ipv4ll_random_ip();
1375 /*first wait a random delay to avoid storm of arp request on boot*/
1376 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1378 dhcp_client->retry_times++;
1379 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1386 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1389 switch_listening_mode(dhcp_client, L_NONE);
1391 remove_timeouts(dhcp_client);
1393 if (dhcp_client->listener_watch > 0) {
1394 g_source_remove(dhcp_client->listener_watch);
1395 dhcp_client->listener_watch = 0;
1398 dhcp_client->state = IPV4LL_PROBE;
1399 dhcp_client->retry_times = 0;
1400 dhcp_client->requested_ip = 0;
1402 g_free(dhcp_client->assigned_ip);
1403 dhcp_client->assigned_ip = NULL;
1406 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1409 struct ether_arp arp;
1410 uint32_t ip_requested;
1411 int source_conflict;
1412 int target_conflict;
1414 memset(&arp, 0, sizeof(arp));
1415 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1419 if (arp.arp_op != htons(ARPOP_REPLY) &&
1420 arp.arp_op != htons(ARPOP_REQUEST))
1423 ip_requested = htonl(dhcp_client->requested_ip);
1424 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1425 sizeof(ip_requested));
1427 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1428 sizeof(ip_requested));
1430 if (!source_conflict && !target_conflict)
1433 dhcp_client->conflicts++;
1435 debug(dhcp_client, "IPV4LL conflict detected");
1437 if (dhcp_client->state == IPV4LL_MONITOR) {
1438 if (!source_conflict)
1440 dhcp_client->state = IPV4LL_DEFEND;
1441 debug(dhcp_client, "DEFEND mode conflicts : %d",
1442 dhcp_client->conflicts);
1443 /*Try to defend with a single announce*/
1444 send_announce_packet(dhcp_client);
1448 if (dhcp_client->state == IPV4LL_DEFEND) {
1449 if (!source_conflict)
1451 else if (dhcp_client->ipv4ll_lost_cb)
1452 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1453 dhcp_client->ipv4ll_lost_data);
1456 ipv4ll_stop(dhcp_client);
1458 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1459 /*restart whole state machine*/
1460 dhcp_client->retry_times++;
1461 dhcp_client->timeout =
1462 g_timeout_add_full(G_PRIORITY_HIGH,
1463 ipv4ll_random_delay_ms(PROBE_WAIT),
1468 /* Here we got a lot of conflicts, RFC3927 states that we have
1469 * to wait RATE_LIMIT_INTERVAL before retrying,
1470 * but we just report failure.
1472 else if (dhcp_client->no_lease_cb)
1473 dhcp_client->no_lease_cb(dhcp_client,
1474 dhcp_client->no_lease_data);
1479 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1481 if (dhcp_client->type == G_DHCP_IPV6) {
1482 struct dhcpv6_packet *packet6 = pkt;
1488 xid = packet6->transaction_id[0] << 16 |
1489 packet6->transaction_id[1] << 8 |
1490 packet6->transaction_id[2];
1492 if (xid != dhcp_client->xid)
1495 struct dhcp_packet *packet = pkt;
1497 if (packet->xid != dhcp_client->xid)
1500 if (packet->hlen != 6)
1503 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1510 static void start_request(GDHCPClient *dhcp_client);
1512 static gboolean request_timeout(gpointer user_data)
1514 GDHCPClient *dhcp_client = user_data;
1516 debug(dhcp_client, "request timeout (retries %d)",
1517 dhcp_client->retry_times);
1519 dhcp_client->retry_times++;
1521 start_request(dhcp_client);
1526 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1527 gpointer user_data);
1529 static int switch_listening_mode(GDHCPClient *dhcp_client,
1530 ListenMode listen_mode)
1532 GIOChannel *listener_channel;
1533 int listener_sockfd;
1535 if (dhcp_client->listen_mode == listen_mode)
1538 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1539 dhcp_client->listen_mode, listen_mode);
1541 if (dhcp_client->listen_mode != L_NONE) {
1542 if (dhcp_client->listener_watch > 0)
1543 g_source_remove(dhcp_client->listener_watch);
1544 dhcp_client->listen_mode = L_NONE;
1545 dhcp_client->listener_sockfd = -1;
1546 dhcp_client->listener_watch = 0;
1549 if (listen_mode == L_NONE)
1552 if (listen_mode == L2)
1553 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1554 else if (listen_mode == L3) {
1555 if (dhcp_client->type == G_DHCP_IPV6)
1556 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1557 dhcp_client->interface,
1560 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1561 dhcp_client->interface,
1563 } else if (listen_mode == L_ARP)
1564 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1568 if (listener_sockfd < 0)
1571 listener_channel = g_io_channel_unix_new(listener_sockfd);
1572 if (!listener_channel) {
1573 /* Failed to create listener channel */
1574 close(listener_sockfd);
1578 dhcp_client->listen_mode = listen_mode;
1579 dhcp_client->listener_sockfd = listener_sockfd;
1581 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1582 dhcp_client->listener_watch =
1583 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1584 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1585 listener_event, dhcp_client,
1587 g_io_channel_unref(listener_channel);
1592 static void start_request(GDHCPClient *dhcp_client)
1594 debug(dhcp_client, "start request (retries %d)",
1595 dhcp_client->retry_times);
1597 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1598 if (dhcp_client->no_lease_cb)
1599 dhcp_client->no_lease_cb(dhcp_client,
1600 dhcp_client->no_lease_data);
1604 if (dhcp_client->retry_times == 0) {
1605 dhcp_client->state = REQUESTING;
1606 switch_listening_mode(dhcp_client, L2);
1609 send_request(dhcp_client);
1611 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1618 static uint32_t get_lease(struct dhcp_packet *packet)
1621 uint32_t lease_seconds;
1623 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1627 lease_seconds = get_be32(option);
1628 /* paranoia: must not be prone to overflows */
1629 lease_seconds &= 0x0fffffff;
1630 if (lease_seconds < 10)
1633 return lease_seconds;
1636 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1638 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1640 remove_timeouts(dhcp_client);
1642 dhcp_client->retry_times = retry_times;
1643 dhcp_client->requested_ip = 0;
1644 dhcp_client->state = INIT_SELECTING;
1645 switch_listening_mode(dhcp_client, L2);
1647 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1650 static gboolean start_expire(gpointer user_data)
1652 GDHCPClient *dhcp_client = user_data;
1654 debug(dhcp_client, "lease expired");
1656 /*remove all timeouts if they are set*/
1657 remove_timeouts(dhcp_client);
1659 restart_dhcp(dhcp_client, 0);
1661 /* ip need to be cleared */
1662 if (dhcp_client->lease_lost_cb)
1663 dhcp_client->lease_lost_cb(dhcp_client,
1664 dhcp_client->lease_lost_data);
1669 static gboolean continue_rebound(gpointer user_data)
1671 GDHCPClient *dhcp_client = user_data;
1674 switch_listening_mode(dhcp_client, L2);
1675 send_request(dhcp_client);
1677 if (dhcp_client->t2_timeout> 0)
1678 g_source_remove(dhcp_client->t2_timeout);
1680 /*recalculate remaining rebind time*/
1681 dhcp_client->T2 >>= 1;
1682 if (dhcp_client->T2 > 60) {
1683 dhcp_get_random(&rand);
1684 dhcp_client->t2_timeout =
1685 g_timeout_add_full(G_PRIORITY_HIGH,
1686 dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1695 static gboolean start_rebound(gpointer user_data)
1697 GDHCPClient *dhcp_client = user_data;
1699 /*remove renew timer*/
1700 if (dhcp_client->t1_timeout > 0)
1701 g_source_remove(dhcp_client->t1_timeout);
1703 debug(dhcp_client, "start rebound");
1704 dhcp_client->state = REBINDING;
1706 /*calculate total rebind time*/
1707 dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1709 /*send the first rebound and reschedule*/
1710 continue_rebound(user_data);
1715 static gboolean continue_renew (gpointer user_data)
1717 GDHCPClient *dhcp_client = user_data;
1720 switch_listening_mode(dhcp_client, L3);
1721 send_request(dhcp_client);
1723 if (dhcp_client->t1_timeout > 0)
1724 g_source_remove(dhcp_client->t1_timeout);
1726 dhcp_client->t1_timeout = 0;
1728 dhcp_client->T1 >>= 1;
1730 if (dhcp_client->T1 > 60) {
1731 dhcp_get_random(&rand);
1732 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1733 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1741 static gboolean start_renew(gpointer user_data)
1743 GDHCPClient *dhcp_client = user_data;
1745 debug(dhcp_client, "start renew");
1746 dhcp_client->state = RENEWING;
1748 /*calculate total renew period*/
1749 dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1751 /*send first renew and reschedule for half the remaining time.*/
1752 continue_renew(user_data);
1757 static void start_bound(GDHCPClient *dhcp_client)
1759 debug(dhcp_client, "start bound");
1761 dhcp_client->state = BOUND;
1763 remove_timeouts(dhcp_client);
1766 *TODO: T1 and T2 should be set through options instead of
1767 * defaults as they are here.
1770 dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1771 dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1772 dhcp_client->expire = dhcp_client->lease_seconds;
1774 dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1776 start_renew, dhcp_client,
1779 dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1781 start_rebound, dhcp_client,
1784 dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1785 dhcp_client->expire,
1786 start_expire, dhcp_client,
1790 static gboolean restart_dhcp_timeout(gpointer user_data)
1792 GDHCPClient *dhcp_client = user_data;
1794 debug(dhcp_client, "restart DHCP timeout");
1796 if (dhcp_client->state == REBOOTING) {
1797 g_free(dhcp_client->last_address);
1798 dhcp_client->last_address = NULL;
1799 restart_dhcp(dhcp_client, 0);
1801 dhcp_client->ack_retry_times++;
1802 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1807 static char *get_ip(uint32_t ip)
1809 struct in_addr addr;
1813 return g_strdup(inet_ntoa(addr));
1816 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1824 if (type == OPTION_STRING)
1825 return g_list_append(list, g_strdup(value));
1827 while ((pos = strchr(pos, ' '))) {
1830 list = g_list_append(list, g_strdup(value));
1835 list = g_list_append(list, g_strdup(value));
1840 static inline uint32_t get_uint32(unsigned char *value)
1842 return value[0] << 24 | value[1] << 16 |
1843 value[2] << 8 | value[3];
1846 static inline uint16_t get_uint16(unsigned char *value)
1848 return value[0] << 8 | value[1];
1851 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1852 struct in6_addr *addr,
1853 unsigned char prefixlen, uint32_t preferred,
1856 GDHCPIAPrefix *ia_prefix;
1858 ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1862 if (dhcp_client->debug_func) {
1863 char addr_str[INET6_ADDRSTRLEN + 1];
1864 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1865 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1866 addr_str, prefixlen, preferred, valid);
1869 memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1870 ia_prefix->prefixlen = prefixlen;
1871 ia_prefix->preferred = preferred;
1872 ia_prefix->valid = valid;
1873 ia_prefix->expire = time(NULL) + valid;
1875 return g_list_prepend(list, ia_prefix);
1878 static GList *get_addresses(GDHCPClient *dhcp_client,
1880 unsigned char *value,
1884 struct in6_addr addr;
1885 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1886 uint16_t option_len, option_code, st = 0, max_len;
1887 int addr_count = 0, prefix_count = 0, i, pos;
1888 unsigned char prefixlen;
1889 unsigned int shortest_valid = 0;
1893 if (!value || len < 4)
1896 iaid = get_uint32(&value[0]);
1897 if (dhcp_client->iaid != iaid)
1900 if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
1901 T1 = get_uint32(&value[4]);
1902 T2 = get_uint32(&value[8]);
1905 /* IA_NA: RFC 3315, 22.4 */
1906 /* IA_PD: RFC 3633, ch 9 */
1916 max_len = len - pos;
1918 debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
1920 /* We have more sub-options in this packet. */
1922 option = dhcpv6_get_sub_option(&value[pos], max_len,
1923 &option_code, &option_len);
1925 debug(dhcp_client, "pos %d option %p code %d len %d",
1926 pos, option, option_code, option_len);
1934 switch (option_code) {
1935 case G_DHCPV6_IAADDR:
1937 memcpy(&addr, &option[0], sizeof(addr));
1939 preferred = get_uint32(&option[i]);
1941 valid = get_uint32(&option[i]);
1946 case G_DHCPV6_STATUS_CODE:
1947 st = get_uint16(&option[0]);
1948 debug(dhcp_client, "error code %d", st);
1949 if (option_len > 2) {
1950 str = g_strndup((gchar *)&option[2],
1952 debug(dhcp_client, "error text: %s", str);
1959 case G_DHCPV6_IA_PREFIX:
1961 preferred = get_uint32(&option[i]);
1963 valid = get_uint32(&option[i]);
1965 prefixlen = option[i];
1967 memcpy(&addr, &option[i], sizeof(addr));
1969 if (preferred < valid) {
1970 /* RFC 3633, ch 10 */
1971 list = add_prefix(dhcp_client, list, &addr,
1972 prefixlen, preferred, valid);
1973 if (shortest_valid > valid)
1974 shortest_valid = valid;
1980 pos += 2 + 2 + option_len;
1982 } while (pos < len);
1984 if (addr_count > 0 && st == 0) {
1985 /* We only support one address atm */
1986 char addr_str[INET6_ADDRSTRLEN + 1];
1988 if (preferred > valid)
1989 /* RFC 3315, 22.6 */
1992 dhcp_client->T1 = T1;
1993 dhcp_client->T2 = T2;
1995 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
1996 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
1997 addr_count, addr_str, T1, T2);
1999 list = g_list_append(list, g_strdup(addr_str));
2001 if (code == G_DHCPV6_IA_NA)
2002 memcpy(&dhcp_client->ia_na, &addr,
2003 sizeof(struct in6_addr));
2005 memcpy(&dhcp_client->ia_ta, &addr,
2006 sizeof(struct in6_addr));
2008 if (valid != dhcp_client->expire)
2009 dhcp_client->expire = valid;
2012 if (prefix_count > 0 && list) {
2014 * This means we have a list of prefixes to delegate.
2016 list = g_list_reverse(list);
2018 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2019 prefix_count, T1, T2);
2021 dhcp_client->T1 = T1;
2022 dhcp_client->T2 = T2;
2024 dhcp_client->expire = shortest_valid;
2027 if (status && *status != 0)
2028 debug(dhcp_client, "status %d", *status);
2033 static GList *get_domains(int maxlen, unsigned char *value)
2039 char dns_name[NS_MAXDNAME + 1];
2041 if (!value || maxlen < 3)
2044 while (pos < maxlen) {
2045 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2047 c = (unsigned char *)dns_name;
2054 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2055 pos += (char *)c - dns_name + 1;
2058 return g_list_reverse(list);
2061 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2063 unsigned char *value,
2074 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
2075 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
2078 "%s server list length (%d) is invalid",
2079 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2083 for (i = 0; i < len; i += 16) {
2085 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2089 if (!inet_ntop(AF_INET6, &value[i], str,
2093 list = g_list_append(list, str);
2097 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
2098 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
2099 case G_DHCPV6_IA_PD: /* RFC 3633, chapter 9 */
2100 list = get_addresses(dhcp_client, code, len, value, status);
2103 case G_DHCPV6_DOMAIN_LIST:
2104 list = get_domains(len, value);
2114 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2115 struct dhcpv6_packet *packet,
2116 uint16_t pkt_len, uint16_t *status)
2118 GList *list, *value_list;
2121 uint16_t option_len;
2123 for (list = dhcp_client->request_list; list; list = list->next) {
2124 code = (uint16_t) GPOINTER_TO_INT(list->data);
2126 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2129 g_hash_table_remove(dhcp_client->code_value_hash,
2130 GINT_TO_POINTER((int) code));
2134 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2135 option_len, option, status);
2137 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2138 option_len, value_list);
2141 g_hash_table_remove(dhcp_client->code_value_hash,
2142 GINT_TO_POINTER((int) code));
2144 g_hash_table_insert(dhcp_client->code_value_hash,
2145 GINT_TO_POINTER((int) code), value_list);
2149 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
2151 GDHCPOptionType type;
2152 GList *list, *value_list;
2157 for (list = dhcp_client->request_list; list; list = list->next) {
2158 code = (uint8_t) GPOINTER_TO_INT(list->data);
2160 option = dhcp_get_option(packet, code);
2162 g_hash_table_remove(dhcp_client->code_value_hash,
2163 GINT_TO_POINTER((int) code));
2167 type = dhcp_get_code_type(code);
2169 option_value = malloc_option_value_string(option, type);
2171 g_hash_table_remove(dhcp_client->code_value_hash,
2172 GINT_TO_POINTER((int) code));
2174 value_list = get_option_value_list(option_value, type);
2176 g_free(option_value);
2179 g_hash_table_remove(dhcp_client->code_value_hash,
2180 GINT_TO_POINTER((int) code));
2182 g_hash_table_insert(dhcp_client->code_value_hash,
2183 GINT_TO_POINTER((int) code), value_list);
2187 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2190 GDHCPClient *dhcp_client = user_data;
2191 struct sockaddr_in dst_addr = { 0 };
2192 struct dhcp_packet packet;
2193 struct dhcpv6_packet *packet6 = NULL;
2194 uint8_t *message_type = NULL, *client_id = NULL, *option,
2196 uint16_t option_len = 0, status = 0;
2199 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2200 uint16_t pkt_len = 0;
2204 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2205 dhcp_client->listener_watch = 0;
2209 if (dhcp_client->listen_mode == L_NONE)
2214 dhcp_client->status_code = 0;
2216 if (dhcp_client->listen_mode == L2) {
2217 re = dhcp_recv_l2_packet(&packet,
2218 dhcp_client->listener_sockfd,
2221 } else if (dhcp_client->listen_mode == L3) {
2222 if (dhcp_client->type == G_DHCP_IPV6) {
2223 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2224 dhcp_client->listener_sockfd);
2227 xid = packet6->transaction_id[0] << 16 |
2228 packet6->transaction_id[1] << 8 |
2229 packet6->transaction_id[2];
2231 re = dhcp_recv_l3_packet(&packet,
2232 dhcp_client->listener_sockfd);
2235 } else if (dhcp_client->listen_mode == L_ARP) {
2236 ipv4ll_recv_arp_packet(dhcp_client);
2244 if (!check_package_owner(dhcp_client, pkt))
2247 if (dhcp_client->type == G_DHCP_IPV6) {
2252 client_id = dhcpv6_get_option(packet6, pkt_len,
2253 G_DHCPV6_CLIENTID, &option_len, &count);
2255 if (!client_id || count == 0 || option_len == 0 ||
2256 memcmp(dhcp_client->duid, client_id,
2257 dhcp_client->duid_len) != 0) {
2259 "client duid error, discarding msg %p/%d/%d",
2260 client_id, option_len, count);
2264 option = dhcpv6_get_option(packet6, pkt_len,
2265 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2266 if (option != 0 && option_len > 0) {
2267 status = option[0]<<8 | option[1];
2269 debug(dhcp_client, "error code %d", status);
2270 if (option_len > 2) {
2271 gchar *txt = g_strndup(
2272 (gchar *)&option[2],
2274 debug(dhcp_client, "error text: %s",
2279 dhcp_client->status_code = status;
2282 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
2287 if (!message_type && !client_id)
2288 /* No message type / client id option, ignore package */
2291 debug(dhcp_client, "received DHCP packet xid 0x%04x "
2292 "(current state %d)", ntohl(xid), dhcp_client->state);
2294 switch (dhcp_client->state) {
2295 case INIT_SELECTING:
2296 if (*message_type != DHCPOFFER)
2299 remove_timeouts(dhcp_client);
2300 dhcp_client->timeout = 0;
2301 dhcp_client->retry_times = 0;
2303 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
2304 dhcp_client->server_ip = get_be32(option);
2305 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2307 dhcp_client->state = REQUESTING;
2309 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2310 dhcp_client->request_bcast = true;
2312 dhcp_client->request_bcast = false;
2314 debug(dhcp_client, "init ip %s -> %sadding broadcast flag",
2315 inet_ntoa(dst_addr.sin_addr),
2316 dhcp_client->request_bcast ? "" : "not ");
2318 start_request(dhcp_client);
2322 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2323 dhcp_client->request_bcast = true;
2325 dhcp_client->request_bcast = false;
2327 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2328 inet_ntoa(dst_addr.sin_addr),
2329 dhcp_client->request_bcast ? "" : "not ");
2334 if (*message_type == DHCPACK) {
2335 dhcp_client->retry_times = 0;
2337 remove_timeouts(dhcp_client);
2339 dhcp_client->lease_seconds = get_lease(&packet);
2341 get_request(dhcp_client, &packet);
2343 switch_listening_mode(dhcp_client, L_NONE);
2345 g_free(dhcp_client->assigned_ip);
2346 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2348 if (dhcp_client->state == REBOOTING) {
2349 option = dhcp_get_option(&packet,
2351 dhcp_client->server_ip = get_be32(option);
2354 /* Address should be set up here */
2355 if (dhcp_client->lease_available_cb)
2356 dhcp_client->lease_available_cb(dhcp_client,
2357 dhcp_client->lease_available_data);
2359 start_bound(dhcp_client);
2360 } else if (*message_type == DHCPNAK) {
2361 dhcp_client->retry_times = 0;
2363 remove_timeouts(dhcp_client);
2365 dhcp_client->timeout = g_timeout_add_seconds_full(
2367 restart_dhcp_timeout,
2374 if (dhcp_client->type != G_DHCP_IPV6)
2377 if (packet6->message != DHCPV6_REPLY &&
2378 packet6->message != DHCPV6_ADVERTISE)
2382 server_id = dhcpv6_get_option(packet6, pkt_len,
2383 G_DHCPV6_SERVERID, &option_len, &count);
2384 if (!server_id || count != 1 || option_len == 0) {
2385 /* RFC 3315, 15.10 */
2387 "server duid error, discarding msg %p/%d/%d",
2388 server_id, option_len, count);
2391 dhcp_client->server_duid = g_try_malloc(option_len);
2392 if (!dhcp_client->server_duid)
2394 memcpy(dhcp_client->server_duid, server_id, option_len);
2395 dhcp_client->server_duid_len = option_len;
2397 if (packet6->message == DHCPV6_REPLY) {
2398 uint8_t *rapid_commit;
2401 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2402 G_DHCPV6_RAPID_COMMIT,
2403 &option_len, &count);
2404 if (!rapid_commit || option_len != 0 ||
2406 /* RFC 3315, 17.1.4 */
2410 switch_listening_mode(dhcp_client, L_NONE);
2412 if (dhcp_client->status_code == 0)
2413 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2414 &dhcp_client->status_code);
2416 if (packet6->message == DHCPV6_ADVERTISE) {
2417 if (dhcp_client->advertise_cb)
2418 dhcp_client->advertise_cb(dhcp_client,
2419 dhcp_client->advertise_data);
2423 if (dhcp_client->solicitation_cb) {
2425 * The dhcp_client might not be valid after the
2426 * callback call so just return immediately.
2428 dhcp_client->solicitation_cb(dhcp_client,
2429 dhcp_client->solicitation_data);
2434 if (dhcp_client->type != G_DHCP_IPV6)
2437 server_id = dhcpv6_get_option(packet6, pkt_len,
2438 G_DHCPV6_SERVERID, &option_len, &count);
2439 if (!dhcp_client->server_duid && server_id &&
2442 * If we do not have server duid yet, then get it now.
2443 * Prefix delegation renew support needs it.
2445 dhcp_client->server_duid = g_try_malloc(option_len);
2446 if (!dhcp_client->server_duid)
2448 memcpy(dhcp_client->server_duid, server_id, option_len);
2449 dhcp_client->server_duid_len = option_len;
2452 case INFORMATION_REQ:
2458 if (dhcp_client->type != G_DHCP_IPV6)
2461 if (packet6->message != DHCPV6_REPLY)
2466 server_id = dhcpv6_get_option(packet6, pkt_len,
2467 G_DHCPV6_SERVERID, &option_len, &count);
2468 if (!server_id || count != 1 || option_len == 0 ||
2469 (dhcp_client->server_duid_len > 0 &&
2470 memcmp(dhcp_client->server_duid, server_id,
2471 dhcp_client->server_duid_len) != 0)) {
2472 /* RFC 3315, 15.10 */
2474 "server duid error, discarding msg %p/%d/%d",
2475 server_id, option_len, count);
2479 switch_listening_mode(dhcp_client, L_NONE);
2481 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2482 &dhcp_client->status_code);
2484 if (dhcp_client->information_req_cb) {
2486 * The dhcp_client might not be valid after the
2487 * callback call so just return immediately.
2489 dhcp_client->information_req_cb(dhcp_client,
2490 dhcp_client->information_req_data);
2493 if (dhcp_client->request_cb) {
2494 dhcp_client->request_cb(dhcp_client,
2495 dhcp_client->request_data);
2498 if (dhcp_client->renew_cb) {
2499 dhcp_client->renew_cb(dhcp_client,
2500 dhcp_client->renew_data);
2503 if (dhcp_client->rebind_cb) {
2504 dhcp_client->rebind_cb(dhcp_client,
2505 dhcp_client->rebind_data);
2508 if (dhcp_client->release_cb) {
2509 dhcp_client->release_cb(dhcp_client,
2510 dhcp_client->release_data);
2513 if (dhcp_client->decline_cb) {
2514 dhcp_client->decline_cb(dhcp_client,
2515 dhcp_client->decline_data);
2518 if (dhcp_client->confirm_cb) {
2520 server_id = dhcpv6_get_option(packet6, pkt_len,
2521 G_DHCPV6_SERVERID, &option_len,
2523 if (!server_id || count != 1 ||
2525 /* RFC 3315, 15.10 */
2527 "confirm server duid error, "
2528 "discarding msg %p/%d/%d",
2529 server_id, option_len, count);
2532 dhcp_client->server_duid = g_try_malloc(option_len);
2533 if (!dhcp_client->server_duid)
2535 memcpy(dhcp_client->server_duid, server_id, option_len);
2536 dhcp_client->server_duid_len = option_len;
2538 dhcp_client->confirm_cb(dhcp_client,
2539 dhcp_client->confirm_data);
2547 debug(dhcp_client, "processed DHCP packet (new state %d)",
2548 dhcp_client->state);
2553 static gboolean discover_timeout(gpointer user_data)
2555 GDHCPClient *dhcp_client = user_data;
2557 dhcp_client->retry_times++;
2560 * We do not send the REQUESTED IP option if we are retrying because
2561 * if the server is non-authoritative it will ignore the request if the
2562 * option is present.
2564 g_dhcp_client_start(dhcp_client, NULL);
2569 static gboolean reboot_timeout(gpointer user_data)
2571 GDHCPClient *dhcp_client = user_data;
2572 dhcp_client->retry_times = 0;
2573 dhcp_client->requested_ip = 0;
2574 dhcp_client->state = INIT_SELECTING;
2576 * We do not send the REQUESTED IP option because the server didn't
2577 * respond when we send DHCPREQUEST with the REQUESTED IP option in
2580 g_dhcp_client_start(dhcp_client, NULL);
2585 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2587 GDHCPClient *dhcp_client = dhcp_data;
2589 debug(dhcp_client, "back to MONITOR mode");
2591 dhcp_client->conflicts = 0;
2592 dhcp_client->state = IPV4LL_MONITOR;
2597 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2599 GDHCPClient *dhcp_client = dhcp_data;
2602 #if defined TIZEN_EXT
2607 debug(dhcp_client, "request timeout (retries %d)",
2608 dhcp_client->retry_times);
2610 if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2611 dhcp_client->retry_times++;
2612 send_announce_packet(dhcp_client);
2616 ip = htonl(dhcp_client->requested_ip);
2617 debug(dhcp_client, "switching to monitor mode");
2618 dhcp_client->state = IPV4LL_MONITOR;
2619 dhcp_client->assigned_ip = get_ip(ip);
2621 if (dhcp_client->ipv4ll_available_cb)
2622 dhcp_client->ipv4ll_available_cb(dhcp_client,
2623 dhcp_client->ipv4ll_available_data);
2624 dhcp_client->conflicts = 0;
2629 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2632 GDHCPClient *dhcp_client = dhcp_data;
2634 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2635 dhcp_client->retry_times);
2637 if (dhcp_client->retry_times == PROBE_NUM) {
2638 dhcp_client->state = IPV4LL_ANNOUNCE;
2639 dhcp_client->retry_times = 0;
2641 dhcp_client->retry_times++;
2642 send_announce_packet(dhcp_client);
2645 dhcp_client->retry_times++;
2646 send_probe_packet(dhcp_client);
2651 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2657 if (dhcp_client->type == G_DHCP_IPV6) {
2658 if (dhcp_client->information_req_cb) {
2659 dhcp_client->state = INFORMATION_REQ;
2660 re = switch_listening_mode(dhcp_client, L3);
2662 switch_listening_mode(dhcp_client, L_NONE);
2663 dhcp_client->state = 0;
2666 send_information_req(dhcp_client);
2668 } else if (dhcp_client->solicitation_cb) {
2669 dhcp_client->state = SOLICITATION;
2670 re = switch_listening_mode(dhcp_client, L3);
2672 switch_listening_mode(dhcp_client, L_NONE);
2673 dhcp_client->state = 0;
2676 send_solicitation(dhcp_client);
2678 } else if (dhcp_client->request_cb) {
2679 dhcp_client->state = REQUEST;
2680 re = switch_listening_mode(dhcp_client, L3);
2682 switch_listening_mode(dhcp_client, L_NONE);
2683 dhcp_client->state = 0;
2686 send_dhcpv6_request(dhcp_client);
2688 } else if (dhcp_client->confirm_cb) {
2689 dhcp_client->state = CONFIRM;
2690 re = switch_listening_mode(dhcp_client, L3);
2692 switch_listening_mode(dhcp_client, L_NONE);
2693 dhcp_client->state = 0;
2696 send_dhcpv6_confirm(dhcp_client);
2698 } else if (dhcp_client->renew_cb) {
2699 dhcp_client->state = RENEW;
2700 re = switch_listening_mode(dhcp_client, L3);
2702 switch_listening_mode(dhcp_client, L_NONE);
2703 dhcp_client->state = 0;
2706 send_dhcpv6_renew(dhcp_client);
2708 } else if (dhcp_client->rebind_cb) {
2709 dhcp_client->state = REBIND;
2710 re = switch_listening_mode(dhcp_client, L3);
2712 switch_listening_mode(dhcp_client, L_NONE);
2713 dhcp_client->state = 0;
2716 send_dhcpv6_rebind(dhcp_client);
2718 } else if (dhcp_client->release_cb) {
2719 dhcp_client->state = RENEW;
2720 re = switch_listening_mode(dhcp_client, L3);
2722 switch_listening_mode(dhcp_client, L_NONE);
2723 dhcp_client->state = 0;
2726 send_dhcpv6_release(dhcp_client);
2727 } else if (dhcp_client->decline_cb) {
2728 dhcp_client->state = DECLINE;
2729 re = switch_listening_mode(dhcp_client, L3);
2731 switch_listening_mode(dhcp_client, L_NONE);
2732 dhcp_client->state = 0;
2735 send_dhcpv6_decline(dhcp_client);
2741 if (dhcp_client->type == G_DHCP_IPV4LL) {
2742 dhcp_client->state = INIT_SELECTING;
2743 ipv4ll_start(dhcp_client);
2747 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2748 if (dhcp_client->no_lease_cb)
2749 dhcp_client->no_lease_cb(dhcp_client,
2750 dhcp_client->no_lease_data);
2751 dhcp_client->retry_times = 0;
2755 if (dhcp_client->retry_times == 0) {
2756 g_free(dhcp_client->assigned_ip);
2757 dhcp_client->assigned_ip = NULL;
2759 dhcp_client->state = INIT_SELECTING;
2760 re = switch_listening_mode(dhcp_client, L2);
2764 dhcp_get_random(&rand);
2765 dhcp_client->xid = rand;
2766 dhcp_client->start = time(NULL);
2769 if (!last_address) {
2772 addr = ntohl(inet_addr(last_address));
2773 if (addr == 0xFFFFFFFF) {
2776 g_free(dhcp_client->last_address);
2777 dhcp_client->last_address = g_strdup(last_address);
2781 if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2782 debug(dhcp_client, "DHCP client start with state init_reboot");
2783 dhcp_client->requested_ip = addr;
2784 dhcp_client->state = REBOOTING;
2785 send_request(dhcp_client);
2787 dhcp_client->timeout = g_timeout_add_seconds_full(
2795 send_discover(dhcp_client, addr);
2797 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2805 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2807 switch_listening_mode(dhcp_client, L_NONE);
2809 if (dhcp_client->state == BOUND ||
2810 dhcp_client->state == RENEWING ||
2811 dhcp_client->state == REBINDING)
2812 send_release(dhcp_client, dhcp_client->server_ip,
2813 dhcp_client->requested_ip);
2815 remove_timeouts(dhcp_client);
2817 if (dhcp_client->listener_watch > 0) {
2818 g_source_remove(dhcp_client->listener_watch);
2819 dhcp_client->listener_watch = 0;
2822 dhcp_client->retry_times = 0;
2823 dhcp_client->ack_retry_times = 0;
2825 dhcp_client->requested_ip = 0;
2826 dhcp_client->state = RELEASED;
2827 dhcp_client->lease_seconds = 0;
2828 dhcp_client->request_bcast = false;
2831 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2832 unsigned char option_code)
2834 return g_hash_table_lookup(dhcp_client->code_value_hash,
2835 GINT_TO_POINTER((int) option_code));
2838 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2839 GDHCPClientEvent event,
2840 GDHCPClientEventFunc func,
2844 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2845 dhcp_client->lease_available_cb = func;
2846 dhcp_client->lease_available_data = data;
2848 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2849 if (dhcp_client->type == G_DHCP_IPV6)
2851 dhcp_client->ipv4ll_available_cb = func;
2852 dhcp_client->ipv4ll_available_data = data;
2854 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2855 dhcp_client->no_lease_cb = func;
2856 dhcp_client->no_lease_data = data;
2858 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2859 dhcp_client->lease_lost_cb = func;
2860 dhcp_client->lease_lost_data = data;
2862 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2863 if (dhcp_client->type == G_DHCP_IPV6)
2865 dhcp_client->ipv4ll_lost_cb = func;
2866 dhcp_client->ipv4ll_lost_data = data;
2868 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2869 dhcp_client->address_conflict_cb = func;
2870 dhcp_client->address_conflict_data = data;
2872 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2873 if (dhcp_client->type != G_DHCP_IPV6)
2875 dhcp_client->information_req_cb = func;
2876 dhcp_client->information_req_data = data;
2878 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2879 if (dhcp_client->type != G_DHCP_IPV6)
2881 dhcp_client->solicitation_cb = func;
2882 dhcp_client->solicitation_data = data;
2884 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2885 if (dhcp_client->type != G_DHCP_IPV6)
2887 dhcp_client->advertise_cb = func;
2888 dhcp_client->advertise_data = data;
2890 case G_DHCP_CLIENT_EVENT_REQUEST:
2891 if (dhcp_client->type != G_DHCP_IPV6)
2893 dhcp_client->request_cb = func;
2894 dhcp_client->request_data = data;
2896 case G_DHCP_CLIENT_EVENT_RENEW:
2897 if (dhcp_client->type != G_DHCP_IPV6)
2899 dhcp_client->renew_cb = func;
2900 dhcp_client->renew_data = data;
2902 case G_DHCP_CLIENT_EVENT_REBIND:
2903 if (dhcp_client->type != G_DHCP_IPV6)
2905 dhcp_client->rebind_cb = func;
2906 dhcp_client->rebind_data = data;
2908 case G_DHCP_CLIENT_EVENT_RELEASE:
2909 if (dhcp_client->type != G_DHCP_IPV6)
2911 dhcp_client->release_cb = func;
2912 dhcp_client->release_data = data;
2914 case G_DHCP_CLIENT_EVENT_CONFIRM:
2915 if (dhcp_client->type != G_DHCP_IPV6)
2917 dhcp_client->confirm_cb = func;
2918 dhcp_client->confirm_data = data;
2920 case G_DHCP_CLIENT_EVENT_DECLINE:
2921 if (dhcp_client->type != G_DHCP_IPV6)
2923 dhcp_client->decline_cb = func;
2924 dhcp_client->decline_data = data;
2929 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2931 return dhcp_client->ifindex;
2934 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
2939 return get_ip(dhcp_client->server_ip);
2942 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2944 return g_strdup(dhcp_client->assigned_ip);
2947 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2949 GList *option = NULL;
2951 if (dhcp_client->type == G_DHCP_IPV6)
2954 switch (dhcp_client->state) {
2956 case IPV4LL_MONITOR:
2957 return g_strdup("255.255.0.0");
2961 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2963 return g_strdup(option->data);
2964 case INIT_SELECTING:
2969 case IPV4LL_ANNOUNCE:
2970 case INFORMATION_REQ:
2983 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2984 unsigned int option_code)
2986 if (!g_list_find(dhcp_client->request_list,
2987 GINT_TO_POINTER((int)option_code)))
2988 dhcp_client->request_list = g_list_prepend(
2989 dhcp_client->request_list,
2990 (GINT_TO_POINTER((int) option_code)));
2992 return G_DHCP_CLIENT_ERROR_NONE;
2995 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2997 g_list_free(dhcp_client->request_list);
2998 dhcp_client->request_list = NULL;
3001 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3003 g_hash_table_remove_all(dhcp_client->send_value_hash);
3006 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3010 storage = g_try_malloc(size + OPT_DATA);
3014 storage[OPT_CODE] = code;
3015 storage[OPT_LEN] = size;
3016 memcpy(&storage[OPT_DATA], data, size);
3021 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3024 return alloc_dhcp_option(code, data, MIN(size, 255));
3027 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3029 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3032 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3034 const unsigned maclen = 6;
3035 const unsigned idlen = maclen + 1;
3036 const uint8_t option_code = G_DHCP_CLIENT_ID;
3037 uint8_t idbuf[idlen];
3038 uint8_t *data_option;
3040 idbuf[0] = ARPHRD_ETHER;
3042 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3044 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3046 return G_DHCP_CLIENT_ERROR_NOMEM;
3048 g_hash_table_insert(dhcp_client->send_value_hash,
3049 GINT_TO_POINTER((int) option_code), data_option);
3051 return G_DHCP_CLIENT_ERROR_NONE;
3054 /* Now only support send hostname */
3055 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3056 unsigned char option_code, const char *option_value)
3058 uint8_t *binary_option;
3060 if (option_code == G_DHCP_HOST_NAME && option_value) {
3061 binary_option = alloc_dhcp_string_option(option_code,
3064 return G_DHCP_CLIENT_ERROR_NOMEM;
3066 g_hash_table_insert(dhcp_client->send_value_hash,
3067 GINT_TO_POINTER((int) option_code), binary_option);
3070 return G_DHCP_CLIENT_ERROR_NONE;
3073 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3078 storage = g_malloc(2 + 2 + len);
3082 storage[0] = code >> 8;
3083 storage[1] = code & 0xff;
3084 storage[2] = len >> 8;
3085 storage[3] = len & 0xff;
3086 memcpy(storage + 2 + 2, option, len);
3091 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3093 return g_hash_table_remove(dhcp_client->send_value_hash,
3094 GINT_TO_POINTER((int)code));
3097 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3098 uint16_t option_code,
3099 uint8_t *option_value,
3100 uint16_t option_len)
3103 uint8_t *binary_option;
3105 debug(dhcp_client, "setting option %d to %p len %d",
3106 option_code, option_value, option_len);
3108 binary_option = alloc_dhcpv6_option(option_code, option_value,
3111 g_hash_table_insert(dhcp_client->send_value_hash,
3112 GINT_TO_POINTER((int) option_code),
3117 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3119 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3122 dhcp_client->last_request = time(NULL);
3125 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3127 if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3130 return dhcp_client->status_code;
3133 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3138 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3143 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3148 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3151 g_dhcp_client_stop(dhcp_client);
3153 g_free(dhcp_client->interface);
3154 g_free(dhcp_client->assigned_ip);
3155 g_free(dhcp_client->last_address);
3156 g_free(dhcp_client->duid);
3157 g_free(dhcp_client->server_duid);
3159 g_list_free(dhcp_client->request_list);
3160 g_list_free(dhcp_client->require_list);
3162 g_hash_table_destroy(dhcp_client->code_value_hash);
3163 g_hash_table_destroy(dhcp_client->send_value_hash);
3165 g_free(dhcp_client);
3166 #if defined TIZEN_EXT
3171 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3172 GDHCPDebugFunc func, gpointer user_data)
3177 dhcp_client->debug_func = func;
3178 dhcp_client->debug_data = user_data;
3181 static GDHCPIAPrefix *copy_prefix(gpointer data)
3183 GDHCPIAPrefix *copy, *prefix = data;
3185 copy = g_try_new(GDHCPIAPrefix, 1);
3189 memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3194 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3196 GSList *copy = NULL;
3199 for (list = prefixes; list; list = list->next)
3200 copy = g_slist_prepend(copy, copy_prefix(list->data));