2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
5 * Copyright (C) 2006 Daniel Roethlisberger
6 * Copyright (C) 2005 Jan Hutter
7 * Hochschule fuer Technik Rapperswil
8 * Copyright (C) 2003 Herbert Xu.
10 * Based on xfrm code from pluto.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/xfrm.h>
31 #include <linux/udp.h>
32 #include <netinet/in.h>
39 #include <sys/ioctl.h>
41 #include "kernel_interface.h"
44 #include <utils/linked_list.h>
45 #include <processing/jobs/delete_child_sa_job.h>
46 #include <processing/jobs/rekey_child_sa_job.h>
47 #include <processing/jobs/acquire_job.h>
48 #include <processing/jobs/callback_job.h>
49 #include <processing/jobs/roam_job.h>
51 /** routing table for routes installed by us */
52 #ifndef IPSEC_ROUTING_TABLE
53 #define IPSEC_ROUTING_TABLE 100
55 #ifndef IPSEC_ROUTING_TABLE_PRIO
56 #define IPSEC_ROUTING_TABLE_PRIO 100
59 /** default priority of installed policies */
61 #define PRIO_HIGH 2000
63 /** delay before firing roam jobs (ms) */
64 #define ROAM_DELAY 100
66 #define BUFFER_SIZE 1024
69 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
70 * 'usual' netlink data x like 'struct xfrm_usersa_info'
72 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
74 * returns a pointer to the next rtattr following rta.
75 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
77 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
79 * returns the total size of attached rta data
80 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
82 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
84 typedef struct kernel_algorithm_t kernel_algorithm_t
;
87 * Mapping from the algorithms defined in IKEv2 to
88 * kernel level algorithm names and their key length
90 struct kernel_algorithm_t
{
92 * Identifier specified in IKEv2
97 * Name of the algorithm, as used as kernel identifier
102 * Key length in bits, if fixed size
106 #define END_OF_LIST -1
109 * Algorithms for encryption
111 static kernel_algorithm_t encryption_algs
[] = {
112 /* {ENCR_DES_IV64, "***", 0}, */
113 {ENCR_DES
, "des", 64},
114 {ENCR_3DES
, "des3_ede", 192},
115 /* {ENCR_RC5, "***", 0}, */
116 /* {ENCR_IDEA, "***", 0}, */
117 {ENCR_CAST
, "cast128", 0},
118 {ENCR_BLOWFISH
, "blowfish", 0},
119 /* {ENCR_3IDEA, "***", 0}, */
120 /* {ENCR_DES_IV32, "***", 0}, */
121 {ENCR_NULL
, "cipher_null", 0},
122 {ENCR_AES_CBC
, "aes", 0},
123 /* {ENCR_AES_CTR, "***", 0}, */
124 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))", 64}, /* key_size = ICV size */
125 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))", 96}, /* key_size = ICV size */
126 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))", 128}, /* key_size = ICV size */
127 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))", 64}, /* key_size = ICV size */
128 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))", 96}, /* key_size = ICV size */
129 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))", 128}, /* key_size = ICV size */
130 {END_OF_LIST
, NULL
, 0},
134 * Algorithms for integrity protection
136 static kernel_algorithm_t integrity_algs
[] = {
137 {AUTH_HMAC_MD5_96
, "md5", 128},
138 {AUTH_HMAC_SHA1_96
, "sha1", 160},
139 {AUTH_HMAC_SHA2_256_128
, "sha256", 256},
140 {AUTH_HMAC_SHA2_384_192
, "sha384", 384},
141 {AUTH_HMAC_SHA2_512_256
, "sha512", 512},
142 /* {AUTH_DES_MAC, "***", 0}, */
143 /* {AUTH_KPDK_MD5, "***", 0}, */
144 {AUTH_AES_XCBC_96
, "xcbc(aes)", 128},
145 {END_OF_LIST
, NULL
, 0},
149 * Algorithms for IPComp
151 static kernel_algorithm_t compression_algs
[] = {
152 /* {IPCOMP_OUI, "***", 0}, */
153 {IPCOMP_DEFLATE
, "deflate", 0},
154 {IPCOMP_LZS
, "lzs", 0},
155 {IPCOMP_LZJH
, "lzjh", 0},
156 {END_OF_LIST
, NULL
, 0},
160 * Look up a kernel algorithm name and its key size
162 static char* lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
163 u_int16_t ikev2_algo
, u_int16_t
*key_size
)
165 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
167 if (ikev2_algo
== kernel_algo
->ikev2_id
)
169 /* match, evaluate key length */
170 if (key_size
&& *key_size
== 0)
171 { /* update key size if not set */
172 *key_size
= kernel_algo
->key_size
;
174 return kernel_algo
->name
;
181 typedef struct route_entry_t route_entry_t
;
184 * installed routing entry
186 struct route_entry_t
{
188 /** Index of the interface the route is bound to */
191 /** Source ip of the route */
194 /** gateway for this route */
197 /** Destination net */
200 /** Destination net prefixlen */
205 * destroy an route_entry_t object
207 static void route_entry_destroy(route_entry_t
*this)
209 this->src_ip
->destroy(this->src_ip
);
210 this->gateway
->destroy(this->gateway
);
211 chunk_free(&this->dst_net
);
215 typedef struct policy_entry_t policy_entry_t
;
218 * installed kernel policy.
220 struct policy_entry_t
{
222 /** direction of this policy: in, out, forward */
225 /** reqid of the policy */
228 /** parameters of installed policy */
229 struct xfrm_selector sel
;
231 /** associated route installed for this policy */
232 route_entry_t
*route
;
234 /** by how many CHILD_SA's this policy is used */
238 typedef struct addr_entry_t addr_entry_t
;
241 * IP address in an inface_entry_t
243 struct addr_entry_t
{
245 /** The ip address */
248 /** virtual IP managed by us */
251 /** scope of the address */
254 /** Number of times this IP is used, if virtual */
259 * destroy a addr_entry_t object
261 static void addr_entry_destroy(addr_entry_t
*this)
263 this->ip
->destroy(this->ip
);
267 typedef struct iface_entry_t iface_entry_t
;
270 * A network interface on this system, containing addr_entry_t's
272 struct iface_entry_t
{
274 /** interface index */
277 /** name of the interface */
278 char ifname
[IFNAMSIZ
];
280 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
283 /** list of addresses as host_t */
284 linked_list_t
*addrs
;
288 * destroy an interface entry
290 static void iface_entry_destroy(iface_entry_t
*this)
292 this->addrs
->destroy_function(this->addrs
, (void*)addr_entry_destroy
);
296 typedef struct private_kernel_interface_t private_kernel_interface_t
;
299 * Private variables and functions of kernel_interface class.
301 struct private_kernel_interface_t
{
303 * Public part of the kernel_interface_t object.
305 kernel_interface_t
public;
308 * mutex to lock access to netlink socket
310 pthread_mutex_t nl_mutex
;
313 * mutex to lock access to various lists
315 pthread_mutex_t mutex
;
318 * condition variable to signal virtual IP add/removal
323 * List of installed policies (policy_entry_t)
325 linked_list_t
*policies
;
328 * Cached list of interfaces and its adresses (iface_entry_t)
330 linked_list_t
*ifaces
;
333 * iterator used in hook()
338 * job receiving netlink events
343 * current sequence number for netlink request
348 * Netlink xfrm socket (IPsec)
353 * netlink xfrm socket to receive acquire and expire events
355 int socket_xfrm_events
;
358 * Netlink rt socket (routing)
363 * Netlink rt socket to receive address change events
365 int socket_rt_events
;
368 * time of the last roam_job
370 struct timeval last_roam
;
373 * whether to install routes along policies
378 * routing table to install routes
383 * priority of used routing table
385 int routing_table_prio
;
389 * convert a IKEv2 specific protocol identifier to the kernel one
391 static u_int8_t
proto_ike2kernel(protocol_id_t proto
)
405 * reverse of ike2kernel
407 static protocol_id_t
proto_kernel2ike(u_int8_t proto
)
421 * convert a host_t to a struct xfrm_address
423 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
425 chunk_t chunk
= host
->get_address(host
);
426 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
430 * convert a traffic selector address range to subnet and its mask.
432 static void ts2subnet(traffic_selector_t
* ts
,
433 xfrm_address_t
*net
, u_int8_t
*mask
)
435 /* there is no way to do this cleanly, as the address range may
436 * be anything else but a subnet. We use from_addr as subnet
437 * and try to calculate a usable subnet mask.
442 size_t size
= (ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE
) ?
4 : 16;
444 from
= ts
->get_from_address(ts
);
445 to
= ts
->get_to_address(ts
);
448 /* go trough all bits of the addresses, beginning in the front.
449 * as long as they are equal, the subnet gets larger
451 for (byte
= 0; byte
< size
; byte
++)
453 for (bit
= 7; bit
>= 0; bit
--)
455 if ((1<<bit
& from
.ptr
[byte
]) != (1<<bit
& to
.ptr
[byte
]))
457 *mask
= ((7 - bit
) + (byte
* 8));
467 memcpy(net
, from
.ptr
, from
.len
);
473 * convert a traffic selector port range to port/portmask
475 static void ts2ports(traffic_selector_t
* ts
,
476 u_int16_t
*port
, u_int16_t
*mask
)
478 /* linux does not seem to accept complex portmasks. Only
479 * any or a specific port is allowed. We set to any, if we have
480 * a port range, or to a specific, if we have one port only.
484 from
= ts
->get_from_port(ts
);
485 to
= ts
->get_to_port(ts
);
500 * convert a pair of traffic_selectors to a xfrm_selector
502 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
503 traffic_selector_t
*dst
)
505 struct xfrm_selector sel
;
507 memset(&sel
, 0, sizeof(sel
));
508 sel
.family
= src
->get_type(src
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
509 /* src or dest proto may be "any" (0), use more restrictive one */
510 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
511 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
512 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
513 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
514 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
522 * Creates an rtattr and adds it to the netlink message
524 static void add_attribute(struct nlmsghdr
*hdr
, int rta_type
, chunk_t data
,
529 if (NLMSG_ALIGN(hdr
->nlmsg_len
) + RTA_ALIGN(data
.len
) > buflen
)
531 DBG1(DBG_KNL
, "unable to add attribute, buffer too small");
535 rta
= (struct rtattr
*)(((char*)hdr
) + NLMSG_ALIGN(hdr
->nlmsg_len
));
536 rta
->rta_type
= rta_type
;
537 rta
->rta_len
= RTA_LENGTH(data
.len
);
538 memcpy(RTA_DATA(rta
), data
.ptr
, data
.len
);
539 hdr
->nlmsg_len
= NLMSG_ALIGN(hdr
->nlmsg_len
) + rta
->rta_len
;
543 * process a XFRM_MSG_ACQUIRE from kernel
545 static void process_acquire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
549 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
550 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
552 if (RTA_OK(rtattr
, rtsize
))
554 if (rtattr
->rta_type
== XFRMA_TMPL
)
556 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rtattr
);
562 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
565 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
566 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid %d", reqid
);
567 job
= (job_t
*)acquire_job_create(reqid
);
568 charon
->processor
->queue_job(charon
->processor
, job
);
572 * process a XFRM_MSG_EXPIRE from kernel
574 static void process_expire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
577 protocol_id_t protocol
;
578 u_int32_t spi
, reqid
;
579 struct xfrm_user_expire
*expire
;
581 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
582 protocol
= proto_kernel2ike(expire
->state
.id
.proto
);
583 spi
= expire
->state
.id
.spi
;
584 reqid
= expire
->state
.reqid
;
586 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
588 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
590 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA 0x%x (reqid %d) which is "
591 "not a CHILD_SA", ntohl(spi
), reqid
);
595 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
596 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
597 protocol
, ntohl(spi
), reqid
);
600 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
604 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
606 charon
->processor
->queue_job(charon
->processor
, job
);
610 * start a roaming job. We delay it for a second and fire only one job
611 * for multiple events. Otherwise we would create two many jobs.
613 static void fire_roam_job(private_kernel_interface_t
*this, bool address
)
617 if (gettimeofday(&now
, NULL
) == 0)
619 if (timercmp(&now
, &this->last_roam
, >))
621 now
.tv_usec
+= ROAM_DELAY
* 1000;
622 while (now
.tv_usec
> 1000000)
625 now
.tv_usec
-= 1000000;
627 this->last_roam
= now
;
628 charon
->scheduler
->schedule_job(charon
->scheduler
,
629 (job_t
*)roam_job_create(address
), ROAM_DELAY
);
635 * process RTM_NEWLINK/RTM_DELLINK from kernel
637 static void process_link(private_kernel_interface_t
*this,
638 struct nlmsghdr
*hdr
, bool event
)
640 struct ifinfomsg
* msg
= (struct ifinfomsg
*)(NLMSG_DATA(hdr
));
641 struct rtattr
*rta
= IFLA_RTA(msg
);
642 size_t rtasize
= IFLA_PAYLOAD (hdr
);
643 iterator_t
*iterator
;
644 iface_entry_t
*current
, *entry
= NULL
;
648 while(RTA_OK(rta
, rtasize
))
650 switch (rta
->rta_type
)
653 name
= RTA_DATA(rta
);
656 rta
= RTA_NEXT(rta
, rtasize
);
663 switch (hdr
->nlmsg_type
)
667 if (msg
->ifi_flags
& IFF_LOOPBACK
)
668 { /* ignore loopback interfaces */
671 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
673 while (iterator
->iterate(iterator
, (void**)¤t
))
675 if (current
->ifindex
== msg
->ifi_index
)
683 entry
= malloc_thing(iface_entry_t
);
684 entry
->ifindex
= msg
->ifi_index
;
686 entry
->addrs
= linked_list_create();
687 this->ifaces
->insert_last(this->ifaces
, entry
);
689 memcpy(entry
->ifname
, name
, IFNAMSIZ
);
690 entry
->ifname
[IFNAMSIZ
-1] = '\0';
693 if (!(entry
->flags
& IFF_UP
) && (msg
->ifi_flags
& IFF_UP
))
696 DBG1(DBG_KNL
, "interface %s activated", name
);
698 if ((entry
->flags
& IFF_UP
) && !(msg
->ifi_flags
& IFF_UP
))
701 DBG1(DBG_KNL
, "interface %s deactivated", name
);
704 entry
->flags
= msg
->ifi_flags
;
705 iterator
->destroy(iterator
);
710 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
712 while (iterator
->iterate(iterator
, (void**)¤t
))
714 if (current
->ifindex
== msg
->ifi_index
)
716 /* we do not remove it, as an address may be added to a
717 * "down" interface and we wan't to know that. */
718 current
->flags
= msg
->ifi_flags
;
722 iterator
->destroy(iterator
);
727 /* send an update to all IKE_SAs */
730 fire_roam_job(this, TRUE
);
735 * process RTM_NEWADDR/RTM_DELADDR from kernel
737 static void process_addr(private_kernel_interface_t
*this,
738 struct nlmsghdr
*hdr
, bool event
)
740 struct ifaddrmsg
* msg
= (struct ifaddrmsg
*)(NLMSG_DATA(hdr
));
741 struct rtattr
*rta
= IFA_RTA(msg
);
742 size_t rtasize
= IFA_PAYLOAD (hdr
);
744 iterator_t
*ifaces
, *addrs
;
745 iface_entry_t
*iface
;
747 chunk_t local
= chunk_empty
, address
= chunk_empty
;
748 bool update
= FALSE
, found
= FALSE
, changed
= FALSE
;
750 while(RTA_OK(rta
, rtasize
))
752 switch (rta
->rta_type
)
755 local
.ptr
= RTA_DATA(rta
);
756 local
.len
= RTA_PAYLOAD(rta
);
759 address
.ptr
= RTA_DATA(rta
);
760 address
.len
= RTA_PAYLOAD(rta
);
763 rta
= RTA_NEXT(rta
, rtasize
);
766 /* For PPP interfaces, we need the IFA_LOCAL address,
767 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
768 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
771 host
= host_create_from_chunk(msg
->ifa_family
, local
, 0);
773 else if (address
.ptr
)
775 host
= host_create_from_chunk(msg
->ifa_family
, address
, 0);
783 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
784 while (ifaces
->iterate(ifaces
, (void**)&iface
))
786 if (iface
->ifindex
== msg
->ifa_index
)
788 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
789 while (addrs
->iterate(addrs
, (void**)&addr
))
791 if (host
->ip_equals(host
, addr
->ip
))
794 if (hdr
->nlmsg_type
== RTM_DELADDR
)
797 addrs
->remove(addrs
);
800 DBG1(DBG_KNL
, "%H disappeared from %s",
801 host
, iface
->ifname
);
803 addr_entry_destroy(addr
);
805 else if (hdr
->nlmsg_type
== RTM_NEWADDR
&& addr
->virtual)
811 addrs
->destroy(addrs
);
813 if (hdr
->nlmsg_type
== RTM_NEWADDR
)
819 addr
= malloc_thing(addr_entry_t
);
820 addr
->ip
= host
->clone(host
);
821 addr
->virtual = FALSE
;
823 addr
->scope
= msg
->ifa_scope
;
825 iface
->addrs
->insert_last(iface
->addrs
, addr
);
828 DBG1(DBG_KNL
, "%H appeared on %s", host
, iface
->ifname
);
832 if (found
&& (iface
->flags
& IFF_UP
))
839 ifaces
->destroy(ifaces
);
842 /* send an update to all IKE_SAs */
843 if (update
&& event
&& changed
)
845 fire_roam_job(this, TRUE
);
850 * Receives events from kernel
852 static job_requeue_t
receive_events(private_kernel_interface_t
*this)
855 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
856 struct sockaddr_nl addr
;
857 socklen_t addr_len
= sizeof(addr
);
858 int len
, oldstate
, maxfd
, selected
;
862 FD_SET(this->socket_xfrm_events
, &rfds
);
863 FD_SET(this->socket_rt_events
, &rfds
);
864 maxfd
= max(this->socket_xfrm_events
, this->socket_rt_events
);
866 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
867 selected
= select(maxfd
+ 1, &rfds
, NULL
, NULL
, NULL
);
868 pthread_setcancelstate(oldstate
, NULL
);
871 DBG1(DBG_KNL
, "selecting on sockets failed: %s", strerror(errno
));
872 return JOB_REQUEUE_FAIR
;
874 if (FD_ISSET(this->socket_xfrm_events
, &rfds
))
876 selected
= this->socket_xfrm_events
;
878 else if (FD_ISSET(this->socket_rt_events
, &rfds
))
880 selected
= this->socket_rt_events
;
884 return JOB_REQUEUE_DIRECT
;
887 len
= recvfrom(selected
, response
, sizeof(response
), MSG_DONTWAIT
,
888 (struct sockaddr
*)&addr
, &addr_len
);
894 /* interrupted, try again */
895 return JOB_REQUEUE_DIRECT
;
897 /* no data ready, select again */
898 return JOB_REQUEUE_DIRECT
;
900 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
902 return JOB_REQUEUE_FAIR
;
905 if (addr
.nl_pid
!= 0)
906 { /* not from kernel. not interested, try another one */
907 return JOB_REQUEUE_DIRECT
;
910 while (NLMSG_OK(hdr
, len
))
912 /* looks good so far, dispatch netlink message */
913 if (selected
== this->socket_xfrm_events
)
915 switch (hdr
->nlmsg_type
)
917 case XFRM_MSG_ACQUIRE
:
918 process_acquire(this, hdr
);
920 case XFRM_MSG_EXPIRE
:
921 process_expire(this, hdr
);
927 else if (selected
== this->socket_rt_events
)
929 switch (hdr
->nlmsg_type
)
933 process_addr(this, hdr
, TRUE
);
934 pthread_cond_signal(&this->cond
);
938 process_link(this, hdr
, TRUE
);
939 pthread_cond_signal(&this->cond
);
943 fire_roam_job(this, FALSE
);
949 hdr
= NLMSG_NEXT(hdr
, len
);
951 return JOB_REQUEUE_DIRECT
;
955 * send a netlink message and wait for a reply
957 static status_t
netlink_send(private_kernel_interface_t
*this,
958 int socket
, struct nlmsghdr
*in
,
959 struct nlmsghdr
**out
, size_t *out_len
)
962 struct sockaddr_nl addr
;
963 chunk_t result
= chunk_empty
, tmp
;
964 struct nlmsghdr
*msg
, peek
;
966 pthread_mutex_lock(&this->nl_mutex
);
968 in
->nlmsg_seq
= ++this->seq
;
969 in
->nlmsg_pid
= getpid();
971 memset(&addr
, 0, sizeof(addr
));
972 addr
.nl_family
= AF_NETLINK
;
978 len
= sendto(socket
, in
, in
->nlmsg_len
, 0,
979 (struct sockaddr
*)&addr
, sizeof(addr
));
981 if (len
!= in
->nlmsg_len
)
985 /* interrupted, try again */
988 pthread_mutex_unlock(&this->nl_mutex
);
989 DBG1(DBG_KNL
, "error sending to netlink socket: %s", strerror(errno
));
998 tmp
.len
= sizeof(buf
);
1000 msg
= (struct nlmsghdr
*)tmp
.ptr
;
1002 memset(&addr
, 0, sizeof(addr
));
1003 addr
.nl_family
= AF_NETLINK
;
1004 addr
.nl_pid
= getpid();
1006 addr_len
= sizeof(addr
);
1008 len
= recvfrom(socket
, tmp
.ptr
, tmp
.len
, 0,
1009 (struct sockaddr
*)&addr
, &addr_len
);
1015 DBG1(DBG_KNL
, "got interrupted");
1016 /* interrupted, try again */
1019 DBG1(DBG_KNL
, "error reading from netlink socket: %s", strerror(errno
));
1020 pthread_mutex_unlock(&this->nl_mutex
);
1023 if (!NLMSG_OK(msg
, len
))
1025 DBG1(DBG_KNL
, "received corrupted netlink message");
1026 pthread_mutex_unlock(&this->nl_mutex
);
1029 if (msg
->nlmsg_seq
!= this->seq
)
1031 DBG1(DBG_KNL
, "received invalid netlink sequence number");
1032 if (msg
->nlmsg_seq
< this->seq
)
1036 pthread_mutex_unlock(&this->nl_mutex
);
1041 result
= chunk_cata("cc", result
, tmp
);
1043 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
1044 * numbers to detect multi header messages */
1045 len
= recvfrom(socket
, &peek
, sizeof(peek
), MSG_PEEK
| MSG_DONTWAIT
,
1046 (struct sockaddr
*)&addr
, &addr_len
);
1048 if (len
== sizeof(peek
) && peek
.nlmsg_seq
== this->seq
)
1050 /* seems to be multipart */
1056 *out_len
= result
.len
;
1057 *out
= (struct nlmsghdr
*)clalloc(result
.ptr
, result
.len
);
1059 pthread_mutex_unlock(&this->nl_mutex
);
1065 * send a netlink message and wait for its acknowlegde
1067 static status_t
netlink_send_ack(private_kernel_interface_t
*this,
1068 int socket
, struct nlmsghdr
*in
)
1070 struct nlmsghdr
*out
, *hdr
;
1073 if (netlink_send(this, socket
, in
, &out
, &len
) != SUCCESS
)
1078 while (NLMSG_OK(hdr
, len
))
1080 switch (hdr
->nlmsg_type
)
1084 struct nlmsgerr
* err
= (struct nlmsgerr
*)NLMSG_DATA(hdr
);
1088 DBG1(DBG_KNL
, "received netlink error: %s (%d)",
1089 strerror(-err
->error
), -err
->error
);
1097 hdr
= NLMSG_NEXT(hdr
, len
);
1104 DBG1(DBG_KNL
, "netlink request not acknowlegded");
1110 * Initialize a list of local addresses.
1112 static status_t
init_address_list(private_kernel_interface_t
*this)
1114 char request
[BUFFER_SIZE
];
1115 struct nlmsghdr
*out
, *current
, *in
;
1116 struct rtgenmsg
*msg
;
1118 iterator_t
*ifaces
, *addrs
;
1119 iface_entry_t
*iface
;
1122 DBG1(DBG_KNL
, "listening on interfaces:");
1124 memset(&request
, 0, sizeof(request
));
1126 in
= (struct nlmsghdr
*)&request
;
1127 in
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtgenmsg
));
1128 in
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_MATCH
| NLM_F_ROOT
;
1129 msg
= (struct rtgenmsg
*)NLMSG_DATA(in
);
1130 msg
->rtgen_family
= AF_UNSPEC
;
1133 in
->nlmsg_type
= RTM_GETLINK
;
1134 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1139 while (NLMSG_OK(current
, len
))
1141 switch (current
->nlmsg_type
)
1146 process_link(this, current
, FALSE
);
1149 current
= NLMSG_NEXT(current
, len
);
1156 /* get all interface addresses */
1157 in
->nlmsg_type
= RTM_GETADDR
;
1158 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1163 while (NLMSG_OK(current
, len
))
1165 switch (current
->nlmsg_type
)
1170 process_addr(this, current
, FALSE
);
1173 current
= NLMSG_NEXT(current
, len
);
1180 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1181 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1183 if (iface
->flags
& IFF_UP
)
1185 DBG1(DBG_KNL
, " %s", iface
->ifname
);
1186 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1187 while (addrs
->iterate(addrs
, (void**)&addr
))
1189 DBG1(DBG_KNL
, " %H", addr
->ip
);
1191 addrs
->destroy(addrs
);
1194 ifaces
->destroy(ifaces
);
1199 * iterator hook to iterate over addrs
1201 static hook_result_t
addr_hook(private_kernel_interface_t
*this,
1202 addr_entry_t
*in
, host_t
**out
)
1205 { /* skip virtual interfaces added by us */
1208 if (in
->scope
>= RT_SCOPE_LINK
)
1209 { /* skip addresses with a unusable scope */
1217 * iterator hook to iterate over ifaces
1219 static hook_result_t
iface_hook(private_kernel_interface_t
*this,
1220 iface_entry_t
*in
, host_t
**out
)
1222 if (!(in
->flags
& IFF_UP
))
1223 { /* skip interfaces not up */
1227 if (this->hiter
== NULL
)
1229 this->hiter
= in
->addrs
->create_iterator(in
->addrs
, TRUE
);
1230 this->hiter
->set_iterator_hook(this->hiter
,
1231 (iterator_hook_t
*)addr_hook
, this);
1233 while (this->hiter
->iterate(this->hiter
, (void**)out
))
1237 this->hiter
->destroy(this->hiter
);
1243 * Implements kernel_interface_t.create_address_iterator.
1245 static iterator_t
*create_address_iterator(private_kernel_interface_t
*this)
1247 iterator_t
*iterator
;
1249 /* This iterator is not only hooked, is is double-hooked. As we have stored
1250 * our addresses in iface_entry->addr_entry->ip, we need to iterate the
1251 * entries in each interface we iterate. This does the iface_hook. The
1252 * addr_hook returns the ip instead of the addr_entry. */
1254 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1255 iterator
->set_iterator_hook(iterator
, (iterator_hook_t
*)iface_hook
, this);
1260 * implementation of kernel_interface_t.get_interface_name
1262 static char *get_interface_name(private_kernel_interface_t
*this, host_t
* ip
)
1264 iterator_t
*ifaces
, *addrs
;
1265 iface_entry_t
*iface
;
1269 DBG2(DBG_KNL
, "getting interface name for %H", ip
);
1271 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1272 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1274 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1275 while (addrs
->iterate(addrs
, (void**)&addr
))
1277 if (ip
->ip_equals(ip
, addr
->ip
))
1279 name
= strdup(iface
->ifname
);
1283 addrs
->destroy(addrs
);
1289 ifaces
->destroy(ifaces
);
1293 DBG2(DBG_KNL
, "%H is on interface %s", ip
, name
);
1297 DBG2(DBG_KNL
, "%H is not a local address", ip
);
1303 * Tries to find an ip address of a local interface that is included in the
1304 * supplied traffic selector.
1306 static status_t
get_address_by_ts(private_kernel_interface_t
*this,
1307 traffic_selector_t
*ts
, host_t
**ip
)
1309 iterator_t
*ifaces
, *addrs
;
1310 iface_entry_t
*iface
;
1316 DBG2(DBG_KNL
, "getting a local address in traffic selector %R", ts
);
1318 /* if we have a family which includes localhost, we do not
1319 * search for an IP, we use the default */
1320 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
1322 if (family
== AF_INET
)
1324 host
= host_create_from_string("127.0.0.1", 0);
1328 host
= host_create_from_string("::1", 0);
1331 if (ts
->includes(ts
, host
))
1333 *ip
= host_create_any(family
);
1334 host
->destroy(host
);
1335 DBG2(DBG_KNL
, "using host %H", *ip
);
1338 host
->destroy(host
);
1340 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1341 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1343 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1344 while (addrs
->iterate(addrs
, (void**)&addr
))
1346 if (ts
->includes(ts
, addr
->ip
))
1349 *ip
= addr
->ip
->clone(addr
->ip
);
1353 addrs
->destroy(addrs
);
1359 ifaces
->destroy(ifaces
);
1363 DBG1(DBG_KNL
, "no local address found in traffic selector %R", ts
);
1366 DBG2(DBG_KNL
, "using host %H", *ip
);
1371 * get the interface of a local address
1373 static int get_interface_index(private_kernel_interface_t
*this, host_t
* ip
)
1375 iterator_t
*ifaces
, *addrs
;
1376 iface_entry_t
*iface
;
1380 DBG2(DBG_KNL
, "getting iface for %H", ip
);
1382 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1383 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1385 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1386 while (addrs
->iterate(addrs
, (void**)&addr
))
1388 if (ip
->ip_equals(ip
, addr
->ip
))
1390 ifindex
= iface
->ifindex
;
1394 addrs
->destroy(addrs
);
1400 ifaces
->destroy(ifaces
);
1404 DBG1(DBG_KNL
, "unable to get interface for %H", ip
);
1410 * get the refcount of a virtual ip
1412 static int get_vip_refcount(private_kernel_interface_t
*this, host_t
* ip
)
1414 iterator_t
*ifaces
, *addrs
;
1415 iface_entry_t
*iface
;
1419 ifaces
= this->ifaces
->create_iterator(this->ifaces
, TRUE
);
1420 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1422 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1423 while (addrs
->iterate(addrs
, (void**)&addr
))
1425 if (addr
->virtual && (iface
->flags
& IFF_UP
) &&
1426 ip
->ip_equals(ip
, addr
->ip
))
1428 refcount
= addr
->refcount
;
1432 addrs
->destroy(addrs
);
1438 ifaces
->destroy(ifaces
);
1444 * Manages the creation and deletion of ip addresses on an interface.
1445 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1447 static status_t
manage_ipaddr(private_kernel_interface_t
*this, int nlmsg_type
,
1448 int flags
, int if_index
, host_t
*ip
)
1450 unsigned char request
[BUFFER_SIZE
];
1451 struct nlmsghdr
*hdr
;
1452 struct ifaddrmsg
*msg
;
1455 memset(&request
, 0, sizeof(request
));
1457 chunk
= ip
->get_address(ip
);
1459 hdr
= (struct nlmsghdr
*)request
;
1460 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1461 hdr
->nlmsg_type
= nlmsg_type
;
1462 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1464 msg
= (struct ifaddrmsg
*)NLMSG_DATA(hdr
);
1465 msg
->ifa_family
= ip
->get_family(ip
);
1467 msg
->ifa_prefixlen
= 8 * chunk
.len
;
1468 msg
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1469 msg
->ifa_index
= if_index
;
1471 add_attribute(hdr
, IFA_LOCAL
, chunk
, sizeof(request
));
1473 return netlink_send_ack(this, this->socket_rt
, hdr
);
1477 * Manages source routes in the routing table.
1478 * By setting the appropriate nlmsg_type, the route added or r.
1480 static status_t
manage_srcroute(private_kernel_interface_t
*this, int nlmsg_type
,
1481 int flags
, route_entry_t
*route
)
1483 unsigned char request
[BUFFER_SIZE
];
1484 struct nlmsghdr
*hdr
;
1488 /* if route is 0.0.0.0/0, we can't install it, as it would
1489 * overwrite the default route. Instead, we add two routes:
1490 * 0.0.0.0/1 and 128.0.0.0/1 */
1491 if (this->routing_table
== 0 && route
->prefixlen
== 0)
1496 half
.dst_net
= chunk_alloca(route
->dst_net
.len
);
1497 memset(half
.dst_net
.ptr
, 0, half
.dst_net
.len
);
1498 half
.src_ip
= route
->src_ip
;
1499 half
.gateway
= route
->gateway
;
1500 half
.if_index
= route
->if_index
;
1503 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1504 half
.dst_net
.ptr
[0] |= 0x80;
1505 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1509 memset(&request
, 0, sizeof(request
));
1511 hdr
= (struct nlmsghdr
*)request
;
1512 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1513 hdr
->nlmsg_type
= nlmsg_type
;
1514 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1516 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1517 msg
->rtm_family
= route
->src_ip
->get_family(route
->src_ip
);
1518 msg
->rtm_dst_len
= route
->prefixlen
;
1519 msg
->rtm_table
= this->routing_table
;
1520 msg
->rtm_protocol
= RTPROT_STATIC
;
1521 msg
->rtm_type
= RTN_UNICAST
;
1522 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1524 add_attribute(hdr
, RTA_DST
, route
->dst_net
, sizeof(request
));
1525 chunk
= route
->src_ip
->get_address(route
->src_ip
);
1526 add_attribute(hdr
, RTA_PREFSRC
, chunk
, sizeof(request
));
1527 chunk
= route
->gateway
->get_address(route
->gateway
);
1528 add_attribute(hdr
, RTA_GATEWAY
, chunk
, sizeof(request
));
1529 chunk
.ptr
= (char*)&route
->if_index
;
1530 chunk
.len
= sizeof(route
->if_index
);
1531 add_attribute(hdr
, RTA_OIF
, chunk
, sizeof(request
));
1533 return netlink_send_ack(this, this->socket_rt
, hdr
);
1537 * create or delete an rule to use our routing table
1539 static status_t
manage_rule(private_kernel_interface_t
*this, int nlmsg_type
,
1540 u_int32_t table
, u_int32_t prio
)
1542 unsigned char request
[BUFFER_SIZE
];
1543 struct nlmsghdr
*hdr
;
1547 memset(&request
, 0, sizeof(request
));
1548 hdr
= (struct nlmsghdr
*)request
;
1549 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1550 hdr
->nlmsg_type
= nlmsg_type
;
1551 if (nlmsg_type
== RTM_NEWRULE
)
1553 hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
1555 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1557 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1558 msg
->rtm_table
= table
;
1559 msg
->rtm_family
= AF_INET
;
1560 msg
->rtm_protocol
= RTPROT_BOOT
;
1561 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1562 msg
->rtm_type
= RTN_UNICAST
;
1564 chunk
= chunk_from_thing(prio
);
1565 add_attribute(hdr
, RTA_PRIORITY
, chunk
, sizeof(request
));
1567 return netlink_send_ack(this, this->socket_rt
, hdr
);
1571 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1573 static bool addr_in_subnet(chunk_t addr
, chunk_t net
, int net_len
)
1577 if (addr
.len
!= net
.len
)
1581 /* scan through all bits, beginning in the front */
1582 for (byte
= 0; byte
< addr
.len
; byte
++)
1584 for (bit
= 7; bit
>= 0; bit
--)
1586 /* check if bits are equal (or we reached the end of the net) */
1587 if (bit
+ byte
* 8 > net_len
)
1591 if (((1<<bit
) & addr
.ptr
[byte
]) != ((1<<bit
) & net
.ptr
[byte
]))
1601 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1603 static host_t
*get_route(private_kernel_interface_t
*this, host_t
*dest
,
1606 unsigned char request
[BUFFER_SIZE
];
1607 struct nlmsghdr
*hdr
, *out
, *current
;
1612 host_t
*src
= NULL
, *gtw
= NULL
;
1614 DBG2(DBG_KNL
, "getting address to reach %H", dest
);
1616 memset(&request
, 0, sizeof(request
));
1618 hdr
= (struct nlmsghdr
*)request
;
1619 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
| NLM_F_ROOT
;
1620 hdr
->nlmsg_type
= RTM_GETROUTE
;
1621 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1623 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1624 msg
->rtm_family
= dest
->get_family(dest
);
1626 chunk
= dest
->get_address(dest
);
1627 add_attribute(hdr
, RTA_DST
, chunk
, sizeof(request
));
1629 if (netlink_send(this, this->socket_rt
, hdr
, &out
, &len
) != SUCCESS
)
1631 DBG1(DBG_KNL
, "getting address to %H failed", dest
);
1635 while (NLMSG_OK(current
, len
))
1637 switch (current
->nlmsg_type
)
1645 chunk_t rta_gtw
, rta_src
, rta_dst
;
1646 u_int32_t rta_oif
= 0;
1648 rta_gtw
= rta_src
= rta_dst
= chunk_empty
;
1649 msg
= (struct rtmsg
*)(NLMSG_DATA(current
));
1651 rtasize
= RTM_PAYLOAD(current
);
1652 while (RTA_OK(rta
, rtasize
))
1654 switch (rta
->rta_type
)
1657 rta_src
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1660 rta_gtw
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1663 rta_dst
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1666 if (RTA_PAYLOAD(rta
) == sizeof(rta_oif
))
1668 rta_oif
= *(u_int32_t
*)RTA_DATA(rta
);
1672 rta
= RTA_NEXT(rta
, rtasize
);
1675 /* apply the route if:
1676 * - it is not from our own ipsec routing table
1677 * - is better than a previous one
1678 * - is the default route or
1679 * - its destination net contains our destination
1681 if ((this->routing_table
== 0 ||msg
->rtm_table
!= this->routing_table
)
1682 && msg
->rtm_dst_len
> best
1683 && (msg
->rtm_dst_len
== 0 || /* default route */
1684 (rta_dst
.ptr
&& addr_in_subnet(chunk
, rta_dst
, msg
->rtm_dst_len
))))
1686 iterator_t
*ifaces
, *addrs
;
1687 iface_entry_t
*iface
;
1690 best
= msg
->rtm_dst_len
;
1694 gtw
= host_create_from_chunk(msg
->rtm_family
, rta_gtw
, 0);
1696 else if (rta_src
.ptr
)
1699 src
= host_create_from_chunk(msg
->rtm_family
, rta_src
, 0);
1700 if (get_vip_refcount(this, src
))
1701 { /* skip source address if it is installed by us */
1704 current
= NLMSG_NEXT(current
, len
);
1710 /* no source addr, get one from the interfaces */
1711 ifaces
= this->ifaces
->create_iterator_locked(
1712 this->ifaces
, &this->mutex
);
1713 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1715 if (iface
->ifindex
== rta_oif
)
1717 addrs
= iface
->addrs
->create_iterator(
1718 iface
->addrs
, TRUE
);
1719 while (addrs
->iterate(addrs
, (void**)&addr
))
1721 chunk_t ip
= addr
->ip
->get_address(addr
->ip
);
1722 if (msg
->rtm_dst_len
== 0
1723 || addr_in_subnet(ip
, rta_dst
, msg
->rtm_dst_len
))
1726 src
= addr
->ip
->clone(addr
->ip
);
1730 addrs
->destroy(addrs
);
1733 ifaces
->destroy(ifaces
);
1739 current
= NLMSG_NEXT(current
, len
);
1752 return dest
->clone(dest
);
1758 * Implementation of kernel_interface_t.get_source_addr.
1760 static host_t
* get_source_addr(private_kernel_interface_t
*this, host_t
*dest
)
1762 return get_route(this, dest
, FALSE
);
1766 * Implementation of kernel_interface_t.add_ip.
1768 static status_t
add_ip(private_kernel_interface_t
*this,
1769 host_t
*virtual_ip
, host_t
*iface_ip
)
1771 iface_entry_t
*iface
;
1773 iterator_t
*addrs
, *ifaces
;
1776 DBG2(DBG_KNL
, "adding virtual IP %H", virtual_ip
);
1778 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1779 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1781 bool iface_found
= FALSE
;
1783 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1784 while (addrs
->iterate(addrs
, (void**)&addr
))
1786 if (iface_ip
->ip_equals(iface_ip
, addr
->ip
))
1790 else if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1793 DBG2(DBG_KNL
, "virtual IP %H already installed on %s",
1794 virtual_ip
, iface
->ifname
);
1795 addrs
->destroy(addrs
);
1796 ifaces
->destroy(ifaces
);
1800 addrs
->destroy(addrs
);
1804 ifindex
= iface
->ifindex
;
1805 addr
= malloc_thing(addr_entry_t
);
1806 addr
->ip
= virtual_ip
->clone(virtual_ip
);
1808 addr
->virtual = TRUE
;
1809 addr
->scope
= RT_SCOPE_UNIVERSE
;
1810 iface
->addrs
->insert_last(iface
->addrs
, addr
);
1812 if (manage_ipaddr(this, RTM_NEWADDR
, NLM_F_CREATE
| NLM_F_EXCL
,
1813 ifindex
, virtual_ip
) == SUCCESS
)
1815 while (get_vip_refcount(this, virtual_ip
) == 0)
1816 { /* wait until address appears */
1817 pthread_cond_wait(&this->cond
, &this->mutex
);
1819 ifaces
->destroy(ifaces
);
1822 ifaces
->destroy(ifaces
);
1823 DBG1(DBG_KNL
, "adding virtual IP %H failed", virtual_ip
);
1827 ifaces
->destroy(ifaces
);
1829 DBG1(DBG_KNL
, "interface address %H not found, unable to install"
1830 "virtual IP %H", iface_ip
, virtual_ip
);
1835 * Implementation of kernel_interface_t.del_ip.
1837 static status_t
del_ip(private_kernel_interface_t
*this, host_t
*virtual_ip
)
1839 iface_entry_t
*iface
;
1841 iterator_t
*addrs
, *ifaces
;
1845 DBG2(DBG_KNL
, "deleting virtual IP %H", virtual_ip
);
1847 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1848 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1850 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1851 while (addrs
->iterate(addrs
, (void**)&addr
))
1853 if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1855 ifindex
= iface
->ifindex
;
1856 if (addr
->refcount
== 1)
1858 status
= manage_ipaddr(this, RTM_DELADDR
, 0,
1859 ifindex
, virtual_ip
);
1860 if (status
== SUCCESS
)
1861 { /* wait until the address is really gone */
1862 while (get_vip_refcount(this, virtual_ip
) > 0)
1864 pthread_cond_wait(&this->cond
, &this->mutex
);
1867 addrs
->destroy(addrs
);
1868 ifaces
->destroy(ifaces
);
1875 DBG2(DBG_KNL
, "virtual IP %H used by other SAs, not deleting",
1877 addrs
->destroy(addrs
);
1878 ifaces
->destroy(ifaces
);
1882 addrs
->destroy(addrs
);
1884 ifaces
->destroy(ifaces
);
1886 DBG2(DBG_KNL
, "virtual IP %H not cached, unable to delete", virtual_ip
);
1891 * Get an SPI for a specific protocol from the kernel.
1893 static status_t
get_spi_internal(private_kernel_interface_t
*this,
1894 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
1895 u_int32_t reqid
, u_int32_t
*spi
)
1897 unsigned char request
[BUFFER_SIZE
];
1898 struct nlmsghdr
*hdr
, *out
;
1899 struct xfrm_userspi_info
*userspi
;
1900 u_int32_t received_spi
= 0;
1903 memset(&request
, 0, sizeof(request
));
1905 hdr
= (struct nlmsghdr
*)request
;
1906 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1907 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1908 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1910 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1911 host2xfrm(src
, &userspi
->info
.saddr
);
1912 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1913 userspi
->info
.id
.proto
= proto
;
1914 userspi
->info
.mode
= TRUE
; /* tunnel mode */
1915 userspi
->info
.reqid
= reqid
;
1916 userspi
->info
.family
= src
->get_family(src
);
1920 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1923 while (NLMSG_OK(hdr
, len
))
1925 switch (hdr
->nlmsg_type
)
1927 case XFRM_MSG_NEWSA
:
1929 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1930 received_spi
= usersa
->id
.spi
;
1935 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1937 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1938 strerror(-err
->error
), -err
->error
);
1942 hdr
= NLMSG_NEXT(hdr
, len
);
1952 if (received_spi
== 0)
1957 *spi
= received_spi
;
1962 * Implementation of kernel_interface_t.get_spi.
1964 static status_t
get_spi(private_kernel_interface_t
*this,
1965 host_t
*src
, host_t
*dst
,
1966 protocol_id_t protocol
, u_int32_t reqid
,
1969 DBG2(DBG_KNL
, "getting SPI for reqid %d", reqid
);
1971 if (get_spi_internal(this, src
, dst
, proto_ike2kernel(protocol
),
1972 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
1974 DBG1(DBG_KNL
, "unable to get SPI for reqid %d", reqid
);
1978 DBG2(DBG_KNL
, "got SPI 0x%x for reqid %d", *spi
, reqid
);
1984 * Implementation of kernel_interface_t.get_cpi.
1986 static status_t
get_cpi(private_kernel_interface_t
*this,
1987 host_t
*src
, host_t
*dst
,
1988 u_int32_t reqid
, u_int16_t
*cpi
)
1990 u_int32_t received_spi
= 0;
1991 DBG2(DBG_KNL
, "getting CPI for reqid %d", reqid
);
1993 if (get_spi_internal(this, src
, dst
,
1994 IPPROTO_COMP
, 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
1996 DBG1(DBG_KNL
, "unable to get CPI for reqid %d", reqid
);
2000 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
2002 DBG2(DBG_KNL
, "got CPI 0x%x for reqid %d", *cpi
, reqid
);
2008 * Implementation of kernel_interface_t.add_sa.
2010 static status_t
add_sa(private_kernel_interface_t
*this,
2011 host_t
*src
, host_t
*dst
, u_int32_t spi
,
2012 protocol_id_t protocol
, u_int32_t reqid
,
2013 u_int64_t expire_soft
, u_int64_t expire_hard
,
2014 u_int16_t enc_alg
, u_int16_t enc_size
,
2015 u_int16_t int_alg
, u_int16_t int_size
,
2016 prf_plus_t
*prf_plus
, mode_t mode
,
2017 u_int16_t ipcomp
, bool encap
,
2020 unsigned char request
[BUFFER_SIZE
];
2022 /* additional 4 octets KEYMAT required for AES-GCM as of RFC4106 8.1. */
2023 u_int16_t add_keymat
= 32;
2024 struct nlmsghdr
*hdr
;
2025 struct xfrm_usersa_info
*sa
;
2027 memset(&request
, 0, sizeof(request
));
2029 DBG2(DBG_KNL
, "adding SAD entry with SPI 0x%x and reqid %d", spi
, reqid
);
2031 hdr
= (struct nlmsghdr
*)request
;
2032 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2033 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
2034 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2036 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
2037 host2xfrm(src
, &sa
->saddr
);
2038 host2xfrm(dst
, &sa
->id
.daddr
);
2040 sa
->id
.proto
= proto_ike2kernel(protocol
);
2041 sa
->family
= src
->get_family(src
);
2043 sa
->replay_window
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
2045 /* we currently do not expire SAs by volume/packet count */
2046 sa
->lft
.soft_byte_limit
= XFRM_INF
;
2047 sa
->lft
.hard_byte_limit
= XFRM_INF
;
2048 sa
->lft
.soft_packet_limit
= XFRM_INF
;
2049 sa
->lft
.hard_packet_limit
= XFRM_INF
;
2050 /* we use lifetimes since added, not since used */
2051 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
2052 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
2053 sa
->lft
.soft_use_expires_seconds
= 0;
2054 sa
->lft
.hard_use_expires_seconds
= 0;
2056 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
2060 case ENCR_UNDEFINED
:
2063 case ENCR_AES_CCM_ICV8
:
2064 case ENCR_AES_CCM_ICV12
:
2065 case ENCR_AES_CCM_ICV16
:
2066 /* AES-CCM needs only 3 additional octets KEYMAT as of RFC 4309 7.1. */
2069 case ENCR_AES_GCM_ICV8
:
2070 case ENCR_AES_GCM_ICV12
:
2071 case ENCR_AES_GCM_ICV16
:
2073 u_int16_t icv_size
= 0;
2074 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
2075 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &icv_size
);
2076 if (alg_name
== NULL
)
2078 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2079 encryption_algorithm_names
, enc_alg
);
2082 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
2083 encryption_algorithm_names
, enc_alg
, enc_size
);
2085 /* additional KEYMAT required */
2086 enc_size
+= add_keymat
;
2088 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) + enc_size
/ 8);
2089 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2090 if (hdr
->nlmsg_len
> sizeof(request
))
2095 struct xfrm_algo_aead
* algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
2096 algo
->alg_key_len
= enc_size
;
2097 algo
->alg_icv_len
= icv_size
;
2098 strcpy(algo
->alg_name
, alg_name
);
2099 prf_plus
->get_bytes(prf_plus
, enc_size
/ 8, algo
->alg_key
);
2101 rthdr
= XFRM_RTA_NEXT(rthdr
);
2106 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
2107 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &enc_size
);
2108 if (alg_name
== NULL
)
2110 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2111 encryption_algorithm_names
, enc_alg
);
2114 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
2115 encryption_algorithm_names
, enc_alg
, enc_size
);
2117 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_size
/ 8);
2118 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2119 if (hdr
->nlmsg_len
> sizeof(request
))
2124 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
2125 algo
->alg_key_len
= enc_size
;
2126 strcpy(algo
->alg_name
, alg_name
);
2127 prf_plus
->get_bytes(prf_plus
, enc_size
/ 8, algo
->alg_key
);
2129 rthdr
= XFRM_RTA_NEXT(rthdr
);
2134 if (int_alg
!= AUTH_UNDEFINED
)
2136 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
2137 alg_name
= lookup_algorithm(integrity_algs
, int_alg
, &int_size
);
2138 if (alg_name
== NULL
)
2140 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2141 integrity_algorithm_names
, int_alg
);
2144 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
2145 integrity_algorithm_names
, int_alg
, int_size
);
2147 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_size
/ 8);
2148 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2149 if (hdr
->nlmsg_len
> sizeof(request
))
2154 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
2155 algo
->alg_key_len
= int_size
;
2156 strcpy(algo
->alg_name
, alg_name
);
2157 prf_plus
->get_bytes(prf_plus
, int_size
/ 8, algo
->alg_key
);
2159 rthdr
= XFRM_RTA_NEXT(rthdr
);
2162 if (ipcomp
!= IPCOMP_NONE
)
2164 rthdr
->rta_type
= XFRMA_ALG_COMP
;
2165 alg_name
= lookup_algorithm(compression_algs
, ipcomp
, NULL
);
2166 if (alg_name
== NULL
)
2168 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2169 ipcomp_transform_names
, ipcomp
);
2172 DBG2(DBG_KNL
, " using compression algorithm %N",
2173 ipcomp_transform_names
, ipcomp
);
2175 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
2176 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2177 if (hdr
->nlmsg_len
> sizeof(request
))
2182 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
2183 algo
->alg_key_len
= 0;
2184 strcpy(algo
->alg_name
, alg_name
);
2186 rthdr
= XFRM_RTA_NEXT(rthdr
);
2191 rthdr
->rta_type
= XFRMA_ENCAP
;
2192 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
2194 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2195 if (hdr
->nlmsg_len
> sizeof(request
))
2200 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
2201 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2202 tmpl
->encap_sport
= htons(src
->get_port(src
));
2203 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
2204 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2205 /* encap_oa could probably be derived from the
2206 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
2207 * pluto does the same as we do here but it uses encap_oa in the
2208 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
2209 * the kernel ignores it anyway
2210 * -> does that mean that NAT-T encap doesn't work in transport mode?
2211 * No. The reason the kernel ignores NAT-OA is that it recomputes
2212 * (or, rather, just ignores) the checksum. If packets pass
2213 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
2214 rthdr
= XFRM_RTA_NEXT(rthdr
);
2217 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2219 DBG1(DBG_KNL
, "unable to add SAD entry with SPI 0x%x", spi
);
2226 * Get the replay state (i.e. sequence numbers) of an SA.
2228 static status_t
get_replay_state(private_kernel_interface_t
*this,
2229 u_int32_t spi
, protocol_id_t protocol
, host_t
*dst
,
2230 struct xfrm_replay_state
*replay
)
2232 unsigned char request
[BUFFER_SIZE
];
2233 struct nlmsghdr
*hdr
, *out
= NULL
;
2234 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
2239 memset(&request
, 0, sizeof(request
));
2241 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI 0x%x", spi
);
2243 hdr
= (struct nlmsghdr
*)request
;
2244 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2245 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
2246 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
2248 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
2249 aevent_id
->flags
= XFRM_AE_RVAL
;
2251 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
2252 aevent_id
->sa_id
.spi
= spi
;
2253 aevent_id
->sa_id
.proto
= proto_ike2kernel(protocol
);
2254 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
2256 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2259 while (NLMSG_OK(hdr
, len
))
2261 switch (hdr
->nlmsg_type
)
2263 case XFRM_MSG_NEWAE
:
2265 out_aevent
= NLMSG_DATA(hdr
);
2270 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2271 DBG1(DBG_KNL
, "querying replay state from SAD entry failed: %s (%d)",
2272 strerror(-err
->error
), -err
->error
);
2276 hdr
= NLMSG_NEXT(hdr
, len
);
2285 if (out_aevent
== NULL
)
2287 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI 0x%x", spi
);
2292 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
2293 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
2294 while(RTA_OK(rta
, rtasize
))
2296 if (rta
->rta_type
== XFRMA_REPLAY_VAL
)
2298 memcpy(replay
, RTA_DATA(rta
), rta
->rta_len
);
2302 rta
= RTA_NEXT(rta
, rtasize
);
2305 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI 0x%x", spi
);
2311 * Implementation of kernel_interface_t.update_sa.
2313 static status_t
update_sa(private_kernel_interface_t
*this,
2314 u_int32_t spi
, protocol_id_t protocol
,
2315 host_t
*src
, host_t
*dst
,
2316 host_t
*new_src
, host_t
*new_dst
, bool encap
)
2318 unsigned char request
[BUFFER_SIZE
], *pos
;
2319 struct nlmsghdr
*hdr
, *out
= NULL
;
2320 struct xfrm_usersa_id
*sa_id
;
2321 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
2325 struct xfrm_encap_tmpl
* tmpl
= NULL
;
2326 bool got_replay_state
;
2327 struct xfrm_replay_state replay
;
2329 memset(&request
, 0, sizeof(request
));
2331 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x for update", spi
);
2333 /* query the exisiting SA first */
2334 hdr
= (struct nlmsghdr
*)request
;
2335 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2336 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2337 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2339 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2340 host2xfrm(dst
, &sa_id
->daddr
);
2342 sa_id
->proto
= proto_ike2kernel(protocol
);
2343 sa_id
->family
= dst
->get_family(dst
);
2345 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2348 while (NLMSG_OK(hdr
, len
))
2350 switch (hdr
->nlmsg_type
)
2352 case XFRM_MSG_NEWSA
:
2354 out_sa
= NLMSG_DATA(hdr
);
2359 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2360 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2361 strerror(-err
->error
), -err
->error
);
2365 hdr
= NLMSG_NEXT(hdr
, len
);
2375 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
2380 /* try to get the replay state */
2381 got_replay_state
= (get_replay_state(
2382 this, spi
, protocol
, dst
, &replay
) == SUCCESS
);
2384 /* delete the old SA */
2385 if (this->public.del_sa(&this->public, dst
, spi
, protocol
) != SUCCESS
)
2387 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI 0x%x", spi
);
2392 DBG2(DBG_KNL
, "updating SAD entry with SPI 0x%x from %#H..%#H to %#H..%#H",
2393 spi
, src
, dst
, new_src
, new_dst
);
2395 /* copy over the SA from out to request */
2396 hdr
= (struct nlmsghdr
*)request
;
2397 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
2398 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2399 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2400 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2401 sa
= NLMSG_DATA(hdr
);
2402 sa
->family
= new_dst
->get_family(new_dst
);
2404 if (!src
->ip_equals(src
, new_src
))
2406 host2xfrm(new_src
, &sa
->saddr
);
2408 if (!dst
->ip_equals(dst
, new_dst
))
2410 host2xfrm(new_dst
, &sa
->id
.daddr
);
2413 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
2414 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
2415 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
2416 while(RTA_OK(rta
, rtasize
))
2418 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2419 if (rta
->rta_type
!= XFRMA_ENCAP
|| encap
)
2421 if (rta
->rta_type
== XFRMA_ENCAP
)
2422 { /* update encap tmpl */
2423 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
2424 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
2425 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
2427 memcpy(pos
, rta
, rta
->rta_len
);
2428 pos
+= RTA_ALIGN(rta
->rta_len
);
2429 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
2431 rta
= RTA_NEXT(rta
, rtasize
);
2434 rta
= (struct rtattr
*)pos
;
2435 if (tmpl
== NULL
&& encap
)
2436 { /* add tmpl if we are enabling it */
2437 rta
->rta_type
= XFRMA_ENCAP
;
2438 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
2440 hdr
->nlmsg_len
+= rta
->rta_len
;
2441 if (hdr
->nlmsg_len
> sizeof(request
))
2446 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
2447 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2448 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
2449 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
2450 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2452 rta
= XFRM_RTA_NEXT(rta
);
2455 if (got_replay_state
)
2456 { /* copy the replay data if available */
2457 rta
->rta_type
= XFRMA_REPLAY_VAL
;
2458 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
2460 hdr
->nlmsg_len
+= rta
->rta_len
;
2461 if (hdr
->nlmsg_len
> sizeof(request
))
2465 memcpy(RTA_DATA(rta
), &replay
, sizeof(replay
));
2467 rta
= XFRM_RTA_NEXT(rta
);
2470 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2472 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
2482 * Implementation of kernel_interface_t.query_sa.
2484 static status_t
query_sa(private_kernel_interface_t
*this, host_t
*dst
,
2485 u_int32_t spi
, protocol_id_t protocol
,
2486 u_int32_t
*use_time
)
2488 unsigned char request
[BUFFER_SIZE
];
2489 struct nlmsghdr
*out
= NULL
, *hdr
;
2490 struct xfrm_usersa_id
*sa_id
;
2491 struct xfrm_usersa_info
*sa
= NULL
;
2494 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x", spi
);
2495 memset(&request
, 0, sizeof(request
));
2497 hdr
= (struct nlmsghdr
*)request
;
2498 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2499 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2500 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2502 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2503 host2xfrm(dst
, &sa_id
->daddr
);
2505 sa_id
->proto
= proto_ike2kernel(protocol
);
2506 sa_id
->family
= dst
->get_family(dst
);
2508 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2511 while (NLMSG_OK(hdr
, len
))
2513 switch (hdr
->nlmsg_type
)
2515 case XFRM_MSG_NEWSA
:
2517 sa
= NLMSG_DATA(hdr
);
2522 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2523 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2524 strerror(-err
->error
), -err
->error
);
2528 hdr
= NLMSG_NEXT(hdr
, len
);
2539 DBG1(DBG_KNL
, "unable to query SAD entry with SPI 0x%x", spi
);
2544 *use_time
= sa
->curlft
.use_time
;
2550 * Implementation of kernel_interface_t.del_sa.
2552 static status_t
del_sa(private_kernel_interface_t
*this, host_t
*dst
,
2553 u_int32_t spi
, protocol_id_t protocol
)
2555 unsigned char request
[BUFFER_SIZE
];
2556 struct nlmsghdr
*hdr
;
2557 struct xfrm_usersa_id
*sa_id
;
2559 memset(&request
, 0, sizeof(request
));
2561 DBG2(DBG_KNL
, "deleting SAD entry with SPI 0x%x", spi
);
2563 hdr
= (struct nlmsghdr
*)request
;
2564 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2565 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
2566 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2568 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2569 host2xfrm(dst
, &sa_id
->daddr
);
2571 sa_id
->proto
= proto_ike2kernel(protocol
);
2572 sa_id
->family
= dst
->get_family(dst
);
2574 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2576 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI 0x%x", spi
);
2579 DBG2(DBG_KNL
, "deleted SAD entry with SPI 0x%x", spi
);
2584 * Implementation of kernel_interface_t.add_policy.
2586 static status_t
add_policy(private_kernel_interface_t
*this,
2587 host_t
*src
, host_t
*dst
,
2588 traffic_selector_t
*src_ts
,
2589 traffic_selector_t
*dst_ts
,
2590 policy_dir_t direction
, protocol_id_t protocol
,
2591 u_int32_t reqid
, bool high_prio
, mode_t mode
,
2594 iterator_t
*iterator
;
2595 policy_entry_t
*current
, *policy
;
2597 unsigned char request
[BUFFER_SIZE
];
2598 struct xfrm_userpolicy_info
*policy_info
;
2599 struct nlmsghdr
*hdr
;
2601 /* create a policy */
2602 policy
= malloc_thing(policy_entry_t
);
2603 memset(policy
, 0, sizeof(policy_entry_t
));
2604 policy
->sel
= ts2selector(src_ts
, dst_ts
);
2605 policy
->direction
= direction
;
2607 /* find the policy, which matches EXACTLY */
2608 pthread_mutex_lock(&this->mutex
);
2609 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
2610 while (iterator
->iterate(iterator
, (void**)¤t
))
2612 if (memcmp(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) == 0 &&
2613 policy
->direction
== current
->direction
)
2615 /* use existing policy */
2616 current
->refcount
++;
2617 DBG2(DBG_KNL
, "policy %R===%R already exists, increasing "
2618 "refcount", src_ts
, dst_ts
);
2625 iterator
->destroy(iterator
);
2627 { /* apply the new one, if we have no such policy */
2628 this->policies
->insert_last(this->policies
, policy
);
2629 policy
->refcount
= 1;
2632 DBG2(DBG_KNL
, "adding policy %R===%R", src_ts
, dst_ts
);
2634 memset(&request
, 0, sizeof(request
));
2635 hdr
= (struct nlmsghdr
*)request
;
2636 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2637 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
2638 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2640 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2641 policy_info
->sel
= policy
->sel
;
2642 policy_info
->dir
= policy
->direction
;
2643 /* calculate priority based on source selector size, small size = high prio */
2644 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
2645 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
2646 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
2647 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
2648 policy_info
->action
= XFRM_POLICY_ALLOW
;
2649 policy_info
->share
= XFRM_SHARE_ANY
;
2650 pthread_mutex_unlock(&this->mutex
);
2652 /* policies don't expire */
2653 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2654 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2655 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2656 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2657 policy_info
->lft
.soft_add_expires_seconds
= 0;
2658 policy_info
->lft
.hard_add_expires_seconds
= 0;
2659 policy_info
->lft
.soft_use_expires_seconds
= 0;
2660 policy_info
->lft
.hard_use_expires_seconds
= 0;
2662 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2663 rthdr
->rta_type
= XFRMA_TMPL
;
2664 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2666 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2667 if (hdr
->nlmsg_len
> sizeof(request
))
2672 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2674 if (ipcomp
!= IPCOMP_NONE
)
2676 tmpl
->reqid
= reqid
;
2677 tmpl
->id
.proto
= IPPROTO_COMP
;
2678 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2680 tmpl
->optional
= direction
!= POLICY_OUT
;
2681 tmpl
->family
= src
->get_family(src
);
2683 host2xfrm(src
, &tmpl
->saddr
);
2684 host2xfrm(dst
, &tmpl
->id
.daddr
);
2686 /* add an additional xfrm_user_tmpl */
2687 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2688 hdr
->nlmsg_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2689 if (hdr
->nlmsg_len
> sizeof(request
))
2697 tmpl
->reqid
= reqid
;
2698 tmpl
->id
.proto
= proto_ike2kernel(protocol
);
2699 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2701 tmpl
->family
= src
->get_family(src
);
2703 host2xfrm(src
, &tmpl
->saddr
);
2704 host2xfrm(dst
, &tmpl
->id
.daddr
);
2706 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2708 DBG1(DBG_KNL
, "unable to add policy %R===%R", src_ts
, dst_ts
);
2712 /* install a route, if:
2713 * - we are NOT updating a policy
2714 * - this is a forward policy (to just get one for each child)
2715 * - we are in tunnel mode
2716 * - we are not using IPv6 (does not work correctly yet!)
2717 * - routing is not disabled via strongswan.conf
2719 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
2720 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
2721 this->install_routes
)
2723 policy
->route
= malloc_thing(route_entry_t
);
2724 if (get_address_by_ts(this, dst_ts
, &policy
->route
->src_ip
) == SUCCESS
)
2726 /* get the nexthop to src (src as we are in POLICY_FWD).*/
2727 policy
->route
->gateway
= get_route(this, src
, TRUE
);
2728 policy
->route
->if_index
= get_interface_index(this, dst
);
2729 policy
->route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2730 memcpy(policy
->route
->dst_net
.ptr
, &policy
->sel
.saddr
, policy
->route
->dst_net
.len
);
2731 policy
->route
->prefixlen
= policy
->sel
.prefixlen_s
;
2733 if (manage_srcroute(this, RTM_NEWROUTE
, NLM_F_CREATE
| NLM_F_EXCL
,
2734 policy
->route
) != SUCCESS
)
2736 DBG1(DBG_KNL
, "unable to install source route for %H",
2737 policy
->route
->src_ip
);
2738 route_entry_destroy(policy
->route
);
2739 policy
->route
= NULL
;
2744 free(policy
->route
);
2745 policy
->route
= NULL
;
2753 * Implementation of kernel_interface_t.query_policy.
2755 static status_t
query_policy(private_kernel_interface_t
*this,
2756 traffic_selector_t
*src_ts
,
2757 traffic_selector_t
*dst_ts
,
2758 policy_dir_t direction
, u_int32_t
*use_time
)
2760 unsigned char request
[BUFFER_SIZE
];
2761 struct nlmsghdr
*out
= NULL
, *hdr
;
2762 struct xfrm_userpolicy_id
*policy_id
;
2763 struct xfrm_userpolicy_info
*policy
= NULL
;
2766 memset(&request
, 0, sizeof(request
));
2768 DBG2(DBG_KNL
, "querying policy %R===%R", src_ts
, dst_ts
);
2770 hdr
= (struct nlmsghdr
*)request
;
2771 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2772 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2773 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2775 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2776 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2777 policy_id
->dir
= direction
;
2779 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2782 while (NLMSG_OK(hdr
, len
))
2784 switch (hdr
->nlmsg_type
)
2786 case XFRM_MSG_NEWPOLICY
:
2788 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2793 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2794 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2795 strerror(-err
->error
), -err
->error
);
2799 hdr
= NLMSG_NEXT(hdr
, len
);
2810 DBG2(DBG_KNL
, "unable to query policy %R===%R", src_ts
, dst_ts
);
2814 *use_time
= (time_t)policy
->curlft
.use_time
;
2821 * Implementation of kernel_interface_t.del_policy.
2823 static status_t
del_policy(private_kernel_interface_t
*this,
2824 traffic_selector_t
*src_ts
,
2825 traffic_selector_t
*dst_ts
,
2826 policy_dir_t direction
)
2828 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
2829 route_entry_t
*route
;
2830 unsigned char request
[BUFFER_SIZE
];
2831 struct nlmsghdr
*hdr
;
2832 struct xfrm_userpolicy_id
*policy_id
;
2833 iterator_t
*iterator
;
2835 DBG2(DBG_KNL
, "deleting policy %R===%R", src_ts
, dst_ts
);
2837 /* create a policy */
2838 memset(&policy
, 0, sizeof(policy_entry_t
));
2839 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2840 policy
.direction
= direction
;
2842 /* find the policy */
2843 iterator
= this->policies
->create_iterator_locked(this->policies
, &this->mutex
);
2844 while (iterator
->iterate(iterator
, (void**)¤t
))
2846 if (memcmp(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) == 0 &&
2847 policy
.direction
== current
->direction
)
2849 to_delete
= current
;
2850 if (--to_delete
->refcount
> 0)
2852 /* is used by more SAs, keep in kernel */
2853 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2854 iterator
->destroy(iterator
);
2857 /* remove if last reference */
2858 iterator
->remove(iterator
);
2862 iterator
->destroy(iterator
);
2865 DBG1(DBG_KNL
, "deleting policy %R===%R failed, not found", src_ts
, dst_ts
);
2869 memset(&request
, 0, sizeof(request
));
2871 hdr
= (struct nlmsghdr
*)request
;
2872 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2873 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2874 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2876 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2877 policy_id
->sel
= to_delete
->sel
;
2878 policy_id
->dir
= direction
;
2880 route
= to_delete
->route
;
2883 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2885 DBG1(DBG_KNL
, "unable to delete policy %R===%R", src_ts
, dst_ts
);
2891 if (manage_srcroute(this, RTM_DELROUTE
, 0, route
) != SUCCESS
)
2893 DBG1(DBG_KNL
, "error uninstalling route installed with "
2894 "policy %R===%R", src_ts
, dst_ts
);
2896 route_entry_destroy(route
);
2902 * Implementation of kernel_interface_t.destroy.
2904 static void destroy(private_kernel_interface_t
*this)
2906 if (this->routing_table
)
2908 manage_rule(this, RTM_DELRULE
, this->routing_table
,
2909 this->routing_table_prio
);
2912 this->job
->cancel(this->job
);
2913 close(this->socket_xfrm_events
);
2914 close(this->socket_xfrm
);
2915 close(this->socket_rt_events
);
2916 close(this->socket_rt
);
2917 this->policies
->destroy(this->policies
);
2918 this->ifaces
->destroy_function(this->ifaces
, (void*)iface_entry_destroy
);
2923 * Described in header.
2925 kernel_interface_t
*kernel_interface_create()
2927 private_kernel_interface_t
*this = malloc_thing(private_kernel_interface_t
);
2928 struct sockaddr_nl addr
;
2930 /* public functions */
2931 this->public.get_spi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
2932 this->public.get_cpi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
2933 this->public.add_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
,u_int64_t
,u_int64_t
,u_int16_t
,u_int16_t
,u_int16_t
,u_int16_t
,prf_plus_t
*,mode_t
,u_int16_t
,bool,bool))add_sa
;
2934 this->public.update_sa
= (status_t(*)(kernel_interface_t
*,u_int32_t
,protocol_id_t
,host_t
*,host_t
*,host_t
*,host_t
*,bool))update_sa
;
2935 this->public.query_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
*))query_sa
;
2936 this->public.del_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
2937 this->public.add_policy
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,protocol_id_t
,u_int32_t
,bool,mode_t
,u_int16_t
))add_policy
;
2938 this->public.query_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
2939 this->public.del_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
2940 this->public.get_interface
= (char*(*)(kernel_interface_t
*,host_t
*))get_interface_name
;
2941 this->public.create_address_iterator
= (iterator_t
*(*)(kernel_interface_t
*))create_address_iterator
;
2942 this->public.get_source_addr
= (host_t
*(*)(kernel_interface_t
*, host_t
*dest
))get_source_addr
;
2943 this->public.add_ip
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*)) add_ip
;
2944 this->public.del_ip
= (status_t(*)(kernel_interface_t
*,host_t
*)) del_ip
;
2945 this->public.destroy
= (void(*)(kernel_interface_t
*)) destroy
;
2947 /* private members */
2948 this->policies
= linked_list_create();
2949 this->ifaces
= linked_list_create();
2952 pthread_mutex_init(&this->mutex
, NULL
);
2953 pthread_mutex_init(&this->nl_mutex
, NULL
);
2954 pthread_cond_init(&this->cond
, NULL
);
2955 timerclear(&this->last_roam
);
2956 this->install_routes
= lib
->settings
->get_bool(lib
->settings
,
2957 "charon.install_routes", TRUE
);
2958 this->routing_table
= lib
->settings
->get_int(lib
->settings
,
2959 "charon.routing_table", IPSEC_ROUTING_TABLE
);
2960 this->routing_table_prio
= lib
->settings
->get_int(lib
->settings
,
2961 "charon.routing_table_prio", IPSEC_ROUTING_TABLE_PRIO
);
2962 memset(&addr
, 0, sizeof(addr
));
2963 addr
.nl_family
= AF_NETLINK
;
2965 /* create and bind RT socket */
2966 this->socket_rt
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2967 if (this->socket_rt
<= 0)
2969 charon
->kill(charon
, "unable to create RT netlink socket");
2972 if (bind(this->socket_rt
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2974 charon
->kill(charon
, "unable to bind RT netlink socket");
2977 /* create and bind RT socket for events (address/interface/route changes) */
2978 this->socket_rt_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2979 if (this->socket_rt_events
<= 0)
2981 charon
->kill(charon
, "unable to create RT event socket");
2983 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR
|
2984 RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_ROUTE
| RTMGRP_LINK
;
2985 if (bind(this->socket_rt_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2987 charon
->kill(charon
, "unable to bind RT event socket");
2990 /* create and bind XFRM socket */
2991 this->socket_xfrm
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2992 if (this->socket_xfrm
<= 0)
2994 charon
->kill(charon
, "unable to create XFRM netlink socket");
2997 if (bind(this->socket_xfrm
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2999 charon
->kill(charon
, "unable to bind XFRM netlink socket");
3002 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
3003 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
3004 if (this->socket_xfrm_events
<= 0)
3006 charon
->kill(charon
, "unable to create XFRM event socket");
3008 addr
.nl_groups
= XFRMGRP_ACQUIRE
| XFRMGRP_EXPIRE
;
3009 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
3011 charon
->kill(charon
, "unable to bind XFRM event socket");
3014 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
3016 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
3018 if (init_address_list(this) != SUCCESS
)
3020 charon
->kill(charon
, "unable to get interface list");
3023 if (this->routing_table
)
3025 if (manage_rule(this, RTM_NEWRULE
, this->routing_table
,
3026 this->routing_table_prio
) != SUCCESS
)
3028 DBG1(DBG_KNL
, "unable to create routing table rule");
3032 return &this->public;