2 * Copyright (C) 2006-2019 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
21 * Copyright (C) 2018 Mellanox Technologies.
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/ioctl.h>
47 #include <linux/ipsec.h>
48 #include <linux/netlink.h>
49 #include <linux/rtnetlink.h>
50 #include <linux/xfrm.h>
51 #include <linux/udp.h>
52 #include <linux/ethtool.h>
53 #include <linux/sockios.h>
62 #include "kernel_netlink_ipsec.h"
63 #include "kernel_netlink_shared.h"
66 #include <utils/debug.h>
67 #include <threading/mutex.h>
68 #include <threading/condvar.h>
69 #include <collections/array.h>
70 #include <collections/hashtable.h>
71 #include <collections/linked_list.h>
73 /** Required for Linux 2.6.26 kernel and later */
74 #ifndef XFRM_STATE_AF_UNSPEC
75 #define XFRM_STATE_AF_UNSPEC 32
78 /** From linux/in.h */
79 #ifndef IP_XFRM_POLICY
80 #define IP_XFRM_POLICY 17
83 /** Missing on uclibc */
84 #ifndef IPV6_XFRM_POLICY
85 #define IPV6_XFRM_POLICY 34
86 #endif /*IPV6_XFRM_POLICY*/
88 /* from linux/udp.h */
93 #ifndef UDP_ENCAP_ESPINUDP
94 #define UDP_ENCAP_ESPINUDP 2
97 /* this is not defined on some platforms */
99 #define SOL_UDP IPPROTO_UDP
102 /** Base priority for installed policies */
103 #define PRIO_BASE 200000
106 * Map the limit for bytes and packets to XFRM_INF by default
108 #define XFRM_LIMIT(x) ((x) == 0 ? XFRM_INF : (x))
111 * Create ORable bitfield of XFRM NL groups
113 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
116 * Returns a pointer to the first rtattr following the nlmsghdr *nlh and the
117 * 'usual' netlink data x like 'struct xfrm_usersa_info'
119 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + \
120 NLMSG_ALIGN(sizeof(x))))
122 * Returns the total size of attached rta data
123 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
125 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
127 typedef struct kernel_algorithm_t kernel_algorithm_t
;
130 * Mapping of IKEv2 kernel identifier to linux crypto API names
132 struct kernel_algorithm_t
{
134 * Identifier specified in IKEv2
139 * Name of the algorithm in linux crypto API
144 ENUM(xfrm_msg_names
, XFRM_MSG_NEWSA
, XFRM_MSG_MAPPING
,
148 "XFRM_MSG_NEWPOLICY",
149 "XFRM_MSG_DELPOLICY",
150 "XFRM_MSG_GETPOLICY",
154 "XFRM_MSG_UPDPOLICY",
156 "XFRM_MSG_POLEXPIRE",
158 "XFRM_MSG_FLUSHPOLICY",
163 "XFRM_MSG_NEWSADINFO",
164 "XFRM_MSG_GETSADINFO",
165 "XFRM_MSG_NEWSPDINFO",
166 "XFRM_MSG_GETSPDINFO",
170 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_OFFLOAD_DEV
,
182 "XFRMA_REPLAY_THRESH",
183 "XFRMA_ETIMER_THRESH",
191 "XFRMA_ALG_AUTH_TRUNC",
194 "XFRMA_REPLAY_ESN_VAL",
195 "XFRMA_SA_EXTRA_FLAGS",
197 "XFRMA_ADDRESS_FILTER",
203 * Algorithms for encryption
205 static kernel_algorithm_t encryption_algs
[] = {
206 /* {ENCR_DES_IV64, "***" }, */
208 {ENCR_3DES
, "des3_ede" },
209 /* {ENCR_RC5, "***" }, */
210 /* {ENCR_IDEA, "***" }, */
211 {ENCR_CAST
, "cast5" },
212 {ENCR_BLOWFISH
, "blowfish" },
213 /* {ENCR_3IDEA, "***" }, */
214 /* {ENCR_DES_IV32, "***" }, */
215 {ENCR_NULL
, "cipher_null" },
216 {ENCR_AES_CBC
, "aes" },
217 {ENCR_AES_CTR
, "rfc3686(ctr(aes))" },
218 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
219 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
220 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
221 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
222 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
223 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
224 {ENCR_NULL_AUTH_AES_GMAC
, "rfc4543(gcm(aes))" },
225 {ENCR_CAMELLIA_CBC
, "cbc(camellia)" },
226 /* {ENCR_CAMELLIA_CTR, "***" }, */
227 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
228 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
229 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
230 {ENCR_SERPENT_CBC
, "serpent" },
231 {ENCR_TWOFISH_CBC
, "twofish" },
232 {ENCR_CHACHA20_POLY1305
, "rfc7539esp(chacha20,poly1305)"},
236 * Algorithms for integrity protection
238 static kernel_algorithm_t integrity_algs
[] = {
239 {AUTH_HMAC_MD5_96
, "md5" },
240 {AUTH_HMAC_MD5_128
, "hmac(md5)" },
241 {AUTH_HMAC_SHA1_96
, "sha1" },
242 {AUTH_HMAC_SHA1_160
, "hmac(sha1)" },
243 {AUTH_HMAC_SHA2_256_96
, "sha256" },
244 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
245 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
246 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
247 /* {AUTH_DES_MAC, "***" }, */
248 /* {AUTH_KPDK_MD5, "***" }, */
249 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
250 {AUTH_AES_CMAC_96
, "cmac(aes)" },
254 * Algorithms for IPComp
256 static kernel_algorithm_t compression_algs
[] = {
257 /* {IPCOMP_OUI, "***" }, */
258 {IPCOMP_DEFLATE
, "deflate" },
259 {IPCOMP_LZS
, "lzs" },
260 {IPCOMP_LZJH
, "lzjh" },
264 * Look up a kernel algorithm name and its key size
266 static const char* lookup_algorithm(transform_type_t type
, int ikev2
)
268 kernel_algorithm_t
*list
;
274 case ENCRYPTION_ALGORITHM
:
275 list
= encryption_algs
;
276 count
= countof(encryption_algs
);
278 case INTEGRITY_ALGORITHM
:
279 list
= integrity_algs
;
280 count
= countof(integrity_algs
);
282 case COMPRESSION_ALGORITHM
:
283 list
= compression_algs
;
284 count
= countof(compression_algs
);
289 for (i
= 0; i
< count
; i
++)
291 if (list
[i
].ikev2
== ikev2
)
296 if (charon
->kernel
->lookup_algorithm(charon
->kernel
, ikev2
, type
, NULL
,
304 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
307 * Private variables and functions of kernel_netlink class.
309 struct private_kernel_netlink_ipsec_t
{
311 * Public part of the kernel_netlink_t object
313 kernel_netlink_ipsec_t
public;
316 * Mutex to lock access to installed policies
321 * Condvar to synchronize access to individual policies
326 * Hash table of installed policies (policy_entry_t)
328 hashtable_t
*policies
;
331 * Hash table of IPsec SAs using policies (ipsec_sa_t)
336 * Netlink xfrm socket (IPsec)
338 netlink_socket_t
*socket_xfrm
;
341 * Netlink xfrm socket to receive acquire and expire events
343 int socket_xfrm_events
;
346 * Whether to install routes along policies
351 * Whether to set protocol and ports on selector installed with transport
354 bool proto_port_transport
;
357 * Whether to always use UPDATE to install policies
362 * Installed port based IKE bypass policies, as bypass_t
367 * Custom priority calculation function
369 uint32_t (*get_priority
)(kernel_ipsec_policy_id_t
*id
,
370 kernel_ipsec_manage_policy_t
*data
);
373 typedef struct ipsec_sa_t ipsec_sa_t
;
376 * IPsec SA assigned to a policy.
379 /** Source address of this SA */
382 /** Destination address of this SA */
391 /** Description of this SA */
394 /** Reference count for this SA */
399 * Hash function for ipsec_sa_t objects
401 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
403 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
404 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
405 chunk_hash_inc(chunk_from_thing(sa
->mark
),
406 chunk_hash_inc(chunk_from_thing(sa
->if_id
),
407 chunk_hash(chunk_from_thing(sa
->cfg
))))));
411 * Equality function for ipsec_sa_t objects
413 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
415 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
416 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
417 sa
->mark
.value
== other_sa
->mark
.value
&&
418 sa
->mark
.mask
== other_sa
->mark
.mask
&&
419 sa
->if_id
== other_sa
->if_id
&&
420 ipsec_sa_cfg_equals(&sa
->cfg
, &other_sa
->cfg
);
424 * Allocate or reference an IPsec SA object
426 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
427 host_t
*src
, host_t
*dst
, mark_t mark
,
428 uint32_t if_id
, ipsec_sa_cfg_t
*cfg
)
430 ipsec_sa_t
*sa
, *found
;
438 found
= this->sas
->get(this->sas
, sa
);
441 sa
->src
= src
->clone(src
);
442 sa
->dst
= dst
->clone(dst
);
443 this->sas
->put(this->sas
, sa
, sa
);
450 ref_get(&sa
->refcount
);
455 * Release and destroy an IPsec SA object
457 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
460 if (ref_put(&sa
->refcount
))
462 this->sas
->remove(this->sas
, sa
);
469 typedef struct policy_sa_t policy_sa_t
;
470 typedef struct policy_sa_out_t policy_sa_out_t
;
473 * Mapping between a policy and an IPsec SA.
476 /** Priority assigned to the policy when installed with this SA */
479 /** Automatic priority assigned to the policy when installed with this SA */
480 uint32_t auto_priority
;
482 /** Type of the policy */
490 * For outbound policies we also cache the traffic selectors in order to install
493 struct policy_sa_out_t
{
494 /** Generic interface */
497 /** Source traffic selector of this policy */
498 traffic_selector_t
*src_ts
;
500 /** Destination traffic selector of this policy */
501 traffic_selector_t
*dst_ts
;
505 * Create a policy_sa(_in)_t object
507 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
508 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
509 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
510 uint32_t if_id
, ipsec_sa_cfg_t
*cfg
)
514 if (dir
== POLICY_OUT
)
516 policy_sa_out_t
*out
;
518 .src_ts
= src_ts
->clone(src_ts
),
519 .dst_ts
= dst_ts
->clone(dst_ts
),
521 policy
= &out
->generic
;
525 INIT(policy
, .priority
= 0);
528 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, if_id
, cfg
);
533 * Destroy a policy_sa(_in)_t object
535 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t dir
,
536 private_kernel_netlink_ipsec_t
*this)
538 if (dir
== POLICY_OUT
)
540 policy_sa_out_t
*out
= (policy_sa_out_t
*)policy
;
541 out
->src_ts
->destroy(out
->src_ts
);
542 out
->dst_ts
->destroy(out
->dst_ts
);
544 ipsec_sa_destroy(this, policy
->sa
);
548 CALLBACK(policy_sa_destroy_cb
, void,
549 policy_sa_t
*policy
, va_list args
)
551 private_kernel_netlink_ipsec_t
*this;
554 VA_ARGS_VGET(args
, dir
, this);
555 policy_sa_destroy(policy
, dir
, this);
558 typedef struct policy_entry_t policy_entry_t
;
561 * Installed kernel policy.
563 struct policy_entry_t
{
565 /** Direction of this policy: in, out, forward */
568 /** Parameters of installed policy */
569 struct xfrm_selector sel
;
574 /** Optional interface ID */
577 /** Associated route installed for this policy */
578 route_entry_t
*route
;
580 /** List of SAs this policy is used by, ordered by priority */
581 linked_list_t
*used_by
;
583 /** reqid for this policy */
586 /** Number of threads waiting to work on this policy */
589 /** TRUE if a thread is working on this policy */
594 * Destroy a policy_entry_t object
596 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
597 policy_entry_t
*policy
)
601 route_entry_destroy(policy
->route
);
605 policy
->used_by
->invoke_function(policy
->used_by
, policy_sa_destroy_cb
,
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_inc(chunk_from_thing(key
->mark
),
619 chunk_hash(chunk_from_thing(key
->if_id
))));
623 * Equality function for policy_entry_t objects
625 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
627 return memeq(&key
->sel
, &other_key
->sel
, sizeof(struct xfrm_selector
)) &&
628 key
->mark
== other_key
->mark
&&
629 key
->if_id
== other_key
->if_id
&&
630 key
->direction
== other_key
->direction
;
634 * Determine number of set bits in 16 bit port mask
636 static inline uint32_t port_mask_bits(uint16_t port_mask
)
639 uint16_t bit_mask
= 0x8000;
641 port_mask
= ntohs(port_mask
);
643 for (bits
= 0; bits
< 16; bits
++)
645 if (!(port_mask
& bit_mask
))
655 * Calculate the priority of a policy
657 * bits 0-0: separate trap and regular policies (0..1) 1 bit
658 * bits 1-1: restriction to network interface (0..1) 1 bit
659 * bits 2-7: src + dst port mask bits (2 * 0..16) 6 bits
660 * bits 8-8: restriction to protocol (0..1) 1 bit
661 * bits 9-17: src + dst network mask bits (2 * 0..128) 9 bits
664 * smallest value: 000000000 0 000000 0 0: 0, lowest priority = 200'000
665 * largest value : 100000000 1 100000 1 1: 131'459, highst priority = 68'541
667 static uint32_t get_priority(policy_entry_t
*policy
, policy_priority_t prio
,
670 uint32_t priority
= PRIO_BASE
, sport_mask_bits
, dport_mask_bits
;
674 case POLICY_PRIORITY_FALLBACK
:
675 priority
+= PRIO_BASE
;
676 /* fall-through to next case */
677 case POLICY_PRIORITY_ROUTED
:
678 case POLICY_PRIORITY_DEFAULT
:
679 priority
+= PRIO_BASE
;
680 /* fall-through to next case */
681 case POLICY_PRIORITY_PASS
:
684 sport_mask_bits
= port_mask_bits(policy
->sel
.sport_mask
);
685 dport_mask_bits
= port_mask_bits(policy
->sel
.dport_mask
);
687 /* calculate priority */
688 priority
-= (policy
->sel
.prefixlen_s
+ policy
->sel
.prefixlen_d
) * 512;
689 priority
-= policy
->sel
.proto ?
256 : 0;
690 priority
-= (sport_mask_bits
+ dport_mask_bits
) * 4;
691 priority
-= (interface
!= NULL
) * 2;
692 priority
-= (prio
!= POLICY_PRIORITY_ROUTED
);
698 * Convert the general ipsec mode to the one defined in xfrm.h
700 static uint8_t mode2kernel(ipsec_mode_t mode
)
705 return XFRM_MODE_TRANSPORT
;
707 return XFRM_MODE_TUNNEL
;
709 return XFRM_MODE_BEET
;
716 * Convert a host_t to a struct xfrm_address
718 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
720 chunk_t chunk
= host
->get_address(host
);
721 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
725 * Convert a struct xfrm_address to a host_t
727 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, uint16_t port
)
734 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
737 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
742 return host_create_from_chunk(family
, chunk
, ntohs(port
));
746 * Convert a traffic selector address range to subnet and its mask.
748 static void ts2subnet(traffic_selector_t
* ts
,
749 xfrm_address_t
*net
, uint8_t *mask
)
754 ts
->to_subnet(ts
, &net_host
, mask
);
755 net_chunk
= net_host
->get_address(net_host
);
756 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
757 net_host
->destroy(net_host
);
761 * Convert a traffic selector port range to port/portmask
763 static void ts2ports(traffic_selector_t
* ts
,
764 uint16_t *port
, uint16_t *mask
)
766 uint16_t from
, to
, bitmask
;
769 from
= ts
->get_from_port(ts
);
770 to
= ts
->get_to_port(ts
);
772 /* Quick check for a single port */
780 /* Compute the port mask for port ranges */
783 for (bit
= 15; bit
>= 0; bit
--)
787 if ((bitmask
& from
) != (bitmask
& to
))
789 *port
= htons(from
& *mask
);
790 *mask
= htons(*mask
);
800 * Convert a pair of traffic_selectors to an xfrm_selector
802 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
803 traffic_selector_t
*dst
,
806 struct xfrm_selector sel
;
809 memset(&sel
, 0, sizeof(sel
));
810 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
811 /* src or dest proto may be "any" (0), use more restrictive one */
812 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
813 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
814 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
815 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
816 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
817 if ((sel
.proto
== IPPROTO_ICMP
|| sel
.proto
== IPPROTO_ICMPV6
) &&
818 (sel
.dport
|| sel
.sport
))
820 /* the kernel expects the ICMP type and code in the source and
821 * destination port fields, respectively. */
822 port
= ntohs(max(sel
.dport
, sel
.sport
));
823 sel
.sport
= htons(traffic_selector_icmp_type(port
));
824 sel
.sport_mask
= sel
.sport ?
~0 : 0;
825 sel
.dport
= htons(traffic_selector_icmp_code(port
));
826 sel
.dport_mask
= sel
.dport ?
~0 : 0;
828 sel
.ifindex
= interface ?
if_nametoindex(interface
) : 0;
835 * Convert an xfrm_selector to a src|dst traffic_selector
837 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
846 addr
= (u_char
*)&sel
->saddr
;
847 prefixlen
= sel
->prefixlen_s
;
850 port
= ntohs(sel
->sport
);
855 addr
= (u_char
*)&sel
->daddr
;
856 prefixlen
= sel
->prefixlen_d
;
859 port
= ntohs(sel
->dport
);
862 if (sel
->proto
== IPPROTO_ICMP
|| sel
->proto
== IPPROTO_ICMPV6
)
863 { /* convert ICMP[v6] message type and code as supplied by the kernel in
864 * source and destination ports (both in network order) */
865 port
= (sel
->sport
>> 8) | (sel
->dport
& 0xff00);
868 /* The Linux 2.6 kernel does not set the selector's family field,
869 * so as a kludge we additionally test the prefix length.
871 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
873 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
875 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
877 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
882 return traffic_selector_create_from_subnet(host
, prefixlen
,
883 sel
->proto
, port
, port ?
: 65535);
889 * Process a XFRM_MSG_ACQUIRE from kernel
891 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
892 struct nlmsghdr
*hdr
)
894 struct xfrm_user_acquire
*acquire
;
897 traffic_selector_t
*src_ts
, *dst_ts
;
901 acquire
= NLMSG_DATA(hdr
);
902 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
903 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
905 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
907 while (RTA_OK(rta
, rtasize
))
909 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
911 if (rta
->rta_type
== XFRMA_TMPL
)
913 struct xfrm_user_tmpl
* tmpl
;
914 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
916 proto
= tmpl
->id
.proto
;
918 rta
= RTA_NEXT(rta
, rtasize
);
927 /* acquire for AH/ESP only, not for IPCOMP */
930 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
931 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
933 charon
->kernel
->acquire(charon
->kernel
, reqid
, src_ts
, dst_ts
);
937 * Process a XFRM_MSG_EXPIRE from kernel
939 static void process_expire(private_kernel_netlink_ipsec_t
*this,
940 struct nlmsghdr
*hdr
)
942 struct xfrm_user_expire
*expire
;
947 expire
= NLMSG_DATA(hdr
);
948 protocol
= expire
->state
.id
.proto
;
949 spi
= expire
->state
.id
.spi
;
951 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
953 if (protocol
== IPPROTO_ESP
|| protocol
== IPPROTO_AH
)
955 dst
= xfrm2host(expire
->state
.family
, &expire
->state
.id
.daddr
, 0);
958 charon
->kernel
->expire(charon
->kernel
, protocol
, spi
, dst
,
966 * Process a XFRM_MSG_MIGRATE from kernel
968 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
969 struct nlmsghdr
*hdr
)
971 struct xfrm_userpolicy_id
*policy_id
;
974 traffic_selector_t
*src_ts
, *dst_ts
;
975 host_t
*local
= NULL
, *remote
= NULL
;
976 host_t
*old_src
= NULL
, *old_dst
= NULL
;
977 host_t
*new_src
= NULL
, *new_dst
= NULL
;
981 policy_id
= NLMSG_DATA(hdr
);
982 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
983 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
985 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
987 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
988 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
989 dir
= (policy_dir_t
)policy_id
->dir
;
991 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
993 while (RTA_OK(rta
, rtasize
))
995 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
996 if (rta
->rta_type
== XFRMA_KMADDRESS
)
998 struct xfrm_user_kmaddress
*kmaddress
;
1000 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
1001 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
1002 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
1003 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
1005 else if (rta
->rta_type
== XFRMA_MIGRATE
)
1007 struct xfrm_user_migrate
*migrate
;
1009 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
1010 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
1011 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
1012 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
1013 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
1014 reqid
= migrate
->reqid
;
1015 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
1016 old_src
, old_dst
, new_src
, new_dst
, reqid
);
1017 DESTROY_IF(old_src
);
1018 DESTROY_IF(old_dst
);
1019 DESTROY_IF(new_src
);
1020 DESTROY_IF(new_dst
);
1022 rta
= RTA_NEXT(rta
, rtasize
);
1025 if (src_ts
&& dst_ts
&& local
&& remote
)
1027 charon
->kernel
->migrate(charon
->kernel
, reqid
, src_ts
, dst_ts
, dir
,
1040 * Process a XFRM_MSG_MAPPING from kernel
1042 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
1043 struct nlmsghdr
*hdr
)
1045 struct xfrm_user_mapping
*mapping
;
1048 mapping
= NLMSG_DATA(hdr
);
1049 spi
= mapping
->id
.spi
;
1051 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
1053 if (mapping
->id
.proto
== IPPROTO_ESP
)
1057 dst
= xfrm2host(mapping
->id
.family
, &mapping
->id
.daddr
, 0);
1060 new = xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
1061 mapping
->new_sport
);
1064 charon
->kernel
->mapping(charon
->kernel
, IPPROTO_ESP
, spi
, dst
,
1074 * Receives events from kernel
1076 static bool receive_events(private_kernel_netlink_ipsec_t
*this, int fd
,
1077 watcher_event_t event
)
1079 char response
[netlink_get_buflen()];
1080 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
1081 struct sockaddr_nl addr
;
1082 socklen_t addr_len
= sizeof(addr
);
1085 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
),
1086 MSG_DONTWAIT
, (struct sockaddr
*)&addr
, &addr_len
);
1092 /* interrupted, try again */
1095 /* no data ready, select again */
1098 DBG1(DBG_KNL
, "unable to receive from XFRM event socket: %s "
1099 "(%d)", strerror(errno
), errno
);
1105 if (addr
.nl_pid
!= 0)
1106 { /* not from kernel. not interested, try another one */
1110 while (NLMSG_OK(hdr
, len
))
1112 switch (hdr
->nlmsg_type
)
1114 case XFRM_MSG_ACQUIRE
:
1115 process_acquire(this, hdr
);
1117 case XFRM_MSG_EXPIRE
:
1118 process_expire(this, hdr
);
1120 case XFRM_MSG_MIGRATE
:
1121 process_migrate(this, hdr
);
1123 case XFRM_MSG_MAPPING
:
1124 process_mapping(this, hdr
);
1127 DBG1(DBG_KNL
, "received unknown event from XFRM event "
1128 "socket: %d", hdr
->nlmsg_type
);
1131 hdr
= NLMSG_NEXT(hdr
, len
);
1136 METHOD(kernel_ipsec_t
, get_features
, kernel_feature_t
,
1137 private_kernel_netlink_ipsec_t
*this)
1139 return KERNEL_ESP_V3_TFC
| KERNEL_POLICY_SPI
;
1143 * Get an SPI for a specific protocol from the kernel.
1145 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
1146 host_t
*src
, host_t
*dst
, uint8_t proto
, uint32_t min
, uint32_t max
,
1149 netlink_buf_t request
;
1150 struct nlmsghdr
*hdr
, *out
;
1151 struct xfrm_userspi_info
*userspi
;
1152 uint32_t received_spi
= 0;
1155 memset(&request
, 0, sizeof(request
));
1158 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1159 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1160 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1162 userspi
= NLMSG_DATA(hdr
);
1163 host2xfrm(src
, &userspi
->info
.saddr
);
1164 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1165 userspi
->info
.id
.proto
= proto
;
1166 userspi
->info
.mode
= XFRM_MODE_TUNNEL
;
1167 userspi
->info
.family
= src
->get_family(src
);
1171 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1174 while (NLMSG_OK(hdr
, len
))
1176 switch (hdr
->nlmsg_type
)
1178 case XFRM_MSG_NEWSA
:
1180 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1181 received_spi
= usersa
->id
.spi
;
1186 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1187 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1188 strerror(-err
->error
), -err
->error
);
1192 hdr
= NLMSG_NEXT(hdr
, len
);
1202 if (received_spi
== 0)
1207 *spi
= received_spi
;
1211 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1212 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1213 uint8_t protocol
, uint32_t *spi
)
1215 uint32_t spi_min
, spi_max
;
1217 spi_min
= lib
->settings
->get_int(lib
->settings
, "%s.spi_min",
1218 KERNEL_SPI_MIN
, lib
->ns
);
1219 spi_max
= lib
->settings
->get_int(lib
->settings
, "%s.spi_max",
1220 KERNEL_SPI_MAX
, lib
->ns
);
1222 if (get_spi_internal(this, src
, dst
, protocol
, min(spi_min
, spi_max
),
1223 max(spi_min
, spi_max
), spi
) != SUCCESS
)
1225 DBG1(DBG_KNL
, "unable to get SPI");
1229 DBG2(DBG_KNL
, "got SPI %.8x", ntohl(*spi
));
1233 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1234 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1237 uint32_t received_spi
= 0;
1239 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1240 0x100, 0xEFFF, &received_spi
) != SUCCESS
)
1242 DBG1(DBG_KNL
, "unable to get CPI");
1246 *cpi
= htons((uint16_t)ntohl(received_spi
));
1248 DBG2(DBG_KNL
, "got CPI %.4x", ntohs(*cpi
));
1253 * Format the mark for debug messages
1255 static void format_mark(char *buf
, int buflen
, mark_t mark
)
1257 if (mark
.value
| mark
.mask
)
1259 snprintf(buf
, buflen
, " (mark %u/0x%08x)", mark
.value
, mark
.mask
);
1264 * Add a XFRM mark to message if required
1266 static bool add_mark(struct nlmsghdr
*hdr
, int buflen
, mark_t mark
)
1268 if (mark
.value
| mark
.mask
)
1270 struct xfrm_mark
*xmrk
;
1272 xmrk
= netlink_reserve(hdr
, buflen
, XFRMA_MARK
, sizeof(*xmrk
));
1277 xmrk
->v
= mark
.value
;
1278 xmrk
->m
= mark
.mask
;
1284 * Add a uint32 attribute to message
1286 static bool add_uint32(struct nlmsghdr
*hdr
, int buflen
,
1287 enum xfrm_attr_type_t type
, uint32_t value
)
1291 xvalue
= netlink_reserve(hdr
, buflen
, type
, sizeof(*xvalue
));
1300 /* ETHTOOL_GSSET_INFO is available since 2.6.34 and ETH_SS_FEATURES (enum) and
1301 * ETHTOOL_GFEATURES since 2.6.39, so check for the latter */
1302 #ifdef ETHTOOL_GFEATURES
1305 * Global metadata used for IPsec HW offload
1308 /** determined HW offload support */
1310 /** bit in feature set */
1312 /** total number of device feature blocks */
1314 } netlink_hw_offload
;
1317 * Check if kernel supports HW offload and determine feature flag
1319 static void netlink_find_offload_feature(const char *ifname
)
1321 struct ethtool_sset_info
*sset_info
;
1322 struct ethtool_gstrings
*cmd
= NULL
;
1324 uint32_t sset_len
, i
;
1326 int err
, query_socket
;
1328 query_socket
= socket(AF_NETLINK
, SOCK_DGRAM
, NETLINK_XFRM
);
1329 if (query_socket
< 0)
1334 /* determine number of device features */
1335 INIT_EXTRA(sset_info
, sizeof(uint32_t),
1336 .cmd
= ETHTOOL_GSSET_INFO
,
1337 .sset_mask
= 1ULL << ETH_SS_FEATURES
,
1339 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1340 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1341 ifr
.ifr_data
= (void*)sset_info
;
1343 err
= ioctl(query_socket
, SIOCETHTOOL
, &ifr
);
1344 if (err
|| sset_info
->sset_mask
!= 1ULL << ETH_SS_FEATURES
)
1348 sset_len
= sset_info
->data
[0];
1350 /* retrieve names of device features */
1351 INIT_EXTRA(cmd
, ETH_GSTRING_LEN
* sset_len
,
1352 .cmd
= ETHTOOL_GSTRINGS
,
1353 .string_set
= ETH_SS_FEATURES
,
1355 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1356 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1357 ifr
.ifr_data
= (void*)cmd
;
1359 err
= ioctl(query_socket
, SIOCETHTOOL
, &ifr
);
1365 /* look for the ESP_HW feature bit */
1366 str
= (char*)cmd
->data
;
1367 for (i
= 0; i
< cmd
->len
; i
++)
1369 if (strneq(str
, "esp-hw-offload", ETH_GSTRING_LEN
))
1371 netlink_hw_offload
.supported
= TRUE
;
1372 netlink_hw_offload
.bit
= i
;
1373 netlink_hw_offload
.total_blocks
= (sset_len
+ 31) / 32;
1376 str
+= ETH_GSTRING_LEN
;
1382 close(query_socket
);
1386 * Check if interface supported HW offload
1388 static bool netlink_detect_offload(const char *ifname
)
1390 struct ethtool_gfeatures
*cmd
;
1391 uint32_t feature_bit
;
1397 if (!netlink_hw_offload
.supported
)
1399 DBG1(DBG_KNL
, "HW offload is not supported by kernel");
1403 query_socket
= socket(AF_NETLINK
, SOCK_DGRAM
, NETLINK_XFRM
);
1404 if (query_socket
< 0)
1409 /* feature is supported by kernel, query device features */
1410 INIT_EXTRA(cmd
, sizeof(cmd
->features
[0]) * netlink_hw_offload
.total_blocks
,
1411 .cmd
= ETHTOOL_GFEATURES
,
1412 .size
= netlink_hw_offload
.total_blocks
,
1414 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1415 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1416 ifr
.ifr_data
= (void*)cmd
;
1418 if (!ioctl(query_socket
, SIOCETHTOOL
, &ifr
))
1420 block
= netlink_hw_offload
.bit
/ 32;
1421 feature_bit
= 1U << (netlink_hw_offload
.bit
% 32);
1422 if (cmd
->features
[block
].active
& feature_bit
)
1430 DBG1(DBG_KNL
, "HW offload is not supported by device");
1433 close(query_socket
);
1439 static void netlink_find_offload_feature(const char *ifname
)
1443 static bool netlink_detect_offload(const char *ifname
)
1451 * There are 3 HW offload configuration values:
1452 * 1. HW_OFFLOAD_NO : Do not configure HW offload.
1453 * 2. HW_OFFLOAD_YES : Configure HW offload.
1454 * Fail SA addition if offload is not supported.
1455 * 3. HW_OFFLOAD_AUTO : Configure HW offload if supported by the kernel
1457 * Do not fail SA addition otherwise.
1459 static bool config_hw_offload(kernel_ipsec_sa_id_t
*id
,
1460 kernel_ipsec_add_sa_t
*data
, struct nlmsghdr
*hdr
,
1463 host_t
*local
= data
->inbound ? id
->dst
: id
->src
;
1464 struct xfrm_user_offload
*offload
;
1465 bool hw_offload_yes
, ret
= FALSE
;
1468 /* do Ipsec configuration without offload */
1469 if (data
->hw_offload
== HW_OFFLOAD_NO
)
1474 hw_offload_yes
= (data
->hw_offload
== HW_OFFLOAD_YES
);
1476 if (!charon
->kernel
->get_interface(charon
->kernel
, local
, &ifname
))
1478 return !hw_offload_yes
;
1481 /* check if interface supports hw_offload */
1482 if (!netlink_detect_offload(ifname
))
1484 ret
= !hw_offload_yes
;
1488 /* activate HW offload */
1489 offload
= netlink_reserve(hdr
, buflen
,
1490 XFRMA_OFFLOAD_DEV
, sizeof(*offload
));
1493 ret
= !hw_offload_yes
;
1496 offload
->ifindex
= if_nametoindex(ifname
);
1497 if (local
->get_family(local
) == AF_INET6
)
1499 offload
->flags
|= XFRM_OFFLOAD_IPV6
;
1501 offload
->flags
|= data
->inbound ? XFRM_OFFLOAD_INBOUND
: 0;
1510 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1511 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1512 kernel_ipsec_add_sa_t
*data
)
1514 netlink_buf_t request
;
1515 const char *alg_name
;
1516 char markstr
[32] = "";
1517 struct nlmsghdr
*hdr
;
1518 struct xfrm_usersa_info
*sa
;
1519 uint16_t icv_size
= 64, ipcomp
= data
->ipcomp
;
1520 ipsec_mode_t mode
= data
->mode
, original_mode
= data
->mode
;
1521 traffic_selector_t
*first_src_ts
, *first_dst_ts
;
1522 status_t status
= FAILED
;
1524 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1525 * we are in the recursive call below */
1526 if (ipcomp
!= IPCOMP_NONE
&& data
->cpi
!= 0)
1528 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1529 kernel_ipsec_sa_id_t ipcomp_id
= {
1532 .spi
= htonl(ntohs(data
->cpi
)),
1533 .proto
= IPPROTO_COMP
,
1537 kernel_ipsec_add_sa_t ipcomp_sa
= {
1538 .reqid
= data
->reqid
,
1540 .src_ts
= data
->src_ts
,
1541 .dst_ts
= data
->dst_ts
,
1543 .enc_alg
= ENCR_UNDEFINED
,
1544 .int_alg
= AUTH_UNDEFINED
,
1546 .ipcomp
= data
->ipcomp
,
1547 .initiator
= data
->initiator
,
1548 .inbound
= data
->inbound
,
1549 .update
= data
->update
,
1551 add_sa(this, &ipcomp_id
, &ipcomp_sa
);
1552 ipcomp
= IPCOMP_NONE
;
1553 /* use transport mode ESP SA, IPComp uses tunnel mode */
1554 mode
= MODE_TRANSPORT
;
1557 memset(&request
, 0, sizeof(request
));
1558 format_mark(markstr
, sizeof(markstr
), id
->mark
);
1560 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}%s",
1561 ntohl(id
->spi
), data
->reqid
, markstr
);
1564 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1565 hdr
->nlmsg_type
= data
->update ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1566 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1568 sa
= NLMSG_DATA(hdr
);
1569 host2xfrm(id
->src
, &sa
->saddr
);
1570 host2xfrm(id
->dst
, &sa
->id
.daddr
);
1571 sa
->id
.spi
= id
->spi
;
1572 sa
->id
.proto
= id
->proto
;
1573 sa
->family
= id
->src
->get_family(id
->src
);
1574 sa
->mode
= mode2kernel(mode
);
1578 sa
->flags
|= XFRM_STATE_NOPMTUDISC
;
1581 if (!data
->copy_ecn
)
1583 sa
->flags
|= XFRM_STATE_NOECN
;
1588 switch (data
->copy_dscp
)
1591 case DSCP_COPY_IN_ONLY
:
1592 sa
->flags
|= XFRM_STATE_DECAP_DSCP
;
1600 switch (data
->copy_dscp
)
1602 case DSCP_COPY_IN_ONLY
:
1605 /* currently the only extra flag */
1606 if (!add_uint32(hdr
, sizeof(request
), XFRMA_SA_EXTRA_FLAGS
,
1607 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
))
1621 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1624 case MODE_TRANSPORT
:
1625 if (original_mode
== MODE_TUNNEL
)
1626 { /* don't install selectors for switched SAs. because only one
1627 * selector can be installed other traffic would get dropped */
1630 if (data
->src_ts
->get_first(data
->src_ts
,
1631 (void**)&first_src_ts
) == SUCCESS
&&
1632 data
->dst_ts
->get_first(data
->dst_ts
,
1633 (void**)&first_dst_ts
) == SUCCESS
)
1635 sa
->sel
= ts2selector(first_src_ts
, first_dst_ts
,
1637 if (!this->proto_port_transport
)
1639 /* don't install proto/port on SA. This would break
1640 * potential secondary SAs for the same address using a
1641 * different prot/port. */
1643 sa
->sel
.dport
= sa
->sel
.dport_mask
= 0;
1644 sa
->sel
.sport
= sa
->sel
.sport_mask
= 0;
1651 if (id
->proto
== IPPROTO_AH
&& sa
->family
== AF_INET
)
1652 { /* use alignment to 4 bytes for IPv4 instead of the incorrect 8 byte
1653 * alignment that's used by default but is only valid for IPv6 */
1654 sa
->flags
|= XFRM_STATE_ALIGN4
;
1657 sa
->reqid
= data
->reqid
;
1658 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(data
->lifetime
->bytes
.rekey
);
1659 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(data
->lifetime
->bytes
.life
);
1660 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(data
->lifetime
->packets
.rekey
);
1661 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(data
->lifetime
->packets
.life
);
1662 /* we use lifetimes since added, not since used */
1663 sa
->lft
.soft_add_expires_seconds
= data
->lifetime
->time
.rekey
;
1664 sa
->lft
.hard_add_expires_seconds
= data
->lifetime
->time
.life
;
1665 sa
->lft
.soft_use_expires_seconds
= 0;
1666 sa
->lft
.hard_use_expires_seconds
= 0;
1668 switch (data
->enc_alg
)
1670 case ENCR_UNDEFINED
:
1673 case ENCR_AES_CCM_ICV16
:
1674 case ENCR_AES_GCM_ICV16
:
1675 case ENCR_NULL_AUTH_AES_GMAC
:
1676 case ENCR_CAMELLIA_CCM_ICV16
:
1677 case ENCR_CHACHA20_POLY1305
:
1680 case ENCR_AES_CCM_ICV12
:
1681 case ENCR_AES_GCM_ICV12
:
1682 case ENCR_CAMELLIA_CCM_ICV12
:
1685 case ENCR_AES_CCM_ICV8
:
1686 case ENCR_AES_GCM_ICV8
:
1687 case ENCR_CAMELLIA_CCM_ICV8
:
1689 struct xfrm_algo_aead
*algo
;
1691 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, data
->enc_alg
);
1692 if (alg_name
== NULL
)
1694 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1695 encryption_algorithm_names
, data
->enc_alg
);
1698 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1699 encryption_algorithm_names
, data
->enc_alg
,
1700 data
->enc_key
.len
* 8);
1702 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AEAD
,
1703 sizeof(*algo
) + data
->enc_key
.len
);
1708 algo
->alg_key_len
= data
->enc_key
.len
* 8;
1709 algo
->alg_icv_len
= icv_size
;
1710 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1711 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1712 memcpy(algo
->alg_key
, data
->enc_key
.ptr
, data
->enc_key
.len
);
1717 struct xfrm_algo
*algo
;
1719 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, data
->enc_alg
);
1720 if (alg_name
== NULL
)
1722 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1723 encryption_algorithm_names
, data
->enc_alg
);
1726 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1727 encryption_algorithm_names
, data
->enc_alg
,
1728 data
->enc_key
.len
* 8);
1730 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_CRYPT
,
1731 sizeof(*algo
) + data
->enc_key
.len
);
1736 algo
->alg_key_len
= data
->enc_key
.len
* 8;
1737 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1738 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1739 memcpy(algo
->alg_key
, data
->enc_key
.ptr
, data
->enc_key
.len
);
1743 if (data
->int_alg
!= AUTH_UNDEFINED
)
1745 u_int trunc_len
= 0;
1747 alg_name
= lookup_algorithm(INTEGRITY_ALGORITHM
, data
->int_alg
);
1748 if (alg_name
== NULL
)
1750 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1751 integrity_algorithm_names
, data
->int_alg
);
1754 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1755 integrity_algorithm_names
, data
->int_alg
, data
->int_key
.len
* 8);
1757 switch (data
->int_alg
)
1759 case AUTH_HMAC_MD5_128
:
1760 case AUTH_HMAC_SHA2_256_128
:
1763 case AUTH_HMAC_SHA1_160
:
1772 struct xfrm_algo_auth
* algo
;
1774 /* the kernel uses SHA256 with 96 bit truncation by default,
1775 * use specified truncation size supported by newer kernels.
1776 * also use this for untruncated MD5 and SHA1. */
1777 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH_TRUNC
,
1778 sizeof(*algo
) + data
->int_key
.len
);
1783 algo
->alg_key_len
= data
->int_key
.len
* 8;
1784 algo
->alg_trunc_len
= trunc_len
;
1785 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1786 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1787 memcpy(algo
->alg_key
, data
->int_key
.ptr
, data
->int_key
.len
);
1791 struct xfrm_algo
* algo
;
1793 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH
,
1794 sizeof(*algo
) + data
->int_key
.len
);
1799 algo
->alg_key_len
= data
->int_key
.len
* 8;
1800 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1801 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1802 memcpy(algo
->alg_key
, data
->int_key
.ptr
, data
->int_key
.len
);
1806 if (ipcomp
!= IPCOMP_NONE
)
1808 struct xfrm_algo
* algo
;
1810 alg_name
= lookup_algorithm(COMPRESSION_ALGORITHM
, ipcomp
);
1811 if (alg_name
== NULL
)
1813 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1814 ipcomp_transform_names
, ipcomp
);
1817 DBG2(DBG_KNL
, " using compression algorithm %N",
1818 ipcomp_transform_names
, ipcomp
);
1820 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_COMP
,
1826 algo
->alg_key_len
= 0;
1827 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1828 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1833 struct xfrm_encap_tmpl
*tmpl
;
1835 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
, sizeof(*tmpl
));
1840 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1841 tmpl
->encap_sport
= htons(id
->src
->get_port(id
->src
));
1842 tmpl
->encap_dport
= htons(id
->dst
->get_port(id
->dst
));
1843 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1844 /* encap_oa could probably be derived from the
1845 * traffic selectors [rfc4306, p39]. In the netlink kernel
1846 * implementation pluto does the same as we do here but it uses
1847 * encap_oa in the pfkey implementation.
1848 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1850 * -> does that mean that NAT-T encap doesn't work in transport mode?
1851 * No. The reason the kernel ignores NAT-OA is that it recomputes
1852 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1853 * checks it marks them "checksum ok" so OA isn't needed. */
1856 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
1861 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
1866 if (ipcomp
== IPCOMP_NONE
&& (data
->mark
.value
| data
->mark
.mask
))
1868 if (!add_uint32(hdr
, sizeof(request
), XFRMA_SET_MARK
,
1869 data
->mark
.value
) ||
1870 !add_uint32(hdr
, sizeof(request
), XFRMA_SET_MARK_MASK
,
1877 if (data
->tfc
&& id
->proto
== IPPROTO_ESP
&& mode
== MODE_TUNNEL
)
1878 { /* the kernel supports TFC padding only for tunnel mode ESP SAs */
1879 if (!add_uint32(hdr
, sizeof(request
), XFRMA_TFCPAD
, data
->tfc
))
1885 if (id
->proto
!= IPPROTO_COMP
)
1887 /* generally, we don't need a replay window for outbound SAs, however,
1888 * when using ESN the kernel rejects the attribute if it is 0 */
1889 if (!data
->inbound
&& data
->replay_window
)
1891 data
->replay_window
= data
->esn ?
1 : 0;
1893 if (data
->replay_window
!= 0 && (data
->esn
|| data
->replay_window
> 32))
1895 /* for ESN or larger replay windows we need the new
1896 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1897 struct xfrm_replay_state_esn
*replay
;
1900 bmp_size
= round_up(data
->replay_window
, sizeof(uint32_t) * 8) / 8;
1901 replay
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
1902 sizeof(*replay
) + bmp_size
);
1907 /* bmp_len contains number uf __u32's */
1908 replay
->bmp_len
= bmp_size
/ sizeof(uint32_t);
1909 replay
->replay_window
= data
->replay_window
;
1910 DBG2(DBG_KNL
, " using replay window of %u packets",
1911 data
->replay_window
);
1915 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1916 sa
->flags
|= XFRM_STATE_ESN
;
1921 DBG2(DBG_KNL
, " using replay window of %u packets",
1922 data
->replay_window
);
1923 sa
->replay_window
= data
->replay_window
;
1926 DBG2(DBG_KNL
, " HW offload: %N", hw_offload_names
, data
->hw_offload
);
1927 if (!config_hw_offload(id
, data
, hdr
, sizeof(request
)))
1929 DBG1(DBG_KNL
, "failed to configure HW offload");
1934 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
1935 if (status
== NOT_FOUND
&& data
->update
)
1937 DBG1(DBG_KNL
, "allocated SPI not found anymore, try to add SAD entry");
1938 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1939 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
1942 if (status
!= SUCCESS
)
1944 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x%s (%N)", ntohl(id
->spi
),
1945 markstr
, status_names
, status
);
1953 memwipe(&request
, sizeof(request
));
1958 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1960 * Allocates into one the replay state structure we get from the kernel.
1962 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
1963 kernel_ipsec_sa_id_t
*sa
,
1964 struct xfrm_replay_state_esn
**replay_esn
,
1965 uint32_t *replay_esn_len
,
1966 struct xfrm_replay_state
**replay
,
1967 struct xfrm_lifetime_cur
**lifetime
)
1969 netlink_buf_t request
;
1970 struct nlmsghdr
*hdr
, *out
= NULL
;
1971 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1976 memset(&request
, 0, sizeof(request
));
1978 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
1982 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1983 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1984 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1986 aevent_id
= NLMSG_DATA(hdr
);
1987 aevent_id
->flags
= XFRM_AE_RVAL
;
1989 host2xfrm(sa
->dst
, &aevent_id
->sa_id
.daddr
);
1990 aevent_id
->sa_id
.spi
= sa
->spi
;
1991 aevent_id
->sa_id
.proto
= sa
->proto
;
1992 aevent_id
->sa_id
.family
= sa
->dst
->get_family(sa
->dst
);
1994 if (!add_mark(hdr
, sizeof(request
), sa
->mark
))
1998 if (sa
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, sa
->if_id
))
2003 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2006 while (NLMSG_OK(hdr
, len
))
2008 switch (hdr
->nlmsg_type
)
2010 case XFRM_MSG_NEWAE
:
2012 out_aevent
= NLMSG_DATA(hdr
);
2017 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2018 DBG1(DBG_KNL
, "querying replay state from SAD entry "
2019 "failed: %s (%d)", strerror(-err
->error
), -err
->error
);
2023 hdr
= NLMSG_NEXT(hdr
, len
);
2034 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
2035 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
2036 while (RTA_OK(rta
, rtasize
))
2038 if (rta
->rta_type
== XFRMA_LTIME_VAL
&&
2039 RTA_PAYLOAD(rta
) == sizeof(**lifetime
))
2042 *lifetime
= malloc(RTA_PAYLOAD(rta
));
2043 memcpy(*lifetime
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2045 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
2046 RTA_PAYLOAD(rta
) == sizeof(**replay
))
2049 *replay
= malloc(RTA_PAYLOAD(rta
));
2050 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2052 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
&&
2053 RTA_PAYLOAD(rta
) >= sizeof(**replay_esn
))
2056 *replay_esn
= malloc(RTA_PAYLOAD(rta
));
2057 *replay_esn_len
= RTA_PAYLOAD(rta
);
2058 memcpy(*replay_esn
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2060 rta
= RTA_NEXT(rta
, rtasize
);
2066 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
2067 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2068 kernel_ipsec_query_sa_t
*data
, uint64_t *bytes
, uint64_t *packets
,
2071 netlink_buf_t request
;
2072 struct nlmsghdr
*out
= NULL
, *hdr
;
2073 struct xfrm_usersa_id
*sa_id
;
2074 struct xfrm_usersa_info
*sa
= NULL
;
2075 status_t status
= FAILED
;
2077 char markstr
[32] = "";
2079 memset(&request
, 0, sizeof(request
));
2080 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2082 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x%s", ntohl(id
->spi
),
2086 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2087 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2088 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2090 sa_id
= NLMSG_DATA(hdr
);
2091 host2xfrm(id
->dst
, &sa_id
->daddr
);
2092 sa_id
->spi
= id
->spi
;
2093 sa_id
->proto
= id
->proto
;
2094 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2096 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2100 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2105 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2108 while (NLMSG_OK(hdr
, len
))
2110 switch (hdr
->nlmsg_type
)
2112 case XFRM_MSG_NEWSA
:
2114 sa
= NLMSG_DATA(hdr
);
2119 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2121 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x%s failed: "
2122 "%s (%d)", ntohl(id
->spi
), markstr
,
2123 strerror(-err
->error
), -err
->error
);
2127 hdr
= NLMSG_NEXT(hdr
, len
);
2138 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x%s",
2139 ntohl(id
->spi
), markstr
);
2145 *bytes
= sa
->curlft
.bytes
;
2149 *packets
= sa
->curlft
.packets
;
2152 { /* curlft contains an "use" time, but that contains a timestamp
2153 * of the first use, not the last. Last use time must be queried
2154 * on the policy on Linux */
2164 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
2165 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2166 kernel_ipsec_del_sa_t
*data
)
2168 netlink_buf_t request
;
2169 struct nlmsghdr
*hdr
;
2170 struct xfrm_usersa_id
*sa_id
;
2171 char markstr
[32] = "";
2173 /* if IPComp was used, we first delete the additional IPComp SA */
2176 kernel_ipsec_sa_id_t ipcomp_id
= {
2179 .spi
= htonl(ntohs(data
->cpi
)),
2180 .proto
= IPPROTO_COMP
,
2183 kernel_ipsec_del_sa_t ipcomp
= {};
2184 del_sa(this, &ipcomp_id
, &ipcomp
);
2187 memset(&request
, 0, sizeof(request
));
2188 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2190 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x%s", ntohl(id
->spi
),
2194 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2195 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
2196 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2198 sa_id
= NLMSG_DATA(hdr
);
2199 host2xfrm(id
->dst
, &sa_id
->daddr
);
2200 sa_id
->spi
= id
->spi
;
2201 sa_id
->proto
= id
->proto
;
2202 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2204 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2208 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2213 switch (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
))
2216 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x%s",
2217 ntohl(id
->spi
), markstr
);
2222 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x%s",
2223 ntohl(id
->spi
), markstr
);
2228 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
2229 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2230 kernel_ipsec_update_sa_t
*data
)
2232 netlink_buf_t request
;
2233 struct nlmsghdr
*hdr
, *out_hdr
= NULL
, *out
= NULL
;
2234 struct xfrm_usersa_id
*sa_id
;
2235 struct xfrm_usersa_info
*sa
;
2239 struct xfrm_encap_tmpl
* encap
= NULL
;
2240 struct xfrm_replay_state
*replay
= NULL
;
2241 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
2242 struct xfrm_lifetime_cur
*lifetime
= NULL
;
2243 uint32_t replay_esn_len
= 0;
2244 kernel_ipsec_del_sa_t del
= { 0 };
2245 status_t status
= FAILED
;
2246 traffic_selector_t
*ts
;
2247 char markstr
[32] = "";
2249 /* if IPComp is used, we first update the IPComp SA */
2252 kernel_ipsec_sa_id_t ipcomp_id
= {
2255 .spi
= htonl(ntohs(data
->cpi
)),
2256 .proto
= IPPROTO_COMP
,
2260 kernel_ipsec_update_sa_t ipcomp
= {
2261 .new_src
= data
->new_src
,
2262 .new_dst
= data
->new_dst
,
2264 update_sa(this, &ipcomp_id
, &ipcomp
);
2267 memset(&request
, 0, sizeof(request
));
2268 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2270 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x%s for update",
2271 ntohl(id
->spi
), markstr
);
2273 /* query the existing SA first */
2275 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2276 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2277 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2279 sa_id
= NLMSG_DATA(hdr
);
2280 host2xfrm(id
->dst
, &sa_id
->daddr
);
2281 sa_id
->spi
= id
->spi
;
2282 sa_id
->proto
= id
->proto
;
2283 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2285 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2289 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2294 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2297 while (NLMSG_OK(hdr
, len
))
2299 switch (hdr
->nlmsg_type
)
2301 case XFRM_MSG_NEWSA
:
2308 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2309 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2310 strerror(-err
->error
), -err
->error
);
2314 hdr
= NLMSG_NEXT(hdr
, len
);
2324 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x%s",
2325 ntohl(id
->spi
), markstr
);
2329 get_replay_state(this, id
, &replay_esn
, &replay_esn_len
, &replay
,
2332 /* delete the old SA (without affecting the IPComp SA) */
2333 if (del_sa(this, id
, &del
) != SUCCESS
)
2335 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x%s",
2336 ntohl(id
->spi
), markstr
);
2340 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x%s from %#H..%#H to "
2341 "%#H..%#H", ntohl(id
->spi
), markstr
, id
->src
, id
->dst
, data
->new_src
,
2343 /* copy over the SA from out to request */
2345 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2346 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2347 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2348 sa
= NLMSG_DATA(hdr
);
2349 memcpy(sa
, NLMSG_DATA(out_hdr
), sizeof(struct xfrm_usersa_info
));
2350 sa
->family
= data
->new_dst
->get_family(data
->new_dst
);
2352 if (!id
->src
->ip_equals(id
->src
, data
->new_src
))
2354 host2xfrm(data
->new_src
, &sa
->saddr
);
2356 ts
= selector2ts(&sa
->sel
, TRUE
);
2357 if (ts
&& ts
->is_host(ts
, id
->src
))
2359 ts
->set_address(ts
, data
->new_src
);
2360 ts2subnet(ts
, &sa
->sel
.saddr
, &sa
->sel
.prefixlen_s
);
2364 if (!id
->dst
->ip_equals(id
->dst
, data
->new_dst
))
2366 host2xfrm(data
->new_dst
, &sa
->id
.daddr
);
2368 ts
= selector2ts(&sa
->sel
, FALSE
);
2369 if (ts
&& ts
->is_host(ts
, id
->dst
))
2371 ts
->set_address(ts
, data
->new_dst
);
2372 ts2subnet(ts
, &sa
->sel
.daddr
, &sa
->sel
.prefixlen_d
);
2377 rta
= XFRM_RTA(out_hdr
, struct xfrm_usersa_info
);
2378 rtasize
= XFRM_PAYLOAD(out_hdr
, struct xfrm_usersa_info
);
2379 while (RTA_OK(rta
, rtasize
))
2381 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2382 if (rta
->rta_type
!= XFRMA_ENCAP
|| data
->new_encap
)
2384 if (rta
->rta_type
== XFRMA_ENCAP
)
2385 { /* update encap tmpl */
2386 encap
= RTA_DATA(rta
);
2387 encap
->encap_sport
= ntohs(data
->new_src
->get_port(data
->new_src
));
2388 encap
->encap_dport
= ntohs(data
->new_dst
->get_port(data
->new_dst
));
2390 if (rta
->rta_type
== XFRMA_OFFLOAD_DEV
)
2391 { /* update offload device */
2392 struct xfrm_user_offload
*offload
;
2396 offload
= RTA_DATA(rta
);
2397 local
= offload
->flags
& XFRM_OFFLOAD_INBOUND ? data
->new_dst
2400 if (charon
->kernel
->get_interface(charon
->kernel
, local
,
2403 offload
->ifindex
= if_nametoindex(ifname
);
2404 if (local
->get_family(local
) == AF_INET6
)
2406 offload
->flags
|= XFRM_OFFLOAD_IPV6
;
2410 offload
->flags
&= ~XFRM_OFFLOAD_IPV6
;
2415 netlink_add_attribute(hdr
, rta
->rta_type
,
2416 chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
)),
2419 rta
= RTA_NEXT(rta
, rtasize
);
2422 if (encap
== NULL
&& data
->new_encap
)
2423 { /* add tmpl if we are enabling it */
2424 encap
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
,
2430 encap
->encap_type
= UDP_ENCAP_ESPINUDP
;
2431 encap
->encap_sport
= ntohs(data
->new_src
->get_port(data
->new_src
));
2432 encap
->encap_dport
= ntohs(data
->new_dst
->get_port(data
->new_dst
));
2433 memset(&encap
->encap_oa
, 0, sizeof (xfrm_address_t
));
2438 struct xfrm_replay_state_esn
*state
;
2440 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
2446 memcpy(state
, replay_esn
, replay_esn_len
);
2450 struct xfrm_replay_state
*state
;
2452 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_VAL
,
2458 memcpy(state
, replay
, sizeof(*state
));
2462 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry with "
2463 "SPI %.8x%s", ntohl(id
->spi
), markstr
);
2467 struct xfrm_lifetime_cur
*state
;
2469 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_LTIME_VAL
,
2475 memcpy(state
, lifetime
, sizeof(*state
));
2479 DBG1(DBG_KNL
, "unable to copy usage stats from old SAD entry with "
2480 "SPI %.8x%s", ntohl(id
->spi
), markstr
);
2483 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2485 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x%s",
2486 ntohl(id
->spi
), markstr
);
2496 memwipe(&request
, sizeof(request
));
2502 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
2503 private_kernel_netlink_ipsec_t
*this)
2505 netlink_buf_t request
;
2506 struct nlmsghdr
*hdr
;
2507 struct xfrm_usersa_flush
*flush
;
2512 { IPPROTO_AH
, "AH" },
2513 { IPPROTO_ESP
, "ESP" },
2514 { IPPROTO_COMP
, "IPComp" },
2518 memset(&request
, 0, sizeof(request
));
2521 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2522 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
2523 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
2525 flush
= NLMSG_DATA(hdr
);
2527 for (i
= 0; i
< countof(protos
); i
++)
2529 DBG2(DBG_KNL
, "flushing all %s SAD entries", protos
[i
].name
);
2531 flush
->proto
= protos
[i
].proto
;
2533 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2535 DBG1(DBG_KNL
, "unable to flush %s SAD entries", protos
[i
].name
);
2543 * Unlock the mutex and signal waiting threads
2545 static void policy_change_done(private_kernel_netlink_ipsec_t
*this,
2546 policy_entry_t
*policy
)
2548 policy
->working
= FALSE
;
2549 if (policy
->waiting
)
2550 { /* don't need to wake threads waiting for other policies */
2551 this->condvar
->broadcast(this->condvar
);
2553 this->mutex
->unlock(this->mutex
);
2557 * Install a route for the given policy if enabled and required
2559 static void install_route(private_kernel_netlink_ipsec_t
*this,
2560 policy_entry_t
*policy
, policy_sa_t
*mapping
, ipsec_sa_t
*ipsec
)
2562 policy_sa_out_t
*out
= (policy_sa_out_t
*)mapping
;
2563 route_entry_t
*route
;
2567 .prefixlen
= policy
->sel
.prefixlen_d
,
2568 .pass
= mapping
->type
== POLICY_PASS
,
2571 if (charon
->kernel
->get_address_by_ts(charon
->kernel
, out
->src_ts
,
2572 &route
->src_ip
, NULL
) != SUCCESS
)
2579 /* allow blank source IP for passthrough policies */
2580 route
->src_ip
= host_create_any(policy
->sel
.family
);
2583 if (!ipsec
->dst
->is_anyaddr(ipsec
->dst
))
2585 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2586 ipsec
->dst
, -1, ipsec
->src
,
2590 { /* for shunt policies */
2591 iface
= xfrm2host(policy
->sel
.family
, &policy
->sel
.daddr
, 0);
2592 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2593 iface
, policy
->sel
.prefixlen_d
,
2594 route
->src_ip
, &route
->if_name
);
2595 iface
->destroy(iface
);
2597 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2598 memcpy(route
->dst_net
.ptr
, &policy
->sel
.daddr
, route
->dst_net
.len
);
2600 /* get the interface to install the route for, if we haven't one yet.
2601 * If we have a local address, use it. Otherwise (for shunt policies)
2602 * use the route's source address. */
2603 if (!route
->if_name
)
2606 if (iface
->is_anyaddr(iface
))
2608 iface
= route
->src_ip
;
2610 if (!charon
->kernel
->get_interface(charon
->kernel
, iface
,
2613 { /* don't require an interface for passthrough policies */
2614 route_entry_destroy(route
);
2620 route_entry_t
*old
= policy
->route
;
2621 if (route_entry_equals(old
, route
))
2623 route_entry_destroy(route
);
2626 /* uninstall previously installed route */
2627 if (charon
->kernel
->del_route(charon
->kernel
, old
->dst_net
,
2628 old
->prefixlen
, old
->gateway
,
2629 old
->src_ip
, old
->if_name
,
2630 old
->pass
) != SUCCESS
)
2632 DBG1(DBG_KNL
, "error uninstalling route installed with policy "
2633 "%R === %R %N", out
->src_ts
, out
->dst_ts
, policy_dir_names
,
2636 route_entry_destroy(old
);
2637 policy
->route
= NULL
;
2640 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s", out
->dst_ts
,
2641 route
->gateway
, route
->src_ip
, route
->if_name
);
2642 switch (charon
->kernel
->add_route(charon
->kernel
, route
->dst_net
,
2643 route
->prefixlen
, route
->gateway
,
2644 route
->src_ip
, route
->if_name
,
2648 DBG1(DBG_KNL
, "unable to install source route for %H",
2652 /* route exists, do not uninstall */
2653 route_entry_destroy(route
);
2656 /* cache the installed route */
2657 policy
->route
= route
;
2663 * Add or update a policy in the kernel.
2665 * Note: The mutex has to be locked when entering this function
2666 * and is unlocked here in any case.
2668 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
2669 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
2671 netlink_buf_t request
;
2672 policy_entry_t clone
;
2673 ipsec_sa_t
*ipsec
= mapping
->sa
;
2674 struct xfrm_userpolicy_info
*policy_info
;
2675 struct nlmsghdr
*hdr
;
2679 /* clone the policy so we are able to check it out again later */
2680 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
2682 memset(&request
, 0, sizeof(request
));
2684 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2685 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
2686 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2688 policy_info
= NLMSG_DATA(hdr
);
2689 policy_info
->sel
= policy
->sel
;
2690 policy_info
->dir
= policy
->direction
;
2692 /* calculate priority based on selector size, small size = high prio */
2693 policy_info
->priority
= mapping
->priority
;
2694 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
2695 : XFRM_POLICY_BLOCK
;
2696 policy_info
->share
= XFRM_SHARE_ANY
;
2698 /* policies don't expire */
2699 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2700 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2701 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2702 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2703 policy_info
->lft
.soft_add_expires_seconds
= 0;
2704 policy_info
->lft
.hard_add_expires_seconds
= 0;
2705 policy_info
->lft
.soft_use_expires_seconds
= 0;
2706 policy_info
->lft
.hard_use_expires_seconds
= 0;
2708 if (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.reqid
)
2710 struct xfrm_user_tmpl
*tmpl
;
2716 { IPPROTO_COMP
, htonl(ntohs(ipsec
->cfg
.ipcomp
.cpi
)),
2717 ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2718 { IPPROTO_ESP
, ipsec
->cfg
.esp
.spi
, ipsec
->cfg
.esp
.use
},
2719 { IPPROTO_AH
, ipsec
->cfg
.ah
.spi
, ipsec
->cfg
.ah
.use
},
2721 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2724 for (i
= 0; i
< countof(protos
); i
++)
2731 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_TMPL
,
2732 count
* sizeof(*tmpl
));
2735 policy_change_done(this, policy
);
2739 for (i
= 0; i
< countof(protos
); i
++)
2745 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2746 tmpl
->id
.proto
= protos
[i
].proto
;
2747 if (policy
->direction
== POLICY_OUT
)
2749 tmpl
->id
.spi
= protos
[i
].spi
;
2751 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2752 tmpl
->mode
= mode2kernel(proto_mode
);
2753 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2754 policy
->direction
!= POLICY_OUT
;
2755 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2757 if (proto_mode
== MODE_TUNNEL
|| proto_mode
== MODE_BEET
)
2758 { /* only for tunnel mode */
2759 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2760 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2765 /* use transport mode for other SAs */
2766 proto_mode
= MODE_TRANSPORT
;
2770 if (!add_mark(hdr
, sizeof(request
), ipsec
->mark
))
2772 policy_change_done(this, policy
);
2776 !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, ipsec
->if_id
))
2778 policy_change_done(this, policy
);
2781 this->mutex
->unlock(this->mutex
);
2783 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2784 if (status
== ALREADY_DONE
&& !update
)
2786 DBG1(DBG_KNL
, "policy already exists, try to update it");
2787 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
2788 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2791 this->mutex
->lock(this->mutex
);
2792 if (status
!= SUCCESS
)
2794 policy_change_done(this, policy
);
2797 /* install a route, if:
2798 * - this is an outbound policy (to just get one for each child)
2799 * - routing is not disabled via strongswan.conf
2800 * - the selector is not for a specific protocol/port
2801 * - no XFRM interface ID is configured
2802 * - we are in tunnel/BEET mode or install a bypass policy
2804 if (policy
->direction
== POLICY_OUT
&& this->install_routes
&&
2805 !policy
->sel
.proto
&& !policy
->sel
.dport
&& !policy
->sel
.sport
&&
2808 if (mapping
->type
== POLICY_PASS
||
2809 (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.mode
!= MODE_TRANSPORT
))
2811 install_route(this, policy
, mapping
, ipsec
);
2814 policy_change_done(this, policy
);
2818 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2819 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2820 kernel_ipsec_manage_policy_t
*data
)
2822 policy_entry_t
*policy
, *current
;
2823 policy_sa_t
*assigned_sa
, *current_sa
;
2824 enumerator_t
*enumerator
;
2825 bool found
= FALSE
, update
= TRUE
;
2826 char markstr
[32] = "";
2827 uint32_t cur_priority
= 0;
2830 /* create a policy */
2832 .sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
),
2833 .mark
= id
->mark
.value
& id
->mark
.mask
,
2835 .direction
= id
->dir
,
2836 .reqid
= data
->sa
->reqid
,
2838 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2840 /* find the policy, which matches EXACTLY */
2841 this->mutex
->lock(this->mutex
);
2842 current
= this->policies
->get(this->policies
, policy
);
2845 if (current
->reqid
&& data
->sa
->reqid
&&
2846 current
->reqid
!= data
->sa
->reqid
)
2848 DBG1(DBG_CFG
, "unable to install policy %R === %R %N%s for reqid "
2849 "%u, the same policy for reqid %u exists",
2850 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
,
2851 data
->sa
->reqid
, current
->reqid
);
2852 policy_entry_destroy(this, policy
);
2853 this->mutex
->unlock(this->mutex
);
2854 return INVALID_STATE
;
2856 /* use existing policy */
2857 DBG2(DBG_KNL
, "policy %R === %R %N%s already exists, increasing "
2858 "refcount", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
2860 policy_entry_destroy(this, policy
);
2865 while (policy
->working
)
2867 this->condvar
->wait(this->condvar
, this->mutex
);
2870 policy
->working
= TRUE
;
2873 { /* use the new one, if we have no such policy */
2874 policy
->used_by
= linked_list_create();
2875 this->policies
->put(this->policies
, policy
, policy
);
2878 /* cache the assigned IPsec SA */
2879 assigned_sa
= policy_sa_create(this, id
->dir
, data
->type
, data
->src
,
2880 data
->dst
, id
->src_ts
, id
->dst_ts
, id
->mark
,
2881 id
->if_id
, data
->sa
);
2882 assigned_sa
->auto_priority
= get_priority(policy
, data
->prio
, id
->interface
);
2883 assigned_sa
->priority
= this->get_priority ?
this->get_priority(id
, data
)
2884 : data
->manual_prio
;
2885 assigned_sa
->priority
= assigned_sa
->priority ?
: assigned_sa
->auto_priority
;
2887 /* insert the SA according to its priority */
2888 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2889 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2891 if (current_sa
->priority
> assigned_sa
->priority
)
2895 if (current_sa
->priority
== assigned_sa
->priority
)
2897 /* in case of equal manual prios order SAs by automatic priority */
2898 if (current_sa
->auto_priority
> assigned_sa
->auto_priority
)
2902 /* prefer SAs with a reqid over those without */
2903 if (current_sa
->auto_priority
== assigned_sa
->auto_priority
&&
2904 (!current_sa
->sa
->cfg
.reqid
|| assigned_sa
->sa
->cfg
.reqid
))
2911 cur_priority
= current_sa
->priority
;
2915 policy
->used_by
->insert_before(policy
->used_by
, enumerator
, assigned_sa
);
2916 enumerator
->destroy(enumerator
);
2918 use_count
= policy
->used_by
->get_count(policy
->used_by
);
2920 { /* we don't update the policy if the priority is lower than that of
2921 * the currently installed one */
2922 policy_change_done(this, policy
);
2923 DBG2(DBG_KNL
, "not updating policy %R === %R %N%s [priority %u, "
2924 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
2925 id
->dir
, markstr
, cur_priority
, use_count
);
2928 policy
->reqid
= assigned_sa
->sa
->cfg
.reqid
;
2930 if (this->policy_update
)
2935 DBG2(DBG_KNL
, "%s policy %R === %R %N%s [priority %u, refcount %d]",
2936 found ?
"updating" : "adding", id
->src_ts
, id
->dst_ts
,
2937 policy_dir_names
, id
->dir
, markstr
, assigned_sa
->priority
, use_count
);
2939 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2941 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N%s",
2942 found ?
"update" : "add", id
->src_ts
, id
->dst_ts
,
2943 policy_dir_names
, id
->dir
, markstr
);
2949 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2950 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2951 kernel_ipsec_query_policy_t
*data
, time_t *use_time
)
2953 netlink_buf_t request
;
2954 struct nlmsghdr
*out
= NULL
, *hdr
;
2955 struct xfrm_userpolicy_id
*policy_id
;
2956 struct xfrm_userpolicy_info
*policy
= NULL
;
2958 char markstr
[32] = "";
2960 memset(&request
, 0, sizeof(request
));
2961 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2963 DBG2(DBG_KNL
, "querying policy %R === %R %N%s", id
->src_ts
, id
->dst_ts
,
2964 policy_dir_names
, id
->dir
, markstr
);
2967 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2968 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2969 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2971 policy_id
= NLMSG_DATA(hdr
);
2972 policy_id
->sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
);
2973 policy_id
->dir
= id
->dir
;
2975 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2979 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2984 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2987 while (NLMSG_OK(hdr
, len
))
2989 switch (hdr
->nlmsg_type
)
2991 case XFRM_MSG_NEWPOLICY
:
2993 policy
= NLMSG_DATA(hdr
);
2998 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2999 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
3000 strerror(-err
->error
), -err
->error
);
3004 hdr
= NLMSG_NEXT(hdr
, len
);
3015 DBG2(DBG_KNL
, "unable to query policy %R === %R %N%s", id
->src_ts
,
3016 id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
3021 if (policy
->curlft
.use_time
)
3023 /* we need the monotonic time, but the kernel returns system time. */
3024 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
3035 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
3036 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
3037 kernel_ipsec_manage_policy_t
*data
)
3039 policy_entry_t
*current
, policy
;
3040 enumerator_t
*enumerator
;
3041 policy_sa_t
*mapping
;
3042 netlink_buf_t request
;
3043 struct nlmsghdr
*hdr
;
3044 struct xfrm_userpolicy_id
*policy_id
;
3045 bool is_installed
= TRUE
;
3046 uint32_t priority
, auto_priority
, cur_priority
;
3047 ipsec_sa_t assigned_sa
= {
3054 char markstr
[32] = "";
3056 status_t status
= SUCCESS
;
3058 format_mark(markstr
, sizeof(markstr
), id
->mark
);
3060 DBG2(DBG_KNL
, "deleting policy %R === %R %N%s", id
->src_ts
, id
->dst_ts
,
3061 policy_dir_names
, id
->dir
, markstr
);
3063 /* create a policy */
3064 memset(&policy
, 0, sizeof(policy_entry_t
));
3065 policy
.sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
);
3066 policy
.mark
= id
->mark
.value
& id
->mark
.mask
;
3067 policy
.if_id
= id
->if_id
;
3068 policy
.direction
= id
->dir
;
3070 /* find the policy */
3071 this->mutex
->lock(this->mutex
);
3072 current
= this->policies
->get(this->policies
, &policy
);
3075 DBG1(DBG_KNL
, "deleting policy %R === %R %N%s failed, not found",
3076 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
3077 this->mutex
->unlock(this->mutex
);
3081 while (current
->working
)
3083 this->condvar
->wait(this->condvar
, this->mutex
);
3085 current
->working
= TRUE
;
3088 /* remove mapping to SA by reqid and priority */
3089 auto_priority
= get_priority(current
, data
->prio
,id
->interface
);
3090 priority
= this->get_priority ?
this->get_priority(id
, data
)
3091 : data
->manual_prio
;
3092 priority
= priority ?
: auto_priority
;
3094 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
3095 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
3097 if (priority
== mapping
->priority
&&
3098 auto_priority
== mapping
->auto_priority
&&
3099 data
->type
== mapping
->type
&&
3100 ipsec_sa_equals(mapping
->sa
, &assigned_sa
))
3102 current
->used_by
->remove_at(current
->used_by
, enumerator
);
3103 policy_sa_destroy(mapping
, id
->dir
, this);
3108 cur_priority
= mapping
->priority
;
3109 is_installed
= FALSE
;
3112 enumerator
->destroy(enumerator
);
3114 use_count
= current
->used_by
->get_count(current
->used_by
);
3116 { /* policy is used by more SAs, keep in kernel */
3117 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
3119 { /* no need to update as the policy was not installed for this SA */
3120 policy_change_done(this, current
);
3121 DBG2(DBG_KNL
, "not updating policy %R === %R %N%s [priority %u, "
3122 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
3123 id
->dir
, markstr
, cur_priority
, use_count
);
3126 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
3127 current
->reqid
= mapping
->sa
->cfg
.reqid
;
3129 DBG2(DBG_KNL
, "updating policy %R === %R %N%s [priority %u, "
3130 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
3131 markstr
, mapping
->priority
, use_count
);
3133 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
3135 DBG1(DBG_KNL
, "unable to update policy %R === %R %N%s",
3136 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
3142 memset(&request
, 0, sizeof(request
));
3145 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3146 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
3147 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
3149 policy_id
= NLMSG_DATA(hdr
);
3150 policy_id
->sel
= current
->sel
;
3151 policy_id
->dir
= id
->dir
;
3153 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
3155 policy_change_done(this, current
);
3158 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
3160 policy_change_done(this, current
);
3166 route_entry_t
*route
= current
->route
;
3167 if (charon
->kernel
->del_route(charon
->kernel
, route
->dst_net
,
3168 route
->prefixlen
, route
->gateway
,
3169 route
->src_ip
, route
->if_name
,
3170 route
->pass
) != SUCCESS
)
3172 DBG1(DBG_KNL
, "error uninstalling route installed with policy "
3173 "%R === %R %N%s", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
3177 this->mutex
->unlock(this->mutex
);
3179 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
3181 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N%s", id
->src_ts
,
3182 id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
3186 this->mutex
->lock(this->mutex
);
3187 if (!current
->waiting
)
3188 { /* only if no other thread still needs the policy */
3189 this->policies
->remove(this->policies
, current
);
3190 policy_entry_destroy(this, current
);
3191 this->mutex
->unlock(this->mutex
);
3195 policy_change_done(this, current
);
3200 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
3201 private_kernel_netlink_ipsec_t
*this)
3203 netlink_buf_t request
;
3204 struct nlmsghdr
*hdr
;
3206 memset(&request
, 0, sizeof(request
));
3208 DBG2(DBG_KNL
, "flushing all policies from SPD");
3211 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3212 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
3213 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
3215 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
3216 * to main or sub policies (default is main) */
3218 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
3220 DBG1(DBG_KNL
, "unable to flush SPD entries");
3227 * Bypass socket using a per-socket policy
3229 static bool add_socket_bypass(private_kernel_netlink_ipsec_t
*this,