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 {
85 uint8_t mac_address[6];
88 uint32_t requested_ip;
90 uint32_t lease_seconds;
91 ListenMode listen_mode;
94 uint8_t ack_retry_times;
98 GIOChannel *listener_channel;
101 GHashTable *code_value_hash;
102 GHashTable *send_value_hash;
103 GDHCPClientEventFunc lease_available_cb;
104 gpointer lease_available_data;
105 GDHCPClientEventFunc ipv4ll_available_cb;
106 gpointer ipv4ll_available_data;
107 GDHCPClientEventFunc no_lease_cb;
108 gpointer no_lease_data;
109 GDHCPClientEventFunc lease_lost_cb;
110 gpointer lease_lost_data;
111 GDHCPClientEventFunc ipv4ll_lost_cb;
112 gpointer ipv4ll_lost_data;
113 GDHCPClientEventFunc address_conflict_cb;
114 gpointer address_conflict_data;
115 GDHCPDebugFunc debug_func;
117 GDHCPClientEventFunc information_req_cb;
118 gpointer information_req_data;
119 GDHCPClientEventFunc solicitation_cb;
120 gpointer solicitation_data;
121 GDHCPClientEventFunc advertise_cb;
122 gpointer advertise_data;
123 GDHCPClientEventFunc request_cb;
124 gpointer request_data;
125 GDHCPClientEventFunc renew_cb;
127 GDHCPClientEventFunc rebind_cb;
128 gpointer rebind_data;
132 unsigned char *server_duid;
134 uint16_t status_code;
137 struct in6_addr ia_na;
138 struct in6_addr ia_ta;
143 static inline void debug(GDHCPClient *client, const char *format, ...)
148 if (client->debug_func == NULL)
151 va_start(ap, format);
153 if (vsnprintf(str, sizeof(str), format, ap) > 0)
154 client->debug_func(str, client->debug_data);
159 /* Initialize the packet with the proper defaults */
160 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
162 if (dhcp_client->type == G_DHCP_IPV6)
163 dhcpv6_init_header(pkt, type);
165 struct dhcp_packet *packet = pkt;
167 dhcp_init_header(packet, type);
168 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
172 static void add_request_options(GDHCPClient *dhcp_client,
173 struct dhcp_packet *packet)
178 int end = dhcp_end_option(packet->options);
180 for (list = dhcp_client->request_list; list; list = list->next) {
181 code = (uint8_t) GPOINTER_TO_INT(list->data);
183 packet->options[end + OPT_DATA + len] = code;
188 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
189 packet->options[end + OPT_LEN] = len;
190 packet->options[end + OPT_DATA + len] = DHCP_END;
197 unsigned char **ptr_buf;
200 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
203 uint8_t *option = value;
205 struct hash_params *params = user_data;
207 /* option[0][1] contains option code */
208 len = option[2] << 8 | option[3];
210 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
213 memcpy(*params->ptr_buf, option, len + 2 + 2);
214 (*params->ptr_buf) += len + 2 + 2;
217 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
218 unsigned char *buf, int max_buf,
219 unsigned char **ptr_buf)
221 struct hash_params params = {
227 if (dhcp_client->type == G_DHCP_IPV4)
230 g_hash_table_foreach(dhcp_client->send_value_hash,
231 add_dhcpv6_binary_option, ¶ms);
233 *ptr_buf = *params.ptr_buf;
236 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
240 buf[1] = code & 0xff;
243 if (len > 0 && msg != NULL)
244 memcpy(&buf[4], msg, len);
247 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
248 struct dhcpv6_packet *packet,
249 unsigned char *buf, int max_buf,
250 unsigned char **ptr_buf)
256 if (dhcp_client->type == G_DHCP_IPV4)
259 for (list = dhcp_client->request_list; list; list = list->next) {
260 code = (uint16_t) GPOINTER_TO_INT(list->data);
263 case G_DHCPV6_CLIENTID:
264 if (dhcp_client->duid == NULL)
267 len = 2 + 2 + dhcp_client->duid_len;
268 if ((*ptr_buf + len) > (buf + max_buf)) {
269 debug(dhcp_client, "Too long dhcpv6 message "
270 "when writing client id option");
274 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
275 dhcp_client->duid_len, dhcp_client->duid);
279 case G_DHCPV6_SERVERID:
280 if (dhcp_client->server_duid == NULL)
283 len = 2 + 2 + dhcp_client->server_duid_len;
284 if ((*ptr_buf + len) > (buf + max_buf)) {
285 debug(dhcp_client, "Too long dhcpv6 message "
286 "when writing server id option");
290 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
291 dhcp_client->server_duid_len,
292 dhcp_client->server_duid);
296 case G_DHCPV6_RAPID_COMMIT:
298 if ((*ptr_buf + len) > (buf + max_buf)) {
299 debug(dhcp_client, "Too long dhcpv6 message "
300 "when writing rapid commit option");
304 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
311 case G_DHCPV6_DNS_SERVERS:
314 case G_DHCPV6_SNTP_SERVERS:
323 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
325 uint8_t *option = value;
326 struct dhcp_packet *packet = user_data;
328 dhcp_add_binary_option(packet, option);
331 static void add_send_options(GDHCPClient *dhcp_client,
332 struct dhcp_packet *packet)
334 g_hash_table_foreach(dhcp_client->send_value_hash,
335 add_binary_option, packet);
338 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
340 struct dhcp_packet packet;
342 debug(dhcp_client, "sending DHCP discover request");
344 init_packet(dhcp_client, &packet, DHCPDISCOVER);
346 packet.xid = dhcp_client->xid;
349 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
351 /* Explicitly saying that we want RFC-compliant packets helps
352 * some buggy DHCP servers to NOT send bigger packets */
353 dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
355 add_request_options(dhcp_client, &packet);
357 add_send_options(dhcp_client, &packet);
359 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
360 INADDR_BROADCAST, SERVER_PORT,
361 MAC_BCAST_ADDR, dhcp_client->ifindex);
364 static int send_select(GDHCPClient *dhcp_client)
366 struct dhcp_packet packet;
368 debug(dhcp_client, "sending DHCP select request");
370 init_packet(dhcp_client, &packet, DHCPREQUEST);
372 packet.xid = dhcp_client->xid;
374 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
375 dhcp_client->requested_ip);
376 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
378 add_request_options(dhcp_client, &packet);
380 add_send_options(dhcp_client, &packet);
382 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
383 INADDR_BROADCAST, SERVER_PORT,
384 MAC_BCAST_ADDR, dhcp_client->ifindex);
387 static int send_renew(GDHCPClient *dhcp_client)
389 struct dhcp_packet packet;
391 debug(dhcp_client, "sending DHCP renew request");
393 init_packet(dhcp_client , &packet, DHCPREQUEST);
394 packet.xid = dhcp_client->xid;
395 packet.ciaddr = dhcp_client->requested_ip;
397 add_request_options(dhcp_client, &packet);
399 add_send_options(dhcp_client, &packet);
401 return dhcp_send_kernel_packet(&packet,
402 dhcp_client->requested_ip, CLIENT_PORT,
403 dhcp_client->server_ip, SERVER_PORT);
406 static int send_rebound(GDHCPClient *dhcp_client)
408 struct dhcp_packet packet;
410 debug(dhcp_client, "sending DHCP rebound request");
412 init_packet(dhcp_client , &packet, DHCPREQUEST);
413 packet.xid = dhcp_client->xid;
414 packet.ciaddr = dhcp_client->requested_ip;
416 add_request_options(dhcp_client, &packet);
418 add_send_options(dhcp_client, &packet);
420 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
421 INADDR_BROADCAST, SERVER_PORT,
422 MAC_BCAST_ADDR, dhcp_client->ifindex);
425 static int send_release(GDHCPClient *dhcp_client,
426 uint32_t server, uint32_t ciaddr)
428 struct dhcp_packet packet;
430 debug(dhcp_client, "sending DHCP release request");
432 init_packet(dhcp_client, &packet, DHCPRELEASE);
434 packet.ciaddr = ciaddr;
436 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
438 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
439 server, SERVER_PORT);
442 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
443 static int switch_listening_mode(GDHCPClient *dhcp_client,
444 ListenMode listen_mode);
446 static gboolean send_probe_packet(gpointer dhcp_data)
448 GDHCPClient *dhcp_client;
451 dhcp_client = dhcp_data;
452 /* if requested_ip is not valid, pick a new address*/
453 if (dhcp_client->requested_ip == 0) {
454 debug(dhcp_client, "pick a new random address");
455 dhcp_client->requested_ip = ipv4ll_random_ip(0);
458 debug(dhcp_client, "sending IPV4LL probe request");
460 if (dhcp_client->retry_times == 1) {
461 dhcp_client->state = IPV4LL_PROBE;
462 switch_listening_mode(dhcp_client, L_ARP);
464 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
465 dhcp_client->requested_ip, dhcp_client->ifindex);
467 if (dhcp_client->retry_times < PROBE_NUM) {
468 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
469 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
470 timeout += PROBE_MIN*1000;
472 timeout = (ANNOUNCE_WAIT * 1000);
474 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
476 ipv4ll_probe_timeout,
482 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
483 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
485 static gboolean send_announce_packet(gpointer dhcp_data)
487 GDHCPClient *dhcp_client;
489 dhcp_client = dhcp_data;
491 debug(dhcp_client, "sending IPV4LL announce request");
493 ipv4ll_send_arp_packet(dhcp_client->mac_address,
494 dhcp_client->requested_ip,
495 dhcp_client->requested_ip,
496 dhcp_client->ifindex);
498 if (dhcp_client->timeout > 0)
499 g_source_remove(dhcp_client->timeout);
500 dhcp_client->timeout = 0;
502 if (dhcp_client->state == IPV4LL_DEFEND) {
503 dhcp_client->timeout =
504 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
506 ipv4ll_defend_timeout,
511 dhcp_client->timeout =
512 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
514 ipv4ll_announce_timeout,
520 static void get_interface_mac_address(int index, uint8_t *mac_address)
525 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
527 perror("Open socket error");
531 memset(&ifr, 0, sizeof(ifr));
532 ifr.ifr_ifindex = index;
534 err = ioctl(sk, SIOCGIFNAME, &ifr);
536 perror("Get interface name error");
540 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
542 perror("Get mac address error");
546 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
552 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
553 unsigned char **duid, int *duid_len)
558 case G_DHCPV6_DUID_LLT:
559 *duid_len = 2 + 2 + 4 + ETH_ALEN;
560 *duid = g_try_malloc(*duid_len);
566 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
569 duid_time = time(0) - DUID_TIME_EPOCH;
570 (*duid)[4] = duid_time >> 24;
571 (*duid)[5] = duid_time >> 16;
572 (*duid)[6] = duid_time >> 8;
573 (*duid)[7] = duid_time & 0xff;
575 case G_DHCPV6_DUID_EN:
577 case G_DHCPV6_DUID_LL:
578 *duid_len = 2 + 2 + ETH_ALEN;
579 *duid = g_try_malloc(*duid_len);
585 get_interface_mac_address(index, &(*duid)[2 + 2]);
594 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
597 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
600 g_free(dhcp_client->duid);
602 dhcp_client->duid = duid;
603 dhcp_client->duid_len = duid_len;
608 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
610 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
613 return dhcp_client->iaid;
616 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
621 get_interface_mac_address(index, buf);
623 memcpy(iaid, &buf[2], 4);
624 dhcp_client->iaid = iaid[0] << 24 |
625 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
628 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
629 uint32_t *T1, uint32_t *T2,
630 time_t *last_renew, time_t *last_rebind)
632 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
636 *T1 = dhcp_client->T1;
639 *T2 = dhcp_client->T2;
641 if (last_renew != NULL)
642 *last_renew = dhcp_client->last_renew;
644 if (last_rebind != NULL)
645 *last_rebind = dhcp_client->last_rebind;
650 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
654 buf[1] = G_DHCPV6_IAADDR;
657 memcpy(&buf[4], &dhcp_client->ia_na, 16);
658 memset(&buf[20], 0, 4); /* preferred */
659 memset(&buf[24], 0, 4); /* valid */
663 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
667 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
669 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
679 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
680 int code, uint32_t *T1, uint32_t *T2,
683 if (code == G_DHCPV6_IA_TA) {
684 uint8_t ia_options[4];
686 put_iaid(dhcp_client, index, ia_options);
688 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
689 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
690 ia_options, sizeof(ia_options));
692 } else if (code == G_DHCPV6_IA_NA) {
694 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
696 if (add_iaaddr == TRUE) {
697 #define IAADDR_LEN (16+4+4)
698 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
700 put_iaid(dhcp_client, index, ia_options);
703 ia_options[4] = *T1 >> 24;
704 ia_options[5] = *T1 >> 16;
705 ia_options[6] = *T1 >> 8;
708 memset(&ia_options[4], 0x00, 4);
711 ia_options[8] = *T2 >> 24;
712 ia_options[9] = *T2 >> 16;
713 ia_options[10] = *T2 >> 8;
714 ia_options[11] = *T2;
716 memset(&ia_options[8], 0x00, 4);
718 create_iaaddr(dhcp_client, &ia_options[12],
721 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
722 ia_options, sizeof(ia_options));
724 uint8_t ia_options[4+4+4];
726 put_iaid(dhcp_client, index, ia_options);
728 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
729 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
731 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
732 ia_options, sizeof(ia_options));
741 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
744 int i, j, len = sizeof(uint16_t) * args;
747 values = g_try_malloc(len);
752 for (i = 0, j = 0; i < args; i++) {
753 uint16_t value = va_arg(va, int);
754 values[j++] = value >> 8;
755 values[j++] = value & 0xff;
759 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
764 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
766 struct dhcpv6_packet *packet;
767 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
771 memset(buf, 0, sizeof(buf));
772 packet = (struct dhcpv6_packet *)&buf[0];
773 ptr = buf + sizeof(struct dhcpv6_packet);
775 debug(dhcp_client, "sending DHCPv6 %s message", msg);
777 init_packet(dhcp_client, packet, type);
779 dhcp_client->xid = packet->transaction_id[0] << 16 |
780 packet->transaction_id[1] << 8 |
781 packet->transaction_id[2];
783 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
785 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
787 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
789 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
791 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
795 static int send_solicitation(GDHCPClient *dhcp_client)
797 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
800 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
802 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
805 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
807 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
810 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
812 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
815 static int send_information_req(GDHCPClient *dhcp_client)
817 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
821 static void remove_value(gpointer data, gpointer user_data)
827 static void remove_option_value(gpointer data)
829 GList *option_value = data;
831 g_list_foreach(option_value, remove_value, NULL);
834 GDHCPClient *g_dhcp_client_new(GDHCPType type,
835 int ifindex, GDHCPClientError *error)
837 GDHCPClient *dhcp_client;
840 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
844 dhcp_client = g_try_new0(GDHCPClient, 1);
845 if (dhcp_client == NULL) {
846 *error = G_DHCP_CLIENT_ERROR_NOMEM;
850 dhcp_client->interface = get_interface_name(ifindex);
851 if (dhcp_client->interface == NULL) {
852 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
856 if (interface_is_up(ifindex) == FALSE) {
857 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
861 get_interface_mac_address(ifindex, dhcp_client->mac_address);
863 dhcp_client->listener_sockfd = -1;
864 dhcp_client->listener_channel = NULL;
865 dhcp_client->listen_mode = L_NONE;
866 dhcp_client->ref_count = 1;
867 dhcp_client->type = type;
868 dhcp_client->ifindex = ifindex;
869 dhcp_client->lease_available_cb = NULL;
870 dhcp_client->ipv4ll_available_cb = NULL;
871 dhcp_client->no_lease_cb = NULL;
872 dhcp_client->lease_lost_cb = NULL;
873 dhcp_client->ipv4ll_lost_cb = NULL;
874 dhcp_client->address_conflict_cb = NULL;
875 dhcp_client->listener_watch = 0;
876 dhcp_client->retry_times = 0;
877 dhcp_client->ack_retry_times = 0;
878 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
879 g_direct_equal, NULL, remove_option_value);
880 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
881 g_direct_equal, NULL, g_free);
882 dhcp_client->request_list = NULL;
883 dhcp_client->require_list = NULL;
884 dhcp_client->duid = NULL;
885 dhcp_client->duid_len = 0;
886 dhcp_client->last_renew = dhcp_client->last_rebind = time(0);
888 *error = G_DHCP_CLIENT_ERROR_NONE;
893 g_free(dhcp_client->interface);
898 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
900 static int dhcp_l2_socket(int ifindex)
903 struct sockaddr_ll sock;
908 * I've selected not to see LL header, so BPF doesn't see it, too.
909 * The filter may also pass non-IP and non-ARP packets, but we do
910 * a more complete check when receiving the message in userspace.
912 * and filter shamelessly stolen from:
914 * http://www.flamewarmaster.de/software/dhcpclient/
916 * There are a few other interesting ideas on that page (look under
917 * "Motivation"). Use of netlink events is most interesting. Think
918 * of various network servers listening for events and reconfiguring.
919 * That would obsolete sending HUP signals and/or make use of restarts.
921 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
924 * TODO: make conditional?
926 static const struct sock_filter filter_instr[] = {
928 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
929 /* L5, L1, is UDP? */
930 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
931 /* ugly check for arp on ethernet-like and IPv4 */
932 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
933 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
935 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
936 /* check udp source and destination ports */
937 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
939 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
941 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
942 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
945 static const struct sock_fprog filter_prog = {
946 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
947 /* casting const away: */
948 .filter = (struct sock_filter *) filter_instr,
951 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
955 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
956 /* Use only if standard ports are in use */
957 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
958 sizeof(filter_prog));
960 memset(&sock, 0, sizeof(sock));
961 sock.sll_family = AF_PACKET;
962 sock.sll_protocol = htons(ETH_P_IP);
963 sock.sll_ifindex = ifindex;
965 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
973 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
975 if (packet->ip.protocol != IPPROTO_UDP)
978 if (packet->ip.version != IPVERSION)
981 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
984 if (packet->udp.dest != htons(CLIENT_PORT))
987 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
993 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
996 struct ip_udp_dhcp_packet packet;
999 memset(&packet, 0, sizeof(packet));
1001 bytes = read(fd, &packet, sizeof(packet));
1005 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1008 if (bytes < ntohs(packet.ip.tot_len))
1009 /* packet is bigger than sizeof(packet), we did partial read */
1012 /* ignore any extra garbage bytes */
1013 bytes = ntohs(packet.ip.tot_len);
1015 if (sanity_check(&packet, bytes) == FALSE)
1018 check = packet.ip.check;
1019 packet.ip.check = 0;
1020 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1023 /* verify UDP checksum. IP header has to be modified for this */
1024 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1025 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1026 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1027 check = packet.udp.check;
1028 packet.udp.check = 0;
1029 if (check && check != dhcp_checksum(&packet, bytes))
1032 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1033 sizeof(packet.udp)));
1035 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1038 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1041 static void ipv4ll_start(GDHCPClient *dhcp_client)
1046 if (dhcp_client->timeout > 0) {
1047 g_source_remove(dhcp_client->timeout);
1048 dhcp_client->timeout = 0;
1051 switch_listening_mode(dhcp_client, L_NONE);
1052 dhcp_client->type = G_DHCP_IPV4LL;
1053 dhcp_client->retry_times = 0;
1054 dhcp_client->requested_ip = 0;
1056 /*try to start with a based mac address ip*/
1057 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1058 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1060 /*first wait a random delay to avoid storm of arp request on boot*/
1061 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1063 dhcp_client->retry_times++;
1064 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1071 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1074 switch_listening_mode(dhcp_client, L_NONE);
1076 if (dhcp_client->timeout > 0)
1077 g_source_remove(dhcp_client->timeout);
1079 if (dhcp_client->listener_watch > 0) {
1080 g_source_remove(dhcp_client->listener_watch);
1081 dhcp_client->listener_watch = 0;
1084 dhcp_client->state = IPV4LL_PROBE;
1085 dhcp_client->retry_times = 0;
1086 dhcp_client->requested_ip = 0;
1088 g_free(dhcp_client->assigned_ip);
1089 dhcp_client->assigned_ip = NULL;
1092 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1095 struct ether_arp arp;
1096 uint32_t ip_requested;
1097 int source_conflict;
1098 int target_conflict;
1100 memset(&arp, 0, sizeof(arp));
1102 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1106 if (arp.arp_op != htons(ARPOP_REPLY) &&
1107 arp.arp_op != htons(ARPOP_REQUEST))
1110 ip_requested = ntohl(dhcp_client->requested_ip);
1111 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1112 sizeof(ip_requested));
1114 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1115 sizeof(ip_requested));
1117 if (!source_conflict && !target_conflict)
1120 dhcp_client->conflicts++;
1122 debug(dhcp_client, "IPV4LL conflict detected");
1124 if (dhcp_client->state == IPV4LL_MONITOR) {
1125 if (!source_conflict)
1127 dhcp_client->state = IPV4LL_DEFEND;
1128 debug(dhcp_client, "DEFEND mode conflicts : %d",
1129 dhcp_client->conflicts);
1130 /*Try to defend with a single announce*/
1131 send_announce_packet(dhcp_client);
1135 if (dhcp_client->state == IPV4LL_DEFEND) {
1136 if (!source_conflict)
1138 else if (dhcp_client->ipv4ll_lost_cb != NULL)
1139 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1140 dhcp_client->ipv4ll_lost_data);
1143 ipv4ll_stop(dhcp_client);
1145 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1146 /*restart whole state machine*/
1147 dhcp_client->retry_times++;
1148 dhcp_client->timeout =
1149 g_timeout_add_full(G_PRIORITY_HIGH,
1150 ipv4ll_random_delay_ms(PROBE_WAIT),
1155 /* Here we got a lot of conflicts, RFC3927 states that we have
1156 * to wait RATE_LIMIT_INTERVAL before retrying,
1157 * but we just report failure.
1159 else if (dhcp_client->no_lease_cb != NULL)
1160 dhcp_client->no_lease_cb(dhcp_client,
1161 dhcp_client->no_lease_data);
1166 static gboolean check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1168 if (dhcp_client->type == G_DHCP_IPV6) {
1169 struct dhcpv6_packet *packet6 = pkt;
1172 if (packet6 == NULL)
1175 xid = packet6->transaction_id[0] << 16 |
1176 packet6->transaction_id[1] << 8 |
1177 packet6->transaction_id[2];
1179 if (xid != dhcp_client->xid)
1182 struct dhcp_packet *packet = pkt;
1184 if (packet->xid != dhcp_client->xid)
1187 if (packet->hlen != 6)
1190 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1197 static void start_request(GDHCPClient *dhcp_client);
1199 static gboolean request_timeout(gpointer user_data)
1201 GDHCPClient *dhcp_client = user_data;
1203 debug(dhcp_client, "request timeout (retries %d)",
1204 dhcp_client->retry_times);
1206 dhcp_client->retry_times++;
1208 start_request(dhcp_client);
1213 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1214 gpointer user_data);
1216 static int switch_listening_mode(GDHCPClient *dhcp_client,
1217 ListenMode listen_mode)
1219 GIOChannel *listener_channel;
1220 int listener_sockfd;
1222 if (dhcp_client->listen_mode == listen_mode)
1225 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1226 dhcp_client->listen_mode, listen_mode);
1228 if (dhcp_client->listen_mode != L_NONE) {
1229 if (dhcp_client->listener_watch > 0)
1230 g_source_remove(dhcp_client->listener_watch);
1231 dhcp_client->listener_channel = NULL;
1232 dhcp_client->listen_mode = L_NONE;
1233 dhcp_client->listener_sockfd = -1;
1234 dhcp_client->listener_watch = 0;
1237 if (listen_mode == L_NONE)
1240 if (listen_mode == L2)
1241 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1242 else if (listen_mode == L3) {
1243 if (dhcp_client->type == G_DHCP_IPV6)
1244 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1245 dhcp_client->interface,
1248 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1249 dhcp_client->interface,
1251 } else if (listen_mode == L_ARP)
1252 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1256 if (listener_sockfd < 0)
1259 listener_channel = g_io_channel_unix_new(listener_sockfd);
1260 if (listener_channel == NULL) {
1261 /* Failed to create listener channel */
1262 close(listener_sockfd);
1266 dhcp_client->listen_mode = listen_mode;
1267 dhcp_client->listener_sockfd = listener_sockfd;
1268 dhcp_client->listener_channel = listener_channel;
1270 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1271 dhcp_client->listener_watch =
1272 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1273 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1274 listener_event, dhcp_client,
1276 g_io_channel_unref(dhcp_client->listener_channel);
1281 static void start_request(GDHCPClient *dhcp_client)
1283 debug(dhcp_client, "start request (retries %d)",
1284 dhcp_client->retry_times);
1286 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1287 dhcp_client->state = INIT_SELECTING;
1288 ipv4ll_start(dhcp_client);
1293 if (dhcp_client->retry_times == 0) {
1294 dhcp_client->state = REQUESTING;
1295 switch_listening_mode(dhcp_client, L2);
1298 send_select(dhcp_client);
1300 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1307 static uint32_t get_lease(struct dhcp_packet *packet)
1310 uint32_t lease_seconds;
1312 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
1313 if (option_u8 == NULL)
1316 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
1317 lease_seconds = ntohl(lease_seconds);
1318 /* paranoia: must not be prone to overflows */
1319 lease_seconds &= 0x0fffffff;
1320 if (lease_seconds < 10)
1323 return lease_seconds;
1326 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1328 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1330 if (dhcp_client->timeout > 0) {
1331 g_source_remove(dhcp_client->timeout);
1332 dhcp_client->timeout = 0;
1335 dhcp_client->retry_times = retry_times;
1336 dhcp_client->requested_ip = 0;
1337 switch_listening_mode(dhcp_client, L2);
1339 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1342 static gboolean start_rebound_timeout(gpointer user_data)
1344 GDHCPClient *dhcp_client = user_data;
1346 debug(dhcp_client, "start rebound timeout");
1348 switch_listening_mode(dhcp_client, L2);
1350 dhcp_client->lease_seconds >>= 1;
1352 /* We need to have enough time to receive ACK package*/
1353 if (dhcp_client->lease_seconds <= 6) {
1355 /* ip need to be cleared */
1356 if (dhcp_client->lease_lost_cb != NULL)
1357 dhcp_client->lease_lost_cb(dhcp_client,
1358 dhcp_client->lease_lost_data);
1360 restart_dhcp(dhcp_client, 0);
1362 send_rebound(dhcp_client);
1364 dhcp_client->timeout =
1365 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1366 dhcp_client->lease_seconds >> 1,
1367 start_rebound_timeout,
1375 static void start_rebound(GDHCPClient *dhcp_client)
1377 debug(dhcp_client, "start rebound");
1379 dhcp_client->state = REBINDING;
1381 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1382 dhcp_client->lease_seconds >> 1,
1383 start_rebound_timeout,
1388 static gboolean start_renew_timeout(gpointer user_data)
1390 GDHCPClient *dhcp_client = user_data;
1392 debug(dhcp_client, "start renew timeout");
1394 dhcp_client->state = RENEWING;
1396 dhcp_client->lease_seconds >>= 1;
1398 switch_listening_mode(dhcp_client, L3);
1399 if (dhcp_client->lease_seconds <= 60)
1400 start_rebound(dhcp_client);
1402 send_renew(dhcp_client);
1404 if (dhcp_client->timeout > 0)
1405 g_source_remove(dhcp_client->timeout);
1407 dhcp_client->timeout =
1408 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1409 dhcp_client->lease_seconds >> 1,
1410 start_renew_timeout,
1418 static void start_bound(GDHCPClient *dhcp_client)
1420 debug(dhcp_client, "start bound");
1422 dhcp_client->state = BOUND;
1424 if (dhcp_client->timeout > 0)
1425 g_source_remove(dhcp_client->timeout);
1427 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1428 dhcp_client->lease_seconds >> 1,
1429 start_renew_timeout, dhcp_client,
1433 static gboolean restart_dhcp_timeout(gpointer user_data)
1435 GDHCPClient *dhcp_client = user_data;
1437 debug(dhcp_client, "restart DHCP timeout");
1439 dhcp_client->ack_retry_times++;
1441 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1446 static char *get_ip(uint32_t ip)
1448 struct in_addr addr;
1452 return g_strdup(inet_ntoa(addr));
1455 /* get a rough idea of how long an option will be */
1456 static const uint8_t len_of_option_as_string[] = {
1457 [OPTION_IP] = sizeof("255.255.255.255 "),
1458 [OPTION_STRING] = 1,
1459 [OPTION_U8] = sizeof("255 "),
1460 [OPTION_U16] = sizeof("65535 "),
1461 [OPTION_U32] = sizeof("4294967295 "),
1464 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1466 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1469 /* Create "opt_value1 option_value2 ..." string */
1470 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1472 unsigned upper_length;
1476 len = option[OPT_LEN - OPT_DATA];
1477 type &= OPTION_TYPE_MASK;
1478 optlen = dhcp_option_lengths[type];
1481 upper_length = len_of_option_as_string[type] *
1482 ((unsigned)len / (unsigned)optlen);
1483 dest = ret = malloc(upper_length + 1);
1487 while (len >= optlen) {
1490 dest += sprint_nip(dest, "", option);
1493 uint16_t val_u16 = dhcp_get_unaligned(
1494 (uint16_t *) option);
1495 dest += sprintf(dest, "%u", ntohs(val_u16));
1499 uint32_t val_u32 = dhcp_get_unaligned(
1500 (uint32_t *) option);
1501 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
1502 "%ld", (unsigned long) ntohl(val_u32));
1506 memcpy(dest, option, len);
1523 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1531 if (type == OPTION_STRING)
1532 return g_list_append(list, g_strdup(value));
1534 while ((pos = strchr(pos, ' ')) != NULL) {
1537 list = g_list_append(list, g_strdup(value));
1542 list = g_list_append(list, g_strdup(value));
1547 static inline uint32_t get_uint32(unsigned char *value)
1549 return value[0] << 24 | value[1] << 16 |
1550 value[2] << 8 | value[3];
1553 static inline uint16_t get_uint16(unsigned char *value)
1555 return value[0] << 8 | value[1];
1558 static GList *get_addresses(GDHCPClient *dhcp_client,
1560 unsigned char *value,
1564 struct in6_addr addr;
1565 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1566 uint16_t option_len, option_code, st = 0, max_len;
1567 int addr_count = 0, i, pos;
1571 iaid = get_uint32(&value[0]);
1572 if (dhcp_client->iaid != iaid)
1575 if (code == G_DHCPV6_IA_NA) {
1576 T1 = get_uint32(&value[4]);
1577 T2 = get_uint32(&value[8]);
1580 /* RFC 3315, 22.4 */
1590 max_len = len - pos;
1592 /* We have more sub-options in this packet. */
1594 option = dhcpv6_get_sub_option(&value[pos], max_len,
1595 &option_code, &option_len);
1597 debug(dhcp_client, "pos %d option %p code %d len %d",
1598 pos, option, option_code, option_len);
1606 switch (option_code) {
1607 case G_DHCPV6_IAADDR:
1609 memcpy(&addr, &option[0], sizeof(addr));
1611 preferred = get_uint32(&option[i]);
1613 valid = get_uint32(&option[i]);
1618 case G_DHCPV6_STATUS_CODE:
1619 st = get_uint16(&option[0]);
1620 debug(dhcp_client, "error code %d", st);
1621 if (option_len > 2) {
1622 str = g_strndup((gchar *)&option[2],
1624 debug(dhcp_client, "error text: %s", str);
1632 pos += 2 + 2 + option_len;
1634 } while (option != NULL);
1636 if (addr_count > 0 && st == 0) {
1637 /* We only support one address atm */
1638 char str[INET6_ADDRSTRLEN + 1];
1640 if (preferred > valid)
1641 /* RFC 3315, 22.6 */
1644 dhcp_client->T1 = T1;
1645 dhcp_client->T2 = T2;
1647 inet_ntop(AF_INET6, &addr, str, INET6_ADDRSTRLEN);
1648 debug(dhcp_client, "count %d addr %s T1 %u T2 %u",
1649 addr_count, str, T1, T2);
1651 list = g_list_append(list, g_strdup(str));
1653 if (code == G_DHCPV6_IA_NA)
1654 memcpy(&dhcp_client->ia_na, &addr,
1655 sizeof(struct in6_addr));
1657 memcpy(&dhcp_client->ia_ta, &addr,
1658 sizeof(struct in6_addr));
1664 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
1666 unsigned char *value,
1674 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
1675 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
1678 "%s server list length (%d) is invalid",
1679 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
1683 for (i = 0; i < len; i += 16) {
1685 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
1689 if (inet_ntop(AF_INET6, &value[i], str,
1690 INET6_ADDRSTRLEN) == NULL)
1693 list = g_list_append(list, str);
1697 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
1698 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
1699 list = get_addresses(dhcp_client, code, len, value, status);
1709 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
1710 struct dhcpv6_packet *packet,
1711 uint16_t pkt_len, uint16_t *status)
1713 GList *list, *value_list;
1716 uint16_t option_len;
1718 for (list = dhcp_client->request_list; list; list = list->next) {
1719 code = (uint16_t) GPOINTER_TO_INT(list->data);
1721 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
1723 if (option == NULL) {
1724 g_hash_table_remove(dhcp_client->code_value_hash,
1725 GINT_TO_POINTER((int) code));
1729 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
1730 option_len, option, status);
1732 debug(dhcp_client, "code %d %p len %d list %p", code, option,
1733 option_len, value_list);
1735 if (value_list == NULL)
1736 g_hash_table_remove(dhcp_client->code_value_hash,
1737 GINT_TO_POINTER((int) code));
1739 g_hash_table_insert(dhcp_client->code_value_hash,
1740 GINT_TO_POINTER((int) code), value_list);
1744 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
1746 GDHCPOptionType type;
1747 GList *list, *value_list;
1752 for (list = dhcp_client->request_list; list; list = list->next) {
1753 code = (uint8_t) GPOINTER_TO_INT(list->data);
1755 option = dhcp_get_option(packet, code);
1756 if (option == NULL) {
1757 g_hash_table_remove(dhcp_client->code_value_hash,
1758 GINT_TO_POINTER((int) code));
1762 type = dhcp_get_code_type(code);
1764 option_value = malloc_option_value_string(option, type);
1765 if (option_value == NULL)
1766 g_hash_table_remove(dhcp_client->code_value_hash,
1767 GINT_TO_POINTER((int) code));
1769 value_list = get_option_value_list(option_value, type);
1771 g_free(option_value);
1773 if (value_list == NULL)
1774 g_hash_table_remove(dhcp_client->code_value_hash,
1775 GINT_TO_POINTER((int) code));
1777 g_hash_table_insert(dhcp_client->code_value_hash,
1778 GINT_TO_POINTER((int) code), value_list);
1782 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1785 GDHCPClient *dhcp_client = user_data;
1786 struct dhcp_packet packet;
1787 struct dhcpv6_packet *packet6 = NULL;
1788 uint8_t *message_type = NULL, *client_id = NULL, *option_u8,
1790 uint16_t option_len = 0, status = 0;
1792 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
1793 uint16_t pkt_len = 0;
1797 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1798 dhcp_client->listener_watch = 0;
1802 if (dhcp_client->listen_mode == L_NONE)
1807 if (dhcp_client->listen_mode == L2)
1808 re = dhcp_recv_l2_packet(&packet,
1809 dhcp_client->listener_sockfd);
1810 else if (dhcp_client->listen_mode == L3) {
1811 if (dhcp_client->type == G_DHCP_IPV6) {
1812 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
1813 dhcp_client->listener_sockfd);
1817 re = dhcp_recv_l3_packet(&packet,
1818 dhcp_client->listener_sockfd);
1819 } else if (dhcp_client->listen_mode == L_ARP) {
1820 re = ipv4ll_recv_arp_packet(dhcp_client);
1829 if (check_package_owner(dhcp_client, pkt) == FALSE)
1832 if (dhcp_client->type == G_DHCP_IPV6) {
1834 client_id = dhcpv6_get_option(packet6, pkt_len,
1835 G_DHCPV6_CLIENTID, &option_len, &count);
1837 if (client_id == NULL || count == 0 || option_len == 0 ||
1838 memcmp(dhcp_client->duid, client_id,
1839 dhcp_client->duid_len) != 0) {
1841 "client duid error, discarding msg %p/%d/%d",
1842 client_id, option_len, count);
1846 option_u8 = dhcpv6_get_option(packet6, pkt_len,
1847 G_DHCPV6_STATUS_CODE, &option_len, NULL);
1848 if (option_u8 != 0 && option_len > 0) {
1849 status = option_u8[0]<<8 | option_u8[1];
1851 debug(dhcp_client, "error code %d", status);
1852 if (option_len > 2) {
1853 gchar *txt = g_strndup(
1854 (gchar *)&option_u8[2],
1856 debug(dhcp_client, "error text: %s",
1861 dhcp_client->status_code = status;
1863 dhcp_client->status_code = 0;
1866 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1868 if (message_type == NULL && client_id == NULL)
1869 /* No message type / client id option, ignore package */
1872 debug(dhcp_client, "received DHCP packet (current state %d)",
1873 dhcp_client->state);
1875 switch (dhcp_client->state) {
1876 case INIT_SELECTING:
1877 if (*message_type != DHCPOFFER)
1880 g_source_remove(dhcp_client->timeout);
1881 dhcp_client->timeout = 0;
1882 dhcp_client->retry_times = 0;
1884 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
1885 dhcp_client->server_ip =
1886 dhcp_get_unaligned((uint32_t *) option_u8);
1887 dhcp_client->requested_ip = packet.yiaddr;
1889 dhcp_client->state = REQUESTING;
1891 start_request(dhcp_client);
1897 if (*message_type == DHCPACK) {
1898 dhcp_client->retry_times = 0;
1900 if (dhcp_client->timeout > 0)
1901 g_source_remove(dhcp_client->timeout);
1902 dhcp_client->timeout = 0;
1904 dhcp_client->lease_seconds = get_lease(&packet);
1906 get_request(dhcp_client, &packet);
1908 switch_listening_mode(dhcp_client, L_NONE);
1910 g_free(dhcp_client->assigned_ip);
1911 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1913 /* Address should be set up here */
1914 if (dhcp_client->lease_available_cb != NULL)
1915 dhcp_client->lease_available_cb(dhcp_client,
1916 dhcp_client->lease_available_data);
1918 start_bound(dhcp_client);
1919 } else if (*message_type == DHCPNAK) {
1920 dhcp_client->retry_times = 0;
1922 if (dhcp_client->timeout > 0)
1923 g_source_remove(dhcp_client->timeout);
1925 dhcp_client->timeout = g_timeout_add_seconds_full(
1927 restart_dhcp_timeout,
1934 if (dhcp_client->type != G_DHCP_IPV6)
1937 if (packet6->message != DHCPV6_REPLY &&
1938 packet6->message != DHCPV6_ADVERTISE)
1942 server_id = dhcpv6_get_option(packet6, pkt_len,
1943 G_DHCPV6_SERVERID, &option_len, &count);
1944 if (server_id == NULL || count != 1 || option_len == 0) {
1945 /* RFC 3315, 15.10 */
1947 "server duid error, discarding msg %p/%d/%d",
1948 server_id, option_len, count);
1951 dhcp_client->server_duid = g_try_malloc(option_len);
1952 if (dhcp_client->server_duid == NULL)
1954 memcpy(dhcp_client->server_duid, server_id, option_len);
1955 dhcp_client->server_duid_len = option_len;
1957 if (packet6->message == DHCPV6_REPLY) {
1958 uint8_t *rapid_commit;
1961 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
1962 G_DHCPV6_RAPID_COMMIT,
1963 &option_len, &count);
1964 if (rapid_commit == NULL || option_len == 0 ||
1966 /* RFC 3315, 17.1.4 */
1970 switch_listening_mode(dhcp_client, L_NONE);
1972 if (dhcp_client->status_code == 0)
1973 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
1974 &dhcp_client->status_code);
1976 if (packet6->message == DHCPV6_ADVERTISE) {
1977 if (dhcp_client->advertise_cb != NULL)
1978 dhcp_client->advertise_cb(dhcp_client,
1979 dhcp_client->advertise_data);
1983 if (dhcp_client->solicitation_cb != NULL) {
1985 * The dhcp_client might not be valid after the
1986 * callback call so just return immediately.
1988 dhcp_client->solicitation_cb(dhcp_client,
1989 dhcp_client->solicitation_data);
1993 case INFORMATION_REQ:
1997 if (dhcp_client->type != G_DHCP_IPV6)
2000 if (packet6->message != DHCPV6_REPLY)
2005 server_id = dhcpv6_get_option(packet6, pkt_len,
2006 G_DHCPV6_SERVERID, &option_len, &count);
2007 if (server_id == NULL || count != 1 || option_len == 0 ||
2008 (dhcp_client->server_duid_len > 0 &&
2009 memcmp(dhcp_client->server_duid, server_id,
2010 dhcp_client->server_duid_len) != 0)) {
2011 /* RFC 3315, 15.10 */
2013 "server duid error, discarding msg %p/%d/%d",
2014 server_id, option_len, count);
2018 switch_listening_mode(dhcp_client, L_NONE);
2020 dhcp_client->status_code = 0;
2021 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2022 &dhcp_client->status_code);
2024 if (dhcp_client->information_req_cb != NULL) {
2026 * The dhcp_client might not be valid after the
2027 * callback call so just return immediately.
2029 dhcp_client->information_req_cb(dhcp_client,
2030 dhcp_client->information_req_data);
2033 if (dhcp_client->request_cb != NULL) {
2034 dhcp_client->request_cb(dhcp_client,
2035 dhcp_client->request_data);
2038 if (dhcp_client->renew_cb != NULL) {
2039 dhcp_client->renew_cb(dhcp_client,
2040 dhcp_client->renew_data);
2043 if (dhcp_client->rebind_cb != NULL) {
2044 dhcp_client->rebind_cb(dhcp_client,
2045 dhcp_client->rebind_data);
2053 debug(dhcp_client, "processed DHCP packet (new state %d)",
2054 dhcp_client->state);
2059 static gboolean discover_timeout(gpointer user_data)
2061 GDHCPClient *dhcp_client = user_data;
2063 dhcp_client->retry_times++;
2066 * We do not send the REQUESTED IP option if we are retrying because
2067 * if the server is non-authoritative it will ignore the request if the
2068 * option is present.
2070 g_dhcp_client_start(dhcp_client, NULL);
2075 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2077 GDHCPClient *dhcp_client = dhcp_data;
2079 debug(dhcp_client, "back to MONITOR mode");
2081 dhcp_client->conflicts = 0;
2082 dhcp_client->state = IPV4LL_MONITOR;
2087 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2089 GDHCPClient *dhcp_client = dhcp_data;
2092 debug(dhcp_client, "request timeout (retries %d)",
2093 dhcp_client->retry_times);
2095 if (dhcp_client->retry_times != ANNOUNCE_NUM){
2096 dhcp_client->retry_times++;
2097 send_announce_packet(dhcp_client);
2101 ip = htonl(dhcp_client->requested_ip);
2102 debug(dhcp_client, "switching to monitor mode");
2103 dhcp_client->state = IPV4LL_MONITOR;
2104 dhcp_client->assigned_ip = get_ip(ip);
2106 if (dhcp_client->ipv4ll_available_cb != NULL)
2107 dhcp_client->ipv4ll_available_cb(dhcp_client,
2108 dhcp_client->ipv4ll_available_data);
2109 dhcp_client->conflicts = 0;
2114 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2117 GDHCPClient *dhcp_client = dhcp_data;
2119 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2120 dhcp_client->retry_times);
2122 if (dhcp_client->retry_times == PROBE_NUM) {
2123 dhcp_client->state = IPV4LL_ANNOUNCE;
2124 dhcp_client->retry_times = 0;
2126 dhcp_client->retry_times++;
2127 send_announce_packet(dhcp_client);
2130 dhcp_client->retry_times++;
2131 send_probe_packet(dhcp_client);
2136 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2141 if (dhcp_client->type == G_DHCP_IPV6) {
2142 if (dhcp_client->information_req_cb) {
2143 dhcp_client->state = INFORMATION_REQ;
2144 re = switch_listening_mode(dhcp_client, L3);
2146 switch_listening_mode(dhcp_client, L_NONE);
2147 dhcp_client->state = 0;
2150 send_information_req(dhcp_client);
2152 } else if (dhcp_client->solicitation_cb) {
2153 dhcp_client->state = SOLICITATION;
2154 re = switch_listening_mode(dhcp_client, L3);
2156 switch_listening_mode(dhcp_client, L_NONE);
2157 dhcp_client->state = 0;
2160 send_solicitation(dhcp_client);
2162 } else if (dhcp_client->request_cb) {
2163 dhcp_client->state = REQUEST;
2164 re = switch_listening_mode(dhcp_client, L3);
2166 switch_listening_mode(dhcp_client, L_NONE);
2167 dhcp_client->state = 0;
2170 send_dhcpv6_request(dhcp_client);
2172 } else if (dhcp_client->renew_cb) {
2173 dhcp_client->state = RENEW;
2174 re = switch_listening_mode(dhcp_client, L3);
2176 switch_listening_mode(dhcp_client, L_NONE);
2177 dhcp_client->state = 0;
2180 send_dhcpv6_renew(dhcp_client);
2182 } else if (dhcp_client->rebind_cb) {
2183 dhcp_client->state = REBIND;
2184 re = switch_listening_mode(dhcp_client, L3);
2186 switch_listening_mode(dhcp_client, L_NONE);
2187 dhcp_client->state = 0;
2190 send_dhcpv6_rebind(dhcp_client);
2196 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2197 ipv4ll_start(dhcp_client);
2201 if (dhcp_client->retry_times == 0) {
2202 g_free(dhcp_client->assigned_ip);
2203 dhcp_client->assigned_ip = NULL;
2205 dhcp_client->state = INIT_SELECTING;
2206 re = switch_listening_mode(dhcp_client, L2);
2210 dhcp_client->xid = rand();
2213 if (last_address == NULL) {
2216 addr = inet_addr(last_address);
2217 if (addr == 0xFFFFFFFF) {
2220 g_free(dhcp_client->last_address);
2221 dhcp_client->last_address = g_strdup(last_address);
2224 send_discover(dhcp_client, addr);
2226 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2234 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2236 switch_listening_mode(dhcp_client, L_NONE);
2238 if (dhcp_client->state == BOUND ||
2239 dhcp_client->state == RENEWING ||
2240 dhcp_client->state == REBINDING)
2241 send_release(dhcp_client, dhcp_client->server_ip,
2242 dhcp_client->requested_ip);
2244 if (dhcp_client->timeout > 0) {
2245 g_source_remove(dhcp_client->timeout);
2246 dhcp_client->timeout = 0;
2249 if (dhcp_client->listener_watch > 0) {
2250 g_source_remove(dhcp_client->listener_watch);
2251 dhcp_client->listener_watch = 0;
2254 dhcp_client->listener_channel = NULL;
2256 dhcp_client->retry_times = 0;
2257 dhcp_client->ack_retry_times = 0;
2259 dhcp_client->requested_ip = 0;
2260 dhcp_client->state = RELEASED;
2261 dhcp_client->lease_seconds = 0;
2264 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2265 unsigned char option_code)
2267 return g_hash_table_lookup(dhcp_client->code_value_hash,
2268 GINT_TO_POINTER((int) option_code));
2271 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2272 GDHCPClientEvent event,
2273 GDHCPClientEventFunc func,
2277 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2278 dhcp_client->lease_available_cb = func;
2279 dhcp_client->lease_available_data = data;
2281 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2282 if (dhcp_client->type == G_DHCP_IPV6)
2284 dhcp_client->ipv4ll_available_cb = func;
2285 dhcp_client->ipv4ll_available_data = data;
2287 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2288 dhcp_client->no_lease_cb = func;
2289 dhcp_client->no_lease_data = data;
2291 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2292 dhcp_client->lease_lost_cb = func;
2293 dhcp_client->lease_lost_data = data;
2295 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2296 if (dhcp_client->type == G_DHCP_IPV6)
2298 dhcp_client->ipv4ll_lost_cb = func;
2299 dhcp_client->ipv4ll_lost_data = data;
2301 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2302 dhcp_client->address_conflict_cb = func;
2303 dhcp_client->address_conflict_data = data;
2305 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2306 if (dhcp_client->type == G_DHCP_IPV4)
2308 dhcp_client->information_req_cb = func;
2309 dhcp_client->information_req_data = data;
2311 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2312 if (dhcp_client->type == G_DHCP_IPV4)
2314 dhcp_client->solicitation_cb = func;
2315 dhcp_client->solicitation_data = data;
2317 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2318 if (dhcp_client->type == G_DHCP_IPV4)
2320 dhcp_client->advertise_cb = func;
2321 dhcp_client->advertise_data = data;
2323 case G_DHCP_CLIENT_EVENT_REQUEST:
2324 if (dhcp_client->type == G_DHCP_IPV4)
2326 dhcp_client->request_cb = func;
2327 dhcp_client->request_data = data;
2329 case G_DHCP_CLIENT_EVENT_RENEW:
2330 if (dhcp_client->type == G_DHCP_IPV4)
2332 dhcp_client->renew_cb = func;
2333 dhcp_client->renew_data = data;
2335 case G_DHCP_CLIENT_EVENT_REBIND:
2336 if (dhcp_client->type == G_DHCP_IPV4)
2338 dhcp_client->rebind_cb = func;
2339 dhcp_client->rebind_data = data;
2344 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2346 return dhcp_client->ifindex;
2349 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2351 return g_strdup(dhcp_client->assigned_ip);
2354 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2356 GList *option = NULL;
2358 if (dhcp_client->type == G_DHCP_IPV6)
2361 switch (dhcp_client->state) {
2363 case IPV4LL_MONITOR:
2364 return g_strdup("255.255.0.0");
2368 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2370 return g_strdup(option->data);
2371 case INIT_SELECTING:
2375 case IPV4LL_ANNOUNCE:
2376 case INFORMATION_REQ:
2386 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2387 unsigned int option_code)
2389 if (g_list_find(dhcp_client->request_list,
2390 GINT_TO_POINTER((int) option_code)) == NULL)
2391 dhcp_client->request_list = g_list_prepend(
2392 dhcp_client->request_list,
2393 (GINT_TO_POINTER((int) option_code)));
2395 return G_DHCP_CLIENT_ERROR_NONE;
2398 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2400 g_list_free(dhcp_client->request_list);
2401 dhcp_client->request_list = NULL;
2404 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2406 g_hash_table_remove_all(dhcp_client->send_value_hash);
2409 static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
2412 int len = strnlen(str, 255);
2414 storage = malloc(len + extra + OPT_DATA);
2415 storage[OPT_CODE] = code;
2416 storage[OPT_LEN] = len + extra;
2417 memcpy(storage + extra + OPT_DATA, str, len);
2422 /* Now only support send hostname */
2423 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
2424 unsigned char option_code, const char *option_value)
2426 uint8_t *binary_option;
2428 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
2429 binary_option = alloc_dhcp_option(option_code,
2432 g_hash_table_insert(dhcp_client->send_value_hash,
2433 GINT_TO_POINTER((int) option_code), binary_option);
2436 return G_DHCP_CLIENT_ERROR_NONE;
2439 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
2444 storage = g_malloc(2 + 2 + len);
2445 if (storage == NULL)
2448 storage[0] = code >> 8;
2449 storage[1] = code & 0xff;
2450 storage[2] = len >> 8;
2451 storage[3] = len & 0xff;
2452 memcpy(storage + 2 + 2, option, len);
2457 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
2458 uint16_t option_code,
2459 uint8_t *option_value,
2460 uint16_t option_len)
2462 if (option_value != NULL) {
2463 uint8_t *binary_option;
2465 debug(dhcp_client, "setting option %d to %p len %d",
2466 option_code, option_value, option_len);
2468 binary_option = alloc_dhcpv6_option(option_code, option_value,
2470 if (binary_option != NULL)
2471 g_hash_table_insert(dhcp_client->send_value_hash,
2472 GINT_TO_POINTER((int) option_code),
2477 void g_dhcpv6_client_reset_renew(GDHCPClient *dhcp_client)
2479 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2482 dhcp_client->last_renew = time(0);
2485 void g_dhcpv6_client_reset_rebind(GDHCPClient *dhcp_client)
2487 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2490 dhcp_client->last_rebind = time(0);
2493 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
2495 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2498 return dhcp_client->status_code;
2501 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
2503 if (dhcp_client == NULL)
2506 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
2511 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
2513 if (dhcp_client == NULL)
2516 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
2519 g_dhcp_client_stop(dhcp_client);
2521 g_free(dhcp_client->interface);
2522 g_free(dhcp_client->assigned_ip);
2523 g_free(dhcp_client->last_address);
2524 g_free(dhcp_client->duid);
2525 g_free(dhcp_client->server_duid);
2527 g_list_free(dhcp_client->request_list);
2528 g_list_free(dhcp_client->require_list);
2530 g_hash_table_destroy(dhcp_client->code_value_hash);
2531 g_hash_table_destroy(dhcp_client->send_value_hash);
2533 g_free(dhcp_client);
2536 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
2537 GDHCPDebugFunc func, gpointer user_data)
2539 if (dhcp_client == NULL)
2542 dhcp_client->debug_func = func;
2543 dhcp_client->debug_data = user_data;