2 * Copyright (C) 2006-2019 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2008-2016 Andreas Steffen
5 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
6 * Copyright (C) 2006 Daniel Roethlisberger
7 * Copyright (C) 2005 Jan Hutter
8 * HSR Hochschule fuer Technik Rapperswil
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * Copyright (C) 2018 Mellanox Technologies.
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/ioctl.h>
47 #include <linux/ipsec.h>
48 #include <linux/netlink.h>
49 #include <linux/rtnetlink.h>
50 #include <linux/xfrm.h>
51 #include <linux/udp.h>
52 #include <linux/ethtool.h>
53 #include <linux/sockios.h>
62 #include "kernel_netlink_ipsec.h"
63 #include "kernel_netlink_shared.h"
66 #include <utils/debug.h>
67 #include <threading/mutex.h>
68 #include <threading/condvar.h>
69 #include <collections/array.h>
70 #include <collections/hashtable.h>
71 #include <collections/linked_list.h>
73 /** Required for Linux 2.6.26 kernel and later */
74 #ifndef XFRM_STATE_AF_UNSPEC
75 #define XFRM_STATE_AF_UNSPEC 32
78 /** From linux/in.h */
79 #ifndef IP_XFRM_POLICY
80 #define IP_XFRM_POLICY 17
83 /** Missing on uclibc */
84 #ifndef IPV6_XFRM_POLICY
85 #define IPV6_XFRM_POLICY 34
86 #endif /*IPV6_XFRM_POLICY*/
88 /* from linux/udp.h */
93 #ifndef UDP_ENCAP_ESPINUDP
94 #define UDP_ENCAP_ESPINUDP 2
97 /* this is not defined on some platforms */
99 #define SOL_UDP IPPROTO_UDP
102 /** Base priority for installed policies */
103 #define PRIO_BASE 200000
106 * Map the limit for bytes and packets to XFRM_INF by default
108 #define XFRM_LIMIT(x) ((x) == 0 ? XFRM_INF : (x))
111 * Create ORable bitfield of XFRM NL groups
113 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
116 * Returns a pointer to the first rtattr following the nlmsghdr *nlh and the
117 * 'usual' netlink data x like 'struct xfrm_usersa_info'
119 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + \
120 NLMSG_ALIGN(sizeof(x))))
122 * Returns the total size of attached rta data
123 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
125 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
127 typedef struct kernel_algorithm_t kernel_algorithm_t
;
130 * Mapping of IKEv2 kernel identifier to linux crypto API names
132 struct kernel_algorithm_t
{
134 * Identifier specified in IKEv2
139 * Name of the algorithm in linux crypto API
144 ENUM(xfrm_msg_names
, XFRM_MSG_NEWSA
, XFRM_MSG_MAPPING
,
148 "XFRM_MSG_NEWPOLICY",
149 "XFRM_MSG_DELPOLICY",
150 "XFRM_MSG_GETPOLICY",
154 "XFRM_MSG_UPDPOLICY",
156 "XFRM_MSG_POLEXPIRE",
158 "XFRM_MSG_FLUSHPOLICY",
163 "XFRM_MSG_NEWSADINFO",
164 "XFRM_MSG_GETSADINFO",
165 "XFRM_MSG_NEWSPDINFO",
166 "XFRM_MSG_GETSPDINFO",
170 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_OFFLOAD_DEV
,
182 "XFRMA_REPLAY_THRESH",
183 "XFRMA_ETIMER_THRESH",
191 "XFRMA_ALG_AUTH_TRUNC",
194 "XFRMA_REPLAY_ESN_VAL",
195 "XFRMA_SA_EXTRA_FLAGS",
197 "XFRMA_ADDRESS_FILTER",
203 * Algorithms for encryption
205 static kernel_algorithm_t encryption_algs
[] = {
206 /* {ENCR_DES_IV64, "***" }, */
208 {ENCR_3DES
, "des3_ede" },
209 /* {ENCR_RC5, "***" }, */
210 /* {ENCR_IDEA, "***" }, */
211 {ENCR_CAST
, "cast5" },
212 {ENCR_BLOWFISH
, "blowfish" },
213 /* {ENCR_3IDEA, "***" }, */
214 /* {ENCR_DES_IV32, "***" }, */
215 {ENCR_NULL
, "cipher_null" },
216 {ENCR_AES_CBC
, "aes" },
217 {ENCR_AES_CTR
, "rfc3686(ctr(aes))" },
218 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
219 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
220 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
221 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
222 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
223 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
224 {ENCR_NULL_AUTH_AES_GMAC
, "rfc4543(gcm(aes))" },
225 {ENCR_CAMELLIA_CBC
, "cbc(camellia)" },
226 /* {ENCR_CAMELLIA_CTR, "***" }, */
227 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
228 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
229 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
230 {ENCR_SERPENT_CBC
, "serpent" },
231 {ENCR_TWOFISH_CBC
, "twofish" },
232 {ENCR_CHACHA20_POLY1305
, "rfc7539esp(chacha20,poly1305)"},
236 * Algorithms for integrity protection
238 static kernel_algorithm_t integrity_algs
[] = {
239 {AUTH_HMAC_MD5_96
, "md5" },
240 {AUTH_HMAC_MD5_128
, "hmac(md5)" },
241 {AUTH_HMAC_SHA1_96
, "sha1" },
242 {AUTH_HMAC_SHA1_160
, "hmac(sha1)" },
243 {AUTH_HMAC_SHA2_256_96
, "sha256" },
244 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
245 {AUTH_HMAC_SHA2_256_256
, "hmac(sha256)" },
246 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
247 {AUTH_HMAC_SHA2_384_384
, "hmac(sha384)" },
248 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
249 {AUTH_HMAC_SHA2_512_512
, "hmac(sha512)" },
250 /* {AUTH_DES_MAC, "***" }, */
251 /* {AUTH_KPDK_MD5, "***" }, */
252 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
253 {AUTH_AES_CMAC_96
, "cmac(aes)" },
257 * Algorithms for IPComp
259 static kernel_algorithm_t compression_algs
[] = {
260 /* {IPCOMP_OUI, "***" }, */
261 {IPCOMP_DEFLATE
, "deflate" },
262 {IPCOMP_LZS
, "lzs" },
263 {IPCOMP_LZJH
, "lzjh" },
267 * Look up a kernel algorithm name and its key size
269 static const char* lookup_algorithm(transform_type_t type
, int ikev2
)
271 kernel_algorithm_t
*list
;
277 case ENCRYPTION_ALGORITHM
:
278 list
= encryption_algs
;
279 count
= countof(encryption_algs
);
281 case INTEGRITY_ALGORITHM
:
282 list
= integrity_algs
;
283 count
= countof(integrity_algs
);
285 case COMPRESSION_ALGORITHM
:
286 list
= compression_algs
;
287 count
= countof(compression_algs
);
292 for (i
= 0; i
< count
; i
++)
294 if (list
[i
].ikev2
== ikev2
)
299 if (charon
->kernel
->lookup_algorithm(charon
->kernel
, ikev2
, type
, NULL
,
307 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
310 * Private variables and functions of kernel_netlink class.
312 struct private_kernel_netlink_ipsec_t
{
314 * Public part of the kernel_netlink_t object
316 kernel_netlink_ipsec_t
public;
319 * Mutex to lock access to installed policies
324 * Condvar to synchronize access to individual policies
329 * Hash table of installed policies (policy_entry_t)
331 hashtable_t
*policies
;
334 * Hash table of IPsec SAs using policies (ipsec_sa_t)
339 * Netlink xfrm socket (IPsec)
341 netlink_socket_t
*socket_xfrm
;
344 * Netlink xfrm socket to receive acquire and expire events
346 int socket_xfrm_events
;
349 * Whether to install routes along policies
354 * Whether to set protocol and ports on selector installed with transport
357 bool proto_port_transport
;
360 * Whether to always use UPDATE to install policies
365 * Installed port based IKE bypass policies, as bypass_t
370 * Custom priority calculation function
372 uint32_t (*get_priority
)(kernel_ipsec_policy_id_t
*id
,
373 kernel_ipsec_manage_policy_t
*data
);
376 typedef struct ipsec_sa_t ipsec_sa_t
;
379 * IPsec SA assigned to a policy.
382 /** Source address of this SA */
385 /** Destination address of this SA */
394 /** Description of this SA */
397 /** Reference count for this SA */
402 * Hash function for ipsec_sa_t objects
404 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
406 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
407 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
408 chunk_hash_inc(chunk_from_thing(sa
->mark
),
409 chunk_hash_inc(chunk_from_thing(sa
->if_id
),
410 chunk_hash(chunk_from_thing(sa
->cfg
))))));
414 * Equality function for ipsec_sa_t objects
416 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
418 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
419 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
420 sa
->mark
.value
== other_sa
->mark
.value
&&
421 sa
->mark
.mask
== other_sa
->mark
.mask
&&
422 sa
->if_id
== other_sa
->if_id
&&
423 ipsec_sa_cfg_equals(&sa
->cfg
, &other_sa
->cfg
);
427 * Allocate or reference an IPsec SA object
429 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
430 host_t
*src
, host_t
*dst
, mark_t mark
,
431 uint32_t if_id
, ipsec_sa_cfg_t
*cfg
)
433 ipsec_sa_t
*sa
, *found
;
441 found
= this->sas
->get(this->sas
, sa
);
444 sa
->src
= src
->clone(src
);
445 sa
->dst
= dst
->clone(dst
);
446 this->sas
->put(this->sas
, sa
, sa
);
453 ref_get(&sa
->refcount
);
458 * Release and destroy an IPsec SA object
460 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
463 if (ref_put(&sa
->refcount
))
465 this->sas
->remove(this->sas
, sa
);
472 typedef struct policy_sa_t policy_sa_t
;
473 typedef struct policy_sa_out_t policy_sa_out_t
;
476 * Mapping between a policy and an IPsec SA.
479 /** Priority assigned to the policy when installed with this SA */
482 /** Automatic priority assigned to the policy when installed with this SA */
483 uint32_t auto_priority
;
485 /** Type of the policy */
493 * For outbound policies we also cache the traffic selectors in order to install
496 struct policy_sa_out_t
{
497 /** Generic interface */
500 /** Source traffic selector of this policy */
501 traffic_selector_t
*src_ts
;
503 /** Destination traffic selector of this policy */
504 traffic_selector_t
*dst_ts
;
508 * Create a policy_sa(_in)_t object
510 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
511 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
512 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
513 uint32_t if_id
, ipsec_sa_cfg_t
*cfg
)
517 if (dir
== POLICY_OUT
)
519 policy_sa_out_t
*out
;
521 .src_ts
= src_ts
->clone(src_ts
),
522 .dst_ts
= dst_ts
->clone(dst_ts
),
524 policy
= &out
->generic
;
528 INIT(policy
, .priority
= 0);
531 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, if_id
, cfg
);
536 * Destroy a policy_sa(_in)_t object
538 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t dir
,
539 private_kernel_netlink_ipsec_t
*this)
541 if (dir
== POLICY_OUT
)
543 policy_sa_out_t
*out
= (policy_sa_out_t
*)policy
;
544 out
->src_ts
->destroy(out
->src_ts
);
545 out
->dst_ts
->destroy(out
->dst_ts
);
547 ipsec_sa_destroy(this, policy
->sa
);
551 CALLBACK(policy_sa_destroy_cb
, void,
552 policy_sa_t
*policy
, va_list args
)
554 private_kernel_netlink_ipsec_t
*this;
557 VA_ARGS_VGET(args
, dir
, this);
558 policy_sa_destroy(policy
, dir
, this);
561 typedef struct policy_entry_t policy_entry_t
;
564 * Installed kernel policy.
566 struct policy_entry_t
{
568 /** Direction of this policy: in, out, forward */
571 /** Parameters of installed policy */
572 struct xfrm_selector sel
;
577 /** Optional interface ID */
580 /** Associated route installed for this policy */
581 route_entry_t
*route
;
583 /** List of SAs this policy is used by, ordered by priority */
584 linked_list_t
*used_by
;
586 /** reqid for this policy */
589 /** Number of threads waiting to work on this policy */
592 /** TRUE if a thread is working on this policy */
597 * Destroy a policy_entry_t object
599 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
600 policy_entry_t
*policy
)
604 route_entry_destroy(policy
->route
);
608 policy
->used_by
->invoke_function(policy
->used_by
, policy_sa_destroy_cb
,
609 policy
->direction
, this);
610 policy
->used_by
->destroy(policy
->used_by
);
616 * Hash function for policy_entry_t objects
618 static u_int
policy_hash(policy_entry_t
*key
)
620 chunk_t chunk
= chunk_from_thing(key
->sel
);
621 return chunk_hash_inc(chunk
, chunk_hash_inc(chunk_from_thing(key
->mark
),
622 chunk_hash(chunk_from_thing(key
->if_id
))));
626 * Equality function for policy_entry_t objects
628 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
630 return memeq(&key
->sel
, &other_key
->sel
, sizeof(struct xfrm_selector
)) &&
631 key
->mark
== other_key
->mark
&&
632 key
->if_id
== other_key
->if_id
&&
633 key
->direction
== other_key
->direction
;
637 * Determine number of set bits in 16 bit port mask
639 static inline uint32_t port_mask_bits(uint16_t port_mask
)
642 uint16_t bit_mask
= 0x8000;
644 port_mask
= ntohs(port_mask
);
646 for (bits
= 0; bits
< 16; bits
++)
648 if (!(port_mask
& bit_mask
))
658 * Calculate the priority of a policy
660 * bits 0-0: separate trap and regular policies (0..1) 1 bit
661 * bits 1-1: restriction to network interface (0..1) 1 bit
662 * bits 2-7: src + dst port mask bits (2 * 0..16) 6 bits
663 * bits 8-8: restriction to protocol (0..1) 1 bit
664 * bits 9-17: src + dst network mask bits (2 * 0..128) 9 bits
667 * smallest value: 000000000 0 000000 0 0: 0, lowest priority = 200'000
668 * largest value : 100000000 1 100000 1 1: 131'459, highst priority = 68'541
670 static uint32_t get_priority(policy_entry_t
*policy
, policy_priority_t prio
,
673 uint32_t priority
= PRIO_BASE
, sport_mask_bits
, dport_mask_bits
;
677 case POLICY_PRIORITY_FALLBACK
:
678 priority
+= PRIO_BASE
;
679 /* fall-through to next case */
680 case POLICY_PRIORITY_ROUTED
:
681 case POLICY_PRIORITY_DEFAULT
:
682 priority
+= PRIO_BASE
;
683 /* fall-through to next case */
684 case POLICY_PRIORITY_PASS
:
687 sport_mask_bits
= port_mask_bits(policy
->sel
.sport_mask
);
688 dport_mask_bits
= port_mask_bits(policy
->sel
.dport_mask
);
690 /* calculate priority */
691 priority
-= (policy
->sel
.prefixlen_s
+ policy
->sel
.prefixlen_d
) * 512;
692 priority
-= policy
->sel
.proto ?
256 : 0;
693 priority
-= (sport_mask_bits
+ dport_mask_bits
) * 4;
694 priority
-= (interface
!= NULL
) * 2;
695 priority
-= (prio
!= POLICY_PRIORITY_ROUTED
);
701 * Convert the general ipsec mode to the one defined in xfrm.h
703 static uint8_t mode2kernel(ipsec_mode_t mode
)
708 return XFRM_MODE_TRANSPORT
;
710 return XFRM_MODE_TUNNEL
;
712 return XFRM_MODE_BEET
;
719 * Convert a host_t to a struct xfrm_address
721 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
723 chunk_t chunk
= host
->get_address(host
);
724 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
728 * Convert a struct xfrm_address to a host_t
730 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, uint16_t port
)
737 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
740 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
745 return host_create_from_chunk(family
, chunk
, ntohs(port
));
749 * Convert a traffic selector address range to subnet and its mask.
751 static void ts2subnet(traffic_selector_t
* ts
,
752 xfrm_address_t
*net
, uint8_t *mask
)
757 ts
->to_subnet(ts
, &net_host
, mask
);
758 net_chunk
= net_host
->get_address(net_host
);
759 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
760 net_host
->destroy(net_host
);
764 * Convert a traffic selector port range to port/portmask
766 static void ts2ports(traffic_selector_t
* ts
,
767 uint16_t *port
, uint16_t *mask
)
769 uint16_t from
, to
, bitmask
;
772 from
= ts
->get_from_port(ts
);
773 to
= ts
->get_to_port(ts
);
775 /* Quick check for a single port */
783 /* Compute the port mask for port ranges */
786 for (bit
= 15; bit
>= 0; bit
--)
790 if ((bitmask
& from
) != (bitmask
& to
))
792 *port
= htons(from
& *mask
);
793 *mask
= htons(*mask
);
803 * Convert a pair of traffic_selectors to an xfrm_selector
805 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
806 traffic_selector_t
*dst
,
809 struct xfrm_selector sel
;
812 memset(&sel
, 0, sizeof(sel
));
813 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
814 /* src or dest proto may be "any" (0), use more restrictive one */
815 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
816 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
817 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
818 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
819 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
820 if ((sel
.proto
== IPPROTO_ICMP
|| sel
.proto
== IPPROTO_ICMPV6
) &&
821 (sel
.dport
|| sel
.sport
))
823 /* the kernel expects the ICMP type and code in the source and
824 * destination port fields, respectively. */
825 port
= ntohs(max(sel
.dport
, sel
.sport
));
826 sel
.sport
= htons(traffic_selector_icmp_type(port
));
827 sel
.sport_mask
= sel
.sport ?
~0 : 0;
828 sel
.dport
= htons(traffic_selector_icmp_code(port
));
829 sel
.dport_mask
= sel
.dport ?
~0 : 0;
831 sel
.ifindex
= interface ?
if_nametoindex(interface
) : 0;
838 * Convert an xfrm_selector to a src|dst traffic_selector
840 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
849 addr
= (u_char
*)&sel
->saddr
;
850 prefixlen
= sel
->prefixlen_s
;
853 port
= ntohs(sel
->sport
);
858 addr
= (u_char
*)&sel
->daddr
;
859 prefixlen
= sel
->prefixlen_d
;
862 port
= ntohs(sel
->dport
);
865 if (sel
->proto
== IPPROTO_ICMP
|| sel
->proto
== IPPROTO_ICMPV6
)
866 { /* convert ICMP[v6] message type and code as supplied by the kernel in
867 * source and destination ports (both in network order) */
868 port
= (sel
->sport
>> 8) | (sel
->dport
& 0xff00);
871 /* The Linux 2.6 kernel does not set the selector's family field,
872 * so as a kludge we additionally test the prefix length.
874 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
876 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
878 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
880 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
885 return traffic_selector_create_from_subnet(host
, prefixlen
,
886 sel
->proto
, port
, port ?
: 65535);
892 * Process a XFRM_MSG_ACQUIRE from kernel
894 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
895 struct nlmsghdr
*hdr
)
897 struct xfrm_user_acquire
*acquire
;
900 traffic_selector_t
*src_ts
, *dst_ts
;
904 acquire
= NLMSG_DATA(hdr
);
905 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
906 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
908 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
910 while (RTA_OK(rta
, rtasize
))
912 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
914 if (rta
->rta_type
== XFRMA_TMPL
)
916 struct xfrm_user_tmpl
* tmpl
;
917 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
919 proto
= tmpl
->id
.proto
;
921 rta
= RTA_NEXT(rta
, rtasize
);
930 /* acquire for AH/ESP only, not for IPCOMP */
933 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
934 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
936 charon
->kernel
->acquire(charon
->kernel
, reqid
, src_ts
, dst_ts
);
940 * Process a XFRM_MSG_EXPIRE from kernel
942 static void process_expire(private_kernel_netlink_ipsec_t
*this,
943 struct nlmsghdr
*hdr
)
945 struct xfrm_user_expire
*expire
;
950 expire
= NLMSG_DATA(hdr
);
951 protocol
= expire
->state
.id
.proto
;
952 spi
= expire
->state
.id
.spi
;
954 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
956 if (protocol
== IPPROTO_ESP
|| protocol
== IPPROTO_AH
)
958 dst
= xfrm2host(expire
->state
.family
, &expire
->state
.id
.daddr
, 0);
961 charon
->kernel
->expire(charon
->kernel
, protocol
, spi
, dst
,
969 * Process a XFRM_MSG_MIGRATE from kernel
971 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
972 struct nlmsghdr
*hdr
)
974 struct xfrm_userpolicy_id
*policy_id
;
977 traffic_selector_t
*src_ts
, *dst_ts
;
978 host_t
*local
= NULL
, *remote
= NULL
;
979 host_t
*old_src
= NULL
, *old_dst
= NULL
;
980 host_t
*new_src
= NULL
, *new_dst
= NULL
;
984 policy_id
= NLMSG_DATA(hdr
);
985 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
986 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
988 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
990 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
991 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
992 dir
= (policy_dir_t
)policy_id
->dir
;
994 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
996 while (RTA_OK(rta
, rtasize
))
998 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
999 if (rta
->rta_type
== XFRMA_KMADDRESS
)
1001 struct xfrm_user_kmaddress
*kmaddress
;
1003 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
1004 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
1005 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
1006 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
1008 else if (rta
->rta_type
== XFRMA_MIGRATE
)
1010 struct xfrm_user_migrate
*migrate
;
1012 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
1013 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
1014 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
1015 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
1016 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
1017 reqid
= migrate
->reqid
;
1018 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
1019 old_src
, old_dst
, new_src
, new_dst
, reqid
);
1020 DESTROY_IF(old_src
);
1021 DESTROY_IF(old_dst
);
1022 DESTROY_IF(new_src
);
1023 DESTROY_IF(new_dst
);
1025 rta
= RTA_NEXT(rta
, rtasize
);
1028 if (src_ts
&& dst_ts
&& local
&& remote
)
1030 charon
->kernel
->migrate(charon
->kernel
, reqid
, src_ts
, dst_ts
, dir
,
1043 * Process a XFRM_MSG_MAPPING from kernel
1045 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
1046 struct nlmsghdr
*hdr
)
1048 struct xfrm_user_mapping
*mapping
;
1051 mapping
= NLMSG_DATA(hdr
);
1052 spi
= mapping
->id
.spi
;
1054 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
1056 if (mapping
->id
.proto
== IPPROTO_ESP
)
1060 dst
= xfrm2host(mapping
->id
.family
, &mapping
->id
.daddr
, 0);
1063 new = xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
1064 mapping
->new_sport
);
1067 charon
->kernel
->mapping(charon
->kernel
, IPPROTO_ESP
, spi
, dst
,
1077 * Receives events from kernel
1079 static bool receive_events(private_kernel_netlink_ipsec_t
*this, int fd
,
1080 watcher_event_t event
)
1082 char response
[netlink_get_buflen()];
1083 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
1084 struct sockaddr_nl addr
;
1085 socklen_t addr_len
= sizeof(addr
);
1088 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
),
1089 MSG_DONTWAIT
, (struct sockaddr
*)&addr
, &addr_len
);
1095 /* interrupted, try again */
1098 /* no data ready, select again */
1101 DBG1(DBG_KNL
, "unable to receive from XFRM event socket: %s "
1102 "(%d)", strerror(errno
), errno
);
1108 if (addr
.nl_pid
!= 0)
1109 { /* not from kernel. not interested, try another one */
1113 while (NLMSG_OK(hdr
, len
))
1115 switch (hdr
->nlmsg_type
)
1117 case XFRM_MSG_ACQUIRE
:
1118 process_acquire(this, hdr
);
1120 case XFRM_MSG_EXPIRE
:
1121 process_expire(this, hdr
);
1123 case XFRM_MSG_MIGRATE
:
1124 process_migrate(this, hdr
);
1126 case XFRM_MSG_MAPPING
:
1127 process_mapping(this, hdr
);
1130 DBG1(DBG_KNL
, "received unknown event from XFRM event "
1131 "socket: %d", hdr
->nlmsg_type
);
1134 hdr
= NLMSG_NEXT(hdr
, len
);
1139 METHOD(kernel_ipsec_t
, get_features
, kernel_feature_t
,
1140 private_kernel_netlink_ipsec_t
*this)
1142 return KERNEL_ESP_V3_TFC
| KERNEL_POLICY_SPI
;
1146 * Get an SPI for a specific protocol from the kernel.
1148 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
1149 host_t
*src
, host_t
*dst
, uint8_t proto
, uint32_t min
, uint32_t max
,
1152 netlink_buf_t request
;
1153 struct nlmsghdr
*hdr
, *out
;
1154 struct xfrm_userspi_info
*userspi
;
1155 uint32_t received_spi
= 0;
1158 memset(&request
, 0, sizeof(request
));
1161 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1162 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1163 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1165 userspi
= NLMSG_DATA(hdr
);
1166 host2xfrm(src
, &userspi
->info
.saddr
);
1167 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1168 userspi
->info
.id
.proto
= proto
;
1169 userspi
->info
.mode
= XFRM_MODE_TUNNEL
;
1170 userspi
->info
.family
= src
->get_family(src
);
1174 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1177 while (NLMSG_OK(hdr
, len
))
1179 switch (hdr
->nlmsg_type
)
1181 case XFRM_MSG_NEWSA
:
1183 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1184 received_spi
= usersa
->id
.spi
;
1189 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1190 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1191 strerror(-err
->error
), -err
->error
);
1195 hdr
= NLMSG_NEXT(hdr
, len
);
1205 if (received_spi
== 0)
1210 *spi
= received_spi
;
1214 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1215 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1216 uint8_t protocol
, uint32_t *spi
)
1218 uint32_t spi_min
, spi_max
;
1220 spi_min
= lib
->settings
->get_int(lib
->settings
, "%s.spi_min",
1221 KERNEL_SPI_MIN
, lib
->ns
);
1222 spi_max
= lib
->settings
->get_int(lib
->settings
, "%s.spi_max",
1223 KERNEL_SPI_MAX
, lib
->ns
);
1225 if (get_spi_internal(this, src
, dst
, protocol
, min(spi_min
, spi_max
),
1226 max(spi_min
, spi_max
), spi
) != SUCCESS
)
1228 DBG1(DBG_KNL
, "unable to get SPI");
1232 DBG2(DBG_KNL
, "got SPI %.8x", ntohl(*spi
));
1236 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1237 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1240 uint32_t received_spi
= 0;
1242 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1243 0x100, 0xEFFF, &received_spi
) != SUCCESS
)
1245 DBG1(DBG_KNL
, "unable to get CPI");
1249 *cpi
= htons((uint16_t)ntohl(received_spi
));
1251 DBG2(DBG_KNL
, "got CPI %.4x", ntohs(*cpi
));
1256 * Format the mark for debug messages
1258 static void format_mark(char *buf
, int buflen
, mark_t mark
)
1260 if (mark
.value
| mark
.mask
)
1262 snprintf(buf
, buflen
, " (mark %u/0x%08x)", mark
.value
, mark
.mask
);
1267 * Add a XFRM mark to message if required
1269 static bool add_mark(struct nlmsghdr
*hdr
, int buflen
, mark_t mark
)
1271 if (mark
.value
| mark
.mask
)
1273 struct xfrm_mark
*xmrk
;
1275 xmrk
= netlink_reserve(hdr
, buflen
, XFRMA_MARK
, sizeof(*xmrk
));
1280 xmrk
->v
= mark
.value
;
1281 xmrk
->m
= mark
.mask
;
1287 * Add a uint32 attribute to message
1289 static bool add_uint32(struct nlmsghdr
*hdr
, int buflen
,
1290 enum xfrm_attr_type_t type
, uint32_t value
)
1294 xvalue
= netlink_reserve(hdr
, buflen
, type
, sizeof(*xvalue
));
1303 /* ETHTOOL_GSSET_INFO is available since 2.6.34 and ETH_SS_FEATURES (enum) and
1304 * ETHTOOL_GFEATURES since 2.6.39, so check for the latter */
1305 #ifdef ETHTOOL_GFEATURES
1308 * Global metadata used for IPsec HW offload
1311 /** determined HW offload support */
1313 /** bit in feature set */
1315 /** total number of device feature blocks */
1317 } netlink_hw_offload
;
1320 * Check if kernel supports HW offload and determine feature flag
1322 static void netlink_find_offload_feature(const char *ifname
)
1324 struct ethtool_sset_info
*sset_info
;
1325 struct ethtool_gstrings
*cmd
= NULL
;
1326 struct ifreq ifr
= { 0 };
1327 uint32_t sset_len
, i
;
1329 int err
, query_socket
;
1331 query_socket
= socket(AF_NETLINK
, SOCK_DGRAM
, NETLINK_XFRM
);
1332 if (query_socket
< 0)
1337 /* determine number of device features */
1338 INIT_EXTRA(sset_info
, sizeof(uint32_t),
1339 .cmd
= ETHTOOL_GSSET_INFO
,
1340 .sset_mask
= 1ULL << ETH_SS_FEATURES
,
1342 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1343 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1344 ifr
.ifr_data
= (void*)sset_info
;
1346 err
= ioctl(query_socket
, SIOCETHTOOL
, &ifr
);
1347 if (err
|| sset_info
->sset_mask
!= 1ULL << ETH_SS_FEATURES
)
1351 sset_len
= sset_info
->data
[0];
1353 /* retrieve names of device features */
1354 INIT_EXTRA(cmd
, ETH_GSTRING_LEN
* sset_len
,
1355 .cmd
= ETHTOOL_GSTRINGS
,
1356 .string_set
= ETH_SS_FEATURES
,
1358 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1359 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1360 ifr
.ifr_data
= (void*)cmd
;
1362 err
= ioctl(query_socket
, SIOCETHTOOL
, &ifr
);
1368 /* look for the ESP_HW feature bit */
1369 str
= (char*)cmd
->data
;
1370 for (i
= 0; i
< cmd
->len
; i
++)
1372 if (strneq(str
, "esp-hw-offload", ETH_GSTRING_LEN
))
1374 netlink_hw_offload
.supported
= TRUE
;
1375 netlink_hw_offload
.bit
= i
;
1376 netlink_hw_offload
.total_blocks
= (sset_len
+ 31) / 32;
1379 str
+= ETH_GSTRING_LEN
;
1385 close(query_socket
);
1389 * Check if interface supported HW offload
1391 static bool netlink_detect_offload(const char *ifname
)
1393 struct ethtool_gfeatures
*cmd
;
1394 uint32_t feature_bit
;
1395 struct ifreq ifr
= { 0 };
1400 if (!netlink_hw_offload
.supported
)
1402 DBG1(DBG_KNL
, "HW offload is not supported by kernel");
1406 query_socket
= socket(AF_NETLINK
, SOCK_DGRAM
, NETLINK_XFRM
);
1407 if (query_socket
< 0)
1412 /* feature is supported by kernel, query device features */
1413 INIT_EXTRA(cmd
, sizeof(cmd
->features
[0]) * netlink_hw_offload
.total_blocks
,
1414 .cmd
= ETHTOOL_GFEATURES
,
1415 .size
= netlink_hw_offload
.total_blocks
,
1417 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1418 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1419 ifr
.ifr_data
= (void*)cmd
;
1421 if (!ioctl(query_socket
, SIOCETHTOOL
, &ifr
))
1423 block
= netlink_hw_offload
.bit
/ 32;
1424 feature_bit
= 1U << (netlink_hw_offload
.bit
% 32);
1425 if (cmd
->features
[block
].active
& feature_bit
)
1433 DBG1(DBG_KNL
, "HW offload is not supported by device");
1436 close(query_socket
);
1442 static void netlink_find_offload_feature(const char *ifname
)
1446 static bool netlink_detect_offload(const char *ifname
)
1454 * There are 3 HW offload configuration values:
1455 * 1. HW_OFFLOAD_NO : Do not configure HW offload.
1456 * 2. HW_OFFLOAD_YES : Configure HW offload.
1457 * Fail SA addition if offload is not supported.
1458 * 3. HW_OFFLOAD_AUTO : Configure HW offload if supported by the kernel
1460 * Do not fail SA addition otherwise.
1462 static bool config_hw_offload(kernel_ipsec_sa_id_t
*id
,
1463 kernel_ipsec_add_sa_t
*data
, struct nlmsghdr
*hdr
,
1466 host_t
*local
= data
->inbound ? id
->dst
: id
->src
;
1467 struct xfrm_user_offload
*offload
;
1468 bool hw_offload_yes
, ret
= FALSE
;
1471 /* do Ipsec configuration without offload */
1472 if (data
->hw_offload
== HW_OFFLOAD_NO
)
1477 hw_offload_yes
= (data
->hw_offload
== HW_OFFLOAD_YES
);
1479 if (!charon
->kernel
->get_interface(charon
->kernel
, local
, &ifname
))
1481 return !hw_offload_yes
;
1484 /* check if interface supports hw_offload */
1485 if (!netlink_detect_offload(ifname
))
1487 ret
= !hw_offload_yes
;
1491 /* activate HW offload */
1492 offload
= netlink_reserve(hdr
, buflen
,
1493 XFRMA_OFFLOAD_DEV
, sizeof(*offload
));
1496 ret
= !hw_offload_yes
;
1499 offload
->ifindex
= if_nametoindex(ifname
);
1500 if (local
->get_family(local
) == AF_INET6
)
1502 offload
->flags
|= XFRM_OFFLOAD_IPV6
;
1504 offload
->flags
|= data
->inbound ? XFRM_OFFLOAD_INBOUND
: 0;
1513 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1514 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1515 kernel_ipsec_add_sa_t
*data
)
1517 netlink_buf_t request
;
1518 const char *alg_name
;
1519 char markstr
[32] = "";
1520 struct nlmsghdr
*hdr
;
1521 struct xfrm_usersa_info
*sa
;
1522 uint16_t icv_size
= 64, ipcomp
= data
->ipcomp
;
1523 ipsec_mode_t mode
= data
->mode
, original_mode
= data
->mode
;
1524 traffic_selector_t
*first_src_ts
, *first_dst_ts
;
1525 status_t status
= FAILED
;
1527 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1528 * we are in the recursive call below */
1529 if (ipcomp
!= IPCOMP_NONE
&& data
->cpi
!= 0)
1531 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1532 kernel_ipsec_sa_id_t ipcomp_id
= {
1535 .spi
= htonl(ntohs(data
->cpi
)),
1536 .proto
= IPPROTO_COMP
,
1540 kernel_ipsec_add_sa_t ipcomp_sa
= {
1541 .reqid
= data
->reqid
,
1543 .src_ts
= data
->src_ts
,
1544 .dst_ts
= data
->dst_ts
,
1546 .enc_alg
= ENCR_UNDEFINED
,
1547 .int_alg
= AUTH_UNDEFINED
,
1549 .ipcomp
= data
->ipcomp
,
1550 .initiator
= data
->initiator
,
1551 .inbound
= data
->inbound
,
1552 .update
= data
->update
,
1554 add_sa(this, &ipcomp_id
, &ipcomp_sa
);
1555 ipcomp
= IPCOMP_NONE
;
1556 /* use transport mode ESP SA, IPComp uses tunnel mode */
1557 mode
= MODE_TRANSPORT
;
1560 memset(&request
, 0, sizeof(request
));
1561 format_mark(markstr
, sizeof(markstr
), id
->mark
);
1563 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}%s",
1564 ntohl(id
->spi
), data
->reqid
, markstr
);
1567 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1568 hdr
->nlmsg_type
= data
->update ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1569 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1571 sa
= NLMSG_DATA(hdr
);
1572 host2xfrm(id
->src
, &sa
->saddr
);
1573 host2xfrm(id
->dst
, &sa
->id
.daddr
);
1574 sa
->id
.spi
= id
->spi
;
1575 sa
->id
.proto
= id
->proto
;
1576 sa
->family
= id
->src
->get_family(id
->src
);
1577 sa
->mode
= mode2kernel(mode
);
1581 sa
->flags
|= XFRM_STATE_NOPMTUDISC
;
1584 if (!data
->copy_ecn
)
1586 sa
->flags
|= XFRM_STATE_NOECN
;
1591 switch (data
->copy_dscp
)
1594 case DSCP_COPY_IN_ONLY
:
1595 sa
->flags
|= XFRM_STATE_DECAP_DSCP
;
1603 switch (data
->copy_dscp
)
1605 case DSCP_COPY_IN_ONLY
:
1608 /* currently the only extra flag */
1609 if (!add_uint32(hdr
, sizeof(request
), XFRMA_SA_EXTRA_FLAGS
,
1610 XFRM_SA_XFLAG_DONT_ENCAP_DSCP
))
1624 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1627 case MODE_TRANSPORT
:
1628 if (original_mode
== MODE_TUNNEL
)
1629 { /* don't install selectors for switched SAs. because only one
1630 * selector can be installed other traffic would get dropped */
1633 if (data
->src_ts
->get_first(data
->src_ts
,
1634 (void**)&first_src_ts
) == SUCCESS
&&
1635 data
->dst_ts
->get_first(data
->dst_ts
,
1636 (void**)&first_dst_ts
) == SUCCESS
)
1638 sa
->sel
= ts2selector(first_src_ts
, first_dst_ts
,
1640 if (!this->proto_port_transport
)
1642 /* don't install proto/port on SA. This would break
1643 * potential secondary SAs for the same address using a
1644 * different prot/port. */
1646 sa
->sel
.dport
= sa
->sel
.dport_mask
= 0;
1647 sa
->sel
.sport
= sa
->sel
.sport_mask
= 0;
1654 if (id
->proto
== IPPROTO_AH
&& sa
->family
== AF_INET
)
1655 { /* use alignment to 4 bytes for IPv4 instead of the incorrect 8 byte
1656 * alignment that's used by default but is only valid for IPv6 */
1657 sa
->flags
|= XFRM_STATE_ALIGN4
;
1660 sa
->reqid
= data
->reqid
;
1661 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(data
->lifetime
->bytes
.rekey
);
1662 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(data
->lifetime
->bytes
.life
);
1663 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(data
->lifetime
->packets
.rekey
);
1664 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(data
->lifetime
->packets
.life
);
1665 /* we use lifetimes since added, not since used */
1666 sa
->lft
.soft_add_expires_seconds
= data
->lifetime
->time
.rekey
;
1667 sa
->lft
.hard_add_expires_seconds
= data
->lifetime
->time
.life
;
1668 sa
->lft
.soft_use_expires_seconds
= 0;
1669 sa
->lft
.hard_use_expires_seconds
= 0;
1671 switch (data
->enc_alg
)
1673 case ENCR_UNDEFINED
:
1676 case ENCR_AES_CCM_ICV16
:
1677 case ENCR_AES_GCM_ICV16
:
1678 case ENCR_NULL_AUTH_AES_GMAC
:
1679 case ENCR_CAMELLIA_CCM_ICV16
:
1680 case ENCR_CHACHA20_POLY1305
:
1683 case ENCR_AES_CCM_ICV12
:
1684 case ENCR_AES_GCM_ICV12
:
1685 case ENCR_CAMELLIA_CCM_ICV12
:
1688 case ENCR_AES_CCM_ICV8
:
1689 case ENCR_AES_GCM_ICV8
:
1690 case ENCR_CAMELLIA_CCM_ICV8
:
1692 struct xfrm_algo_aead
*algo
;
1694 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, data
->enc_alg
);
1695 if (alg_name
== NULL
)
1697 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1698 encryption_algorithm_names
, data
->enc_alg
);
1701 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1702 encryption_algorithm_names
, data
->enc_alg
,
1703 data
->enc_key
.len
* 8);
1705 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AEAD
,
1706 sizeof(*algo
) + data
->enc_key
.len
);
1711 algo
->alg_key_len
= data
->enc_key
.len
* 8;
1712 algo
->alg_icv_len
= icv_size
;
1713 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1714 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1715 memcpy(algo
->alg_key
, data
->enc_key
.ptr
, data
->enc_key
.len
);
1720 struct xfrm_algo
*algo
;
1722 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, data
->enc_alg
);
1723 if (alg_name
== NULL
)
1725 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1726 encryption_algorithm_names
, data
->enc_alg
);
1729 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1730 encryption_algorithm_names
, data
->enc_alg
,
1731 data
->enc_key
.len
* 8);
1733 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_CRYPT
,
1734 sizeof(*algo
) + data
->enc_key
.len
);
1739 algo
->alg_key_len
= data
->enc_key
.len
* 8;
1740 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1741 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1742 memcpy(algo
->alg_key
, data
->enc_key
.ptr
, data
->enc_key
.len
);
1746 if (data
->int_alg
!= AUTH_UNDEFINED
)
1748 u_int trunc_len
= 0;
1750 alg_name
= lookup_algorithm(INTEGRITY_ALGORITHM
, data
->int_alg
);
1751 if (alg_name
== NULL
)
1753 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1754 integrity_algorithm_names
, data
->int_alg
);
1757 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1758 integrity_algorithm_names
, data
->int_alg
, data
->int_key
.len
* 8);
1760 switch (data
->int_alg
)
1762 case AUTH_HMAC_MD5_128
:
1763 case AUTH_HMAC_SHA2_256_128
:
1766 case AUTH_HMAC_SHA1_160
:
1769 case AUTH_HMAC_SHA2_256_256
:
1772 case AUTH_HMAC_SHA2_384_384
:
1775 case AUTH_HMAC_SHA2_512_512
:
1784 struct xfrm_algo_auth
* algo
;
1786 /* the kernel uses SHA256 with 96 bit truncation by default,
1787 * use specified truncation size supported by newer kernels.
1788 * also use this for untruncated MD5, SHA1 and SHA2. */
1789 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH_TRUNC
,
1790 sizeof(*algo
) + data
->int_key
.len
);
1795 algo
->alg_key_len
= data
->int_key
.len
* 8;
1796 algo
->alg_trunc_len
= trunc_len
;
1797 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1798 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1799 memcpy(algo
->alg_key
, data
->int_key
.ptr
, data
->int_key
.len
);
1803 struct xfrm_algo
* algo
;
1805 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH
,
1806 sizeof(*algo
) + data
->int_key
.len
);
1811 algo
->alg_key_len
= data
->int_key
.len
* 8;
1812 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1813 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1814 memcpy(algo
->alg_key
, data
->int_key
.ptr
, data
->int_key
.len
);
1818 if (ipcomp
!= IPCOMP_NONE
)
1820 struct xfrm_algo
* algo
;
1822 alg_name
= lookup_algorithm(COMPRESSION_ALGORITHM
, ipcomp
);
1823 if (alg_name
== NULL
)
1825 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1826 ipcomp_transform_names
, ipcomp
);
1829 DBG2(DBG_KNL
, " using compression algorithm %N",
1830 ipcomp_transform_names
, ipcomp
);
1832 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_COMP
,
1838 algo
->alg_key_len
= 0;
1839 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1840 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1845 struct xfrm_encap_tmpl
*tmpl
;
1847 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
, sizeof(*tmpl
));
1852 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1853 tmpl
->encap_sport
= htons(id
->src
->get_port(id
->src
));
1854 tmpl
->encap_dport
= htons(id
->dst
->get_port(id
->dst
));
1855 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1856 /* encap_oa could probably be derived from the
1857 * traffic selectors [rfc4306, p39]. In the netlink kernel
1858 * implementation pluto does the same as we do here but it uses
1859 * encap_oa in the pfkey implementation.
1860 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1862 * -> does that mean that NAT-T encap doesn't work in transport mode?
1863 * No. The reason the kernel ignores NAT-OA is that it recomputes
1864 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1865 * checks it marks them "checksum ok" so OA isn't needed. */
1868 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
1873 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
1878 if (ipcomp
== IPCOMP_NONE
&& (data
->mark
.value
| data
->mark
.mask
))
1880 if (!add_uint32(hdr
, sizeof(request
), XFRMA_SET_MARK
,
1881 data
->mark
.value
) ||
1882 !add_uint32(hdr
, sizeof(request
), XFRMA_SET_MARK_MASK
,
1889 if (data
->tfc
&& id
->proto
== IPPROTO_ESP
&& mode
== MODE_TUNNEL
)
1890 { /* the kernel supports TFC padding only for tunnel mode ESP SAs */
1891 if (!add_uint32(hdr
, sizeof(request
), XFRMA_TFCPAD
, data
->tfc
))
1897 if (id
->proto
!= IPPROTO_COMP
)
1899 /* generally, we don't need a replay window for outbound SAs, however,
1900 * when using ESN the kernel rejects the attribute if it is 0 */
1901 if (!data
->inbound
&& data
->replay_window
)
1903 data
->replay_window
= data
->esn ?
1 : 0;
1905 if (data
->replay_window
!= 0 && (data
->esn
|| data
->replay_window
> 32))
1907 /* for ESN or larger replay windows we need the new
1908 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1909 struct xfrm_replay_state_esn
*replay
;
1912 bmp_size
= round_up(data
->replay_window
, sizeof(uint32_t) * 8) / 8;
1913 replay
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
1914 sizeof(*replay
) + bmp_size
);
1919 /* bmp_len contains number uf __u32's */
1920 replay
->bmp_len
= bmp_size
/ sizeof(uint32_t);
1921 replay
->replay_window
= data
->replay_window
;
1922 DBG2(DBG_KNL
, " using replay window of %u packets",
1923 data
->replay_window
);
1927 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1928 sa
->flags
|= XFRM_STATE_ESN
;
1933 DBG2(DBG_KNL
, " using replay window of %u packets",
1934 data
->replay_window
);
1935 sa
->replay_window
= data
->replay_window
;
1938 DBG2(DBG_KNL
, " HW offload: %N", hw_offload_names
, data
->hw_offload
);
1939 if (!config_hw_offload(id
, data
, hdr
, sizeof(request
)))
1941 DBG1(DBG_KNL
, "failed to configure HW offload");
1946 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
1947 if (status
== NOT_FOUND
&& data
->update
)
1949 DBG1(DBG_KNL
, "allocated SPI not found anymore, try to add SAD entry");
1950 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1951 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
1954 if (status
!= SUCCESS
)
1956 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x%s (%N)", ntohl(id
->spi
),
1957 markstr
, status_names
, status
);
1965 memwipe(&request
, sizeof(request
));
1970 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1972 * Allocates into one the replay state structure we get from the kernel.
1974 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
1975 kernel_ipsec_sa_id_t
*sa
,
1976 struct xfrm_replay_state_esn
**replay_esn
,
1977 uint32_t *replay_esn_len
,
1978 struct xfrm_replay_state
**replay
,
1979 struct xfrm_lifetime_cur
**lifetime
)
1981 netlink_buf_t request
;
1982 struct nlmsghdr
*hdr
, *out
= NULL
;
1983 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1988 memset(&request
, 0, sizeof(request
));
1990 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
1994 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1995 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1996 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1998 aevent_id
= NLMSG_DATA(hdr
);
1999 aevent_id
->flags
= XFRM_AE_RVAL
;
2001 host2xfrm(sa
->dst
, &aevent_id
->sa_id
.daddr
);
2002 aevent_id
->sa_id
.spi
= sa
->spi
;
2003 aevent_id
->sa_id
.proto
= sa
->proto
;
2004 aevent_id
->sa_id
.family
= sa
->dst
->get_family(sa
->dst
);
2006 if (!add_mark(hdr
, sizeof(request
), sa
->mark
))
2010 if (sa
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, sa
->if_id
))
2015 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2018 while (NLMSG_OK(hdr
, len
))
2020 switch (hdr
->nlmsg_type
)
2022 case XFRM_MSG_NEWAE
:
2024 out_aevent
= NLMSG_DATA(hdr
);
2029 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2030 DBG1(DBG_KNL
, "querying replay state from SAD entry "
2031 "failed: %s (%d)", strerror(-err
->error
), -err
->error
);
2035 hdr
= NLMSG_NEXT(hdr
, len
);
2046 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
2047 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
2048 while (RTA_OK(rta
, rtasize
))
2050 if (rta
->rta_type
== XFRMA_LTIME_VAL
&&
2051 RTA_PAYLOAD(rta
) == sizeof(**lifetime
))
2054 *lifetime
= malloc(RTA_PAYLOAD(rta
));
2055 memcpy(*lifetime
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2057 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
2058 RTA_PAYLOAD(rta
) == sizeof(**replay
))
2061 *replay
= malloc(RTA_PAYLOAD(rta
));
2062 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2064 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
&&
2065 RTA_PAYLOAD(rta
) >= sizeof(**replay_esn
))
2068 *replay_esn
= malloc(RTA_PAYLOAD(rta
));
2069 *replay_esn_len
= RTA_PAYLOAD(rta
);
2070 memcpy(*replay_esn
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
2072 rta
= RTA_NEXT(rta
, rtasize
);
2078 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
2079 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2080 kernel_ipsec_query_sa_t
*data
, uint64_t *bytes
, uint64_t *packets
,
2083 netlink_buf_t request
;
2084 struct nlmsghdr
*out
= NULL
, *hdr
;
2085 struct xfrm_usersa_id
*sa_id
;
2086 struct xfrm_usersa_info
*sa
= NULL
;
2087 status_t status
= FAILED
;
2089 char markstr
[32] = "";
2091 memset(&request
, 0, sizeof(request
));
2092 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2094 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x%s", ntohl(id
->spi
),
2098 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2099 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2100 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2102 sa_id
= NLMSG_DATA(hdr
);
2103 host2xfrm(id
->dst
, &sa_id
->daddr
);
2104 sa_id
->spi
= id
->spi
;
2105 sa_id
->proto
= id
->proto
;
2106 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2108 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2112 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2117 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2120 while (NLMSG_OK(hdr
, len
))
2122 switch (hdr
->nlmsg_type
)
2124 case XFRM_MSG_NEWSA
:
2126 sa
= NLMSG_DATA(hdr
);
2131 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2133 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x%s failed: "
2134 "%s (%d)", ntohl(id
->spi
), markstr
,
2135 strerror(-err
->error
), -err
->error
);
2139 hdr
= NLMSG_NEXT(hdr
, len
);
2150 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x%s",
2151 ntohl(id
->spi
), markstr
);
2157 *bytes
= sa
->curlft
.bytes
;
2161 *packets
= sa
->curlft
.packets
;
2164 { /* curlft contains an "use" time, but that contains a timestamp
2165 * of the first use, not the last. Last use time must be queried
2166 * on the policy on Linux */
2176 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
2177 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2178 kernel_ipsec_del_sa_t
*data
)
2180 netlink_buf_t request
;
2181 struct nlmsghdr
*hdr
;
2182 struct xfrm_usersa_id
*sa_id
;
2183 char markstr
[32] = "";
2185 /* if IPComp was used, we first delete the additional IPComp SA */
2188 kernel_ipsec_sa_id_t ipcomp_id
= {
2191 .spi
= htonl(ntohs(data
->cpi
)),
2192 .proto
= IPPROTO_COMP
,
2195 kernel_ipsec_del_sa_t ipcomp
= {};
2196 del_sa(this, &ipcomp_id
, &ipcomp
);
2199 memset(&request
, 0, sizeof(request
));
2200 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2202 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x%s", ntohl(id
->spi
),
2206 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2207 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
2208 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2210 sa_id
= NLMSG_DATA(hdr
);
2211 host2xfrm(id
->dst
, &sa_id
->daddr
);
2212 sa_id
->spi
= id
->spi
;
2213 sa_id
->proto
= id
->proto
;
2214 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2216 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2220 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2225 switch (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
))
2228 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x%s",
2229 ntohl(id
->spi
), markstr
);
2234 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x%s",
2235 ntohl(id
->spi
), markstr
);
2240 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
2241 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2242 kernel_ipsec_update_sa_t
*data
)
2244 netlink_buf_t request
;
2245 struct nlmsghdr
*hdr
, *out_hdr
= NULL
, *out
= NULL
;
2246 struct xfrm_usersa_id
*sa_id
;
2247 struct xfrm_usersa_info
*sa
;
2251 struct xfrm_encap_tmpl
* encap
= NULL
;
2252 struct xfrm_replay_state
*replay
= NULL
;
2253 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
2254 struct xfrm_lifetime_cur
*lifetime
= NULL
;
2255 uint32_t replay_esn_len
= 0;
2256 kernel_ipsec_del_sa_t del
= { 0 };
2257 status_t status
= FAILED
;
2258 traffic_selector_t
*ts
;
2259 char markstr
[32] = "";
2261 /* if IPComp is used, we first update the IPComp SA */
2264 kernel_ipsec_sa_id_t ipcomp_id
= {
2267 .spi
= htonl(ntohs(data
->cpi
)),
2268 .proto
= IPPROTO_COMP
,
2272 kernel_ipsec_update_sa_t ipcomp
= {
2273 .new_src
= data
->new_src
,
2274 .new_dst
= data
->new_dst
,
2276 update_sa(this, &ipcomp_id
, &ipcomp
);
2279 memset(&request
, 0, sizeof(request
));
2280 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2282 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x%s for update",
2283 ntohl(id
->spi
), markstr
);
2285 /* query the existing SA first */
2287 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2288 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
2289 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
2291 sa_id
= NLMSG_DATA(hdr
);
2292 host2xfrm(id
->dst
, &sa_id
->daddr
);
2293 sa_id
->spi
= id
->spi
;
2294 sa_id
->proto
= id
->proto
;
2295 sa_id
->family
= id
->dst
->get_family(id
->dst
);
2297 if (!add_mark(hdr
, sizeof(request
), id
->mark
))
2301 if (id
->if_id
&& !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, id
->if_id
))
2306 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2309 while (NLMSG_OK(hdr
, len
))
2311 switch (hdr
->nlmsg_type
)
2313 case XFRM_MSG_NEWSA
:
2320 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2321 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
2322 strerror(-err
->error
), -err
->error
);
2326 hdr
= NLMSG_NEXT(hdr
, len
);
2336 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x%s",
2337 ntohl(id
->spi
), markstr
);
2341 get_replay_state(this, id
, &replay_esn
, &replay_esn_len
, &replay
,
2344 /* delete the old SA (without affecting the IPComp SA) */
2345 if (del_sa(this, id
, &del
) != SUCCESS
)
2347 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x%s",
2348 ntohl(id
->spi
), markstr
);
2352 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x%s from %#H..%#H to "
2353 "%#H..%#H", ntohl(id
->spi
), markstr
, id
->src
, id
->dst
, data
->new_src
,
2355 /* copy over the SA from out to request */
2357 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2358 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
2359 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
2360 sa
= NLMSG_DATA(hdr
);
2361 memcpy(sa
, NLMSG_DATA(out_hdr
), sizeof(struct xfrm_usersa_info
));
2362 sa
->family
= data
->new_dst
->get_family(data
->new_dst
);
2364 if (!id
->src
->ip_equals(id
->src
, data
->new_src
))
2366 host2xfrm(data
->new_src
, &sa
->saddr
);
2368 ts
= selector2ts(&sa
->sel
, TRUE
);
2369 if (ts
&& ts
->is_host(ts
, id
->src
))
2371 ts
->set_address(ts
, data
->new_src
);
2372 ts2subnet(ts
, &sa
->sel
.saddr
, &sa
->sel
.prefixlen_s
);
2376 if (!id
->dst
->ip_equals(id
->dst
, data
->new_dst
))
2378 host2xfrm(data
->new_dst
, &sa
->id
.daddr
);
2380 ts
= selector2ts(&sa
->sel
, FALSE
);
2381 if (ts
&& ts
->is_host(ts
, id
->dst
))
2383 ts
->set_address(ts
, data
->new_dst
);
2384 ts2subnet(ts
, &sa
->sel
.daddr
, &sa
->sel
.prefixlen_d
);
2389 rta
= XFRM_RTA(out_hdr
, struct xfrm_usersa_info
);
2390 rtasize
= XFRM_PAYLOAD(out_hdr
, struct xfrm_usersa_info
);
2391 while (RTA_OK(rta
, rtasize
))
2393 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2394 if (rta
->rta_type
!= XFRMA_ENCAP
|| data
->new_encap
)
2396 if (rta
->rta_type
== XFRMA_ENCAP
)
2397 { /* update encap tmpl */
2398 encap
= RTA_DATA(rta
);
2399 encap
->encap_sport
= ntohs(data
->new_src
->get_port(data
->new_src
));
2400 encap
->encap_dport
= ntohs(data
->new_dst
->get_port(data
->new_dst
));
2402 if (rta
->rta_type
== XFRMA_OFFLOAD_DEV
)
2403 { /* update offload device */
2404 struct xfrm_user_offload
*offload
;
2408 offload
= RTA_DATA(rta
);
2409 local
= offload
->flags
& XFRM_OFFLOAD_INBOUND ? data
->new_dst
2412 if (charon
->kernel
->get_interface(charon
->kernel
, local
,
2415 offload
->ifindex
= if_nametoindex(ifname
);
2416 if (local
->get_family(local
) == AF_INET6
)
2418 offload
->flags
|= XFRM_OFFLOAD_IPV6
;
2422 offload
->flags
&= ~XFRM_OFFLOAD_IPV6
;
2427 netlink_add_attribute(hdr
, rta
->rta_type
,
2428 chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
)),
2431 rta
= RTA_NEXT(rta
, rtasize
);
2434 if (encap
== NULL
&& data
->new_encap
)
2435 { /* add tmpl if we are enabling it */
2436 encap
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
,
2442 encap
->encap_type
= UDP_ENCAP_ESPINUDP
;
2443 encap
->encap_sport
= ntohs(data
->new_src
->get_port(data
->new_src
));
2444 encap
->encap_dport
= ntohs(data
->new_dst
->get_port(data
->new_dst
));
2445 memset(&encap
->encap_oa
, 0, sizeof (xfrm_address_t
));
2450 struct xfrm_replay_state_esn
*state
;
2452 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
2458 memcpy(state
, replay_esn
, replay_esn_len
);
2462 struct xfrm_replay_state
*state
;
2464 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_VAL
,
2470 memcpy(state
, replay
, sizeof(*state
));
2474 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry with "
2475 "SPI %.8x%s", ntohl(id
->spi
), markstr
);
2479 struct xfrm_lifetime_cur
*state
;
2481 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_LTIME_VAL
,
2487 memcpy(state
, lifetime
, sizeof(*state
));
2491 DBG1(DBG_KNL
, "unable to copy usage stats from old SAD entry with "
2492 "SPI %.8x%s", ntohl(id
->spi
), markstr
);
2495 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2497 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x%s",
2498 ntohl(id
->spi
), markstr
);
2508 memwipe(&request
, sizeof(request
));
2514 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
2515 private_kernel_netlink_ipsec_t
*this)
2517 netlink_buf_t request
;
2518 struct nlmsghdr
*hdr
;
2519 struct xfrm_usersa_flush
*flush
;
2524 { IPPROTO_AH
, "AH" },
2525 { IPPROTO_ESP
, "ESP" },
2526 { IPPROTO_COMP
, "IPComp" },
2530 memset(&request
, 0, sizeof(request
));
2533 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2534 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
2535 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
2537 flush
= NLMSG_DATA(hdr
);
2539 for (i
= 0; i
< countof(protos
); i
++)
2541 DBG2(DBG_KNL
, "flushing all %s SAD entries", protos
[i
].name
);
2543 flush
->proto
= protos
[i
].proto
;
2545 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2547 DBG1(DBG_KNL
, "unable to flush %s SAD entries", protos
[i
].name
);
2555 * Unlock the mutex and signal waiting threads
2557 static void policy_change_done(private_kernel_netlink_ipsec_t
*this,
2558 policy_entry_t
*policy
)
2560 policy
->working
= FALSE
;
2561 if (policy
->waiting
)
2562 { /* don't need to wake threads waiting for other policies */
2563 this->condvar
->broadcast(this->condvar
);
2565 this->mutex
->unlock(this->mutex
);
2569 * Install a route for the given policy if enabled and required
2571 static void install_route(private_kernel_netlink_ipsec_t
*this,
2572 policy_entry_t
*policy
, policy_sa_t
*mapping
, ipsec_sa_t
*ipsec
)
2574 policy_sa_out_t
*out
= (policy_sa_out_t
*)mapping
;
2575 route_entry_t
*route
;
2579 .prefixlen
= policy
->sel
.prefixlen_d
,
2580 .pass
= mapping
->type
== POLICY_PASS
,
2583 if (charon
->kernel
->get_address_by_ts(charon
->kernel
, out
->src_ts
,
2584 &route
->src_ip
, NULL
) != SUCCESS
)
2591 /* allow blank source IP for passthrough policies */
2592 route
->src_ip
= host_create_any(policy
->sel
.family
);
2595 if (!ipsec
->dst
->is_anyaddr(ipsec
->dst
))
2597 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2598 ipsec
->dst
, -1, ipsec
->src
,
2602 { /* for shunt policies */
2603 iface
= xfrm2host(policy
->sel
.family
, &policy
->sel
.daddr
, 0);
2604 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2605 iface
, policy
->sel
.prefixlen_d
,
2606 route
->src_ip
, &route
->if_name
);
2607 iface
->destroy(iface
);
2609 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2610 memcpy(route
->dst_net
.ptr
, &policy
->sel
.daddr
, route
->dst_net
.len
);
2612 /* get the interface to install the route for, if we haven't one yet.
2613 * If we have a local address, use it. Otherwise (for shunt policies)
2614 * use the route's source address. */
2615 if (!route
->if_name
)
2618 if (iface
->is_anyaddr(iface
))
2620 iface
= route
->src_ip
;
2622 if (!charon
->kernel
->get_interface(charon
->kernel
, iface
,
2625 { /* don't require an interface for passthrough policies */
2626 route_entry_destroy(route
);
2632 route_entry_t
*old
= policy
->route
;
2633 if (route_entry_equals(old
, route
))
2635 route_entry_destroy(route
);
2638 /* uninstall previously installed route */
2639 if (charon
->kernel
->del_route(charon
->kernel
, old
->dst_net
,
2640 old
->prefixlen
, old
->gateway
,
2641 old
->src_ip
, old
->if_name
,
2642 old
->pass
) != SUCCESS
)
2644 DBG1(DBG_KNL
, "error uninstalling route installed with policy "
2645 "%R === %R %N", out
->src_ts
, out
->dst_ts
, policy_dir_names
,
2648 route_entry_destroy(old
);
2649 policy
->route
= NULL
;
2652 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s", out
->dst_ts
,
2653 route
->gateway
, route
->src_ip
, route
->if_name
);
2654 switch (charon
->kernel
->add_route(charon
->kernel
, route
->dst_net
,
2655 route
->prefixlen
, route
->gateway
,
2656 route
->src_ip
, route
->if_name
,
2660 DBG1(DBG_KNL
, "unable to install source route for %H",
2664 /* route exists, do not uninstall */
2665 route_entry_destroy(route
);
2668 /* cache the installed route */
2669 policy
->route
= route
;
2675 * Add or update a policy in the kernel.
2677 * Note: The mutex has to be locked when entering this function
2678 * and is unlocked here in any case.
2680 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
2681 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
2683 netlink_buf_t request
;
2684 policy_entry_t clone
;
2685 ipsec_sa_t
*ipsec
= mapping
->sa
;
2686 struct xfrm_userpolicy_info
*policy_info
;
2687 struct nlmsghdr
*hdr
;
2691 /* clone the policy so we are able to check it out again later */
2692 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
2694 memset(&request
, 0, sizeof(request
));
2696 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2697 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
2698 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2700 policy_info
= NLMSG_DATA(hdr
);
2701 policy_info
->sel
= policy
->sel
;
2702 policy_info
->dir
= policy
->direction
;
2704 /* calculate priority based on selector size, small size = high prio */
2705 policy_info
->priority
= mapping
->priority
;
2706 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
2707 : XFRM_POLICY_BLOCK
;
2708 policy_info
->share
= XFRM_SHARE_ANY
;
2710 /* policies don't expire */
2711 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2712 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2713 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2714 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2715 policy_info
->lft
.soft_add_expires_seconds
= 0;
2716 policy_info
->lft
.hard_add_expires_seconds
= 0;
2717 policy_info
->lft
.soft_use_expires_seconds
= 0;
2718 policy_info
->lft
.hard_use_expires_seconds
= 0;
2720 if (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.reqid
)
2722 struct xfrm_user_tmpl
*tmpl
;
2728 { IPPROTO_COMP
, htonl(ntohs(ipsec
->cfg
.ipcomp
.cpi
)),
2729 ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2730 { IPPROTO_ESP
, ipsec
->cfg
.esp
.spi
, ipsec
->cfg
.esp
.use
},
2731 { IPPROTO_AH
, ipsec
->cfg
.ah
.spi
, ipsec
->cfg
.ah
.use
},
2733 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2736 for (i
= 0; i
< countof(protos
); i
++)
2743 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_TMPL
,
2744 count
* sizeof(*tmpl
));
2747 policy_change_done(this, policy
);
2751 for (i
= 0; i
< countof(protos
); i
++)
2757 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2758 tmpl
->id
.proto
= protos
[i
].proto
;
2759 if (policy
->direction
== POLICY_OUT
)
2761 tmpl
->id
.spi
= protos
[i
].spi
;
2763 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2764 tmpl
->mode
= mode2kernel(proto_mode
);
2765 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2766 policy
->direction
!= POLICY_OUT
;
2767 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2769 if (proto_mode
== MODE_TUNNEL
|| proto_mode
== MODE_BEET
)
2770 { /* only for tunnel mode */
2771 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2772 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2777 /* use transport mode for other SAs */
2778 proto_mode
= MODE_TRANSPORT
;
2782 if (!add_mark(hdr
, sizeof(request
), ipsec
->mark
))
2784 policy_change_done(this, policy
);
2788 !add_uint32(hdr
, sizeof(request
), XFRMA_IF_ID
, ipsec
->if_id
))
2790 policy_change_done(this, policy
);
2793 this->mutex
->unlock(this->mutex
);
2795 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2796 if (status
== ALREADY_DONE
&& !update
)
2798 DBG1(DBG_KNL
, "policy already exists, try to update it");
2799 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
2800 status
= this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
);
2803 this->mutex
->lock(this->mutex
);
2804 if (status
!= SUCCESS
)
2806 policy_change_done(this, policy
);
2809 /* install a route, if:
2810 * - this is an outbound policy (to just get one for each child)
2811 * - routing is not disabled via strongswan.conf
2812 * - the selector is not for a specific protocol/port
2813 * - no XFRM interface ID is configured
2814 * - we are in tunnel/BEET mode or install a bypass policy
2816 if (policy
->direction
== POLICY_OUT
&& this->install_routes
&&
2817 !policy
->sel
.proto
&& !policy
->sel
.dport
&& !policy
->sel
.sport
&&
2820 if (mapping
->type
== POLICY_PASS
||
2821 (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.mode
!= MODE_TRANSPORT
))
2823 install_route(this, policy
, mapping
, ipsec
);
2826 policy_change_done(this, policy
);
2830 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2831 private_kernel_netlink_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2832 kernel_ipsec_manage_policy_t
*data
)
2834 policy_entry_t
*policy
, *current
;
2835 policy_sa_t
*assigned_sa
, *current_sa
;
2836 enumerator_t
*enumerator
;
2837 bool found
= FALSE
, update
= TRUE
;
2838 char markstr
[32] = "";
2839 uint32_t cur_priority
= 0;
2842 /* create a policy */
2844 .sel
= ts2selector(id
->src_ts
, id
->dst_ts
, id
->interface
),
2845 .mark
= id
->mark
.value
& id
->mark
.mask
,
2847 .direction
= id
->dir
,
2848 .reqid
= data
->sa
->reqid
,
2850 format_mark(markstr
, sizeof(markstr
), id
->mark
);
2852 /* find the policy, which matches EXACTLY */
2853 this->mutex
->lock(this->mutex
);
2854 current
= this->policies
->get(this->policies
, policy
);
2857 if (current
->reqid
&& data
->sa
->reqid
&&
2858 current
->reqid
!= data
->sa
->reqid
)