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 {
82 uint8_t mac_address[6];
85 uint32_t requested_ip;
87 uint32_t lease_seconds;
88 ListenMode listen_mode;
91 uint8_t ack_retry_times;
95 GIOChannel *listener_channel;
98 GHashTable *code_value_hash;
99 GHashTable *send_value_hash;
100 GDHCPClientEventFunc lease_available_cb;
101 gpointer lease_available_data;
102 GDHCPClientEventFunc ipv4ll_available_cb;
103 gpointer ipv4ll_available_data;
104 GDHCPClientEventFunc no_lease_cb;
105 gpointer no_lease_data;
106 GDHCPClientEventFunc lease_lost_cb;
107 gpointer lease_lost_data;
108 GDHCPClientEventFunc ipv4ll_lost_cb;
109 gpointer ipv4ll_lost_data;
110 GDHCPClientEventFunc address_conflict_cb;
111 gpointer address_conflict_data;
112 GDHCPDebugFunc debug_func;
114 GDHCPClientEventFunc information_req_cb;
115 gpointer information_req_data;
116 GDHCPClientEventFunc solicitation_cb;
117 gpointer solicitation_data;
118 GDHCPClientEventFunc advertise_cb;
119 gpointer advertise_data;
123 unsigned char *server_duid;
125 uint16_t status_code;
128 struct in6_addr ia_na;
129 struct in6_addr ia_ta;
132 static inline void debug(GDHCPClient *client, const char *format, ...)
137 if (client->debug_func == NULL)
140 va_start(ap, format);
142 if (vsnprintf(str, sizeof(str), format, ap) > 0)
143 client->debug_func(str, client->debug_data);
148 /* Initialize the packet with the proper defaults */
149 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
151 if (dhcp_client->type == G_DHCP_IPV6)
152 dhcpv6_init_header(pkt, type);
154 struct dhcp_packet *packet = pkt;
156 dhcp_init_header(packet, type);
157 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
161 static void add_request_options(GDHCPClient *dhcp_client,
162 struct dhcp_packet *packet)
167 int end = dhcp_end_option(packet->options);
169 for (list = dhcp_client->request_list; list; list = list->next) {
170 code = (uint8_t) GPOINTER_TO_INT(list->data);
172 packet->options[end + OPT_DATA + len] = code;
177 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
178 packet->options[end + OPT_LEN] = len;
179 packet->options[end + OPT_DATA + len] = DHCP_END;
186 unsigned char **ptr_buf;
189 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
192 uint8_t *option = value;
194 struct hash_params *params = user_data;
196 /* option[0][1] contains option code */
197 len = option[2] << 8 | option[3];
199 if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
202 memcpy(*params->ptr_buf, option, len + 2 + 2);
203 (*params->ptr_buf) += len + 2 + 2;
206 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
207 unsigned char *buf, int max_buf,
208 unsigned char **ptr_buf)
210 struct hash_params params = {
216 if (dhcp_client->type == G_DHCP_IPV4)
219 g_hash_table_foreach(dhcp_client->send_value_hash,
220 add_dhcpv6_binary_option, ¶ms);
222 *ptr_buf = *params.ptr_buf;
225 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
229 buf[1] = code & 0xff;
232 if (len > 0 && msg != NULL)
233 memcpy(&buf[4], msg, len);
236 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
237 struct dhcpv6_packet *packet,
238 unsigned char *buf, int max_buf,
239 unsigned char **ptr_buf)
245 if (dhcp_client->type == G_DHCP_IPV4)
248 for (list = dhcp_client->request_list; list; list = list->next) {
249 code = (uint16_t) GPOINTER_TO_INT(list->data);
252 case G_DHCPV6_CLIENTID:
253 if (dhcp_client->duid == NULL)
256 len = 2 + 2 + dhcp_client->duid_len;
257 if ((*ptr_buf + len) > (buf + max_buf)) {
258 debug(dhcp_client, "Too long dhcpv6 message "
259 "when writing client id option");
263 copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
264 dhcp_client->duid_len, dhcp_client->duid);
268 case G_DHCPV6_SERVERID:
269 if (dhcp_client->server_duid == NULL)
272 len = 2 + 2 + dhcp_client->server_duid_len;
273 if ((*ptr_buf + len) > (buf + max_buf)) {
274 debug(dhcp_client, "Too long dhcpv6 message "
275 "when writing server id option");
279 copy_option(*ptr_buf, G_DHCPV6_SERVERID,
280 dhcp_client->server_duid_len,
281 dhcp_client->server_duid);
285 case G_DHCPV6_RAPID_COMMIT:
287 if ((*ptr_buf + len) > (buf + max_buf)) {
288 debug(dhcp_client, "Too long dhcpv6 message "
289 "when writing rapid commit option");
293 copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
300 case G_DHCPV6_DNS_SERVERS:
303 case G_DHCPV6_SNTP_SERVERS:
312 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
314 uint8_t *option = value;
315 struct dhcp_packet *packet = user_data;
317 dhcp_add_binary_option(packet, option);
320 static void add_send_options(GDHCPClient *dhcp_client,
321 struct dhcp_packet *packet)
323 g_hash_table_foreach(dhcp_client->send_value_hash,
324 add_binary_option, packet);
327 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
329 struct dhcp_packet packet;
331 debug(dhcp_client, "sending DHCP discover request");
333 init_packet(dhcp_client, &packet, DHCPDISCOVER);
335 packet.xid = dhcp_client->xid;
338 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP, requested);
340 /* Explicitly saying that we want RFC-compliant packets helps
341 * some buggy DHCP servers to NOT send bigger packets */
342 dhcp_add_simple_option(&packet, DHCP_MAX_SIZE, htons(576));
344 add_request_options(dhcp_client, &packet);
346 add_send_options(dhcp_client, &packet);
348 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
349 INADDR_BROADCAST, SERVER_PORT,
350 MAC_BCAST_ADDR, dhcp_client->ifindex);
353 static int send_select(GDHCPClient *dhcp_client)
355 struct dhcp_packet packet;
357 debug(dhcp_client, "sending DHCP select request");
359 init_packet(dhcp_client, &packet, DHCPREQUEST);
361 packet.xid = dhcp_client->xid;
363 dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
364 dhcp_client->requested_ip);
365 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
367 add_request_options(dhcp_client, &packet);
369 add_send_options(dhcp_client, &packet);
371 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
372 INADDR_BROADCAST, SERVER_PORT,
373 MAC_BCAST_ADDR, dhcp_client->ifindex);
376 static int send_renew(GDHCPClient *dhcp_client)
378 struct dhcp_packet packet;
380 debug(dhcp_client, "sending DHCP renew request");
382 init_packet(dhcp_client , &packet, DHCPREQUEST);
383 packet.xid = dhcp_client->xid;
384 packet.ciaddr = dhcp_client->requested_ip;
386 add_request_options(dhcp_client, &packet);
388 add_send_options(dhcp_client, &packet);
390 return dhcp_send_kernel_packet(&packet,
391 dhcp_client->requested_ip, CLIENT_PORT,
392 dhcp_client->server_ip, SERVER_PORT);
395 static int send_rebound(GDHCPClient *dhcp_client)
397 struct dhcp_packet packet;
399 debug(dhcp_client, "sending DHCP rebound request");
401 init_packet(dhcp_client , &packet, DHCPREQUEST);
402 packet.xid = dhcp_client->xid;
403 packet.ciaddr = dhcp_client->requested_ip;
405 add_request_options(dhcp_client, &packet);
407 add_send_options(dhcp_client, &packet);
409 return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
410 INADDR_BROADCAST, SERVER_PORT,
411 MAC_BCAST_ADDR, dhcp_client->ifindex);
414 static int send_release(GDHCPClient *dhcp_client,
415 uint32_t server, uint32_t ciaddr)
417 struct dhcp_packet packet;
419 debug(dhcp_client, "sending DHCP release request");
421 init_packet(dhcp_client, &packet, DHCPRELEASE);
423 packet.ciaddr = ciaddr;
425 dhcp_add_simple_option(&packet, DHCP_SERVER_ID, server);
427 return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
428 server, SERVER_PORT);
431 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
432 static int switch_listening_mode(GDHCPClient *dhcp_client,
433 ListenMode listen_mode);
435 static gboolean send_probe_packet(gpointer dhcp_data)
437 GDHCPClient *dhcp_client;
440 dhcp_client = dhcp_data;
441 /* if requested_ip is not valid, pick a new address*/
442 if (dhcp_client->requested_ip == 0) {
443 debug(dhcp_client, "pick a new random address");
444 dhcp_client->requested_ip = ipv4ll_random_ip(0);
447 debug(dhcp_client, "sending IPV4LL probe request");
449 if (dhcp_client->retry_times == 1) {
450 dhcp_client->state = IPV4LL_PROBE;
451 switch_listening_mode(dhcp_client, L_ARP);
453 ipv4ll_send_arp_packet(dhcp_client->mac_address, 0,
454 dhcp_client->requested_ip, dhcp_client->ifindex);
456 if (dhcp_client->retry_times < PROBE_NUM) {
457 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
458 timeout = ipv4ll_random_delay_ms(PROBE_MAX-PROBE_MIN);
459 timeout += PROBE_MIN*1000;
461 timeout = (ANNOUNCE_WAIT * 1000);
463 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
465 ipv4ll_probe_timeout,
471 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
472 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
474 static gboolean send_announce_packet(gpointer dhcp_data)
476 GDHCPClient *dhcp_client;
478 dhcp_client = dhcp_data;
480 debug(dhcp_client, "sending IPV4LL announce request");
482 ipv4ll_send_arp_packet(dhcp_client->mac_address,
483 dhcp_client->requested_ip,
484 dhcp_client->requested_ip,
485 dhcp_client->ifindex);
487 if (dhcp_client->timeout > 0)
488 g_source_remove(dhcp_client->timeout);
489 dhcp_client->timeout = 0;
491 if (dhcp_client->state == IPV4LL_DEFEND) {
492 dhcp_client->timeout =
493 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
495 ipv4ll_defend_timeout,
500 dhcp_client->timeout =
501 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
503 ipv4ll_announce_timeout,
509 static void get_interface_mac_address(int index, uint8_t *mac_address)
514 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
516 perror("Open socket error");
520 memset(&ifr, 0, sizeof(ifr));
521 ifr.ifr_ifindex = index;
523 err = ioctl(sk, SIOCGIFNAME, &ifr);
525 perror("Get interface name error");
529 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
531 perror("Get mac address error");
535 memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
541 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
542 unsigned char **duid, int *duid_len)
547 case G_DHCPV6_DUID_LLT:
548 *duid_len = 2 + 2 + 4 + ETH_ALEN;
549 *duid = g_try_malloc(*duid_len);
555 get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
558 duid_time = time(0) - DUID_TIME_EPOCH;
559 (*duid)[4] = duid_time >> 24;
560 (*duid)[5] = duid_time >> 16;
561 (*duid)[6] = duid_time >> 8;
562 (*duid)[7] = duid_time & 0xff;
564 case G_DHCPV6_DUID_EN:
566 case G_DHCPV6_DUID_LL:
567 *duid_len = 2 + 2 + ETH_ALEN;
568 *duid = g_try_malloc(*duid_len);
574 get_interface_mac_address(index, &(*duid)[2 + 2]);
583 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
586 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
589 g_free(dhcp_client->duid);
591 dhcp_client->duid = duid;
592 dhcp_client->duid_len = duid_len;
597 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
599 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
602 return dhcp_client->iaid;
605 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
610 get_interface_mac_address(index, buf);
612 memcpy(iaid, &buf[2], 4);
613 dhcp_client->iaid = iaid[0] << 24 |
614 iaid[1] << 16 | iaid[2] << 8 | iaid[3];
617 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
618 uint32_t *T1, uint32_t *T2)
620 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
624 *T1 = dhcp_client->T1;
627 *T2 = dhcp_client->T2;
632 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
636 buf[1] = G_DHCPV6_IAADDR;
639 memcpy(&buf[4], &dhcp_client->ia_na, 16);
640 memset(&buf[20], 0, 4); /* preferred */
641 memset(&buf[24], 0, 4); /* valid */
645 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
649 iaid = g_dhcpv6_client_get_iaid(dhcp_client);
651 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
661 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
662 int code, uint32_t *T1, uint32_t *T2,
665 if (code == G_DHCPV6_IA_TA) {
666 uint8_t ia_options[4];
668 put_iaid(dhcp_client, index, ia_options);
670 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
671 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
672 ia_options, sizeof(ia_options));
674 } else if (code == G_DHCPV6_IA_NA) {
676 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
678 if (add_iaaddr == TRUE) {
679 #define IAADDR_LEN (16+4+4)
680 uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
682 put_iaid(dhcp_client, index, ia_options);
685 ia_options[4] = *T1 >> 24;
686 ia_options[5] = *T1 >> 16;
687 ia_options[6] = *T1 >> 8;
690 memset(&ia_options[4], 0x00, 4);
693 ia_options[8] = *T2 >> 24;
694 ia_options[9] = *T2 >> 16;
695 ia_options[10] = *T2 >> 8;
696 ia_options[11] = *T2;
698 memset(&ia_options[8], 0x00, 4);
700 create_iaaddr(dhcp_client, &ia_options[12],
703 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
704 ia_options, sizeof(ia_options));
706 uint8_t ia_options[4+4+4];
708 put_iaid(dhcp_client, index, ia_options);
710 memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
711 memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
713 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
714 ia_options, sizeof(ia_options));
723 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
726 int i, j, len = sizeof(uint16_t) * args;
729 values = g_try_malloc(len);
734 for (i = 0, j = 0; i < args; i++) {
735 uint16_t value = va_arg(va, int);
736 values[j++] = value >> 8;
737 values[j++] = value & 0xff;
741 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
746 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
748 struct dhcpv6_packet *packet;
749 uint8_t buf[MAX_DHCPV6_PKT_SIZE];
753 memset(buf, 0, sizeof(buf));
754 packet = (struct dhcpv6_packet *)&buf[0];
755 ptr = buf + sizeof(struct dhcpv6_packet);
757 debug(dhcp_client, "sending DHCPv6 %s message", msg);
759 init_packet(dhcp_client, packet, type);
761 dhcp_client->xid = packet->transaction_id[0] << 16 |
762 packet->transaction_id[1] << 8 |
763 packet->transaction_id[2];
765 max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
767 add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
769 add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
771 ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
773 debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
777 static int send_solicitation(GDHCPClient *dhcp_client)
779 return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
782 static int send_information_req(GDHCPClient *dhcp_client)
784 return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
788 static void remove_value(gpointer data, gpointer user_data)
794 static void remove_option_value(gpointer data)
796 GList *option_value = data;
798 g_list_foreach(option_value, remove_value, NULL);
801 GDHCPClient *g_dhcp_client_new(GDHCPType type,
802 int ifindex, GDHCPClientError *error)
804 GDHCPClient *dhcp_client;
807 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
811 dhcp_client = g_try_new0(GDHCPClient, 1);
812 if (dhcp_client == NULL) {
813 *error = G_DHCP_CLIENT_ERROR_NOMEM;
817 dhcp_client->interface = get_interface_name(ifindex);
818 if (dhcp_client->interface == NULL) {
819 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
823 if (interface_is_up(ifindex) == FALSE) {
824 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
828 get_interface_mac_address(ifindex, dhcp_client->mac_address);
830 dhcp_client->listener_sockfd = -1;
831 dhcp_client->listener_channel = NULL;
832 dhcp_client->listen_mode = L_NONE;
833 dhcp_client->ref_count = 1;
834 dhcp_client->type = type;
835 dhcp_client->ifindex = ifindex;
836 dhcp_client->lease_available_cb = NULL;
837 dhcp_client->ipv4ll_available_cb = NULL;
838 dhcp_client->no_lease_cb = NULL;
839 dhcp_client->lease_lost_cb = NULL;
840 dhcp_client->ipv4ll_lost_cb = NULL;
841 dhcp_client->address_conflict_cb = NULL;
842 dhcp_client->listener_watch = 0;
843 dhcp_client->retry_times = 0;
844 dhcp_client->ack_retry_times = 0;
845 dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
846 g_direct_equal, NULL, remove_option_value);
847 dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
848 g_direct_equal, NULL, g_free);
849 dhcp_client->request_list = NULL;
850 dhcp_client->require_list = NULL;
851 dhcp_client->duid = NULL;
852 dhcp_client->duid_len = 0;
854 *error = G_DHCP_CLIENT_ERROR_NONE;
859 g_free(dhcp_client->interface);
864 #define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
866 static int dhcp_l2_socket(int ifindex)
869 struct sockaddr_ll sock;
874 * I've selected not to see LL header, so BPF doesn't see it, too.
875 * The filter may also pass non-IP and non-ARP packets, but we do
876 * a more complete check when receiving the message in userspace.
878 * and filter shamelessly stolen from:
880 * http://www.flamewarmaster.de/software/dhcpclient/
882 * There are a few other interesting ideas on that page (look under
883 * "Motivation"). Use of netlink events is most interesting. Think
884 * of various network servers listening for events and reconfiguring.
885 * That would obsolete sending HUP signals and/or make use of restarts.
887 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
890 * TODO: make conditional?
892 static const struct sock_filter filter_instr[] = {
894 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
895 /* L5, L1, is UDP? */
896 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
897 /* ugly check for arp on ethernet-like and IPv4 */
898 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
899 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
901 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
902 /* check udp source and destination ports */
903 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
905 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
907 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
908 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
911 static const struct sock_fprog filter_prog = {
912 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
913 /* casting const away: */
914 .filter = (struct sock_filter *) filter_instr,
917 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
921 if (SERVER_PORT == 67 && CLIENT_PORT == 68)
922 /* Use only if standard ports are in use */
923 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
924 sizeof(filter_prog));
926 memset(&sock, 0, sizeof(sock));
927 sock.sll_family = AF_PACKET;
928 sock.sll_protocol = htons(ETH_P_IP);
929 sock.sll_ifindex = ifindex;
931 if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
939 static gboolean sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
941 if (packet->ip.protocol != IPPROTO_UDP)
944 if (packet->ip.version != IPVERSION)
947 if (packet->ip.ihl != sizeof(packet->ip) >> 2)
950 if (packet->udp.dest != htons(CLIENT_PORT))
953 if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
959 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd)
962 struct ip_udp_dhcp_packet packet;
965 memset(&packet, 0, sizeof(packet));
967 bytes = read(fd, &packet, sizeof(packet));
971 if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
974 if (bytes < ntohs(packet.ip.tot_len))
975 /* packet is bigger than sizeof(packet), we did partial read */
978 /* ignore any extra garbage bytes */
979 bytes = ntohs(packet.ip.tot_len);
981 if (sanity_check(&packet, bytes) == FALSE)
984 check = packet.ip.check;
986 if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
989 /* verify UDP checksum. IP header has to be modified for this */
990 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
991 /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
992 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
993 check = packet.udp.check;
994 packet.udp.check = 0;
995 if (check && check != dhcp_checksum(&packet, bytes))
998 memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
999 sizeof(packet.udp)));
1001 if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1004 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1007 static void ipv4ll_start(GDHCPClient *dhcp_client)
1012 if (dhcp_client->timeout > 0) {
1013 g_source_remove(dhcp_client->timeout);
1014 dhcp_client->timeout = 0;
1017 switch_listening_mode(dhcp_client, L_NONE);
1018 dhcp_client->type = G_DHCP_IPV4LL;
1019 dhcp_client->retry_times = 0;
1020 dhcp_client->requested_ip = 0;
1022 /*try to start with a based mac address ip*/
1023 seed = (dhcp_client->mac_address[4] << 8 | dhcp_client->mac_address[4]);
1024 dhcp_client->requested_ip = ipv4ll_random_ip(seed);
1026 /*first wait a random delay to avoid storm of arp request on boot*/
1027 timeout = ipv4ll_random_delay_ms(PROBE_WAIT);
1029 dhcp_client->retry_times++;
1030 dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1037 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1040 switch_listening_mode(dhcp_client, L_NONE);
1042 if (dhcp_client->timeout > 0)
1043 g_source_remove(dhcp_client->timeout);
1045 if (dhcp_client->listener_watch > 0) {
1046 g_source_remove(dhcp_client->listener_watch);
1047 dhcp_client->listener_watch = 0;
1050 dhcp_client->state = IPV4LL_PROBE;
1051 dhcp_client->retry_times = 0;
1052 dhcp_client->requested_ip = 0;
1054 g_free(dhcp_client->assigned_ip);
1055 dhcp_client->assigned_ip = NULL;
1058 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1061 struct ether_arp arp;
1062 uint32_t ip_requested;
1063 int source_conflict;
1064 int target_conflict;
1066 memset(&arp, 0, sizeof(arp));
1068 bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1072 if (arp.arp_op != htons(ARPOP_REPLY) &&
1073 arp.arp_op != htons(ARPOP_REQUEST))
1076 ip_requested = ntohl(dhcp_client->requested_ip);
1077 source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1078 sizeof(ip_requested));
1080 target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1081 sizeof(ip_requested));
1083 if (!source_conflict && !target_conflict)
1086 dhcp_client->conflicts++;
1088 debug(dhcp_client, "IPV4LL conflict detected");
1090 if (dhcp_client->state == IPV4LL_MONITOR) {
1091 if (!source_conflict)
1093 dhcp_client->state = IPV4LL_DEFEND;
1094 debug(dhcp_client, "DEFEND mode conflicts : %d",
1095 dhcp_client->conflicts);
1096 /*Try to defend with a single announce*/
1097 send_announce_packet(dhcp_client);
1101 if (dhcp_client->state == IPV4LL_DEFEND) {
1102 if (!source_conflict)
1104 else if (dhcp_client->ipv4ll_lost_cb != NULL)
1105 dhcp_client->ipv4ll_lost_cb(dhcp_client,
1106 dhcp_client->ipv4ll_lost_data);
1109 ipv4ll_stop(dhcp_client);
1111 if (dhcp_client->conflicts < MAX_CONFLICTS) {
1112 /*restart whole state machine*/
1113 dhcp_client->retry_times++;
1114 dhcp_client->timeout =
1115 g_timeout_add_full(G_PRIORITY_HIGH,
1116 ipv4ll_random_delay_ms(PROBE_WAIT),
1121 /* Here we got a lot of conflicts, RFC3927 states that we have
1122 * to wait RATE_LIMIT_INTERVAL before retrying,
1123 * but we just report failure.
1125 else if (dhcp_client->no_lease_cb != NULL)
1126 dhcp_client->no_lease_cb(dhcp_client,
1127 dhcp_client->no_lease_data);
1132 static gboolean check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1134 if (dhcp_client->type == G_DHCP_IPV6) {
1135 struct dhcpv6_packet *packet6 = pkt;
1138 if (packet6 == NULL)
1141 xid = packet6->transaction_id[0] << 16 |
1142 packet6->transaction_id[1] << 8 |
1143 packet6->transaction_id[2];
1145 if (xid != dhcp_client->xid)
1148 struct dhcp_packet *packet = pkt;
1150 if (packet->xid != dhcp_client->xid)
1153 if (packet->hlen != 6)
1156 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1163 static void start_request(GDHCPClient *dhcp_client);
1165 static gboolean request_timeout(gpointer user_data)
1167 GDHCPClient *dhcp_client = user_data;
1169 debug(dhcp_client, "request timeout (retries %d)",
1170 dhcp_client->retry_times);
1172 dhcp_client->retry_times++;
1174 start_request(dhcp_client);
1179 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1180 gpointer user_data);
1182 static int switch_listening_mode(GDHCPClient *dhcp_client,
1183 ListenMode listen_mode)
1185 GIOChannel *listener_channel;
1186 int listener_sockfd;
1188 if (dhcp_client->listen_mode == listen_mode)
1191 debug(dhcp_client, "switch listening mode (%d ==> %d)",
1192 dhcp_client->listen_mode, listen_mode);
1194 if (dhcp_client->listen_mode != L_NONE) {
1195 if (dhcp_client->listener_watch > 0)
1196 g_source_remove(dhcp_client->listener_watch);
1197 dhcp_client->listener_channel = NULL;
1198 dhcp_client->listen_mode = L_NONE;
1199 dhcp_client->listener_sockfd = -1;
1200 dhcp_client->listener_watch = 0;
1203 if (listen_mode == L_NONE)
1206 if (listen_mode == L2)
1207 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1208 else if (listen_mode == L3) {
1209 if (dhcp_client->type == G_DHCP_IPV6)
1210 listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1211 dhcp_client->interface,
1214 listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1215 dhcp_client->interface,
1217 } else if (listen_mode == L_ARP)
1218 listener_sockfd = ipv4ll_arp_socket(dhcp_client->ifindex);
1222 if (listener_sockfd < 0)
1225 listener_channel = g_io_channel_unix_new(listener_sockfd);
1226 if (listener_channel == NULL) {
1227 /* Failed to create listener channel */
1228 close(listener_sockfd);
1232 dhcp_client->listen_mode = listen_mode;
1233 dhcp_client->listener_sockfd = listener_sockfd;
1234 dhcp_client->listener_channel = listener_channel;
1236 g_io_channel_set_close_on_unref(listener_channel, TRUE);
1237 dhcp_client->listener_watch =
1238 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1239 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1240 listener_event, dhcp_client,
1242 g_io_channel_unref(dhcp_client->listener_channel);
1247 static void start_request(GDHCPClient *dhcp_client)
1249 debug(dhcp_client, "start request (retries %d)",
1250 dhcp_client->retry_times);
1252 if (dhcp_client->retry_times == REQUEST_RETRIES) {
1253 dhcp_client->state = INIT_SELECTING;
1254 ipv4ll_start(dhcp_client);
1259 if (dhcp_client->retry_times == 0) {
1260 dhcp_client->state = REQUESTING;
1261 switch_listening_mode(dhcp_client, L2);
1264 send_select(dhcp_client);
1266 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1273 static uint32_t get_lease(struct dhcp_packet *packet)
1276 uint32_t lease_seconds;
1278 option_u8 = dhcp_get_option(packet, DHCP_LEASE_TIME);
1279 if (option_u8 == NULL)
1282 lease_seconds = dhcp_get_unaligned((uint32_t *) option_u8);
1283 lease_seconds = ntohl(lease_seconds);
1284 /* paranoia: must not be prone to overflows */
1285 lease_seconds &= 0x0fffffff;
1286 if (lease_seconds < 10)
1289 return lease_seconds;
1292 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1294 debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1296 if (dhcp_client->timeout > 0) {
1297 g_source_remove(dhcp_client->timeout);
1298 dhcp_client->timeout = 0;
1301 dhcp_client->retry_times = retry_times;
1302 dhcp_client->requested_ip = 0;
1303 switch_listening_mode(dhcp_client, L2);
1305 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1308 static gboolean start_rebound_timeout(gpointer user_data)
1310 GDHCPClient *dhcp_client = user_data;
1312 debug(dhcp_client, "start rebound timeout");
1314 switch_listening_mode(dhcp_client, L2);
1316 dhcp_client->lease_seconds >>= 1;
1318 /* We need to have enough time to receive ACK package*/
1319 if (dhcp_client->lease_seconds <= 6) {
1321 /* ip need to be cleared */
1322 if (dhcp_client->lease_lost_cb != NULL)
1323 dhcp_client->lease_lost_cb(dhcp_client,
1324 dhcp_client->lease_lost_data);
1326 restart_dhcp(dhcp_client, 0);
1328 send_rebound(dhcp_client);
1330 dhcp_client->timeout =
1331 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1332 dhcp_client->lease_seconds >> 1,
1333 start_rebound_timeout,
1341 static void start_rebound(GDHCPClient *dhcp_client)
1343 debug(dhcp_client, "start rebound");
1345 dhcp_client->state = REBINDING;
1347 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1348 dhcp_client->lease_seconds >> 1,
1349 start_rebound_timeout,
1354 static gboolean start_renew_timeout(gpointer user_data)
1356 GDHCPClient *dhcp_client = user_data;
1358 debug(dhcp_client, "start renew timeout");
1360 dhcp_client->state = RENEWING;
1362 dhcp_client->lease_seconds >>= 1;
1364 switch_listening_mode(dhcp_client, L3);
1365 if (dhcp_client->lease_seconds <= 60)
1366 start_rebound(dhcp_client);
1368 send_renew(dhcp_client);
1370 if (dhcp_client->timeout > 0)
1371 g_source_remove(dhcp_client->timeout);
1373 dhcp_client->timeout =
1374 g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1375 dhcp_client->lease_seconds >> 1,
1376 start_renew_timeout,
1384 static void start_bound(GDHCPClient *dhcp_client)
1386 debug(dhcp_client, "start bound");
1388 dhcp_client->state = BOUND;
1390 if (dhcp_client->timeout > 0)
1391 g_source_remove(dhcp_client->timeout);
1393 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1394 dhcp_client->lease_seconds >> 1,
1395 start_renew_timeout, dhcp_client,
1399 static gboolean restart_dhcp_timeout(gpointer user_data)
1401 GDHCPClient *dhcp_client = user_data;
1403 debug(dhcp_client, "restart DHCP timeout");
1405 dhcp_client->ack_retry_times++;
1407 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1412 static char *get_ip(uint32_t ip)
1414 struct in_addr addr;
1418 return g_strdup(inet_ntoa(addr));
1421 /* get a rough idea of how long an option will be */
1422 static const uint8_t len_of_option_as_string[] = {
1423 [OPTION_IP] = sizeof("255.255.255.255 "),
1424 [OPTION_STRING] = 1,
1425 [OPTION_U8] = sizeof("255 "),
1426 [OPTION_U16] = sizeof("65535 "),
1427 [OPTION_U32] = sizeof("4294967295 "),
1430 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1432 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1435 /* Create "opt_value1 option_value2 ..." string */
1436 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1438 unsigned upper_length;
1442 len = option[OPT_LEN - OPT_DATA];
1443 type &= OPTION_TYPE_MASK;
1444 optlen = dhcp_option_lengths[type];
1447 upper_length = len_of_option_as_string[type] *
1448 ((unsigned)len / (unsigned)optlen);
1449 dest = ret = malloc(upper_length + 1);
1453 while (len >= optlen) {
1456 dest += sprint_nip(dest, "", option);
1459 uint16_t val_u16 = dhcp_get_unaligned(
1460 (uint16_t *) option);
1461 dest += sprintf(dest, "%u", ntohs(val_u16));
1465 uint32_t val_u32 = dhcp_get_unaligned(
1466 (uint32_t *) option);
1467 dest += sprintf(dest, type == OPTION_U32 ? "%lu" :
1468 "%ld", (unsigned long) ntohl(val_u32));
1472 memcpy(dest, option, len);
1489 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1497 if (type == OPTION_STRING)
1498 return g_list_append(list, g_strdup(value));
1500 while ((pos = strchr(pos, ' ')) != NULL) {
1503 list = g_list_append(list, g_strdup(value));
1508 list = g_list_append(list, g_strdup(value));
1513 static inline uint32_t get_uint32(unsigned char *value)
1515 return value[0] << 24 | value[1] << 16 |
1516 value[2] << 8 | value[3];
1519 static inline uint16_t get_uint16(unsigned char *value)
1521 return value[0] << 8 | value[1];
1524 static GList *get_addresses(GDHCPClient *dhcp_client,
1526 unsigned char *value,
1530 struct in6_addr addr;
1531 uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
1532 uint16_t option_len, option_code, st = 0, max_len;
1533 int addr_count = 0, i, pos;
1537 iaid = get_uint32(&value[0]);
1538 if (dhcp_client->iaid != iaid)
1541 if (code == G_DHCPV6_IA_NA) {
1542 T1 = get_uint32(&value[4]);
1543 T2 = get_uint32(&value[8]);
1546 /* RFC 3315, 22.4 */
1556 max_len = len - pos;
1558 /* We have more sub-options in this packet. */
1560 option = dhcpv6_get_sub_option(&value[pos], max_len,
1561 &option_code, &option_len);
1563 debug(dhcp_client, "pos %d option %p code %d len %d",
1564 pos, option, option_code, option_len);
1572 switch (option_code) {
1573 case G_DHCPV6_IAADDR:
1575 memcpy(&addr, &option[0], sizeof(addr));
1577 preferred = get_uint32(&option[i]);
1579 valid = get_uint32(&option[i]);
1584 case G_DHCPV6_STATUS_CODE:
1585 st = get_uint16(&option[0]);
1586 debug(dhcp_client, "error code %d", st);
1587 if (option_len > 2) {
1588 str = g_strndup((gchar *)&option[2],
1590 debug(dhcp_client, "error text: %s", str);
1598 pos += 2 + 2 + option_len;
1600 } while (option != NULL);
1602 if (addr_count > 0 && st == 0) {
1603 /* We only support one address atm */
1604 char str[INET6_ADDRSTRLEN + 1];
1606 if (preferred > valid)
1607 /* RFC 3315, 22.6 */
1610 dhcp_client->T1 = T1;
1611 dhcp_client->T2 = T2;
1613 inet_ntop(AF_INET6, &addr, str, INET6_ADDRSTRLEN);
1614 debug(dhcp_client, "count %d addr %s T1 %u T2 %u",
1615 addr_count, str, T1, T2);
1617 list = g_list_append(list, g_strdup(str));
1619 if (code == G_DHCPV6_IA_NA)
1620 memcpy(&dhcp_client->ia_na, &addr,
1621 sizeof(struct in6_addr));
1623 memcpy(&dhcp_client->ia_ta, &addr,
1624 sizeof(struct in6_addr));
1630 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
1632 unsigned char *value,
1640 case G_DHCPV6_DNS_SERVERS: /* RFC 3646, chapter 3 */
1641 case G_DHCPV6_SNTP_SERVERS: /* RFC 4075, chapter 4 */
1644 "%s server list length (%d) is invalid",
1645 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
1649 for (i = 0; i < len; i += 16) {
1651 str = g_try_malloc0(INET6_ADDRSTRLEN+1);
1655 if (inet_ntop(AF_INET6, &value[i], str,
1656 INET6_ADDRSTRLEN) == NULL)
1659 list = g_list_append(list, str);
1663 case G_DHCPV6_IA_NA: /* RFC 3315, chapter 22.4 */
1664 case G_DHCPV6_IA_TA: /* RFC 3315, chapter 22.5 */
1665 list = get_addresses(dhcp_client, code, len, value, status);
1675 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
1676 struct dhcpv6_packet *packet,
1677 uint16_t pkt_len, uint16_t *status)
1679 GList *list, *value_list;
1682 uint16_t option_len;
1684 for (list = dhcp_client->request_list; list; list = list->next) {
1685 code = (uint16_t) GPOINTER_TO_INT(list->data);
1687 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
1689 if (option == NULL) {
1690 g_hash_table_remove(dhcp_client->code_value_hash,
1691 GINT_TO_POINTER((int) code));
1695 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
1696 option_len, option, status);
1698 debug(dhcp_client, "code %d %p len %d list %p", code, option,
1699 option_len, value_list);
1701 if (value_list == NULL)
1702 g_hash_table_remove(dhcp_client->code_value_hash,
1703 GINT_TO_POINTER((int) code));
1705 g_hash_table_insert(dhcp_client->code_value_hash,
1706 GINT_TO_POINTER((int) code), value_list);
1710 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet)
1712 GDHCPOptionType type;
1713 GList *list, *value_list;
1718 for (list = dhcp_client->request_list; list; list = list->next) {
1719 code = (uint8_t) GPOINTER_TO_INT(list->data);
1721 option = dhcp_get_option(packet, code);
1722 if (option == NULL) {
1723 g_hash_table_remove(dhcp_client->code_value_hash,
1724 GINT_TO_POINTER((int) code));
1728 type = dhcp_get_code_type(code);
1730 option_value = malloc_option_value_string(option, type);
1731 if (option_value == NULL)
1732 g_hash_table_remove(dhcp_client->code_value_hash,
1733 GINT_TO_POINTER((int) code));
1735 value_list = get_option_value_list(option_value, type);
1737 g_free(option_value);
1739 if (value_list == NULL)
1740 g_hash_table_remove(dhcp_client->code_value_hash,
1741 GINT_TO_POINTER((int) code));
1743 g_hash_table_insert(dhcp_client->code_value_hash,
1744 GINT_TO_POINTER((int) code), value_list);
1748 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1751 GDHCPClient *dhcp_client = user_data;
1752 struct dhcp_packet packet;
1753 struct dhcpv6_packet *packet6 = NULL;
1754 uint8_t *message_type = NULL, *client_id = NULL, *option_u8,
1756 uint16_t option_len = 0, status = 0;
1758 unsigned char buf[MAX_DHCPV6_PKT_SIZE];
1759 uint16_t pkt_len = 0;
1763 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1764 dhcp_client->listener_watch = 0;
1768 if (dhcp_client->listen_mode == L_NONE)
1773 if (dhcp_client->listen_mode == L2)
1774 re = dhcp_recv_l2_packet(&packet,
1775 dhcp_client->listener_sockfd);
1776 else if (dhcp_client->listen_mode == L3) {
1777 if (dhcp_client->type == G_DHCP_IPV6) {
1778 re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
1779 dhcp_client->listener_sockfd);
1783 re = dhcp_recv_l3_packet(&packet,
1784 dhcp_client->listener_sockfd);
1785 } else if (dhcp_client->listen_mode == L_ARP) {
1786 re = ipv4ll_recv_arp_packet(dhcp_client);
1795 if (check_package_owner(dhcp_client, pkt) == FALSE)
1798 if (dhcp_client->type == G_DHCP_IPV6) {
1800 client_id = dhcpv6_get_option(packet6, pkt_len,
1801 G_DHCPV6_CLIENTID, &option_len, &count);
1803 if (client_id == NULL || count == 0 || option_len == 0 ||
1804 memcmp(dhcp_client->duid, client_id,
1805 dhcp_client->duid_len) != 0) {
1807 "client duid error, discarding msg %p/%d/%d",
1808 client_id, option_len, count);
1812 option_u8 = dhcpv6_get_option(packet6, pkt_len,
1813 G_DHCPV6_STATUS_CODE, &option_len, NULL);
1814 if (option_u8 != 0 && option_len > 0) {
1815 status = option_u8[0]<<8 | option_u8[1];
1817 debug(dhcp_client, "error code %d", status);
1818 if (option_len > 2) {
1819 gchar *txt = g_strndup(
1820 (gchar *)&option_u8[2],
1822 debug(dhcp_client, "error text: %s",
1827 dhcp_client->status_code = status;
1829 dhcp_client->status_code = 0;
1832 message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
1834 if (message_type == NULL && client_id == NULL)
1835 /* No message type / client id option, ignore package */
1838 debug(dhcp_client, "received DHCP packet (current state %d)",
1839 dhcp_client->state);
1841 switch (dhcp_client->state) {
1842 case INIT_SELECTING:
1843 if (*message_type != DHCPOFFER)
1846 g_source_remove(dhcp_client->timeout);
1847 dhcp_client->timeout = 0;
1848 dhcp_client->retry_times = 0;
1850 option_u8 = dhcp_get_option(&packet, DHCP_SERVER_ID);
1851 dhcp_client->server_ip =
1852 dhcp_get_unaligned((uint32_t *) option_u8);
1853 dhcp_client->requested_ip = packet.yiaddr;
1855 dhcp_client->state = REQUESTING;
1857 start_request(dhcp_client);
1863 if (*message_type == DHCPACK) {
1864 dhcp_client->retry_times = 0;
1866 if (dhcp_client->timeout > 0)
1867 g_source_remove(dhcp_client->timeout);
1868 dhcp_client->timeout = 0;
1870 dhcp_client->lease_seconds = get_lease(&packet);
1872 get_request(dhcp_client, &packet);
1874 switch_listening_mode(dhcp_client, L_NONE);
1876 g_free(dhcp_client->assigned_ip);
1877 dhcp_client->assigned_ip = get_ip(packet.yiaddr);
1879 /* Address should be set up here */
1880 if (dhcp_client->lease_available_cb != NULL)
1881 dhcp_client->lease_available_cb(dhcp_client,
1882 dhcp_client->lease_available_data);
1884 start_bound(dhcp_client);
1885 } else if (*message_type == DHCPNAK) {
1886 dhcp_client->retry_times = 0;
1888 if (dhcp_client->timeout > 0)
1889 g_source_remove(dhcp_client->timeout);
1891 dhcp_client->timeout = g_timeout_add_seconds_full(
1893 restart_dhcp_timeout,
1900 if (dhcp_client->type != G_DHCP_IPV6)
1903 if (packet6->message != DHCPV6_REPLY &&
1904 packet6->message != DHCPV6_ADVERTISE)
1908 server_id = dhcpv6_get_option(packet6, pkt_len,
1909 G_DHCPV6_SERVERID, &option_len, &count);
1910 if (server_id == NULL || count != 1 || option_len == 0) {
1911 /* RFC 3315, 15.10 */
1913 "server duid error, discarding msg %p/%d/%d",
1914 server_id, option_len, count);
1917 dhcp_client->server_duid = g_try_malloc(option_len);
1918 if (dhcp_client->server_duid == NULL)
1920 memcpy(dhcp_client->server_duid, server_id, option_len);
1921 dhcp_client->server_duid_len = option_len;
1923 if (packet6->message == DHCPV6_REPLY) {
1924 uint8_t *rapid_commit;
1927 rapid_commit = dhcpv6_get_option(packet6, pkt_len,
1928 G_DHCPV6_RAPID_COMMIT,
1929 &option_len, &count);
1930 if (rapid_commit == NULL || option_len == 0 ||
1932 /* RFC 3315, 17.1.4 */
1936 switch_listening_mode(dhcp_client, L_NONE);
1938 if (dhcp_client->status_code == 0)
1939 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
1940 &dhcp_client->status_code);
1942 if (packet6->message == DHCPV6_ADVERTISE) {
1943 if (dhcp_client->advertise_cb != NULL)
1944 dhcp_client->advertise_cb(dhcp_client,
1945 dhcp_client->advertise_data);
1949 if (dhcp_client->solicitation_cb != NULL) {
1951 * The dhcp_client might not be valid after the
1952 * callback call so just return immediately.
1954 dhcp_client->solicitation_cb(dhcp_client,
1955 dhcp_client->solicitation_data);
1959 case INFORMATION_REQ:
1960 if (dhcp_client->type != G_DHCP_IPV6)
1963 if (packet6->message != DHCPV6_REPLY)
1968 server_id = dhcpv6_get_option(packet6, pkt_len,
1969 G_DHCPV6_SERVERID, &option_len, &count);
1970 if (server_id == NULL || count != 1 || option_len == 0) {
1971 /* RFC 3315, 15.10 */
1973 "server duid error, discarding msg %p/%d/%d",
1974 server_id, option_len, count);
1978 switch_listening_mode(dhcp_client, L_NONE);
1980 dhcp_client->status_code = 0;
1981 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
1982 &dhcp_client->status_code);
1984 if (dhcp_client->information_req_cb != NULL) {
1986 * The dhcp_client might not be valid after the
1987 * callback call so just return immediately.
1989 dhcp_client->information_req_cb(dhcp_client,
1990 dhcp_client->information_req_data);
1998 debug(dhcp_client, "processed DHCP packet (new state %d)",
1999 dhcp_client->state);
2004 static gboolean discover_timeout(gpointer user_data)
2006 GDHCPClient *dhcp_client = user_data;
2008 dhcp_client->retry_times++;
2011 * We do not send the REQUESTED IP option if we are retrying because
2012 * if the server is non-authoritative it will ignore the request if the
2013 * option is present.
2015 g_dhcp_client_start(dhcp_client, NULL);
2020 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2022 GDHCPClient *dhcp_client = dhcp_data;
2024 debug(dhcp_client, "back to MONITOR mode");
2026 dhcp_client->conflicts = 0;
2027 dhcp_client->state = IPV4LL_MONITOR;
2032 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2034 GDHCPClient *dhcp_client = dhcp_data;
2037 debug(dhcp_client, "request timeout (retries %d)",
2038 dhcp_client->retry_times);
2040 if (dhcp_client->retry_times != ANNOUNCE_NUM){
2041 dhcp_client->retry_times++;
2042 send_announce_packet(dhcp_client);
2046 ip = htonl(dhcp_client->requested_ip);
2047 debug(dhcp_client, "switching to monitor mode");
2048 dhcp_client->state = IPV4LL_MONITOR;
2049 dhcp_client->assigned_ip = get_ip(ip);
2051 if (dhcp_client->ipv4ll_available_cb != NULL)
2052 dhcp_client->ipv4ll_available_cb(dhcp_client,
2053 dhcp_client->ipv4ll_available_data);
2054 dhcp_client->conflicts = 0;
2059 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2062 GDHCPClient *dhcp_client = dhcp_data;
2064 debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2065 dhcp_client->retry_times);
2067 if (dhcp_client->retry_times == PROBE_NUM) {
2068 dhcp_client->state = IPV4LL_ANNOUNCE;
2069 dhcp_client->retry_times = 0;
2071 dhcp_client->retry_times++;
2072 send_announce_packet(dhcp_client);
2075 dhcp_client->retry_times++;
2076 send_probe_packet(dhcp_client);
2081 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2086 if (dhcp_client->type == G_DHCP_IPV6) {
2087 if (dhcp_client->information_req_cb) {
2088 dhcp_client->state = INFORMATION_REQ;
2089 re = switch_listening_mode(dhcp_client, L3);
2091 switch_listening_mode(dhcp_client, L_NONE);
2092 dhcp_client->state = 0;
2095 send_information_req(dhcp_client);
2097 } else if (dhcp_client->solicitation_cb) {
2098 dhcp_client->state = SOLICITATION;
2099 re = switch_listening_mode(dhcp_client, L3);
2101 switch_listening_mode(dhcp_client, L_NONE);
2102 dhcp_client->state = 0;
2105 send_solicitation(dhcp_client);
2111 if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2112 ipv4ll_start(dhcp_client);
2116 if (dhcp_client->retry_times == 0) {
2117 g_free(dhcp_client->assigned_ip);
2118 dhcp_client->assigned_ip = NULL;
2120 dhcp_client->state = INIT_SELECTING;
2121 re = switch_listening_mode(dhcp_client, L2);
2125 dhcp_client->xid = rand();
2128 if (last_address == NULL) {
2131 addr = inet_addr(last_address);
2132 if (addr == 0xFFFFFFFF) {
2135 g_free(dhcp_client->last_address);
2136 dhcp_client->last_address = g_strdup(last_address);
2139 send_discover(dhcp_client, addr);
2141 dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2149 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
2151 switch_listening_mode(dhcp_client, L_NONE);
2153 if (dhcp_client->state == BOUND ||
2154 dhcp_client->state == RENEWING ||
2155 dhcp_client->state == REBINDING)
2156 send_release(dhcp_client, dhcp_client->server_ip,
2157 dhcp_client->requested_ip);
2159 if (dhcp_client->timeout > 0) {
2160 g_source_remove(dhcp_client->timeout);
2161 dhcp_client->timeout = 0;
2164 if (dhcp_client->listener_watch > 0) {
2165 g_source_remove(dhcp_client->listener_watch);
2166 dhcp_client->listener_watch = 0;
2169 dhcp_client->listener_channel = NULL;
2171 dhcp_client->retry_times = 0;
2172 dhcp_client->ack_retry_times = 0;
2174 dhcp_client->requested_ip = 0;
2175 dhcp_client->state = RELEASED;
2176 dhcp_client->lease_seconds = 0;
2179 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
2180 unsigned char option_code)
2182 return g_hash_table_lookup(dhcp_client->code_value_hash,
2183 GINT_TO_POINTER((int) option_code));
2186 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
2187 GDHCPClientEvent event,
2188 GDHCPClientEventFunc func,
2192 case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
2193 dhcp_client->lease_available_cb = func;
2194 dhcp_client->lease_available_data = data;
2196 case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
2197 if (dhcp_client->type == G_DHCP_IPV6)
2199 dhcp_client->ipv4ll_available_cb = func;
2200 dhcp_client->ipv4ll_available_data = data;
2202 case G_DHCP_CLIENT_EVENT_NO_LEASE:
2203 dhcp_client->no_lease_cb = func;
2204 dhcp_client->no_lease_data = data;
2206 case G_DHCP_CLIENT_EVENT_LEASE_LOST:
2207 dhcp_client->lease_lost_cb = func;
2208 dhcp_client->lease_lost_data = data;
2210 case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
2211 if (dhcp_client->type == G_DHCP_IPV6)
2213 dhcp_client->ipv4ll_lost_cb = func;
2214 dhcp_client->ipv4ll_lost_data = data;
2216 case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
2217 dhcp_client->address_conflict_cb = func;
2218 dhcp_client->address_conflict_data = data;
2220 case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
2221 if (dhcp_client->type == G_DHCP_IPV4)
2223 dhcp_client->information_req_cb = func;
2224 dhcp_client->information_req_data = data;
2226 case G_DHCP_CLIENT_EVENT_SOLICITATION:
2227 if (dhcp_client->type == G_DHCP_IPV4)
2229 dhcp_client->solicitation_cb = func;
2230 dhcp_client->solicitation_data = data;
2232 case G_DHCP_CLIENT_EVENT_ADVERTISE:
2233 if (dhcp_client->type == G_DHCP_IPV4)
2235 dhcp_client->advertise_cb = func;
2236 dhcp_client->advertise_data = data;
2241 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
2243 return dhcp_client->ifindex;
2246 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
2248 return g_strdup(dhcp_client->assigned_ip);
2251 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
2253 GList *option = NULL;
2255 if (dhcp_client->type == G_DHCP_IPV6)
2258 switch (dhcp_client->state) {
2260 case IPV4LL_MONITOR:
2261 return g_strdup("255.255.0.0");
2265 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
2267 return g_strdup(option->data);
2268 case INIT_SELECTING:
2272 case IPV4LL_ANNOUNCE:
2273 case INFORMATION_REQ:
2280 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
2281 unsigned int option_code)
2283 if (g_list_find(dhcp_client->request_list,
2284 GINT_TO_POINTER((int) option_code)) == NULL)
2285 dhcp_client->request_list = g_list_prepend(
2286 dhcp_client->request_list,
2287 (GINT_TO_POINTER((int) option_code)));
2289 return G_DHCP_CLIENT_ERROR_NONE;
2292 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
2294 g_list_free(dhcp_client->request_list);
2295 dhcp_client->request_list = NULL;
2298 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
2300 g_hash_table_remove_all(dhcp_client->send_value_hash);
2303 static uint8_t *alloc_dhcp_option(int code, const char *str, int extra)
2306 int len = strnlen(str, 255);
2308 storage = malloc(len + extra + OPT_DATA);
2309 storage[OPT_CODE] = code;
2310 storage[OPT_LEN] = len + extra;
2311 memcpy(storage + extra + OPT_DATA, str, len);
2316 /* Now only support send hostname */
2317 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
2318 unsigned char option_code, const char *option_value)
2320 uint8_t *binary_option;
2322 if (option_code == G_DHCP_HOST_NAME && option_value != NULL) {
2323 binary_option = alloc_dhcp_option(option_code,
2326 g_hash_table_insert(dhcp_client->send_value_hash,
2327 GINT_TO_POINTER((int) option_code), binary_option);
2330 return G_DHCP_CLIENT_ERROR_NONE;
2333 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
2338 storage = g_malloc(2 + 2 + len);
2339 if (storage == NULL)
2342 storage[0] = code >> 8;
2343 storage[1] = code & 0xff;
2344 storage[2] = len >> 8;
2345 storage[3] = len & 0xff;
2346 memcpy(storage + 2 + 2, option, len);
2351 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
2352 uint16_t option_code,
2353 uint8_t *option_value,
2354 uint16_t option_len)
2356 if (option_value != NULL) {
2357 uint8_t *binary_option;
2359 debug(dhcp_client, "setting option %d to %p len %d",
2360 option_code, option_value, option_len);
2362 binary_option = alloc_dhcpv6_option(option_code, option_value,
2364 if (binary_option != NULL)
2365 g_hash_table_insert(dhcp_client->send_value_hash,
2366 GINT_TO_POINTER((int) option_code),
2371 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
2373 if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
2376 return dhcp_client->status_code;
2379 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
2381 if (dhcp_client == NULL)
2384 __sync_fetch_and_add(&dhcp_client->ref_count, 1);
2389 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
2391 if (dhcp_client == NULL)
2394 if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
2397 g_dhcp_client_stop(dhcp_client);
2399 g_free(dhcp_client->interface);
2400 g_free(dhcp_client->assigned_ip);
2401 g_free(dhcp_client->last_address);
2402 g_free(dhcp_client->duid);
2403 g_free(dhcp_client->server_duid);
2405 g_list_free(dhcp_client->request_list);
2406 g_list_free(dhcp_client->require_list);
2408 g_hash_table_destroy(dhcp_client->code_value_hash);
2409 g_hash_table_destroy(dhcp_client->send_value_hash);
2411 g_free(dhcp_client);
2414 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
2415 GDHCPDebugFunc func, gpointer user_data)
2417 if (dhcp_client == NULL)
2420 dhcp_client->debug_func = func;
2421 dhcp_client->debug_data = user_data;