3 * DHCP client library with GLib integration
5 * Copyright (C) 2009-2012 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 {
86 uint8_t mac_address[6];
89 uint32_t requested_ip;
92 uint32_t lease_seconds;
93 ListenMode listen_mode;
96 uint8_t ack_retry_times;
100 GIOChannel *listener_channel;
103 GHashTable *code_value_hash;
104 GHashTable *send_value_hash;
105 GDHCPClientEventFunc lease_available_cb;
106 gpointer lease_available_data;
107 GDHCPClientEventFunc ipv4ll_available_cb;
108 gpointer ipv4ll_available_data;
109 GDHCPClientEventFunc no_lease_cb;
110 gpointer no_lease_data;
111 GDHCPClientEventFunc lease_lost_cb;
112 gpointer lease_lost_data;
113 GDHCPClientEventFunc ipv4ll_lost_cb;
114 gpointer ipv4ll_lost_data;
115 GDHCPClientEventFunc address_conflict_cb;
116 gpointer address_conflict_data;
117 GDHCPDebugFunc debug_func;
119 GDHCPClientEventFunc information_req_cb;
120 gpointer information_req_data;
121 GDHCPClientEventFunc solicitation_cb;
122 gpointer solicitation_data;
123 GDHCPClientEventFunc advertise_cb;
124 gpointer advertise_data;
125 GDHCPClientEventFunc request_cb;
126 gpointer request_data;
127 GDHCPClientEventFunc renew_cb;
129 GDHCPClientEventFunc rebind_cb;
130 gpointer rebind_data;
131 GDHCPClientEventFunc release_cb;
132 gpointer release_data;
136 unsigned char *server_duid;
138 uint16_t status_code;
141 struct in6_addr ia_na;
142 struct in6_addr ia_ta;
148 static inline void debug(GDHCPClient *client, const char *format, ...)
153 if (client->debug_func == NULL)
156 va_start(ap, format);
158 if (vsnprintf(str, sizeof(str), format, ap) > 0)
159 client->debug_func(str, client->debug_data);
164 /* Initialize the packet with the proper defaults */
165 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
167 if (dhcp_client->type == G_DHCP_IPV6)
168 dhcpv6_init_header(pkt, type);
170 struct dhcp_packet *packet = pkt;
172 dhcp_init_header(packet, type);
173 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
177 static void add_request_options(GDHCPClient *dhcp_client,
178 struct dhcp_packet *packet)
183 int end = dhcp_end_option(packet->options);
185 for (list = dhcp_client->request_list; list; list = list->next) {
186 code = (uint8_t) GPOINTER_TO_INT(list->data);
188 packet->options[end + OPT_DATA + len] = code;
193 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
194 packet->options[end + OPT_LEN] = len;
195 packet->options[end + OPT_DATA + len] = DHCP_END;
202 unsigned char **ptr_buf;
205 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
208 uint8_t *option = value;
210 struct hash_params *params = user_data;
212 /* option[0][1] contains option code */
213 len = option[2] << 8 | option[3];
215 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
218 memcpy(*params->ptr_buf, option, len + 2 + 2);
219 (*params->ptr_buf) += len + 2 + 2;
222 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
223 unsigned char *buf, int max_buf,
224 unsigned char **ptr_buf)
226 struct hash_params params = {
232 if (dhcp_client->type == G_DHCP_IPV4)
235 g_hash_table_foreach(dhcp_client->send_value_hash,
236 add_dhcpv6_binary_option, ¶ms);
238 *ptr_buf = *params.ptr_buf;
241 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
245 buf[1] = code & 0xff;
248 if (len > 0 && msg != NULL)
249 memcpy(&buf[4], msg, len);
252 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
253 struct dhcpv6_packet *packet,
254 unsigned char *buf, int max_buf,
255 unsigned char **ptr_buf)
261 if (dhcp_client->type == G_DHCP_IPV4)
264 for (list = dhcp_client->request_list; list; list = list->next) {
265 code = (uint16_t) GPOINTER_TO_INT(list->data);
268 case G_DHCPV6_CLIENTID:
269 if (dhcp_client->duid == NULL)
272 len = 2 + 2 + dhcp_client->duid_len;
273 if ((*ptr_buf + len) > (buf + max_buf)) {
274 debug(dhcp_client, "Too long dhcpv6 message "
275 "when writing client id option");
279 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
280 dhcp_client->duid_len, dhcp_client->duid);
284 case G_DHCPV6_SERVERID:
285 if (dhcp_client->server_duid == NULL)
288 len = 2 + 2 + dhcp_client->server_duid_len;
289 if ((*ptr_buf + len) > (buf + max_buf)) {
290 debug(dhcp_client, "Too long dhcpv6 message "
291 "when writing server id option");
295 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
296 dhcp_client->server_duid_len,
297 dhcp_client->server_duid);
301 case G_DHCPV6_RAPID_COMMIT:
303 if ((*ptr_buf + len) > (buf + max_buf)) {
304 debug(dhcp_client, "Too long dhcpv6 message "
305 "when writing rapid commit option");
309 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
316 case G_DHCPV6_DNS_SERVERS:
319 case G_DHCPV6_SNTP_SERVERS:
328 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
330 uint8_t *option = value;
331 struct dhcp_packet *packet = user_data;
333 dhcp_add_binary_option(packet, option);
336 static void add_send_options(GDHCPClient *dhcp_client,
337 struct dhcp_packet *packet)
339 g_hash_table_foreach(dhcp_client->send_value_hash,
340 add_binary_option, packet);
344 * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
345 * represents the number of seconds elapsed from the start of
346 * attempting DHCP to satisfy some DHCP servers that allow for an
347 * "authoritative" reply before responding.
349 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
351 return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
354 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
356 struct dhcp_packet packet;
358 debug(dhcp_client, "sending DHCP discover request");
360 init_packet(dhcp_client, &packet, DHCPDISCOVER);
362 packet.xid = dhcp_client->xid;
363 packet.secs = dhcp_attempt_secs(dhcp_client);
366 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
368 /* Explicitly saying that we want RFC-compliant packets helps
369 * some buggy DHCP servers to NOT send bigger packets */
370 dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
372 add_request_options(dhcp_client, &packet);
374 add_send_options(dhcp_client, &packet);
376 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
377 INADDR_BROADCAST, SERVER_PORT,
378 MAC_BCAST_ADDR, dhcp_client->ifindex);
381 static int send_select(GDHCPClient *dhcp_client)
383 struct dhcp_packet packet;
385 debug(dhcp_client, "sending DHCP select request");
387 init_packet(dhcp_client, &packet, DHCPREQUEST);
389 packet.xid = dhcp_client->xid;
390 packet.secs = dhcp_attempt_secs(dhcp_client);
392 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
393 dhcp_client->requested_ip);
394 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
396 add_request_options(dhcp_client, &packet);
398 add_send_options(dhcp_client, &packet);
400 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
401 INADDR_BROADCAST, SERVER_PORT,
402 MAC_BCAST_ADDR, dhcp_client->ifindex);
405 static int send_renew(GDHCPClient *dhcp_client)
407 struct dhcp_packet packet;
409 debug(dhcp_client, "sending DHCP renew request");
411 init_packet(dhcp_client , &packet, DHCPREQUEST);
412 packet.xid = dhcp_client->xid;
413 packet.ciaddr = dhcp_client->requested_ip;
415 add_request_options(dhcp_client, &packet);
417 add_send_options(dhcp_client, &packet);
419 return dhcp_send_kernel_packet(&packet,
420 dhcp_client->requested_ip, CLIENT_PORT,
421 dhcp_client->server_ip, SERVER_PORT);
424 static int send_rebound(GDHCPClient *dhcp_client)
426 struct dhcp_packet packet;
428 debug(dhcp_client, "sending DHCP rebound request");
430 init_packet(dhcp_client , &packet, DHCPREQUEST);
431 packet.xid = dhcp_client->xid;
432 packet.ciaddr = dhcp_client->requested_ip;
434 add_request_options(dhcp_client, &packet);
436 add_send_options(dhcp_client, &packet);
438 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
439 INADDR_BROADCAST, SERVER_PORT,
440 MAC_BCAST_ADDR, dhcp_client->ifindex);
443 static int send_release(GDHCPClient *dhcp_client,
444 uint32_t server, uint32_t ciaddr)
446 struct dhcp_packet packet;
448 debug(dhcp_client, "sending DHCP release request");
450 init_packet(dhcp_client, &packet, DHCPRELEASE);
452 packet.ciaddr = ciaddr;
454 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
456 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
457 server, SERVER_PORT);
460 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
461 static int switch_listening_mode(GDHCPClient *dhcp_client,
462 ListenMode listen_mode);
464 static gboolean send_probe_packet(gpointer dhcp_data)
466 GDHCPClient *dhcp_client;
469 dhcp_client = dhcp_data;
470 /* if requested_ip is not valid, pick a new address*/
471 if (dhcp_client->requested_ip == 0) {
472 debug(dhcp_client, "pick a new random address");
473 dhcp_client->requested_ip = ipv4ll_random_ip(0);
476 debug(dhcp_client, "sending IPV4LL probe request");
478 if (dhcp_client->retry_times == 1) {
479 dhcp_client->state = IPV4LL_PROBE;
480 switch_listening_mode(dhcp_client, L_ARP);
482 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
483 dhcp_client->requested_ip, dhcp_client->ifindex);
485 if (dhcp_client->retry_times < PROBE_NUM) {
486 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
487 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
488 timeout += PROBE_MIN*1000;
490 timeout = (ANNOUNCE_WAIT * 1000);
492 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
494 ipv4ll_probe_timeout,
500 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
501 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
503 static gboolean send_announce_packet(gpointer dhcp_data)
505 GDHCPClient *dhcp_client;
507 dhcp_client = dhcp_data;
509 debug(dhcp_client, "sending IPV4LL announce request");
511 ipv4ll_send_arp_packet(dhcp_client->mac_address,
512 dhcp_client->requested_ip,
513 dhcp_client->requested_ip,
514 dhcp_client->ifindex);
516 if (dhcp_client->timeout > 0)
517 g_source_remove(dhcp_client->timeout);
518 dhcp_client->timeout = 0;
520 if (dhcp_client->state == IPV4LL_DEFEND) {
521 dhcp_client->timeout =
522 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
524 ipv4ll_defend_timeout,
529 dhcp_client->timeout =
530 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
532 ipv4ll_announce_timeout,
538 static void get_interface_mac_address(int index, uint8_t *mac_address)
543 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
545 perror("Open socket error");
549 memset(&ifr, 0, sizeof(ifr));
550 ifr.ifr_ifindex = index;
552 err = ioctl(sk, SIOCGIFNAME, &ifr);
554 perror("Get interface name error");
558 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
560 perror("Get mac address error");
564 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
570 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
571 unsigned char **duid, int *duid_len)
576 case G_DHCPV6_DUID_LLT:
577 *duid_len = 2 + 2 + 4 + ETH_ALEN;
578 *duid = g_try_malloc(*duid_len);
584 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
587 duid_time = time(NULL) - DUID_TIME_EPOCH;
588 (*duid)[4] = duid_time >> 24;
589 (*duid)[5] = duid_time >> 16;
590 (*duid)[6] = duid_time >> 8;
591 (*duid)[7] = duid_time & 0xff;
593 case G_DHCPV6_DUID_EN:
595 case G_DHCPV6_DUID_LL:
596 *duid_len = 2 + 2 + ETH_ALEN;
597 *duid = g_try_malloc(*duid_len);
603 get_interface_mac_address(index, &(*duid)[2 + 2]);
612 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
615 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
618 g_free(dhcp_client->duid);
620 dhcp_client->duid = duid;
621 dhcp_client->duid_len = duid_len;
626 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
628 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
631 return dhcp_client->iaid;
634 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
639 get_interface_mac_address(index, buf);
641 memcpy(iaid, &buf[2], 4);
642 dhcp_client->iaid = iaid[0] << 24 |
643 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
646 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
647 uint32_t *T1, uint32_t *T2,
648 time_t *last_renew, time_t *last_rebind,
651 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
655 *T1 = dhcp_client->T1;
658 *T2 = dhcp_client->T2;
660 if (last_renew != NULL)
661 *last_renew = dhcp_client->last_renew;
663 if (last_rebind != NULL)
664 *last_rebind = dhcp_client->last_rebind;
667 *expire = dhcp_client->expire;
672 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
676 buf[1] = G_DHCPV6_IAADDR;
679 memcpy(&buf[4], &dhcp_client->ia_na, 16);
680 memset(&buf[20], 0, 4); /* preferred */
681 memset(&buf[24], 0, 4); /* valid */
685 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
689 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
691 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
701 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
702 int code, uint32_t *T1, uint32_t *T2,
705 if (code == G_DHCPV6_IA_TA) {
706 uint8_t ia_options[4];
708 put_iaid(dhcp_client, index, ia_options);
710 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
711 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
712 ia_options, sizeof(ia_options));
714 } else if (code == G_DHCPV6_IA_NA) {
716 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
718 if (add_iaaddr == TRUE) {
719 #define IAADDR_LEN (16+4+4)
720 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
722 put_iaid(dhcp_client, index, ia_options);
725 ia_options[4] = *T1 >> 24;
726 ia_options[5] = *T1 >> 16;
727 ia_options[6] = *T1 >> 8;
730 memset(&ia_options[4], 0x00, 4);
733 ia_options[8] = *T2 >> 24;
734 ia_options[9] = *T2 >> 16;
735 ia_options[10] = *T2 >> 8;
736 ia_options[11] = *T2;
738 memset(&ia_options[8], 0x00, 4);
740 create_iaaddr(dhcp_client, &ia_options[12],
743 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
744 ia_options, sizeof(ia_options));
746 uint8_t ia_options[4+4+4];
748 put_iaid(dhcp_client, index, ia_options);
750 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
751 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
753 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
754 ia_options, sizeof(ia_options));
763 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
766 int i, j, len = sizeof(uint16_t) * args;
769 values = g_try_malloc(len);
774 for (i = 0, j = 0; i < args; i++) {
775 uint16_t value = va_arg(va, int);
776 values[j++] = value >> 8;
777 values[j++] = value & 0xff;
781 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
786 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
788 struct dhcpv6_packet *packet;
789 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
793 memset(buf, 0, sizeof(buf));
794 packet = (struct dhcpv6_packet *)&buf[0];
795 ptr = buf + sizeof(struct dhcpv6_packet);
797 debug(dhcp_client, "sending DHCPv6 %s message", msg);
799 init_packet(dhcp_client, packet, type);
801 dhcp_client->xid = packet->transaction_id[0] << 16 |
802 packet->transaction_id[1] << 8 |
803 packet->transaction_id[2];
805 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
807 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
809 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
811 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
813 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
817 static int send_solicitation(GDHCPClient *dhcp_client)
819 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
822 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
824 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
827 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
829 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
832 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
834 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
837 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
839 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
842 static int send_information_req(GDHCPClient *dhcp_client)
844 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
848 static void remove_value(gpointer data, gpointer user_data)
854 static void remove_option_value(gpointer data)
856 GList *option_value = data;
858 g_list_foreach(option_value, remove_value, NULL);
861 GDHCPClient *g_dhcp_client_new(GDHCPType type,
862 int ifindex, GDHCPClientError *error)
864 GDHCPClient *dhcp_client;
867 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
871 dhcp_client = g_try_new0(GDHCPClient, 1);
872 if (dhcp_client == NULL) {
873 *error = G_DHCP_CLIENT_ERROR_NOMEM;
877 dhcp_client->interface = get_interface_name(ifindex);
878 if (dhcp_client->interface == NULL) {
879 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
883 if (interface_is_up(ifindex) == FALSE) {
884 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
888 get_interface_mac_address(ifindex, dhcp_client->mac_address);
890 dhcp_client->listener_sockfd = -1;
891 dhcp_client->listener_channel = NULL;
892 dhcp_client->listen_mode = L_NONE;
893 dhcp_client->ref_count = 1;
894 dhcp_client->type = type;
895 dhcp_client->ifindex = ifindex;
896 dhcp_client->lease_available_cb = NULL;
897 dhcp_client->ipv4ll_available_cb = NULL;
898 dhcp_client->no_lease_cb = NULL;
899 dhcp_client->lease_lost_cb = NULL;
900 dhcp_client->ipv4ll_lost_cb = NULL;
901 dhcp_client->address_conflict_cb = NULL;
902 dhcp_client->listener_watch = 0;
903 dhcp_client->retry_times = 0;
904 dhcp_client->ack_retry_times = 0;
905 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
906 g_direct_equal, NULL, remove_option_value);
907 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
908 g_direct_equal, NULL, g_free);
909 dhcp_client->request_list = NULL;
910 dhcp_client->require_list = NULL;
911 dhcp_client->duid = NULL;
912 dhcp_client->duid_len = 0;
913 dhcp_client->last_renew = dhcp_client->last_rebind = time(NULL);
914 dhcp_client->expire = 0;
916 *error = G_DHCP_CLIENT_ERROR_NONE;
921 g_free(dhcp_client->interface);
926 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
928 static int dhcp_l2_socket(int ifindex)
931 struct sockaddr_ll sock;
936 * I've selected not to see LL header, so BPF doesn't see it, too.
937 * The filter may also pass non-IP and non-ARP packets, but we do
938 * a more complete check when receiving the message in userspace.
940 * and filter shamelessly stolen from:
942 * http://www.flamewarmaster.de/software/dhcpclient/
944 * There are a few other interesting ideas on that page (look under
945 * "Motivation"). Use of netlink events is most interesting. Think
946 * of various network servers listening for events and reconfiguring.
947 * That would obsolete sending HUP signals and/or make use of restarts.
949 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
952 * TODO: make conditional?
954 static const struct sock_filter filter_instr[] = {
956 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
957 /* L5, L1, is UDP? */
958 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
959 /* ugly check for arp on ethernet-like and IPv4 */
960 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
961 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
963 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
964 /* check udp source and destination ports */
965 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
967 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
969 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
970 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
973 static const struct sock_fprog filter_prog = {
974 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
975 /* casting const away: */
976 .filter = (struct sock_filter *) filter_instr,
979 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
983 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
984 /* Use only if standard ports are in use */
985 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
986 sizeof(filter_prog));
988 memset(&sock, 0, sizeof(sock));
989 sock.sll_family = AF_PACKET;
990 sock.sll_protocol = htons(ETH_P_IP);
991 sock.sll_ifindex = ifindex;
993 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1001 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1003 if (packet->ip.protocol != IPPROTO_UDP)
1006 if (packet->ip.version != IPVERSION)
1009 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1012 if (packet->udp.dest != htons(CLIENT_PORT))
1015 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1021 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
1024 struct ip_udp_dhcp_packet packet;
1027 memset(&packet, 0, sizeof(packet));
1029 bytes = read(fd, &packet, sizeof(packet));
1033 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1036 if (bytes < ntohs(packet.ip.tot_len))
1037 /* packet is bigger than sizeof(packet), we did partial read */
1040 /* ignore any extra garbage bytes */
1041 bytes = ntohs(packet.ip.tot_len);
1043 if (sanity_check(&packet, bytes) == FALSE)
1046 check = packet.ip.check;
1047 packet.ip.check = 0;
1048 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1051 /* verify UDP checksum. IP header has to be modified for this */
1052 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1053 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1054 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1055 check = packet.udp.check;
1056 packet.udp.check = 0;
1057 if (check && check != dhcp_checksum(&packet, bytes))
1060 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1061 sizeof(packet.udp)));
1063 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1066 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1069 static void ipv4ll_start(GDHCPClient *dhcp_client)
1074 if (dhcp_client->timeout > 0) {
1075 g_source_remove(dhcp_client->timeout);
1076 dhcp_client->timeout = 0;
1079 switch_listening_mode(dhcp_client, L_NONE);
1080 dhcp_client->type = G_DHCP_IPV4LL;
1081 dhcp_client->retry_times = 0;
1082 dhcp_client->requested_ip = 0;
1084 /*try to start with a based mac address ip*/
1085 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1086 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1088 /*first wait a random delay to avoid storm of arp request on boot*/
1089 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1091 dhcp_client->retry_times++;
1092 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1099 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1102 switch_listening_mode(dhcp_client, L_NONE);
1104 if (dhcp_client->timeout > 0)
1105 g_source_remove(dhcp_client->timeout);
1107 if (dhcp_client->listener_watch > 0) {
1108 g_source_remove(dhcp_client->listener_watch);
1109 dhcp_client->listener_watch = 0;
1112 dhcp_client->state = IPV4LL_PROBE;
1113 dhcp_client->retry_times = 0;
1114 dhcp_client->requested_ip = 0;
1116 g_free(dhcp_client->assigned_ip);
1117 dhcp_client->assigned_ip = NULL;
1120 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1123 struct ether_arp arp;
1124 uint32_t ip_requested;
1125 int source_conflict;
1126 int target_conflict;
1128 memset(&arp, 0, sizeof(arp));
1129 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1133 if (arp.arp_op != htons(ARPOP_REPLY) &&
1134 arp.arp_op != htons(ARPOP_REQUEST))
1137 ip_requested = ntohl(dhcp_client->requested_ip);
1138 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1139 sizeof(ip_requested));
1141 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1142 sizeof(ip_requested));
1144 if (!source_conflict && !target_conflict)
1147 dhcp_client->conflicts++;
1149 debug(dhcp_client, "IPV4LL conflict detected");
1151 if (dhcp_client->state == IPV4LL_MONITOR) {
1152 if (!source_conflict)
1154 dhcp_client->state = IPV4LL_DEFEND;
1155 debug(dhcp_client, "DEFEND mode conflicts : %d",
1156 dhcp_client->conflicts);
1157 /*Try to defend with a single announce*/
1158 send_announce_packet(dhcp_client);
1162 if (dhcp_client->state == IPV4LL_DEFEND) {
1163 if (!source_conflict)
1165 else if (dhcp_client->ipv4ll_lost_cb != NULL)
1166 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1167 dhcp_client->ipv4ll_lost_data);
1170 ipv4ll_stop(dhcp_client);
1172 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1173 /*restart whole state machine*/
1174 dhcp_client->retry_times++;
1175 dhcp_client->timeout =
1176 g_timeout_add_full(G_PRIORITY_HIGH,
1177 ipv4ll_random_delay_ms(PROBE_WAIT),
1182 /* Here we got a lot of conflicts, RFC3927 states that we have
1183 * to wait RATE_LIMIT_INTERVAL before retrying,
1184 * but we just report failure.
1186 else if (dhcp_client->no_lease_cb != NULL)
1187 dhcp_client->no_lease_cb(dhcp_client,
1188 dhcp_client->no_lease_data);
1193 static gboolean check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1195 if (dhcp_client->type == G_DHCP_IPV6) {
1196 struct dhcpv6_packet *packet6 = pkt;
1199 if (packet6 == NULL)
1202 xid = packet6->transaction_id[0] << 16 |
1203 packet6->transaction_id[1] << 8 |
1204 packet6->transaction_id[2];
1206 if (xid != dhcp_client->xid)
1209 struct dhcp_packet *packet = pkt;
1211 if (packet->xid != dhcp_client->xid)
1214 if (packet->hlen != 6)
1217 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1224 static void start_request(GDHCPClient *dhcp_client);
1226 static gboolean request_timeout(gpointer user_data)
1228 GDHCPClient *dhcp_client = user_data;
1230 debug(dhcp_client, "request timeout (retries %d)",
1231 dhcp_client->retry_times);
1233 dhcp_client->retry_times++;
1235 start_request(dhcp_client);
1240 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1241 gpointer user_data);
1243 static int switch_listening_mode(GDHCPClient *dhcp_client,
1244 ListenMode listen_mode)
1246 GIOChannel *listener_channel;
1247 int listener_sockfd;
1249 if (dhcp_client->listen_mode == listen_mode)
1252 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1253 dhcp_client->listen_mode, listen_mode);
1255 if (dhcp_client->listen_mode != L_NONE) {
1256 if (dhcp_client->listener_watch > 0)
1257 g_source_remove(dhcp_client->listener_watch);
1258 dhcp_client->listener_channel = NULL;
1259 dhcp_client->listen_mode = L_NONE;
1260 dhcp_client->listener_sockfd = -1;
1261 dhcp_client->listener_watch = 0;
1264 if (listen_mode == L_NONE)
1267 if (listen_mode == L2)
1268 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1269 else if (listen_mode == L3) {
1270 if (dhcp_client->type == G_DHCP_IPV6)
1271 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1272 dhcp_client->interface,
1275 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1276 dhcp_client->interface,
1278 } else if (listen_mode == L_ARP)
1279 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1283 if (listener_sockfd < 0)
1286 listener_channel = g_io_channel_unix_new(listener_sockfd);
1287 if (listener_channel == NULL) {
1288 /* Failed to create listener channel */
1289 close(listener_sockfd);
1293 dhcp_client->listen_mode = listen_mode;
1294 dhcp_client->listener_sockfd = listener_sockfd;
1295 dhcp_client->listener_channel = listener_channel;
1297 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1298 dhcp_client->listener_watch =
1299 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1300 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1301 listener_event, dhcp_client,
1303 g_io_channel_unref(dhcp_client->listener_channel);
1308 static void start_request(GDHCPClient *dhcp_client)
1310 debug(dhcp_client, "start request (retries %d)",
1311 dhcp_client->retry_times);
1313 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1314 dhcp_client->state = INIT_SELECTING;
1315 ipv4ll_start(dhcp_client);
1320 if (dhcp_client->retry_times == 0) {
1321 dhcp_client->state = REQUESTING;
1322 switch_listening_mode(dhcp_client, L2);
1325 send_select(dhcp_client);
1327 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1334 static uint32_t get_lease(struct dhcp_packet *packet)
1337 uint32_t lease_seconds;
1339 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
1340 if (option_u8 == NULL)
1343 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
1344 lease_seconds = ntohl(lease_seconds);
1345 /* paranoia: must not be prone to overflows */
1346 lease_seconds &= 0x0fffffff;
1347 if (lease_seconds < 10)
1350 return lease_seconds;
1353 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1355 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1357 if (dhcp_client->timeout > 0) {
1358 g_source_remove(dhcp_client->timeout);
1359 dhcp_client->timeout = 0;
1362 dhcp_client->retry_times = retry_times;
1363 dhcp_client->requested_ip = 0;
1364 dhcp_client->state = INIT_SELECTING;
1365 switch_listening_mode(dhcp_client, L2);
1367 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1370 static gboolean start_rebound_timeout(gpointer user_data)
1372 GDHCPClient *dhcp_client = user_data;
1374 debug(dhcp_client, "start rebound timeout");
1376 switch_listening_mode(dhcp_client, L2);
1378 dhcp_client->lease_seconds >>= 1;
1380 /* We need to have enough time to receive ACK package*/
1381 if (dhcp_client->lease_seconds <= 6) {
1383 /* ip need to be cleared */
1384 if (dhcp_client->lease_lost_cb != NULL)
1385 dhcp_client->lease_lost_cb(dhcp_client,
1386 dhcp_client->lease_lost_data);
1388 restart_dhcp(dhcp_client, 0);
1390 send_rebound(dhcp_client);
1392 dhcp_client->timeout =
1393 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1394 dhcp_client->lease_seconds >> 1,
1395 start_rebound_timeout,
1403 static void start_rebound(GDHCPClient *dhcp_client)
1405 debug(dhcp_client, "start rebound");
1407 dhcp_client->state = REBINDING;
1409 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1410 dhcp_client->lease_seconds >> 1,
1411 start_rebound_timeout,
1416 static gboolean start_renew_timeout(gpointer user_data)
1418 GDHCPClient *dhcp_client = user_data;
1420 debug(dhcp_client, "start renew timeout");
1422 dhcp_client->state = RENEWING;
1424 dhcp_client->lease_seconds >>= 1;
1426 switch_listening_mode(dhcp_client, L3);
1427 if (dhcp_client->lease_seconds <= 60)
1428 start_rebound(dhcp_client);
1430 send_renew(dhcp_client);
1432 if (dhcp_client->timeout > 0)
1433 g_source_remove(dhcp_client->timeout);
1435 dhcp_client->timeout =
1436 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1437 dhcp_client->lease_seconds >> 1,
1438 start_renew_timeout,
1446 static void start_bound(GDHCPClient *dhcp_client)
1448 debug(dhcp_client, "start bound");
1450 dhcp_client->state = BOUND;
1452 if (dhcp_client->timeout > 0)
1453 g_source_remove(dhcp_client->timeout);
1455 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1456 dhcp_client->lease_seconds >> 1,
1457 start_renew_timeout, dhcp_client,
1461 static gboolean restart_dhcp_timeout(gpointer user_data)
1463 GDHCPClient *dhcp_client = user_data;
1465 debug(dhcp_client, "restart DHCP timeout");
1467 dhcp_client->ack_retry_times++;
1469 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1474 static char *get_ip(uint32_t ip)
1476 struct in_addr addr;
1480 return g_strdup(inet_ntoa(addr));
1483 /* get a rough idea of how long an option will be */
1484 static const uint8_t len_of_option_as_string[] = {
1485 [OPTION_IP] = sizeof("255.255.255.255 "),
1486 [OPTION_STRING] = 1,
1487 [OPTION_U8] = sizeof("255 "),
1488 [OPTION_U16] = sizeof("65535 "),
1489 [OPTION_U32] = sizeof("4294967295 "),
1492 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1494 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1497 /* Create "opt_value1 option_value2 ..." string */
1498 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1500 unsigned upper_length;
1504 len = option[OPT_LEN - OPT_DATA];
1505 type &= OPTION_TYPE_MASK;
1506 optlen = dhcp_option_lengths[type];
1509 upper_length = len_of_option_as_string[type] *
1510 ((unsigned)len / (unsigned)optlen);
1511 dest = ret = malloc(upper_length + 1);
1515 while (len >= optlen) {
1518 dest += sprint_nip(dest, "", option);
1521 uint16_t val_u16 = dhcp_get_unaligned(
1522 (uint16_t *) option);
1523 dest += sprintf(dest, "%u", ntohs(val_u16));
1527 uint32_t val_u32 = dhcp_get_unaligned(
1528 (uint32_t *) option);
1529 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
1530 "%ld", (unsigned long) ntohl(val_u32));
1534 memcpy(dest, option, len);
1551 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1559 if (type == OPTION_STRING)
1560 return g_list_append(list, g_strdup(value));
1562 while ((pos = strchr(pos, ' ')) != NULL) {
1565 list = g_list_append(list, g_strdup(value));
1570 list = g_list_append(list, g_strdup(value));
1575 static inline uint32_t get_uint32(unsigned char *value)
1577 return value[0] << 24 | value[1] << 16 |
1578 value[2] << 8 | value[3];
1581 static inline uint16_t get_uint16(unsigned char *value)
1583 return value[0] << 8 | value[1];
1586 static GList *get_addresses(GDHCPClient *dhcp_client,
1588 unsigned char *value,
1592 struct in6_addr addr;
1593 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1594 uint16_t option_len, option_code, st = 0, max_len;
1595 int addr_count = 0, i, pos;
1599 if (value == NULL || len < 4)
1602 iaid = get_uint32(&value[0]);
1603 if (dhcp_client->iaid != iaid)
1606 if (code == G_DHCPV6_IA_NA) {
1607 T1 = get_uint32(&value[4]);
1608 T2 = get_uint32(&value[8]);
1611 /* RFC 3315, 22.4 */
1621 max_len = len - pos;
1623 /* We have more sub-options in this packet. */
1625 option = dhcpv6_get_sub_option(&value[pos], max_len,
1626 &option_code, &option_len);
1628 debug(dhcp_client, "pos %d option %p code %d len %d",
1629 pos, option, option_code, option_len);
1637 switch (option_code) {
1638 case G_DHCPV6_IAADDR:
1640 memcpy(&addr, &option[0], sizeof(addr));
1642 preferred = get_uint32(&option[i]);
1644 valid = get_uint32(&option[i]);
1649 case G_DHCPV6_STATUS_CODE:
1650 st = get_uint16(&option[0]);
1651 debug(dhcp_client, "error code %d", st);
1652 if (option_len > 2) {
1653 str = g_strndup((gchar *)&option[2],
1655 debug(dhcp_client, "error text: %s", str);
1663 pos += 2 + 2 + option_len;
1665 } while (option != NULL);
1667 if (addr_count > 0 && st == 0) {
1668 /* We only support one address atm */
1669 char addr_str[INET6_ADDRSTRLEN + 1];
1671 if (preferred > valid)
1672 /* RFC 3315, 22.6 */
1675 dhcp_client->T1 = T1;
1676 dhcp_client->T2 = T2;
1678 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
1679 debug(dhcp_client, "count %d addr %s T1 %u T2 %u",
1680 addr_count, addr_str, T1, T2);
1682 list = g_list_append(list, g_strdup(addr_str));
1684 if (code == G_DHCPV6_IA_NA)
1685 memcpy(&dhcp_client->ia_na, &addr,
1686 sizeof(struct in6_addr));
1688 memcpy(&dhcp_client->ia_ta, &addr,
1689 sizeof(struct in6_addr));
1691 g_dhcpv6_client_set_expire(dhcp_client, valid);
1697 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
1699 unsigned char *value,
1710 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
1711 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
1714 "%s server list length (%d) is invalid",
1715 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
1719 for (i = 0; i < len; i += 16) {
1721 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
1725 if (inet_ntop(AF_INET6, &value[i], str,
1726 INET6_ADDRSTRLEN) == NULL)
1729 list = g_list_append(list, str);
1733 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
1734 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
1735 list = get_addresses(dhcp_client, code, len, value, status);
1745 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
1746 struct dhcpv6_packet *packet,
1747 uint16_t pkt_len, uint16_t *status)
1749 GList *list, *value_list;
1752 uint16_t option_len;
1754 for (list = dhcp_client->request_list; list; list = list->next) {
1755 code = (uint16_t) GPOINTER_TO_INT(list->data);
1757 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
1759 if (option == NULL) {
1760 g_hash_table_remove(dhcp_client->code_value_hash,
1761 GINT_TO_POINTER((int) code));
1765 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
1766 option_len, option, status);
1768 debug(dhcp_client, "code %d %p len %d list %p", code, option,
1769 option_len, value_list);
1771 if (value_list == NULL)
1772 g_hash_table_remove(dhcp_client->code_value_hash,
1773 GINT_TO_POINTER((int) code));
1775 g_hash_table_insert(dhcp_client->code_value_hash,
1776 GINT_TO_POINTER((int) code), value_list);
1780 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
1782 GDHCPOptionType type;
1783 GList *list, *value_list;
1788 for (list = dhcp_client->request_list; list; list = list->next) {
1789 code = (uint8_t) GPOINTER_TO_INT(list->data);
1791 option = dhcp_get_option(packet, code);
1792 if (option == NULL) {
1793 g_hash_table_remove(dhcp_client->code_value_hash,
1794 GINT_TO_POINTER((int) code));
1798 type = dhcp_get_code_type(code);
1800 option_value = malloc_option_value_string(option, type);
1801 if (option_value == NULL)
1802 g_hash_table_remove(dhcp_client->code_value_hash,
1803 GINT_TO_POINTER((int) code));
1805 value_list = get_option_value_list(option_value, type);
1807 g_free(option_value);
1809 if (value_list == NULL)
1810 g_hash_table_remove(dhcp_client->code_value_hash,
1811 GINT_TO_POINTER((int) code));
1813 g_hash_table_insert(dhcp_client->code_value_hash,
1814 GINT_TO_POINTER((int) code), value_list);
1818 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1821 GDHCPClient *dhcp_client = user_data;
1822 struct dhcp_packet packet;
1823 struct dhcpv6_packet *packet6 = NULL;
1824 uint8_t *message_type = NULL, *client_id = NULL, *option_u8,
1826 uint16_t option_len = 0, status = 0;
1828 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
1829 uint16_t pkt_len = 0;
1833 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1834 dhcp_client->listener_watch = 0;
1838 if (dhcp_client->listen_mode == L_NONE)
1843 if (dhcp_client->listen_mode == L2)
1844 re = dhcp_recv_l2_packet(&packet,
1845 dhcp_client->listener_sockfd);
1846 else if (dhcp_client->listen_mode == L3) {
1847 if (dhcp_client->type == G_DHCP_IPV6) {
1848 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
1849 dhcp_client->listener_sockfd);
1853 re = dhcp_recv_l3_packet(&packet,
1854 dhcp_client->listener_sockfd);
1855 } else if (dhcp_client->listen_mode == L_ARP) {
1856 ipv4ll_recv_arp_packet(dhcp_client);
1865 if (check_package_owner(dhcp_client, pkt) == FALSE)
1868 if (dhcp_client->type == G_DHCP_IPV6) {
1869 if (packet6 == NULL)
1873 client_id = dhcpv6_get_option(packet6, pkt_len,
1874 G_DHCPV6_CLIENTID, &option_len, &count);
1876 if (client_id == NULL || count == 0 || option_len == 0 ||
1877 memcmp(dhcp_client->duid, client_id,
1878 dhcp_client->duid_len) != 0) {
1880 "client duid error, discarding msg %p/%d/%d",
1881 client_id, option_len, count);
1885 option_u8 = dhcpv6_get_option(packet6, pkt_len,
1886 G_DHCPV6_STATUS_CODE, &option_len, NULL);
1887 if (option_u8 != 0 && option_len > 0) {
1888 status = option_u8[0]<<8 | option_u8[1];
1890 debug(dhcp_client, "error code %d", status);
1891 if (option_len > 2) {
1892 gchar *txt = g_strndup(
1893 (gchar *)&option_u8[2],
1895 debug(dhcp_client, "error text: %s",
1900 dhcp_client->status_code = status;
1902 dhcp_client->status_code = 0;
1905 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1906 if (message_type == NULL)
1910 if (message_type == NULL && client_id == NULL)
1911 /* No message type / client id option, ignore package */
1914 debug(dhcp_client, "received DHCP packet (current state %d)",
1915 dhcp_client->state);
1917 switch (dhcp_client->state) {
1918 case INIT_SELECTING:
1919 if (*message_type != DHCPOFFER)
1922 g_source_remove(dhcp_client->timeout);
1923 dhcp_client->timeout = 0;
1924 dhcp_client->retry_times = 0;
1926 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
1927 dhcp_client->server_ip =
1928 dhcp_get_unaligned((uint32_t *) option_u8);
1929 dhcp_client->requested_ip = packet.yiaddr;
1931 dhcp_client->state = REQUESTING;
1933 start_request(dhcp_client);
1939 if (*message_type == DHCPACK) {
1940 dhcp_client->retry_times = 0;
1942 if (dhcp_client->timeout > 0)
1943 g_source_remove(dhcp_client->timeout);
1944 dhcp_client->timeout = 0;
1946 dhcp_client->lease_seconds = get_lease(&packet);
1948 get_request(dhcp_client, &packet);
1950 switch_listening_mode(dhcp_client, L_NONE);
1952 g_free(dhcp_client->assigned_ip);
1953 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1955 /* Address should be set up here */
1956 if (dhcp_client->lease_available_cb != NULL)
1957 dhcp_client->lease_available_cb(dhcp_client,
1958 dhcp_client->lease_available_data);
1960 start_bound(dhcp_client);
1961 } else if (*message_type == DHCPNAK) {
1962 dhcp_client->retry_times = 0;
1964 if (dhcp_client->timeout > 0)
1965 g_source_remove(dhcp_client->timeout);
1967 dhcp_client->timeout = g_timeout_add_seconds_full(
1969 restart_dhcp_timeout,
1976 if (dhcp_client->type != G_DHCP_IPV6)
1979 if (packet6->message != DHCPV6_REPLY &&
1980 packet6->message != DHCPV6_ADVERTISE)
1984 server_id = dhcpv6_get_option(packet6, pkt_len,
1985 G_DHCPV6_SERVERID, &option_len, &count);
1986 if (server_id == NULL || count != 1 || option_len == 0) {
1987 /* RFC 3315, 15.10 */
1989 "server duid error, discarding msg %p/%d/%d",
1990 server_id, option_len, count);
1993 dhcp_client->server_duid = g_try_malloc(option_len);
1994 if (dhcp_client->server_duid == NULL)
1996 memcpy(dhcp_client->server_duid, server_id, option_len);
1997 dhcp_client->server_duid_len = option_len;
1999 if (packet6->message == DHCPV6_REPLY) {
2000 uint8_t *rapid_commit;
2003 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2004 G_DHCPV6_RAPID_COMMIT,
2005 &option_len, &count);
2006 if (rapid_commit == NULL || option_len == 0 ||
2008 /* RFC 3315, 17.1.4 */
2012 switch_listening_mode(dhcp_client, L_NONE);
2014 if (dhcp_client->status_code == 0)
2015 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2016 &dhcp_client->status_code);
2018 if (packet6->message == DHCPV6_ADVERTISE) {
2019 if (dhcp_client->advertise_cb != NULL)
2020 dhcp_client->advertise_cb(dhcp_client,
2021 dhcp_client->advertise_data);
2025 if (dhcp_client->solicitation_cb != NULL) {
2027 * The dhcp_client might not be valid after the
2028 * callback call so just return immediately.
2030 dhcp_client->solicitation_cb(dhcp_client,
2031 dhcp_client->solicitation_data);
2035 case INFORMATION_REQ:
2040 if (dhcp_client->type != G_DHCP_IPV6)
2043 if (packet6->message != DHCPV6_REPLY)
2048 server_id = dhcpv6_get_option(packet6, pkt_len,
2049 G_DHCPV6_SERVERID, &option_len, &count);
2050 if (server_id == NULL || count != 1 || option_len == 0 ||
2051 (dhcp_client->server_duid_len > 0 &&
2052 memcmp(dhcp_client->server_duid, server_id,
2053 dhcp_client->server_duid_len) != 0)) {
2054 /* RFC 3315, 15.10 */
2056 "server duid error, discarding msg %p/%d/%d",
2057 server_id, option_len, count);
2061 switch_listening_mode(dhcp_client, L_NONE);
2063 dhcp_client->status_code = 0;
2064 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2065 &dhcp_client->status_code);
2067 if (dhcp_client->information_req_cb != NULL) {
2069 * The dhcp_client might not be valid after the
2070 * callback call so just return immediately.
2072 dhcp_client->information_req_cb(dhcp_client,
2073 dhcp_client->information_req_data);
2076 if (dhcp_client->request_cb != NULL) {
2077 dhcp_client->request_cb(dhcp_client,
2078 dhcp_client->request_data);
2081 if (dhcp_client->renew_cb != NULL) {
2082 dhcp_client->renew_cb(dhcp_client,
2083 dhcp_client->renew_data);
2086 if (dhcp_client->rebind_cb != NULL) {
2087 dhcp_client->rebind_cb(dhcp_client,
2088 dhcp_client->rebind_data);
2091 if (dhcp_client->release_cb != NULL) {
2092 dhcp_client->release_cb(dhcp_client,
2093 dhcp_client->release_data);
2101 debug(dhcp_client, "processed DHCP packet (new state %d)",
2102 dhcp_client->state);
2107 static gboolean discover_timeout(gpointer user_data)
2109 GDHCPClient *dhcp_client = user_data;
2111 dhcp_client->retry_times++;
2114 * We do not send the REQUESTED IP option if we are retrying because
2115 * if the server is non-authoritative it will ignore the request if the
2116 * option is present.
2118 g_dhcp_client_start(dhcp_client, NULL);
2123 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2125 GDHCPClient *dhcp_client = dhcp_data;
2127 debug(dhcp_client, "back to MONITOR mode");
2129 dhcp_client->conflicts = 0;
2130 dhcp_client->state = IPV4LL_MONITOR;
2135 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2137 GDHCPClient *dhcp_client = dhcp_data;
2140 debug(dhcp_client, "request timeout (retries %d)",
2141 dhcp_client->retry_times);
2143 if (dhcp_client->retry_times != ANNOUNCE_NUM){
2144 dhcp_client->retry_times++;
2145 send_announce_packet(dhcp_client);
2149 ip = htonl(dhcp_client->requested_ip);
2150 debug(dhcp_client, "switching to monitor mode");
2151 dhcp_client->state = IPV4LL_MONITOR;
2152 dhcp_client->assigned_ip = get_ip(ip);
2154 if (dhcp_client->ipv4ll_available_cb != NULL)
2155 dhcp_client->ipv4ll_available_cb(dhcp_client,
2156 dhcp_client->ipv4ll_available_data);
2157 dhcp_client->conflicts = 0;
2162 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2165 GDHCPClient *dhcp_client = dhcp_data;
2167 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2168 dhcp_client->retry_times);
2170 if (dhcp_client->retry_times == PROBE_NUM) {
2171 dhcp_client->state = IPV4LL_ANNOUNCE;
2172 dhcp_client->retry_times = 0;
2174 dhcp_client->retry_times++;
2175 send_announce_packet(dhcp_client);
2178 dhcp_client->retry_times++;
2179 send_probe_packet(dhcp_client);
2184 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2189 if (dhcp_client->type == G_DHCP_IPV6) {
2190 if (dhcp_client->information_req_cb) {
2191 dhcp_client->state = INFORMATION_REQ;
2192 re = switch_listening_mode(dhcp_client, L3);
2194 switch_listening_mode(dhcp_client, L_NONE);
2195 dhcp_client->state = 0;
2198 send_information_req(dhcp_client);
2200 } else if (dhcp_client->solicitation_cb) {
2201 dhcp_client->state = SOLICITATION;
2202 re = switch_listening_mode(dhcp_client, L3);
2204 switch_listening_mode(dhcp_client, L_NONE);
2205 dhcp_client->state = 0;
2208 send_solicitation(dhcp_client);
2210 } else if (dhcp_client->request_cb) {
2211 dhcp_client->state = REQUEST;
2212 re = switch_listening_mode(dhcp_client, L3);
2214 switch_listening_mode(dhcp_client, L_NONE);
2215 dhcp_client->state = 0;
2218 send_dhcpv6_request(dhcp_client);
2220 } else if (dhcp_client->renew_cb) {
2221 dhcp_client->state = RENEW;
2222 re = switch_listening_mode(dhcp_client, L3);
2224 switch_listening_mode(dhcp_client, L_NONE);
2225 dhcp_client->state = 0;
2228 send_dhcpv6_renew(dhcp_client);
2230 } else if (dhcp_client->rebind_cb) {
2231 dhcp_client->state = REBIND;
2232 re = switch_listening_mode(dhcp_client, L3);
2234 switch_listening_mode(dhcp_client, L_NONE);
2235 dhcp_client->state = 0;
2238 send_dhcpv6_rebind(dhcp_client);
2240 } else if (dhcp_client->release_cb) {
2241 dhcp_client->state = RENEW;
2242 re = switch_listening_mode(dhcp_client, L3);
2244 switch_listening_mode(dhcp_client, L_NONE);
2245 dhcp_client->state = 0;
2248 send_dhcpv6_release(dhcp_client);
2254 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2255 ipv4ll_start(dhcp_client);
2259 if (dhcp_client->retry_times == 0) {
2260 g_free(dhcp_client->assigned_ip);
2261 dhcp_client->assigned_ip = NULL;
2263 dhcp_client->state = INIT_SELECTING;
2264 re = switch_listening_mode(dhcp_client, L2);
2268 dhcp_client->xid = rand();
2269 dhcp_client->start = time(NULL);
2272 if (last_address == NULL) {
2275 addr = inet_addr(last_address);
2276 if (addr == 0xFFFFFFFF) {
2279 g_free(dhcp_client->last_address);
2280 dhcp_client->last_address = g_strdup(last_address);
2283 send_discover(dhcp_client, addr);
2285 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2293 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2295 switch_listening_mode(dhcp_client, L_NONE);
2297 if (dhcp_client->state == BOUND ||
2298 dhcp_client->state == RENEWING ||
2299 dhcp_client->state == REBINDING)
2300 send_release(dhcp_client, dhcp_client->server_ip,
2301 dhcp_client->requested_ip);
2303 if (dhcp_client->timeout > 0) {
2304 g_source_remove(dhcp_client->timeout);
2305 dhcp_client->timeout = 0;
2308 if (dhcp_client->listener_watch > 0) {
2309 g_source_remove(dhcp_client->listener_watch);
2310 dhcp_client->listener_watch = 0;
2313 dhcp_client->listener_channel = NULL;
2315 dhcp_client->retry_times = 0;
2316 dhcp_client->ack_retry_times = 0;
2318 dhcp_client->requested_ip = 0;
2319 dhcp_client->state = RELEASED;
2320 dhcp_client->lease_seconds = 0;
2323 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2324 unsigned char option_code)
2326 return g_hash_table_lookup(dhcp_client->code_value_hash,
2327 GINT_TO_POINTER((int) option_code));
2330 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2331 GDHCPClientEvent event,
2332 GDHCPClientEventFunc func,
2336 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2337 dhcp_client->lease_available_cb = func;
2338 dhcp_client->lease_available_data = data;
2340 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2341 if (dhcp_client->type == G_DHCP_IPV6)
2343 dhcp_client->ipv4ll_available_cb = func;
2344 dhcp_client->ipv4ll_available_data = data;
2346 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2347 dhcp_client->no_lease_cb = func;
2348 dhcp_client->no_lease_data = data;
2350 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2351 dhcp_client->lease_lost_cb = func;
2352 dhcp_client->lease_lost_data = data;
2354 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2355 if (dhcp_client->type == G_DHCP_IPV6)
2357 dhcp_client->ipv4ll_lost_cb = func;
2358 dhcp_client->ipv4ll_lost_data = data;
2360 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2361 dhcp_client->address_conflict_cb = func;
2362 dhcp_client->address_conflict_data = data;
2364 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2365 if (dhcp_client->type == G_DHCP_IPV4)
2367 dhcp_client->information_req_cb = func;
2368 dhcp_client->information_req_data = data;
2370 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2371 if (dhcp_client->type == G_DHCP_IPV4)
2373 dhcp_client->solicitation_cb = func;
2374 dhcp_client->solicitation_data = data;
2376 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2377 if (dhcp_client->type == G_DHCP_IPV4)
2379 dhcp_client->advertise_cb = func;
2380 dhcp_client->advertise_data = data;
2382 case G_DHCP_CLIENT_EVENT_REQUEST:
2383 if (dhcp_client->type == G_DHCP_IPV4)
2385 dhcp_client->request_cb = func;
2386 dhcp_client->request_data = data;
2388 case G_DHCP_CLIENT_EVENT_RENEW:
2389 if (dhcp_client->type == G_DHCP_IPV4)
2391 dhcp_client->renew_cb = func;
2392 dhcp_client->renew_data = data;
2394 case G_DHCP_CLIENT_EVENT_REBIND:
2395 if (dhcp_client->type == G_DHCP_IPV4)
2397 dhcp_client->rebind_cb = func;
2398 dhcp_client->rebind_data = data;
2400 case G_DHCP_CLIENT_EVENT_RELEASE:
2401 if (dhcp_client->type == G_DHCP_IPV4)
2403 dhcp_client->release_cb = func;
2404 dhcp_client->release_data = data;
2409 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2411 return dhcp_client->ifindex;
2414 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2416 return g_strdup(dhcp_client->assigned_ip);
2419 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2421 GList *option = NULL;
2423 if (dhcp_client->type == G_DHCP_IPV6)
2426 switch (dhcp_client->state) {
2428 case IPV4LL_MONITOR:
2429 return g_strdup("255.255.0.0");
2433 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2435 return g_strdup(option->data);
2436 case INIT_SELECTING:
2440 case IPV4LL_ANNOUNCE:
2441 case INFORMATION_REQ:
2452 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2453 unsigned int option_code)
2455 if (g_list_find(dhcp_client->request_list,
2456 GINT_TO_POINTER((int) option_code)) == NULL)
2457 dhcp_client->request_list = g_list_prepend(
2458 dhcp_client->request_list,
2459 (GINT_TO_POINTER((int) option_code)));
2461 return G_DHCP_CLIENT_ERROR_NONE;
2464 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2466 g_list_free(dhcp_client->request_list);
2467 dhcp_client->request_list = NULL;
2470 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2472 g_hash_table_remove_all(dhcp_client->send_value_hash);
2475 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
2479 storage = g_try_malloc(size + OPT_DATA);
2480 if (storage == NULL)
2483 storage[OPT_CODE] = code;
2484 storage[OPT_LEN] = size;
2485 memcpy(&storage[OPT_DATA], data, size);
2490 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data, unsigned size)
2492 return alloc_dhcp_option(code, data, MIN(size, 255));
2495 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
2497 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
2500 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
2502 const unsigned maclen = 6;
2503 const unsigned idlen = maclen + 1;
2504 const uint8_t option_code = G_DHCP_CLIENT_ID;
2505 uint8_t idbuf[idlen];
2506 uint8_t *data_option;
2508 idbuf[0] = ARPHRD_ETHER;
2510 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
2512 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
2513 if (data_option == NULL)
2514 return G_DHCP_CLIENT_ERROR_NOMEM;
2516 g_hash_table_insert(dhcp_client->send_value_hash,
2517 GINT_TO_POINTER((int) option_code), data_option);
2519 return G_DHCP_CLIENT_ERROR_NONE;
2522 /* Now only support send hostname */
2523 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
2524 unsigned char option_code, const char *option_value)
2526 uint8_t *binary_option;
2528 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
2529 binary_option = alloc_dhcp_string_option(option_code,
2531 if (binary_option == NULL)
2532 return G_DHCP_CLIENT_ERROR_NOMEM;
2534 g_hash_table_insert(dhcp_client->send_value_hash,
2535 GINT_TO_POINTER((int) option_code), binary_option);
2538 return G_DHCP_CLIENT_ERROR_NONE;
2541 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
2546 storage = g_malloc(2 + 2 + len);
2547 if (storage == NULL)
2550 storage[0] = code >> 8;
2551 storage[1] = code & 0xff;
2552 storage[2] = len >> 8;
2553 storage[3] = len & 0xff;
2554 memcpy(storage + 2 + 2, option, len);
2559 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
2560 uint16_t option_code,
2561 uint8_t *option_value,
2562 uint16_t option_len)
2564 if (option_value != NULL) {
2565 uint8_t *binary_option;
2567 debug(dhcp_client, "setting option %d to %p len %d",
2568 option_code, option_value, option_len);
2570 binary_option = alloc_dhcpv6_option(option_code, option_value,
2572 if (binary_option != NULL)
2573 g_hash_table_insert(dhcp_client->send_value_hash,
2574 GINT_TO_POINTER((int) option_code),
2579 void g_dhcpv6_client_reset_renew(GDHCPClient *dhcp_client)
2581 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2584 dhcp_client->last_renew = time(NULL);
2587 void g_dhcpv6_client_reset_rebind(GDHCPClient *dhcp_client)
2589 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2592 dhcp_client->last_rebind = time(NULL);
2595 void g_dhcpv6_client_set_expire(GDHCPClient *dhcp_client, uint32_t timeout)
2597 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2600 dhcp_client->expire = time(NULL) + timeout;
2603 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
2605 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2608 return dhcp_client->status_code;
2611 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
2613 if (dhcp_client == NULL)
2616 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
2621 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
2623 if (dhcp_client == NULL)
2626 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
2629 g_dhcp_client_stop(dhcp_client);
2631 g_free(dhcp_client->interface);
2632 g_free(dhcp_client->assigned_ip);
2633 g_free(dhcp_client->last_address);
2634 g_free(dhcp_client->duid);
2635 g_free(dhcp_client->server_duid);
2637 g_list_free(dhcp_client->request_list);
2638 g_list_free(dhcp_client->require_list);
2640 g_hash_table_destroy(dhcp_client->code_value_hash);
2641 g_hash_table_destroy(dhcp_client->send_value_hash);
2643 g_free(dhcp_client);
2646 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
2647 GDHCPDebugFunc func, gpointer user_data)
2649 if (dhcp_client == NULL)
2652 dhcp_client->debug_func = func;
2653 dhcp_client->debug_data = user_data;