2 * Copyright (C) 2006-2016 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2008-2016 Andreas Steffen
5 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
6 * Copyright (C) 2006 Daniel Roethlisberger
7 * Copyright (C) 2005 Jan Hutter
8 * HSR Hochschule fuer Technik Rapperswil
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <linux/ipsec.h>
26 #include <linux/netlink.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/xfrm.h>
29 #include <linux/udp.h>
38 #include "kernel_netlink_ipsec.h"
39 #include "kernel_netlink_shared.h"
42 #include <utils/debug.h>
43 #include <threading/mutex.h>
44 #include <threading/condvar.h>
45 #include <collections/array.h>
46 #include <collections/hashtable.h>
47 #include <collections/linked_list.h>
49 /** Required for Linux 2.6.26 kernel and later */
50 #ifndef XFRM_STATE_AF_UNSPEC
51 #define XFRM_STATE_AF_UNSPEC 32
54 /** From linux/in.h */
55 #ifndef IP_XFRM_POLICY
56 #define IP_XFRM_POLICY 17
59 /** Missing on uclibc */
60 #ifndef IPV6_XFRM_POLICY
61 #define IPV6_XFRM_POLICY 34
62 #endif /*IPV6_XFRM_POLICY*/
64 /* from linux/udp.h */
69 #ifndef UDP_ENCAP_ESPINUDP
70 #define UDP_ENCAP_ESPINUDP 2
73 /* this is not defined on some platforms */
75 #define SOL_UDP IPPROTO_UDP
78 /** Base priority for installed policies */
79 #define PRIO_BASE 100000
81 /** Default lifetime of an acquire XFRM state (in seconds) */
82 #define DEFAULT_ACQUIRE_LIFETIME 165
85 * Map the limit for bytes and packets to XFRM_INF by default
87 #define XFRM_LIMIT(x) ((x) == 0 ? XFRM_INF : (x))
90 * Create ORable bitfield of XFRM NL groups
92 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
95 * Returns a pointer to the first rtattr following the nlmsghdr *nlh and the
96 * 'usual' netlink data x like 'struct xfrm_usersa_info'
98 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + \
99 NLMSG_ALIGN(sizeof(x))))
101 * Returns the total size of attached rta data
102 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
104 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
106 typedef struct kernel_algorithm_t kernel_algorithm_t
;
109 * Mapping of IKEv2 kernel identifier to linux crypto API names
111 struct kernel_algorithm_t
{
113 * Identifier specified in IKEv2
118 * Name of the algorithm in linux crypto API
123 ENUM(xfrm_msg_names
, XFRM_MSG_NEWSA
, XFRM_MSG_MAPPING
,
127 "XFRM_MSG_NEWPOLICY",
128 "XFRM_MSG_DELPOLICY",
129 "XFRM_MSG_GETPOLICY",
133 "XFRM_MSG_UPDPOLICY",
135 "XFRM_MSG_POLEXPIRE",
137 "XFRM_MSG_FLUSHPOLICY",
142 "XFRM_MSG_NEWSADINFO",
143 "XFRM_MSG_GETSADINFO",
144 "XFRM_MSG_NEWSPDINFO",
145 "XFRM_MSG_GETSPDINFO",
149 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_REPLAY_ESN_VAL
,
161 "XFRMA_REPLAY_THRESH",
162 "XFRMA_ETIMER_THRESH",
170 "XFRMA_ALG_AUTH_TRUNC",
173 "XFRMA_REPLAY_ESN_VAL",
177 * Algorithms for encryption
179 static kernel_algorithm_t encryption_algs
[] = {
180 /* {ENCR_DES_IV64, "***" }, */
182 {ENCR_3DES
, "des3_ede" },
183 /* {ENCR_RC5, "***" }, */
184 /* {ENCR_IDEA, "***" }, */
185 {ENCR_CAST
, "cast5" },
186 {ENCR_BLOWFISH
, "blowfish" },
187 /* {ENCR_3IDEA, "***" }, */
188 /* {ENCR_DES_IV32, "***" }, */
189 {ENCR_NULL
, "cipher_null" },
190 {ENCR_AES_CBC
, "aes" },
191 {ENCR_AES_CTR
, "rfc3686(ctr(aes))" },
192 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
193 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
194 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
195 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
196 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
197 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
198 {ENCR_NULL_AUTH_AES_GMAC
, "rfc4543(gcm(aes))" },
199 {ENCR_CAMELLIA_CBC
, "cbc(camellia)" },
200 /* {ENCR_CAMELLIA_CTR, "***" }, */
201 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
202 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
203 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
204 {ENCR_SERPENT_CBC
, "serpent" },
205 {ENCR_TWOFISH_CBC
, "twofish" },
206 {ENCR_CHACHA20_POLY1305
, "rfc7539esp(chacha20,poly1305)"},
210 * Algorithms for integrity protection
212 static kernel_algorithm_t integrity_algs
[] = {
213 {AUTH_HMAC_MD5_96
, "md5" },
214 {AUTH_HMAC_MD5_128
, "hmac(md5)" },
215 {AUTH_HMAC_SHA1_96
, "sha1" },
216 {AUTH_HMAC_SHA1_160
, "hmac(sha1)" },
217 {AUTH_HMAC_SHA2_256_96
, "sha256" },
218 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
219 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
220 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
221 /* {AUTH_DES_MAC, "***" }, */
222 /* {AUTH_KPDK_MD5, "***" }, */
223 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
227 * Algorithms for IPComp
229 static kernel_algorithm_t compression_algs
[] = {
230 /* {IPCOMP_OUI, "***" }, */
231 {IPCOMP_DEFLATE
, "deflate" },
232 {IPCOMP_LZS
, "lzs" },
233 {IPCOMP_LZJH
, "lzjh" },
237 * Look up a kernel algorithm name and its key size
239 static char* lookup_algorithm(transform_type_t type
, int ikev2
)
241 kernel_algorithm_t
*list
;
247 case ENCRYPTION_ALGORITHM
:
248 list
= encryption_algs
;
249 count
= countof(encryption_algs
);
251 case INTEGRITY_ALGORITHM
:
252 list
= integrity_algs
;
253 count
= countof(integrity_algs
);
255 case COMPRESSION_ALGORITHM
:
256 list
= compression_algs
;
257 count
= countof(compression_algs
);
262 for (i
= 0; i
< count
; i
++)
264 if (list
[i
].ikev2
== ikev2
)
269 if (charon
->kernel
->lookup_algorithm(charon
->kernel
, ikev2
, type
, NULL
,
277 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
280 * Private variables and functions of kernel_netlink class.
282 struct private_kernel_netlink_ipsec_t
{
284 * Public part of the kernel_netlink_t object
286 kernel_netlink_ipsec_t
public;
289 * Mutex to lock access to installed policies
294 * Condvar to synchronize access to individual policies
299 * Hash table of installed policies (policy_entry_t)
301 hashtable_t
*policies
;
304 * Hash table of IPsec SAs using policies (ipsec_sa_t)
309 * Netlink xfrm socket (IPsec)
311 netlink_socket_t
*socket_xfrm
;
314 * Netlink xfrm socket to receive acquire and expire events
316 int socket_xfrm_events
;
319 * Whether to install routes along policies
324 * Whether to set protocol and ports on selector installed with transport
327 bool proto_port_transport
;
330 * Whether to always use UPDATE to install policies
335 * Installed port based IKE bypass policies, as bypass_t
340 * Custom priority calculation function
342 uint32_t (*get_priority
)(kernel_ipsec_policy_id_t
*id
,
343 kernel_ipsec_manage_policy_t
*data
);
346 typedef struct route_entry_t route_entry_t
;
349 * Installed routing entry
351 struct route_entry_t
{
352 /** Name of the interface the route is bound to */
355 /** Source ip of the route */
358 /** Gateway for this route */
361 /** Destination net */
364 /** Destination net prefixlen */
369 * Destroy a route_entry_t object
371 static void route_entry_destroy(route_entry_t
*this)
374 this->src_ip
->destroy(this->src_ip
);
375 DESTROY_IF(this->gateway
);
376 chunk_free(&this->dst_net
);
381 * Compare two route_entry_t objects
383 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
385 return a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
386 a
->src_ip
->ip_equals(a
->src_ip
, b
->src_ip
) &&
387 a
->gateway
->ip_equals(a
->gateway
, b
->gateway
) &&
388 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
;
391 typedef struct ipsec_sa_t ipsec_sa_t
;
394 * IPsec SA assigned to a policy.
397 /** Source address of this SA */
400 /** Destination address of this SA */
406 /** Description of this SA */
409 /** Reference count for this SA */
414 * Hash function for ipsec_sa_t objects
416 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
418 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
419 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
420 chunk_hash_inc(chunk_from_thing(sa
->mark
),
421 chunk_hash(chunk_from_thing(sa
->cfg
)))));
425 * Equality function for ipsec_sa_t objects
427 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
429 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
430 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
431 sa
->mark
.value
== other_sa
->mark
.value
&&
432 sa
->mark
.mask
== other_sa
->mark
.mask
&&
433 ipsec_sa_cfg_equals(&sa
->cfg
, &other_sa
->cfg
);
437 * Allocate or reference an IPsec SA object
439 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
440 host_t
*src
, host_t
*dst
, mark_t mark
,
443 ipsec_sa_t
*sa
, *found
;
450 found
= this->sas
->get(this->sas
, sa
);
453 sa
->src
= src
->clone(src
);
454 sa
->dst
= dst
->clone(dst
);
455 this->sas
->put(this->sas
, sa
, sa
);
462 ref_get(&sa
->refcount
);
467 * Release and destroy an IPsec SA object
469 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
472 if (ref_put(&sa
->refcount
))
474 this->sas
->remove(this->sas
, sa
);
481 typedef struct policy_sa_t policy_sa_t
;
482 typedef struct policy_sa_in_t policy_sa_in_t
;
485 * Mapping between a policy and an IPsec SA.
488 /** Priority assigned to the policy when installed with this SA */
491 /** Automatic priority assigned to the policy when installed with this SA */
492 uint32_t auto_priority
;
494 /** Type of the policy */
502 * For inbound policies we also cache the traffic selectors in order to install
505 struct policy_sa_in_t
{
506 /** Generic interface */
509 /** Source traffic selector of this policy */
510 traffic_selector_t
*src_ts
;
512 /** Destination traffic selector of this policy */
513 traffic_selector_t
*dst_ts
;
517 * Create a policy_sa(_in)_t object
519 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
520 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
521 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
526 if (dir
== POLICY_IN
)
530 .src_ts
= src_ts
->clone(src_ts
),
531 .dst_ts
= dst_ts
->clone(dst_ts
),
533 policy
= &in
->generic
;
537 INIT(policy
, .priority
= 0);
540 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, cfg
);
545 * Destroy a policy_sa(_in)_t object
547 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t
*dir
,
548 private_kernel_netlink_ipsec_t
*this)
550 if (*dir
== POLICY_IN
)
552 policy_sa_in_t
*in
= (policy_sa_in_t
*)policy
;
553 in
->src_ts
->destroy(in
->src_ts
);
554 in
->dst_ts
->destroy(in
->dst_ts
);
556 ipsec_sa_destroy(this, policy
->sa
);
560 typedef struct policy_entry_t policy_entry_t
;
563 * Installed kernel policy.
565 struct policy_entry_t
{
567 /** Direction of this policy: in, out, forward */
570 /** Parameters of installed policy */
571 struct xfrm_selector sel
;
576 /** Associated route installed for this policy */
577 route_entry_t
*route
;
579 /** List of SAs this policy is used by, ordered by priority */
580 linked_list_t
*used_by
;
582 /** reqid for this policy */
585 /** Number of threads waiting to work on this policy */
588 /** TRUE if a thread is working on this policy */
593 * Destroy a policy_entry_t object
595 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
596 policy_entry_t
*policy
)
600 route_entry_destroy(policy
->route
);
604 policy
->used_by
->invoke_function(policy
->used_by
,
605 (linked_list_invoke_t
)policy_sa_destroy
,
606 &policy
->direction
, this);
607 policy
->used_by
->destroy(policy
->used_by
);
613 * Hash function for policy_entry_t objects
615 static u_int
policy_hash(policy_entry_t
*key
)
617 chunk_t chunk
= chunk_from_thing(key
->sel
);
618 return chunk_hash_inc(chunk
, chunk_hash(chunk_from_thing(key
->mark
)));
622 * Equality function for policy_entry_t objects
624 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
626 return memeq(&key
->sel
, &other_key
->sel
, sizeof(struct xfrm_selector
)) &&
627 key
->mark
== other_key
->mark
&&
628 key
->direction
== other_key
->direction
;
632 * Determine number of set bits in 16 bit port mask
634 static inline uint32_t port_mask_bits(uint16_t port_mask
)
637 uint16_t bit_mask
= 0x8000;
639 port_mask
= ntohs(port_mask
);
641 for (bits
= 0; bits
< 16; bits
++)
643 if (!(port_mask
& bit_mask
))
653 * Calculate the priority of a policy
655 * bits 0-0: restriction to network interface (0..1) 1 bit
656 * bits 1-6: src + dst port mask bits (2 * 0..16) 6 bits
657 * bits 7-7: restriction to protocol (0..1) 1 bit
658 * bits 8-16: src + dst network mask bits (2 * 0..128) 9 bits
661 * smallest value: 000000000 0 000000 0: 0, lowest priority = 100'000
662 * largest value : 100000000 1 100000 1: 65'729, highst priority = 34'271
664 static uint32_t get_priority(policy_entry_t
*policy
, policy_priority_t prio
,
667 uint32_t priority
= PRIO_BASE
, sport_mask_bits
, dport_mask_bits
;
671 case POLICY_PRIORITY_FALLBACK
:
672 priority
+= PRIO_BASE
;
673 /* fall-through to next case */
674 case POLICY_PRIORITY_ROUTED
:
675 priority
+= PRIO_BASE
;
676 /* fall-through to next case */
677 case POLICY_PRIORITY_DEFAULT
:
678 priority
+= PRIO_BASE
;
679 /* fall-through to next case */
680 case POLICY_PRIORITY_PASS
:
683 sport_mask_bits
= port_mask_bits(policy
->sel
.sport_mask
);
684 dport_mask_bits
= port_mask_bits(policy
->sel
.dport_mask
);
686 /* calculate priority */
687 priority
-= (policy
->sel
.prefixlen_s
+ policy
->sel
.prefixlen_d
) * 256;
688 priority
-= policy
->sel
.proto ?
128 : 0;
689 priority
-= (sport_mask_bits
+ dport_mask_bits
) * 2;
690 priority
-= (interface
!= NULL
);
696 * Convert the general ipsec mode to the one defined in xfrm.h
698 static uint8_t mode2kernel(ipsec_mode_t mode
)
703 return XFRM_MODE_TRANSPORT
;
705 return XFRM_MODE_TUNNEL
;
707 return XFRM_MODE_BEET
;
714 * Convert a host_t to a struct xfrm_address
716 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
718 chunk_t chunk
= host
->get_address(host
);
719 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
723 * Convert a struct xfrm_address to a host_t
725 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, uint16_t port
)
732 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
735 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
740 return host_create_from_chunk(family
, chunk
, ntohs(port
));
744 * Convert a traffic selector address range to subnet and its mask.
746 static void ts2subnet(traffic_selector_t
* ts
,
747 xfrm_address_t
*net
, uint8_t *mask
)
752 ts
->to_subnet(ts
, &net_host
, mask
);
753 net_chunk
= net_host
->get_address(net_host
);
754 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
755 net_host
->destroy(net_host
);
759 * Convert a traffic selector port range to port/portmask
761 static void ts2ports(traffic_selector_t
* ts
,
762 uint16_t *port
, uint16_t *mask
)
764 uint16_t from
, to
, bitmask
;
767 from
= ts
->get_from_port(ts
);
768 to
= ts
->get_to_port(ts
);
770 /* Quick check for a single port */
778 /* Compute the port mask for port ranges */
781 for (bit
= 15; bit
>= 0; bit
--)
785 if ((bitmask
& from
) != (bitmask
& to
))
787 *port
= htons(from
& *mask
);
788 *mask
= htons(*mask
);
798 * Convert a pair of traffic_selectors to an xfrm_selector
800 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
801 traffic_selector_t
*dst
,
804 struct xfrm_selector sel
;
807 memset(&sel
, 0, sizeof(sel
));
808 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
809 /* src or dest proto may be "any" (0), use more restrictive one */
810 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
811 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
812 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
813 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
814 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
815 if ((sel
.proto
== IPPROTO_ICMP
|| sel
.proto
== IPPROTO_ICMPV6
) &&
816 (sel
.dport
|| sel
.sport
))
818 /* the kernel expects the ICMP type and code in the source and
819 * destination port fields, respectively. */
820 port
= ntohs(max(sel
.dport
, sel
.sport
));
821 sel
.sport
= htons(traffic_selector_icmp_type(port
));
822 sel
.sport_mask
= sel
.sport ?
~0 : 0;
823 sel
.dport
= htons(traffic_selector_icmp_code(port
));
824 sel
.dport_mask
= sel
.dport ?
~0 : 0;
826 sel
.ifindex
= interface ?
if_nametoindex(interface
) : 0;
833 * Convert an xfrm_selector to a src|dst traffic_selector
835 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
844 addr
= (u_char
*)&sel
->saddr
;
845 prefixlen
= sel
->prefixlen_s
;
848 port
= ntohs(sel
->sport
);
853 addr
= (u_char
*)&sel
->daddr
;
854 prefixlen
= sel
->prefixlen_d
;
857 port
= ntohs(sel
->dport
);
860 if (sel
->proto
== IPPROTO_ICMP
|| sel
->proto
== IPPROTO_ICMPV6
)
861 { /* convert ICMP[v6] message type and code as supplied by the kernel in
862 * source and destination ports (both in network order) */
863 port
= (sel
->sport
>> 8) | (sel
->dport
& 0xff00);
866 /* The Linux 2.6 kernel does not set the selector's family field,
867 * so as a kludge we additionally test the prefix length.
869 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
871 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
873 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
875 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
880 return traffic_selector_create_from_subnet(host
, prefixlen
,
881 sel
->proto
, port
, port ?
: 65535);
887 * Process a XFRM_MSG_ACQUIRE from kernel
889 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
890 struct nlmsghdr
*hdr
)
892 struct xfrm_user_acquire
*acquire
;
895 traffic_selector_t
*src_ts
, *dst_ts
;
899 acquire
= NLMSG_DATA(hdr
);
900 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
901 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
903 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
905 while (RTA_OK(rta
, rtasize
))
907 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
909 if (rta
->rta_type
== XFRMA_TMPL
)
911 struct xfrm_user_tmpl
* tmpl
;
912 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
914 proto
= tmpl
->id
.proto
;
916 rta
= RTA_NEXT(rta
, rtasize
);
925 /* acquire for AH/ESP only, not for IPCOMP */
928 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
929 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
931 charon
->kernel
->acquire(charon
->kernel
, reqid
, src_ts
, dst_ts
);
935 * Process a XFRM_MSG_EXPIRE from kernel
937 static void process_expire(private_kernel_netlink_ipsec_t
*this,
938 struct nlmsghdr
*hdr
)
940 struct xfrm_user_expire
*expire
;
945 expire
= NLMSG_DATA(hdr
);
946 protocol
= expire
->state
.id
.proto
;
947 spi
= expire
->state
.id
.spi
;
949 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
951 if (protocol
== IPPROTO_ESP
|| protocol
== IPPROTO_AH
)
953 dst
= xfrm2host(expire
->state
.family
, &expire
->state
.id
.daddr
, 0);
956 charon
->kernel
->expire(charon
->kernel
, protocol
, spi
, dst
,
964 * Process a XFRM_MSG_MIGRATE from kernel
966 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
967 struct nlmsghdr
*hdr
)
969 struct xfrm_userpolicy_id
*policy_id
;
972 traffic_selector_t
*src_ts
, *dst_ts
;
973 host_t
*local
= NULL
, *remote
= NULL
;
974 host_t
*old_src
= NULL
, *old_dst
= NULL
;
975 host_t
*new_src
= NULL
, *new_dst
= NULL
;
979 policy_id
= NLMSG_DATA(hdr
);
980 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
981 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
983 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
985 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
986 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
987 dir
= (policy_dir_t
)policy_id
->dir
;
989 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
991 while (RTA_OK(rta
, rtasize
))
993 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
994 if (rta
->rta_type
== XFRMA_KMADDRESS
)
996 struct xfrm_user_kmaddress
*kmaddress
;
998 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
999 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
1000 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
1001 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
1003 else if (rta
->rta_type
== XFRMA_MIGRATE
)
1005 struct xfrm_user_migrate
*migrate
;
1007 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
1008 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
1009 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
1010 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
1011 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
1012 reqid
= migrate
->reqid
;
1013 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
1014 old_src
, old_dst
, new_src
, new_dst
, reqid
);
1015 DESTROY_IF(old_src
);
1016 DESTROY_IF(old_dst
);
1017 DESTROY_IF(new_src
);
1018 DESTROY_IF(new_dst
);
1020 rta
= RTA_NEXT(rta
, rtasize
);
1023 if (src_ts
&& dst_ts
&& local
&& remote
)
1025 charon
->kernel
->migrate(charon
->kernel
, reqid
, src_ts
, dst_ts
, dir
,
1038 * Process a XFRM_MSG_MAPPING from kernel
1040 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
1041 struct nlmsghdr
*hdr
)
1043 struct xfrm_user_mapping
*mapping
;
1046 mapping
= NLMSG_DATA(hdr
);
1047 spi
= mapping
->id
.spi
;
1049 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
1051 if (mapping
->id
.proto
== IPPROTO_ESP
)
1055 dst
= xfrm2host(mapping
->id
.family
, &mapping
->id
.daddr
, 0);
1058 new = xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
1059 mapping
->new_sport
);
1062 charon
->kernel
->mapping(charon
->kernel
, IPPROTO_ESP
, spi
, dst
,
1072 * Receives events from kernel
1074 static bool receive_events(private_kernel_netlink_ipsec_t
*this, int fd
,
1075 watcher_event_t event
)
1077 char response
[1024];
1078 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
1079 struct sockaddr_nl addr
;
1080 socklen_t addr_len
= sizeof(addr
);
1083 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
),
1084 MSG_DONTWAIT
, (struct sockaddr
*)&addr
, &addr_len
);
1090 /* interrupted, try again */
1093 /* no data ready, select again */
1096 DBG1(DBG_KNL
, "unable to receive from XFRM event socket: %s "
1097 "(%d)", strerror(errno
), errno
);
1103 if (addr
.nl_pid
!= 0)
1104 { /* not from kernel. not interested, try another one */
1108 while (NLMSG_OK(hdr
, len
))
1110 switch (hdr
->nlmsg_type
)
1112 case XFRM_MSG_ACQUIRE
:
1113 process_acquire(this, hdr
);
1115 case XFRM_MSG_EXPIRE
:
1116 process_expire(this, hdr
);
1118 case XFRM_MSG_MIGRATE
:
1119 process_migrate(this, hdr
);
1121 case XFRM_MSG_MAPPING
:
1122 process_mapping(this, hdr
);
1125 DBG1(DBG_KNL
, "received unknown event from XFRM event "
1126 "socket: %d", hdr
->nlmsg_type
);
1129 hdr
= NLMSG_NEXT(hdr
, len
);
1134 METHOD(kernel_ipsec_t
, get_features
, kernel_feature_t
,
1135 private_kernel_netlink_ipsec_t
*this)
1137 return KERNEL_ESP_V3_TFC
;
1141 * Get an SPI for a specific protocol from the kernel.
1143 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
1144 host_t
*src
, host_t
*dst
, uint8_t proto
, uint32_t min
, uint32_t max
,
1147 netlink_buf_t request
;
1148 struct nlmsghdr
*hdr
, *out
;
1149 struct xfrm_userspi_info
*userspi
;
1150 uint32_t received_spi
= 0;
1153 memset(&request
, 0, sizeof(request
));
1156 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1157 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1158 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1160 userspi
= NLMSG_DATA(hdr
);
1161 host2xfrm(src
, &userspi
->info
.saddr
);
1162 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1163 userspi
->info
.id
.proto
= proto
;
1164 userspi
->info
.mode
= XFRM_MODE_TUNNEL
;
1165 userspi
->info
.family
= src
->get_family(src
);
1169 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1172 while (NLMSG_OK(hdr
, len
))
1174 switch (hdr
->nlmsg_type
)
1176 case XFRM_MSG_NEWSA
:
1178 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1179 received_spi
= usersa
->id
.spi
;
1184 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1185 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1186 strerror(-err
->error
), -err
->error
);
1190 hdr
= NLMSG_NEXT(hdr
, len
);
1200 if (received_spi
== 0)
1205 *spi
= received_spi
;
1209 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1210 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1211 uint8_t protocol
, uint32_t *spi
)
1213 if (get_spi_internal(this, src
, dst
, protocol
,
1214 0xc0000000, 0xcFFFFFFF, spi
) != SUCCESS
)
1216 DBG1(DBG_KNL
, "unable to get SPI");
1220 DBG2(DBG_KNL
, "got SPI %.8x", ntohl(*spi
));
1224 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1225 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1228 uint32_t received_spi
= 0;
1230 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1231 0x100, 0xEFFF, &received_spi
) != SUCCESS
)
1233 DBG1(DBG_KNL
, "unable to get CPI");
1237 *cpi
= htons((uint16_t)ntohl(received_spi
));
1239 DBG2(DBG_KNL
, "got CPI %.4x", ntohs(*cpi
));
1244 * Format the mark for debug messages
1246 static void format_mark(char *buf
, int buflen
, mark_t mark
)
1250 snprintf(buf
, buflen
, " (mark %u/0x%08x)", mark
.value
, mark
.mask
);
1255 * Add a XFRM mark to message if required
1257 static bool add_mark(struct nlmsghdr
*hdr
, int buflen
, mark_t mark
)
1261 struct xfrm_mark
*xmrk
;
1263 xmrk
= netlink_reserve(hdr
, buflen
, XFRMA_MARK
, sizeof(*xmrk
));
1268 xmrk
->v
= mark
.value
;
1269 xmrk
->m
= mark
.mask
;
1274 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1275 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1276 kernel_ipsec_add_sa_t
*data
)
1278 netlink_buf_t request
;
1279 char *alg_name
, markstr
[32] = "";
1280 struct nlmsghdr
*hdr
;
1281 struct xfrm_usersa_info
*sa
;
1282 uint16_t icv_size
= 64, ipcomp
= data
->ipcomp
;
1283 ipsec_mode_t mode
= data
->mode
, original_mode
= data
->mode
;
1284 traffic_selector_t
*first_src_ts
, *first_dst_ts
;
1285 status_t status
= FAILED
;
1287 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1288 * we are in the recursive call below */
1289 if (ipcomp
!= IPCOMP_NONE
&& data
->cpi
!= 0)
1291 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1292 kernel_ipsec_sa_id_t ipcomp_id
= {
1295 .spi
= htonl(ntohs(data
->cpi
)),
1296 .proto
= IPPROTO_COMP
,
1299 kernel_ipsec_add_sa_t ipcomp_sa
= {
1300 .reqid
= data
->reqid
,
1302 .src_ts
= data
->src_ts
,
1303 .dst_ts
= data
->dst_ts
,
1305 .enc_alg
= ENCR_UNDEFINED
,
1306 .int_alg
= AUTH_UNDEFINED
,
1308 .ipcomp
= data
->ipcomp
,
1309 .initiator
= data
->initiator
,
1310 .inbound
= data
->inbound
,
1311 .update
= data
->update
,
1313 add_sa(this, &ipcomp_id
, &ipcomp_sa
);
1314 ipcomp
= IPCOMP_NONE
;
1315 /* use transport mode ESP SA, IPComp uses tunnel mode */
1316 mode
= MODE_TRANSPORT
;
1319 memset(&request
, 0, sizeof(request
));
1320 format_mark(markstr
, sizeof(markstr
), id
->mark
);
1322 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}%s",
1323 ntohl(id
->spi
), data
->reqid
, markstr
);
1326 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1327 hdr
->nlmsg_type
= data
->update ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1328 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1330 sa
= NLMSG_DATA(hdr
);
1331 host2xfrm(id
->src
, &sa
->saddr
);
1332 host2xfrm(id
->dst
, &sa
->id
.daddr
);
1333 sa
->id
.spi
= id
->spi
;
1334 sa
->id
.proto
= id
->proto
;
1335 sa
->family
= id
->src
->get_family(id
->src
);
1336 sa
->mode
= mode2kernel(mode
);
1340 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1343 case MODE_TRANSPORT
:
1344 if (original_mode
== MODE_TUNNEL
)
1345 { /* don't install selectors for switched SAs. because only one
1346 * selector can be installed other traffic would get dropped */
1349 if (data
->src_ts
->get_first(data
->src_ts
,
1350 (void**)&first_src_ts
) == SUCCESS
&&
1351 data
->dst_ts
->get_first(data
->dst_ts
,
1352 (void**)&first_dst_ts
) == SUCCESS
)
1354 sa
->sel
= ts2selector(first_src_ts
, first_dst_ts
,
1356 if (!this->proto_port_transport
)
1358 /* don't install proto/port on SA. This would break
1359 * potential secondary SAs for the same address using a
1360 * different prot/port. */
1362 sa
->sel
.dport
= sa
->sel
.dport_mask
= 0;
1363 sa
->sel
.sport
= sa
->sel
.sport_mask
= 0;
1371 sa
->reqid
= data
->reqid
;
1372 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(data
->lifetime
->bytes
.rekey
);
1373 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(data
->lifetime
->bytes
.life
);
1374 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(data
->lifetime
->packets
.rekey
);
1375 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(data
->lifetime
->packets
.life
);
1376 /* we use lifetimes since added, not since used */
1377 sa
->lft
.soft_add_expires_seconds
= data
->lifetime
->time
.rekey
;
1378 sa
->lft
.hard_add_expires_seconds
= data
->lifetime
->time
.life
;
1379 sa
->lft
.soft_use_expires_seconds
= 0;
1380 sa
->lft
.hard_use_expires_seconds
= 0;
1382 switch (data
->enc_alg
)
1384 case ENCR_UNDEFINED
:
1387 case ENCR_AES_CCM_ICV16
:
1388 case ENCR_AES_GCM_ICV16
:
1389 case ENCR_NULL_AUTH_AES_GMAC
:
1390 case ENCR_CAMELLIA_CCM_ICV16
:
1391 case ENCR_CHACHA20_POLY1305
:
1394 case ENCR_AES_CCM_ICV12
:
1395 case ENCR_AES_GCM_ICV12
:
1396 case ENCR_CAMELLIA_CCM_ICV12
:
1399 case ENCR_AES_CCM_ICV8
:
1400 case ENCR_AES_GCM_ICV8
:
1401 case ENCR_CAMELLIA_CCM_ICV8
:
1403 struct xfrm_algo_aead
*algo
;
1405 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, data
->enc_alg
);
1406 if (alg_name
== NULL
)
1408 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1409 encryption_algorithm_names
, data
->enc_alg
);
1412 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1413 encryption_algorithm_names
, data
->enc_alg
,
1414 data
->enc_key
.len
* 8);
1416 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AEAD
,
1417 sizeof(*algo
) + data
->enc_key
.len
);
1422 algo
->alg_key_len
= data
->enc_key
.len
* 8;
1423 algo
->alg_icv_len
= icv_size
;
1424 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1425 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1426 memcpy(algo
->alg_key
, data
->enc_key
.ptr
, data
->enc_key
.len
);
1431 struct xfrm_algo
*algo
;
1433 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, data
->enc_alg
);
1434 if (alg_name
== NULL
)
1436 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1437 encryption_algorithm_names
, data
->enc_alg
);
1440 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1441 encryption_algorithm_names
, data
->enc_alg
,
1442 data
->enc_key
.len
* 8);
1444 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_CRYPT
,
1445 sizeof(*algo
) + data
->enc_key
.len
);
1450 algo
->alg_key_len
= data
->enc_key
.len
* 8;
1451 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1452 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1453 memcpy(algo
->alg_key
, data
->enc_key
.ptr
, data
->enc_key
.len
);
1457 if (data
->int_alg
!= AUTH_UNDEFINED
)
1459 u_int trunc_len
= 0;
1461 alg_name
= lookup_algorithm(INTEGRITY_ALGORITHM
, data
->int_alg
);
1462 if (alg_name
== NULL
)
1464 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1465 integrity_algorithm_names
, data
->int_alg
);
1468 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1469 integrity_algorithm_names
, data
->int_alg
, data
->int_key
.len
* 8);
1471 switch (data
->int_alg
)
1473 case AUTH_HMAC_MD5_128
:
1474 case AUTH_HMAC_SHA2_256_128
:
1477 case AUTH_HMAC_SHA1_160
:
1486 struct xfrm_algo_auth
* algo
;
1488 /* the kernel uses SHA256 with 96 bit truncation by default,
1489 * use specified truncation size supported by newer kernels.
1490 * also use this for untruncated MD5 and SHA1. */
1491 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH_TRUNC
,
1492 sizeof(*algo
) + data
->int_key
.len
);
1497 algo
->alg_key_len
= data
->int_key
.len
* 8;
1498 algo
->alg_trunc_len
= trunc_len
;
1499 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1500 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1501 memcpy(algo
->alg_key
, data
->int_key
.ptr
, data
->int_key
.len
);
1505 struct xfrm_algo
* algo
;
1507 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH
,
1508 sizeof(*algo
) + data
->int_key
.len
);
1513 algo
->alg_key_len
= data
->int_key
.len
* 8;
1514 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1515 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1516 memcpy(algo
->alg_key
, data
->int_key
.ptr
, data
->int_key
.len
);
1520 if (ipcomp
!= IPCOMP_NONE
)
1522 struct xfrm_algo
* algo
;
1524 alg_name
= lookup_algorithm(COMPRESSION_ALGORITHM
, ipcomp
);
1525 if (alg_name
== NULL
)
1527 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1528 ipcomp_transform_names
, ipcomp
);
1531 DBG2(DBG_KNL
, " using compression algorithm %N",
1532 ipcomp_transform_names
, ipcomp
);
1534 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_COMP
,
1540 algo
->alg_key_len
= 0;
1541 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1542 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1547 struct xfrm_encap_tmpl
*tmpl
;
1549 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
, sizeof(*tmpl
));
1554 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1555 tmpl
->encap_sport
= htons(id
->src
->get_port(id
->src
));
1556 tmpl
->encap_dport
= htons(id
->dst
->get_port(id
->dst
));
1557 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1558 /* encap_oa could probably be derived from the
1559 * traffic selectors [rfc4306, p39]. In the netlink kernel
1560 * implementation pluto does the same as we do here but it uses
1561 * encap_oa in the pfkey implementation.
1562 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1564 * -> does that mean that NAT-T encap doesn't work in transport mode?
1565 * No. The reason the kernel ignores NAT-OA is that it recomputes
1566 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1567 * checks it marks them "checksum ok" so OA isn't needed. */
1570 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
1575 if (data
->tfc
&& id
->proto
== IPPROTO_ESP
&& mode
== MODE_TUNNEL
)
1576 { /* the kernel supports TFC padding only for tunnel mode ESP SAs */
1579 tfcpad
= netlink_reserve(hdr
, sizeof(request
), XFRMA_TFCPAD
,
1585 *tfcpad
= data
->tfc
;
1588 if (id
->proto
!= IPPROTO_COMP
)
1590 if (data
->replay_window
!= 0 && (data
->esn
|| data
->replay_window
> 32))
1592 /* for ESN or larger replay windows we need the new
1593 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1594 struct xfrm_replay_state_esn
*replay
;
1597 bmp_size
= round_up(data
->replay_window
, sizeof(uint32_t) * 8) / 8;
1598 replay
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
1599 sizeof(*replay
) + bmp_size
);
1604 /* bmp_len contains number uf __u32's */
1605 replay
->bmp_len
= bmp_size
/ sizeof(uint32_t);
1606 replay
->replay_window
= data
->replay_window
;
1607 DBG2(DBG_KNL
, " using replay window of %u packets",
1608 data
->replay_window
);
1612 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1613 sa
->flags
|= XFRM_STATE_ESN
;
1618 DBG2(DBG_KNL
, " using replay window of %u packets",
1619 data
->replay_window
);
1620 sa
->replay_window
= data
->replay_window
;
1624 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1626 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x%s", ntohl(id
->spi
),
1634 memwipe(&request
, sizeof(request
));
1639 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1641 * Allocates into one the replay state structure we get from the kernel.
1643 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
1644 kernel_ipsec_sa_id_t
*sa
,
1645 struct xfrm_replay_state_esn
**replay_esn
,
1646 uint32_t *replay_esn_len
,
1647 struct xfrm_replay_state
**replay
,
1648 struct xfrm_lifetime_cur
**lifetime
)
1650 netlink_buf_t request
;
1651 struct nlmsghdr
*hdr
, *out
= NULL
;
1652 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1657 memset(&request
, 0, sizeof(request
));
1659 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
1663 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1664 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1665 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1667 aevent_id
= NLMSG_DATA(hdr
);
1668 aevent_id
->flags
= XFRM_AE_RVAL
;
1670 host2xfrm(sa
->dst
, &aevent_id
->sa_id
.daddr
);
1671 aevent_id
->sa_id
.spi
= sa
->spi
;
1672 aevent_id
->sa_id
.proto
= sa
->proto
;
1673 aevent_id
->sa_id
.family
= sa
->dst
->get_family(sa
->dst
);
1675 if (!add_mark(hdr
, sizeof(request
), sa
->mark
))
1680 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1683 while (NLMSG_OK(hdr
, len
))
1685 switch (hdr
->nlmsg_type
)
1687 case XFRM_MSG_NEWAE
:
1689 out_aevent
= NLMSG_DATA(hdr
);
1694 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1695 DBG1(DBG_KNL
, "querying replay state from SAD entry "
1696 "failed: %s (%d)", strerror(-err
->error
), -err
->error
);
1700 hdr
= NLMSG_NEXT(hdr
, len
);
1711 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1712 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1713 while (RTA_OK(rta
, rtasize
))
1715 if (rta
->rta_type
== XFRMA_LTIME_VAL
&&
1716 RTA_PAYLOAD(rta
) == sizeof(**lifetime
))
1719 *lifetime
= malloc(RTA_PAYLOAD(rta
));
1720 memcpy(*lifetime
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1722 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1723 RTA_PAYLOAD(rta
) == sizeof(**replay
))
1726 *replay
= malloc(RTA_PAYLOAD(rta
));
1727 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1729 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
&&
1730 RTA_PAYLOAD(rta
) >= sizeof(**replay_esn
))
1733 *replay_esn
= malloc(RTA_PAYLOAD(rta
));
1734 *replay_esn_len
= RTA_PAYLOAD(rta
);
1735 memcpy(*replay_esn
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1737 rta
= RTA_NEXT(rta
, rtasize
);
1743 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1744 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1745 kernel_ipsec_query_sa_t
*data
, uint64_t *bytes
, uint64_t *packets
,
1748 netlink_buf_t request
;
1749 struct nlmsghdr
*out
= NULL
, *hdr
;
1750 struct xfrm_usersa_id
*sa_id
;
1751 struct xfrm_usersa_info
*sa
= NULL
;
1752 status_t status
= FAILED
;
1754 char markstr
[32] = "";
1756 memset(&request
, 0, sizeof(request
));
1757 format_mark(markstr
, sizeof(markstr
), id
->mark
);
1759 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x%s", ntohl(id
->spi
),
1763 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1764 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1765 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1767 sa_id
= NLMSG_DATA(hdr
);
1768 host2xfrm(id
->dst
, &sa_id
->daddr
);
1769 sa_id
->spi
= id
->spi
;
1770 sa_id
->proto
= id
->proto
;
1771 sa_id
->family
= id
->dst
->get_family(id
->dst
);
1773 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
1778 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1781 while (NLMSG_OK(hdr
, len
))
1783 switch (hdr
->nlmsg_type
)
1785 case XFRM_MSG_NEWSA
:
1787 sa
= NLMSG_DATA(hdr
);
1792 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1794 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x%s failed: "
1795 "%s (%d)", ntohl(id
->spi
), markstr
,
1796 strerror(-err
->error
), -err
->error
);
1800 hdr
= NLMSG_NEXT(hdr
, len
);
1811 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x%s",
1812 ntohl(id
->spi
), markstr
);
1818 *bytes
= sa
->curlft
.bytes
;
1822 *packets
= sa
->curlft
.packets
;
1825 { /* curlft contains an "use" time, but that contains a timestamp
1826 * of the first use, not the last. Last use time must be queried
1827 * on the policy on Linux */
1837 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1838 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1839 kernel_ipsec_del_sa_t
*data
)
1841 netlink_buf_t request
;
1842 struct nlmsghdr
*hdr
;
1843 struct xfrm_usersa_id
*sa_id
;
1844 char markstr
[32] = "";
1846 /* if IPComp was used, we first delete the additional IPComp SA */
1849 kernel_ipsec_sa_id_t ipcomp_id
= {
1852 .spi
= htonl(ntohs(data
->cpi
)),
1853 .proto
= IPPROTO_COMP
,
1856 kernel_ipsec_del_sa_t ipcomp
= {};
1857 del_sa(this, &ipcomp_id
, &ipcomp
);
1860 memset(&request
, 0, sizeof(request
));
1861 format_mark(markstr
, sizeof(markstr
), id
->mark
);
1863 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x%s", ntohl(id
->spi
),
1867 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1868 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1869 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1871 sa_id
= NLMSG_DATA(hdr
);
1872 host2xfrm(id
->dst
, &sa_id
->daddr
);
1873 sa_id
->spi
= id
->spi
;
1874 sa_id
->proto
= id
->proto
;
1875 sa_id
->family
= id
->dst
->get_family(id
->dst
);
1877 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
1882 switch (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
))
1885 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x%s",
1886 ntohl(id
->spi
), markstr
);
1891 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x%s",
1892 ntohl(id
->spi
), markstr
);
1897 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1898 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1899 kernel_ipsec_update_sa_t
*data
)
1901 netlink_buf_t request
;
1902 struct nlmsghdr
*hdr
, *out
= NULL
;
1903 struct xfrm_usersa_id
*sa_id
;
1904 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1908 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1909 struct xfrm_replay_state
*replay
= NULL
;
1910 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
1911 struct xfrm_lifetime_cur
*lifetime
= NULL
;
1912 uint32_t replay_esn_len
= 0;
1913 kernel_ipsec_del_sa_t del
= { 0 };
1914 status_t status
= FAILED
;
1915 char markstr
[32] = "";
1917 /* if IPComp is used, we first update the IPComp SA */
1920 kernel_ipsec_sa_id_t ipcomp_id
= {
1923 .spi
= htonl(ntohs(data
->cpi
)),
1924 .proto
= IPPROTO_COMP
,
1927 kernel_ipsec_update_sa_t ipcomp
= {
1928 .new_src
= data
->new_src
,
1929 .new_dst
= data
->new_dst
,
1931 update_sa(this, &ipcomp_id
, &ipcomp
);
1934 memset(&request
, 0, sizeof(request
));
1935 format_mark(markstr
, sizeof(markstr
), id
->mark
);
1937 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x%s for update",
1938 ntohl(id
->spi
), markstr
);
1940 /* query the existing SA first */
1942 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1943 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1944 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1946 sa_id
= NLMSG_DATA(hdr
);
1947 host2xfrm(id
->dst
, &sa_id
->daddr
);
1948 sa_id
->spi
= id
->spi
;
1949 sa_id
->proto
= id
->proto
;
1950 sa_id
->family
= id
->dst
->get_family(id
->dst
);
1952 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
1957 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1960 while (NLMSG_OK(hdr
, len
))
1962 switch (hdr
->nlmsg_type
)
1964 case XFRM_MSG_NEWSA
:
1966 out_sa
= NLMSG_DATA(hdr
);
1971 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1972 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1973 strerror(-err
->error
), -err
->error
);
1977 hdr
= NLMSG_NEXT(hdr
, len
);
1987 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x%s",
1988 ntohl(id
->spi
), markstr
);
1992 get_replay_state(this, id
, &replay_esn
, &replay_esn_len
, &replay
,
1995 /* delete the old SA (without affecting the IPComp SA) */
1996 if (del_sa(this, id
, &del
) != SUCCESS
)
1998 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x%s",
1999 ntohl(id
->spi
), markstr
);
2003 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x%s from %#H..%#H to "
2004 "%#H..%#H", ntohl(id
->spi
), markstr
, id
->src
, id
->dst
, data
->new_src
,
2006 /* copy over the SA from out to request */
2008 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2009 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2010 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2011 sa
= NLMSG_DATA(hdr
);
2012 memcpy(sa
, NLMSG_DATA(out
), sizeof(struct xfrm_usersa_info
));
2013 sa
->family
= data
->new_dst
->get_family(data
->new_dst
);
2015 if (!id
->src
->ip_equals(id
->src
, data
->new_src
))
2017 host2xfrm(data
->new_src
, &sa
->saddr
);
2019 if (!id
->dst
->ip_equals(id
->dst
, data
->new_dst
))
2021 host2xfrm(data
->new_dst
, &sa
->id
.daddr
);
2024 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
2025 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
2026 while (RTA_OK(rta
, rtasize
))
2028 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2029 if (rta
->rta_type
!= XFRMA_ENCAP
|| data
->new_encap
)
2031 if (rta
->rta_type
== XFRMA_ENCAP
)
2032 { /* update encap tmpl */
2033 tmpl
= RTA_DATA(rta
);
2034 tmpl
->encap_sport
= ntohs(data
->new_src
->get_port(data
->new_src
));
2035 tmpl
->encap_dport
= ntohs(data
->new_dst
->get_port(data
->new_dst
));
2037 netlink_add_attribute(hdr
, rta
->rta_type
,
2038 chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
)),
2041 rta
= RTA_NEXT(rta
, rtasize
);
2044 if (tmpl
== NULL
&& data
->new_encap
)
2045 { /* add tmpl if we are enabling it */
2046 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
, sizeof(*tmpl
));
2051 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2052 tmpl
->encap_sport
= ntohs(data
->new_src
->get_port(data
->new_src
));
2053 tmpl
->encap_dport
= ntohs(data
->new_dst
->get_port(data
->new_dst
));
2054 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2059 struct xfrm_replay_state_esn
*state
;
2061 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
2067 memcpy(state
, replay_esn
, replay_esn_len
);
2071 struct xfrm_replay_state
*state
;
2073 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_VAL
,
2079 memcpy(state
, replay
, sizeof(*state
));
2083 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry with "
2084 "SPI %.8x%s", ntohl(id
->spi
), markstr
);
2088 struct xfrm_lifetime_cur
*state
;
2090 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_LTIME_VAL
,
2096 memcpy(state
, lifetime
, sizeof(*state
));
2100 DBG1(DBG_KNL
, "unable to copy usage stats from old SAD entry with "
2101 "SPI %.8x%s", ntohl(id
->spi
), markstr
);
2104 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2106 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x%s",
2107 ntohl(id
->spi
), markstr
);
2117 memwipe(&request
, sizeof(request
));
2123 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
2124 private_kernel_netlink_ipsec_t
*this)
2126 netlink_buf_t request
;
2127 struct nlmsghdr
*hdr
;
2128 struct xfrm_usersa_flush
*flush
;
2133 { IPPROTO_AH
, "AH" },
2134 { IPPROTO_ESP
, "ESP" },
2135 { IPPROTO_COMP
, "IPComp" },
2139 memset(&request
, 0, sizeof(request
));
2142 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2143 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
2144 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
2146 flush
= NLMSG_DATA(hdr
);
2148 for (i
= 0; i
< countof(protos
); i
++)
2150 DBG2(DBG_KNL
, "flushing all %s SAD entries", protos
[i
].name
);
2152 flush
->proto
= protos
[i
].proto
;
2154 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2156 DBG1(DBG_KNL
, "unable to flush %s SAD entries", protos
[i
].name
);
2164 * Unlock the mutex and signal waiting threads
2166 static void policy_change_done(private_kernel_netlink_ipsec_t
*this,
2167 policy_entry_t
*policy
)
2169 policy
->working
= FALSE
;
2170 if (policy
->waiting
)
2171 { /* don't need to wake threads waiting for other policies */
2172 this->condvar
->broadcast(this->condvar
);
2174 this->mutex
->unlock(this->mutex
);
2178 * Add or update a policy in the kernel.
2180 * Note: The mutex has to be locked when entering this function
2181 * and is unlocked here in any case.
2183 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
2184 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
2186 netlink_buf_t request
;
2187 policy_entry_t clone
;
2188 ipsec_sa_t
*ipsec
= mapping
->sa
;
2189 struct xfrm_userpolicy_info
*policy_info
;
2190 struct nlmsghdr
*hdr
;
2194 /* clone the policy so we are able to check it out again later */
2195 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
2197 memset(&request
, 0, sizeof(request
));
2199 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2200 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
2201 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2203 policy_info
= NLMSG_DATA(hdr
);
2204 policy_info
->sel
= policy
->sel
;
2205 policy_info
->dir
= policy
->direction
;
2207 /* calculate priority based on selector size, small size = high prio */
2208 policy_info
->priority
= mapping
->priority
;
2209 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
2210 : XFRM_POLICY_BLOCK
;
2211 policy_info
->share
= XFRM_SHARE_ANY
;
2213 /* policies don't expire */
2214 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2215 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2216 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2217 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2218 policy_info
->lft
.soft_add_expires_seconds
= 0;
2219 policy_info
->lft
.hard_add_expires_seconds
= 0;
2220 policy_info
->lft
.soft_use_expires_seconds
= 0;
2221 policy_info
->lft
.hard_use_expires_seconds
= 0;
2223 if (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.reqid
)
2225 struct xfrm_user_tmpl
*tmpl
;
2230 { IPPROTO_COMP
, ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2231 { IPPROTO_ESP
, ipsec
->cfg
.esp
.use
},
2232 { IPPROTO_AH
, ipsec
->cfg
.ah
.use
},
2234 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2237 for (i
= 0; i
< countof(protos
); i
++)
2244 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_TMPL
,
2245 count
* sizeof(*tmpl
));
2248 policy_change_done(this, policy
);
2252 for (i
= 0; i
< countof(protos
); i
++)
2258 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2259 tmpl
->id
.proto
= protos
[i
].proto
;
2260 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2261 tmpl
->mode
= mode2kernel(proto_mode
);
2262 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2263 policy
->direction
!= POLICY_OUT
;
2264 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2266 if (proto_mode
== MODE_TUNNEL
|| proto_mode
== MODE_BEET
)
2267 { /* only for tunnel mode */
2268 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2269 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2274 /* use transport mode for other SAs */
2275 proto_mode
= MODE_TRANSPORT
;
2279 if (!add_mark(hdr
, sizeof(request
), ipsec
->mark
))
2281 policy_change_done(this, policy
);
2284 this->mutex
->unlock(this->mutex
);
2286 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2287 if (status
== ALREADY_DONE
&& !update
)
2289 DBG1(DBG_KNL
, "policy already exists, try to update it");
2290 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
2291 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2294 this->mutex
->lock(this->mutex
);
2295 if (status
!= SUCCESS
)
2297 policy_change_done(this, policy
);
2300 /* install a route, if:
2301 * - this is a inbound policy (to just get one for each child)
2302 * - we are in tunnel/BEET mode or install a bypass policy
2303 * - routing is not disabled via strongswan.conf
2305 if (policy
->direction
== POLICY_IN
&& this->install_routes
&&
2306 (mapping
->type
!= POLICY_IPSEC
|| ipsec
->cfg
.mode
!= MODE_TRANSPORT
))
2308 policy_sa_in_t
*in
= (policy_sa_in_t
*)mapping
;
2309 route_entry_t
*route
;
2313 .prefixlen
= policy
->sel
.prefixlen_s
,
2316 if (charon
->kernel
->get_address_by_ts(charon
->kernel
, in
->dst_ts
,
2317 &route
->src_ip
, NULL
) == SUCCESS
)
2319 /* get the nexthop to src (src as we are in POLICY_IN) */
2320 if (!ipsec
->src
->is_anyaddr(ipsec
->src
))
2322 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2323 ipsec
->src
, -1, ipsec
->dst
, NULL
);
2326 { /* for shunt policies */
2327 iface
= xfrm2host(policy
->sel
.family
, &policy
->sel
.saddr
, 0);
2328 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2329 iface
, policy
->sel
.prefixlen_s
,
2330 route
->src_ip
, NULL
);
2331 iface
->destroy(iface
);
2333 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2334 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
2336 /* get the interface to install the route for. If we have a local
2337 * address, use it. Otherwise (for shunt policies) use the
2338 * routes source address. */
2340 if (iface
->is_anyaddr(iface
))
2342 iface
= route
->src_ip
;
2344 /* install route via outgoing interface */
2345 if (!charon
->kernel
->get_interface(charon
->kernel
, iface
,
2348 policy_change_done(this, policy
);
2349 route_entry_destroy(route
);
2355 route_entry_t
*old
= policy
->route
;
2356 if (route_entry_equals(old
, route
))
2358 policy_change_done(this, policy
);
2359 route_entry_destroy(route
);
2362 /* uninstall previously installed route */
2363 if (charon
->kernel
->del_route(charon
->kernel
, old
->dst_net
,
2364 old
->prefixlen
, old
->gateway
,
2365 old
->src_ip
, old
->if_name
) != SUCCESS
)
2367 DBG1(DBG_KNL
, "error uninstalling route installed with "
2368 "policy %R === %R %N", in
->src_ts
, in
->dst_ts
,
2369 policy_dir_names
, policy
->direction
);
2371 route_entry_destroy(old
);
2372 policy
->route
= NULL
;
2375 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2376 in
->src_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2377 switch (charon
->kernel
->add_route(charon
->kernel
, route
->dst_net
,
2378 route
->prefixlen
, route
->gateway
,
2379 route
->src_ip
, route
->if_name
))
2382 DBG1(DBG_KNL
, "unable to install source route for %H",
2386 /* route exists, do not uninstall */
2387 route_entry_destroy(route
);
2390 /* cache the installed route */
2391 policy
->route
= route
;
2400 policy_change_done(this, policy
);
2404 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2405 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2406 kernel_ipsec_manage_policy_t
*data
)
2408 policy_entry_t
*policy
, *current
;
2409 policy_sa_t
*assigned_sa
, *current_sa
;
2410 enumerator_t
*enumerator
;
2411 bool found
= FALSE
, update
= TRUE
;
2412 char markstr
[32] = "";
2413 uint32_t cur_priority
= 0;
2416 /* create a policy */
2418 .sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
),
2419 .mark
= id
->mark
.value
& id
->mark
.mask
,
2420 .direction
= id
->dir
,
2421 .reqid
= data
->sa
->reqid
,
2423 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2425 /* find the policy, which matches EXACTLY */
2426 this->mutex
->lock(this->mutex
);
2427 current
= this->policies
->get(this->policies
, policy
);
2430 if (current
->reqid
&& data
->sa
->reqid
&&
2431 current
->reqid
!= data
->sa
->reqid
)
2433 DBG1(DBG_CFG
, "unable to install policy %R === %R %N%s for reqid "
2434 "%u, the same policy for reqid %u exists",
2435 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
,
2436 data
->sa
->reqid
, current
->reqid
);
2437 policy_entry_destroy(this, policy
);
2438 this->mutex
->unlock(this->mutex
);
2439 return INVALID_STATE
;
2441 /* use existing policy */
2442 DBG2(DBG_KNL
, "policy %R === %R %N%s already exists, increasing "
2443 "refcount", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
2445 policy_entry_destroy(this, policy
);
2450 while (policy
->working
)
2452 this->condvar
->wait(this->condvar
, this->mutex
);
2455 policy
->working
= TRUE
;
2458 { /* use the new one, if we have no such policy */
2459 policy
->used_by
= linked_list_create();
2460 this->policies
->put(this->policies
, policy
, policy
);
2463 /* cache the assigned IPsec SA */
2464 assigned_sa
= policy_sa_create(this, id
->dir
, data
->type
, data
->src
,
2465 data
->dst
, id
->src_ts
, id
->dst_ts
, id
->mark
, data
->sa
);
2466 assigned_sa
->auto_priority
= get_priority(policy
, data
->prio
, id
->interface
);
2467 assigned_sa
->priority
= this->get_priority ?
this->get_priority(id
, data
)
2468 : data
->manual_prio
;
2469 assigned_sa
->priority
= assigned_sa
->priority ?
: assigned_sa
->auto_priority
;
2471 /* insert the SA according to its priority */
2472 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2473 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2475 if (current_sa
->priority
> assigned_sa
->priority
)
2479 if (current_sa
->priority
== assigned_sa
->priority
)
2481 /* in case of equal manual prios order SAs by automatic priority */
2482 if (current_sa
->auto_priority
> assigned_sa
->auto_priority
)
2486 /* prefer SAs with a reqid over those without */
2487 if (current_sa
->auto_priority
== assigned_sa
->auto_priority
&&
2488 (!current_sa
->sa
->cfg
.reqid
|| assigned_sa
->sa
->cfg
.reqid
))
2495 cur_priority
= current_sa
->priority
;
2499 policy
->used_by
->insert_before(policy
->used_by
, enumerator
, assigned_sa
);
2500 enumerator
->destroy(enumerator
);
2502 use_count
= policy
->used_by
->get_count(policy
->used_by
);
2504 { /* we don't update the policy if the priority is lower than that of
2505 * the currently installed one */
2506 policy_change_done(this, policy
);
2507 DBG2(DBG_KNL
, "not updating policy %R === %R %N%s [priority %u,"
2508 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
2509 id
->dir
, markstr
, cur_priority
, use_count
);
2513 if (this->policy_update
)
2518 DBG2(DBG_KNL
, "%s policy %R === %R %N%s [priority %u, refcount %d]",
2519 found ?
"updating" : "adding", id
->src_ts
, id
->dst_ts
,
2520 policy_dir_names
, id
->dir
, markstr
, assigned_sa
->priority
, use_count
);
2522 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2524 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N%s",
2525 found ?
"update" : "add", id
->src_ts
, id
->dst_ts
,
2526 policy_dir_names
, id
->dir
, markstr
);
2532 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2533 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2534 kernel_ipsec_query_policy_t
*data
, time_t *use_time
)
2536 netlink_buf_t request
;
2537 struct nlmsghdr
*out
= NULL
, *hdr
;
2538 struct xfrm_userpolicy_id
*policy_id
;
2539 struct xfrm_userpolicy_info
*policy
= NULL
;
2541 char markstr
[32] = "";
2543 memset(&request
, 0, sizeof(request
));
2544 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2546 DBG2(DBG_KNL
, "querying policy %R === %R %N%s", id
->src_ts
, id
->dst_ts
,
2547 policy_dir_names
, id
->dir
, markstr
);
2550 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2551 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2552 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2554 policy_id
= NLMSG_DATA(hdr
);
2555 policy_id
->sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
);
2556 policy_id
->dir
= id
->dir
;
2558 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2563 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2566 while (NLMSG_OK(hdr
, len
))
2568 switch (hdr
->nlmsg_type
)
2570 case XFRM_MSG_NEWPOLICY
:
2572 policy
= NLMSG_DATA(hdr
);
2577 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2578 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2579 strerror(-err
->error
), -err
->error
);
2583 hdr
= NLMSG_NEXT(hdr
, len
);
2594 DBG2(DBG_KNL
, "unable to query policy %R === %R %N%s", id
->src_ts
,
2595 id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
2600 if (policy
->curlft
.use_time
)
2602 /* we need the monotonic time, but the kernel returns system time. */
2603 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
2614 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2615 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2616 kernel_ipsec_manage_policy_t
*data
)
2618 policy_entry_t
*current
, policy
;
2619 enumerator_t
*enumerator
;
2620 policy_sa_t
*mapping
;
2621 netlink_buf_t request
;
2622 struct nlmsghdr
*hdr
;
2623 struct xfrm_userpolicy_id
*policy_id
;
2624 bool is_installed
= TRUE
;
2625 uint32_t priority
, auto_priority
, cur_priority
;
2626 ipsec_sa_t assigned_sa
= {
2632 char markstr
[32] = "";
2634 status_t status
= SUCCESS
;
2636 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2638 DBG2(DBG_KNL
, "deleting policy %R === %R %N%s", id
->src_ts
, id
->dst_ts
,
2639 policy_dir_names
, id
->dir
, markstr
);
2641 /* create a policy */
2642 memset(&policy
, 0, sizeof(policy_entry_t
));
2643 policy
.sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
);
2644 policy
.mark
= id
->mark
.value
& id
->mark
.mask
;
2645 policy
.direction
= id
->dir
;
2647 /* find the policy */
2648 this->mutex
->lock(this->mutex
);
2649 current
= this->policies
->get(this->policies
, &policy
);
2652 DBG1(DBG_KNL
, "deleting policy %R === %R %N%s failed, not found",
2653 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
2654 this->mutex
->unlock(this->mutex
);
2658 while (current
->working
)
2660 this->condvar
->wait(this->condvar
, this->mutex
);
2662 current
->working
= TRUE
;
2665 /* remove mapping to SA by reqid and priority */
2666 auto_priority
= get_priority(current
, data
->prio
,id
->interface
);
2667 priority
= this->get_priority ?
this->get_priority(id
, data
)
2668 : data
->manual_prio
;
2669 priority
= priority ?
: auto_priority
;
2671 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
2672 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2674 if (priority
== mapping
->priority
&&
2675 auto_priority
== mapping
->auto_priority
&&
2676 data
->type
== mapping
->type
&&
2677 ipsec_sa_equals(mapping
->sa
, &assigned_sa
))
2679 current
->used_by
->remove_at(current
->used_by
, enumerator
);
2680 policy_sa_destroy(mapping
, &id
->dir
, this);
2685 cur_priority
= mapping
->priority
;
2686 is_installed
= FALSE
;
2689 enumerator
->destroy(enumerator
);
2691 use_count
= current
->used_by
->get_count(current
->used_by
);
2693 { /* policy is used by more SAs, keep in kernel */
2694 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2696 { /* no need to update as the policy was not installed for this SA */
2697 policy_change_done(this, current
);
2698 DBG2(DBG_KNL
, "not updating policy %R === %R %N%s [priority %u, "
2699 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
2700 id
->dir
, markstr
, cur_priority
, use_count
);
2703 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
2705 DBG2(DBG_KNL
, "updating policy %R === %R %N%s [priority %u, "
2706 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
2707 markstr
, mapping
->priority
, use_count
);
2709 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
2711 DBG1(DBG_KNL
, "unable to update policy %R === %R %N%s",
2712 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
2718 memset(&request
, 0, sizeof(request
));
2721 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2722 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2723 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2725 policy_id
= NLMSG_DATA(hdr
);
2726 policy_id
->sel
= current
->sel
;
2727 policy_id
->dir
= id
->dir
;
2729 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2731 policy_change_done(this, current
);
2737 route_entry_t
*route
= current
->route
;
2738 if (charon
->kernel
->del_route(charon
->kernel
, route
->dst_net
,
2739 route
->prefixlen
, route
->gateway
,
2740 route
->src_ip
, route
->if_name
) != SUCCESS
)
2742 DBG1(DBG_KNL
, "error uninstalling route installed with policy "
2743 "%R === %R %N%s", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
2747 this->mutex
->unlock(this->mutex
);
2749 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2751 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N%s", id
->src_ts
,
2752 id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
2756 this->mutex
->lock(this->mutex
);
2757 if (!current
->waiting
)
2758 { /* only if no other thread still needs the policy */
2759 this->policies
->remove(this->policies
, current
);
2760 policy_entry_destroy(this, current
);
2761 this->mutex
->unlock(this->mutex
);
2765 policy_change_done(this, current
);
2770 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
2771 private_kernel_netlink_ipsec_t
*this)
2773 netlink_buf_t request
;
2774 struct nlmsghdr
*hdr
;
2776 memset(&request
, 0, sizeof(request
));
2778 DBG2(DBG_KNL
, "flushing all policies from SPD");
2781 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2782 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
2783 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
2785 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2786 * to main or sub policies (default is main) */
2788 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2790 DBG1(DBG_KNL
, "unable to flush SPD entries");
2797 * Bypass socket using a per-socket policy
2799 static bool add_socket_bypass(private_kernel_netlink_ipsec_t
*this,
2802 struct xfrm_userpolicy_info policy
;
2803 u_int sol
, ipsec_policy
;
2809 ipsec_policy
= IP_XFRM_POLICY
;
2813 ipsec_policy
= IPV6_XFRM_POLICY
;
2819 memset(&policy
, 0, sizeof(policy
));
2820 policy
.action
= XFRM_POLICY_ALLOW
;
2821 policy
.sel
.family
= family
;
2823 policy
.dir
= XFRM_POLICY_OUT
;
2824 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2826 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s (%d)",
2827 strerror(errno
), errno
);
2830 policy
.dir
= XFRM_POLICY_IN
;
2831 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2833 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s (%d)",
2834 strerror(errno
), errno
);
2841 * Port based IKE bypass policy
2844 /** address family */
2846 /** layer 4 protocol */
2848 /** port number, network order */
2853 * Add or remove a bypass policy from/to kernel
2855 static bool manage_bypass(private_kernel_netlink_ipsec_t
*this,
2856 int type
, policy_dir_t dir
, bypass_t
*bypass
)
2858 netlink_buf_t request
;
2859 struct xfrm_selector
*sel
;
2860 struct nlmsghdr
*hdr
;
2862 memset(&request
, 0, sizeof(request
));
2864 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2865 hdr
->nlmsg_type
= type
;
2867 if (type
== XFRM_MSG_NEWPOLICY
)
2869 struct xfrm_userpolicy_info
*policy
;
2871 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2873 policy
= NLMSG_DATA(hdr
);
2875 policy
->priority
= 32;
2876 policy
->action
= XFRM_POLICY_ALLOW
;
2877 policy
->share
= XFRM_SHARE_ANY
;
2879 policy
->lft
.soft_byte_limit
= XFRM_INF
;
2880 policy
->lft
.soft_packet_limit
= XFRM_INF
;
2881 policy
->lft
.hard_byte_limit
= XFRM_INF
;
2882 policy
->lft
.hard_packet_limit
= XFRM_INF
;
2886 else /* XFRM_MSG_DELPOLICY */
2888 struct xfrm_userpolicy_id
*policy
;
2890 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2892 policy
= NLMSG_DATA(hdr
);
2898 sel
->family
= bypass
->family
;
2899 sel
->proto
= bypass
->proto
;
2900 if (dir
== POLICY_IN
)
2902 sel
->dport
= bypass
->port
;
2903 sel
->dport_mask
= 0xffff;
2907 sel
->sport
= bypass
->port
;
2908 sel
->sport_mask
= 0xffff;
2910 return this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) == SUCCESS
;
2914 * Bypass socket using a port-based bypass policy
2916 static bool add_port_bypass(private_kernel_netlink_ipsec_t
*this,
2921 struct sockaddr_in in
;
2922 struct sockaddr_in6 in6
;
2929 len
= sizeof(saddr
);
2930 if (getsockname(fd
, &saddr
.sa
, &len
) != 0)
2934 #ifdef SO_PROTOCOL /* since 2.6.32 */
2935 len
= sizeof(bypass
.proto
);
2936 if (getsockopt(fd
, SOL_SOCKET
, SO_PROTOCOL
, &bypass
.proto
, &len
) != 0)
2938 { /* assume UDP if SO_PROTOCOL not supported */
2939 bypass
.proto
= IPPROTO_UDP
;
2944 bypass
.port
= saddr
.in
.sin_port
;
2947 bypass
.port
= saddr
.in6
.sin6_port
;
2953 if (!manage_bypass(this, XFRM_MSG_NEWPOLICY
, POLICY_IN
, &bypass
))
2957 if (!manage_bypass(this, XFRM_MSG_NEWPOLICY
, POLICY_OUT
, &bypass
))
2959 manage_bypass(this, XFRM_MSG_DELPOLICY
, POLICY_IN
, &bypass
);
2962 array_insert(this->bypass
, ARRAY_TAIL
, &bypass
);
2968 * Remove installed port based bypass policy
2970 static void remove_port_bypass(bypass_t
*bypass
, int idx
,
2971 private_kernel_netlink_ipsec_t
*this)
2973 manage_bypass(this, XFRM_MSG_DELPOLICY
, POLICY_OUT
, bypass
);
2974 manage_bypass(this, XFRM_MSG_DELPOLICY
, POLICY_IN
, bypass
);
2977 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2978 private_kernel_netlink_ipsec_t
*this, int fd
, int family
)
2980 if (lib
->settings
->get_bool(lib
->settings
,
2981 "%s.plugins.kernel-netlink.port_bypass", FALSE
, lib
->ns
))
2983 return add_port_bypass(this, fd
, family
);
2985 return add_socket_bypass(this, fd
, family
);
2988 METHOD(kernel_ipsec_t
, enable_udp_decap
, bool,
2989 private_kernel_netlink_ipsec_t
*this, int fd
, int family
, uint16_t port
)
2991 int type
= UDP_ENCAP_ESPINUDP
;
2993 if (setsockopt(fd
, SOL_UDP
, UDP_ENCAP
, &type
, sizeof(type
)) < 0)
2995 DBG1(DBG_KNL
, "unable to set UDP_ENCAP: %s", strerror(errno
));
3001 METHOD(kernel_ipsec_t
, destroy
, void,
3002 private_kernel_netlink_ipsec_t
*this)
3004 enumerator_t
*enumerator
;
3005 policy_entry_t
*policy
;
3007 array_destroy_function(this->bypass
,
3008 (array_callback_t
)remove_port_bypass
, this);
3009 if (this->socket_xfrm_events
> 0)
3011 lib
->watcher
->remove(lib
->watcher
, this->socket_xfrm_events
);
3012 close(this->socket_xfrm_events
);
3014 DESTROY_IF(this->socket_xfrm
);
3015 enumerator
= this->policies
->create_enumerator(this->policies
);
3016 while (enumerator
->enumerate(enumerator
, &policy
, &policy
))
3018 policy_entry_destroy(this, policy
);
3020 enumerator
->destroy(enumerator
);
3021 this->policies
->destroy(this->policies
);
3022 this->sas
->destroy(this->sas
);
3023 this->condvar
->destroy(this->condvar
);
3024 this->mutex
->destroy(this->mutex
);
3029 * Described in header.
3031 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
3033 private_kernel_netlink_ipsec_t
*this;
3034 bool register_for_events
= TRUE
;
3040 .get_features
= _get_features
,
3041 .get_spi
= _get_spi
,
3042 .get_cpi
= _get_cpi
,
3044 .update_sa
= _update_sa
,
3045 .query_sa
= _query_sa
,
3047 .flush_sas
= _flush_sas
,
3048 .add_policy
= _add_policy
,
3049 .query_policy
= _query_policy
,
3050 .del_policy
= _del_policy
,
3051 .flush_policies
= _flush_policies
,
3052 .bypass_socket
= _bypass_socket
,
3053 .enable_udp_decap
= _enable_udp_decap
,
3054 .destroy
= _destroy
,
3057 .policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
3058 (hashtable_equals_t
)policy_equals
, 32),
3059 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
3060 (hashtable_equals_t
)ipsec_sa_equals
, 32),
3061 .bypass
= array_create(sizeof(bypass_t
), 0),
3062 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
3063 .condvar
= condvar_create(CONDVAR_TYPE_DEFAULT
),
3064 .get_priority
= dlsym(RTLD_DEFAULT
,
3065 "kernel_netlink_get_priority_custom"),
3066 .policy_update
= lib
->settings
->get_bool(lib
->settings
,
3067 "%s.plugins.kernel-netlink.policy_update", FALSE
, lib
->ns
),
3068 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
3069 "%s.install_routes", TRUE
, lib
->ns
),
3070 .proto_port_transport
= lib
->settings
->get_bool(lib
->settings
,
3071 "%s.plugins.kernel-netlink.set_proto_port_transport_sa",
3075 if (streq(lib
->ns
, "starter"))
3076 { /* starter has no threads, so we do not register for kernel events */
3077 register_for_events
= FALSE
;
3080 f
= fopen("/proc/sys/net/core/xfrm_acq_expires", "w");
3083 fprintf(f
, "%u", lib
->settings
->get_int(lib
->settings
,
3084 "%s.plugins.kernel-netlink.xfrm_acq_expires",
3085 DEFAULT_ACQUIRE_LIFETIME
, lib
->ns
));
3089 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
, xfrm_msg_names
,
3090 lib
->settings
->get_bool(lib
->settings
,
3091 "%s.plugins.kernel-netlink.parallel_xfrm", FALSE
, lib
->ns
));
3092 if (!this->socket_xfrm
)
3098 if (register_for_events
)
3100 struct sockaddr_nl addr
;
3102 memset(&addr
, 0, sizeof(addr
));
3103 addr
.nl_family
= AF_NETLINK
;
3105 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
3106 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
3107 if (this->socket_xfrm_events
<= 0)
3109 DBG1(DBG_KNL
, "unable to create XFRM event socket: %s (%d)",
3110 strerror(errno
), errno
);
3114 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
3115 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
3116 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
3118 DBG1(DBG_KNL
, "unable to bind XFRM event socket: %s (%d)",
3119 strerror(errno
), errno
);
3123 lib
->watcher
->add(lib
->watcher
, this->socket_xfrm_events
, WATCHER_READ
,
3124 (watcher_cb_t
)receive_events
, this);
3127 return &this->public;