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_option_uint32(&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_option_uint16(&packet, DHCP_MAX_SIZE, 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_option_uint32(&packet, DHCP_REQUESTED_IP,
393 dhcp_client->requested_ip);
394 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
395 dhcp_client->server_ip);
397 add_request_options(dhcp_client, &packet);
399 add_send_options(dhcp_client, &packet);
401 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
402 INADDR_BROADCAST, SERVER_PORT,
403 MAC_BCAST_ADDR, dhcp_client->ifindex);
406 static int send_renew(GDHCPClient *dhcp_client)
408 struct dhcp_packet packet;
410 debug(dhcp_client, "sending DHCP renew request");
412 init_packet(dhcp_client , &packet, DHCPREQUEST);
413 packet.xid = dhcp_client->xid;
414 packet.ciaddr = htonl(dhcp_client->requested_ip);
416 add_request_options(dhcp_client, &packet);
418 add_send_options(dhcp_client, &packet);
420 return dhcp_send_kernel_packet(&packet,
421 dhcp_client->requested_ip, CLIENT_PORT,
422 dhcp_client->server_ip, SERVER_PORT);
425 static int send_rebound(GDHCPClient *dhcp_client)
427 struct dhcp_packet packet;
429 debug(dhcp_client, "sending DHCP rebound request");
431 init_packet(dhcp_client , &packet, DHCPREQUEST);
432 packet.xid = dhcp_client->xid;
433 packet.ciaddr = htonl(dhcp_client->requested_ip);
435 add_request_options(dhcp_client, &packet);
437 add_send_options(dhcp_client, &packet);
439 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
440 INADDR_BROADCAST, SERVER_PORT,
441 MAC_BCAST_ADDR, dhcp_client->ifindex);
444 static int send_release(GDHCPClient *dhcp_client,
445 uint32_t server, uint32_t ciaddr)
447 struct dhcp_packet packet;
449 debug(dhcp_client, "sending DHCP release request");
451 init_packet(dhcp_client, &packet, DHCPRELEASE);
453 packet.ciaddr = htonl(ciaddr);
455 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
457 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
458 server, SERVER_PORT);
461 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
462 static int switch_listening_mode(GDHCPClient *dhcp_client,
463 ListenMode listen_mode);
465 static gboolean send_probe_packet(gpointer dhcp_data)
467 GDHCPClient *dhcp_client;
470 dhcp_client = dhcp_data;
471 /* if requested_ip is not valid, pick a new address*/
472 if (dhcp_client->requested_ip == 0) {
473 debug(dhcp_client, "pick a new random address");
474 dhcp_client->requested_ip = ipv4ll_random_ip(0);
477 debug(dhcp_client, "sending IPV4LL probe request");
479 if (dhcp_client->retry_times == 1) {
480 dhcp_client->state = IPV4LL_PROBE;
481 switch_listening_mode(dhcp_client, L_ARP);
483 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
484 dhcp_client->requested_ip, dhcp_client->ifindex);
486 if (dhcp_client->retry_times < PROBE_NUM) {
487 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
488 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
489 timeout += PROBE_MIN*1000;
491 timeout = (ANNOUNCE_WAIT * 1000);
493 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
495 ipv4ll_probe_timeout,
501 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
502 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
504 static gboolean send_announce_packet(gpointer dhcp_data)
506 GDHCPClient *dhcp_client;
508 dhcp_client = dhcp_data;
510 debug(dhcp_client, "sending IPV4LL announce request");
512 ipv4ll_send_arp_packet(dhcp_client->mac_address,
513 dhcp_client->requested_ip,
514 dhcp_client->requested_ip,
515 dhcp_client->ifindex);
517 if (dhcp_client->timeout > 0)
518 g_source_remove(dhcp_client->timeout);
519 dhcp_client->timeout = 0;
521 if (dhcp_client->state == IPV4LL_DEFEND) {
522 dhcp_client->timeout =
523 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
525 ipv4ll_defend_timeout,
530 dhcp_client->timeout =
531 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
533 ipv4ll_announce_timeout,
539 static void get_interface_mac_address(int index, uint8_t *mac_address)
544 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
546 perror("Open socket error");
550 memset(&ifr, 0, sizeof(ifr));
551 ifr.ifr_ifindex = index;
553 err = ioctl(sk, SIOCGIFNAME, &ifr);
555 perror("Get interface name error");
559 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
561 perror("Get mac address error");
565 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
571 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
572 unsigned char **duid, int *duid_len)
577 case G_DHCPV6_DUID_LLT:
578 *duid_len = 2 + 2 + 4 + ETH_ALEN;
579 *duid = g_try_malloc(*duid_len);
585 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
588 duid_time = time(NULL) - DUID_TIME_EPOCH;
589 (*duid)[4] = duid_time >> 24;
590 (*duid)[5] = duid_time >> 16;
591 (*duid)[6] = duid_time >> 8;
592 (*duid)[7] = duid_time & 0xff;
594 case G_DHCPV6_DUID_EN:
596 case G_DHCPV6_DUID_LL:
597 *duid_len = 2 + 2 + ETH_ALEN;
598 *duid = g_try_malloc(*duid_len);
604 get_interface_mac_address(index, &(*duid)[2 + 2]);
613 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
616 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
619 g_free(dhcp_client->duid);
621 dhcp_client->duid = duid;
622 dhcp_client->duid_len = duid_len;
627 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
629 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
632 return dhcp_client->iaid;
635 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
640 get_interface_mac_address(index, buf);
642 memcpy(iaid, &buf[2], 4);
643 dhcp_client->iaid = iaid[0] << 24 |
644 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
647 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
648 uint32_t *T1, uint32_t *T2,
649 time_t *last_renew, time_t *last_rebind,
652 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
656 *T1 = dhcp_client->T1;
659 *T2 = dhcp_client->T2;
661 if (last_renew != NULL)
662 *last_renew = dhcp_client->last_renew;
664 if (last_rebind != NULL)
665 *last_rebind = dhcp_client->last_rebind;
668 *expire = dhcp_client->expire;
673 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
677 buf[1] = G_DHCPV6_IAADDR;
680 memcpy(&buf[4], &dhcp_client->ia_na, 16);
681 memset(&buf[20], 0, 4); /* preferred */
682 memset(&buf[24], 0, 4); /* valid */
686 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
690 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
692 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
702 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
703 int code, uint32_t *T1, uint32_t *T2,
706 if (code == G_DHCPV6_IA_TA) {
707 uint8_t ia_options[4];
709 put_iaid(dhcp_client, index, ia_options);
711 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
712 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
713 ia_options, sizeof(ia_options));
715 } else if (code == G_DHCPV6_IA_NA) {
717 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
719 if (add_iaaddr == TRUE) {
720 #define IAADDR_LEN (16+4+4)
721 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
723 put_iaid(dhcp_client, index, ia_options);
726 ia_options[4] = *T1 >> 24;
727 ia_options[5] = *T1 >> 16;
728 ia_options[6] = *T1 >> 8;
731 memset(&ia_options[4], 0x00, 4);
734 ia_options[8] = *T2 >> 24;
735 ia_options[9] = *T2 >> 16;
736 ia_options[10] = *T2 >> 8;
737 ia_options[11] = *T2;
739 memset(&ia_options[8], 0x00, 4);
741 create_iaaddr(dhcp_client, &ia_options[12],
744 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
745 ia_options, sizeof(ia_options));
747 uint8_t ia_options[4+4+4];
749 put_iaid(dhcp_client, index, ia_options);
751 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
752 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
754 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
755 ia_options, sizeof(ia_options));
764 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
767 int i, j, len = sizeof(uint16_t) * args;
770 values = g_try_malloc(len);
775 for (i = 0, j = 0; i < args; i++) {
776 uint16_t value = va_arg(va, int);
777 values[j++] = value >> 8;
778 values[j++] = value & 0xff;
782 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
789 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
791 struct dhcpv6_packet *packet;
792 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
796 memset(buf, 0, sizeof(buf));
797 packet = (struct dhcpv6_packet *)&buf[0];
798 ptr = buf + sizeof(struct dhcpv6_packet);
800 debug(dhcp_client, "sending DHCPv6 %s message", msg);
802 init_packet(dhcp_client, packet, type);
804 dhcp_client->xid = packet->transaction_id[0] << 16 |
805 packet->transaction_id[1] << 8 |
806 packet->transaction_id[2];
808 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
810 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
812 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
814 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
816 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
820 static int send_solicitation(GDHCPClient *dhcp_client)
822 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
825 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
827 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
830 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
832 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
835 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
837 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
840 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
842 return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
845 static int send_information_req(GDHCPClient *dhcp_client)
847 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
851 static void remove_value(gpointer data, gpointer user_data)
857 static void remove_option_value(gpointer data)
859 GList *option_value = data;
861 g_list_foreach(option_value, remove_value, NULL);
864 GDHCPClient *g_dhcp_client_new(GDHCPType type,
865 int ifindex, GDHCPClientError *error)
867 GDHCPClient *dhcp_client;
870 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
874 dhcp_client = g_try_new0(GDHCPClient, 1);
875 if (dhcp_client == NULL) {
876 *error = G_DHCP_CLIENT_ERROR_NOMEM;
880 dhcp_client->interface = get_interface_name(ifindex);
881 if (dhcp_client->interface == NULL) {
882 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
886 if (interface_is_up(ifindex) == FALSE) {
887 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
891 get_interface_mac_address(ifindex, dhcp_client->mac_address);
893 dhcp_client->listener_sockfd = -1;
894 dhcp_client->listener_channel = NULL;
895 dhcp_client->listen_mode = L_NONE;
896 dhcp_client->ref_count = 1;
897 dhcp_client->type = type;
898 dhcp_client->ifindex = ifindex;
899 dhcp_client->lease_available_cb = NULL;
900 dhcp_client->ipv4ll_available_cb = NULL;
901 dhcp_client->no_lease_cb = NULL;
902 dhcp_client->lease_lost_cb = NULL;
903 dhcp_client->ipv4ll_lost_cb = NULL;
904 dhcp_client->address_conflict_cb = NULL;
905 dhcp_client->listener_watch = 0;
906 dhcp_client->retry_times = 0;
907 dhcp_client->ack_retry_times = 0;
908 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
909 g_direct_equal, NULL, remove_option_value);
910 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
911 g_direct_equal, NULL, g_free);
912 dhcp_client->request_list = NULL;
913 dhcp_client->require_list = NULL;
914 dhcp_client->duid = NULL;
915 dhcp_client->duid_len = 0;
916 dhcp_client->last_renew = dhcp_client->last_rebind = time(NULL);
917 dhcp_client->expire = 0;
919 *error = G_DHCP_CLIENT_ERROR_NONE;
924 g_free(dhcp_client->interface);
929 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
931 static int dhcp_l2_socket(int ifindex)
934 struct sockaddr_ll sock;
939 * I've selected not to see LL header, so BPF doesn't see it, too.
940 * The filter may also pass non-IP and non-ARP packets, but we do
941 * a more complete check when receiving the message in userspace.
943 * and filter shamelessly stolen from:
945 * http://www.flamewarmaster.de/software/dhcpclient/
947 * There are a few other interesting ideas on that page (look under
948 * "Motivation"). Use of netlink events is most interesting. Think
949 * of various network servers listening for events and reconfiguring.
950 * That would obsolete sending HUP signals and/or make use of restarts.
952 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
955 * TODO: make conditional?
957 static const struct sock_filter filter_instr[] = {
959 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
960 /* L5, L1, is UDP? */
961 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
962 /* ugly check for arp on ethernet-like and IPv4 */
963 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
964 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
966 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
967 /* check udp source and destination ports */
968 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
970 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
972 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
973 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
976 static const struct sock_fprog filter_prog = {
977 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
978 /* casting const away: */
979 .filter = (struct sock_filter *) filter_instr,
982 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
986 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
987 /* Use only if standard ports are in use */
988 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
989 sizeof(filter_prog));
991 memset(&sock, 0, sizeof(sock));
992 sock.sll_family = AF_PACKET;
993 sock.sll_protocol = htons(ETH_P_IP);
994 sock.sll_ifindex = ifindex;
996 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1004 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1006 if (packet->ip.protocol != IPPROTO_UDP)
1009 if (packet->ip.version != IPVERSION)
1012 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1015 if (packet->udp.dest != htons(CLIENT_PORT))
1018 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1024 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
1027 struct ip_udp_dhcp_packet packet;
1030 memset(&packet, 0, sizeof(packet));
1032 bytes = read(fd, &packet, sizeof(packet));
1036 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1039 if (bytes < ntohs(packet.ip.tot_len))
1040 /* packet is bigger than sizeof(packet), we did partial read */
1043 /* ignore any extra garbage bytes */
1044 bytes = ntohs(packet.ip.tot_len);
1046 if (sanity_check(&packet, bytes) == FALSE)
1049 check = packet.ip.check;
1050 packet.ip.check = 0;
1051 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1054 /* verify UDP checksum. IP header has to be modified for this */
1055 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1056 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1057 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1058 check = packet.udp.check;
1059 packet.udp.check = 0;
1060 if (check && check != dhcp_checksum(&packet, bytes))
1063 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1064 sizeof(packet.udp)));
1066 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1069 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1072 static void ipv4ll_start(GDHCPClient *dhcp_client)
1077 if (dhcp_client->timeout > 0) {
1078 g_source_remove(dhcp_client->timeout);
1079 dhcp_client->timeout = 0;
1082 switch_listening_mode(dhcp_client, L_NONE);
1083 dhcp_client->type = G_DHCP_IPV4LL;
1084 dhcp_client->retry_times = 0;
1085 dhcp_client->requested_ip = 0;
1087 /*try to start with a based mac address ip*/
1088 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1089 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1091 /*first wait a random delay to avoid storm of arp request on boot*/
1092 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1094 dhcp_client->retry_times++;
1095 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1102 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1105 switch_listening_mode(dhcp_client, L_NONE);
1107 if (dhcp_client->timeout > 0)
1108 g_source_remove(dhcp_client->timeout);
1110 if (dhcp_client->listener_watch > 0) {
1111 g_source_remove(dhcp_client->listener_watch);
1112 dhcp_client->listener_watch = 0;
1115 dhcp_client->state = IPV4LL_PROBE;
1116 dhcp_client->retry_times = 0;
1117 dhcp_client->requested_ip = 0;
1119 g_free(dhcp_client->assigned_ip);
1120 dhcp_client->assigned_ip = NULL;
1123 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1126 struct ether_arp arp;
1127 uint32_t ip_requested;
1128 int source_conflict;
1129 int target_conflict;
1131 memset(&arp, 0, sizeof(arp));
1132 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1136 if (arp.arp_op != htons(ARPOP_REPLY) &&
1137 arp.arp_op != htons(ARPOP_REQUEST))
1140 ip_requested = htonl(dhcp_client->requested_ip);
1141 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1142 sizeof(ip_requested));
1144 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1145 sizeof(ip_requested));
1147 if (!source_conflict && !target_conflict)
1150 dhcp_client->conflicts++;
1152 debug(dhcp_client, "IPV4LL conflict detected");
1154 if (dhcp_client->state == IPV4LL_MONITOR) {
1155 if (!source_conflict)
1157 dhcp_client->state = IPV4LL_DEFEND;
1158 debug(dhcp_client, "DEFEND mode conflicts : %d",
1159 dhcp_client->conflicts);
1160 /*Try to defend with a single announce*/
1161 send_announce_packet(dhcp_client);
1165 if (dhcp_client->state == IPV4LL_DEFEND) {
1166 if (!source_conflict)
1168 else if (dhcp_client->ipv4ll_lost_cb != NULL)
1169 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1170 dhcp_client->ipv4ll_lost_data);
1173 ipv4ll_stop(dhcp_client);
1175 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1176 /*restart whole state machine*/
1177 dhcp_client->retry_times++;
1178 dhcp_client->timeout =
1179 g_timeout_add_full(G_PRIORITY_HIGH,
1180 ipv4ll_random_delay_ms(PROBE_WAIT),
1185 /* Here we got a lot of conflicts, RFC3927 states that we have
1186 * to wait RATE_LIMIT_INTERVAL before retrying,
1187 * but we just report failure.
1189 else if (dhcp_client->no_lease_cb != NULL)
1190 dhcp_client->no_lease_cb(dhcp_client,
1191 dhcp_client->no_lease_data);
1196 static gboolean check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1198 if (dhcp_client->type == G_DHCP_IPV6) {
1199 struct dhcpv6_packet *packet6 = pkt;
1202 if (packet6 == NULL)
1205 xid = packet6->transaction_id[0] << 16 |
1206 packet6->transaction_id[1] << 8 |
1207 packet6->transaction_id[2];
1209 if (xid != dhcp_client->xid)
1212 struct dhcp_packet *packet = pkt;
1214 if (packet->xid != dhcp_client->xid)
1217 if (packet->hlen != 6)
1220 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1227 static void start_request(GDHCPClient *dhcp_client);
1229 static gboolean request_timeout(gpointer user_data)
1231 GDHCPClient *dhcp_client = user_data;
1233 debug(dhcp_client, "request timeout (retries %d)",
1234 dhcp_client->retry_times);
1236 dhcp_client->retry_times++;
1238 start_request(dhcp_client);
1243 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1244 gpointer user_data);
1246 static int switch_listening_mode(GDHCPClient *dhcp_client,
1247 ListenMode listen_mode)
1249 GIOChannel *listener_channel;
1250 int listener_sockfd;
1252 if (dhcp_client->listen_mode == listen_mode)
1255 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1256 dhcp_client->listen_mode, listen_mode);
1258 if (dhcp_client->listen_mode != L_NONE) {
1259 if (dhcp_client->listener_watch > 0)
1260 g_source_remove(dhcp_client->listener_watch);
1261 dhcp_client->listener_channel = NULL;
1262 dhcp_client->listen_mode = L_NONE;
1263 dhcp_client->listener_sockfd = -1;
1264 dhcp_client->listener_watch = 0;
1267 if (listen_mode == L_NONE)
1270 if (listen_mode == L2)
1271 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1272 else if (listen_mode == L3) {
1273 if (dhcp_client->type == G_DHCP_IPV6)
1274 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1275 dhcp_client->interface,
1278 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1279 dhcp_client->interface,
1281 } else if (listen_mode == L_ARP)
1282 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1286 if (listener_sockfd < 0)
1289 listener_channel = g_io_channel_unix_new(listener_sockfd);
1290 if (listener_channel == NULL) {
1291 /* Failed to create listener channel */
1292 close(listener_sockfd);
1296 dhcp_client->listen_mode = listen_mode;
1297 dhcp_client->listener_sockfd = listener_sockfd;
1298 dhcp_client->listener_channel = listener_channel;
1300 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1301 dhcp_client->listener_watch =
1302 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1303 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1304 listener_event, dhcp_client,
1306 g_io_channel_unref(dhcp_client->listener_channel);
1311 static void start_request(GDHCPClient *dhcp_client)
1313 debug(dhcp_client, "start request (retries %d)",
1314 dhcp_client->retry_times);
1316 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1317 dhcp_client->state = INIT_SELECTING;
1318 ipv4ll_start(dhcp_client);
1323 if (dhcp_client->retry_times == 0) {
1324 dhcp_client->state = REQUESTING;
1325 switch_listening_mode(dhcp_client, L2);
1328 send_select(dhcp_client);
1330 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1337 static uint32_t get_lease(struct dhcp_packet *packet)
1340 uint32_t lease_seconds;
1342 option = dhcp_get_option(packet, DHCP_LEASE_TIME);
1346 lease_seconds = get_be32(option);
1347 /* paranoia: must not be prone to overflows */
1348 lease_seconds &= 0x0fffffff;
1349 if (lease_seconds < 10)
1352 return lease_seconds;
1355 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1357 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1359 if (dhcp_client->timeout > 0) {
1360 g_source_remove(dhcp_client->timeout);
1361 dhcp_client->timeout = 0;
1364 dhcp_client->retry_times = retry_times;
1365 dhcp_client->requested_ip = 0;
1366 dhcp_client->state = INIT_SELECTING;
1367 switch_listening_mode(dhcp_client, L2);
1369 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1372 static gboolean start_rebound_timeout(gpointer user_data)
1374 GDHCPClient *dhcp_client = user_data;
1376 debug(dhcp_client, "start rebound timeout");
1378 switch_listening_mode(dhcp_client, L2);
1380 dhcp_client->lease_seconds >>= 1;
1382 /* We need to have enough time to receive ACK package*/
1383 if (dhcp_client->lease_seconds <= 6) {
1385 /* ip need to be cleared */
1386 if (dhcp_client->lease_lost_cb != NULL)
1387 dhcp_client->lease_lost_cb(dhcp_client,
1388 dhcp_client->lease_lost_data);
1390 restart_dhcp(dhcp_client, 0);
1392 send_rebound(dhcp_client);
1394 dhcp_client->timeout =
1395 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1396 dhcp_client->lease_seconds >> 1,
1397 start_rebound_timeout,
1405 static void start_rebound(GDHCPClient *dhcp_client)
1407 debug(dhcp_client, "start rebound");
1409 dhcp_client->state = REBINDING;
1411 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1412 dhcp_client->lease_seconds >> 1,
1413 start_rebound_timeout,
1418 static gboolean start_renew_timeout(gpointer user_data)
1420 GDHCPClient *dhcp_client = user_data;
1422 debug(dhcp_client, "start renew timeout");
1424 dhcp_client->state = RENEWING;
1426 dhcp_client->lease_seconds >>= 1;
1428 switch_listening_mode(dhcp_client, L3);
1429 if (dhcp_client->lease_seconds <= 60)
1430 start_rebound(dhcp_client);
1432 send_renew(dhcp_client);
1434 if (dhcp_client->timeout > 0)
1435 g_source_remove(dhcp_client->timeout);
1437 dhcp_client->timeout =
1438 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1439 dhcp_client->lease_seconds >> 1,
1440 start_renew_timeout,
1448 static void start_bound(GDHCPClient *dhcp_client)
1450 debug(dhcp_client, "start bound");
1452 dhcp_client->state = BOUND;
1454 if (dhcp_client->timeout > 0)
1455 g_source_remove(dhcp_client->timeout);
1457 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1458 dhcp_client->lease_seconds >> 1,
1459 start_renew_timeout, dhcp_client,
1463 static gboolean restart_dhcp_timeout(gpointer user_data)
1465 GDHCPClient *dhcp_client = user_data;
1467 debug(dhcp_client, "restart DHCP timeout");
1469 dhcp_client->ack_retry_times++;
1471 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1476 static char *get_ip(uint32_t ip)
1478 struct in_addr addr;
1482 return g_strdup(inet_ntoa(addr));
1485 /* get a rough idea of how long an option will be */
1486 static const uint8_t len_of_option_as_string[] = {
1487 [OPTION_IP] = sizeof("255.255.255.255 "),
1488 [OPTION_STRING] = 1,
1489 [OPTION_U8] = sizeof("255 "),
1490 [OPTION_U16] = sizeof("65535 "),
1491 [OPTION_U32] = sizeof("4294967295 "),
1494 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1496 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1499 /* Create "opt_value1 option_value2 ..." string */
1500 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1502 unsigned upper_length;
1506 len = option[OPT_LEN - OPT_DATA];
1507 type &= OPTION_TYPE_MASK;
1508 optlen = dhcp_option_lengths[type];
1511 upper_length = len_of_option_as_string[type] *
1512 ((unsigned)len / (unsigned)optlen);
1513 dest = ret = g_malloc(upper_length + 1);
1517 while (len >= optlen) {
1520 dest += sprint_nip(dest, "", option);
1523 uint16_t val_u16 = get_be16(option);
1524 dest += sprintf(dest, "%u", val_u16);
1528 uint32_t val_u32 = get_be32(option);
1529 dest += sprintf(dest, "%u", val_u32);
1533 memcpy(dest, option, len);
1550 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1558 if (type == OPTION_STRING)
1559 return g_list_append(list, g_strdup(value));
1561 while ((pos = strchr(pos, ' ')) != NULL) {
1564 list = g_list_append(list, g_strdup(value));
1569 list = g_list_append(list, g_strdup(value));
1574 static inline uint32_t get_uint32(unsigned char *value)
1576 return value[0] << 24 | value[1] << 16 |
1577 value[2] << 8 | value[3];
1580 static inline uint16_t get_uint16(unsigned char *value)
1582 return value[0] << 8 | value[1];
1585 static GList *get_addresses(GDHCPClient *dhcp_client,
1587 unsigned char *value,
1591 struct in6_addr addr;
1592 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1593 uint16_t option_len, option_code, st = 0, max_len;
1594 int addr_count = 0, i, pos;
1598 if (value == NULL || len < 4)
1601 iaid = get_uint32(&value[0]);
1602 if (dhcp_client->iaid != iaid)
1605 if (code == G_DHCPV6_IA_NA) {
1606 T1 = get_uint32(&value[4]);
1607 T2 = get_uint32(&value[8]);
1610 /* RFC 3315, 22.4 */
1620 max_len = len - pos;
1622 /* We have more sub-options in this packet. */
1624 option = dhcpv6_get_sub_option(&value[pos], max_len,
1625 &option_code, &option_len);
1627 debug(dhcp_client, "pos %d option %p code %d len %d",
1628 pos, option, option_code, option_len);
1636 switch (option_code) {
1637 case G_DHCPV6_IAADDR:
1639 memcpy(&addr, &option[0], sizeof(addr));
1641 preferred = get_uint32(&option[i]);
1643 valid = get_uint32(&option[i]);
1648 case G_DHCPV6_STATUS_CODE:
1649 st = get_uint16(&option[0]);
1650 debug(dhcp_client, "error code %d", st);
1651 if (option_len > 2) {
1652 str = g_strndup((gchar *)&option[2],
1654 debug(dhcp_client, "error text: %s", str);
1662 pos += 2 + 2 + option_len;
1664 } while (option != NULL);
1666 if (addr_count > 0 && st == 0) {
1667 /* We only support one address atm */
1668 char addr_str[INET6_ADDRSTRLEN + 1];
1670 if (preferred > valid)
1671 /* RFC 3315, 22.6 */
1674 dhcp_client->T1 = T1;
1675 dhcp_client->T2 = T2;
1677 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
1678 debug(dhcp_client, "count %d addr %s T1 %u T2 %u",
1679 addr_count, addr_str, T1, T2);
1681 list = g_list_append(list, g_strdup(addr_str));
1683 if (code == G_DHCPV6_IA_NA)
1684 memcpy(&dhcp_client->ia_na, &addr,
1685 sizeof(struct in6_addr));
1687 memcpy(&dhcp_client->ia_ta, &addr,
1688 sizeof(struct in6_addr));
1690 g_dhcpv6_client_set_expire(dhcp_client, valid);
1696 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
1698 unsigned char *value,
1709 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
1710 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
1713 "%s server list length (%d) is invalid",
1714 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
1718 for (i = 0; i < len; i += 16) {
1720 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
1724 if (inet_ntop(AF_INET6, &value[i], str,
1725 INET6_ADDRSTRLEN) == NULL)
1728 list = g_list_append(list, str);
1732 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
1733 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
1734 list = get_addresses(dhcp_client, code, len, value, status);
1744 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
1745 struct dhcpv6_packet *packet,
1746 uint16_t pkt_len, uint16_t *status)
1748 GList *list, *value_list;
1751 uint16_t option_len;
1753 for (list = dhcp_client->request_list; list; list = list->next) {
1754 code = (uint16_t) GPOINTER_TO_INT(list->data);
1756 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
1758 if (option == NULL) {
1759 g_hash_table_remove(dhcp_client->code_value_hash,
1760 GINT_TO_POINTER((int) code));
1764 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
1765 option_len, option, status);
1767 debug(dhcp_client, "code %d %p len %d list %p", code, option,
1768 option_len, value_list);
1770 if (value_list == NULL)
1771 g_hash_table_remove(dhcp_client->code_value_hash,
1772 GINT_TO_POINTER((int) code));
1774 g_hash_table_insert(dhcp_client->code_value_hash,
1775 GINT_TO_POINTER((int) code), value_list);
1779 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
1781 GDHCPOptionType type;
1782 GList *list, *value_list;
1787 for (list = dhcp_client->request_list; list; list = list->next) {
1788 code = (uint8_t) GPOINTER_TO_INT(list->data);
1790 option = dhcp_get_option(packet, code);
1791 if (option == NULL) {
1792 g_hash_table_remove(dhcp_client->code_value_hash,
1793 GINT_TO_POINTER((int) code));
1797 type = dhcp_get_code_type(code);
1799 option_value = malloc_option_value_string(option, type);
1800 if (option_value == NULL)
1801 g_hash_table_remove(dhcp_client->code_value_hash,
1802 GINT_TO_POINTER((int) code));
1804 value_list = get_option_value_list(option_value, type);
1806 g_free(option_value);
1808 if (value_list == NULL)
1809 g_hash_table_remove(dhcp_client->code_value_hash,
1810 GINT_TO_POINTER((int) code));
1812 g_hash_table_insert(dhcp_client->code_value_hash,
1813 GINT_TO_POINTER((int) code), value_list);
1817 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1820 GDHCPClient *dhcp_client = user_data;
1821 struct dhcp_packet packet;
1822 struct dhcpv6_packet *packet6 = NULL;
1823 uint8_t *message_type = NULL, *client_id = NULL, *option,
1825 uint16_t option_len = 0, status = 0;
1827 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
1828 uint16_t pkt_len = 0;
1832 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1833 dhcp_client->listener_watch = 0;
1837 if (dhcp_client->listen_mode == L_NONE)
1842 if (dhcp_client->listen_mode == L2)
1843 re = dhcp_recv_l2_packet(&packet,
1844 dhcp_client->listener_sockfd);
1845 else if (dhcp_client->listen_mode == L3) {
1846 if (dhcp_client->type == G_DHCP_IPV6) {
1847 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
1848 dhcp_client->listener_sockfd);
1852 re = dhcp_recv_l3_packet(&packet,
1853 dhcp_client->listener_sockfd);
1854 } else if (dhcp_client->listen_mode == L_ARP) {
1855 ipv4ll_recv_arp_packet(dhcp_client);
1864 if (check_package_owner(dhcp_client, pkt) == FALSE)
1867 if (dhcp_client->type == G_DHCP_IPV6) {
1868 if (packet6 == NULL)
1872 client_id = dhcpv6_get_option(packet6, pkt_len,
1873 G_DHCPV6_CLIENTID, &option_len, &count);
1875 if (client_id == NULL || count == 0 || option_len == 0 ||
1876 memcmp(dhcp_client->duid, client_id,
1877 dhcp_client->duid_len) != 0) {
1879 "client duid error, discarding msg %p/%d/%d",
1880 client_id, option_len, count);
1884 option = dhcpv6_get_option(packet6, pkt_len,
1885 G_DHCPV6_STATUS_CODE, &option_len, NULL);
1886 if (option != 0 && option_len > 0) {
1887 status = option[0]<<8 | option[1];
1889 debug(dhcp_client, "error code %d", status);
1890 if (option_len > 2) {
1891 gchar *txt = g_strndup(
1892 (gchar *)&option[2],
1894 debug(dhcp_client, "error text: %s",
1899 dhcp_client->status_code = status;
1901 dhcp_client->status_code = 0;
1904 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1905 if (message_type == NULL)
1909 if (message_type == NULL && client_id == NULL)
1910 /* No message type / client id option, ignore package */
1913 debug(dhcp_client, "received DHCP packet (current state %d)",
1914 dhcp_client->state);
1916 switch (dhcp_client->state) {
1917 case INIT_SELECTING:
1918 if (*message_type != DHCPOFFER)
1921 g_source_remove(dhcp_client->timeout);
1922 dhcp_client->timeout = 0;
1923 dhcp_client->retry_times = 0;
1925 option = dhcp_get_option(&packet, DHCP_SERVER_ID);
1926 dhcp_client->server_ip = get_be32(option);
1927 dhcp_client->requested_ip = ntohl(packet.yiaddr);
1929 dhcp_client->state = REQUESTING;
1931 start_request(dhcp_client);
1937 if (*message_type == DHCPACK) {
1938 dhcp_client->retry_times = 0;
1940 if (dhcp_client->timeout > 0)
1941 g_source_remove(dhcp_client->timeout);
1942 dhcp_client->timeout = 0;
1944 dhcp_client->lease_seconds = get_lease(&packet);
1946 get_request(dhcp_client, &packet);
1948 switch_listening_mode(dhcp_client, L_NONE);
1950 g_free(dhcp_client->assigned_ip);
1951 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1953 /* Address should be set up here */
1954 if (dhcp_client->lease_available_cb != NULL)
1955 dhcp_client->lease_available_cb(dhcp_client,
1956 dhcp_client->lease_available_data);
1958 start_bound(dhcp_client);
1959 } else if (*message_type == DHCPNAK) {
1960 dhcp_client->retry_times = 0;
1962 if (dhcp_client->timeout > 0)
1963 g_source_remove(dhcp_client->timeout);
1965 dhcp_client->timeout = g_timeout_add_seconds_full(
1967 restart_dhcp_timeout,
1974 if (dhcp_client->type != G_DHCP_IPV6)
1977 if (packet6->message != DHCPV6_REPLY &&
1978 packet6->message != DHCPV6_ADVERTISE)
1982 server_id = dhcpv6_get_option(packet6, pkt_len,
1983 G_DHCPV6_SERVERID, &option_len, &count);
1984 if (server_id == NULL || count != 1 || option_len == 0) {
1985 /* RFC 3315, 15.10 */
1987 "server duid error, discarding msg %p/%d/%d",
1988 server_id, option_len, count);
1991 dhcp_client->server_duid = g_try_malloc(option_len);
1992 if (dhcp_client->server_duid == NULL)
1994 memcpy(dhcp_client->server_duid, server_id, option_len);
1995 dhcp_client->server_duid_len = option_len;
1997 if (packet6->message == DHCPV6_REPLY) {
1998 uint8_t *rapid_commit;
2001 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2002 G_DHCPV6_RAPID_COMMIT,
2003 &option_len, &count);
2004 if (rapid_commit == NULL || option_len == 0 ||
2006 /* RFC 3315, 17.1.4 */
2010 switch_listening_mode(dhcp_client, L_NONE);
2012 if (dhcp_client->status_code == 0)
2013 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2014 &dhcp_client->status_code);
2016 if (packet6->message == DHCPV6_ADVERTISE) {
2017 if (dhcp_client->advertise_cb != NULL)
2018 dhcp_client->advertise_cb(dhcp_client,
2019 dhcp_client->advertise_data);
2023 if (dhcp_client->solicitation_cb != NULL) {
2025 * The dhcp_client might not be valid after the
2026 * callback call so just return immediately.
2028 dhcp_client->solicitation_cb(dhcp_client,
2029 dhcp_client->solicitation_data);
2033 case INFORMATION_REQ:
2038 if (dhcp_client->type != G_DHCP_IPV6)
2041 if (packet6->message != DHCPV6_REPLY)
2046 server_id = dhcpv6_get_option(packet6, pkt_len,
2047 G_DHCPV6_SERVERID, &option_len, &count);
2048 if (server_id == NULL || count != 1 || option_len == 0 ||
2049 (dhcp_client->server_duid_len > 0 &&
2050 memcmp(dhcp_client->server_duid, server_id,
2051 dhcp_client->server_duid_len) != 0)) {
2052 /* RFC 3315, 15.10 */
2054 "server duid error, discarding msg %p/%d/%d",
2055 server_id, option_len, count);
2059 switch_listening_mode(dhcp_client, L_NONE);
2061 dhcp_client->status_code = 0;
2062 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2063 &dhcp_client->status_code);
2065 if (dhcp_client->information_req_cb != NULL) {
2067 * The dhcp_client might not be valid after the
2068 * callback call so just return immediately.
2070 dhcp_client->information_req_cb(dhcp_client,
2071 dhcp_client->information_req_data);
2074 if (dhcp_client->request_cb != NULL) {
2075 dhcp_client->request_cb(dhcp_client,
2076 dhcp_client->request_data);
2079 if (dhcp_client->renew_cb != NULL) {
2080 dhcp_client->renew_cb(dhcp_client,
2081 dhcp_client->renew_data);
2084 if (dhcp_client->rebind_cb != NULL) {
2085 dhcp_client->rebind_cb(dhcp_client,
2086 dhcp_client->rebind_data);
2089 if (dhcp_client->release_cb != NULL) {
2090 dhcp_client->release_cb(dhcp_client,
2091 dhcp_client->release_data);
2099 debug(dhcp_client, "processed DHCP packet (new state %d)",
2100 dhcp_client->state);
2105 static gboolean discover_timeout(gpointer user_data)
2107 GDHCPClient *dhcp_client = user_data;
2109 dhcp_client->retry_times++;
2112 * We do not send the REQUESTED IP option if we are retrying because
2113 * if the server is non-authoritative it will ignore the request if the
2114 * option is present.
2116 g_dhcp_client_start(dhcp_client, NULL);
2121 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2123 GDHCPClient *dhcp_client = dhcp_data;
2125 debug(dhcp_client, "back to MONITOR mode");
2127 dhcp_client->conflicts = 0;
2128 dhcp_client->state = IPV4LL_MONITOR;
2133 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2135 GDHCPClient *dhcp_client = dhcp_data;
2138 debug(dhcp_client, "request timeout (retries %d)",
2139 dhcp_client->retry_times);
2141 if (dhcp_client->retry_times != ANNOUNCE_NUM){
2142 dhcp_client->retry_times++;
2143 send_announce_packet(dhcp_client);
2147 ip = htonl(dhcp_client->requested_ip);
2148 debug(dhcp_client, "switching to monitor mode");
2149 dhcp_client->state = IPV4LL_MONITOR;
2150 dhcp_client->assigned_ip = get_ip(ip);
2152 if (dhcp_client->ipv4ll_available_cb != NULL)
2153 dhcp_client->ipv4ll_available_cb(dhcp_client,
2154 dhcp_client->ipv4ll_available_data);
2155 dhcp_client->conflicts = 0;
2160 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2163 GDHCPClient *dhcp_client = dhcp_data;
2165 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2166 dhcp_client->retry_times);
2168 if (dhcp_client->retry_times == PROBE_NUM) {
2169 dhcp_client->state = IPV4LL_ANNOUNCE;
2170 dhcp_client->retry_times = 0;
2172 dhcp_client->retry_times++;
2173 send_announce_packet(dhcp_client);
2176 dhcp_client->retry_times++;
2177 send_probe_packet(dhcp_client);
2182 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2187 if (dhcp_client->type == G_DHCP_IPV6) {
2188 if (dhcp_client->information_req_cb) {
2189 dhcp_client->state = INFORMATION_REQ;
2190 re = switch_listening_mode(dhcp_client, L3);
2192 switch_listening_mode(dhcp_client, L_NONE);
2193 dhcp_client->state = 0;
2196 send_information_req(dhcp_client);
2198 } else if (dhcp_client->solicitation_cb) {
2199 dhcp_client->state = SOLICITATION;
2200 re = switch_listening_mode(dhcp_client, L3);
2202 switch_listening_mode(dhcp_client, L_NONE);
2203 dhcp_client->state = 0;
2206 send_solicitation(dhcp_client);
2208 } else if (dhcp_client->request_cb) {
2209 dhcp_client->state = REQUEST;
2210 re = switch_listening_mode(dhcp_client, L3);
2212 switch_listening_mode(dhcp_client, L_NONE);
2213 dhcp_client->state = 0;
2216 send_dhcpv6_request(dhcp_client);
2218 } else if (dhcp_client->renew_cb) {
2219 dhcp_client->state = RENEW;
2220 re = switch_listening_mode(dhcp_client, L3);
2222 switch_listening_mode(dhcp_client, L_NONE);
2223 dhcp_client->state = 0;
2226 send_dhcpv6_renew(dhcp_client);
2228 } else if (dhcp_client->rebind_cb) {
2229 dhcp_client->state = REBIND;
2230 re = switch_listening_mode(dhcp_client, L3);
2232 switch_listening_mode(dhcp_client, L_NONE);
2233 dhcp_client->state = 0;
2236 send_dhcpv6_rebind(dhcp_client);
2238 } else if (dhcp_client->release_cb) {
2239 dhcp_client->state = RENEW;
2240 re = switch_listening_mode(dhcp_client, L3);
2242 switch_listening_mode(dhcp_client, L_NONE);
2243 dhcp_client->state = 0;
2246 send_dhcpv6_release(dhcp_client);
2252 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2253 ipv4ll_start(dhcp_client);
2257 if (dhcp_client->retry_times == 0) {
2258 g_free(dhcp_client->assigned_ip);
2259 dhcp_client->assigned_ip = NULL;
2261 dhcp_client->state = INIT_SELECTING;
2262 re = switch_listening_mode(dhcp_client, L2);
2266 dhcp_client->xid = rand();
2267 dhcp_client->start = time(NULL);
2270 if (last_address == NULL) {
2273 addr = inet_addr(last_address);
2274 if (addr == 0xFFFFFFFF) {
2277 g_free(dhcp_client->last_address);
2278 dhcp_client->last_address = g_strdup(last_address);
2281 send_discover(dhcp_client, addr);
2283 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2291 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2293 switch_listening_mode(dhcp_client, L_NONE);
2295 if (dhcp_client->state == BOUND ||
2296 dhcp_client->state == RENEWING ||
2297 dhcp_client->state == REBINDING)
2298 send_release(dhcp_client, dhcp_client->server_ip,
2299 dhcp_client->requested_ip);
2301 if (dhcp_client->timeout > 0) {
2302 g_source_remove(dhcp_client->timeout);
2303 dhcp_client->timeout = 0;
2306 if (dhcp_client->listener_watch > 0) {
2307 g_source_remove(dhcp_client->listener_watch);
2308 dhcp_client->listener_watch = 0;
2311 dhcp_client->listener_channel = NULL;
2313 dhcp_client->retry_times = 0;
2314 dhcp_client->ack_retry_times = 0;
2316 dhcp_client->requested_ip = 0;
2317 dhcp_client->state = RELEASED;
2318 dhcp_client->lease_seconds = 0;
2321 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2322 unsigned char option_code)
2324 return g_hash_table_lookup(dhcp_client->code_value_hash,
2325 GINT_TO_POINTER((int) option_code));
2328 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2329 GDHCPClientEvent event,
2330 GDHCPClientEventFunc func,
2334 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2335 dhcp_client->lease_available_cb = func;
2336 dhcp_client->lease_available_data = data;
2338 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2339 if (dhcp_client->type == G_DHCP_IPV6)
2341 dhcp_client->ipv4ll_available_cb = func;
2342 dhcp_client->ipv4ll_available_data = data;
2344 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2345 dhcp_client->no_lease_cb = func;
2346 dhcp_client->no_lease_data = data;
2348 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2349 dhcp_client->lease_lost_cb = func;
2350 dhcp_client->lease_lost_data = data;
2352 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2353 if (dhcp_client->type == G_DHCP_IPV6)
2355 dhcp_client->ipv4ll_lost_cb = func;
2356 dhcp_client->ipv4ll_lost_data = data;
2358 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2359 dhcp_client->address_conflict_cb = func;
2360 dhcp_client->address_conflict_data = data;
2362 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2363 if (dhcp_client->type == G_DHCP_IPV4)
2365 dhcp_client->information_req_cb = func;
2366 dhcp_client->information_req_data = data;
2368 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2369 if (dhcp_client->type == G_DHCP_IPV4)
2371 dhcp_client->solicitation_cb = func;
2372 dhcp_client->solicitation_data = data;
2374 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2375 if (dhcp_client->type == G_DHCP_IPV4)
2377 dhcp_client->advertise_cb = func;
2378 dhcp_client->advertise_data = data;
2380 case G_DHCP_CLIENT_EVENT_REQUEST:
2381 if (dhcp_client->type == G_DHCP_IPV4)
2383 dhcp_client->request_cb = func;
2384 dhcp_client->request_data = data;
2386 case G_DHCP_CLIENT_EVENT_RENEW:
2387 if (dhcp_client->type == G_DHCP_IPV4)
2389 dhcp_client->renew_cb = func;
2390 dhcp_client->renew_data = data;
2392 case G_DHCP_CLIENT_EVENT_REBIND:
2393 if (dhcp_client->type == G_DHCP_IPV4)
2395 dhcp_client->rebind_cb = func;
2396 dhcp_client->rebind_data = data;
2398 case G_DHCP_CLIENT_EVENT_RELEASE:
2399 if (dhcp_client->type == G_DHCP_IPV4)
2401 dhcp_client->release_cb = func;
2402 dhcp_client->release_data = data;
2407 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2409 return dhcp_client->ifindex;
2412 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2414 return g_strdup(dhcp_client->assigned_ip);
2417 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2419 GList *option = NULL;
2421 if (dhcp_client->type == G_DHCP_IPV6)
2424 switch (dhcp_client->state) {
2426 case IPV4LL_MONITOR:
2427 return g_strdup("255.255.0.0");
2431 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2433 return g_strdup(option->data);
2434 case INIT_SELECTING:
2438 case IPV4LL_ANNOUNCE:
2439 case INFORMATION_REQ:
2450 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2451 unsigned int option_code)
2453 if (g_list_find(dhcp_client->request_list,
2454 GINT_TO_POINTER((int) option_code)) == NULL)
2455 dhcp_client->request_list = g_list_prepend(
2456 dhcp_client->request_list,
2457 (GINT_TO_POINTER((int) option_code)));
2459 return G_DHCP_CLIENT_ERROR_NONE;
2462 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2464 g_list_free(dhcp_client->request_list);
2465 dhcp_client->request_list = NULL;
2468 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2470 g_hash_table_remove_all(dhcp_client->send_value_hash);
2473 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
2477 storage = g_try_malloc(size + OPT_DATA);
2478 if (storage == NULL)
2481 storage[OPT_CODE] = code;
2482 storage[OPT_LEN] = size;
2483 memcpy(&storage[OPT_DATA], data, size);
2488 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data, unsigned size)
2490 return alloc_dhcp_option(code, data, MIN(size, 255));
2493 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
2495 return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
2498 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
2500 const unsigned maclen = 6;
2501 const unsigned idlen = maclen + 1;
2502 const uint8_t option_code = G_DHCP_CLIENT_ID;
2503 uint8_t idbuf[idlen];
2504 uint8_t *data_option;
2506 idbuf[0] = ARPHRD_ETHER;
2508 memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
2510 data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
2511 if (data_option == NULL)
2512 return G_DHCP_CLIENT_ERROR_NOMEM;
2514 g_hash_table_insert(dhcp_client->send_value_hash,
2515 GINT_TO_POINTER((int) option_code), data_option);
2517 return G_DHCP_CLIENT_ERROR_NONE;
2520 /* Now only support send hostname */
2521 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
2522 unsigned char option_code, const char *option_value)
2524 uint8_t *binary_option;
2526 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
2527 binary_option = alloc_dhcp_string_option(option_code,
2529 if (binary_option == NULL)
2530 return G_DHCP_CLIENT_ERROR_NOMEM;
2532 g_hash_table_insert(dhcp_client->send_value_hash,
2533 GINT_TO_POINTER((int) option_code), binary_option);
2536 return G_DHCP_CLIENT_ERROR_NONE;
2539 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
2544 storage = g_malloc(2 + 2 + len);
2545 if (storage == NULL)
2548 storage[0] = code >> 8;
2549 storage[1] = code & 0xff;
2550 storage[2] = len >> 8;
2551 storage[3] = len & 0xff;
2552 memcpy(storage + 2 + 2, option, len);
2557 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
2558 uint16_t option_code,
2559 uint8_t *option_value,
2560 uint16_t option_len)
2562 if (option_value != NULL) {
2563 uint8_t *binary_option;
2565 debug(dhcp_client, "setting option %d to %p len %d",
2566 option_code, option_value, option_len);
2568 binary_option = alloc_dhcpv6_option(option_code, option_value,
2570 if (binary_option != NULL)
2571 g_hash_table_insert(dhcp_client->send_value_hash,
2572 GINT_TO_POINTER((int) option_code),
2577 void g_dhcpv6_client_reset_renew(GDHCPClient *dhcp_client)
2579 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2582 dhcp_client->last_renew = time(NULL);
2585 void g_dhcpv6_client_reset_rebind(GDHCPClient *dhcp_client)
2587 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2590 dhcp_client->last_rebind = time(NULL);
2593 void g_dhcpv6_client_set_expire(GDHCPClient *dhcp_client, uint32_t timeout)
2595 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2598 dhcp_client->expire = time(NULL) + timeout;
2601 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
2603 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2606 return dhcp_client->status_code;
2609 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
2611 if (dhcp_client == NULL)
2614 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
2619 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
2621 if (dhcp_client == NULL)
2624 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
2627 g_dhcp_client_stop(dhcp_client);
2629 g_free(dhcp_client->interface);
2630 g_free(dhcp_client->assigned_ip);
2631 g_free(dhcp_client->last_address);
2632 g_free(dhcp_client->duid);
2633 g_free(dhcp_client->server_duid);
2635 g_list_free(dhcp_client->request_list);
2636 g_list_free(dhcp_client->require_list);
2638 g_hash_table_destroy(dhcp_client->code_value_hash);
2639 g_hash_table_destroy(dhcp_client->send_value_hash);
2641 g_free(dhcp_client);
2644 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
2645 GDHCPDebugFunc func, gpointer user_data)
2647 if (dhcp_client == NULL)
2650 dhcp_client->debug_func = func;
2651 dhcp_client->debug_data = user_data;