3 * DHCP client library with GLib integration
5 * Copyright (C) 2009-2011 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>
35 #include <netpacket/packet.h>
36 #include <netinet/if_ether.h>
37 #include <net/ethernet.h>
40 #include <linux/filter.h>
48 #define DISCOVER_TIMEOUT 3
49 #define DISCOVER_RETRIES 10
51 #define REQUEST_TIMEOUT 3
52 #define REQUEST_RETRIES 5
54 typedef enum _listen_mode {
61 typedef enum _dhcp_client_state {
84 uint8_t mac_address[6];
87 uint32_t requested_ip;
89 uint32_t lease_seconds;
90 ListenMode listen_mode;
93 uint8_t ack_retry_times;
97 GIOChannel *listener_channel;
100 GHashTable *code_value_hash;
101 GHashTable *send_value_hash;
102 GDHCPClientEventFunc lease_available_cb;
103 gpointer lease_available_data;
104 GDHCPClientEventFunc ipv4ll_available_cb;
105 gpointer ipv4ll_available_data;
106 GDHCPClientEventFunc no_lease_cb;
107 gpointer no_lease_data;
108 GDHCPClientEventFunc lease_lost_cb;
109 gpointer lease_lost_data;
110 GDHCPClientEventFunc ipv4ll_lost_cb;
111 gpointer ipv4ll_lost_data;
112 GDHCPClientEventFunc address_conflict_cb;
113 gpointer address_conflict_data;
114 GDHCPDebugFunc debug_func;
116 GDHCPClientEventFunc information_req_cb;
117 gpointer information_req_data;
118 GDHCPClientEventFunc solicitation_cb;
119 gpointer solicitation_data;
120 GDHCPClientEventFunc advertise_cb;
121 gpointer advertise_data;
122 GDHCPClientEventFunc request_cb;
123 gpointer request_data;
124 GDHCPClientEventFunc renew_cb;
129 unsigned char *server_duid;
131 uint16_t status_code;
134 struct in6_addr ia_na;
135 struct in6_addr ia_ta;
139 static inline void debug(GDHCPClient *client, const char *format, ...)
144 if (client->debug_func == NULL)
147 va_start(ap, format);
149 if (vsnprintf(str, sizeof(str), format, ap) > 0)
150 client->debug_func(str, client->debug_data);
155 /* Initialize the packet with the proper defaults */
156 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
158 if (dhcp_client->type == G_DHCP_IPV6)
159 dhcpv6_init_header(pkt, type);
161 struct dhcp_packet *packet = pkt;
163 dhcp_init_header(packet, type);
164 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
168 static void add_request_options(GDHCPClient *dhcp_client,
169 struct dhcp_packet *packet)
174 int end = dhcp_end_option(packet->options);
176 for (list = dhcp_client->request_list; list; list = list->next) {
177 code = (uint8_t) GPOINTER_TO_INT(list->data);
179 packet->options[end + OPT_DATA + len] = code;
184 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
185 packet->options[end + OPT_LEN] = len;
186 packet->options[end + OPT_DATA + len] = DHCP_END;
193 unsigned char **ptr_buf;
196 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
199 uint8_t *option = value;
201 struct hash_params *params = user_data;
203 /* option[0][1] contains option code */
204 len = option[2] << 8 | option[3];
206 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
209 memcpy(*params->ptr_buf, option, len + 2 + 2);
210 (*params->ptr_buf) += len + 2 + 2;
213 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
214 unsigned char *buf, int max_buf,
215 unsigned char **ptr_buf)
217 struct hash_params params = {
223 if (dhcp_client->type == G_DHCP_IPV4)
226 g_hash_table_foreach(dhcp_client->send_value_hash,
227 add_dhcpv6_binary_option, ¶ms);
229 *ptr_buf = *params.ptr_buf;
232 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
236 buf[1] = code & 0xff;
239 if (len > 0 && msg != NULL)
240 memcpy(&buf[4], msg, len);
243 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
244 struct dhcpv6_packet *packet,
245 unsigned char *buf, int max_buf,
246 unsigned char **ptr_buf)
252 if (dhcp_client->type == G_DHCP_IPV4)
255 for (list = dhcp_client->request_list; list; list = list->next) {
256 code = (uint16_t) GPOINTER_TO_INT(list->data);
259 case G_DHCPV6_CLIENTID:
260 if (dhcp_client->duid == NULL)
263 len = 2 + 2 + dhcp_client->duid_len;
264 if ((*ptr_buf + len) > (buf + max_buf)) {
265 debug(dhcp_client, "Too long dhcpv6 message "
266 "when writing client id option");
270 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
271 dhcp_client->duid_len, dhcp_client->duid);
275 case G_DHCPV6_SERVERID:
276 if (dhcp_client->server_duid == NULL)
279 len = 2 + 2 + dhcp_client->server_duid_len;
280 if ((*ptr_buf + len) > (buf + max_buf)) {
281 debug(dhcp_client, "Too long dhcpv6 message "
282 "when writing server id option");
286 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
287 dhcp_client->server_duid_len,
288 dhcp_client->server_duid);
292 case G_DHCPV6_RAPID_COMMIT:
294 if ((*ptr_buf + len) > (buf + max_buf)) {
295 debug(dhcp_client, "Too long dhcpv6 message "
296 "when writing rapid commit option");
300 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
307 case G_DHCPV6_DNS_SERVERS:
310 case G_DHCPV6_SNTP_SERVERS:
319 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
321 uint8_t *option = value;
322 struct dhcp_packet *packet = user_data;
324 dhcp_add_binary_option(packet, option);
327 static void add_send_options(GDHCPClient *dhcp_client,
328 struct dhcp_packet *packet)
330 g_hash_table_foreach(dhcp_client->send_value_hash,
331 add_binary_option, packet);
334 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
336 struct dhcp_packet packet;
338 debug(dhcp_client, "sending DHCP discover request");
340 init_packet(dhcp_client, &packet, DHCPDISCOVER);
342 packet.xid = dhcp_client->xid;
345 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
347 /* Explicitly saying that we want RFC-compliant packets helps
348 * some buggy DHCP servers to NOT send bigger packets */
349 dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
351 add_request_options(dhcp_client, &packet);
353 add_send_options(dhcp_client, &packet);
355 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
356 INADDR_BROADCAST, SERVER_PORT,
357 MAC_BCAST_ADDR, dhcp_client->ifindex);
360 static int send_select(GDHCPClient *dhcp_client)
362 struct dhcp_packet packet;
364 debug(dhcp_client, "sending DHCP select request");
366 init_packet(dhcp_client, &packet, DHCPREQUEST);
368 packet.xid = dhcp_client->xid;
370 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
371 dhcp_client->requested_ip);
372 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
374 add_request_options(dhcp_client, &packet);
376 add_send_options(dhcp_client, &packet);
378 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
379 INADDR_BROADCAST, SERVER_PORT,
380 MAC_BCAST_ADDR, dhcp_client->ifindex);
383 static int send_renew(GDHCPClient *dhcp_client)
385 struct dhcp_packet packet;
387 debug(dhcp_client, "sending DHCP renew request");
389 init_packet(dhcp_client , &packet, DHCPREQUEST);
390 packet.xid = dhcp_client->xid;
391 packet.ciaddr = dhcp_client->requested_ip;
393 add_request_options(dhcp_client, &packet);
395 add_send_options(dhcp_client, &packet);
397 return dhcp_send_kernel_packet(&packet,
398 dhcp_client->requested_ip, CLIENT_PORT,
399 dhcp_client->server_ip, SERVER_PORT);
402 static int send_rebound(GDHCPClient *dhcp_client)
404 struct dhcp_packet packet;
406 debug(dhcp_client, "sending DHCP rebound request");
408 init_packet(dhcp_client , &packet, DHCPREQUEST);
409 packet.xid = dhcp_client->xid;
410 packet.ciaddr = dhcp_client->requested_ip;
412 add_request_options(dhcp_client, &packet);
414 add_send_options(dhcp_client, &packet);
416 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
417 INADDR_BROADCAST, SERVER_PORT,
418 MAC_BCAST_ADDR, dhcp_client->ifindex);
421 static int send_release(GDHCPClient *dhcp_client,
422 uint32_t server, uint32_t ciaddr)
424 struct dhcp_packet packet;
426 debug(dhcp_client, "sending DHCP release request");
428 init_packet(dhcp_client, &packet, DHCPRELEASE);
430 packet.ciaddr = ciaddr;
432 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
434 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
435 server, SERVER_PORT);
438 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
439 static int switch_listening_mode(GDHCPClient *dhcp_client,
440 ListenMode listen_mode);
442 static gboolean send_probe_packet(gpointer dhcp_data)
444 GDHCPClient *dhcp_client;
447 dhcp_client = dhcp_data;
448 /* if requested_ip is not valid, pick a new address*/
449 if (dhcp_client->requested_ip == 0) {
450 debug(dhcp_client, "pick a new random address");
451 dhcp_client->requested_ip = ipv4ll_random_ip(0);
454 debug(dhcp_client, "sending IPV4LL probe request");
456 if (dhcp_client->retry_times == 1) {
457 dhcp_client->state = IPV4LL_PROBE;
458 switch_listening_mode(dhcp_client, L_ARP);
460 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
461 dhcp_client->requested_ip, dhcp_client->ifindex);
463 if (dhcp_client->retry_times < PROBE_NUM) {
464 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
465 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
466 timeout += PROBE_MIN*1000;
468 timeout = (ANNOUNCE_WAIT * 1000);
470 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
472 ipv4ll_probe_timeout,
478 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
479 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
481 static gboolean send_announce_packet(gpointer dhcp_data)
483 GDHCPClient *dhcp_client;
485 dhcp_client = dhcp_data;
487 debug(dhcp_client, "sending IPV4LL announce request");
489 ipv4ll_send_arp_packet(dhcp_client->mac_address,
490 dhcp_client->requested_ip,
491 dhcp_client->requested_ip,
492 dhcp_client->ifindex);
494 if (dhcp_client->timeout > 0)
495 g_source_remove(dhcp_client->timeout);
496 dhcp_client->timeout = 0;
498 if (dhcp_client->state == IPV4LL_DEFEND) {
499 dhcp_client->timeout =
500 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
502 ipv4ll_defend_timeout,
507 dhcp_client->timeout =
508 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
510 ipv4ll_announce_timeout,
516 static void get_interface_mac_address(int index, uint8_t *mac_address)
521 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
523 perror("Open socket error");
527 memset(&ifr, 0, sizeof(ifr));
528 ifr.ifr_ifindex = index;
530 err = ioctl(sk, SIOCGIFNAME, &ifr);
532 perror("Get interface name error");
536 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
538 perror("Get mac address error");
542 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
548 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
549 unsigned char **duid, int *duid_len)
554 case G_DHCPV6_DUID_LLT:
555 *duid_len = 2 + 2 + 4 + ETH_ALEN;
556 *duid = g_try_malloc(*duid_len);
562 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
565 duid_time = time(0) - DUID_TIME_EPOCH;
566 (*duid)[4] = duid_time >> 24;
567 (*duid)[5] = duid_time >> 16;
568 (*duid)[6] = duid_time >> 8;
569 (*duid)[7] = duid_time & 0xff;
571 case G_DHCPV6_DUID_EN:
573 case G_DHCPV6_DUID_LL:
574 *duid_len = 2 + 2 + ETH_ALEN;
575 *duid = g_try_malloc(*duid_len);
581 get_interface_mac_address(index, &(*duid)[2 + 2]);
590 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
593 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
596 g_free(dhcp_client->duid);
598 dhcp_client->duid = duid;
599 dhcp_client->duid_len = duid_len;
604 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
606 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
609 return dhcp_client->iaid;
612 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
617 get_interface_mac_address(index, buf);
619 memcpy(iaid, &buf[2], 4);
620 dhcp_client->iaid = iaid[0] << 24 |
621 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
624 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
625 uint32_t *T1, uint32_t *T2, time_t *last_renew)
627 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
631 *T1 = dhcp_client->T1;
634 *T2 = dhcp_client->T2;
636 if (last_renew != NULL)
637 *last_renew = dhcp_client->last_renew;
642 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
646 buf[1] = G_DHCPV6_IAADDR;
649 memcpy(&buf[4], &dhcp_client->ia_na, 16);
650 memset(&buf[20], 0, 4); /* preferred */
651 memset(&buf[24], 0, 4); /* valid */
655 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
659 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
661 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
671 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
672 int code, uint32_t *T1, uint32_t *T2,
675 if (code == G_DHCPV6_IA_TA) {
676 uint8_t ia_options[4];
678 put_iaid(dhcp_client, index, ia_options);
680 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
681 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
682 ia_options, sizeof(ia_options));
684 } else if (code == G_DHCPV6_IA_NA) {
686 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
688 if (add_iaaddr == TRUE) {
689 #define IAADDR_LEN (16+4+4)
690 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
692 put_iaid(dhcp_client, index, ia_options);
695 ia_options[4] = *T1 >> 24;
696 ia_options[5] = *T1 >> 16;
697 ia_options[6] = *T1 >> 8;
700 memset(&ia_options[4], 0x00, 4);
703 ia_options[8] = *T2 >> 24;
704 ia_options[9] = *T2 >> 16;
705 ia_options[10] = *T2 >> 8;
706 ia_options[11] = *T2;
708 memset(&ia_options[8], 0x00, 4);
710 create_iaaddr(dhcp_client, &ia_options[12],
713 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
714 ia_options, sizeof(ia_options));
716 uint8_t ia_options[4+4+4];
718 put_iaid(dhcp_client, index, ia_options);
720 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
721 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
723 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
724 ia_options, sizeof(ia_options));
733 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
736 int i, j, len = sizeof(uint16_t) * args;
739 values = g_try_malloc(len);
744 for (i = 0, j = 0; i < args; i++) {
745 uint16_t value = va_arg(va, int);
746 values[j++] = value >> 8;
747 values[j++] = value & 0xff;
751 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
756 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
758 struct dhcpv6_packet *packet;
759 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
763 memset(buf, 0, sizeof(buf));
764 packet = (struct dhcpv6_packet *)&buf[0];
765 ptr = buf + sizeof(struct dhcpv6_packet);
767 debug(dhcp_client, "sending DHCPv6 %s message", msg);
769 init_packet(dhcp_client, packet, type);
771 dhcp_client->xid = packet->transaction_id[0] << 16 |
772 packet->transaction_id[1] << 8 |
773 packet->transaction_id[2];
775 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
777 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
779 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
781 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
783 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
787 static int send_solicitation(GDHCPClient *dhcp_client)
789 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
792 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
794 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
797 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
799 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
802 static int send_information_req(GDHCPClient *dhcp_client)
804 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
808 static void remove_value(gpointer data, gpointer user_data)
814 static void remove_option_value(gpointer data)
816 GList *option_value = data;
818 g_list_foreach(option_value, remove_value, NULL);
821 GDHCPClient *g_dhcp_client_new(GDHCPType type,
822 int ifindex, GDHCPClientError *error)
824 GDHCPClient *dhcp_client;
827 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
831 dhcp_client = g_try_new0(GDHCPClient, 1);
832 if (dhcp_client == NULL) {
833 *error = G_DHCP_CLIENT_ERROR_NOMEM;
837 dhcp_client->interface = get_interface_name(ifindex);
838 if (dhcp_client->interface == NULL) {
839 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
843 if (interface_is_up(ifindex) == FALSE) {
844 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
848 get_interface_mac_address(ifindex, dhcp_client->mac_address);
850 dhcp_client->listener_sockfd = -1;
851 dhcp_client->listener_channel = NULL;
852 dhcp_client->listen_mode = L_NONE;
853 dhcp_client->ref_count = 1;
854 dhcp_client->type = type;
855 dhcp_client->ifindex = ifindex;
856 dhcp_client->lease_available_cb = NULL;
857 dhcp_client->ipv4ll_available_cb = NULL;
858 dhcp_client->no_lease_cb = NULL;
859 dhcp_client->lease_lost_cb = NULL;
860 dhcp_client->ipv4ll_lost_cb = NULL;
861 dhcp_client->address_conflict_cb = NULL;
862 dhcp_client->listener_watch = 0;
863 dhcp_client->retry_times = 0;
864 dhcp_client->ack_retry_times = 0;
865 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
866 g_direct_equal, NULL, remove_option_value);
867 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
868 g_direct_equal, NULL, g_free);
869 dhcp_client->request_list = NULL;
870 dhcp_client->require_list = NULL;
871 dhcp_client->duid = NULL;
872 dhcp_client->duid_len = 0;
873 dhcp_client->last_renew = time(0);
875 *error = G_DHCP_CLIENT_ERROR_NONE;
880 g_free(dhcp_client->interface);
885 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
887 static int dhcp_l2_socket(int ifindex)
890 struct sockaddr_ll sock;
895 * I've selected not to see LL header, so BPF doesn't see it, too.
896 * The filter may also pass non-IP and non-ARP packets, but we do
897 * a more complete check when receiving the message in userspace.
899 * and filter shamelessly stolen from:
901 * http://www.flamewarmaster.de/software/dhcpclient/
903 * There are a few other interesting ideas on that page (look under
904 * "Motivation"). Use of netlink events is most interesting. Think
905 * of various network servers listening for events and reconfiguring.
906 * That would obsolete sending HUP signals and/or make use of restarts.
908 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
911 * TODO: make conditional?
913 static const struct sock_filter filter_instr[] = {
915 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
916 /* L5, L1, is UDP? */
917 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
918 /* ugly check for arp on ethernet-like and IPv4 */
919 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
920 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
922 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
923 /* check udp source and destination ports */
924 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
926 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
928 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
929 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
932 static const struct sock_fprog filter_prog = {
933 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
934 /* casting const away: */
935 .filter = (struct sock_filter *) filter_instr,
938 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
942 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
943 /* Use only if standard ports are in use */
944 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
945 sizeof(filter_prog));
947 memset(&sock, 0, sizeof(sock));
948 sock.sll_family = AF_PACKET;
949 sock.sll_protocol = htons(ETH_P_IP);
950 sock.sll_ifindex = ifindex;
952 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
960 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
962 if (packet->ip.protocol != IPPROTO_UDP)
965 if (packet->ip.version != IPVERSION)
968 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
971 if (packet->udp.dest != htons(CLIENT_PORT))
974 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
980 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
983 struct ip_udp_dhcp_packet packet;
986 memset(&packet, 0, sizeof(packet));
988 bytes = read(fd, &packet, sizeof(packet));
992 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
995 if (bytes < ntohs(packet.ip.tot_len))
996 /* packet is bigger than sizeof(packet), we did partial read */
999 /* ignore any extra garbage bytes */
1000 bytes = ntohs(packet.ip.tot_len);
1002 if (sanity_check(&packet, bytes) == FALSE)
1005 check = packet.ip.check;
1006 packet.ip.check = 0;
1007 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1010 /* verify UDP checksum. IP header has to be modified for this */
1011 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1012 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1013 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1014 check = packet.udp.check;
1015 packet.udp.check = 0;
1016 if (check && check != dhcp_checksum(&packet, bytes))
1019 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1020 sizeof(packet.udp)));
1022 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1025 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1028 static void ipv4ll_start(GDHCPClient *dhcp_client)
1033 if (dhcp_client->timeout > 0) {
1034 g_source_remove(dhcp_client->timeout);
1035 dhcp_client->timeout = 0;
1038 switch_listening_mode(dhcp_client, L_NONE);
1039 dhcp_client->type = G_DHCP_IPV4LL;
1040 dhcp_client->retry_times = 0;
1041 dhcp_client->requested_ip = 0;
1043 /*try to start with a based mac address ip*/
1044 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1045 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1047 /*first wait a random delay to avoid storm of arp request on boot*/
1048 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1050 dhcp_client->retry_times++;
1051 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1058 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1061 switch_listening_mode(dhcp_client, L_NONE);
1063 if (dhcp_client->timeout > 0)
1064 g_source_remove(dhcp_client->timeout);
1066 if (dhcp_client->listener_watch > 0) {
1067 g_source_remove(dhcp_client->listener_watch);
1068 dhcp_client->listener_watch = 0;
1071 dhcp_client->state = IPV4LL_PROBE;
1072 dhcp_client->retry_times = 0;
1073 dhcp_client->requested_ip = 0;
1075 g_free(dhcp_client->assigned_ip);
1076 dhcp_client->assigned_ip = NULL;
1079 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1082 struct ether_arp arp;
1083 uint32_t ip_requested;
1084 int source_conflict;
1085 int target_conflict;
1087 memset(&arp, 0, sizeof(arp));
1089 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1093 if (arp.arp_op != htons(ARPOP_REPLY) &&
1094 arp.arp_op != htons(ARPOP_REQUEST))
1097 ip_requested = ntohl(dhcp_client->requested_ip);
1098 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1099 sizeof(ip_requested));
1101 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1102 sizeof(ip_requested));
1104 if (!source_conflict && !target_conflict)
1107 dhcp_client->conflicts++;
1109 debug(dhcp_client, "IPV4LL conflict detected");
1111 if (dhcp_client->state == IPV4LL_MONITOR) {
1112 if (!source_conflict)
1114 dhcp_client->state = IPV4LL_DEFEND;
1115 debug(dhcp_client, "DEFEND mode conflicts : %d",
1116 dhcp_client->conflicts);
1117 /*Try to defend with a single announce*/
1118 send_announce_packet(dhcp_client);
1122 if (dhcp_client->state == IPV4LL_DEFEND) {
1123 if (!source_conflict)
1125 else if (dhcp_client->ipv4ll_lost_cb != NULL)
1126 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1127 dhcp_client->ipv4ll_lost_data);
1130 ipv4ll_stop(dhcp_client);
1132 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1133 /*restart whole state machine*/
1134 dhcp_client->retry_times++;
1135 dhcp_client->timeout =
1136 g_timeout_add_full(G_PRIORITY_HIGH,
1137 ipv4ll_random_delay_ms(PROBE_WAIT),
1142 /* Here we got a lot of conflicts, RFC3927 states that we have
1143 * to wait RATE_LIMIT_INTERVAL before retrying,
1144 * but we just report failure.
1146 else if (dhcp_client->no_lease_cb != NULL)
1147 dhcp_client->no_lease_cb(dhcp_client,
1148 dhcp_client->no_lease_data);
1153 static gboolean check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1155 if (dhcp_client->type == G_DHCP_IPV6) {
1156 struct dhcpv6_packet *packet6 = pkt;
1159 if (packet6 == NULL)
1162 xid = packet6->transaction_id[0] << 16 |
1163 packet6->transaction_id[1] << 8 |
1164 packet6->transaction_id[2];
1166 if (xid != dhcp_client->xid)
1169 struct dhcp_packet *packet = pkt;
1171 if (packet->xid != dhcp_client->xid)
1174 if (packet->hlen != 6)
1177 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1184 static void start_request(GDHCPClient *dhcp_client);
1186 static gboolean request_timeout(gpointer user_data)
1188 GDHCPClient *dhcp_client = user_data;
1190 debug(dhcp_client, "request timeout (retries %d)",
1191 dhcp_client->retry_times);
1193 dhcp_client->retry_times++;
1195 start_request(dhcp_client);
1200 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1201 gpointer user_data);
1203 static int switch_listening_mode(GDHCPClient *dhcp_client,
1204 ListenMode listen_mode)
1206 GIOChannel *listener_channel;
1207 int listener_sockfd;
1209 if (dhcp_client->listen_mode == listen_mode)
1212 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1213 dhcp_client->listen_mode, listen_mode);
1215 if (dhcp_client->listen_mode != L_NONE) {
1216 if (dhcp_client->listener_watch > 0)
1217 g_source_remove(dhcp_client->listener_watch);
1218 dhcp_client->listener_channel = NULL;
1219 dhcp_client->listen_mode = L_NONE;
1220 dhcp_client->listener_sockfd = -1;
1221 dhcp_client->listener_watch = 0;
1224 if (listen_mode == L_NONE)
1227 if (listen_mode == L2)
1228 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1229 else if (listen_mode == L3) {
1230 if (dhcp_client->type == G_DHCP_IPV6)
1231 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1232 dhcp_client->interface,
1235 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1236 dhcp_client->interface,
1238 } else if (listen_mode == L_ARP)
1239 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1243 if (listener_sockfd < 0)
1246 listener_channel = g_io_channel_unix_new(listener_sockfd);
1247 if (listener_channel == NULL) {
1248 /* Failed to create listener channel */
1249 close(listener_sockfd);
1253 dhcp_client->listen_mode = listen_mode;
1254 dhcp_client->listener_sockfd = listener_sockfd;
1255 dhcp_client->listener_channel = listener_channel;
1257 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1258 dhcp_client->listener_watch =
1259 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1260 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1261 listener_event, dhcp_client,
1263 g_io_channel_unref(dhcp_client->listener_channel);
1268 static void start_request(GDHCPClient *dhcp_client)
1270 debug(dhcp_client, "start request (retries %d)",
1271 dhcp_client->retry_times);
1273 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1274 dhcp_client->state = INIT_SELECTING;
1275 ipv4ll_start(dhcp_client);
1280 if (dhcp_client->retry_times == 0) {
1281 dhcp_client->state = REQUESTING;
1282 switch_listening_mode(dhcp_client, L2);
1285 send_select(dhcp_client);
1287 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1294 static uint32_t get_lease(struct dhcp_packet *packet)
1297 uint32_t lease_seconds;
1299 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
1300 if (option_u8 == NULL)
1303 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
1304 lease_seconds = ntohl(lease_seconds);
1305 /* paranoia: must not be prone to overflows */
1306 lease_seconds &= 0x0fffffff;
1307 if (lease_seconds < 10)
1310 return lease_seconds;
1313 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1315 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1317 if (dhcp_client->timeout > 0) {
1318 g_source_remove(dhcp_client->timeout);
1319 dhcp_client->timeout = 0;
1322 dhcp_client->retry_times = retry_times;
1323 dhcp_client->requested_ip = 0;
1324 switch_listening_mode(dhcp_client, L2);
1326 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1329 static gboolean start_rebound_timeout(gpointer user_data)
1331 GDHCPClient *dhcp_client = user_data;
1333 debug(dhcp_client, "start rebound timeout");
1335 switch_listening_mode(dhcp_client, L2);
1337 dhcp_client->lease_seconds >>= 1;
1339 /* We need to have enough time to receive ACK package*/
1340 if (dhcp_client->lease_seconds <= 6) {
1342 /* ip need to be cleared */
1343 if (dhcp_client->lease_lost_cb != NULL)
1344 dhcp_client->lease_lost_cb(dhcp_client,
1345 dhcp_client->lease_lost_data);
1347 restart_dhcp(dhcp_client, 0);
1349 send_rebound(dhcp_client);
1351 dhcp_client->timeout =
1352 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1353 dhcp_client->lease_seconds >> 1,
1354 start_rebound_timeout,
1362 static void start_rebound(GDHCPClient *dhcp_client)
1364 debug(dhcp_client, "start rebound");
1366 dhcp_client->state = REBINDING;
1368 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1369 dhcp_client->lease_seconds >> 1,
1370 start_rebound_timeout,
1375 static gboolean start_renew_timeout(gpointer user_data)
1377 GDHCPClient *dhcp_client = user_data;
1379 debug(dhcp_client, "start renew timeout");
1381 dhcp_client->state = RENEWING;
1383 dhcp_client->lease_seconds >>= 1;
1385 switch_listening_mode(dhcp_client, L3);
1386 if (dhcp_client->lease_seconds <= 60)
1387 start_rebound(dhcp_client);
1389 send_renew(dhcp_client);
1391 if (dhcp_client->timeout > 0)
1392 g_source_remove(dhcp_client->timeout);
1394 dhcp_client->timeout =
1395 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1396 dhcp_client->lease_seconds >> 1,
1397 start_renew_timeout,
1405 static void start_bound(GDHCPClient *dhcp_client)
1407 debug(dhcp_client, "start bound");
1409 dhcp_client->state = BOUND;
1411 if (dhcp_client->timeout > 0)
1412 g_source_remove(dhcp_client->timeout);
1414 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1415 dhcp_client->lease_seconds >> 1,
1416 start_renew_timeout, dhcp_client,
1420 static gboolean restart_dhcp_timeout(gpointer user_data)
1422 GDHCPClient *dhcp_client = user_data;
1424 debug(dhcp_client, "restart DHCP timeout");
1426 dhcp_client->ack_retry_times++;
1428 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1433 static char *get_ip(uint32_t ip)
1435 struct in_addr addr;
1439 return g_strdup(inet_ntoa(addr));
1442 /* get a rough idea of how long an option will be */
1443 static const uint8_t len_of_option_as_string[] = {
1444 [OPTION_IP] = sizeof("255.255.255.255 "),
1445 [OPTION_STRING] = 1,
1446 [OPTION_U8] = sizeof("255 "),
1447 [OPTION_U16] = sizeof("65535 "),
1448 [OPTION_U32] = sizeof("4294967295 "),
1451 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1453 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1456 /* Create "opt_value1 option_value2 ..." string */
1457 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1459 unsigned upper_length;
1463 len = option[OPT_LEN - OPT_DATA];
1464 type &= OPTION_TYPE_MASK;
1465 optlen = dhcp_option_lengths[type];
1468 upper_length = len_of_option_as_string[type] *
1469 ((unsigned)len / (unsigned)optlen);
1470 dest = ret = malloc(upper_length + 1);
1474 while (len >= optlen) {
1477 dest += sprint_nip(dest, "", option);
1480 uint16_t val_u16 = dhcp_get_unaligned(
1481 (uint16_t *) option);
1482 dest += sprintf(dest, "%u", ntohs(val_u16));
1486 uint32_t val_u32 = dhcp_get_unaligned(
1487 (uint32_t *) option);
1488 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
1489 "%ld", (unsigned long) ntohl(val_u32));
1493 memcpy(dest, option, len);
1510 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1518 if (type == OPTION_STRING)
1519 return g_list_append(list, g_strdup(value));
1521 while ((pos = strchr(pos, ' ')) != NULL) {
1524 list = g_list_append(list, g_strdup(value));
1529 list = g_list_append(list, g_strdup(value));
1534 static inline uint32_t get_uint32(unsigned char *value)
1536 return value[0] << 24 | value[1] << 16 |
1537 value[2] << 8 | value[3];
1540 static inline uint16_t get_uint16(unsigned char *value)
1542 return value[0] << 8 | value[1];
1545 static GList *get_addresses(GDHCPClient *dhcp_client,
1547 unsigned char *value,
1551 struct in6_addr addr;
1552 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1553 uint16_t option_len, option_code, st = 0, max_len;
1554 int addr_count = 0, i, pos;
1558 iaid = get_uint32(&value[0]);
1559 if (dhcp_client->iaid != iaid)
1562 if (code == G_DHCPV6_IA_NA) {
1563 T1 = get_uint32(&value[4]);
1564 T2 = get_uint32(&value[8]);
1567 /* RFC 3315, 22.4 */
1577 max_len = len - pos;
1579 /* We have more sub-options in this packet. */
1581 option = dhcpv6_get_sub_option(&value[pos], max_len,
1582 &option_code, &option_len);
1584 debug(dhcp_client, "pos %d option %p code %d len %d",
1585 pos, option, option_code, option_len);
1593 switch (option_code) {
1594 case G_DHCPV6_IAADDR:
1596 memcpy(&addr, &option[0], sizeof(addr));
1598 preferred = get_uint32(&option[i]);
1600 valid = get_uint32(&option[i]);
1605 case G_DHCPV6_STATUS_CODE:
1606 st = get_uint16(&option[0]);
1607 debug(dhcp_client, "error code %d", st);
1608 if (option_len > 2) {
1609 str = g_strndup((gchar *)&option[2],
1611 debug(dhcp_client, "error text: %s", str);
1619 pos += 2 + 2 + option_len;
1621 } while (option != NULL);
1623 if (addr_count > 0 && st == 0) {
1624 /* We only support one address atm */
1625 char str[INET6_ADDRSTRLEN + 1];
1627 if (preferred > valid)
1628 /* RFC 3315, 22.6 */
1631 dhcp_client->T1 = T1;
1632 dhcp_client->T2 = T2;
1634 inet_ntop(AF_INET6, &addr, str, INET6_ADDRSTRLEN);
1635 debug(dhcp_client, "count %d addr %s T1 %u T2 %u",
1636 addr_count, str, T1, T2);
1638 list = g_list_append(list, g_strdup(str));
1640 if (code == G_DHCPV6_IA_NA)
1641 memcpy(&dhcp_client->ia_na, &addr,
1642 sizeof(struct in6_addr));
1644 memcpy(&dhcp_client->ia_ta, &addr,
1645 sizeof(struct in6_addr));
1651 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
1653 unsigned char *value,
1661 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
1662 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
1665 "%s server list length (%d) is invalid",
1666 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
1670 for (i = 0; i < len; i += 16) {
1672 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
1676 if (inet_ntop(AF_INET6, &value[i], str,
1677 INET6_ADDRSTRLEN) == NULL)
1680 list = g_list_append(list, str);
1684 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
1685 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
1686 list = get_addresses(dhcp_client, code, len, value, status);
1696 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
1697 struct dhcpv6_packet *packet,
1698 uint16_t pkt_len, uint16_t *status)
1700 GList *list, *value_list;
1703 uint16_t option_len;
1705 for (list = dhcp_client->request_list; list; list = list->next) {
1706 code = (uint16_t) GPOINTER_TO_INT(list->data);
1708 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
1710 if (option == NULL) {
1711 g_hash_table_remove(dhcp_client->code_value_hash,
1712 GINT_TO_POINTER((int) code));
1716 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
1717 option_len, option, status);
1719 debug(dhcp_client, "code %d %p len %d list %p", code, option,
1720 option_len, value_list);
1722 if (value_list == NULL)
1723 g_hash_table_remove(dhcp_client->code_value_hash,
1724 GINT_TO_POINTER((int) code));
1726 g_hash_table_insert(dhcp_client->code_value_hash,
1727 GINT_TO_POINTER((int) code), value_list);
1731 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
1733 GDHCPOptionType type;
1734 GList *list, *value_list;
1739 for (list = dhcp_client->request_list; list; list = list->next) {
1740 code = (uint8_t) GPOINTER_TO_INT(list->data);
1742 option = dhcp_get_option(packet, code);
1743 if (option == NULL) {
1744 g_hash_table_remove(dhcp_client->code_value_hash,
1745 GINT_TO_POINTER((int) code));
1749 type = dhcp_get_code_type(code);
1751 option_value = malloc_option_value_string(option, type);
1752 if (option_value == NULL)
1753 g_hash_table_remove(dhcp_client->code_value_hash,
1754 GINT_TO_POINTER((int) code));
1756 value_list = get_option_value_list(option_value, type);
1758 g_free(option_value);
1760 if (value_list == NULL)
1761 g_hash_table_remove(dhcp_client->code_value_hash,
1762 GINT_TO_POINTER((int) code));
1764 g_hash_table_insert(dhcp_client->code_value_hash,
1765 GINT_TO_POINTER((int) code), value_list);
1769 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1772 GDHCPClient *dhcp_client = user_data;
1773 struct dhcp_packet packet;
1774 struct dhcpv6_packet *packet6 = NULL;
1775 uint8_t *message_type = NULL, *client_id = NULL, *option_u8,
1777 uint16_t option_len = 0, status = 0;
1779 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
1780 uint16_t pkt_len = 0;
1784 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1785 dhcp_client->listener_watch = 0;
1789 if (dhcp_client->listen_mode == L_NONE)
1794 if (dhcp_client->listen_mode == L2)
1795 re = dhcp_recv_l2_packet(&packet,
1796 dhcp_client->listener_sockfd);
1797 else if (dhcp_client->listen_mode == L3) {
1798 if (dhcp_client->type == G_DHCP_IPV6) {
1799 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
1800 dhcp_client->listener_sockfd);
1804 re = dhcp_recv_l3_packet(&packet,
1805 dhcp_client->listener_sockfd);
1806 } else if (dhcp_client->listen_mode == L_ARP) {
1807 re = ipv4ll_recv_arp_packet(dhcp_client);
1816 if (check_package_owner(dhcp_client, pkt) == FALSE)
1819 if (dhcp_client->type == G_DHCP_IPV6) {
1821 client_id = dhcpv6_get_option(packet6, pkt_len,
1822 G_DHCPV6_CLIENTID, &option_len, &count);
1824 if (client_id == NULL || count == 0 || option_len == 0 ||
1825 memcmp(dhcp_client->duid, client_id,
1826 dhcp_client->duid_len) != 0) {
1828 "client duid error, discarding msg %p/%d/%d",
1829 client_id, option_len, count);
1833 option_u8 = dhcpv6_get_option(packet6, pkt_len,
1834 G_DHCPV6_STATUS_CODE, &option_len, NULL);
1835 if (option_u8 != 0 && option_len > 0) {
1836 status = option_u8[0]<<8 | option_u8[1];
1838 debug(dhcp_client, "error code %d", status);
1839 if (option_len > 2) {
1840 gchar *txt = g_strndup(
1841 (gchar *)&option_u8[2],
1843 debug(dhcp_client, "error text: %s",
1848 dhcp_client->status_code = status;
1850 dhcp_client->status_code = 0;
1853 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1855 if (message_type == NULL && client_id == NULL)
1856 /* No message type / client id option, ignore package */
1859 debug(dhcp_client, "received DHCP packet (current state %d)",
1860 dhcp_client->state);
1862 switch (dhcp_client->state) {
1863 case INIT_SELECTING:
1864 if (*message_type != DHCPOFFER)
1867 g_source_remove(dhcp_client->timeout);
1868 dhcp_client->timeout = 0;
1869 dhcp_client->retry_times = 0;
1871 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
1872 dhcp_client->server_ip =
1873 dhcp_get_unaligned((uint32_t *) option_u8);
1874 dhcp_client->requested_ip = packet.yiaddr;
1876 dhcp_client->state = REQUESTING;
1878 start_request(dhcp_client);
1884 if (*message_type == DHCPACK) {
1885 dhcp_client->retry_times = 0;
1887 if (dhcp_client->timeout > 0)
1888 g_source_remove(dhcp_client->timeout);
1889 dhcp_client->timeout = 0;
1891 dhcp_client->lease_seconds = get_lease(&packet);
1893 get_request(dhcp_client, &packet);
1895 switch_listening_mode(dhcp_client, L_NONE);
1897 g_free(dhcp_client->assigned_ip);
1898 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1900 /* Address should be set up here */
1901 if (dhcp_client->lease_available_cb != NULL)
1902 dhcp_client->lease_available_cb(dhcp_client,
1903 dhcp_client->lease_available_data);
1905 start_bound(dhcp_client);
1906 } else if (*message_type == DHCPNAK) {
1907 dhcp_client->retry_times = 0;
1909 if (dhcp_client->timeout > 0)
1910 g_source_remove(dhcp_client->timeout);
1912 dhcp_client->timeout = g_timeout_add_seconds_full(
1914 restart_dhcp_timeout,
1921 if (dhcp_client->type != G_DHCP_IPV6)
1924 if (packet6->message != DHCPV6_REPLY &&
1925 packet6->message != DHCPV6_ADVERTISE)
1929 server_id = dhcpv6_get_option(packet6, pkt_len,
1930 G_DHCPV6_SERVERID, &option_len, &count);
1931 if (server_id == NULL || count != 1 || option_len == 0) {
1932 /* RFC 3315, 15.10 */
1934 "server duid error, discarding msg %p/%d/%d",
1935 server_id, option_len, count);
1938 dhcp_client->server_duid = g_try_malloc(option_len);
1939 if (dhcp_client->server_duid == NULL)
1941 memcpy(dhcp_client->server_duid, server_id, option_len);
1942 dhcp_client->server_duid_len = option_len;
1944 if (packet6->message == DHCPV6_REPLY) {
1945 uint8_t *rapid_commit;
1948 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
1949 G_DHCPV6_RAPID_COMMIT,
1950 &option_len, &count);
1951 if (rapid_commit == NULL || option_len == 0 ||
1953 /* RFC 3315, 17.1.4 */
1957 switch_listening_mode(dhcp_client, L_NONE);
1959 if (dhcp_client->status_code == 0)
1960 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
1961 &dhcp_client->status_code);
1963 if (packet6->message == DHCPV6_ADVERTISE) {
1964 if (dhcp_client->advertise_cb != NULL)
1965 dhcp_client->advertise_cb(dhcp_client,
1966 dhcp_client->advertise_data);
1970 if (dhcp_client->solicitation_cb != NULL) {
1972 * The dhcp_client might not be valid after the
1973 * callback call so just return immediately.
1975 dhcp_client->solicitation_cb(dhcp_client,
1976 dhcp_client->solicitation_data);
1980 case INFORMATION_REQ:
1983 if (dhcp_client->type != G_DHCP_IPV6)
1986 if (packet6->message != DHCPV6_REPLY)
1991 server_id = dhcpv6_get_option(packet6, pkt_len,
1992 G_DHCPV6_SERVERID, &option_len, &count);
1993 if (server_id == NULL || count != 1 || option_len == 0 ||
1994 (dhcp_client->server_duid_len > 0 &&
1995 memcmp(dhcp_client->server_duid, server_id,
1996 dhcp_client->server_duid_len) != 0)) {
1997 /* RFC 3315, 15.10 */
1999 "server duid error, discarding msg %p/%d/%d",
2000 server_id, option_len, count);
2004 switch_listening_mode(dhcp_client, L_NONE);
2006 dhcp_client->status_code = 0;
2007 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2008 &dhcp_client->status_code);
2010 if (dhcp_client->information_req_cb != NULL) {
2012 * The dhcp_client might not be valid after the
2013 * callback call so just return immediately.
2015 dhcp_client->information_req_cb(dhcp_client,
2016 dhcp_client->information_req_data);
2019 if (dhcp_client->request_cb != NULL) {
2020 dhcp_client->request_cb(dhcp_client,
2021 dhcp_client->request_data);
2024 if (dhcp_client->renew_cb != NULL) {
2025 dhcp_client->renew_cb(dhcp_client,
2026 dhcp_client->renew_data);
2034 debug(dhcp_client, "processed DHCP packet (new state %d)",
2035 dhcp_client->state);
2040 static gboolean discover_timeout(gpointer user_data)
2042 GDHCPClient *dhcp_client = user_data;
2044 dhcp_client->retry_times++;
2047 * We do not send the REQUESTED IP option if we are retrying because
2048 * if the server is non-authoritative it will ignore the request if the
2049 * option is present.
2051 g_dhcp_client_start(dhcp_client, NULL);
2056 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2058 GDHCPClient *dhcp_client = dhcp_data;
2060 debug(dhcp_client, "back to MONITOR mode");
2062 dhcp_client->conflicts = 0;
2063 dhcp_client->state = IPV4LL_MONITOR;
2068 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2070 GDHCPClient *dhcp_client = dhcp_data;
2073 debug(dhcp_client, "request timeout (retries %d)",
2074 dhcp_client->retry_times);
2076 if (dhcp_client->retry_times != ANNOUNCE_NUM){
2077 dhcp_client->retry_times++;
2078 send_announce_packet(dhcp_client);
2082 ip = htonl(dhcp_client->requested_ip);
2083 debug(dhcp_client, "switching to monitor mode");
2084 dhcp_client->state = IPV4LL_MONITOR;
2085 dhcp_client->assigned_ip = get_ip(ip);
2087 if (dhcp_client->ipv4ll_available_cb != NULL)
2088 dhcp_client->ipv4ll_available_cb(dhcp_client,
2089 dhcp_client->ipv4ll_available_data);
2090 dhcp_client->conflicts = 0;
2095 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2098 GDHCPClient *dhcp_client = dhcp_data;
2100 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2101 dhcp_client->retry_times);
2103 if (dhcp_client->retry_times == PROBE_NUM) {
2104 dhcp_client->state = IPV4LL_ANNOUNCE;
2105 dhcp_client->retry_times = 0;
2107 dhcp_client->retry_times++;
2108 send_announce_packet(dhcp_client);
2111 dhcp_client->retry_times++;
2112 send_probe_packet(dhcp_client);
2117 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2122 if (dhcp_client->type == G_DHCP_IPV6) {
2123 if (dhcp_client->information_req_cb) {
2124 dhcp_client->state = INFORMATION_REQ;
2125 re = switch_listening_mode(dhcp_client, L3);
2127 switch_listening_mode(dhcp_client, L_NONE);
2128 dhcp_client->state = 0;
2131 send_information_req(dhcp_client);
2133 } else if (dhcp_client->solicitation_cb) {
2134 dhcp_client->state = SOLICITATION;
2135 re = switch_listening_mode(dhcp_client, L3);
2137 switch_listening_mode(dhcp_client, L_NONE);
2138 dhcp_client->state = 0;
2141 send_solicitation(dhcp_client);
2143 } else if (dhcp_client->request_cb) {
2144 dhcp_client->state = REQUEST;
2145 re = switch_listening_mode(dhcp_client, L3);
2147 switch_listening_mode(dhcp_client, L_NONE);
2148 dhcp_client->state = 0;
2151 send_dhcpv6_request(dhcp_client);
2153 } else if (dhcp_client->renew_cb) {
2154 dhcp_client->state = RENEW;
2155 re = switch_listening_mode(dhcp_client, L3);
2157 switch_listening_mode(dhcp_client, L_NONE);
2158 dhcp_client->state = 0;
2161 send_dhcpv6_renew(dhcp_client);
2167 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2168 ipv4ll_start(dhcp_client);
2172 if (dhcp_client->retry_times == 0) {
2173 g_free(dhcp_client->assigned_ip);
2174 dhcp_client->assigned_ip = NULL;
2176 dhcp_client->state = INIT_SELECTING;
2177 re = switch_listening_mode(dhcp_client, L2);
2181 dhcp_client->xid = rand();
2184 if (last_address == NULL) {
2187 addr = inet_addr(last_address);
2188 if (addr == 0xFFFFFFFF) {
2191 g_free(dhcp_client->last_address);
2192 dhcp_client->last_address = g_strdup(last_address);
2195 send_discover(dhcp_client, addr);
2197 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2205 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2207 switch_listening_mode(dhcp_client, L_NONE);
2209 if (dhcp_client->state == BOUND ||
2210 dhcp_client->state == RENEWING ||
2211 dhcp_client->state == REBINDING)
2212 send_release(dhcp_client, dhcp_client->server_ip,
2213 dhcp_client->requested_ip);
2215 if (dhcp_client->timeout > 0) {
2216 g_source_remove(dhcp_client->timeout);
2217 dhcp_client->timeout = 0;
2220 if (dhcp_client->listener_watch > 0) {
2221 g_source_remove(dhcp_client->listener_watch);
2222 dhcp_client->listener_watch = 0;
2225 dhcp_client->listener_channel = NULL;
2227 dhcp_client->retry_times = 0;
2228 dhcp_client->ack_retry_times = 0;
2230 dhcp_client->requested_ip = 0;
2231 dhcp_client->state = RELEASED;
2232 dhcp_client->lease_seconds = 0;
2235 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2236 unsigned char option_code)
2238 return g_hash_table_lookup(dhcp_client->code_value_hash,
2239 GINT_TO_POINTER((int) option_code));
2242 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2243 GDHCPClientEvent event,
2244 GDHCPClientEventFunc func,
2248 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2249 dhcp_client->lease_available_cb = func;
2250 dhcp_client->lease_available_data = data;
2252 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2253 if (dhcp_client->type == G_DHCP_IPV6)
2255 dhcp_client->ipv4ll_available_cb = func;
2256 dhcp_client->ipv4ll_available_data = data;
2258 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2259 dhcp_client->no_lease_cb = func;
2260 dhcp_client->no_lease_data = data;
2262 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2263 dhcp_client->lease_lost_cb = func;
2264 dhcp_client->lease_lost_data = data;
2266 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2267 if (dhcp_client->type == G_DHCP_IPV6)
2269 dhcp_client->ipv4ll_lost_cb = func;
2270 dhcp_client->ipv4ll_lost_data = data;
2272 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2273 dhcp_client->address_conflict_cb = func;
2274 dhcp_client->address_conflict_data = data;
2276 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2277 if (dhcp_client->type == G_DHCP_IPV4)
2279 dhcp_client->information_req_cb = func;
2280 dhcp_client->information_req_data = data;
2282 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2283 if (dhcp_client->type == G_DHCP_IPV4)
2285 dhcp_client->solicitation_cb = func;
2286 dhcp_client->solicitation_data = data;
2288 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2289 if (dhcp_client->type == G_DHCP_IPV4)
2291 dhcp_client->advertise_cb = func;
2292 dhcp_client->advertise_data = data;
2294 case G_DHCP_CLIENT_EVENT_REQUEST:
2295 if (dhcp_client->type == G_DHCP_IPV4)
2297 dhcp_client->request_cb = func;
2298 dhcp_client->request_data = data;
2300 case G_DHCP_CLIENT_EVENT_RENEW:
2301 if (dhcp_client->type == G_DHCP_IPV4)
2303 dhcp_client->renew_cb = func;
2304 dhcp_client->renew_data = data;
2309 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2311 return dhcp_client->ifindex;
2314 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2316 return g_strdup(dhcp_client->assigned_ip);
2319 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2321 GList *option = NULL;
2323 if (dhcp_client->type == G_DHCP_IPV6)
2326 switch (dhcp_client->state) {
2328 case IPV4LL_MONITOR:
2329 return g_strdup("255.255.0.0");
2333 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2335 return g_strdup(option->data);
2336 case INIT_SELECTING:
2340 case IPV4LL_ANNOUNCE:
2341 case INFORMATION_REQ:
2350 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2351 unsigned int option_code)
2353 if (g_list_find(dhcp_client->request_list,
2354 GINT_TO_POINTER((int) option_code)) == NULL)
2355 dhcp_client->request_list = g_list_prepend(
2356 dhcp_client->request_list,
2357 (GINT_TO_POINTER((int) option_code)));
2359 return G_DHCP_CLIENT_ERROR_NONE;
2362 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2364 g_list_free(dhcp_client->request_list);
2365 dhcp_client->request_list = NULL;
2368 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2370 g_hash_table_remove_all(dhcp_client->send_value_hash);
2373 static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
2376 int len = strnlen(str, 255);
2378 storage = malloc(len + extra + OPT_DATA);
2379 storage[OPT_CODE] = code;
2380 storage[OPT_LEN] = len + extra;
2381 memcpy(storage + extra + OPT_DATA, str, len);
2386 /* Now only support send hostname */
2387 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
2388 unsigned char option_code, const char *option_value)
2390 uint8_t *binary_option;
2392 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
2393 binary_option = alloc_dhcp_option(option_code,
2396 g_hash_table_insert(dhcp_client->send_value_hash,
2397 GINT_TO_POINTER((int) option_code), binary_option);
2400 return G_DHCP_CLIENT_ERROR_NONE;
2403 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
2408 storage = g_malloc(2 + 2 + len);
2409 if (storage == NULL)
2412 storage[0] = code >> 8;
2413 storage[1] = code & 0xff;
2414 storage[2] = len >> 8;
2415 storage[3] = len & 0xff;
2416 memcpy(storage + 2 + 2, option, len);
2421 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
2422 uint16_t option_code,
2423 uint8_t *option_value,
2424 uint16_t option_len)
2426 if (option_value != NULL) {
2427 uint8_t *binary_option;
2429 debug(dhcp_client, "setting option %d to %p len %d",
2430 option_code, option_value, option_len);
2432 binary_option = alloc_dhcpv6_option(option_code, option_value,
2434 if (binary_option != NULL)
2435 g_hash_table_insert(dhcp_client->send_value_hash,
2436 GINT_TO_POINTER((int) option_code),
2441 void g_dhcpv6_client_reset_renew(GDHCPClient *dhcp_client)
2443 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2446 dhcp_client->last_renew = time(0);
2449 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
2451 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2454 return dhcp_client->status_code;
2457 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
2459 if (dhcp_client == NULL)
2462 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
2467 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
2469 if (dhcp_client == NULL)
2472 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
2475 g_dhcp_client_stop(dhcp_client);
2477 g_free(dhcp_client->interface);
2478 g_free(dhcp_client->assigned_ip);
2479 g_free(dhcp_client->last_address);
2480 g_free(dhcp_client->duid);
2481 g_free(dhcp_client->server_duid);
2483 g_list_free(dhcp_client->request_list);
2484 g_list_free(dhcp_client->require_list);
2486 g_hash_table_destroy(dhcp_client->code_value_hash);
2487 g_hash_table_destroy(dhcp_client->send_value_hash);
2489 g_free(dhcp_client);
2492 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
2493 GDHCPDebugFunc func, gpointer user_data)
2495 if (dhcp_client == NULL)
2498 dhcp_client->debug_func = func;
2499 dhcp_client->debug_data = user_data;