2 * Copyright (C) 2008 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <linux/ipsec.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/udp.h>
28 #include "kernel_pfkey_ipsec.h"
31 #include <utils/host.h>
32 #include <processing/jobs/callback_job.h>
33 #include <processing/jobs/acquire_job.h>
34 #include <processing/jobs/rekey_child_sa_job.h>
35 #include <processing/jobs/delete_child_sa_job.h>
36 #include <processing/jobs/update_sa_job.h>
38 /** default priority of installed policies */
40 #define PRIO_HIGH 2000
42 /** buffer size for PF_KEY messages */
43 #define PFKEY_BUFFER_SIZE 4096
45 /** PF_KEY messages are 64 bit aligned */
46 #define PFKEY_ALIGNMENT 8
47 /** aligns len to 64 bits */
48 #define PFKEY_ALIGN(len) (((len) + PFKEY_ALIGNMENT - 1) & ~(PFKEY_ALIGNMENT - 1))
49 /** calculates the properly padded length in 64 bit chunks */
50 #define PFKEY_LEN(len) ((PFKEY_ALIGN(len) / PFKEY_ALIGNMENT))
51 /** calculates user mode length i.e. in bytes */
52 #define PFKEY_USER_LEN(len) ((len) * PFKEY_ALIGNMENT)
54 /** given a PF_KEY message header and an extension this updates the length in the header */
55 #define PFKEY_EXT_ADD(msg, ext) ((msg)->sadb_msg_len += ((struct sadb_ext*)ext)->sadb_ext_len)
56 /** given a PF_KEY message header this returns a pointer to the next extension */
57 #define PFKEY_EXT_ADD_NEXT(msg) ((struct sadb_ext*)(((char*)(msg)) + PFKEY_USER_LEN((msg)->sadb_msg_len)))
58 /** copy an extension and append it to a PF_KEY message */
59 #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))))
60 /** given a PF_KEY extension this returns a pointer to the next extension */
61 #define PFKEY_EXT_NEXT(ext) ((struct sadb_ext*)(((char*)(ext)) + PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len)))
62 /** given a PF_KEY extension this returns a pointer to the next extension also updates len (len in 64 bit words) */
63 #define PFKEY_EXT_NEXT_LEN(ext,len) ((len) -= (ext)->sadb_ext_len, PFKEY_EXT_NEXT(ext))
64 /** true if ext has a valid length and len is large enough to contain ext (assuming len in 64 bit words) */
65 #define PFKEY_EXT_OK(ext,len) ((len) >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
66 (ext)->sadb_ext_len >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
67 (ext)->sadb_ext_len <= (len))
69 typedef struct private_kernel_pfkey_ipsec_t private_kernel_pfkey_ipsec_t
;
72 * Private variables and functions of kernel_pfkey class.
74 struct private_kernel_pfkey_ipsec_t
77 * Public part of the kernel_pfkey_t object.
79 kernel_pfkey_ipsec_t
public;
82 * mutex to lock access to various lists
84 pthread_mutex_t mutex
;
87 * List of installed policies (policy_entry_t)
89 linked_list_t
*policies
;
92 * whether to install routes along policies
97 * job receiving PF_KEY events
102 * mutex to lock access to the PF_KEY socket
104 pthread_mutex_t mutex_pfkey
;
108 * PF_KEY socket to communicate with the kernel
114 * PF_KEY socket to receive acquire and expire events
120 * sequence number for messages sent to the kernel
125 typedef struct route_entry_t route_entry_t
;
128 * installed routing entry
130 struct route_entry_t
{
131 /** Name of the interface the route is bound to */
134 /** Source ip of the route */
137 /** gateway for this route */
140 /** Destination net */
143 /** Destination net prefixlen */
148 * destroy an route_entry_t object
150 static void route_entry_destroy(route_entry_t
*this)
153 this->src_ip
->destroy(this->src_ip
);
154 this->gateway
->destroy(this->gateway
);
155 chunk_free(&this->dst_net
);
159 typedef struct policy_entry_t policy_entry_t
;
162 * installed kernel policy.
164 struct policy_entry_t
{
166 /** reqid of this policy */
169 /** index assigned by the kernel */
172 /** direction of this policy: in, out, forward */
175 /** parameters of installed policy */
177 /** subnet and port */
185 /** associated route installed for this policy */
186 route_entry_t
*route
;
188 /** by how many CHILD_SA's this policy is used */
193 * create a policy_entry_t object
195 static policy_entry_t
*create_policy_entry(traffic_selector_t
*src_ts
,
196 traffic_selector_t
*dst_ts
, policy_dir_t dir
, u_int32_t reqid
)
198 policy_entry_t
*policy
= malloc_thing(policy_entry_t
);
199 policy
->reqid
= reqid
;
201 policy
->direction
= dir
;
202 policy
->route
= NULL
;
203 policy
->refcount
= 0;
205 src_ts
->to_subnet(src_ts
, &policy
->src
.net
, &policy
->src
.mask
);
206 dst_ts
->to_subnet(dst_ts
, &policy
->dst
.net
, &policy
->dst
.mask
);
208 /* src or dest proto may be "any" (0), use more restrictive one */
209 policy
->src
.proto
= max(src_ts
->get_protocol(src_ts
), dst_ts
->get_protocol(dst_ts
));
210 policy
->src
.proto
= policy
->src
.proto ? policy
->src
.proto
: IPSEC_PROTO_ANY
;
211 policy
->dst
.proto
= policy
->src
.proto
;
217 * destroy a policy_entry_t object
219 static void policy_entry_destroy(policy_entry_t
*this)
221 DESTROY_IF(this->src
.net
);
222 DESTROY_IF(this->dst
.net
);
225 route_entry_destroy(this->route
);
231 * compares two policy_entry_t
233 static inline bool policy_entry_equals(policy_entry_t
*current
, policy_entry_t
*policy
)
235 return current
->direction
== policy
->direction
&&
236 current
->src
.proto
== policy
->src
.proto
&&
237 current
->dst
.proto
== policy
->dst
.proto
&&
238 current
->src
.mask
== policy
->src
.mask
&&
239 current
->dst
.mask
== policy
->dst
.mask
&&
240 current
->src
.net
->equals(current
->src
.net
, policy
->src
.net
) &&
241 current
->dst
.net
->equals(current
->dst
.net
, policy
->dst
.net
);
245 * compare the given kernel index with that of a policy
247 static inline bool policy_entry_match_byindex(policy_entry_t
*current
, u_int32_t
*index
)
249 return current
->index
== *index
;
252 typedef struct pfkey_msg_t pfkey_msg_t
;
257 * PF_KEY message base
259 struct sadb_msg
*msg
;
263 * PF_KEY message extensions
266 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
268 struct sadb_ext
*reserved
; /* SADB_EXT_RESERVED */
269 struct sadb_sa
*sa
; /* SADB_EXT_SA */
270 struct sadb_lifetime
*lft_current
; /* SADB_EXT_LIFETIME_CURRENT */
271 struct sadb_lifetime
*lft_hard
; /* SADB_EXT_LIFETIME_HARD */
272 struct sadb_lifetime
*lft_soft
; /* SADB_EXT_LIFETIME_SOFT */
273 struct sadb_address
*src
; /* SADB_EXT_ADDRESS_SRC */
274 struct sadb_address
*dst
; /* SADB_EXT_ADDRESS_DST */
275 struct sadb_address
*proxy
; /* SADB_EXT_ADDRESS_PROXY */
276 struct sadb_key
*key_auth
; /* SADB_EXT_KEY_AUTH */
277 struct sadb_key
*key_encr
; /* SADB_EXT_KEY_ENCRYPT */
278 struct sadb_ident
*id_src
; /* SADB_EXT_IDENTITY_SRC */
279 struct sadb_ident
*id_dst
; /* SADB_EXT_IDENTITY_DST */
280 struct sadb_sens
*sensitivity
; /* SADB_EXT_SENSITIVITY */
281 struct sadb_prop
*proposal
; /* SADB_EXT_PROPOSAL */
282 struct sadb_supported
*supported_auth
; /* SADB_EXT_SUPPORTED_AUTH */
283 struct sadb_supported
*supported_encr
; /* SADB_EXT_SUPPORTED_ENCRYPT */
284 struct sadb_spirange
*spirange
; /* SADB_EXT_SPIRANGE */
285 struct sadb_x_kmprivate
*x_kmprivate
; /* SADB_X_EXT_KMPRIVATE */
286 struct sadb_x_policy
*x_policy
; /* SADB_X_EXT_POLICY */
287 struct sadb_x_sa2
*x_sa2
; /* SADB_X_EXT_SA2 */
288 struct sadb_x_nat_t_type
*x_natt_type
; /* SADB_X_EXT_NAT_T_TYPE */
289 struct sadb_x_nat_t_port
*x_natt_sport
; /* SADB_X_EXT_NAT_T_SPORT */
290 struct sadb_x_nat_t_port
*x_natt_dport
; /* SADB_X_EXT_NAT_T_DPORT */
291 struct sadb_address
*x_natt_oa
; /* SADB_X_EXT_NAT_T_OA */
292 struct sadb_x_sec_ctx
*x_sec_ctx
; /* SADB_X_EXT_SEC_CTX */
293 struct sadb_x_kmaddress
*x_kmaddress
; /* SADB_X_EXT_KMADDRESS */
294 } __attribute__((__packed__
));
298 ENUM(sadb_ext_type_names
, SADB_EXT_RESERVED
, SADB_X_EXT_KMADDRESS
,
301 "SADB_EXT_LIFETIME_CURRENT",
302 "SADB_EXT_LIFETIME_HARD",
303 "SADB_EXT_LIFETIME_SOFT",
304 "SADB_EXT_ADDRESS_SRC",
305 "SADB_EXT_ADDRESS_DST",
306 "SADB_EXT_ADDRESS_PROXY",
308 "SADB_EXT_KEY_ENCRYPT",
309 "SADB_EXT_IDENTITY_SRC",
310 "SADB_EXT_IDENTITY_DST",
311 "SADB_EXT_SENSITIVITY",
313 "SADB_EXT_SUPPORTED_AUTH",
314 "SADB_EXT_SUPPORTED_ENCRYPT",
316 "SADB_X_EXT_KMPRIVATE",
319 "SADB_X_EXT_NAT_T_TYPE",
320 "SADB_X_EXT_NAT_T_SPORT",
321 "SADB_X_EXT_NAT_T_DPORT",
322 "SADB_X_EXT_NAT_T_OA",
323 "SADB_X_EXT_SEC_CTX",
324 "SADB_X_EXT_KMADDRESS"
327 * convert a IKEv2 specific protocol identifier to the PF_KEY sa type
329 static u_int8_t
proto_ike2satype(protocol_id_t proto
)
334 return SADB_SATYPE_ESP
;
336 return SADB_SATYPE_AH
;
338 return SADB_X_SATYPE_IPCOMP
;
345 * convert a PF_KEY sa type to a IKEv2 specific protocol identifier
347 static protocol_id_t
proto_satype2ike(u_int8_t proto
)
351 case SADB_SATYPE_ESP
:
355 case SADB_X_SATYPE_IPCOMP
:
363 * convert a IKEv2 specific protocol identifier to the IP protocol identifier
365 static u_int8_t
proto_ike2ip(protocol_id_t proto
)
379 * convert the general ipsec mode to the one defined in ipsec.h
381 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
386 return IPSEC_MODE_TRANSPORT
;
388 return IPSEC_MODE_TUNNEL
;
390 return IPSEC_MODE_BEET
;
397 * convert the general policy direction to the one defined in ipsec.h
399 static u_int8_t
dir2kernel(policy_dir_t dir
)
404 return IPSEC_DIR_INBOUND
;
406 return IPSEC_DIR_OUTBOUND
;
408 return IPSEC_DIR_FWD
;
414 typedef struct kernel_algorithm_t kernel_algorithm_t
;
417 * Mapping of IKEv2 algorithms to PF_KEY algorithms
419 struct kernel_algorithm_t
{
421 * Identifier specified in IKEv2
426 * Identifier as defined in pfkeyv2.h
431 #define END_OF_LIST -1
434 * Algorithms for encryption
436 static kernel_algorithm_t encryption_algs
[] = {
437 /* {ENCR_DES_IV64, 0 }, */
438 {ENCR_DES
, SADB_EALG_DESCBC
},
439 {ENCR_3DES
, SADB_EALG_3DESCBC
},
440 /* {ENCR_RC5, 0 }, */
441 /* {ENCR_IDEA, 0 }, */
442 {ENCR_CAST
, SADB_X_EALG_CASTCBC
},
443 {ENCR_BLOWFISH
, SADB_X_EALG_BLOWFISHCBC
},
444 /* {ENCR_3IDEA, 0 }, */
445 /* {ENCR_DES_IV32, 0 }, */
446 {ENCR_NULL
, SADB_EALG_NULL
},
447 {ENCR_AES_CBC
, SADB_X_EALG_AESCBC
},
448 /* {ENCR_AES_CTR, SADB_X_EALG_AESCTR }, */
449 /* {ENCR_AES_CCM_ICV8, SADB_X_EALG_AES_CCM_ICV8 }, */
450 /* {ENCR_AES_CCM_ICV12, SADB_X_EALG_AES_CCM_ICV12 }, */
451 /* {ENCR_AES_CCM_ICV16, SADB_X_EALG_AES_CCM_ICV16 }, */
452 /* {ENCR_AES_GCM_ICV8, SADB_X_EALG_AES_GCM_ICV8 }, */
453 /* {ENCR_AES_GCM_ICV12, SADB_X_EALG_AES_GCM_ICV12 }, */
454 /* {ENCR_AES_GCM_ICV16, SADB_X_EALG_AES_GCM_ICV16 }, */
459 * Algorithms for integrity protection
461 static kernel_algorithm_t integrity_algs
[] = {
462 {AUTH_HMAC_MD5_96
, SADB_AALG_MD5HMAC
},
463 {AUTH_HMAC_SHA1_96
, SADB_AALG_SHA1HMAC
},
464 {AUTH_HMAC_SHA2_256_128
, SADB_X_AALG_SHA2_256HMAC
},
465 {AUTH_HMAC_SHA2_384_192
, SADB_X_AALG_SHA2_384HMAC
},
466 {AUTH_HMAC_SHA2_512_256
, SADB_X_AALG_SHA2_512HMAC
},
467 /* {AUTH_DES_MAC, 0, }, */
468 /* {AUTH_KPDK_MD5, 0, }, */
469 {AUTH_AES_XCBC_96
, SADB_X_AALG_AES_XCBC_MAC
, },
474 * Algorithms for IPComp
476 static kernel_algorithm_t compression_algs
[] = {
477 /* {IPCOMP_OUI, 0 }, */
478 {IPCOMP_DEFLATE
, SADB_X_CALG_DEFLATE
},
479 {IPCOMP_LZS
, SADB_X_CALG_LZS
},
480 {IPCOMP_LZJH
, SADB_X_CALG_LZJH
},
485 * Look up a kernel algorithm ID and its key size
487 static int lookup_algorithm(kernel_algorithm_t
*list
, int ikev2
)
489 while (list
->ikev2
!= END_OF_LIST
)
491 if (ikev2
== list
->ikev2
)
501 * add a host behind a sadb_address extension
503 static void host2ext(host_t
*host
, struct sadb_address
*ext
)
505 sockaddr_t
*host_addr
= host
->get_sockaddr(host
);
506 socklen_t
*len
= host
->get_sockaddr_len(host
);
507 memcpy((char*)(ext
+ 1), host_addr
, *len
);
508 ext
->sadb_address_len
= PFKEY_LEN(sizeof(*ext
) + *len
);
512 * add udp encap extensions to a sadb_msg
514 static void add_encap_ext(struct sadb_msg
*msg
, host_t
*src
, host_t
*dst
)
516 struct sadb_x_nat_t_type
* nat_type
;
517 struct sadb_x_nat_t_port
* nat_port
;
519 nat_type
= (struct sadb_x_nat_t_type
*)PFKEY_EXT_ADD_NEXT(msg
);
520 nat_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
521 nat_type
->sadb_x_nat_t_type_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_type
));
522 nat_type
->sadb_x_nat_t_type_type
= UDP_ENCAP_ESPINUDP
;
523 PFKEY_EXT_ADD(msg
, nat_type
);
525 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
526 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
527 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_port
));
528 nat_port
->sadb_x_nat_t_port_port
= htons(src
->get_port(src
));
529 PFKEY_EXT_ADD(msg
, nat_port
);
531 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
532 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
533 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_port
));
534 nat_port
->sadb_x_nat_t_port_port
= htons(dst
->get_port(dst
));
535 PFKEY_EXT_ADD(msg
, nat_port
);
539 * Convert a sadb_address to a traffic_selector
541 static traffic_selector_t
* sadb_address2ts(struct sadb_address
*address
)
543 traffic_selector_t
*ts
;
547 u_int16_t port
, from_port
, to_port
;
549 /* The Linux 2.6 kernel does not set the protocol and port information
550 * in the src and dst sadb_address extensions of the SADB_ACQUIRE message.
552 host
= host_create_from_sockaddr((sockaddr_t
*)&address
[1]) ;
553 type
= (host
->get_family(host
) == AF_INET
) ? TS_IPV4_ADDR_RANGE
:
555 addr
= host
->get_address(host
);
556 port
= host
->get_port(host
);
564 from_port
= to_port
= port
;
566 ts
= traffic_selector_create_from_bytes(address
->sadb_address_proto
, type
,
567 addr
, from_port
, addr
, to_port
);
573 * Parses a pfkey message received from the kernel
575 static status_t
parse_pfkey_message(struct sadb_msg
*msg
, pfkey_msg_t
*out
)
577 struct sadb_ext
* ext
;
580 memset(out
, 0, sizeof(pfkey_msg_t
));
583 len
= msg
->sadb_msg_len
;
584 len
-= PFKEY_LEN(sizeof(struct sadb_msg
));
586 ext
= (struct sadb_ext
*)(((char*)msg
) + sizeof(struct sadb_msg
));
588 while (len
>= PFKEY_LEN(sizeof(struct sadb_ext
)))
590 DBG2(DBG_KNL
, " %N", sadb_ext_type_names
, ext
->sadb_ext_type
);
591 if (ext
->sadb_ext_len
< PFKEY_LEN(sizeof(struct sadb_ext
)) ||
592 ext
->sadb_ext_len
> len
)
594 DBG1(DBG_KNL
, "length of %N extension is invalid",
595 sadb_ext_type_names
, ext
->sadb_ext_type
);
599 if ((ext
->sadb_ext_type
> SADB_EXT_MAX
) || (!ext
->sadb_ext_type
))
601 DBG1(DBG_KNL
, "type of PF_KEY extension (%d) is invalid", ext
->sadb_ext_type
);
605 if (out
->ext
[ext
->sadb_ext_type
])
607 DBG1(DBG_KNL
, "duplicate %N extension",
608 sadb_ext_type_names
, ext
->sadb_ext_type
);
612 out
->ext
[ext
->sadb_ext_type
] = ext
;
613 ext
= PFKEY_EXT_NEXT_LEN(ext
, len
);
618 DBG1(DBG_KNL
, "PF_KEY message length is invalid");
626 * Send a message to a specific PF_KEY socket and handle the response.
628 static status_t
pfkey_send_socket(private_kernel_pfkey_ipsec_t
*this, int socket
,
629 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
631 unsigned char buf
[PFKEY_BUFFER_SIZE
];
632 struct sadb_msg
*msg
;
635 pthread_mutex_lock(&this->mutex_pfkey
);
637 in
->sadb_msg_seq
= ++this->seq
;
638 in
->sadb_msg_pid
= getpid();
640 in_len
= PFKEY_USER_LEN(in
->sadb_msg_len
);
644 len
= send(socket
, in
, in_len
, 0);
650 /* interrupted, try again */
653 pthread_mutex_unlock(&this->mutex_pfkey
);
654 DBG1(DBG_KNL
, "error sending to PF_KEY socket: %s", strerror(errno
));
662 msg
= (struct sadb_msg
*)buf
;
664 len
= recv(socket
, buf
, sizeof(buf
), 0);
670 DBG1(DBG_KNL
, "got interrupted");
671 /* interrupted, try again */
674 DBG1(DBG_KNL
, "error reading from PF_KEY socket: %s", strerror(errno
));
675 pthread_mutex_unlock(&this->mutex_pfkey
);
678 if (len
< sizeof(struct sadb_msg
) ||
679 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
681 DBG1(DBG_KNL
, "received corrupted PF_KEY message");
682 pthread_mutex_unlock(&this->mutex_pfkey
);
685 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
687 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY message");
688 pthread_mutex_unlock(&this->mutex_pfkey
);
691 if (msg
->sadb_msg_pid
!= in
->sadb_msg_pid
)
693 DBG2(DBG_KNL
, "received PF_KEY message is not intended for us");
696 if (msg
->sadb_msg_seq
!= this->seq
)
698 DBG1(DBG_KNL
, "received PF_KEY message with invalid sequence number, "
699 "was %d expected %d", msg
->sadb_msg_seq
, this->seq
);
700 if (msg
->sadb_msg_seq
< this->seq
)
704 pthread_mutex_unlock(&this->mutex_pfkey
);
707 if (msg
->sadb_msg_type
!= in
->sadb_msg_type
)
709 DBG2(DBG_KNL
, "received PF_KEY message of wrong type, "
710 "was %d expected %d, ignoring",
711 msg
->sadb_msg_type
, in
->sadb_msg_type
);
717 *out
= (struct sadb_msg
*)malloc(len
);
718 memcpy(*out
, buf
, len
);
720 pthread_mutex_unlock(&this->mutex_pfkey
);
726 * Send a message to the default PF_KEY socket and handle the response.
728 static status_t
pfkey_send(private_kernel_pfkey_ipsec_t
*this,
729 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
731 return pfkey_send_socket(this, this->socket
, in
, out
, out_len
);
735 * Process a SADB_ACQUIRE message from the kernel
737 static void process_acquire(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
739 pfkey_msg_t response
;
740 u_int32_t index
, reqid
= 0;
741 traffic_selector_t
*src_ts
, *dst_ts
;
742 policy_entry_t
*policy
;
745 switch (msg
->sadb_msg_satype
)
747 case SADB_SATYPE_UNSPEC
:
748 case SADB_SATYPE_ESP
:
752 /* acquire for AH/ESP only */
755 DBG2(DBG_KNL
, "received an SADB_ACQUIRE");
757 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
759 DBG1(DBG_KNL
, "parsing SADB_ACQUIRE from kernel failed");
763 index
= response
.x_policy
->sadb_x_policy_id
;
764 pthread_mutex_lock(&this->mutex
);
765 if (this->policies
->find_first(this->policies
,
766 (linked_list_match_t
)policy_entry_match_byindex
, (void**)&policy
, &index
) == SUCCESS
)
768 reqid
= policy
->reqid
;
772 DBG1(DBG_KNL
, "received an SADB_ACQUIRE with policy id %d but no matching policy found",
775 src_ts
= sadb_address2ts(response
.src
);
776 dst_ts
= sadb_address2ts(response
.dst
);
777 pthread_mutex_unlock(&this->mutex
);
779 DBG1(DBG_KNL
, "creating acquire job %R === %R for CHILD_SA with reqid {%d}",
780 src_ts
, dst_ts
, reqid
);
781 job
= (job_t
*)acquire_job_create(reqid
, src_ts
, dst_ts
);
782 charon
->processor
->queue_job(charon
->processor
, job
);
786 * Process a SADB_EXPIRE message from the kernel
788 static void process_expire(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
790 pfkey_msg_t response
;
791 protocol_id_t protocol
;
792 u_int32_t spi
, reqid
;
796 DBG2(DBG_KNL
, "received an SADB_EXPIRE");
798 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
800 DBG1(DBG_KNL
, "parsing SADB_EXPIRE from kernel failed");
804 protocol
= proto_satype2ike(msg
->sadb_msg_satype
);
805 spi
= response
.sa
->sadb_sa_spi
;
806 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
807 hard
= response
.lft_hard
!= NULL
;
809 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
811 DBG2(DBG_KNL
, "ignoring SADB_EXPIRE for SA with SPI %.8x and reqid {%d} "
812 "which is not a CHILD_SA", ntohl(spi
), reqid
);
816 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
817 hard ?
"delete" : "rekey", protocol_id_names
,
818 protocol
, ntohl(spi
), reqid
);
821 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
825 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
827 charon
->processor
->queue_job(charon
->processor
, job
);
831 * Process a SADB_MIGRATE message from the kernel
833 static void process_migrate(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
835 pfkey_msg_t response
;
837 DBG2(DBG_KNL
, "received an SADB_X_MIGRATE");
839 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
841 DBG1(DBG_KNL
, "parsing SADB_X_MIGRATE from kernel failed");
847 * Process a SADB_X_NAT_T_NEW_MAPPING message from the kernel
849 static void process_mapping(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
851 pfkey_msg_t response
;
852 u_int32_t spi
, reqid
;
856 DBG2(DBG_KNL
, "received an SADB_X_NAT_T_NEW_MAPPING");
858 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
860 DBG1(DBG_KNL
, "parsing SADB_X_NAT_T_NEW_MAPPING from kernel failed");
866 DBG1(DBG_KNL
, "received SADB_X_NAT_T_NEW_MAPPING is missing required information");
870 spi
= response
.sa
->sadb_sa_spi
;
871 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
873 if (proto_satype2ike(msg
->sadb_msg_satype
) == PROTO_ESP
)
875 sockaddr_t
*sa
= (sockaddr_t
*)(response
.dst
+ 1);
876 switch (sa
->sa_family
)
880 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
881 sin
->sin_port
= response
.x_natt_dport
->sadb_x_nat_t_port_port
;
885 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
886 sin6
->sin6_port
= response
.x_natt_dport
->sadb_x_nat_t_port_port
;
891 host
= host_create_from_sockaddr(sa
);
894 DBG1(DBG_KNL
, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
895 "reqid {%d} changed, queuing update job", ntohl(spi
), reqid
);
896 job
= (job_t
*)update_sa_job_create(reqid
, host
);
897 charon
->processor
->queue_job(charon
->processor
, job
);
903 * Receives events from kernel
905 static job_requeue_t
receive_events(private_kernel_pfkey_ipsec_t
*this)
907 unsigned char buf
[PFKEY_BUFFER_SIZE
];
908 struct sadb_msg
*msg
= (struct sadb_msg
*)buf
;
911 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
912 len
= recv(this->socket_events
, buf
, sizeof(buf
), 0);
913 pthread_setcancelstate(oldstate
, NULL
);
920 /* interrupted, try again */
921 return JOB_REQUEUE_DIRECT
;
923 /* no data ready, select again */
924 return JOB_REQUEUE_DIRECT
;
926 DBG1(DBG_KNL
, "unable to receive from PF_KEY event socket");
928 return JOB_REQUEUE_FAIR
;
932 if (len
< sizeof(struct sadb_msg
) ||
933 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
935 DBG2(DBG_KNL
, "received corrupted PF_KEY message");
936 return JOB_REQUEUE_DIRECT
;
938 if (msg
->sadb_msg_pid
!= 0)
939 { /* not from kernel. not interested, try another one */
940 return JOB_REQUEUE_DIRECT
;
942 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
944 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY message");
945 return JOB_REQUEUE_DIRECT
;
948 switch (msg
->sadb_msg_type
)
951 process_acquire(this, msg
);
954 process_expire(this, msg
);
957 process_migrate(this, msg
);
959 case SADB_X_NAT_T_NEW_MAPPING
:
960 process_mapping(this, msg
);
966 return JOB_REQUEUE_DIRECT
;
970 * Implementation of kernel_interface_t.get_spi.
972 static status_t
get_spi(private_kernel_pfkey_ipsec_t
*this,
973 host_t
*src
, host_t
*dst
,
974 protocol_id_t protocol
, u_int32_t reqid
,
977 unsigned char request
[PFKEY_BUFFER_SIZE
];
978 struct sadb_msg
*msg
, *out
;
980 struct sadb_x_sa2
*sa2
;
981 struct sadb_address
*addr
;
982 struct sadb_spirange
*range
;
983 pfkey_msg_t response
;
984 u_int32_t received_spi
= 0;
987 memset(&request
, 0, sizeof(request
));
989 msg
= (struct sadb_msg
*)request
;
990 msg
->sadb_msg_version
= PF_KEY_V2
;
991 msg
->sadb_msg_type
= SADB_GETSPI
;
992 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
993 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
995 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
996 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
997 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
998 sa2
->sadb_x_sa2_reqid
= reqid
;
999 PFKEY_EXT_ADD(msg
, sa2
);
1001 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1002 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1003 host2ext(src
, addr
);
1004 PFKEY_EXT_ADD(msg
, addr
);
1006 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1007 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1008 host2ext(dst
, addr
);
1009 PFKEY_EXT_ADD(msg
, addr
);
1011 range
= (struct sadb_spirange
*)PFKEY_EXT_ADD_NEXT(msg
);
1012 range
->sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
1013 range
->sadb_spirange_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1014 range
->sadb_spirange_min
= 0xc0000000;
1015 range
->sadb_spirange_max
= 0xcFFFFFFF;
1016 PFKEY_EXT_ADD(msg
, range
);
1018 if (pfkey_send(this, msg
, &out
, &len
) == SUCCESS
)
1020 if (out
->sadb_msg_errno
)
1022 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1023 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1025 else if (parse_pfkey_message(out
, &response
) == SUCCESS
)
1027 received_spi
= response
.sa
->sadb_sa_spi
;
1032 if (received_spi
== 0)
1037 *spi
= received_spi
;
1042 * Implementation of kernel_interface_t.get_cpi.
1044 static status_t
get_cpi(private_kernel_pfkey_ipsec_t
*this,
1045 host_t
*src
, host_t
*dst
,
1046 u_int32_t reqid
, u_int16_t
*cpi
)
1052 * Implementation of kernel_interface_t.add_sa.
1054 static status_t
add_sa(private_kernel_pfkey_ipsec_t
*this,
1055 host_t
*src
, host_t
*dst
, u_int32_t spi
,
1056 protocol_id_t protocol
, u_int32_t reqid
,
1057 u_int64_t expire_soft
, u_int64_t expire_hard
,
1058 u_int16_t enc_alg
, chunk_t enc_key
,
1059 u_int16_t int_alg
, chunk_t int_key
,
1060 ipsec_mode_t mode
, u_int16_t ipcomp
, bool encap
,
1063 unsigned char request
[PFKEY_BUFFER_SIZE
];
1064 struct sadb_msg
*msg
, *out
;
1066 struct sadb_x_sa2
*sa2
;
1067 struct sadb_address
*addr
;
1068 struct sadb_lifetime
*lft
;
1069 struct sadb_key
*key
;
1072 memset(&request
, 0, sizeof(request
));
1074 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%d}", ntohl(spi
), reqid
);
1076 msg
= (struct sadb_msg
*)request
;
1077 msg
->sadb_msg_version
= PF_KEY_V2
;
1078 msg
->sadb_msg_type
= replace ? SADB_UPDATE
: SADB_ADD
;
1079 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
1080 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1082 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1083 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1084 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1085 sa
->sadb_sa_spi
= spi
;
1086 sa
->sadb_sa_replay
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
1087 sa
->sadb_sa_auth
= lookup_algorithm(integrity_algs
, int_alg
);
1088 sa
->sadb_sa_encrypt
= lookup_algorithm(encryption_algs
, enc_alg
);
1089 PFKEY_EXT_ADD(msg
, sa
);
1091 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
1092 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
1093 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1094 sa2
->sadb_x_sa2_mode
= mode2kernel(mode
);
1095 sa2
->sadb_x_sa2_reqid
= reqid
;
1096 PFKEY_EXT_ADD(msg
, sa2
);
1098 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1099 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1100 host2ext(src
, addr
);
1101 PFKEY_EXT_ADD(msg
, addr
);
1103 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1104 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1105 host2ext(dst
, addr
);
1106 PFKEY_EXT_ADD(msg
, addr
);
1108 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1109 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
1110 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1111 lft
->sadb_lifetime_addtime
= expire_soft
;
1112 PFKEY_EXT_ADD(msg
, lft
);
1114 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1115 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1116 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1117 lft
->sadb_lifetime_addtime
= expire_hard
;
1118 PFKEY_EXT_ADD(msg
, lft
);
1120 if (enc_alg
!= ENCR_UNDEFINED
)
1122 if (!sa
->sadb_sa_encrypt
)
1124 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1125 encryption_algorithm_names
, enc_alg
);
1128 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1129 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1131 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1132 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
1133 key
->sadb_key_bits
= enc_key
.len
* 8;
1134 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + enc_key
.len
);
1135 memcpy(key
+ 1, enc_key
.ptr
, enc_key
.len
);
1137 PFKEY_EXT_ADD(msg
, key
);
1140 if (int_alg
!= AUTH_UNDEFINED
)
1142 if (!sa
->sadb_sa_auth
)
1144 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1145 integrity_algorithm_names
, int_alg
);
1148 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1149 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1151 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1152 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
1153 key
->sadb_key_bits
= int_key
.len
* 8;
1154 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + int_key
.len
);
1155 memcpy(key
+ 1, int_key
.ptr
, int_key
.len
);
1157 PFKEY_EXT_ADD(msg
, key
);
1160 if (ipcomp
!= IPCOMP_NONE
)
1167 add_encap_ext(msg
, src
, dst
);
1170 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1172 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1175 else if (out
->sadb_msg_errno
)
1177 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x: %s (%d)",
1178 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1188 * Implementation of kernel_interface_t.update_sa.
1190 static status_t
update_sa(private_kernel_pfkey_ipsec_t
*this,
1191 u_int32_t spi
, protocol_id_t protocol
,
1192 host_t
*src
, host_t
*dst
,
1193 host_t
*new_src
, host_t
*new_dst
, bool encap
)
1195 unsigned char request
[PFKEY_BUFFER_SIZE
];
1196 struct sadb_msg
*msg
, *out
;
1198 struct sadb_address
*addr
;
1199 pfkey_msg_t response
;
1202 memset(&request
, 0, sizeof(request
));
1204 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1206 msg
= (struct sadb_msg
*)request
;
1207 msg
->sadb_msg_version
= PF_KEY_V2
;
1208 msg
->sadb_msg_type
= SADB_GET
;
1209 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
1210 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1212 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1213 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1214 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1215 sa
->sadb_sa_spi
= spi
;
1216 PFKEY_EXT_ADD(msg
, sa
);
1218 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1219 * it is not used for anything, so we just send dst twice */
1220 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1221 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1222 host2ext(dst
, addr
);
1223 PFKEY_EXT_ADD(msg
, addr
);
1225 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1226 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1227 host2ext(dst
, addr
);
1228 PFKEY_EXT_ADD(msg
, addr
);
1230 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1232 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x",
1236 else if (out
->sadb_msg_errno
)
1238 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: %s (%d)",
1239 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1243 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1245 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: parsing response "
1246 "from kernel failed", ntohl(spi
));
1251 /* delete the old SA */
1252 if (this->public.interface
.del_sa(&this->public.interface
, dst
, spi
, protocol
) != SUCCESS
)
1254 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x", ntohl(spi
));
1259 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1260 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1262 memset(&request
, 0, sizeof(request
));
1264 msg
= (struct sadb_msg
*)request
;
1265 msg
->sadb_msg_version
= PF_KEY_V2
;
1266 msg
->sadb_msg_type
= SADB_ADD
;
1267 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
1268 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1270 PFKEY_EXT_COPY(msg
, response
.sa
);
1271 PFKEY_EXT_COPY(msg
, response
.x_sa2
);
1273 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1274 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1275 host2ext(new_src
, addr
);
1276 PFKEY_EXT_ADD(msg
, addr
);
1278 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1279 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1280 host2ext(new_dst
, addr
);
1281 PFKEY_EXT_ADD(msg
, addr
);
1283 PFKEY_EXT_COPY(msg
, response
.lft_soft
);
1284 PFKEY_EXT_COPY(msg
, response
.lft_hard
);
1286 if (response
.key_encr
)
1288 PFKEY_EXT_COPY(msg
, response
.key_encr
);
1291 if (response
.key_auth
)
1293 PFKEY_EXT_COPY(msg
, response
.key_auth
);
1298 add_encap_ext(msg
, new_src
, new_dst
);
1303 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1305 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1308 else if (out
->sadb_msg_errno
)
1310 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: %s (%d)",
1311 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1321 * Implementation of kernel_interface_t.del_sa.
1323 static status_t
del_sa(private_kernel_pfkey_ipsec_t
*this, host_t
*dst
,
1324 u_int32_t spi
, protocol_id_t protocol
)
1326 unsigned char request
[PFKEY_BUFFER_SIZE
];
1327 struct sadb_msg
*msg
, *out
;
1329 struct sadb_address
*addr
;
1332 memset(&request
, 0, sizeof(request
));
1334 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1336 msg
= (struct sadb_msg
*)request
;
1337 msg
->sadb_msg_version
= PF_KEY_V2
;
1338 msg
->sadb_msg_type
= SADB_DELETE
;
1339 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
1340 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1342 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1343 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1344 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1345 sa
->sadb_sa_spi
= spi
;
1346 PFKEY_EXT_ADD(msg
, sa
);
1348 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1349 * it is not used for anything, so we just send dst twice */
1350 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1351 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1352 host2ext(dst
, addr
);
1353 PFKEY_EXT_ADD(msg
, addr
);
1355 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1356 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1357 host2ext(dst
, addr
);
1358 PFKEY_EXT_ADD(msg
, addr
);
1360 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1362 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1365 else if (out
->sadb_msg_errno
)
1367 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1368 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1373 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1379 * Implementation of kernel_interface_t.add_policy.
1381 static status_t
add_policy(private_kernel_pfkey_ipsec_t
*this,
1382 host_t
*src
, host_t
*dst
,
1383 traffic_selector_t
*src_ts
,
1384 traffic_selector_t
*dst_ts
,
1385 policy_dir_t direction
, protocol_id_t protocol
,
1386 u_int32_t reqid
, bool high_prio
, ipsec_mode_t mode
,
1389 unsigned char request
[PFKEY_BUFFER_SIZE
];
1390 struct sadb_msg
*msg
, *out
;
1391 struct sadb_x_policy
*pol
;
1392 struct sadb_address
*addr
;
1393 struct sadb_x_ipsecrequest
*req
;
1394 policy_entry_t
*policy
, *found
= NULL
;
1395 pfkey_msg_t response
;
1398 /* create a policy */
1399 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, reqid
);
1401 /* find a matching policy */
1402 pthread_mutex_lock(&this->mutex
);
1403 if (this->policies
->find_first(this->policies
,
1404 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) == SUCCESS
)
1406 /* use existing policy */
1408 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
1409 "refcount", src_ts
, dst_ts
,
1410 policy_dir_names
, direction
);
1411 policy_entry_destroy(policy
);
1416 /* apply the new one, if we have no such policy */
1417 this->policies
->insert_last(this->policies
, policy
);
1418 policy
->refcount
= 1;
1421 memset(&request
, 0, sizeof(request
));
1423 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
1424 policy_dir_names
, direction
);
1426 msg
= (struct sadb_msg
*)request
;
1427 msg
->sadb_msg_version
= PF_KEY_V2
;
1428 msg
->sadb_msg_type
= found ? SADB_X_SPDUPDATE
: SADB_X_SPDADD
;
1429 msg
->sadb_msg_satype
= 0;
1430 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1432 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1433 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1434 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1435 pol
->sadb_x_policy_id
= 0;
1436 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1437 /* calculate priority based on source selector size, small size = high prio */
1438 pol
->sadb_x_policy_priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
1439 pol
->sadb_x_policy_priority
-= policy
->src
.mask
* 10;
1440 pol
->sadb_x_policy_priority
-= policy
->src
.proto
!= IPSEC_PROTO_ANY ?
2 : 0;
1441 pol
->sadb_x_policy_priority
-= policy
->src
.net
->get_port(policy
->src
.net
) ?
1 : 0;
1442 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1444 /* one or more sadb_x_ipsecrequest extensions are added to the sadb_x_policy extension */
1445 req
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
1446 req
->sadb_x_ipsecrequest_proto
= proto_ike2ip(protocol
);
1447 /* !!! the length of this struct MUST be in octets instead of 64 bit words */
1448 req
->sadb_x_ipsecrequest_len
= sizeof(struct sadb_x_ipsecrequest
);
1449 req
->sadb_x_ipsecrequest_mode
= mode2kernel(mode
);
1450 req
->sadb_x_ipsecrequest_reqid
= reqid
;
1451 req
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
1452 if (mode
== MODE_TUNNEL
)
1456 sa
= src
->get_sockaddr(src
);
1457 sl
= *src
->get_sockaddr_len(src
);
1458 memcpy(req
+ 1, sa
, sl
);
1459 sa
= dst
->get_sockaddr(dst
);
1460 memcpy((u_int8_t
*)(req
+ 1) + sl
, sa
, sl
);
1461 req
->sadb_x_ipsecrequest_len
+= sl
* 2;
1464 pol
->sadb_x_policy_len
+= PFKEY_LEN(req
->sadb_x_ipsecrequest_len
);
1465 PFKEY_EXT_ADD(msg
, pol
);
1467 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1468 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1469 addr
->sadb_address_proto
= policy
->src
.proto
;
1470 addr
->sadb_address_prefixlen
= policy
->src
.mask
;
1471 host2ext(policy
->src
.net
, addr
);
1472 PFKEY_EXT_ADD(msg
, addr
);
1474 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1475 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1476 addr
->sadb_address_proto
= policy
->dst
.proto
;
1477 addr
->sadb_address_prefixlen
= policy
->dst
.mask
;
1478 host2ext(policy
->dst
.net
, addr
);
1479 PFKEY_EXT_ADD(msg
, addr
);
1481 pthread_mutex_unlock(&this->mutex
);
1483 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1485 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
1486 policy_dir_names
, direction
);
1489 else if (out
->sadb_msg_errno
)
1491 DBG1(DBG_KNL
, "unable to add policy %R === %R %N: %s (%d)", src_ts
, dst_ts
,
1492 policy_dir_names
, direction
,
1493 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1497 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1499 DBG1(DBG_KNL
, "unable to add policy %R === %R %N: parsing response "
1500 "from kernel failed", src_ts
, dst_ts
, policy_dir_names
, direction
);
1505 pthread_mutex_lock(&this->mutex
);
1507 /* we try to find the policy again and update the kernel index */
1508 if (this->policies
->find_last(this->policies
, NULL
, (void**)&policy
) != SUCCESS
)
1510 DBG2(DBG_KNL
, "unable to update index, the policy %R === %R %N is "
1511 "already gone, ignoring", src_ts
, dst_ts
, policy_dir_names
, direction
);
1512 pthread_mutex_unlock(&this->mutex
);
1516 policy
->index
= response
.x_policy
->sadb_x_policy_id
;
1519 /* install a route, if:
1520 * - we are NOT updating a policy
1521 * - this is a forward policy (to just get one for each child)
1522 * - we are in tunnel mode
1523 * - we are not using IPv6 (does not work correctly yet!)
1524 * - routing is not disabled via strongswan.conf
1526 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1527 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
1528 this->install_routes
)
1530 route_entry_t
*route
= malloc_thing(route_entry_t
);
1532 if (charon
->kernel_interface
->get_address_by_ts(charon
->kernel_interface
,
1533 dst_ts
, &route
->src_ip
) == SUCCESS
)
1535 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1536 route
->gateway
= charon
->kernel_interface
->get_nexthop(
1537 charon
->kernel_interface
, src
);
1538 route
->if_name
= charon
->kernel_interface
->get_interface(
1539 charon
->kernel_interface
, dst
);
1540 route
->dst_net
= chunk_clone(policy
->src
.net
->get_address(policy
->src
.net
));
1541 route
->prefixlen
= policy
->src
.mask
;
1543 switch (charon
->kernel_interface
->add_route(charon
->kernel_interface
,
1544 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1545 route
->src_ip
, route
->if_name
))
1548 DBG1(DBG_KNL
, "unable to install source route for %H",
1552 /* route exists, do not uninstall */
1553 route_entry_destroy(route
);
1556 /* cache the installed route */
1557 policy
->route
= route
;
1567 pthread_mutex_unlock(&this->mutex
);
1573 * Implementation of kernel_interface_t.query_policy.
1575 static status_t
query_policy(private_kernel_pfkey_ipsec_t
*this,
1576 traffic_selector_t
*src_ts
,
1577 traffic_selector_t
*dst_ts
,
1578 policy_dir_t direction
, u_int32_t
*use_time
)
1580 unsigned char request
[PFKEY_BUFFER_SIZE
];
1581 struct sadb_msg
*msg
, *out
;
1582 struct sadb_x_policy
*pol
;
1583 struct sadb_address
*addr
;
1584 policy_entry_t
*policy
, *found
= NULL
;
1585 pfkey_msg_t response
;
1588 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
1589 policy_dir_names
, direction
);
1591 /* create a policy */
1592 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, 0);
1594 /* find a matching policy */
1595 pthread_mutex_lock(&this->mutex
);
1596 if (this->policies
->find_first(this->policies
,
1597 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) != SUCCESS
)
1599 DBG1(DBG_KNL
, "querying policy %R === %R %N failed, not found", src_ts
,
1600 dst_ts
, policy_dir_names
, direction
);
1601 policy_entry_destroy(policy
);
1602 pthread_mutex_unlock(&this->mutex
);
1605 policy_entry_destroy(policy
);
1608 memset(&request
, 0, sizeof(request
));
1610 msg
= (struct sadb_msg
*)request
;
1611 msg
->sadb_msg_version
= PF_KEY_V2
;
1612 msg
->sadb_msg_type
= SADB_X_SPDGET
;
1613 msg
->sadb_msg_satype
= 0;
1614 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1616 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1617 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1618 pol
->sadb_x_policy_id
= policy
->index
;
1619 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1620 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1621 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1622 PFKEY_EXT_ADD(msg
, pol
);
1624 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1625 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1626 addr
->sadb_address_proto
= policy
->src
.proto
;
1627 addr
->sadb_address_prefixlen
= policy
->src
.mask
;
1628 host2ext(policy
->src
.net
, addr
);
1629 PFKEY_EXT_ADD(msg
, addr
);
1631 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1632 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1633 addr
->sadb_address_proto
= policy
->dst
.proto
;
1634 addr
->sadb_address_prefixlen
= policy
->dst
.mask
;
1635 host2ext(policy
->dst
.net
, addr
);
1636 PFKEY_EXT_ADD(msg
, addr
);
1638 pthread_mutex_unlock(&this->mutex
);
1640 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1642 DBG1(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
1643 policy_dir_names
, direction
);
1646 else if (out
->sadb_msg_errno
)
1648 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: %s (%d)", src_ts
,
1649 dst_ts
, policy_dir_names
, direction
,
1650 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1654 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1656 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: parsing response "
1657 "from kernel failed", src_ts
, dst_ts
, policy_dir_names
, direction
);
1662 *use_time
= response
.lft_current
->sadb_lifetime_usetime
;
1670 * Implementation of kernel_interface_t.del_policy.
1672 static status_t
del_policy(private_kernel_pfkey_ipsec_t
*this,
1673 traffic_selector_t
*src_ts
,
1674 traffic_selector_t
*dst_ts
,
1675 policy_dir_t direction
)
1677 unsigned char request
[PFKEY_BUFFER_SIZE
];
1678 struct sadb_msg
*msg
, *out
;
1679 struct sadb_x_policy
*pol
;
1680 struct sadb_address
*addr
;
1681 policy_entry_t
*policy
, *found
= NULL
;
1682 route_entry_t
*route
;
1685 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
1686 policy_dir_names
, direction
);
1688 /* create a policy */
1689 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, 0);
1691 /* find a matching policy */
1692 pthread_mutex_lock(&this->mutex
);
1693 if (this->policies
->find_first(this->policies
,
1694 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) == SUCCESS
)
1696 if (--found
->refcount
> 0)
1698 /* is used by more SAs, keep in kernel */
1699 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1700 policy_entry_destroy(policy
);
1701 pthread_mutex_unlock(&this->mutex
);
1704 /* remove if last reference */
1705 this->policies
->remove(this->policies
, found
, NULL
);
1706 policy_entry_destroy(policy
);
1711 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
1712 dst_ts
, policy_dir_names
, direction
);
1713 policy_entry_destroy(policy
);
1714 pthread_mutex_unlock(&this->mutex
);
1717 pthread_mutex_unlock(&this->mutex
);
1719 memset(&request
, 0, sizeof(request
));
1721 msg
= (struct sadb_msg
*)request
;
1722 msg
->sadb_msg_version
= PF_KEY_V2
;
1723 msg
->sadb_msg_type
= SADB_X_SPDDELETE
;
1724 msg
->sadb_msg_satype
= 0;
1725 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1727 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1728 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1729 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1730 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1731 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1732 PFKEY_EXT_ADD(msg
, pol
);
1734 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1735 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1736 addr
->sadb_address_proto
= policy
->src
.proto
;
1737 addr
->sadb_address_prefixlen
= policy
->src
.mask
;
1738 host2ext(policy
->src
.net
, addr
);
1739 PFKEY_EXT_ADD(msg
, addr
);
1741 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1742 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1743 addr
->sadb_address_proto
= policy
->dst
.proto
;
1744 addr
->sadb_address_prefixlen
= policy
->dst
.mask
;
1745 host2ext(policy
->dst
.net
, addr
);
1746 PFKEY_EXT_ADD(msg
, addr
);
1748 route
= policy
->route
;
1749 policy
->route
= NULL
;
1750 policy_entry_destroy(policy
);
1752 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1754 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
1755 policy_dir_names
, direction
);
1758 else if (out
->sadb_msg_errno
)
1760 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N: %s (%d)", src_ts
,
1761 dst_ts
, policy_dir_names
, direction
,
1762 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1770 if (charon
->kernel_interface
->del_route(charon
->kernel_interface
,
1771 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1772 route
->src_ip
, route
->if_name
) != SUCCESS
)
1774 DBG1(DBG_KNL
, "error uninstalling route installed with "
1775 "policy %R === %R %N", src_ts
, dst_ts
,
1776 policy_dir_names
, direction
);
1778 route_entry_destroy(route
);
1785 * Register a socket for AQUIRE/EXPIRE messages
1787 static status_t
register_pfkey_socket(private_kernel_pfkey_ipsec_t
*this, u_int8_t satype
)
1789 unsigned char request
[PFKEY_BUFFER_SIZE
];
1790 struct sadb_msg
*msg
, *out
;
1791 struct sadb_x_policy
*pol
;
1792 struct sadb_address
*addr
;
1793 policy_entry_t
*policy
, *found
= NULL
;
1794 pfkey_msg_t response
;
1797 memset(&request
, 0, sizeof(request
));
1799 msg
= (struct sadb_msg
*)request
;
1800 msg
->sadb_msg_version
= PF_KEY_V2
;
1801 msg
->sadb_msg_type
= SADB_REGISTER
;
1802 msg
->sadb_msg_satype
= satype
;
1803 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1805 if (pfkey_send_socket(this, this->socket_events
, msg
, &out
, &len
) != SUCCESS
)
1807 DBG1(DBG_KNL
, "unable to register PF_KEY socket");
1810 else if (out
->sadb_msg_errno
)
1812 DBG1(DBG_KNL
, "unable to register PF_KEY socket: %s (%d)",
1813 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1822 * Implementation of kernel_interface_t.destroy.
1824 static void destroy(private_kernel_pfkey_ipsec_t
*this)
1826 this->job
->cancel(this->job
);
1827 close(this->socket
);
1828 close(this->socket_events
);
1829 this->policies
->destroy_function(this->policies
, (void*)policy_entry_destroy
);
1834 * Described in header.
1836 kernel_pfkey_ipsec_t
*kernel_pfkey_ipsec_create()
1838 private_kernel_pfkey_ipsec_t
*this = malloc_thing(private_kernel_pfkey_ipsec_t
);
1840 /* public functions */
1841 this->public.interface
.get_spi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
1842 this->public.interface
.get_cpi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
1843 this->public.interface
.add_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
,u_int64_t
,u_int64_t
,u_int16_t
,chunk_t
,u_int16_t
,chunk_t
,ipsec_mode_t
,u_int16_t
,bool,bool))add_sa
;
1844 this->public.interface
.update_sa
= (status_t(*)(kernel_ipsec_t
*,u_int32_t
,protocol_id_t
,host_t
*,host_t
*,host_t
*,host_t
*,bool))update_sa
;
1845 this->public.interface
.del_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
1846 this->public.interface
.add_policy
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,protocol_id_t
,u_int32_t
,bool,ipsec_mode_t
,u_int16_t
))add_policy
;
1847 this->public.interface
.query_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
1848 this->public.interface
.del_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
1850 this->public.interface
.destroy
= (void(*)(kernel_ipsec_t
*)) destroy
;
1852 /* private members */
1853 this->policies
= linked_list_create();
1854 pthread_mutex_init(&this->mutex
, NULL
);
1855 this->install_routes
= lib
->settings
->get_bool(lib
->settings
, "charon.install_routes", TRUE
);
1856 pthread_mutex_init(&this->mutex_pfkey
, NULL
);
1859 /* create a PF_KEY socket to communicate with the kernel */
1860 this->socket
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
1861 if (this->socket
<= 0)
1863 charon
->kill(charon
, "unable to create PF_KEY socket");
1866 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
1867 this->socket_events
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
1868 if (this->socket_events
<= 0)
1870 charon
->kill(charon
, "unable to create PF_KEY event socket");
1873 /* register the event socket */
1874 if (register_pfkey_socket(this, SADB_SATYPE_ESP
) != SUCCESS
||
1875 register_pfkey_socket(this, SADB_SATYPE_AH
) != SUCCESS
)
1877 charon
->kill(charon
, "unable to register PF_KEY event socket");
1880 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
1882 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
1884 return &this->public;