2 * Copyright (C) 2008-2018 Tobias Brunner
3 * Copyright (C) 2008 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * Copyright (C) 2014 Nanoteq Pty Ltd
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this software and associated documentation files (the "Software"), to deal
21 * in the Software without restriction, including without limitation the rights
22 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23 * copies of the Software, and to permit persons to whom the Software is
24 * furnished to do so, subject to the following conditions:
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 #include <sys/types.h>
40 #include <sys/socket.h>
43 #include <limits.h> /* for LONG_MAX */
46 #ifdef HAVE_NET_PFKEYV2_H
47 #include <net/pfkeyv2.h>
49 #include <linux/pfkeyv2.h>
52 #ifdef SADB_X_EXT_NAT_T_TYPE
56 #ifdef HAVE_NETIPSEC_IPSEC_H
57 #include <netipsec/ipsec.h>
58 #elif defined(HAVE_NETINET6_IPSEC_H)
59 #include <netinet6/ipsec.h>
61 #include <linux/ipsec.h>
65 #ifdef HAVE_LINUX_UDP_H
66 #include <linux/udp.h>
68 #include <netinet/udp.h>
69 #endif /*HAVE_LINUX_UDP_H*/
76 #include <sys/sysctl.h>
79 #include "kernel_pfkey_ipsec.h"
82 #include <utils/debug.h>
83 #include <networking/host.h>
84 #include <collections/linked_list.h>
85 #include <collections/hashtable.h>
86 #include <threading/mutex.h>
88 /** non linux specific */
91 #define IPPROTO_COMP IPPROTO_IPCOMP
95 #ifndef SADB_X_AALG_SHA2_256HMAC
96 #define SADB_X_AALG_SHA2_256HMAC SADB_X_AALG_SHA2_256
97 #define SADB_X_AALG_SHA2_384HMAC SADB_X_AALG_SHA2_384
98 #define SADB_X_AALG_SHA2_512HMAC SADB_X_AALG_SHA2_512
101 #ifndef SADB_X_EALG_AESCBC
102 #define SADB_X_EALG_AESCBC SADB_X_EALG_AES
105 #ifndef SADB_X_EALG_CASTCBC
106 #define SADB_X_EALG_CASTCBC SADB_X_EALG_CAST128CBC
109 #if !defined(SADB_X_EALG_AES_GCM_ICV8) && defined(SADB_X_EALG_AESGCM8)
110 #define SADB_X_EALG_AES_GCM_ICV8 SADB_X_EALG_AESGCM8
111 #define SADB_X_EALG_AES_GCM_ICV12 SADB_X_EALG_AESGCM12
112 #define SADB_X_EALG_AES_GCM_ICV16 SADB_X_EALG_AESGCM16
116 #define SOL_IP IPPROTO_IP
117 #define SOL_IPV6 IPPROTO_IPV6
120 /** from linux/in.h */
121 #ifndef IP_IPSEC_POLICY
122 #define IP_IPSEC_POLICY 16
125 /** missing on uclibc */
126 #ifndef IPV6_IPSEC_POLICY
127 #define IPV6_IPSEC_POLICY 34
130 /* from linux/udp.h */
132 #define UDP_ENCAP 100
135 #ifndef UDP_ENCAP_ESPINUDP
136 #define UDP_ENCAP_ESPINUDP 2
139 /* this is not defined on some platforms */
141 #define SOL_UDP IPPROTO_UDP
144 /** Base priority for installed policies */
145 #define PRIO_BASE 200000
148 /** from xnu/bsd/net/pfkeyv2.h */
149 #define SADB_X_EXT_NATT 0x002
152 uint16_t sadb_sa_natt_port
;
153 uint16_t sadb_reserved0
;
154 uint32_t sadb_reserved1
;
158 /** buffer size for PF_KEY messages */
159 #define PFKEY_BUFFER_SIZE 4096
161 /** PF_KEY messages are 64 bit aligned */
162 #define PFKEY_ALIGNMENT 8
163 /** aligns len to 64 bits */
164 #define PFKEY_ALIGN(len) (((len) + PFKEY_ALIGNMENT - 1) & ~(PFKEY_ALIGNMENT - 1))
165 /** calculates the properly padded length in 64 bit chunks */
166 #define PFKEY_LEN(len) ((PFKEY_ALIGN(len) / PFKEY_ALIGNMENT))
167 /** calculates user mode length i.e. in bytes */
168 #define PFKEY_USER_LEN(len) ((len) * PFKEY_ALIGNMENT)
170 /** given a PF_KEY message header and an extension this updates the length in the header */
171 #define PFKEY_EXT_ADD(msg, ext) ((msg)->sadb_msg_len += ((struct sadb_ext*)ext)->sadb_ext_len)
172 /** given a PF_KEY message header this returns a pointer to the next extension */
173 #define PFKEY_EXT_ADD_NEXT(msg) ((struct sadb_ext*)(((char*)(msg)) + PFKEY_USER_LEN((msg)->sadb_msg_len)))
174 /** copy an extension and append it to a PF_KEY message */
175 #define PFKEY_EXT_COPY(msg, ext) (PFKEY_EXT_ADD(msg, memcpy(PFKEY_EXT_ADD_NEXT(msg), ext, PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len))))
176 /** given a PF_KEY extension this returns a pointer to the next extension */
177 #define PFKEY_EXT_NEXT(ext) ((struct sadb_ext*)(((char*)(ext)) + PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len)))
178 /** given a PF_KEY extension this returns a pointer to the next extension also updates len (len in 64 bit words) */
179 #define PFKEY_EXT_NEXT_LEN(ext,len) ((len) -= (ext)->sadb_ext_len, PFKEY_EXT_NEXT(ext))
180 /** true if ext has a valid length and len is large enough to contain ext (assuming len in 64 bit words) */
181 #define PFKEY_EXT_OK(ext,len) ((len) >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
182 (ext)->sadb_ext_len >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
183 (ext)->sadb_ext_len <= (len))
185 typedef struct private_kernel_pfkey_ipsec_t private_kernel_pfkey_ipsec_t
;
188 * Private variables and functions of kernel_pfkey class.
190 struct private_kernel_pfkey_ipsec_t
193 * Public part of the kernel_pfkey_t object.
195 kernel_pfkey_ipsec_t
public;
198 * mutex to lock access to various lists
203 * List of installed policies (policy_entry_t)
205 linked_list_t
*policies
;
208 * List of exclude routes (exclude_route_t)
210 linked_list_t
*excludes
;
213 * Hash table of IPsec SAs using policies (ipsec_sa_t)
218 * whether to install routes along policies
223 * whether to install the route via internal interface
225 bool route_via_internal
;
228 * mutex to lock access to the PF_KEY socket
230 mutex_t
*mutex_pfkey
;
233 * PF_KEY socket to communicate with the kernel
238 * PF_KEY socket to receive acquire and expire events
243 * sequence number for messages sent to the kernel
248 typedef struct exclude_route_t exclude_route_t
;
251 * Exclude route definition
253 struct exclude_route_t
{
254 /** destination address of exclude */
256 /** source address for route */
258 /** nexthop exclude has been installed */
260 /** references to this route */
265 * clean up a route exclude entry
267 static void exclude_route_destroy(exclude_route_t
*this)
269 this->dst
->destroy(this->dst
);
270 this->src
->destroy(this->src
);
271 this->gtw
->destroy(this->gtw
);
275 typedef struct route_entry_t route_entry_t
;
278 * installed routing entry
280 struct route_entry_t
{
281 /** name of the interface the route is bound to */
284 /** source ip of the route */
287 /** gateway for this route */
290 /** destination net */
293 /** destination net prefixlen */
296 /** reference to exclude route, if any */
297 exclude_route_t
*exclude
;
301 * destroy an route_entry_t object
303 static void route_entry_destroy(route_entry_t
*this)
306 DESTROY_IF(this->src_ip
);
307 DESTROY_IF(this->gateway
);
308 chunk_free(&this->dst_net
);
313 * compare two route_entry_t objects
315 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
317 return a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
318 a
->src_ip
->ip_equals(a
->src_ip
, b
->src_ip
) &&
319 a
->gateway
&& b
->gateway
&&
320 a
->gateway
->ip_equals(a
->gateway
, b
->gateway
) &&
321 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
;
324 typedef struct ipsec_sa_t ipsec_sa_t
;
327 * IPsec SA assigned to a policy.
330 /** Source address of this SA */
333 /** Destination address of this SA */
336 /** Description of this SA */
339 /** Reference count for this SA */
344 * Hash function for ipsec_sa_t objects
346 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
348 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
349 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
350 chunk_hash(chunk_from_thing(sa
->cfg
))));
354 * Equality function for ipsec_sa_t objects
356 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
358 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
359 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
360 ipsec_sa_cfg_equals(&sa
->cfg
, &other_sa
->cfg
);
364 * Allocate or reference an IPsec SA object
366 static ipsec_sa_t
*ipsec_sa_create(private_kernel_pfkey_ipsec_t
*this,
367 host_t
*src
, host_t
*dst
,
370 ipsec_sa_t
*sa
, *found
;
376 found
= this->sas
->get(this->sas
, sa
);
379 sa
->src
= src
->clone(src
);
380 sa
->dst
= dst
->clone(dst
);
381 this->sas
->put(this->sas
, sa
, sa
);
388 ref_get(&sa
->refcount
);
393 * Release and destroy an IPsec SA object
395 static void ipsec_sa_destroy(private_kernel_pfkey_ipsec_t
*this,
398 if (ref_put(&sa
->refcount
))
400 this->sas
->remove(this->sas
, sa
);
407 typedef struct policy_sa_t policy_sa_t
;
408 typedef struct policy_sa_out_t policy_sa_out_t
;
411 * Mapping between a policy and an IPsec SA.
414 /** Priority assigned to the policy when installed with this SA */
417 /** Base priority assigned to the policy when installed with this SA */
418 uint32_t auto_priority
;
420 /** Type of the policy */
428 * For outbound policies we also cache the traffic selectors in order to install
431 struct policy_sa_out_t
{
432 /** Generic interface */
435 /** Source traffic selector of this policy */
436 traffic_selector_t
*src_ts
;
438 /** Destination traffic selector of this policy */
439 traffic_selector_t
*dst_ts
;
443 * Create a policy_sa(_in)_t object
445 static policy_sa_t
*policy_sa_create(private_kernel_pfkey_ipsec_t
*this,
446 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
447 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, ipsec_sa_cfg_t
*cfg
)
451 if (dir
== POLICY_OUT
)
453 policy_sa_out_t
*out
;
455 .src_ts
= src_ts
->clone(src_ts
),
456 .dst_ts
= dst_ts
->clone(dst_ts
),
458 policy
= &out
->generic
;
462 INIT(policy
, .priority
= 0);
465 policy
->sa
= ipsec_sa_create(this, src
, dst
, cfg
);
470 * Destroy a policy_sa(_in)_t object
472 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t dir
,
473 private_kernel_pfkey_ipsec_t
*this)
475 if (dir
== POLICY_OUT
)
477 policy_sa_out_t
*out
= (policy_sa_out_t
*)policy
;
478 out
->src_ts
->destroy(out
->src_ts
);
479 out
->dst_ts
->destroy(out
->dst_ts
);
481 ipsec_sa_destroy(this, policy
->sa
);
485 CALLBACK(policy_sa_destroy_cb
, void,
486 policy_sa_t
*policy
, va_list args
)
488 private_kernel_pfkey_ipsec_t
*this;
491 VA_ARGS_VGET(args
, dir
, this);
492 policy_sa_destroy(policy
, dir
, this);
495 typedef struct policy_entry_t policy_entry_t
;
498 * installed kernel policy.
500 struct policy_entry_t
{
501 /** Index assigned by the kernel */
504 /** Direction of this policy: in, out, forward */
507 /** Parameters of installed policy */
509 /** Subnet and port */
517 /** Associated route installed for this policy */
518 route_entry_t
*route
;
520 /** List of SAs this policy is used by, ordered by priority */
521 linked_list_t
*used_by
;
525 * Create a policy_entry_t object
527 static policy_entry_t
*create_policy_entry(traffic_selector_t
*src_ts
,
528 traffic_selector_t
*dst_ts
,
531 policy_entry_t
*policy
;
538 src_ts
->to_subnet(src_ts
, &policy
->src
.net
, &policy
->src
.mask
);
539 dst_ts
->to_subnet(dst_ts
, &policy
->dst
.net
, &policy
->dst
.mask
);
541 /* src or dest proto may be "any" (0), use more restrictive one */
542 proto
= max(src_ts
->get_protocol(src_ts
), dst_ts
->get_protocol(dst_ts
));
543 /* map the ports to ICMP type/code how the Linux kernel expects them, that
544 * is, type in src, code in dst */
545 if (proto
== IPPROTO_ICMP
|| proto
== IPPROTO_ICMPV6
)
547 port
= max(policy
->src
.net
->get_port(policy
->src
.net
),
548 policy
->dst
.net
->get_port(policy
->dst
.net
));
549 policy
->src
.net
->set_port(policy
->src
.net
,
550 traffic_selector_icmp_type(port
));
551 policy
->dst
.net
->set_port(policy
->dst
.net
,
552 traffic_selector_icmp_code(port
));
556 proto
= IPSEC_PROTO_ANY
;
558 policy
->src
.proto
= policy
->dst
.proto
= proto
;
564 * Destroy a policy_entry_t object
566 static void policy_entry_destroy(policy_entry_t
*policy
,
567 private_kernel_pfkey_ipsec_t
*this)
571 route_entry_destroy(policy
->route
);
575 policy
->used_by
->invoke_function(policy
->used_by
, policy_sa_destroy_cb
,
576 policy
->direction
, this);
577 policy
->used_by
->destroy(policy
->used_by
);
579 DESTROY_IF(policy
->src
.net
);
580 DESTROY_IF(policy
->dst
.net
);
584 CALLBACK(policy_entry_destroy_cb
, void,
585 policy_entry_t
*policy
, va_list args
)
587 private_kernel_pfkey_ipsec_t
*this;
589 VA_ARGS_VGET(args
, this);
590 policy_entry_destroy(policy
, this);
593 CALLBACK(policy_entry_equals
, bool,
594 policy_entry_t
*current
, va_list args
)
596 policy_entry_t
*policy
;
598 VA_ARGS_VGET(args
, policy
);
599 return current
->direction
== policy
->direction
&&
600 current
->src
.proto
== policy
->src
.proto
&&
601 current
->dst
.proto
== policy
->dst
.proto
&&
602 current
->src
.mask
== policy
->src
.mask
&&
603 current
->dst
.mask
== policy
->dst
.mask
&&
604 current
->src
.net
->equals(current
->src
.net
, policy
->src
.net
) &&
605 current
->dst
.net
->equals(current
->dst
.net
, policy
->dst
.net
);
608 CALLBACK(policy_entry_match_byindex
, bool,
609 policy_entry_t
*current
, va_list args
)
613 VA_ARGS_VGET(args
, index
);
614 return current
->index
== index
;
618 * Calculate the priority of a policy
620 * This is the same formula we use in the kernel-netlink interface, but some
621 * features are currently not or only partially supported by PF_KEY.
623 * bits 0-0: separate trap and regular policies (0..1) 1 bit
624 * bits 1-1: reserved for interface restriction (0..1) 1 bit
625 * bits 2-7: src + dst port mask bits (2 * 0..16) 6 bits
626 * bits 8-8: restriction to protocol (0..1) 1 bit
627 * bits 9-17: src + dst network mask bits (2 * 0..128) 9 bits
630 * smallest value: 000000000 0 000000 0 0: 0, lowest priority = 100'000
631 * largest value : 100000000 1 100000 0 1: 131'457, highst priority = 68'543
633 static inline uint32_t get_priority(policy_entry_t
*policy
,
634 policy_priority_t prio
)
636 uint32_t priority
= PRIO_BASE
;
640 case POLICY_PRIORITY_FALLBACK
:
641 priority
+= PRIO_BASE
;
643 case POLICY_PRIORITY_ROUTED
:
644 case POLICY_PRIORITY_DEFAULT
:
645 priority
+= PRIO_BASE
;
647 case POLICY_PRIORITY_PASS
:
651 /* calculate priority */
652 priority
-= (policy
->src
.mask
+ policy
->dst
.mask
) * 512;
653 priority
-= policy
->src
.proto
!= IPSEC_PROTO_ANY ?
256 : 0;
654 priority
-= policy
->src
.net
->get_port(policy
->src
.net
) ?
64 : 0;
655 priority
-= policy
->dst
.net
->get_port(policy
->dst
.net
) ?
64 : 0;
656 priority
-= (prio
!= POLICY_PRIORITY_ROUTED
);
660 typedef struct pfkey_msg_t pfkey_msg_t
;
665 * PF_KEY message base
667 struct sadb_msg
*msg
;
670 * PF_KEY message extensions
673 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
675 struct sadb_ext
*reserved
; /* SADB_EXT_RESERVED */
676 struct sadb_sa
*sa
; /* SADB_EXT_SA */
677 struct sadb_lifetime
*lft_current
; /* SADB_EXT_LIFETIME_CURRENT */
678 struct sadb_lifetime
*lft_hard
; /* SADB_EXT_LIFETIME_HARD */
679 struct sadb_lifetime
*lft_soft
; /* SADB_EXT_LIFETIME_SOFT */
680 struct sadb_address
*src
; /* SADB_EXT_ADDRESS_SRC */
681 struct sadb_address
*dst
; /* SADB_EXT_ADDRESS_DST */
682 struct sadb_address
*proxy
; /* SADB_EXT_ADDRESS_PROXY */
683 struct sadb_key
*key_auth
; /* SADB_EXT_KEY_AUTH */
684 struct sadb_key
*key_encr
; /* SADB_EXT_KEY_ENCRYPT */
685 struct sadb_ident
*id_src
; /* SADB_EXT_IDENTITY_SRC */
686 struct sadb_ident
*id_dst
; /* SADB_EXT_IDENTITY_DST */
687 struct sadb_sens
*sensitivity
; /* SADB_EXT_SENSITIVITY */
688 struct sadb_prop
*proposal
; /* SADB_EXT_PROPOSAL */
689 struct sadb_supported
*supported_auth
; /* SADB_EXT_SUPPORTED_AUTH */
690 struct sadb_supported
*supported_encr
; /* SADB_EXT_SUPPORTED_ENCRYPT */
691 struct sadb_spirange
*spirange
; /* SADB_EXT_SPIRANGE */
692 struct sadb_x_kmprivate
*x_kmprivate
; /* SADB_X_EXT_KMPRIVATE */
693 struct sadb_x_policy
*x_policy
; /* SADB_X_EXT_POLICY */
694 struct sadb_x_sa2
*x_sa2
; /* SADB_X_EXT_SA2 */
695 struct sadb_x_nat_t_type
*x_natt_type
; /* SADB_X_EXT_NAT_T_TYPE */
696 struct sadb_x_nat_t_port
*x_natt_sport
; /* SADB_X_EXT_NAT_T_SPORT */
697 struct sadb_x_nat_t_port
*x_natt_dport
; /* SADB_X_EXT_NAT_T_DPORT */
698 struct sadb_address
*x_natt_oa
; /* SADB_X_EXT_NAT_T_OA */
699 struct sadb_x_sec_ctx
*x_sec_ctx
; /* SADB_X_EXT_SEC_CTX */
700 struct sadb_x_kmaddress
*x_kmaddress
; /* SADB_X_EXT_KMADDRESS */
701 } __attribute__((__packed__
));
705 ENUM(sadb_ext_type_names
, SADB_EXT_RESERVED
, SADB_EXT_MAX
,
708 "SADB_EXT_LIFETIME_CURRENT",
709 "SADB_EXT_LIFETIME_HARD",
710 "SADB_EXT_LIFETIME_SOFT",
711 "SADB_EXT_ADDRESS_SRC",
712 "SADB_EXT_ADDRESS_DST",
713 "SADB_EXT_ADDRESS_PROXY",
715 "SADB_EXT_KEY_ENCRYPT",
716 "SADB_EXT_IDENTITY_SRC",
717 "SADB_EXT_IDENTITY_DST",
718 "SADB_EXT_SENSITIVITY",
720 "SADB_EXT_SUPPORTED_AUTH",
721 "SADB_EXT_SUPPORTED_ENCRYPT",
723 "SADB_X_EXT_KMPRIVATE",
726 "SADB_X_EXT_NAT_T_TYPE",
727 "SADB_X_EXT_NAT_T_SPORT",
728 "SADB_X_EXT_NAT_T_DPORT",
729 "SADB_X_EXT_NAT_T_OA",
730 "SADB_X_EXT_SEC_CTX",
731 "SADB_X_EXT_KMADDRESS"
735 * convert a protocol identifier to the PF_KEY sa type
737 static uint8_t proto2satype(uint8_t proto
)
742 return SADB_SATYPE_ESP
;
744 return SADB_SATYPE_AH
;
746 return SADB_X_SATYPE_IPCOMP
;
753 * convert a PF_KEY sa type to a protocol identifier
755 static uint8_t satype2proto(uint8_t satype
)
759 case SADB_SATYPE_ESP
:
763 case SADB_X_SATYPE_IPCOMP
:
771 * convert the general ipsec mode to the one defined in ipsec.h
773 static uint8_t mode2kernel(ipsec_mode_t mode
)
778 return IPSEC_MODE_TRANSPORT
;
780 return IPSEC_MODE_TUNNEL
;
781 #ifdef HAVE_IPSEC_MODE_BEET
783 return IPSEC_MODE_BEET
;
791 * convert the general policy direction to the one defined in ipsec.h
793 static uint8_t dir2kernel(policy_dir_t dir
)
798 return IPSEC_DIR_INBOUND
;
800 return IPSEC_DIR_OUTBOUND
;
801 #ifdef HAVE_IPSEC_DIR_FWD
803 return IPSEC_DIR_FWD
;
806 return IPSEC_DIR_INVALID
;
811 * convert the policy type to the one defined in ipsec.h
813 static inline uint16_t type2kernel(policy_type_t type
)
818 return IPSEC_POLICY_IPSEC
;
820 return IPSEC_POLICY_NONE
;
822 return IPSEC_POLICY_DISCARD
;
827 #ifdef SADB_X_MIGRATE
829 * convert the policy direction in ipsec.h to the general one.
831 static policy_dir_t
kernel2dir(uint8_t dir
)
835 case IPSEC_DIR_INBOUND
:
837 case IPSEC_DIR_OUTBOUND
:
839 #ifdef HAVE_IPSEC_DIR_FWD
847 #endif /*SADB_X_MIGRATE*/
849 typedef struct kernel_algorithm_t kernel_algorithm_t
;
852 * Mapping of IKEv2 algorithms to PF_KEY algorithms
854 struct kernel_algorithm_t
{
856 * Identifier specified in IKEv2
861 * Identifier as defined in pfkeyv2.h
866 #define END_OF_LIST -1
869 * Algorithms for encryption
871 static kernel_algorithm_t encryption_algs
[] = {
872 /* {ENCR_DES_IV64, 0 }, */
873 {ENCR_DES
, SADB_EALG_DESCBC
},
874 {ENCR_3DES
, SADB_EALG_3DESCBC
},
875 /* {ENCR_RC5, 0 }, */
876 /* {ENCR_IDEA, 0 }, */
877 {ENCR_CAST
, SADB_X_EALG_CASTCBC
},
878 {ENCR_BLOWFISH
, SADB_X_EALG_BLOWFISHCBC
},
879 /* {ENCR_3IDEA, 0 }, */
880 /* {ENCR_DES_IV32, 0 }, */
881 {ENCR_NULL
, SADB_EALG_NULL
},
882 {ENCR_AES_CBC
, SADB_X_EALG_AESCBC
},
883 #ifdef SADB_X_EALG_AESCTR
884 {ENCR_AES_CTR
, SADB_X_EALG_AESCTR
},
886 /* {ENCR_AES_CCM_ICV8, SADB_X_EALG_AES_CCM_ICV8 }, */
887 /* {ENCR_AES_CCM_ICV12, SADB_X_EALG_AES_CCM_ICV12 }, */
888 /* {ENCR_AES_CCM_ICV16, SADB_X_EALG_AES_CCM_ICV16 }, */
889 #ifdef SADB_X_EALG_AES_GCM_ICV8 /* assume the others are defined too */
890 {ENCR_AES_GCM_ICV8
, SADB_X_EALG_AES_GCM_ICV8
},
891 {ENCR_AES_GCM_ICV12
, SADB_X_EALG_AES_GCM_ICV12
},
892 {ENCR_AES_GCM_ICV16
, SADB_X_EALG_AES_GCM_ICV16
},
893 #elif defined(SADB_X_EALG_AES_GCM) /* macOS */
894 {ENCR_AES_GCM_ICV16
, SADB_X_EALG_AES_GCM
},
896 #ifdef SADB_X_EALG_CAMELLIACBC
897 {ENCR_CAMELLIA_CBC
, SADB_X_EALG_CAMELLIACBC
},
899 #ifdef SADB_X_EALG_CHACHA20POLY1305
900 {ENCR_CHACHA20_POLY1305
, SADB_X_EALG_CHACHA20POLY1305
},
906 * Algorithms for integrity protection
908 static kernel_algorithm_t integrity_algs
[] = {
909 {AUTH_HMAC_MD5_96
, SADB_AALG_MD5HMAC
},
910 {AUTH_HMAC_SHA1_96
, SADB_AALG_SHA1HMAC
},
911 {AUTH_HMAC_SHA2_256_128
, SADB_X_AALG_SHA2_256HMAC
},
912 {AUTH_HMAC_SHA2_384_192
, SADB_X_AALG_SHA2_384HMAC
},
913 {AUTH_HMAC_SHA2_512_256
, SADB_X_AALG_SHA2_512HMAC
},
914 /* {AUTH_DES_MAC, 0, }, */
915 /* {AUTH_KPDK_MD5, 0, }, */
916 #ifdef SADB_X_AALG_AES_XCBC_MAC
917 {AUTH_AES_XCBC_96
, SADB_X_AALG_AES_XCBC_MAC
, },
923 * Algorithms for IPComp, unused yet
925 static kernel_algorithm_t compression_algs
[] = {
926 /* {IPCOMP_OUI, 0 }, */
927 {IPCOMP_DEFLATE
, SADB_X_CALG_DEFLATE
},
928 #ifdef SADB_X_CALG_LZS
929 {IPCOMP_LZS
, SADB_X_CALG_LZS
},
931 #ifdef SADB_X_CALG_LZJH
932 {IPCOMP_LZJH
, SADB_X_CALG_LZJH
},
938 * Look up a kernel algorithm ID and its key size
940 static int lookup_algorithm(transform_type_t type
, int ikev2
)
942 kernel_algorithm_t
*list
;
947 case ENCRYPTION_ALGORITHM
:
948 list
= encryption_algs
;
950 case INTEGRITY_ALGORITHM
:
951 list
= integrity_algs
;
953 case COMPRESSION_ALGORITHM
:
954 list
= compression_algs
;
959 while (list
->ikev2
!= END_OF_LIST
)
961 if (ikev2
== list
->ikev2
)
967 charon
->kernel
->lookup_algorithm(charon
->kernel
, ikev2
, type
, &alg
, NULL
);
972 * Helper to set a port in a sockaddr_t, the port has to be in host order
974 static void set_port(sockaddr_t
*addr
, uint16_t port
)
976 switch (addr
->sa_family
)
980 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
981 sin
->sin_port
= htons(port
);
986 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
987 sin6
->sin6_port
= htons(port
);
994 * Copy a host_t as sockaddr_t to the given memory location.
995 * @return the number of bytes copied
997 static size_t hostcpy(void *dest
, host_t
*host
, bool include_port
)
999 sockaddr_t
*addr
= host
->get_sockaddr(host
), *dest_addr
= dest
;
1000 socklen_t
*len
= host
->get_sockaddr_len(host
);
1002 memcpy(dest
, addr
, *len
);
1003 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1004 dest_addr
->sa_len
= *len
;
1008 set_port(dest_addr
, 0);
1014 * add a host to the given sadb_msg
1016 static void add_addr_ext(struct sadb_msg
*msg
, host_t
*host
, uint16_t type
,
1017 uint8_t proto
, uint8_t prefixlen
, bool include_port
)
1019 struct sadb_address
*addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1022 addr
->sadb_address_exttype
= type
;
1023 addr
->sadb_address_proto
= proto
;
1024 addr
->sadb_address_prefixlen
= prefixlen
;
1025 len
= hostcpy(addr
+ 1, host
, include_port
);
1026 addr
->sadb_address_len
= PFKEY_LEN(sizeof(*addr
) + len
);
1027 PFKEY_EXT_ADD(msg
, addr
);
1032 * add udp encap extensions to a sadb_msg
1034 static void add_encap_ext(struct sadb_msg
*msg
, host_t
*src
, host_t
*dst
)
1036 struct sadb_x_nat_t_type
* nat_type
;
1037 struct sadb_x_nat_t_port
* nat_port
;
1039 nat_type
= (struct sadb_x_nat_t_type
*)PFKEY_EXT_ADD_NEXT(msg
);
1040 nat_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
1041 nat_type
->sadb_x_nat_t_type_len
= PFKEY_LEN(sizeof(*nat_type
));
1042 nat_type
->sadb_x_nat_t_type_type
= UDP_ENCAP_ESPINUDP
;
1043 PFKEY_EXT_ADD(msg
, nat_type
);
1045 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
1046 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
1047 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(*nat_port
));
1048 nat_port
->sadb_x_nat_t_port_port
= htons(src
->get_port(src
));
1049 PFKEY_EXT_ADD(msg
, nat_port
);
1051 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
1052 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
1053 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(*nat_port
));
1054 nat_port
->sadb_x_nat_t_port_port
= htons(dst
->get_port(dst
));
1055 PFKEY_EXT_ADD(msg
, nat_port
);
1057 #endif /*HAVE_NATT*/
1060 * Convert a sadb_address to a traffic_selector
1062 static traffic_selector_t
* sadb_address2ts(struct sadb_address
*address
)
1064 traffic_selector_t
*ts
;
1068 proto
= address
->sadb_address_proto
;
1069 proto
= proto
== IPSEC_PROTO_ANY ?
0 : proto
;
1071 /* The Linux 2.6 kernel does not set the protocol and port information
1072 * in the src and dst sadb_address extensions of the SADB_ACQUIRE message.
1074 host
= host_create_from_sockaddr((sockaddr_t
*)&address
[1]);
1075 ts
= traffic_selector_create_from_subnet(host
,
1076 address
->sadb_address_prefixlen
,
1077 proto
, host
->get_port(host
),
1078 host
->get_port(host
) ?
: 65535);
1083 * Parses a pfkey message received from the kernel
1085 static status_t
parse_pfkey_message(struct sadb_msg
*msg
, pfkey_msg_t
*out
)
1087 struct sadb_ext
* ext
;
1090 memset(out
, 0, sizeof(pfkey_msg_t
));
1093 len
= msg
->sadb_msg_len
;
1094 len
-= PFKEY_LEN(sizeof(struct sadb_msg
));
1096 ext
= (struct sadb_ext
*)(((char*)msg
) + sizeof(struct sadb_msg
));
1098 while (len
>= PFKEY_LEN(sizeof(struct sadb_ext
)))
1100 DBG3(DBG_KNL
, " %N", sadb_ext_type_names
, ext
->sadb_ext_type
);
1101 if (ext
->sadb_ext_len
< PFKEY_LEN(sizeof(struct sadb_ext
)) ||
1102 ext
->sadb_ext_len
> len
)
1104 DBG1(DBG_KNL
, "length of %N extension is invalid",
1105 sadb_ext_type_names
, ext
->sadb_ext_type
);
1109 if ((ext
->sadb_ext_type
> SADB_EXT_MAX
) || (!ext
->sadb_ext_type
))
1111 DBG1(DBG_KNL
, "type of PF_KEY extension (%d) is invalid",
1112 ext
->sadb_ext_type
);
1116 if (out
->ext
[ext
->sadb_ext_type
])
1118 DBG1(DBG_KNL
, "duplicate %N extension",
1119 sadb_ext_type_names
, ext
->sadb_ext_type
);
1123 out
->ext
[ext
->sadb_ext_type
] = ext
;
1124 ext
= PFKEY_EXT_NEXT_LEN(ext
, len
);
1129 DBG1(DBG_KNL
, "PF_KEY message length is invalid");
1137 * Send a message to a specific PF_KEY socket and handle the response.
1139 static status_t
pfkey_send_socket(private_kernel_pfkey_ipsec_t
*this, int socket
,
1140 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
1142 unsigned char buf
[PFKEY_BUFFER_SIZE
];
1143 struct sadb_msg
*msg
;
1146 this->mutex_pfkey
->lock(this->mutex_pfkey
);
1148 /* FIXME: our usage of sequence numbers is probably wrong. check RFC 2367,
1149 * in particular the behavior in response to an SADB_ACQUIRE. */
1150 in
->sadb_msg_seq
= ++this->seq
;
1151 in
->sadb_msg_pid
= getpid();
1153 in_len
= PFKEY_USER_LEN(in
->sadb_msg_len
);
1157 len
= send(socket
, in
, in_len
, 0);
1163 /* interrupted, try again */
1166 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1167 DBG1(DBG_KNL
, "error sending to PF_KEY socket: %s",
1176 msg
= (struct sadb_msg
*)buf
;
1178 len
= recv(socket
, buf
, sizeof(buf
), 0);
1184 DBG1(DBG_KNL
, "got interrupted");
1185 /* interrupted, try again */
1188 DBG1(DBG_KNL
, "error reading from PF_KEY socket: %s",
1190 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1193 if (len
< sizeof(struct sadb_msg
) ||
1194 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
1196 DBG1(DBG_KNL
, "received corrupted PF_KEY message");
1197 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1200 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
1202 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY "
1204 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1207 if (msg
->sadb_msg_pid
!= in
->sadb_msg_pid
)
1209 DBG2(DBG_KNL
, "received PF_KEY message is not intended for us");
1212 if (msg
->sadb_msg_seq
!= this->seq
)
1214 DBG2(DBG_KNL
, "received PF_KEY message with unexpected sequence "
1215 "number, was %d expected %d", msg
->sadb_msg_seq
,
1217 if (msg
->sadb_msg_seq
== 0)
1219 /* FreeBSD and Mac OS X do this for the response to
1220 * SADB_X_SPDGET (but not for the response to SADB_GET).
1221 * FreeBSD: 'key_spdget' in /usr/src/sys/netipsec/key.c. */
1223 else if (msg
->sadb_msg_seq
< this->seq
)
1229 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1233 if (msg
->sadb_msg_type
!= in
->sadb_msg_type
)
1235 DBG2(DBG_KNL
, "received PF_KEY message of wrong type, "
1236 "was %d expected %d, ignoring", msg
->sadb_msg_type
,
1243 *out
= (struct sadb_msg
*)malloc(len
);
1244 memcpy(*out
, buf
, len
);
1246 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1251 * Send a message to the default PF_KEY socket and handle the response.
1253 static status_t
pfkey_send(private_kernel_pfkey_ipsec_t
*this,
1254 struct sadb_msg
*in
, struct sadb_msg
**out
,
1257 return pfkey_send_socket(this, this->socket
, in
, out
, out_len
);
1261 * Process a SADB_ACQUIRE message from the kernel
1263 static void process_acquire(private_kernel_pfkey_ipsec_t
*this,
1264 struct sadb_msg
* msg
)
1266 pfkey_msg_t response
;
1267 uint32_t index
, reqid
= 0;
1268 traffic_selector_t
*src_ts
, *dst_ts
;
1269 policy_entry_t
*policy
;
1272 switch (msg
->sadb_msg_satype
)
1274 case SADB_SATYPE_UNSPEC
:
1275 case SADB_SATYPE_ESP
:
1276 case SADB_SATYPE_AH
:
1279 /* acquire for AH/ESP only */
1282 DBG2(DBG_KNL
, "received an SADB_ACQUIRE");
1284 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1286 DBG1(DBG_KNL
, "parsing SADB_ACQUIRE from kernel failed");
1292 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
1296 index
= response
.x_policy
->sadb_x_policy_id
;
1297 this->mutex
->lock(this->mutex
);
1298 if (this->policies
->find_first(this->policies
, policy_entry_match_byindex
,
1299 (void**)&policy
, index
) &&
1300 policy
->used_by
->get_first(policy
->used_by
, (void**)&sa
) == SUCCESS
)
1302 reqid
= sa
->sa
->cfg
.reqid
;
1306 DBG1(DBG_KNL
, "received an SADB_ACQUIRE with policy id %d but no "
1307 "matching policy found", index
);
1309 this->mutex
->unlock(this->mutex
);
1312 src_ts
= sadb_address2ts(response
.src
);
1313 dst_ts
= sadb_address2ts(response
.dst
);
1315 charon
->kernel
->acquire(charon
->kernel
, reqid
, src_ts
, dst_ts
);
1319 * Process a SADB_EXPIRE message from the kernel
1321 static void process_expire(private_kernel_pfkey_ipsec_t
*this,
1322 struct sadb_msg
* msg
)
1324 pfkey_msg_t response
;
1330 DBG2(DBG_KNL
, "received an SADB_EXPIRE");
1332 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1334 DBG1(DBG_KNL
, "parsing SADB_EXPIRE from kernel failed");
1338 protocol
= satype2proto(msg
->sadb_msg_satype
);
1339 spi
= response
.sa
->sadb_sa_spi
;
1340 hard
= response
.lft_hard
!= NULL
;
1342 if (protocol
== IPPROTO_ESP
|| protocol
== IPPROTO_AH
)
1344 dst
= host_create_from_sockaddr((sockaddr_t
*)(response
.dst
+ 1));
1347 charon
->kernel
->expire(charon
->kernel
, protocol
, spi
, dst
, hard
);
1353 #ifdef SADB_X_MIGRATE
1355 * Process a SADB_X_MIGRATE message from the kernel
1357 static void process_migrate(private_kernel_pfkey_ipsec_t
*this,
1358 struct sadb_msg
* msg
)
1360 pfkey_msg_t response
;
1361 traffic_selector_t
*src_ts
, *dst_ts
;
1364 host_t
*local
= NULL
, *remote
= NULL
;
1366 DBG2(DBG_KNL
, "received an SADB_X_MIGRATE");
1368 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1370 DBG1(DBG_KNL
, "parsing SADB_X_MIGRATE from kernel failed");
1373 src_ts
= sadb_address2ts(response
.src
);
1374 dst_ts
= sadb_address2ts(response
.dst
);
1375 dir
= kernel2dir(response
.x_policy
->sadb_x_policy_dir
);
1376 DBG2(DBG_KNL
, " policy %R === %R %N, id %u", src_ts
, dst_ts
,
1377 policy_dir_names
, dir
);
1379 /* SADB_X_EXT_KMADDRESS is not present in unpatched kernels < 2.6.28 */
1380 if (response
.x_kmaddress
)
1382 sockaddr_t
*local_addr
, *remote_addr
;
1385 local_addr
= (sockaddr_t
*)&response
.x_kmaddress
[1];
1386 local
= host_create_from_sockaddr(local_addr
);
1387 local_len
= (local_addr
->sa_family
== AF_INET6
)?
1388 sizeof(struct sockaddr_in6
) : sizeof(struct sockaddr_in
);
1389 remote_addr
= (sockaddr_t
*)((uint8_t*)local_addr
+ local_len
);
1390 remote
= host_create_from_sockaddr(remote_addr
);
1391 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
1394 if (src_ts
&& dst_ts
&& local
&& remote
)
1396 charon
->kernel
->migrate(charon
->kernel
, reqid
, src_ts
, dst_ts
, dir
,
1407 #endif /*SADB_X_MIGRATE*/
1409 #ifdef SADB_X_NAT_T_NEW_MAPPING
1411 * Process a SADB_X_NAT_T_NEW_MAPPING message from the kernel
1413 static void process_mapping(private_kernel_pfkey_ipsec_t
*this,
1414 struct sadb_msg
* msg
)
1416 pfkey_msg_t response
;
1421 DBG2(DBG_KNL
, "received an SADB_X_NAT_T_NEW_MAPPING");
1423 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1425 DBG1(DBG_KNL
, "parsing SADB_X_NAT_T_NEW_MAPPING from kernel failed");
1429 if (!response
.x_sa2
)
1431 DBG1(DBG_KNL
, "received SADB_X_NAT_T_NEW_MAPPING is missing required "
1436 spi
= response
.sa
->sadb_sa_spi
;
1438 if (satype2proto(msg
->sadb_msg_satype
) != IPPROTO_ESP
)
1443 sa
= (sockaddr_t
*)(response
.dst
+ 1);
1444 dst
= host_create_from_sockaddr(sa
);
1445 switch (sa
->sa_family
)
1449 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
1450 sin
->sin_port
= htons(response
.x_natt_dport
->sadb_x_nat_t_port_port
);
1455 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
1456 sin6
->sin6_port
= htons(response
.x_natt_dport
->sadb_x_nat_t_port_port
);
1464 new = host_create_from_sockaddr(sa
);
1467 charon
->kernel
->mapping(charon
->kernel
, IPPROTO_ESP
, spi
, dst
, new);
1473 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1476 * Receives events from kernel
1478 static bool receive_events(private_kernel_pfkey_ipsec_t
*this, int fd
,
1479 watcher_event_t event
)
1481 unsigned char buf
[PFKEY_BUFFER_SIZE
];
1482 struct sadb_msg
*msg
= (struct sadb_msg
*)buf
;
1485 len
= recvfrom(this->socket_events
, buf
, sizeof(buf
), MSG_DONTWAIT
, NULL
, 0);
1491 /* interrupted, try again */
1494 /* no data ready, select again */
1497 DBG1(DBG_KNL
, "unable to receive from PF_KEY event socket");
1503 if (len
< sizeof(struct sadb_msg
) ||
1504 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
1506 DBG2(DBG_KNL
, "received corrupted PF_KEY message");
1509 if (msg
->sadb_msg_pid
!= 0)
1510 { /* not from kernel. not interested, try another one */
1513 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
1515 DBG1(DBG_KNL
, "buffer was too small to receive the complete "
1520 switch (msg
->sadb_msg_type
)
1523 process_acquire(this, msg
);
1526 process_expire(this, msg
);
1528 #ifdef SADB_X_MIGRATE
1529 case SADB_X_MIGRATE
:
1530 process_migrate(this, msg
);
1532 #endif /*SADB_X_MIGRATE*/
1533 #ifdef SADB_X_NAT_T_NEW_MAPPING
1534 case SADB_X_NAT_T_NEW_MAPPING
:
1535 process_mapping(this, msg
);
1537 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1546 * Get an SPI for a specific protocol from the kernel.
1549 static status_t
get_spi_internal(private_kernel_pfkey_ipsec_t
*this,
1550 host_t
*src
, host_t
*dst
, uint8_t proto
, uint32_t min
, uint32_t max
,
1553 unsigned char request
[PFKEY_BUFFER_SIZE
];
1554 struct sadb_msg
*msg
, *out
;
1555 struct sadb_spirange
*range
;
1556 pfkey_msg_t response
;
1557 uint32_t received_spi
= 0;
1560 memset(&request
, 0, sizeof(request
));
1562 msg
= (struct sadb_msg
*)request
;
1563 msg
->sadb_msg_version
= PF_KEY_V2
;
1564 msg
->sadb_msg_type
= SADB_GETSPI
;
1565 msg
->sadb_msg_satype
= proto2satype(proto
);
1566 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1568 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
1569 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
1571 range
= (struct sadb_spirange
*)PFKEY_EXT_ADD_NEXT(msg
);
1572 range
->sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
1573 range
->sadb_spirange_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1574 range
->sadb_spirange_min
= min
;
1575 range
->sadb_spirange_max
= max
;
1576 PFKEY_EXT_ADD(msg
, range
);
1578 if (pfkey_send(this, msg
, &out
, &len
) == SUCCESS
)
1580 if (out
->sadb_msg_errno
)
1582 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1583 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1585 else if (parse_pfkey_message(out
, &response
) == SUCCESS
)
1587 received_spi
= response
.sa
->sadb_sa_spi
;
1592 if (received_spi
== 0)
1597 *spi
= received_spi
;
1601 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1602 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1603 uint8_t protocol
, uint32_t *spi
)
1605 uint32_t spi_min
, spi_max
;
1607 spi_min
= lib
->settings
->get_int(lib
->settings
, "%s.spi_min",
1608 KERNEL_SPI_MIN
, lib
->ns
);
1609 spi_max
= lib
->settings
->get_int(lib
->settings
, "%s.spi_max",
1610 KERNEL_SPI_MAX
, lib
->ns
);
1612 if (get_spi_internal(this, src
, dst
, protocol
, min(spi_min
, spi_max
),
1613 max(spi_min
, spi_max
), spi
) != SUCCESS
)
1615 DBG1(DBG_KNL
, "unable to get SPI");
1619 DBG2(DBG_KNL
, "got SPI %.8x", ntohl(*spi
));
1623 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1624 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1627 uint32_t received_spi
= 0;
1629 DBG2(DBG_KNL
, "getting CPI");
1631 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1632 0x100, 0xEFFF, &received_spi
) != SUCCESS
)
1634 DBG1(DBG_KNL
, "unable to get CPI");
1638 *cpi
= htons((uint16_t)ntohl(received_spi
));
1640 DBG2(DBG_KNL
, "got CPI %.4x", ntohs(*cpi
));
1644 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1645 private_kernel_pfkey_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1646 kernel_ipsec_add_sa_t
*data
)
1648 unsigned char request
[PFKEY_BUFFER_SIZE
];
1649 struct sadb_msg
*msg
, *out
;
1651 struct sadb_x_sa2
*sa2
;
1652 struct sadb_lifetime
*lft
;
1653 struct sadb_key
*key
;
1655 uint16_t ipcomp
= data
->ipcomp
;
1656 ipsec_mode_t mode
= data
->mode
;
1658 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1659 * we are in the recursive call below */
1660 if (ipcomp
!= IPCOMP_NONE
&& data
->cpi
!= 0)
1662 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1663 kernel_ipsec_sa_id_t ipcomp_id
= {
1666 .spi
= htonl(ntohs(data
->cpi
)),
1667 .proto
= IPPROTO_COMP
,
1670 kernel_ipsec_add_sa_t ipcomp_sa
= {
1671 .reqid
= data
->reqid
,
1673 .src_ts
= data
->src_ts
,
1674 .dst_ts
= data
->dst_ts
,
1676 .enc_alg
= ENCR_UNDEFINED
,
1677 .int_alg
= AUTH_UNDEFINED
,
1679 .ipcomp
= data
->ipcomp
,
1680 .initiator
= data
->initiator
,
1681 .inbound
= data
->inbound
,
1682 .update
= data
->update
,
1684 add_sa(this, &ipcomp_id
, &ipcomp_sa
);
1685 ipcomp
= IPCOMP_NONE
;
1686 /* use transport mode ESP SA, IPComp uses tunnel mode */
1687 mode
= MODE_TRANSPORT
;
1692 /* As we didn't know the reqid during SPI allocation, we used reqid
1693 * zero. Unfortunately we can't SADB_UPDATE to the new reqid, hence we
1694 * have to delete the SPI allocation state manually. The reqid
1695 * selector does not count for that, therefore we have to delete
1696 * that state before installing the new SA to avoid deleting the
1697 * the new state after installing it. */
1698 kernel_ipsec_sa_id_t del_id
= {
1704 kernel_ipsec_del_sa_t del
= { 0 };
1706 if (this->public.interface
.del_sa(&this->public.interface
, &del_id
,
1709 DBG1(DBG_KNL
, "deleting SPI allocation SA failed");
1713 memset(&request
, 0, sizeof(request
));
1715 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}",
1716 ntohl(id
->spi
), data
->reqid
);
1718 msg
= (struct sadb_msg
*)request
;
1719 msg
->sadb_msg_version
= PF_KEY_V2
;
1720 msg
->sadb_msg_type
= SADB_ADD
;
1721 msg
->sadb_msg_satype
= proto2satype(id
->proto
);
1722 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1727 struct sadb_sa_2
*sa_2
;
1728 sa_2
= (struct sadb_sa_2
*)PFKEY_EXT_ADD_NEXT(msg
);
1729 sa_2
->sadb_sa_natt_port
= id
->dst
->get_port(id
->dst
);
1731 sa
->sadb_sa_flags
|= SADB_X_EXT_NATT
;
1732 len
= sizeof(struct sadb_sa_2
);
1737 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1738 len
= sizeof(struct sadb_sa
);
1740 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1741 sa
->sadb_sa_len
= PFKEY_LEN(len
);
1742 sa
->sadb_sa_spi
= id
->spi
;
1743 sa
->sadb_sa_state
= SADB_SASTATE_MATURE
;
1744 if (id
->proto
== IPPROTO_COMP
)
1746 sa
->sadb_sa_encrypt
= lookup_algorithm(COMPRESSION_ALGORITHM
,
1751 /* Linux interprets sadb_sa_replay as number of packets/bits in the
1752 * replay window, whereas on BSD it's the size of the window in bytes.
1753 * Only set for the inbound SA as it's not relevant for the outbound
1754 * SA and might waste memory with large windows. */
1758 sa
->sadb_sa_replay
= min(data
->replay_window
, 32);
1760 sa
->sadb_sa_replay
= min((data
->replay_window
+ 7) / 8, UINT8_MAX
);
1765 #ifdef SADB_X_SAFLAGS_ESN
1766 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1767 sa
->sadb_sa_flags
|= SADB_X_SAFLAGS_ESN
;
1769 DBG1(DBG_KNL
, "extended sequence numbers (ESN) not supported by "
1774 sa
->sadb_sa_auth
= lookup_algorithm(INTEGRITY_ALGORITHM
, data
->int_alg
);
1775 sa
->sadb_sa_encrypt
= lookup_algorithm(ENCRYPTION_ALGORITHM
,
1778 PFKEY_EXT_ADD(msg
, sa
);
1780 #ifdef SADB_X_EXT_SA_REPLAY
1783 struct sadb_x_sa_replay
*repl
;
1785 repl
= (struct sadb_x_sa_replay
*)PFKEY_EXT_ADD_NEXT(msg
);
1786 repl
->sadb_x_sa_replay_exttype
= SADB_X_EXT_SA_REPLAY
;
1787 repl
->sadb_x_sa_replay_len
= PFKEY_LEN(sizeof(struct sadb_x_sa_replay
));
1788 repl
->sadb_x_sa_replay_replay
= min(data
->replay_window
, UINT32_MAX
-32);
1789 PFKEY_EXT_ADD(msg
, repl
);
1793 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
1794 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
1795 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1796 sa2
->sadb_x_sa2_mode
= mode2kernel(mode
);
1797 sa2
->sadb_x_sa2_reqid
= data
->reqid
;
1798 PFKEY_EXT_ADD(msg
, sa2
);
1800 add_addr_ext(msg
, id
->src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
1801 add_addr_ext(msg
, id
->dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
1803 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1804 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
1805 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1806 lft
->sadb_lifetime_allocations
= data
->lifetime
->packets
.rekey
;
1807 lft
->sadb_lifetime_bytes
= data
->lifetime
->bytes
.rekey
;
1808 lft
->sadb_lifetime_addtime
= data
->lifetime
->time
.rekey
;
1809 lft
->sadb_lifetime_usetime
= 0; /* we only use addtime */
1810 PFKEY_EXT_ADD(msg
, lft
);
1812 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1813 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1814 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1815 lft
->sadb_lifetime_allocations
= data
->lifetime
->packets
.life
;
1816 lft
->sadb_lifetime_bytes
= data
->lifetime
->bytes
.life
;
1817 lft
->sadb_lifetime_addtime
= data
->lifetime
->time
.life
;
1818 lft
->sadb_lifetime_usetime
= 0; /* we only use addtime */
1819 PFKEY_EXT_ADD(msg
, lft
);
1821 if (data
->enc_alg
!= ENCR_UNDEFINED
)
1823 if (!sa
->sadb_sa_encrypt
)
1825 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1826 encryption_algorithm_names
, data
->enc_alg
);
1829 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1830 encryption_algorithm_names
, data
->enc_alg
, data
->enc_key
.len
* 8);
1832 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1833 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
1834 key
->sadb_key_bits
= data
->enc_key
.len
* 8;
1835 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + data
->enc_key
.len
);
1836 memcpy(key
+ 1, data
->enc_key
.ptr
, data
->enc_key
.len
);
1838 PFKEY_EXT_ADD(msg
, key
);
1841 if (data
->int_alg
!= AUTH_UNDEFINED
)
1843 if (!sa
->sadb_sa_auth
)
1845 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1846 integrity_algorithm_names
, data
->int_alg
);
1849 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1850 integrity_algorithm_names
, data
->int_alg
, data
->int_key
.len
* 8);
1852 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1853 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
1854 key
->sadb_key_bits
= data
->int_key
.len
* 8;
1855 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + data
->int_key
.len
);
1856 memcpy(key
+ 1, data
->int_key
.ptr
, data
->int_key
.len
);
1858 PFKEY_EXT_ADD(msg
, key
);
1864 add_encap_ext(msg
, id
->src
, id
->dst
);
1866 #endif /*HAVE_NATT*/
1868 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1870 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x",
1874 else if (out
->sadb_msg_errno
)
1876 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x: %s (%d)",
1877 ntohl(id
->spi
), strerror(out
->sadb_msg_errno
),
1878 out
->sadb_msg_errno
);
1887 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1888 private_kernel_pfkey_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
1889 kernel_ipsec_update_sa_t
*data
)
1891 unsigned char request
[PFKEY_BUFFER_SIZE
];
1892 struct sadb_msg
*msg
, *out
;
1894 pfkey_msg_t response
;
1897 #ifndef SADB_X_EXT_NEW_ADDRESS_SRC
1898 /* we can't update the SA if any of the ip addresses have changed.
1899 * that's because we can't use SADB_UPDATE and by deleting and readding the
1900 * SA the sequence numbers would get lost */
1901 if (!id
->src
->ip_equals(id
->src
, data
->new_src
) ||
1902 !id
->dst
->ip_equals(id
->dst
, data
->new_dst
))
1904 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: address "
1905 "changes are not supported", ntohl(id
->spi
));
1906 return NOT_SUPPORTED
;
1908 #endif /*SADB_X_EXT_NEW_ADDRESS_SRC*/
1910 /* if IPComp is used, we first update the IPComp SA */
1913 kernel_ipsec_sa_id_t ipcomp_id
= {
1916 .spi
= htonl(ntohs(data
->cpi
)),
1917 .proto
= IPPROTO_COMP
,
1920 kernel_ipsec_update_sa_t ipcomp
= {
1921 .new_src
= data
->new_src
,
1922 .new_dst
= data
->new_dst
,
1924 update_sa(this, &ipcomp_id
, &ipcomp
);
1927 memset(&request
, 0, sizeof(request
));
1929 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update",
1932 msg
= (struct sadb_msg
*)request
;
1933 msg
->sadb_msg_version
= PF_KEY_V2
;
1934 msg
->sadb_msg_type
= SADB_GET
;
1935 msg
->sadb_msg_satype
= proto2satype(id
->proto
);
1936 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1938 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1939 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1940 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1941 sa
->sadb_sa_spi
= id
->spi
;
1942 sa
->sadb_sa_state
= SADB_SASTATE_MATURE
;
1943 PFKEY_EXT_ADD(msg
, sa
);
1945 add_addr_ext(msg
, id
->src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
1946 add_addr_ext(msg
, id
->dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
1948 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1950 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x",
1954 else if (out
->sadb_msg_errno
)
1956 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: %s (%d)",
1957 ntohl(id
->spi
), strerror(out
->sadb_msg_errno
),
1958 out
->sadb_msg_errno
);
1962 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1964 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: parsing "
1965 "response from kernel failed", ntohl(id
->spi
));
1970 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1971 ntohl(id
->spi
), id
->src
, id
->dst
, data
->new_src
, data
->new_dst
);
1973 memset(&request
, 0, sizeof(request
));
1975 msg
= (struct sadb_msg
*)request
;
1976 msg
->sadb_msg_version
= PF_KEY_V2
;
1977 msg
->sadb_msg_type
= SADB_UPDATE
;
1978 msg
->sadb_msg_satype
= proto2satype(id
->proto
);
1979 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1983 struct sadb_sa_2
*sa_2
;
1984 sa_2
= (struct sadb_sa_2
*)PFKEY_EXT_ADD_NEXT(msg
);
1985 sa_2
->sa
.sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa_2
));
1986 memcpy(&sa_2
->sa
, response
.sa
, sizeof(struct sadb_sa
));
1987 if (data
->new_encap
)
1989 sa_2
->sadb_sa_natt_port
= data
->new_dst
->get_port(data
->new_dst
);
1990 sa_2
->sa
.sadb_sa_flags
|= SADB_X_EXT_NATT
;
1994 PFKEY_EXT_COPY(msg
, response
.sa
);
1996 PFKEY_EXT_COPY(msg
, response
.x_sa2
);
1998 PFKEY_EXT_COPY(msg
, response
.src
);
1999 PFKEY_EXT_COPY(msg
, response
.dst
);
2001 PFKEY_EXT_COPY(msg
, response
.lft_soft
);
2002 PFKEY_EXT_COPY(msg
, response
.lft_hard
);
2005 /* FreeBSD 11.1 does not allow key updates via SADB_UPDATE for mature SAs */
2006 if (response
.key_encr
)
2008 PFKEY_EXT_COPY(msg
, response
.key_encr
);
2011 if (response
.key_auth
)
2013 PFKEY_EXT_COPY(msg
, response
.key_auth
);
2018 if (data
->new_encap
)
2020 add_encap_ext(msg
, data
->new_src
, data
->new_dst
);
2022 #endif /*HAVE_NATT*/
2024 #ifdef SADB_X_EXT_NEW_ADDRESS_SRC
2025 if (!id
->src
->ip_equals(id
->src
, data
->new_src
))
2027 add_addr_ext(msg
, data
->new_src
, SADB_X_EXT_NEW_ADDRESS_SRC
, 0, 0,
2030 if (!id
->dst
->ip_equals(id
->dst
, data
->new_dst
))
2032 add_addr_ext(msg
, data
->new_dst
, SADB_X_EXT_NEW_ADDRESS_DST
, 0, 0,
2035 #endif /*SADB_X_EXT_NEW_ADDRESS_SRC*/
2039 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2041 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x",
2045 else if (out
->sadb_msg_errno
)
2047 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: %s (%d)",
2048 ntohl(id
->spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2057 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
2058 private_kernel_pfkey_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2059 kernel_ipsec_query_sa_t
*data
, uint64_t *bytes
, uint64_t *packets
,
2062 unsigned char request
[PFKEY_BUFFER_SIZE
];
2063 struct sadb_msg
*msg
, *out
;
2065 pfkey_msg_t response
;
2068 memset(&request
, 0, sizeof(request
));
2070 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(id
->spi
));
2072 msg
= (struct sadb_msg
*)request
;
2073 msg
->sadb_msg_version
= PF_KEY_V2
;
2074 msg
->sadb_msg_type
= SADB_GET
;
2075 msg
->sadb_msg_satype
= proto2satype(id
->proto
);
2076 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2078 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
2079 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
2080 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
2081 sa
->sadb_sa_spi
= id
->spi
;
2082 PFKEY_EXT_ADD(msg
, sa
);
2084 /* the Linux Kernel doesn't care for the src address, but other systems do
2087 add_addr_ext(msg
, id
->src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
2088 add_addr_ext(msg
, id
->dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
2090 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2092 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x",
2096 else if (out
->sadb_msg_errno
)
2098 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: %s (%d)",
2099 ntohl(id
->spi
), strerror(out
->sadb_msg_errno
),
2100 out
->sadb_msg_errno
);
2104 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
2106 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x",
2113 *bytes
= response
.lft_current
->sadb_lifetime_bytes
;
2117 /* at least on Linux and FreeBSD this contains the number of packets */
2118 *packets
= response
.lft_current
->sadb_lifetime_allocations
;
2123 /* OS X uses the "last" time of use in usetime */
2124 *time
= response
.lft_current
->sadb_lifetime_usetime
;
2125 #else /* !__APPLE__ */
2126 /* on Linux, sadb_lifetime_usetime is set to the "first" time of use,
2127 * which is actually correct according to PF_KEY. We have to query
2128 * policies for the last usetime. */
2130 #endif /* !__APPLE__ */
2137 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
2138 private_kernel_pfkey_ipsec_t
*this, kernel_ipsec_sa_id_t
*id
,
2139 kernel_ipsec_del_sa_t
*data
)
2141 unsigned char request
[PFKEY_BUFFER_SIZE
];
2142 struct sadb_msg
*msg
, *out
;
2146 /* if IPComp was used, we first delete the additional IPComp SA */
2149 kernel_ipsec_sa_id_t ipcomp_id
= {
2152 .spi
= htonl(ntohs(data
->cpi
)),
2153 .proto
= IPPROTO_COMP
,
2156 kernel_ipsec_del_sa_t ipcomp
= { 0 };
2157 del_sa(this, &ipcomp_id
, &ipcomp
);
2160 memset(&request
, 0, sizeof(request
));
2162 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(id
->spi
));
2164 msg
= (struct sadb_msg
*)request
;
2165 msg
->sadb_msg_version
= PF_KEY_V2
;
2166 msg
->sadb_msg_type
= SADB_DELETE
;
2167 msg
->sadb_msg_satype
= proto2satype(id
->proto
);
2168 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2170 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
2171 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
2172 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
2173 sa
->sadb_sa_spi
= id
->spi
;
2174 PFKEY_EXT_ADD(msg
, sa
);
2176 /* the Linux Kernel doesn't care for the src address, but other systems do
2179 add_addr_ext(msg
, id
->src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
2180 add_addr_ext(msg
, id
->dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
2182 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2184 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x",
2188 else if (out
->sadb_msg_errno
)
2190 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x: %s (%d)",
2191 ntohl(id
->spi
), strerror(out
->sadb_msg_errno
),
2192 out
->sadb_msg_errno
);
2197 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(id
->spi
));
2202 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
2203 private_kernel_pfkey_ipsec_t
*this)
2205 unsigned char request
[PFKEY_BUFFER_SIZE
];
2206 struct sadb_msg
*msg
, *out
;
2211 { SADB_SATYPE_AH
, "AH" },
2212 { SADB_SATYPE_ESP
, "ESP" },
2213 { SADB_X_SATYPE_IPCOMP
, "IPComp" },
2218 memset(&request
, 0, sizeof(request
));
2220 msg
= (struct sadb_msg
*)request
;
2221 msg
->sadb_msg_version
= PF_KEY_V2
;
2222 msg
->sadb_msg_type
= SADB_FLUSH
;
2223 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2225 for (i
= 0; i
< countof(protos
); i
++)
2227 DBG2(DBG_KNL
, "flushing all %s SAD entries", protos
[i
].name
);
2229 msg
->sadb_msg_satype
= protos
[i
].proto
;
2230 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2232 DBG1(DBG_KNL
, "unable to flush %s SAD entries", protos
[i
].name
);
2235 else if (out
->sadb_msg_errno
)
2237 DBG1(DBG_KNL
, "unable to flush %s SAD entries: %s (%d)",
2238 protos
[i
].name
, strerror(out
->sadb_msg_errno
),
2239 out
->sadb_msg_errno
);
2249 * Add an explicit exclude route to a routing entry
2251 static void add_exclude_route(private_kernel_pfkey_ipsec_t
*this,
2252 route_entry_t
*route
, host_t
*src
, host_t
*dst
)
2254 enumerator_t
*enumerator
;
2255 exclude_route_t
*exclude
;
2258 enumerator
= this->excludes
->create_enumerator(this->excludes
);
2259 while (enumerator
->enumerate(enumerator
, &exclude
))
2261 if (dst
->ip_equals(dst
, exclude
->dst
))
2263 route
->exclude
= exclude
;
2267 enumerator
->destroy(enumerator
);
2269 if (!route
->exclude
)
2271 DBG2(DBG_KNL
, "installing new exclude route for %H src %H", dst
, src
);
2272 gtw
= charon
->kernel
->get_nexthop(charon
->kernel
, dst
, -1, NULL
, NULL
);
2275 char *if_name
= NULL
;
2277 if (charon
->kernel
->get_interface(charon
->kernel
, src
, &if_name
) &&
2278 charon
->kernel
->add_route(charon
->kernel
,
2279 dst
->get_address(dst
),
2280 dst
->get_family(dst
) == AF_INET ?
32 : 128,
2281 gtw
, src
, if_name
) == SUCCESS
)
2284 .dst
= dst
->clone(dst
),
2285 .src
= src
->clone(src
),
2286 .gtw
= gtw
->clone(gtw
),
2289 route
->exclude
= exclude
;
2290 this->excludes
->insert_last(this->excludes
, exclude
);
2294 DBG1(DBG_KNL
, "installing exclude route for %H failed", dst
);
2301 DBG1(DBG_KNL
, "gateway lookup for for %H failed", dst
);
2307 * Remove an exclude route attached to a routing entry
2309 static void remove_exclude_route(private_kernel_pfkey_ipsec_t
*this,
2310 route_entry_t
*route
)
2314 enumerator_t
*enumerator
;
2315 exclude_route_t
*exclude
;
2316 bool removed
= FALSE
;
2319 enumerator
= this->excludes
->create_enumerator(this->excludes
);
2320 while (enumerator
->enumerate(enumerator
, &exclude
))
2322 if (route
->exclude
== exclude
)
2324 if (--exclude
->refs
== 0)
2326 this->excludes
->remove_at(this->excludes
, enumerator
);
2332 enumerator
->destroy(enumerator
);
2336 char *if_name
= NULL
;
2338 dst
= route
->exclude
->dst
;
2339 DBG2(DBG_KNL
, "uninstalling exclude route for %H src %H",
2340 dst
, route
->exclude
->src
);
2341 if (charon
->kernel
->get_interface(
2343 route
->exclude
->src
, &if_name
) &&
2344 charon
->kernel
->del_route(charon
->kernel
,
2345 dst
->get_address(dst
),
2346 dst
->get_family(dst
) == AF_INET ?
32 : 128,
2347 route
->exclude
->gtw
, route
->exclude
->src
,
2348 if_name
) != SUCCESS
)
2350 DBG1(DBG_KNL
, "uninstalling exclude route for %H failed", dst
);
2352 exclude_route_destroy(route
->exclude
);
2355 route
->exclude
= NULL
;
2360 * Try to install a route to the given outbound policy
2362 static bool install_route(private_kernel_pfkey_ipsec_t
*this,
2363 policy_entry_t
*policy
, policy_sa_out_t
*out
)
2365 route_entry_t
*route
, *old
;
2366 host_t
*host
, *src
, *dst
;
2369 if (charon
->kernel
->get_address_by_ts(charon
->kernel
, out
->src_ts
, &host
,
2370 &is_virtual
) != SUCCESS
)
2376 .prefixlen
= policy
->dst
.mask
,
2378 .dst_net
= chunk_clone(policy
->dst
.net
->get_address(policy
->dst
.net
)),
2381 src
= out
->generic
.sa
->src
;
2382 dst
= out
->generic
.sa
->dst
;
2384 if (!dst
->is_anyaddr(dst
))
2386 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
, dst
, -1,
2387 src
, &route
->if_name
);
2389 /* if the IP is virtual, we install the route over the interface it has
2390 * been installed on. Otherwise we use the interface we use for IKE, as
2391 * this is required for example on Linux. */
2392 if (is_virtual
|| this->route_via_internal
)
2394 free(route
->if_name
);
2395 route
->if_name
= NULL
;
2396 src
= route
->src_ip
;
2400 { /* for shunt policies */
2401 route
->gateway
= charon
->kernel
->get_nexthop(charon
->kernel
,
2402 policy
->dst
.net
, policy
->dst
.mask
,
2403 route
->src_ip
, &route
->if_name
);
2405 /* we don't have a source address, use the address we found */
2406 src
= route
->src_ip
;
2409 /* get interface for route, using source address */
2410 if (!route
->if_name
&&
2411 !charon
->kernel
->get_interface(charon
->kernel
, src
, &route
->if_name
))
2413 route_entry_destroy(route
);
2419 old
= policy
->route
;
2421 if (route_entry_equals(old
, route
))
2422 { /* such a route already exists */
2423 route_entry_destroy(route
);
2426 /* uninstall previously installed route */
2427 if (charon
->kernel
->del_route(charon
->kernel
, old
->dst_net
,
2428 old
->prefixlen
, old
->gateway
,
2429 old
->src_ip
, old
->if_name
) != SUCCESS
)
2431 DBG1(DBG_KNL
, "error uninstalling route installed with policy "
2432 "%R === %R %N", out
->src_ts
, out
->dst_ts
,
2433 policy_dir_names
, policy
->direction
);
2435 route_entry_destroy(old
);
2436 policy
->route
= NULL
;
2439 /* if remote traffic selector covers the IKE peer, add an exclude route */
2440 if (charon
->kernel
->get_features(charon
->kernel
) & KERNEL_REQUIRE_EXCLUDE_ROUTE
)
2442 if (out
->dst_ts
->is_host(out
->dst_ts
, dst
))
2444 DBG1(DBG_KNL
, "can't install route for %R === %R %N, conflicts "
2445 "with IKE traffic", out
->src_ts
, out
->dst_ts
, policy_dir_names
,
2447 route_entry_destroy(route
);
2450 if (out
->dst_ts
->includes(out
->dst_ts
, dst
))
2452 add_exclude_route(this, route
, out
->generic
.sa
->src
, dst
);
2456 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2457 out
->dst_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2459 switch (charon
->kernel
->add_route(charon
->kernel
, route
->dst_net
,
2460 route
->prefixlen
, route
->gateway
,
2461 route
->src_ip
, route
->if_name
))
2464 /* route exists, do not uninstall */
2465 remove_exclude_route(this, route
);
2466 route_entry_destroy(route
);
2469 /* cache the installed route */
2470 policy
->route
= route
;
2473 DBG1(DBG_KNL
, "installing route failed: %R via %H src %H dev %s",
2474 out
->dst_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2475 remove_exclude_route(this, route
);
2476 route_entry_destroy(route
);
2482 * Check if any significant data has changed to warrant sending an update to
2485 static bool policy_update_required(policy_sa_t
*current
, policy_sa_t
*updated
)
2487 if (current
->type
!= updated
->type
2488 #ifdef HAVE_STRUCT_SADB_X_POLICY_SADB_X_POLICY_PRIORITY
2489 || current
->priority
!= updated
->priority
2495 if (current
->type
== POLICY_IPSEC
)
2497 ipsec_sa_cfg_t
*cur
= ¤t
->sa
->cfg
, *upd
= &updated
->sa
->cfg
;
2499 /* we don't use ipsec_sa_cfg_equals() here as e.g. SPIs are not
2500 * relevant for this kernel interface, so we don't have to update the
2501 * policy during a rekeying */
2502 if (cur
->mode
!= upd
->mode
||
2503 cur
->reqid
!= upd
->reqid
||
2504 cur
->esp
.use
!= upd
->esp
.use
||
2505 cur
->ah
.use
!= upd
->ah
.use
||
2506 cur
->ipcomp
.transform
!= upd
->ipcomp
.transform
)
2510 if (cur
->mode
== MODE_TUNNEL
&&
2511 (!current
->sa
->src
->ip_equals(current
->sa
->src
, updated
->sa
->src
) ||
2512 !current
->sa
->dst
->ip_equals(current
->sa
->dst
, updated
->sa
->dst
)))
2521 * Add or update a policy in the kernel.
2523 * Note: The mutex has to be locked when entering this function.
2525 static status_t
add_policy_internal(private_kernel_pfkey_ipsec_t
*this,
2526 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
2528 unsigned char request
[PFKEY_BUFFER_SIZE
];
2529 struct sadb_msg
*msg
, *out
;
2530 struct sadb_x_policy
*pol
;
2531 struct sadb_x_ipsecrequest
*req
;
2532 ipsec_sa_t
*ipsec
= mapping
->sa
;
2533 pfkey_msg_t response
;
2535 ipsec_mode_t proto_mode
;
2538 memset(&request
, 0, sizeof(request
));
2540 msg
= (struct sadb_msg
*)request
;
2541 msg
->sadb_msg_version
= PF_KEY_V2
;
2542 msg
->sadb_msg_type
= update ? SADB_X_SPDUPDATE
: SADB_X_SPDADD
;
2543 msg
->sadb_msg_satype
= 0;
2544 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2546 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
2547 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2548 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
2549 pol
->sadb_x_policy_id
= 0;
2550 pol
->sadb_x_policy_dir
= dir2kernel(policy
->direction
);
2551 pol
->sadb_x_policy_type
= type2kernel(mapping
->type
);
2552 #ifdef HAVE_STRUCT_SADB_X_POLICY_SADB_X_POLICY_PRIORITY
2553 pol
->sadb_x_policy_priority
= mapping
->priority
;
2556 if (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.reqid
)
2558 /* one or more sadb_x_ipsecrequest extensions are added to the
2559 * sadb_x_policy extension */
2560 proto_mode
= ipsec
->cfg
.mode
;
2562 req
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
2564 if (ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
)
2566 req
->sadb_x_ipsecrequest_proto
= IPPROTO_COMP
;
2568 /* !!! the length here MUST be in octets instead of 64 bit words */
2569 req
->sadb_x_ipsecrequest_len
= sizeof(struct sadb_x_ipsecrequest
);
2570 req
->sadb_x_ipsecrequest_mode
= mode2kernel(ipsec
->cfg
.mode
);
2571 req
->sadb_x_ipsecrequest_reqid
= ipsec
->cfg
.reqid
;
2572 req
->sadb_x_ipsecrequest_level
= (policy
->direction
== POLICY_OUT
) ?
2573 IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_USE
;
2574 if (ipsec
->cfg
.mode
== MODE_TUNNEL
)
2576 len
= hostcpy(req
+ 1, ipsec
->src
, FALSE
);
2577 req
->sadb_x_ipsecrequest_len
+= len
;
2578 len
= hostcpy((char*)(req
+ 1) + len
, ipsec
->dst
, FALSE
);
2579 req
->sadb_x_ipsecrequest_len
+= len
;
2580 /* use transport mode for other SAs */
2581 proto_mode
= MODE_TRANSPORT
;
2584 pol
->sadb_x_policy_len
+= PFKEY_LEN(req
->sadb_x_ipsecrequest_len
);
2585 req
= (struct sadb_x_ipsecrequest
*)((char*)(req
) +
2586 req
->sadb_x_ipsecrequest_len
);
2589 req
->sadb_x_ipsecrequest_proto
= ipsec
->cfg
.esp
.use ? IPPROTO_ESP
2591 /* !!! the length here MUST be in octets instead of 64 bit words */
2592 req
->sadb_x_ipsecrequest_len
= sizeof(struct sadb_x_ipsecrequest
);
2593 req
->sadb_x_ipsecrequest_mode
= mode2kernel(proto_mode
);
2594 req
->sadb_x_ipsecrequest_reqid
= ipsec
->cfg
.reqid
;
2595 req
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
2596 if (proto_mode
== MODE_TUNNEL
)
2598 len
= hostcpy(req
+ 1, ipsec
->src
, FALSE
);
2599 req
->sadb_x_ipsecrequest_len
+= len
;
2600 len
= hostcpy((char*)(req
+ 1) + len
, ipsec
->dst
, FALSE
);
2601 req
->sadb_x_ipsecrequest_len
+= len
;
2604 pol
->sadb_x_policy_len
+= PFKEY_LEN(req
->sadb_x_ipsecrequest_len
);
2606 PFKEY_EXT_ADD(msg
, pol
);
2608 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
2609 policy
->src
.mask
, TRUE
);
2610 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
2611 policy
->dst
.mask
, TRUE
);
2614 { /* on FreeBSD a lifetime has to be defined to be able to later query
2615 * the current use time. */
2616 struct sadb_lifetime
*lft
;
2617 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
2618 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2619 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
2620 lft
->sadb_lifetime_addtime
= LONG_MAX
;
2621 PFKEY_EXT_ADD(msg
, lft
);
2625 this->mutex
->unlock(this->mutex
);
2627 status
= pfkey_send(this, msg
, &out
, &len
);
2628 if (status
== SUCCESS
&& !update
&& out
->sadb_msg_errno
== EEXIST
)
2630 DBG1(DBG_KNL
, "policy already exists, try to update it");
2632 msg
->sadb_msg_type
= SADB_X_SPDUPDATE
;
2633 status
= pfkey_send(this, msg
, &out
, &len
);
2635 if (status
!= SUCCESS
)
2639 else if (out
->sadb_msg_errno
)
2641 DBG1(DBG_KNL
, "unable to %s policy: %s (%d)",
2642 update ?
"update" : "add", strerror(out
->sadb_msg_errno
),
2643 out
->sadb_msg_errno
);
2647 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
2649 DBG1(DBG_KNL
, "unable to %s policy: parsing response from kernel "
2650 "failed", update ?
"update" : "add");
2655 /* we try to find the policy again and update the kernel index */
2656 this->mutex
->lock(this->mutex
);
2657 if (!this->policies
->find_first(this->policies
, NULL
, (void**)&policy
))
2659 DBG2(DBG_KNL
, "unable to update index, the policy is already gone, "
2661 this->mutex
->unlock(this->mutex
);
2665 policy
->index
= response
.x_policy
->sadb_x_policy_id
;
2668 /* install a route, if:
2669 * - this is an outbound policy (to just get one for each child)
2670 * - routing is not disabled via strongswan.conf
2671 * - the selector is not for a specific protocol/port
2672 * - we are in tunnel mode or install a bypass policy
2674 if (policy
->direction
== POLICY_OUT
&& this->install_routes
&&
2675 policy
->src
.proto
== IPSEC_PROTO_ANY
&&
2676 !policy
->src
.net
->get_port(policy
->src
.net
) &&
2677 !policy
->dst
.net
->get_port(policy
->dst
.net
))
2679 if (mapping
->type
== POLICY_PASS
||
2680 (mapping
->type
== POLICY_IPSEC
&& ipsec
->cfg
.mode
!= MODE_TRANSPORT
))
2682 install_route(this, policy
, (policy_sa_out_t
*)mapping
);
2685 this->mutex
->unlock(this->mutex
);
2689 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2690 private_kernel_pfkey_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2691 kernel_ipsec_manage_policy_t
*data
)
2693 policy_entry_t
*policy
, *found
= NULL
;
2694 policy_sa_t
*assigned_sa
, *current_sa
= NULL
;
2695 enumerator_t
*enumerator
;
2698 if (dir2kernel(id
->dir
) == IPSEC_DIR_INVALID
)
2699 { /* FWD policies are not supported on all platforms */
2703 /* create a policy */
2704 policy
= create_policy_entry(id
->src_ts
, id
->dst_ts
, id
->dir
);
2706 /* find a matching policy */
2707 this->mutex
->lock(this->mutex
);
2708 if (this->policies
->find_first(this->policies
, policy_entry_equals
,
2709 (void**)&found
, policy
))
2710 { /* use existing policy */
2711 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
2712 "refcount", id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
);
2713 policy_entry_destroy(policy
, this);
2717 { /* use the new one, if we have no such policy */
2718 this->policies
->insert_first(this->policies
, policy
);
2719 policy
->used_by
= linked_list_create();
2722 /* cache the assigned IPsec SA */
2723 assigned_sa
= policy_sa_create(this, id
->dir
, data
->type
, data
->src
,
2724 data
->dst
, id
->src_ts
, id
->dst_ts
, data
->sa
);
2725 assigned_sa
->auto_priority
= get_priority(policy
, data
->prio
);
2726 assigned_sa
->priority
= data
->manual_prio ? data
->manual_prio
:
2727 assigned_sa
->auto_priority
;
2730 /* insert the SA according to its priority */
2731 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2732 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2734 if (current_sa
->priority
> assigned_sa
->priority
)
2738 if (current_sa
->priority
== assigned_sa
->priority
)
2740 /* in case of equal manual prios order SAs by automatic priority */
2741 if (current_sa
->auto_priority
> assigned_sa
->auto_priority
)
2745 /* prefer SAs with a reqid over those without */
2746 if (current_sa
->auto_priority
== assigned_sa
->auto_priority
&&
2747 (!current_sa
->sa
->cfg
.reqid
|| assigned_sa
->sa
->cfg
.reqid
))
2754 policy
->used_by
->insert_before(policy
->used_by
, enumerator
, assigned_sa
);
2755 enumerator
->destroy(enumerator
);
2757 if (update
&& current_sa
)
2758 { /* check if there are actually any relevant changes, if not, we don't
2759 * send an update to the kernel as e.g. FreeBSD doesn't do that
2760 * atomically, causing unnecessary traffic loss during rekeyings */
2761 update
= policy_update_required(current_sa
, assigned_sa
);
2765 { /* we don't update the policy if the priority is lower than that of the
2766 * currently installed one */
2767 this->mutex
->unlock(this->mutex
);
2771 DBG2(DBG_KNL
, "%s policy %R === %R %N",
2772 found ?
"updating" : "adding", id
->src_ts
, id
->dst_ts
,
2773 policy_dir_names
, id
->dir
);
2775 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2777 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N",
2778 found ?
"update" : "add", id
->src_ts
, id
->dst_ts
,
2779 policy_dir_names
, id
->dir
);
2785 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2786 private_kernel_pfkey_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2787 kernel_ipsec_query_policy_t
*data
, time_t *use_time
)
2789 unsigned char request
[PFKEY_BUFFER_SIZE
];
2790 struct sadb_msg
*msg
, *out
;
2791 struct sadb_x_policy
*pol
;
2792 policy_entry_t
*policy
, *found
= NULL
;
2793 pfkey_msg_t response
;
2796 if (dir2kernel(id
->dir
) == IPSEC_DIR_INVALID
)
2797 { /* FWD policies are not supported on all platforms */
2801 DBG2(DBG_KNL
, "querying policy %R === %R %N", id
->src_ts
, id
->dst_ts
,
2802 policy_dir_names
, id
->dir
);
2804 /* create a policy */
2805 policy
= create_policy_entry(id
->src_ts
, id
->dst_ts
, id
->dir
);
2807 /* find a matching policy */
2808 this->mutex
->lock(this->mutex
);
2809 if (!this->policies
->find_first(this->policies
, policy_entry_equals
,
2810 (void**)&found
, policy
))
2812 DBG1(DBG_KNL
, "querying policy %R === %R %N failed, not found",
2813 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
);
2814 policy_entry_destroy(policy
, this);
2815 this->mutex
->unlock(this->mutex
);
2818 policy_entry_destroy(policy
, this);
2821 memset(&request
, 0, sizeof(request
));
2823 msg
= (struct sadb_msg
*)request
;
2824 msg
->sadb_msg_version
= PF_KEY_V2
;
2825 msg
->sadb_msg_type
= SADB_X_SPDGET
;
2826 msg
->sadb_msg_satype
= 0;
2827 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2829 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
2830 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2831 pol
->sadb_x_policy_id
= policy
->index
;
2832 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
2833 pol
->sadb_x_policy_dir
= dir2kernel(id
->dir
);
2834 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2835 PFKEY_EXT_ADD(msg
, pol
);
2837 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
2838 policy
->src
.mask
, TRUE
);
2839 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
2840 policy
->dst
.mask
, TRUE
);
2842 this->mutex
->unlock(this->mutex
);
2844 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2846 DBG1(DBG_KNL
, "unable to query policy %R === %R %N", id
->src_ts
,
2847 id
->dst_ts
, policy_dir_names
, id
->dir
);
2850 else if (out
->sadb_msg_errno
)
2852 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: %s (%d)",
2853 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
2854 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2858 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
2860 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: parsing response "
2861 "from kernel failed", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
2866 else if (response
.lft_current
== NULL
)
2868 DBG2(DBG_KNL
, "unable to query policy %R === %R %N: kernel reports no "
2869 "use time", id
->src_ts
, id
->dst_ts
, policy_dir_names
,
2875 /* we need the monotonic time, but the kernel returns system time. */
2876 if (response
.lft_current
->sadb_lifetime_usetime
)
2878 *use_time
= time_monotonic(NULL
) -
2879 (time(NULL
) - response
.lft_current
->sadb_lifetime_usetime
);
2889 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2890 private_kernel_pfkey_ipsec_t
*this, kernel_ipsec_policy_id_t
*id
,
2891 kernel_ipsec_manage_policy_t
*data
)
2893 unsigned char request
[PFKEY_BUFFER_SIZE
];
2894 struct sadb_msg
*msg
, *out
;
2895 struct sadb_x_policy
*pol
;
2896 policy_entry_t
*policy
, *found
= NULL
;
2897 policy_sa_t
*mapping
, *to_remove
= NULL
;
2898 enumerator_t
*enumerator
;
2899 bool first
= TRUE
, is_installed
= TRUE
;
2900 uint32_t priority
, auto_priority
;
2902 ipsec_sa_t assigned_sa
= {
2908 if (dir2kernel(id
->dir
) == IPSEC_DIR_INVALID
)
2909 { /* FWD policies are not supported on all platforms */
2913 DBG2(DBG_KNL
, "deleting policy %R === %R %N", id
->src_ts
, id
->dst_ts
,
2914 policy_dir_names
, id
->dir
);
2916 /* create a policy */
2917 policy
= create_policy_entry(id
->src_ts
, id
->dst_ts
, id
->dir
);
2919 /* find a matching policy */
2920 this->mutex
->lock(this->mutex
);
2921 if (!this->policies
->find_first(this->policies
, policy_entry_equals
,
2922 (void**)&found
, policy
))
2924 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found",
2925 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
);
2926 policy_entry_destroy(policy
, this);
2927 this->mutex
->unlock(this->mutex
);
2930 policy_entry_destroy(policy
, this);
2933 /* remove mapping to SA by reqid and priority, if multiple match, which
2934 * could happen when rekeying due to an address change, remove the oldest */
2935 auto_priority
= get_priority(policy
, data
->prio
);
2936 priority
= data
->manual_prio ? data
->manual_prio
: auto_priority
;
2937 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2938 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2940 if (priority
== mapping
->priority
&&
2941 auto_priority
== mapping
->auto_priority
&&
2942 data
->type
== mapping
->type
&&
2943 ipsec_sa_equals(mapping
->sa
, &assigned_sa
))
2945 to_remove
= mapping
;
2946 is_installed
= first
;
2948 else if (priority
< mapping
->priority
)
2954 enumerator
->destroy(enumerator
);
2956 { /* sanity check */
2957 this->mutex
->unlock(this->mutex
);
2960 policy
->used_by
->remove(policy
->used_by
, to_remove
, NULL
);
2962 if (policy
->used_by
->get_count(policy
->used_by
) > 0)
2963 { /* policy is used by more SAs, keep in kernel */
2964 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2967 { /* check if there are actually any relevant changes, if not, we do
2968 * not send an update to the kernel as e.g. FreeBSD doesn't do that
2969 * atomically, causing unnecessary traffic loss during rekeyings */
2970 policy
->used_by
->get_first(policy
->used_by
, (void**)&mapping
);
2971 is_installed
= policy_update_required(mapping
, to_remove
);
2973 policy_sa_destroy(to_remove
, id
->dir
, this);
2976 { /* no need to update as the policy */
2977 this->mutex
->unlock(this->mutex
);
2981 DBG2(DBG_KNL
, "updating policy %R === %R %N", id
->src_ts
, id
->dst_ts
,
2982 policy_dir_names
, id
->dir
);
2983 if (add_policy_internal(this, policy
, mapping
, TRUE
) != SUCCESS
)
2985 DBG1(DBG_KNL
, "unable to update policy %R === %R %N",
2986 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
);
2992 memset(&request
, 0, sizeof(request
));
2994 msg
= (struct sadb_msg
*)request
;
2995 msg
->sadb_msg_version
= PF_KEY_V2
;
2996 msg
->sadb_msg_type
= SADB_X_SPDDELETE
;
2997 msg
->sadb_msg_satype
= 0;
2998 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
3000 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
3001 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3002 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
3003 pol
->sadb_x_policy_dir
= dir2kernel(id
->dir
);
3004 pol
->sadb_x_policy_type
= type2kernel(to_remove
->type
);
3005 PFKEY_EXT_ADD(msg
, pol
);
3007 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
3008 policy
->src
.mask
, TRUE
);
3009 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
3010 policy
->dst
.mask
, TRUE
);
3014 route_entry_t
*route
= policy
->route
;
3015 if (charon
->kernel
->del_route(charon
->kernel
, route
->dst_net
,
3016 route
->prefixlen
, route
->gateway
,
3017 route
->src_ip
, route
->if_name
) != SUCCESS
)
3019 DBG1(DBG_KNL
, "error uninstalling route installed with "
3020 "policy %R === %R %N", id
->src_ts
, id
->dst_ts
,
3021 policy_dir_names
, id
->dir
);
3023 remove_exclude_route(this, route
);
3026 this->policies
->remove(this->policies
, found
, NULL
);
3027 policy_sa_destroy(to_remove
, id
->dir
, this);
3028 policy_entry_destroy(policy
, this);
3029 this->mutex
->unlock(this->mutex
);
3031 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
3033 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", id
->src_ts
,
3034 id
->dst_ts
, policy_dir_names
, id
->dir
);
3037 else if (out
->sadb_msg_errno
)
3039 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N: %s (%d)",
3040 id
->src_ts
, id
->dst_ts
, policy_dir_names
, id
->dir
,
3041 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
3049 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
3050 private_kernel_pfkey_ipsec_t
*this)
3052 unsigned char request
[PFKEY_BUFFER_SIZE
];
3053 struct sadb_msg
*msg
, *out
;
3056 memset(&request
, 0, sizeof(request
));
3058 DBG2(DBG_KNL
, "flushing all policies from SPD");
3060 msg
= (struct sadb_msg
*)request
;
3061 msg
->sadb_msg_version
= PF_KEY_V2
;
3062 msg
->sadb_msg_type
= SADB_X_SPDFLUSH
;
3063 msg
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
3064 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
3066 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
3068 DBG1(DBG_KNL
, "unable to flush SPD entries");
3071 else if (out
->sadb_msg_errno
)
3073 DBG1(DBG_KNL
, "unable to flush SPD entries: %s (%d)",
3074 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
3083 * Register a socket for ACQUIRE/EXPIRE messages
3085 static status_t
register_pfkey_socket(private_kernel_pfkey_ipsec_t
*this,
3088 unsigned char request
[PFKEY_BUFFER_SIZE
];
3089 struct sadb_msg
*msg
, *out
;
3092 memset(&request
, 0, sizeof(request
));
3094 msg
= (struct sadb_msg
*)request
;
3095 msg
->sadb_msg_version
= PF_KEY_V2
;
3096 msg
->sadb_msg_type
= SADB_REGISTER
;
3097 msg
->sadb_msg_satype
= satype
;
3098 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
3100 if (pfkey_send_socket(this, this->socket_events
, msg
, &out
, &len
) != SUCCESS
)
3102 DBG1(DBG_KNL
, "unable to register PF_KEY socket");
3105 else if (out
->sadb_msg_errno
)
3107 DBG1(DBG_KNL
, "unable to register PF_KEY socket: %s (%d)",
3108 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
3116 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
3117 private_kernel_pfkey_ipsec_t
*this, int fd
, int family
)
3119 struct sadb_x_policy policy
;
3120 u_int sol
, ipsec_policy
;
3127 ipsec_policy
= IP_IPSEC_POLICY
;
3133 ipsec_policy
= IPV6_IPSEC_POLICY
;
3140 memset(&policy
, 0, sizeof(policy
));
3141 policy
.sadb_x_policy_len
= sizeof(policy
) / sizeof(uint64_t);
3142 policy
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3143 policy
.sadb_x_policy_type
= IPSEC_POLICY_BYPASS
;
3145 policy
.sadb_x_policy_dir
= IPSEC_DIR_OUTBOUND
;
3146 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
3148 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
3152 policy
.sadb_x_policy_dir
= IPSEC_DIR_INBOUND
;
3153 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
3155 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
3162 METHOD(kernel_ipsec_t
, enable_udp_decap
, bool,
3163 private_kernel_pfkey_ipsec_t
*this, int fd
, int family
, uint16_t port
)
3166 int type
= UDP_ENCAP_ESPINUDP
;
3168 if (setsockopt(fd
, SOL_UDP
, UDP_ENCAP
, &type
, sizeof(type
)) < 0)
3170 DBG1(DBG_KNL
, "unable to set UDP_ENCAP: %s", strerror(errno
));
3173 #else /* __APPLE__ */
3176 if (sysctlbyname("net.inet.ipsec.esp_port", NULL
, NULL
, &intport
,
3177 sizeof(intport
)) != 0)
3179 DBG1(DBG_KNL
, "could not set net.inet.ipsec.esp_port to %d: %s",
3180 port
, strerror(errno
));
3183 #endif /* __APPLE__ */
3188 METHOD(kernel_ipsec_t
, destroy
, void,
3189 private_kernel_pfkey_ipsec_t
*this)
3191 if (this->socket
> 0)
3193 close(this->socket
);
3195 if (this->socket_events
> 0)
3197 lib
->watcher
->remove(lib
->watcher
, this->socket_events
);
3198 close(this->socket_events
);
3200 this->policies
->invoke_function(this->policies
, policy_entry_destroy_cb
,
3202 this->policies
->destroy(this->policies
);
3203 this->excludes
->destroy(this->excludes
);
3204 this->sas
->destroy(this->sas
);
3205 this->mutex
->destroy(this->mutex
);
3206 this->mutex_pfkey
->destroy(this->mutex_pfkey
);
3211 * Described in header.
3213 kernel_pfkey_ipsec_t
*kernel_pfkey_ipsec_create()
3215 private_kernel_pfkey_ipsec_t
*this;
3216 bool register_for_events
= TRUE
;
3222 .get_spi
= _get_spi
,
3223 .get_cpi
= _get_cpi
,
3225 .update_sa
= _update_sa
,
3226 .query_sa
= _query_sa
,
3228 .flush_sas
= _flush_sas
,
3229 .add_policy
= _add_policy
,
3230 .query_policy
= _query_policy
,
3231 .del_policy
= _del_policy
,
3232 .flush_policies
= _flush_policies
,
3233 .bypass_socket
= _bypass_socket
,
3234 .enable_udp_decap
= _enable_udp_decap
,
3235 .destroy
= _destroy
,
3238 .policies
= linked_list_create(),
3239 .excludes
= linked_list_create(),
3240 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
3241 (hashtable_equals_t
)ipsec_sa_equals
, 32),
3242 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
3243 .mutex_pfkey
= mutex_create(MUTEX_TYPE_DEFAULT
),
3244 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
3245 "%s.install_routes", TRUE
,
3247 .route_via_internal
= lib
->settings
->get_bool(lib
->settings
,
3248 "%s.plugins.kernel-pfkey.route_via_internal",
3252 if (streq(lib
->ns
, "starter"))
3253 { /* starter has no threads, so we do not register for kernel events */
3254 register_for_events
= FALSE
;
3257 /* create a PF_KEY socket to communicate with the kernel */
3258 this->socket
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
3259 if (this->socket
<= 0)
3261 DBG1(DBG_KNL
, "unable to create PF_KEY socket");
3266 if (register_for_events
)
3268 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
3269 this->socket_events
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
3270 if (this->socket_events
<= 0)
3272 DBG1(DBG_KNL
, "unable to create PF_KEY event socket");
3277 rcv_buffer
= lib
->settings
->get_int(lib
->settings
,
3278 "%s.plugins.kernel-pfkey.events_buffer_size", 0, lib
->ns
);
3281 if (setsockopt(this->socket_events
, SOL_SOCKET
, SO_RCVBUF
,
3282 &rcv_buffer
, sizeof(rcv_buffer
)) == -1)
3284 DBG1(DBG_KNL
, "unable to set receive buffer size on PF_KEY "
3285 "event socket: %s", strerror(errno
));