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
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/ioctl.h>
21 #include <linux/udp.h>
29 #include "kernel_klips_ipsec.h"
32 #include <utils/debug.h>
33 #include <collections/linked_list.h>
34 #include <threading/thread.h>
35 #include <threading/mutex.h>
36 #include <processing/jobs/callback_job.h>
38 /** default timeout for generated SPIs (in seconds) */
39 #define SPI_TIMEOUT 30
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 /** special SPI values used for policies in KLIPS */
71 #define SPI_REJECT 258
74 #define SPI_TRAPSUBNET 261
76 /** the prefix of the name of KLIPS ipsec devices */
77 #define IPSEC_DEV_PREFIX "ipsec"
78 /** this is the default number of ipsec devices */
79 #define DEFAULT_IPSEC_DEV_COUNT 4
80 /** TRUE if the given name matches an ipsec device */
81 #define IS_IPSEC_DEV(name) (strneq((name), IPSEC_DEV_PREFIX, sizeof(IPSEC_DEV_PREFIX) - 1))
83 /** the following stuff is from ipsec_tunnel.h */
84 struct ipsectunnelconf
91 #define cf_name cf_u.cfu_name
94 #define IPSEC_SET_DEV (SIOCDEVPRIVATE)
95 #define IPSEC_DEL_DEV (SIOCDEVPRIVATE + 1)
96 #define IPSEC_CLR_DEV (SIOCDEVPRIVATE + 2)
98 typedef struct private_kernel_klips_ipsec_t private_kernel_klips_ipsec_t
;
101 * Private variables and functions of kernel_klips class.
103 struct private_kernel_klips_ipsec_t
106 * Public part of the kernel_klips_t object.
108 kernel_klips_ipsec_t
public;
111 * mutex to lock access to various lists
116 * List of installed policies (policy_entry_t)
118 linked_list_t
*policies
;
121 * List of allocated SPIs without installed SA (sa_entry_t)
123 linked_list_t
*allocated_spis
;
126 * List of installed SAs (sa_entry_t)
128 linked_list_t
*installed_sas
;
131 * whether to install routes along policies
136 * List of ipsec devices (ipsec_dev_t)
138 linked_list_t
*ipsec_devices
;
141 * mutex to lock access to the PF_KEY socket
143 mutex_t
*mutex_pfkey
;
146 * PF_KEY socket to communicate with the kernel
151 * PF_KEY socket to receive acquire and expire events
156 * sequence number for messages sent to the kernel
163 typedef struct ipsec_dev_t ipsec_dev_t
;
169 /** name of the virtual ipsec interface */
172 /** name of the physical interface */
173 char phys_name
[IFNAMSIZ
];
175 /** by how many CHILD_SA's this ipsec device is used */
180 * compare the given name with the virtual device name
182 static inline bool ipsec_dev_match_byname(ipsec_dev_t
*current
, char *name
)
184 return name
&& streq(current
->name
, name
);
188 * compare the given name with the physical device name
190 static inline bool ipsec_dev_match_byphys(ipsec_dev_t
*current
, char *name
)
192 return name
&& streq(current
->phys_name
, name
);
196 * matches free ipsec devices
198 static inline bool ipsec_dev_match_free(ipsec_dev_t
*current
)
200 return current
->refcount
== 0;
204 * tries to find an ipsec_dev_t object by name
206 static status_t
find_ipsec_dev(private_kernel_klips_ipsec_t
*this, char *name
,
209 linked_list_match_t match
= (linked_list_match_t
)(IS_IPSEC_DEV(name
) ?
210 ipsec_dev_match_byname
: ipsec_dev_match_byphys
);
211 return this->ipsec_devices
->find_first(this->ipsec_devices
, match
,
216 * attach an ipsec device to a physical interface
218 static status_t
attach_ipsec_dev(char* name
, char *phys_name
)
222 struct ipsectunnelconf
*itc
= (struct ipsectunnelconf
*)&req
.ifr_data
;
226 DBG2(DBG_KNL
, "attaching virtual interface %s to %s", name
, phys_name
);
228 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) <= 0)
233 strncpy(req
.ifr_name
, phys_name
, IFNAMSIZ
);
234 if (ioctl(sock
, SIOCGIFFLAGS
, &req
) < 0)
239 phys_flags
= req
.ifr_flags
;
241 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
242 if (ioctl(sock
, SIOCGIFFLAGS
, &req
) < 0)
248 if (req
.ifr_flags
& IFF_UP
)
250 /* if it's already up, it is already attached, detach it first */
251 ioctl(sock
, IPSEC_DEL_DEV
, &req
);
255 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
256 strncpy(itc
->cf_name
, phys_name
, sizeof(itc
->cf_name
));
257 ioctl(sock
, IPSEC_SET_DEV
, &req
);
259 /* copy address from physical to virtual */
260 strncpy(req
.ifr_name
, phys_name
, IFNAMSIZ
);
261 if (ioctl(sock
, SIOCGIFADDR
, &req
) == 0)
263 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
264 ioctl(sock
, SIOCSIFADDR
, &req
);
267 /* copy net mask from physical to virtual */
268 strncpy(req
.ifr_name
, phys_name
, IFNAMSIZ
);
269 if (ioctl(sock
, SIOCGIFNETMASK
, &req
) == 0)
271 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
272 ioctl(sock
, SIOCSIFNETMASK
, &req
);
275 /* copy other flags and addresses */
276 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
277 if (ioctl(sock
, SIOCGIFFLAGS
, &req
) == 0)
279 if (phys_flags
& IFF_POINTOPOINT
)
281 req
.ifr_flags
|= IFF_POINTOPOINT
;
282 req
.ifr_flags
&= ~IFF_BROADCAST
;
283 ioctl(sock
, SIOCSIFFLAGS
, &req
);
285 strncpy(req
.ifr_name
, phys_name
, IFNAMSIZ
);
286 if (ioctl(sock
, SIOCGIFDSTADDR
, &req
) == 0)
288 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
289 ioctl(sock
, SIOCSIFDSTADDR
, &req
);
292 else if (phys_flags
& IFF_BROADCAST
)
294 req
.ifr_flags
&= ~IFF_POINTOPOINT
;
295 req
.ifr_flags
|= IFF_BROADCAST
;
296 ioctl(sock
, SIOCSIFFLAGS
, &req
);
298 strncpy(req
.ifr_name
, phys_name
, IFNAMSIZ
);
299 if (ioctl(sock
, SIOCGIFBRDADDR
, &req
)==0)
301 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
302 ioctl(sock
, SIOCSIFBRDADDR
, &req
);
307 req
.ifr_flags
&= ~IFF_POINTOPOINT
;
308 req
.ifr_flags
&= ~IFF_BROADCAST
;
309 ioctl(sock
, SIOCSIFFLAGS
, &req
);
313 mtu
= lib
->settings
->get_int(lib
->settings
,
314 "%s.plugins.kernel-klips.ipsec_dev_mtu", 0,
318 /* guess MTU as physical MTU - ESP overhead [- NAT-T overhead]
319 * ESP overhead : 73 bytes
320 * NAT-T overhead : 8 bytes ==> 81 bytes
322 * assuming tunnel mode with AES encryption and integrity
323 * outer IP header : 20 bytes
324 * (NAT-T UDP header: 8 bytes)
325 * ESP header : 8 bytes
327 * padding : 15 bytes (worst-case)
328 * pad len / NH : 2 bytes
329 * auth data : 12 bytes
331 strncpy(req
.ifr_name
, phys_name
, IFNAMSIZ
);
332 ioctl(sock
, SIOCGIFMTU
, &req
);
333 mtu
= req
.ifr_mtu
- 81;
337 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
339 ioctl(sock
, SIOCSIFMTU
, &req
);
341 /* bring ipsec device UP */
342 if (ioctl(sock
, SIOCGIFFLAGS
, &req
) == 0)
344 req
.ifr_flags
|= IFF_UP
;
345 ioctl(sock
, SIOCSIFFLAGS
, &req
);
353 * detach an ipsec device from a physical interface
355 static status_t
detach_ipsec_dev(char* name
, char *phys_name
)
360 DBG2(DBG_KNL
, "detaching virtual interface %s from %s", name
,
361 strlen(phys_name
) ? phys_name
: "any physical interface");
363 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) <= 0)
368 strncpy(req
.ifr_name
, name
, IFNAMSIZ
);
369 if (ioctl(sock
, SIOCGIFFLAGS
, &req
) < 0)
375 /* shutting interface down */
376 if (req
.ifr_flags
& IFF_UP
)
378 req
.ifr_flags
&= ~IFF_UP
;
379 ioctl(sock
, SIOCSIFFLAGS
, &req
);
383 memset(&req
.ifr_addr
, 0, sizeof(req
.ifr_addr
));
384 req
.ifr_addr
.sa_family
= AF_INET
;
385 ioctl(sock
, SIOCSIFADDR
, &req
);
387 /* detach interface */
388 ioctl(sock
, IPSEC_DEL_DEV
, &req
);
395 * destroy an ipsec_dev_t object
397 static void ipsec_dev_destroy(ipsec_dev_t
*this)
399 detach_ipsec_dev(this->name
, this->phys_name
);
404 typedef struct route_entry_t route_entry_t
;
407 * installed routing entry
409 struct route_entry_t
{
410 /** Name of the interface the route is bound to */
413 /** Source ip of the route */
416 /** Gateway for this route */
419 /** Destination net */
422 /** Destination net prefixlen */
427 * destroy an route_entry_t object
429 static void route_entry_destroy(route_entry_t
*this)
432 this->src_ip
->destroy(this->src_ip
);
433 this->gateway
->destroy(this->gateway
);
434 chunk_free(&this->dst_net
);
438 typedef struct policy_entry_t policy_entry_t
;
441 * installed kernel policy.
443 struct policy_entry_t
{
445 /** reqid of this policy, if setup as trap */
448 /** direction of this policy: in, out, forward */
451 /** parameters of installed policy */
453 /** subnet and port */
461 /** associated route installed for this policy */
462 route_entry_t
*route
;
464 /** by how many CHILD_SA's this policy is actively used */
467 /** by how many CHILD_SA's this policy is trapped */
472 * convert a numerical netmask to a host_t
474 static host_t
*mask2host(int family
, u_int8_t mask
)
476 static const u_char bitmask
[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
477 chunk_t chunk
= chunk_alloca(family
== AF_INET ?
4 : 16);
478 int bytes
= mask
/ 8, bits
= mask
% 8;
479 memset(chunk
.ptr
, 0xFF, bytes
);
480 memset(chunk
.ptr
+ bytes
, 0, chunk
.len
- bytes
);
483 chunk
.ptr
[bytes
] = bitmask
[bits
];
485 return host_create_from_chunk(family
, chunk
, 0);
489 * check if a host is in a subnet (host with netmask in bits)
491 static bool is_host_in_net(host_t
*host
, host_t
*net
, u_int8_t mask
)
493 static const u_char bitmask
[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
494 chunk_t host_chunk
, net_chunk
;
495 int bytes
= mask
/ 8, bits
= mask
% 8;
497 host_chunk
= host
->get_address(host
);
498 net_chunk
= net
->get_address(net
);
500 if (host_chunk
.len
!= net_chunk
.len
)
505 if (memeq(host_chunk
.ptr
, net_chunk
.ptr
, bytes
))
507 return (bits
== 0) ||
508 (host_chunk
.ptr
[bytes
] & bitmask
[bits
]) ==
509 (net_chunk
.ptr
[bytes
] & bitmask
[bits
]);
516 * create a policy_entry_t object
518 static policy_entry_t
*create_policy_entry(traffic_selector_t
*src_ts
,
519 traffic_selector_t
*dst_ts
, policy_dir_t dir
)
521 policy_entry_t
*policy
= malloc_thing(policy_entry_t
);
523 policy
->direction
= dir
;
524 policy
->route
= NULL
;
525 policy
->activecount
= 0;
526 policy
->trapcount
= 0;
528 src_ts
->to_subnet(src_ts
, &policy
->src
.net
, &policy
->src
.mask
);
529 dst_ts
->to_subnet(dst_ts
, &policy
->dst
.net
, &policy
->dst
.mask
);
531 /* src or dest proto may be "any" (0), use more restrictive one */
532 policy
->src
.proto
= max(src_ts
->get_protocol(src_ts
), dst_ts
->get_protocol(dst_ts
));
533 policy
->src
.proto
= policy
->src
.proto ? policy
->src
.proto
: 0;
534 policy
->dst
.proto
= policy
->src
.proto
;
540 * destroy a policy_entry_t object
542 static void policy_entry_destroy(policy_entry_t
*this)
544 DESTROY_IF(this->src
.net
);
545 DESTROY_IF(this->dst
.net
);
548 route_entry_destroy(this->route
);
554 * compares two policy_entry_t
556 static inline bool policy_entry_equals(policy_entry_t
*current
, policy_entry_t
*policy
)
558 return current
->direction
== policy
->direction
&&
559 current
->src
.proto
== policy
->src
.proto
&&
560 current
->dst
.proto
== policy
->dst
.proto
&&
561 current
->src
.mask
== policy
->src
.mask
&&
562 current
->dst
.mask
== policy
->dst
.mask
&&
563 current
->src
.net
->equals(current
->src
.net
, policy
->src
.net
) &&
564 current
->dst
.net
->equals(current
->dst
.net
, policy
->dst
.net
);
567 static inline bool policy_entry_match_byaddrs(policy_entry_t
*current
, host_t
*src
,
570 return is_host_in_net(src
, current
->src
.net
, current
->src
.mask
) &&
571 is_host_in_net(dst
, current
->dst
.net
, current
->dst
.mask
);
574 typedef struct sa_entry_t sa_entry_t
;
577 * used for two things:
578 * - allocated SPIs that have not yet resulted in an installed SA
579 * - installed inbound SAs with enabled UDP encapsulation
583 /** protocol of this SA */
586 /** reqid of this SA */
589 /** SPI of this SA */
592 /** src address of this SA */
595 /** dst address of this SA */
598 /** TRUE if this SA uses UDP encapsulation */
601 /** TRUE if this SA is inbound */
606 * create an sa_entry_t object
608 static sa_entry_t
*create_sa_entry(u_int8_t protocol
, u_int32_t spi
,
609 u_int32_t reqid
, host_t
*src
, host_t
*dst
,
610 bool encap
, bool inbound
)
612 sa_entry_t
*sa
= malloc_thing(sa_entry_t
);
613 sa
->protocol
= protocol
;
616 sa
->src
= src ? src
->clone(src
) : NULL
;
617 sa
->dst
= dst ? dst
->clone(dst
) : NULL
;
619 sa
->inbound
= inbound
;
624 * destroy an sa_entry_t object
626 static void sa_entry_destroy(sa_entry_t
*this)
628 DESTROY_IF(this->src
);
629 DESTROY_IF(this->dst
);
634 * match an sa_entry_t for an inbound SA that uses UDP encapsulation by spi and src (remote) address
636 static inline bool sa_entry_match_encapbysrc(sa_entry_t
*current
, u_int32_t
*spi
,
639 return current
->encap
&& current
->inbound
&&
640 current
->spi
== *spi
&& src
->ip_equals(src
, current
->src
);
644 * match an sa_entry_t by protocol, spi and dst address (as the kernel does it)
646 static inline bool sa_entry_match_bydst(sa_entry_t
*current
, u_int8_t
*protocol
,
647 u_int32_t
*spi
, host_t
*dst
)
649 return current
->protocol
== *protocol
&& current
->spi
== *spi
&& dst
->ip_equals(dst
, current
->dst
);
653 * match an sa_entry_t by protocol, reqid and spi
655 static inline bool sa_entry_match_byid(sa_entry_t
*current
, u_int8_t
*protocol
,
656 u_int32_t
*spi
, u_int32_t
*reqid
)
658 return current
->protocol
== *protocol
&& current
->spi
== *spi
&& current
->reqid
== *reqid
;
661 typedef struct pfkey_msg_t pfkey_msg_t
;
666 * PF_KEY message base
668 struct sadb_msg
*msg
;
672 * PF_KEY message extensions
675 struct sadb_ext
*ext
[SADB_EXT_MAX
+ 1];
677 struct sadb_ext
*reserved
; /* SADB_EXT_RESERVED */
678 struct sadb_sa
*sa
; /* SADB_EXT_SA */
679 struct sadb_lifetime
*lft_current
; /* SADB_EXT_LIFETIME_CURRENT */
680 struct sadb_lifetime
*lft_hard
; /* SADB_EXT_LIFETIME_HARD */
681 struct sadb_lifetime
*lft_soft
; /* SADB_EXT_LIFETIME_SOFT */
682 struct sadb_address
*src
; /* SADB_EXT_ADDRESS_SRC */
683 struct sadb_address
*dst
; /* SADB_EXT_ADDRESS_DST */
684 struct sadb_address
*proxy
; /* SADB_EXT_ADDRESS_PROXY */
685 struct sadb_key
*key_auth
; /* SADB_EXT_KEY_AUTH */
686 struct sadb_key
*key_encr
; /* SADB_EXT_KEY_ENCRYPT */
687 struct sadb_ident
*id_src
; /* SADB_EXT_IDENTITY_SRC */
688 struct sadb_ident
*id_dst
; /* SADB_EXT_IDENTITY_DST */
689 struct sadb_sens
*sensitivity
; /* SADB_EXT_SENSITIVITY */
690 struct sadb_prop
*proposal
; /* SADB_EXT_PROPOSAL */
691 struct sadb_supported
*supported_auth
; /* SADB_EXT_SUPPORTED_AUTH */
692 struct sadb_supported
*supported_encr
; /* SADB_EXT_SUPPORTED_ENCRYPT */
693 struct sadb_spirange
*spirange
; /* SADB_EXT_SPIRANGE */
694 struct sadb_x_kmprivate
*x_kmprivate
; /* SADB_X_EXT_KMPRIVATE */
695 struct sadb_ext
*x_policy
; /* SADB_X_EXT_SATYPE2 */
696 struct sadb_ext
*x_sa2
; /* SADB_X_EXT_SA2 */
697 struct sadb_address
*x_dst2
; /* SADB_X_EXT_ADDRESS_DST2 */
698 struct sadb_address
*x_src_flow
; /* SADB_X_EXT_ADDRESS_SRC_FLOW */
699 struct sadb_address
*x_dst_flow
; /* SADB_X_EXT_ADDRESS_DST_FLOW */
700 struct sadb_address
*x_src_mask
; /* SADB_X_EXT_ADDRESS_SRC_MASK */
701 struct sadb_address
*x_dst_mask
; /* SADB_X_EXT_ADDRESS_DST_MASK */
702 struct sadb_x_debug
*x_debug
; /* SADB_X_EXT_DEBUG */
703 struct sadb_protocol
*x_protocol
; /* SADB_X_EXT_PROTOCOL */
704 struct sadb_x_nat_t_type
*x_natt_type
; /* SADB_X_EXT_NAT_T_TYPE */
705 struct sadb_x_nat_t_port
*x_natt_sport
; /* SADB_X_EXT_NAT_T_SPORT */
706 struct sadb_x_nat_t_port
*x_natt_dport
; /* SADB_X_EXT_NAT_T_DPORT */
707 struct sadb_address
*x_natt_oa
; /* SADB_X_EXT_NAT_T_OA */
708 } __attribute__((__packed__
));
713 * convert a protocol identifier to the PF_KEY sa type
715 static u_int8_t
proto2satype(u_int8_t proto
)
720 return SADB_SATYPE_ESP
;
722 return SADB_SATYPE_AH
;
724 return SADB_X_SATYPE_COMP
;
731 * convert a PF_KEY sa type to a protocol identifier
733 static u_int8_t
satype2proto(u_int8_t satype
)
737 case SADB_SATYPE_ESP
:
741 case SADB_X_SATYPE_COMP
:
748 typedef struct kernel_algorithm_t kernel_algorithm_t
;
751 * Mapping of IKEv2 algorithms to PF_KEY algorithms
753 struct kernel_algorithm_t
{
755 * Identifier specified in IKEv2
760 * Identifier as defined in pfkeyv2.h
765 #define END_OF_LIST -1
768 * Algorithms for encryption
770 static kernel_algorithm_t encryption_algs
[] = {
771 /* {ENCR_DES_IV64, 0 }, */
772 {ENCR_DES
, SADB_EALG_DESCBC
},
773 {ENCR_3DES
, SADB_EALG_3DESCBC
},
774 /* {ENCR_RC5, 0 }, */
775 /* {ENCR_IDEA, 0 }, */
776 /* {ENCR_CAST, 0 }, */
777 {ENCR_BLOWFISH
, SADB_EALG_BFCBC
},
778 /* {ENCR_3IDEA, 0 }, */
779 /* {ENCR_DES_IV32, 0 }, */
780 {ENCR_NULL
, SADB_EALG_NULL
},
781 {ENCR_AES_CBC
, SADB_EALG_AESCBC
},
782 /* {ENCR_AES_CTR, 0 }, */
783 /* {ENCR_AES_CCM_ICV8, 0 }, */
784 /* {ENCR_AES_CCM_ICV12, 0 }, */
785 /* {ENCR_AES_CCM_ICV16, 0 }, */
786 /* {ENCR_AES_GCM_ICV8, 0 }, */
787 /* {ENCR_AES_GCM_ICV12, 0 }, */
788 /* {ENCR_AES_GCM_ICV16, 0 }, */
793 * Algorithms for integrity protection
795 static kernel_algorithm_t integrity_algs
[] = {
796 {AUTH_HMAC_MD5_96
, SADB_AALG_MD5HMAC
},
797 {AUTH_HMAC_SHA1_96
, SADB_AALG_SHA1HMAC
},
798 {AUTH_HMAC_SHA2_256_128
, SADB_AALG_SHA256_HMAC
},
799 {AUTH_HMAC_SHA2_384_192
, SADB_AALG_SHA384_HMAC
},
800 {AUTH_HMAC_SHA2_512_256
, SADB_AALG_SHA512_HMAC
},
801 /* {AUTH_DES_MAC, 0, }, */
802 /* {AUTH_KPDK_MD5, 0, }, */
803 /* {AUTH_AES_XCBC_96, 0, }, */
809 * Algorithms for IPComp, unused yet
811 static kernel_algorithm_t compression_algs
[] = {
812 /* {IPCOMP_OUI, 0 }, */
813 {IPCOMP_DEFLATE
, SADB_X_CALG_DEFLATE
},
814 {IPCOMP_LZS
, SADB_X_CALG_LZS
},
815 /* {IPCOMP_LZJH, 0 }, */
821 * Look up a kernel algorithm ID and its key size
823 static int lookup_algorithm(transform_type_t type
, int ikev2
)
825 kernel_algorithm_t
*list
;
830 case ENCRYPTION_ALGORITHM
:
831 list
= encryption_algs
;
833 case INTEGRITY_ALGORITHM
:
834 list
= integrity_algs
;
839 while (list
->ikev2
!= END_OF_LIST
)
841 if (ikev2
== list
->ikev2
)
847 hydra
->kernel_interface
->lookup_algorithm(hydra
->kernel_interface
, ikev2
,
853 * add a host behind a sadb_address extension
855 static void host2ext(host_t
*host
, struct sadb_address
*ext
)
857 sockaddr_t
*host_addr
= host
->get_sockaddr(host
);
858 socklen_t
*len
= host
->get_sockaddr_len(host
);
859 memcpy((char*)(ext
+ 1), host_addr
, *len
);
860 ext
->sadb_address_len
= PFKEY_LEN(sizeof(*ext
) + *len
);
864 * add a host to the given sadb_msg
866 static void add_addr_ext(struct sadb_msg
*msg
, host_t
*host
, u_int16_t type
)
868 struct sadb_address
*addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
869 addr
->sadb_address_exttype
= type
;
870 host2ext(host
, addr
);
871 PFKEY_EXT_ADD(msg
, addr
);
875 * adds an empty address extension to the given sadb_msg
877 static void add_anyaddr_ext(struct sadb_msg
*msg
, int family
, u_int8_t type
)
879 socklen_t len
= (family
== AF_INET
) ?
sizeof(struct sockaddr_in
) :
880 sizeof(struct sockaddr_in6
);
881 struct sadb_address
*addr
= (struct sadb_address
*)PFKEY_EXT_ADD_NEXT(msg
);
882 addr
->sadb_address_exttype
= type
;
883 sockaddr_t
*saddr
= (sockaddr_t
*)(addr
+ 1);
884 saddr
->sa_family
= family
;
885 addr
->sadb_address_len
= PFKEY_LEN(sizeof(*addr
) + len
);
886 PFKEY_EXT_ADD(msg
, addr
);
890 * add udp encap extensions to a sadb_msg
892 static void add_encap_ext(struct sadb_msg
*msg
, host_t
*src
, host_t
*dst
,
895 struct sadb_x_nat_t_type
* nat_type
;
896 struct sadb_x_nat_t_port
* nat_port
;
900 nat_type
= (struct sadb_x_nat_t_type
*)PFKEY_EXT_ADD_NEXT(msg
);
901 nat_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
902 nat_type
->sadb_x_nat_t_type_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_type
));
903 nat_type
->sadb_x_nat_t_type_type
= UDP_ENCAP_ESPINUDP
;
904 PFKEY_EXT_ADD(msg
, nat_type
);
907 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
908 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
909 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_port
));
910 nat_port
->sadb_x_nat_t_port_port
= src
->get_port(src
);
911 PFKEY_EXT_ADD(msg
, nat_port
);
913 nat_port
= (struct sadb_x_nat_t_port
*)PFKEY_EXT_ADD_NEXT(msg
);
914 nat_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
915 nat_port
->sadb_x_nat_t_port_len
= PFKEY_LEN(sizeof(struct sadb_x_nat_t_port
));
916 nat_port
->sadb_x_nat_t_port_port
= dst
->get_port(dst
);
917 PFKEY_EXT_ADD(msg
, nat_port
);
921 * build an SADB_X_ADDFLOW msg
923 static void build_addflow(struct sadb_msg
*msg
, u_int8_t satype
, u_int32_t spi
,
924 host_t
*src
, host_t
*dst
, host_t
*src_net
, u_int8_t src_mask
,
925 host_t
*dst_net
, u_int8_t dst_mask
, u_int8_t protocol
, bool replace
)
928 struct sadb_protocol
*proto
;
931 msg
->sadb_msg_version
= PF_KEY_V2
;
932 msg
->sadb_msg_type
= SADB_X_ADDFLOW
;
933 msg
->sadb_msg_satype
= satype
;
934 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
936 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
937 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
938 sa
->sadb_sa_spi
= spi
;
939 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
940 sa
->sadb_sa_flags
= replace ? SADB_X_SAFLAGS_REPLACEFLOW
: 0;
941 PFKEY_EXT_ADD(msg
, sa
);
945 add_anyaddr_ext(msg
, src_net
->get_family(src_net
), SADB_EXT_ADDRESS_SRC
);
949 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
);
954 add_anyaddr_ext(msg
, dst_net
->get_family(dst_net
), SADB_EXT_ADDRESS_DST
);
958 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
);
961 add_addr_ext(msg
, src_net
, SADB_X_EXT_ADDRESS_SRC_FLOW
);
962 add_addr_ext(msg
, dst_net
, SADB_X_EXT_ADDRESS_DST_FLOW
);
964 host
= mask2host(src_net
->get_family(src_net
), src_mask
);
965 add_addr_ext(msg
, host
, SADB_X_EXT_ADDRESS_SRC_MASK
);
968 host
= mask2host(dst_net
->get_family(dst_net
), dst_mask
);
969 add_addr_ext(msg
, host
, SADB_X_EXT_ADDRESS_DST_MASK
);
972 proto
= (struct sadb_protocol
*)PFKEY_EXT_ADD_NEXT(msg
);
973 proto
->sadb_protocol_exttype
= SADB_X_EXT_PROTOCOL
;
974 proto
->sadb_protocol_len
= PFKEY_LEN(sizeof(struct sadb_protocol
));
975 proto
->sadb_protocol_proto
= protocol
;
976 PFKEY_EXT_ADD(msg
, proto
);
980 * build an SADB_X_DELFLOW msg
982 static void build_delflow(struct sadb_msg
*msg
, u_int8_t satype
,
983 host_t
*src_net
, u_int8_t src_mask
, host_t
*dst_net
, u_int8_t dst_mask
,
986 struct sadb_protocol
*proto
;
989 msg
->sadb_msg_version
= PF_KEY_V2
;
990 msg
->sadb_msg_type
= SADB_X_DELFLOW
;
991 msg
->sadb_msg_satype
= satype
;
992 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
994 add_addr_ext(msg
, src_net
, SADB_X_EXT_ADDRESS_SRC_FLOW
);
995 add_addr_ext(msg
, dst_net
, SADB_X_EXT_ADDRESS_DST_FLOW
);
997 host
= mask2host(src_net
->get_family(src_net
),
999 add_addr_ext(msg
, host
, SADB_X_EXT_ADDRESS_SRC_MASK
);
1000 host
->destroy(host
);
1002 host
= mask2host(dst_net
->get_family(dst_net
),
1004 add_addr_ext(msg
, host
, SADB_X_EXT_ADDRESS_DST_MASK
);
1005 host
->destroy(host
);
1007 proto
= (struct sadb_protocol
*)PFKEY_EXT_ADD_NEXT(msg
);
1008 proto
->sadb_protocol_exttype
= SADB_X_EXT_PROTOCOL
;
1009 proto
->sadb_protocol_len
= PFKEY_LEN(sizeof(struct sadb_protocol
));
1010 proto
->sadb_protocol_proto
= protocol
;
1011 PFKEY_EXT_ADD(msg
, proto
);
1015 * Parses a pfkey message received from the kernel
1017 static status_t
parse_pfkey_message(struct sadb_msg
*msg
, pfkey_msg_t
*out
)
1019 struct sadb_ext
* ext
;
1022 memset(out
, 0, sizeof(pfkey_msg_t
));
1025 len
= msg
->sadb_msg_len
;
1026 len
-= PFKEY_LEN(sizeof(struct sadb_msg
));
1028 ext
= (struct sadb_ext
*)(((char*)msg
) + sizeof(struct sadb_msg
));
1030 while (len
>= PFKEY_LEN(sizeof(struct sadb_ext
)))
1032 if (ext
->sadb_ext_len
< PFKEY_LEN(sizeof(struct sadb_ext
)) ||
1033 ext
->sadb_ext_len
> len
)
1035 DBG1(DBG_KNL
, "length of PF_KEY extension (%d) is invalid", ext
->sadb_ext_type
);
1039 if ((ext
->sadb_ext_type
> SADB_EXT_MAX
) || (!ext
->sadb_ext_type
))
1041 DBG1(DBG_KNL
, "type of PF_KEY extension (%d) is invalid", ext
->sadb_ext_type
);
1045 if (out
->ext
[ext
->sadb_ext_type
])
1047 DBG1(DBG_KNL
, "duplicate PF_KEY extension of type (%d)", ext
->sadb_ext_type
);
1051 out
->ext
[ext
->sadb_ext_type
] = ext
;
1052 ext
= PFKEY_EXT_NEXT_LEN(ext
, len
);
1057 DBG1(DBG_KNL
, "PF_KEY message length is invalid");
1065 * Send a message to a specific PF_KEY socket and handle the response.
1067 static status_t
pfkey_send_socket(private_kernel_klips_ipsec_t
*this, int socket
,
1068 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
1070 unsigned char buf
[PFKEY_BUFFER_SIZE
];
1071 struct sadb_msg
*msg
;
1074 this->mutex_pfkey
->lock(this->mutex_pfkey
);
1076 in
->sadb_msg_seq
= ++this->seq
;
1077 in
->sadb_msg_pid
= getpid();
1079 in_len
= PFKEY_USER_LEN(in
->sadb_msg_len
);
1083 len
= send(socket
, in
, in_len
, 0);
1090 /* interrupted, try again */
1095 /* we should also get a response for these from KLIPS */
1098 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1099 DBG1(DBG_KNL
, "error sending to PF_KEY socket: %s (%d)",
1100 strerror(errno
), errno
);
1109 msg
= (struct sadb_msg
*)buf
;
1111 len
= recv(socket
, buf
, sizeof(buf
), 0);
1117 DBG1(DBG_KNL
, "got interrupted");
1118 /* interrupted, try again */
1121 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1122 DBG1(DBG_KNL
, "error reading from PF_KEY socket: %s", strerror(errno
));
1125 if (len
< sizeof(struct sadb_msg
) ||
1126 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
1128 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1129 DBG1(DBG_KNL
, "received corrupted PF_KEY message");
1132 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
1134 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1135 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY message");
1138 if (msg
->sadb_msg_pid
!= in
->sadb_msg_pid
)
1140 DBG2(DBG_KNL
, "received PF_KEY message is not intended for us");
1143 if (msg
->sadb_msg_seq
!= this->seq
)
1145 DBG1(DBG_KNL
, "received PF_KEY message with invalid sequence number,"
1146 " was %d expected %d", msg
->sadb_msg_seq
, this->seq
);
1147 if (msg
->sadb_msg_seq
< this->seq
)
1151 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1154 if (msg
->sadb_msg_type
!= in
->sadb_msg_type
)
1156 DBG2(DBG_KNL
, "received PF_KEY message of wrong type,"
1157 " was %d expected %d, ignoring",
1158 msg
->sadb_msg_type
, in
->sadb_msg_type
);
1164 *out
= (struct sadb_msg
*)malloc(len
);
1165 memcpy(*out
, buf
, len
);
1167 this->mutex_pfkey
->unlock(this->mutex_pfkey
);
1173 * Send a message to the default PF_KEY socket.
1175 static status_t
pfkey_send(private_kernel_klips_ipsec_t
*this,
1176 struct sadb_msg
*in
, struct sadb_msg
**out
, size_t *out_len
)
1178 return pfkey_send_socket(this, this->socket
, in
, out
, out_len
);
1182 * Send a message to the default PF_KEY socket and handle the response.
1184 static status_t
pfkey_send_ack(private_kernel_klips_ipsec_t
*this, struct sadb_msg
*in
)
1186 struct sadb_msg
*out
;
1189 if (pfkey_send(this, in
, &out
, &len
) != SUCCESS
)
1193 else if (out
->sadb_msg_errno
)
1195 DBG1(DBG_KNL
, "PF_KEY error: %s (%d)",
1196 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1205 * Add an eroute to KLIPS
1207 static status_t
add_eroute(private_kernel_klips_ipsec_t
*this, u_int8_t satype
,
1208 u_int32_t spi
, host_t
*src
, host_t
*dst
, host_t
*src_net
, u_int8_t src_mask
,
1209 host_t
*dst_net
, u_int8_t dst_mask
, u_int8_t protocol
, bool replace
)
1211 unsigned char request
[PFKEY_BUFFER_SIZE
];
1212 struct sadb_msg
*msg
= (struct sadb_msg
*)request
;
1214 memset(&request
, 0, sizeof(request
));
1216 build_addflow(msg
, satype
, spi
, src
, dst
, src_net
, src_mask
,
1217 dst_net
, dst_mask
, protocol
, replace
);
1219 return pfkey_send_ack(this, msg
);
1223 * Delete an eroute fom KLIPS
1225 static status_t
del_eroute(private_kernel_klips_ipsec_t
*this, u_int8_t satype
,
1226 host_t
*src_net
, u_int8_t src_mask
, host_t
*dst_net
, u_int8_t dst_mask
,
1229 unsigned char request
[PFKEY_BUFFER_SIZE
];
1230 struct sadb_msg
*msg
= (struct sadb_msg
*)request
;
1232 memset(&request
, 0, sizeof(request
));
1234 build_delflow(msg
, satype
, src_net
, src_mask
, dst_net
, dst_mask
, protocol
);
1236 return pfkey_send_ack(this, msg
);
1240 * Process a SADB_ACQUIRE message from the kernel
1242 static void process_acquire(private_kernel_klips_ipsec_t
*this, struct sadb_msg
* msg
)
1244 pfkey_msg_t response
;
1248 policy_entry_t
*policy
;
1250 switch (msg
->sadb_msg_satype
)
1252 case SADB_SATYPE_UNSPEC
:
1253 case SADB_SATYPE_ESP
:
1254 case SADB_SATYPE_AH
:
1257 /* acquire for AH/ESP only */
1261 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1263 DBG1(DBG_KNL
, "parsing SADB_ACQUIRE from kernel failed");
1267 /* KLIPS provides us only with the source and destination address,
1268 * and the transport protocol of the packet that triggered the policy.
1269 * we use this information to find a matching policy in our cache.
1270 * because KLIPS installs a narrow %hold eroute covering only this information,
1271 * we replace both the %trap and this %hold eroutes with a broader %hold
1272 * eroute covering the whole policy */
1273 src
= host_create_from_sockaddr((sockaddr_t
*)(response
.src
+ 1));
1274 dst
= host_create_from_sockaddr((sockaddr_t
*)(response
.dst
+ 1));
1275 proto
= response
.src
->sadb_address_proto
;
1276 if (!src
|| !dst
|| src
->get_family(src
) != dst
->get_family(dst
))
1278 DBG1(DBG_KNL
, "received an SADB_ACQUIRE with invalid hosts");
1282 DBG2(DBG_KNL
, "received an SADB_ACQUIRE for %H == %H : %d", src
, dst
, proto
);
1283 this->mutex
->lock(this->mutex
);
1284 if (this->policies
->find_first(this->policies
,
1285 (linked_list_match_t
)policy_entry_match_byaddrs
,
1286 (void**)&policy
, src
, dst
) != SUCCESS
)
1288 this->mutex
->unlock(this->mutex
);
1289 DBG1(DBG_KNL
, "received an SADB_ACQUIRE, but found no matching policy");
1292 if ((reqid
= policy
->reqid
) == 0)
1294 this->mutex
->unlock(this->mutex
);
1295 DBG1(DBG_KNL
, "received an SADB_ACQUIRE, but policy is not routed anymore");
1299 /* add a broad %hold eroute that replaces the %trap eroute */
1300 add_eroute(this, SADB_X_SATYPE_INT
, htonl(SPI_HOLD
), NULL
, NULL
,
1301 policy
->src
.net
, policy
->src
.mask
, policy
->dst
.net
, policy
->dst
.mask
,
1302 policy
->src
.proto
, TRUE
);
1304 /* remove the narrow %hold eroute installed by KLIPS */
1305 del_eroute(this, SADB_X_SATYPE_INT
, src
, 32, dst
, 32, proto
);
1307 this->mutex
->unlock(this->mutex
);
1309 hydra
->kernel_interface
->acquire(hydra
->kernel_interface
, reqid
, NULL
,
1314 * Process a SADB_X_NAT_T_NEW_MAPPING message from the kernel
1316 static void process_mapping(private_kernel_klips_ipsec_t
*this, struct sadb_msg
* msg
)
1318 pfkey_msg_t response
;
1319 u_int32_t spi
, reqid
;
1320 host_t
*old_src
, *new_src
;
1322 DBG2(DBG_KNL
, "received an SADB_X_NAT_T_NEW_MAPPING");
1324 if (parse_pfkey_message(msg
, &response
) != SUCCESS
)
1326 DBG1(DBG_KNL
, "parsing SADB_X_NAT_T_NEW_MAPPING from kernel failed");
1330 spi
= response
.sa
->sadb_sa_spi
;
1332 if (satype2proto(msg
->sadb_msg_satype
) == IPPROTO_ESP
)
1335 sockaddr_t
*addr
= (sockaddr_t
*)(response
.src
+ 1);
1336 old_src
= host_create_from_sockaddr(addr
);
1338 this->mutex
->lock(this->mutex
);
1339 if (!old_src
|| this->installed_sas
->find_first(this->installed_sas
,
1340 (linked_list_match_t
)sa_entry_match_encapbysrc
,
1341 (void**)&sa
, &spi
, old_src
) != SUCCESS
)
1343 this->mutex
->unlock(this->mutex
);
1344 DBG1(DBG_KNL
, "received an SADB_X_NAT_T_NEW_MAPPING, but found no matching SA");
1348 this->mutex
->unlock(this->mutex
);
1350 addr
= (sockaddr_t
*)(response
.dst
+ 1);
1351 switch (addr
->sa_family
)
1355 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
1356 sin
->sin_port
= htons(response
.x_natt_dport
->sadb_x_nat_t_port_port
);
1360 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
1361 sin6
->sin6_port
= htons(response
.x_natt_dport
->sadb_x_nat_t_port_port
);
1366 new_src
= host_create_from_sockaddr(addr
);
1369 hydra
->kernel_interface
->mapping(hydra
->kernel_interface
, reqid
,
1376 * Receives events from kernel
1378 static job_requeue_t
receive_events(private_kernel_klips_ipsec_t
*this)
1380 unsigned char buf
[PFKEY_BUFFER_SIZE
];
1381 struct sadb_msg
*msg
= (struct sadb_msg
*)buf
;
1385 oldstate
= thread_cancelability(TRUE
);
1386 len
= recv(this->socket_events
, buf
, sizeof(buf
), 0);
1387 thread_cancelability(oldstate
);
1394 /* interrupted, try again */
1395 return JOB_REQUEUE_DIRECT
;
1397 /* no data ready, select again */
1398 return JOB_REQUEUE_DIRECT
;
1400 DBG1(DBG_KNL
, "unable to receive from PF_KEY event socket");
1402 return JOB_REQUEUE_FAIR
;
1406 if (len
< sizeof(struct sadb_msg
) ||
1407 msg
->sadb_msg_len
< PFKEY_LEN(sizeof(struct sadb_msg
)))
1409 DBG2(DBG_KNL
, "received corrupted PF_KEY message");
1410 return JOB_REQUEUE_DIRECT
;
1412 if (msg
->sadb_msg_pid
!= 0)
1413 { /* not from kernel. not interested, try another one */
1414 return JOB_REQUEUE_DIRECT
;
1416 if (msg
->sadb_msg_len
> len
/ PFKEY_ALIGNMENT
)
1418 DBG1(DBG_KNL
, "buffer was too small to receive the complete PF_KEY message");
1419 return JOB_REQUEUE_DIRECT
;
1422 switch (msg
->sadb_msg_type
)
1425 process_acquire(this, msg
);
1428 /* SADB_EXPIRE events in KLIPS are only triggered by traffic (even
1429 * for the time based limits). So if there is no traffic for a
1430 * longer period than configured as hard limit, we wouldn't be able
1431 * to rekey the SA and just receive the hard expire and thus delete
1433 * To avoid this behavior and to make the daemon behave as with the
1434 * other kernel plugins, we implement the expiration of SAs
1437 case SADB_X_NAT_T_NEW_MAPPING
:
1438 process_mapping(this, msg
);
1444 return JOB_REQUEUE_DIRECT
;
1448 /** an SPI has expired */
1450 /** a CHILD_SA has to be rekeyed */
1452 /** a CHILD_SA has to be deleted */
1456 typedef struct sa_expire_t sa_expire_t
;
1458 struct sa_expire_t
{
1459 /** kernel interface */
1460 private_kernel_klips_ipsec_t
*this;
1461 /** the SPI of the expiring SA */
1463 /** the protocol of the expiring SA */
1465 /** the reqid of the expiring SA*/
1467 /** what type of expire this is */
1472 * Called when an SA expires
1474 static job_requeue_t
sa_expires(sa_expire_t
*expire
)
1476 private_kernel_klips_ipsec_t
*this = expire
->this;
1477 u_int8_t protocol
= expire
->protocol
;
1478 u_int32_t spi
= expire
->spi
, reqid
= expire
->reqid
;
1479 bool hard
= expire
->type
!= EXPIRE_TYPE_SOFT
;
1480 sa_entry_t
*cached_sa
;
1481 linked_list_t
*list
;
1483 /* for an expired SPI we first check whether the CHILD_SA got installed
1484 * in the meantime, for expired SAs we check whether they are still installed */
1485 list
= expire
->type
== EXPIRE_TYPE_SPI ?
this->allocated_spis
: this->installed_sas
;
1487 this->mutex
->lock(this->mutex
);
1488 if (list
->find_first(list
, (linked_list_match_t
)sa_entry_match_byid
,
1489 (void**)&cached_sa
, &protocol
, &spi
, &reqid
) != SUCCESS
)
1491 /* we found no entry:
1492 * - for SPIs, a CHILD_SA has been installed
1493 * - for SAs, the CHILD_SA has already been deleted */
1494 this->mutex
->unlock(this->mutex
);
1495 return JOB_REQUEUE_NONE
;
1499 list
->remove(list
, cached_sa
, NULL
);
1500 sa_entry_destroy(cached_sa
);
1502 this->mutex
->unlock(this->mutex
);
1504 hydra
->kernel_interface
->expire(hydra
->kernel_interface
, reqid
, protocol
,
1506 return JOB_REQUEUE_NONE
;
1510 * Schedule an expire job for an SA. Time is in seconds.
1512 static void schedule_expire(private_kernel_klips_ipsec_t
*this,
1513 u_int8_t protocol
, u_int32_t spi
,
1514 u_int32_t reqid
, expire_type_t type
, u_int32_t time
)
1516 callback_job_t
*job
;
1517 sa_expire_t
*expire
= malloc_thing(sa_expire_t
);
1518 expire
->this = this;
1519 expire
->protocol
= protocol
;
1521 expire
->reqid
= reqid
;
1522 expire
->type
= type
;
1523 job
= callback_job_create((callback_job_cb_t
)sa_expires
, expire
, free
, NULL
);
1524 lib
->scheduler
->schedule_job(lib
->scheduler
, (job_t
*)job
, time
);
1527 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1528 private_kernel_klips_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1529 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
1531 /* we cannot use SADB_GETSPI because KLIPS does not allow us to set the
1532 * NAT-T type in an SADB_UPDATE which we would have to use to update the
1533 * implicitly created SA.
1538 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
1539 if (!rng
|| !rng
->get_bytes(rng
, sizeof(spi_gen
), (void*)&spi_gen
))
1541 DBG1(DBG_KNL
, "allocating SPI failed");
1547 /* allocated SPIs lie within the range from 0xc0000000 to 0xcFFFFFFF */
1548 spi_gen
= 0xc0000000 | (spi_gen
& 0x0FFFFFFF);
1550 *spi
= htonl(spi_gen
);
1552 this->mutex
->lock(this->mutex
);
1553 this->allocated_spis
->insert_last(this->allocated_spis
,
1554 create_sa_entry(protocol
, *spi
, reqid
, NULL
, NULL
, FALSE
, TRUE
));
1555 this->mutex
->unlock(this->mutex
);
1556 schedule_expire(this, protocol
, *spi
, reqid
, EXPIRE_TYPE_SPI
, SPI_TIMEOUT
);
1561 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1562 private_kernel_klips_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1563 u_int32_t reqid
, u_int16_t
*cpi
)
1569 * Add a pseudo IPIP SA for tunnel mode with KLIPS.
1571 static status_t
add_ipip_sa(private_kernel_klips_ipsec_t
*this,
1572 host_t
*src
, host_t
*dst
, u_int32_t spi
, u_int32_t reqid
)
1574 unsigned char request
[PFKEY_BUFFER_SIZE
];
1575 struct sadb_msg
*msg
, *out
;
1579 memset(&request
, 0, sizeof(request
));
1581 DBG2(DBG_KNL
, "adding pseudo IPIP SA with SPI %.8x and reqid {%d}", ntohl(spi
), reqid
);
1583 msg
= (struct sadb_msg
*)request
;
1584 msg
->sadb_msg_version
= PF_KEY_V2
;
1585 msg
->sadb_msg_type
= SADB_ADD
;
1586 msg
->sadb_msg_satype
= SADB_X_SATYPE_IPIP
;
1587 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1589 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1590 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1591 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1592 sa
->sadb_sa_spi
= spi
;
1593 sa
->sadb_sa_state
= SADB_SASTATE_MATURE
;
1594 PFKEY_EXT_ADD(msg
, sa
);
1596 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
);
1597 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
);
1599 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1601 DBG1(DBG_KNL
, "unable to add pseudo IPIP SA with SPI %.8x", ntohl(spi
));
1604 else if (out
->sadb_msg_errno
)
1606 DBG1(DBG_KNL
, "unable to add pseudo IPIP SA with SPI %.8x: %s (%d)",
1607 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1617 * group the IPIP SA required for tunnel mode with the outer SA
1619 static status_t
group_ipip_sa(private_kernel_klips_ipsec_t
*this,
1620 host_t
*src
, host_t
*dst
, u_int32_t spi
,
1621 u_int8_t protocol
, u_int32_t reqid
)
1623 unsigned char request
[PFKEY_BUFFER_SIZE
];
1624 struct sadb_msg
*msg
, *out
;
1626 struct sadb_x_satype
*satype
;
1629 memset(&request
, 0, sizeof(request
));
1631 DBG2(DBG_KNL
, "grouping SAs with SPI %.8x and reqid {%d}", ntohl(spi
), reqid
);
1633 msg
= (struct sadb_msg
*)request
;
1634 msg
->sadb_msg_version
= PF_KEY_V2
;
1635 msg
->sadb_msg_type
= SADB_X_GRPSA
;
1636 msg
->sadb_msg_satype
= SADB_X_SATYPE_IPIP
;
1637 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1639 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1640 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1641 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1642 sa
->sadb_sa_spi
= spi
;
1643 sa
->sadb_sa_state
= SADB_SASTATE_MATURE
;
1644 PFKEY_EXT_ADD(msg
, sa
);
1646 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
);
1648 satype
= (struct sadb_x_satype
*)PFKEY_EXT_ADD_NEXT(msg
);
1649 satype
->sadb_x_satype_exttype
= SADB_X_EXT_SATYPE2
;
1650 satype
->sadb_x_satype_len
= PFKEY_LEN(sizeof(struct sadb_x_satype
));
1651 satype
->sadb_x_satype_satype
= proto2satype(protocol
);
1652 PFKEY_EXT_ADD(msg
, satype
);
1654 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1655 sa
->sadb_sa_exttype
= SADB_X_EXT_SA2
;
1656 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1657 sa
->sadb_sa_spi
= spi
;
1658 sa
->sadb_sa_state
= SADB_SASTATE_MATURE
;
1659 PFKEY_EXT_ADD(msg
, sa
);
1661 add_addr_ext(msg
, dst
, SADB_X_EXT_ADDRESS_DST2
);
1663 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1665 DBG1(DBG_KNL
, "unable to group SAs with SPI %.8x", ntohl(spi
));
1668 else if (out
->sadb_msg_errno
)
1670 DBG1(DBG_KNL
, "unable to group SAs with SPI %.8x: %s (%d)",
1671 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1680 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1681 private_kernel_klips_ipsec_t
*this, host_t
*src
, host_t
*dst
, u_int32_t spi
,
1682 u_int8_t protocol
, u_int32_t reqid
, mark_t mark
, u_int32_t tfc
,
1683 lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
1684 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
,
1685 u_int16_t ipcomp
, u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
1686 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
)
1688 unsigned char request
[PFKEY_BUFFER_SIZE
];
1689 struct sadb_msg
*msg
, *out
;
1691 struct sadb_key
*key
;
1696 /* for inbound SAs we allocated an SPI via get_spi, so we first check
1697 * whether that SPI has already expired (race condition) */
1698 sa_entry_t
*alloc_spi
;
1699 this->mutex
->lock(this->mutex
);
1700 if (this->allocated_spis
->find_first(this->allocated_spis
,
1701 (linked_list_match_t
)sa_entry_match_byid
, (void**)&alloc_spi
,
1702 &protocol
, &spi
, &reqid
) != SUCCESS
)
1704 this->mutex
->unlock(this->mutex
);
1705 DBG1(DBG_KNL
, "allocated SPI %.8x has already expired", ntohl(spi
));
1710 this->allocated_spis
->remove(this->allocated_spis
, alloc_spi
, NULL
);
1711 sa_entry_destroy(alloc_spi
);
1713 this->mutex
->unlock(this->mutex
);
1716 memset(&request
, 0, sizeof(request
));
1718 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%d}", ntohl(spi
), reqid
);
1720 msg
= (struct sadb_msg
*)request
;
1721 msg
->sadb_msg_version
= PF_KEY_V2
;
1722 msg
->sadb_msg_type
= SADB_ADD
;
1723 msg
->sadb_msg_satype
= proto2satype(protocol
);
1724 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1726 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1727 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1728 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1729 sa
->sadb_sa_spi
= spi
;
1730 sa
->sadb_sa_state
= SADB_SASTATE_MATURE
;
1731 sa
->sadb_sa_replay
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
1732 sa
->sadb_sa_auth
= lookup_algorithm(INTEGRITY_ALGORITHM
, int_alg
);
1733 sa
->sadb_sa_encrypt
= lookup_algorithm(ENCRYPTION_ALGORITHM
, enc_alg
);
1734 PFKEY_EXT_ADD(msg
, sa
);
1736 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
);
1737 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
);
1739 if (enc_alg
!= ENCR_UNDEFINED
)
1741 if (!sa
->sadb_sa_encrypt
)
1743 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1744 encryption_algorithm_names
, enc_alg
);
1747 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1748 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1750 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1751 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
1752 key
->sadb_key_bits
= enc_key
.len
* 8;
1753 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + enc_key
.len
);
1754 memcpy(key
+ 1, enc_key
.ptr
, enc_key
.len
);
1756 PFKEY_EXT_ADD(msg
, key
);
1759 if (int_alg
!= AUTH_UNDEFINED
)
1761 if (!sa
->sadb_sa_auth
)
1763 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1764 integrity_algorithm_names
, int_alg
);
1767 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1768 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1770 key
= (struct sadb_key
*)PFKEY_EXT_ADD_NEXT(msg
);
1771 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
1772 key
->sadb_key_bits
= int_key
.len
* 8;
1773 key
->sadb_key_len
= PFKEY_LEN(sizeof(struct sadb_key
) + int_key
.len
);
1774 memcpy(key
+ 1, int_key
.ptr
, int_key
.len
);
1776 PFKEY_EXT_ADD(msg
, key
);
1779 if (ipcomp
!= IPCOMP_NONE
)
1786 add_encap_ext(msg
, src
, dst
, FALSE
);
1789 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1791 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1794 else if (out
->sadb_msg_errno
)
1796 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x: %s (%d)",
1797 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1803 /* for tunnel mode SAs we have to install an additional IPIP SA and
1804 * group the two SAs together */
1805 if (mode
== MODE_TUNNEL
)
1807 if (add_ipip_sa(this, src
, dst
, spi
, reqid
) != SUCCESS
||
1808 group_ipip_sa(this, src
, dst
, spi
, protocol
, reqid
) != SUCCESS
)
1810 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1815 this->mutex
->lock(this->mutex
);
1816 /* we cache this SA for two reasons:
1817 * - in case an SADB_X_NAT_T_MAPPING_NEW event occurs (we need to find the reqid then)
1818 * - to decide if an expired SA is still installed */
1819 this->installed_sas
->insert_last(this->installed_sas
,
1820 create_sa_entry(protocol
, spi
, reqid
, src
, dst
, encap
, inbound
));
1821 this->mutex
->unlock(this->mutex
);
1823 /* Although KLIPS supports SADB_EXT_LIFETIME_SOFT/HARD, we handle the lifetime
1824 * of SAs manually in the plugin. Refer to the comments in receive_events()
1826 if (lifetime
->time
.rekey
)
1828 schedule_expire(this, protocol
, spi
, reqid
, EXPIRE_TYPE_SOFT
, lifetime
->time
.rekey
);
1831 if (lifetime
->time
.life
)
1833 schedule_expire(this, protocol
, spi
, reqid
, EXPIRE_TYPE_HARD
, lifetime
->time
.life
);
1839 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1840 private_kernel_klips_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1841 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1842 bool encap
, bool new_encap
, mark_t mark
)
1844 unsigned char request
[PFKEY_BUFFER_SIZE
];
1845 struct sadb_msg
*msg
, *out
;
1849 /* we can't update the SA if any of the ip addresses have changed.
1850 * that's because we can't use SADB_UPDATE and by deleting and readding the
1851 * SA the sequence numbers would get lost */
1852 if (!src
->ip_equals(src
, new_src
) ||
1853 !dst
->ip_equals(dst
, new_dst
))
1855 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: address changes"
1856 " are not supported", ntohl(spi
));
1857 return NOT_SUPPORTED
;
1860 /* because KLIPS does not allow us to change the NAT-T type in an SADB_UPDATE,
1861 * we can't update the SA if the encap flag has changed since installing it */
1862 if (encap
!= new_encap
)
1864 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: change of UDP"
1865 " encapsulation is not supported", ntohl(spi
));
1866 return NOT_SUPPORTED
;
1869 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1870 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1872 memset(&request
, 0, sizeof(request
));
1874 msg
= (struct sadb_msg
*)request
;
1875 msg
->sadb_msg_version
= PF_KEY_V2
;
1876 msg
->sadb_msg_type
= SADB_UPDATE
;
1877 msg
->sadb_msg_satype
= proto2satype(protocol
);
1878 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1880 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1881 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1882 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1883 sa
->sadb_sa_spi
= spi
;
1884 sa
->sadb_sa_encrypt
= SADB_EALG_AESCBC
; /* ignored */
1885 sa
->sadb_sa_auth
= SADB_AALG_SHA1HMAC
; /* ignored */
1886 sa
->sadb_sa_state
= SADB_SASTATE_MATURE
;
1887 PFKEY_EXT_ADD(msg
, sa
);
1889 add_addr_ext(msg
, src
, SADB_EXT_ADDRESS_SRC
);
1890 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
);
1892 add_encap_ext(msg
, new_src
, new_dst
, TRUE
);
1894 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1896 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1899 else if (out
->sadb_msg_errno
)
1901 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x: %s (%d)",
1902 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1911 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1912 private_kernel_klips_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1913 u_int32_t spi
, u_int8_t protocol
, mark_t mark
,
1914 u_int64_t
*bytes
, u_int64_t
*packets
)
1916 return NOT_SUPPORTED
; /* TODO */
1919 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1920 private_kernel_klips_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1921 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1923 unsigned char request
[PFKEY_BUFFER_SIZE
];
1924 struct sadb_msg
*msg
, *out
;
1926 sa_entry_t
*cached_sa
;
1929 memset(&request
, 0, sizeof(request
));
1931 /* all grouped SAs are automatically deleted by KLIPS as soon as
1932 * one of them is deleted, therefore we delete only the main one */
1933 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1935 this->mutex
->lock(this->mutex
);
1936 /* this should not fail, but we don't care if it does, let the kernel decide
1937 * whether this SA exists or not */
1938 if (this->installed_sas
->find_first(this->installed_sas
,
1939 (linked_list_match_t
)sa_entry_match_bydst
, (void**)&cached_sa
,
1940 &protocol
, &spi
, dst
) == SUCCESS
)
1942 this->installed_sas
->remove(this->installed_sas
, cached_sa
, NULL
);
1943 sa_entry_destroy(cached_sa
);
1945 this->mutex
->unlock(this->mutex
);
1947 msg
= (struct sadb_msg
*)request
;
1948 msg
->sadb_msg_version
= PF_KEY_V2
;
1949 msg
->sadb_msg_type
= SADB_DELETE
;
1950 msg
->sadb_msg_satype
= proto2satype(protocol
);
1951 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
1953 sa
= (struct sadb_sa
*)PFKEY_EXT_ADD_NEXT(msg
);
1954 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
1955 sa
->sadb_sa_len
= PFKEY_LEN(sizeof(struct sadb_sa
));
1956 sa
->sadb_sa_spi
= spi
;
1957 PFKEY_EXT_ADD(msg
, sa
);
1959 /* the kernel wants an SADB_EXT_ADDRESS_SRC to be present even though
1960 * it is not used for anything. */
1961 add_anyaddr_ext(msg
, dst
->get_family(dst
), SADB_EXT_ADDRESS_SRC
);
1962 add_addr_ext(msg
, dst
, SADB_EXT_ADDRESS_DST
);
1964 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
1966 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1969 else if (out
->sadb_msg_errno
)
1971 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1972 ntohl(spi
), strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
1977 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1982 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
1983 private_kernel_klips_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1984 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
1985 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
1986 mark_t mark
, policy_priority_t priority
)
1988 unsigned char request
[PFKEY_BUFFER_SIZE
];
1989 struct sadb_msg
*msg
, *out
;
1990 policy_entry_t
*policy
, *found
= NULL
;
1995 if (direction
== POLICY_FWD
)
1997 /* no forward policies for KLIPS */
2001 /* tunnel mode policies direct the packets into the pseudo IPIP SA */
2002 satype
= (sa
->mode
== MODE_TUNNEL
) ? SADB_X_SATYPE_IPIP
2003 : proto2satype(sa
->esp
.use ? IPPROTO_ESP
2005 spi
= sa
->esp
.use ? sa
->esp
.spi
: sa
->ah
.spi
;
2007 /* create a policy */
2008 policy
= create_policy_entry(src_ts
, dst_ts
, direction
);
2010 /* find a matching policy */
2011 this->mutex
->lock(this->mutex
);
2012 if (this->policies
->find_first(this->policies
,
2013 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) == SUCCESS
)
2015 /* use existing policy */
2016 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing"
2017 " refcount", src_ts
, dst_ts
,
2018 policy_dir_names
, direction
);
2019 policy_entry_destroy(policy
);
2024 /* apply the new one, if we have no such policy */
2025 this->policies
->insert_last(this->policies
, policy
);
2028 if (priority
== POLICY_PRIORITY_ROUTED
)
2030 /* we install this as a %trap eroute in the kernel, later to be
2031 * triggered by packets matching the policy (-> ACQUIRE). */
2032 spi
= htonl(SPI_TRAP
);
2033 satype
= SADB_X_SATYPE_INT
;
2035 /* the reqid is always set to the latest child SA that trapped this
2036 * policy. we will need this reqid upon receiving an acquire. */
2037 policy
->reqid
= sa
->reqid
;
2039 /* increase the trap counter */
2040 policy
->trapcount
++;
2042 if (policy
->activecount
)
2044 /* we do not replace the current policy in the kernel while a
2045 * policy is actively used */
2046 this->mutex
->unlock(this->mutex
);
2052 /* increase the reference counter */
2053 policy
->activecount
++;
2056 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
2057 policy_dir_names
, direction
);
2059 memset(&request
, 0, sizeof(request
));
2061 msg
= (struct sadb_msg
*)request
;
2063 /* FIXME: SADB_X_SAFLAGS_INFLOW may be required, if we add an inbound policy for an IPIP SA */
2064 build_addflow(msg
, satype
, spi
,
2065 priority
== POLICY_PRIORITY_ROUTED ? NULL
: src
,
2066 priority
== POLICY_PRIORITY_ROUTED ? NULL
: dst
,
2067 policy
->src
.net
, policy
->src
.mask
, policy
->dst
.net
,
2068 policy
->dst
.mask
, policy
->src
.proto
, found
!= NULL
);
2070 this->mutex
->unlock(this->mutex
);
2072 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2074 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
2075 policy_dir_names
, direction
);
2078 else if (out
->sadb_msg_errno
)
2080 DBG1(DBG_KNL
, "unable to add policy %R === %R %N: %s (%d)", src_ts
, dst_ts
,
2081 policy_dir_names
, direction
,
2082 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2088 this->mutex
->lock(this->mutex
);
2090 /* we try to find the policy again and install the route if needed */
2091 if (this->policies
->find_last(this->policies
, NULL
, (void**)&policy
) != SUCCESS
)
2093 this->mutex
->unlock(this->mutex
);
2094 DBG2(DBG_KNL
, "the policy %R === %R %N is already gone, ignoring",
2095 src_ts
, dst_ts
, policy_dir_names
, direction
);
2099 /* KLIPS requires a special route that directs traffic that matches this
2100 * policy to one of the virtual ipsec interfaces. The virtual interface
2101 * has to be attached to the physical one the traffic runs over.
2102 * This is a special case of the source route we install in other kernel
2104 * In the following cases we do NOT install a source route (but just a
2106 * - we are not in tunnel mode
2107 * - we are using IPv6 (does not work correctly yet!)
2108 * - routing is disabled via strongswan.conf
2110 if (policy
->route
== NULL
&& direction
== POLICY_OUT
)
2114 route_entry_t
*route
= malloc_thing(route_entry_t
);
2115 route
->src_ip
= NULL
;
2117 if (sa
->mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
2118 this->install_routes
)
2120 hydra
->kernel_interface
->get_address_by_ts(hydra
->kernel_interface
,
2121 src_ts
, &route
->src_ip
);
2126 route
->src_ip
= host_create_any(src
->get_family(src
));
2129 /* find the virtual interface */
2130 hydra
->kernel_interface
->get_interface(hydra
->kernel_interface
,
2132 if (find_ipsec_dev(this, iface
, &dev
) == SUCCESS
)
2134 /* above, we got either the name of a virtual or a physical
2135 * interface. for both cases it means we already have the devices
2136 * properly attached (assuming that we are exclusively attaching
2137 * ipsec devices). */
2142 /* there is no record of a mapping with the returned interface.
2143 * thus, we attach the first free virtual interface we find to
2144 * it. As above we assume we are the only client fiddling with
2146 if (this->ipsec_devices
->find_first(this->ipsec_devices
,
2147 (linked_list_match_t
)ipsec_dev_match_free
,
2148 (void**)&dev
) == SUCCESS
)
2150 if (attach_ipsec_dev(dev
->name
, iface
) == SUCCESS
)
2152 strncpy(dev
->phys_name
, iface
, IFNAMSIZ
);
2157 DBG1(DBG_KNL
, "failed to attach virtual interface %s"
2158 " to %s", dev
->name
, iface
);
2159 this->mutex
->unlock(this->mutex
);
2166 this->mutex
->unlock(this->mutex
);
2167 DBG1(DBG_KNL
, "failed to attach a virtual interface to %s: no"
2168 " virtual interfaces left", iface
);
2174 route
->if_name
= strdup(dev
->name
);
2176 /* get the nexthop to dst */
2177 route
->gateway
= hydra
->kernel_interface
->get_nexthop(
2178 hydra
->kernel_interface
, dst
, route
->src_ip
);
2179 route
->dst_net
= chunk_clone(policy
->dst
.net
->get_address(policy
->dst
.net
));
2180 route
->prefixlen
= policy
->dst
.mask
;
2182 switch (hydra
->kernel_interface
->add_route(hydra
->kernel_interface
,
2183 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2184 route
->src_ip
, route
->if_name
))
2187 DBG1(DBG_KNL
, "unable to install route for policy %R === %R",
2191 /* route exists, do not uninstall */
2192 route_entry_destroy(route
);
2195 /* cache the installed route */
2196 policy
->route
= route
;
2201 this->mutex
->unlock(this->mutex
);
2206 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2207 private_kernel_klips_ipsec_t
*this, traffic_selector_t
*src_ts
,
2208 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
2209 u_int32_t
*use_time
)
2211 #define IDLE_PREFIX "idle="
2212 static const char *path_eroute
= "/proc/net/ipsec_eroute";
2213 static const char *path_spi
= "/proc/net/ipsec_spi";
2215 char line
[1024], src
[INET6_ADDRSTRLEN
+ 9], dst
[INET6_ADDRSTRLEN
+ 9];
2216 char *said
= NULL
, *pos
;
2217 policy_entry_t
*policy
, *found
= NULL
;
2218 status_t status
= FAILED
;
2220 if (direction
== POLICY_FWD
)
2222 /* we do not install forward policies */
2226 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
2227 policy_dir_names
, direction
);
2229 /* create a policy */
2230 policy
= create_policy_entry(src_ts
, dst_ts
, direction
);
2232 /* find a matching policy */
2233 this->mutex
->lock(this->mutex
);
2234 if (this->policies
->find_first(this->policies
,
2235 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) != SUCCESS
)
2237 this->mutex
->unlock(this->mutex
);
2238 DBG1(DBG_KNL
, "querying policy %R === %R %N failed, not found", src_ts
,
2239 dst_ts
, policy_dir_names
, direction
);
2240 policy_entry_destroy(policy
);
2243 policy_entry_destroy(policy
);
2246 /* src and dst selectors in KLIPS are of the form NET_ADDR/NETBITS:PROTO */
2247 snprintf(src
, sizeof(src
), "%H/%d:%d", policy
->src
.net
, policy
->src
.mask
,
2249 src
[sizeof(src
) - 1] = '\0';
2250 snprintf(dst
, sizeof(dst
), "%H/%d:%d", policy
->dst
.net
, policy
->dst
.mask
,
2252 dst
[sizeof(dst
) - 1] = '\0';
2254 this->mutex
->unlock(this->mutex
);
2256 /* we try to find the matching eroute first */
2257 file
= fopen(path_eroute
, "r");
2260 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: %s (%d)", src_ts
,
2261 dst_ts
, policy_dir_names
, direction
, strerror(errno
), errno
);
2265 /* read line by line where each line looks like:
2266 * packets src -> dst => said */
2267 while (fgets(line
, sizeof(line
), file
))
2269 enumerator_t
*enumerator
;
2273 enumerator
= enumerator_create_token(line
, " \t", " \t\n");
2274 while (enumerator
->enumerate(enumerator
, &token
))
2278 case 0: /* packets */
2281 if (streq(token
, src
))
2289 if (streq(token
, dst
))
2297 said
= strdup(token
);
2302 enumerator
->destroy(enumerator
);
2306 /* eroute matched */
2314 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: found no matching"
2315 " eroute", src_ts
, dst_ts
, policy_dir_names
, direction
);
2319 /* compared with the one in the spi entry the SA ID from the eroute entry
2320 * has an additional ":PROTO" appended, which we need to cut off */
2321 pos
= strrchr(said
, ':');
2324 /* now we try to find the matching spi entry */
2325 file
= fopen(path_spi
, "r");
2328 DBG1(DBG_KNL
, "unable to query policy %R === %R %N: %s (%d)", src_ts
,
2329 dst_ts
, policy_dir_names
, direction
, strerror(errno
), errno
);
2333 while (fgets(line
, sizeof(line
), file
))
2335 if (strneq(line
, said
, strlen(said
)))
2337 /* fine we found the correct line, now find the idle time */
2338 u_int32_t idle_time
;
2339 pos
= strstr(line
, IDLE_PREFIX
);
2342 /* no idle time, i.e. this SA has not been used yet */
2345 if (sscanf(pos
, IDLE_PREFIX
"%u", &idle_time
) <= 0)
2347 /* idle time not valid */
2351 *use_time
= time_monotonic(NULL
) - idle_time
;
2362 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2363 private_kernel_klips_ipsec_t
*this, traffic_selector_t
*src_ts
,
2364 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
2365 mark_t mark
, policy_priority_t priority
)
2367 unsigned char request
[PFKEY_BUFFER_SIZE
];
2368 struct sadb_msg
*msg
= (struct sadb_msg
*)request
, *out
;
2369 policy_entry_t
*policy
, *found
= NULL
;
2370 route_entry_t
*route
;
2373 if (direction
== POLICY_FWD
)
2375 /* no forward policies for KLIPS */
2379 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
2380 policy_dir_names
, direction
);
2382 /* create a policy */
2383 policy
= create_policy_entry(src_ts
, dst_ts
, direction
);
2385 /* find a matching policy */
2386 this->mutex
->lock(this->mutex
);
2387 if (this->policies
->find_first(this->policies
,
2388 (linked_list_match_t
)policy_entry_equals
, (void**)&found
, policy
) != SUCCESS
)
2390 this->mutex
->unlock(this->mutex
);
2391 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
2392 dst_ts
, policy_dir_names
, direction
);
2393 policy_entry_destroy(policy
);
2396 policy_entry_destroy(policy
);
2398 /* decrease appropriate counter */
2399 priority
== POLICY_PRIORITY_ROUTED ? found
->trapcount
--
2400 : found
->activecount
--;
2402 if (found
->trapcount
== 0)
2404 /* if this policy is finally unrouted, we reset the reqid because it
2405 * may still be actively used and there might be a pending acquire for
2410 if (found
->activecount
> 0)
2412 /* is still used by SAs, keep in kernel */
2413 this->mutex
->unlock(this->mutex
);
2414 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2417 else if (found
->activecount
== 0 && found
->trapcount
> 0)
2419 /* for a policy that is not used actively anymore, but is still trapped
2420 * by another child SA we replace the current eroute with a %trap eroute */
2421 DBG2(DBG_KNL
, "policy still routed by another CHILD_SA, not removed");
2422 memset(&request
, 0, sizeof(request
));
2423 build_addflow(msg
, SADB_X_SATYPE_INT
, htonl(SPI_TRAP
), NULL
, NULL
,
2424 found
->src
.net
, found
->src
.mask
, found
->dst
.net
,
2425 found
->dst
.mask
, found
->src
.proto
, TRUE
);
2426 this->mutex
->unlock(this->mutex
);
2427 return pfkey_send_ack(this, msg
);
2430 /* remove if last reference */
2431 this->policies
->remove(this->policies
, found
, NULL
);
2434 this->mutex
->unlock(this->mutex
);
2436 memset(&request
, 0, sizeof(request
));
2438 build_delflow(msg
, 0, policy
->src
.net
, policy
->src
.mask
, policy
->dst
.net
,
2439 policy
->dst
.mask
, policy
->src
.proto
);
2441 route
= policy
->route
;
2442 policy
->route
= NULL
;
2443 policy_entry_destroy(policy
);
2445 if (pfkey_send(this, msg
, &out
, &len
) != SUCCESS
)
2447 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
2448 policy_dir_names
, direction
);
2451 else if (out
->sadb_msg_errno
)
2453 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N: %s (%d)", src_ts
,
2454 dst_ts
, policy_dir_names
, direction
,
2455 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2465 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2466 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2467 route
->src_ip
, route
->if_name
) != SUCCESS
)
2469 DBG1(DBG_KNL
, "error uninstalling route installed with"
2470 " policy %R === %R %N", src_ts
, dst_ts
,
2471 policy_dir_names
, direction
);
2474 /* we have to detach the ipsec interface from the physical one over which
2475 * this SA ran (if it is not used by any other) */
2476 this->mutex
->lock(this->mutex
);
2478 if (find_ipsec_dev(this, route
->if_name
, &dev
) == SUCCESS
)
2480 /* fine, we found a matching device object, let's check if we have
2482 if (--dev
->refcount
== 0)
2484 if (detach_ipsec_dev(dev
->name
, dev
->phys_name
) != SUCCESS
)
2486 DBG1(DBG_KNL
, "failed to detach virtual interface %s"
2487 " from %s", dev
->name
, dev
->phys_name
);
2489 dev
->phys_name
[0] = '\0';
2493 this->mutex
->unlock(this->mutex
);
2495 route_entry_destroy(route
);
2502 * Initialize the list of ipsec devices
2504 static void init_ipsec_devices(private_kernel_klips_ipsec_t
*this)
2506 int i
, count
= lib
->settings
->get_int(lib
->settings
,
2507 "%s.plugins.kernel-klips.ipsec_dev_count",
2508 DEFAULT_IPSEC_DEV_COUNT
, hydra
->daemon
);
2510 for (i
= 0; i
< count
; ++i
)
2512 ipsec_dev_t
*dev
= malloc_thing(ipsec_dev_t
);
2513 snprintf(dev
->name
, IFNAMSIZ
, IPSEC_DEV_PREFIX
"%d", i
);
2514 dev
->name
[IFNAMSIZ
- 1] = '\0';
2515 dev
->phys_name
[0] = '\0';
2517 this->ipsec_devices
->insert_last(this->ipsec_devices
, dev
);
2519 /* detach any previously attached ipsec device */
2520 detach_ipsec_dev(dev
->name
, dev
->phys_name
);
2525 * Register a socket for ACQUIRE/EXPIRE messages
2527 static status_t
register_pfkey_socket(private_kernel_klips_ipsec_t
*this, u_int8_t satype
)
2529 unsigned char request
[PFKEY_BUFFER_SIZE
];
2530 struct sadb_msg
*msg
, *out
;
2533 memset(&request
, 0, sizeof(request
));
2535 msg
= (struct sadb_msg
*)request
;
2536 msg
->sadb_msg_version
= PF_KEY_V2
;
2537 msg
->sadb_msg_type
= SADB_REGISTER
;
2538 msg
->sadb_msg_satype
= satype
;
2539 msg
->sadb_msg_len
= PFKEY_LEN(sizeof(struct sadb_msg
));
2541 if (pfkey_send_socket(this, this->socket_events
, msg
, &out
, &len
) != SUCCESS
)
2543 DBG1(DBG_KNL
, "unable to register PF_KEY socket");
2546 else if (out
->sadb_msg_errno
)
2548 DBG1(DBG_KNL
, "unable to register PF_KEY socket: %s (%d)",
2549 strerror(out
->sadb_msg_errno
), out
->sadb_msg_errno
);
2557 METHOD(kernel_ipsec_t
, destroy
, void,
2558 private_kernel_klips_ipsec_t
*this)
2560 if (this->socket
> 0)
2562 close(this->socket
);
2564 if (this->socket_events
> 0)
2566 close(this->socket_events
);
2568 this->mutex_pfkey
->destroy(this->mutex_pfkey
);
2569 this->mutex
->destroy(this->mutex
);
2570 this->ipsec_devices
->destroy_function(this->ipsec_devices
, (void*)ipsec_dev_destroy
);
2571 this->installed_sas
->destroy_function(this->installed_sas
, (void*)sa_entry_destroy
);
2572 this->allocated_spis
->destroy_function(this->allocated_spis
, (void*)sa_entry_destroy
);
2573 this->policies
->destroy_function(this->policies
, (void*)policy_entry_destroy
);
2578 * Described in header.
2580 kernel_klips_ipsec_t
*kernel_klips_ipsec_create()
2582 private_kernel_klips_ipsec_t
*this;
2587 .get_spi
= _get_spi
,
2588 .get_cpi
= _get_cpi
,
2590 .update_sa
= _update_sa
,
2591 .query_sa
= _query_sa
,
2593 .flush_sas
= (void*)return_failed
,
2594 .add_policy
= _add_policy
,
2595 .query_policy
= _query_policy
,
2596 .del_policy
= _del_policy
,
2597 .flush_policies
= (void*)return_failed
,
2598 /* KLIPS does not need a bypass policy for IKE */
2599 .bypass_socket
= (void*)return_true
,
2600 /* KLIPS does not need enabling UDP decap explicitly */
2601 .enable_udp_decap
= (void*)return_true
,
2602 .destroy
= _destroy
,
2605 .policies
= linked_list_create(),
2606 .allocated_spis
= linked_list_create(),
2607 .installed_sas
= linked_list_create(),
2608 .ipsec_devices
= linked_list_create(),
2609 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2610 .mutex_pfkey
= mutex_create(MUTEX_TYPE_DEFAULT
),
2611 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2612 "%s.install_routes", TRUE
,
2616 /* initialize ipsec devices */
2617 init_ipsec_devices(this);
2619 /* create a PF_KEY socket to communicate with the kernel */
2620 this->socket
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
2621 if (this->socket
<= 0)
2623 DBG1(DBG_KNL
, "unable to create PF_KEY socket");
2628 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
2629 this->socket_events
= socket(PF_KEY
, SOCK_RAW
, PF_KEY_V2
);
2630 if (this->socket_events
<= 0)
2632 DBG1(DBG_KNL
, "unable to create PF_KEY event socket");
2637 /* register the event socket */
2638 if (register_pfkey_socket(this, SADB_SATYPE_ESP
) != SUCCESS
||
2639 register_pfkey_socket(this, SADB_SATYPE_AH
) != SUCCESS
)
2641 DBG1(DBG_KNL
, "unable to register PF_KEY event socket");
2646 lib
->processor
->queue_job(lib
->processor
,
2647 (job_t
*)callback_job_create_with_prio((callback_job_cb_t
)receive_events
,
2648 this, NULL
, (callback_job_cancel_t
)return_false
, JOB_PRIO_CRITICAL
));
2650 return &this->public;