3 * DHCP client library with GLib integration
5 * Copyright (C) 2009-2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
33 #include <arpa/inet.h>
35 #include <netpacket/packet.h>
36 #include <netinet/if_ether.h>
37 #include <net/ethernet.h>
40 #include <linux/filter.h>
48 #define DISCOVER_TIMEOUT 3
49 #define DISCOVER_RETRIES 10
51 #define REQUEST_TIMEOUT 3
52 #define REQUEST_RETRIES 5
54 typedef enum _listen_mode {
61 typedef enum _dhcp_client_state {
83 uint8_t mac_address[6];
86 uint32_t requested_ip;
88 uint32_t lease_seconds;
89 ListenMode listen_mode;
92 uint8_t ack_retry_times;
96 GIOChannel *listener_channel;
99 GHashTable *code_value_hash;
100 GHashTable *send_value_hash;
101 GDHCPClientEventFunc lease_available_cb;
102 gpointer lease_available_data;
103 GDHCPClientEventFunc ipv4ll_available_cb;
104 gpointer ipv4ll_available_data;
105 GDHCPClientEventFunc no_lease_cb;
106 gpointer no_lease_data;
107 GDHCPClientEventFunc lease_lost_cb;
108 gpointer lease_lost_data;
109 GDHCPClientEventFunc ipv4ll_lost_cb;
110 gpointer ipv4ll_lost_data;
111 GDHCPClientEventFunc address_conflict_cb;
112 gpointer address_conflict_data;
113 GDHCPDebugFunc debug_func;
115 GDHCPClientEventFunc information_req_cb;
116 gpointer information_req_data;
117 GDHCPClientEventFunc solicitation_cb;
118 gpointer solicitation_data;
119 GDHCPClientEventFunc advertise_cb;
120 gpointer advertise_data;
121 GDHCPClientEventFunc request_cb;
122 gpointer request_data;
126 unsigned char *server_duid;
128 uint16_t status_code;
131 struct in6_addr ia_na;
132 struct in6_addr ia_ta;
135 static inline void debug(GDHCPClient *client, const char *format, ...)
140 if (client->debug_func == NULL)
143 va_start(ap, format);
145 if (vsnprintf(str, sizeof(str), format, ap) > 0)
146 client->debug_func(str, client->debug_data);
151 /* Initialize the packet with the proper defaults */
152 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
154 if (dhcp_client->type == G_DHCP_IPV6)
155 dhcpv6_init_header(pkt, type);
157 struct dhcp_packet *packet = pkt;
159 dhcp_init_header(packet, type);
160 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
164 static void add_request_options(GDHCPClient *dhcp_client,
165 struct dhcp_packet *packet)
170 int end = dhcp_end_option(packet->options);
172 for (list = dhcp_client->request_list; list; list = list->next) {
173 code = (uint8_t) GPOINTER_TO_INT(list->data);
175 packet->options[end + OPT_DATA + len] = code;
180 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
181 packet->options[end + OPT_LEN] = len;
182 packet->options[end + OPT_DATA + len] = DHCP_END;
189 unsigned char **ptr_buf;
192 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
195 uint8_t *option = value;
197 struct hash_params *params = user_data;
199 /* option[0][1] contains option code */
200 len = option[2] << 8 | option[3];
202 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
205 memcpy(*params->ptr_buf, option, len + 2 + 2);
206 (*params->ptr_buf) += len + 2 + 2;
209 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
210 unsigned char *buf, int max_buf,
211 unsigned char **ptr_buf)
213 struct hash_params params = {
219 if (dhcp_client->type == G_DHCP_IPV4)
222 g_hash_table_foreach(dhcp_client->send_value_hash,
223 add_dhcpv6_binary_option, ¶ms);
225 *ptr_buf = *params.ptr_buf;
228 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
232 buf[1] = code & 0xff;
235 if (len > 0 && msg != NULL)
236 memcpy(&buf[4], msg, len);
239 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
240 struct dhcpv6_packet *packet,
241 unsigned char *buf, int max_buf,
242 unsigned char **ptr_buf)
248 if (dhcp_client->type == G_DHCP_IPV4)
251 for (list = dhcp_client->request_list; list; list = list->next) {
252 code = (uint16_t) GPOINTER_TO_INT(list->data);
255 case G_DHCPV6_CLIENTID:
256 if (dhcp_client->duid == NULL)
259 len = 2 + 2 + dhcp_client->duid_len;
260 if ((*ptr_buf + len) > (buf + max_buf)) {
261 debug(dhcp_client, "Too long dhcpv6 message "
262 "when writing client id option");
266 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
267 dhcp_client->duid_len, dhcp_client->duid);
271 case G_DHCPV6_SERVERID:
272 if (dhcp_client->server_duid == NULL)
275 len = 2 + 2 + dhcp_client->server_duid_len;
276 if ((*ptr_buf + len) > (buf + max_buf)) {
277 debug(dhcp_client, "Too long dhcpv6 message "
278 "when writing server id option");
282 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
283 dhcp_client->server_duid_len,
284 dhcp_client->server_duid);
288 case G_DHCPV6_RAPID_COMMIT:
290 if ((*ptr_buf + len) > (buf + max_buf)) {
291 debug(dhcp_client, "Too long dhcpv6 message "
292 "when writing rapid commit option");
296 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
303 case G_DHCPV6_DNS_SERVERS:
306 case G_DHCPV6_SNTP_SERVERS:
315 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
317 uint8_t *option = value;
318 struct dhcp_packet *packet = user_data;
320 dhcp_add_binary_option(packet, option);
323 static void add_send_options(GDHCPClient *dhcp_client,
324 struct dhcp_packet *packet)
326 g_hash_table_foreach(dhcp_client->send_value_hash,
327 add_binary_option, packet);
330 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
332 struct dhcp_packet packet;
334 debug(dhcp_client, "sending DHCP discover request");
336 init_packet(dhcp_client, &packet, DHCPDISCOVER);
338 packet.xid = dhcp_client->xid;
341 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
343 /* Explicitly saying that we want RFC-compliant packets helps
344 * some buggy DHCP servers to NOT send bigger packets */
345 dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
347 add_request_options(dhcp_client, &packet);
349 add_send_options(dhcp_client, &packet);
351 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
352 INADDR_BROADCAST, SERVER_PORT,
353 MAC_BCAST_ADDR, dhcp_client->ifindex);
356 static int send_select(GDHCPClient *dhcp_client)
358 struct dhcp_packet packet;
360 debug(dhcp_client, "sending DHCP select request");
362 init_packet(dhcp_client, &packet, DHCPREQUEST);
364 packet.xid = dhcp_client->xid;
366 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
367 dhcp_client->requested_ip);
368 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
370 add_request_options(dhcp_client, &packet);
372 add_send_options(dhcp_client, &packet);
374 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
375 INADDR_BROADCAST, SERVER_PORT,
376 MAC_BCAST_ADDR, dhcp_client->ifindex);
379 static int send_renew(GDHCPClient *dhcp_client)
381 struct dhcp_packet packet;
383 debug(dhcp_client, "sending DHCP renew request");
385 init_packet(dhcp_client , &packet, DHCPREQUEST);
386 packet.xid = dhcp_client->xid;
387 packet.ciaddr = dhcp_client->requested_ip;
389 add_request_options(dhcp_client, &packet);
391 add_send_options(dhcp_client, &packet);
393 return dhcp_send_kernel_packet(&packet,
394 dhcp_client->requested_ip, CLIENT_PORT,
395 dhcp_client->server_ip, SERVER_PORT);
398 static int send_rebound(GDHCPClient *dhcp_client)
400 struct dhcp_packet packet;
402 debug(dhcp_client, "sending DHCP rebound request");
404 init_packet(dhcp_client , &packet, DHCPREQUEST);
405 packet.xid = dhcp_client->xid;
406 packet.ciaddr = dhcp_client->requested_ip;
408 add_request_options(dhcp_client, &packet);
410 add_send_options(dhcp_client, &packet);
412 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
413 INADDR_BROADCAST, SERVER_PORT,
414 MAC_BCAST_ADDR, dhcp_client->ifindex);
417 static int send_release(GDHCPClient *dhcp_client,
418 uint32_t server, uint32_t ciaddr)
420 struct dhcp_packet packet;
422 debug(dhcp_client, "sending DHCP release request");
424 init_packet(dhcp_client, &packet, DHCPRELEASE);
426 packet.ciaddr = ciaddr;
428 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
430 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
431 server, SERVER_PORT);
434 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
435 static int switch_listening_mode(GDHCPClient *dhcp_client,
436 ListenMode listen_mode);
438 static gboolean send_probe_packet(gpointer dhcp_data)
440 GDHCPClient *dhcp_client;
443 dhcp_client = dhcp_data;
444 /* if requested_ip is not valid, pick a new address*/
445 if (dhcp_client->requested_ip == 0) {
446 debug(dhcp_client, "pick a new random address");
447 dhcp_client->requested_ip = ipv4ll_random_ip(0);
450 debug(dhcp_client, "sending IPV4LL probe request");
452 if (dhcp_client->retry_times == 1) {
453 dhcp_client->state = IPV4LL_PROBE;
454 switch_listening_mode(dhcp_client, L_ARP);
456 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
457 dhcp_client->requested_ip, dhcp_client->ifindex);
459 if (dhcp_client->retry_times < PROBE_NUM) {
460 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
461 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
462 timeout += PROBE_MIN*1000;
464 timeout = (ANNOUNCE_WAIT * 1000);
466 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
468 ipv4ll_probe_timeout,
474 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
475 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
477 static gboolean send_announce_packet(gpointer dhcp_data)
479 GDHCPClient *dhcp_client;
481 dhcp_client = dhcp_data;
483 debug(dhcp_client, "sending IPV4LL announce request");
485 ipv4ll_send_arp_packet(dhcp_client->mac_address,
486 dhcp_client->requested_ip,
487 dhcp_client->requested_ip,
488 dhcp_client->ifindex);
490 if (dhcp_client->timeout > 0)
491 g_source_remove(dhcp_client->timeout);
492 dhcp_client->timeout = 0;
494 if (dhcp_client->state == IPV4LL_DEFEND) {
495 dhcp_client->timeout =
496 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
498 ipv4ll_defend_timeout,
503 dhcp_client->timeout =
504 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
506 ipv4ll_announce_timeout,
512 static void get_interface_mac_address(int index, uint8_t *mac_address)
517 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
519 perror("Open socket error");
523 memset(&ifr, 0, sizeof(ifr));
524 ifr.ifr_ifindex = index;
526 err = ioctl(sk, SIOCGIFNAME, &ifr);
528 perror("Get interface name error");
532 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
534 perror("Get mac address error");
538 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
544 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
545 unsigned char **duid, int *duid_len)
550 case G_DHCPV6_DUID_LLT:
551 *duid_len = 2 + 2 + 4 + ETH_ALEN;
552 *duid = g_try_malloc(*duid_len);
558 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
561 duid_time = time(0) - DUID_TIME_EPOCH;
562 (*duid)[4] = duid_time >> 24;
563 (*duid)[5] = duid_time >> 16;
564 (*duid)[6] = duid_time >> 8;
565 (*duid)[7] = duid_time & 0xff;
567 case G_DHCPV6_DUID_EN:
569 case G_DHCPV6_DUID_LL:
570 *duid_len = 2 + 2 + ETH_ALEN;
571 *duid = g_try_malloc(*duid_len);
577 get_interface_mac_address(index, &(*duid)[2 + 2]);
586 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
589 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
592 g_free(dhcp_client->duid);
594 dhcp_client->duid = duid;
595 dhcp_client->duid_len = duid_len;
600 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
602 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
605 return dhcp_client->iaid;
608 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
613 get_interface_mac_address(index, buf);
615 memcpy(iaid, &buf[2], 4);
616 dhcp_client->iaid = iaid[0] << 24 |
617 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
620 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
621 uint32_t *T1, uint32_t *T2)
623 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
627 *T1 = dhcp_client->T1;
630 *T2 = dhcp_client->T2;
635 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
639 buf[1] = G_DHCPV6_IAADDR;
642 memcpy(&buf[4], &dhcp_client->ia_na, 16);
643 memset(&buf[20], 0, 4); /* preferred */
644 memset(&buf[24], 0, 4); /* valid */
648 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
652 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
654 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
664 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
665 int code, uint32_t *T1, uint32_t *T2,
668 if (code == G_DHCPV6_IA_TA) {
669 uint8_t ia_options[4];
671 put_iaid(dhcp_client, index, ia_options);
673 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
674 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
675 ia_options, sizeof(ia_options));
677 } else if (code == G_DHCPV6_IA_NA) {
679 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
681 if (add_iaaddr == TRUE) {
682 #define IAADDR_LEN (16+4+4)
683 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
685 put_iaid(dhcp_client, index, ia_options);
688 ia_options[4] = *T1 >> 24;
689 ia_options[5] = *T1 >> 16;
690 ia_options[6] = *T1 >> 8;
693 memset(&ia_options[4], 0x00, 4);
696 ia_options[8] = *T2 >> 24;
697 ia_options[9] = *T2 >> 16;
698 ia_options[10] = *T2 >> 8;
699 ia_options[11] = *T2;
701 memset(&ia_options[8], 0x00, 4);
703 create_iaaddr(dhcp_client, &ia_options[12],
706 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
707 ia_options, sizeof(ia_options));
709 uint8_t ia_options[4+4+4];
711 put_iaid(dhcp_client, index, ia_options);
713 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
714 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
716 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
717 ia_options, sizeof(ia_options));
726 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
729 int i, j, len = sizeof(uint16_t) * args;
732 values = g_try_malloc(len);
737 for (i = 0, j = 0; i < args; i++) {
738 uint16_t value = va_arg(va, int);
739 values[j++] = value >> 8;
740 values[j++] = value & 0xff;
744 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
749 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
751 struct dhcpv6_packet *packet;
752 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
756 memset(buf, 0, sizeof(buf));
757 packet = (struct dhcpv6_packet *)&buf[0];
758 ptr = buf + sizeof(struct dhcpv6_packet);
760 debug(dhcp_client, "sending DHCPv6 %s message", msg);
762 init_packet(dhcp_client, packet, type);
764 dhcp_client->xid = packet->transaction_id[0] << 16 |
765 packet->transaction_id[1] << 8 |
766 packet->transaction_id[2];
768 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
770 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
772 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
774 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
776 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
780 static int send_solicitation(GDHCPClient *dhcp_client)
782 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
785 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
787 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
790 static int send_information_req(GDHCPClient *dhcp_client)
792 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
796 static void remove_value(gpointer data, gpointer user_data)
802 static void remove_option_value(gpointer data)
804 GList *option_value = data;
806 g_list_foreach(option_value, remove_value, NULL);
809 GDHCPClient *g_dhcp_client_new(GDHCPType type,
810 int ifindex, GDHCPClientError *error)
812 GDHCPClient *dhcp_client;
815 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
819 dhcp_client = g_try_new0(GDHCPClient, 1);
820 if (dhcp_client == NULL) {
821 *error = G_DHCP_CLIENT_ERROR_NOMEM;
825 dhcp_client->interface = get_interface_name(ifindex);
826 if (dhcp_client->interface == NULL) {
827 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
831 if (interface_is_up(ifindex) == FALSE) {
832 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
836 get_interface_mac_address(ifindex, dhcp_client->mac_address);
838 dhcp_client->listener_sockfd = -1;
839 dhcp_client->listener_channel = NULL;
840 dhcp_client->listen_mode = L_NONE;
841 dhcp_client->ref_count = 1;
842 dhcp_client->type = type;
843 dhcp_client->ifindex = ifindex;
844 dhcp_client->lease_available_cb = NULL;
845 dhcp_client->ipv4ll_available_cb = NULL;
846 dhcp_client->no_lease_cb = NULL;
847 dhcp_client->lease_lost_cb = NULL;
848 dhcp_client->ipv4ll_lost_cb = NULL;
849 dhcp_client->address_conflict_cb = NULL;
850 dhcp_client->listener_watch = 0;
851 dhcp_client->retry_times = 0;
852 dhcp_client->ack_retry_times = 0;
853 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
854 g_direct_equal, NULL, remove_option_value);
855 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
856 g_direct_equal, NULL, g_free);
857 dhcp_client->request_list = NULL;
858 dhcp_client->require_list = NULL;
859 dhcp_client->duid = NULL;
860 dhcp_client->duid_len = 0;
862 *error = G_DHCP_CLIENT_ERROR_NONE;
867 g_free(dhcp_client->interface);
872 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
874 static int dhcp_l2_socket(int ifindex)
877 struct sockaddr_ll sock;
882 * I've selected not to see LL header, so BPF doesn't see it, too.
883 * The filter may also pass non-IP and non-ARP packets, but we do
884 * a more complete check when receiving the message in userspace.
886 * and filter shamelessly stolen from:
888 * http://www.flamewarmaster.de/software/dhcpclient/
890 * There are a few other interesting ideas on that page (look under
891 * "Motivation"). Use of netlink events is most interesting. Think
892 * of various network servers listening for events and reconfiguring.
893 * That would obsolete sending HUP signals and/or make use of restarts.
895 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
898 * TODO: make conditional?
900 static const struct sock_filter filter_instr[] = {
902 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
903 /* L5, L1, is UDP? */
904 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
905 /* ugly check for arp on ethernet-like and IPv4 */
906 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
907 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
909 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
910 /* check udp source and destination ports */
911 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
913 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
915 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
916 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
919 static const struct sock_fprog filter_prog = {
920 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
921 /* casting const away: */
922 .filter = (struct sock_filter *) filter_instr,
925 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
929 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
930 /* Use only if standard ports are in use */
931 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
932 sizeof(filter_prog));
934 memset(&sock, 0, sizeof(sock));
935 sock.sll_family = AF_PACKET;
936 sock.sll_protocol = htons(ETH_P_IP);
937 sock.sll_ifindex = ifindex;
939 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
947 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
949 if (packet->ip.protocol != IPPROTO_UDP)
952 if (packet->ip.version != IPVERSION)
955 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
958 if (packet->udp.dest != htons(CLIENT_PORT))
961 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
967 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
970 struct ip_udp_dhcp_packet packet;
973 memset(&packet, 0, sizeof(packet));
975 bytes = read(fd, &packet, sizeof(packet));
979 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
982 if (bytes < ntohs(packet.ip.tot_len))
983 /* packet is bigger than sizeof(packet), we did partial read */
986 /* ignore any extra garbage bytes */
987 bytes = ntohs(packet.ip.tot_len);
989 if (sanity_check(&packet, bytes) == FALSE)
992 check = packet.ip.check;
994 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
997 /* verify UDP checksum. IP header has to be modified for this */
998 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
999 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1000 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1001 check = packet.udp.check;
1002 packet.udp.check = 0;
1003 if (check && check != dhcp_checksum(&packet, bytes))
1006 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1007 sizeof(packet.udp)));
1009 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1012 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1015 static void ipv4ll_start(GDHCPClient *dhcp_client)
1020 if (dhcp_client->timeout > 0) {
1021 g_source_remove(dhcp_client->timeout);
1022 dhcp_client->timeout = 0;
1025 switch_listening_mode(dhcp_client, L_NONE);
1026 dhcp_client->type = G_DHCP_IPV4LL;
1027 dhcp_client->retry_times = 0;
1028 dhcp_client->requested_ip = 0;
1030 /*try to start with a based mac address ip*/
1031 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1032 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1034 /*first wait a random delay to avoid storm of arp request on boot*/
1035 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1037 dhcp_client->retry_times++;
1038 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1045 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1048 switch_listening_mode(dhcp_client, L_NONE);
1050 if (dhcp_client->timeout > 0)
1051 g_source_remove(dhcp_client->timeout);
1053 if (dhcp_client->listener_watch > 0) {
1054 g_source_remove(dhcp_client->listener_watch);
1055 dhcp_client->listener_watch = 0;
1058 dhcp_client->state = IPV4LL_PROBE;
1059 dhcp_client->retry_times = 0;
1060 dhcp_client->requested_ip = 0;
1062 g_free(dhcp_client->assigned_ip);
1063 dhcp_client->assigned_ip = NULL;
1066 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1069 struct ether_arp arp;
1070 uint32_t ip_requested;
1071 int source_conflict;
1072 int target_conflict;
1074 memset(&arp, 0, sizeof(arp));
1076 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1080 if (arp.arp_op != htons(ARPOP_REPLY) &&
1081 arp.arp_op != htons(ARPOP_REQUEST))
1084 ip_requested = ntohl(dhcp_client->requested_ip);
1085 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1086 sizeof(ip_requested));
1088 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1089 sizeof(ip_requested));
1091 if (!source_conflict && !target_conflict)
1094 dhcp_client->conflicts++;
1096 debug(dhcp_client, "IPV4LL conflict detected");
1098 if (dhcp_client->state == IPV4LL_MONITOR) {
1099 if (!source_conflict)
1101 dhcp_client->state = IPV4LL_DEFEND;
1102 debug(dhcp_client, "DEFEND mode conflicts : %d",
1103 dhcp_client->conflicts);
1104 /*Try to defend with a single announce*/
1105 send_announce_packet(dhcp_client);
1109 if (dhcp_client->state == IPV4LL_DEFEND) {
1110 if (!source_conflict)
1112 else if (dhcp_client->ipv4ll_lost_cb != NULL)
1113 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1114 dhcp_client->ipv4ll_lost_data);
1117 ipv4ll_stop(dhcp_client);
1119 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1120 /*restart whole state machine*/
1121 dhcp_client->retry_times++;
1122 dhcp_client->timeout =
1123 g_timeout_add_full(G_PRIORITY_HIGH,
1124 ipv4ll_random_delay_ms(PROBE_WAIT),
1129 /* Here we got a lot of conflicts, RFC3927 states that we have
1130 * to wait RATE_LIMIT_INTERVAL before retrying,
1131 * but we just report failure.
1133 else if (dhcp_client->no_lease_cb != NULL)
1134 dhcp_client->no_lease_cb(dhcp_client,
1135 dhcp_client->no_lease_data);
1140 static gboolean check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1142 if (dhcp_client->type == G_DHCP_IPV6) {
1143 struct dhcpv6_packet *packet6 = pkt;
1146 if (packet6 == NULL)
1149 xid = packet6->transaction_id[0] << 16 |
1150 packet6->transaction_id[1] << 8 |
1151 packet6->transaction_id[2];
1153 if (xid != dhcp_client->xid)
1156 struct dhcp_packet *packet = pkt;
1158 if (packet->xid != dhcp_client->xid)
1161 if (packet->hlen != 6)
1164 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1171 static void start_request(GDHCPClient *dhcp_client);
1173 static gboolean request_timeout(gpointer user_data)
1175 GDHCPClient *dhcp_client = user_data;
1177 debug(dhcp_client, "request timeout (retries %d)",
1178 dhcp_client->retry_times);
1180 dhcp_client->retry_times++;
1182 start_request(dhcp_client);
1187 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1188 gpointer user_data);
1190 static int switch_listening_mode(GDHCPClient *dhcp_client,
1191 ListenMode listen_mode)
1193 GIOChannel *listener_channel;
1194 int listener_sockfd;
1196 if (dhcp_client->listen_mode == listen_mode)
1199 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1200 dhcp_client->listen_mode, listen_mode);
1202 if (dhcp_client->listen_mode != L_NONE) {
1203 if (dhcp_client->listener_watch > 0)
1204 g_source_remove(dhcp_client->listener_watch);
1205 dhcp_client->listener_channel = NULL;
1206 dhcp_client->listen_mode = L_NONE;
1207 dhcp_client->listener_sockfd = -1;
1208 dhcp_client->listener_watch = 0;
1211 if (listen_mode == L_NONE)
1214 if (listen_mode == L2)
1215 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1216 else if (listen_mode == L3) {
1217 if (dhcp_client->type == G_DHCP_IPV6)
1218 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1219 dhcp_client->interface,
1222 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1223 dhcp_client->interface,
1225 } else if (listen_mode == L_ARP)
1226 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1230 if (listener_sockfd < 0)
1233 listener_channel = g_io_channel_unix_new(listener_sockfd);
1234 if (listener_channel == NULL) {
1235 /* Failed to create listener channel */
1236 close(listener_sockfd);
1240 dhcp_client->listen_mode = listen_mode;
1241 dhcp_client->listener_sockfd = listener_sockfd;
1242 dhcp_client->listener_channel = listener_channel;
1244 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1245 dhcp_client->listener_watch =
1246 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1247 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1248 listener_event, dhcp_client,
1250 g_io_channel_unref(dhcp_client->listener_channel);
1255 static void start_request(GDHCPClient *dhcp_client)
1257 debug(dhcp_client, "start request (retries %d)",
1258 dhcp_client->retry_times);
1260 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1261 dhcp_client->state = INIT_SELECTING;
1262 ipv4ll_start(dhcp_client);
1267 if (dhcp_client->retry_times == 0) {
1268 dhcp_client->state = REQUESTING;
1269 switch_listening_mode(dhcp_client, L2);
1272 send_select(dhcp_client);
1274 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1281 static uint32_t get_lease(struct dhcp_packet *packet)
1284 uint32_t lease_seconds;
1286 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
1287 if (option_u8 == NULL)
1290 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
1291 lease_seconds = ntohl(lease_seconds);
1292 /* paranoia: must not be prone to overflows */
1293 lease_seconds &= 0x0fffffff;
1294 if (lease_seconds < 10)
1297 return lease_seconds;
1300 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1302 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1304 if (dhcp_client->timeout > 0) {
1305 g_source_remove(dhcp_client->timeout);
1306 dhcp_client->timeout = 0;
1309 dhcp_client->retry_times = retry_times;
1310 dhcp_client->requested_ip = 0;
1311 switch_listening_mode(dhcp_client, L2);
1313 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1316 static gboolean start_rebound_timeout(gpointer user_data)
1318 GDHCPClient *dhcp_client = user_data;
1320 debug(dhcp_client, "start rebound timeout");
1322 switch_listening_mode(dhcp_client, L2);
1324 dhcp_client->lease_seconds >>= 1;
1326 /* We need to have enough time to receive ACK package*/
1327 if (dhcp_client->lease_seconds <= 6) {
1329 /* ip need to be cleared */
1330 if (dhcp_client->lease_lost_cb != NULL)
1331 dhcp_client->lease_lost_cb(dhcp_client,
1332 dhcp_client->lease_lost_data);
1334 restart_dhcp(dhcp_client, 0);
1336 send_rebound(dhcp_client);
1338 dhcp_client->timeout =
1339 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1340 dhcp_client->lease_seconds >> 1,
1341 start_rebound_timeout,
1349 static void start_rebound(GDHCPClient *dhcp_client)
1351 debug(dhcp_client, "start rebound");
1353 dhcp_client->state = REBINDING;
1355 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1356 dhcp_client->lease_seconds >> 1,
1357 start_rebound_timeout,
1362 static gboolean start_renew_timeout(gpointer user_data)
1364 GDHCPClient *dhcp_client = user_data;
1366 debug(dhcp_client, "start renew timeout");
1368 dhcp_client->state = RENEWING;
1370 dhcp_client->lease_seconds >>= 1;
1372 switch_listening_mode(dhcp_client, L3);
1373 if (dhcp_client->lease_seconds <= 60)
1374 start_rebound(dhcp_client);
1376 send_renew(dhcp_client);
1378 if (dhcp_client->timeout > 0)
1379 g_source_remove(dhcp_client->timeout);
1381 dhcp_client->timeout =
1382 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1383 dhcp_client->lease_seconds >> 1,
1384 start_renew_timeout,
1392 static void start_bound(GDHCPClient *dhcp_client)
1394 debug(dhcp_client, "start bound");
1396 dhcp_client->state = BOUND;
1398 if (dhcp_client->timeout > 0)
1399 g_source_remove(dhcp_client->timeout);
1401 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1402 dhcp_client->lease_seconds >> 1,
1403 start_renew_timeout, dhcp_client,
1407 static gboolean restart_dhcp_timeout(gpointer user_data)
1409 GDHCPClient *dhcp_client = user_data;
1411 debug(dhcp_client, "restart DHCP timeout");
1413 dhcp_client->ack_retry_times++;
1415 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1420 static char *get_ip(uint32_t ip)
1422 struct in_addr addr;
1426 return g_strdup(inet_ntoa(addr));
1429 /* get a rough idea of how long an option will be */
1430 static const uint8_t len_of_option_as_string[] = {
1431 [OPTION_IP] = sizeof("255.255.255.255 "),
1432 [OPTION_STRING] = 1,
1433 [OPTION_U8] = sizeof("255 "),
1434 [OPTION_U16] = sizeof("65535 "),
1435 [OPTION_U32] = sizeof("4294967295 "),
1438 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1440 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1443 /* Create "opt_value1 option_value2 ..." string */
1444 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1446 unsigned upper_length;
1450 len = option[OPT_LEN - OPT_DATA];
1451 type &= OPTION_TYPE_MASK;
1452 optlen = dhcp_option_lengths[type];
1455 upper_length = len_of_option_as_string[type] *
1456 ((unsigned)len / (unsigned)optlen);
1457 dest = ret = malloc(upper_length + 1);
1461 while (len >= optlen) {
1464 dest += sprint_nip(dest, "", option);
1467 uint16_t val_u16 = dhcp_get_unaligned(
1468 (uint16_t *) option);
1469 dest += sprintf(dest, "%u", ntohs(val_u16));
1473 uint32_t val_u32 = dhcp_get_unaligned(
1474 (uint32_t *) option);
1475 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
1476 "%ld", (unsigned long) ntohl(val_u32));
1480 memcpy(dest, option, len);
1497 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1505 if (type == OPTION_STRING)
1506 return g_list_append(list, g_strdup(value));
1508 while ((pos = strchr(pos, ' ')) != NULL) {
1511 list = g_list_append(list, g_strdup(value));
1516 list = g_list_append(list, g_strdup(value));
1521 static inline uint32_t get_uint32(unsigned char *value)
1523 return value[0] << 24 | value[1] << 16 |
1524 value[2] << 8 | value[3];
1527 static inline uint16_t get_uint16(unsigned char *value)
1529 return value[0] << 8 | value[1];
1532 static GList *get_addresses(GDHCPClient *dhcp_client,
1534 unsigned char *value,
1538 struct in6_addr addr;
1539 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1540 uint16_t option_len, option_code, st = 0, max_len;
1541 int addr_count = 0, i, pos;
1545 iaid = get_uint32(&value[0]);
1546 if (dhcp_client->iaid != iaid)
1549 if (code == G_DHCPV6_IA_NA) {
1550 T1 = get_uint32(&value[4]);
1551 T2 = get_uint32(&value[8]);
1554 /* RFC 3315, 22.4 */
1564 max_len = len - pos;
1566 /* We have more sub-options in this packet. */
1568 option = dhcpv6_get_sub_option(&value[pos], max_len,
1569 &option_code, &option_len);
1571 debug(dhcp_client, "pos %d option %p code %d len %d",
1572 pos, option, option_code, option_len);
1580 switch (option_code) {
1581 case G_DHCPV6_IAADDR:
1583 memcpy(&addr, &option[0], sizeof(addr));
1585 preferred = get_uint32(&option[i]);
1587 valid = get_uint32(&option[i]);
1592 case G_DHCPV6_STATUS_CODE:
1593 st = get_uint16(&option[0]);
1594 debug(dhcp_client, "error code %d", st);
1595 if (option_len > 2) {
1596 str = g_strndup((gchar *)&option[2],
1598 debug(dhcp_client, "error text: %s", str);
1606 pos += 2 + 2 + option_len;
1608 } while (option != NULL);
1610 if (addr_count > 0 && st == 0) {
1611 /* We only support one address atm */
1612 char str[INET6_ADDRSTRLEN + 1];
1614 if (preferred > valid)
1615 /* RFC 3315, 22.6 */
1618 dhcp_client->T1 = T1;
1619 dhcp_client->T2 = T2;
1621 inet_ntop(AF_INET6, &addr, str, INET6_ADDRSTRLEN);
1622 debug(dhcp_client, "count %d addr %s T1 %u T2 %u",
1623 addr_count, str, T1, T2);
1625 list = g_list_append(list, g_strdup(str));
1627 if (code == G_DHCPV6_IA_NA)
1628 memcpy(&dhcp_client->ia_na, &addr,
1629 sizeof(struct in6_addr));
1631 memcpy(&dhcp_client->ia_ta, &addr,
1632 sizeof(struct in6_addr));
1638 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
1640 unsigned char *value,
1648 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
1649 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
1652 "%s server list length (%d) is invalid",
1653 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
1657 for (i = 0; i < len; i += 16) {
1659 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
1663 if (inet_ntop(AF_INET6, &value[i], str,
1664 INET6_ADDRSTRLEN) == NULL)
1667 list = g_list_append(list, str);
1671 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
1672 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
1673 list = get_addresses(dhcp_client, code, len, value, status);
1683 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
1684 struct dhcpv6_packet *packet,
1685 uint16_t pkt_len, uint16_t *status)
1687 GList *list, *value_list;
1690 uint16_t option_len;
1692 for (list = dhcp_client->request_list; list; list = list->next) {
1693 code = (uint16_t) GPOINTER_TO_INT(list->data);
1695 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
1697 if (option == NULL) {
1698 g_hash_table_remove(dhcp_client->code_value_hash,
1699 GINT_TO_POINTER((int) code));
1703 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
1704 option_len, option, status);
1706 debug(dhcp_client, "code %d %p len %d list %p", code, option,
1707 option_len, value_list);
1709 if (value_list == NULL)
1710 g_hash_table_remove(dhcp_client->code_value_hash,
1711 GINT_TO_POINTER((int) code));
1713 g_hash_table_insert(dhcp_client->code_value_hash,
1714 GINT_TO_POINTER((int) code), value_list);
1718 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
1720 GDHCPOptionType type;
1721 GList *list, *value_list;
1726 for (list = dhcp_client->request_list; list; list = list->next) {
1727 code = (uint8_t) GPOINTER_TO_INT(list->data);
1729 option = dhcp_get_option(packet, code);
1730 if (option == NULL) {
1731 g_hash_table_remove(dhcp_client->code_value_hash,
1732 GINT_TO_POINTER((int) code));
1736 type = dhcp_get_code_type(code);
1738 option_value = malloc_option_value_string(option, type);
1739 if (option_value == NULL)
1740 g_hash_table_remove(dhcp_client->code_value_hash,
1741 GINT_TO_POINTER((int) code));
1743 value_list = get_option_value_list(option_value, type);
1745 g_free(option_value);
1747 if (value_list == NULL)
1748 g_hash_table_remove(dhcp_client->code_value_hash,
1749 GINT_TO_POINTER((int) code));
1751 g_hash_table_insert(dhcp_client->code_value_hash,
1752 GINT_TO_POINTER((int) code), value_list);
1756 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1759 GDHCPClient *dhcp_client = user_data;
1760 struct dhcp_packet packet;
1761 struct dhcpv6_packet *packet6 = NULL;
1762 uint8_t *message_type = NULL, *client_id = NULL, *option_u8,
1764 uint16_t option_len = 0, status = 0;
1766 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
1767 uint16_t pkt_len = 0;
1771 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1772 dhcp_client->listener_watch = 0;
1776 if (dhcp_client->listen_mode == L_NONE)
1781 if (dhcp_client->listen_mode == L2)
1782 re = dhcp_recv_l2_packet(&packet,
1783 dhcp_client->listener_sockfd);
1784 else if (dhcp_client->listen_mode == L3) {
1785 if (dhcp_client->type == G_DHCP_IPV6) {
1786 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
1787 dhcp_client->listener_sockfd);
1791 re = dhcp_recv_l3_packet(&packet,
1792 dhcp_client->listener_sockfd);
1793 } else if (dhcp_client->listen_mode == L_ARP) {
1794 re = ipv4ll_recv_arp_packet(dhcp_client);
1803 if (check_package_owner(dhcp_client, pkt) == FALSE)
1806 if (dhcp_client->type == G_DHCP_IPV6) {
1808 client_id = dhcpv6_get_option(packet6, pkt_len,
1809 G_DHCPV6_CLIENTID, &option_len, &count);
1811 if (client_id == NULL || count == 0 || option_len == 0 ||
1812 memcmp(dhcp_client->duid, client_id,
1813 dhcp_client->duid_len) != 0) {
1815 "client duid error, discarding msg %p/%d/%d",
1816 client_id, option_len, count);
1820 option_u8 = dhcpv6_get_option(packet6, pkt_len,
1821 G_DHCPV6_STATUS_CODE, &option_len, NULL);
1822 if (option_u8 != 0 && option_len > 0) {
1823 status = option_u8[0]<<8 | option_u8[1];
1825 debug(dhcp_client, "error code %d", status);
1826 if (option_len > 2) {
1827 gchar *txt = g_strndup(
1828 (gchar *)&option_u8[2],
1830 debug(dhcp_client, "error text: %s",
1835 dhcp_client->status_code = status;
1837 dhcp_client->status_code = 0;
1840 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1842 if (message_type == NULL && client_id == NULL)
1843 /* No message type / client id option, ignore package */
1846 debug(dhcp_client, "received DHCP packet (current state %d)",
1847 dhcp_client->state);
1849 switch (dhcp_client->state) {
1850 case INIT_SELECTING:
1851 if (*message_type != DHCPOFFER)
1854 g_source_remove(dhcp_client->timeout);
1855 dhcp_client->timeout = 0;
1856 dhcp_client->retry_times = 0;
1858 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
1859 dhcp_client->server_ip =
1860 dhcp_get_unaligned((uint32_t *) option_u8);
1861 dhcp_client->requested_ip = packet.yiaddr;
1863 dhcp_client->state = REQUESTING;
1865 start_request(dhcp_client);
1871 if (*message_type == DHCPACK) {
1872 dhcp_client->retry_times = 0;
1874 if (dhcp_client->timeout > 0)
1875 g_source_remove(dhcp_client->timeout);
1876 dhcp_client->timeout = 0;
1878 dhcp_client->lease_seconds = get_lease(&packet);
1880 get_request(dhcp_client, &packet);
1882 switch_listening_mode(dhcp_client, L_NONE);
1884 g_free(dhcp_client->assigned_ip);
1885 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1887 /* Address should be set up here */
1888 if (dhcp_client->lease_available_cb != NULL)
1889 dhcp_client->lease_available_cb(dhcp_client,
1890 dhcp_client->lease_available_data);
1892 start_bound(dhcp_client);
1893 } else if (*message_type == DHCPNAK) {
1894 dhcp_client->retry_times = 0;
1896 if (dhcp_client->timeout > 0)
1897 g_source_remove(dhcp_client->timeout);
1899 dhcp_client->timeout = g_timeout_add_seconds_full(
1901 restart_dhcp_timeout,
1908 if (dhcp_client->type != G_DHCP_IPV6)
1911 if (packet6->message != DHCPV6_REPLY &&
1912 packet6->message != DHCPV6_ADVERTISE)
1916 server_id = dhcpv6_get_option(packet6, pkt_len,
1917 G_DHCPV6_SERVERID, &option_len, &count);
1918 if (server_id == NULL || count != 1 || option_len == 0) {
1919 /* RFC 3315, 15.10 */
1921 "server duid error, discarding msg %p/%d/%d",
1922 server_id, option_len, count);
1925 dhcp_client->server_duid = g_try_malloc(option_len);
1926 if (dhcp_client->server_duid == NULL)
1928 memcpy(dhcp_client->server_duid, server_id, option_len);
1929 dhcp_client->server_duid_len = option_len;
1931 if (packet6->message == DHCPV6_REPLY) {
1932 uint8_t *rapid_commit;
1935 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
1936 G_DHCPV6_RAPID_COMMIT,
1937 &option_len, &count);
1938 if (rapid_commit == NULL || option_len == 0 ||
1940 /* RFC 3315, 17.1.4 */
1944 switch_listening_mode(dhcp_client, L_NONE);
1946 if (dhcp_client->status_code == 0)
1947 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
1948 &dhcp_client->status_code);
1950 if (packet6->message == DHCPV6_ADVERTISE) {
1951 if (dhcp_client->advertise_cb != NULL)
1952 dhcp_client->advertise_cb(dhcp_client,
1953 dhcp_client->advertise_data);
1957 if (dhcp_client->solicitation_cb != NULL) {
1959 * The dhcp_client might not be valid after the
1960 * callback call so just return immediately.
1962 dhcp_client->solicitation_cb(dhcp_client,
1963 dhcp_client->solicitation_data);
1967 case INFORMATION_REQ:
1969 if (dhcp_client->type != G_DHCP_IPV6)
1972 if (packet6->message != DHCPV6_REPLY)
1977 server_id = dhcpv6_get_option(packet6, pkt_len,
1978 G_DHCPV6_SERVERID, &option_len, &count);
1979 if (server_id == NULL || count != 1 || option_len == 0 ||
1980 (dhcp_client->server_duid_len > 0 &&
1981 memcmp(dhcp_client->server_duid, server_id,
1982 dhcp_client->server_duid_len) != 0)) {
1983 /* RFC 3315, 15.10 */
1985 "server duid error, discarding msg %p/%d/%d",
1986 server_id, option_len, count);
1990 switch_listening_mode(dhcp_client, L_NONE);
1992 dhcp_client->status_code = 0;
1993 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
1994 &dhcp_client->status_code);
1996 if (dhcp_client->information_req_cb != NULL) {
1998 * The dhcp_client might not be valid after the
1999 * callback call so just return immediately.
2001 dhcp_client->information_req_cb(dhcp_client,
2002 dhcp_client->information_req_data);
2005 if (dhcp_client->request_cb != NULL) {
2006 dhcp_client->request_cb(dhcp_client,
2007 dhcp_client->request_data);
2015 debug(dhcp_client, "processed DHCP packet (new state %d)",
2016 dhcp_client->state);
2021 static gboolean discover_timeout(gpointer user_data)
2023 GDHCPClient *dhcp_client = user_data;
2025 dhcp_client->retry_times++;
2028 * We do not send the REQUESTED IP option if we are retrying because
2029 * if the server is non-authoritative it will ignore the request if the
2030 * option is present.
2032 g_dhcp_client_start(dhcp_client, NULL);
2037 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2039 GDHCPClient *dhcp_client = dhcp_data;
2041 debug(dhcp_client, "back to MONITOR mode");
2043 dhcp_client->conflicts = 0;
2044 dhcp_client->state = IPV4LL_MONITOR;
2049 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2051 GDHCPClient *dhcp_client = dhcp_data;
2054 debug(dhcp_client, "request timeout (retries %d)",
2055 dhcp_client->retry_times);
2057 if (dhcp_client->retry_times != ANNOUNCE_NUM){
2058 dhcp_client->retry_times++;
2059 send_announce_packet(dhcp_client);
2063 ip = htonl(dhcp_client->requested_ip);
2064 debug(dhcp_client, "switching to monitor mode");
2065 dhcp_client->state = IPV4LL_MONITOR;
2066 dhcp_client->assigned_ip = get_ip(ip);
2068 if (dhcp_client->ipv4ll_available_cb != NULL)
2069 dhcp_client->ipv4ll_available_cb(dhcp_client,
2070 dhcp_client->ipv4ll_available_data);
2071 dhcp_client->conflicts = 0;
2076 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2079 GDHCPClient *dhcp_client = dhcp_data;
2081 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2082 dhcp_client->retry_times);
2084 if (dhcp_client->retry_times == PROBE_NUM) {
2085 dhcp_client->state = IPV4LL_ANNOUNCE;
2086 dhcp_client->retry_times = 0;
2088 dhcp_client->retry_times++;
2089 send_announce_packet(dhcp_client);
2092 dhcp_client->retry_times++;
2093 send_probe_packet(dhcp_client);
2098 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2103 if (dhcp_client->type == G_DHCP_IPV6) {
2104 if (dhcp_client->information_req_cb) {
2105 dhcp_client->state = INFORMATION_REQ;
2106 re = switch_listening_mode(dhcp_client, L3);
2108 switch_listening_mode(dhcp_client, L_NONE);
2109 dhcp_client->state = 0;
2112 send_information_req(dhcp_client);
2114 } else if (dhcp_client->solicitation_cb) {
2115 dhcp_client->state = SOLICITATION;
2116 re = switch_listening_mode(dhcp_client, L3);
2118 switch_listening_mode(dhcp_client, L_NONE);
2119 dhcp_client->state = 0;
2122 send_solicitation(dhcp_client);
2124 } else if (dhcp_client->request_cb) {
2125 dhcp_client->state = REQUEST;
2126 re = switch_listening_mode(dhcp_client, L3);
2128 switch_listening_mode(dhcp_client, L_NONE);
2129 dhcp_client->state = 0;
2132 send_dhcpv6_request(dhcp_client);
2138 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2139 ipv4ll_start(dhcp_client);
2143 if (dhcp_client->retry_times == 0) {
2144 g_free(dhcp_client->assigned_ip);
2145 dhcp_client->assigned_ip = NULL;
2147 dhcp_client->state = INIT_SELECTING;
2148 re = switch_listening_mode(dhcp_client, L2);
2152 dhcp_client->xid = rand();
2155 if (last_address == NULL) {
2158 addr = inet_addr(last_address);
2159 if (addr == 0xFFFFFFFF) {
2162 g_free(dhcp_client->last_address);
2163 dhcp_client->last_address = g_strdup(last_address);
2166 send_discover(dhcp_client, addr);
2168 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2176 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2178 switch_listening_mode(dhcp_client, L_NONE);
2180 if (dhcp_client->state == BOUND ||
2181 dhcp_client->state == RENEWING ||
2182 dhcp_client->state == REBINDING)
2183 send_release(dhcp_client, dhcp_client->server_ip,
2184 dhcp_client->requested_ip);
2186 if (dhcp_client->timeout > 0) {
2187 g_source_remove(dhcp_client->timeout);
2188 dhcp_client->timeout = 0;
2191 if (dhcp_client->listener_watch > 0) {
2192 g_source_remove(dhcp_client->listener_watch);
2193 dhcp_client->listener_watch = 0;
2196 dhcp_client->listener_channel = NULL;
2198 dhcp_client->retry_times = 0;
2199 dhcp_client->ack_retry_times = 0;
2201 dhcp_client->requested_ip = 0;
2202 dhcp_client->state = RELEASED;
2203 dhcp_client->lease_seconds = 0;
2206 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2207 unsigned char option_code)
2209 return g_hash_table_lookup(dhcp_client->code_value_hash,
2210 GINT_TO_POINTER((int) option_code));
2213 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2214 GDHCPClientEvent event,
2215 GDHCPClientEventFunc func,
2219 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2220 dhcp_client->lease_available_cb = func;
2221 dhcp_client->lease_available_data = data;
2223 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2224 if (dhcp_client->type == G_DHCP_IPV6)
2226 dhcp_client->ipv4ll_available_cb = func;
2227 dhcp_client->ipv4ll_available_data = data;
2229 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2230 dhcp_client->no_lease_cb = func;
2231 dhcp_client->no_lease_data = data;
2233 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2234 dhcp_client->lease_lost_cb = func;
2235 dhcp_client->lease_lost_data = data;
2237 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2238 if (dhcp_client->type == G_DHCP_IPV6)
2240 dhcp_client->ipv4ll_lost_cb = func;
2241 dhcp_client->ipv4ll_lost_data = data;
2243 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2244 dhcp_client->address_conflict_cb = func;
2245 dhcp_client->address_conflict_data = data;
2247 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2248 if (dhcp_client->type == G_DHCP_IPV4)
2250 dhcp_client->information_req_cb = func;
2251 dhcp_client->information_req_data = data;
2253 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2254 if (dhcp_client->type == G_DHCP_IPV4)
2256 dhcp_client->solicitation_cb = func;
2257 dhcp_client->solicitation_data = data;
2259 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2260 if (dhcp_client->type == G_DHCP_IPV4)
2262 dhcp_client->advertise_cb = func;
2263 dhcp_client->advertise_data = data;
2265 case G_DHCP_CLIENT_EVENT_REQUEST:
2266 if (dhcp_client->type == G_DHCP_IPV4)
2268 dhcp_client->request_cb = func;
2269 dhcp_client->request_data = data;
2274 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2276 return dhcp_client->ifindex;
2279 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2281 return g_strdup(dhcp_client->assigned_ip);
2284 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2286 GList *option = NULL;
2288 if (dhcp_client->type == G_DHCP_IPV6)
2291 switch (dhcp_client->state) {
2293 case IPV4LL_MONITOR:
2294 return g_strdup("255.255.0.0");
2298 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2300 return g_strdup(option->data);
2301 case INIT_SELECTING:
2305 case IPV4LL_ANNOUNCE:
2306 case INFORMATION_REQ:
2314 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2315 unsigned int option_code)
2317 if (g_list_find(dhcp_client->request_list,
2318 GINT_TO_POINTER((int) option_code)) == NULL)
2319 dhcp_client->request_list = g_list_prepend(
2320 dhcp_client->request_list,
2321 (GINT_TO_POINTER((int) option_code)));
2323 return G_DHCP_CLIENT_ERROR_NONE;
2326 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2328 g_list_free(dhcp_client->request_list);
2329 dhcp_client->request_list = NULL;
2332 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2334 g_hash_table_remove_all(dhcp_client->send_value_hash);
2337 static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
2340 int len = strnlen(str, 255);
2342 storage = malloc(len + extra + OPT_DATA);
2343 storage[OPT_CODE] = code;
2344 storage[OPT_LEN] = len + extra;
2345 memcpy(storage + extra + OPT_DATA, str, len);
2350 /* Now only support send hostname */
2351 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
2352 unsigned char option_code, const char *option_value)
2354 uint8_t *binary_option;
2356 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
2357 binary_option = alloc_dhcp_option(option_code,
2360 g_hash_table_insert(dhcp_client->send_value_hash,
2361 GINT_TO_POINTER((int) option_code), binary_option);
2364 return G_DHCP_CLIENT_ERROR_NONE;
2367 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
2372 storage = g_malloc(2 + 2 + len);
2373 if (storage == NULL)
2376 storage[0] = code >> 8;
2377 storage[1] = code & 0xff;
2378 storage[2] = len >> 8;
2379 storage[3] = len & 0xff;
2380 memcpy(storage + 2 + 2, option, len);
2385 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
2386 uint16_t option_code,
2387 uint8_t *option_value,
2388 uint16_t option_len)
2390 if (option_value != NULL) {
2391 uint8_t *binary_option;
2393 debug(dhcp_client, "setting option %d to %p len %d",
2394 option_code, option_value, option_len);
2396 binary_option = alloc_dhcpv6_option(option_code, option_value,
2398 if (binary_option != NULL)
2399 g_hash_table_insert(dhcp_client->send_value_hash,
2400 GINT_TO_POINTER((int) option_code),
2405 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
2407 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2410 return dhcp_client->status_code;
2413 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
2415 if (dhcp_client == NULL)
2418 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
2423 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
2425 if (dhcp_client == NULL)
2428 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
2431 g_dhcp_client_stop(dhcp_client);
2433 g_free(dhcp_client->interface);
2434 g_free(dhcp_client->assigned_ip);
2435 g_free(dhcp_client->last_address);
2436 g_free(dhcp_client->duid);
2437 g_free(dhcp_client->server_duid);
2439 g_list_free(dhcp_client->request_list);
2440 g_list_free(dhcp_client->require_list);
2442 g_hash_table_destroy(dhcp_client->code_value_hash);
2443 g_hash_table_destroy(dhcp_client->send_value_hash);
2445 g_free(dhcp_client);
2448 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
2449 GDHCPDebugFunc func, gpointer user_data)
2451 if (dhcp_client == NULL)
2454 dhcp_client->debug_func = func;
2455 dhcp_client->debug_data = user_data;