2 * @file kernel_interface.c
4 * @brief Implementation of kernel_interface_t.
9 * Copyright (C) 2005-2007 Martin Willi
10 * Copyright (C) 2006-2007 Tobias Brunner
11 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
12 * Copyright (C) 2006 Daniel Roethlisberger
13 * Copyright (C) 2005 Jan Hutter
14 * Hochschule fuer Technik Rapperswil
15 * Copyright (C) 2003 Herbert Xu.
17 * Based on xfrm code from pluto.
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the
21 * Free Software Foundation; either version 2 of the License, or (at your
22 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/xfrm.h>
36 #include <linux/udp.h>
44 #include <sys/ioctl.h>
46 #include "kernel_interface.h"
49 #include <utils/linked_list.h>
50 #include <processing/jobs/delete_child_sa_job.h>
51 #include <processing/jobs/rekey_child_sa_job.h>
52 #include <processing/jobs/acquire_job.h>
53 #include <processing/jobs/callback_job.h>
54 #include <processing/jobs/roam_job.h>
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 /** kernel level protocol identifiers */
68 /** default priority of installed policies */
70 #define PRIO_HIGH 2000
72 /** delay before firing roam jobs (ms) */
73 #define ROAM_DELAY 100
75 #define BUFFER_SIZE 1024
78 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
79 * 'usual' netlink data x like 'struct xfrm_usersa_info'
81 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
83 * returns a pointer to the next rtattr following rta.
84 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
86 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
88 * returns the total size of attached rta data
89 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
91 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
93 typedef struct kernel_algorithm_t kernel_algorithm_t
;
96 * Mapping from the algorithms defined in IKEv2 to
97 * kernel level algorithm names and their key length
99 struct kernel_algorithm_t
{
101 * Identifier specified in IKEv2
106 * Name of the algorithm, as used as kernel identifier
111 * Key length in bits, if fixed size
115 #define END_OF_LIST -1
118 * Algorithms for encryption
120 kernel_algorithm_t encryption_algs
[] = {
121 /* {ENCR_DES_IV64, "***", 0}, */
122 {ENCR_DES
, "des", 64},
123 {ENCR_3DES
, "des3_ede", 192},
124 /* {ENCR_RC5, "***", 0}, */
125 /* {ENCR_IDEA, "***", 0}, */
126 {ENCR_CAST
, "cast128", 0},
127 {ENCR_BLOWFISH
, "blowfish", 0},
128 /* {ENCR_3IDEA, "***", 0}, */
129 /* {ENCR_DES_IV32, "***", 0}, */
130 {ENCR_NULL
, "cipher_null", 0},
131 {ENCR_AES_CBC
, "aes", 0},
132 /* {ENCR_AES_CTR, "***", 0}, */
133 {END_OF_LIST
, NULL
, 0},
137 * Algorithms for integrity protection
139 kernel_algorithm_t integrity_algs
[] = {
140 {AUTH_HMAC_MD5_96
, "md5", 128},
141 {AUTH_HMAC_SHA1_96
, "sha1", 160},
142 {AUTH_HMAC_SHA2_256_128
, "sha256", 256},
143 {AUTH_HMAC_SHA2_384_192
, "sha384", 384},
144 {AUTH_HMAC_SHA2_512_256
, "sha512", 512},
145 /* {AUTH_DES_MAC, "***", 0}, */
146 /* {AUTH_KPDK_MD5, "***", 0}, */
147 {AUTH_AES_XCBC_96
, "xcbc(aes)", 128},
148 {END_OF_LIST
, NULL
, 0},
152 * Look up a kernel algorithm name and its key size
154 char* lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
155 algorithm_t
*ikev2_algo
, u_int
*key_size
)
157 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
159 if (ikev2_algo
->algorithm
== kernel_algo
->ikev2_id
)
161 /* match, evaluate key length */
162 if (ikev2_algo
->key_size
)
163 { /* variable length */
164 *key_size
= ikev2_algo
->key_size
;
168 *key_size
= kernel_algo
->key_size
;
170 return kernel_algo
->name
;
177 typedef struct route_entry_t route_entry_t
;
180 * installed routing entry
182 struct route_entry_t
{
184 /** Index of the interface the route is bound to */
187 /** Source ip of the route */
190 /** gateway for this route */
193 /** Destination net */
196 /** Destination net prefixlen */
201 * destroy an route_entry_t object
203 static void route_entry_destroy(route_entry_t
*this)
205 this->src_ip
->destroy(this->src_ip
);
206 this->gateway
->destroy(this->gateway
);
207 chunk_free(&this->dst_net
);
211 typedef struct policy_entry_t policy_entry_t
;
214 * installed kernel policy.
216 struct policy_entry_t
{
218 /** direction of this policy: in, out, forward */
221 /** reqid of the policy */
224 /** parameters of installed policy */
225 struct xfrm_selector sel
;
227 /** associated route installed for this policy */
228 route_entry_t
*route
;
230 /** by how many CHILD_SA's this policy is used */
234 typedef struct addr_entry_t addr_entry_t
;
237 * IP address in an inface_entry_t
239 struct addr_entry_t
{
241 /** The ip address */
244 /** virtual IP managed by us */
247 /** scope of the address */
250 /** Number of times this IP is used, if virtual */
255 * destroy a addr_entry_t object
257 static void addr_entry_destroy(addr_entry_t
*this)
259 this->ip
->destroy(this->ip
);
263 typedef struct iface_entry_t iface_entry_t
;
266 * A network interface on this system, containing addr_entry_t's
268 struct iface_entry_t
{
270 /** interface index */
273 /** name of the interface */
274 char ifname
[IFNAMSIZ
];
276 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
279 /** list of addresses as host_t */
280 linked_list_t
*addrs
;
284 * destroy an interface entry
286 static void iface_entry_destroy(iface_entry_t
*this)
288 this->addrs
->destroy_function(this->addrs
, (void*)addr_entry_destroy
);
292 typedef struct private_kernel_interface_t private_kernel_interface_t
;
295 * Private variables and functions of kernel_interface class.
297 struct private_kernel_interface_t
{
299 * Public part of the kernel_interface_t object.
301 kernel_interface_t
public;
304 * mutex to lock access to the various lists
306 pthread_mutex_t mutex
;
309 * condition variable to signal virtual IP add/removal
314 * List of installed policies (policy_entry_t)
316 linked_list_t
*policies
;
319 * Cached list of interfaces and its adresses (iface_entry_t)
321 linked_list_t
*ifaces
;
324 * iterator used in hook()
329 * job receiving netlink events
334 * current sequence number for netlink request
339 * Netlink xfrm socket (IPsec)
344 * netlink xfrm socket to receive acquire and expire events
346 int socket_xfrm_events
;
349 * Netlink rt socket (routing)
354 * Netlink rt socket to receive address change events
356 int socket_rt_events
;
359 * time of the last roam_job
361 struct timeval last_roam
;
365 * convert a host_t to a struct xfrm_address
367 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
369 chunk_t chunk
= host
->get_address(host
);
370 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
374 * convert a traffic selector address range to subnet and its mask.
376 static void ts2subnet(traffic_selector_t
* ts
,
377 xfrm_address_t
*net
, u_int8_t
*mask
)
379 /* there is no way to do this cleanly, as the address range may
380 * be anything else but a subnet. We use from_addr as subnet
381 * and try to calculate a usable subnet mask.
386 size_t size
= (ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE
) ?
4 : 16;
388 from
= ts
->get_from_address(ts
);
389 to
= ts
->get_to_address(ts
);
392 /* go trough all bits of the addresses, beginning in the front.
393 * as long as they are equal, the subnet gets larger
395 for (byte
= 0; byte
< size
; byte
++)
397 for (bit
= 7; bit
>= 0; bit
--)
399 if ((1<<bit
& from
.ptr
[byte
]) != (1<<bit
& to
.ptr
[byte
]))
401 *mask
= ((7 - bit
) + (byte
* 8));
411 memcpy(net
, from
.ptr
, from
.len
);
417 * convert a traffic selector port range to port/portmask
419 static void ts2ports(traffic_selector_t
* ts
,
420 u_int16_t
*port
, u_int16_t
*mask
)
422 /* linux does not seem to accept complex portmasks. Only
423 * any or a specific port is allowed. We set to any, if we have
424 * a port range, or to a specific, if we have one port only.
428 from
= ts
->get_from_port(ts
);
429 to
= ts
->get_to_port(ts
);
444 * convert a pair of traffic_selectors to a xfrm_selector
446 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
447 traffic_selector_t
*dst
)
449 struct xfrm_selector sel
;
451 memset(&sel
, 0, sizeof(sel
));
452 sel
.family
= src
->get_type(src
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
453 /* src or dest proto may be "any" (0), use more restrictive one */
454 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
455 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
456 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
457 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
458 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
466 * Creates an rtattr and adds it to the netlink message
468 static void add_attribute(struct nlmsghdr
*hdr
, int rta_type
, chunk_t data
,
473 if (NLMSG_ALIGN(hdr
->nlmsg_len
) + RTA_ALIGN(data
.len
) > buflen
)
475 DBG1(DBG_KNL
, "unable to add attribute, buffer too small");
479 rta
= (struct rtattr
*)(((char*)hdr
) + NLMSG_ALIGN(hdr
->nlmsg_len
));
480 rta
->rta_type
= rta_type
;
481 rta
->rta_len
= RTA_LENGTH(data
.len
);
482 memcpy(RTA_DATA(rta
), data
.ptr
, data
.len
);
483 hdr
->nlmsg_len
= NLMSG_ALIGN(hdr
->nlmsg_len
) + rta
->rta_len
;
487 * process a XFRM_MSG_ACQUIRE from kernel
489 static void process_acquire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
493 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
494 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
496 if (RTA_OK(rtattr
, rtsize
))
498 if (rtattr
->rta_type
== XFRMA_TMPL
)
500 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rtattr
);
506 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
509 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
510 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid %d", reqid
);
511 job
= (job_t
*)acquire_job_create(reqid
);
512 charon
->processor
->queue_job(charon
->processor
, job
);
516 * process a XFRM_MSG_EXPIRE from kernel
518 static void process_expire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
521 protocol_id_t protocol
;
522 u_int32_t spi
, reqid
;
523 struct xfrm_user_expire
*expire
;
525 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
526 protocol
= expire
->state
.id
.proto
== KERNEL_ESP ? PROTO_ESP
: PROTO_AH
;
527 spi
= expire
->state
.id
.spi
;
528 reqid
= expire
->state
.reqid
;
530 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
531 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
532 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
533 protocol
, ntohl(spi
), reqid
);
536 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
540 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
542 charon
->processor
->queue_job(charon
->processor
, job
);
546 * start a roaming job. We delay it for a second and fire only one job
547 * for multiple events. Otherwise we would create two many jobs.
549 static void fire_roam_job(private_kernel_interface_t
*this, bool address
)
553 if (gettimeofday(&now
, NULL
) == 0)
555 if (timercmp(&now
, &this->last_roam
, >))
557 now
.tv_usec
+= ROAM_DELAY
* 1000;
558 while (now
.tv_usec
> 1000000)
561 now
.tv_usec
-= 1000000;
563 this->last_roam
= now
;
564 charon
->scheduler
->schedule_job(charon
->scheduler
,
565 (job_t
*)roam_job_create(address
), ROAM_DELAY
);
571 * process RTM_NEWLINK/RTM_DELLINK from kernel
573 static void process_link(private_kernel_interface_t
*this,
574 struct nlmsghdr
*hdr
, bool event
)
576 struct ifinfomsg
* msg
= (struct ifinfomsg
*)(NLMSG_DATA(hdr
));
577 struct rtattr
*rta
= IFLA_RTA(msg
);
578 size_t rtasize
= IFLA_PAYLOAD (hdr
);
579 iterator_t
*iterator
;
580 iface_entry_t
*current
, *entry
= NULL
;
584 while(RTA_OK(rta
, rtasize
))
586 switch (rta
->rta_type
)
589 name
= RTA_DATA(rta
);
592 rta
= RTA_NEXT(rta
, rtasize
);
599 switch (hdr
->nlmsg_type
)
603 if (msg
->ifi_flags
& IFF_LOOPBACK
)
604 { /* ignore loopback interfaces */
607 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
609 while (iterator
->iterate(iterator
, (void**)¤t
))
611 if (current
->ifindex
== msg
->ifi_index
)
619 entry
= malloc_thing(iface_entry_t
);
620 entry
->ifindex
= msg
->ifi_index
;
622 entry
->addrs
= linked_list_create();
623 this->ifaces
->insert_last(this->ifaces
, entry
);
625 memcpy(entry
->ifname
, name
, IFNAMSIZ
);
626 entry
->ifname
[IFNAMSIZ
-1] = '\0';
629 if (!(entry
->flags
& IFF_UP
) && (msg
->ifi_flags
& IFF_UP
))
632 DBG1(DBG_KNL
, "interface %s activated", name
);
634 if ((entry
->flags
& IFF_UP
) && !(msg
->ifi_flags
& IFF_UP
))
637 DBG1(DBG_KNL
, "interface %s deactivated", name
);
640 entry
->flags
= msg
->ifi_flags
;
641 iterator
->destroy(iterator
);
646 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
648 while (iterator
->iterate(iterator
, (void**)¤t
))
650 if (current
->ifindex
== msg
->ifi_index
)
652 /* we do not remove it, as an address may be added to a
653 * "down" interface and we wan't to know that. */
654 current
->flags
= msg
->ifi_flags
;
658 iterator
->destroy(iterator
);
663 /* send an update to all IKE_SAs */
666 fire_roam_job(this, TRUE
);
671 * process RTM_NEWADDR/RTM_DELADDR from kernel
673 static void process_addr(private_kernel_interface_t
*this,
674 struct nlmsghdr
*hdr
, bool event
)
676 struct ifaddrmsg
* msg
= (struct ifaddrmsg
*)(NLMSG_DATA(hdr
));
677 struct rtattr
*rta
= IFA_RTA(msg
);
678 size_t rtasize
= IFA_PAYLOAD (hdr
);
680 iterator_t
*ifaces
, *addrs
;
681 iface_entry_t
*iface
;
683 chunk_t local
= chunk_empty
, address
= chunk_empty
;
684 bool update
= FALSE
, found
= FALSE
, changed
= FALSE
;
686 while(RTA_OK(rta
, rtasize
))
688 switch (rta
->rta_type
)
691 local
.ptr
= RTA_DATA(rta
);
692 local
.len
= RTA_PAYLOAD(rta
);
695 address
.ptr
= RTA_DATA(rta
);
696 address
.len
= RTA_PAYLOAD(rta
);
699 rta
= RTA_NEXT(rta
, rtasize
);
702 /* For PPP interfaces, we need the IFA_LOCAL address,
703 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
704 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
707 host
= host_create_from_chunk(msg
->ifa_family
, local
, 0);
709 else if (address
.ptr
)
711 host
= host_create_from_chunk(msg
->ifa_family
, address
, 0);
719 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
720 while (ifaces
->iterate(ifaces
, (void**)&iface
))
722 if (iface
->ifindex
== msg
->ifa_index
)
724 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
725 while (addrs
->iterate(addrs
, (void**)&addr
))
727 if (host
->ip_equals(host
, addr
->ip
))
730 if (hdr
->nlmsg_type
== RTM_DELADDR
)
733 addrs
->remove(addrs
);
736 DBG1(DBG_KNL
, "%H disappeared from %s",
737 host
, iface
->ifname
);
739 addr_entry_destroy(addr
);
741 else if (hdr
->nlmsg_type
== RTM_NEWADDR
&& addr
->virtual)
747 addrs
->destroy(addrs
);
749 if (hdr
->nlmsg_type
== RTM_NEWADDR
)
755 addr
= malloc_thing(addr_entry_t
);
756 addr
->ip
= host
->clone(host
);
757 addr
->virtual = FALSE
;
759 addr
->scope
= msg
->ifa_scope
;
761 iface
->addrs
->insert_last(iface
->addrs
, addr
);
764 DBG1(DBG_KNL
, "%H appeared on %s", host
, iface
->ifname
);
768 if (found
&& (iface
->flags
& IFF_UP
))
775 ifaces
->destroy(ifaces
);
778 /* send an update to all IKE_SAs */
779 if (update
&& event
&& changed
)
781 fire_roam_job(this, TRUE
);
786 * Receives events from kernel
788 static job_requeue_t
receive_events(private_kernel_interface_t
*this)
791 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
792 struct sockaddr_nl addr
;
793 socklen_t addr_len
= sizeof(addr
);
794 int len
, oldstate
, maxfd
, selected
;
798 FD_SET(this->socket_xfrm_events
, &rfds
);
799 FD_SET(this->socket_rt_events
, &rfds
);
800 maxfd
= max(this->socket_xfrm_events
, this->socket_rt_events
);
802 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
803 selected
= select(maxfd
+ 1, &rfds
, NULL
, NULL
, NULL
);
804 pthread_setcancelstate(oldstate
, NULL
);
807 DBG1(DBG_KNL
, "selecting on sockets failed: %s", strerror(errno
));
808 return JOB_REQUEUE_FAIR
;
810 if (FD_ISSET(this->socket_xfrm_events
, &rfds
))
812 selected
= this->socket_xfrm_events
;
814 else if (FD_ISSET(this->socket_rt_events
, &rfds
))
816 selected
= this->socket_rt_events
;
820 return JOB_REQUEUE_DIRECT
;
823 len
= recvfrom(selected
, response
, sizeof(response
), MSG_DONTWAIT
,
824 (struct sockaddr
*)&addr
, &addr_len
);
830 /* interrupted, try again */
831 return JOB_REQUEUE_DIRECT
;
833 /* no data ready, select again */
834 return JOB_REQUEUE_DIRECT
;
836 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
838 return JOB_REQUEUE_FAIR
;
841 if (addr
.nl_pid
!= 0)
842 { /* not from kernel. not interested, try another one */
843 return JOB_REQUEUE_DIRECT
;
846 while (NLMSG_OK(hdr
, len
))
848 /* looks good so far, dispatch netlink message */
849 if (selected
== this->socket_xfrm_events
)
851 switch (hdr
->nlmsg_type
)
853 case XFRM_MSG_ACQUIRE
:
854 process_acquire(this, hdr
);
856 case XFRM_MSG_EXPIRE
:
857 process_expire(this, hdr
);
863 else if (selected
== this->socket_rt_events
)
865 switch (hdr
->nlmsg_type
)
869 process_addr(this, hdr
, TRUE
);
870 pthread_cond_signal(&this->cond
);
874 process_link(this, hdr
, TRUE
);
875 pthread_cond_signal(&this->cond
);
879 fire_roam_job(this, FALSE
);
885 hdr
= NLMSG_NEXT(hdr
, len
);
887 return JOB_REQUEUE_DIRECT
;
891 * send a netlink message and wait for a reply
893 static status_t
netlink_send(private_kernel_interface_t
*this,
894 int socket
, struct nlmsghdr
*in
,
895 struct nlmsghdr
**out
, size_t *out_len
)
898 struct sockaddr_nl addr
;
899 chunk_t result
= chunk_empty
, tmp
;
900 struct nlmsghdr
*msg
, peek
;
902 pthread_mutex_lock(&this->mutex
);
904 in
->nlmsg_seq
= ++this->seq
;
905 in
->nlmsg_pid
= getpid();
907 memset(&addr
, 0, sizeof(addr
));
908 addr
.nl_family
= AF_NETLINK
;
914 len
= sendto(socket
, in
, in
->nlmsg_len
, 0,
915 (struct sockaddr
*)&addr
, sizeof(addr
));
917 if (len
!= in
->nlmsg_len
)
921 /* interrupted, try again */
924 pthread_mutex_unlock(&this->mutex
);
925 DBG1(DBG_KNL
, "error sending to netlink socket: %s", strerror(errno
));
934 tmp
.len
= sizeof(buf
);
936 msg
= (struct nlmsghdr
*)tmp
.ptr
;
938 memset(&addr
, 0, sizeof(addr
));
939 addr
.nl_family
= AF_NETLINK
;
940 addr
.nl_pid
= getpid();
942 addr_len
= sizeof(addr
);
944 len
= recvfrom(socket
, tmp
.ptr
, tmp
.len
, 0,
945 (struct sockaddr
*)&addr
, &addr_len
);
951 DBG1(DBG_KNL
, "got interrupted");
952 /* interrupted, try again */
955 DBG1(DBG_KNL
, "error reading from netlink socket: %s", strerror(errno
));
956 pthread_mutex_unlock(&this->mutex
);
959 if (!NLMSG_OK(msg
, len
))
961 DBG1(DBG_KNL
, "received corrupted netlink message");
962 pthread_mutex_unlock(&this->mutex
);
965 if (msg
->nlmsg_seq
!= this->seq
)
967 DBG1(DBG_KNL
, "received invalid netlink sequence number");
968 if (msg
->nlmsg_seq
< this->seq
)
972 pthread_mutex_unlock(&this->mutex
);
977 result
= chunk_cata("cc", result
, tmp
);
979 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
980 * numbers to detect multi header messages */
981 len
= recvfrom(socket
, &peek
, sizeof(peek
), MSG_PEEK
| MSG_DONTWAIT
,
982 (struct sockaddr
*)&addr
, &addr_len
);
984 if (len
== sizeof(peek
) && peek
.nlmsg_seq
== this->seq
)
986 /* seems to be multipart */
992 *out_len
= result
.len
;
993 *out
= (struct nlmsghdr
*)clalloc(result
.ptr
, result
.len
);
995 pthread_mutex_unlock(&this->mutex
);
1001 * send a netlink message and wait for its acknowlegde
1003 static status_t
netlink_send_ack(private_kernel_interface_t
*this,
1004 int socket
, struct nlmsghdr
*in
)
1006 struct nlmsghdr
*out
, *hdr
;
1009 if (netlink_send(this, socket
, in
, &out
, &len
) != SUCCESS
)
1014 while (NLMSG_OK(hdr
, len
))
1016 switch (hdr
->nlmsg_type
)
1020 struct nlmsgerr
* err
= (struct nlmsgerr
*)NLMSG_DATA(hdr
);
1024 DBG1(DBG_KNL
, "received netlink error: %s (%d)",
1025 strerror(-err
->error
), -err
->error
);
1033 hdr
= NLMSG_NEXT(hdr
, len
);
1040 DBG1(DBG_KNL
, "netlink request not acknowlegded");
1046 * Initialize a list of local addresses.
1048 static status_t
init_address_list(private_kernel_interface_t
*this)
1050 char request
[BUFFER_SIZE
];
1051 struct nlmsghdr
*out
, *current
, *in
;
1052 struct rtgenmsg
*msg
;
1054 iterator_t
*ifaces
, *addrs
;
1055 iface_entry_t
*iface
;
1058 DBG1(DBG_KNL
, "listening on interfaces:");
1060 memset(&request
, 0, sizeof(request
));
1062 in
= (struct nlmsghdr
*)&request
;
1063 in
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtgenmsg
));
1064 in
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_MATCH
| NLM_F_ROOT
;
1065 msg
= (struct rtgenmsg
*)NLMSG_DATA(in
);
1066 msg
->rtgen_family
= AF_UNSPEC
;
1069 in
->nlmsg_type
= RTM_GETLINK
;
1070 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1075 while (NLMSG_OK(current
, len
))
1077 switch (current
->nlmsg_type
)
1082 process_link(this, current
, FALSE
);
1085 current
= NLMSG_NEXT(current
, len
);
1092 /* get all interface addresses */
1093 in
->nlmsg_type
= RTM_GETADDR
;
1094 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1099 while (NLMSG_OK(current
, len
))
1101 switch (current
->nlmsg_type
)
1106 process_addr(this, current
, FALSE
);
1109 current
= NLMSG_NEXT(current
, len
);
1116 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1117 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1119 if (iface
->flags
& IFF_UP
)
1121 DBG1(DBG_KNL
, " %s", iface
->ifname
);
1122 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1123 while (addrs
->iterate(addrs
, (void**)&addr
))
1125 DBG1(DBG_KNL
, " %H", addr
->ip
);
1127 addrs
->destroy(addrs
);
1130 ifaces
->destroy(ifaces
);
1135 * iterator hook to iterate over addrs
1137 static hook_result_t
addr_hook(private_kernel_interface_t
*this,
1138 addr_entry_t
*in
, host_t
**out
)
1141 { /* skip virtual interfaces added by us */
1144 if (in
->scope
>= RT_SCOPE_LINK
)
1145 { /* skip addresses with a unusable scope */
1153 * iterator hook to iterate over ifaces
1155 static hook_result_t
iface_hook(private_kernel_interface_t
*this,
1156 iface_entry_t
*in
, host_t
**out
)
1158 if (!(in
->flags
& IFF_UP
))
1159 { /* skip interfaces not up */
1163 if (this->hiter
== NULL
)
1165 this->hiter
= in
->addrs
->create_iterator(in
->addrs
, TRUE
);
1166 this->hiter
->set_iterator_hook(this->hiter
,
1167 (iterator_hook_t
*)addr_hook
, this);
1169 while (this->hiter
->iterate(this->hiter
, (void**)out
))
1173 this->hiter
->destroy(this->hiter
);
1179 * Implements kernel_interface_t.create_address_iterator.
1181 static iterator_t
*create_address_iterator(private_kernel_interface_t
*this)
1183 iterator_t
*iterator
;
1185 /* This iterator is not only hooked, is is double-hooked. As we have stored
1186 * our addresses in iface_entry->addr_entry->ip, we need to iterate the
1187 * entries in each interface we iterate. This does the iface_hook. The
1188 * addr_hook returns the ip instead of the addr_entry. */
1190 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1191 iterator
->set_iterator_hook(iterator
, (iterator_hook_t
*)iface_hook
, this);
1196 * implementation of kernel_interface_t.get_interface_name
1198 static char *get_interface_name(private_kernel_interface_t
*this, host_t
* ip
)
1200 iterator_t
*ifaces
, *addrs
;
1201 iface_entry_t
*iface
;
1205 DBG2(DBG_KNL
, "getting interface name for %H", ip
);
1207 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1208 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1210 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1211 while (addrs
->iterate(addrs
, (void**)&addr
))
1213 if (ip
->ip_equals(ip
, addr
->ip
))
1215 name
= strdup(iface
->ifname
);
1219 addrs
->destroy(addrs
);
1225 ifaces
->destroy(ifaces
);
1229 DBG2(DBG_KNL
, "%H is on interface %s", ip
, name
);
1233 DBG2(DBG_KNL
, "%H is not a local address", ip
);
1239 * Tries to find an ip address of a local interface that is included in the
1240 * supplied traffic selector.
1242 static status_t
get_address_by_ts(private_kernel_interface_t
*this,
1243 traffic_selector_t
*ts
, host_t
**ip
)
1245 iterator_t
*ifaces
, *addrs
;
1246 iface_entry_t
*iface
;
1252 DBG2(DBG_KNL
, "getting a local address in traffic selector %R", ts
);
1254 /* if we have a family which includes localhost, we do not
1255 * search for an IP, we use the default */
1256 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
1258 if (family
== AF_INET
)
1260 host
= host_create_from_string("127.0.0.1", 0);
1264 host
= host_create_from_string("::1", 0);
1267 if (ts
->includes(ts
, host
))
1269 *ip
= host_create_any(family
);
1270 host
->destroy(host
);
1271 DBG2(DBG_KNL
, "using host %H", *ip
);
1274 host
->destroy(host
);
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 (ts
->includes(ts
, addr
->ip
))
1285 *ip
= addr
->ip
->clone(addr
->ip
);
1289 addrs
->destroy(addrs
);
1295 ifaces
->destroy(ifaces
);
1299 DBG1(DBG_KNL
, "no local address found in traffic selector %R", ts
);
1302 DBG2(DBG_KNL
, "using host %H", *ip
);
1307 * get the interface of a local address
1309 static int get_interface_index(private_kernel_interface_t
*this, host_t
* ip
)
1311 iterator_t
*ifaces
, *addrs
;
1312 iface_entry_t
*iface
;
1316 DBG2(DBG_KNL
, "getting iface for %H", ip
);
1318 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1319 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1321 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1322 while (addrs
->iterate(addrs
, (void**)&addr
))
1324 if (ip
->ip_equals(ip
, addr
->ip
))
1326 ifindex
= iface
->ifindex
;
1330 addrs
->destroy(addrs
);
1336 ifaces
->destroy(ifaces
);
1340 DBG1(DBG_KNL
, "unable to get interface for %H", ip
);
1346 * get the refcount of a virtual ip
1348 static int get_vip_refcount(private_kernel_interface_t
*this, host_t
* ip
)
1350 iterator_t
*ifaces
, *addrs
;
1351 iface_entry_t
*iface
;
1355 ifaces
= this->ifaces
->create_iterator(this->ifaces
, TRUE
);
1356 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1358 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1359 while (addrs
->iterate(addrs
, (void**)&addr
))
1361 if (addr
->virtual && (iface
->flags
& IFF_UP
) &&
1362 ip
->ip_equals(ip
, addr
->ip
))
1364 refcount
= addr
->refcount
;
1368 addrs
->destroy(addrs
);
1374 ifaces
->destroy(ifaces
);
1380 * Manages the creation and deletion of ip addresses on an interface.
1381 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1383 static status_t
manage_ipaddr(private_kernel_interface_t
*this, int nlmsg_type
,
1384 int flags
, int if_index
, host_t
*ip
)
1386 unsigned char request
[BUFFER_SIZE
];
1387 struct nlmsghdr
*hdr
;
1388 struct ifaddrmsg
*msg
;
1391 memset(&request
, 0, sizeof(request
));
1393 chunk
= ip
->get_address(ip
);
1395 hdr
= (struct nlmsghdr
*)request
;
1396 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1397 hdr
->nlmsg_type
= nlmsg_type
;
1398 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1400 msg
= (struct ifaddrmsg
*)NLMSG_DATA(hdr
);
1401 msg
->ifa_family
= ip
->get_family(ip
);
1403 msg
->ifa_prefixlen
= 8 * chunk
.len
;
1404 msg
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1405 msg
->ifa_index
= if_index
;
1407 add_attribute(hdr
, IFA_LOCAL
, chunk
, sizeof(request
));
1409 return netlink_send_ack(this, this->socket_rt
, hdr
);
1413 * Manages source routes in the routing table.
1414 * By setting the appropriate nlmsg_type, the route added or r.
1416 static status_t
manage_srcroute(private_kernel_interface_t
*this, int nlmsg_type
,
1417 int flags
, route_entry_t
*route
)
1419 unsigned char request
[BUFFER_SIZE
];
1420 struct nlmsghdr
*hdr
;
1424 #if IPSEC_ROUTING_TABLE == 0
1425 /* if route is 0.0.0.0/0, we can't install it, as it would
1426 * overwrite the default route. Instead, we add two routes:
1427 * 0.0.0.0/1 and 128.0.0.0/1 */
1428 if (route
->prefixlen
== 0)
1433 half
.dst_net
= chunk_alloca(route
->dst_net
.len
);
1434 memset(half
.dst_net
.ptr
, 0, half
.dst_net
.len
);
1435 half
.src_ip
= route
->src_ip
;
1436 half
.gateway
= route
->gateway
;
1437 half
.if_index
= route
->if_index
;
1440 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1441 half
.dst_net
.ptr
[0] |= 0x80;
1442 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1447 memset(&request
, 0, sizeof(request
));
1449 hdr
= (struct nlmsghdr
*)request
;
1450 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1451 hdr
->nlmsg_type
= nlmsg_type
;
1452 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1454 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1455 msg
->rtm_family
= route
->src_ip
->get_family(route
->src_ip
);
1456 msg
->rtm_dst_len
= route
->prefixlen
;
1457 msg
->rtm_table
= IPSEC_ROUTING_TABLE
;
1458 msg
->rtm_protocol
= RTPROT_STATIC
;
1459 msg
->rtm_type
= RTN_UNICAST
;
1460 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1462 add_attribute(hdr
, RTA_DST
, route
->dst_net
, sizeof(request
));
1463 chunk
= route
->src_ip
->get_address(route
->src_ip
);
1464 add_attribute(hdr
, RTA_PREFSRC
, chunk
, sizeof(request
));
1465 chunk
= route
->gateway
->get_address(route
->gateway
);
1466 add_attribute(hdr
, RTA_GATEWAY
, chunk
, sizeof(request
));
1467 chunk
.ptr
= (char*)&route
->if_index
;
1468 chunk
.len
= sizeof(route
->if_index
);
1469 add_attribute(hdr
, RTA_OIF
, chunk
, sizeof(request
));
1471 return netlink_send_ack(this, this->socket_rt
, hdr
);
1475 * create or delete an rule to use our routing table
1477 static status_t
manage_rule(private_kernel_interface_t
*this, int nlmsg_type
,
1478 u_int32_t table
, u_int32_t prio
)
1480 unsigned char request
[BUFFER_SIZE
];
1481 struct nlmsghdr
*hdr
;
1485 memset(&request
, 0, sizeof(request
));
1486 hdr
= (struct nlmsghdr
*)request
;
1487 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1488 hdr
->nlmsg_type
= nlmsg_type
;
1489 if (nlmsg_type
== RTM_NEWRULE
)
1491 hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
1493 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1495 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1496 msg
->rtm_table
= table
;
1497 msg
->rtm_family
= AF_INET
;
1498 msg
->rtm_protocol
= RTPROT_BOOT
;
1499 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1500 msg
->rtm_type
= RTN_UNICAST
;
1502 chunk
= chunk_from_thing(prio
);
1503 add_attribute(hdr
, RTA_PRIORITY
, chunk
, sizeof(request
));
1505 return netlink_send_ack(this, this->socket_rt
, hdr
);
1509 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1511 static bool addr_in_subnet(chunk_t addr
, chunk_t net
, int net_len
)
1515 if (addr
.len
!= net
.len
)
1519 /* scan through all bits, beginning in the front */
1520 for (byte
= 0; byte
< addr
.len
; byte
++)
1522 for (bit
= 7; bit
>= 0; bit
--)
1524 /* check if bits are equal (or we reached the end of the net) */
1525 if (bit
+ byte
* 8 > net_len
)
1529 if (((1<<bit
) & addr
.ptr
[byte
]) != ((1<<bit
) & net
.ptr
[byte
]))
1539 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1541 static host_t
*get_route(private_kernel_interface_t
*this, host_t
*dest
,
1544 unsigned char request
[BUFFER_SIZE
];
1545 struct nlmsghdr
*hdr
, *out
, *current
;
1550 host_t
*src
= NULL
, *gtw
= NULL
;
1552 DBG2(DBG_KNL
, "getting address to reach %H", dest
);
1554 memset(&request
, 0, sizeof(request
));
1556 hdr
= (struct nlmsghdr
*)request
;
1557 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
| NLM_F_ROOT
;
1558 hdr
->nlmsg_type
= RTM_GETROUTE
;
1559 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1561 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1562 msg
->rtm_family
= dest
->get_family(dest
);
1564 chunk
= dest
->get_address(dest
);
1565 add_attribute(hdr
, RTA_DST
, chunk
, sizeof(request
));
1567 if (netlink_send(this, this->socket_rt
, hdr
, &out
, &len
) != SUCCESS
)
1569 DBG1(DBG_KNL
, "getting address to %H failed", dest
);
1573 while (NLMSG_OK(current
, len
))
1575 switch (current
->nlmsg_type
)
1583 chunk_t rta_gtw
, rta_src
, rta_dst
;
1584 u_int32_t rta_oif
= 0;
1586 rta_gtw
= rta_src
= rta_dst
= chunk_empty
;
1587 msg
= (struct rtmsg
*)(NLMSG_DATA(current
));
1589 rtasize
= RTM_PAYLOAD(current
);
1590 while (RTA_OK(rta
, rtasize
))
1592 switch (rta
->rta_type
)
1595 rta_src
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1598 rta_gtw
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1601 rta_dst
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1604 if (RTA_PAYLOAD(rta
) == sizeof(rta_oif
))
1606 rta_oif
= *(u_int32_t
*)RTA_DATA(rta
);
1610 rta
= RTA_NEXT(rta
, rtasize
);
1613 /* apply the route if:
1614 * - it is not from our own ipsec routing table
1615 * - is better than a previous one
1616 * - is the default route or
1617 * - its destination net contains our destination
1619 if (msg
->rtm_table
!= IPSEC_ROUTING_TABLE
1620 && msg
->rtm_dst_len
> best
1621 && (msg
->rtm_dst_len
== 0 || /* default route */
1622 (rta_dst
.ptr
&& addr_in_subnet(chunk
, rta_dst
, msg
->rtm_dst_len
))))
1624 iterator_t
*ifaces
, *addrs
;
1625 iface_entry_t
*iface
;
1628 best
= msg
->rtm_dst_len
;
1632 gtw
= host_create_from_chunk(msg
->rtm_family
, rta_gtw
, 0);
1634 else if (rta_src
.ptr
)
1637 src
= host_create_from_chunk(msg
->rtm_family
, rta_src
, 0);
1641 /* no source addr, get one from the interfaces */
1642 ifaces
= this->ifaces
->create_iterator_locked(
1643 this->ifaces
, &this->mutex
);
1644 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1646 if (iface
->ifindex
== rta_oif
)
1648 addrs
= iface
->addrs
->create_iterator(
1649 iface
->addrs
, TRUE
);
1650 while (addrs
->iterate(addrs
, (void**)&addr
))
1652 chunk_t ip
= addr
->ip
->get_address(addr
->ip
);
1653 if (msg
->rtm_dst_len
== 0
1654 || addr_in_subnet(ip
, rta_dst
, msg
->rtm_dst_len
))
1657 src
= addr
->ip
->clone(addr
->ip
);
1661 addrs
->destroy(addrs
);
1664 ifaces
->destroy(ifaces
);
1670 current
= NLMSG_NEXT(current
, len
);
1683 return dest
->clone(dest
);
1689 * Implementation of kernel_interface_t.get_source_addr.
1691 static host_t
* get_source_addr(private_kernel_interface_t
*this, host_t
*dest
)
1693 return get_route(this, dest
, FALSE
);
1697 * Implementation of kernel_interface_t.add_ip.
1699 static status_t
add_ip(private_kernel_interface_t
*this,
1700 host_t
*virtual_ip
, host_t
*iface_ip
)
1702 iface_entry_t
*iface
;
1704 iterator_t
*addrs
, *ifaces
;
1707 DBG2(DBG_KNL
, "adding virtual IP %H", virtual_ip
);
1709 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1710 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1712 bool iface_found
= FALSE
;
1714 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1715 while (addrs
->iterate(addrs
, (void**)&addr
))
1717 if (iface_ip
->ip_equals(iface_ip
, addr
->ip
))
1721 else if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1724 DBG2(DBG_KNL
, "virtual IP %H already installed on %s",
1725 virtual_ip
, iface
->ifname
);
1726 addrs
->destroy(addrs
);
1727 ifaces
->destroy(ifaces
);
1731 addrs
->destroy(addrs
);
1735 ifindex
= iface
->ifindex
;
1736 addr
= malloc_thing(addr_entry_t
);
1737 addr
->ip
= virtual_ip
->clone(virtual_ip
);
1739 addr
->virtual = TRUE
;
1740 addr
->scope
= RT_SCOPE_UNIVERSE
;
1741 iface
->addrs
->insert_last(iface
->addrs
, addr
);
1743 if (manage_ipaddr(this, RTM_NEWADDR
, NLM_F_CREATE
| NLM_F_EXCL
,
1744 ifindex
, virtual_ip
) == SUCCESS
)
1746 while (get_vip_refcount(this, virtual_ip
) == 0)
1747 { /* wait until address appears */
1748 pthread_cond_wait(&this->cond
, &this->mutex
);
1750 ifaces
->destroy(ifaces
);
1753 ifaces
->destroy(ifaces
);
1754 DBG1(DBG_KNL
, "adding virtual IP %H failed", virtual_ip
);
1758 ifaces
->destroy(ifaces
);
1760 DBG1(DBG_KNL
, "interface address %H not found, unable to install"
1761 "virtual IP %H", iface_ip
, virtual_ip
);
1766 * Implementation of kernel_interface_t.del_ip.
1768 static status_t
del_ip(private_kernel_interface_t
*this, host_t
*virtual_ip
)
1770 iface_entry_t
*iface
;
1772 iterator_t
*addrs
, *ifaces
;
1776 DBG2(DBG_KNL
, "deleting virtual IP %H", virtual_ip
);
1778 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1779 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1781 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1782 while (addrs
->iterate(addrs
, (void**)&addr
))
1784 if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1786 ifindex
= iface
->ifindex
;
1787 if (addr
->refcount
== 1)
1789 status
= manage_ipaddr(this, RTM_DELADDR
, 0,
1790 ifindex
, virtual_ip
);
1791 if (status
== SUCCESS
)
1792 { /* wait until the address is really gone */
1793 while (get_vip_refcount(this, virtual_ip
) > 0)
1795 pthread_cond_wait(&this->cond
, &this->mutex
);
1798 addrs
->destroy(addrs
);
1799 ifaces
->destroy(ifaces
);
1806 DBG2(DBG_KNL
, "virtual IP %H used by other SAs, not deleting",
1808 addrs
->destroy(addrs
);
1809 ifaces
->destroy(ifaces
);
1813 addrs
->destroy(addrs
);
1815 ifaces
->destroy(ifaces
);
1817 DBG2(DBG_KNL
, "virtual IP %H not cached, unable to delete", virtual_ip
);
1822 * Implementation of kernel_interface_t.get_spi.
1824 static status_t
get_spi(private_kernel_interface_t
*this,
1825 host_t
*src
, host_t
*dst
,
1826 protocol_id_t protocol
, u_int32_t reqid
,
1829 unsigned char request
[BUFFER_SIZE
];
1830 struct nlmsghdr
*hdr
, *out
;
1831 struct xfrm_userspi_info
*userspi
;
1832 u_int32_t received_spi
= 0;
1835 memset(&request
, 0, sizeof(request
));
1837 DBG2(DBG_KNL
, "getting SPI for reqid %d", reqid
);
1839 hdr
= (struct nlmsghdr
*)request
;
1840 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1841 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1842 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1844 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1845 host2xfrm(src
, &userspi
->info
.saddr
);
1846 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1847 userspi
->info
.id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1848 userspi
->info
.mode
= TRUE
; /* tunnel mode */
1849 userspi
->info
.reqid
= reqid
;
1850 userspi
->info
.family
= src
->get_family(src
);
1851 userspi
->min
= 0xc0000000;
1852 userspi
->max
= 0xcFFFFFFF;
1854 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1857 while (NLMSG_OK(hdr
, len
))
1859 switch (hdr
->nlmsg_type
)
1861 case XFRM_MSG_NEWSA
:
1863 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1864 received_spi
= usersa
->id
.spi
;
1869 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1871 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1872 strerror(-err
->error
), -err
->error
);
1876 hdr
= NLMSG_NEXT(hdr
, len
);
1886 if (received_spi
== 0)
1888 DBG1(DBG_KNL
, "unable to get SPI for reqid %d", reqid
);
1892 DBG2(DBG_KNL
, "got SPI 0x%x for reqid %d", received_spi
, reqid
);
1894 *spi
= received_spi
;
1899 * Implementation of kernel_interface_t.add_sa.
1901 static status_t
add_sa(private_kernel_interface_t
*this,
1902 host_t
*src
, host_t
*dst
, u_int32_t spi
,
1903 protocol_id_t protocol
, u_int32_t reqid
,
1904 u_int64_t expire_soft
, u_int64_t expire_hard
,
1905 algorithm_t
*enc_alg
, algorithm_t
*int_alg
,
1906 prf_plus_t
*prf_plus
, mode_t mode
, bool encap
,
1909 unsigned char request
[BUFFER_SIZE
];
1912 struct nlmsghdr
*hdr
;
1913 struct xfrm_usersa_info
*sa
;
1915 memset(&request
, 0, sizeof(request
));
1917 DBG2(DBG_KNL
, "adding SAD entry with SPI 0x%x", spi
);
1919 hdr
= (struct nlmsghdr
*)request
;
1920 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1921 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1922 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1924 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1925 host2xfrm(src
, &sa
->saddr
);
1926 host2xfrm(dst
, &sa
->id
.daddr
);
1928 sa
->id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1929 sa
->family
= src
->get_family(src
);
1931 sa
->replay_window
= 32;
1933 /* we currently do not expire SAs by volume/packet count */
1934 sa
->lft
.soft_byte_limit
= XFRM_INF
;
1935 sa
->lft
.hard_byte_limit
= XFRM_INF
;
1936 sa
->lft
.soft_packet_limit
= XFRM_INF
;
1937 sa
->lft
.hard_packet_limit
= XFRM_INF
;
1938 /* we use lifetimes since added, not since used */
1939 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
1940 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
1941 sa
->lft
.soft_use_expires_seconds
= 0;
1942 sa
->lft
.hard_use_expires_seconds
= 0;
1944 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1946 if (enc_alg
->algorithm
!= ENCR_UNDEFINED
)
1948 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
1949 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &key_size
);
1950 if (alg_name
== NULL
)
1952 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1953 encryption_algorithm_names
, enc_alg
->algorithm
);
1956 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1957 encryption_algorithm_names
, enc_alg
->algorithm
, key_size
);
1959 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
1960 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1961 if (hdr
->nlmsg_len
> sizeof(request
))
1966 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1967 algo
->alg_key_len
= key_size
;
1968 strcpy(algo
->alg_name
, alg_name
);
1969 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
1971 rthdr
= XFRM_RTA_NEXT(rthdr
);
1974 if (int_alg
->algorithm
!= AUTH_UNDEFINED
)
1976 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1977 alg_name
= lookup_algorithm(integrity_algs
, int_alg
, &key_size
);
1978 if (alg_name
== NULL
)
1980 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1981 integrity_algorithm_names
, int_alg
->algorithm
);
1984 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1985 integrity_algorithm_names
, int_alg
->algorithm
, key_size
);
1987 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
1988 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1989 if (hdr
->nlmsg_len
> sizeof(request
))
1994 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1995 algo
->alg_key_len
= key_size
;
1996 strcpy(algo
->alg_name
, alg_name
);
1997 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
1999 rthdr
= XFRM_RTA_NEXT(rthdr
);
2002 /* TODO: add IPComp here */
2006 rthdr
->rta_type
= XFRMA_ENCAP
;
2007 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
2009 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2010 if (hdr
->nlmsg_len
> sizeof(request
))
2015 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
2016 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2017 tmpl
->encap_sport
= htons(src
->get_port(src
));
2018 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
2019 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2020 /* encap_oa could probably be derived from the
2021 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
2022 * pluto does the same as we do here but it uses encap_oa in the
2023 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
2024 * the kernel ignores it anyway
2025 * -> does that mean that NAT-T encap doesn't work in transport mode?
2026 * No. The reason the kernel ignores NAT-OA is that it recomputes
2027 * (or, rather, just ignores) the checksum. If packets pass
2028 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
2029 rthdr
= XFRM_RTA_NEXT(rthdr
);
2032 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2034 DBG1(DBG_KNL
, "unable to add SAD entry with SPI 0x%x", spi
);
2041 * Implementation of kernel_interface_t.update_sa.
2043 static status_t
update_sa(private_kernel_interface_t
*this,
2044 u_int32_t spi
, protocol_id_t protocol
,
2045 host_t
*src
, host_t
*dst
,
2046 host_t
*new_src
, host_t
*new_dst
, bool encap
)
2048 unsigned char request
[BUFFER_SIZE
], *pos
;
2049 struct nlmsghdr
*hdr
, *out
= NULL
;
2050 struct xfrm_usersa_id
*sa_id
;
2051 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
2055 struct xfrm_encap_tmpl
* tmpl
= NULL
;
2057 memset(&request
, 0, sizeof(request
));
2059 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x for update", spi
);
2061 /* query the exisiting SA first */
2062 hdr
= (struct nlmsghdr
*)request
;
2063 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2064 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2065 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2067 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2068 host2xfrm(dst
, &sa_id
->daddr
);
2070 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
2071 sa_id
->family
= dst
->get_family(dst
);
2073 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2076 while (NLMSG_OK(hdr
, len
))
2078 switch (hdr
->nlmsg_type
)
2080 case XFRM_MSG_NEWSA
:
2082 out_sa
= NLMSG_DATA(hdr
);
2087 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2088 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2089 strerror(-err
->error
), -err
->error
);
2093 hdr
= NLMSG_NEXT(hdr
, len
);
2101 if (out_sa
== NULL
||
2102 this->public.del_sa(&this->public, dst
, spi
, protocol
) != SUCCESS
)
2104 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
2109 DBG2(DBG_KNL
, "updating SAD entry with SPI 0x%x from %#H..%#H to %#H..%#H",
2110 spi
, src
, dst
, new_src
, new_dst
);
2112 /* copy over the SA from out to request */
2113 hdr
= (struct nlmsghdr
*)request
;
2114 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
2115 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2116 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2117 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2118 sa
= NLMSG_DATA(hdr
);
2119 sa
->family
= new_dst
->get_family(new_dst
);
2121 if (!src
->ip_equals(src
, new_src
))
2123 host2xfrm(new_src
, &sa
->saddr
);
2125 if (!dst
->ip_equals(dst
, new_dst
))
2127 host2xfrm(new_dst
, &sa
->id
.daddr
);
2130 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
2131 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
2132 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
2133 while(RTA_OK(rta
, rtasize
))
2135 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2136 if (rta
->rta_type
!= XFRMA_ENCAP
|| encap
)
2138 if (rta
->rta_type
== XFRMA_ENCAP
)
2139 { /* update encap tmpl */
2140 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
2141 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
2142 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
2144 memcpy(pos
, rta
, rta
->rta_len
);
2145 pos
+= rta
->rta_len
;
2146 hdr
->nlmsg_len
+= rta
->rta_len
;
2148 rta
= RTA_NEXT(rta
, rtasize
);
2150 if (tmpl
== NULL
&& encap
)
2151 { /* add tmpl if we are enabling it */
2152 rta
= (struct rtattr
*)pos
;
2153 rta
->rta_type
= XFRMA_ENCAP
;
2154 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
2155 hdr
->nlmsg_len
+= rta
->rta_len
;
2156 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
2157 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2158 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
2159 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
2160 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2163 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2165 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
2175 * Implementation of kernel_interface_t.query_sa.
2177 static status_t
query_sa(private_kernel_interface_t
*this, host_t
*dst
,
2178 u_int32_t spi
, protocol_id_t protocol
,
2179 u_int32_t
*use_time
)
2181 unsigned char request
[BUFFER_SIZE
];
2182 struct nlmsghdr
*out
= NULL
, *hdr
;
2183 struct xfrm_usersa_id
*sa_id
;
2184 struct xfrm_usersa_info
*sa
= NULL
;
2187 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x", spi
);
2188 memset(&request
, 0, sizeof(request
));
2190 hdr
= (struct nlmsghdr
*)request
;
2191 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2192 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2193 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2195 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2196 host2xfrm(dst
, &sa_id
->daddr
);
2198 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
2199 sa_id
->family
= dst
->get_family(dst
);
2201 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2204 while (NLMSG_OK(hdr
, len
))
2206 switch (hdr
->nlmsg_type
)
2208 case XFRM_MSG_NEWSA
:
2210 sa
= NLMSG_DATA(hdr
);
2215 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2216 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2217 strerror(-err
->error
), -err
->error
);
2221 hdr
= NLMSG_NEXT(hdr
, len
);
2232 DBG1(DBG_KNL
, "unable to query SAD entry with SPI 0x%x", spi
);
2237 *use_time
= sa
->curlft
.use_time
;
2243 * Implementation of kernel_interface_t.del_sa.
2245 static status_t
del_sa(private_kernel_interface_t
*this, host_t
*dst
,
2246 u_int32_t spi
, protocol_id_t protocol
)
2248 unsigned char request
[BUFFER_SIZE
];
2249 struct nlmsghdr
*hdr
;
2250 struct xfrm_usersa_id
*sa_id
;
2252 memset(&request
, 0, sizeof(request
));
2254 DBG2(DBG_KNL
, "deleting SAD entry with SPI 0x%x", spi
);
2256 hdr
= (struct nlmsghdr
*)request
;
2257 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2258 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
2259 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2261 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2262 host2xfrm(dst
, &sa_id
->daddr
);
2264 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
2265 sa_id
->family
= dst
->get_family(dst
);
2267 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2269 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI 0x%x", spi
);
2272 DBG2(DBG_KNL
, "deleted SAD entry with SPI 0x%x", spi
);
2277 * Implementation of kernel_interface_t.add_policy.
2279 static status_t
add_policy(private_kernel_interface_t
*this,
2280 host_t
*src
, host_t
*dst
,
2281 traffic_selector_t
*src_ts
,
2282 traffic_selector_t
*dst_ts
,
2283 policy_dir_t direction
, protocol_id_t protocol
,
2284 u_int32_t reqid
, bool high_prio
, mode_t mode
)
2286 iterator_t
*iterator
;
2287 policy_entry_t
*current
, *policy
;
2289 unsigned char request
[BUFFER_SIZE
];
2290 struct xfrm_userpolicy_info
*policy_info
;
2291 struct nlmsghdr
*hdr
;
2293 /* create a policy */
2294 policy
= malloc_thing(policy_entry_t
);
2295 memset(policy
, 0, sizeof(policy_entry_t
));
2296 policy
->sel
= ts2selector(src_ts
, dst_ts
);
2297 policy
->direction
= direction
;
2299 /* find the policy, which matches EXACTLY */
2300 pthread_mutex_lock(&this->mutex
);
2301 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
2302 while (iterator
->iterate(iterator
, (void**)¤t
))
2304 if (memcmp(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) == 0 &&
2305 policy
->direction
== current
->direction
)
2307 /* use existing policy */
2308 current
->refcount
++;
2309 DBG2(DBG_KNL
, "policy %R===%R already exists, increasing ",
2310 "refcount", src_ts
, dst_ts
);
2317 iterator
->destroy(iterator
);
2319 { /* apply the new one, if we have no such policy */
2320 this->policies
->insert_last(this->policies
, policy
);
2321 policy
->refcount
= 1;
2324 DBG2(DBG_KNL
, "adding policy %R===%R", src_ts
, dst_ts
);
2326 memset(&request
, 0, sizeof(request
));
2327 hdr
= (struct nlmsghdr
*)request
;
2328 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2329 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
2330 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2332 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2333 policy_info
->sel
= policy
->sel
;
2334 policy_info
->dir
= policy
->direction
;
2335 /* calculate priority based on source selector size, small size = high prio */
2336 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
2337 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
2338 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
2339 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
2340 policy_info
->action
= XFRM_POLICY_ALLOW
;
2341 policy_info
->share
= XFRM_SHARE_ANY
;
2342 pthread_mutex_unlock(&this->mutex
);
2344 /* policies don't expire */
2345 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2346 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2347 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2348 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2349 policy_info
->lft
.soft_add_expires_seconds
= 0;
2350 policy_info
->lft
.hard_add_expires_seconds
= 0;
2351 policy_info
->lft
.soft_use_expires_seconds
= 0;
2352 policy_info
->lft
.hard_use_expires_seconds
= 0;
2354 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2355 rthdr
->rta_type
= XFRMA_TMPL
;
2357 rthdr
->rta_len
= sizeof(struct xfrm_user_tmpl
);
2358 rthdr
->rta_len
= RTA_LENGTH(rthdr
->rta_len
);
2360 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2361 if (hdr
->nlmsg_len
> sizeof(request
))
2366 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2367 tmpl
->reqid
= reqid
;
2368 tmpl
->id
.proto
= (protocol
== PROTO_AH
) ? KERNEL_AH
: KERNEL_ESP
;
2369 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2371 tmpl
->family
= src
->get_family(src
);
2373 host2xfrm(src
, &tmpl
->saddr
);
2374 host2xfrm(dst
, &tmpl
->id
.daddr
);
2376 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2378 DBG1(DBG_KNL
, "unable to add policy %R===%R", src_ts
, dst_ts
);
2382 /* install a route, if:
2383 * - we are NOT updating a policy
2384 * - this is a forward policy (to just get one for each child)
2385 * - we are in tunnel mode
2386 * - we are not using IPv6 (does not work correctly yet!)
2388 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
2389 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
)
2391 policy
->route
= malloc_thing(route_entry_t
);
2392 if (get_address_by_ts(this, dst_ts
, &policy
->route
->src_ip
) == SUCCESS
)
2394 /* get the nexthop to src (src as we are in POLICY_FWD).*/
2395 policy
->route
->gateway
= get_route(this, src
, TRUE
);
2396 policy
->route
->if_index
= get_interface_index(this, dst
);
2397 policy
->route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2398 memcpy(policy
->route
->dst_net
.ptr
, &policy
->sel
.saddr
, policy
->route
->dst_net
.len
);
2399 policy
->route
->prefixlen
= policy
->sel
.prefixlen_s
;
2401 if (manage_srcroute(this, RTM_NEWROUTE
, NLM_F_CREATE
| NLM_F_EXCL
,
2402 policy
->route
) != SUCCESS
)
2404 DBG1(DBG_KNL
, "unable to install source route for %H",
2405 policy
->route
->src_ip
);
2406 route_entry_destroy(policy
->route
);
2407 policy
->route
= NULL
;
2412 free(policy
->route
);
2413 policy
->route
= NULL
;
2421 * Implementation of kernel_interface_t.query_policy.
2423 static status_t
query_policy(private_kernel_interface_t
*this,
2424 traffic_selector_t
*src_ts
,
2425 traffic_selector_t
*dst_ts
,
2426 policy_dir_t direction
, u_int32_t
*use_time
)
2428 unsigned char request
[BUFFER_SIZE
];
2429 struct nlmsghdr
*out
= NULL
, *hdr
;
2430 struct xfrm_userpolicy_id
*policy_id
;
2431 struct xfrm_userpolicy_info
*policy
= NULL
;
2434 memset(&request
, 0, sizeof(request
));
2436 DBG2(DBG_KNL
, "querying policy %R===%R", src_ts
, dst_ts
);
2438 hdr
= (struct nlmsghdr
*)request
;
2439 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2440 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2441 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2443 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2444 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2445 policy_id
->dir
= direction
;
2447 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2450 while (NLMSG_OK(hdr
, len
))
2452 switch (hdr
->nlmsg_type
)
2454 case XFRM_MSG_NEWPOLICY
:
2456 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2461 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2462 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2463 strerror(-err
->error
), -err
->error
);
2467 hdr
= NLMSG_NEXT(hdr
, len
);
2478 DBG2(DBG_KNL
, "unable to query policy %R===%R", src_ts
, dst_ts
);
2482 *use_time
= (time_t)policy
->curlft
.use_time
;
2489 * Implementation of kernel_interface_t.del_policy.
2491 static status_t
del_policy(private_kernel_interface_t
*this,
2492 traffic_selector_t
*src_ts
,
2493 traffic_selector_t
*dst_ts
,
2494 policy_dir_t direction
)
2496 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
2497 route_entry_t
*route
;
2498 unsigned char request
[BUFFER_SIZE
];
2499 struct nlmsghdr
*hdr
;
2500 struct xfrm_userpolicy_id
*policy_id
;
2501 iterator_t
*iterator
;
2503 DBG2(DBG_KNL
, "deleting policy %R===%R", src_ts
, dst_ts
);
2505 /* create a policy */
2506 memset(&policy
, 0, sizeof(policy_entry_t
));
2507 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2508 policy
.direction
= direction
;
2510 /* find the policy */
2511 iterator
= this->policies
->create_iterator_locked(this->policies
, &this->mutex
);
2512 while (iterator
->iterate(iterator
, (void**)¤t
))
2514 if (memcmp(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) == 0 &&
2515 policy
.direction
== current
->direction
)
2517 to_delete
= current
;
2518 if (--to_delete
->refcount
> 0)
2520 /* is used by more SAs, keep in kernel */
2521 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2522 iterator
->destroy(iterator
);
2525 /* remove if last reference */
2526 iterator
->remove(iterator
);
2530 iterator
->destroy(iterator
);
2533 DBG1(DBG_KNL
, "deleting policy %R===%R failed, not found", src_ts
, dst_ts
);
2537 memset(&request
, 0, sizeof(request
));
2539 hdr
= (struct nlmsghdr
*)request
;
2540 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2541 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2542 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2544 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2545 policy_id
->sel
= to_delete
->sel
;
2546 policy_id
->dir
= direction
;
2548 route
= to_delete
->route
;
2551 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2553 DBG1(DBG_KNL
, "unable to delete policy %R===%R", src_ts
, dst_ts
);
2559 if (manage_srcroute(this, RTM_DELROUTE
, 0, route
) != SUCCESS
)
2561 DBG1(DBG_KNL
, "error uninstalling route installed with "
2562 "policy %R===%R", src_ts
, dst_ts
);
2564 route_entry_destroy(route
);
2570 * Implementation of kernel_interface_t.destroy.
2572 static void destroy(private_kernel_interface_t
*this)
2574 manage_rule(this, RTM_DELRULE
, IPSEC_ROUTING_TABLE
, IPSEC_ROUTING_TABLE_PRIO
);
2576 this->job
->cancel(this->job
);
2577 close(this->socket_xfrm_events
);
2578 close(this->socket_xfrm
);
2579 close(this->socket_rt_events
);
2580 close(this->socket_rt
);
2581 this->policies
->destroy(this->policies
);
2582 this->ifaces
->destroy_function(this->ifaces
, (void*)iface_entry_destroy
);
2587 * Described in header.
2589 kernel_interface_t
*kernel_interface_create()
2591 private_kernel_interface_t
*this = malloc_thing(private_kernel_interface_t
);
2592 struct sockaddr_nl addr
;
2593 pthread_mutexattr_t attr
;
2595 /* public functions */
2596 this->public.get_spi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
2597 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
,algorithm_t
*,algorithm_t
*,prf_plus_t
*,mode_t
,bool,bool))add_sa
;
2598 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
;
2599 this->public.query_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
*))query_sa
;
2600 this->public.del_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
2601 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
))add_policy
;
2602 this->public.query_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
2603 this->public.del_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
2604 this->public.get_interface
= (char*(*)(kernel_interface_t
*,host_t
*))get_interface_name
;
2605 this->public.create_address_iterator
= (iterator_t
*(*)(kernel_interface_t
*))create_address_iterator
;
2606 this->public.get_source_addr
= (host_t
*(*)(kernel_interface_t
*, host_t
*dest
))get_source_addr
;
2607 this->public.add_ip
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*)) add_ip
;
2608 this->public.del_ip
= (status_t(*)(kernel_interface_t
*,host_t
*)) del_ip
;
2609 this->public.destroy
= (void(*)(kernel_interface_t
*)) destroy
;
2611 /* private members */
2612 this->policies
= linked_list_create();
2613 this->ifaces
= linked_list_create();
2616 pthread_mutexattr_init(&attr
);
2617 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
2618 pthread_mutex_init(&this->mutex
, &attr
);
2619 pthread_mutexattr_destroy(&attr
);
2620 pthread_cond_init(&this->cond
, NULL
);
2621 timerclear(&this->last_roam
);
2623 memset(&addr
, 0, sizeof(addr
));
2624 addr
.nl_family
= AF_NETLINK
;
2626 /* create and bind RT socket */
2627 this->socket_rt
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2628 if (this->socket_rt
<= 0)
2630 charon
->kill(charon
, "unable to create RT netlink socket");
2633 if (bind(this->socket_rt
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2635 charon
->kill(charon
, "unable to bind RT netlink socket");
2638 /* create and bind RT socket for events (address/interface/route changes) */
2639 this->socket_rt_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2640 if (this->socket_rt_events
<= 0)
2642 charon
->kill(charon
, "unable to create RT event socket");
2644 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR
|
2645 RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_ROUTE
| RTMGRP_LINK
;
2646 if (bind(this->socket_rt_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2648 charon
->kill(charon
, "unable to bind RT event socket");
2651 /* create and bind XFRM socket */
2652 this->socket_xfrm
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2653 if (this->socket_xfrm
<= 0)
2655 charon
->kill(charon
, "unable to create XFRM netlink socket");
2658 if (bind(this->socket_xfrm
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2660 charon
->kill(charon
, "unable to bind XFRM netlink socket");
2663 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
2664 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2665 if (this->socket_xfrm_events
<= 0)
2667 charon
->kill(charon
, "unable to create XFRM event socket");
2669 addr
.nl_groups
= XFRMGRP_ACQUIRE
| XFRMGRP_EXPIRE
;
2670 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2672 charon
->kill(charon
, "unable to bind XFRM event socket");
2675 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
2677 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
2679 if (init_address_list(this) != SUCCESS
)
2681 charon
->kill(charon
, "unable to get interface list");
2684 if (manage_rule(this, RTM_NEWRULE
, IPSEC_ROUTING_TABLE
,
2685 IPSEC_ROUTING_TABLE_PRIO
) != SUCCESS
)
2687 DBG1(DBG_KNL
, "unable to create routing table rule");
2690 return &this->public;