2 * Copyright (C) 2008-2010 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 "kernel_pfkey_ipsec.h"
59 #include <utils/host.h>
60 #include <utils/linked_list.h>
61 #include <threading/thread.h>
62 #include <threading/mutex.h>
63 #include <processing/jobs/callback_job.h>
65 /** non linux specific */
68 #define IPPROTO_COMP IPPROTO_IPCOMP
72 #ifndef SADB_X_AALG_SHA2_256HMAC
73 #define SADB_X_AALG_SHA2_256HMAC SADB_X_AALG_SHA2_256
74 #define SADB_X_AALG_SHA2_384HMAC SADB_X_AALG_SHA2_384
75 #define SADB_X_AALG_SHA2_512HMAC SADB_X_AALG_SHA2_512
78 #ifndef SADB_X_EALG_AESCBC
79 #define SADB_X_EALG_AESCBC SADB_X_EALG_AES
82 #ifndef SADB_X_EALG_CASTCBC
83 #define SADB_X_EALG_CASTCBC SADB_X_EALG_CAST128CBC
87 #define SOL_IP IPPROTO_IP
88 #define SOL_IPV6 IPPROTO_IPV6
91 /** from linux/in.h */
92 #ifndef IP_IPSEC_POLICY
93 #define IP_IPSEC_POLICY 16
96 /** missing on uclibc */
97 #ifndef IPV6_IPSEC_POLICY
98 #define IPV6_IPSEC_POLICY 34
101 /** default priority of installed policies */
102 #define PRIO_LOW 1024
103 #define PRIO_HIGH 512
106 /** from xnu/bsd/net/pfkeyv2.h */
107 #define SADB_X_EXT_NATT 0x002
110 u_int16_t sadb_sa_natt_port
;
111 u_int16_t sadb_reserved0
;
112 u_int32_t sadb_reserved1
;
116 /** buffer size for PF_KEY messages */
117 #define PFKEY_BUFFER_SIZE 4096
119 /** PF_KEY messages are 64 bit aligned */
120 #define PFKEY_ALIGNMENT 8
121 /** aligns len to 64 bits */
122 #define PFKEY_ALIGN(len) (((len) + PFKEY_ALIGNMENT - 1) & ~(PFKEY_ALIGNMENT - 1))
123 /** calculates the properly padded length in 64 bit chunks */
124 #define PFKEY_LEN(len) ((PFKEY_ALIGN(len) / PFKEY_ALIGNMENT))
125 /** calculates user mode length i.e. in bytes */
126 #define PFKEY_USER_LEN(len) ((len) * PFKEY_ALIGNMENT)
128 /** given a PF_KEY message header and an extension this updates the length in the header */
129 #define PFKEY_EXT_ADD(msg, ext) ((msg)->sadb_msg_len += ((struct sadb_ext*)ext)->sadb_ext_len)
130 /** given a PF_KEY message header this returns a pointer to the next extension */
131 #define PFKEY_EXT_ADD_NEXT(msg) ((struct sadb_ext*)(((char*)(msg)) + PFKEY_USER_LEN((msg)->sadb_msg_len)))
132 /** copy an extension and append it to a PF_KEY message */
133 #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))))
134 /** given a PF_KEY extension this returns a pointer to the next extension */
135 #define PFKEY_EXT_NEXT(ext) ((struct sadb_ext*)(((char*)(ext)) + PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len)))
136 /** given a PF_KEY extension this returns a pointer to the next extension also updates len (len in 64 bit words) */
137 #define PFKEY_EXT_NEXT_LEN(ext,len) ((len) -= (ext)->sadb_ext_len, PFKEY_EXT_NEXT(ext))
138 /** true if ext has a valid length and len is large enough to contain ext (assuming len in 64 bit words) */
139 #define PFKEY_EXT_OK(ext,len) ((len) >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
140 (ext)->sadb_ext_len >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
141 (ext)->sadb_ext_len <= (len))
143 typedef struct private_kernel_pfkey_ipsec_t private_kernel_pfkey_ipsec_t
;
146 * Private variables and functions of kernel_pfkey class.
148 struct private_kernel_pfkey_ipsec_t
151 * Public part of the kernel_pfkey_t object.
153 kernel_pfkey_ipsec_t
public;
156 * mutex to lock access to various lists
161 * List of installed policies (policy_entry_t)
163 linked_list_t
*policies
;
166 * whether to install routes along policies
171 * job receiving PF_KEY events
176 * mutex to lock access to the PF_KEY socket
178 mutex_t
*mutex_pfkey
;
181 * PF_KEY socket to communicate with the kernel
186 * PF_KEY socket to receive acquire and expire events
191 * sequence number for messages sent to the kernel
196 typedef struct route_entry_t route_entry_t
;
199 * installed routing entry
201 struct route_entry_t
{
202 /** Name of the interface the route is bound to */
205 /** Source ip of the route */
208 /** gateway for this route */
211 /** Destination net */
214 /** Destination net prefixlen */
219 * destroy an route_entry_t object
221 static void route_entry_destroy(route_entry_t
*this)
224 DESTROY_IF(this->src_ip
);
225 DESTROY_IF(this->gateway
);
226 chunk_free(&this->dst_net
);
230 typedef struct policy_entry_t policy_entry_t
;
233 * installed kernel policy.
235 struct policy_entry_t
{
237 /** reqid of this policy */
240 /** index assigned by the kernel */
243 /** direction of this policy: in, out, forward */
246 /** parameters of installed policy */
248 /** subnet and port */
256 /** associated route installed for this policy */
257 route_entry_t
*route
;
259 /** by how many CHILD_SA's this policy is used */
264 * create a policy_entry_t object
266 static policy_entry_t
*create_policy_entry(traffic_selector_t
*src_ts
,
267 traffic_selector_t
*dst_ts
, policy_dir_t dir
, u_int32_t reqid
)
269 policy_entry_t
*policy
= malloc_thing(policy_entry_t
);
270 policy
->reqid
= reqid
;
272 policy
->direction
= dir
;
273 policy
->route
= NULL
;
274 policy
->refcount
= 0;
276 src_ts
->to_subnet(src_ts
, &policy
->src
.net
, &policy
->src
.mask
);
277 dst_ts
->to_subnet(dst_ts
, &policy
->dst
.net
, &policy
->dst
.mask
);
279 /* src or dest proto may be "any" (0), use more restrictive one */
280 policy
->src
.proto
= max(src_ts
->get_protocol(src_ts
), dst_ts
->get_protocol(dst_ts
));
281 policy
->src
.proto
= policy
->src
.proto ? policy
->src
.proto
: IPSEC_PROTO_ANY
;
282 policy
->dst
.proto
= policy
->src
.proto
;
288 * destroy a policy_entry_t object
290 static void policy_entry_destroy(policy_entry_t
*this)
292 DESTROY_IF(this->src
.net
);
293 DESTROY_IF(this->dst
.net
);
296 route_entry_destroy(this->route
);
302 * compares two policy_entry_t
304 static inline bool policy_entry_equals(policy_entry_t
*current
, policy_entry_t
*policy
)
306 return current
->direction
== policy
->direction
&&
307 current
->src
.proto
== policy
->src
.proto
&&
308 current
->dst
.proto
== policy
->dst
.proto
&&
309 current
->src
.mask
== policy
->src
.mask
&&
310 current
->dst
.mask
== policy
->dst
.mask
&&
311 current
->src
.net
->equals(current
->src
.net
, policy
->src
.net
) &&
312 current
->dst
.net
->equals(current
->dst
.net
, policy
->dst
.net
);
316 * compare the given kernel index with that of a policy
318 static inline bool policy_entry_match_byindex(policy_entry_t
*current
, u_int32_t
*index
)
320 return current
->index
== *index
;
323 typedef struct pfkey_msg_t pfkey_msg_t
;
328 * PF_KEY message base
330 struct sadb_msg
*msg
;
333 * PF_KEY message extensions
336 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
338 struct sadb_ext
*reserved
; /* SADB_EXT_RESERVED */
339 struct sadb_sa
*sa
; /* SADB_EXT_SA */
340 struct sadb_lifetime
*lft_current
; /* SADB_EXT_LIFETIME_CURRENT */
341 struct sadb_lifetime
*lft_hard
; /* SADB_EXT_LIFETIME_HARD */
342 struct sadb_lifetime
*lft_soft
; /* SADB_EXT_LIFETIME_SOFT */
343 struct sadb_address
*src
; /* SADB_EXT_ADDRESS_SRC */
344 struct sadb_address
*dst
; /* SADB_EXT_ADDRESS_DST */
345 struct sadb_address
*proxy
; /* SADB_EXT_ADDRESS_PROXY */
346 struct sadb_key
*key_auth
; /* SADB_EXT_KEY_AUTH */
347 struct sadb_key
*key_encr
; /* SADB_EXT_KEY_ENCRYPT */
348 struct sadb_ident
*id_src
; /* SADB_EXT_IDENTITY_SRC */
349 struct sadb_ident
*id_dst
; /* SADB_EXT_IDENTITY_DST */
350 struct sadb_sens
*sensitivity
; /* SADB_EXT_SENSITIVITY */
351 struct sadb_prop
*proposal
; /* SADB_EXT_PROPOSAL */
352 struct sadb_supported
*supported_auth
; /* SADB_EXT_SUPPORTED_AUTH */
353 struct sadb_supported
*supported_encr
; /* SADB_EXT_SUPPORTED_ENCRYPT */
354 struct sadb_spirange
*spirange
; /* SADB_EXT_SPIRANGE */
355 struct sadb_x_kmprivate
*x_kmprivate
; /* SADB_X_EXT_KMPRIVATE */
356 struct sadb_x_policy
*x_policy
; /* SADB_X_EXT_POLICY */
357 struct sadb_x_sa2
*x_sa2
; /* SADB_X_EXT_SA2 */
358 struct sadb_x_nat_t_type
*x_natt_type
; /* SADB_X_EXT_NAT_T_TYPE */
359 struct sadb_x_nat_t_port
*x_natt_sport
; /* SADB_X_EXT_NAT_T_SPORT */
360 struct sadb_x_nat_t_port
*x_natt_dport
; /* SADB_X_EXT_NAT_T_DPORT */
361 struct sadb_address
*x_natt_oa
; /* SADB_X_EXT_NAT_T_OA */
362 struct sadb_x_sec_ctx
*x_sec_ctx
; /* SADB_X_EXT_SEC_CTX */
363 struct sadb_x_kmaddress
*x_kmaddress
; /* SADB_X_EXT_KMADDRESS */
364 } __attribute__((__packed__
));
368 ENUM(sadb_ext_type_names
, SADB_EXT_RESERVED
, SADB_EXT_MAX
,
371 "SADB_EXT_LIFETIME_CURRENT",
372 "SADB_EXT_LIFETIME_HARD",
373 "SADB_EXT_LIFETIME_SOFT",
374 "SADB_EXT_ADDRESS_SRC",
375 "SADB_EXT_ADDRESS_DST",
376 "SADB_EXT_ADDRESS_PROXY",
378 "SADB_EXT_KEY_ENCRYPT",
379 "SADB_EXT_IDENTITY_SRC",
380 "SADB_EXT_IDENTITY_DST",
381 "SADB_EXT_SENSITIVITY",
383 "SADB_EXT_SUPPORTED_AUTH",
384 "SADB_EXT_SUPPORTED_ENCRYPT",
386 "SADB_X_EXT_KMPRIVATE",
389 "SADB_X_EXT_NAT_T_TYPE",
390 "SADB_X_EXT_NAT_T_SPORT",
391 "SADB_X_EXT_NAT_T_DPORT",
392 "SADB_X_EXT_NAT_T_OA",
393 "SADB_X_EXT_SEC_CTX",
394 "SADB_X_EXT_KMADDRESS"
398 * convert a protocol identifier to the PF_KEY sa type
400 static u_int8_t
proto2satype(u_int8_t proto
)
405 return SADB_SATYPE_ESP
;
407 return SADB_SATYPE_AH
;
409 return SADB_X_SATYPE_IPCOMP
;
416 * convert a PF_KEY sa type to a protocol identifier
418 static u_int8_t
satype2proto(u_int8_t satype
)
422 case SADB_SATYPE_ESP
:
426 case SADB_X_SATYPE_IPCOMP
:
434 * convert the general ipsec mode to the one defined in ipsec.h
436 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
441 return IPSEC_MODE_TRANSPORT
;
443 return IPSEC_MODE_TUNNEL
;
444 #ifdef HAVE_IPSEC_MODE_BEET
446 return IPSEC_MODE_BEET
;
454 * convert the general policy direction to the one defined in ipsec.h
456 static u_int8_t
dir2kernel(policy_dir_t dir
)
461 return IPSEC_DIR_INBOUND
;
463 return IPSEC_DIR_OUTBOUND
;
464 #ifdef HAVE_IPSEC_DIR_FWD
466 return IPSEC_DIR_FWD
;
469 return IPSEC_DIR_INVALID
;
473 #ifdef SADB_X_MIGRATE
475 * convert the policy direction in ipsec.h to the general one.
477 static policy_dir_t
kernel2dir(u_int8_t dir
)
481 case IPSEC_DIR_INBOUND
:
483 case IPSEC_DIR_OUTBOUND
:
485 #ifdef HAVE_IPSEC_DIR_FWD
493 #endif /*SADB_X_MIGRATE*/
495 typedef struct kernel_algorithm_t kernel_algorithm_t
;
498 * Mapping of IKEv2 algorithms to PF_KEY algorithms
500 struct kernel_algorithm_t
{
502 * Identifier specified in IKEv2
507 * Identifier as defined in pfkeyv2.h
512 #define END_OF_LIST -1
515 * Algorithms for encryption
517 static kernel_algorithm_t encryption_algs
[] = {
518 /* {ENCR_DES_IV64, 0 }, */
519 {ENCR_DES
, SADB_EALG_DESCBC
},
520 {ENCR_3DES
, SADB_EALG_3DESCBC
},
521 /* {ENCR_RC5, 0 }, */
522 /* {ENCR_IDEA, 0 }, */
523 {ENCR_CAST
, SADB_X_EALG_CASTCBC
},
524 {ENCR_BLOWFISH
, SADB_X_EALG_BLOWFISHCBC
},
525 /* {ENCR_3IDEA, 0 }, */
526 /* {ENCR_DES_IV32, 0 }, */
527 {ENCR_NULL
, SADB_EALG_NULL
},
528 {ENCR_AES_CBC
, SADB_X_EALG_AESCBC
},
529 /* {ENCR_AES_CTR, SADB_X_EALG_AESCTR }, */
530 /* {ENCR_AES_CCM_ICV8, SADB_X_EALG_AES_CCM_ICV8 }, */
531 /* {ENCR_AES_CCM_ICV12, SADB_X_EALG_AES_CCM_ICV12 }, */
532 /* {ENCR_AES_CCM_ICV16, SADB_X_EALG_AES_CCM_ICV16 }, */
533 /* {ENCR_AES_GCM_ICV8, SADB_X_EALG_AES_GCM_ICV8 }, */
534 /* {ENCR_AES_GCM_ICV12, SADB_X_EALG_AES_GCM_ICV12 }, */
535 /* {ENCR_AES_GCM_ICV16, SADB_X_EALG_AES_GCM_ICV16 }, */
540 * Algorithms for integrity protection
542 static kernel_algorithm_t integrity_algs
[] = {
543 {AUTH_HMAC_MD5_96
, SADB_AALG_MD5HMAC
},
544 {AUTH_HMAC_SHA1_96
, SADB_AALG_SHA1HMAC
},
545 {AUTH_HMAC_SHA2_256_128
, SADB_X_AALG_SHA2_256HMAC
},
546 {AUTH_HMAC_SHA2_384_192
, SADB_X_AALG_SHA2_384HMAC
},
547 {AUTH_HMAC_SHA2_512_256
, SADB_X_AALG_SHA2_512HMAC
},
548 /* {AUTH_DES_MAC, 0, }, */
549 /* {AUTH_KPDK_MD5, 0, }, */
550 #ifdef SADB_X_AALG_AES_XCBC_MAC
551 {AUTH_AES_XCBC_96
, SADB_X_AALG_AES_XCBC_MAC
, },
558 * Algorithms for IPComp, unused yet
560 static kernel_algorithm_t compression_algs
[] = {
561 /* {IPCOMP_OUI, 0 }, */
562 {IPCOMP_DEFLATE
, SADB_X_CALG_DEFLATE
},
563 {IPCOMP_LZS
, SADB_X_CALG_LZS
},
564 {IPCOMP_LZJH
, SADB_X_CALG_LZJH
},
570 * Look up a kernel algorithm ID and its key size
572 static int lookup_algorithm(kernel_algorithm_t
*list
, int ikev2
)
574 while (list
->ikev2
!= END_OF_LIST
)
576 if (ikev2
== list
->ikev2
)
586 * Copy a host_t as sockaddr_t to the given memory location. Ports are
587 * reset to zero as per RFC 2367.
588 * @return the number of bytes copied
590 static size_t hostcpy(void *dest
, host_t
*host
)
592 sockaddr_t
*addr
= host
->get_sockaddr(host
), *dest_addr
= dest
;
593 socklen_t
*len
= host
->get_sockaddr_len(host
);
594 memcpy(dest
, addr
, *len
);
595 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
596 dest_addr
->sa_len
= *len
;
598 switch (dest_addr
->sa_family
)
602 struct sockaddr_in
*sin
= dest
;
608 struct sockaddr_in6
*sin6
= dest
;
617 * add a host behind an sadb_address extension
619 static void host2ext(host_t
*host
, struct sadb_address
*ext
)
621 size_t len
= hostcpy(ext
+ 1, host
);
622 ext
->sadb_address_len
= PFKEY_LEN(sizeof(*ext
) + len
);
626 * add a host to the given sadb_msg
628 static void add_addr_ext(struct sadb_msg
*msg
, host_t
*host
, u_int16_t type
,
629 u_int8_t proto
, u_int8_t prefixlen
)
631 struct sadb_address
*addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
632 addr
->sadb_address_exttype
= type
;
633 addr
->sadb_address_proto
= proto
;
634 addr
->sadb_address_prefixlen
= prefixlen
;
635 host2ext(host
, addr
);
636 PFKEY_EXT_ADD(msg
, addr
);
640 * adds an empty address extension to the given sadb_msg
642 static void add_anyaddr_ext(struct sadb_msg
*msg
, int family
, u_int8_t type
)
644 socklen_t len
= (family
== AF_INET
) ?
sizeof(struct sockaddr_in
) :
645 sizeof(struct sockaddr_in6
);
646 struct sadb_address
*addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
647 addr
->sadb_address_exttype
= type
;
648 sockaddr_t
*saddr
= (sockaddr_t
*)(addr
+ 1);
649 saddr
->sa_family
= family
;
650 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
653 addr
->sadb_address_len
= PFKEY_LEN(sizeof(*addr
) + len
);
654 PFKEY_EXT_ADD(msg
, addr
);
659 * add udp encap extensions to a sadb_msg
661 static void add_encap_ext(struct sadb_msg
*msg
, host_t
*src
, host_t
*dst
)
663 struct sadb_x_nat_t_type
* nat_type
;
664 struct sadb_x_nat_t_port
* nat_port
;
666 nat_type
= (struct sadb_x_nat_t_type
*)PFKEY_EXT_ADD_NEXT(msg
);
667 nat_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
668 nat_type
->sadb_x_nat_t_type_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_type
));
669 nat_type
->sadb_x_nat_t_type_type
= UDP_ENCAP_ESPINUDP
;
670 PFKEY_EXT_ADD(msg
, nat_type
);
672 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
673 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
674 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_port
));
675 nat_port
->sadb_x_nat_t_port_port
= htons(src
->get_port(src
));
676 PFKEY_EXT_ADD(msg
, nat_port
);
678 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
679 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
680 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_port
));
681 nat_port
->sadb_x_nat_t_port_port
= htons(dst
->get_port(dst
));
682 PFKEY_EXT_ADD(msg
, nat_port
);
687 * Convert a sadb_address to a traffic_selector
689 static traffic_selector_t
* sadb_address2ts(struct sadb_address
*address
)
691 traffic_selector_t
*ts
;
694 /* The Linux 2.6 kernel does not set the protocol and port information
695 * in the src and dst sadb_address extensions of the SADB_ACQUIRE message.
697 host
= host_create_from_sockaddr((sockaddr_t
*)&address
[1]) ;
698 ts
= traffic_selector_create_from_subnet(host
, address
->sadb_address_prefixlen
,
699 address
->sadb_address_proto
, host
->get_port(host
));
704 * Parses a pfkey message received from the kernel
706 static status_t
parse_pfkey_message(struct sadb_msg
*msg
, pfkey_msg_t
*out
)
708 struct sadb_ext
* ext
;
711 memset(out
, 0, sizeof(pfkey_msg_t
));
714 len
= msg
->sadb_msg_len
;
715 len
-= PFKEY_LEN(sizeof(struct sadb_msg
));
717 ext
= (struct sadb_ext
*)(((char*)msg
) + sizeof(struct sadb_msg
));
719 while (len
>= PFKEY_LEN(sizeof(struct sadb_ext
)))
721 DBG3(DBG_KNL
, " %N", sadb_ext_type_names
, ext
->sadb_ext_type
);
722 if (ext
->sadb_ext_len
< PFKEY_LEN(sizeof(struct sadb_ext
)) ||
723 ext
->sadb_ext_len
> len
)
725 DBG1(DBG_KNL
, "length of %N extension is invalid",
726 sadb_ext_type_names
, ext
->sadb_ext_type
);
730 if ((ext
->sadb_ext_type
> SADB_EXT_MAX
) || (!ext
->sadb_ext_type
))
732 DBG1(DBG_KNL
, "type of PF_KEY extension (%d) is invalid", ext
->sadb_ext_type
);
736 if (out
->ext
[ext
->sadb_ext_type
])
738 DBG1(DBG_KNL
, "duplicate %N extension",
739 sadb_ext_type_names
, ext
->sadb_ext_type
);
743 out
->ext
[ext
->sadb_ext_type
] = ext
;
744 ext
= PFKEY_EXT_NEXT_LEN(ext
, len
);
749 DBG1(DBG_KNL
, "PF_KEY message length is invalid");
757 * Send a message to a specific PF_KEY socket and handle the response.
759 static status_t
pfkey_send_socket(private_kernel_pfkey_ipsec_t
*this, int socket
,
760 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
762 unsigned char buf
[PFKEY_BUFFER_SIZE
];
763 struct sadb_msg
*msg
;
766 this->mutex_pfkey
->lock(this->mutex_pfkey
);
768 /* FIXME: our usage of sequence numbers is probably wrong. check RFC 2367,
769 * in particular the behavior in response to an SADB_ACQUIRE. */
770 in
->sadb_msg_seq
= ++this->seq
;
771 in
->sadb_msg_pid
= getpid();
773 in_len
= PFKEY_USER_LEN(in
->sadb_msg_len
);
777 len
= send(socket
, in
, in_len
, 0);
783 /* interrupted, try again */
786 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
787 DBG1(DBG_KNL
, "error sending to PF_KEY socket: %s", strerror(errno
));
795 msg
= (struct sadb_msg
*)buf
;
797 len
= recv(socket
, buf
, sizeof(buf
), 0);
803 DBG1(DBG_KNL
, "got interrupted");
804 /* interrupted, try again */
807 DBG1(DBG_KNL
, "error reading from PF_KEY socket: %s", strerror(errno
));
808 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
811 if (len
< sizeof(struct sadb_msg
) ||
812 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
814 DBG1(DBG_KNL
, "received corrupted PF_KEY message");
815 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
818 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
820 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY message");
821 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
824 if (msg
->sadb_msg_pid
!= in
->sadb_msg_pid
)
826 DBG2(DBG_KNL
, "received PF_KEY message is not intended for us");
829 if (msg
->sadb_msg_seq
!= this->seq
)
831 DBG1(DBG_KNL
, "received PF_KEY message with unexpected sequence "
832 "number, was %d expected %d", msg
->sadb_msg_seq
, this->seq
);
833 if (msg
->sadb_msg_seq
== 0)
835 /* FreeBSD and Mac OS X do this for the response to
836 * SADB_X_SPDGET (but not for the response to SADB_GET).
837 * FreeBSD: 'key_spdget' in /usr/src/sys/netipsec/key.c. */
839 else if (msg
->sadb_msg_seq
< this->seq
)
845 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
849 if (msg
->sadb_msg_type
!= in
->sadb_msg_type
)
851 DBG2(DBG_KNL
, "received PF_KEY message of wrong type, "
852 "was %d expected %d, ignoring",
853 msg
->sadb_msg_type
, in
->sadb_msg_type
);
859 *out
= (struct sadb_msg
*)malloc(len
);
860 memcpy(*out
, buf
, len
);
862 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
868 * Send a message to the default PF_KEY socket and handle the response.
870 static status_t
pfkey_send(private_kernel_pfkey_ipsec_t
*this,
871 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
873 return pfkey_send_socket(this, this->socket
, in
, out
, out_len
);
877 * Process a SADB_ACQUIRE message from the kernel
879 static void process_acquire(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
881 pfkey_msg_t response
;
882 u_int32_t index
, reqid
= 0;
883 traffic_selector_t
*src_ts
, *dst_ts
;
884 policy_entry_t
*policy
;
886 switch (msg
->sadb_msg_satype
)
888 case SADB_SATYPE_UNSPEC
:
889 case SADB_SATYPE_ESP
:
893 /* acquire for AH/ESP only */
896 DBG2(DBG_KNL
, "received an SADB_ACQUIRE");
898 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
900 DBG1(DBG_KNL
, "parsing SADB_ACQUIRE from kernel failed");
904 index
= response
.x_policy
->sadb_x_policy_id
;
905 this->mutex
->lock(this->mutex
);
906 if (this->policies
->find_first(this->policies
,
907 (linked_list_match_t
)policy_entry_match_byindex
, (void**)&policy
, &index
) == SUCCESS
)
909 reqid
= policy
->reqid
;
913 DBG1(DBG_KNL
, "received an SADB_ACQUIRE with policy id %d but no"
914 " matching policy found", index
);
916 src_ts
= sadb_address2ts(response
.src
);
917 dst_ts
= sadb_address2ts(response
.dst
);
918 this->mutex
->unlock(this->mutex
);
920 hydra
->kernel_interface
->acquire(hydra
->kernel_interface
, reqid
, src_ts
,
925 * Process a SADB_EXPIRE message from the kernel
927 static void process_expire(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
929 pfkey_msg_t response
;
931 u_int32_t spi
, reqid
;
934 DBG2(DBG_KNL
, "received an SADB_EXPIRE");
936 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
938 DBG1(DBG_KNL
, "parsing SADB_EXPIRE from kernel failed");
942 protocol
= satype2proto(msg
->sadb_msg_satype
);
943 spi
= response
.sa
->sadb_sa_spi
;
944 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
945 hard
= response
.lft_hard
!= NULL
;
947 if (protocol
!= IPPROTO_ESP
&& protocol
!= IPPROTO_AH
)
949 DBG2(DBG_KNL
, "ignoring SADB_EXPIRE for SA with SPI %.8x and reqid {%u} "
950 "which is not a CHILD_SA", ntohl(spi
), reqid
);
954 hydra
->kernel_interface
->expire(hydra
->kernel_interface
, reqid
, protocol
,
958 #ifdef SADB_X_MIGRATE
960 * Process a SADB_X_MIGRATE message from the kernel
962 static void process_migrate(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
964 pfkey_msg_t response
;
965 traffic_selector_t
*src_ts
, *dst_ts
;
968 host_t
*local
= NULL
, *remote
= NULL
;
970 DBG2(DBG_KNL
, "received an SADB_X_MIGRATE");
972 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
974 DBG1(DBG_KNL
, "parsing SADB_X_MIGRATE from kernel failed");
977 src_ts
= sadb_address2ts(response
.src
);
978 dst_ts
= sadb_address2ts(response
.dst
);
979 dir
= kernel2dir(response
.x_policy
->sadb_x_policy_dir
);
980 DBG2(DBG_KNL
, " policy %R === %R %N, id %u", src_ts
, dst_ts
,
981 policy_dir_names
, dir
);
983 /* SADB_X_EXT_KMADDRESS is not present in unpatched kernels < 2.6.28 */
984 if (response
.x_kmaddress
)
986 sockaddr_t
*local_addr
, *remote_addr
;
989 local_addr
= (sockaddr_t
*)&response
.x_kmaddress
[1];
990 local
= host_create_from_sockaddr(local_addr
);
991 local_len
= (local_addr
->sa_family
== AF_INET6
)?
992 sizeof(struct sockaddr_in6
) : sizeof(struct sockaddr_in
);
993 remote_addr
= (sockaddr_t
*)((u_int8_t
*)local_addr
+ local_len
);
994 remote
= host_create_from_sockaddr(remote_addr
);
995 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
998 if (src_ts
&& dst_ts
&& local
&& remote
)
1000 hydra
->kernel_interface
->migrate(hydra
->kernel_interface
, reqid
,
1001 src_ts
, dst_ts
, dir
, local
, remote
);
1011 #endif /*SADB_X_MIGRATE*/
1013 #ifdef SADB_X_NAT_T_NEW_MAPPING
1015 * Process a SADB_X_NAT_T_NEW_MAPPING message from the kernel
1017 static void process_mapping(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
1019 pfkey_msg_t response
;
1020 u_int32_t spi
, reqid
;
1023 DBG2(DBG_KNL
, "received an SADB_X_NAT_T_NEW_MAPPING");
1025 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1027 DBG1(DBG_KNL
, "parsing SADB_X_NAT_T_NEW_MAPPING from kernel failed");
1031 if (!response
.x_sa2
)
1033 DBG1(DBG_KNL
, "received SADB_X_NAT_T_NEW_MAPPING is missing required "
1038 spi
= response
.sa
->sadb_sa_spi
;
1039 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
1041 if (satype2proto(msg
->sadb_msg_satype
) == IPPROTO_ESP
)
1043 sockaddr_t
*sa
= (sockaddr_t
*)(response
.dst
+ 1);
1044 switch (sa
->sa_family
)
1048 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
1049 sin
->sin_port
= htons(response
.x_natt_dport
->sadb_x_nat_t_port_port
);
1053 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
1054 sin6
->sin6_port
= htons(response
.x_natt_dport
->sadb_x_nat_t_port_port
);
1059 host
= host_create_from_sockaddr(sa
);
1062 hydra
->kernel_interface
->mapping(hydra
->kernel_interface
, reqid
,
1067 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1070 * Receives events from kernel
1072 static job_requeue_t
receive_events(private_kernel_pfkey_ipsec_t
*this)
1074 unsigned char buf
[PFKEY_BUFFER_SIZE
];
1075 struct sadb_msg
*msg
= (struct sadb_msg
*)buf
;
1079 oldstate
= thread_cancelability(TRUE
);
1080 len
= recvfrom(this->socket_events
, buf
, sizeof(buf
), 0, NULL
, 0);
1081 thread_cancelability(oldstate
);
1088 /* interrupted, try again */
1089 return JOB_REQUEUE_DIRECT
;
1091 /* no data ready, select again */
1092 return JOB_REQUEUE_DIRECT
;
1094 DBG1(DBG_KNL
, "unable to receive from PF_KEY event socket");
1096 return JOB_REQUEUE_FAIR
;
1100 if (len
< sizeof(struct sadb_msg
) ||
1101 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
1103 DBG2(DBG_KNL
, "received corrupted PF_KEY message");
1104 return JOB_REQUEUE_DIRECT
;
1106 if (msg
->sadb_msg_pid
!= 0)
1107 { /* not from kernel. not interested, try another one */
1108 return JOB_REQUEUE_DIRECT
;
1110 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
1112 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY message");
1113 return JOB_REQUEUE_DIRECT
;
1116 switch (msg
->sadb_msg_type
)
1119 process_acquire(this, msg
);
1122 process_expire(this, msg
);
1124 #ifdef SADB_X_MIGRATE
1125 case SADB_X_MIGRATE
:
1126 process_migrate(this, msg
);
1128 #endif /*SADB_X_MIGRATE*/
1129 #ifdef SADB_X_NAT_T_NEW_MAPPING
1130 case SADB_X_NAT_T_NEW_MAPPING
:
1131 process_mapping(this, msg
);
1133 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1138 return JOB_REQUEUE_DIRECT
;
1141 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1142 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1143 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
1145 unsigned char request
[PFKEY_BUFFER_SIZE
];
1146 struct sadb_msg
*msg
, *out
;
1147 struct sadb_x_sa2
*sa2
;
1148 struct sadb_spirange
*range
;
1149 pfkey_msg_t response
;
1150 u_int32_t received_spi
= 0;
1153 memset(&request
, 0, sizeof(request
));
1155 msg
= (struct sadb_msg
*)request
;
1156 msg
->sadb_msg_version
= PF_KEY_V2
;
1157 msg
->sadb_msg_type
= SADB_GETSPI
;
1158 msg
->sadb_msg_satype
= proto2satype(protocol
);
1159 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1161 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
1162 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
1163 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1164 sa2
->sadb_x_sa2_reqid
= reqid
;
1165 PFKEY_EXT_ADD(msg
, sa2
);
1167 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0);
1168 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0);
1170 range
= (struct sadb_spirange
*)PFKEY_EXT_ADD_NEXT(msg
);
1171 range
->sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
1172 range
->sadb_spirange_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1173 range
->sadb_spirange_min
= 0xc0000000;
1174 range
->sadb_spirange_max
= 0xcFFFFFFF;
1175 PFKEY_EXT_ADD(msg
, range
);
1177 if (pfkey_send(this, msg
, &out
, &len
) == SUCCESS
)
1179 if (out
->sadb_msg_errno
)
1181 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1182 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1184 else if (parse_pfkey_message(out
, &response
) == SUCCESS
)
1186 received_spi
= response
.sa
->sadb_sa_spi
;
1191 if (received_spi
== 0)
1196 *spi
= received_spi
;
1200 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1201 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1202 u_int32_t reqid
, u_int16_t
*cpi
)
1207 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1208 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
, u_int32_t spi
,
1209 u_int8_t protocol
, u_int32_t reqid
, mark_t mark
, u_int32_t tfc
,
1210 lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
1211 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
,
1212 u_int16_t ipcomp
, u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
1213 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
)
1215 unsigned char request
[PFKEY_BUFFER_SIZE
];
1216 struct sadb_msg
*msg
, *out
;
1218 struct sadb_x_sa2
*sa2
;
1219 struct sadb_lifetime
*lft
;
1220 struct sadb_key
*key
;
1223 memset(&request
, 0, sizeof(request
));
1225 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}", ntohl(spi
), reqid
);
1227 msg
= (struct sadb_msg
*)request
;
1228 msg
->sadb_msg_version
= PF_KEY_V2
;
1229 msg
->sadb_msg_type
= inbound ? SADB_UPDATE
: SADB_ADD
;
1230 msg
->sadb_msg_satype
= proto2satype(protocol
);
1231 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1236 struct sadb_sa_2
*sa_2
;
1237 sa_2
= (struct sadb_sa_2
*)PFKEY_EXT_ADD_NEXT(msg
);
1238 sa_2
->sadb_sa_natt_port
= dst
->get_port(dst
);
1240 sa
->sadb_sa_flags
|= SADB_X_EXT_NATT
;
1241 len
= sizeof(struct sadb_sa_2
);
1246 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1247 len
= sizeof(struct sadb_sa
);
1249 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1250 sa
->sadb_sa_len
= PFKEY_LEN(len
);
1251 sa
->sadb_sa_spi
= spi
;
1252 sa
->sadb_sa_replay
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
1253 sa
->sadb_sa_auth
= lookup_algorithm(integrity_algs
, int_alg
);
1254 sa
->sadb_sa_encrypt
= lookup_algorithm(encryption_algs
, enc_alg
);
1255 PFKEY_EXT_ADD(msg
, sa
);
1257 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
1258 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
1259 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1260 sa2
->sadb_x_sa2_mode
= mode2kernel(mode
);
1261 sa2
->sadb_x_sa2_reqid
= reqid
;
1262 PFKEY_EXT_ADD(msg
, sa2
);
1264 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0);
1265 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0);
1267 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1268 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
1269 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1270 lft
->sadb_lifetime_allocations
= lifetime
->packets
.rekey
;
1271 lft
->sadb_lifetime_bytes
= lifetime
->bytes
.rekey
;
1272 lft
->sadb_lifetime_addtime
= lifetime
->time
.rekey
;
1273 lft
->sadb_lifetime_usetime
= 0; /* we only use addtime */
1274 PFKEY_EXT_ADD(msg
, lft
);
1276 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1277 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1278 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1279 lft
->sadb_lifetime_allocations
= lifetime
->packets
.life
;
1280 lft
->sadb_lifetime_bytes
= lifetime
->bytes
.life
;
1281 lft
->sadb_lifetime_addtime
= lifetime
->time
.life
;
1282 lft
->sadb_lifetime_usetime
= 0; /* we only use addtime */
1283 PFKEY_EXT_ADD(msg
, lft
);
1285 if (enc_alg
!= ENCR_UNDEFINED
)
1287 if (!sa
->sadb_sa_encrypt
)
1289 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1290 encryption_algorithm_names
, enc_alg
);
1293 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1294 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1296 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1297 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
1298 key
->sadb_key_bits
= enc_key
.len
* 8;
1299 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + enc_key
.len
);
1300 memcpy(key
+ 1, enc_key
.ptr
, enc_key
.len
);
1302 PFKEY_EXT_ADD(msg
, key
);
1305 if (int_alg
!= AUTH_UNDEFINED
)
1307 if (!sa
->sadb_sa_auth
)
1309 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1310 integrity_algorithm_names
, int_alg
);
1313 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1314 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1316 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1317 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
1318 key
->sadb_key_bits
= int_key
.len
* 8;
1319 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + int_key
.len
);
1320 memcpy(key
+ 1, int_key
.ptr
, int_key
.len
);
1322 PFKEY_EXT_ADD(msg
, key
);
1325 if (ipcomp
!= IPCOMP_NONE
)
1333 add_encap_ext(msg
, src
, dst
);
1335 #endif /*HAVE_NATT*/
1337 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1339 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1342 else if (out
->sadb_msg_errno
)
1344 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x: %s (%d)",
1345 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1354 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1355 private_kernel_pfkey_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1356 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1357 bool encap
, bool new_encap
, mark_t mark
)
1359 unsigned char request
[PFKEY_BUFFER_SIZE
];
1360 struct sadb_msg
*msg
, *out
;
1362 pfkey_msg_t response
;
1365 /* we can't update the SA if any of the ip addresses have changed.
1366 * that's because we can't use SADB_UPDATE and by deleting and readding the
1367 * SA the sequence numbers would get lost */
1368 if (!src
->ip_equals(src
, new_src
) ||
1369 !dst
->ip_equals(dst
, new_dst
))
1371 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: address changes"
1372 " are not supported", ntohl(spi
));
1373 return NOT_SUPPORTED
;
1376 memset(&request
, 0, sizeof(request
));
1378 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1380 msg
= (struct sadb_msg
*)request
;
1381 msg
->sadb_msg_version
= PF_KEY_V2
;
1382 msg
->sadb_msg_type
= SADB_GET
;
1383 msg
->sadb_msg_satype
= proto2satype(protocol
);
1384 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1386 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1387 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1388 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1389 sa
->sadb_sa_spi
= spi
;
1390 PFKEY_EXT_ADD(msg
, sa
);
1392 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1393 * it is not used for anything. */
1394 add_anyaddr_ext(msg
, dst
->get_family(dst
), SADB_EXT_ADDRESS_SRC
);
1395 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0);
1397 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1399 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x",
1403 else if (out
->sadb_msg_errno
)
1405 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: %s (%d)",
1406 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1410 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1412 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: parsing response "
1413 "from kernel failed", ntohl(spi
));
1418 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1419 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1421 memset(&request
, 0, sizeof(request
));
1423 msg
= (struct sadb_msg
*)request
;
1424 msg
->sadb_msg_version
= PF_KEY_V2
;
1425 msg
->sadb_msg_type
= SADB_UPDATE
;
1426 msg
->sadb_msg_satype
= proto2satype(protocol
);
1427 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1431 struct sadb_sa_2
*sa_2
;
1432 sa_2
= (struct sadb_sa_2
*)PFKEY_EXT_ADD_NEXT(msg
);
1433 sa_2
->sa
.sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa_2
));
1434 memcpy(&sa_2
->sa
, response
.sa
, sizeof(struct sadb_sa
));
1437 sa_2
->sadb_sa_natt_port
= new_dst
->get_port(new_dst
);
1438 sa_2
->sa
.sadb_sa_flags
|= SADB_X_EXT_NATT
;
1442 PFKEY_EXT_COPY(msg
, response
.sa
);
1444 PFKEY_EXT_COPY(msg
, response
.x_sa2
);
1446 PFKEY_EXT_COPY(msg
, response
.src
);
1447 PFKEY_EXT_COPY(msg
, response
.dst
);
1449 PFKEY_EXT_COPY(msg
, response
.lft_soft
);
1450 PFKEY_EXT_COPY(msg
, response
.lft_hard
);
1452 if (response
.key_encr
)
1454 PFKEY_EXT_COPY(msg
, response
.key_encr
);
1457 if (response
.key_auth
)
1459 PFKEY_EXT_COPY(msg
, response
.key_auth
);
1465 add_encap_ext(msg
, new_src
, new_dst
);
1467 #endif /*HAVE_NATT*/
1471 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1473 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1476 else if (out
->sadb_msg_errno
)
1478 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: %s (%d)",
1479 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1488 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1489 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1490 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
1492 unsigned char request
[PFKEY_BUFFER_SIZE
];
1493 struct sadb_msg
*msg
, *out
;
1495 pfkey_msg_t response
;
1498 memset(&request
, 0, sizeof(request
));
1500 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1502 msg
= (struct sadb_msg
*)request
;
1503 msg
->sadb_msg_version
= PF_KEY_V2
;
1504 msg
->sadb_msg_type
= SADB_GET
;
1505 msg
->sadb_msg_satype
= proto2satype(protocol
);
1506 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1508 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1509 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1510 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1511 sa
->sadb_sa_spi
= spi
;
1512 PFKEY_EXT_ADD(msg
, sa
);
1514 /* the Linux Kernel doesn't care for the src address, but other systems do
1517 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0);
1518 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0);
1520 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1522 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1525 else if (out
->sadb_msg_errno
)
1527 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: %s (%d)",
1528 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1532 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1534 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1538 *bytes
= response
.lft_current
->sadb_lifetime_bytes
;
1544 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1545 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1546 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1548 unsigned char request
[PFKEY_BUFFER_SIZE
];
1549 struct sadb_msg
*msg
, *out
;
1553 memset(&request
, 0, sizeof(request
));
1555 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1557 msg
= (struct sadb_msg
*)request
;
1558 msg
->sadb_msg_version
= PF_KEY_V2
;
1559 msg
->sadb_msg_type
= SADB_DELETE
;
1560 msg
->sadb_msg_satype
= proto2satype(protocol
);
1561 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1563 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1564 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1565 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1566 sa
->sadb_sa_spi
= spi
;
1567 PFKEY_EXT_ADD(msg
, sa
);
1569 /* the Linux Kernel doesn't care for the src address, but other systems do
1572 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
, 0, 0);
1573 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
, 0, 0);
1575 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1577 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1580 else if (out
->sadb_msg_errno
)
1582 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1583 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1588 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1593 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
1594 private_kernel_pfkey_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1595 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
1596 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
1597 mark_t mark
, bool routed
)
1599 unsigned char request
[PFKEY_BUFFER_SIZE
];
1600 struct sadb_msg
*msg
, *out
;
1601 struct sadb_x_policy
*pol
;
1602 struct sadb_x_ipsecrequest
*req
;
1603 policy_entry_t
*policy
, *found
= NULL
;
1604 pfkey_msg_t response
;
1607 if (dir2kernel(direction
) == IPSEC_DIR_INVALID
)
1609 /* FWD policies are not supported on all platforms */
1613 /* create a policy */
1614 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, sa
->reqid
);
1616 /* find a matching policy */
1617 this->mutex
->lock(this->mutex
);
1618 if (this->policies
->find_first(this->policies
,
1619 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) == SUCCESS
)
1621 /* use existing policy */
1623 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
1624 "refcount", src_ts
, dst_ts
,
1625 policy_dir_names
, direction
);
1626 policy_entry_destroy(policy
);
1631 /* apply the new one, if we have no such policy */
1632 this->policies
->insert_last(this->policies
, policy
);
1633 policy
->refcount
= 1;
1636 memset(&request
, 0, sizeof(request
));
1638 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
1639 policy_dir_names
, direction
);
1641 msg
= (struct sadb_msg
*)request
;
1642 msg
->sadb_msg_version
= PF_KEY_V2
;
1643 msg
->sadb_msg_type
= found ? SADB_X_SPDUPDATE
: SADB_X_SPDADD
;
1644 msg
->sadb_msg_satype
= 0;
1645 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1647 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1648 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1649 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1650 pol
->sadb_x_policy_id
= 0;
1651 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1652 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1653 #ifdef HAVE_STRUCT_SADB_X_POLICY_SADB_X_POLICY_PRIORITY
1654 /* calculate priority based on selector size, small size = high prio */
1655 pol
->sadb_x_policy_priority
= routed ? PRIO_LOW
: PRIO_HIGH
;
1656 pol
->sadb_x_policy_priority
-= policy
->src
.mask
;
1657 pol
->sadb_x_policy_priority
-= policy
->dst
.mask
;
1658 pol
->sadb_x_policy_priority
<<= 2; /* make some room for the flags */
1659 pol
->sadb_x_policy_priority
+= policy
->src
.net
->get_port(policy
->src
.net
) ||
1660 policy
->dst
.net
->get_port(policy
->dst
.net
) ?
0 : 2;
1661 pol
->sadb_x_policy_priority
+= policy
->src
.proto
!= IPSEC_PROTO_ANY ?
0 : 1;
1664 /* one or more sadb_x_ipsecrequest extensions are added to the sadb_x_policy extension */
1665 req
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
1666 req
->sadb_x_ipsecrequest_proto
= sa
->esp
.use ? IPPROTO_ESP
: IPPROTO_AH
;
1667 /* !!! the length of this struct MUST be in octets instead of 64 bit words */
1668 req
->sadb_x_ipsecrequest_len
= sizeof(struct sadb_x_ipsecrequest
);
1669 req
->sadb_x_ipsecrequest_mode
= mode2kernel(sa
->mode
);
1670 req
->sadb_x_ipsecrequest_reqid
= sa
->reqid
;
1671 req
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
1672 if (sa
->mode
== MODE_TUNNEL
)
1674 len
= hostcpy(req
+ 1, src
);
1675 req
->sadb_x_ipsecrequest_len
+= len
;
1676 len
= hostcpy((char*)(req
+ 1) + len
, dst
);
1677 req
->sadb_x_ipsecrequest_len
+= len
;
1680 pol
->sadb_x_policy_len
+= PFKEY_LEN(req
->sadb_x_ipsecrequest_len
);
1681 PFKEY_EXT_ADD(msg
, pol
);
1683 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
1685 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
1689 { /* on FreeBSD a lifetime has to be defined to be able to later query
1690 * the current use time. */
1691 struct sadb_lifetime
*lft
;
1692 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1693 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1694 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1695 lft
->sadb_lifetime_addtime
= LONG_MAX
;
1696 PFKEY_EXT_ADD(msg
, lft
);
1700 this->mutex
->unlock(this->mutex
);
1702 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1704 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
1705 policy_dir_names
, direction
);
1708 else if (out
->sadb_msg_errno
)
1710 DBG1(DBG_KNL
, "unable to add policy %R === %R %N: %s (%d)", src_ts
, dst_ts
,
1711 policy_dir_names
, direction
,
1712 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1716 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1718 DBG1(DBG_KNL
, "unable to add policy %R === %R %N: parsing response "
1719 "from kernel failed", src_ts
, dst_ts
, policy_dir_names
, direction
);
1724 this->mutex
->lock(this->mutex
);
1726 /* we try to find the policy again and update the kernel index */
1727 if (this->policies
->find_last(this->policies
, NULL
, (void**)&policy
) != SUCCESS
)
1729 DBG2(DBG_KNL
, "unable to update index, the policy %R === %R %N is "
1730 "already gone, ignoring", src_ts
, dst_ts
, policy_dir_names
, direction
);
1731 this->mutex
->unlock(this->mutex
);
1735 policy
->index
= response
.x_policy
->sadb_x_policy_id
;
1738 /* install a route, if:
1739 * - we are NOT updating a policy
1740 * - this is a forward policy (to just get one for each child)
1741 * - we are in tunnel mode
1742 * - we are not using IPv6 (does not work correctly yet!)
1743 * - routing is not disabled via strongswan.conf
1745 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1746 sa
->mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
1747 this->install_routes
)
1749 route_entry_t
*route
= malloc_thing(route_entry_t
);
1751 if (hydra
->kernel_interface
->get_address_by_ts(hydra
->kernel_interface
,
1752 dst_ts
, &route
->src_ip
) == SUCCESS
)
1754 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1755 route
->gateway
= hydra
->kernel_interface
->get_nexthop(
1756 hydra
->kernel_interface
, src
);
1757 route
->if_name
= hydra
->kernel_interface
->get_interface(
1758 hydra
->kernel_interface
, dst
);
1759 route
->dst_net
= chunk_clone(policy
->src
.net
->get_address(policy
->src
.net
));
1760 route
->prefixlen
= policy
->src
.mask
;
1764 switch (hydra
->kernel_interface
->add_route(
1765 hydra
->kernel_interface
, route
->dst_net
,
1766 route
->prefixlen
, route
->gateway
,
1767 route
->src_ip
, route
->if_name
))
1770 DBG1(DBG_KNL
, "unable to install source route for %H",
1774 /* route exists, do not uninstall */
1775 route_entry_destroy(route
);
1778 /* cache the installed route */
1779 policy
->route
= route
;
1785 route_entry_destroy(route
);
1794 this->mutex
->unlock(this->mutex
);
1799 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
1800 private_kernel_pfkey_ipsec_t
*this, traffic_selector_t
*src_ts
,
1801 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
1802 u_int32_t
*use_time
)
1804 unsigned char request
[PFKEY_BUFFER_SIZE
];
1805 struct sadb_msg
*msg
, *out
;
1806 struct sadb_x_policy
*pol
;
1807 policy_entry_t
*policy
, *found
= NULL
;
1808 pfkey_msg_t response
;
1811 if (dir2kernel(direction
) == IPSEC_DIR_INVALID
)
1813 /* FWD policies are not supported on all platforms */
1817 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
1818 policy_dir_names
, direction
);
1820 /* create a policy */
1821 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, 0);
1823 /* find a matching policy */
1824 this->mutex
->lock(this->mutex
);
1825 if (this->policies
->find_first(this->policies
,
1826 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) != SUCCESS
)
1828 DBG1(DBG_KNL
, "querying policy %R === %R %N failed, not found", src_ts
,
1829 dst_ts
, policy_dir_names
, direction
);
1830 policy_entry_destroy(policy
);
1831 this->mutex
->unlock(this->mutex
);
1834 policy_entry_destroy(policy
);
1837 memset(&request
, 0, sizeof(request
));
1839 msg
= (struct sadb_msg
*)request
;
1840 msg
->sadb_msg_version
= PF_KEY_V2
;
1841 msg
->sadb_msg_type
= SADB_X_SPDGET
;
1842 msg
->sadb_msg_satype
= 0;
1843 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1845 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1846 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1847 pol
->sadb_x_policy_id
= policy
->index
;
1848 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1849 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1850 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1851 PFKEY_EXT_ADD(msg
, pol
);
1853 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
1855 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
1858 this->mutex
->unlock(this->mutex
);
1860 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1862 DBG1(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
1863 policy_dir_names
, direction
);
1866 else if (out
->sadb_msg_errno
)
1868 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: %s (%d)", src_ts
,
1869 dst_ts
, policy_dir_names
, direction
,
1870 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1874 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1876 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: parsing response "
1877 "from kernel failed", src_ts
, dst_ts
, policy_dir_names
, direction
);
1881 else if (response
.lft_current
== NULL
)
1883 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: kernel reports no "
1884 "use time", src_ts
, dst_ts
, policy_dir_names
, direction
);
1888 /* we need the monotonic time, but the kernel returns system time. */
1889 if (response
.lft_current
->sadb_lifetime_usetime
)
1891 *use_time
= time_monotonic(NULL
) -
1892 (time(NULL
) - response
.lft_current
->sadb_lifetime_usetime
);
1903 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
1904 private_kernel_pfkey_ipsec_t
*this, traffic_selector_t
*src_ts
,
1905 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
1908 unsigned char request
[PFKEY_BUFFER_SIZE
];
1909 struct sadb_msg
*msg
, *out
;
1910 struct sadb_x_policy
*pol
;
1911 policy_entry_t
*policy
, *found
= NULL
;
1912 route_entry_t
*route
;
1915 if (dir2kernel(direction
) == IPSEC_DIR_INVALID
)
1917 /* FWD policies are not supported on all platforms */
1921 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
1922 policy_dir_names
, direction
);
1924 /* create a policy */
1925 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, 0);
1927 /* find a matching policy */
1928 this->mutex
->lock(this->mutex
);
1929 if (this->policies
->find_first(this->policies
,
1930 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) == SUCCESS
)
1932 if (--found
->refcount
> 0)
1934 /* is used by more SAs, keep in kernel */
1935 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1936 policy_entry_destroy(policy
);
1937 this->mutex
->unlock(this->mutex
);
1940 /* remove if last reference */
1941 this->policies
->remove(this->policies
, found
, NULL
);
1942 policy_entry_destroy(policy
);
1947 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
1948 dst_ts
, policy_dir_names
, direction
);
1949 policy_entry_destroy(policy
);
1950 this->mutex
->unlock(this->mutex
);
1953 this->mutex
->unlock(this->mutex
);
1955 memset(&request
, 0, sizeof(request
));
1957 msg
= (struct sadb_msg
*)request
;
1958 msg
->sadb_msg_version
= PF_KEY_V2
;
1959 msg
->sadb_msg_type
= SADB_X_SPDDELETE
;
1960 msg
->sadb_msg_satype
= 0;
1961 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1963 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1964 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1965 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1966 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1967 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1968 PFKEY_EXT_ADD(msg
, pol
);
1970 add_addr_ext(msg
, policy
->src
.net
, SADB_EXT_ADDRESS_SRC
, policy
->src
.proto
,
1972 add_addr_ext(msg
, policy
->dst
.net
, SADB_EXT_ADDRESS_DST
, policy
->dst
.proto
,
1975 route
= policy
->route
;
1976 policy
->route
= NULL
;
1977 policy_entry_destroy(policy
);
1979 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1981 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
1982 policy_dir_names
, direction
);
1985 else if (out
->sadb_msg_errno
)
1987 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N: %s (%d)", src_ts
,
1988 dst_ts
, policy_dir_names
, direction
,
1989 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1997 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
1998 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1999 route
->src_ip
, route
->if_name
) != SUCCESS
)
2001 DBG1(DBG_KNL
, "error uninstalling route installed with "
2002 "policy %R === %R %N", src_ts
, dst_ts
,
2003 policy_dir_names
, direction
);
2005 route_entry_destroy(route
);
2012 * Register a socket for AQUIRE/EXPIRE messages
2014 static status_t
register_pfkey_socket(private_kernel_pfkey_ipsec_t
*this,
2017 unsigned char request
[PFKEY_BUFFER_SIZE
];
2018 struct sadb_msg
*msg
, *out
;
2021 memset(&request
, 0, sizeof(request
));
2023 msg
= (struct sadb_msg
*)request
;
2024 msg
->sadb_msg_version
= PF_KEY_V2
;
2025 msg
->sadb_msg_type
= SADB_REGISTER
;
2026 msg
->sadb_msg_satype
= satype
;
2027 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2029 if (pfkey_send_socket(this, this->socket_events
, msg
, &out
, &len
) != SUCCESS
)
2031 DBG1(DBG_KNL
, "unable to register PF_KEY socket");
2034 else if (out
->sadb_msg_errno
)
2036 DBG1(DBG_KNL
, "unable to register PF_KEY socket: %s (%d)",
2037 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2045 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2046 private_kernel_pfkey_ipsec_t
*this, int fd
, int family
)
2048 struct sadb_x_policy policy
;
2049 u_int sol
, ipsec_policy
;
2056 ipsec_policy
= IP_IPSEC_POLICY
;
2062 ipsec_policy
= IPV6_IPSEC_POLICY
;
2069 memset(&policy
, 0, sizeof(policy
));
2070 policy
.sadb_x_policy_len
= sizeof(policy
) / sizeof(u_int64_t
);
2071 policy
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2072 policy
.sadb_x_policy_type
= IPSEC_POLICY_BYPASS
;
2074 policy
.sadb_x_policy_dir
= IPSEC_DIR_OUTBOUND
;
2075 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2077 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2081 policy
.sadb_x_policy_dir
= IPSEC_DIR_INBOUND
;
2082 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2084 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2091 METHOD(kernel_ipsec_t
, destroy
, void,
2092 private_kernel_pfkey_ipsec_t
*this)
2096 this->job
->cancel(this->job
);
2098 if (this->socket
> 0)
2100 close(this->socket
);
2102 if (this->socket_events
> 0)
2104 close(this->socket_events
);
2106 this->policies
->destroy_function(this->policies
, (void*)policy_entry_destroy
);
2107 this->mutex
->destroy(this->mutex
);
2108 this->mutex_pfkey
->destroy(this->mutex_pfkey
);
2113 * Described in header.
2115 kernel_pfkey_ipsec_t
*kernel_pfkey_ipsec_create()
2117 private_kernel_pfkey_ipsec_t
*this;
2122 .get_spi
= _get_spi
,
2123 .get_cpi
= _get_cpi
,
2125 .update_sa
= _update_sa
,
2126 .query_sa
= _query_sa
,
2128 .add_policy
= _add_policy
,
2129 .query_policy
= _query_policy
,
2130 .del_policy
= _del_policy
,
2131 .bypass_socket
= _bypass_socket
,
2132 .destroy
= _destroy
,
2135 .policies
= linked_list_create(),
2136 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2137 .mutex_pfkey
= mutex_create(MUTEX_TYPE_DEFAULT
),
2138 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2139 "%s.install_routes", TRUE
,
2143 if (streq(hydra
->daemon
, "pluto"))
2144 { /* no routes for pluto, they are installed via updown script */
2145 this->install_routes
= FALSE
;
2148 /* create a PF_KEY socket to communicate with the kernel */
2149 this->socket
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
2150 if (this->socket
<= 0)
2152 DBG1(DBG_KNL
, "unable to create PF_KEY socket");
2157 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
2158 this->socket_events
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
2159 if (this->socket_events
<= 0)
2161 DBG1(DBG_KNL
, "unable to create PF_KEY event socket");
2166 /* register the event socket */
2167 if (register_pfkey_socket(this, SADB_SATYPE_ESP
) != SUCCESS
||
2168 register_pfkey_socket(this, SADB_SATYPE_AH
) != SUCCESS
)
2170 DBG1(DBG_KNL
, "unable to register PF_KEY event socket");
2175 this->job
= callback_job_create_with_prio((callback_job_cb_t
)receive_events
,
2176 this, NULL
, NULL
, JOB_PRIO_CRITICAL
);
2177 lib
->processor
->queue_job(lib
->processor
, (job_t
*)this->job
);
2179 return &this->public;