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 /** required for Linux 2.6.26 kernel and later */
52 #ifndef XFRM_STATE_AF_UNSPEC
53 #define XFRM_STATE_AF_UNSPEC 32
56 /** routing table for routes installed by us */
57 #ifndef IPSEC_ROUTING_TABLE
58 #define IPSEC_ROUTING_TABLE 100
60 #ifndef IPSEC_ROUTING_TABLE_PRIO
61 #define IPSEC_ROUTING_TABLE_PRIO 100
64 /** default priority of installed policies */
66 #define PRIO_HIGH 2000
68 /** delay before firing roam jobs (ms) */
69 #define ROAM_DELAY 100
71 #define BUFFER_SIZE 1024
74 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
75 * 'usual' netlink data x like 'struct xfrm_usersa_info'
77 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
79 * returns a pointer to the next rtattr following rta.
80 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
82 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
84 * returns the total size of attached rta data
85 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
87 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
89 typedef struct kernel_algorithm_t kernel_algorithm_t
;
92 * Mapping from the algorithms defined in IKEv2 to
93 * kernel level algorithm names and their key length
95 struct kernel_algorithm_t
{
97 * Identifier specified in IKEv2
102 * Name of the algorithm, as used as kernel identifier
107 * Key length in bits, if fixed size
111 #define END_OF_LIST -1
114 * Algorithms for encryption
116 static kernel_algorithm_t encryption_algs
[] = {
117 /* {ENCR_DES_IV64, "***", 0}, */
118 {ENCR_DES
, "des", 64},
119 {ENCR_3DES
, "des3_ede", 192},
120 /* {ENCR_RC5, "***", 0}, */
121 /* {ENCR_IDEA, "***", 0}, */
122 {ENCR_CAST
, "cast128", 0},
123 {ENCR_BLOWFISH
, "blowfish", 0},
124 /* {ENCR_3IDEA, "***", 0}, */
125 /* {ENCR_DES_IV32, "***", 0}, */
126 {ENCR_NULL
, "cipher_null", 0},
127 {ENCR_AES_CBC
, "aes", 0},
128 /* {ENCR_AES_CTR, "***", 0}, */
129 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))", 64}, /* key_size = ICV size */
130 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))", 96}, /* key_size = ICV size */
131 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))", 128}, /* key_size = ICV size */
132 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))", 64}, /* key_size = ICV size */
133 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))", 96}, /* key_size = ICV size */
134 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))", 128}, /* key_size = ICV size */
135 {END_OF_LIST
, NULL
, 0},
139 * Algorithms for integrity protection
141 static kernel_algorithm_t integrity_algs
[] = {
142 {AUTH_HMAC_MD5_96
, "md5", 128},
143 {AUTH_HMAC_SHA1_96
, "sha1", 160},
144 {AUTH_HMAC_SHA2_256_128
, "sha256", 256},
145 {AUTH_HMAC_SHA2_384_192
, "sha384", 384},
146 {AUTH_HMAC_SHA2_512_256
, "sha512", 512},
147 /* {AUTH_DES_MAC, "***", 0}, */
148 /* {AUTH_KPDK_MD5, "***", 0}, */
149 {AUTH_AES_XCBC_96
, "xcbc(aes)", 128},
150 {END_OF_LIST
, NULL
, 0},
154 * Algorithms for IPComp
156 static kernel_algorithm_t compression_algs
[] = {
157 /* {IPCOMP_OUI, "***", 0}, */
158 {IPCOMP_DEFLATE
, "deflate", 0},
159 {IPCOMP_LZS
, "lzs", 0},
160 {IPCOMP_LZJH
, "lzjh", 0},
161 {END_OF_LIST
, NULL
, 0},
165 * Look up a kernel algorithm name and its key size
167 static char* lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
168 u_int16_t ikev2_algo
, u_int16_t
*key_size
)
170 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
172 if (ikev2_algo
== kernel_algo
->ikev2_id
)
174 /* match, evaluate key length */
175 if (key_size
&& *key_size
== 0)
176 { /* update key size if not set */
177 *key_size
= kernel_algo
->key_size
;
179 return kernel_algo
->name
;
186 typedef struct route_entry_t route_entry_t
;
189 * installed routing entry
191 struct route_entry_t
{
193 /** Index of the interface the route is bound to */
196 /** Source ip of the route */
199 /** gateway for this route */
202 /** Destination net */
205 /** Destination net prefixlen */
210 * destroy an route_entry_t object
212 static void route_entry_destroy(route_entry_t
*this)
214 this->src_ip
->destroy(this->src_ip
);
215 this->gateway
->destroy(this->gateway
);
216 chunk_free(&this->dst_net
);
220 typedef struct policy_entry_t policy_entry_t
;
223 * installed kernel policy.
225 struct policy_entry_t
{
227 /** direction of this policy: in, out, forward */
230 /** reqid of the policy */
233 /** parameters of installed policy */
234 struct xfrm_selector sel
;
236 /** associated route installed for this policy */
237 route_entry_t
*route
;
239 /** by how many CHILD_SA's this policy is used */
243 typedef struct addr_entry_t addr_entry_t
;
246 * IP address in an inface_entry_t
248 struct addr_entry_t
{
250 /** The ip address */
253 /** virtual IP managed by us */
256 /** scope of the address */
259 /** Number of times this IP is used, if virtual */
264 * destroy a addr_entry_t object
266 static void addr_entry_destroy(addr_entry_t
*this)
268 this->ip
->destroy(this->ip
);
272 typedef struct iface_entry_t iface_entry_t
;
275 * A network interface on this system, containing addr_entry_t's
277 struct iface_entry_t
{
279 /** interface index */
282 /** name of the interface */
283 char ifname
[IFNAMSIZ
];
285 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
288 /** list of addresses as host_t */
289 linked_list_t
*addrs
;
293 * destroy an interface entry
295 static void iface_entry_destroy(iface_entry_t
*this)
297 this->addrs
->destroy_function(this->addrs
, (void*)addr_entry_destroy
);
301 typedef struct private_kernel_interface_t private_kernel_interface_t
;
304 * Private variables and functions of kernel_interface class.
306 struct private_kernel_interface_t
{
308 * Public part of the kernel_interface_t object.
310 kernel_interface_t
public;
313 * mutex to lock access to netlink socket
315 pthread_mutex_t nl_mutex
;
318 * mutex to lock access to various lists
320 pthread_mutex_t mutex
;
323 * condition variable to signal virtual IP add/removal
328 * List of installed policies (policy_entry_t)
330 linked_list_t
*policies
;
333 * Cached list of interfaces and its adresses (iface_entry_t)
335 linked_list_t
*ifaces
;
338 * iterator used in hook()
343 * job receiving netlink events
348 * current sequence number for netlink request
353 * Netlink xfrm socket (IPsec)
358 * netlink xfrm socket to receive acquire and expire events
360 int socket_xfrm_events
;
363 * Netlink rt socket (routing)
368 * Netlink rt socket to receive address change events
370 int socket_rt_events
;
373 * time of the last roam_job
375 struct timeval last_roam
;
378 * whether to install routes along policies
383 * routing table to install routes
388 * priority of used routing table
390 int routing_table_prio
;
394 * convert a IKEv2 specific protocol identifier to the kernel one
396 static u_int8_t
proto_ike2kernel(protocol_id_t proto
)
410 * reverse of ike2kernel
412 static protocol_id_t
proto_kernel2ike(u_int8_t proto
)
426 * convert a host_t to a struct xfrm_address
428 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
430 chunk_t chunk
= host
->get_address(host
);
431 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
435 * convert a traffic selector address range to subnet and its mask.
437 static void ts2subnet(traffic_selector_t
* ts
,
438 xfrm_address_t
*net
, u_int8_t
*mask
)
440 /* there is no way to do this cleanly, as the address range may
441 * be anything else but a subnet. We use from_addr as subnet
442 * and try to calculate a usable subnet mask.
447 size_t size
= (ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE
) ?
4 : 16;
449 from
= ts
->get_from_address(ts
);
450 to
= ts
->get_to_address(ts
);
453 /* go trough all bits of the addresses, beginning in the front.
454 * as long as they are equal, the subnet gets larger
456 for (byte
= 0; byte
< size
; byte
++)
458 for (bit
= 7; bit
>= 0; bit
--)
460 if ((1<<bit
& from
.ptr
[byte
]) != (1<<bit
& to
.ptr
[byte
]))
462 *mask
= ((7 - bit
) + (byte
* 8));
472 memcpy(net
, from
.ptr
, from
.len
);
478 * convert a traffic selector port range to port/portmask
480 static void ts2ports(traffic_selector_t
* ts
,
481 u_int16_t
*port
, u_int16_t
*mask
)
483 /* linux does not seem to accept complex portmasks. Only
484 * any or a specific port is allowed. We set to any, if we have
485 * a port range, or to a specific, if we have one port only.
489 from
= ts
->get_from_port(ts
);
490 to
= ts
->get_to_port(ts
);
505 * convert a pair of traffic_selectors to a xfrm_selector
507 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
508 traffic_selector_t
*dst
)
510 struct xfrm_selector sel
;
512 memset(&sel
, 0, sizeof(sel
));
513 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
514 /* src or dest proto may be "any" (0), use more restrictive one */
515 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
516 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
517 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
518 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
519 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
527 * Creates an rtattr and adds it to the netlink message
529 static void add_attribute(struct nlmsghdr
*hdr
, int rta_type
, chunk_t data
,
534 if (NLMSG_ALIGN(hdr
->nlmsg_len
) + RTA_ALIGN(data
.len
) > buflen
)
536 DBG1(DBG_KNL
, "unable to add attribute, buffer too small");
540 rta
= (struct rtattr
*)(((char*)hdr
) + NLMSG_ALIGN(hdr
->nlmsg_len
));
541 rta
->rta_type
= rta_type
;
542 rta
->rta_len
= RTA_LENGTH(data
.len
);
543 memcpy(RTA_DATA(rta
), data
.ptr
, data
.len
);
544 hdr
->nlmsg_len
= NLMSG_ALIGN(hdr
->nlmsg_len
) + rta
->rta_len
;
548 * process a XFRM_MSG_ACQUIRE from kernel
550 static void process_acquire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
554 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
555 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
557 if (RTA_OK(rtattr
, rtsize
))
559 if (rtattr
->rta_type
== XFRMA_TMPL
)
561 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rtattr
);
567 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
570 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
571 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid %d", reqid
);
572 job
= (job_t
*)acquire_job_create(reqid
);
573 charon
->processor
->queue_job(charon
->processor
, job
);
577 * process a XFRM_MSG_EXPIRE from kernel
579 static void process_expire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
582 protocol_id_t protocol
;
583 u_int32_t spi
, reqid
;
584 struct xfrm_user_expire
*expire
;
586 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
587 protocol
= proto_kernel2ike(expire
->state
.id
.proto
);
588 spi
= expire
->state
.id
.spi
;
589 reqid
= expire
->state
.reqid
;
591 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
593 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
595 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA 0x%x (reqid %d) which is "
596 "not a CHILD_SA", ntohl(spi
), reqid
);
600 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
601 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
602 protocol
, ntohl(spi
), reqid
);
605 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
609 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
611 charon
->processor
->queue_job(charon
->processor
, job
);
615 * start a roaming job. We delay it for a second and fire only one job
616 * for multiple events. Otherwise we would create two many jobs.
618 static void fire_roam_job(private_kernel_interface_t
*this, bool address
)
622 if (gettimeofday(&now
, NULL
) == 0)
624 if (timercmp(&now
, &this->last_roam
, >))
626 now
.tv_usec
+= ROAM_DELAY
* 1000;
627 while (now
.tv_usec
> 1000000)
630 now
.tv_usec
-= 1000000;
632 this->last_roam
= now
;
633 charon
->scheduler
->schedule_job(charon
->scheduler
,
634 (job_t
*)roam_job_create(address
), ROAM_DELAY
);
640 * process RTM_NEWLINK/RTM_DELLINK from kernel
642 static void process_link(private_kernel_interface_t
*this,
643 struct nlmsghdr
*hdr
, bool event
)
645 struct ifinfomsg
* msg
= (struct ifinfomsg
*)(NLMSG_DATA(hdr
));
646 struct rtattr
*rta
= IFLA_RTA(msg
);
647 size_t rtasize
= IFLA_PAYLOAD (hdr
);
648 iterator_t
*iterator
;
649 iface_entry_t
*current
, *entry
= NULL
;
653 while(RTA_OK(rta
, rtasize
))
655 switch (rta
->rta_type
)
658 name
= RTA_DATA(rta
);
661 rta
= RTA_NEXT(rta
, rtasize
);
668 switch (hdr
->nlmsg_type
)
672 if (msg
->ifi_flags
& IFF_LOOPBACK
)
673 { /* ignore loopback interfaces */
676 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
678 while (iterator
->iterate(iterator
, (void**)¤t
))
680 if (current
->ifindex
== msg
->ifi_index
)
688 entry
= malloc_thing(iface_entry_t
);
689 entry
->ifindex
= msg
->ifi_index
;
691 entry
->addrs
= linked_list_create();
692 this->ifaces
->insert_last(this->ifaces
, entry
);
694 memcpy(entry
->ifname
, name
, IFNAMSIZ
);
695 entry
->ifname
[IFNAMSIZ
-1] = '\0';
698 if (!(entry
->flags
& IFF_UP
) && (msg
->ifi_flags
& IFF_UP
))
701 DBG1(DBG_KNL
, "interface %s activated", name
);
703 if ((entry
->flags
& IFF_UP
) && !(msg
->ifi_flags
& IFF_UP
))
706 DBG1(DBG_KNL
, "interface %s deactivated", name
);
709 entry
->flags
= msg
->ifi_flags
;
710 iterator
->destroy(iterator
);
715 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
717 while (iterator
->iterate(iterator
, (void**)¤t
))
719 if (current
->ifindex
== msg
->ifi_index
)
721 /* we do not remove it, as an address may be added to a
722 * "down" interface and we wan't to know that. */
723 current
->flags
= msg
->ifi_flags
;
727 iterator
->destroy(iterator
);
732 /* send an update to all IKE_SAs */
735 fire_roam_job(this, TRUE
);
740 * process RTM_NEWADDR/RTM_DELADDR from kernel
742 static void process_addr(private_kernel_interface_t
*this,
743 struct nlmsghdr
*hdr
, bool event
)
745 struct ifaddrmsg
* msg
= (struct ifaddrmsg
*)(NLMSG_DATA(hdr
));
746 struct rtattr
*rta
= IFA_RTA(msg
);
747 size_t rtasize
= IFA_PAYLOAD (hdr
);
749 iterator_t
*ifaces
, *addrs
;
750 iface_entry_t
*iface
;
752 chunk_t local
= chunk_empty
, address
= chunk_empty
;
753 bool update
= FALSE
, found
= FALSE
, changed
= FALSE
;
755 while(RTA_OK(rta
, rtasize
))
757 switch (rta
->rta_type
)
760 local
.ptr
= RTA_DATA(rta
);
761 local
.len
= RTA_PAYLOAD(rta
);
764 address
.ptr
= RTA_DATA(rta
);
765 address
.len
= RTA_PAYLOAD(rta
);
768 rta
= RTA_NEXT(rta
, rtasize
);
771 /* For PPP interfaces, we need the IFA_LOCAL address,
772 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
773 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
776 host
= host_create_from_chunk(msg
->ifa_family
, local
, 0);
778 else if (address
.ptr
)
780 host
= host_create_from_chunk(msg
->ifa_family
, address
, 0);
788 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
789 while (ifaces
->iterate(ifaces
, (void**)&iface
))
791 if (iface
->ifindex
== msg
->ifa_index
)
793 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
794 while (addrs
->iterate(addrs
, (void**)&addr
))
796 if (host
->ip_equals(host
, addr
->ip
))
799 if (hdr
->nlmsg_type
== RTM_DELADDR
)
802 addrs
->remove(addrs
);
805 DBG1(DBG_KNL
, "%H disappeared from %s",
806 host
, iface
->ifname
);
808 addr_entry_destroy(addr
);
810 else if (hdr
->nlmsg_type
== RTM_NEWADDR
&& addr
->virtual)
816 addrs
->destroy(addrs
);
818 if (hdr
->nlmsg_type
== RTM_NEWADDR
)
824 addr
= malloc_thing(addr_entry_t
);
825 addr
->ip
= host
->clone(host
);
826 addr
->virtual = FALSE
;
828 addr
->scope
= msg
->ifa_scope
;
830 iface
->addrs
->insert_last(iface
->addrs
, addr
);
833 DBG1(DBG_KNL
, "%H appeared on %s", host
, iface
->ifname
);
837 if (found
&& (iface
->flags
& IFF_UP
))
844 ifaces
->destroy(ifaces
);
847 /* send an update to all IKE_SAs */
848 if (update
&& event
&& changed
)
850 fire_roam_job(this, TRUE
);
855 * Receives events from kernel
857 static job_requeue_t
receive_events(private_kernel_interface_t
*this)
860 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
861 struct sockaddr_nl addr
;
862 socklen_t addr_len
= sizeof(addr
);
863 int len
, oldstate
, maxfd
, selected
;
867 FD_SET(this->socket_xfrm_events
, &rfds
);
868 FD_SET(this->socket_rt_events
, &rfds
);
869 maxfd
= max(this->socket_xfrm_events
, this->socket_rt_events
);
871 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
872 selected
= select(maxfd
+ 1, &rfds
, NULL
, NULL
, NULL
);
873 pthread_setcancelstate(oldstate
, NULL
);
876 DBG1(DBG_KNL
, "selecting on sockets failed: %s", strerror(errno
));
877 return JOB_REQUEUE_FAIR
;
879 if (FD_ISSET(this->socket_xfrm_events
, &rfds
))
881 selected
= this->socket_xfrm_events
;
883 else if (FD_ISSET(this->socket_rt_events
, &rfds
))
885 selected
= this->socket_rt_events
;
889 return JOB_REQUEUE_DIRECT
;
892 len
= recvfrom(selected
, response
, sizeof(response
), MSG_DONTWAIT
,
893 (struct sockaddr
*)&addr
, &addr_len
);
899 /* interrupted, try again */
900 return JOB_REQUEUE_DIRECT
;
902 /* no data ready, select again */
903 return JOB_REQUEUE_DIRECT
;
905 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
907 return JOB_REQUEUE_FAIR
;
910 if (addr
.nl_pid
!= 0)
911 { /* not from kernel. not interested, try another one */
912 return JOB_REQUEUE_DIRECT
;
915 while (NLMSG_OK(hdr
, len
))
917 /* looks good so far, dispatch netlink message */
918 if (selected
== this->socket_xfrm_events
)
920 switch (hdr
->nlmsg_type
)
922 case XFRM_MSG_ACQUIRE
:
923 process_acquire(this, hdr
);
925 case XFRM_MSG_EXPIRE
:
926 process_expire(this, hdr
);
932 else if (selected
== this->socket_rt_events
)
934 switch (hdr
->nlmsg_type
)
938 process_addr(this, hdr
, TRUE
);
939 pthread_cond_signal(&this->cond
);
943 process_link(this, hdr
, TRUE
);
944 pthread_cond_signal(&this->cond
);
948 fire_roam_job(this, FALSE
);
954 hdr
= NLMSG_NEXT(hdr
, len
);
956 return JOB_REQUEUE_DIRECT
;
960 * send a netlink message and wait for a reply
962 static status_t
netlink_send(private_kernel_interface_t
*this,
963 int socket
, struct nlmsghdr
*in
,
964 struct nlmsghdr
**out
, size_t *out_len
)
967 struct sockaddr_nl addr
;
968 chunk_t result
= chunk_empty
, tmp
;
969 struct nlmsghdr
*msg
, peek
;
971 pthread_mutex_lock(&this->nl_mutex
);
973 in
->nlmsg_seq
= ++this->seq
;
974 in
->nlmsg_pid
= getpid();
976 memset(&addr
, 0, sizeof(addr
));
977 addr
.nl_family
= AF_NETLINK
;
983 len
= sendto(socket
, in
, in
->nlmsg_len
, 0,
984 (struct sockaddr
*)&addr
, sizeof(addr
));
986 if (len
!= in
->nlmsg_len
)
990 /* interrupted, try again */
993 pthread_mutex_unlock(&this->nl_mutex
);
994 DBG1(DBG_KNL
, "error sending to netlink socket: %s", strerror(errno
));
1003 tmp
.len
= sizeof(buf
);
1005 msg
= (struct nlmsghdr
*)tmp
.ptr
;
1007 memset(&addr
, 0, sizeof(addr
));
1008 addr
.nl_family
= AF_NETLINK
;
1009 addr
.nl_pid
= getpid();
1011 addr_len
= sizeof(addr
);
1013 len
= recvfrom(socket
, tmp
.ptr
, tmp
.len
, 0,
1014 (struct sockaddr
*)&addr
, &addr_len
);
1020 DBG1(DBG_KNL
, "got interrupted");
1021 /* interrupted, try again */
1024 DBG1(DBG_KNL
, "error reading from netlink socket: %s", strerror(errno
));
1025 pthread_mutex_unlock(&this->nl_mutex
);
1028 if (!NLMSG_OK(msg
, len
))
1030 DBG1(DBG_KNL
, "received corrupted netlink message");
1031 pthread_mutex_unlock(&this->nl_mutex
);
1034 if (msg
->nlmsg_seq
!= this->seq
)
1036 DBG1(DBG_KNL
, "received invalid netlink sequence number");
1037 if (msg
->nlmsg_seq
< this->seq
)
1041 pthread_mutex_unlock(&this->nl_mutex
);
1046 result
= chunk_cata("cc", result
, tmp
);
1048 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
1049 * numbers to detect multi header messages */
1050 len
= recvfrom(socket
, &peek
, sizeof(peek
), MSG_PEEK
| MSG_DONTWAIT
,
1051 (struct sockaddr
*)&addr
, &addr_len
);
1053 if (len
== sizeof(peek
) && peek
.nlmsg_seq
== this->seq
)
1055 /* seems to be multipart */
1061 *out_len
= result
.len
;
1062 *out
= (struct nlmsghdr
*)clalloc(result
.ptr
, result
.len
);
1064 pthread_mutex_unlock(&this->nl_mutex
);
1070 * send a netlink message and wait for its acknowlegde
1072 static status_t
netlink_send_ack(private_kernel_interface_t
*this,
1073 int socket
, struct nlmsghdr
*in
)
1075 struct nlmsghdr
*out
, *hdr
;
1078 if (netlink_send(this, socket
, in
, &out
, &len
) != SUCCESS
)
1083 while (NLMSG_OK(hdr
, len
))
1085 switch (hdr
->nlmsg_type
)
1089 struct nlmsgerr
* err
= (struct nlmsgerr
*)NLMSG_DATA(hdr
);
1093 DBG1(DBG_KNL
, "received netlink error: %s (%d)",
1094 strerror(-err
->error
), -err
->error
);
1102 hdr
= NLMSG_NEXT(hdr
, len
);
1109 DBG1(DBG_KNL
, "netlink request not acknowlegded");
1115 * Initialize a list of local addresses.
1117 static status_t
init_address_list(private_kernel_interface_t
*this)
1119 char request
[BUFFER_SIZE
];
1120 struct nlmsghdr
*out
, *current
, *in
;
1121 struct rtgenmsg
*msg
;
1123 iterator_t
*ifaces
, *addrs
;
1124 iface_entry_t
*iface
;
1127 DBG1(DBG_KNL
, "listening on interfaces:");
1129 memset(&request
, 0, sizeof(request
));
1131 in
= (struct nlmsghdr
*)&request
;
1132 in
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtgenmsg
));
1133 in
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_MATCH
| NLM_F_ROOT
;
1134 msg
= (struct rtgenmsg
*)NLMSG_DATA(in
);
1135 msg
->rtgen_family
= AF_UNSPEC
;
1138 in
->nlmsg_type
= RTM_GETLINK
;
1139 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1144 while (NLMSG_OK(current
, len
))
1146 switch (current
->nlmsg_type
)
1151 process_link(this, current
, FALSE
);
1154 current
= NLMSG_NEXT(current
, len
);
1161 /* get all interface addresses */
1162 in
->nlmsg_type
= RTM_GETADDR
;
1163 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1168 while (NLMSG_OK(current
, len
))
1170 switch (current
->nlmsg_type
)
1175 process_addr(this, current
, FALSE
);
1178 current
= NLMSG_NEXT(current
, len
);
1185 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1186 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1188 if (iface
->flags
& IFF_UP
)
1190 DBG1(DBG_KNL
, " %s", iface
->ifname
);
1191 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1192 while (addrs
->iterate(addrs
, (void**)&addr
))
1194 DBG1(DBG_KNL
, " %H", addr
->ip
);
1196 addrs
->destroy(addrs
);
1199 ifaces
->destroy(ifaces
);
1204 * iterator hook to iterate over addrs
1206 static hook_result_t
addr_hook(private_kernel_interface_t
*this,
1207 addr_entry_t
*in
, host_t
**out
)
1210 { /* skip virtual interfaces added by us */
1213 if (in
->scope
>= RT_SCOPE_LINK
)
1214 { /* skip addresses with a unusable scope */
1222 * iterator hook to iterate over ifaces
1224 static hook_result_t
iface_hook(private_kernel_interface_t
*this,
1225 iface_entry_t
*in
, host_t
**out
)
1227 if (!(in
->flags
& IFF_UP
))
1228 { /* skip interfaces not up */
1232 if (this->hiter
== NULL
)
1234 this->hiter
= in
->addrs
->create_iterator(in
->addrs
, TRUE
);
1235 this->hiter
->set_iterator_hook(this->hiter
,
1236 (iterator_hook_t
*)addr_hook
, this);
1238 while (this->hiter
->iterate(this->hiter
, (void**)out
))
1242 this->hiter
->destroy(this->hiter
);
1248 * Implements kernel_interface_t.create_address_iterator.
1250 static iterator_t
*create_address_iterator(private_kernel_interface_t
*this)
1252 iterator_t
*iterator
;
1254 /* This iterator is not only hooked, is is double-hooked. As we have stored
1255 * our addresses in iface_entry->addr_entry->ip, we need to iterate the
1256 * entries in each interface we iterate. This does the iface_hook. The
1257 * addr_hook returns the ip instead of the addr_entry. */
1259 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1260 iterator
->set_iterator_hook(iterator
, (iterator_hook_t
*)iface_hook
, this);
1265 * implementation of kernel_interface_t.get_interface_name
1267 static char *get_interface_name(private_kernel_interface_t
*this, host_t
* ip
)
1269 iterator_t
*ifaces
, *addrs
;
1270 iface_entry_t
*iface
;
1274 DBG2(DBG_KNL
, "getting interface name for %H", ip
);
1276 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1277 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1279 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1280 while (addrs
->iterate(addrs
, (void**)&addr
))
1282 if (ip
->ip_equals(ip
, addr
->ip
))
1284 name
= strdup(iface
->ifname
);
1288 addrs
->destroy(addrs
);
1294 ifaces
->destroy(ifaces
);
1298 DBG2(DBG_KNL
, "%H is on interface %s", ip
, name
);
1302 DBG2(DBG_KNL
, "%H is not a local address", ip
);
1308 * Tries to find an ip address of a local interface that is included in the
1309 * supplied traffic selector.
1311 static status_t
get_address_by_ts(private_kernel_interface_t
*this,
1312 traffic_selector_t
*ts
, host_t
**ip
)
1314 iterator_t
*ifaces
, *addrs
;
1315 iface_entry_t
*iface
;
1321 DBG2(DBG_KNL
, "getting a local address in traffic selector %R", ts
);
1323 /* if we have a family which includes localhost, we do not
1324 * search for an IP, we use the default */
1325 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
1327 if (family
== AF_INET
)
1329 host
= host_create_from_string("127.0.0.1", 0);
1333 host
= host_create_from_string("::1", 0);
1336 if (ts
->includes(ts
, host
))
1338 *ip
= host_create_any(family
);
1339 host
->destroy(host
);
1340 DBG2(DBG_KNL
, "using host %H", *ip
);
1343 host
->destroy(host
);
1345 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1346 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1348 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1349 while (addrs
->iterate(addrs
, (void**)&addr
))
1351 if (ts
->includes(ts
, addr
->ip
))
1354 *ip
= addr
->ip
->clone(addr
->ip
);
1358 addrs
->destroy(addrs
);
1364 ifaces
->destroy(ifaces
);
1368 DBG1(DBG_KNL
, "no local address found in traffic selector %R", ts
);
1371 DBG2(DBG_KNL
, "using host %H", *ip
);
1376 * get the interface of a local address
1378 static int get_interface_index(private_kernel_interface_t
*this, host_t
* ip
)
1380 iterator_t
*ifaces
, *addrs
;
1381 iface_entry_t
*iface
;
1385 DBG2(DBG_KNL
, "getting iface for %H", ip
);
1387 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1388 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1390 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1391 while (addrs
->iterate(addrs
, (void**)&addr
))
1393 if (ip
->ip_equals(ip
, addr
->ip
))
1395 ifindex
= iface
->ifindex
;
1399 addrs
->destroy(addrs
);
1405 ifaces
->destroy(ifaces
);
1409 DBG1(DBG_KNL
, "unable to get interface for %H", ip
);
1415 * get the refcount of a virtual ip
1417 static int get_vip_refcount(private_kernel_interface_t
*this, host_t
* ip
)
1419 iterator_t
*ifaces
, *addrs
;
1420 iface_entry_t
*iface
;
1424 ifaces
= this->ifaces
->create_iterator(this->ifaces
, TRUE
);
1425 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1427 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1428 while (addrs
->iterate(addrs
, (void**)&addr
))
1430 if (addr
->virtual && (iface
->flags
& IFF_UP
) &&
1431 ip
->ip_equals(ip
, addr
->ip
))
1433 refcount
= addr
->refcount
;
1437 addrs
->destroy(addrs
);
1443 ifaces
->destroy(ifaces
);
1449 * Manages the creation and deletion of ip addresses on an interface.
1450 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1452 static status_t
manage_ipaddr(private_kernel_interface_t
*this, int nlmsg_type
,
1453 int flags
, int if_index
, host_t
*ip
)
1455 unsigned char request
[BUFFER_SIZE
];
1456 struct nlmsghdr
*hdr
;
1457 struct ifaddrmsg
*msg
;
1460 memset(&request
, 0, sizeof(request
));
1462 chunk
= ip
->get_address(ip
);
1464 hdr
= (struct nlmsghdr
*)request
;
1465 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1466 hdr
->nlmsg_type
= nlmsg_type
;
1467 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1469 msg
= (struct ifaddrmsg
*)NLMSG_DATA(hdr
);
1470 msg
->ifa_family
= ip
->get_family(ip
);
1472 msg
->ifa_prefixlen
= 8 * chunk
.len
;
1473 msg
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1474 msg
->ifa_index
= if_index
;
1476 add_attribute(hdr
, IFA_LOCAL
, chunk
, sizeof(request
));
1478 return netlink_send_ack(this, this->socket_rt
, hdr
);
1482 * Manages source routes in the routing table.
1483 * By setting the appropriate nlmsg_type, the route added or r.
1485 static status_t
manage_srcroute(private_kernel_interface_t
*this, int nlmsg_type
,
1486 int flags
, route_entry_t
*route
)
1488 unsigned char request
[BUFFER_SIZE
];
1489 struct nlmsghdr
*hdr
;
1493 /* if route is 0.0.0.0/0, we can't install it, as it would
1494 * overwrite the default route. Instead, we add two routes:
1495 * 0.0.0.0/1 and 128.0.0.0/1 */
1496 if (this->routing_table
== 0 && route
->prefixlen
== 0)
1501 half
.dst_net
= chunk_alloca(route
->dst_net
.len
);
1502 memset(half
.dst_net
.ptr
, 0, half
.dst_net
.len
);
1503 half
.src_ip
= route
->src_ip
;
1504 half
.gateway
= route
->gateway
;
1505 half
.if_index
= route
->if_index
;
1508 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1509 half
.dst_net
.ptr
[0] |= 0x80;
1510 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1514 memset(&request
, 0, sizeof(request
));
1516 hdr
= (struct nlmsghdr
*)request
;
1517 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1518 hdr
->nlmsg_type
= nlmsg_type
;
1519 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1521 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1522 msg
->rtm_family
= route
->src_ip
->get_family(route
->src_ip
);
1523 msg
->rtm_dst_len
= route
->prefixlen
;
1524 msg
->rtm_table
= this->routing_table
;
1525 msg
->rtm_protocol
= RTPROT_STATIC
;
1526 msg
->rtm_type
= RTN_UNICAST
;
1527 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1529 add_attribute(hdr
, RTA_DST
, route
->dst_net
, sizeof(request
));
1530 chunk
= route
->src_ip
->get_address(route
->src_ip
);
1531 add_attribute(hdr
, RTA_PREFSRC
, chunk
, sizeof(request
));
1532 chunk
= route
->gateway
->get_address(route
->gateway
);
1533 add_attribute(hdr
, RTA_GATEWAY
, chunk
, sizeof(request
));
1534 chunk
.ptr
= (char*)&route
->if_index
;
1535 chunk
.len
= sizeof(route
->if_index
);
1536 add_attribute(hdr
, RTA_OIF
, chunk
, sizeof(request
));
1538 return netlink_send_ack(this, this->socket_rt
, hdr
);
1542 * create or delete an rule to use our routing table
1544 static status_t
manage_rule(private_kernel_interface_t
*this, int nlmsg_type
,
1545 u_int32_t table
, u_int32_t prio
)
1547 unsigned char request
[BUFFER_SIZE
];
1548 struct nlmsghdr
*hdr
;
1552 memset(&request
, 0, sizeof(request
));
1553 hdr
= (struct nlmsghdr
*)request
;
1554 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1555 hdr
->nlmsg_type
= nlmsg_type
;
1556 if (nlmsg_type
== RTM_NEWRULE
)
1558 hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
1560 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1562 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1563 msg
->rtm_table
= table
;
1564 msg
->rtm_family
= AF_INET
;
1565 msg
->rtm_protocol
= RTPROT_BOOT
;
1566 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1567 msg
->rtm_type
= RTN_UNICAST
;
1569 chunk
= chunk_from_thing(prio
);
1570 add_attribute(hdr
, RTA_PRIORITY
, chunk
, sizeof(request
));
1572 return netlink_send_ack(this, this->socket_rt
, hdr
);
1576 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1578 static bool addr_in_subnet(chunk_t addr
, chunk_t net
, int net_len
)
1582 if (addr
.len
!= net
.len
)
1586 /* scan through all bits, beginning in the front */
1587 for (byte
= 0; byte
< addr
.len
; byte
++)
1589 for (bit
= 7; bit
>= 0; bit
--)
1591 /* check if bits are equal (or we reached the end of the net) */
1592 if (bit
+ byte
* 8 > net_len
)
1596 if (((1<<bit
) & addr
.ptr
[byte
]) != ((1<<bit
) & net
.ptr
[byte
]))
1606 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1608 static host_t
*get_route(private_kernel_interface_t
*this, host_t
*dest
,
1611 unsigned char request
[BUFFER_SIZE
];
1612 struct nlmsghdr
*hdr
, *out
, *current
;
1617 host_t
*src
= NULL
, *gtw
= NULL
;
1619 DBG2(DBG_KNL
, "getting address to reach %H", dest
);
1621 memset(&request
, 0, sizeof(request
));
1623 hdr
= (struct nlmsghdr
*)request
;
1624 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
| NLM_F_ROOT
;
1625 hdr
->nlmsg_type
= RTM_GETROUTE
;
1626 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1628 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1629 msg
->rtm_family
= dest
->get_family(dest
);
1631 chunk
= dest
->get_address(dest
);
1632 add_attribute(hdr
, RTA_DST
, chunk
, sizeof(request
));
1634 if (netlink_send(this, this->socket_rt
, hdr
, &out
, &len
) != SUCCESS
)
1636 DBG1(DBG_KNL
, "getting address to %H failed", dest
);
1640 while (NLMSG_OK(current
, len
))
1642 switch (current
->nlmsg_type
)
1650 chunk_t rta_gtw
, rta_src
, rta_dst
;
1651 u_int32_t rta_oif
= 0;
1653 rta_gtw
= rta_src
= rta_dst
= chunk_empty
;
1654 msg
= (struct rtmsg
*)(NLMSG_DATA(current
));
1656 rtasize
= RTM_PAYLOAD(current
);
1657 while (RTA_OK(rta
, rtasize
))
1659 switch (rta
->rta_type
)
1662 rta_src
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1665 rta_gtw
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1668 rta_dst
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1671 if (RTA_PAYLOAD(rta
) == sizeof(rta_oif
))
1673 rta_oif
= *(u_int32_t
*)RTA_DATA(rta
);
1677 rta
= RTA_NEXT(rta
, rtasize
);
1680 /* apply the route if:
1681 * - it is not from our own ipsec routing table
1682 * - is better than a previous one
1683 * - is the default route or
1684 * - its destination net contains our destination
1686 if ((this->routing_table
== 0 ||msg
->rtm_table
!= this->routing_table
)
1687 && msg
->rtm_dst_len
> best
1688 && (msg
->rtm_dst_len
== 0 || /* default route */
1689 (rta_dst
.ptr
&& addr_in_subnet(chunk
, rta_dst
, msg
->rtm_dst_len
))))
1691 iterator_t
*ifaces
, *addrs
;
1692 iface_entry_t
*iface
;
1695 best
= msg
->rtm_dst_len
;
1699 gtw
= host_create_from_chunk(msg
->rtm_family
, rta_gtw
, 0);
1701 else if (rta_src
.ptr
)
1704 src
= host_create_from_chunk(msg
->rtm_family
, rta_src
, 0);
1705 if (get_vip_refcount(this, src
))
1706 { /* skip source address if it is installed by us */
1709 current
= NLMSG_NEXT(current
, len
);
1715 /* no source addr, get one from the interfaces */
1716 ifaces
= this->ifaces
->create_iterator_locked(
1717 this->ifaces
, &this->mutex
);
1718 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1720 if (iface
->ifindex
== rta_oif
)
1722 addrs
= iface
->addrs
->create_iterator(
1723 iface
->addrs
, TRUE
);
1724 while (addrs
->iterate(addrs
, (void**)&addr
))
1726 chunk_t ip
= addr
->ip
->get_address(addr
->ip
);
1727 if (msg
->rtm_dst_len
== 0
1728 || addr_in_subnet(ip
, rta_dst
, msg
->rtm_dst_len
))
1731 src
= addr
->ip
->clone(addr
->ip
);
1735 addrs
->destroy(addrs
);
1738 ifaces
->destroy(ifaces
);
1744 current
= NLMSG_NEXT(current
, len
);
1757 return dest
->clone(dest
);
1763 * Implementation of kernel_interface_t.get_source_addr.
1765 static host_t
* get_source_addr(private_kernel_interface_t
*this, host_t
*dest
)
1767 return get_route(this, dest
, FALSE
);
1771 * Implementation of kernel_interface_t.add_ip.
1773 static status_t
add_ip(private_kernel_interface_t
*this,
1774 host_t
*virtual_ip
, host_t
*iface_ip
)
1776 iface_entry_t
*iface
;
1778 iterator_t
*addrs
, *ifaces
;
1781 DBG2(DBG_KNL
, "adding virtual IP %H", virtual_ip
);
1783 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1784 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1786 bool iface_found
= FALSE
;
1788 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1789 while (addrs
->iterate(addrs
, (void**)&addr
))
1791 if (iface_ip
->ip_equals(iface_ip
, addr
->ip
))
1795 else if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1798 DBG2(DBG_KNL
, "virtual IP %H already installed on %s",
1799 virtual_ip
, iface
->ifname
);
1800 addrs
->destroy(addrs
);
1801 ifaces
->destroy(ifaces
);
1805 addrs
->destroy(addrs
);
1809 ifindex
= iface
->ifindex
;
1810 addr
= malloc_thing(addr_entry_t
);
1811 addr
->ip
= virtual_ip
->clone(virtual_ip
);
1813 addr
->virtual = TRUE
;
1814 addr
->scope
= RT_SCOPE_UNIVERSE
;
1815 iface
->addrs
->insert_last(iface
->addrs
, addr
);
1817 if (manage_ipaddr(this, RTM_NEWADDR
, NLM_F_CREATE
| NLM_F_EXCL
,
1818 ifindex
, virtual_ip
) == SUCCESS
)
1820 while (get_vip_refcount(this, virtual_ip
) == 0)
1821 { /* wait until address appears */
1822 pthread_cond_wait(&this->cond
, &this->mutex
);
1824 ifaces
->destroy(ifaces
);
1827 ifaces
->destroy(ifaces
);
1828 DBG1(DBG_KNL
, "adding virtual IP %H failed", virtual_ip
);
1832 ifaces
->destroy(ifaces
);
1834 DBG1(DBG_KNL
, "interface address %H not found, unable to install"
1835 "virtual IP %H", iface_ip
, virtual_ip
);
1840 * Implementation of kernel_interface_t.del_ip.
1842 static status_t
del_ip(private_kernel_interface_t
*this, host_t
*virtual_ip
)
1844 iface_entry_t
*iface
;
1846 iterator_t
*addrs
, *ifaces
;
1850 DBG2(DBG_KNL
, "deleting virtual IP %H", virtual_ip
);
1852 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1853 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1855 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1856 while (addrs
->iterate(addrs
, (void**)&addr
))
1858 if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1860 ifindex
= iface
->ifindex
;
1861 if (addr
->refcount
== 1)
1863 status
= manage_ipaddr(this, RTM_DELADDR
, 0,
1864 ifindex
, virtual_ip
);
1865 if (status
== SUCCESS
)
1866 { /* wait until the address is really gone */
1867 while (get_vip_refcount(this, virtual_ip
) > 0)
1869 pthread_cond_wait(&this->cond
, &this->mutex
);
1872 addrs
->destroy(addrs
);
1873 ifaces
->destroy(ifaces
);
1880 DBG2(DBG_KNL
, "virtual IP %H used by other SAs, not deleting",
1882 addrs
->destroy(addrs
);
1883 ifaces
->destroy(ifaces
);
1887 addrs
->destroy(addrs
);
1889 ifaces
->destroy(ifaces
);
1891 DBG2(DBG_KNL
, "virtual IP %H not cached, unable to delete", virtual_ip
);
1896 * Get an SPI for a specific protocol from the kernel.
1898 static status_t
get_spi_internal(private_kernel_interface_t
*this,
1899 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
1900 u_int32_t reqid
, u_int32_t
*spi
)
1902 unsigned char request
[BUFFER_SIZE
];
1903 struct nlmsghdr
*hdr
, *out
;
1904 struct xfrm_userspi_info
*userspi
;
1905 u_int32_t received_spi
= 0;
1908 memset(&request
, 0, sizeof(request
));
1910 hdr
= (struct nlmsghdr
*)request
;
1911 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1912 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1913 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1915 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1916 host2xfrm(src
, &userspi
->info
.saddr
);
1917 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1918 userspi
->info
.id
.proto
= proto
;
1919 userspi
->info
.mode
= TRUE
; /* tunnel mode */
1920 userspi
->info
.reqid
= reqid
;
1921 userspi
->info
.family
= src
->get_family(src
);
1925 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1928 while (NLMSG_OK(hdr
, len
))
1930 switch (hdr
->nlmsg_type
)
1932 case XFRM_MSG_NEWSA
:
1934 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1935 received_spi
= usersa
->id
.spi
;
1940 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1942 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1943 strerror(-err
->error
), -err
->error
);
1947 hdr
= NLMSG_NEXT(hdr
, len
);
1957 if (received_spi
== 0)
1962 *spi
= received_spi
;
1967 * Implementation of kernel_interface_t.get_spi.
1969 static status_t
get_spi(private_kernel_interface_t
*this,
1970 host_t
*src
, host_t
*dst
,
1971 protocol_id_t protocol
, u_int32_t reqid
,
1974 DBG2(DBG_KNL
, "getting SPI for reqid %d", reqid
);
1976 if (get_spi_internal(this, src
, dst
, proto_ike2kernel(protocol
),
1977 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
1979 DBG1(DBG_KNL
, "unable to get SPI for reqid %d", reqid
);
1983 DBG2(DBG_KNL
, "got SPI 0x%x for reqid %d", *spi
, reqid
);
1989 * Implementation of kernel_interface_t.get_cpi.
1991 static status_t
get_cpi(private_kernel_interface_t
*this,
1992 host_t
*src
, host_t
*dst
,
1993 u_int32_t reqid
, u_int16_t
*cpi
)
1995 u_int32_t received_spi
= 0;
1996 DBG2(DBG_KNL
, "getting CPI for reqid %d", reqid
);
1998 if (get_spi_internal(this, src
, dst
,
1999 IPPROTO_COMP
, 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
2001 DBG1(DBG_KNL
, "unable to get CPI for reqid %d", reqid
);
2005 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
2007 DBG2(DBG_KNL
, "got CPI 0x%x for reqid %d", *cpi
, reqid
);
2013 * Implementation of kernel_interface_t.add_sa.
2015 static status_t
add_sa(private_kernel_interface_t
*this,
2016 host_t
*src
, host_t
*dst
, u_int32_t spi
,
2017 protocol_id_t protocol
, u_int32_t reqid
,
2018 u_int64_t expire_soft
, u_int64_t expire_hard
,
2019 u_int16_t enc_alg
, u_int16_t enc_size
,
2020 u_int16_t int_alg
, u_int16_t int_size
,
2021 prf_plus_t
*prf_plus
, mode_t mode
,
2022 u_int16_t ipcomp
, bool encap
,
2025 unsigned char request
[BUFFER_SIZE
];
2027 /* additional 4 octets KEYMAT required for AES-GCM as of RFC4106 8.1. */
2028 u_int16_t add_keymat
= 32;
2029 struct nlmsghdr
*hdr
;
2030 struct xfrm_usersa_info
*sa
;
2032 memset(&request
, 0, sizeof(request
));
2034 DBG2(DBG_KNL
, "adding SAD entry with SPI 0x%x and reqid %d", spi
, reqid
);
2036 hdr
= (struct nlmsghdr
*)request
;
2037 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2038 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
2039 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2041 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
2042 host2xfrm(src
, &sa
->saddr
);
2043 host2xfrm(dst
, &sa
->id
.daddr
);
2045 sa
->id
.proto
= proto_ike2kernel(protocol
);
2046 sa
->family
= src
->get_family(src
);
2048 sa
->replay_window
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
2049 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
2051 /* we currently do not expire SAs by volume/packet count */
2052 sa
->lft
.soft_byte_limit
= XFRM_INF
;
2053 sa
->lft
.hard_byte_limit
= XFRM_INF
;
2054 sa
->lft
.soft_packet_limit
= XFRM_INF
;
2055 sa
->lft
.hard_packet_limit
= XFRM_INF
;
2056 /* we use lifetimes since added, not since used */
2057 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
2058 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
2059 sa
->lft
.soft_use_expires_seconds
= 0;
2060 sa
->lft
.hard_use_expires_seconds
= 0;
2062 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
2066 case ENCR_UNDEFINED
:
2069 case ENCR_AES_CCM_ICV8
:
2070 case ENCR_AES_CCM_ICV12
:
2071 case ENCR_AES_CCM_ICV16
:
2072 /* AES-CCM needs only 3 additional octets KEYMAT as of RFC 4309 7.1. */
2075 case ENCR_AES_GCM_ICV8
:
2076 case ENCR_AES_GCM_ICV12
:
2077 case ENCR_AES_GCM_ICV16
:
2079 u_int16_t icv_size
= 0;
2080 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
2081 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &icv_size
);
2082 if (alg_name
== NULL
)
2084 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2085 encryption_algorithm_names
, enc_alg
);
2088 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
2089 encryption_algorithm_names
, enc_alg
, enc_size
);
2091 /* additional KEYMAT required */
2092 enc_size
+= add_keymat
;
2094 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) + enc_size
/ 8);
2095 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2096 if (hdr
->nlmsg_len
> sizeof(request
))
2101 struct xfrm_algo_aead
* algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
2102 algo
->alg_key_len
= enc_size
;
2103 algo
->alg_icv_len
= icv_size
;
2104 strcpy(algo
->alg_name
, alg_name
);
2105 prf_plus
->get_bytes(prf_plus
, enc_size
/ 8, algo
->alg_key
);
2107 rthdr
= XFRM_RTA_NEXT(rthdr
);
2112 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
2113 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &enc_size
);
2114 if (alg_name
== NULL
)
2116 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2117 encryption_algorithm_names
, enc_alg
);
2120 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
2121 encryption_algorithm_names
, enc_alg
, enc_size
);
2123 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_size
/ 8);
2124 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2125 if (hdr
->nlmsg_len
> sizeof(request
))
2130 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
2131 algo
->alg_key_len
= enc_size
;
2132 strcpy(algo
->alg_name
, alg_name
);
2133 prf_plus
->get_bytes(prf_plus
, enc_size
/ 8, algo
->alg_key
);
2135 rthdr
= XFRM_RTA_NEXT(rthdr
);
2140 if (int_alg
!= AUTH_UNDEFINED
)
2142 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
2143 alg_name
= lookup_algorithm(integrity_algs
, int_alg
, &int_size
);
2144 if (alg_name
== NULL
)
2146 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2147 integrity_algorithm_names
, int_alg
);
2150 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
2151 integrity_algorithm_names
, int_alg
, int_size
);
2153 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_size
/ 8);
2154 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2155 if (hdr
->nlmsg_len
> sizeof(request
))
2160 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
2161 algo
->alg_key_len
= int_size
;
2162 strcpy(algo
->alg_name
, alg_name
);
2163 prf_plus
->get_bytes(prf_plus
, int_size
/ 8, algo
->alg_key
);
2165 rthdr
= XFRM_RTA_NEXT(rthdr
);
2168 if (ipcomp
!= IPCOMP_NONE
)
2170 rthdr
->rta_type
= XFRMA_ALG_COMP
;
2171 alg_name
= lookup_algorithm(compression_algs
, ipcomp
, NULL
);
2172 if (alg_name
== NULL
)
2174 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2175 ipcomp_transform_names
, ipcomp
);
2178 DBG2(DBG_KNL
, " using compression algorithm %N",
2179 ipcomp_transform_names
, ipcomp
);
2181 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
2182 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2183 if (hdr
->nlmsg_len
> sizeof(request
))
2188 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
2189 algo
->alg_key_len
= 0;
2190 strcpy(algo
->alg_name
, alg_name
);
2192 rthdr
= XFRM_RTA_NEXT(rthdr
);
2197 rthdr
->rta_type
= XFRMA_ENCAP
;
2198 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
2200 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2201 if (hdr
->nlmsg_len
> sizeof(request
))
2206 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
2207 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2208 tmpl
->encap_sport
= htons(src
->get_port(src
));
2209 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
2210 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2211 /* encap_oa could probably be derived from the
2212 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
2213 * pluto does the same as we do here but it uses encap_oa in the
2214 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
2215 * the kernel ignores it anyway
2216 * -> does that mean that NAT-T encap doesn't work in transport mode?
2217 * No. The reason the kernel ignores NAT-OA is that it recomputes
2218 * (or, rather, just ignores) the checksum. If packets pass
2219 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
2220 rthdr
= XFRM_RTA_NEXT(rthdr
);
2223 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2225 DBG1(DBG_KNL
, "unable to add SAD entry with SPI 0x%x", spi
);
2232 * Get the replay state (i.e. sequence numbers) of an SA.
2234 static status_t
get_replay_state(private_kernel_interface_t
*this,
2235 u_int32_t spi
, protocol_id_t protocol
, host_t
*dst
,
2236 struct xfrm_replay_state
*replay
)
2238 unsigned char request
[BUFFER_SIZE
];
2239 struct nlmsghdr
*hdr
, *out
= NULL
;
2240 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
2245 memset(&request
, 0, sizeof(request
));
2247 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI 0x%x", spi
);
2249 hdr
= (struct nlmsghdr
*)request
;
2250 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2251 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
2252 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
2254 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
2255 aevent_id
->flags
= XFRM_AE_RVAL
;
2257 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
2258 aevent_id
->sa_id
.spi
= spi
;
2259 aevent_id
->sa_id
.proto
= proto_ike2kernel(protocol
);
2260 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
2262 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2265 while (NLMSG_OK(hdr
, len
))
2267 switch (hdr
->nlmsg_type
)
2269 case XFRM_MSG_NEWAE
:
2271 out_aevent
= NLMSG_DATA(hdr
);
2276 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2277 DBG1(DBG_KNL
, "querying replay state from SAD entry failed: %s (%d)",
2278 strerror(-err
->error
), -err
->error
);
2282 hdr
= NLMSG_NEXT(hdr
, len
);
2291 if (out_aevent
== NULL
)
2293 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI 0x%x", spi
);
2298 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
2299 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
2300 while(RTA_OK(rta
, rtasize
))
2302 if (rta
->rta_type
== XFRMA_REPLAY_VAL
)
2304 memcpy(replay
, RTA_DATA(rta
), rta
->rta_len
);
2308 rta
= RTA_NEXT(rta
, rtasize
);
2311 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI 0x%x", spi
);
2317 * Implementation of kernel_interface_t.update_sa.
2319 static status_t
update_sa(private_kernel_interface_t
*this,
2320 u_int32_t spi
, protocol_id_t protocol
,
2321 host_t
*src
, host_t
*dst
,
2322 host_t
*new_src
, host_t
*new_dst
, bool encap
)
2324 unsigned char request
[BUFFER_SIZE
], *pos
;
2325 struct nlmsghdr
*hdr
, *out
= NULL
;
2326 struct xfrm_usersa_id
*sa_id
;
2327 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
2331 struct xfrm_encap_tmpl
* tmpl
= NULL
;
2332 bool got_replay_state
;
2333 struct xfrm_replay_state replay
;
2335 memset(&request
, 0, sizeof(request
));
2337 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x for update", spi
);
2339 /* query the exisiting SA first */
2340 hdr
= (struct nlmsghdr
*)request
;
2341 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2342 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2343 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2345 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2346 host2xfrm(dst
, &sa_id
->daddr
);
2348 sa_id
->proto
= proto_ike2kernel(protocol
);
2349 sa_id
->family
= dst
->get_family(dst
);
2351 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2354 while (NLMSG_OK(hdr
, len
))
2356 switch (hdr
->nlmsg_type
)
2358 case XFRM_MSG_NEWSA
:
2360 out_sa
= NLMSG_DATA(hdr
);
2365 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2366 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2367 strerror(-err
->error
), -err
->error
);
2371 hdr
= NLMSG_NEXT(hdr
, len
);
2381 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
2386 /* try to get the replay state */
2387 got_replay_state
= (get_replay_state(
2388 this, spi
, protocol
, dst
, &replay
) == SUCCESS
);
2390 /* delete the old SA */
2391 if (this->public.del_sa(&this->public, dst
, spi
, protocol
) != SUCCESS
)
2393 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI 0x%x", spi
);
2398 DBG2(DBG_KNL
, "updating SAD entry with SPI 0x%x from %#H..%#H to %#H..%#H",
2399 spi
, src
, dst
, new_src
, new_dst
);
2401 /* copy over the SA from out to request */
2402 hdr
= (struct nlmsghdr
*)request
;
2403 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
2404 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2405 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2406 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2407 sa
= NLMSG_DATA(hdr
);
2408 sa
->family
= new_dst
->get_family(new_dst
);
2410 if (!src
->ip_equals(src
, new_src
))
2412 host2xfrm(new_src
, &sa
->saddr
);
2414 if (!dst
->ip_equals(dst
, new_dst
))
2416 host2xfrm(new_dst
, &sa
->id
.daddr
);
2419 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
2420 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
2421 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
2422 while(RTA_OK(rta
, rtasize
))
2424 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2425 if (rta
->rta_type
!= XFRMA_ENCAP
|| encap
)
2427 if (rta
->rta_type
== XFRMA_ENCAP
)
2428 { /* update encap tmpl */
2429 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
2430 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
2431 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
2433 memcpy(pos
, rta
, rta
->rta_len
);
2434 pos
+= RTA_ALIGN(rta
->rta_len
);
2435 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
2437 rta
= RTA_NEXT(rta
, rtasize
);
2440 rta
= (struct rtattr
*)pos
;
2441 if (tmpl
== NULL
&& encap
)
2442 { /* add tmpl if we are enabling it */
2443 rta
->rta_type
= XFRMA_ENCAP
;
2444 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
2446 hdr
->nlmsg_len
+= rta
->rta_len
;
2447 if (hdr
->nlmsg_len
> sizeof(request
))
2452 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
2453 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2454 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
2455 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
2456 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2458 rta
= XFRM_RTA_NEXT(rta
);
2461 if (got_replay_state
)
2462 { /* copy the replay data if available */
2463 rta
->rta_type
= XFRMA_REPLAY_VAL
;
2464 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
2466 hdr
->nlmsg_len
+= rta
->rta_len
;
2467 if (hdr
->nlmsg_len
> sizeof(request
))
2471 memcpy(RTA_DATA(rta
), &replay
, sizeof(replay
));
2473 rta
= XFRM_RTA_NEXT(rta
);
2476 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2478 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
2488 * Implementation of kernel_interface_t.query_sa.
2490 static status_t
query_sa(private_kernel_interface_t
*this, host_t
*dst
,
2491 u_int32_t spi
, protocol_id_t protocol
,
2492 u_int32_t
*use_time
)
2494 unsigned char request
[BUFFER_SIZE
];
2495 struct nlmsghdr
*out
= NULL
, *hdr
;
2496 struct xfrm_usersa_id
*sa_id
;
2497 struct xfrm_usersa_info
*sa
= NULL
;
2500 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x", spi
);
2501 memset(&request
, 0, sizeof(request
));
2503 hdr
= (struct nlmsghdr
*)request
;
2504 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2505 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2506 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2508 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2509 host2xfrm(dst
, &sa_id
->daddr
);
2511 sa_id
->proto
= proto_ike2kernel(protocol
);
2512 sa_id
->family
= dst
->get_family(dst
);
2514 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2517 while (NLMSG_OK(hdr
, len
))
2519 switch (hdr
->nlmsg_type
)
2521 case XFRM_MSG_NEWSA
:
2523 sa
= NLMSG_DATA(hdr
);
2528 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2529 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2530 strerror(-err
->error
), -err
->error
);
2534 hdr
= NLMSG_NEXT(hdr
, len
);
2545 DBG1(DBG_KNL
, "unable to query SAD entry with SPI 0x%x", spi
);
2550 *use_time
= sa
->curlft
.use_time
;
2556 * Implementation of kernel_interface_t.del_sa.
2558 static status_t
del_sa(private_kernel_interface_t
*this, host_t
*dst
,
2559 u_int32_t spi
, protocol_id_t protocol
)
2561 unsigned char request
[BUFFER_SIZE
];
2562 struct nlmsghdr
*hdr
;
2563 struct xfrm_usersa_id
*sa_id
;
2565 memset(&request
, 0, sizeof(request
));
2567 DBG2(DBG_KNL
, "deleting SAD entry with SPI 0x%x", spi
);
2569 hdr
= (struct nlmsghdr
*)request
;
2570 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2571 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
2572 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2574 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2575 host2xfrm(dst
, &sa_id
->daddr
);
2577 sa_id
->proto
= proto_ike2kernel(protocol
);
2578 sa_id
->family
= dst
->get_family(dst
);
2580 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2582 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI 0x%x", spi
);
2585 DBG2(DBG_KNL
, "deleted SAD entry with SPI 0x%x", spi
);
2590 * Implementation of kernel_interface_t.add_policy.
2592 static status_t
add_policy(private_kernel_interface_t
*this,
2593 host_t
*src
, host_t
*dst
,
2594 traffic_selector_t
*src_ts
,
2595 traffic_selector_t
*dst_ts
,
2596 policy_dir_t direction
, protocol_id_t protocol
,
2597 u_int32_t reqid
, bool high_prio
, mode_t mode
,
2600 iterator_t
*iterator
;
2601 policy_entry_t
*current
, *policy
;
2603 unsigned char request
[BUFFER_SIZE
];
2604 struct xfrm_userpolicy_info
*policy_info
;
2605 struct nlmsghdr
*hdr
;
2607 /* create a policy */
2608 policy
= malloc_thing(policy_entry_t
);
2609 memset(policy
, 0, sizeof(policy_entry_t
));
2610 policy
->sel
= ts2selector(src_ts
, dst_ts
);
2611 policy
->direction
= direction
;
2613 /* find the policy, which matches EXACTLY */
2614 pthread_mutex_lock(&this->mutex
);
2615 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
2616 while (iterator
->iterate(iterator
, (void**)¤t
))
2618 if (memcmp(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) == 0 &&
2619 policy
->direction
== current
->direction
)
2621 /* use existing policy */
2622 current
->refcount
++;
2623 DBG2(DBG_KNL
, "policy %R===%R already exists, increasing "
2624 "refcount", src_ts
, dst_ts
);
2631 iterator
->destroy(iterator
);
2633 { /* apply the new one, if we have no such policy */
2634 this->policies
->insert_last(this->policies
, policy
);
2635 policy
->refcount
= 1;
2638 DBG2(DBG_KNL
, "adding policy %R===%R", src_ts
, dst_ts
);
2640 memset(&request
, 0, sizeof(request
));
2641 hdr
= (struct nlmsghdr
*)request
;
2642 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2643 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
2644 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2646 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2647 policy_info
->sel
= policy
->sel
;
2648 policy_info
->dir
= policy
->direction
;
2649 /* calculate priority based on source selector size, small size = high prio */
2650 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
2651 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
2652 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
2653 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
2654 policy_info
->action
= XFRM_POLICY_ALLOW
;
2655 policy_info
->share
= XFRM_SHARE_ANY
;
2656 pthread_mutex_unlock(&this->mutex
);
2658 /* policies don't expire */
2659 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2660 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2661 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2662 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2663 policy_info
->lft
.soft_add_expires_seconds
= 0;
2664 policy_info
->lft
.hard_add_expires_seconds
= 0;
2665 policy_info
->lft
.soft_use_expires_seconds
= 0;
2666 policy_info
->lft
.hard_use_expires_seconds
= 0;
2668 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2669 rthdr
->rta_type
= XFRMA_TMPL
;
2670 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2672 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2673 if (hdr
->nlmsg_len
> sizeof(request
))
2678 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2680 if (ipcomp
!= IPCOMP_NONE
)
2682 tmpl
->reqid
= reqid
;
2683 tmpl
->id
.proto
= IPPROTO_COMP
;
2684 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2686 tmpl
->optional
= direction
!= POLICY_OUT
;
2687 tmpl
->family
= src
->get_family(src
);
2689 host2xfrm(src
, &tmpl
->saddr
);
2690 host2xfrm(dst
, &tmpl
->id
.daddr
);
2692 /* add an additional xfrm_user_tmpl */
2693 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2694 hdr
->nlmsg_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2695 if (hdr
->nlmsg_len
> sizeof(request
))
2703 tmpl
->reqid
= reqid
;
2704 tmpl
->id
.proto
= proto_ike2kernel(protocol
);
2705 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2707 tmpl
->family
= src
->get_family(src
);
2709 host2xfrm(src
, &tmpl
->saddr
);
2710 host2xfrm(dst
, &tmpl
->id
.daddr
);
2712 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2714 DBG1(DBG_KNL
, "unable to add policy %R===%R", src_ts
, dst_ts
);
2718 /* install a route, if:
2719 * - we are NOT updating a policy
2720 * - this is a forward policy (to just get one for each child)
2721 * - we are in tunnel mode
2722 * - we are not using IPv6 (does not work correctly yet!)
2723 * - routing is not disabled via strongswan.conf
2725 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
2726 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
2727 this->install_routes
)
2729 policy
->route
= malloc_thing(route_entry_t
);
2730 if (get_address_by_ts(this, dst_ts
, &policy
->route
->src_ip
) == SUCCESS
)
2732 /* get the nexthop to src (src as we are in POLICY_FWD).*/
2733 policy
->route
->gateway
= get_route(this, src
, TRUE
);
2734 policy
->route
->if_index
= get_interface_index(this, dst
);
2735 policy
->route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2736 memcpy(policy
->route
->dst_net
.ptr
, &policy
->sel
.saddr
, policy
->route
->dst_net
.len
);
2737 policy
->route
->prefixlen
= policy
->sel
.prefixlen_s
;
2739 if (manage_srcroute(this, RTM_NEWROUTE
, NLM_F_CREATE
| NLM_F_EXCL
,
2740 policy
->route
) != SUCCESS
)
2742 DBG1(DBG_KNL
, "unable to install source route for %H",
2743 policy
->route
->src_ip
);
2744 route_entry_destroy(policy
->route
);
2745 policy
->route
= NULL
;
2750 free(policy
->route
);
2751 policy
->route
= NULL
;
2759 * Implementation of kernel_interface_t.query_policy.
2761 static status_t
query_policy(private_kernel_interface_t
*this,
2762 traffic_selector_t
*src_ts
,
2763 traffic_selector_t
*dst_ts
,
2764 policy_dir_t direction
, u_int32_t
*use_time
)
2766 unsigned char request
[BUFFER_SIZE
];
2767 struct nlmsghdr
*out
= NULL
, *hdr
;
2768 struct xfrm_userpolicy_id
*policy_id
;
2769 struct xfrm_userpolicy_info
*policy
= NULL
;
2772 memset(&request
, 0, sizeof(request
));
2774 DBG2(DBG_KNL
, "querying policy %R===%R", src_ts
, dst_ts
);
2776 hdr
= (struct nlmsghdr
*)request
;
2777 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2778 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2779 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2781 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2782 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2783 policy_id
->dir
= direction
;
2785 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2788 while (NLMSG_OK(hdr
, len
))
2790 switch (hdr
->nlmsg_type
)
2792 case XFRM_MSG_NEWPOLICY
:
2794 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2799 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2800 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2801 strerror(-err
->error
), -err
->error
);
2805 hdr
= NLMSG_NEXT(hdr
, len
);
2816 DBG2(DBG_KNL
, "unable to query policy %R===%R", src_ts
, dst_ts
);
2820 *use_time
= (time_t)policy
->curlft
.use_time
;
2827 * Implementation of kernel_interface_t.del_policy.
2829 static status_t
del_policy(private_kernel_interface_t
*this,
2830 traffic_selector_t
*src_ts
,
2831 traffic_selector_t
*dst_ts
,
2832 policy_dir_t direction
)
2834 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
2835 route_entry_t
*route
;
2836 unsigned char request
[BUFFER_SIZE
];
2837 struct nlmsghdr
*hdr
;
2838 struct xfrm_userpolicy_id
*policy_id
;
2839 iterator_t
*iterator
;
2841 DBG2(DBG_KNL
, "deleting policy %R===%R", src_ts
, dst_ts
);
2843 /* create a policy */
2844 memset(&policy
, 0, sizeof(policy_entry_t
));
2845 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2846 policy
.direction
= direction
;
2848 /* find the policy */
2849 iterator
= this->policies
->create_iterator_locked(this->policies
, &this->mutex
);
2850 while (iterator
->iterate(iterator
, (void**)¤t
))
2852 if (memcmp(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) == 0 &&
2853 policy
.direction
== current
->direction
)
2855 to_delete
= current
;
2856 if (--to_delete
->refcount
> 0)
2858 /* is used by more SAs, keep in kernel */
2859 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2860 iterator
->destroy(iterator
);
2863 /* remove if last reference */
2864 iterator
->remove(iterator
);
2868 iterator
->destroy(iterator
);
2871 DBG1(DBG_KNL
, "deleting policy %R===%R failed, not found", src_ts
, dst_ts
);
2875 memset(&request
, 0, sizeof(request
));
2877 hdr
= (struct nlmsghdr
*)request
;
2878 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2879 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2880 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2882 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2883 policy_id
->sel
= to_delete
->sel
;
2884 policy_id
->dir
= direction
;
2886 route
= to_delete
->route
;
2889 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2891 DBG1(DBG_KNL
, "unable to delete policy %R===%R", src_ts
, dst_ts
);
2897 if (manage_srcroute(this, RTM_DELROUTE
, 0, route
) != SUCCESS
)
2899 DBG1(DBG_KNL
, "error uninstalling route installed with "
2900 "policy %R===%R", src_ts
, dst_ts
);
2902 route_entry_destroy(route
);
2908 * Implementation of kernel_interface_t.destroy.
2910 static void destroy(private_kernel_interface_t
*this)
2912 if (this->routing_table
)
2914 manage_rule(this, RTM_DELRULE
, this->routing_table
,
2915 this->routing_table_prio
);
2918 this->job
->cancel(this->job
);
2919 close(this->socket_xfrm_events
);
2920 close(this->socket_xfrm
);
2921 close(this->socket_rt_events
);
2922 close(this->socket_rt
);
2923 this->policies
->destroy(this->policies
);
2924 this->ifaces
->destroy_function(this->ifaces
, (void*)iface_entry_destroy
);
2929 * Described in header.
2931 kernel_interface_t
*kernel_interface_create()
2933 private_kernel_interface_t
*this = malloc_thing(private_kernel_interface_t
);
2934 struct sockaddr_nl addr
;
2936 /* public functions */
2937 this->public.get_spi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
2938 this->public.get_cpi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
2939 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
;
2940 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
;
2941 this->public.query_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
*))query_sa
;
2942 this->public.del_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
2943 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
;
2944 this->public.query_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
2945 this->public.del_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
2946 this->public.get_interface
= (char*(*)(kernel_interface_t
*,host_t
*))get_interface_name
;
2947 this->public.create_address_iterator
= (iterator_t
*(*)(kernel_interface_t
*))create_address_iterator
;
2948 this->public.get_source_addr
= (host_t
*(*)(kernel_interface_t
*, host_t
*dest
))get_source_addr
;
2949 this->public.add_ip
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*)) add_ip
;
2950 this->public.del_ip
= (status_t(*)(kernel_interface_t
*,host_t
*)) del_ip
;
2951 this->public.destroy
= (void(*)(kernel_interface_t
*)) destroy
;
2953 /* private members */
2954 this->policies
= linked_list_create();
2955 this->ifaces
= linked_list_create();
2958 pthread_mutex_init(&this->mutex
, NULL
);
2959 pthread_mutex_init(&this->nl_mutex
, NULL
);
2960 pthread_cond_init(&this->cond
, NULL
);
2961 timerclear(&this->last_roam
);
2962 this->install_routes
= lib
->settings
->get_bool(lib
->settings
,
2963 "charon.install_routes", TRUE
);
2964 this->routing_table
= lib
->settings
->get_int(lib
->settings
,
2965 "charon.routing_table", IPSEC_ROUTING_TABLE
);
2966 this->routing_table_prio
= lib
->settings
->get_int(lib
->settings
,
2967 "charon.routing_table_prio", IPSEC_ROUTING_TABLE_PRIO
);
2968 memset(&addr
, 0, sizeof(addr
));
2969 addr
.nl_family
= AF_NETLINK
;
2971 /* create and bind RT socket */
2972 this->socket_rt
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2973 if (this->socket_rt
<= 0)
2975 charon
->kill(charon
, "unable to create RT netlink socket");
2978 if (bind(this->socket_rt
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2980 charon
->kill(charon
, "unable to bind RT netlink socket");
2983 /* create and bind RT socket for events (address/interface/route changes) */
2984 this->socket_rt_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2985 if (this->socket_rt_events
<= 0)
2987 charon
->kill(charon
, "unable to create RT event socket");
2989 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR
|
2990 RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_ROUTE
| RTMGRP_LINK
;
2991 if (bind(this->socket_rt_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2993 charon
->kill(charon
, "unable to bind RT event socket");
2996 /* create and bind XFRM socket */
2997 this->socket_xfrm
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2998 if (this->socket_xfrm
<= 0)
3000 charon
->kill(charon
, "unable to create XFRM netlink socket");
3003 if (bind(this->socket_xfrm
, (struct sockaddr
*)&addr
, sizeof(addr
)))