2 * Copyright (C) 2008-2012 Tobias Brunner
3 * Copyright (C) 2008 Andreas Steffen
4 * 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 #include <sys/types.h>
18 #include <sys/socket.h>
21 #include <limits.h> /* for LONG_MAX */
24 #ifdef HAVE_NET_PFKEYV2_H
25 #include <net/pfkeyv2.h>
28 #include <linux/pfkeyv2.h>
31 #ifdef SADB_X_EXT_NAT_T_TYPE
35 #ifdef HAVE_NETIPSEC_IPSEC_H
36 #include <netipsec/ipsec.h>
37 #elif defined(HAVE_NETINET6_IPSEC_H)
38 #include <netinet6/ipsec.h>
40 #include <linux/ipsec.h>
44 #ifdef HAVE_LINUX_UDP_H
45 #include <linux/udp.h>
47 #include <netinet/udp.h>
48 #endif /*HAVE_LINUX_UDP_H*/
55 #include <sys/sysctl.h>
58 #include "kernel_pfkey_ipsec.h"
62 #include <utils/host.h>
63 #include <utils/linked_list.h>
64 #include <utils/hashtable.h>
65 #include <threading/thread.h>
66 #include <threading/mutex.h>
67 #include <processing/jobs/callback_job.h>
69 /** non linux specific */
72 #define IPPROTO_COMP IPPROTO_IPCOMP
76 #ifndef SADB_X_AALG_SHA2_256HMAC
77 #define SADB_X_AALG_SHA2_256HMAC SADB_X_AALG_SHA2_256
78 #define SADB_X_AALG_SHA2_384HMAC SADB_X_AALG_SHA2_384
79 #define SADB_X_AALG_SHA2_512HMAC SADB_X_AALG_SHA2_512
82 #ifndef SADB_X_EALG_AESCBC
83 #define SADB_X_EALG_AESCBC SADB_X_EALG_AES
86 #ifndef SADB_X_EALG_CASTCBC
87 #define SADB_X_EALG_CASTCBC SADB_X_EALG_CAST128CBC
91 #define SOL_IP IPPROTO_IP
92 #define SOL_IPV6 IPPROTO_IPV6
95 /** from linux/in.h */
96 #ifndef IP_IPSEC_POLICY
97 #define IP_IPSEC_POLICY 16
100 /** missing on uclibc */
101 #ifndef IPV6_IPSEC_POLICY
102 #define IPV6_IPSEC_POLICY 34
105 /* from linux/udp.h */
107 #define UDP_ENCAP 100
110 #ifndef UDP_ENCAP_ESPINUDP
111 #define UDP_ENCAP_ESPINUDP 2
114 /* this is not defined on some platforms */
116 #define SOL_UDP IPPROTO_UDP
119 /** default priority of installed policies */
120 #define PRIO_BASE 512
123 /** from xnu/bsd/net/pfkeyv2.h */
124 #define SADB_X_EXT_NATT 0x002
127 u_int16_t sadb_sa_natt_port
;
128 u_int16_t sadb_reserved0
;
129 u_int32_t sadb_reserved1
;
133 /** buffer size for PF_KEY messages */
134 #define PFKEY_BUFFER_SIZE 4096
136 /** PF_KEY messages are 64 bit aligned */
137 #define PFKEY_ALIGNMENT 8
138 /** aligns len to 64 bits */
139 #define PFKEY_ALIGN(len) (((len) + PFKEY_ALIGNMENT - 1) & ~(PFKEY_ALIGNMENT - 1))
140 /** calculates the properly padded length in 64 bit chunks */
141 #define PFKEY_LEN(len) ((PFKEY_ALIGN(len) / PFKEY_ALIGNMENT))
142 /** calculates user mode length i.e. in bytes */
143 #define PFKEY_USER_LEN(len) ((len) * PFKEY_ALIGNMENT)
145 /** given a PF_KEY message header and an extension this updates the length in the header */
146 #define PFKEY_EXT_ADD(msg, ext) ((msg)->sadb_msg_len += ((struct sadb_ext*)ext)->sadb_ext_len)
147 /** given a PF_KEY message header this returns a pointer to the next extension */
148 #define PFKEY_EXT_ADD_NEXT(msg) ((struct sadb_ext*)(((char*)(msg)) + PFKEY_USER_LEN((msg)->sadb_msg_len)))
149 /** copy an extension and append it to a PF_KEY message */
150 #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))))
151 /** given a PF_KEY extension this returns a pointer to the next extension */
152 #define PFKEY_EXT_NEXT(ext) ((struct sadb_ext*)(((char*)(ext)) + PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len)))
153 /** given a PF_KEY extension this returns a pointer to the next extension also updates len (len in 64 bit words) */
154 #define PFKEY_EXT_NEXT_LEN(ext,len) ((len) -= (ext)->sadb_ext_len, PFKEY_EXT_NEXT(ext))
155 /** true if ext has a valid length and len is large enough to contain ext (assuming len in 64 bit words) */
156 #define PFKEY_EXT_OK(ext,len) ((len) >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
157 (ext)->sadb_ext_len >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
158 (ext)->sadb_ext_len <= (len))
160 typedef struct private_kernel_pfkey_ipsec_t private_kernel_pfkey_ipsec_t
;
163 * Private variables and functions of kernel_pfkey class.
165 struct private_kernel_pfkey_ipsec_t
168 * Public part of the kernel_pfkey_t object.
170 kernel_pfkey_ipsec_t
public;
173 * mutex to lock access to various lists
178 * List of installed policies (policy_entry_t)
180 linked_list_t
*policies
;
183 * Hash table of IPsec SAs using policies (ipsec_sa_t)
188 * whether to install routes along policies
193 * mutex to lock access to the PF_KEY socket
195 mutex_t
*mutex_pfkey
;
198 * PF_KEY socket to communicate with the kernel
203 * PF_KEY socket to receive acquire and expire events
208 * sequence number for messages sent to the kernel
213 typedef struct route_entry_t route_entry_t
;
216 * installed routing entry
218 struct route_entry_t
{
219 /** name of the interface the route is bound to */
222 /** source ip of the route */
225 /** gateway for this route */
228 /** destination net */
231 /** destination net prefixlen */
236 * destroy an route_entry_t object
238 static void route_entry_destroy(route_entry_t
*this)
241 DESTROY_IF(this->src_ip
);
242 DESTROY_IF(this->gateway
);
243 chunk_free(&this->dst_net
);
248 * compare two route_entry_t objects
250 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
252 return a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
253 a
->src_ip
->ip_equals(a
->src_ip
, b
->src_ip
) &&
254 a
->gateway
->ip_equals(a
->gateway
, b
->gateway
) &&
255 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
;
258 typedef struct ipsec_sa_t ipsec_sa_t
;
261 * IPsec SA assigned to a policy.
264 /** Source address of this SA */
267 /** Destination address of this SA */
270 /** Description of this SA */
273 /** Reference count for this SA */
278 * Hash function for ipsec_sa_t objects
280 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
282 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
283 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
284 chunk_hash(chunk_from_thing(sa
->cfg
))));
288 * Equality function for ipsec_sa_t objects
290 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
292 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
293 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
294 memeq(&sa
->cfg
, &other_sa
->cfg
, sizeof(ipsec_sa_cfg_t
));
298 * Allocate or reference an IPsec SA object
300 static ipsec_sa_t
*ipsec_sa_create(private_kernel_pfkey_ipsec_t
*this,
301 host_t
*src
, host_t
*dst
,
304 ipsec_sa_t
*sa
, *found
;
310 found
= this->sas
->get(this->sas
, sa
);
313 sa
->src
= src
->clone(src
);
314 sa
->dst
= dst
->clone(dst
);
315 this->sas
->put(this->sas
, sa
, sa
);
322 ref_get(&sa
->refcount
);
327 * Release and destroy an IPsec SA object
329 static void ipsec_sa_destroy(private_kernel_pfkey_ipsec_t
*this,
332 if (ref_put(&sa
->refcount
))
334 this->sas
->remove(this->sas
, sa
);
341 typedef struct policy_sa_t policy_sa_t
;
342 typedef struct policy_sa_fwd_t policy_sa_fwd_t
;
345 * Mapping between a policy and an IPsec SA.
348 /** Priority assigned to the policy when installed with this SA */
351 /** Type of the policy */
359 * For forward policies we also cache the traffic selectors in order to install
362 struct policy_sa_fwd_t
{
363 /** Generic interface */
366 /** Source traffic selector of this policy */
367 traffic_selector_t
*src_ts
;
369 /** Destination traffic selector of this policy */
370 traffic_selector_t
*dst_ts
;
374 * Create a policy_sa(_fwd)_t object
376 static policy_sa_t
*policy_sa_create(private_kernel_pfkey_ipsec_t
*this,
377 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
378 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, ipsec_sa_cfg_t
*cfg
)
382 if (dir
== POLICY_FWD
)
384 policy_sa_fwd_t
*fwd
;
386 .src_ts
= src_ts
->clone(src_ts
),
387 .dst_ts
= dst_ts
->clone(dst_ts
),
389 policy
= &fwd
->generic
;
393 INIT(policy
, .priority
= 0);
396 policy
->sa
= ipsec_sa_create(this, src
, dst
, cfg
);
401 * Destroy a policy_sa(_fwd)_t object
403 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t
*dir
,
404 private_kernel_pfkey_ipsec_t
*this)
406 if (*dir
== POLICY_FWD
)
408 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)policy
;
409 fwd
->src_ts
->destroy(fwd
->src_ts
);
410 fwd
->dst_ts
->destroy(fwd
->dst_ts
);
412 ipsec_sa_destroy(this, policy
->sa
);
416 typedef struct policy_entry_t policy_entry_t
;
419 * installed kernel policy.
421 struct policy_entry_t
{
422 /** Index assigned by the kernel */
425 /** Direction of this policy: in, out, forward */
428 /** Parameters of installed policy */
430 /** Subnet and port */
438 /** Associated route installed for this policy */
439 route_entry_t
*route
;
441 /** List of SAs this policy is used by, ordered by priority */
442 linked_list_t
*used_by
;
446 * Create a policy_entry_t object
448 static policy_entry_t
*create_policy_entry(traffic_selector_t
*src_ts
,
449 traffic_selector_t
*dst_ts
,
452 policy_entry_t
*policy
;
457 src_ts
->to_subnet(src_ts
, &policy
->src
.net
, &policy
->src
.mask
);
458 dst_ts
->to_subnet(dst_ts
, &policy
->dst
.net
, &policy
->dst
.mask
);
460 /* src or dest proto may be "any" (0), use more restrictive one */
461 policy
->src
.proto
= max(src_ts
->get_protocol(src_ts
),
462 dst_ts
->get_protocol(dst_ts
));
463 policy
->src
.proto
= policy
->src
.proto ? policy
->src
.proto
: IPSEC_PROTO_ANY
;
464 policy
->dst
.proto
= policy
->src
.proto
;
470 * Destroy a policy_entry_t object
472 static void policy_entry_destroy(policy_entry_t
*policy
,
473 private_kernel_pfkey_ipsec_t
*this)
477 route_entry_destroy(policy
->route
);
481 policy
->used_by
->invoke_function(policy
->used_by
,
482 (linked_list_invoke_t
)policy_sa_destroy
,
483 &policy
->direction
, this);
484 policy
->used_by
->destroy(policy
->used_by
);
486 DESTROY_IF(policy
->src
.net
);
487 DESTROY_IF(policy
->dst
.net
);
492 * compares two policy_entry_t
494 static inline bool policy_entry_equals(policy_entry_t
*current
,
495 policy_entry_t
*policy
)
497 return current
->direction
== policy
->direction
&&
498 current
->src
.proto
== policy
->src
.proto
&&
499 current
->dst
.proto
== policy
->dst
.proto
&&
500 current
->src
.mask
== policy
->src
.mask
&&
501 current
->dst
.mask
== policy
->dst
.mask
&&
502 current
->src
.net
->equals(current
->src
.net
, policy
->src
.net
) &&
503 current
->dst
.net
->equals(current
->dst
.net
, policy
->dst
.net
);
507 * compare the given kernel index with that of a policy
509 static inline bool policy_entry_match_byindex(policy_entry_t
*current
,
512 return current
->index
== *index
;
516 * Calculate the priority of a policy
518 static inline u_int32_t
get_priority(policy_entry_t
*policy
,
519 policy_priority_t prio
)
521 u_int32_t priority
= PRIO_BASE
;
524 case POLICY_PRIORITY_FALLBACK
:
527 case POLICY_PRIORITY_ROUTED
:
530 case POLICY_PRIORITY_DEFAULT
:
533 /* calculate priority based on selector size, small size = high prio */
534 priority
-= policy
->src
.mask
;
535 priority
-= policy
->dst
.mask
;
536 priority
<<= 2; /* make some room for the two flags */
537 priority
+= policy
->src
.net
->get_port(policy
->src
.net
) ||
538 policy
->dst
.net
->get_port(policy
->dst
.net
) ?
540 priority
+= policy
->src
.proto
!= IPSEC_PROTO_ANY ?
0 : 1;
544 typedef struct pfkey_msg_t pfkey_msg_t
;
549 * PF_KEY message base
551 struct sadb_msg
*msg
;
554 * PF_KEY message extensions
557 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
559 struct sadb_ext
*reserved
; /* SADB_EXT_RESERVED */
560 struct sadb_sa
*sa
; /* SADB_EXT_SA */
561 struct sadb_lifetime
*lft_current
; /* SADB_EXT_LIFETIME_CURRENT */
562 struct sadb_lifetime
*lft_hard
; /* SADB_EXT_LIFETIME_HARD */
563 struct sadb_lifetime
*lft_soft
; /* SADB_EXT_LIFETIME_SOFT */
564 struct sadb_address
*src
; /* SADB_EXT_ADDRESS_SRC */
565 struct sadb_address
*dst
; /* SADB_EXT_ADDRESS_DST */
566 struct sadb_address
*proxy
; /* SADB_EXT_ADDRESS_PROXY */
567 struct sadb_key
*key_auth
; /* SADB_EXT_KEY_AUTH */
568 struct sadb_key
*key_encr
; /* SADB_EXT_KEY_ENCRYPT */
569 struct sadb_ident
*id_src
; /* SADB_EXT_IDENTITY_SRC */
570 struct sadb_ident
*id_dst
; /* SADB_EXT_IDENTITY_DST */
571 struct sadb_sens
*sensitivity
; /* SADB_EXT_SENSITIVITY */
572 struct sadb_prop
*proposal
; /* SADB_EXT_PROPOSAL */
573 struct sadb_supported
*supported_auth
; /* SADB_EXT_SUPPORTED_AUTH */
574 struct sadb_supported
*supported_encr
; /* SADB_EXT_SUPPORTED_ENCRYPT */
575 struct sadb_spirange
*spirange
; /* SADB_EXT_SPIRANGE */
576 struct sadb_x_kmprivate
*x_kmprivate
; /* SADB_X_EXT_KMPRIVATE */
577 struct sadb_x_policy
*x_policy
; /* SADB_X_EXT_POLICY */
578 struct sadb_x_sa2
*x_sa2
; /* SADB_X_EXT_SA2 */
579 struct sadb_x_nat_t_type
*x_natt_type
; /* SADB_X_EXT_NAT_T_TYPE */
580 struct sadb_x_nat_t_port
*x_natt_sport
; /* SADB_X_EXT_NAT_T_SPORT */
581 struct sadb_x_nat_t_port
*x_natt_dport
; /* SADB_X_EXT_NAT_T_DPORT */
582 struct sadb_address
*x_natt_oa
; /* SADB_X_EXT_NAT_T_OA */
583 struct sadb_x_sec_ctx
*x_sec_ctx
; /* SADB_X_EXT_SEC_CTX */
584 struct sadb_x_kmaddress
*x_kmaddress
; /* SADB_X_EXT_KMADDRESS */
585 } __attribute__((__packed__
));
589 ENUM(sadb_ext_type_names
, SADB_EXT_RESERVED
, SADB_EXT_MAX
,
592 "SADB_EXT_LIFETIME_CURRENT",
593 "SADB_EXT_LIFETIME_HARD",
594 "SADB_EXT_LIFETIME_SOFT",
595 "SADB_EXT_ADDRESS_SRC",
596 "SADB_EXT_ADDRESS_DST",
597 "SADB_EXT_ADDRESS_PROXY",
599 "SADB_EXT_KEY_ENCRYPT",
600 "SADB_EXT_IDENTITY_SRC",
601 "SADB_EXT_IDENTITY_DST",
602 "SADB_EXT_SENSITIVITY",
604 "SADB_EXT_SUPPORTED_AUTH",
605 "SADB_EXT_SUPPORTED_ENCRYPT",
607 "SADB_X_EXT_KMPRIVATE",
610 "SADB_X_EXT_NAT_T_TYPE",
611 "SADB_X_EXT_NAT_T_SPORT",
612 "SADB_X_EXT_NAT_T_DPORT",
613 "SADB_X_EXT_NAT_T_OA",
614 "SADB_X_EXT_SEC_CTX",
615 "SADB_X_EXT_KMADDRESS"
619 * convert a protocol identifier to the PF_KEY sa type
621 static u_int8_t
proto2satype(u_int8_t proto
)
626 return SADB_SATYPE_ESP
;
628 return SADB_SATYPE_AH
;
630 return SADB_X_SATYPE_IPCOMP
;
637 * convert a PF_KEY sa type to a protocol identifier
639 static u_int8_t
satype2proto(u_int8_t satype
)
643 case SADB_SATYPE_ESP
:
647 case SADB_X_SATYPE_IPCOMP
:
655 * convert the general ipsec mode to the one defined in ipsec.h
657 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
662 return IPSEC_MODE_TRANSPORT
;
664 return IPSEC_MODE_TUNNEL
;
665 #ifdef HAVE_IPSEC_MODE_BEET
667 return IPSEC_MODE_BEET
;
675 * convert the general policy direction to the one defined in ipsec.h
677 static u_int8_t
dir2kernel(policy_dir_t dir
)
682 return IPSEC_DIR_INBOUND
;
684 return IPSEC_DIR_OUTBOUND
;
685 #ifdef HAVE_IPSEC_DIR_FWD
687 return IPSEC_DIR_FWD
;
690 return IPSEC_DIR_INVALID
;
695 * convert the policy type to the one defined in ipsec.h
697 static inline u_int16_t
type2kernel(policy_type_t type
)
702 return IPSEC_POLICY_IPSEC
;
704 return IPSEC_POLICY_NONE
;
706 return IPSEC_POLICY_DISCARD
;
711 #ifdef SADB_X_MIGRATE
713 * convert the policy direction in ipsec.h to the general one.
715 static policy_dir_t
kernel2dir(u_int8_t dir
)
719 case IPSEC_DIR_INBOUND
:
721 case IPSEC_DIR_OUTBOUND
:
723 #ifdef HAVE_IPSEC_DIR_FWD
731 #endif /*SADB_X_MIGRATE*/
733 typedef struct kernel_algorithm_t kernel_algorithm_t
;
736 * Mapping of IKEv2 algorithms to PF_KEY algorithms
738 struct kernel_algorithm_t
{
740 * Identifier specified in IKEv2
745 * Identifier as defined in pfkeyv2.h
750 #define END_OF_LIST -1
753 * Algorithms for encryption
755 static kernel_algorithm_t encryption_algs
[] = {
756 /* {ENCR_DES_IV64, 0 }, */
757 {ENCR_DES
, SADB_EALG_DESCBC
},
758 {ENCR_3DES
, SADB_EALG_3DESCBC
},
759 /* {ENCR_RC5, 0 }, */
760 /* {ENCR_IDEA, 0 }, */
761 {ENCR_CAST
, SADB_X_EALG_CASTCBC
},
762 {ENCR_BLOWFISH
, SADB_X_EALG_BLOWFISHCBC
},
763 /* {ENCR_3IDEA, 0 }, */
764 /* {ENCR_DES_IV32, 0 }, */
765 {ENCR_NULL
, SADB_EALG_NULL
},
766 {ENCR_AES_CBC
, SADB_X_EALG_AESCBC
},
767 /* {ENCR_AES_CTR, SADB_X_EALG_AESCTR }, */
768 /* {ENCR_AES_CCM_ICV8, SADB_X_EALG_AES_CCM_ICV8 }, */
769 /* {ENCR_AES_CCM_ICV12, SADB_X_EALG_AES_CCM_ICV12 }, */
770 /* {ENCR_AES_CCM_ICV16, SADB_X_EALG_AES_CCM_ICV16 }, */
771 /* {ENCR_AES_GCM_ICV8, SADB_X_EALG_AES_GCM_ICV8 }, */
772 /* {ENCR_AES_GCM_ICV12, SADB_X_EALG_AES_GCM_ICV12 }, */
773 /* {ENCR_AES_GCM_ICV16, SADB_X_EALG_AES_GCM_ICV16 }, */
778 * Algorithms for integrity protection
780 static kernel_algorithm_t integrity_algs
[] = {
781 {AUTH_HMAC_MD5_96
, SADB_AALG_MD5HMAC
},
782 {AUTH_HMAC_SHA1_96
, SADB_AALG_SHA1HMAC
},
783 {AUTH_HMAC_SHA2_256_128
, SADB_X_AALG_SHA2_256HMAC
},
784 {AUTH_HMAC_SHA2_384_192
, SADB_X_AALG_SHA2_384HMAC
},
785 {AUTH_HMAC_SHA2_512_256
, SADB_X_AALG_SHA2_512HMAC
},
786 /* {AUTH_DES_MAC, 0, }, */
787 /* {AUTH_KPDK_MD5, 0, }, */
788 #ifdef SADB_X_AALG_AES_XCBC_MAC
789 {AUTH_AES_XCBC_96
, SADB_X_AALG_AES_XCBC_MAC
, },
796 * Algorithms for IPComp, unused yet
798 static kernel_algorithm_t compression_algs
[] = {
799 /* {IPCOMP_OUI, 0 }, */
800 {IPCOMP_DEFLATE
, SADB_X_CALG_DEFLATE
},
801 {IPCOMP_LZS
, SADB_X_CALG_LZS
},
802 {IPCOMP_LZJH
, SADB_X_CALG_LZJH
},
808 * Look up a kernel algorithm ID and its key size
810 static int lookup_algorithm(transform_type_t type
, int ikev2
)
812 kernel_algorithm_t
*list
;
817 case ENCRYPTION_ALGORITHM
:
818 list
= encryption_algs
;
820 case INTEGRITY_ALGORITHM
:
821 list
= integrity_algs
;
826 while (list
->ikev2
!= END_OF_LIST
)
828 if (ikev2
== list
->ikev2
)
834 hydra
->kernel_interface
->lookup_algorithm(hydra
->kernel_interface
, ikev2
,
840 * Copy a host_t as sockaddr_t to the given memory location.
841 * @return the number of bytes copied
843 static size_t hostcpy(void *dest
, host_t
*host
, bool include_port
)
845 sockaddr_t
*addr
= host
->get_sockaddr(host
), *dest_addr
= dest
;
846 socklen_t
*len
= host
->get_sockaddr_len(host
);
847 u_int16_t port
= htons(host
->get_port(host
));
849 memcpy(dest
, addr
, *len
);
850 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
851 dest_addr
->sa_len
= *len
;
853 switch (dest_addr
->sa_family
)
857 struct sockaddr_in
*sin
= dest
;
858 sin
->sin_port
= include_port ? port
: 0;
863 struct sockaddr_in6
*sin6
= dest
;
864 sin6
->sin6_port
= include_port ? port
: 0;
872 * add a host behind an sadb_address extension
874 static void host2ext(host_t
*host
, struct sadb_address
*ext
, bool include_port
)
876 size_t len
= hostcpy(ext
+ 1, host
, include_port
);
877 ext
->sadb_address_len
= PFKEY_LEN(sizeof(*ext
) + len
);
881 * add a host to the given sadb_msg
883 static void add_addr_ext(struct sadb_msg
*msg
, host_t
*host
, u_int16_t type
,
884 u_int8_t proto
, u_int8_t prefixlen
, bool include_port
)
886 struct sadb_address
*addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
887 addr
->sadb_address_exttype
= type
;
888 addr
->sadb_address_proto
= proto
;
889 addr
->sadb_address_prefixlen
= prefixlen
;
890 host2ext(host
, addr
, include_port
);
891 PFKEY_EXT_ADD(msg
, addr
);
895 * adds an empty address extension to the given sadb_msg
897 static void add_anyaddr_ext(struct sadb_msg
*msg
, int family
, u_int8_t type
)
899 socklen_t len
= (family
== AF_INET
) ?
sizeof(struct sockaddr_in
) :
900 sizeof(struct sockaddr_in6
);
901 struct sadb_address
*addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
902 addr
->sadb_address_exttype
= type
;
903 sockaddr_t
*saddr
= (sockaddr_t
*)(addr
+ 1);
904 saddr
->sa_family
= family
;
905 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
908 addr
->sadb_address_len
= PFKEY_LEN(sizeof(*addr
) + len
);
909 PFKEY_EXT_ADD(msg
, addr
);
914 * add udp encap extensions to a sadb_msg
916 static void add_encap_ext(struct sadb_msg
*msg
, host_t
*src
, host_t
*dst
)
918 struct sadb_x_nat_t_type
* nat_type
;
919 struct sadb_x_nat_t_port
* nat_port
;
921 nat_type
= (struct sadb_x_nat_t_type
*)PFKEY_EXT_ADD_NEXT(msg
);
922 nat_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
923 nat_type
->sadb_x_nat_t_type_len
= PFKEY_LEN(sizeof(*nat_type
));
924 nat_type
->sadb_x_nat_t_type_type
= UDP_ENCAP_ESPINUDP
;
925 PFKEY_EXT_ADD(msg
, nat_type
);
927 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
928 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
929 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(*nat_port
));
930 nat_port
->sadb_x_nat_t_port_port
= htons(src
->get_port(src
));
931 PFKEY_EXT_ADD(msg
, nat_port
);
933 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
934 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
935 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(*nat_port
));
936 nat_port
->sadb_x_nat_t_port_port
= htons(dst
->get_port(dst
));
937 PFKEY_EXT_ADD(msg
, nat_port
);
942 * Convert a sadb_address to a traffic_selector
944 static traffic_selector_t
* sadb_address2ts(struct sadb_address
*address
)
946 traffic_selector_t
*ts
;
949 /* The Linux 2.6 kernel does not set the protocol and port information
950 * in the src and dst sadb_address extensions of the SADB_ACQUIRE message.
952 host
= host_create_from_sockaddr((sockaddr_t
*)&address
[1]);
953 ts
= traffic_selector_create_from_subnet(host
,
954 address
->sadb_address_prefixlen
,
955 address
->sadb_address_proto
,
956 host
->get_port(host
));
961 * Parses a pfkey message received from the kernel
963 static status_t
parse_pfkey_message(struct sadb_msg
*msg
, pfkey_msg_t
*out
)
965 struct sadb_ext
* ext
;
968 memset(out
, 0, sizeof(pfkey_msg_t
));
971 len
= msg
->sadb_msg_len
;
972 len
-= PFKEY_LEN(sizeof(struct sadb_msg
));
974 ext
= (struct sadb_ext
*)(((char*)msg
) + sizeof(struct sadb_msg
));
976 while (len
>= PFKEY_LEN(sizeof(struct sadb_ext
)))
978 DBG3(DBG_KNL
, " %N", sadb_ext_type_names
, ext
->sadb_ext_type
);
979 if (ext
->sadb_ext_len
< PFKEY_LEN(sizeof(struct sadb_ext
)) ||
980 ext
->sadb_ext_len
> len
)
982 DBG1(DBG_KNL
, "length of %N extension is invalid",
983 sadb_ext_type_names
, ext
->sadb_ext_type
);
987 if ((ext
->sadb_ext_type
> SADB_EXT_MAX
) || (!ext
->sadb_ext_type
))
989 DBG1(DBG_KNL
, "type of PF_KEY extension (%d) is invalid",
994 if (out
->ext
[ext
->sadb_ext_type
])
996 DBG1(DBG_KNL
, "duplicate %N extension",
997 sadb_ext_type_names
, ext
->sadb_ext_type
);
1001 out
->ext
[ext
->sadb_ext_type
] = ext
;
1002 ext
= PFKEY_EXT_NEXT_LEN(ext
, len
);
1007 DBG1(DBG_KNL
, "PF_KEY message length is invalid");
1015 * Send a message to a specific PF_KEY socket and handle the response.
1017 static status_t
pfkey_send_socket(private_kernel_pfkey_ipsec_t
*this, int socket
,
1018 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
1020 unsigned char buf
[PFKEY_BUFFER_SIZE
];
1021 struct sadb_msg
*msg
;
1024 this->mutex_pfkey
->lock(this->mutex_pfkey
);
1026 /* FIXME: our usage of sequence numbers is probably wrong. check RFC 2367,
1027 * in particular the behavior in response to an SADB_ACQUIRE. */
1028 in
->sadb_msg_seq
= ++this->seq
;
1029 in
->sadb_msg_pid
= getpid();
1031 in_len
= PFKEY_USER_LEN(in
->sadb_msg_len
);
1035 len
= send(socket
, in
, in_len
, 0);
1041 /* interrupted, try again */
1044 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1045 DBG1(DBG_KNL
, "error sending to PF_KEY socket: %s",
1054 msg
= (struct sadb_msg
*)buf
;
1056 len
= recv(socket
, buf
, sizeof(buf
), 0);
1062 DBG1(DBG_KNL
, "got interrupted");
1063 /* interrupted, try again */
1066 DBG1(DBG_KNL
, "error reading from PF_KEY socket: %s",
1068 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1071 if (len
< sizeof(struct sadb_msg
) ||
1072 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
1074 DBG1(DBG_KNL
, "received corrupted PF_KEY message");
1075 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1078 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
1080 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY "
1082 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1085 if (msg
->sadb_msg_pid
!= in
->sadb_msg_pid
)
1087 DBG2(DBG_KNL
, "received PF_KEY message is not intended for us");
1090 if (msg
->sadb_msg_seq
!= this->seq
)
1092 DBG1(DBG_KNL
, "received PF_KEY message with unexpected sequence "
1093 "number, was %d expected %d", msg
->sadb_msg_seq
,
1095 if (msg
->sadb_msg_seq
== 0)
1097 /* FreeBSD and Mac OS X do this for the response to
1098 * SADB_X_SPDGET (but not for the response to SADB_GET).
1099 * FreeBSD: 'key_spdget' in /usr/src/sys/netipsec/key.c. */
1101 else if (msg
->sadb_msg_seq
< this->seq
)
1107 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1111 if (msg
->sadb_msg_type
!= in
->sadb_msg_type
)
1113 DBG2(DBG_KNL
, "received PF_KEY message of wrong type, "
1114 "was %d expected %d, ignoring", msg
->sadb_msg_type
,
1121 *out
= (struct sadb_msg
*)malloc(len
);
1122 memcpy(*out
, buf
, len
);
1124 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1129 * Send a message to the default PF_KEY socket and handle the response.
1131 static status_t
pfkey_send(private_kernel_pfkey_ipsec_t
*this,
1132 struct sadb_msg
*in
, struct sadb_msg
**out
,
1135 return pfkey_send_socket(this, this->socket
, in
, out
, out_len
);
1139 * Process a SADB_ACQUIRE message from the kernel
1141 static void process_acquire(private_kernel_pfkey_ipsec_t
*this,
1142 struct sadb_msg
* msg
)
1144 pfkey_msg_t response
;
1145 u_int32_t index
, reqid
= 0;
1146 traffic_selector_t
*src_ts
, *dst_ts
;
1147 policy_entry_t
*policy
;
1150 switch (msg
->sadb_msg_satype
)
1152 case SADB_SATYPE_UNSPEC
:
1153 case SADB_SATYPE_ESP
:
1154 case SADB_SATYPE_AH
:
1157 /* acquire for AH/ESP only */
1160 DBG2(DBG_KNL
, "received an SADB_ACQUIRE");
1162 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1164 DBG1(DBG_KNL
, "parsing SADB_ACQUIRE from kernel failed");
1168 index
= response
.x_policy
->sadb_x_policy_id
;
1169 this->mutex
->lock(this->mutex
);
1170 if (this->policies
->find_first(this->policies
,
1171 (linked_list_match_t
)policy_entry_match_byindex
,
1172 (void**)&policy
, &index
) == SUCCESS
&&
1173 policy
->used_by
->get_first(policy
->used_by
, (void**)&sa
) == SUCCESS
)
1175 reqid
= sa
->sa
->cfg
.reqid
;
1179 DBG1(DBG_KNL
, "received an SADB_ACQUIRE with policy id %d but no "
1180 "matching policy found", index
);
1182 this->mutex
->unlock(this->mutex
);
1184 src_ts
= sadb_address2ts(response
.src
);
1185 dst_ts
= sadb_address2ts(response
.dst
);
1187 hydra
->kernel_interface
->acquire(hydra
->kernel_interface
, reqid
, src_ts
,
1192 * Process a SADB_EXPIRE message from the kernel
1194 static void process_expire(private_kernel_pfkey_ipsec_t
*this,
1195 struct sadb_msg
* msg
)
1197 pfkey_msg_t response
;
1199 u_int32_t spi
, reqid
;
1202 DBG2(DBG_KNL
, "received an SADB_EXPIRE");
1204 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1206 DBG1(DBG_KNL
, "parsing SADB_EXPIRE from kernel failed");
1210 protocol
= satype2proto(msg
->sadb_msg_satype
);
1211 spi
= response
.sa
->sadb_sa_spi
;
1212 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
1213 hard
= response
.lft_hard
!= NULL
;
1215 if (protocol
!= IPPROTO_ESP
&& protocol
!= IPPROTO_AH
)
1217 DBG2(DBG_KNL
, "ignoring SADB_EXPIRE for SA with SPI %.8x and "
1218 "reqid {%u} which is not a CHILD_SA", ntohl(spi
), reqid
);
1222 hydra
->kernel_interface
->expire(hydra
->kernel_interface
, reqid
, protocol
,
1226 #ifdef SADB_X_MIGRATE
1228 * Process a SADB_X_MIGRATE message from the kernel
1230 static void process_migrate(private_kernel_pfkey_ipsec_t
*this,
1231 struct sadb_msg
* msg
)
1233 pfkey_msg_t response
;
1234 traffic_selector_t
*src_ts
, *dst_ts
;
1236 u_int32_t reqid
= 0;
1237 host_t
*local
= NULL
, *remote
= NULL
;
1239 DBG2(DBG_KNL
, "received an SADB_X_MIGRATE");
1241 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1243 DBG1(DBG_KNL
, "parsing SADB_X_MIGRATE from kernel failed");
1246 src_ts
= sadb_address2ts(response
.src
);
1247 dst_ts
= sadb_address2ts(response
.dst
);
1248 dir
= kernel2dir(response
.x_policy
->sadb_x_policy_dir
);
1249 DBG2(DBG_KNL
, " policy %R === %R %N, id %u", src_ts
, dst_ts
,
1250 policy_dir_names
, dir
);
1252 /* SADB_X_EXT_KMADDRESS is not present in unpatched kernels < 2.6.28 */
1253 if (response
.x_kmaddress
)
1255 sockaddr_t
*local_addr
, *remote_addr
;
1256 u_int32_t local_len
;
1258 local_addr
= (sockaddr_t
*)&response
.x_kmaddress
[1];
1259 local
= host_create_from_sockaddr(local_addr
);
1260 local_len
= (local_addr
->sa_family
== AF_INET6
)?
1261 sizeof(struct sockaddr_in6
) : sizeof(struct sockaddr_in
);
1262 remote_addr
= (sockaddr_t
*)((u_int8_t
*)local_addr
+ local_len
);
1263 remote
= host_create_from_sockaddr(remote_addr
);
1264 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
1267 if (src_ts
&& dst_ts
&& local
&& remote
)
1269 hydra
->kernel_interface
->migrate(hydra
->kernel_interface
, reqid
,
1270 src_ts
, dst_ts
, dir
, local
, remote
);
1280 #endif /*SADB_X_MIGRATE*/
1282 #ifdef SADB_X_NAT_T_NEW_MAPPING
1284 * Process a SADB_X_NAT_T_NEW_MAPPING message from the kernel
1286 static void process_mapping(private_kernel_pfkey_ipsec_t
*this,
1287 struct sadb_msg
* msg
)
1289 pfkey_msg_t response
;
1290 u_int32_t spi
, reqid
;
1294 DBG2(DBG_KNL
, "received an SADB_X_NAT_T_NEW_MAPPING");
1296 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1298 DBG1(DBG_KNL
, "parsing SADB_X_NAT_T_NEW_MAPPING from kernel failed");
1302 if (!response
.x_sa2
)
1304 DBG1(DBG_KNL
, "received SADB_X_NAT_T_NEW_MAPPING is missing required "
1309 spi
= response
.sa
->sadb_sa_spi
;
1310 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
1312 if (satype2proto(msg
->sadb_msg_satype
) != IPPROTO_ESP
)
1317 sa
= (sockaddr_t
*)(response
.dst
+ 1);
1318 switch (sa
->sa_family
)
1322 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
1323 sin
->sin_port
= htons(response
.x_natt_dport
->sadb_x_nat_t_port_port
);
1327 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
1328 sin6
->sin6_port
= htons(response
.x_natt_dport
->sadb_x_nat_t_port_port
);
1334 host
= host_create_from_sockaddr(sa
);
1337 hydra
->kernel_interface
->mapping(hydra
->kernel_interface
, reqid
,
1341 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1344 * Receives events from kernel
1346 static job_requeue_t
receive_events(private_kernel_pfkey_ipsec_t
*this)
1348 unsigned char buf
[PFKEY_BUFFER_SIZE
];
1349 struct sadb_msg
*msg
= (struct sadb_msg
*)buf
;
1353 oldstate
= thread_cancelability(TRUE
);
1354 len
= recvfrom(this->socket_events
, buf
, sizeof(buf
), 0, NULL
, 0);
1355 thread_cancelability(oldstate
);
1362 /* interrupted, try again */
1363 return JOB_REQUEUE_DIRECT
;
1365 /* no data ready, select again */
1366 return JOB_REQUEUE_DIRECT
;
1368 DBG1(DBG_KNL
, "unable to receive from PF_KEY event socket");
1370 return JOB_REQUEUE_FAIR
;
1374 if (len
< sizeof(struct sadb_msg
) ||
1375 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
1377 DBG2(DBG_KNL
, "received corrupted PF_KEY message");
1378 return JOB_REQUEUE_DIRECT
;
1380 if (msg
->sadb_msg_pid
!= 0)
1381 { /* not from kernel. not interested, try another one */
1382 return JOB_REQUEUE_DIRECT
;
1384 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
1386 DBG1(DBG_KNL
, "buffer was too small to receive the complete "
1388 return JOB_REQUEUE_DIRECT
;
1391 switch (msg
->sadb_msg_type
)
1394 process_acquire(this, msg
);
1397 process_expire(this, msg
);
1399 #ifdef SADB_X_MIGRATE
1400 case SADB_X_MIGRATE
:
1401 process_migrate(this, msg
);
1403 #endif /*SADB_X_MIGRATE*/
1404 #ifdef SADB_X_NAT_T_NEW_MAPPING
1405 case SADB_X_NAT_T_NEW_MAPPING
:
1406 process_mapping(this, msg
);
1408 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1413 return JOB_REQUEUE_DIRECT
;
1416 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1417 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1418 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
1420 unsigned char request
[PFKEY_BUFFER_SIZE
];
1421 struct sadb_msg
*msg
, *out
;
1422 struct sadb_x_sa2
*sa2
;
1423 struct sadb_spirange
*range
;
1424 pfkey_msg_t response
;
1425 u_int32_t received_spi
= 0;
1428 memset(&request
, 0, sizeof(request
));
1430 msg
= (struct sadb_msg
*)request
;
1431 msg
->sadb_msg_version
= PF_KEY_V2
;
1432 msg
->sadb_msg_type
= SADB_GETSPI
;
1433 msg
->sadb_msg_satype
= proto2satype(protocol
);
1434 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1436 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
1437 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
1438 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1439 sa2
->sadb_x_sa2_reqid
= reqid
;
1440 PFKEY_EXT_ADD(msg
, sa2
);
1442 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
1443 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
1445 range
= (struct sadb_spirange
*)PFKEY_EXT_ADD_NEXT(msg
);
1446 range
->sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
1447 range
->sadb_spirange_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1448 range
->sadb_spirange_min
= 0xc0000000;
1449 range
->sadb_spirange_max
= 0xcFFFFFFF;
1450 PFKEY_EXT_ADD(msg
, range
);
1452 if (pfkey_send(this, msg
, &out
, &len
) == SUCCESS
)
1454 if (out
->sadb_msg_errno
)
1456 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1457 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1459 else if (parse_pfkey_message(out
, &response
) == SUCCESS
)
1461 received_spi
= response
.sa
->sadb_sa_spi
;
1466 if (received_spi
== 0)
1471 *spi
= received_spi
;
1475 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1476 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1477 u_int32_t reqid
, u_int16_t
*cpi
)
1482 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1483 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
, u_int32_t spi
,
1484 u_int8_t protocol
, u_int32_t reqid
, mark_t mark
, u_int32_t tfc
,
1485 lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
1486 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
,
1487 u_int16_t ipcomp
, u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
1488 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
)
1490 unsigned char request
[PFKEY_BUFFER_SIZE
];
1491 struct sadb_msg
*msg
, *out
;
1493 struct sadb_x_sa2
*sa2
;
1494 struct sadb_lifetime
*lft
;
1495 struct sadb_key
*key
;
1498 memset(&request
, 0, sizeof(request
));
1500 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}",
1503 msg
= (struct sadb_msg
*)request
;
1504 msg
->sadb_msg_version
= PF_KEY_V2
;
1505 msg
->sadb_msg_type
= inbound ? SADB_UPDATE
: SADB_ADD
;
1506 msg
->sadb_msg_satype
= proto2satype(protocol
);
1507 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1512 struct sadb_sa_2
*sa_2
;
1513 sa_2
= (struct sadb_sa_2
*)PFKEY_EXT_ADD_NEXT(msg
);
1514 sa_2
->sadb_sa_natt_port
= dst
->get_port(dst
);
1516 sa
->sadb_sa_flags
|= SADB_X_EXT_NATT
;
1517 len
= sizeof(struct sadb_sa_2
);
1522 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1523 len
= sizeof(struct sadb_sa
);
1525 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1526 sa
->sadb_sa_len
= PFKEY_LEN(len
);
1527 sa
->sadb_sa_spi
= spi
;
1528 sa
->sadb_sa_replay
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
1529 sa
->sadb_sa_auth
= lookup_algorithm(INTEGRITY_ALGORITHM
, int_alg
);
1530 sa
->sadb_sa_encrypt
= lookup_algorithm(ENCRYPTION_ALGORITHM
, enc_alg
);
1531 PFKEY_EXT_ADD(msg
, sa
);
1533 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
1534 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
1535 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1536 sa2
->sadb_x_sa2_mode
= mode2kernel(mode
);
1537 sa2
->sadb_x_sa2_reqid
= reqid
;
1538 PFKEY_EXT_ADD(msg
, sa2
);
1540 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
1541 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
1543 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1544 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
1545 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1546 lft
->sadb_lifetime_allocations
= lifetime
->packets
.rekey
;
1547 lft
->sadb_lifetime_bytes
= lifetime
->bytes
.rekey
;
1548 lft
->sadb_lifetime_addtime
= lifetime
->time
.rekey
;
1549 lft
->sadb_lifetime_usetime
= 0; /* we only use addtime */
1550 PFKEY_EXT_ADD(msg
, lft
);
1552 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1553 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1554 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1555 lft
->sadb_lifetime_allocations
= lifetime
->packets
.life
;
1556 lft
->sadb_lifetime_bytes
= lifetime
->bytes
.life
;
1557 lft
->sadb_lifetime_addtime
= lifetime
->time
.life
;
1558 lft
->sadb_lifetime_usetime
= 0; /* we only use addtime */
1559 PFKEY_EXT_ADD(msg
, lft
);
1561 if (enc_alg
!= ENCR_UNDEFINED
)
1563 if (!sa
->sadb_sa_encrypt
)
1565 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1566 encryption_algorithm_names
, enc_alg
);
1569 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1570 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1572 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1573 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
1574 key
->sadb_key_bits
= enc_key
.len
* 8;
1575 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + enc_key
.len
);
1576 memcpy(key
+ 1, enc_key
.ptr
, enc_key
.len
);
1578 PFKEY_EXT_ADD(msg
, key
);
1581 if (int_alg
!= AUTH_UNDEFINED
)
1583 if (!sa
->sadb_sa_auth
)
1585 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1586 integrity_algorithm_names
, int_alg
);
1589 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1590 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1592 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1593 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
1594 key
->sadb_key_bits
= int_key
.len
* 8;
1595 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + int_key
.len
);
1596 memcpy(key
+ 1, int_key
.ptr
, int_key
.len
);
1598 PFKEY_EXT_ADD(msg
, key
);
1601 if (ipcomp
!= IPCOMP_NONE
)
1609 add_encap_ext(msg
, src
, dst
);
1611 #endif /*HAVE_NATT*/
1613 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1615 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1618 else if (out
->sadb_msg_errno
)
1620 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x: %s (%d)",
1621 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1630 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1631 private_kernel_pfkey_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1632 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1633 bool encap
, bool new_encap
, mark_t mark
)
1635 unsigned char request
[PFKEY_BUFFER_SIZE
];
1636 struct sadb_msg
*msg
, *out
;
1638 pfkey_msg_t response
;
1641 /* we can't update the SA if any of the ip addresses have changed.
1642 * that's because we can't use SADB_UPDATE and by deleting and readding the
1643 * SA the sequence numbers would get lost */
1644 if (!src
->ip_equals(src
, new_src
) ||
1645 !dst
->ip_equals(dst
, new_dst
))
1647 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: address "
1648 "changes are not supported", ntohl(spi
));
1649 return NOT_SUPPORTED
;
1652 memset(&request
, 0, sizeof(request
));
1654 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1656 msg
= (struct sadb_msg
*)request
;
1657 msg
->sadb_msg_version
= PF_KEY_V2
;
1658 msg
->sadb_msg_type
= SADB_GET
;
1659 msg
->sadb_msg_satype
= proto2satype(protocol
);
1660 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1662 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1663 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1664 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1665 sa
->sadb_sa_spi
= spi
;
1666 PFKEY_EXT_ADD(msg
, sa
);
1668 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1669 * it is not used for anything. */
1670 add_anyaddr_ext(msg
, dst
->get_family(dst
), SADB_EXT_ADDRESS_SRC
);
1671 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
1673 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1675 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1678 else if (out
->sadb_msg_errno
)
1680 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: %s (%d)",
1681 ntohl(spi
), strerror(out
->sadb_msg_errno
),
1682 out
->sadb_msg_errno
);
1686 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1688 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: parsing "
1689 "response from kernel failed", ntohl(spi
));
1694 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1695 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1697 memset(&request
, 0, sizeof(request
));
1699 msg
= (struct sadb_msg
*)request
;
1700 msg
->sadb_msg_version
= PF_KEY_V2
;
1701 msg
->sadb_msg_type
= SADB_UPDATE
;
1702 msg
->sadb_msg_satype
= proto2satype(protocol
);
1703 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1707 struct sadb_sa_2
*sa_2
;
1708 sa_2
= (struct sadb_sa_2
*)PFKEY_EXT_ADD_NEXT(msg
);
1709 sa_2
->sa
.sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa_2
));
1710 memcpy(&sa_2
->sa
, response
.sa
, sizeof(struct sadb_sa
));
1713 sa_2
->sadb_sa_natt_port
= new_dst
->get_port(new_dst
);
1714 sa_2
->sa
.sadb_sa_flags
|= SADB_X_EXT_NATT
;
1718 PFKEY_EXT_COPY(msg
, response
.sa
);
1720 PFKEY_EXT_COPY(msg
, response
.x_sa2
);
1722 PFKEY_EXT_COPY(msg
, response
.src
);
1723 PFKEY_EXT_COPY(msg
, response
.dst
);
1725 PFKEY_EXT_COPY(msg
, response
.lft_soft
);
1726 PFKEY_EXT_COPY(msg
, response
.lft_hard
);
1728 if (response
.key_encr
)
1730 PFKEY_EXT_COPY(msg
, response
.key_encr
);
1733 if (response
.key_auth
)
1735 PFKEY_EXT_COPY(msg
, response
.key_auth
);
1741 add_encap_ext(msg
, new_src
, new_dst
);
1743 #endif /*HAVE_NATT*/
1747 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1749 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1752 else if (out
->sadb_msg_errno
)
1754 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: %s (%d)",
1755 ntohl(spi
), strerror(out
->sadb_msg_errno
),
1756 out
->sadb_msg_errno
);
1765 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1766 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1767 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
1769 unsigned char request
[PFKEY_BUFFER_SIZE
];
1770 struct sadb_msg
*msg
, *out
;
1772 pfkey_msg_t response
;
1775 memset(&request
, 0, sizeof(request
));
1777 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1779 msg
= (struct sadb_msg
*)request
;
1780 msg
->sadb_msg_version
= PF_KEY_V2
;
1781 msg
->sadb_msg_type
= SADB_GET
;
1782 msg
->sadb_msg_satype
= proto2satype(protocol
);
1783 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1785 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1786 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1787 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1788 sa
->sadb_sa_spi
= spi
;
1789 PFKEY_EXT_ADD(msg
, sa
);
1791 /* the Linux Kernel doesn't care for the src address, but other systems do
1794 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
1795 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
1797 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1799 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1802 else if (out
->sadb_msg_errno
)
1804 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: %s (%d)",
1805 ntohl(spi
), strerror(out
->sadb_msg_errno
),
1806 out
->sadb_msg_errno
);
1810 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1812 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1816 *bytes
= response
.lft_current
->sadb_lifetime_bytes
;
1822 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1823 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1824 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1826 unsigned char request
[PFKEY_BUFFER_SIZE
];
1827 struct sadb_msg
*msg
, *out
;
1831 memset(&request
, 0, sizeof(request
));
1833 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1835 msg
= (struct sadb_msg
*)request
;
1836 msg
->sadb_msg_version
= PF_KEY_V2
;
1837 msg
->sadb_msg_type
= SADB_DELETE
;
1838 msg
->sadb_msg_satype
= proto2satype(protocol
);
1839 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1841 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1842 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1843 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1844 sa
->sadb_sa_spi
= spi
;
1845 PFKEY_EXT_ADD(msg
, sa
);
1847 /* the Linux Kernel doesn't care for the src address, but other systems do
1850 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0, FALSE
);
1851 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0, FALSE
);
1853 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1855 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1858 else if (out
->sadb_msg_errno
)
1860 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1861 ntohl(spi
), strerror(out
->sadb_msg_errno
),
1862 out
->sadb_msg_errno
);
1867 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1872 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
1873 private_kernel_pfkey_ipsec_t
*this)
1875 unsigned char request
[PFKEY_BUFFER_SIZE
];
1876 struct sadb_msg
*msg
, *out
;
1879 memset(&request
, 0, sizeof(request
));
1881 DBG2(DBG_KNL
, "flushing all SAD entries");
1883 msg
= (struct sadb_msg
*)request
;
1884 msg
->sadb_msg_version
= PF_KEY_V2
;
1885 msg
->sadb_msg_type
= SADB_FLUSH
;
1886 msg
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
1887 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1889 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1891 DBG1(DBG_KNL
, "unable to flush SAD entries");
1894 else if (out
->sadb_msg_errno
)
1896 DBG1(DBG_KNL
, "unable to flush SAD entries: %s (%d)",
1897 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1906 * Add or update a policy in the kernel.
1908 * Note: The mutex has to be locked when entering this function.
1910 static status_t
add_policy_internal(private_kernel_pfkey_ipsec_t
*this,
1911 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
1913 unsigned char request
[PFKEY_BUFFER_SIZE
];
1914 struct sadb_msg
*msg
, *out
;
1915 struct sadb_x_policy
*pol
;
1916 struct sadb_x_ipsecrequest
*req
;
1917 ipsec_sa_t
*ipsec
= mapping
->sa
;
1918 pfkey_msg_t response
;
1921 memset(&request
, 0, sizeof(request
));
1923 msg
= (struct sadb_msg
*)request
;
1924 msg
->sadb_msg_version
= PF_KEY_V2
;
1925 msg
->sadb_msg_type
= update ? SADB_X_SPDUPDATE
: SADB_X_SPDADD
;
1926 msg
->sadb_msg_satype
= 0;
1927 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1929 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1930 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1931 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1932 pol
->sadb_x_policy_id
= 0;
1933 pol
->sadb_x_policy_dir
= dir2kernel(policy
->direction
);
1934 pol
->sadb_x_policy_type
= type2kernel(mapping
->type
);
1935 #ifdef HAVE_STRUCT_SADB_X_POLICY_SADB_X_POLICY_PRIORITY
1936 pol
->sadb_x_policy_priority
= mapping
->priority
;
1939 /* one or more sadb_x_ipsecrequest extensions are added to the
1940 * sadb_x_policy extension */
1941 req
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
1942 req
->sadb_x_ipsecrequest_proto
= ipsec
->cfg
.esp
.use ? IPPROTO_ESP
1944 /* !!! the length here MUST be in octets instead of 64 bit words */
1945 req
->sadb_x_ipsecrequest_len
= sizeof(struct sadb_x_ipsecrequest
);
1946 req
->sadb_x_ipsecrequest_mode
= mode2kernel(ipsec
->cfg
.mode
);
1947 req
->sadb_x_ipsecrequest_reqid
= ipsec
->cfg
.reqid
;
1948 req
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
1949 if (ipsec
->cfg
.mode
== MODE_TUNNEL
)
1951 len
= hostcpy(req
+ 1, ipsec
->src
, FALSE
);
1952 req
->sadb_x_ipsecrequest_len
+= len
;
1953 len
= hostcpy((char*)(req
+ 1) + len
, ipsec
->dst
, FALSE
);
1954 req
->sadb_x_ipsecrequest_len
+= len
;
1957 pol
->sadb_x_policy_len
+= PFKEY_LEN(req
->sadb_x_ipsecrequest_len
);
1958 PFKEY_EXT_ADD(msg
, pol
);
1960 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
1961 policy
->src
.mask
, TRUE
);
1962 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
1963 policy
->dst
.mask
, TRUE
);
1966 { /* on FreeBSD a lifetime has to be defined to be able to later query
1967 * the current use time. */
1968 struct sadb_lifetime
*lft
;
1969 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1970 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1971 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1972 lft
->sadb_lifetime_addtime
= LONG_MAX
;
1973 PFKEY_EXT_ADD(msg
, lft
);
1977 this->mutex
->unlock(this->mutex
);
1979 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1983 else if (out
->sadb_msg_errno
)
1985 DBG1(DBG_KNL
, "unable to %s policy: %s (%d)",
1986 update ?
"update" : "add", strerror(out
->sadb_msg_errno
),
1987 out
->sadb_msg_errno
);
1991 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1993 DBG1(DBG_KNL
, "unable to %s policy: parsing response from kernel "
1994 "failed", update ?
"update" : "add");
1999 /* we try to find the policy again and update the kernel index */
2000 this->mutex
->lock(this->mutex
);
2001 if (this->policies
->find_last(this->policies
, NULL
,
2002 (void**)&policy
) != SUCCESS
)
2004 DBG2(DBG_KNL
, "unable to update index, the policy is already gone, "
2006 this->mutex
->unlock(this->mutex
);
2010 policy
->index
= response
.x_policy
->sadb_x_policy_id
;
2013 /* install a route, if:
2014 * - this is a forward policy (to just get one for each child)
2015 * - we are in tunnel mode
2016 * - routing is not disabled via strongswan.conf
2018 if (policy
->direction
== POLICY_FWD
&&
2019 ipsec
->cfg
.mode
!= MODE_TRANSPORT
&& this->install_routes
)
2021 route_entry_t
*route
= malloc_thing(route_entry_t
);
2022 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)mapping
;
2024 if (hydra
->kernel_interface
->get_address_by_ts(hydra
->kernel_interface
,
2025 fwd
->dst_ts
, &route
->src_ip
) == SUCCESS
)
2027 /* get the nexthop to src (src as we are in POLICY_FWD).*/
2028 route
->gateway
= hydra
->kernel_interface
->get_nexthop(
2029 hydra
->kernel_interface
, ipsec
->src
,
2031 route
->dst_net
= chunk_clone(policy
->src
.net
->get_address(
2033 route
->prefixlen
= policy
->src
.mask
;
2035 /* install route via outgoing interface */
2036 if (!hydra
->kernel_interface
->get_interface(hydra
->kernel_interface
,
2037 ipsec
->dst
, &route
->if_name
))
2039 this->mutex
->unlock(this->mutex
);
2040 route_entry_destroy(route
);
2046 route_entry_t
*old
= policy
->route
;
2047 if (route_entry_equals(old
, route
))
2049 this->mutex
->unlock(this->mutex
);
2050 route_entry_destroy(route
);
2053 /* uninstall previously installed route */
2054 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2055 old
->dst_net
, old
->prefixlen
, old
->gateway
,
2056 old
->src_ip
, old
->if_name
) != SUCCESS
)
2058 DBG1(DBG_KNL
, "error uninstalling route installed with "
2059 "policy %R === %R %N", fwd
->src_ts
,
2060 fwd
->dst_ts
, policy_dir_names
,
2063 route_entry_destroy(old
);
2064 policy
->route
= NULL
;
2067 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2068 fwd
->src_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2069 switch (hydra
->kernel_interface
->add_route(
2070 hydra
->kernel_interface
, route
->dst_net
,
2071 route
->prefixlen
, route
->gateway
,
2072 route
->src_ip
, route
->if_name
))
2075 DBG1(DBG_KNL
, "unable to install source route for %H",
2079 /* route exists, do not uninstall */
2080 route_entry_destroy(route
);
2083 /* cache the installed route */
2084 policy
->route
= route
;
2093 this->mutex
->unlock(this->mutex
);
2097 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2098 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
2099 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
2100 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
2101 mark_t mark
, policy_priority_t priority
)
2103 policy_entry_t
*policy
, *found
= NULL
;
2104 policy_sa_t
*assigned_sa
, *current_sa
;
2105 enumerator_t
*enumerator
;
2108 if (dir2kernel(direction
) == IPSEC_DIR_INVALID
)
2109 { /* FWD policies are not supported on all platforms */
2113 /* create a policy */
2114 policy
= create_policy_entry(src_ts
, dst_ts
, direction
);
2116 /* find a matching policy */
2117 this->mutex
->lock(this->mutex
);
2118 if (this->policies
->find_first(this->policies
,
2119 (linked_list_match_t
)policy_entry_equals
,
2120 (void**)&found
, policy
) == SUCCESS
)
2121 { /* use existing policy */
2122 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
2123 "refcount", src_ts
, dst_ts
, policy_dir_names
, direction
);
2124 policy_entry_destroy(policy
, this);
2128 { /* use the new one, if we have no such policy */
2129 this->policies
->insert_last(this->policies
, policy
);
2130 policy
->used_by
= linked_list_create();
2133 /* cache the assigned IPsec SA */
2134 assigned_sa
= policy_sa_create(this, direction
, type
, src
, dst
, src_ts
,
2136 assigned_sa
->priority
= get_priority(policy
, priority
);
2138 /* insert the SA according to its priority */
2139 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2140 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2142 if (current_sa
->priority
>= assigned_sa
->priority
)
2148 policy
->used_by
->insert_before(policy
->used_by
, enumerator
, assigned_sa
);
2149 enumerator
->destroy(enumerator
);
2152 { /* we don't update the policy if the priority is lower than that of the
2153 * currently installed one */
2154 this->mutex
->unlock(this->mutex
);
2158 DBG2(DBG_KNL
, "%s policy %R === %R %N",
2159 found ?
"updating" : "adding", src_ts
, dst_ts
,
2160 policy_dir_names
, direction
);
2162 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2164 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N",
2165 found ?
"update" : "add", src_ts
, dst_ts
,
2166 policy_dir_names
, direction
);
2172 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2173 private_kernel_pfkey_ipsec_t
*this, traffic_selector_t
*src_ts
,
2174 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
2175 u_int32_t
*use_time
)
2177 unsigned char request
[PFKEY_BUFFER_SIZE
];
2178 struct sadb_msg
*msg
, *out
;
2179 struct sadb_x_policy
*pol
;
2180 policy_entry_t
*policy
, *found
= NULL
;
2181 pfkey_msg_t response
;
2184 if (dir2kernel(direction
) == IPSEC_DIR_INVALID
)
2185 { /* FWD policies are not supported on all platforms */
2189 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
2190 policy_dir_names
, direction
);
2192 /* create a policy */
2193 policy
= create_policy_entry(src_ts
, dst_ts
, direction
);
2195 /* find a matching policy */
2196 this->mutex
->lock(this->mutex
);
2197 if (this->policies
->find_first(this->policies
,
2198 (linked_list_match_t
)policy_entry_equals
,
2199 (void**)&found
, policy
) != SUCCESS
)
2201 DBG1(DBG_KNL
, "querying policy %R === %R %N failed, not found", src_ts
,
2202 dst_ts
, policy_dir_names
, direction
);
2203 policy_entry_destroy(policy
, this);
2204 this->mutex
->unlock(this->mutex
);
2207 policy_entry_destroy(policy
, this);
2210 memset(&request
, 0, sizeof(request
));
2212 msg
= (struct sadb_msg
*)request
;
2213 msg
->sadb_msg_version
= PF_KEY_V2
;
2214 msg
->sadb_msg_type
= SADB_X_SPDGET
;
2215 msg
->sadb_msg_satype
= 0;
2216 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2218 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
2219 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2220 pol
->sadb_x_policy_id
= policy
->index
;
2221 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
2222 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
2223 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2224 PFKEY_EXT_ADD(msg
, pol
);
2226 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
2227 policy
->src
.mask
, TRUE
);
2228 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
2229 policy
->dst
.mask
, TRUE
);
2231 this->mutex
->unlock(this->mutex
);
2233 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2235 DBG1(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
2236 policy_dir_names
, direction
);
2239 else if (out
->sadb_msg_errno
)
2241 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: %s (%d)", src_ts
,
2242 dst_ts
, policy_dir_names
, direction
,
2243 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2247 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
2249 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: parsing response "
2250 "from kernel failed", src_ts
, dst_ts
, policy_dir_names
,
2255 else if (response
.lft_current
== NULL
)
2257 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: kernel reports no "
2258 "use time", src_ts
, dst_ts
, policy_dir_names
, direction
);
2263 /* we need the monotonic time, but the kernel returns system time. */
2264 if (response
.lft_current
->sadb_lifetime_usetime
)
2266 *use_time
= time_monotonic(NULL
) -
2267 (time(NULL
) - response
.lft_current
->sadb_lifetime_usetime
);
2277 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2278 private_kernel_pfkey_ipsec_t
*this, traffic_selector_t
*src_ts
,
2279 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
2280 mark_t mark
, policy_priority_t prio
)
2282 unsigned char request
[PFKEY_BUFFER_SIZE
];
2283 struct sadb_msg
*msg
, *out
;
2284 struct sadb_x_policy
*pol
;
2285 policy_entry_t
*policy
, *found
= NULL
;
2286 policy_sa_t
*mapping
;
2287 enumerator_t
*enumerator
;
2288 bool is_installed
= TRUE
;
2292 if (dir2kernel(direction
) == IPSEC_DIR_INVALID
)
2293 { /* FWD policies are not supported on all platforms */
2297 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
2298 policy_dir_names
, direction
);
2300 /* create a policy */
2301 policy
= create_policy_entry(src_ts
, dst_ts
, direction
);
2303 /* find a matching policy */
2304 this->mutex
->lock(this->mutex
);
2305 if (this->policies
->find_first(this->policies
,
2306 (linked_list_match_t
)policy_entry_equals
,
2307 (void**)&found
, policy
) != SUCCESS
)
2309 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
2310 dst_ts
, policy_dir_names
, direction
);
2311 policy_entry_destroy(policy
, this);
2312 this->mutex
->unlock(this->mutex
);
2315 policy_entry_destroy(policy
, this);
2318 /* remove mapping to SA by reqid and priority */
2319 priority
= get_priority(policy
, prio
);
2320 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2321 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2323 if (reqid
== mapping
->sa
->cfg
.reqid
&& priority
== mapping
->priority
)
2325 policy
->used_by
->remove_at(policy
->used_by
, enumerator
);
2328 is_installed
= FALSE
;
2330 enumerator
->destroy(enumerator
);
2332 if (policy
->used_by
->get_count(policy
->used_by
) > 0)
2333 { /* policy is used by more SAs, keep in kernel */
2334 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2335 policy_sa_destroy(mapping
, &direction
, this);
2338 { /* no need to update as the policy was not installed for this SA */
2339 this->mutex
->unlock(this->mutex
);
2343 DBG2(DBG_KNL
, "updating policy %R === %R %N", src_ts
, dst_ts
,
2344 policy_dir_names
, direction
);
2345 policy
->used_by
->get_first(policy
->used_by
, (void**)&mapping
);
2346 if (add_policy_internal(this, policy
, mapping
, TRUE
) != SUCCESS
)
2348 DBG1(DBG_KNL
, "unable to update policy %R === %R %N",
2349 src_ts
, dst_ts
, policy_dir_names
, direction
);
2355 memset(&request
, 0, sizeof(request
));
2357 msg
= (struct sadb_msg
*)request
;
2358 msg
->sadb_msg_version
= PF_KEY_V2
;
2359 msg
->sadb_msg_type
= SADB_X_SPDDELETE
;
2360 msg
->sadb_msg_satype
= 0;
2361 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2363 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
2364 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2365 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
2366 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
2367 pol
->sadb_x_policy_type
= type2kernel(mapping
->type
);
2368 PFKEY_EXT_ADD(msg
, pol
);
2370 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
2371 policy
->src
.mask
, TRUE
);
2372 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
2373 policy
->dst
.mask
, TRUE
);
2377 route_entry_t
*route
= policy
->route
;
2378 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2379 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2380 route
->src_ip
, route
->if_name
) != SUCCESS
)
2382 DBG1(DBG_KNL
, "error uninstalling route installed with "
2383 "policy %R === %R %N", src_ts
, dst_ts
,
2384 policy_dir_names
, direction
);
2388 this->policies
->remove(this->policies
, found
, NULL
);
2389 policy_sa_destroy(mapping
, &direction
, this);
2390 policy_entry_destroy(policy
, this);
2391 this->mutex
->unlock(this->mutex
);
2393 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2395 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
2396 policy_dir_names
, direction
);
2399 else if (out
->sadb_msg_errno
)
2401 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N: %s (%d)", src_ts
,
2402 dst_ts
, policy_dir_names
, direction
,
2403 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2411 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
2412 private_kernel_pfkey_ipsec_t
*this)
2414 unsigned char request
[PFKEY_BUFFER_SIZE
];
2415 struct sadb_msg
*msg
, *out
;
2418 memset(&request
, 0, sizeof(request
));
2420 DBG2(DBG_KNL
, "flushing all policies from SPD");
2422 msg
= (struct sadb_msg
*)request
;
2423 msg
->sadb_msg_version
= PF_KEY_V2
;
2424 msg
->sadb_msg_type
= SADB_X_SPDFLUSH
;
2425 msg
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2426 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2428 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2430 DBG1(DBG_KNL
, "unable to flush SPD entries");
2433 else if (out
->sadb_msg_errno
)
2435 DBG1(DBG_KNL
, "unable to flush SPD entries: %s (%d)",
2436 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2445 * Register a socket for ACQUIRE/EXPIRE messages
2447 static status_t
register_pfkey_socket(private_kernel_pfkey_ipsec_t
*this,
2450 unsigned char request
[PFKEY_BUFFER_SIZE
];
2451 struct sadb_msg
*msg
, *out
;
2454 memset(&request
, 0, sizeof(request
));
2456 msg
= (struct sadb_msg
*)request
;
2457 msg
->sadb_msg_version
= PF_KEY_V2
;
2458 msg
->sadb_msg_type
= SADB_REGISTER
;
2459 msg
->sadb_msg_satype
= satype
;
2460 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2462 if (pfkey_send_socket(this, this->socket_events
, msg
, &out
, &len
) != SUCCESS
)
2464 DBG1(DBG_KNL
, "unable to register PF_KEY socket");
2467 else if (out
->sadb_msg_errno
)
2469 DBG1(DBG_KNL
, "unable to register PF_KEY socket: %s (%d)",
2470 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2478 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2479 private_kernel_pfkey_ipsec_t
*this, int fd
, int family
)
2481 struct sadb_x_policy policy
;
2482 u_int sol
, ipsec_policy
;
2489 ipsec_policy
= IP_IPSEC_POLICY
;
2495 ipsec_policy
= IPV6_IPSEC_POLICY
;
2502 memset(&policy
, 0, sizeof(policy
));
2503 policy
.sadb_x_policy_len
= sizeof(policy
) / sizeof(u_int64_t
);
2504 policy
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2505 policy
.sadb_x_policy_type
= IPSEC_POLICY_BYPASS
;
2507 policy
.sadb_x_policy_dir
= IPSEC_DIR_OUTBOUND
;
2508 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2510 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2514 policy
.sadb_x_policy_dir
= IPSEC_DIR_INBOUND
;
2515 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2517 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2524 METHOD(kernel_ipsec_t
, enable_udp_decap
, bool,
2525 private_kernel_pfkey_ipsec_t
*this, int fd
, int family
, u_int16_t port
)
2528 int type
= UDP_ENCAP_ESPINUDP
;
2530 if (setsockopt(fd
, SOL_UDP
, UDP_ENCAP
, &type
, sizeof(type
)) < 0)
2532 DBG1(DBG_KNL
, "unable to set UDP_ENCAP: %s", strerror(errno
));
2535 #else /* __APPLE__ */
2536 if (sysctlbyname("net.inet.ipsec.esp_port", NULL
, NULL
, &port
,
2539 DBG1(DBG_KNL
, "could not set net.inet.ipsec.esp_port to %d: %s",
2540 port
, strerror(errno
));
2543 #endif /* __APPLE__ */
2548 METHOD(kernel_ipsec_t
, destroy
, void,
2549 private_kernel_pfkey_ipsec_t
*this)
2551 if (this->socket
> 0)
2553 close(this->socket
);
2555 if (this->socket_events
> 0)
2557 close(this->socket_events
);
2559 this->policies
->invoke_function(this->policies
,
2560 (linked_list_invoke_t
)policy_entry_destroy
,
2562 this->policies
->destroy(this->policies
);
2563 this->sas
->destroy(this->sas
);
2564 this->mutex
->destroy(this->mutex
);
2565 this->mutex_pfkey
->destroy(this->mutex_pfkey
);
2570 * Described in header.
2572 kernel_pfkey_ipsec_t
*kernel_pfkey_ipsec_create()
2574 private_kernel_pfkey_ipsec_t
*this;
2575 bool register_for_events
= TRUE
;
2580 .get_spi
= _get_spi
,
2581 .get_cpi
= _get_cpi
,
2583 .update_sa
= _update_sa
,
2584 .query_sa
= _query_sa
,
2586 .flush_sas
= _flush_sas
,
2587 .add_policy
= _add_policy
,
2588 .query_policy
= _query_policy
,
2589 .del_policy
= _del_policy
,
2590 .flush_policies
= _flush_policies
,
2591 .bypass_socket
= _bypass_socket
,
2592 .enable_udp_decap
= _enable_udp_decap
,
2593 .destroy
= _destroy
,
2596 .policies
= linked_list_create(),
2597 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
2598 (hashtable_equals_t
)ipsec_sa_equals
, 32),
2599 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2600 .mutex_pfkey
= mutex_create(MUTEX_TYPE_DEFAULT
),
2601 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2602 "%s.install_routes", TRUE
,
2606 if (streq(hydra
->daemon
, "pluto"))
2607 { /* no routes for pluto, they are installed via updown script */
2608 this->install_routes
= FALSE
;
2610 else if (streq(hydra
->daemon
, "starter"))
2611 { /* starter has no threads, so we do not register for kernel events */
2612 register_for_events
= FALSE
;
2615 /* create a PF_KEY socket to communicate with the kernel */
2616 this->socket
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
2617 if (this->socket
<= 0)
2619 DBG1(DBG_KNL
, "unable to create PF_KEY socket");
2624 if (register_for_events
)
2626 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
2627 this->socket_events
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
2628 if (this->socket_events
<= 0)
2630 DBG1(DBG_KNL
, "unable to create PF_KEY event socket");
2635 /* register the event socket */
2636 if (register_pfkey_socket(this, SADB_SATYPE_ESP
) != SUCCESS
||
2637 register_pfkey_socket(this, SADB_SATYPE_AH
) != SUCCESS
)
2639 DBG1(DBG_KNL
, "unable to register PF_KEY event socket");
2644 lib
->processor
->queue_job(lib
->processor
,
2645 (job_t
*)callback_job_create_with_prio(
2646 (callback_job_cb_t
)receive_events
, this, NULL
,
2647 (callback_job_cancel_t
)return_false
, JOB_PRIO_CRITICAL
));
2650 return &this->public;