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 <processing/jobs/callback_job.h>
32 #include <processing/jobs/acquire_job.h>
33 #include <processing/jobs/rekey_child_sa_job.h>
34 #include <processing/jobs/delete_child_sa_job.h>
35 #include <processing/jobs/update_sa_job.h>
37 /** default priority of installed policies */
39 #define PRIO_HIGH 2000
41 /** buffer size for PF_KEY messages */
42 #define PFKEY_BUFFER_SIZE 2048
44 /** PF_KEY messages are 64 bit aligned */
45 #define PFKEY_ALIGNMENT 8
46 /** aligns len to 64 bits */
47 #define PFKEY_ALIGN(len) (((len) + PFKEY_ALIGNMENT - 1) & ~(PFKEY_ALIGNMENT - 1))
48 /** calculates the properly padded length in 64 bit chunks */
49 #define PFKEY_LEN(len) ((PFKEY_ALIGN(len) / PFKEY_ALIGNMENT))
50 /** calculates user mode length i.e. in bytes */
51 #define PFKEY_USER_LEN(len) ((len) * PFKEY_ALIGNMENT)
53 /** given a PF_KEY message header and an extension this updates the length in the header */
54 #define PFKEY_EXT_ADD(msg, ext) ((msg)->sadb_msg_len += ((struct sadb_ext*)ext)->sadb_ext_len)
55 /** given a PF_KEY message header this returns a pointer to the next extension */
56 #define PFKEY_EXT_ADD_NEXT(msg) ((struct sadb_ext*)(((char*)(msg)) + PFKEY_USER_LEN((msg)->sadb_msg_len)))
57 /** copy an extension and append it to a PF_KEY message */
58 #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))))
59 /** given a PF_KEY extension this returns a pointer to the next extension */
60 #define PFKEY_EXT_NEXT(ext) ((struct sadb_ext*)(((char*)(ext)) + PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len)))
61 /** given a PF_KEY extension this returns a pointer to the next extension also updates len (len in 64 bit words) */
62 #define PFKEY_EXT_NEXT_LEN(ext,len) ((len) -= (ext)->sadb_ext_len, PFKEY_EXT_NEXT(ext))
63 /** true if ext has a valid length and len is large enough to contain ext (assuming len in 64 bit words) */
64 #define PFKEY_EXT_OK(ext,len) ((len) >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
65 (ext)->sadb_ext_len >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
66 (ext)->sadb_ext_len <= (len))
68 typedef struct private_kernel_pfkey_ipsec_t private_kernel_pfkey_ipsec_t
;
71 * Private variables and functions of kernel_pfkey class.
73 struct private_kernel_pfkey_ipsec_t
76 * Public part of the kernel_pfkey_t object.
78 kernel_pfkey_ipsec_t
public;
81 * mutex to lock access to various lists
83 pthread_mutex_t mutex
;
86 * List of installed policies (policy_entry_t)
88 linked_list_t
*policies
;
91 * whether to install routes along policies
96 * job receiving PF_KEY events
101 * mutex to lock access to the PF_KEY socket
103 pthread_mutex_t mutex_pfkey
;
107 * PF_KEY socket to communicate with the kernel
113 * PF_KEY socket to receive acquire and expire events
119 * sequence number for messages sent to the kernel
124 typedef struct route_entry_t route_entry_t
;
127 * installed routing entry
129 struct route_entry_t
{
130 /** Name of the interface the route is bound to */
133 /** Source ip of the route */
136 /** gateway for this route */
139 /** Destination net */
142 /** Destination net prefixlen */
147 * destroy an route_entry_t object
149 static void route_entry_destroy(route_entry_t
*this)
152 this->src_ip
->destroy(this->src_ip
);
153 this->gateway
->destroy(this->gateway
);
154 chunk_free(&this->dst_net
);
158 typedef struct policy_entry_t policy_entry_t
;
161 * installed kernel policy.
163 struct policy_entry_t
{
165 /** reqid of this policy */
168 /** index assigned by the kernel */
171 /** direction of this policy: in, out, forward */
174 /** parameters of installed policy */
176 /** subnet and port */
184 /** associated route installed for this policy */
185 route_entry_t
*route
;
187 /** by how many CHILD_SA's this policy is used */
192 * create a policy_entry_t object
194 static policy_entry_t
*create_policy_entry(traffic_selector_t
*src_ts
,
195 traffic_selector_t
*dst_ts
, policy_dir_t dir
, u_int32_t reqid
)
197 policy_entry_t
*policy
= malloc_thing(policy_entry_t
);
198 policy
->reqid
= reqid
;
200 policy
->direction
= dir
;
201 policy
->route
= NULL
;
202 policy
->refcount
= 0;
204 src_ts
->to_subnet(src_ts
, &policy
->src
.net
, &policy
->src
.mask
);
205 dst_ts
->to_subnet(dst_ts
, &policy
->dst
.net
, &policy
->dst
.mask
);
207 /* src or dest proto may be "any" (0), use more restrictive one */
208 policy
->src
.proto
= max(src_ts
->get_protocol(src_ts
), dst_ts
->get_protocol(dst_ts
));
209 policy
->src
.proto
= policy
->src
.proto ? policy
->src
.proto
: IPSEC_PROTO_ANY
;
210 policy
->dst
.proto
= policy
->src
.proto
;
216 * destroy a policy_entry_t object
218 static void policy_entry_destroy(policy_entry_t
*this)
220 DESTROY_IF(this->src
.net
);
221 DESTROY_IF(this->dst
.net
);
224 route_entry_destroy(this->route
);
230 * compares two policy_entry_t
232 static inline bool policy_entry_equals(policy_entry_t
*current
, policy_entry_t
*policy
)
234 return current
->direction
== policy
->direction
&&
235 current
->src
.proto
== policy
->src
.proto
&&
236 current
->dst
.proto
== policy
->dst
.proto
&&
237 current
->src
.mask
== policy
->src
.mask
&&
238 current
->dst
.mask
== policy
->dst
.mask
&&
239 current
->src
.net
->equals(current
->src
.net
, policy
->src
.net
) &&
240 current
->dst
.net
->equals(current
->dst
.net
, policy
->dst
.net
);
244 * compare the given kernel index with that of a policy
246 static inline bool policy_entry_match_byindex(policy_entry_t
*current
, u_int32_t
*index
)
248 return current
->index
== *index
;
251 typedef struct pfkey_msg_t pfkey_msg_t
;
256 * PF_KEY message base
258 struct sadb_msg
*msg
;
262 * PF_KEY message extensions
265 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
267 struct sadb_ext
*reserved
; /* SADB_EXT_RESERVED */
268 struct sadb_sa
*sa
; /* SADB_EXT_SA */
269 struct sadb_lifetime
*lft_current
; /* SADB_EXT_LIFETIME_CURRENT */
270 struct sadb_lifetime
*lft_hard
; /* SADB_EXT_LIFETIME_HARD */
271 struct sadb_lifetime
*lft_soft
; /* SADB_EXT_LIFETIME_SOFT */
272 struct sadb_address
*src
; /* SADB_EXT_ADDRESS_SRC */
273 struct sadb_address
*dst
; /* SADB_EXT_ADDRESS_DST */
274 struct sadb_address
*proxy
; /* SADB_EXT_ADDRESS_PROXY */
275 struct sadb_key
*key_auth
; /* SADB_EXT_KEY_AUTH */
276 struct sadb_key
*key_encr
; /* SADB_EXT_KEY_ENCRYPT */
277 struct sadb_ident
*id_src
; /* SADB_EXT_IDENTITY_SRC */
278 struct sadb_ident
*id_dst
; /* SADB_EXT_IDENTITY_DST */
279 struct sadb_sens
*sensitivity
; /* SADB_EXT_SENSITIVITY */
280 struct sadb_prop
*proposal
; /* SADB_EXT_PROPOSAL */
281 struct sadb_supported
*supported_auth
; /* SADB_EXT_SUPPORTED_AUTH */
282 struct sadb_supported
*supported_encr
; /* SADB_EXT_SUPPORTED_ENCRYPT */
283 struct sadb_spirange
*spirange
; /* SADB_EXT_SPIRANGE */
284 struct sadb_x_kmprivate
*x_kmprivate
; /* SADB_X_EXT_KMPRIVATE */
285 struct sadb_x_policy
*x_policy
; /* SADB_X_EXT_POLICY */
286 struct sadb_x_sa2
*x_sa2
; /* SADB_X_EXT_SA2 */
287 struct sadb_x_nat_t_type
*x_natt_type
; /* SADB_X_EXT_NAT_T_TYPE */
288 struct sadb_x_nat_t_port
*x_natt_sport
; /* SADB_X_EXT_NAT_T_SPORT */
289 struct sadb_x_nat_t_port
*x_natt_dport
; /* SADB_X_EXT_NAT_T_DPORT */
290 struct sadb_address
*x_natt_oa
; /* SADB_X_EXT_NAT_T_OA */
291 struct sadb_x_sec_ctx
*x_sec_ctx
; /* SADB_X_EXT_SEC_CTX */
292 } __attribute__((__packed__
));
297 * convert a IKEv2 specific protocol identifier to the PF_KEY sa type
299 static u_int8_t
proto_ike2satype(protocol_id_t proto
)
304 return SADB_SATYPE_ESP
;
306 return SADB_SATYPE_AH
;
308 return SADB_X_SATYPE_IPCOMP
;
315 * convert a PF_KEY sa type to a IKEv2 specific protocol identifier
317 static protocol_id_t
proto_satype2ike(u_int8_t proto
)
321 case SADB_SATYPE_ESP
:
325 case SADB_X_SATYPE_IPCOMP
:
333 * convert a IKEv2 specific protocol identifier to the IP protocol identifier
335 static u_int8_t
proto_ike2ip(protocol_id_t proto
)
349 * convert the general ipsec mode to the one defined in ipsec.h
351 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
356 return IPSEC_MODE_TRANSPORT
;
358 return IPSEC_MODE_TUNNEL
;
360 return IPSEC_MODE_BEET
;
367 * convert the general policy direction to the one defined in ipsec.h
369 static u_int8_t
dir2kernel(policy_dir_t dir
)
374 return IPSEC_DIR_INBOUND
;
376 return IPSEC_DIR_OUTBOUND
;
378 return IPSEC_DIR_FWD
;
384 typedef struct kernel_algorithm_t kernel_algorithm_t
;
387 * Mapping from the algorithms defined in IKEv2 to
388 * kernel level algorithm identifiers and their key length
390 struct kernel_algorithm_t
{
392 * Identifier specified in IKEv2
397 * Identifier as defined in pfkeyv2.h
402 * Key length in bits, if fixed size
407 #define END_OF_LIST -1
410 * Algorithms for encryption
412 static kernel_algorithm_t encryption_algs
[] = {
413 /* {ENCR_DES_IV64, 0, 0}, */
414 {ENCR_DES
, SADB_EALG_DESCBC
, 64},
415 {ENCR_3DES
, SADB_EALG_3DESCBC
, 192},
416 /* {ENCR_RC5, 0, 0}, */
417 /* {ENCR_IDEA, 0, 0}, */
418 {ENCR_CAST
, SADB_X_EALG_CASTCBC
, 0},
419 {ENCR_BLOWFISH
, SADB_X_EALG_BLOWFISHCBC
, 0},
420 /* {ENCR_3IDEA, 0, 0}, */
421 /* {ENCR_DES_IV32, 0, 0}, */
422 {ENCR_NULL
, SADB_EALG_NULL
, 0},
423 {ENCR_AES_CBC
, SADB_X_EALG_AESCBC
, 0},
424 /* {ENCR_AES_CTR, 0, 0}, */
425 /* {ENCR_AES_CCM_ICV8, 0, 64}, */ /* key_size = ICV size */
426 /* {ENCR_AES_CCM_ICV12, 0, 96}, */ /* key_size = ICV size */
427 /* {ENCR_AES_CCM_ICV16, 0, 128},*/ /* key_size = ICV size */
428 /* {ENCR_AES_GCM_ICV8, 0, 64}, */ /* key_size = ICV size */
429 /* {ENCR_AES_GCM_ICV12, 0, 96}, */ /* key_size = ICV size */
430 /* {ENCR_AES_GCM_ICV16, 0, 128},*/ /* key_size = ICV size */
435 * Algorithms for integrity protection
437 static kernel_algorithm_t integrity_algs
[] = {
438 {AUTH_HMAC_MD5_96
, SADB_AALG_MD5HMAC
, 128},
439 {AUTH_HMAC_SHA1_96
, SADB_AALG_SHA1HMAC
, 160},
440 {AUTH_HMAC_SHA2_256_128
, SADB_X_AALG_SHA2_256HMAC
, 256},
441 {AUTH_HMAC_SHA2_384_192
, SADB_X_AALG_SHA2_384HMAC
, 384},
442 {AUTH_HMAC_SHA2_512_256
, SADB_X_AALG_SHA2_512HMAC
, 512},
443 /* {AUTH_DES_MAC, 0, 0}, */
444 /* {AUTH_KPDK_MD5, 0, 0}, */
445 {AUTH_AES_XCBC_96
, SADB_X_AALG_AES_XCBC_MAC
, 128},
450 * Algorithms for IPComp
452 static kernel_algorithm_t compression_algs
[] = {
453 /* {IPCOMP_OUI, 0, 0}, */
454 {IPCOMP_DEFLATE
, SADB_X_CALG_DEFLATE
, 0},
455 {IPCOMP_LZS
, SADB_X_CALG_LZS
, 0},
456 {IPCOMP_LZJH
, SADB_X_CALG_LZJH
, 0},
461 * Look up a kernel algorithm ID and its key size
463 static int lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
464 u_int16_t ikev2_algo
, u_int16_t
*key_size
)
466 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
468 if (ikev2_algo
== kernel_algo
->ikev2_id
)
470 /* match, evaluate key length */
471 if (key_size
&& *key_size
== 0)
472 { /* update key size if not set */
473 *key_size
= kernel_algo
->key_size
;
475 return kernel_algo
->kernel_id
;
483 * add a host behind a sadb_address extension
485 static void host2ext(host_t
*host
, struct sadb_address
*ext
)
487 sockaddr_t
*host_addr
= host
->get_sockaddr(host
);
488 socklen_t
*len
= host
->get_sockaddr_len(host
);
489 memcpy((char*)(ext
+ 1), host_addr
, *len
);
490 ext
->sadb_address_len
= PFKEY_LEN(sizeof(*ext
) + *len
);
494 * add udp encap extensions to a sadb_msg
496 static void add_encap_ext(struct sadb_msg
*msg
, host_t
*src
, host_t
*dst
)
498 struct sadb_x_nat_t_type
* nat_type
;
499 struct sadb_x_nat_t_port
* nat_port
;
501 nat_type
= (struct sadb_x_nat_t_type
*)PFKEY_EXT_ADD_NEXT(msg
);
502 nat_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
503 nat_type
->sadb_x_nat_t_type_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_type
));
504 nat_type
->sadb_x_nat_t_type_type
= UDP_ENCAP_ESPINUDP
;
505 PFKEY_EXT_ADD(msg
, nat_type
);
507 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
508 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
509 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_port
));
510 nat_port
->sadb_x_nat_t_port_port
= htons(src
->get_port(src
));
511 PFKEY_EXT_ADD(msg
, nat_port
);
513 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
514 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
515 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_port
));
516 nat_port
->sadb_x_nat_t_port_port
= htons(dst
->get_port(dst
));
517 PFKEY_EXT_ADD(msg
, nat_port
);
521 * Parses a pfkey message received from the kernel
523 static status_t
parse_pfkey_message(struct sadb_msg
*msg
, pfkey_msg_t
*out
)
525 struct sadb_ext
* ext
;
528 memset(out
, 0, sizeof(pfkey_msg_t
));
531 len
= msg
->sadb_msg_len
;
532 len
-= PFKEY_LEN(sizeof(struct sadb_msg
));
534 ext
= (struct sadb_ext
*)(((char*)msg
) + sizeof(struct sadb_msg
));
536 while (len
>= PFKEY_LEN(sizeof(struct sadb_ext
)))
538 if (ext
->sadb_ext_len
< PFKEY_LEN(sizeof(struct sadb_ext
)) ||
539 ext
->sadb_ext_len
> len
)
541 DBG1(DBG_KNL
, "length of PF_KEY extension (%d) is invalid", ext
->sadb_ext_type
);
545 if ((ext
->sadb_ext_type
> SADB_EXT_MAX
) || (!ext
->sadb_ext_type
))
547 DBG1(DBG_KNL
, "type of PF_KEY extension (%d) is invalid", ext
->sadb_ext_type
);
551 if (out
->ext
[ext
->sadb_ext_type
])
553 DBG1(DBG_KNL
, "duplicate PF_KEY extension of type (%d)", ext
->sadb_ext_type
);
557 out
->ext
[ext
->sadb_ext_type
] = ext
;
558 ext
= PFKEY_EXT_NEXT_LEN(ext
, len
);
563 DBG1(DBG_KNL
, "PF_KEY message length is invalid");
571 * Send a message to a specific PF_KEY socket and handle the response.
573 static status_t
pfkey_send_socket(private_kernel_pfkey_ipsec_t
*this, int socket
,
574 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
576 unsigned char buf
[PFKEY_BUFFER_SIZE
];
577 struct sadb_msg
*msg
;
580 pthread_mutex_lock(&this->mutex_pfkey
);
582 in
->sadb_msg_seq
= ++this->seq
;
583 in
->sadb_msg_pid
= getpid();
585 in_len
= PFKEY_USER_LEN(in
->sadb_msg_len
);
589 len
= send(socket
, in
, in_len
, 0);
595 /* interrupted, try again */
598 pthread_mutex_unlock(&this->mutex_pfkey
);
599 DBG1(DBG_KNL
, "error sending to PF_KEY socket: %s", strerror(errno
));
607 msg
= (struct sadb_msg
*)buf
;
609 len
= recv(socket
, buf
, sizeof(buf
), 0);
615 DBG1(DBG_KNL
, "got interrupted");
616 /* interrupted, try again */
619 DBG1(DBG_KNL
, "error reading from PF_KEY socket: %s", strerror(errno
));
620 pthread_mutex_unlock(&this->mutex_pfkey
);
623 if (len
< sizeof(struct sadb_msg
) ||
624 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
626 DBG1(DBG_KNL
, "received corrupted PF_KEY message");
627 pthread_mutex_unlock(&this->mutex_pfkey
);
630 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
632 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY message");
633 pthread_mutex_unlock(&this->mutex_pfkey
);
636 if (msg
->sadb_msg_pid
!= in
->sadb_msg_pid
)
638 DBG2(DBG_KNL
, "received PF_KEY message is not intended for us");
641 if (msg
->sadb_msg_seq
!= this->seq
)
643 DBG1(DBG_KNL
, "received PF_KEY message with invalid sequence number, "
644 "was %d expected %d", msg
->sadb_msg_seq
, this->seq
);
645 if (msg
->sadb_msg_seq
< this->seq
)
649 pthread_mutex_unlock(&this->mutex_pfkey
);
652 if (msg
->sadb_msg_type
!= in
->sadb_msg_type
)
654 DBG2(DBG_KNL
, "received PF_KEY message of wrong type, "
655 "was %d expected %d, ignoring",
656 msg
->sadb_msg_type
, in
->sadb_msg_type
);
662 *out
= (struct sadb_msg
*)malloc(len
);
663 memcpy(*out
, buf
, len
);
665 pthread_mutex_unlock(&this->mutex_pfkey
);
671 * Send a message to the default PF_KEY socket and handle the response.
673 static status_t
pfkey_send(private_kernel_pfkey_ipsec_t
*this,
674 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
676 return pfkey_send_socket(this, this->socket
, in
, out
, out_len
);
680 * Process a SADB_ACQUIRE message from the kernel
682 static void process_acquire(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
684 pfkey_msg_t response
;
685 u_int32_t index
, reqid
;
686 policy_entry_t
*policy
;
689 switch (msg
->sadb_msg_satype
)
691 case SADB_SATYPE_UNSPEC
:
692 case SADB_SATYPE_ESP
:
696 /* acquire for AH/ESP only */
700 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
702 DBG1(DBG_KNL
, "parsing SADB_ACQUIRE from kernel failed");
706 index
= response
.x_policy
->sadb_x_policy_id
;
707 DBG2(DBG_KNL
, "received an SADB_ACQUIRE, %d", index
);
708 pthread_mutex_lock(&this->mutex
);
709 if (this->policies
->find_first(this->policies
,
710 (linked_list_match_t
)policy_entry_match_byindex
, (void**)&policy
, &index
) != SUCCESS
)
712 DBG1(DBG_KNL
, "received an SADB_ACQUIRE, but found no matching policy");
713 pthread_mutex_unlock(&this->mutex
);
716 reqid
= policy
->reqid
;
717 DBG2(DBG_KNL
, "received an SADB_ACQUIRE, %d", reqid
);
718 pthread_mutex_unlock(&this->mutex
);
720 DBG2(DBG_KNL
, "received an SADB_ACQUIRE");
721 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid {%d}", reqid
);
722 job
= (job_t
*)acquire_job_create(reqid
);
723 charon
->processor
->queue_job(charon
->processor
, job
);
727 * Process a SADB_EXPIRE message from the kernel
729 static void process_expire(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
731 pfkey_msg_t response
;
732 protocol_id_t protocol
;
733 u_int32_t spi
, reqid
;
737 DBG2(DBG_KNL
, "received an SADB_EXPIRE");
739 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
741 DBG1(DBG_KNL
, "parsing SADB_EXPIRE from kernel failed");
745 protocol
= proto_satype2ike(msg
->sadb_msg_satype
);
746 spi
= response
.sa
->sadb_sa_spi
;
747 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
748 hard
= response
.lft_hard
!= NULL
;
750 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
752 DBG2(DBG_KNL
, "ignoring SADB_EXPIRE for SA with SPI %.8x and reqid {%d} "
753 "which is not a CHILD_SA", ntohl(spi
), reqid
);
757 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
758 hard ?
"delete" : "rekey", protocol_id_names
,
759 protocol
, ntohl(spi
), reqid
);
762 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
766 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
768 charon
->processor
->queue_job(charon
->processor
, job
);
772 * Process a SADB_X_NAT_T_NEW_MAPPING message from the kernel
774 static void process_mapping(private_kernel_pfkey_ipsec_t
*this, struct sadb_msg
* msg
)
776 pfkey_msg_t response
;
777 u_int32_t spi
, reqid
;
781 DBG2(DBG_KNL
, "received an SADB_X_NAT_T_NEW_MAPPING");
783 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
785 DBG1(DBG_KNL
, "parsing SADB_X_NAT_T_NEW_MAPPING from kernel failed");
791 DBG1(DBG_KNL
, "received SADB_X_NAT_T_NEW_MAPPING is missing required information");
795 spi
= response
.sa
->sadb_sa_spi
;
796 reqid
= response
.x_sa2
->sadb_x_sa2_reqid
;
798 if (proto_satype2ike(msg
->sadb_msg_satype
) == PROTO_ESP
)
800 sockaddr_t
*sa
= (sockaddr_t
*)(response
.dst
+ 1);
801 switch (sa
->sa_family
)
805 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
806 sin
->sin_port
= response
.x_natt_dport
->sadb_x_nat_t_port_port
;
810 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
811 sin6
->sin6_port
= response
.x_natt_dport
->sadb_x_nat_t_port_port
;
816 host
= host_create_from_sockaddr(sa
);
819 DBG1(DBG_KNL
, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
820 "reqid {%d} changed, queuing update job", ntohl(spi
), reqid
);
821 job
= (job_t
*)update_sa_job_create(reqid
, host
);
822 charon
->processor
->queue_job(charon
->processor
, job
);
828 * Receives events from kernel
830 static job_requeue_t
receive_events(private_kernel_pfkey_ipsec_t
*this)
832 unsigned char buf
[PFKEY_BUFFER_SIZE
];
833 struct sadb_msg
*msg
= (struct sadb_msg
*)buf
;
836 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
837 len
= recv(this->socket_events
, buf
, sizeof(buf
), 0);
838 pthread_setcancelstate(oldstate
, NULL
);
845 /* interrupted, try again */
846 return JOB_REQUEUE_DIRECT
;
848 /* no data ready, select again */
849 return JOB_REQUEUE_DIRECT
;
851 DBG1(DBG_KNL
, "unable to receive from PF_KEY event socket");
853 return JOB_REQUEUE_FAIR
;
857 if (len
< sizeof(struct sadb_msg
) ||
858 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
860 DBG2(DBG_KNL
, "received corrupted PF_KEY message");
861 return JOB_REQUEUE_DIRECT
;
863 if (msg
->sadb_msg_pid
!= 0)
864 { /* not from kernel. not interested, try another one */
865 return JOB_REQUEUE_DIRECT
;
867 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
869 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY message");
870 return JOB_REQUEUE_DIRECT
;
873 switch (msg
->sadb_msg_type
)
876 process_acquire(this, msg
);
879 process_expire(this, msg
);
881 case SADB_X_NAT_T_NEW_MAPPING
:
882 process_mapping(this, msg
);
888 return JOB_REQUEUE_DIRECT
;
892 * Implementation of kernel_interface_t.get_spi.
894 static status_t
get_spi(private_kernel_pfkey_ipsec_t
*this,
895 host_t
*src
, host_t
*dst
,
896 protocol_id_t protocol
, u_int32_t reqid
,
899 unsigned char request
[PFKEY_BUFFER_SIZE
];
900 struct sadb_msg
*msg
, *out
;
902 struct sadb_x_sa2
*sa2
;
903 struct sadb_address
*addr
;
904 struct sadb_spirange
*range
;
905 pfkey_msg_t response
;
906 u_int32_t received_spi
= 0;
909 memset(&request
, 0, sizeof(request
));
911 msg
= (struct sadb_msg
*)request
;
912 msg
->sadb_msg_version
= PF_KEY_V2
;
913 msg
->sadb_msg_type
= SADB_GETSPI
;
914 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
915 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
917 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
918 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
919 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
920 sa2
->sadb_x_sa2_reqid
= reqid
;
921 PFKEY_EXT_ADD(msg
, sa2
);
923 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
924 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
926 PFKEY_EXT_ADD(msg
, addr
);
928 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
929 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
931 PFKEY_EXT_ADD(msg
, addr
);
933 range
= (struct sadb_spirange
*)PFKEY_EXT_ADD_NEXT(msg
);
934 range
->sadb_spirange_exttype
= SADB_EXT_SPIRANGE
;
935 range
->sadb_spirange_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
936 range
->sadb_spirange_min
= 0xc0000000;
937 range
->sadb_spirange_max
= 0xcFFFFFFF;
938 PFKEY_EXT_ADD(msg
, range
);
940 if (pfkey_send(this, msg
, &out
, &len
) == SUCCESS
)
942 if (out
->sadb_msg_errno
)
944 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
945 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
947 else if (parse_pfkey_message(out
, &response
) == SUCCESS
)
949 received_spi
= response
.sa
->sadb_sa_spi
;
954 if (received_spi
== 0)
964 * Implementation of kernel_interface_t.get_cpi.
966 static status_t
get_cpi(private_kernel_pfkey_ipsec_t
*this,
967 host_t
*src
, host_t
*dst
,
968 u_int32_t reqid
, u_int16_t
*cpi
)
974 * Implementation of kernel_interface_t.add_sa.
976 static status_t
add_sa(private_kernel_pfkey_ipsec_t
*this,
977 host_t
*src
, host_t
*dst
, u_int32_t spi
,
978 protocol_id_t protocol
, u_int32_t reqid
,
979 u_int64_t expire_soft
, u_int64_t expire_hard
,
980 u_int16_t enc_alg
, u_int16_t enc_size
,
981 u_int16_t int_alg
, u_int16_t int_size
,
982 prf_plus_t
*prf_plus
, ipsec_mode_t mode
,
983 u_int16_t ipcomp
, bool encap
,
986 unsigned char request
[PFKEY_BUFFER_SIZE
];
987 struct sadb_msg
*msg
, *out
;
989 struct sadb_x_sa2
*sa2
;
990 struct sadb_address
*addr
;
991 struct sadb_lifetime
*lft
;
992 struct sadb_key
*key
;
995 memset(&request
, 0, sizeof(request
));
997 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%d}", ntohl(spi
), reqid
);
999 msg
= (struct sadb_msg
*)request
;
1000 msg
->sadb_msg_version
= PF_KEY_V2
;
1001 msg
->sadb_msg_type
= replace ? SADB_UPDATE
: SADB_ADD
;
1002 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
1003 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1005 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1006 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1007 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1008 sa
->sadb_sa_spi
= spi
;
1009 sa
->sadb_sa_replay
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
1010 sa
->sadb_sa_auth
= lookup_algorithm(integrity_algs
,
1011 int_alg
, &int_size
);
1012 sa
->sadb_sa_encrypt
= lookup_algorithm(encryption_algs
,
1013 enc_alg
, &enc_size
);
1014 PFKEY_EXT_ADD(msg
, sa
);
1016 sa2
= (struct sadb_x_sa2
*)PFKEY_EXT_ADD_NEXT(msg
);
1017 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
1018 sa2
->sadb_x_sa2_len
= PFKEY_LEN(sizeof(struct sadb_spirange
));
1019 sa2
->sadb_x_sa2_mode
= mode2kernel(mode
);
1020 sa2
->sadb_x_sa2_reqid
= reqid
;
1021 PFKEY_EXT_ADD(msg
, sa2
);
1023 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1024 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1025 host2ext(src
, addr
);
1026 PFKEY_EXT_ADD(msg
, addr
);
1028 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1029 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1030 host2ext(dst
, addr
);
1031 PFKEY_EXT_ADD(msg
, addr
);
1033 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1034 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
1035 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1036 lft
->sadb_lifetime_addtime
= expire_soft
;
1037 PFKEY_EXT_ADD(msg
, lft
);
1039 lft
= (struct sadb_lifetime
*)PFKEY_EXT_ADD_NEXT(msg
);
1040 lft
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1041 lft
->sadb_lifetime_len
= PFKEY_LEN(sizeof(struct sadb_lifetime
));
1042 lft
->sadb_lifetime_addtime
= expire_hard
;
1043 PFKEY_EXT_ADD(msg
, lft
);
1045 if (enc_alg
!= ENCR_UNDEFINED
)
1047 if (!sa
->sadb_sa_encrypt
)
1049 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1050 encryption_algorithm_names
, enc_alg
);
1053 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1054 encryption_algorithm_names
, enc_alg
, enc_size
);
1056 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1057 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
1058 key
->sadb_key_bits
= enc_size
;
1059 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + enc_size
/ 8);
1060 prf_plus
->get_bytes(prf_plus
, enc_size
/ 8, (void*)(key
+ 1));
1062 PFKEY_EXT_ADD(msg
, key
);
1065 if (int_alg
!= AUTH_UNDEFINED
)
1067 if (!sa
->sadb_sa_auth
)
1069 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1070 integrity_algorithm_names
, int_alg
);
1073 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1074 integrity_algorithm_names
, int_alg
, int_size
);
1076 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1077 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
1078 key
->sadb_key_bits
= int_size
;
1079 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + int_size
/ 8);
1080 prf_plus
->get_bytes(prf_plus
, int_size
/ 8, (void*)(key
+ 1));
1082 PFKEY_EXT_ADD(msg
, key
);
1085 if (ipcomp
!= IPCOMP_NONE
)
1092 add_encap_ext(msg
, src
, dst
);
1095 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1097 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1100 else if (out
->sadb_msg_errno
)
1102 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x: %s (%d)",
1103 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1113 * Implementation of kernel_interface_t.update_sa.
1115 static status_t
update_sa(private_kernel_pfkey_ipsec_t
*this,
1116 u_int32_t spi
, protocol_id_t protocol
,
1117 host_t
*src
, host_t
*dst
,
1118 host_t
*new_src
, host_t
*new_dst
, bool encap
)
1120 unsigned char request
[PFKEY_BUFFER_SIZE
];
1121 struct sadb_msg
*msg
, *out
;
1123 struct sadb_address
*addr
;
1124 pfkey_msg_t response
;
1127 memset(&request
, 0, sizeof(request
));
1129 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1131 msg
= (struct sadb_msg
*)request
;
1132 msg
->sadb_msg_version
= PF_KEY_V2
;
1133 msg
->sadb_msg_type
= SADB_GET
;
1134 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
1135 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1137 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1138 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1139 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1140 sa
->sadb_sa_spi
= spi
;
1141 PFKEY_EXT_ADD(msg
, sa
);
1143 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1144 * it is not used for anything, so we just send dst twice */
1145 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1146 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1147 host2ext(dst
, addr
);
1148 PFKEY_EXT_ADD(msg
, addr
);
1150 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1151 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1152 host2ext(dst
, addr
);
1153 PFKEY_EXT_ADD(msg
, addr
);
1155 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1157 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x",
1161 else if (out
->sadb_msg_errno
)
1163 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: %s (%d)",
1164 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1168 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1170 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x: parsing response "
1171 "from kernel failed", ntohl(spi
));
1176 /* delete the old SA */
1177 if (this->public.interface
.del_sa(&this->public.interface
, dst
, spi
, protocol
) != SUCCESS
)
1179 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x", ntohl(spi
));
1184 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1185 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1187 memset(&request
, 0, sizeof(request
));
1189 msg
= (struct sadb_msg
*)request
;
1190 msg
->sadb_msg_version
= PF_KEY_V2
;
1191 msg
->sadb_msg_type
= SADB_ADD
;
1192 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
1193 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1195 PFKEY_EXT_COPY(msg
, response
.sa
);
1196 PFKEY_EXT_COPY(msg
, response
.x_sa2
);
1198 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1199 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1200 host2ext(new_src
, addr
);
1201 PFKEY_EXT_ADD(msg
, addr
);
1203 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1204 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1205 host2ext(new_dst
, addr
);
1206 PFKEY_EXT_ADD(msg
, addr
);
1208 PFKEY_EXT_COPY(msg
, response
.lft_soft
);
1209 PFKEY_EXT_COPY(msg
, response
.lft_hard
);
1211 if (response
.key_encr
)
1213 PFKEY_EXT_COPY(msg
, response
.key_encr
);
1216 if (response
.key_auth
)
1218 PFKEY_EXT_COPY(msg
, response
.key_auth
);
1223 add_encap_ext(msg
, new_src
, new_dst
);
1228 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1230 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1233 else if (out
->sadb_msg_errno
)
1235 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: %s (%d)",
1236 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1246 * Implementation of kernel_interface_t.del_sa.
1248 static status_t
del_sa(private_kernel_pfkey_ipsec_t
*this, host_t
*dst
,
1249 u_int32_t spi
, protocol_id_t protocol
)
1251 unsigned char request
[PFKEY_BUFFER_SIZE
];
1252 struct sadb_msg
*msg
, *out
;
1254 struct sadb_address
*addr
;
1257 memset(&request
, 0, sizeof(request
));
1259 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1261 msg
= (struct sadb_msg
*)request
;
1262 msg
->sadb_msg_version
= PF_KEY_V2
;
1263 msg
->sadb_msg_type
= SADB_DELETE
;
1264 msg
->sadb_msg_satype
= proto_ike2satype(protocol
);
1265 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1267 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1268 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1269 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1270 sa
->sadb_sa_spi
= spi
;
1271 PFKEY_EXT_ADD(msg
, sa
);
1273 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1274 * it is not used for anything, so we just send dst twice */
1275 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1276 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1277 host2ext(dst
, addr
);
1278 PFKEY_EXT_ADD(msg
, addr
);
1280 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1281 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1282 host2ext(dst
, addr
);
1283 PFKEY_EXT_ADD(msg
, addr
);
1285 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1287 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1290 else if (out
->sadb_msg_errno
)
1292 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1293 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1298 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1304 * Implementation of kernel_interface_t.add_policy.
1306 static status_t
add_policy(private_kernel_pfkey_ipsec_t
*this,
1307 host_t
*src
, host_t
*dst
,
1308 traffic_selector_t
*src_ts
,
1309 traffic_selector_t
*dst_ts
,
1310 policy_dir_t direction
, protocol_id_t protocol
,
1311 u_int32_t reqid
, bool high_prio
, ipsec_mode_t mode
,
1314 unsigned char request
[PFKEY_BUFFER_SIZE
];
1315 struct sadb_msg
*msg
, *out
;
1316 struct sadb_x_policy
*pol
;
1317 struct sadb_address
*addr
;
1318 struct sadb_x_ipsecrequest
*req
;
1319 policy_entry_t
*policy
, *found
= NULL
;
1320 pfkey_msg_t response
;
1323 /* create a policy */
1324 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, reqid
);
1326 /* find a matching policy */
1327 pthread_mutex_lock(&this->mutex
);
1328 if (this->policies
->find_first(this->policies
,
1329 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) == SUCCESS
)
1331 /* use existing policy */
1333 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
1334 "refcount", src_ts
, dst_ts
,
1335 policy_dir_names
, direction
);
1336 policy_entry_destroy(policy
);
1341 /* apply the new one, if we have no such policy */
1342 this->policies
->insert_last(this->policies
, policy
);
1343 policy
->refcount
= 1;
1346 memset(&request
, 0, sizeof(request
));
1348 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
1349 policy_dir_names
, direction
);
1351 msg
= (struct sadb_msg
*)request
;
1352 msg
->sadb_msg_version
= PF_KEY_V2
;
1353 msg
->sadb_msg_type
= found ? SADB_X_SPDUPDATE
: SADB_X_SPDADD
;
1354 msg
->sadb_msg_satype
= 0;
1355 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1357 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1358 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1359 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1360 pol
->sadb_x_policy_id
= 0;
1361 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1362 /* calculate priority based on source selector size, small size = high prio */
1363 pol
->sadb_x_policy_priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
1364 pol
->sadb_x_policy_priority
-= policy
->src
.mask
* 10;
1365 pol
->sadb_x_policy_priority
-= policy
->src
.proto
!= IPSEC_PROTO_ANY ?
2 : 0;
1366 pol
->sadb_x_policy_priority
-= policy
->src
.net
->get_port(policy
->src
.net
) ?
1 : 0;
1367 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1369 /* one or more sadb_x_ipsecrequest extensions are added to the sadb_x_policy extension */
1370 req
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
1371 req
->sadb_x_ipsecrequest_proto
= proto_ike2ip(protocol
);
1372 /* !!! the length of this struct MUST be in octets instead of 64 bit words */
1373 req
->sadb_x_ipsecrequest_len
= sizeof(struct sadb_x_ipsecrequest
);
1374 req
->sadb_x_ipsecrequest_mode
= mode2kernel(mode
);
1375 req
->sadb_x_ipsecrequest_reqid
= reqid
;
1376 req
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
1377 if (mode
== MODE_TUNNEL
)
1381 sa
= src
->get_sockaddr(src
);
1382 sl
= *src
->get_sockaddr_len(src
);
1383 memcpy(req
+ 1, sa
, sl
);
1384 sa
= dst
->get_sockaddr(dst
);
1385 memcpy((u_int8_t
*)(req
+ 1) + sl
, sa
, sl
);
1386 req
->sadb_x_ipsecrequest_len
+= sl
* 2;
1389 pol
->sadb_x_policy_len
+= PFKEY_LEN(req
->sadb_x_ipsecrequest_len
);
1390 PFKEY_EXT_ADD(msg
, pol
);
1392 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1393 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1394 addr
->sadb_address_proto
= policy
->src
.proto
;
1395 addr
->sadb_address_prefixlen
= policy
->src
.mask
;
1396 host2ext(policy
->src
.net
, addr
);
1397 PFKEY_EXT_ADD(msg
, addr
);
1399 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1400 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1401 addr
->sadb_address_proto
= policy
->dst
.proto
;
1402 addr
->sadb_address_prefixlen
= policy
->dst
.mask
;
1403 host2ext(policy
->dst
.net
, addr
);
1404 PFKEY_EXT_ADD(msg
, addr
);
1406 pthread_mutex_unlock(&this->mutex
);
1408 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1410 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
1411 policy_dir_names
, direction
);
1414 else if (out
->sadb_msg_errno
)
1416 DBG1(DBG_KNL
, "unable to add policy %R === %R %N: %s (%d)", src_ts
, dst_ts
,
1417 policy_dir_names
, direction
,
1418 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1422 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1424 DBG1(DBG_KNL
, "unable to add policy %R === %R %N: parsing response "
1425 "from kernel failed", src_ts
, dst_ts
, policy_dir_names
, direction
);
1430 pthread_mutex_lock(&this->mutex
);
1432 /* we try to find the policy again and update the kernel index */
1433 if (this->policies
->find_last(this->policies
, NULL
, (void**)&policy
) != SUCCESS
)
1435 DBG2(DBG_KNL
, "unable to update index, the policy %R === %R %N is "
1436 "already gone, ignoring", src_ts
, dst_ts
, policy_dir_names
, direction
);
1437 pthread_mutex_unlock(&this->mutex
);
1441 policy
->index
= response
.x_policy
->sadb_x_policy_id
;
1444 /* install a route, if:
1445 * - we are NOT updating a policy
1446 * - this is a forward policy (to just get one for each child)
1447 * - we are in tunnel mode
1448 * - we are not using IPv6 (does not work correctly yet!)
1449 * - routing is not disabled via strongswan.conf
1451 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1452 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
1453 this->install_routes
)
1455 route_entry_t
*route
= malloc_thing(route_entry_t
);
1457 if (charon
->kernel_interface
->get_address_by_ts(charon
->kernel_interface
,
1458 dst_ts
, &route
->src_ip
) == SUCCESS
)
1460 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1461 route
->gateway
= charon
->kernel_interface
->get_nexthop(
1462 charon
->kernel_interface
, src
);
1463 route
->if_name
= charon
->kernel_interface
->get_interface(
1464 charon
->kernel_interface
, dst
);
1465 route
->dst_net
= chunk_clone(policy
->src
.net
->get_address(policy
->src
.net
));
1466 route
->prefixlen
= policy
->src
.mask
;
1468 switch (charon
->kernel_interface
->add_route(charon
->kernel_interface
,
1469 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1470 route
->src_ip
, route
->if_name
))
1473 DBG1(DBG_KNL
, "unable to install source route for %H",
1477 /* route exists, do not uninstall */
1478 route_entry_destroy(route
);
1481 /* cache the installed route */
1482 policy
->route
= route
;
1492 pthread_mutex_unlock(&this->mutex
);
1498 * Implementation of kernel_interface_t.query_policy.
1500 static status_t
query_policy(private_kernel_pfkey_ipsec_t
*this,
1501 traffic_selector_t
*src_ts
,
1502 traffic_selector_t
*dst_ts
,
1503 policy_dir_t direction
, u_int32_t
*use_time
)
1505 unsigned char request
[PFKEY_BUFFER_SIZE
];
1506 struct sadb_msg
*msg
, *out
;
1507 struct sadb_x_policy
*pol
;
1508 struct sadb_address
*addr
;
1509 policy_entry_t
*policy
, *found
= NULL
;
1510 pfkey_msg_t response
;
1513 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
1514 policy_dir_names
, direction
);
1516 /* create a policy */
1517 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, 0);
1519 /* find a matching policy */
1520 pthread_mutex_lock(&this->mutex
);
1521 if (this->policies
->find_first(this->policies
,
1522 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) != SUCCESS
)
1524 DBG1(DBG_KNL
, "querying policy %R === %R %N failed, not found", src_ts
,
1525 dst_ts
, policy_dir_names
, direction
);
1526 policy_entry_destroy(policy
);
1527 pthread_mutex_unlock(&this->mutex
);
1530 policy_entry_destroy(policy
);
1533 memset(&request
, 0, sizeof(request
));
1535 msg
= (struct sadb_msg
*)request
;
1536 msg
->sadb_msg_version
= PF_KEY_V2
;
1537 msg
->sadb_msg_type
= SADB_X_SPDGET
;
1538 msg
->sadb_msg_satype
= 0;
1539 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1541 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1542 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1543 pol
->sadb_x_policy_id
= policy
->index
;
1544 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1545 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1546 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1547 PFKEY_EXT_ADD(msg
, pol
);
1549 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1550 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1551 addr
->sadb_address_proto
= policy
->src
.proto
;
1552 addr
->sadb_address_prefixlen
= policy
->src
.mask
;
1553 host2ext(policy
->src
.net
, addr
);
1554 PFKEY_EXT_ADD(msg
, addr
);
1556 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1557 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1558 addr
->sadb_address_proto
= policy
->dst
.proto
;
1559 addr
->sadb_address_prefixlen
= policy
->dst
.mask
;
1560 host2ext(policy
->dst
.net
, addr
);
1561 PFKEY_EXT_ADD(msg
, addr
);
1563 pthread_mutex_unlock(&this->mutex
);
1565 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1567 DBG1(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
1568 policy_dir_names
, direction
);
1571 else if (out
->sadb_msg_errno
)
1573 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: %s (%d)", src_ts
,
1574 dst_ts
, policy_dir_names
, direction
,
1575 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1579 else if (parse_pfkey_message(out
, &response
) != SUCCESS
)
1581 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: parsing response "
1582 "from kernel failed", src_ts
, dst_ts
, policy_dir_names
, direction
);
1587 *use_time
= response
.lft_current
->sadb_lifetime_usetime
;
1595 * Implementation of kernel_interface_t.del_policy.
1597 static status_t
del_policy(private_kernel_pfkey_ipsec_t
*this,
1598 traffic_selector_t
*src_ts
,
1599 traffic_selector_t
*dst_ts
,
1600 policy_dir_t direction
)
1602 unsigned char request
[PFKEY_BUFFER_SIZE
];
1603 struct sadb_msg
*msg
, *out
;
1604 struct sadb_x_policy
*pol
;
1605 struct sadb_address
*addr
;
1606 policy_entry_t
*policy
, *found
= NULL
;
1607 route_entry_t
*route
;
1610 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
1611 policy_dir_names
, direction
);
1613 /* create a policy */
1614 policy
= create_policy_entry(src_ts
, dst_ts
, direction
, 0);
1616 /* find a matching policy */
1617 pthread_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 if (--found
->refcount
> 0)
1623 /* is used by more SAs, keep in kernel */
1624 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1625 policy_entry_destroy(policy
);
1626 pthread_mutex_unlock(&this->mutex
);
1629 /* remove if last reference */
1630 this->policies
->remove(this->policies
, found
, NULL
);
1631 policy_entry_destroy(policy
);
1636 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
1637 dst_ts
, policy_dir_names
, direction
);
1638 policy_entry_destroy(policy
);
1639 pthread_mutex_unlock(&this->mutex
);
1642 pthread_mutex_unlock(&this->mutex
);
1644 memset(&request
, 0, sizeof(request
));
1646 msg
= (struct sadb_msg
*)request
;
1647 msg
->sadb_msg_version
= PF_KEY_V2
;
1648 msg
->sadb_msg_type
= SADB_X_SPDDELETE
;
1649 msg
->sadb_msg_satype
= 0;
1650 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1652 pol
= (struct sadb_x_policy
*)PFKEY_EXT_ADD_NEXT(msg
);
1653 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1654 pol
->sadb_x_policy_len
= PFKEY_LEN(sizeof(struct sadb_x_policy
));
1655 pol
->sadb_x_policy_dir
= dir2kernel(direction
);
1656 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1657 PFKEY_EXT_ADD(msg
, pol
);
1659 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1660 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1661 addr
->sadb_address_proto
= policy
->src
.proto
;
1662 addr
->sadb_address_prefixlen
= policy
->src
.mask
;
1663 host2ext(policy
->src
.net
, addr
);
1664 PFKEY_EXT_ADD(msg
, addr
);
1666 addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
1667 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1668 addr
->sadb_address_proto
= policy
->dst
.proto
;
1669 addr
->sadb_address_prefixlen
= policy
->dst
.mask
;
1670 host2ext(policy
->dst
.net
, addr
);
1671 PFKEY_EXT_ADD(msg
, addr
);
1673 route
= policy
->route
;
1674 policy
->route
= NULL
;
1675 policy_entry_destroy(policy
);
1677 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1679 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
1680 policy_dir_names
, direction
);
1683 else if (out
->sadb_msg_errno
)
1685 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N: %s (%d)", src_ts
,
1686 dst_ts
, policy_dir_names
, direction
,
1687 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1695 if (charon
->kernel_interface
->del_route(charon
->kernel_interface
,
1696 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1697 route
->src_ip
, route
->if_name
) != SUCCESS
)
1699 DBG1(DBG_KNL
, "error uninstalling route installed with "
1700 "policy %R === %R %N", src_ts
, dst_ts
,
1701 policy_dir_names
, direction
);
1703 route_entry_destroy(route
);
1710 * Register a socket for AQUIRE/EXPIRE messages
1712 static status_t
register_pfkey_socket(private_kernel_pfkey_ipsec_t
*this, u_int8_t satype
)
1714 unsigned char request
[PFKEY_BUFFER_SIZE
];
1715 struct sadb_msg
*msg
, *out
;
1716 struct sadb_x_policy
*pol
;
1717 struct sadb_address
*addr
;
1718 policy_entry_t
*policy
, *found
= NULL
;
1719 pfkey_msg_t response
;
1722 memset(&request
, 0, sizeof(request
));
1724 msg
= (struct sadb_msg
*)request
;
1725 msg
->sadb_msg_version
= PF_KEY_V2
;
1726 msg
->sadb_msg_type
= SADB_REGISTER
;
1727 msg
->sadb_msg_satype
= satype
;
1728 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1730 if (pfkey_send_socket(this, this->socket_events
, msg
, &out
, &len
) != SUCCESS
)
1732 DBG1(DBG_KNL
, "unable to register PF_KEY socket");
1735 else if (out
->sadb_msg_errno
)
1737 DBG1(DBG_KNL
, "unable to register PF_KEY socket: %s (%d)",
1738 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1747 * Implementation of kernel_interface_t.destroy.
1749 static void destroy(private_kernel_pfkey_ipsec_t
*this)
1751 this->job
->cancel(this->job
);
1752 close(this->socket
);
1753 close(this->socket_events
);
1754 this->policies
->destroy_function(this->policies
, (void*)policy_entry_destroy
);
1759 * Described in header.
1761 kernel_pfkey_ipsec_t
*kernel_pfkey_ipsec_create()
1763 private_kernel_pfkey_ipsec_t
*this = malloc_thing(private_kernel_pfkey_ipsec_t
);
1765 /* public functions */
1766 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
;
1767 this->public.interface
.get_cpi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
1768 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
,u_int16_t
,u_int16_t
,u_int16_t
,prf_plus_t
*,ipsec_mode_t
,u_int16_t
,bool,bool))add_sa
;
1769 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
;
1770 this->public.interface
.del_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
1771 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
;
1772 this->public.interface
.query_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
1773 this->public.interface
.del_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
1775 this->public.interface
.destroy
= (void(*)(kernel_ipsec_t
*)) destroy
;
1777 /* private members */
1778 this->policies
= linked_list_create();
1779 pthread_mutex_init(&this->mutex
, NULL
);
1780 this->install_routes
= lib
->settings
->get_bool(lib
->settings
, "charon.install_routes", TRUE
);
1781 pthread_mutex_init(&this->mutex_pfkey
, NULL
);
1784 /* create a PF_KEY socket to communicate with the kernel */
1785 this->socket
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
1786 if (this->socket
<= 0)
1788 charon
->kill(charon
, "unable to create PF_KEY socket");
1791 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
1792 this->socket_events
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
1793 if (this->socket_events
<= 0)
1795 charon
->kill(charon
, "unable to create PF_KEY event socket");
1798 /* register the event socket */
1799 if (register_pfkey_socket(this, SADB_SATYPE_ESP
) != SUCCESS
||
1800 register_pfkey_socket(this, SADB_SATYPE_AH
) != SUCCESS
)
1802 charon
->kill(charon
, "unable to register PF_KEY event socket");
1805 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
1807 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
1809 return &this->public;