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>
43 #include <sys/ioctl.h>
45 #include "kernel_interface.h"
48 #include <utils/linked_list.h>
49 #include <processing/jobs/delete_child_sa_job.h>
50 #include <processing/jobs/rekey_child_sa_job.h>
51 #include <processing/jobs/acquire_job.h>
52 #include <processing/jobs/callback_job.h>
53 #include <processing/jobs/roam_job.h>
55 /** routing table for routes installed by us */
56 #ifndef IPSEC_ROUTING_TABLE
57 #define IPSEC_ROUTING_TABLE 100
59 #ifndef IPSEC_ROUTING_TABLE_PRIO
60 #define IPSEC_ROUTING_TABLE_PRIO 100
63 /** kernel level protocol identifiers */
67 /** default priority of installed policies */
69 #define PRIO_HIGH 2000
71 /** delay before firing roam jobs (ms) */
72 #define ROAM_DELAY 100
74 #define BUFFER_SIZE 1024
77 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
78 * 'usual' netlink data x like 'struct xfrm_usersa_info'
80 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
82 * returns a pointer to the next rtattr following rta.
83 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
85 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
87 * returns the total size of attached rta data
88 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
90 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
92 typedef struct kernel_algorithm_t kernel_algorithm_t
;
95 * Mapping from the algorithms defined in IKEv2 to
96 * kernel level algorithm names and their key length
98 struct kernel_algorithm_t
{
100 * Identifier specified in IKEv2
105 * Name of the algorithm, as used as kernel identifier
110 * Key length in bits, if fixed size
114 #define END_OF_LIST -1
117 * Algorithms for encryption
119 kernel_algorithm_t encryption_algs
[] = {
120 /* {ENCR_DES_IV64, "***", 0}, */
121 {ENCR_DES
, "des", 64},
122 {ENCR_3DES
, "des3_ede", 192},
123 /* {ENCR_RC5, "***", 0}, */
124 /* {ENCR_IDEA, "***", 0}, */
125 {ENCR_CAST
, "cast128", 0},
126 {ENCR_BLOWFISH
, "blowfish", 0},
127 /* {ENCR_3IDEA, "***", 0}, */
128 /* {ENCR_DES_IV32, "***", 0}, */
129 {ENCR_NULL
, "cipher_null", 0},
130 {ENCR_AES_CBC
, "aes", 0},
131 /* {ENCR_AES_CTR, "***", 0}, */
132 {END_OF_LIST
, NULL
, 0},
136 * Algorithms for integrity protection
138 kernel_algorithm_t integrity_algs
[] = {
139 {AUTH_HMAC_MD5_96
, "md5", 128},
140 {AUTH_HMAC_SHA1_96
, "sha1", 160},
141 {AUTH_HMAC_SHA2_256_128
, "sha256", 256},
142 {AUTH_HMAC_SHA2_384_192
, "sha384", 384},
143 {AUTH_HMAC_SHA2_512_256
, "sha512", 512},
144 /* {AUTH_DES_MAC, "***", 0}, */
145 /* {AUTH_KPDK_MD5, "***", 0}, */
146 {AUTH_AES_XCBC_96
, "xcbc(aes)", 128},
147 {END_OF_LIST
, NULL
, 0},
151 * Look up a kernel algorithm name and its key size
153 char* lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
154 algorithm_t
*ikev2_algo
, u_int
*key_size
)
156 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
158 if (ikev2_algo
->algorithm
== kernel_algo
->ikev2_id
)
160 /* match, evaluate key length */
161 if (ikev2_algo
->key_size
)
162 { /* variable length */
163 *key_size
= ikev2_algo
->key_size
;
167 *key_size
= kernel_algo
->key_size
;
169 return kernel_algo
->name
;
176 typedef struct route_entry_t route_entry_t
;
179 * installed routing entry
181 struct route_entry_t
{
183 /** Index of the interface the route is bound to */
186 /** Source ip of the route */
189 /** gateway for this route */
192 /** Destination net */
195 /** Destination net prefixlen */
200 * destroy an route_entry_t object
202 static void route_entry_destroy(route_entry_t
*this)
204 this->src_ip
->destroy(this->src_ip
);
205 this->gateway
->destroy(this->gateway
);
206 chunk_free(&this->dst_net
);
210 typedef struct policy_entry_t policy_entry_t
;
213 * installed kernel policy.
215 struct policy_entry_t
{
217 /** direction of this policy: in, out, forward */
220 /** reqid of the policy */
223 /** parameters of installed policy */
224 struct xfrm_selector sel
;
226 /** associated route installed for this policy */
227 route_entry_t
*route
;
229 /** by how many CHILD_SA's this policy is used */
233 typedef struct addr_entry_t addr_entry_t
;
236 * IP address in an inface_entry_t
238 struct addr_entry_t
{
240 /** The ip address */
243 /** virtual IP managed by us */
246 /** scope of the address */
249 /** Number of times this IP is used, if virtual */
254 * destroy a addr_entry_t object
256 static void addr_entry_destroy(addr_entry_t
*this)
258 this->ip
->destroy(this->ip
);
262 typedef struct iface_entry_t iface_entry_t
;
265 * A network interface on this system, containing addr_entry_t's
267 struct iface_entry_t
{
269 /** interface index */
272 /** name of the interface */
273 char ifname
[IFNAMSIZ
];
275 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
278 /** list of addresses as host_t */
279 linked_list_t
*addrs
;
283 * destroy an interface entry
285 static void iface_entry_destroy(iface_entry_t
*this)
287 this->addrs
->destroy_function(this->addrs
, (void*)addr_entry_destroy
);
291 typedef struct private_kernel_interface_t private_kernel_interface_t
;
294 * Private variables and functions of kernel_interface class.
296 struct private_kernel_interface_t
{
298 * Public part of the kernel_interface_t object.
300 kernel_interface_t
public;
303 * mutex to lock access to the various lists
305 pthread_mutex_t mutex
;
308 * List of installed policies (policy_entry_t)
310 linked_list_t
*policies
;
313 * Cached list of interfaces and its adresses (iface_entry_t)
315 linked_list_t
*ifaces
;
318 * iterator used in hook()
323 * job receiving netlink events
328 * current sequence number for netlink request
333 * Netlink xfrm socket (IPsec)
338 * netlink xfrm socket to receive acquire and expire events
340 int socket_xfrm_events
;
343 * Netlink rt socket (routing)
348 * Netlink rt socket to receive address change events
350 int socket_rt_events
;
353 * time of the last roam_job
355 struct timeval last_roam
;
359 * convert a host_t to a struct xfrm_address
361 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
363 chunk_t chunk
= host
->get_address(host
);
364 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
368 * convert a traffic selector address range to subnet and its mask.
370 static void ts2subnet(traffic_selector_t
* ts
,
371 xfrm_address_t
*net
, u_int8_t
*mask
)
373 /* there is no way to do this cleanly, as the address range may
374 * be anything else but a subnet. We use from_addr as subnet
375 * and try to calculate a usable subnet mask.
380 size_t size
= (ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE
) ?
4 : 16;
382 from
= ts
->get_from_address(ts
);
383 to
= ts
->get_to_address(ts
);
386 /* go trough all bits of the addresses, beginning in the front.
387 * as long as they are equal, the subnet gets larger
389 for (byte
= 0; byte
< size
; byte
++)
391 for (bit
= 7; bit
>= 0; bit
--)
393 if ((1<<bit
& from
.ptr
[byte
]) != (1<<bit
& to
.ptr
[byte
]))
395 *mask
= ((7 - bit
) + (byte
* 8));
405 memcpy(net
, from
.ptr
, from
.len
);
411 * convert a traffic selector port range to port/portmask
413 static void ts2ports(traffic_selector_t
* ts
,
414 u_int16_t
*port
, u_int16_t
*mask
)
416 /* linux does not seem to accept complex portmasks. Only
417 * any or a specific port is allowed. We set to any, if we have
418 * a port range, or to a specific, if we have one port only.
422 from
= ts
->get_from_port(ts
);
423 to
= ts
->get_to_port(ts
);
438 * convert a pair of traffic_selectors to a xfrm_selector
440 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
441 traffic_selector_t
*dst
)
443 struct xfrm_selector sel
;
445 memset(&sel
, 0, sizeof(sel
));
446 sel
.family
= src
->get_type(src
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
447 /* src or dest proto may be "any" (0), use more restrictive one */
448 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
449 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
450 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
451 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
452 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
460 * Creates an rtattr and adds it to the netlink message
462 static void add_attribute(struct nlmsghdr
*hdr
, int rta_type
, chunk_t data
,
467 if (NLMSG_ALIGN(hdr
->nlmsg_len
) + RTA_ALIGN(data
.len
) > buflen
)
469 DBG1(DBG_KNL
, "unable to add attribute, buffer too small");
473 rta
= (struct rtattr
*)(((char*)hdr
) + NLMSG_ALIGN(hdr
->nlmsg_len
));
474 rta
->rta_type
= rta_type
;
475 rta
->rta_len
= RTA_LENGTH(data
.len
);
476 memcpy(RTA_DATA(rta
), data
.ptr
, data
.len
);
477 hdr
->nlmsg_len
= NLMSG_ALIGN(hdr
->nlmsg_len
) + rta
->rta_len
;
481 * process a XFRM_MSG_ACQUIRE from kernel
483 static void process_acquire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
487 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
488 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
490 if (RTA_OK(rtattr
, rtsize
))
492 if (rtattr
->rta_type
== XFRMA_TMPL
)
494 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rtattr
);
500 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
503 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
504 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid %d", reqid
);
505 job
= (job_t
*)acquire_job_create(reqid
);
506 charon
->processor
->queue_job(charon
->processor
, job
);
510 * process a XFRM_MSG_EXPIRE from kernel
512 static void process_expire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
515 protocol_id_t protocol
;
516 u_int32_t spi
, reqid
;
517 struct xfrm_user_expire
*expire
;
519 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
520 protocol
= expire
->state
.id
.proto
== KERNEL_ESP ? PROTO_ESP
: PROTO_AH
;
521 spi
= expire
->state
.id
.spi
;
522 reqid
= expire
->state
.reqid
;
524 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
525 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
526 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
527 protocol
, ntohl(spi
), reqid
);
530 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
534 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
536 charon
->processor
->queue_job(charon
->processor
, job
);
540 * start a roaming job. We delay it for a second and fire only one job
541 * for multiple events. Otherwise we would create two many jobs.
543 static void fire_roam_job(private_kernel_interface_t
*this, bool address
)
547 if (gettimeofday(&now
, NULL
) == 0)
549 if (timercmp(&now
, &this->last_roam
, >))
551 now
.tv_usec
+= ROAM_DELAY
* 1000;
552 while (now
.tv_usec
> 1000000)
555 now
.tv_usec
-= 1000000;
557 this->last_roam
= now
;
558 charon
->scheduler
->schedule_job(charon
->scheduler
,
559 (job_t
*)roam_job_create(address
), ROAM_DELAY
);
565 * process RTM_NEWLINK/RTM_DELLINK from kernel
567 static void process_link(private_kernel_interface_t
*this,
568 struct nlmsghdr
*hdr
, bool event
)
570 struct ifinfomsg
* msg
= (struct ifinfomsg
*)(NLMSG_DATA(hdr
));
571 struct rtattr
*rta
= IFLA_RTA(msg
);
572 size_t rtasize
= IFLA_PAYLOAD (hdr
);
573 iterator_t
*iterator
;
574 iface_entry_t
*current
, *entry
= NULL
;
578 while(RTA_OK(rta
, rtasize
))
580 switch (rta
->rta_type
)
583 name
= RTA_DATA(rta
);
586 rta
= RTA_NEXT(rta
, rtasize
);
593 switch (hdr
->nlmsg_type
)
597 if (msg
->ifi_flags
& IFF_LOOPBACK
)
598 { /* ignore loopback interfaces */
601 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
603 while (iterator
->iterate(iterator
, (void**)¤t
))
605 if (current
->ifindex
== msg
->ifi_index
)
613 entry
= malloc_thing(iface_entry_t
);
614 entry
->ifindex
= msg
->ifi_index
;
616 entry
->addrs
= linked_list_create();
617 this->ifaces
->insert_last(this->ifaces
, entry
);
619 memcpy(entry
->ifname
, name
, IFNAMSIZ
);
620 entry
->ifname
[IFNAMSIZ
-1] = '\0';
623 if (!(entry
->flags
& IFF_UP
) && (msg
->ifi_flags
& IFF_UP
))
626 DBG1(DBG_KNL
, "interface %s activated", name
);
628 if ((entry
->flags
& IFF_UP
) && !(msg
->ifi_flags
& IFF_UP
))
631 DBG1(DBG_KNL
, "interface %s deactivated", name
);
634 entry
->flags
= msg
->ifi_flags
;
635 iterator
->destroy(iterator
);
640 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
642 while (iterator
->iterate(iterator
, (void**)¤t
))
644 if (current
->ifindex
== msg
->ifi_index
)
646 /* we do not remove it, as an address may be added to a
647 * "down" interface and we wan't to know that. */
648 current
->flags
= msg
->ifi_flags
;
652 iterator
->destroy(iterator
);
657 /* send an update to all IKE_SAs */
660 fire_roam_job(this, TRUE
);
665 * process RTM_NEWADDR/RTM_DELADDR from kernel
667 static void process_addr(private_kernel_interface_t
*this,
668 struct nlmsghdr
*hdr
, bool event
)
670 struct ifaddrmsg
* msg
= (struct ifaddrmsg
*)(NLMSG_DATA(hdr
));
671 struct rtattr
*rta
= IFA_RTA(msg
);
672 size_t rtasize
= IFA_PAYLOAD (hdr
);
674 iterator_t
*ifaces
, *addrs
;
675 iface_entry_t
*iface
;
677 chunk_t local
= chunk_empty
, address
= chunk_empty
;
678 bool update
= FALSE
, found
= FALSE
, changed
= FALSE
;
680 while(RTA_OK(rta
, rtasize
))
682 switch (rta
->rta_type
)
685 local
.ptr
= RTA_DATA(rta
);
686 local
.len
= RTA_PAYLOAD(rta
);
689 address
.ptr
= RTA_DATA(rta
);
690 address
.len
= RTA_PAYLOAD(rta
);
693 rta
= RTA_NEXT(rta
, rtasize
);
696 /* For PPP interfaces, we need the IFA_LOCAL address,
697 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
698 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
701 host
= host_create_from_chunk(msg
->ifa_family
, local
, 0);
703 else if (address
.ptr
)
705 host
= host_create_from_chunk(msg
->ifa_family
, address
, 0);
713 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
714 while (ifaces
->iterate(ifaces
, (void**)&iface
))
716 if (iface
->ifindex
== msg
->ifa_index
)
718 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
719 while (addrs
->iterate(addrs
, (void**)&addr
))
721 if (host
->ip_equals(host
, addr
->ip
))
724 if (hdr
->nlmsg_type
== RTM_DELADDR
)
727 addrs
->remove(addrs
);
728 addr_entry_destroy(addr
);
729 DBG1(DBG_KNL
, "%H disappeared from %s", host
, iface
->ifname
);
733 addrs
->destroy(addrs
);
735 if (hdr
->nlmsg_type
== RTM_NEWADDR
)
741 addr
= malloc_thing(addr_entry_t
);
742 addr
->ip
= host
->clone(host
);
743 addr
->virtual = FALSE
;
745 addr
->scope
= msg
->ifa_scope
;
747 iface
->addrs
->insert_last(iface
->addrs
, addr
);
750 DBG1(DBG_KNL
, "%H appeared on %s", host
, iface
->ifname
);
754 if (found
&& (iface
->flags
& IFF_UP
))
761 ifaces
->destroy(ifaces
);
764 /* send an update to all IKE_SAs */
765 if (update
&& event
&& changed
)
767 fire_roam_job(this, TRUE
);
772 * Receives events from kernel
774 static job_requeue_t
receive_events(private_kernel_interface_t
*this)
777 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
778 struct sockaddr_nl addr
;
779 socklen_t addr_len
= sizeof(addr
);
780 int len
, oldstate
, maxfd
, selected
;
784 FD_SET(this->socket_xfrm_events
, &rfds
);
785 FD_SET(this->socket_rt_events
, &rfds
);
786 maxfd
= max(this->socket_xfrm_events
, this->socket_rt_events
);
788 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
789 selected
= select(maxfd
+ 1, &rfds
, NULL
, NULL
, NULL
);
790 pthread_setcancelstate(oldstate
, NULL
);
793 DBG1(DBG_KNL
, "selecting on sockets failed: %s", strerror(errno
));
794 return JOB_REQUEUE_FAIR
;
796 if (FD_ISSET(this->socket_xfrm_events
, &rfds
))
798 selected
= this->socket_xfrm_events
;
800 else if (FD_ISSET(this->socket_rt_events
, &rfds
))
802 selected
= this->socket_rt_events
;
806 return JOB_REQUEUE_DIRECT
;
809 len
= recvfrom(selected
, response
, sizeof(response
), MSG_DONTWAIT
,
810 (struct sockaddr
*)&addr
, &addr_len
);
816 /* interrupted, try again */
817 return JOB_REQUEUE_DIRECT
;
819 /* no data ready, select again */
820 return JOB_REQUEUE_DIRECT
;
822 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
824 return JOB_REQUEUE_FAIR
;
827 if (addr
.nl_pid
!= 0)
828 { /* not from kernel. not interested, try another one */
829 return JOB_REQUEUE_DIRECT
;
832 while (NLMSG_OK(hdr
, len
))
834 /* looks good so far, dispatch netlink message */
835 if (selected
== this->socket_xfrm_events
)
837 switch (hdr
->nlmsg_type
)
839 case XFRM_MSG_ACQUIRE
:
840 process_acquire(this, hdr
);
842 case XFRM_MSG_EXPIRE
:
843 process_expire(this, hdr
);
849 else if (selected
== this->socket_rt_events
)
851 switch (hdr
->nlmsg_type
)
855 process_addr(this, hdr
, TRUE
);
859 process_link(this, hdr
, TRUE
);
863 fire_roam_job(this, FALSE
);
869 hdr
= NLMSG_NEXT(hdr
, len
);
871 return JOB_REQUEUE_DIRECT
;
875 * send a netlink message and wait for a reply
877 static status_t
netlink_send(private_kernel_interface_t
*this,
878 int socket
, struct nlmsghdr
*in
,
879 struct nlmsghdr
**out
, size_t *out_len
)
882 struct sockaddr_nl addr
;
883 chunk_t result
= chunk_empty
, tmp
;
884 struct nlmsghdr
*msg
, peek
;
886 pthread_mutex_lock(&this->mutex
);
888 in
->nlmsg_seq
= ++this->seq
;
889 in
->nlmsg_pid
= getpid();
891 memset(&addr
, 0, sizeof(addr
));
892 addr
.nl_family
= AF_NETLINK
;
898 len
= sendto(socket
, in
, in
->nlmsg_len
, 0,
899 (struct sockaddr
*)&addr
, sizeof(addr
));
901 if (len
!= in
->nlmsg_len
)
905 /* interrupted, try again */
908 pthread_mutex_unlock(&this->mutex
);
909 DBG1(DBG_KNL
, "error sending to netlink socket: %s", strerror(errno
));
918 tmp
.len
= sizeof(buf
);
920 msg
= (struct nlmsghdr
*)tmp
.ptr
;
922 memset(&addr
, 0, sizeof(addr
));
923 addr
.nl_family
= AF_NETLINK
;
924 addr
.nl_pid
= getpid();
926 addr_len
= sizeof(addr
);
928 len
= recvfrom(socket
, tmp
.ptr
, tmp
.len
, 0,
929 (struct sockaddr
*)&addr
, &addr_len
);
935 DBG1(DBG_KNL
, "got interrupted");
936 /* interrupted, try again */
939 DBG1(DBG_KNL
, "error reading from netlink socket: %s", strerror(errno
));
940 pthread_mutex_unlock(&this->mutex
);
943 if (!NLMSG_OK(msg
, len
))
945 DBG1(DBG_KNL
, "received corrupted netlink message");
946 pthread_mutex_unlock(&this->mutex
);
949 if (msg
->nlmsg_seq
!= this->seq
)
951 DBG1(DBG_KNL
, "received invalid netlink sequence number");
952 if (msg
->nlmsg_seq
< this->seq
)
956 pthread_mutex_unlock(&this->mutex
);
961 result
= chunk_cata("cc", result
, tmp
);
963 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
964 * numbers to detect multi header messages */
965 len
= recvfrom(socket
, &peek
, sizeof(peek
), MSG_PEEK
| MSG_DONTWAIT
,
966 (struct sockaddr
*)&addr
, &addr_len
);
968 if (len
== sizeof(peek
) && peek
.nlmsg_seq
== this->seq
)
970 /* seems to be multipart */
976 *out_len
= result
.len
;
977 *out
= (struct nlmsghdr
*)clalloc(result
.ptr
, result
.len
);
979 pthread_mutex_unlock(&this->mutex
);
985 * send a netlink message and wait for its acknowlegde
987 static status_t
netlink_send_ack(private_kernel_interface_t
*this,
988 int socket
, struct nlmsghdr
*in
)
990 struct nlmsghdr
*out
, *hdr
;
993 if (netlink_send(this, socket
, in
, &out
, &len
) != SUCCESS
)
998 while (NLMSG_OK(hdr
, len
))
1000 switch (hdr
->nlmsg_type
)
1004 struct nlmsgerr
* err
= (struct nlmsgerr
*)NLMSG_DATA(hdr
);
1008 DBG1(DBG_KNL
, "received netlink error: %s (%d)",
1009 strerror(-err
->error
), -err
->error
);
1017 hdr
= NLMSG_NEXT(hdr
, len
);
1024 DBG1(DBG_KNL
, "netlink request not acknowlegded");
1030 * Initialize a list of local addresses.
1032 static status_t
init_address_list(private_kernel_interface_t
*this)
1034 char request
[BUFFER_SIZE
];
1035 struct nlmsghdr
*out
, *current
, *in
;
1036 struct rtgenmsg
*msg
;
1038 iterator_t
*ifaces
, *addrs
;
1039 iface_entry_t
*iface
;
1042 DBG1(DBG_KNL
, "listening on interfaces:");
1044 memset(&request
, 0, sizeof(request
));
1046 in
= (struct nlmsghdr
*)&request
;
1047 in
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtgenmsg
));
1048 in
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_MATCH
| NLM_F_ROOT
;
1049 msg
= (struct rtgenmsg
*)NLMSG_DATA(in
);
1050 msg
->rtgen_family
= AF_UNSPEC
;
1053 in
->nlmsg_type
= RTM_GETLINK
;
1054 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1059 while (NLMSG_OK(current
, len
))
1061 switch (current
->nlmsg_type
)
1066 process_link(this, current
, FALSE
);
1069 current
= NLMSG_NEXT(current
, len
);
1076 /* get all interface addresses */
1077 in
->nlmsg_type
= RTM_GETADDR
;
1078 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1083 while (NLMSG_OK(current
, len
))
1085 switch (current
->nlmsg_type
)
1090 process_addr(this, current
, FALSE
);
1093 current
= NLMSG_NEXT(current
, len
);
1100 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1101 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1103 if (iface
->flags
& IFF_UP
)
1105 DBG1(DBG_KNL
, " %s", iface
->ifname
);
1106 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1107 while (addrs
->iterate(addrs
, (void**)&addr
))
1109 DBG1(DBG_KNL
, " %H", addr
->ip
);
1111 addrs
->destroy(addrs
);
1114 ifaces
->destroy(ifaces
);
1119 * iterator hook to iterate over addrs
1121 static hook_result_t
addr_hook(private_kernel_interface_t
*this,
1122 addr_entry_t
*in
, host_t
**out
)
1125 { /* skip virtual interfaces added by us */
1128 if (in
->scope
>= RT_SCOPE_LINK
)
1129 { /* skip addresses with a unusable scope */
1137 * iterator hook to iterate over ifaces
1139 static hook_result_t
iface_hook(private_kernel_interface_t
*this,
1140 iface_entry_t
*in
, host_t
**out
)
1142 if (!(in
->flags
& IFF_UP
))
1143 { /* skip interfaces not up */
1147 if (this->hiter
== NULL
)
1149 this->hiter
= in
->addrs
->create_iterator(in
->addrs
, TRUE
);
1150 this->hiter
->set_iterator_hook(this->hiter
,
1151 (iterator_hook_t
*)addr_hook
, this);
1153 while (this->hiter
->iterate(this->hiter
, (void**)out
))
1157 this->hiter
->destroy(this->hiter
);
1163 * Implements kernel_interface_t.create_address_iterator.
1165 static iterator_t
*create_address_iterator(private_kernel_interface_t
*this)
1167 iterator_t
*iterator
;
1169 /* This iterator is not only hooked, is is double-hooked. As we have stored
1170 * our addresses in iface_entry->addr_entry->ip, we need to iterate the
1171 * entries in each interface we iterate. This does the iface_hook. The
1172 * addr_hook returns the ip instead of the addr_entry. */
1174 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1175 iterator
->set_iterator_hook(iterator
, (iterator_hook_t
*)iface_hook
, this);
1180 * implementation of kernel_interface_t.get_interface_name
1182 static char *get_interface_name(private_kernel_interface_t
*this, host_t
* ip
)
1184 iterator_t
*ifaces
, *addrs
;
1185 iface_entry_t
*iface
;
1189 DBG2(DBG_KNL
, "getting interface name for %H", ip
);
1191 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1192 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1194 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1195 while (addrs
->iterate(addrs
, (void**)&addr
))
1197 if (ip
->ip_equals(ip
, addr
->ip
))
1199 name
= strdup(iface
->ifname
);
1203 addrs
->destroy(addrs
);
1209 ifaces
->destroy(ifaces
);
1213 DBG2(DBG_KNL
, "%H is on interface %s", ip
, name
);
1217 DBG2(DBG_KNL
, "%H is not a local address", ip
);
1223 * Tries to find an ip address of a local interface that is included in the
1224 * supplied traffic selector.
1226 static status_t
get_address_by_ts(private_kernel_interface_t
*this,
1227 traffic_selector_t
*ts
, host_t
**ip
)
1229 iterator_t
*ifaces
, *addrs
;
1230 iface_entry_t
*iface
;
1236 DBG2(DBG_KNL
, "getting a local address in traffic selector %R", ts
);
1238 /* if we have a family which includes localhost, we do not
1239 * search for an IP, we use the default */
1240 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
1242 if (family
== AF_INET
)
1244 host
= host_create_from_string("127.0.0.1", 0);
1248 host
= host_create_from_string("::1", 0);
1251 if (ts
->includes(ts
, host
))
1253 *ip
= host_create_any(family
);
1254 host
->destroy(host
);
1255 DBG2(DBG_KNL
, "using host %H", *ip
);
1258 host
->destroy(host
);
1260 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1261 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1263 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1264 while (addrs
->iterate(addrs
, (void**)&addr
))
1266 if (ts
->includes(ts
, addr
->ip
))
1269 *ip
= addr
->ip
->clone(addr
->ip
);
1273 addrs
->destroy(addrs
);
1279 ifaces
->destroy(ifaces
);
1283 DBG1(DBG_KNL
, "no local address found in traffic selector %R", ts
);
1286 DBG2(DBG_KNL
, "using host %H", *ip
);
1291 * get the interface of a local address
1293 static int get_interface_index(private_kernel_interface_t
*this, host_t
* ip
)
1295 iterator_t
*ifaces
, *addrs
;
1296 iface_entry_t
*iface
;
1300 DBG2(DBG_KNL
, "getting iface for %H", ip
);
1302 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1303 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1305 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1306 while (addrs
->iterate(addrs
, (void**)&addr
))
1308 if (ip
->ip_equals(ip
, addr
->ip
))
1310 ifindex
= iface
->ifindex
;
1314 addrs
->destroy(addrs
);
1320 ifaces
->destroy(ifaces
);
1324 DBG1(DBG_KNL
, "unable to get interface for %H", ip
);
1330 * Manages the creation and deletion of ip addresses on an interface.
1331 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1333 static status_t
manage_ipaddr(private_kernel_interface_t
*this, int nlmsg_type
,
1334 int flags
, int if_index
, host_t
*ip
)
1336 unsigned char request
[BUFFER_SIZE
];
1337 struct nlmsghdr
*hdr
;
1338 struct ifaddrmsg
*msg
;
1341 memset(&request
, 0, sizeof(request
));
1343 chunk
= ip
->get_address(ip
);
1345 hdr
= (struct nlmsghdr
*)request
;
1346 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1347 hdr
->nlmsg_type
= nlmsg_type
;
1348 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1350 msg
= (struct ifaddrmsg
*)NLMSG_DATA(hdr
);
1351 msg
->ifa_family
= ip
->get_family(ip
);
1353 msg
->ifa_prefixlen
= 8 * chunk
.len
;
1354 msg
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1355 msg
->ifa_index
= if_index
;
1357 add_attribute(hdr
, IFA_LOCAL
, chunk
, sizeof(request
));
1359 return netlink_send_ack(this, this->socket_rt
, hdr
);
1363 * Manages source routes in the routing table.
1364 * By setting the appropriate nlmsg_type, the route added or r.
1366 static status_t
manage_srcroute(private_kernel_interface_t
*this, int nlmsg_type
,
1367 int flags
, route_entry_t
*route
)
1369 unsigned char request
[BUFFER_SIZE
];
1370 struct nlmsghdr
*hdr
;
1374 #if IPSEC_ROUTING_TABLE == 0
1375 /* if route is 0.0.0.0/0, we can't install it, as it would
1376 * overwrite the default route. Instead, we add two routes:
1377 * 0.0.0.0/1 and 128.0.0.0/1 */
1378 if (route
->prefixlen
== 0)
1383 half
.dst_net
= chunk_alloca(route
->dst_net
.len
);
1384 memset(half
.dst_net
.ptr
, 0, half
.dst_net
.len
);
1385 half
.src_ip
= route
->src_ip
;
1386 half
.gateway
= route
->gateway
;
1387 half
.if_index
= route
->if_index
;
1390 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1391 half
.dst_net
.ptr
[0] |= 0x80;
1392 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1397 memset(&request
, 0, sizeof(request
));
1399 hdr
= (struct nlmsghdr
*)request
;
1400 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1401 hdr
->nlmsg_type
= nlmsg_type
;
1402 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1404 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1405 msg
->rtm_family
= route
->src_ip
->get_family(route
->src_ip
);
1406 msg
->rtm_dst_len
= route
->prefixlen
;
1407 msg
->rtm_table
= IPSEC_ROUTING_TABLE
;
1408 msg
->rtm_protocol
= RTPROT_STATIC
;
1409 msg
->rtm_type
= RTN_UNICAST
;
1410 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1412 add_attribute(hdr
, RTA_DST
, route
->dst_net
, sizeof(request
));
1413 chunk
= route
->src_ip
->get_address(route
->src_ip
);
1414 add_attribute(hdr
, RTA_PREFSRC
, chunk
, sizeof(request
));
1415 chunk
= route
->gateway
->get_address(route
->gateway
);
1416 add_attribute(hdr
, RTA_GATEWAY
, chunk
, sizeof(request
));
1417 chunk
.ptr
= (char*)&route
->if_index
;
1418 chunk
.len
= sizeof(route
->if_index
);
1419 add_attribute(hdr
, RTA_OIF
, chunk
, sizeof(request
));
1421 return netlink_send_ack(this, this->socket_rt
, hdr
);
1425 * create or delete an rule to use our routing table
1427 static status_t
manage_rule(private_kernel_interface_t
*this, int nlmsg_type
,
1428 u_int32_t table
, u_int32_t prio
)
1430 unsigned char request
[BUFFER_SIZE
];
1431 struct nlmsghdr
*hdr
;
1435 memset(&request
, 0, sizeof(request
));
1436 hdr
= (struct nlmsghdr
*)request
;
1437 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1438 hdr
->nlmsg_type
= nlmsg_type
;
1439 if (nlmsg_type
== RTM_NEWRULE
)
1441 hdr
->nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
1443 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1445 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1446 msg
->rtm_table
= table
;
1447 msg
->rtm_family
= AF_INET
;
1448 msg
->rtm_protocol
= RTPROT_BOOT
;
1449 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1450 msg
->rtm_type
= RTN_UNICAST
;
1452 chunk
= chunk_from_thing(prio
);
1453 add_attribute(hdr
, RTA_PRIORITY
, chunk
, sizeof(request
));
1455 return netlink_send_ack(this, this->socket_rt
, hdr
);
1459 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1461 static bool addr_in_subnet(chunk_t addr
, chunk_t net
, int net_len
)
1465 if (addr
.len
!= net
.len
)
1469 /* scan through all bits, beginning in the front */
1470 for (byte
= 0; byte
< addr
.len
; byte
++)
1472 for (bit
= 7; bit
>= 0; bit
--)
1474 /* check if bits are equal (or we reached the end of the net) */
1475 if (bit
+ byte
* 8 > net_len
)
1479 if (((1<<bit
) & addr
.ptr
[byte
]) != ((1<<bit
) & net
.ptr
[byte
]))
1489 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1491 static host_t
*get_route(private_kernel_interface_t
*this, host_t
*dest
,
1494 unsigned char request
[BUFFER_SIZE
];
1495 struct nlmsghdr
*hdr
, *out
, *current
;
1500 host_t
*src
= NULL
, *gtw
= NULL
;
1502 DBG2(DBG_KNL
, "getting address to reach %H", dest
);
1504 memset(&request
, 0, sizeof(request
));
1506 hdr
= (struct nlmsghdr
*)request
;
1507 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
| NLM_F_ROOT
;
1508 hdr
->nlmsg_type
= RTM_GETROUTE
;
1509 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1511 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1512 msg
->rtm_family
= dest
->get_family(dest
);
1514 chunk
= dest
->get_address(dest
);
1515 add_attribute(hdr
, RTA_DST
, chunk
, sizeof(request
));
1517 if (netlink_send(this, this->socket_rt
, hdr
, &out
, &len
) != SUCCESS
)
1519 DBG1(DBG_KNL
, "getting address to %H failed", dest
);
1523 while (NLMSG_OK(current
, len
))
1525 switch (current
->nlmsg_type
)
1533 chunk_t rta_gtw
, rta_src
, rta_dst
;
1534 u_int32_t rta_oif
= 0;
1536 rta_gtw
= rta_src
= rta_dst
= chunk_empty
;
1537 msg
= (struct rtmsg
*)(NLMSG_DATA(current
));
1539 rtasize
= RTM_PAYLOAD(current
);
1540 while (RTA_OK(rta
, rtasize
))
1542 switch (rta
->rta_type
)
1545 rta_src
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1548 rta_gtw
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1551 rta_dst
= chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1554 if (RTA_PAYLOAD(rta
) == sizeof(rta_oif
))
1556 rta_oif
= *(u_int32_t
*)RTA_DATA(rta
);
1560 rta
= RTA_NEXT(rta
, rtasize
);
1563 /* apply the route if:
1564 * - it is not from our own ipsec routing table
1565 * - is better than a previous one
1566 * - is the default route or
1567 * - its destination net contains our destination
1569 if (msg
->rtm_table
!= IPSEC_ROUTING_TABLE
1570 && msg
->rtm_dst_len
> best
1571 && (msg
->rtm_dst_len
== 0 || /* default route */
1572 (rta_dst
.ptr
&& addr_in_subnet(chunk
, rta_dst
, msg
->rtm_dst_len
))))
1574 iterator_t
*ifaces
, *addrs
;
1575 iface_entry_t
*iface
;
1578 best
= msg
->rtm_dst_len
;
1582 gtw
= host_create_from_chunk(msg
->rtm_family
, rta_gtw
, 0);
1584 else if (rta_src
.ptr
)
1587 src
= host_create_from_chunk(msg
->rtm_family
, rta_src
, 0);
1591 /* no source addr, get one from the interfaces */
1592 ifaces
= this->ifaces
->create_iterator_locked(
1593 this->ifaces
, &this->mutex
);
1594 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1596 if (iface
->ifindex
== rta_oif
)
1598 addrs
= iface
->addrs
->create_iterator(
1599 iface
->addrs
, TRUE
);
1600 while (addrs
->iterate(addrs
, (void**)&addr
))
1602 chunk_t ip
= addr
->ip
->get_address(addr
->ip
);
1603 if (msg
->rtm_dst_len
== 0
1604 || addr_in_subnet(ip
, rta_dst
, msg
->rtm_dst_len
))
1607 src
= addr
->ip
->clone(addr
->ip
);
1611 addrs
->destroy(addrs
);
1614 ifaces
->destroy(ifaces
);
1620 current
= NLMSG_NEXT(current
, len
);
1633 return dest
->clone(dest
);
1639 * Implementation of kernel_interface_t.get_source_addr.
1641 static host_t
* get_source_addr(private_kernel_interface_t
*this, host_t
*dest
)
1643 return get_route(this, dest
, FALSE
);
1647 * Implementation of kernel_interface_t.add_ip.
1649 static status_t
add_ip(private_kernel_interface_t
*this,
1650 host_t
*virtual_ip
, host_t
*iface_ip
)
1652 iface_entry_t
*iface
;
1654 iterator_t
*addrs
, *ifaces
;
1656 DBG2(DBG_KNL
, "adding virtual IP %H", virtual_ip
);
1658 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1659 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1661 bool iface_found
= FALSE
;
1663 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1664 while (addrs
->iterate(addrs
, (void**)&addr
))
1666 if (iface_ip
->ip_equals(iface_ip
, addr
->ip
))
1670 else if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1673 DBG2(DBG_KNL
, "virtual IP %H already installed on %s",
1674 virtual_ip
, iface
->ifname
);
1675 addrs
->destroy(addrs
);
1676 ifaces
->destroy(ifaces
);
1680 addrs
->destroy(addrs
);
1684 int ifindex
= iface
->ifindex
;
1685 ifaces
->destroy(ifaces
);
1686 if (manage_ipaddr(this, RTM_NEWADDR
, NLM_F_CREATE
| NLM_F_EXCL
,
1687 ifindex
, virtual_ip
) == SUCCESS
)
1689 addr
= malloc_thing(addr_entry_t
);
1690 addr
->ip
= virtual_ip
->clone(virtual_ip
);
1692 addr
->virtual = TRUE
;
1693 addr
->scope
= RT_SCOPE_UNIVERSE
;
1694 pthread_mutex_lock(&this->mutex
);
1695 iface
->addrs
->insert_last(iface
->addrs
, addr
);
1696 pthread_mutex_unlock(&this->mutex
);
1699 DBG1(DBG_KNL
, "adding virtual IP %H failed", virtual_ip
);
1705 ifaces
->destroy(ifaces
);
1707 DBG1(DBG_KNL
, "interface address %H not found, unable to install"
1708 "virtual IP %H", iface_ip
, virtual_ip
);
1713 * Implementation of kernel_interface_t.del_ip.
1715 static status_t
del_ip(private_kernel_interface_t
*this, host_t
*virtual_ip
)
1717 iface_entry_t
*iface
;
1719 iterator_t
*addrs
, *ifaces
;
1721 DBG2(DBG_KNL
, "deleting virtual IP %H", virtual_ip
);
1723 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1724 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1726 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1727 while (addrs
->iterate(addrs
, (void**)&addr
))
1729 if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1731 int ifindex
= iface
->ifindex
;
1733 if (addr
->refcount
== 0)
1735 addrs
->remove(addrs
);
1736 addrs
->destroy(addrs
);
1737 ifaces
->destroy(ifaces
);
1738 addr_entry_destroy(addr
);
1739 return manage_ipaddr(this, RTM_DELADDR
, 0,
1740 ifindex
, virtual_ip
);
1742 DBG2(DBG_KNL
, "virtual IP %H used by other SAs, not deleting",
1744 addrs
->destroy(addrs
);
1745 ifaces
->destroy(ifaces
);
1749 addrs
->destroy(addrs
);
1751 ifaces
->destroy(ifaces
);
1753 DBG2(DBG_KNL
, "virtual IP %H not cached, unable to delete", virtual_ip
);
1758 * Implementation of kernel_interface_t.get_spi.
1760 static status_t
get_spi(private_kernel_interface_t
*this,
1761 host_t
*src
, host_t
*dst
,
1762 protocol_id_t protocol
, u_int32_t reqid
,
1765 unsigned char request
[BUFFER_SIZE
];
1766 struct nlmsghdr
*hdr
, *out
;
1767 struct xfrm_userspi_info
*userspi
;
1768 u_int32_t received_spi
= 0;
1771 memset(&request
, 0, sizeof(request
));
1773 DBG2(DBG_KNL
, "getting SPI for reqid %d", reqid
);
1775 hdr
= (struct nlmsghdr
*)request
;
1776 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1777 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1778 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1780 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1781 host2xfrm(src
, &userspi
->info
.saddr
);
1782 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1783 userspi
->info
.id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1784 userspi
->info
.mode
= TRUE
; /* tunnel mode */
1785 userspi
->info
.reqid
= reqid
;
1786 userspi
->info
.family
= src
->get_family(src
);
1787 userspi
->min
= 0xc0000000;
1788 userspi
->max
= 0xcFFFFFFF;
1790 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1793 while (NLMSG_OK(hdr
, len
))
1795 switch (hdr
->nlmsg_type
)
1797 case XFRM_MSG_NEWSA
:
1799 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1800 received_spi
= usersa
->id
.spi
;
1805 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1807 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1808 strerror(-err
->error
), -err
->error
);
1812 hdr
= NLMSG_NEXT(hdr
, len
);
1822 if (received_spi
== 0)
1824 DBG1(DBG_KNL
, "unable to get SPI for reqid %d", reqid
);
1828 DBG2(DBG_KNL
, "got SPI 0x%x for reqid %d", received_spi
, reqid
);
1830 *spi
= received_spi
;
1835 * Implementation of kernel_interface_t.add_sa.
1837 static status_t
add_sa(private_kernel_interface_t
*this,
1838 host_t
*src
, host_t
*dst
, u_int32_t spi
,
1839 protocol_id_t protocol
, u_int32_t reqid
,
1840 u_int64_t expire_soft
, u_int64_t expire_hard
,
1841 algorithm_t
*enc_alg
, algorithm_t
*int_alg
,
1842 prf_plus_t
*prf_plus
, mode_t mode
, bool encap
,
1845 unsigned char request
[BUFFER_SIZE
];
1848 struct nlmsghdr
*hdr
;
1849 struct xfrm_usersa_info
*sa
;
1851 memset(&request
, 0, sizeof(request
));
1853 DBG2(DBG_KNL
, "adding SAD entry with SPI 0x%x", spi
);
1855 hdr
= (struct nlmsghdr
*)request
;
1856 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1857 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1858 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1860 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1861 host2xfrm(src
, &sa
->saddr
);
1862 host2xfrm(dst
, &sa
->id
.daddr
);
1864 sa
->id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1865 sa
->family
= src
->get_family(src
);
1867 sa
->replay_window
= 32;
1869 /* we currently do not expire SAs by volume/packet count */
1870 sa
->lft
.soft_byte_limit
= XFRM_INF
;
1871 sa
->lft
.hard_byte_limit
= XFRM_INF
;
1872 sa
->lft
.soft_packet_limit
= XFRM_INF
;
1873 sa
->lft
.hard_packet_limit
= XFRM_INF
;
1874 /* we use lifetimes since added, not since used */
1875 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
1876 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
1877 sa
->lft
.soft_use_expires_seconds
= 0;
1878 sa
->lft
.hard_use_expires_seconds
= 0;
1880 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1882 if (enc_alg
->algorithm
!= ENCR_UNDEFINED
)
1884 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
1885 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &key_size
);
1886 if (alg_name
== NULL
)
1888 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1889 encryption_algorithm_names
, enc_alg
->algorithm
);
1892 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1893 encryption_algorithm_names
, enc_alg
->algorithm
, key_size
);
1895 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
1896 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1897 if (hdr
->nlmsg_len
> sizeof(request
))
1902 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1903 algo
->alg_key_len
= key_size
;
1904 strcpy(algo
->alg_name
, alg_name
);
1905 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
1907 rthdr
= XFRM_RTA_NEXT(rthdr
);
1910 if (int_alg
->algorithm
!= AUTH_UNDEFINED
)
1912 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1913 alg_name
= lookup_algorithm(integrity_algs
, int_alg
, &key_size
);
1914 if (alg_name
== NULL
)
1916 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1917 integrity_algorithm_names
, int_alg
->algorithm
);
1920 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1921 integrity_algorithm_names
, int_alg
->algorithm
, key_size
);
1923 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
1924 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1925 if (hdr
->nlmsg_len
> sizeof(request
))
1930 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1931 algo
->alg_key_len
= key_size
;
1932 strcpy(algo
->alg_name
, alg_name
);
1933 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
1935 rthdr
= XFRM_RTA_NEXT(rthdr
);
1938 /* TODO: add IPComp here */
1942 rthdr
->rta_type
= XFRMA_ENCAP
;
1943 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1945 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1946 if (hdr
->nlmsg_len
> sizeof(request
))
1951 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1952 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1953 tmpl
->encap_sport
= htons(src
->get_port(src
));
1954 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1955 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1956 /* encap_oa could probably be derived from the
1957 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1958 * pluto does the same as we do here but it uses encap_oa in the
1959 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1960 * the kernel ignores it anyway
1961 * -> does that mean that NAT-T encap doesn't work in transport mode?
1962 * No. The reason the kernel ignores NAT-OA is that it recomputes
1963 * (or, rather, just ignores) the checksum. If packets pass
1964 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1965 rthdr
= XFRM_RTA_NEXT(rthdr
);
1968 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
1970 DBG1(DBG_KNL
, "unable to add SAD entry with SPI 0x%x", spi
);
1977 * Implementation of kernel_interface_t.update_sa.
1979 static status_t
update_sa(private_kernel_interface_t
*this,
1980 u_int32_t spi
, protocol_id_t protocol
,
1981 host_t
*src
, host_t
*dst
,
1982 host_t
*new_src
, host_t
*new_dst
, bool encap
)
1984 unsigned char request
[BUFFER_SIZE
], *pos
;
1985 struct nlmsghdr
*hdr
, *out
= NULL
;
1986 struct xfrm_usersa_id
*sa_id
;
1987 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1991 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1993 memset(&request
, 0, sizeof(request
));
1995 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x for update", spi
);
1997 /* query the exisiting SA first */
1998 hdr
= (struct nlmsghdr
*)request
;
1999 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2000 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2001 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2003 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2004 host2xfrm(dst
, &sa_id
->daddr
);
2006 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
2007 sa_id
->family
= dst
->get_family(dst
);
2009 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2012 while (NLMSG_OK(hdr
, len
))
2014 switch (hdr
->nlmsg_type
)
2016 case XFRM_MSG_NEWSA
:
2018 out_sa
= NLMSG_DATA(hdr
);
2023 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2024 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2025 strerror(-err
->error
), -err
->error
);
2029 hdr
= NLMSG_NEXT(hdr
, len
);
2037 if (out_sa
== NULL
||
2038 this->public.del_sa(&this->public, dst
, spi
, protocol
) != SUCCESS
)
2040 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
2045 DBG2(DBG_KNL
, "updating SAD entry with SPI 0x%x from %#H..%#H to %#H..%#H",
2046 spi
, src
, dst
, new_src
, new_dst
);
2048 /* copy over the SA from out to request */
2049 hdr
= (struct nlmsghdr
*)request
;
2050 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
2051 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2052 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2053 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2054 sa
= NLMSG_DATA(hdr
);
2055 sa
->family
= new_dst
->get_family(new_dst
);
2057 if (!src
->ip_equals(src
, new_src
))
2059 host2xfrm(new_src
, &sa
->saddr
);
2061 if (!dst
->ip_equals(dst
, new_dst
))
2063 host2xfrm(new_dst
, &sa
->id
.daddr
);
2066 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
2067 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
2068 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
2069 while(RTA_OK(rta
, rtasize
))
2071 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2072 if (rta
->rta_type
!= XFRMA_ENCAP
|| encap
)
2074 if (rta
->rta_type
== XFRMA_ENCAP
)
2075 { /* update encap tmpl */
2076 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
2077 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
2078 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
2080 memcpy(pos
, rta
, rta
->rta_len
);
2081 pos
+= rta
->rta_len
;
2082 hdr
->nlmsg_len
+= rta
->rta_len
;
2084 rta
= RTA_NEXT(rta
, rtasize
);
2086 if (tmpl
== NULL
&& encap
)
2087 { /* add tmpl if we are enabling it */
2088 rta
= (struct rtattr
*)pos
;
2089 rta
->rta_type
= XFRMA_ENCAP
;
2090 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
2091 hdr
->nlmsg_len
+= rta
->rta_len
;
2092 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
2093 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2094 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
2095 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
2096 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2099 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2101 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
2111 * Implementation of kernel_interface_t.query_sa.
2113 static status_t
query_sa(private_kernel_interface_t
*this, host_t
*dst
,
2114 u_int32_t spi
, protocol_id_t protocol
,
2115 u_int32_t
*use_time
)
2117 unsigned char request
[BUFFER_SIZE
];
2118 struct nlmsghdr
*out
= NULL
, *hdr
;
2119 struct xfrm_usersa_id
*sa_id
;
2120 struct xfrm_usersa_info
*sa
= NULL
;
2123 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x", spi
);
2124 memset(&request
, 0, sizeof(request
));
2126 hdr
= (struct nlmsghdr
*)request
;
2127 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2128 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2129 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2131 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2132 host2xfrm(dst
, &sa_id
->daddr
);
2134 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
2135 sa_id
->family
= dst
->get_family(dst
);
2137 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2140 while (NLMSG_OK(hdr
, len
))
2142 switch (hdr
->nlmsg_type
)
2144 case XFRM_MSG_NEWSA
:
2146 sa
= NLMSG_DATA(hdr
);
2151 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2152 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2153 strerror(-err
->error
), -err
->error
);
2157 hdr
= NLMSG_NEXT(hdr
, len
);
2168 DBG1(DBG_KNL
, "unable to query SAD entry with SPI 0x%x", spi
);
2173 *use_time
= sa
->curlft
.use_time
;
2179 * Implementation of kernel_interface_t.del_sa.
2181 static status_t
del_sa(private_kernel_interface_t
*this, host_t
*dst
,
2182 u_int32_t spi
, protocol_id_t protocol
)
2184 unsigned char request
[BUFFER_SIZE
];
2185 struct nlmsghdr
*hdr
;
2186 struct xfrm_usersa_id
*sa_id
;
2188 memset(&request
, 0, sizeof(request
));
2190 DBG2(DBG_KNL
, "deleting SAD entry with SPI 0x%x", spi
);
2192 hdr
= (struct nlmsghdr
*)request
;
2193 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2194 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
2195 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2197 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
2198 host2xfrm(dst
, &sa_id
->daddr
);
2200 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
2201 sa_id
->family
= dst
->get_family(dst
);
2203 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2205 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI 0x%x", spi
);
2208 DBG2(DBG_KNL
, "deleted SAD entry with SPI 0x%x", spi
);
2213 * Implementation of kernel_interface_t.add_policy.
2215 static status_t
add_policy(private_kernel_interface_t
*this,
2216 host_t
*src
, host_t
*dst
,
2217 traffic_selector_t
*src_ts
,
2218 traffic_selector_t
*dst_ts
,
2219 policy_dir_t direction
, protocol_id_t protocol
,
2220 u_int32_t reqid
, bool high_prio
, mode_t mode
)
2222 iterator_t
*iterator
;
2223 policy_entry_t
*current
, *policy
;
2225 unsigned char request
[BUFFER_SIZE
];
2226 struct xfrm_userpolicy_info
*policy_info
;
2227 struct nlmsghdr
*hdr
;
2229 /* create a policy */
2230 policy
= malloc_thing(policy_entry_t
);
2231 memset(policy
, 0, sizeof(policy_entry_t
));
2232 policy
->sel
= ts2selector(src_ts
, dst_ts
);
2233 policy
->direction
= direction
;
2235 /* find the policy, which matches EXACTLY */
2236 pthread_mutex_lock(&this->mutex
);
2237 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
2238 while (iterator
->iterate(iterator
, (void**)¤t
))
2240 if (memcmp(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) == 0 &&
2241 policy
->direction
== current
->direction
)
2243 /* use existing policy */
2244 current
->refcount
++;
2245 DBG2(DBG_KNL
, "policy %R===%R already exists, increasing ",
2246 "refcount", src_ts
, dst_ts
);
2253 iterator
->destroy(iterator
);
2255 { /* apply the new one, if we have no such policy */
2256 this->policies
->insert_last(this->policies
, policy
);
2257 policy
->refcount
= 1;
2260 DBG2(DBG_KNL
, "adding policy %R===%R", src_ts
, dst_ts
);
2262 memset(&request
, 0, sizeof(request
));
2263 hdr
= (struct nlmsghdr
*)request
;
2264 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2265 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
2266 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2268 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2269 policy_info
->sel
= policy
->sel
;
2270 policy_info
->dir
= policy
->direction
;
2271 /* calculate priority based on source selector size, small size = high prio */
2272 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
2273 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
2274 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
2275 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
2276 policy_info
->action
= XFRM_POLICY_ALLOW
;
2277 policy_info
->share
= XFRM_SHARE_ANY
;
2278 pthread_mutex_unlock(&this->mutex
);
2280 /* policies don't expire */
2281 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2282 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2283 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2284 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2285 policy_info
->lft
.soft_add_expires_seconds
= 0;
2286 policy_info
->lft
.hard_add_expires_seconds
= 0;
2287 policy_info
->lft
.soft_use_expires_seconds
= 0;
2288 policy_info
->lft
.hard_use_expires_seconds
= 0;
2290 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2291 rthdr
->rta_type
= XFRMA_TMPL
;
2293 rthdr
->rta_len
= sizeof(struct xfrm_user_tmpl
);
2294 rthdr
->rta_len
= RTA_LENGTH(rthdr
->rta_len
);
2296 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2297 if (hdr
->nlmsg_len
> sizeof(request
))
2302 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2303 tmpl
->reqid
= reqid
;
2304 tmpl
->id
.proto
= (protocol
== PROTO_AH
) ? KERNEL_AH
: KERNEL_ESP
;
2305 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2307 tmpl
->family
= src
->get_family(src
);
2309 host2xfrm(src
, &tmpl
->saddr
);
2310 host2xfrm(dst
, &tmpl
->id
.daddr
);
2312 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2314 DBG1(DBG_KNL
, "unable to add policy %R===%R", src_ts
, dst_ts
);
2318 /* install a route, if:
2319 * - we are NOT updating a policy
2320 * - this is a forward policy (to just get one for each child)
2321 * - we are in tunnel mode
2322 * - we are not using IPv6 (does not work correctly yet!)
2324 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
2325 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
)
2327 policy
->route
= malloc_thing(route_entry_t
);
2328 if (get_address_by_ts(this, dst_ts
, &policy
->route
->src_ip
) == SUCCESS
)
2330 /* get the nexthop to src (src as we are in POLICY_FWD).*/
2331 policy
->route
->gateway
= get_route(this, src
, TRUE
);
2332 policy
->route
->if_index
= get_interface_index(this, dst
);
2333 policy
->route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2334 memcpy(policy
->route
->dst_net
.ptr
, &policy
->sel
.saddr
, policy
->route
->dst_net
.len
);
2335 policy
->route
->prefixlen
= policy
->sel
.prefixlen_s
;
2337 if (manage_srcroute(this, RTM_NEWROUTE
, NLM_F_CREATE
| NLM_F_EXCL
,
2338 policy
->route
) != SUCCESS
)
2340 DBG1(DBG_KNL
, "unable to install source route for %H",
2341 policy
->route
->src_ip
);
2342 route_entry_destroy(policy
->route
);
2343 policy
->route
= NULL
;
2348 free(policy
->route
);
2349 policy
->route
= NULL
;
2357 * Implementation of kernel_interface_t.query_policy.
2359 static status_t
query_policy(private_kernel_interface_t
*this,
2360 traffic_selector_t
*src_ts
,
2361 traffic_selector_t
*dst_ts
,
2362 policy_dir_t direction
, u_int32_t
*use_time
)
2364 unsigned char request
[BUFFER_SIZE
];
2365 struct nlmsghdr
*out
= NULL
, *hdr
;
2366 struct xfrm_userpolicy_id
*policy_id
;
2367 struct xfrm_userpolicy_info
*policy
= NULL
;
2370 memset(&request
, 0, sizeof(request
));
2372 DBG2(DBG_KNL
, "querying policy %R===%R", src_ts
, dst_ts
);
2374 hdr
= (struct nlmsghdr
*)request
;
2375 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2376 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2377 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2379 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2380 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2381 policy_id
->dir
= direction
;
2383 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2386 while (NLMSG_OK(hdr
, len
))
2388 switch (hdr
->nlmsg_type
)
2390 case XFRM_MSG_NEWPOLICY
:
2392 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2397 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2398 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2399 strerror(-err
->error
), -err
->error
);
2403 hdr
= NLMSG_NEXT(hdr
, len
);
2414 DBG2(DBG_KNL
, "unable to query policy %R===%R", src_ts
, dst_ts
);
2418 *use_time
= (time_t)policy
->curlft
.use_time
;
2425 * Implementation of kernel_interface_t.del_policy.
2427 static status_t
del_policy(private_kernel_interface_t
*this,
2428 traffic_selector_t
*src_ts
,
2429 traffic_selector_t
*dst_ts
,
2430 policy_dir_t direction
)
2432 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
2433 route_entry_t
*route
;
2434 unsigned char request
[BUFFER_SIZE
];
2435 struct nlmsghdr
*hdr
;
2436 struct xfrm_userpolicy_id
*policy_id
;
2437 iterator_t
*iterator
;
2439 DBG2(DBG_KNL
, "deleting policy %R===%R", src_ts
, dst_ts
);
2441 /* create a policy */
2442 memset(&policy
, 0, sizeof(policy_entry_t
));
2443 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2444 policy
.direction
= direction
;
2446 /* find the policy */
2447 iterator
= this->policies
->create_iterator_locked(this->policies
, &this->mutex
);
2448 while (iterator
->iterate(iterator
, (void**)¤t
))
2450 if (memcmp(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) == 0 &&
2451 policy
.direction
== current
->direction
)
2453 to_delete
= current
;
2454 if (--to_delete
->refcount
> 0)
2456 /* is used by more SAs, keep in kernel */
2457 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2458 iterator
->destroy(iterator
);
2461 /* remove if last reference */
2462 iterator
->remove(iterator
);
2466 iterator
->destroy(iterator
);
2469 DBG1(DBG_KNL
, "deleting policy %R===%R failed, not found", src_ts
, dst_ts
);
2473 memset(&request
, 0, sizeof(request
));
2475 hdr
= (struct nlmsghdr
*)request
;
2476 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2477 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2478 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2480 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2481 policy_id
->sel
= to_delete
->sel
;
2482 policy_id
->dir
= direction
;
2484 route
= to_delete
->route
;
2487 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2489 DBG1(DBG_KNL
, "unable to delete policy %R===%R", src_ts
, dst_ts
);
2495 if (manage_srcroute(this, RTM_DELROUTE
, 0, route
) != SUCCESS
)
2497 DBG1(DBG_KNL
, "error uninstalling route installed with "
2498 "policy %R===%R", src_ts
, dst_ts
);
2500 route_entry_destroy(route
);
2506 * Implementation of kernel_interface_t.destroy.
2508 static void destroy(private_kernel_interface_t
*this)
2510 manage_rule(this, RTM_DELRULE
, IPSEC_ROUTING_TABLE
, IPSEC_ROUTING_TABLE_PRIO
);
2512 this->job
->cancel(this->job
);
2513 close(this->socket_xfrm_events
);
2514 close(this->socket_xfrm
);
2515 close(this->socket_rt_events
);
2516 close(this->socket_rt
);
2517 this->policies
->destroy(this->policies
);
2518 this->ifaces
->destroy_function(this->ifaces
, (void*)iface_entry_destroy
);
2523 * Described in header.
2525 kernel_interface_t
*kernel_interface_create()
2527 private_kernel_interface_t
*this = malloc_thing(private_kernel_interface_t
);
2528 struct sockaddr_nl addr
;
2530 /* public functions */
2531 this->public.get_spi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
2532 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
;
2533 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
;
2534 this->public.query_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
*))query_sa
;
2535 this->public.del_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
2536 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
;
2537 this->public.query_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
2538 this->public.del_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
2539 this->public.get_interface
= (char*(*)(kernel_interface_t
*,host_t
*))get_interface_name
;
2540 this->public.create_address_iterator
= (iterator_t
*(*)(kernel_interface_t
*))create_address_iterator
;
2541 this->public.get_source_addr
= (host_t
*(*)(kernel_interface_t
*, host_t
*dest
))get_source_addr
;
2542 this->public.add_ip
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*)) add_ip
;
2543 this->public.del_ip
= (status_t(*)(kernel_interface_t
*,host_t
*)) del_ip
;
2544 this->public.destroy
= (void(*)(kernel_interface_t
*)) destroy
;
2546 /* private members */
2547 this->policies
= linked_list_create();
2548 this->ifaces
= linked_list_create();
2551 pthread_mutex_init(&this->mutex
,NULL
);
2552 timerclear(&this->last_roam
);
2554 memset(&addr
, 0, sizeof(addr
));
2555 addr
.nl_family
= AF_NETLINK
;
2557 /* create and bind RT socket */
2558 this->socket_rt
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2559 if (this->socket_rt
<= 0)
2561 charon
->kill(charon
, "unable to create RT netlink socket");
2564 if (bind(this->socket_rt
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2566 charon
->kill(charon
, "unable to bind RT netlink socket");
2569 /* create and bind RT socket for events (address/interface/route changes) */
2570 this->socket_rt_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2571 if (this->socket_rt_events
<= 0)
2573 charon
->kill(charon
, "unable to create RT event socket");
2575 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR
|
2576 RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_ROUTE
| RTMGRP_LINK
;
2577 if (bind(this->socket_rt_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2579 charon
->kill(charon
, "unable to bind RT event socket");
2582 /* create and bind XFRM socket */
2583 this->socket_xfrm
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2584 if (this->socket_xfrm
<= 0)
2586 charon
->kill(charon
, "unable to create XFRM netlink socket");
2589 if (bind(this->socket_xfrm
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2591 charon
->kill(charon
, "unable to bind XFRM netlink socket");
2594 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
2595 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2596 if (this->socket_xfrm_events
<= 0)
2598 charon
->kill(charon
, "unable to create XFRM event socket");
2600 addr
.nl_groups
= XFRMGRP_ACQUIRE
| XFRMGRP_EXPIRE
;
2601 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2603 charon
->kill(charon
, "unable to bind XFRM event socket");
2606 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
2608 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
2610 if (init_address_list(this) != SUCCESS
)
2612 charon
->kill(charon
, "unable to get interface list");
2615 if (manage_rule(this, RTM_NEWRULE
, IPSEC_ROUTING_TABLE
,
2616 IPSEC_ROUTING_TABLE_PRIO
) != SUCCESS
)
2618 DBG1(DBG_KNL
, "unable to create routing table rule");
2621 return &this->public;