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;
144 static inline void debug(GDHCPClient *client, const char *format, ...)
149 if (client->debug_func == NULL)
152 va_start(ap, format);
154 if (vsnprintf(str, sizeof(str), format, ap) > 0)
155 client->debug_func(str, client->debug_data);
160 /* Initialize the packet with the proper defaults */
161 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
163 if (dhcp_client->type == G_DHCP_IPV6)
164 dhcpv6_init_header(pkt, type);
166 struct dhcp_packet *packet = pkt;
168 dhcp_init_header(packet, type);
169 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
173 static void add_request_options(GDHCPClient *dhcp_client,
174 struct dhcp_packet *packet)
179 int end = dhcp_end_option(packet->options);
181 for (list = dhcp_client->request_list; list; list = list->next) {
182 code = (uint8_t) GPOINTER_TO_INT(list->data);
184 packet->options[end + OPT_DATA + len] = code;
189 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
190 packet->options[end + OPT_LEN] = len;
191 packet->options[end + OPT_DATA + len] = DHCP_END;
198 unsigned char **ptr_buf;
201 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
204 uint8_t *option = value;
206 struct hash_params *params = user_data;
208 /* option[0][1] contains option code */
209 len = option[2] << 8 | option[3];
211 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
214 memcpy(*params->ptr_buf, option, len + 2 + 2);
215 (*params->ptr_buf) += len + 2 + 2;
218 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
219 unsigned char *buf, int max_buf,
220 unsigned char **ptr_buf)
222 struct hash_params params = {
228 if (dhcp_client->type == G_DHCP_IPV4)
231 g_hash_table_foreach(dhcp_client->send_value_hash,
232 add_dhcpv6_binary_option, ¶ms);
234 *ptr_buf = *params.ptr_buf;
237 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
241 buf[1] = code & 0xff;
244 if (len > 0 && msg != NULL)
245 memcpy(&buf[4], msg, len);
248 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
249 struct dhcpv6_packet *packet,
250 unsigned char *buf, int max_buf,
251 unsigned char **ptr_buf)
257 if (dhcp_client->type == G_DHCP_IPV4)
260 for (list = dhcp_client->request_list; list; list = list->next) {
261 code = (uint16_t) GPOINTER_TO_INT(list->data);
264 case G_DHCPV6_CLIENTID:
265 if (dhcp_client->duid == NULL)
268 len = 2 + 2 + dhcp_client->duid_len;
269 if ((*ptr_buf + len) > (buf + max_buf)) {
270 debug(dhcp_client, "Too long dhcpv6 message "
271 "when writing client id option");
275 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
276 dhcp_client->duid_len, dhcp_client->duid);
280 case G_DHCPV6_SERVERID:
281 if (dhcp_client->server_duid == NULL)
284 len = 2 + 2 + dhcp_client->server_duid_len;
285 if ((*ptr_buf + len) > (buf + max_buf)) {
286 debug(dhcp_client, "Too long dhcpv6 message "
287 "when writing server id option");
291 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
292 dhcp_client->server_duid_len,
293 dhcp_client->server_duid);
297 case G_DHCPV6_RAPID_COMMIT:
299 if ((*ptr_buf + len) > (buf + max_buf)) {
300 debug(dhcp_client, "Too long dhcpv6 message "
301 "when writing rapid commit option");
305 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
312 case G_DHCPV6_DNS_SERVERS:
315 case G_DHCPV6_SNTP_SERVERS:
324 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
326 uint8_t *option = value;
327 struct dhcp_packet *packet = user_data;
329 dhcp_add_binary_option(packet, option);
332 static void add_send_options(GDHCPClient *dhcp_client,
333 struct dhcp_packet *packet)
335 g_hash_table_foreach(dhcp_client->send_value_hash,
336 add_binary_option, packet);
339 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
341 struct dhcp_packet packet;
343 debug(dhcp_client, "sending DHCP discover request");
345 init_packet(dhcp_client, &packet, DHCPDISCOVER);
347 packet.xid = dhcp_client->xid;
350 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
352 /* Explicitly saying that we want RFC-compliant packets helps
353 * some buggy DHCP servers to NOT send bigger packets */
354 dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
356 add_request_options(dhcp_client, &packet);
358 add_send_options(dhcp_client, &packet);
360 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
361 INADDR_BROADCAST, SERVER_PORT,
362 MAC_BCAST_ADDR, dhcp_client->ifindex);
365 static int send_select(GDHCPClient *dhcp_client)
367 struct dhcp_packet packet;
369 debug(dhcp_client, "sending DHCP select request");
371 init_packet(dhcp_client, &packet, DHCPREQUEST);
373 packet.xid = dhcp_client->xid;
375 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
376 dhcp_client->requested_ip);
377 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
379 add_request_options(dhcp_client, &packet);
381 add_send_options(dhcp_client, &packet);
383 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
384 INADDR_BROADCAST, SERVER_PORT,
385 MAC_BCAST_ADDR, dhcp_client->ifindex);
388 static int send_renew(GDHCPClient *dhcp_client)
390 struct dhcp_packet packet;
392 debug(dhcp_client, "sending DHCP renew request");
394 init_packet(dhcp_client , &packet, DHCPREQUEST);
395 packet.xid = dhcp_client->xid;
396 packet.ciaddr = dhcp_client->requested_ip;
398 add_request_options(dhcp_client, &packet);
400 add_send_options(dhcp_client, &packet);
402 return dhcp_send_kernel_packet(&packet,
403 dhcp_client->requested_ip, CLIENT_PORT,
404 dhcp_client->server_ip, SERVER_PORT);
407 static int send_rebound(GDHCPClient *dhcp_client)
409 struct dhcp_packet packet;
411 debug(dhcp_client, "sending DHCP rebound request");
413 init_packet(dhcp_client , &packet, DHCPREQUEST);
414 packet.xid = dhcp_client->xid;
415 packet.ciaddr = dhcp_client->requested_ip;
417 add_request_options(dhcp_client, &packet);
419 add_send_options(dhcp_client, &packet);
421 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
422 INADDR_BROADCAST, SERVER_PORT,
423 MAC_BCAST_ADDR, dhcp_client->ifindex);
426 static int send_release(GDHCPClient *dhcp_client,
427 uint32_t server, uint32_t ciaddr)
429 struct dhcp_packet packet;
431 debug(dhcp_client, "sending DHCP release request");
433 init_packet(dhcp_client, &packet, DHCPRELEASE);
435 packet.ciaddr = ciaddr;
437 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
439 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
440 server, SERVER_PORT);
443 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
444 static int switch_listening_mode(GDHCPClient *dhcp_client,
445 ListenMode listen_mode);
447 static gboolean send_probe_packet(gpointer dhcp_data)
449 GDHCPClient *dhcp_client;
452 dhcp_client = dhcp_data;
453 /* if requested_ip is not valid, pick a new address*/
454 if (dhcp_client->requested_ip == 0) {
455 debug(dhcp_client, "pick a new random address");
456 dhcp_client->requested_ip = ipv4ll_random_ip(0);
459 debug(dhcp_client, "sending IPV4LL probe request");
461 if (dhcp_client->retry_times == 1) {
462 dhcp_client->state = IPV4LL_PROBE;
463 switch_listening_mode(dhcp_client, L_ARP);
465 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
466 dhcp_client->requested_ip, dhcp_client->ifindex);
468 if (dhcp_client->retry_times < PROBE_NUM) {
469 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
470 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
471 timeout += PROBE_MIN*1000;
473 timeout = (ANNOUNCE_WAIT * 1000);
475 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
477 ipv4ll_probe_timeout,
483 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
484 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
486 static gboolean send_announce_packet(gpointer dhcp_data)
488 GDHCPClient *dhcp_client;
490 dhcp_client = dhcp_data;
492 debug(dhcp_client, "sending IPV4LL announce request");
494 ipv4ll_send_arp_packet(dhcp_client->mac_address,
495 dhcp_client->requested_ip,
496 dhcp_client->requested_ip,
497 dhcp_client->ifindex);
499 if (dhcp_client->timeout > 0)
500 g_source_remove(dhcp_client->timeout);
501 dhcp_client->timeout = 0;
503 if (dhcp_client->state == IPV4LL_DEFEND) {
504 dhcp_client->timeout =
505 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
507 ipv4ll_defend_timeout,
512 dhcp_client->timeout =
513 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
515 ipv4ll_announce_timeout,
521 static void get_interface_mac_address(int index, uint8_t *mac_address)
526 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
528 perror("Open socket error");
532 memset(&ifr, 0, sizeof(ifr));
533 ifr.ifr_ifindex = index;
535 err = ioctl(sk, SIOCGIFNAME, &ifr);
537 perror("Get interface name error");
541 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
543 perror("Get mac address error");
547 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
553 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
554 unsigned char **duid, int *duid_len)
559 case G_DHCPV6_DUID_LLT:
560 *duid_len = 2 + 2 + 4 + ETH_ALEN;
561 *duid = g_try_malloc(*duid_len);
567 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
570 duid_time = time(0) - DUID_TIME_EPOCH;
571 (*duid)[4] = duid_time >> 24;
572 (*duid)[5] = duid_time >> 16;
573 (*duid)[6] = duid_time >> 8;
574 (*duid)[7] = duid_time & 0xff;
576 case G_DHCPV6_DUID_EN:
578 case G_DHCPV6_DUID_LL:
579 *duid_len = 2 + 2 + ETH_ALEN;
580 *duid = g_try_malloc(*duid_len);
586 get_interface_mac_address(index, &(*duid)[2 + 2]);
595 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
598 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
601 g_free(dhcp_client->duid);
603 dhcp_client->duid = duid;
604 dhcp_client->duid_len = duid_len;
609 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
611 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
614 return dhcp_client->iaid;
617 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
622 get_interface_mac_address(index, buf);
624 memcpy(iaid, &buf[2], 4);
625 dhcp_client->iaid = iaid[0] << 24 |
626 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
629 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
630 uint32_t *T1, uint32_t *T2,
631 time_t *last_renew, time_t *last_rebind,
634 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
638 *T1 = dhcp_client->T1;
641 *T2 = dhcp_client->T2;
643 if (last_renew != NULL)
644 *last_renew = dhcp_client->last_renew;
646 if (last_rebind != NULL)
647 *last_rebind = dhcp_client->last_rebind;
650 *expire = dhcp_client->expire;
655 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
659 buf[1] = G_DHCPV6_IAADDR;
662 memcpy(&buf[4], &dhcp_client->ia_na, 16);
663 memset(&buf[20], 0, 4); /* preferred */
664 memset(&buf[24], 0, 4); /* valid */
668 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
672 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
674 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
684 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
685 int code, uint32_t *T1, uint32_t *T2,
688 if (code == G_DHCPV6_IA_TA) {
689 uint8_t ia_options[4];
691 put_iaid(dhcp_client, index, ia_options);
693 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
694 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
695 ia_options, sizeof(ia_options));
697 } else if (code == G_DHCPV6_IA_NA) {
699 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
701 if (add_iaaddr == TRUE) {
702 #define IAADDR_LEN (16+4+4)
703 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
705 put_iaid(dhcp_client, index, ia_options);
708 ia_options[4] = *T1 >> 24;
709 ia_options[5] = *T1 >> 16;
710 ia_options[6] = *T1 >> 8;
713 memset(&ia_options[4], 0x00, 4);
716 ia_options[8] = *T2 >> 24;
717 ia_options[9] = *T2 >> 16;
718 ia_options[10] = *T2 >> 8;
719 ia_options[11] = *T2;
721 memset(&ia_options[8], 0x00, 4);
723 create_iaaddr(dhcp_client, &ia_options[12],
726 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
727 ia_options, sizeof(ia_options));
729 uint8_t ia_options[4+4+4];
731 put_iaid(dhcp_client, index, ia_options);
733 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
734 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
736 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
737 ia_options, sizeof(ia_options));
746 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
749 int i, j, len = sizeof(uint16_t) * args;
752 values = g_try_malloc(len);
757 for (i = 0, j = 0; i < args; i++) {
758 uint16_t value = va_arg(va, int);
759 values[j++] = value >> 8;
760 values[j++] = value & 0xff;
764 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
769 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
771 struct dhcpv6_packet *packet;
772 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
776 memset(buf, 0, sizeof(buf));
777 packet = (struct dhcpv6_packet *)&buf[0];
778 ptr = buf + sizeof(struct dhcpv6_packet);
780 debug(dhcp_client, "sending DHCPv6 %s message", msg);
782 init_packet(dhcp_client, packet, type);
784 dhcp_client->xid = packet->transaction_id[0] << 16 |
785 packet->transaction_id[1] << 8 |
786 packet->transaction_id[2];
788 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
790 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
792 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
794 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
796 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
800 static int send_solicitation(GDHCPClient *dhcp_client)
802 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
805 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
807 return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
810 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
812 return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
815 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
817 return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
820 static int send_information_req(GDHCPClient *dhcp_client)
822 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
826 static void remove_value(gpointer data, gpointer user_data)
832 static void remove_option_value(gpointer data)
834 GList *option_value = data;
836 g_list_foreach(option_value, remove_value, NULL);
839 GDHCPClient *g_dhcp_client_new(GDHCPType type,
840 int ifindex, GDHCPClientError *error)
842 GDHCPClient *dhcp_client;
845 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
849 dhcp_client = g_try_new0(GDHCPClient, 1);
850 if (dhcp_client == NULL) {
851 *error = G_DHCP_CLIENT_ERROR_NOMEM;
855 dhcp_client->interface = get_interface_name(ifindex);
856 if (dhcp_client->interface == NULL) {
857 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
861 if (interface_is_up(ifindex) == FALSE) {
862 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
866 get_interface_mac_address(ifindex, dhcp_client->mac_address);
868 dhcp_client->listener_sockfd = -1;
869 dhcp_client->listener_channel = NULL;
870 dhcp_client->listen_mode = L_NONE;
871 dhcp_client->ref_count = 1;
872 dhcp_client->type = type;
873 dhcp_client->ifindex = ifindex;
874 dhcp_client->lease_available_cb = NULL;
875 dhcp_client->ipv4ll_available_cb = NULL;
876 dhcp_client->no_lease_cb = NULL;
877 dhcp_client->lease_lost_cb = NULL;
878 dhcp_client->ipv4ll_lost_cb = NULL;
879 dhcp_client->address_conflict_cb = NULL;
880 dhcp_client->listener_watch = 0;
881 dhcp_client->retry_times = 0;
882 dhcp_client->ack_retry_times = 0;
883 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
884 g_direct_equal, NULL, remove_option_value);
885 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
886 g_direct_equal, NULL, g_free);
887 dhcp_client->request_list = NULL;
888 dhcp_client->require_list = NULL;
889 dhcp_client->duid = NULL;
890 dhcp_client->duid_len = 0;
891 dhcp_client->last_renew = dhcp_client->last_rebind = time(0);
892 dhcp_client->expire = 0;
894 *error = G_DHCP_CLIENT_ERROR_NONE;
899 g_free(dhcp_client->interface);
904 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
906 static int dhcp_l2_socket(int ifindex)
909 struct sockaddr_ll sock;
914 * I've selected not to see LL header, so BPF doesn't see it, too.
915 * The filter may also pass non-IP and non-ARP packets, but we do
916 * a more complete check when receiving the message in userspace.
918 * and filter shamelessly stolen from:
920 * http://www.flamewarmaster.de/software/dhcpclient/
922 * There are a few other interesting ideas on that page (look under
923 * "Motivation"). Use of netlink events is most interesting. Think
924 * of various network servers listening for events and reconfiguring.
925 * That would obsolete sending HUP signals and/or make use of restarts.
927 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
930 * TODO: make conditional?
932 static const struct sock_filter filter_instr[] = {
934 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
935 /* L5, L1, is UDP? */
936 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
937 /* ugly check for arp on ethernet-like and IPv4 */
938 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
939 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
941 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
942 /* check udp source and destination ports */
943 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
945 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
947 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
948 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
951 static const struct sock_fprog filter_prog = {
952 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
953 /* casting const away: */
954 .filter = (struct sock_filter *) filter_instr,
957 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
961 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
962 /* Use only if standard ports are in use */
963 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
964 sizeof(filter_prog));
966 memset(&sock, 0, sizeof(sock));
967 sock.sll_family = AF_PACKET;
968 sock.sll_protocol = htons(ETH_P_IP);
969 sock.sll_ifindex = ifindex;
971 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
979 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
981 if (packet->ip.protocol != IPPROTO_UDP)
984 if (packet->ip.version != IPVERSION)
987 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
990 if (packet->udp.dest != htons(CLIENT_PORT))
993 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
999 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
1002 struct ip_udp_dhcp_packet packet;
1005 memset(&packet, 0, sizeof(packet));
1007 bytes = read(fd, &packet, sizeof(packet));
1011 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1014 if (bytes < ntohs(packet.ip.tot_len))
1015 /* packet is bigger than sizeof(packet), we did partial read */
1018 /* ignore any extra garbage bytes */
1019 bytes = ntohs(packet.ip.tot_len);
1021 if (sanity_check(&packet, bytes) == FALSE)
1024 check = packet.ip.check;
1025 packet.ip.check = 0;
1026 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1029 /* verify UDP checksum. IP header has to be modified for this */
1030 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1031 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1032 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1033 check = packet.udp.check;
1034 packet.udp.check = 0;
1035 if (check && check != dhcp_checksum(&packet, bytes))
1038 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1039 sizeof(packet.udp)));
1041 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1044 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1047 static void ipv4ll_start(GDHCPClient *dhcp_client)
1052 if (dhcp_client->timeout > 0) {
1053 g_source_remove(dhcp_client->timeout);
1054 dhcp_client->timeout = 0;
1057 switch_listening_mode(dhcp_client, L_NONE);
1058 dhcp_client->type = G_DHCP_IPV4LL;
1059 dhcp_client->retry_times = 0;
1060 dhcp_client->requested_ip = 0;
1062 /*try to start with a based mac address ip*/
1063 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1064 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1066 /*first wait a random delay to avoid storm of arp request on boot*/
1067 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1069 dhcp_client->retry_times++;
1070 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1077 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1080 switch_listening_mode(dhcp_client, L_NONE);
1082 if (dhcp_client->timeout > 0)
1083 g_source_remove(dhcp_client->timeout);
1085 if (dhcp_client->listener_watch > 0) {
1086 g_source_remove(dhcp_client->listener_watch);
1087 dhcp_client->listener_watch = 0;
1090 dhcp_client->state = IPV4LL_PROBE;
1091 dhcp_client->retry_times = 0;
1092 dhcp_client->requested_ip = 0;
1094 g_free(dhcp_client->assigned_ip);
1095 dhcp_client->assigned_ip = NULL;
1098 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1101 struct ether_arp arp;
1102 uint32_t ip_requested;
1103 int source_conflict;
1104 int target_conflict;
1106 memset(&arp, 0, sizeof(arp));
1108 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1112 if (arp.arp_op != htons(ARPOP_REPLY) &&
1113 arp.arp_op != htons(ARPOP_REQUEST))
1116 ip_requested = ntohl(dhcp_client->requested_ip);
1117 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1118 sizeof(ip_requested));
1120 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1121 sizeof(ip_requested));
1123 if (!source_conflict && !target_conflict)
1126 dhcp_client->conflicts++;
1128 debug(dhcp_client, "IPV4LL conflict detected");
1130 if (dhcp_client->state == IPV4LL_MONITOR) {
1131 if (!source_conflict)
1133 dhcp_client->state = IPV4LL_DEFEND;
1134 debug(dhcp_client, "DEFEND mode conflicts : %d",
1135 dhcp_client->conflicts);
1136 /*Try to defend with a single announce*/
1137 send_announce_packet(dhcp_client);
1141 if (dhcp_client->state == IPV4LL_DEFEND) {
1142 if (!source_conflict)
1144 else if (dhcp_client->ipv4ll_lost_cb != NULL)
1145 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1146 dhcp_client->ipv4ll_lost_data);
1149 ipv4ll_stop(dhcp_client);
1151 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1152 /*restart whole state machine*/
1153 dhcp_client->retry_times++;
1154 dhcp_client->timeout =
1155 g_timeout_add_full(G_PRIORITY_HIGH,
1156 ipv4ll_random_delay_ms(PROBE_WAIT),
1161 /* Here we got a lot of conflicts, RFC3927 states that we have
1162 * to wait RATE_LIMIT_INTERVAL before retrying,
1163 * but we just report failure.
1165 else if (dhcp_client->no_lease_cb != NULL)
1166 dhcp_client->no_lease_cb(dhcp_client,
1167 dhcp_client->no_lease_data);
1172 static gboolean check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1174 if (dhcp_client->type == G_DHCP_IPV6) {
1175 struct dhcpv6_packet *packet6 = pkt;
1178 if (packet6 == NULL)
1181 xid = packet6->transaction_id[0] << 16 |
1182 packet6->transaction_id[1] << 8 |
1183 packet6->transaction_id[2];
1185 if (xid != dhcp_client->xid)
1188 struct dhcp_packet *packet = pkt;
1190 if (packet->xid != dhcp_client->xid)
1193 if (packet->hlen != 6)
1196 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1203 static void start_request(GDHCPClient *dhcp_client);
1205 static gboolean request_timeout(gpointer user_data)
1207 GDHCPClient *dhcp_client = user_data;
1209 debug(dhcp_client, "request timeout (retries %d)",
1210 dhcp_client->retry_times);
1212 dhcp_client->retry_times++;
1214 start_request(dhcp_client);
1219 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1220 gpointer user_data);
1222 static int switch_listening_mode(GDHCPClient *dhcp_client,
1223 ListenMode listen_mode)
1225 GIOChannel *listener_channel;
1226 int listener_sockfd;
1228 if (dhcp_client->listen_mode == listen_mode)
1231 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1232 dhcp_client->listen_mode, listen_mode);
1234 if (dhcp_client->listen_mode != L_NONE) {
1235 if (dhcp_client->listener_watch > 0)
1236 g_source_remove(dhcp_client->listener_watch);
1237 dhcp_client->listener_channel = NULL;
1238 dhcp_client->listen_mode = L_NONE;
1239 dhcp_client->listener_sockfd = -1;
1240 dhcp_client->listener_watch = 0;
1243 if (listen_mode == L_NONE)
1246 if (listen_mode == L2)
1247 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1248 else if (listen_mode == L3) {
1249 if (dhcp_client->type == G_DHCP_IPV6)
1250 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1251 dhcp_client->interface,
1254 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1255 dhcp_client->interface,
1257 } else if (listen_mode == L_ARP)
1258 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1262 if (listener_sockfd < 0)
1265 listener_channel = g_io_channel_unix_new(listener_sockfd);
1266 if (listener_channel == NULL) {
1267 /* Failed to create listener channel */
1268 close(listener_sockfd);
1272 dhcp_client->listen_mode = listen_mode;
1273 dhcp_client->listener_sockfd = listener_sockfd;
1274 dhcp_client->listener_channel = listener_channel;
1276 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1277 dhcp_client->listener_watch =
1278 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1279 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1280 listener_event, dhcp_client,
1282 g_io_channel_unref(dhcp_client->listener_channel);
1287 static void start_request(GDHCPClient *dhcp_client)
1289 debug(dhcp_client, "start request (retries %d)",
1290 dhcp_client->retry_times);
1292 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1293 dhcp_client->state = INIT_SELECTING;
1294 ipv4ll_start(dhcp_client);
1299 if (dhcp_client->retry_times == 0) {
1300 dhcp_client->state = REQUESTING;
1301 switch_listening_mode(dhcp_client, L2);
1304 send_select(dhcp_client);
1306 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1313 static uint32_t get_lease(struct dhcp_packet *packet)
1316 uint32_t lease_seconds;
1318 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
1319 if (option_u8 == NULL)
1322 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
1323 lease_seconds = ntohl(lease_seconds);
1324 /* paranoia: must not be prone to overflows */
1325 lease_seconds &= 0x0fffffff;
1326 if (lease_seconds < 10)
1329 return lease_seconds;
1332 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1334 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1336 if (dhcp_client->timeout > 0) {
1337 g_source_remove(dhcp_client->timeout);
1338 dhcp_client->timeout = 0;
1341 dhcp_client->retry_times = retry_times;
1342 dhcp_client->requested_ip = 0;
1343 switch_listening_mode(dhcp_client, L2);
1345 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1348 static gboolean start_rebound_timeout(gpointer user_data)
1350 GDHCPClient *dhcp_client = user_data;
1352 debug(dhcp_client, "start rebound timeout");
1354 switch_listening_mode(dhcp_client, L2);
1356 dhcp_client->lease_seconds >>= 1;
1358 /* We need to have enough time to receive ACK package*/
1359 if (dhcp_client->lease_seconds <= 6) {
1361 /* ip need to be cleared */
1362 if (dhcp_client->lease_lost_cb != NULL)
1363 dhcp_client->lease_lost_cb(dhcp_client,
1364 dhcp_client->lease_lost_data);
1366 restart_dhcp(dhcp_client, 0);
1368 send_rebound(dhcp_client);
1370 dhcp_client->timeout =
1371 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1372 dhcp_client->lease_seconds >> 1,
1373 start_rebound_timeout,
1381 static void start_rebound(GDHCPClient *dhcp_client)
1383 debug(dhcp_client, "start rebound");
1385 dhcp_client->state = REBINDING;
1387 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1388 dhcp_client->lease_seconds >> 1,
1389 start_rebound_timeout,
1394 static gboolean start_renew_timeout(gpointer user_data)
1396 GDHCPClient *dhcp_client = user_data;
1398 debug(dhcp_client, "start renew timeout");
1400 dhcp_client->state = RENEWING;
1402 dhcp_client->lease_seconds >>= 1;
1404 switch_listening_mode(dhcp_client, L3);
1405 if (dhcp_client->lease_seconds <= 60)
1406 start_rebound(dhcp_client);
1408 send_renew(dhcp_client);
1410 if (dhcp_client->timeout > 0)
1411 g_source_remove(dhcp_client->timeout);
1413 dhcp_client->timeout =
1414 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1415 dhcp_client->lease_seconds >> 1,
1416 start_renew_timeout,
1424 static void start_bound(GDHCPClient *dhcp_client)
1426 debug(dhcp_client, "start bound");
1428 dhcp_client->state = BOUND;
1430 if (dhcp_client->timeout > 0)
1431 g_source_remove(dhcp_client->timeout);
1433 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1434 dhcp_client->lease_seconds >> 1,
1435 start_renew_timeout, dhcp_client,
1439 static gboolean restart_dhcp_timeout(gpointer user_data)
1441 GDHCPClient *dhcp_client = user_data;
1443 debug(dhcp_client, "restart DHCP timeout");
1445 dhcp_client->ack_retry_times++;
1447 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1452 static char *get_ip(uint32_t ip)
1454 struct in_addr addr;
1458 return g_strdup(inet_ntoa(addr));
1461 /* get a rough idea of how long an option will be */
1462 static const uint8_t len_of_option_as_string[] = {
1463 [OPTION_IP] = sizeof("255.255.255.255 "),
1464 [OPTION_STRING] = 1,
1465 [OPTION_U8] = sizeof("255 "),
1466 [OPTION_U16] = sizeof("65535 "),
1467 [OPTION_U32] = sizeof("4294967295 "),
1470 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1472 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1475 /* Create "opt_value1 option_value2 ..." string */
1476 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1478 unsigned upper_length;
1482 len = option[OPT_LEN - OPT_DATA];
1483 type &= OPTION_TYPE_MASK;
1484 optlen = dhcp_option_lengths[type];
1487 upper_length = len_of_option_as_string[type] *
1488 ((unsigned)len / (unsigned)optlen);
1489 dest = ret = malloc(upper_length + 1);
1493 while (len >= optlen) {
1496 dest += sprint_nip(dest, "", option);
1499 uint16_t val_u16 = dhcp_get_unaligned(
1500 (uint16_t *) option);
1501 dest += sprintf(dest, "%u", ntohs(val_u16));
1505 uint32_t val_u32 = dhcp_get_unaligned(
1506 (uint32_t *) option);
1507 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
1508 "%ld", (unsigned long) ntohl(val_u32));
1512 memcpy(dest, option, len);
1529 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1537 if (type == OPTION_STRING)
1538 return g_list_append(list, g_strdup(value));
1540 while ((pos = strchr(pos, ' ')) != NULL) {
1543 list = g_list_append(list, g_strdup(value));
1548 list = g_list_append(list, g_strdup(value));
1553 static inline uint32_t get_uint32(unsigned char *value)
1555 return value[0] << 24 | value[1] << 16 |
1556 value[2] << 8 | value[3];
1559 static inline uint16_t get_uint16(unsigned char *value)
1561 return value[0] << 8 | value[1];
1564 static GList *get_addresses(GDHCPClient *dhcp_client,
1566 unsigned char *value,
1570 struct in6_addr addr;
1571 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1572 uint16_t option_len, option_code, st = 0, max_len;
1573 int addr_count = 0, i, pos;
1577 iaid = get_uint32(&value[0]);
1578 if (dhcp_client->iaid != iaid)
1581 if (code == G_DHCPV6_IA_NA) {
1582 T1 = get_uint32(&value[4]);
1583 T2 = get_uint32(&value[8]);
1586 /* RFC 3315, 22.4 */
1596 max_len = len - pos;
1598 /* We have more sub-options in this packet. */
1600 option = dhcpv6_get_sub_option(&value[pos], max_len,
1601 &option_code, &option_len);
1603 debug(dhcp_client, "pos %d option %p code %d len %d",
1604 pos, option, option_code, option_len);
1612 switch (option_code) {
1613 case G_DHCPV6_IAADDR:
1615 memcpy(&addr, &option[0], sizeof(addr));
1617 preferred = get_uint32(&option[i]);
1619 valid = get_uint32(&option[i]);
1624 case G_DHCPV6_STATUS_CODE:
1625 st = get_uint16(&option[0]);
1626 debug(dhcp_client, "error code %d", st);
1627 if (option_len > 2) {
1628 str = g_strndup((gchar *)&option[2],
1630 debug(dhcp_client, "error text: %s", str);
1638 pos += 2 + 2 + option_len;
1640 } while (option != NULL);
1642 if (addr_count > 0 && st == 0) {
1643 /* We only support one address atm */
1644 char str[INET6_ADDRSTRLEN + 1];
1646 if (preferred > valid)
1647 /* RFC 3315, 22.6 */
1650 dhcp_client->T1 = T1;
1651 dhcp_client->T2 = T2;
1653 inet_ntop(AF_INET6, &addr, str, INET6_ADDRSTRLEN);
1654 debug(dhcp_client, "count %d addr %s T1 %u T2 %u",
1655 addr_count, str, T1, T2);
1657 list = g_list_append(list, g_strdup(str));
1659 if (code == G_DHCPV6_IA_NA)
1660 memcpy(&dhcp_client->ia_na, &addr,
1661 sizeof(struct in6_addr));
1663 memcpy(&dhcp_client->ia_ta, &addr,
1664 sizeof(struct in6_addr));
1666 g_dhcpv6_client_set_expire(dhcp_client, valid);
1672 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
1674 unsigned char *value,
1682 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
1683 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
1686 "%s server list length (%d) is invalid",
1687 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
1691 for (i = 0; i < len; i += 16) {
1693 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
1697 if (inet_ntop(AF_INET6, &value[i], str,
1698 INET6_ADDRSTRLEN) == NULL)
1701 list = g_list_append(list, str);
1705 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
1706 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
1707 list = get_addresses(dhcp_client, code, len, value, status);
1717 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
1718 struct dhcpv6_packet *packet,
1719 uint16_t pkt_len, uint16_t *status)
1721 GList *list, *value_list;
1724 uint16_t option_len;
1726 for (list = dhcp_client->request_list; list; list = list->next) {
1727 code = (uint16_t) GPOINTER_TO_INT(list->data);
1729 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
1731 if (option == NULL) {
1732 g_hash_table_remove(dhcp_client->code_value_hash,
1733 GINT_TO_POINTER((int) code));
1737 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
1738 option_len, option, status);
1740 debug(dhcp_client, "code %d %p len %d list %p", code, option,
1741 option_len, value_list);
1743 if (value_list == NULL)
1744 g_hash_table_remove(dhcp_client->code_value_hash,
1745 GINT_TO_POINTER((int) code));
1747 g_hash_table_insert(dhcp_client->code_value_hash,
1748 GINT_TO_POINTER((int) code), value_list);
1752 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
1754 GDHCPOptionType type;
1755 GList *list, *value_list;
1760 for (list = dhcp_client->request_list; list; list = list->next) {
1761 code = (uint8_t) GPOINTER_TO_INT(list->data);
1763 option = dhcp_get_option(packet, code);
1764 if (option == NULL) {
1765 g_hash_table_remove(dhcp_client->code_value_hash,
1766 GINT_TO_POINTER((int) code));
1770 type = dhcp_get_code_type(code);
1772 option_value = malloc_option_value_string(option, type);
1773 if (option_value == NULL)
1774 g_hash_table_remove(dhcp_client->code_value_hash,
1775 GINT_TO_POINTER((int) code));
1777 value_list = get_option_value_list(option_value, type);
1779 g_free(option_value);
1781 if (value_list == NULL)
1782 g_hash_table_remove(dhcp_client->code_value_hash,
1783 GINT_TO_POINTER((int) code));
1785 g_hash_table_insert(dhcp_client->code_value_hash,
1786 GINT_TO_POINTER((int) code), value_list);
1790 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1793 GDHCPClient *dhcp_client = user_data;
1794 struct dhcp_packet packet;
1795 struct dhcpv6_packet *packet6 = NULL;
1796 uint8_t *message_type = NULL, *client_id = NULL, *option_u8,
1798 uint16_t option_len = 0, status = 0;
1800 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
1801 uint16_t pkt_len = 0;
1805 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1806 dhcp_client->listener_watch = 0;
1810 if (dhcp_client->listen_mode == L_NONE)
1815 if (dhcp_client->listen_mode == L2)
1816 re = dhcp_recv_l2_packet(&packet,
1817 dhcp_client->listener_sockfd);
1818 else if (dhcp_client->listen_mode == L3) {
1819 if (dhcp_client->type == G_DHCP_IPV6) {
1820 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
1821 dhcp_client->listener_sockfd);
1825 re = dhcp_recv_l3_packet(&packet,
1826 dhcp_client->listener_sockfd);
1827 } else if (dhcp_client->listen_mode == L_ARP) {
1828 re = ipv4ll_recv_arp_packet(dhcp_client);
1837 if (check_package_owner(dhcp_client, pkt) == FALSE)
1840 if (dhcp_client->type == G_DHCP_IPV6) {
1842 client_id = dhcpv6_get_option(packet6, pkt_len,
1843 G_DHCPV6_CLIENTID, &option_len, &count);
1845 if (client_id == NULL || count == 0 || option_len == 0 ||
1846 memcmp(dhcp_client->duid, client_id,
1847 dhcp_client->duid_len) != 0) {
1849 "client duid error, discarding msg %p/%d/%d",
1850 client_id, option_len, count);
1854 option_u8 = dhcpv6_get_option(packet6, pkt_len,
1855 G_DHCPV6_STATUS_CODE, &option_len, NULL);
1856 if (option_u8 != 0 && option_len > 0) {
1857 status = option_u8[0]<<8 | option_u8[1];
1859 debug(dhcp_client, "error code %d", status);
1860 if (option_len > 2) {
1861 gchar *txt = g_strndup(
1862 (gchar *)&option_u8[2],
1864 debug(dhcp_client, "error text: %s",
1869 dhcp_client->status_code = status;
1871 dhcp_client->status_code = 0;
1874 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1876 if (message_type == NULL && client_id == NULL)
1877 /* No message type / client id option, ignore package */
1880 debug(dhcp_client, "received DHCP packet (current state %d)",
1881 dhcp_client->state);
1883 switch (dhcp_client->state) {
1884 case INIT_SELECTING:
1885 if (*message_type != DHCPOFFER)
1888 g_source_remove(dhcp_client->timeout);
1889 dhcp_client->timeout = 0;
1890 dhcp_client->retry_times = 0;
1892 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
1893 dhcp_client->server_ip =
1894 dhcp_get_unaligned((uint32_t *) option_u8);
1895 dhcp_client->requested_ip = packet.yiaddr;
1897 dhcp_client->state = REQUESTING;
1899 start_request(dhcp_client);
1905 if (*message_type == DHCPACK) {
1906 dhcp_client->retry_times = 0;
1908 if (dhcp_client->timeout > 0)
1909 g_source_remove(dhcp_client->timeout);
1910 dhcp_client->timeout = 0;
1912 dhcp_client->lease_seconds = get_lease(&packet);
1914 get_request(dhcp_client, &packet);
1916 switch_listening_mode(dhcp_client, L_NONE);
1918 g_free(dhcp_client->assigned_ip);
1919 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1921 /* Address should be set up here */
1922 if (dhcp_client->lease_available_cb != NULL)
1923 dhcp_client->lease_available_cb(dhcp_client,
1924 dhcp_client->lease_available_data);
1926 start_bound(dhcp_client);
1927 } else if (*message_type == DHCPNAK) {
1928 dhcp_client->retry_times = 0;
1930 if (dhcp_client->timeout > 0)
1931 g_source_remove(dhcp_client->timeout);
1933 dhcp_client->timeout = g_timeout_add_seconds_full(
1935 restart_dhcp_timeout,
1942 if (dhcp_client->type != G_DHCP_IPV6)
1945 if (packet6->message != DHCPV6_REPLY &&
1946 packet6->message != DHCPV6_ADVERTISE)
1950 server_id = dhcpv6_get_option(packet6, pkt_len,
1951 G_DHCPV6_SERVERID, &option_len, &count);
1952 if (server_id == NULL || count != 1 || option_len == 0) {
1953 /* RFC 3315, 15.10 */
1955 "server duid error, discarding msg %p/%d/%d",
1956 server_id, option_len, count);
1959 dhcp_client->server_duid = g_try_malloc(option_len);
1960 if (dhcp_client->server_duid == NULL)
1962 memcpy(dhcp_client->server_duid, server_id, option_len);
1963 dhcp_client->server_duid_len = option_len;
1965 if (packet6->message == DHCPV6_REPLY) {
1966 uint8_t *rapid_commit;
1969 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
1970 G_DHCPV6_RAPID_COMMIT,
1971 &option_len, &count);
1972 if (rapid_commit == NULL || option_len == 0 ||
1974 /* RFC 3315, 17.1.4 */
1978 switch_listening_mode(dhcp_client, L_NONE);
1980 if (dhcp_client->status_code == 0)
1981 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
1982 &dhcp_client->status_code);
1984 if (packet6->message == DHCPV6_ADVERTISE) {
1985 if (dhcp_client->advertise_cb != NULL)
1986 dhcp_client->advertise_cb(dhcp_client,
1987 dhcp_client->advertise_data);
1991 if (dhcp_client->solicitation_cb != NULL) {
1993 * The dhcp_client might not be valid after the
1994 * callback call so just return immediately.
1996 dhcp_client->solicitation_cb(dhcp_client,
1997 dhcp_client->solicitation_data);
2001 case INFORMATION_REQ:
2005 if (dhcp_client->type != G_DHCP_IPV6)
2008 if (packet6->message != DHCPV6_REPLY)
2013 server_id = dhcpv6_get_option(packet6, pkt_len,
2014 G_DHCPV6_SERVERID, &option_len, &count);
2015 if (server_id == NULL || count != 1 || option_len == 0 ||
2016 (dhcp_client->server_duid_len > 0 &&
2017 memcmp(dhcp_client->server_duid, server_id,
2018 dhcp_client->server_duid_len) != 0)) {
2019 /* RFC 3315, 15.10 */
2021 "server duid error, discarding msg %p/%d/%d",
2022 server_id, option_len, count);
2026 switch_listening_mode(dhcp_client, L_NONE);
2028 dhcp_client->status_code = 0;
2029 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2030 &dhcp_client->status_code);
2032 if (dhcp_client->information_req_cb != NULL) {
2034 * The dhcp_client might not be valid after the
2035 * callback call so just return immediately.
2037 dhcp_client->information_req_cb(dhcp_client,
2038 dhcp_client->information_req_data);
2041 if (dhcp_client->request_cb != NULL) {
2042 dhcp_client->request_cb(dhcp_client,
2043 dhcp_client->request_data);
2046 if (dhcp_client->renew_cb != NULL) {
2047 dhcp_client->renew_cb(dhcp_client,
2048 dhcp_client->renew_data);
2051 if (dhcp_client->rebind_cb != NULL) {
2052 dhcp_client->rebind_cb(dhcp_client,
2053 dhcp_client->rebind_data);
2061 debug(dhcp_client, "processed DHCP packet (new state %d)",
2062 dhcp_client->state);
2067 static gboolean discover_timeout(gpointer user_data)
2069 GDHCPClient *dhcp_client = user_data;
2071 dhcp_client->retry_times++;
2074 * We do not send the REQUESTED IP option if we are retrying because
2075 * if the server is non-authoritative it will ignore the request if the
2076 * option is present.
2078 g_dhcp_client_start(dhcp_client, NULL);
2083 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2085 GDHCPClient *dhcp_client = dhcp_data;
2087 debug(dhcp_client, "back to MONITOR mode");
2089 dhcp_client->conflicts = 0;
2090 dhcp_client->state = IPV4LL_MONITOR;
2095 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2097 GDHCPClient *dhcp_client = dhcp_data;
2100 debug(dhcp_client, "request timeout (retries %d)",
2101 dhcp_client->retry_times);
2103 if (dhcp_client->retry_times != ANNOUNCE_NUM){
2104 dhcp_client->retry_times++;
2105 send_announce_packet(dhcp_client);
2109 ip = htonl(dhcp_client->requested_ip);
2110 debug(dhcp_client, "switching to monitor mode");
2111 dhcp_client->state = IPV4LL_MONITOR;
2112 dhcp_client->assigned_ip = get_ip(ip);
2114 if (dhcp_client->ipv4ll_available_cb != NULL)
2115 dhcp_client->ipv4ll_available_cb(dhcp_client,
2116 dhcp_client->ipv4ll_available_data);
2117 dhcp_client->conflicts = 0;
2122 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2125 GDHCPClient *dhcp_client = dhcp_data;
2127 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2128 dhcp_client->retry_times);
2130 if (dhcp_client->retry_times == PROBE_NUM) {
2131 dhcp_client->state = IPV4LL_ANNOUNCE;
2132 dhcp_client->retry_times = 0;
2134 dhcp_client->retry_times++;
2135 send_announce_packet(dhcp_client);
2138 dhcp_client->retry_times++;
2139 send_probe_packet(dhcp_client);
2144 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2149 if (dhcp_client->type == G_DHCP_IPV6) {
2150 if (dhcp_client->information_req_cb) {
2151 dhcp_client->state = INFORMATION_REQ;
2152 re = switch_listening_mode(dhcp_client, L3);
2154 switch_listening_mode(dhcp_client, L_NONE);
2155 dhcp_client->state = 0;
2158 send_information_req(dhcp_client);
2160 } else if (dhcp_client->solicitation_cb) {
2161 dhcp_client->state = SOLICITATION;
2162 re = switch_listening_mode(dhcp_client, L3);
2164 switch_listening_mode(dhcp_client, L_NONE);
2165 dhcp_client->state = 0;
2168 send_solicitation(dhcp_client);
2170 } else if (dhcp_client->request_cb) {
2171 dhcp_client->state = REQUEST;
2172 re = switch_listening_mode(dhcp_client, L3);
2174 switch_listening_mode(dhcp_client, L_NONE);
2175 dhcp_client->state = 0;
2178 send_dhcpv6_request(dhcp_client);
2180 } else if (dhcp_client->renew_cb) {
2181 dhcp_client->state = RENEW;
2182 re = switch_listening_mode(dhcp_client, L3);
2184 switch_listening_mode(dhcp_client, L_NONE);
2185 dhcp_client->state = 0;
2188 send_dhcpv6_renew(dhcp_client);
2190 } else if (dhcp_client->rebind_cb) {
2191 dhcp_client->state = REBIND;
2192 re = switch_listening_mode(dhcp_client, L3);
2194 switch_listening_mode(dhcp_client, L_NONE);
2195 dhcp_client->state = 0;
2198 send_dhcpv6_rebind(dhcp_client);
2204 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2205 ipv4ll_start(dhcp_client);
2209 if (dhcp_client->retry_times == 0) {
2210 g_free(dhcp_client->assigned_ip);
2211 dhcp_client->assigned_ip = NULL;
2213 dhcp_client->state = INIT_SELECTING;
2214 re = switch_listening_mode(dhcp_client, L2);
2218 dhcp_client->xid = rand();
2221 if (last_address == NULL) {
2224 addr = inet_addr(last_address);
2225 if (addr == 0xFFFFFFFF) {
2228 g_free(dhcp_client->last_address);
2229 dhcp_client->last_address = g_strdup(last_address);
2232 send_discover(dhcp_client, addr);
2234 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2242 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2244 switch_listening_mode(dhcp_client, L_NONE);
2246 if (dhcp_client->state == BOUND ||
2247 dhcp_client->state == RENEWING ||
2248 dhcp_client->state == REBINDING)
2249 send_release(dhcp_client, dhcp_client->server_ip,
2250 dhcp_client->requested_ip);
2252 if (dhcp_client->timeout > 0) {
2253 g_source_remove(dhcp_client->timeout);
2254 dhcp_client->timeout = 0;
2257 if (dhcp_client->listener_watch > 0) {
2258 g_source_remove(dhcp_client->listener_watch);
2259 dhcp_client->listener_watch = 0;
2262 dhcp_client->listener_channel = NULL;
2264 dhcp_client->retry_times = 0;
2265 dhcp_client->ack_retry_times = 0;
2267 dhcp_client->requested_ip = 0;
2268 dhcp_client->state = RELEASED;
2269 dhcp_client->lease_seconds = 0;
2272 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2273 unsigned char option_code)
2275 return g_hash_table_lookup(dhcp_client->code_value_hash,
2276 GINT_TO_POINTER((int) option_code));
2279 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2280 GDHCPClientEvent event,
2281 GDHCPClientEventFunc func,
2285 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2286 dhcp_client->lease_available_cb = func;
2287 dhcp_client->lease_available_data = data;
2289 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2290 if (dhcp_client->type == G_DHCP_IPV6)
2292 dhcp_client->ipv4ll_available_cb = func;
2293 dhcp_client->ipv4ll_available_data = data;
2295 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2296 dhcp_client->no_lease_cb = func;
2297 dhcp_client->no_lease_data = data;
2299 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2300 dhcp_client->lease_lost_cb = func;
2301 dhcp_client->lease_lost_data = data;
2303 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2304 if (dhcp_client->type == G_DHCP_IPV6)
2306 dhcp_client->ipv4ll_lost_cb = func;
2307 dhcp_client->ipv4ll_lost_data = data;
2309 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2310 dhcp_client->address_conflict_cb = func;
2311 dhcp_client->address_conflict_data = data;
2313 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2314 if (dhcp_client->type == G_DHCP_IPV4)
2316 dhcp_client->information_req_cb = func;
2317 dhcp_client->information_req_data = data;
2319 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2320 if (dhcp_client->type == G_DHCP_IPV4)
2322 dhcp_client->solicitation_cb = func;
2323 dhcp_client->solicitation_data = data;
2325 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2326 if (dhcp_client->type == G_DHCP_IPV4)
2328 dhcp_client->advertise_cb = func;
2329 dhcp_client->advertise_data = data;
2331 case G_DHCP_CLIENT_EVENT_REQUEST:
2332 if (dhcp_client->type == G_DHCP_IPV4)
2334 dhcp_client->request_cb = func;
2335 dhcp_client->request_data = data;
2337 case G_DHCP_CLIENT_EVENT_RENEW:
2338 if (dhcp_client->type == G_DHCP_IPV4)
2340 dhcp_client->renew_cb = func;
2341 dhcp_client->renew_data = data;
2343 case G_DHCP_CLIENT_EVENT_REBIND:
2344 if (dhcp_client->type == G_DHCP_IPV4)
2346 dhcp_client->rebind_cb = func;
2347 dhcp_client->rebind_data = data;
2352 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2354 return dhcp_client->ifindex;
2357 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2359 return g_strdup(dhcp_client->assigned_ip);
2362 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2364 GList *option = NULL;
2366 if (dhcp_client->type == G_DHCP_IPV6)
2369 switch (dhcp_client->state) {
2371 case IPV4LL_MONITOR:
2372 return g_strdup("255.255.0.0");
2376 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2378 return g_strdup(option->data);
2379 case INIT_SELECTING:
2383 case IPV4LL_ANNOUNCE:
2384 case INFORMATION_REQ:
2394 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2395 unsigned int option_code)
2397 if (g_list_find(dhcp_client->request_list,
2398 GINT_TO_POINTER((int) option_code)) == NULL)
2399 dhcp_client->request_list = g_list_prepend(
2400 dhcp_client->request_list,
2401 (GINT_TO_POINTER((int) option_code)));
2403 return G_DHCP_CLIENT_ERROR_NONE;
2406 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2408 g_list_free(dhcp_client->request_list);
2409 dhcp_client->request_list = NULL;
2412 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2414 g_hash_table_remove_all(dhcp_client->send_value_hash);
2417 static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
2420 int len = strnlen(str, 255);
2422 storage = malloc(len + extra + OPT_DATA);
2423 storage[OPT_CODE] = code;
2424 storage[OPT_LEN] = len + extra;
2425 memcpy(storage + extra + OPT_DATA, str, len);
2430 /* Now only support send hostname */
2431 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
2432 unsigned char option_code, const char *option_value)
2434 uint8_t *binary_option;
2436 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
2437 binary_option = alloc_dhcp_option(option_code,
2440 g_hash_table_insert(dhcp_client->send_value_hash,
2441 GINT_TO_POINTER((int) option_code), binary_option);
2444 return G_DHCP_CLIENT_ERROR_NONE;
2447 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
2452 storage = g_malloc(2 + 2 + len);
2453 if (storage == NULL)
2456 storage[0] = code >> 8;
2457 storage[1] = code & 0xff;
2458 storage[2] = len >> 8;
2459 storage[3] = len & 0xff;
2460 memcpy(storage + 2 + 2, option, len);
2465 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
2466 uint16_t option_code,
2467 uint8_t *option_value,
2468 uint16_t option_len)
2470 if (option_value != NULL) {
2471 uint8_t *binary_option;
2473 debug(dhcp_client, "setting option %d to %p len %d",
2474 option_code, option_value, option_len);
2476 binary_option = alloc_dhcpv6_option(option_code, option_value,
2478 if (binary_option != NULL)
2479 g_hash_table_insert(dhcp_client->send_value_hash,
2480 GINT_TO_POINTER((int) option_code),
2485 void g_dhcpv6_client_reset_renew(GDHCPClient *dhcp_client)
2487 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2490 dhcp_client->last_renew = time(0);
2493 void g_dhcpv6_client_reset_rebind(GDHCPClient *dhcp_client)
2495 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2498 dhcp_client->last_rebind = time(0);
2501 void g_dhcpv6_client_set_expire(GDHCPClient *dhcp_client, uint32_t timeout)
2503 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2506 dhcp_client->expire = time(0) + timeout;
2509 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
2511 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2514 return dhcp_client->status_code;
2517 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
2519 if (dhcp_client == NULL)
2522 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
2527 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
2529 if (dhcp_client == NULL)
2532 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
2535 g_dhcp_client_stop(dhcp_client);
2537 g_free(dhcp_client->interface);
2538 g_free(dhcp_client->assigned_ip);
2539 g_free(dhcp_client->last_address);
2540 g_free(dhcp_client->duid);
2541 g_free(dhcp_client->server_duid);
2543 g_list_free(dhcp_client->request_list);
2544 g_list_free(dhcp_client->require_list);
2546 g_hash_table_destroy(dhcp_client->code_value_hash);
2547 g_hash_table_destroy(dhcp_client->send_value_hash);
2549 g_free(dhcp_client);
2552 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
2553 GDHCPDebugFunc func, gpointer user_data)
2555 if (dhcp_client == NULL)
2558 dhcp_client->debug_func = func;
2559 dhcp_client->debug_data = user_data;