2 * Copyright (C) 2006-2023 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
9 * Copyright (C) secunet Security Networks AG
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * Copyright (C) 2018 Mellanox Technologies.
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <sys/utsname.h>
49 #include <linux/ipsec.h>
50 #include <linux/netlink.h>
51 #include <linux/rtnetlink.h>
52 #include <linux/xfrm.h>
53 #include <linux/udp.h>
54 #include <linux/ethtool.h>
55 #include <linux/sockios.h>
64 #include "kernel_netlink_ipsec.h"
65 #include "kernel_netlink_shared.h"
66 #include "kernel_netlink_xfrmi.h"
69 #include <utils/debug.h>
70 #include <threading/mutex.h>
71 #include <threading/condvar.h>
72 #include <collections/array.h>
73 #include <collections/hashtable.h>
74 #include <collections/linked_list.h>
76 /** Required for Linux 2.6.26 kernel and later */
77 #ifndef XFRM_STATE_AF_UNSPEC
78 #define XFRM_STATE_AF_UNSPEC 32
81 /** From linux/in.h */
82 #ifndef IP_XFRM_POLICY
83 #define IP_XFRM_POLICY 17
86 /** Missing on uclibc */
87 #ifndef IPV6_XFRM_POLICY
88 #define IPV6_XFRM_POLICY 34
89 #endif /*IPV6_XFRM_POLICY*/
91 /* from linux/udp.h */
96 #ifndef UDP_ENCAP_ESPINUDP
97 #define UDP_ENCAP_ESPINUDP 2
100 /* this is not defined on some platforms */
102 #define SOL_UDP IPPROTO_UDP
105 /** Base priority for installed policies */
106 #define PRIO_BASE 200000
109 * Map the limit for bytes and packets to XFRM_INF by default
111 #define XFRM_LIMIT(x) ((x) == 0 ? XFRM_INF : (x))
114 * Returns a pointer to the first rtattr following the nlmsghdr *nlh and the
115 * 'usual' netlink data x like 'struct xfrm_usersa_info'
117 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + \
118 NLMSG_ALIGN(sizeof(x))))
120 * Returns the total size of attached rta data
121 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
123 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
125 typedef struct kernel_algorithm_t kernel_algorithm_t
;
128 * Mapping of IKEv2 kernel identifier to linux crypto API names
130 struct kernel_algorithm_t
{
132 * Identifier specified in IKEv2
137 * Name of the algorithm in linux crypto API
142 ENUM(xfrm_msg_names
, XFRM_MSG_NEWSA
, __XFRM_MSG_MAX
,
146 "XFRM_MSG_NEWPOLICY",
147 "XFRM_MSG_DELPOLICY",
148 "XFRM_MSG_GETPOLICY",
152 "XFRM_MSG_UPDPOLICY",
154 "XFRM_MSG_POLEXPIRE",
156 "XFRM_MSG_FLUSHPOLICY",
161 "XFRM_MSG_NEWSADINFO",
162 "XFRM_MSG_GETSADINFO",
163 "XFRM_MSG_NEWSPDINFO",
164 "XFRM_MSG_GETSPDINFO",
166 "XFRM_MSG_SETDEFAULT",
167 "XFRM_MSG_GETDEFAULT",
171 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, __XFRMA_MAX
,
183 "XFRMA_REPLAY_THRESH",
184 "XFRMA_ETIMER_THRESH",
192 "XFRMA_ALG_AUTH_TRUNC",
195 "XFRMA_REPLAY_ESN_VAL",
196 "XFRMA_SA_EXTRA_FLAGS",
198 "XFRMA_ADDRESS_FILTER",
202 "XFRMA_SET_MARK_MASK",
204 "XFRMA_MTIMER_THRESH",
206 "XFRMA_NAT_KEEPALIVE_INTERVAL",
208 "XFRMA_IPTFS_DROP_TIME",
209 "XFRMA_IPTFS_REORDER_WINDOW",
210 "XFRMA_IPTFS_DONT_FRAG",
211 "XFRMA_IPTFS_INIT_DELAY",
212 "XFRMA_IPTFS_MAX_QSIZE",
213 "XFRMA_IPTFS_PKT_SIZE",
218 * Algorithms for encryption
220 static kernel_algorithm_t encryption_algs
[] = {
221 /* {ENCR_DES_IV64, "***" }, */
223 {ENCR_3DES
, "des3_ede" },
224 /* {ENCR_RC5, "***" }, */
225 /* {ENCR_IDEA, "***" }, */
226 {ENCR_CAST
, "cast5" },
227 {ENCR_BLOWFISH
, "blowfish" },
228 /* {ENCR_3IDEA, "***" }, */
229 /* {ENCR_DES_IV32, "***" }, */
230 {ENCR_NULL
, "cipher_null" },
231 {ENCR_AES_CBC
, "aes" },
232 {ENCR_AES_CTR
, "rfc3686(ctr(aes))" },
233 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
234 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
235 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
236 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
237 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
238 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
239 {ENCR_NULL_AUTH_AES_GMAC
, "rfc4543(gcm(aes))" },
240 {ENCR_CAMELLIA_CBC
, "cbc(camellia)" },
241 /* {ENCR_CAMELLIA_CTR, "***" }, */
242 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
243 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
244 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
245 {ENCR_SERPENT_CBC
, "serpent" },
246 {ENCR_TWOFISH_CBC
, "twofish" },
247 {ENCR_CHACHA20_POLY1305
, "rfc7539esp(chacha20,poly1305)"},
251 * Algorithms for integrity protection
253 static kernel_algorithm_t integrity_algs
[] = {
254 {AUTH_HMAC_MD5_96
, "md5" },
255 {AUTH_HMAC_MD5_128
, "hmac(md5)" },
256 {AUTH_HMAC_SHA1_96
, "sha1" },
257 {AUTH_HMAC_SHA1_160
, "hmac(sha1)" },
258 {AUTH_HMAC_SHA2_256_96
, "sha256" },
259 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
260 {AUTH_HMAC_SHA2_256_256
, "hmac(sha256)" },
261 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
262 {AUTH_HMAC_SHA2_384_384
, "hmac(sha384)" },
263 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
264 {AUTH_HMAC_SHA2_512_512
, "hmac(sha512)" },
265 /* {AUTH_DES_MAC, "***" }, */
266 /* {AUTH_KPDK_MD5, "***" }, */
267 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
268 {AUTH_AES_CMAC_96
, "cmac(aes)" },
272 * Algorithms for IPComp
274 static kernel_algorithm_t compression_algs
[] = {
275 /* {IPCOMP_OUI, "***" }, */
276 {IPCOMP_DEFLATE
, "deflate" },
277 {IPCOMP_LZS
, "lzs" },
278 {IPCOMP_LZJH
, "lzjh" },
282 * Look up a kernel algorithm name and its key size
284 static const char* lookup_algorithm(transform_type_t type
, int ikev2
)
286 kernel_algorithm_t
*list
;
292 case ENCRYPTION_ALGORITHM
:
293 list
= encryption_algs
;
294 count
= countof(encryption_algs
);
296 case INTEGRITY_ALGORITHM
:
297 list
= integrity_algs
;
298 count
= countof(integrity_algs
);
300 case COMPRESSION_ALGORITHM
:
301 list
= compression_algs
;
302 count
= countof(compression_algs
);
307 for (i
= 0; i
< count
; i
++)
309 if (list
[i
].ikev2
== ikev2
)
314 if (charon
->kernel
->lookup_algorithm(charon
->kernel
, ikev2
, type
, NULL
,
322 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
325 * Private variables and functions of kernel_netlink class.
327 struct private_kernel_netlink_ipsec_t
{
329 * Public part of the kernel_netlink_t object
331 kernel_netlink_ipsec_t
public;
334 * Mutex to lock access to installed policies
339 * Condvar to synchronize access to individual policies
344 * Hash table of installed policies (policy_entry_t)
346 hashtable_t
*policies
;
349 * Hash table of IPsec SAs using policies (ipsec_sa_t)
354 * Netlink xfrm socket (IPsec)
356 netlink_socket_t
*socket_xfrm
;
359 * XFRM interface manager
361 kernel_netlink_xfrmi_t
*xfrmi
;
364 * Netlink xfrm socket to receive acquire and expire events
366 netlink_event_socket_t
*socket_xfrm_events
;
369 * Whether the kernel reports the last use time on SAs
374 * Whether the kernel supports setting the SA direction
379 * Whether to install routes along policies
384 * Whether to install routes via XFRM interfaces
386 bool install_routes_xfrmi
;
389 * Whether to set protocol and ports on selector installed with transport
392 bool proto_port_transport
;
395 * Whether to always use UPDATE to install policies
400 * Whether to use port-based policies instead of socket policies for the
406 * Installed port-based IKE bypass policies, as bypass_t
408 * If they are potentially offloaded, the offload mutex has to be locked
414 * Interfaces that potentially support HW offloading, as offload_iface_t
416 hashtable_t
*offload_interfaces
;
419 * Mutex to safely access the interfaces and bypasses
421 mutex_t
*offload_mutex
;
424 * Netlink routing socket to receive link events
426 netlink_event_socket_t
*socket_link_events
;
429 * Custom priority calculation function
431 uint32_t (*get_priority
)(kernel_ipsec_policy_id_t
*id
,
432 kernel_ipsec_manage_policy_t
*data
);
435 typedef struct ipsec_sa_t ipsec_sa_t
;
438 * IPsec SA assigned to a policy.
441 /** Source address of this SA */
444 /** Destination address of this SA */
453 /** Optional HW offload */
454 hw_offload_t hw_offload
;
456 /** Description of this SA */
459 /** Reference count for this SA */
464 * Hash function for ipsec_sa_t objects
466 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
468 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
469 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
470 chunk_hash_inc(chunk_from_thing(sa
->mark
),
471 chunk_hash_inc(chunk_from_thing(sa
->if_id
),
472 chunk_hash_inc(chunk_from_thing(sa
->hw_offload
),
473 chunk_hash(chunk_from_thing(sa
->cfg
)))))));
477 * Equality function for ipsec_sa_t objects
479 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
481 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
482 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
483 sa
->mark
.value
== other_sa
->mark
.value
&&
484 sa
->mark
.mask
== other_sa
->mark
.mask
&&
485 sa
->if_id
== other_sa
->if_id
&&
486 sa
->hw_offload
== other_sa
->hw_offload
&&
487 ipsec_sa_cfg_equals(&sa
->cfg
, &other_sa
->cfg
);
491 * Allocate or reference an IPsec SA object
493 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
494 host_t
*src
, host_t
*dst
, mark_t mark
,
495 uint32_t if_id
, hw_offload_t hw_offload
,
498 ipsec_sa_t
*sa
, *found
;
504 .hw_offload
= hw_offload
,
507 found
= this->sas
->get(this->sas
, sa
);
510 sa
->src
= src
->clone(src
);
511 sa
->dst
= dst
->clone(dst
);
512 this->sas
->put(this->sas
, sa
, sa
);
519 ref_get(&sa
->refcount
);
524 * Release and destroy an IPsec SA object
526 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
529 if (ref_put(&sa
->refcount
))
531 this->sas
->remove(this->sas
, sa
);
538 typedef struct policy_sa_t policy_sa_t
;
539 typedef struct policy_sa_out_t policy_sa_out_t
;
542 * Mapping between a policy and an IPsec SA.
545 /** Priority assigned to the policy when installed with this SA */
548 /** Automatic priority assigned to the policy when installed with this SA */
549 uint32_t auto_priority
;
551 /** Type of the policy */
554 /** Whether to trigger per-CPU acquires for this policy */
562 * For outbound policies we also cache the traffic selectors in order to install
565 struct policy_sa_out_t
{
566 /** Generic interface */
569 /** Source traffic selector of this policy */
570 traffic_selector_t
*src_ts
;
572 /** Destination traffic selector of this policy */
573 traffic_selector_t
*dst_ts
;
577 * Create a policy_sa(_out)_t object
579 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
580 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
581 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
582 uint32_t if_id
, hw_offload_t hw_offload
, bool pcpu_acquires
,
587 if (dir
== POLICY_OUT
)
589 policy_sa_out_t
*out
;
591 .src_ts
= src_ts
->clone(src_ts
),
592 .dst_ts
= dst_ts
->clone(dst_ts
),
594 policy
= &out
->generic
;
598 INIT(policy
, .priority
= 0);
601 policy
->pcpu_acquires
= pcpu_acquires
;
602 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, if_id
, hw_offload
, cfg
);
607 * Destroy a policy_sa(_in)_t object
609 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t dir
,
610 private_kernel_netlink_ipsec_t
*this)
612 if (dir
== POLICY_OUT
)
614 policy_sa_out_t
*out
= (policy_sa_out_t
*)policy
;
615 out
->src_ts
->destroy(out
->src_ts
);
616 out
->dst_ts
->destroy(out
->dst_ts
);
618 ipsec_sa_destroy(this, policy
->sa
);
622 CALLBACK(policy_sa_destroy_cb
, void,
623 policy_sa_t
*policy
, va_list args
)
625 private_kernel_netlink_ipsec_t
*this;
628 VA_ARGS_VGET(args
, dir
, this);
629 policy_sa_destroy(policy
, dir
, this);
632 typedef struct policy_entry_t policy_entry_t
;
635 * Installed kernel policy.
637 struct policy_entry_t
{
639 /** Direction of this policy: in, out, forward */
642 /** Parameters of installed policy */
643 struct xfrm_selector sel
;
648 /** Optional interface ID */
651 /** Optional security label */
654 /** Associated route installed for this policy */
655 route_entry_t
*route
;
657 /** List of SAs this policy is used by, ordered by priority */
658 linked_list_t
*used_by
;
660 /** reqid for this policy */
663 /** Number of threads waiting to work on this policy */
666 /** TRUE if a thread is working on this policy */
671 * Destroy a policy_entry_t object
673 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
674 policy_entry_t
*policy
)
678 route_entry_destroy(policy
->route
);
682 policy
->used_by
->invoke_function(policy
->used_by
, policy_sa_destroy_cb
,
683 policy
->direction
, this);
684 policy
->used_by
->destroy(policy
->used_by
);
686 DESTROY_IF(policy
->label
);
691 * Hash function for policy_entry_t objects
693 static u_int
policy_hash(policy_entry_t
*key
)
695 chunk_t chunk
= chunk_from_thing(key
->sel
);
698 hash
= chunk_hash_inc(chunk
, chunk_hash_inc(chunk_from_thing(key
->mark
),
699 chunk_hash(chunk_from_thing(key
->if_id
))));
702 hash
= key
->label
->hash(key
->label
, hash
);
708 * Equality function for policy_entry_t objects
710 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
712 return memeq(&key
->sel
, &other_key
->sel
, sizeof(struct xfrm_selector
)) &&
713 key
->mark
== other_key
->mark
&&
714 key
->if_id
== other_key
->if_id
&&
715 key
->direction
== other_key
->direction
&&
716 sec_labels_equal(key
->label
, other_key
->label
);
720 * Determine number of set bits in 16 bit port mask
722 static inline uint32_t port_mask_bits(uint16_t port_mask
)
725 uint16_t bit_mask
= 0x8000;
727 port_mask
= ntohs(port_mask
);
729 for (bits
= 0; bits
< 16; bits
++)
731 if (!(port_mask
& bit_mask
))
741 * Calculate the priority of a policy
743 * bits 0-0: separate trap and regular policies (0..1) 1 bit
744 * bits 1-1: restriction to network interface (0..1) 1 bit
745 * bits 2-7: src + dst port mask bits (2 * 0..16) 6 bits
746 * bits 8-8: restriction to protocol (0..1) 1 bit
747 * bits 9-17: src + dst network mask bits (2 * 0..128) 9 bits
750 * smallest value: 000000000 0 000000 0 0: 0, lowest priority = 200'000
751 * largest value : 100000000 1 100000 1 1: 131'459, highst priority = 68'541
753 static uint32_t get_priority(policy_entry_t
*policy
, policy_priority_t prio
,
756 uint32_t priority
= PRIO_BASE
, sport_mask_bits
, dport_mask_bits
;
760 case POLICY_PRIORITY_FALLBACK
:
761 priority
+= PRIO_BASE
;
762 /* fall-through to next case */
763 case POLICY_PRIORITY_ROUTED
:
764 case POLICY_PRIORITY_DEFAULT
:
765 priority
+= PRIO_BASE
;
766 /* fall-through to next case */
767 case POLICY_PRIORITY_PASS
:
770 sport_mask_bits
= port_mask_bits(policy
->sel
.sport_mask
);
771 dport_mask_bits
= port_mask_bits(policy
->sel
.dport_mask
);
773 /* calculate priority */
774 priority
-= (policy
->sel
.prefixlen_s
+ policy
->sel
.prefixlen_d
) * 512;
775 priority
-= policy
->sel
.proto
? 256 : 0;
776 priority
-= (sport_mask_bits
+ dport_mask_bits
) * 4;
777 priority
-= (interface
!= NULL
) * 2;
778 priority
-= (prio
!= POLICY_PRIORITY_ROUTED
);
784 * Convert the general ipsec mode to the one defined in xfrm.h
786 static uint8_t mode2kernel(ipsec_mode_t mode
)
791 return XFRM_MODE_TRANSPORT
;
793 return XFRM_MODE_TUNNEL
;
795 return XFRM_MODE_BEET
;
797 return XFRM_MODE_IPTFS
;
804 * Convert a host_t to a struct xfrm_address
806 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
808 chunk_t chunk
= host
->get_address(host
);
809 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
813 * Convert a struct xfrm_address to a host_t
815 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, uint16_t port
)
822 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
825 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
830 return host_create_from_chunk(family
, chunk
, ntohs(port
));
834 * Convert a traffic selector address range to subnet and its mask.
836 static void ts2subnet(traffic_selector_t
* ts
,
837 xfrm_address_t
*net
, uint8_t *mask
)
842 ts
->to_subnet(ts
, &net_host
, mask
);
843 net_chunk
= net_host
->get_address(net_host
);
844 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
845 net_host
->destroy(net_host
);
849 * Convert a traffic selector port range to port/portmask
851 static void ts2ports(traffic_selector_t
* ts
,
852 uint16_t *port
, uint16_t *mask
)
854 uint16_t from
, to
, bitmask
;
857 from
= ts
->get_from_port(ts
);
858 to
= ts
->get_to_port(ts
);
860 /* Quick check for a single port */
868 /* Compute the port mask for port ranges */
871 for (bit
= 15; bit
>= 0; bit
--)
875 if ((bitmask
& from
) != (bitmask
& to
))
877 *port
= htons(from
& *mask
);
878 *mask
= htons(*mask
);
888 * Convert a pair of traffic_selectors to an xfrm_selector
890 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
891 traffic_selector_t
*dst
,
894 struct xfrm_selector sel
;
897 memset(&sel
, 0, sizeof(sel
));
898 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
899 /* src or dest proto may be "any" (0), use more restrictive one */
900 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
901 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
902 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
903 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
904 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
905 if ((sel
.proto
== IPPROTO_ICMP
|| sel
.proto
== IPPROTO_ICMPV6
) &&
906 (sel
.dport
|| sel
.sport
))
908 /* the kernel expects the ICMP type and code in the source and
909 * destination port fields, respectively. */
910 port
= ntohs(max(sel
.dport
, sel
.sport
));
911 sel
.sport
= htons(traffic_selector_icmp_type(port
));
912 sel
.sport_mask
= sel
.sport
? ~0 : 0;
913 sel
.dport
= htons(traffic_selector_icmp_code(port
));
914 sel
.dport_mask
= sel
.dport
? ~0 : 0;
916 sel
.ifindex
= interface
? if_nametoindex(interface
) : 0;
923 * Convert an xfrm_selector to a src|dst traffic_selector
925 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
934 addr
= (u_char
*)&sel
->saddr
;
935 prefixlen
= sel
->prefixlen_s
;
938 port
= ntohs(sel
->sport
);
943 addr
= (u_char
*)&sel
->daddr
;
944 prefixlen
= sel
->prefixlen_d
;
947 port
= ntohs(sel
->dport
);
950 if (sel
->proto
== IPPROTO_ICMP
|| sel
->proto
== IPPROTO_ICMPV6
)
951 { /* convert ICMP[v6] message type and code as supplied by the kernel in
952 * source and destination ports (both in network order) */
953 port
= (sel
->sport
>> 8) | (sel
->dport
& 0xff00);
956 /* The Linux 2.6 kernel does not set the selector's family field,
957 * so as a kludge we additionally test the prefix length.
959 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
961 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
963 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
965 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
970 return traffic_selector_create_from_subnet(host
, prefixlen
,
971 sel
->proto
, port
, port
?: 65535);
977 * Process a XFRM_MSG_ACQUIRE from kernel
979 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
980 struct nlmsghdr
*hdr
)
982 struct xfrm_user_acquire
*acquire
;
985 kernel_acquire_data_t data
= {
988 chunk_t label
= chunk_empty
;
992 acquire
= NLMSG_DATA(hdr
);
993 proto
= acquire
->id
.proto
;
994 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
995 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
997 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
999 while (RTA_OK(rta
, rtasize
))
1001 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
1003 if (rta
->rta_type
== XFRMA_TMPL
)
1005 struct xfrm_user_tmpl
* tmpl
= RTA_DATA(rta
);
1006 reqid
= tmpl
->reqid
;
1008 if (rta
->rta_type
== XFRMA_SA_PCPU
)
1010 data
.cpu
= *(uint32_t*)RTA_DATA(rta
);
1013 if (rta
->rta_type
== XFRMA_SEC_CTX
)
1015 struct xfrm_user_sec_ctx
*ctx
= RTA_DATA(rta
);
1017 if (ctx
->ctx_doi
== XFRM_SC_DOI_LSM
&&
1018 ctx
->ctx_alg
== XFRM_SC_ALG_SELINUX
)
1020 label
= chunk_create((void*)(ctx
+ 1), ctx
->ctx_len
);
1024 rta
= RTA_NEXT(rta
, rtasize
);
1033 /* acquire for AH/ESP only, not for IPCOMP */
1036 data
.src
= selector2ts(&acquire
->sel
, TRUE
);
1037 data
.dst
= selector2ts(&acquire
->sel
, FALSE
);
1038 data
.label
= label
.len
? sec_label_from_encoding(label
) : NULL
;
1039 data
.seq
= acquire
->seq
;
1041 charon
->kernel
->acquire(charon
->kernel
, reqid
, &data
);
1043 DESTROY_IF(data
.src
);
1044 DESTROY_IF(data
.dst
);
1045 DESTROY_IF(data
.label
);
1049 * Process a XFRM_MSG_EXPIRE from kernel
1051 static void process_expire(private_kernel_netlink_ipsec_t
*this,
1052 struct nlmsghdr
*hdr
)
1054 struct xfrm_user_expire
*expire
;
1059 expire
= NLMSG_DATA(hdr
);
1060 protocol
= expire
->state
.id
.proto
;
1061 spi
= expire
->state
.id
.spi
;
1063 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
1065 if (protocol
== IPPROTO_ESP
|| protocol
== IPPROTO_AH
)
1067 dst
= xfrm2host(expire
->state
.family
, &expire
->state
.id
.daddr
, 0);
1070 charon
->kernel
->expire(charon
->kernel
, protocol
, spi
, dst
,
1078 * Process a XFRM_MSG_MIGRATE from kernel
1080 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
1081 struct nlmsghdr
*hdr
)
1083 struct xfrm_userpolicy_id
*policy_id
;
1086 traffic_selector_t
*src_ts
, *dst_ts
;
1087 host_t
*local
= NULL
, *remote
= NULL
;
1088 host_t
*old_src
= NULL
, *old_dst
= NULL
;
1089 host_t
*new_src
= NULL
, *new_dst
= NULL
;
1093 policy_id
= NLMSG_DATA(hdr
);
1094 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
1095 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
1097 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
1099 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
1100 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
1101 dir
= (policy_dir_t
)policy_id
->dir
;
1103 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
1105 while (RTA_OK(rta
, rtasize
))
1107 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
1108 if (rta
->rta_type
== XFRMA_KMADDRESS
)
1110 struct xfrm_user_kmaddress
*kmaddress
;
1112 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
1113 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
1114 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
1115 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
1117 else if (rta
->rta_type
== XFRMA_MIGRATE
)
1119 struct xfrm_user_migrate
*migrate
;
1121 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
1122 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
1123 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
1124 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
1125 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
1126 reqid
= migrate
->reqid
;
1127 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
1128 old_src
, old_dst
, new_src
, new_dst
, reqid
);
1129 DESTROY_IF(old_src
);
1130 DESTROY_IF(old_dst
);
1131 DESTROY_IF(new_src
);
1132 DESTROY_IF(new_dst
);
1134 rta
= RTA_NEXT(rta
, rtasize
);
1137 if (src_ts
&& dst_ts
&& local
&& remote
)
1139 charon
->kernel
->migrate(charon
->kernel
, reqid
, src_ts
, dst_ts
, dir
,
1152 * Process a XFRM_MSG_MAPPING from kernel
1154 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
1155 struct nlmsghdr
*hdr
)
1157 struct xfrm_user_mapping
*mapping
;
1160 mapping
= NLMSG_DATA(hdr
);
1161 spi
= mapping
->id
.spi
;
1163 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
1165 if (mapping
->id
.proto
== IPPROTO_ESP
)
1169 dst
= xfrm2host(mapping
->id
.family
, &mapping
->id
.daddr
, 0);
1172 if (!mapping
->old_sport
)
1174 /* ignore mappings for per-CPU SAs with 0 source port */
1175 DBG1(DBG_KNL
, "ignore NAT mapping change for per-resource "
1176 "CHILD_SA %N/0x%08x/%H", protocol_id_names
, PROTO_ESP
,
1181 new = xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
1182 mapping
->new_sport
);
1185 charon
->kernel
->mapping(charon
->kernel
, IPPROTO_ESP
, spi
, dst
,
1195 CALLBACK(receive_events
, void,
1196 private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
1198 switch (hdr
->nlmsg_type
)
1200 case XFRM_MSG_ACQUIRE
:
1201 process_acquire(this, hdr
);
1203 case XFRM_MSG_EXPIRE
:
1204 process_expire(this, hdr
);
1206 case XFRM_MSG_MIGRATE
:
1207 process_migrate(this, hdr
);
1209 case XFRM_MSG_MAPPING
:
1210 process_mapping(this, hdr
);
1213 DBG1(DBG_KNL
, "received unknown event from XFRM event "
1214 "socket: %d", hdr
->nlmsg_type
);
1219 METHOD(kernel_ipsec_t
, get_features
, kernel_feature_t
,
1220 private_kernel_netlink_ipsec_t
*this)
1222 return KERNEL_ESP_V3_TFC
| KERNEL_POLICY_SPI
| KERNEL_ACQUIRE_SEQ
|
1223 (this->sa_lastused
? KERNEL_SA_USE_TIME
: 0);
1227 * Format the mark for debug messages
1229 static void format_mark(char *buf
, int buflen
, mark_t mark
)
1231 if (mark
.value
| mark
.mask
)
1233 snprintf(buf
, buflen
, " (mark %u/0x%08x)", mark
.value
, mark
.mask
);
1238 * Add a XFRM mark to message if required
1240 static bool add_mark(struct nlmsghdr
*hdr
, int buflen
, mark_t mark
)
1242 if (mark
.value
| mark
.mask
)
1244 struct xfrm_mark
*xmrk
;
1246 xmrk
= netlink_reserve(hdr
, buflen
, XFRMA_MARK
, sizeof(*xmrk
));
1251 xmrk
->v
= mark
.value
;
1252 xmrk
->m
= mark
.mask
;
1258 * Format the security label for debug messages
1260 static void format_label(char *buf
, int buflen
, sec_label_t
*label
)
1264 snprintf(buf
, buflen
, " (ctx %s)", label
->get_string(label
));
1269 * Add a security label to message if required
1271 static bool add_label(struct nlmsghdr
*hdr
, int buflen
, sec_label_t
*label
)
1276 struct xfrm_user_sec_ctx
*ctx
;
1277 chunk_t enc
= label
->get_encoding(label
);
1278 int len
= sizeof(*ctx
) + enc
.len
;
1280 ctx
= netlink_reserve(hdr
, buflen
, XFRMA_SEC_CTX
, len
);
1285 /* this attribute for some reason duplicates the generic header */
1286 ctx
->exttype
= XFRMA_SEC_CTX
;
1289 ctx
->ctx_doi
= XFRM_SC_DOI_LSM
;
1290 ctx
->ctx_alg
= XFRM_SC_ALG_SELINUX
;
1291 ctx
->ctx_len
= enc
.len
;
1292 memcpy((void*)(ctx
+ 1), enc
.ptr
, enc
.len
);
1299 * Add a uint32 attribute to message
1301 static bool add_uint32(struct nlmsghdr
*hdr
, int buflen
,
1302 enum xfrm_attr_type_t type
, uint32_t value
)
1306 xvalue
= netlink_reserve(hdr
, buflen
, type
, sizeof(*xvalue
));
1316 * Add a uint16 attribute to message
1318 static bool add_uint16(struct nlmsghdr
*hdr
, int buflen
,
1319 enum xfrm_attr_type_t type
, uint16_t value
)
1323 xvalue
= netlink_reserve(hdr
, buflen
, type
, sizeof(*xvalue
));
1333 * Add a uint8 attribute to message
1335 static bool add_uint8(struct nlmsghdr
*hdr
, int buflen
,
1336 enum xfrm_attr_type_t type
, uint8_t value
)
1340 xvalue
= netlink_reserve(hdr
, buflen
, type
, sizeof(*xvalue
));
1350 * Get an SPI for a specific protocol from the kernel.
1352 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
1353 host_t
*src
, host_t
*dst
, uint8_t proto
, uint32_t min
, uint32_t max
,
1356 netlink_buf_t request
;
1357 struct nlmsghdr
*hdr
, *out
;
1358 struct xfrm_userspi_info
*userspi
;
1359 uint32_t received_spi
= 0;
1362 memset(&request
, 0, sizeof(request
));
1365 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1366 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1367 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1369 userspi
= NLMSG_DATA(hdr
);
1370 host2xfrm(src
, &userspi
->info
.saddr
);
1371 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1372 userspi
->info
.id
.proto
= proto
;
1373 userspi
->info
.mode
= XFRM_MODE_TUNNEL
;
1374 userspi
->info
.family
= src
->get_family(src
);
1379 !add_uint8(hdr
, sizeof(request
), XFRMA_SA_DIR
, XFRM_SA_DIR_IN
))
1384 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1387 while (NLMSG_OK(hdr
, len
))
1389 switch (hdr
->nlmsg_type
)
1391 case XFRM_MSG_NEWSA
:
1393 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1394 received_spi
= usersa
->id
.spi
;
1399 netlink_log_error(hdr
, "allocating SPI failed");
1403 hdr
= NLMSG_NEXT(hdr
, len
);
1413 if (received_spi
== 0)
1418 *spi
= received_spi
;
1422 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1423 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1424 uint8_t protocol
, uint32_t *spi
)
1426 uint32_t spi_min
, spi_max
;
1428 spi_min
= lib
->settings
->get_int(lib
->settings
, "%s.spi_min",
1429 KERNEL_SPI_MIN
, lib
->ns
);
1430 spi_max
= lib
->settings
->get_int(lib
->settings
, "%s.spi_max",
1431 KERNEL_SPI_MAX
, lib
->ns
);
1433 if (get_spi_internal(this, src
, dst
, protocol
, min(spi_min
, spi_max
),
1434 max(spi_min
, spi_max
), spi
) != SUCCESS
)
1436 DBG1(DBG_KNL
, "unable to get SPI");
1440 DBG2(DBG_KNL
, "got SPI %.8x", ntohl(*spi
));
1444 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1445 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1448 uint32_t received_spi
= 0;
1450 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1451 0x100, 0xEFFF, &received_spi
) != SUCCESS
)
1453 DBG1(DBG_KNL
, "unable to get CPI");
1457 *cpi
= htons((uint16_t)ntohl(received_spi
));
1459 DBG2(DBG_KNL
, "got CPI %.4x", ntohs(*cpi
));
1463 /* ETHTOOL_GSSET_INFO is available since 2.6.34 and ETH_SS_FEATURES (enum) and
1464 * ETHTOOL_GFEATURES since 2.6.39, so check for the latter */
1465 #ifdef ETHTOOL_GFEATURES
1468 * Global metadata used for IPsec HW offload
1471 /** determined HW offload support */
1473 /** bit in feature set */
1475 /** total number of device feature blocks */
1477 } netlink_hw_offload
;
1480 * Check if kernel supports HW offload and determine feature flag
1482 static bool netlink_find_offload_feature(const char *ifname
)
1484 struct ethtool_sset_info
*sset_info
;
1485 struct ethtool_gstrings
*cmd
= NULL
;
1486 struct ifreq ifr
= { 0 };
1487 uint32_t sset_len
, i
;
1489 int err
, query_socket
;
1491 query_socket
= socket(AF_NETLINK
, SOCK_DGRAM
, NETLINK_XFRM
);
1492 if (query_socket
< 0)
1497 /* determine number of device features */
1498 INIT_EXTRA(sset_info
, sizeof(uint32_t),
1499 .cmd
= ETHTOOL_GSSET_INFO
,
1500 .sset_mask
= 1ULL << ETH_SS_FEATURES
,
1502 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
-1);
1503 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1504 ifr
.ifr_data
= (void*)sset_info
;
1506 err
= ioctl(query_socket
, SIOCETHTOOL
, &ifr
);
1507 if (err
|| sset_info
->sset_mask
!= 1ULL << ETH_SS_FEATURES
)
1511 sset_len
= sset_info
->data
[0];
1513 /* retrieve names of device features */
1514 INIT_EXTRA(cmd
, ETH_GSTRING_LEN
* sset_len
,
1515 .cmd
= ETHTOOL_GSTRINGS
,
1516 .string_set
= ETH_SS_FEATURES
,
1518 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
-1);
1519 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1520 ifr
.ifr_data
= (void*)cmd
;
1522 err
= ioctl(query_socket
, SIOCETHTOOL
, &ifr
);
1528 /* look for the ESP_HW feature bit */
1529 str
= (char*)cmd
->data
;
1530 for (i
= 0; i
< cmd
->len
; i
++)
1532 if (strneq(str
, "esp-hw-offload", ETH_GSTRING_LEN
))
1534 netlink_hw_offload
.supported
= TRUE
;
1535 netlink_hw_offload
.bit
= i
;
1536 netlink_hw_offload
.total_blocks
= (sset_len
+ 31) / 32;
1539 str
+= ETH_GSTRING_LEN
;
1545 close(query_socket
);
1546 return netlink_hw_offload
.supported
;
1550 * Check if interface supports HW offload
1552 static bool netlink_detect_offload(const char *ifname
)
1554 struct ethtool_gfeatures
*cmd
;
1555 uint32_t feature_bit
;
1556 struct ifreq ifr
= { 0 };
1561 if (!netlink_hw_offload
.supported
)
1563 DBG1(DBG_KNL
, "HW offload is not supported by kernel");
1567 query_socket
= socket(AF_NETLINK
, SOCK_DGRAM
, NETLINK_XFRM
);
1568 if (query_socket
< 0)
1573 /* feature is supported by kernel, query device features */
1574 INIT_EXTRA(cmd
, sizeof(cmd
->features
[0]) * netlink_hw_offload
.total_blocks
,
1575 .cmd
= ETHTOOL_GFEATURES
,
1576 .size
= netlink_hw_offload
.total_blocks
,
1578 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
-1);
1579 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1580 ifr
.ifr_data
= (void*)cmd
;
1582 if (!ioctl(query_socket
, SIOCETHTOOL
, &ifr
))
1584 block
= netlink_hw_offload
.bit
/ 32;
1585 feature_bit
= 1U << (netlink_hw_offload
.bit
% 32);
1586 if (cmd
->features
[block
].active
& feature_bit
)
1592 close(query_socket
);
1598 static bool netlink_find_offload_feature(const char *ifname
)
1603 static bool netlink_detect_offload(const char *ifname
)
1611 * Add a HW offload attribute to the given message, return it if it was added.
1613 * There are 4 HW offload configuration values:
1614 * 1. HW_OFFLOAD_NO : Do not configure HW offload.
1615 * 2. HW_OFFLOAD_CRYPTO : Configure crypto HW offload.
1616 * Fail SA addition if crypto offload is not supported.
1617 * 3. HW_OFFLOAD_PACKET : Configure packet HW offload.
1618 * Fail SA addition if packet offload is not supported.
1619 * 4. HW_OFFLOAD_AUTO : Configure packet HW offload if supported by the kernel
1620 * and device. If not, configure crypto HW offload if
1621 * supported by the kernel and device.
1622 * Do not fail SA addition if offload is not supported.
1624 static bool add_hw_offload(struct nlmsghdr
*hdr
, int buflen
, host_t
*local
,
1625 char *interface
, hw_offload_t hw_offload
,
1626 struct xfrm_user_offload
**offload
)
1631 /* do IPsec configuration without offload */
1632 if (hw_offload
== HW_OFFLOAD_NO
)
1637 /* unless offloading is forced, we return TRUE even if we fail */
1638 ret
= (hw_offload
== HW_OFFLOAD_AUTO
);
1640 if (!local
|| local
->is_anyaddr(local
) ||
1641 !charon
->kernel
->get_interface(charon
->kernel
, local
, &ifname
))
1643 if (!interface
|| !interface
[0])
1647 ifname
= strdup(interface
);
1650 /* check if interface supports hw_offload */
1651 if (!netlink_detect_offload(ifname
))
1653 DBG1(DBG_KNL
, "HW offload is not supported by device %s", ifname
);
1657 /* activate HW offload */
1658 *offload
= netlink_reserve(hdr
, buflen
,
1659 XFRMA_OFFLOAD_DEV
, sizeof(**offload
));
1664 (*offload
)->ifindex
= if_nametoindex(ifname
);
1666 if (hw_offload
== HW_OFFLOAD_PACKET
||
1667 hw_offload
== HW_OFFLOAD_AUTO
)
1669 (*offload
)->flags
|= XFRM_OFFLOAD_PACKET
;
1680 * Add a HW offload attribute to the given SA-related message.
1682 static bool add_hw_offload_sa(struct nlmsghdr
*hdr
, int buflen
,
1683 kernel_ipsec_sa_id_t
*id
,
1684 kernel_ipsec_add_sa_t
*data
,
1685 struct xfrm_user_offload
**offload
)
1687 host_t
*local
= data
->inbound
? id
->dst
: id
->src
;
1689 if (!add_hw_offload(hdr
, buflen
, local
, NULL
, data
->hw_offload
, offload
))
1695 (*offload
)->flags
|= data
->inbound
? XFRM_OFFLOAD_INBOUND
: 0;
1701 * Add a HW offload attribute to the given policy-related message.
1703 static bool add_hw_offload_policy(struct nlmsghdr
*hdr
, int buflen
,
1704 policy_entry_t
*policy
,
1705 policy_sa_t
*mapping
,
1706 struct xfrm_user_offload
**offload
)
1708 ipsec_sa_t
*ipsec
= mapping
->sa
;
1709 host_t
*local
= ipsec
->src
;
1710 char ifname
[IFNAMSIZ
] = "";
1712 /* only packet offloading is supported for policies, which we try to use
1713 * in automatic mode */
1714 if (ipsec
->hw_offload
!= HW_OFFLOAD_PACKET
&&
1715 ipsec
->hw_offload
!= HW_OFFLOAD_AUTO
)
1720 switch (policy
->direction
)
1723 /* FWD policies are not offloaded, they are enforced by the kernel */
1729 if (policy
->sel
.ifindex
)
1731 if_indextoname(policy
->sel
.ifindex
, ifname
);
1733 return add_hw_offload(hdr
, buflen
, local
, ifname
, ipsec
->hw_offload
, offload
);
1736 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1737 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1738 kernel_ipsec_add_sa_t
*data
)
1740 netlink_buf_t request
;
1741 const char *alg_name
;
1742 char markstr
[32] = "";
1743 struct nlmsghdr
*hdr
;
1744 struct xfrm_usersa_info
*sa
;
1745 struct xfrm_user_offload
*offload
= NULL
;
1746 uint16_t icv_size
= 64, ipcomp
= data
->ipcomp
;
1747 ipsec_mode_t mode
= data
->mode
, original_mode
= data
->mode
;
1748 traffic_selector_t
*first_src_ts
, *first_dst_ts
;
1749 status_t status
= FAILED
;
1751 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1752 * we are in the recursive call below */
1753 if (ipcomp
!= IPCOMP_NONE
&& data
->cpi
!= 0)
1755 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1756 kernel_ipsec_sa_id_t ipcomp_id
= {
1759 .spi
= htonl(ntohs(data
->cpi
)),
1760 .proto
= IPPROTO_COMP
,
1764 kernel_ipsec_add_sa_t ipcomp_sa
= {
1765 .reqid
= data
->reqid
,
1767 .src_ts
= data
->src_ts
,
1768 .dst_ts
= data
->dst_ts
,
1770 .enc_alg
= ENCR_UNDEFINED
,
1771 .int_alg
= AUTH_UNDEFINED
,
1773 .ipcomp
= data
->ipcomp
,
1775 .initiator
= data
->initiator
,
1776 .inbound
= data
->inbound
,
1777 .update
= data
->update
,
1779 add_sa(this, &ipcomp_id
, &ipcomp_sa
);
1780 ipcomp
= IPCOMP_NONE
;
1781 /* use transport mode ESP SA, IPComp uses tunnel mode */
1782 mode
= MODE_TRANSPORT
;
1785 memset(&request
, 0, sizeof(request
));
1786 format_mark(markstr
, sizeof(markstr
), id
->mark
);
1788 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}%s",
1789 ntohl(id
->spi
), data
->reqid
, markstr
);
1792 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1793 hdr
->nlmsg_type
= data
->update
? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1794 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1796 sa
= NLMSG_DATA(hdr
);
1797 host2xfrm(id
->src
, &sa
->saddr
);
1798 host2xfrm(id
->dst
, &sa
->id
.daddr
);
1799 sa
->id
.spi
= id
->spi
;
1800 sa
->id
.proto
= id
->proto
;
1801 sa
->family
= id
->src
->get_family(id
->src
);
1802 sa
->mode
= mode2kernel(mode
);
1803 sa
->seq
= data
->seq
;
1805 if (!data
->copy_ecn
)
1807 sa
->flags
|= XFRM_STATE_NOECN
;
1812 switch (data
->copy_dscp
)
1815 case DSCP_COPY_IN_ONLY
:
1816 sa
->flags
|= XFRM_STATE_DECAP_DSCP
;
1826 sa
->flags
|= XFRM_STATE_NOPMTUDISC
;
1828 switch (data
->copy_dscp
)
1830 case DSCP_COPY_IN_ONLY
:
1833 /* currently the only extra flag */
1834 if (!add_uint32(hdr
, sizeof(request
), XFRMA_SA_EXTRA_FLAGS
,
1835 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
))
1850 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1853 case MODE_TRANSPORT
:
1854 if (original_mode
== MODE_TUNNEL
)
1855 { /* don't install selectors for switched SAs. because only one
1856 * selector can be installed other traffic would get dropped */
1859 if (data
->src_ts
->get_first(data
->src_ts
,
1860 (void**)&first_src_ts
) == SUCCESS
&&
1861 data
->dst_ts
->get_first(data
->dst_ts
,
1862 (void**)&first_dst_ts
) == SUCCESS
)
1864 sa
->sel
= ts2selector(first_src_ts
, first_dst_ts
,
1866 if (!this->proto_port_transport
)
1868 /* don't install proto/port on SA. This would break
1869 * potential secondary SAs for the same address using a
1870 * different prot/port. */
1872 sa
->sel
.dport
= sa
->sel
.dport_mask
= 0;
1873 sa
->sel
.sport
= sa
->sel
.sport_mask
= 0;
1880 if (id
->proto
== IPPROTO_AH
&& sa
->family
== AF_INET
)
1881 { /* use alignment to 4 bytes for IPv4 instead of the incorrect 8 byte
1882 * alignment that's used by default but is only valid for IPv6 */
1883 sa
->flags
|= XFRM_STATE_ALIGN4
;
1886 sa
->reqid
= data
->reqid
;
1887 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(data
->lifetime
->bytes
.rekey
);
1888 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(data
->lifetime
->bytes
.life
);
1889 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(data
->lifetime
->packets
.rekey
);
1890 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(data
->lifetime
->packets
.life
);
1891 /* we use lifetimes since added, not since used */
1892 sa
->lft
.soft_add_expires_seconds
= data
->lifetime
->time
.rekey
;
1893 sa
->lft
.hard_add_expires_seconds
= data
->lifetime
->time
.life
;
1894 sa
->lft
.soft_use_expires_seconds
= 0;
1895 sa
->lft
.hard_use_expires_seconds
= 0;
1897 switch (data
->enc_alg
)
1899 case ENCR_UNDEFINED
:
1902 case ENCR_AES_CCM_ICV16
:
1903 case ENCR_AES_GCM_ICV16
:
1904 case ENCR_NULL_AUTH_AES_GMAC
:
1905 case ENCR_CAMELLIA_CCM_ICV16
:
1906 case ENCR_CHACHA20_POLY1305
:
1909 case ENCR_AES_CCM_ICV12
:
1910 case ENCR_AES_GCM_ICV12
:
1911 case ENCR_CAMELLIA_CCM_ICV12
:
1914 case ENCR_AES_CCM_ICV8
:
1915 case ENCR_AES_GCM_ICV8
:
1916 case ENCR_CAMELLIA_CCM_ICV8
:
1918 struct xfrm_algo_aead
*algo
;
1920 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, data
->enc_alg
);
1921 if (alg_name
== NULL
)
1923 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1924 encryption_algorithm_names
, data
->enc_alg
);
1927 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1928 encryption_algorithm_names
, data
->enc_alg
,
1929 data
->enc_key
.len
* 8);
1931 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AEAD
,
1932 sizeof(*algo
) + data
->enc_key
.len
);
1937 algo
->alg_key_len
= data
->enc_key
.len
* 8;
1938 algo
->alg_icv_len
= icv_size
;
1939 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
)-1);
1940 algo
->alg_name
[sizeof(algo
->alg_name
)-1] = '\0';
1941 memcpy(algo
->alg_key
, data
->enc_key
.ptr
, data
->enc_key
.len
);
1946 struct xfrm_algo
*algo
;
1948 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, data
->enc_alg
);
1949 if (alg_name
== NULL
)
1951 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1952 encryption_algorithm_names
, data
->enc_alg
);
1955 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1956 encryption_algorithm_names
, data
->enc_alg
,
1957 data
->enc_key
.len
* 8);
1959 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_CRYPT
,
1960 sizeof(*algo
) + data
->enc_key
.len
);
1965 algo
->alg_key_len
= data
->enc_key
.len
* 8;
1966 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
)-1);
1967 algo
->alg_name
[sizeof(algo
->alg_name
)-1] = '\0';
1968 memcpy(algo
->alg_key
, data
->enc_key
.ptr
, data
->enc_key
.len
);
1972 if (data
->int_alg
!= AUTH_UNDEFINED
)
1974 u_int trunc_len
= 0;
1976 alg_name
= lookup_algorithm(INTEGRITY_ALGORITHM
, data
->int_alg
);
1977 if (alg_name
== NULL
)
1979 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1980 integrity_algorithm_names
, data
->int_alg
);
1983 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1984 integrity_algorithm_names
, data
->int_alg
, data
->int_key
.len
* 8);
1986 switch (data
->int_alg
)
1988 case AUTH_HMAC_MD5_128
:
1989 case AUTH_HMAC_SHA2_256_128
:
1992 case AUTH_HMAC_SHA1_160
:
1995 case AUTH_HMAC_SHA2_256_256
:
1998 case AUTH_HMAC_SHA2_384_384
:
2001 case AUTH_HMAC_SHA2_512_512
:
2010 struct xfrm_algo_auth
* algo
;
2012 /* the kernel uses SHA256 with 96 bit truncation by default,
2013 * use specified truncation size supported by newer kernels.
2014 * also use this for untruncated MD5, SHA1 and SHA2. */
2015 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH_TRUNC
,
2016 sizeof(*algo
) + data
->int_key
.len
);
2021 algo
->alg_key_len
= data
->int_key
.len
* 8;
2022 algo
->alg_trunc_len
= trunc_len
;
2023 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
)-1);
2024 algo
->alg_name
[sizeof(algo
->alg_name
)-1] = '\0';
2025 memcpy(algo
->alg_key
, data
->int_key
.ptr
, data
->int_key
.len
);
2029 struct xfrm_algo
* algo
;
2031 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH
,
2032 sizeof(*algo
) + data
->int_key
.len
);
2037 algo
->alg_key_len
= data
->int_key
.len
* 8;
2038 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
)-1);
2039 algo
->alg_name
[sizeof(algo
->alg_name
)-1] = '\0';
2040 memcpy(algo
->alg_key
, data
->int_key
.ptr
, data
->int_key
.len
);
2044 if (ipcomp
!= IPCOMP_NONE
)
2046 struct xfrm_algo
* algo
;
2048 alg_name
= lookup_algorithm(COMPRESSION_ALGORITHM
, ipcomp
);
2049 if (alg_name
== NULL
)
2051 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
2052 ipcomp_transform_names
, ipcomp
);
2055 DBG2(DBG_KNL
, " using compression algorithm %N",
2056 ipcomp_transform_names
, ipcomp
);
2058 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_COMP
,
2064 algo
->alg_key_len
= 0;
2065 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
)-1);
2066 algo
->alg_name
[sizeof(algo
->alg_name
)-1] = '\0';
2071 struct xfrm_encap_tmpl
*tmpl
;
2073 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
, sizeof(*tmpl
));
2078 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
2079 tmpl
->encap_sport
= htons(id
->src
->get_port(id
->src
));
2080 tmpl
->encap_dport
= htons(id
->dst
->get_port(id
->dst
));
2081 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
2082 /* encap_oa could probably be derived from the
2083 * traffic selectors [rfc4306, p39]. In the netlink kernel
2084 * implementation pluto does the same as we do here but it uses
2085 * encap_oa in the pfkey implementation.
2086 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
2088 * -> does that mean that NAT-T encap doesn't work in transport mode?
2089 * No. The reason the kernel ignores NAT-OA is that it recomputes
2090 * (or, rather, just ignores) the checksum. If packets pass the IPsec
2091 * checks it marks them "checksum ok" so OA isn't needed. */
2093 /* if the remote port is set to 0 for UDP-encapsulated per-CPU SAs, we
2094 * increase the treshold for mapping changes as it gets otherwise
2095 * triggered with every packet */
2096 if (data
->inbound
&& !id
->src
->get_port(id
->src
) &&
2097 !add_uint32(hdr
, sizeof(request
), XFRMA_MTIMER_THRESH
, UINT32_MAX
))
2103 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2108 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2113 if (!add_label(hdr
, sizeof(request
), data
->label
))
2118 if (ipcomp
== IPCOMP_NONE
&& (data
->mark
.value
| data
->mark
.mask
))
2120 if (!add_uint32(hdr
, sizeof(request
), XFRMA_SET_MARK
,
2121 data
->mark
.value
) ||
2122 !add_uint32(hdr
, sizeof(request
), XFRMA_SET_MARK_MASK
,
2129 if (data
->tfc
&& id
->proto
== IPPROTO_ESP
&& mode
== MODE_TUNNEL
)
2130 { /* the kernel supports TFC padding only for tunnel mode ESP SAs */
2131 if (!add_uint32(hdr
, sizeof(request
), XFRMA_TFCPAD
, data
->tfc
))
2138 !add_uint8(hdr
, sizeof(request
), XFRMA_SA_DIR
,
2139 data
->inbound
? XFRM_SA_DIR_IN
: XFRM_SA_DIR_OUT
))
2144 if (data
->cpu
!= CPU_ID_MAX
)
2146 if (!add_uint32(hdr
, sizeof(request
), XFRMA_SA_PCPU
, data
->cpu
))
2150 DBG2(DBG_KNL
, " using CPU ID: %u", data
->cpu
);
2153 if (mode
== MODE_IPTFS
)
2157 if (!add_uint32(hdr
, sizeof(request
), XFRMA_IPTFS_DROP_TIME
,
2158 lib
->settings
->get_int(lib
->settings
,
2159 "%s.iptfs.drop_time", 1000000, lib
->ns
)))
2163 if (!add_uint16(hdr
, sizeof(request
), XFRMA_IPTFS_REORDER_WINDOW
,
2164 lib
->settings
->get_int(lib
->settings
,
2165 "%s.iptfs.reorder_window", 3, lib
->ns
)))
2172 if (!add_uint32(hdr
, sizeof(request
), XFRMA_IPTFS_INIT_DELAY
,
2173 lib
->settings
->get_int(lib
->settings
,
2174 "%s.iptfs.init_delay", 0, lib
->ns
)))
2178 if (!add_uint32(hdr
, sizeof(request
), XFRMA_IPTFS_MAX_QSIZE
,
2179 lib
->settings
->get_int(lib
->settings
,
2180 "%s.iptfs.max_queue_size", 1024 * 1024, lib
->ns
)))
2184 if (!add_uint32(hdr
, sizeof(request
), XFRMA_IPTFS_PKT_SIZE
,
2185 lib
->settings
->get_int(lib
->settings
,
2186 "%s.iptfs.packet_size", 0, lib
->ns
)))
2190 if ((data
->iptfs_dont_frag
||
2191 lib
->settings
->get_bool(lib
->settings
,
2192 "%s.iptfs.dont_fragment", FALSE
, lib
->ns
)) &&
2193 !netlink_reserve(hdr
, sizeof(request
), XFRMA_IPTFS_DONT_FRAG
, 0))
2200 if (id
->proto
!= IPPROTO_COMP
)
2202 /* we don't need a replay window for outbound SAs, however, older
2203 * kernels reject the attribute if it is 0 when using ESN, while
2204 * newer kernels reject it if > 0 if the SA's direction is set */
2205 if (!data
->inbound
&& data
->replay_window
)
2207 data
->replay_window
= (data
->esn
&& !this->sa_dir
) ? 1 : 0;
2209 if (data
->esn
|| data
->replay_window
> 32)
2211 /* for ESN or larger replay windows we need the new
2212 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
2213 struct xfrm_replay_state_esn
*replay
;
2216 bmp_size
= round_up(data
->replay_window
, sizeof(uint32_t) * 8) / 8;
2217 replay
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
2218 sizeof(*replay
) + bmp_size
);
2223 /* bmp_len contains number uf __u32's */
2224 replay
->bmp_len
= bmp_size
/ sizeof(uint32_t);
2225 replay
->replay_window
= data
->replay_window
;
2226 DBG2(DBG_KNL
, " using replay window of %u packets",
2227 data
->replay_window
);
2231 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
2232 sa
->flags
|= XFRM_STATE_ESN
;
2237 DBG2(DBG_KNL
, " using replay window of %u packets",
2238 data
->replay_window
);
2239 sa
->replay_window
= data
->replay_window
;
2242 DBG2(DBG_KNL
, " HW offload: %N", hw_offload_names
, data
->hw_offload
);
2243 if (!add_hw_offload_sa(hdr
, sizeof(request
), id
, data
, &offload
))
2245 DBG1(DBG_KNL
, "failed to configure HW offload");
2250 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2252 if (status
!= SUCCESS
&& offload
&& data
->hw_offload
== HW_OFFLOAD_AUTO
)
2254 DBG1(DBG_KNL
, "failed to install SA with %N HW offload, trying with "
2255 "%N HW offload", hw_offload_names
, HW_OFFLOAD_PACKET
,
2256 hw_offload_names
, HW_OFFLOAD_CRYPTO
);
2257 offload
->flags
&= ~XFRM_OFFLOAD_PACKET
;
2258 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2261 if (status
== NOT_FOUND
&& data
->update
)
2263 DBG1(DBG_KNL
, "allocated SPI not found anymore, try to add SAD entry");
2264 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2265 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2268 if (status
!= SUCCESS
)
2270 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x%s (%N)", ntohl(id
->spi
),
2271 markstr
, status_names
, status
);
2279 memwipe(&request
, sizeof(request
));
2284 * Get the usage stats (packets/bytes) and classic replay state (i.e. sequence
2285 * numbers for small windows/non-ESN) of an SA.
2287 * Allocates and copies the attributes we get from the kernel.
2289 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
2290 kernel_ipsec_sa_id_t
*sa
,
2291 struct xfrm_replay_state
**replay
,
2292 struct xfrm_lifetime_cur
**lifetime
)
2294 netlink_buf_t request
;
2295 struct nlmsghdr
*hdr
, *out
= NULL
;
2296 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
2301 memset(&request
, 0, sizeof(request
));
2303 DBG3(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
2307 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2308 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
2309 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
2311 aevent_id
= NLMSG_DATA(hdr
);
2312 aevent_id
->flags
= XFRM_AE_RVAL
;
2314 host2xfrm(sa
->dst
, &aevent_id
->sa_id
.daddr
);
2315 aevent_id
->sa_id
.spi
= sa
->spi
;
2316 aevent_id
->sa_id
.proto
= sa
->proto
;
2317 aevent_id
->sa_id
.family
= sa
->dst
->get_family(sa
->dst
);
2319 if (!add_mark(hdr
, sizeof(request
), sa
->mark
))
2323 if (sa
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, sa
->if_id
))
2328 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2331 while (NLMSG_OK(hdr
, len
))
2333 switch (hdr
->nlmsg_type
)
2335 case XFRM_MSG_NEWAE
:
2337 out_aevent
= NLMSG_DATA(hdr
);
2342 netlink_log_error(hdr
, "querying replay state from SAD "
2347 hdr
= NLMSG_NEXT(hdr
, len
);
2358 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
2359 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
2360 while (RTA_OK(rta
, rtasize
))
2362 if (rta
->rta_type
== XFRMA_LTIME_VAL
&&
2363 RTA_PAYLOAD(rta
) == sizeof(**lifetime
))
2366 *lifetime
= malloc(RTA_PAYLOAD(rta
));
2367 memcpy(*lifetime
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2369 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
2370 RTA_PAYLOAD(rta
) == sizeof(**replay
))
2373 *replay
= malloc(RTA_PAYLOAD(rta
));
2374 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2376 rta
= RTA_NEXT(rta
, rtasize
);
2383 * Get the last used time of an SA if provided by the kernel
2385 static bool get_lastused(struct nlmsghdr
*hdr
, uint64_t *lastused
)
2390 rta
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
2391 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_usersa_info
);
2392 while (RTA_OK(rta
, rtasize
))
2394 if (rta
->rta_type
== XFRMA_LASTUSED
&&
2395 RTA_PAYLOAD(rta
) == sizeof(*lastused
))
2397 *lastused
= *(uint64_t*)RTA_DATA(rta
);
2400 rta
= RTA_NEXT(rta
, rtasize
);
2405 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
2406 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2407 kernel_ipsec_query_sa_t
*data
, uint64_t *bytes
, uint64_t *packets
,
2410 netlink_buf_t request
;
2411 struct nlmsghdr
*out
= NULL
, *hdr
;
2412 struct xfrm_usersa_id
*sa_id
;
2413 struct xfrm_usersa_info
*sa
= NULL
;
2414 status_t status
= FAILED
;
2416 char markstr
[32] = "";
2418 memset(&request
, 0, sizeof(request
));
2419 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2421 DBG3(DBG_KNL
, "querying SAD entry with SPI %.8x%s", ntohl(id
->spi
),
2425 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2426 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2427 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2429 sa_id
= NLMSG_DATA(hdr
);
2430 host2xfrm(id
->dst
, &sa_id
->daddr
);
2431 sa_id
->spi
= id
->spi
;
2432 sa_id
->proto
= id
->proto
;
2433 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2435 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2439 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2444 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2447 while (NLMSG_OK(hdr
, len
))
2449 switch (hdr
->nlmsg_type
)
2451 case XFRM_MSG_NEWSA
:
2453 sa
= NLMSG_DATA(hdr
);
2458 netlink_log_error(hdr
, "querying SAD entry failed");
2462 hdr
= NLMSG_NEXT(hdr
, len
);
2473 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x%s",
2474 ntohl(id
->spi
), markstr
);
2480 *bytes
= sa
->curlft
.bytes
;
2484 *packets
= sa
->curlft
.packets
;
2488 uint64_t lastused
= 0;
2490 /* curlft.use_time contains the timestamp of the SA's first use, not
2491 * the last, but we might get the last use time in an attribute */
2492 if (this->sa_lastused
&& get_lastused(hdr
, &lastused
))
2494 *use_time
= time_monotonic(NULL
) - (time(NULL
) - lastused
);
2508 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
2509 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2510 kernel_ipsec_del_sa_t
*data
)
2512 netlink_buf_t request
;
2513 struct nlmsghdr
*hdr
;
2514 struct xfrm_usersa_id
*sa_id
;
2515 char markstr
[32] = "";
2517 /* if IPComp was used, we first delete the additional IPComp SA */
2520 kernel_ipsec_sa_id_t ipcomp_id
= {
2523 .spi
= htonl(ntohs(data
->cpi
)),
2524 .proto
= IPPROTO_COMP
,
2527 kernel_ipsec_del_sa_t ipcomp
= {};
2528 del_sa(this, &ipcomp_id
, &ipcomp
);
2531 memset(&request
, 0, sizeof(request
));
2532 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2534 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x%s", ntohl(id
->spi
),
2538 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2539 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
2540 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2542 sa_id
= NLMSG_DATA(hdr
);
2543 host2xfrm(id
->dst
, &sa_id
->daddr
);
2544 sa_id
->spi
= id
->spi
;
2545 sa_id
->proto
= id
->proto
;
2546 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2548 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2552 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2557 switch (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
))
2560 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x%s",
2561 ntohl(id
->spi
), markstr
);
2566 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x%s",
2567 ntohl(id
->spi
), markstr
);
2572 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
2573 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2574 kernel_ipsec_update_sa_t
*data
)
2576 netlink_buf_t request
;
2577 struct nlmsghdr
*hdr
, *out_hdr
= NULL
, *out
= NULL
;
2578 struct xfrm_usersa_id
*sa_id
;
2579 struct xfrm_usersa_info
*sa
;
2583 struct xfrm_encap_tmpl
* encap
= NULL
;
2584 struct xfrm_replay_state
*replay
= NULL
;
2585 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
2586 struct xfrm_lifetime_cur
*lifetime
= NULL
;
2587 bool replay_state_seen
= FALSE
;
2588 kernel_ipsec_del_sa_t del
= { 0 };
2589 status_t status
= FAILED
;
2590 traffic_selector_t
*ts
;
2591 char markstr
[32] = "";
2593 /* if IPComp is used, we first update the IPComp SA */
2596 kernel_ipsec_sa_id_t ipcomp_id
= {
2599 .spi
= htonl(ntohs(data
->cpi
)),
2600 .proto
= IPPROTO_COMP
,
2604 kernel_ipsec_update_sa_t ipcomp
= {
2605 .new_src
= data
->new_src
,
2606 .new_dst
= data
->new_dst
,
2607 .new_reqid
= data
->new_reqid
,
2609 update_sa(this, &ipcomp_id
, &ipcomp
);
2612 memset(&request
, 0, sizeof(request
));
2613 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2615 DBG3(DBG_KNL
, "querying SAD entry with SPI %.8x%s for update",
2616 ntohl(id
->spi
), markstr
);
2618 /* query the existing SA first */
2620 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2621 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2622 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2624 sa_id
= NLMSG_DATA(hdr
);
2625 host2xfrm(id
->dst
, &sa_id
->daddr
);
2626 sa_id
->spi
= id
->spi
;
2627 sa_id
->proto
= id
->proto
;
2628 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2630 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2634 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2639 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2642 while (NLMSG_OK(hdr
, len
))
2644 switch (hdr
->nlmsg_type
)
2646 case XFRM_MSG_NEWSA
:
2653 netlink_log_error(hdr
, "querying SAD entry failed");
2657 hdr
= NLMSG_NEXT(hdr
, len
);
2667 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x%s",
2668 ntohl(id
->spi
), markstr
);
2672 get_replay_state(this, id
, &replay
, &lifetime
);
2674 /* delete the old SA (without affecting the IPComp SA) */
2675 if (del_sa(this, id
, &del
) != SUCCESS
)
2677 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x%s",
2678 ntohl(id
->spi
), markstr
);
2682 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x%s from %#H..%#H to "
2683 "%#H..%#H", ntohl(id
->spi
), markstr
, id
->src
, id
->dst
, data
->new_src
,
2685 /* copy over the SA from out to request */
2687 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2688 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2689 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2690 sa
= NLMSG_DATA(hdr
);
2691 memcpy(sa
, NLMSG_DATA(out_hdr
), sizeof(struct xfrm_usersa_info
));
2692 sa
->family
= data
->new_dst
->get_family(data
->new_dst
);
2693 if (data
->new_reqid
)
2695 sa
->reqid
= data
->new_reqid
;
2698 if (!id
->src
->ip_equals(id
->src
, data
->new_src
))
2700 host2xfrm(data
->new_src
, &sa
->saddr
);
2702 ts
= selector2ts(&sa
->sel
, TRUE
);
2703 if (ts
&& ts
->is_host(ts
, id
->src
))
2705 ts
->set_address(ts
, data
->new_src
);
2706 ts2subnet(ts
, &sa
->sel
.saddr
, &sa
->sel
.prefixlen_s
);
2710 if (!id
->dst
->ip_equals(id
->dst
, data
->new_dst
))
2712 host2xfrm(data
->new_dst
, &sa
->id
.daddr
);
2714 ts
= selector2ts(&sa
->sel
, FALSE
);
2715 if (ts
&& ts
->is_host(ts
, id
->dst
))
2717 ts
->set_address(ts
, data
->new_dst
);
2718 ts2subnet(ts
, &sa
->sel
.daddr
, &sa
->sel
.prefixlen_d
);
2723 rta
= XFRM_RTA(out_hdr
, struct xfrm_usersa_info
);
2724 rtasize
= XFRM_PAYLOAD(out_hdr
, struct xfrm_usersa_info
);
2725 while (RTA_OK(rta
, rtasize
))
2727 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2728 if (rta
->rta_type
!= XFRMA_ENCAP
|| data
->new_encap
)
2730 if (rta
->rta_type
== XFRMA_ENCAP
)
2731 { /* update encap tmpl */
2732 encap
= RTA_DATA(rta
);
2733 encap
->encap_sport
= ntohs(data
->new_src
->get_port(data
->new_src
));
2734 encap
->encap_dport
= ntohs(data
->new_dst
->get_port(data
->new_dst
));
2736 if (rta
->rta_type
== XFRMA_OFFLOAD_DEV
)
2737 { /* update offload device */
2738 struct xfrm_user_offload
*offload
;
2742 offload
= RTA_DATA(rta
);
2743 local
= offload
->flags
& XFRM_OFFLOAD_INBOUND
? data
->new_dst
2746 if (charon
->kernel
->get_interface(charon
->kernel
, local
,
2749 offload
->ifindex
= if_nametoindex(ifname
);
2753 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
||
2754 rta
->rta_type
== XFRMA_REPLAY_VAL
)
2756 replay_state_seen
= TRUE
;
2758 netlink_add_attribute(hdr
, rta
->rta_type
,
2759 chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
)),
2762 rta
= RTA_NEXT(rta
, rtasize
);
2765 if (encap
== NULL
&& data
->new_encap
)
2766 { /* add tmpl if we are enabling it */
2767 encap
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
,
2773 encap
->encap_type
= UDP_ENCAP_ESPINUDP
;
2774 encap
->encap_sport
= ntohs(data
->new_src
->get_port(data
->new_src
));
2775 encap
->encap_dport
= ntohs(data
->new_dst
->get_port(data
->new_dst
));
2776 memset(&encap
->encap_oa
, 0, sizeof (xfrm_address_t
));
2779 if (!replay_state_seen
)
2783 struct xfrm_replay_state
*state
;
2785 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_VAL
,
2791 memcpy(state
, replay
, sizeof(*state
));
2795 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry with "
2796 "SPI %.8x%s", ntohl(id
->spi
), markstr
);
2801 struct xfrm_lifetime_cur
*state
;
2803 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_LTIME_VAL
,
2809 memcpy(state
, lifetime
, sizeof(*state
));
2813 DBG1(DBG_KNL
, "unable to copy usage stats from old SAD entry with "
2814 "SPI %.8x%s", ntohl(id
->spi
), markstr
);
2817 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2819 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x%s",
2820 ntohl(id
->spi
), markstr
);
2830 memwipe(&request
, sizeof(request
));
2836 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
2837 private_kernel_netlink_ipsec_t
*this)
2839 netlink_buf_t request
;
2840 struct nlmsghdr
*hdr
;
2841 struct xfrm_usersa_flush
*flush
;
2846 { IPPROTO_AH
, "AH" },
2847 { IPPROTO_ESP
, "ESP" },
2848 { IPPROTO_COMP
, "IPComp" },
2852 memset(&request
, 0, sizeof(request
));
2855 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2856 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
2857 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
2859 flush
= NLMSG_DATA(hdr
);
2861 for (i
= 0; i
< countof(protos
); i
++)
2863 DBG2(DBG_KNL
, "flushing all %s SAD entries", protos
[i
].name
);
2865 flush
->proto
= protos
[i
].proto
;
2867 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2869 DBG1(DBG_KNL
, "unable to flush %s SAD entries", protos
[i
].name
);
2877 * Unlock the mutex and signal waiting threads
2879 static void policy_change_done(private_kernel_netlink_ipsec_t
*this,
2880 policy_entry_t
*policy
)
2882 policy
->working
= FALSE
;
2883 if (policy
->waiting
)
2884 { /* don't need to wake threads waiting for other policies */
2885 this->condvar
->broadcast(this->condvar
);
2887 this->mutex
->unlock(this->mutex
);
2891 * Find an XFRM interface with the given ID
2893 static bool find_xfrmi(private_kernel_netlink_ipsec_t
*this, uint32_t target
,
2896 enumerator_t
*enumerator
;
2900 enumerator
= this->xfrmi
->create_enumerator(this->xfrmi
);
2901 while (enumerator
->enumerate(enumerator
, &name
, &if_id
, NULL
, NULL
))
2903 if (if_id
== target
)
2905 *if_name
= strdup(name
);
2906 enumerator
->destroy(enumerator
);
2910 enumerator
->destroy(enumerator
);
2915 * Install a route for the given policy if enabled and required
2917 static void install_route(private_kernel_netlink_ipsec_t
*this,
2918 policy_entry_t
*policy
, policy_sa_t
*mapping
, ipsec_sa_t
*ipsec
)
2920 policy_sa_out_t
*out
= (policy_sa_out_t
*)mapping
;
2921 route_entry_t
*route
;
2925 .prefixlen
= policy
->sel
.prefixlen_d
,
2926 .pass
= mapping
->type
== POLICY_PASS
,
2929 if (charon
->kernel
->get_address_by_ts(charon
->kernel
, out
->src_ts
,
2930 &route
->src_ip
, NULL
) != SUCCESS
)
2937 /* allow blank source IP for passthrough policies */
2938 route
->src_ip
= host_create_any(policy
->sel
.family
);
2941 if (!ipsec
->dst
->is_anyaddr(ipsec
->dst
))
2943 /* if if_ids are used, install a route via XFRM interface if any,
2944 * otherwise install the route via the interface we reach the peer */
2945 if (!policy
->if_id
|| !this->xfrmi
||
2946 !find_xfrmi(this, policy
->if_id
, &route
->if_name
))
2948 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2949 ipsec
->dst
, -1, ipsec
->src
,
2954 { /* for shunt policies */
2955 iface
= xfrm2host(policy
->sel
.family
, &policy
->sel
.daddr
, 0);
2956 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2957 iface
, policy
->sel
.prefixlen_d
,
2958 route
->src_ip
, &route
->if_name
);
2959 iface
->destroy(iface
);
2961 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET
? 4 : 16);
2962 memcpy(route
->dst_net
.ptr
, &policy
->sel
.daddr
, route
->dst_net
.len
);
2964 /* get the interface to install the route for, if we haven't one yet.
2965 * If we have a local address, use it. Otherwise (for shunt policies)
2966 * use the route's source address. */
2967 if (!route
->if_name
)
2970 if (iface
->is_anyaddr(iface
))
2972 iface
= route
->src_ip
;
2974 if (!charon
->kernel
->get_interface(charon
->kernel
, iface
,
2977 { /* don't require an interface for passthrough policies */
2978 route_entry_destroy(route
);
2984 route_entry_t
*old
= policy
->route
;
2985 if (route_entry_equals(old
, route
))
2987 route_entry_destroy(route
);
2990 /* uninstall previously installed route */
2991 if (charon
->kernel
->del_route(charon
->kernel
, old
->dst_net
,
2992 old
->prefixlen
, old
->gateway
,
2993 old
->src_ip
, old
->if_name
,
2994 old
->pass
) != SUCCESS
)
2996 DBG1(DBG_KNL
, "error uninstalling route installed with policy "
2997 "%R === %R %N", out
->src_ts
, out
->dst_ts
, policy_dir_names
,
3000 route_entry_destroy(old
);
3001 policy
->route
= NULL
;
3004 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s", out
->dst_ts
,
3005 route
->gateway
, route
->src_ip
, route
->if_name
);
3006 switch (charon
->kernel
->add_route(charon
->kernel
, route
->dst_net
,
3007 route
->prefixlen
, route
->gateway
,
3008 route
->src_ip
, route
->if_name
,
3012 DBG1(DBG_KNL
, "unable to install source route for %H",
3016 /* route exists, do not uninstall */
3017 route_entry_destroy(route
);
3020 /* cache the installed route */
3021 policy
->route
= route
;
3027 * Add or update a policy in the kernel.
3029 * Note: The mutex has to be locked when entering this function
3030 * and is unlocked here in any case.
3032 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
3033 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
3035 netlink_buf_t request
;
3036 policy_entry_t clone
;
3037 ipsec_sa_t
*ipsec
= mapping
->sa
;
3038 struct xfrm_userpolicy_info
*policy_info
;
3039 struct xfrm_user_offload
*offload
= NULL
;
3040 struct nlmsghdr
*hdr
;
3044 /* clone the policy so we are able to check it out again later */
3045 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
3047 memset(&request
, 0, sizeof(request
));
3049 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3050 hdr
->nlmsg_type
= update
? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
3051 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
3053 policy_info
= NLMSG_DATA(hdr
);
3054 policy_info
->sel
= policy
->sel
;
3055 policy_info
->dir
= policy
->direction
;
3057 if (mapping
->pcpu_acquires
)
3059 policy_info
->flags
|= XFRM_POLICY_CPU_ACQUIRE
;
3062 /* calculate priority based on selector size, small size = high prio */
3063 policy_info
->priority
= mapping
->priority
;
3064 policy_info
->action
= mapping
->type
!= POLICY_DROP
? XFRM_POLICY_ALLOW
3065 : XFRM_POLICY_BLOCK
;
3066 policy_info
->share
= XFRM_SHARE_ANY
;
3068 /* policies don't expire */
3069 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
3070 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
3071 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
3072 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
3073 policy_info
->lft
.soft_add_expires_seconds
= 0;
3074 policy_info
->lft
.hard_add_expires_seconds
= 0;
3075 policy_info
->lft
.soft_use_expires_seconds
= 0;
3076 policy_info
->lft
.hard_use_expires_seconds
= 0;
3078 if (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.reqid
)
3080 struct xfrm_user_tmpl
*tmpl
;
3086 { IPPROTO_COMP
, htonl(ntohs(ipsec
->cfg
.ipcomp
.cpi
)),
3087 ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
3088 { IPPROTO_ESP
, ipsec
->cfg
.esp
.spi
, ipsec
->cfg
.esp
.use
},
3089 { IPPROTO_AH
, ipsec
->cfg
.ah
.spi
, ipsec
->cfg
.ah
.use
},
3091 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
3094 for (i
= 0; i
< countof(protos
); i
++)
3101 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_TMPL
,
3102 count
* sizeof(*tmpl
));
3105 policy_change_done(this, policy
);
3109 for (i
= 0; i
< countof(protos
); i
++)
3115 tmpl
->reqid
= ipsec
->cfg
.reqid
;
3116 tmpl
->id
.proto
= protos
[i
].proto
;
3117 /* in order to match SAs with all matching labels, we can't have the
3118 * SPI in the template, similarly for per-CPU policies and sub-SAs */
3119 if (policy
->direction
== POLICY_OUT
&& !policy
->label
&&
3120 !mapping
->pcpu_acquires
)
3122 tmpl
->id
.spi
= protos
[i
].spi
;
3124 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
3125 tmpl
->mode
= mode2kernel(proto_mode
);
3126 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
3127 policy
->direction
!= POLICY_OUT
;
3128 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
3130 if (proto_mode
== MODE_TUNNEL
|| proto_mode
== MODE_BEET
||
3131 proto_mode
== MODE_IPTFS
)
3132 { /* only for tunnel mode */
3133 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
3134 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
3139 /* use transport mode for other SAs */
3140 proto_mode
= MODE_TRANSPORT
;
3144 if (!add_mark(hdr
, sizeof(request
), ipsec
->mark
))
3146 policy_change_done(this, policy
);
3150 !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, ipsec
->if_id
))
3152 policy_change_done(this, policy
);
3155 if (!add_label(hdr
, sizeof(request
), policy
->label
))
3157 policy_change_done(this, policy
);
3160 /* make sure this is the last attribute added to the message */
3161 if (!add_hw_offload_policy(hdr
, sizeof(request
), policy
, mapping
, &offload
))
3163 policy_change_done(this, policy
);
3166 this->mutex
->unlock(this->mutex
);
3168 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
3170 if (status
!= SUCCESS
&& offload
&& mapping
->sa
->hw_offload
== HW_OFFLOAD_AUTO
)
3172 DBG1(DBG_KNL
, "failed to install SA with %N HW offload, trying without "
3173 "offload", hw_offload_names
, HW_OFFLOAD_PACKET
);
3174 /* the kernel only allows offloading with packet offload and rejects
3175 * the attribute if that flag is not set, so remove it again */
3176 hdr
->nlmsg_len
-= RTA_ALIGN(RTA_LENGTH(sizeof(*offload
)));
3177 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
3180 if (status
== ALREADY_DONE
&& !update
)
3182 DBG1(DBG_KNL
, "policy already exists, try to update it");
3183 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
3184 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
3187 this->mutex
->lock(this->mutex
);
3188 if (status
!= SUCCESS
)
3190 policy_change_done(this, policy
);
3193 /* install a route, if:
3194 * - this is an outbound policy (to just get one for each child)
3195 * - routing is not disabled via strongswan.conf
3196 * - the selector is not for a specific protocol/port
3197 * - routes via XFRM interfaces are enabled or no interface ID is configured
3198 * - we are in tunnel/BEET mode or install a bypass policy
3200 if (policy
->direction
== POLICY_OUT
&& this->install_routes
&&
3201 !policy
->sel
.proto
&& !policy
->sel
.dport
&& !policy
->sel
.sport
&&
3202 (this->install_routes_xfrmi
|| !policy
->if_id
))
3204 if (mapping
->type
== POLICY_PASS
||
3205 (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.mode
!= MODE_TRANSPORT
))
3207 install_route(this, policy
, mapping
, ipsec
);
3210 policy_change_done(this, policy
);
3214 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
3215 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
3216 kernel_ipsec_manage_policy_t
*data
)
3218 policy_entry_t
*policy
, *current
;
3219 policy_sa_t
*assigned_sa
, *current_sa
;
3220 enumerator_t
*enumerator
;
3221 bool found
= FALSE
, update
= TRUE
;
3222 char markstr
[32] = "", labelstr
[128] = "";
3223 uint32_t cur_priority DBG_UNUSED
= 0;
3224 int use_count DBG_UNUSED
;
3226 /* create a policy */
3228 .sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
),
3229 .mark
= id
->mark
.value
& id
->mark
.mask
,
3231 .label
= id
->label
? id
->label
->clone(id
->label
) : NULL
,
3232 .direction
= id
->dir
,
3233 .reqid
= data
->sa
->reqid
,
3235 format_mark(markstr
, sizeof(markstr
), id
->mark
);
3236 format_label(labelstr
, sizeof(labelstr
), id
->label
);
3238 /* find the policy, which matches EXACTLY */
3239 this->mutex
->lock(this->mutex
);
3240 current
= this->policies
->get(this->policies
, policy
);
3242 { /* use existing policy */
3243 DBG2(DBG_KNL
, "policy %R === %R %N%s%s already exists, increasing "
3244 "refcount", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
3246 policy_entry_destroy(this, policy
);
3251 while (policy
->working
)
3253 this->condvar
->wait(this->condvar
, this->mutex
);
3256 policy
->working
= TRUE
;
3259 { /* use the new one, if we have no such policy */
3260 policy
->used_by
= linked_list_create();
3261 this->policies
->put(this->policies
, policy
, policy
);
3264 /* cache the assigned IPsec SA */
3265 assigned_sa
= policy_sa_create(this, id
->dir
, data
->type
, data
->src
,
3266 data
->dst
, id
->src_ts
, id
->dst_ts
, id
->mark
,
3267 id
->if_id
, data
->hw_offload
,
3268 data
->pcpu_acquires
, data
->sa
);
3269 assigned_sa
->auto_priority
= get_priority(policy
, data
->prio
, id
->interface
);
3270 assigned_sa
->priority
= this->get_priority
? this->get_priority(id
, data
)
3271 : data
->manual_prio
;
3272 assigned_sa
->priority
= assigned_sa
->priority
?: assigned_sa
->auto_priority
;
3274 /* insert the SA according to its priority */
3275 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
3276 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
3278 if (current_sa
->priority
> assigned_sa
->priority
)
3282 if (current_sa
->priority
== assigned_sa
->priority
)
3284 /* in case of equal manual prios order SAs by automatic priority */
3285 if (current_sa
->auto_priority
> assigned_sa
->auto_priority
)
3289 /* prefer SAs with a reqid over those without */
3290 if (current_sa
->auto_priority
== assigned_sa
->auto_priority
&&
3291 (!current_sa
->sa
->cfg
.reqid
|| assigned_sa
->sa
->cfg
.reqid
))
3298 cur_priority
= current_sa
->priority
;
3302 policy
->used_by
->insert_before(policy
->used_by
, enumerator
, assigned_sa
);
3303 enumerator
->destroy(enumerator
);
3305 use_count
= policy
->used_by
->get_count(policy
->used_by
);
3307 { /* we don't update the policy if the priority is lower than that of
3308 * the currently installed one */
3309 policy_change_done(this, policy
);
3310 DBG2(DBG_KNL
, "not updating policy %R === %R %N%s%s [priority %u, "
3311 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
3312 id
->dir
, markstr
, labelstr
, cur_priority
, use_count
);
3315 if (policy
->reqid
!= assigned_sa
->sa
->cfg
.reqid
)
3317 DBG1(DBG_CFG
, "updating reqid for policy %R === %R %N%s%s from %u "
3318 "to %u", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
3319 markstr
, labelstr
, policy
->reqid
, assigned_sa
->sa
->cfg
.reqid
);
3320 policy
->reqid
= assigned_sa
->sa
->cfg
.reqid
;
3323 if (this->policy_update
)
3328 DBG2(DBG_KNL
, "%s policy %R === %R %N%s%s [priority %u, refcount %d]",
3329 found
? "updating" : "adding", id
->src_ts
, id
->dst_ts
,
3330 policy_dir_names
, id
->dir
, markstr
, labelstr
, assigned_sa
->priority
,
3333 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
3335 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N%s%s",
3336 found
? "update" : "add", id
->src_ts
, id
->dst_ts
,
3337 policy_dir_names
, id
->dir
, markstr
, labelstr
);
3343 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
3344 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
3345 kernel_ipsec_query_policy_t
*data
, time_t *use_time
)
3347 netlink_buf_t request
;
3348 struct nlmsghdr
*out
= NULL
, *hdr
;
3349 struct xfrm_userpolicy_id
*policy_id
;
3350 struct xfrm_userpolicy_info
*policy
= NULL
;
3352 char markstr
[32] = "", labelstr
[128] = "";
3354 memset(&request
, 0, sizeof(request
));
3355 format_mark(markstr
, sizeof(markstr
), id
->mark
);
3356 format_label(labelstr
, sizeof(labelstr
), id
->label
);
3358 DBG3(DBG_KNL
, "querying policy %R === %R %N%s%s", id
->src_ts
, id
->dst_ts
,
3359 policy_dir_names
, id
->dir
, markstr
, labelstr
);
3362 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
3363 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
3364 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
3366 policy_id
= NLMSG_DATA(hdr
);
3367 policy_id
->sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
);
3368 policy_id
->dir
= id
->dir
;
3370 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
3374 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
3378 if (!add_label(hdr
, sizeof(request
), id
->label
))
3383 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
3386 while (NLMSG_OK(hdr
, len
))
3388 switch (hdr
->nlmsg_type
)
3390 case XFRM_MSG_NEWPOLICY
:
3392 policy
= NLMSG_DATA(hdr
);
3397 netlink_log_error(hdr
, "querying policy failed");
3401 hdr
= NLMSG_NEXT(hdr
, len
);
3412 DBG2(DBG_KNL
, "unable to query policy %R === %R %N%s", id
->src_ts
,
3413 id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
);
3418 if (policy
->curlft
.use_time
)
3420 /* we need the monotonic time, but the kernel returns system time. */
3421 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
3432 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
3433 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
3434 kernel_ipsec_manage_policy_t
*data
)
3436 policy_entry_t
*current
, policy
;
3437 enumerator_t
*enumerator
;
3438 policy_sa_t
*mapping
;
3439 netlink_buf_t request
;
3440 struct nlmsghdr
*hdr
;
3441 struct xfrm_userpolicy_id
*policy_id
;
3442 bool is_installed
= TRUE
;
3443 uint32_t priority
, auto_priority
, cur_priority DBG_UNUSED
;
3444 ipsec_sa_t assigned_sa
= {
3449 .hw_offload
= data
->hw_offload
,
3452 char markstr
[32] = "", labelstr
[128] = "";
3454 status_t status
= SUCCESS
;
3456 format_mark(markstr
, sizeof(markstr
), id
->mark
);
3457 format_label(labelstr
, sizeof(labelstr
), id
->label
);
3459 DBG2(DBG_KNL
, "deleting policy %R === %R %N%s%s", id
->src_ts
, id
->dst_ts
,
3460 policy_dir_names
, id
->dir
, markstr
, labelstr
);
3462 /* create a policy */
3463 memset(&policy
, 0, sizeof(policy_entry_t
));
3464 policy
.sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
);
3465 policy
.mark
= id
->mark
.value
& id
->mark
.mask
;
3466 policy
.if_id
= id
->if_id
;
3467 policy
.label
= id
->label
;
3468 policy
.direction
= id
->dir
;
3470 /* find the policy */
3471 this->mutex
->lock(this->mutex
);
3472 current
= this->policies
->get(this->policies
, &policy
);
3475 DBG1(DBG_KNL
, "deleting policy %R === %R %N%s%s failed, not found",
3476 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
,
3478 this->mutex
->unlock(this->mutex
);
3482 while (current
->working
)
3484 this->condvar
->wait(this->condvar
, this->mutex
);
3486 current
->working
= TRUE
;
3489 /* remove mapping to SA by reqid and priority */
3490 auto_priority
= get_priority(current
, data
->prio
, id
->interface
);
3491 priority
= this->get_priority
? this->get_priority(id
, data
)
3492 : data
->manual_prio
;
3493 priority
= priority
?: auto_priority
;
3495 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
3496 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
3498 if (priority
== mapping
->priority
&&
3499 auto_priority
== mapping
->auto_priority
&&
3500 data
->type
== mapping
->type
&&
3501 data
->pcpu_acquires
== mapping
->pcpu_acquires
&&
3502 ipsec_sa_equals(mapping
->sa
, &assigned_sa
))
3504 current
->used_by
->remove_at(current
->used_by
, enumerator
);
3505 policy_sa_destroy(mapping
, id
->dir
, this);
3510 cur_priority
= mapping
->priority
;
3511 is_installed
= FALSE
;
3514 enumerator
->destroy(enumerator
);
3516 use_count
= current
->used_by
->get_count(current
->used_by
);
3518 { /* policy is used by more SAs, keep in kernel */
3519 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
3521 { /* no need to update as the policy was not installed for this SA */
3522 policy_change_done(this, current
);
3523 DBG2(DBG_KNL
, "not updating policy %R === %R %N%s%s [priority %u, "
3524 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
3525 id
->dir
, markstr
, labelstr
, cur_priority
, use_count
);
3528 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
3529 if (current
->reqid
!= mapping
->sa
->cfg
.reqid
)
3531 DBG1(DBG_CFG
, "updating reqid for policy %R === %R %N%s%s from %u "
3532 "to %u", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
3533 markstr
, labelstr
, current
->reqid
, mapping
->sa
->cfg
.reqid
);
3534 current
->reqid
= mapping
->sa
->cfg
.reqid
;
3537 DBG2(DBG_KNL
, "updating policy %R === %R %N%s%s [priority %u, "
3538 "refcount %d]", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
3539 markstr
, labelstr
, mapping
->priority
, use_count
);
3541 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
3543 DBG1(DBG_KNL
, "unable to update policy %R === %R %N%s%s",
3544 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
,
3551 memset(&request
, 0, sizeof(request
));
3554 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3555 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
3556 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
3558 policy_id
= NLMSG_DATA(hdr
);
3559 policy_id
->sel
= current
->sel
;
3560 policy_id
->dir
= id
->dir
;
3562 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
3564 policy_change_done(this, current
);
3567 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
3569 policy_change_done(this, current
);
3572 if (!add_label(hdr
, sizeof(request
), id
->label
))
3574 policy_change_done(this, current
);
3580 route_entry_t
*route
= current
->route
;
3581 if (charon
->kernel
->del_route(charon
->kernel
, route
->dst_net
,
3582 route
->prefixlen
, route
->gateway
,
3583 route
->src_ip
, route
->if_name
,
3584 route
->pass
) != SUCCESS
)
3586 DBG1(DBG_KNL
, "error uninstalling route installed with policy "
3587 "%R === %R %N%s%s", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
3588 id
->dir
, markstr
, labelstr
);
3591 this->mutex
->unlock(this->mutex
);
3593 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
3595 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N%s%s", id
->src_ts
,
3596 id
->dst_ts
, policy_dir_names
, id
->dir
, markstr
, labelstr
);
3600 this->mutex
->lock(this->mutex
);
3601 if (!current
->waiting
)
3602 { /* only if no other thread still needs the policy */
3603 this->policies
->remove(this->policies
, current
);
3604 policy_entry_destroy(this, current
);
3605 this->mutex
->unlock(this->mutex
);
3609 policy_change_done(this, current
);
3614 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
3615 private_kernel_netlink_ipsec_t
*this)
3617 netlink_buf_t request
;
3618 struct nlmsghdr
*hdr
;
3620 memset(&request
, 0, sizeof(request
));
3622 DBG2(DBG_KNL
, "flushing all policies from SPD");
3625 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3626 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
3627 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
3629 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
3630 * to main or sub policies (default is main) */
3632 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
3634 DBG1(DBG_KNL
, "unable to flush SPD entries");
3641 * Bypass socket using a per-socket policy
3643 static bool add_socket_bypass(private_kernel_netlink_ipsec_t
*this,
3646 struct xfrm_userpolicy_info policy
;
3647 u_int sol
, ipsec_policy
;
3653 ipsec_policy
= IP_XFRM_POLICY
;
3657 ipsec_policy
= IPV6_XFRM_POLICY
;
3663 memset(&policy
, 0, sizeof(policy
));
3664 policy
.action
= XFRM_POLICY_ALLOW
;
3665 policy
.sel
.family
= family
;
3667 policy
.dir
= XFRM_POLICY_OUT
;
3668 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
3670 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s (%d)",
3671 strerror(errno
), errno
);
3674 policy
.dir
= XFRM_POLICY_IN
;
3675 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
3677 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s (%d)",
3678 strerror(errno
), errno
);
3685 * Keep track of interface and its offload support
3695 * Name of the interface
3697 char ifname
[IFNAMSIZ
];
3708 /** Offload support unknown */
3709 IFACE_OFFLOAD_UNKNOWN
,
3710 /** No offload supported */
3712 /** Interface supports at least crypto offload */
3713 IFACE_OFFLOAD_DETECTED
,
3714 /** Interface supports crypto offload, but no packet and policy offload */
3715 IFACE_OFFLOAD_CRYPTO
,
3716 /** Packet and policy offload supported */
3717 IFACE_OFFLOAD_PACKET
,
3723 * Port based IKE bypass policy
3726 /** address family */
3728 /** layer 4 protocol */
3730 /** port number, network order */
3735 * Add or remove a bypass policy from/to kernel. If an interface is given,
3736 * the policy is tried to be offloaded to that interface.
3738 static bool manage_bypass(private_kernel_netlink_ipsec_t
*this,
3739 int type
, policy_dir_t dir
, bypass_t
*bypass
,
3742 netlink_buf_t request
;
3743 struct xfrm_selector
*sel
;
3744 struct xfrm_user_offload
*offload
= NULL
;
3745 struct nlmsghdr
*hdr
;
3747 memset(&request
, 0, sizeof(request
));
3749 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3750 hdr
->nlmsg_type
= type
;
3752 if (type
== XFRM_MSG_NEWPOLICY
)
3754 struct xfrm_userpolicy_info
*policy
;
3756 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
3758 policy
= NLMSG_DATA(hdr
);
3760 policy
->priority
= 32;
3761 policy
->action
= XFRM_POLICY_ALLOW
;
3762 policy
->share
= XFRM_SHARE_ANY
;
3764 policy
->lft
.soft_byte_limit
= XFRM_INF
;
3765 policy
->lft
.soft_packet_limit
= XFRM_INF
;
3766 policy
->lft
.hard_byte_limit
= XFRM_INF
;
3767 policy
->lft
.hard_packet_limit
= XFRM_INF
;
3772 !add_hw_offload(hdr
, sizeof(request
), NULL
, ifname
,
3773 HW_OFFLOAD_PACKET
, &offload
))
3778 else /* XFRM_MSG_DELPOLICY */
3780 struct xfrm_userpolicy_id
*policy
;
3782 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
3784 policy
= NLMSG_DATA(hdr
);
3790 sel
->family
= bypass
->family
;
3791 sel
->proto
= bypass
->proto
;
3792 if (dir
== POLICY_IN
)
3794 sel
->dport
= bypass
->port
;
3795 sel
->dport_mask
= 0xffff;
3799 sel
->sport
= bypass
->port
;
3800 sel
->sport_mask
= 0xffff;
3804 sel
->ifindex
= if_nametoindex(ifname
);
3806 return this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) == SUCCESS
;
3809 CALLBACK(remove_port_bypass
, void,
3810 bypass_t
*bypass
, int idx
, void *user
)
3812 private_kernel_netlink_ipsec_t
*this = user
;
3813 enumerator_t
*enumerator
;
3814 offload_iface_t
*iface
;
3816 if (this->port_bypass
)
3818 manage_bypass(this, XFRM_MSG_DELPOLICY
, POLICY_OUT
, bypass
, NULL
);
3819 manage_bypass(this, XFRM_MSG_DELPOLICY
, POLICY_IN
, bypass
, NULL
);
3821 if (this->offload_interfaces
)
3823 enumerator
= this->offload_interfaces
->create_enumerator(this->offload_interfaces
);
3824 while (enumerator
->enumerate(enumerator
, NULL
, &iface
))
3826 if (iface
->offload
== IFACE_OFFLOAD_PACKET
&&
3827 iface
->flags
& IFF_UP
)
3829 manage_bypass(this, XFRM_MSG_DELPOLICY
, POLICY_OUT
, bypass
,
3831 manage_bypass(this, XFRM_MSG_DELPOLICY
, POLICY_IN
, bypass
,
3835 enumerator
->destroy(enumerator
);
3840 * Bypass socket using a port-based bypass policy, optionally offloaded to a
3843 static bool add_port_bypass(private_kernel_netlink_ipsec_t
*this,
3844 bypass_t
*bypass
, char *ifname
)
3846 if (!manage_bypass(this, XFRM_MSG_NEWPOLICY
, POLICY_IN
, bypass
, ifname
))
3850 if (!manage_bypass(this, XFRM_MSG_NEWPOLICY
, POLICY_OUT
, bypass
, ifname
))
3852 manage_bypass(this, XFRM_MSG_DELPOLICY
, POLICY_IN
, bypass
, ifname
);
3859 * Offload the given port-based bypass policy to the given interface if possible.
3861 * offload_mutex is assumed to be locked.
3863 static bool offload_bypass_iface(private_kernel_netlink_ipsec_t
*this,
3864 bypass_t
*bypass
, offload_iface_t
*iface
)
3866 if ((iface
->offload
== IFACE_OFFLOAD_DETECTED
||
3867 iface
->offload
== IFACE_OFFLOAD_PACKET
))
3869 if (add_port_bypass(this, bypass
, iface
->ifname
))
3871 iface
->offload
= IFACE_OFFLOAD_PACKET
;
3874 else if (iface
->offload
== IFACE_OFFLOAD_DETECTED
)
3876 iface
->offload
= IFACE_OFFLOAD_CRYPTO
;
3883 * Offload all known port-based bypass policies to the given interface.
3885 * offload_mutex is assumed to be locked.
3887 static void offload_bypasses(private_kernel_netlink_ipsec_t
*this,
3888 offload_iface_t
*iface
)
3890 enumerator_t
*enumerator
;
3893 enumerator
= array_create_enumerator(this->bypass
);
3894 while (enumerator
->enumerate(enumerator
, &bypass
))
3896 if (!offload_bypass_iface(this, bypass
, iface
))
3897 { /* could indicate a failure but generally means that the interface
3898 * does not support offloading */
3902 enumerator
->destroy(enumerator
);
3906 * Offload a new port-based bypass policy to all known interfaces.
3908 * offload_mutex is assumed to be locked.
3910 static void offload_bypass(private_kernel_netlink_ipsec_t
*this,
3913 enumerator_t
*enumerator
;
3914 offload_iface_t
*iface
;
3916 enumerator
= this->offload_interfaces
->create_enumerator(this->offload_interfaces
);
3917 while (enumerator
->enumerate(enumerator
, NULL
, &iface
))
3919 if (iface
->flags
& IFF_UP
)
3921 offload_bypass_iface(this, bypass
, iface
);
3924 enumerator
->destroy(enumerator
);
3928 * Offload a bypass policy on supported hardware if the kernel supports it and
3929 * optionally install a port-based bypass policy in software.
3931 static bool add_and_offload_port_bypass(private_kernel_netlink_ipsec_t
*this,
3936 struct sockaddr_in in
;
3937 struct sockaddr_in6 in6
;
3944 len
= sizeof(saddr
);
3945 if (getsockname(fd
, &saddr
.sa
, &len
) != 0)
3949 #ifdef SO_PROTOCOL /* since 2.6.32 */
3950 len
= sizeof(bypass
.proto
);
3951 if (getsockopt(fd
, SOL_SOCKET
, SO_PROTOCOL
, &bypass
.proto
, &len
) != 0)
3953 { /* assume UDP if SO_PROTOCOL not supported */
3954 bypass
.proto
= IPPROTO_UDP
;
3959 bypass
.port
= saddr
.in
.sin_port
;
3962 bypass
.port
= saddr
.in6
.sin6_port
;
3968 if (this->port_bypass
&&
3969 !add_port_bypass(this, &bypass
, NULL
))
3973 if (this->offload_interfaces
)
3975 this->offload_mutex
->lock(this->offload_mutex
);
3976 offload_bypass(this, &bypass
);
3977 /* store it even if no policy was offloaded because an interface that
3978 * supports offloading might get activated later */
3979 array_insert_create_value(&this->bypass
, sizeof(bypass_t
),
3980 ARRAY_TAIL
, &bypass
);
3981 this->offload_mutex
->unlock(this->offload_mutex
);
3985 array_insert_create_value(&this->bypass
, sizeof(bypass_t
),
3986 ARRAY_TAIL
, &bypass
);
3991 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
3992 private_kernel_netlink_ipsec_t
*this, int fd
, int family
)
3994 if ((this->offload_interfaces
|| this->port_bypass
) &&
3995 !add_and_offload_port_bypass(this, fd
, family
))
3999 return this->port_bypass
|| add_socket_bypass(this, fd
, family
);
4002 METHOD(kernel_ipsec_t
, enable_udp_decap
, bool,
4003 private_kernel_netlink_ipsec_t
*this, int fd
, int family
, uint16_t port
)
4005 int type
= UDP_ENCAP_ESPINUDP
;
4007 if (setsockopt(fd
, SOL_UDP
, UDP_ENCAP
, &type
, sizeof(type
)) < 0)
4009 DBG1(DBG_KNL
, "unable to set UDP_ENCAP: %s", strerror(errno
));
4013 if (setsockopt(fd
, SOL_UDP
, UDP_GRO
, &type
, sizeof(type
)) < 0)
4015 DBG1(DBG_KNL
, "unable to set UDP_GRO: %s", strerror(errno
));
4021 CALLBACK(receive_link_events
, void,
4022 private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
4024 struct ifinfomsg
*msg
= NLMSG_DATA(hdr
);
4025 struct rtattr
*rta
= IFLA_RTA(msg
);
4026 size_t rtasize
= IFLA_PAYLOAD (hdr
);
4027 offload_iface_t
*iface
= NULL
;
4030 if (hdr
->nlmsg_type
!= RTM_NEWLINK
&&
4031 hdr
->nlmsg_type
!= RTM_DELLINK
)
4036 while (RTA_OK(rta
, rtasize
))
4038 switch (rta
->rta_type
)
4041 name
= RTA_DATA(rta
);
4044 rta
= RTA_NEXT(rta
, rtasize
);
4051 this->offload_mutex
->lock(this->offload_mutex
);
4052 if (hdr
->nlmsg_type
== RTM_NEWLINK
)
4054 iface
= this->offload_interfaces
->get(this->offload_interfaces
,
4055 (void*)(uintptr_t)msg
->ifi_index
);
4059 .ifindex
= msg
->ifi_index
4061 this->offload_interfaces
->put(this->offload_interfaces
,
4062 (void*)(uintptr_t)msg
->ifi_index
,
4065 /* update name in case an interface is renamed */
4066 strncpy(iface
->ifname
, name
, IFNAMSIZ
-1);
4067 iface
->ifname
[IFNAMSIZ
-1] = '\0';
4069 if (iface
->offload
== IFACE_OFFLOAD_UNKNOWN
)
4071 if (netlink_detect_offload(iface
->ifname
))
4073 iface
->offload
= IFACE_OFFLOAD_DETECTED
;
4077 iface
->offload
= IFACE_OFFLOAD_NONE
;
4081 /* if an interface is activated or newly detected, try to offload known
4082 * IKE bypass policies. we don't have to do anything if the interface
4083 * goes down as the kernel automatically removes the state it has for
4084 * offloaded policies */
4085 if (!(iface
->flags
& IFF_UP
) && (msg
->ifi_flags
& IFF_UP
))
4087 offload_bypasses(this, iface
);
4089 iface
->flags
= msg
->ifi_flags
;
4093 iface
= this->offload_interfaces
->remove(this->offload_interfaces
,
4094 (void*)(uintptr_t)msg
->ifi_index
);
4097 this->offload_mutex
->unlock(this->offload_mutex
);
4101 * Enumerate all interfaces and check if they support offloading
4103 static bool init_offload_interfaces(private_kernel_netlink_ipsec_t
*this)
4105 netlink_buf_t request
;
4106 netlink_socket_t
*socket
;
4107 struct nlmsghdr
*out
, *current
, *in
;
4108 struct rtgenmsg
*msg
;
4111 socket
= netlink_socket_create(NETLINK_ROUTE
, NULL
, FALSE
);
4117 memset(&request
, 0, sizeof(request
));
4120 in
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
;
4121 in
->nlmsg_type
= RTM_GETLINK
;
4122 in
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtgenmsg
));
4124 msg
= NLMSG_DATA(in
);
4125 msg
->rtgen_family
= AF_UNSPEC
;
4127 if (socket
->send(socket
, in
, &out
, &len
) != SUCCESS
)
4129 socket
->destroy(socket
);
4134 while (NLMSG_OK(current
, len
))
4136 receive_link_events(this, current
);
4137 current
= NLMSG_NEXT(current
, len
);
4140 socket
->destroy(socket
);
4144 METHOD(kernel_ipsec_t
, destroy
, void,
4145 private_kernel_netlink_ipsec_t
*this)
4147 enumerator_t
*enumerator
;
4148 policy_entry_t
*policy
;
4149 offload_iface_t
*iface
;
4151 DESTROY_IF(this->socket_link_events
);
4152 DESTROY_IF(this->socket_xfrm_events
);
4153 array_destroy_function(this->bypass
, remove_port_bypass
, this);
4156 lib
->set(lib
, KERNEL_NETLINK_XFRMI_MANAGER
, NULL
);
4157 kernel_netlink_xfrmi_destroy(this->xfrmi
);
4159 DESTROY_IF(this->socket_xfrm
);
4160 enumerator
= this->policies
->create_enumerator(this->policies
);
4161 while (enumerator
->enumerate(enumerator
, NULL
, &policy
))
4163 policy_entry_destroy(this, policy
);
4165 enumerator
->destroy(enumerator
);
4166 this->policies
->destroy(this->policies
);
4167 this->sas
->destroy(this->sas
);
4168 if (this->offload_interfaces
)
4170 enumerator
= this->offload_interfaces
->create_enumerator(this->offload_interfaces
);
4171 while (enumerator
->enumerate(enumerator
, NULL
, &iface
))
4175 enumerator
->destroy(enumerator
);
4176 this->offload_interfaces
->destroy(this->offload_interfaces
);
4178 this->condvar
->destroy(this->condvar
);
4179 this->mutex
->destroy(this->mutex
);
4180 DESTROY_IF(this->offload_mutex
);
4185 * Get the currently configured SPD hashing thresholds for an address family
4187 static bool get_spd_hash_thresh(private_kernel_netlink_ipsec_t
*this,
4188 int type
, uint8_t *lbits
, uint8_t *rbits
)
4190 netlink_buf_t request
;
4191 struct nlmsghdr
*hdr
, *out
;
4192 struct xfrmu_spdhthresh
*thresh
;
4194 size_t len
, rtasize
;
4195 bool success
= FALSE
;
4197 memset(&request
, 0, sizeof(request
));
4200 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
4201 hdr
->nlmsg_type
= XFRM_MSG_GETSPDINFO
;
4202 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(uint32_t));
4204 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
4207 while (NLMSG_OK(hdr
, len
))
4209 switch (hdr
->nlmsg_type
)
4211 case XFRM_MSG_NEWSPDINFO
:
4213 rta
= XFRM_RTA(hdr
, uint32_t);
4214 rtasize
= XFRM_PAYLOAD(hdr
, uint32_t);
4215 while (RTA_OK(rta
, rtasize
))
4217 if (rta
->rta_type
== type
&&
4218 RTA_PAYLOAD(rta
) == sizeof(*thresh
))
4220 thresh
= RTA_DATA(rta
);
4221 *lbits
= thresh
->lbits
;
4222 *rbits
= thresh
->rbits
;
4226 rta
= RTA_NEXT(rta
, rtasize
);
4232 netlink_log_error(hdr
, "getting SPD hash threshold failed");
4236 hdr
= NLMSG_NEXT(hdr
, len
);
4249 * Configure SPD hashing threshold for an address family
4251 static void setup_spd_hash_thresh(private_kernel_netlink_ipsec_t
*this,
4252 char *key
, int type
, uint8_t def
)
4254 struct xfrmu_spdhthresh
*thresh
;
4255 struct nlmsghdr
*hdr
;
4256 netlink_buf_t request
;
4257 uint8_t lbits
, rbits
;
4259 if (!get_spd_hash_thresh(this, type
, &lbits
, &rbits
))
4263 memset(&request
, 0, sizeof(request
));
4266 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4267 hdr
->nlmsg_type
= XFRM_MSG_NEWSPDINFO
;
4268 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(uint32_t));
4270 thresh
= netlink_reserve(hdr
, sizeof(request
), type
, sizeof(*thresh
));
4271 thresh
->lbits
= lib
->settings
->get_int(lib
->settings
,
4272 "%s.plugins.kernel-netlink.spdh_thresh.%s.lbits",
4274 thresh
->rbits
= lib
->settings
->get_int(lib
->settings
,
4275 "%s.plugins.kernel-netlink.spdh_thresh.%s.rbits",
4277 if (thresh
->lbits
!= lbits
|| thresh
->rbits
!= rbits
)
4279 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
4281 DBG1(DBG_KNL
, "setting SPD hash threshold failed");
4287 * Check for kernel features (currently only via version number)
4289 static void check_kernel_features(private_kernel_netlink_ipsec_t
*this)
4291 struct utsname utsname
;
4294 if (uname(&utsname
) == 0)
4296 switch(sscanf(utsname
.release
, "%d.%d.%d", &a
, &b
, &c
))
4300 /* before 6.2 the kernel only provided the last used time for
4301 * specific outbound IPv6 SAs */
4302 this->sa_lastused
= a
> 6 || (a
== 6 && b
>= 2);
4303 /* 6.10 added support for SA direction and enforces certain
4304 * flags e.g. 0 replay window for outbound SAs */
4305 this->sa_dir
= a
> 6 || (a
== 6 && b
>= 10);
4314 * Described in header.
4316 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
4318 private_kernel_netlink_ipsec_t
*this;
4324 .get_features
= _get_features
,
4325 .get_spi
= _get_spi
,
4326 .get_cpi
= _get_cpi
,
4328 .update_sa
= _update_sa
,
4329 .query_sa
= _query_sa
,
4331 .flush_sas
= _flush_sas
,
4332 .add_policy
= _add_policy
,
4333 .query_policy
= _query_policy
,
4334 .del_policy
= _del_policy
,
4335 .flush_policies
= _flush_policies
,
4336 .bypass_socket
= _bypass_socket
,
4337 .enable_udp_decap
= _enable_udp_decap
,
4338 .destroy
= _destroy
,
4341 .policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
4342 (hashtable_equals_t
)policy_equals
, 32),
4343 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
4344 (hashtable_equals_t
)ipsec_sa_equals
, 32),
4345 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
4346 .condvar
= condvar_create(CONDVAR_TYPE_DEFAULT
),
4347 .get_priority
= dlsym(RTLD_DEFAULT
,
4348 "kernel_netlink_get_priority_custom"),
4349 .policy_update
= lib
->settings
->get_bool(lib
->settings
,
4350 "%s.plugins.kernel-netlink.policy_update",
4352 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
4353 "%s.install_routes", TRUE
, lib
->ns
),
4354 .install_routes_xfrmi
= lib
->settings
->get_bool(lib
->settings
,
4355 "%s.plugins.kernel-netlink.install_routes_xfrmi",
4357 .proto_port_transport
= lib
->settings
->get_bool(lib
->settings
,
4358 "%s.plugins.kernel-netlink.set_proto_port_transport_sa",
4360 .port_bypass
= lib
->settings
->get_bool(lib
->settings
,
4361 "%s.plugins.kernel-netlink.port_bypass", FALSE
, lib
->ns
),
4364 check_kernel_features(this);
4366 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
, xfrm_msg_names
,
4367 lib
->settings
->get_bool(lib
->settings
,
4368 "%s.plugins.kernel-netlink.parallel_xfrm", FALSE
, lib
->ns
));
4369 if (!this->socket_xfrm
)
4375 setup_spd_hash_thresh(this, "ipv4", XFRMA_SPD_IPV4_HTHRESH
, 32);
4376 setup_spd_hash_thresh(this, "ipv6", XFRMA_SPD_IPV6_HTHRESH
, 128);
4378 groups
= nl_group(XFRMNLGRP_ACQUIRE
) | nl_group(XFRMNLGRP_EXPIRE
) |
4379 nl_group(XFRMNLGRP_MIGRATE
) | nl_group(XFRMNLGRP_MAPPING
);
4380 this->socket_xfrm_events
= netlink_event_socket_create(NETLINK_XFRM
, groups
,
4381 receive_events
, this);
4382 if (!this->socket_xfrm_events
)
4388 if (netlink_find_offload_feature(lib
->settings
->get_str(lib
->settings
,
4389 "%s.plugins.kernel-netlink.hw_offload_feature_interface",
4392 this->offload_interfaces
= hashtable_create(hashtable_hash_ptr
,
4393 hashtable_equals_ptr
, 8);
4394 this->offload_mutex
= mutex_create(MUTEX_TYPE_DEFAULT
);
4395 this->socket_link_events
= netlink_event_socket_create(NETLINK_ROUTE
,
4396 nl_group(RTNLGRP_LINK
),
4397 receive_link_events
, this);
4398 if (!this->socket_link_events
||
4399 !init_offload_interfaces(this))
4406 this->xfrmi
= kernel_netlink_xfrmi_create(TRUE
);
4409 lib
->set(lib
, KERNEL_NETLINK_XFRMI_MANAGER
, this->xfrmi
);
4411 return &this->public;