2 * Copyright (C) 2006-2011 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2008 Andreas Steffen
5 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
6 * Copyright (C) 2006 Daniel Roethlisberger
7 * Copyright (C) 2005 Jan Hutter
8 * Hochschule fuer Technik Rapperswil
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <linux/ipsec.h>
25 #include <linux/netlink.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/xfrm.h>
28 #include <linux/udp.h>
35 #include "kernel_netlink_ipsec.h"
36 #include "kernel_netlink_shared.h"
40 #include <threading/thread.h>
41 #include <threading/mutex.h>
42 #include <utils/hashtable.h>
43 #include <utils/linked_list.h>
44 #include <processing/jobs/callback_job.h>
46 /** Required for Linux 2.6.26 kernel and later */
47 #ifndef XFRM_STATE_AF_UNSPEC
48 #define XFRM_STATE_AF_UNSPEC 32
51 /** From linux/in.h */
52 #ifndef IP_XFRM_POLICY
53 #define IP_XFRM_POLICY 17
56 /** Missing on uclibc */
57 #ifndef IPV6_XFRM_POLICY
58 #define IPV6_XFRM_POLICY 34
59 #endif /*IPV6_XFRM_POLICY*/
61 /** Default priority of installed policies */
64 /** Default replay window size, if not set using charon.replay_window */
65 #define DEFAULT_REPLAY_WINDOW 32
68 * Map the limit for bytes and packets to XFRM_INF by default
70 #define XFRM_LIMIT(x) ((x) == 0 ? XFRM_INF : (x))
73 * Create ORable bitfield of XFRM NL groups
75 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
78 * Returns a pointer to the first rtattr following the nlmsghdr *nlh and the
79 * 'usual' netlink data x like 'struct xfrm_usersa_info'
81 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + \
82 NLMSG_ALIGN(sizeof(x))))
84 * Returns a pointer to the next rtattr following rta.
85 * !!! Do not use this to parse messages. Use RTA_NEXT and RTA_OK instead !!!
87 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + \
88 RTA_ALIGN((rta)->rta_len)))
90 * Returns the total size of attached rta data
91 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
93 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
95 typedef struct kernel_algorithm_t kernel_algorithm_t
;
98 * Mapping of IKEv2 kernel identifier to linux crypto API names
100 struct kernel_algorithm_t
{
102 * Identifier specified in IKEv2
107 * Name of the algorithm in linux crypto API
112 ENUM(xfrm_msg_names
, XFRM_MSG_NEWSA
, XFRM_MSG_MAPPING
,
116 "XFRM_MSG_NEWPOLICY",
117 "XFRM_MSG_DELPOLICY",
118 "XFRM_MSG_GETPOLICY",
122 "XFRM_MSG_UPDPOLICY",
124 "XFRM_MSG_POLEXPIRE",
126 "XFRM_MSG_FLUSHPOLICY",
131 "XFRM_MSG_NEWSADINFO",
132 "XFRM_MSG_GETSADINFO",
133 "XFRM_MSG_NEWSPDINFO",
134 "XFRM_MSG_GETSPDINFO",
138 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_KMADDRESS
,
150 "XFRMA_REPLAY_THRESH",
151 "XFRMA_ETIMER_THRESH",
161 #define END_OF_LIST -1
164 * Algorithms for encryption
166 static kernel_algorithm_t encryption_algs
[] = {
167 /* {ENCR_DES_IV64, "***" }, */
169 {ENCR_3DES
, "des3_ede" },
170 /* {ENCR_RC5, "***" }, */
171 /* {ENCR_IDEA, "***" }, */
172 {ENCR_CAST
, "cast128" },
173 {ENCR_BLOWFISH
, "blowfish" },
174 /* {ENCR_3IDEA, "***" }, */
175 /* {ENCR_DES_IV32, "***" }, */
176 {ENCR_NULL
, "cipher_null" },
177 {ENCR_AES_CBC
, "aes" },
178 {ENCR_AES_CTR
, "rfc3686(ctr(aes))" },
179 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
180 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
181 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
182 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
183 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
184 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
185 {ENCR_NULL_AUTH_AES_GMAC
, "rfc4543(gcm(aes))" },
186 {ENCR_CAMELLIA_CBC
, "cbc(camellia)" },
187 /* {ENCR_CAMELLIA_CTR, "***" }, */
188 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
189 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
190 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
191 {ENCR_SERPENT_CBC
, "serpent" },
192 {ENCR_TWOFISH_CBC
, "twofish" },
197 * Algorithms for integrity protection
199 static kernel_algorithm_t integrity_algs
[] = {
200 {AUTH_HMAC_MD5_96
, "md5" },
201 {AUTH_HMAC_SHA1_96
, "sha1" },
202 {AUTH_HMAC_SHA2_256_96
, "sha256" },
203 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
204 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
205 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
206 /* {AUTH_DES_MAC, "***" }, */
207 /* {AUTH_KPDK_MD5, "***" }, */
208 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
213 * Algorithms for IPComp
215 static kernel_algorithm_t compression_algs
[] = {
216 /* {IPCOMP_OUI, "***" }, */
217 {IPCOMP_DEFLATE
, "deflate" },
218 {IPCOMP_LZS
, "lzs" },
219 {IPCOMP_LZJH
, "lzjh" },
224 * Look up a kernel algorithm name and its key size
226 static char* lookup_algorithm(kernel_algorithm_t
*list
, int ikev2
)
228 while (list
->ikev2
!= END_OF_LIST
)
230 if (list
->ikev2
== ikev2
)
239 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
242 * Private variables and functions of kernel_netlink class.
244 struct private_kernel_netlink_ipsec_t
{
246 * Public part of the kernel_netlink_t object
248 kernel_netlink_ipsec_t
public;
251 * Mutex to lock access to installed policies
256 * Hash table of installed policies (policy_entry_t)
258 hashtable_t
*policies
;
261 * Hash table of IPsec SAs using policies (ipsec_sa_t)
266 * Job receiving netlink events
271 * Netlink xfrm socket (IPsec)
273 netlink_socket_t
*socket_xfrm
;
276 * Netlink xfrm socket to receive acquire and expire events
278 int socket_xfrm_events
;
281 * Whether to install routes along policies
286 * Whether to track the history of a policy
291 * Size of the replay window, in packets
293 u_int32_t replay_window
;
296 * Size of the replay window bitmap, in bytes
298 u_int32_t replay_bmp
;
301 typedef struct route_entry_t route_entry_t
;
304 * Installed routing entry
306 struct route_entry_t
{
307 /** Name of the interface the route is bound to */
310 /** Source ip of the route */
313 /** Gateway for this route */
316 /** Destination net */
319 /** Destination net prefixlen */
324 * Destroy a route_entry_t object
326 static void route_entry_destroy(route_entry_t
*this)
329 this->src_ip
->destroy(this->src_ip
);
330 DESTROY_IF(this->gateway
);
331 chunk_free(&this->dst_net
);
336 * Compare two route_entry_t objects
338 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
340 return a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
341 a
->src_ip
->equals(a
->src_ip
, b
->src_ip
) &&
342 a
->gateway
->equals(a
->gateway
, b
->gateway
) &&
343 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
;
346 typedef struct ipsec_sa_t ipsec_sa_t
;
349 * IPsec SA assigned to a policy.
352 /** Source address of this SA */
355 /** Destination address of this SA */
361 /** Description of this SA */
364 /** Reference count for this SA */
369 * Hash function for ipsec_sa_t objects
371 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
373 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
374 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
375 chunk_hash_inc(chunk_from_thing(sa
->mark
),
376 chunk_hash(chunk_from_thing(sa
->cfg
)))));
380 * Equality function for ipsec_sa_t objects
382 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
384 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
385 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
386 memeq(&sa
->mark
, &other_sa
->mark
, sizeof(mark_t
)) &&
387 memeq(&sa
->cfg
, &other_sa
->cfg
, sizeof(ipsec_sa_cfg_t
));
391 * Allocate or reference an IPsec SA object
393 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
394 host_t
*src
, host_t
*dst
, mark_t mark
,
397 ipsec_sa_t
*sa
, *found
;
404 found
= this->sas
->get(this->sas
, sa
);
407 sa
->src
= src
->clone(src
);
408 sa
->dst
= dst
->clone(dst
);
409 this->sas
->put(this->sas
, sa
, sa
);
416 ref_get(&sa
->refcount
);
421 * Release and destroy an IPsec SA object
423 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
426 if (ref_put(&sa
->refcount
))
428 this->sas
->remove(this->sas
, sa
);
435 typedef struct policy_sa_t policy_sa_t
;
436 typedef struct policy_sa_fwd_t policy_sa_fwd_t
;
439 * Mapping between a policy and an IPsec SA.
442 /** Priority assigned to the policy when installed with this SA */
445 /** Type of the policy */
453 * For forward policies we also cache the traffic selectors in order to install
456 struct policy_sa_fwd_t
{
457 /** Generic interface */
460 /** Source traffic selector of this policy */
461 traffic_selector_t
*src_ts
;
463 /** Destination traffic selector of this policy */
464 traffic_selector_t
*dst_ts
;
468 * Create a policy_sa(_fwd)_t object
470 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
471 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
472 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
477 if (dir
== POLICY_FWD
)
479 policy_sa_fwd_t
*fwd
;
481 .src_ts
= src_ts
->clone(src_ts
),
482 .dst_ts
= dst_ts
->clone(dst_ts
),
484 policy
= &fwd
->generic
;
488 INIT(policy
, .priority
= 0);
491 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, cfg
);
496 * Destroy a policy_sa(_fwd)_t object
498 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t
*dir
,
499 private_kernel_netlink_ipsec_t
*this)
501 if (*dir
== POLICY_FWD
)
503 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)policy
;
504 fwd
->src_ts
->destroy(fwd
->src_ts
);
505 fwd
->dst_ts
->destroy(fwd
->dst_ts
);
507 ipsec_sa_destroy(this, policy
->sa
);
511 typedef struct policy_entry_t policy_entry_t
;
514 * Installed kernel policy.
516 struct policy_entry_t
{
518 /** Direction of this policy: in, out, forward */
521 /** Parameters of installed policy */
522 struct xfrm_selector sel
;
527 /** Associated route installed for this policy */
528 route_entry_t
*route
;
530 /** List of SAs this policy is used by, ordered by priority */
531 linked_list_t
*used_by
;
535 * Destroy a policy_entry_t object
537 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
538 policy_entry_t
*policy
)
542 route_entry_destroy(policy
->route
);
546 policy
->used_by
->invoke_function(policy
->used_by
,
547 (linked_list_invoke_t
)policy_sa_destroy
,
548 &policy
->direction
, this);
549 policy
->used_by
->destroy(policy
->used_by
);
555 * Hash function for policy_entry_t objects
557 static u_int
policy_hash(policy_entry_t
*key
)
559 chunk_t chunk
= chunk_create((void*)&key
->sel
,
560 sizeof(struct xfrm_selector
) + sizeof(u_int32_t
));
561 return chunk_hash(chunk
);
565 * Equality function for policy_entry_t objects
567 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
569 return memeq(&key
->sel
, &other_key
->sel
,
570 sizeof(struct xfrm_selector
) + sizeof(u_int32_t
)) &&
571 key
->direction
== other_key
->direction
;
575 * Calculate the priority of a policy
577 static inline u_int32_t
get_priority(policy_entry_t
*policy
,
578 policy_priority_t prio
)
580 u_int32_t priority
= PRIO_BASE
;
583 case POLICY_PRIORITY_FALLBACK
:
586 case POLICY_PRIORITY_ROUTED
:
589 case POLICY_PRIORITY_DEFAULT
:
592 /* calculate priority based on selector size, small size = high prio */
593 priority
-= policy
->sel
.prefixlen_s
;
594 priority
-= policy
->sel
.prefixlen_d
;
595 priority
<<= 2; /* make some room for the two flags */
596 priority
+= policy
->sel
.sport_mask
|| policy
->sel
.dport_mask ?
0 : 2;
597 priority
+= policy
->sel
.proto ?
0 : 1;
602 * Convert the general ipsec mode to the one defined in xfrm.h
604 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
609 return XFRM_MODE_TRANSPORT
;
611 return XFRM_MODE_TUNNEL
;
613 return XFRM_MODE_BEET
;
620 * Convert a host_t to a struct xfrm_address
622 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
624 chunk_t chunk
= host
->get_address(host
);
625 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
629 * Convert a struct xfrm_address to a host_t
631 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
638 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
641 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
646 return host_create_from_chunk(family
, chunk
, ntohs(port
));
650 * Convert a traffic selector address range to subnet and its mask.
652 static void ts2subnet(traffic_selector_t
* ts
,
653 xfrm_address_t
*net
, u_int8_t
*mask
)
658 ts
->to_subnet(ts
, &net_host
, mask
);
659 net_chunk
= net_host
->get_address(net_host
);
660 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
661 net_host
->destroy(net_host
);
665 * Convert a traffic selector port range to port/portmask
667 static void ts2ports(traffic_selector_t
* ts
,
668 u_int16_t
*port
, u_int16_t
*mask
)
670 /* Linux does not seem to accept complex portmasks. Only
671 * any or a specific port is allowed. We set to any, if we have
672 * a port range, or to a specific, if we have one port only.
676 from
= ts
->get_from_port(ts
);
677 to
= ts
->get_to_port(ts
);
692 * Convert a pair of traffic_selectors to an xfrm_selector
694 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
695 traffic_selector_t
*dst
)
697 struct xfrm_selector sel
;
699 memset(&sel
, 0, sizeof(sel
));
700 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
701 /* src or dest proto may be "any" (0), use more restrictive one */
702 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
703 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
704 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
705 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
706 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
714 * Convert an xfrm_selector to a src|dst traffic_selector
716 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
725 addr
= (u_char
*)&sel
->saddr
;
726 prefixlen
= sel
->prefixlen_s
;
729 port
= htons(sel
->sport
);
734 addr
= (u_char
*)&sel
->daddr
;
735 prefixlen
= sel
->prefixlen_d
;
738 port
= htons(sel
->dport
);
742 /* The Linux 2.6 kernel does not set the selector's family field,
743 * so as a kludge we additionally test the prefix length.
745 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
747 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
749 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
751 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
756 return traffic_selector_create_from_subnet(host
, prefixlen
,
763 * Process a XFRM_MSG_ACQUIRE from kernel
765 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
766 struct nlmsghdr
*hdr
)
768 struct xfrm_user_acquire
*acquire
;
771 traffic_selector_t
*src_ts
, *dst_ts
;
775 acquire
= (struct xfrm_user_acquire
*)NLMSG_DATA(hdr
);
776 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
777 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
779 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
781 while (RTA_OK(rta
, rtasize
))
783 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
785 if (rta
->rta_type
== XFRMA_TMPL
)
787 struct xfrm_user_tmpl
* tmpl
;
788 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
790 proto
= tmpl
->id
.proto
;
792 rta
= RTA_NEXT(rta
, rtasize
);
801 /* acquire for AH/ESP only, not for IPCOMP */
804 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
805 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
807 hydra
->kernel_interface
->acquire(hydra
->kernel_interface
, reqid
, src_ts
,
812 * Process a XFRM_MSG_EXPIRE from kernel
814 static void process_expire(private_kernel_netlink_ipsec_t
*this,
815 struct nlmsghdr
*hdr
)
817 struct xfrm_user_expire
*expire
;
818 u_int32_t spi
, reqid
;
821 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
822 protocol
= expire
->state
.id
.proto
;
823 spi
= expire
->state
.id
.spi
;
824 reqid
= expire
->state
.reqid
;
826 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
828 if (protocol
!= IPPROTO_ESP
&& protocol
!= IPPROTO_AH
)
830 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and "
831 "reqid {%u} which is not a CHILD_SA", ntohl(spi
), reqid
);
835 hydra
->kernel_interface
->expire(hydra
->kernel_interface
, reqid
, protocol
,
836 spi
, expire
->hard
!= 0);
840 * Process a XFRM_MSG_MIGRATE from kernel
842 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
843 struct nlmsghdr
*hdr
)
845 struct xfrm_userpolicy_id
*policy_id
;
848 traffic_selector_t
*src_ts
, *dst_ts
;
849 host_t
*local
= NULL
, *remote
= NULL
;
850 host_t
*old_src
= NULL
, *old_dst
= NULL
;
851 host_t
*new_src
= NULL
, *new_dst
= NULL
;
855 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
856 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
857 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
859 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
861 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
862 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
863 dir
= (policy_dir_t
)policy_id
->dir
;
865 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
867 while (RTA_OK(rta
, rtasize
))
869 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
870 if (rta
->rta_type
== XFRMA_KMADDRESS
)
872 struct xfrm_user_kmaddress
*kmaddress
;
874 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
875 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
876 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
877 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
879 else if (rta
->rta_type
== XFRMA_MIGRATE
)
881 struct xfrm_user_migrate
*migrate
;
883 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
884 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
885 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
886 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
887 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
888 reqid
= migrate
->reqid
;
889 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
890 old_src
, old_dst
, new_src
, new_dst
, reqid
);
896 rta
= RTA_NEXT(rta
, rtasize
);
899 if (src_ts
&& dst_ts
&& local
&& remote
)
901 hydra
->kernel_interface
->migrate(hydra
->kernel_interface
, reqid
,
902 src_ts
, dst_ts
, dir
, local
, remote
);
914 * Process a XFRM_MSG_MAPPING from kernel
916 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
917 struct nlmsghdr
*hdr
)
919 struct xfrm_user_mapping
*mapping
;
920 u_int32_t spi
, reqid
;
922 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
923 spi
= mapping
->id
.spi
;
924 reqid
= mapping
->reqid
;
926 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
928 if (mapping
->id
.proto
== IPPROTO_ESP
)
931 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
935 hydra
->kernel_interface
->mapping(hydra
->kernel_interface
, reqid
,
942 * Receives events from kernel
944 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
947 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
948 struct sockaddr_nl addr
;
949 socklen_t addr_len
= sizeof(addr
);
953 oldstate
= thread_cancelability(TRUE
);
954 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
955 (struct sockaddr
*)&addr
, &addr_len
);
956 thread_cancelability(oldstate
);
963 /* interrupted, try again */
964 return JOB_REQUEUE_DIRECT
;
966 /* no data ready, select again */
967 return JOB_REQUEUE_DIRECT
;
969 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
971 return JOB_REQUEUE_FAIR
;
975 if (addr
.nl_pid
!= 0)
976 { /* not from kernel. not interested, try another one */
977 return JOB_REQUEUE_DIRECT
;
980 while (NLMSG_OK(hdr
, len
))
982 switch (hdr
->nlmsg_type
)
984 case XFRM_MSG_ACQUIRE
:
985 process_acquire(this, hdr
);
987 case XFRM_MSG_EXPIRE
:
988 process_expire(this, hdr
);
990 case XFRM_MSG_MIGRATE
:
991 process_migrate(this, hdr
);
993 case XFRM_MSG_MAPPING
:
994 process_mapping(this, hdr
);
997 DBG1(DBG_KNL
, "received unknown event from xfrm event "
998 "socket: %d", hdr
->nlmsg_type
);
1001 hdr
= NLMSG_NEXT(hdr
, len
);
1003 return JOB_REQUEUE_DIRECT
;
1007 * Get an SPI for a specific protocol from the kernel.
1009 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
1010 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
1011 u_int32_t reqid
, u_int32_t
*spi
)
1013 netlink_buf_t request
;
1014 struct nlmsghdr
*hdr
, *out
;
1015 struct xfrm_userspi_info
*userspi
;
1016 u_int32_t received_spi
= 0;
1019 memset(&request
, 0, sizeof(request
));
1021 hdr
= (struct nlmsghdr
*)request
;
1022 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1023 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1024 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1026 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1027 host2xfrm(src
, &userspi
->info
.saddr
);
1028 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1029 userspi
->info
.id
.proto
= proto
;
1030 userspi
->info
.mode
= XFRM_MODE_TUNNEL
;
1031 userspi
->info
.reqid
= reqid
;
1032 userspi
->info
.family
= src
->get_family(src
);
1036 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1039 while (NLMSG_OK(hdr
, len
))
1041 switch (hdr
->nlmsg_type
)
1043 case XFRM_MSG_NEWSA
:
1045 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1046 received_spi
= usersa
->id
.spi
;
1051 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1052 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1053 strerror(-err
->error
), -err
->error
);
1057 hdr
= NLMSG_NEXT(hdr
, len
);
1067 if (received_spi
== 0)
1072 *spi
= received_spi
;
1076 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1077 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1078 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
1080 DBG2(DBG_KNL
, "getting SPI for reqid {%u}", reqid
);
1082 if (get_spi_internal(this, src
, dst
, protocol
,
1083 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
1085 DBG1(DBG_KNL
, "unable to get SPI for reqid {%u}", reqid
);
1089 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%u}", ntohl(*spi
), reqid
);
1093 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1094 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1095 u_int32_t reqid
, u_int16_t
*cpi
)
1097 u_int32_t received_spi
= 0;
1099 DBG2(DBG_KNL
, "getting CPI for reqid {%u}", reqid
);
1101 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1102 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
1104 DBG1(DBG_KNL
, "unable to get CPI for reqid {%u}", reqid
);
1108 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
1110 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%u}", ntohs(*cpi
), reqid
);
1114 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1115 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1116 u_int32_t spi
, u_int8_t protocol
, u_int32_t reqid
, mark_t mark
,
1117 u_int32_t tfc
, lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
1118 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
, u_int16_t ipcomp
,
1119 u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
1120 traffic_selector_t
* src_ts
, traffic_selector_t
* dst_ts
)
1122 netlink_buf_t request
;
1124 struct nlmsghdr
*hdr
;
1125 struct xfrm_usersa_info
*sa
;
1126 u_int16_t icv_size
= 64;
1127 status_t status
= FAILED
;
1129 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1130 * we are in the recursive call below */
1131 if (ipcomp
!= IPCOMP_NONE
&& cpi
!= 0)
1133 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1134 add_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, reqid
, mark
,
1135 tfc
, &lft
, ENCR_UNDEFINED
, chunk_empty
, AUTH_UNDEFINED
,
1136 chunk_empty
, mode
, ipcomp
, 0, FALSE
, FALSE
, inbound
, NULL
, NULL
);
1137 ipcomp
= IPCOMP_NONE
;
1138 /* use transport mode ESP SA, IPComp uses tunnel mode */
1139 mode
= MODE_TRANSPORT
;
1142 memset(&request
, 0, sizeof(request
));
1146 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u} (mark "
1147 "%u/0x%8x)", ntohl(spi
), reqid
, mark
.value
, mark
.mask
);
1151 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}",
1154 hdr
= (struct nlmsghdr
*)request
;
1155 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1156 hdr
->nlmsg_type
= inbound ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1157 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1159 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1160 host2xfrm(src
, &sa
->saddr
);
1161 host2xfrm(dst
, &sa
->id
.daddr
);
1163 sa
->id
.proto
= protocol
;
1164 sa
->family
= src
->get_family(src
);
1165 sa
->mode
= mode2kernel(mode
);
1169 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1172 case MODE_TRANSPORT
:
1173 if(src_ts
&& dst_ts
)
1175 sa
->sel
= ts2selector(src_ts
, dst_ts
);
1183 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.rekey
);
1184 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.life
);
1185 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(lifetime
->packets
.rekey
);
1186 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(lifetime
->packets
.life
);
1187 /* we use lifetimes since added, not since used */
1188 sa
->lft
.soft_add_expires_seconds
= lifetime
->time
.rekey
;
1189 sa
->lft
.hard_add_expires_seconds
= lifetime
->time
.life
;
1190 sa
->lft
.soft_use_expires_seconds
= 0;
1191 sa
->lft
.hard_use_expires_seconds
= 0;
1193 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1197 case ENCR_UNDEFINED
:
1200 case ENCR_AES_CCM_ICV16
:
1201 case ENCR_AES_GCM_ICV16
:
1202 case ENCR_NULL_AUTH_AES_GMAC
:
1203 case ENCR_CAMELLIA_CCM_ICV16
:
1206 case ENCR_AES_CCM_ICV12
:
1207 case ENCR_AES_GCM_ICV12
:
1208 case ENCR_CAMELLIA_CCM_ICV12
:
1211 case ENCR_AES_CCM_ICV8
:
1212 case ENCR_AES_GCM_ICV8
:
1213 case ENCR_CAMELLIA_CCM_ICV8
:
1215 struct xfrm_algo_aead
*algo
;
1217 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
1218 if (alg_name
== NULL
)
1220 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1221 encryption_algorithm_names
, enc_alg
);
1224 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1225 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1227 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
1228 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) +
1230 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1231 if (hdr
->nlmsg_len
> sizeof(request
))
1236 algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
1237 algo
->alg_key_len
= enc_key
.len
* 8;
1238 algo
->alg_icv_len
= icv_size
;
1239 strcpy(algo
->alg_name
, alg_name
);
1240 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1242 rthdr
= XFRM_RTA_NEXT(rthdr
);
1247 struct xfrm_algo
*algo
;
1249 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
1250 if (alg_name
== NULL
)
1252 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1253 encryption_algorithm_names
, enc_alg
);
1256 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1257 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1259 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
1260 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_key
.len
);
1261 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1262 if (hdr
->nlmsg_len
> sizeof(request
))
1267 algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1268 algo
->alg_key_len
= enc_key
.len
* 8;
1269 strcpy(algo
->alg_name
, alg_name
);
1270 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1272 rthdr
= XFRM_RTA_NEXT(rthdr
);
1276 if (int_alg
!= AUTH_UNDEFINED
)
1278 alg_name
= lookup_algorithm(integrity_algs
, int_alg
);
1279 if (alg_name
== NULL
)
1281 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1282 integrity_algorithm_names
, int_alg
);
1285 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1286 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1288 if (int_alg
== AUTH_HMAC_SHA2_256_128
)
1290 struct xfrm_algo_auth
* algo
;
1292 /* the kernel uses SHA256 with 96 bit truncation by default,
1293 * use specified truncation size supported by newer kernels */
1294 rthdr
->rta_type
= XFRMA_ALG_AUTH_TRUNC
;
1295 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_auth
) +
1298 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1299 if (hdr
->nlmsg_len
> sizeof(request
))
1304 algo
= (struct xfrm_algo_auth
*)RTA_DATA(rthdr
);
1305 algo
->alg_key_len
= int_key
.len
* 8;
1306 algo
->alg_trunc_len
= 128;
1307 strcpy(algo
->alg_name
, alg_name
);
1308 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1312 struct xfrm_algo
* algo
;
1314 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1315 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_key
.len
);
1317 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1318 if (hdr
->nlmsg_len
> sizeof(request
))
1323 algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1324 algo
->alg_key_len
= int_key
.len
* 8;
1325 strcpy(algo
->alg_name
, alg_name
);
1326 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1328 rthdr
= XFRM_RTA_NEXT(rthdr
);
1331 if (ipcomp
!= IPCOMP_NONE
)
1333 rthdr
->rta_type
= XFRMA_ALG_COMP
;
1334 alg_name
= lookup_algorithm(compression_algs
, ipcomp
);
1335 if (alg_name
== NULL
)
1337 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1338 ipcomp_transform_names
, ipcomp
);
1341 DBG2(DBG_KNL
, " using compression algorithm %N",
1342 ipcomp_transform_names
, ipcomp
);
1344 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
1345 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1346 if (hdr
->nlmsg_len
> sizeof(request
))
1351 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1352 algo
->alg_key_len
= 0;
1353 strcpy(algo
->alg_name
, alg_name
);
1355 rthdr
= XFRM_RTA_NEXT(rthdr
);
1360 struct xfrm_encap_tmpl
*tmpl
;
1362 rthdr
->rta_type
= XFRMA_ENCAP
;
1363 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1365 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1366 if (hdr
->nlmsg_len
> sizeof(request
))
1371 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1372 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1373 tmpl
->encap_sport
= htons(src
->get_port(src
));
1374 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1375 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1376 /* encap_oa could probably be derived from the
1377 * traffic selectors [rfc4306, p39]. In the netlink kernel
1378 * implementation pluto does the same as we do here but it uses
1379 * encap_oa in the pfkey implementation.
1380 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1382 * -> does that mean that NAT-T encap doesn't work in transport mode?
1383 * No. The reason the kernel ignores NAT-OA is that it recomputes
1384 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1385 * checks it marks them "checksum ok" so OA isn't needed. */
1386 rthdr
= XFRM_RTA_NEXT(rthdr
);
1391 struct xfrm_mark
*mrk
;
1393 rthdr
->rta_type
= XFRMA_MARK
;
1394 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1396 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1397 if (hdr
->nlmsg_len
> sizeof(request
))
1402 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1403 mrk
->v
= mark
.value
;
1405 rthdr
= XFRM_RTA_NEXT(rthdr
);
1412 rthdr
->rta_type
= XFRMA_TFCPAD
;
1413 rthdr
->rta_len
= RTA_LENGTH(sizeof(u_int32_t
));
1415 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1416 if (hdr
->nlmsg_len
> sizeof(request
))
1421 tfcpad
= (u_int32_t
*)RTA_DATA(rthdr
);
1423 rthdr
= XFRM_RTA_NEXT(rthdr
);
1426 if (protocol
!= IPPROTO_COMP
)
1428 if (esn
|| this->replay_window
> DEFAULT_REPLAY_WINDOW
)
1430 /* for ESN or larger replay windows we need the new
1431 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1432 struct xfrm_replay_state_esn
*replay
;
1434 rthdr
->rta_type
= XFRMA_REPLAY_ESN_VAL
;
1435 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state_esn
) +
1436 (this->replay_window
+ 7) / 8);
1438 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1439 if (hdr
->nlmsg_len
> sizeof(request
))
1444 replay
= (struct xfrm_replay_state_esn
*)RTA_DATA(rthdr
);
1445 /* bmp_len contains number uf __u32's */
1446 replay
->bmp_len
= this->replay_bmp
;
1447 replay
->replay_window
= this->replay_window
;
1448 DBG2(DBG_KNL
, " using replay window of %u bytes",
1449 this->replay_window
);
1451 rthdr
= XFRM_RTA_NEXT(rthdr
);
1454 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1455 sa
->flags
|= XFRM_STATE_ESN
;
1460 sa
->replay_window
= DEFAULT_REPLAY_WINDOW
;
1464 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1468 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x "
1469 "(mark %u/0x%8x)", ntohl(spi
), mark
.value
, mark
.mask
);
1473 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1481 memwipe(request
, sizeof(request
));
1486 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1488 * Allocates into one the replay state structure we get from the kernel.
1490 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
1491 u_int32_t spi
, u_int8_t protocol
, host_t
*dst
,
1492 struct xfrm_replay_state_esn
**replay_esn
,
1493 struct xfrm_replay_state
**replay
)
1495 netlink_buf_t request
;
1496 struct nlmsghdr
*hdr
, *out
= NULL
;
1497 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1502 memset(&request
, 0, sizeof(request
));
1504 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
1507 hdr
= (struct nlmsghdr
*)request
;
1508 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1509 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1510 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1512 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
1513 aevent_id
->flags
= XFRM_AE_RVAL
;
1515 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
1516 aevent_id
->sa_id
.spi
= spi
;
1517 aevent_id
->sa_id
.proto
= protocol
;
1518 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
1520 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1523 while (NLMSG_OK(hdr
, len
))
1525 switch (hdr
->nlmsg_type
)
1527 case XFRM_MSG_NEWAE
:
1529 out_aevent
= NLMSG_DATA(hdr
);
1534 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1535 DBG1(DBG_KNL
, "querying replay state from SAD entry "
1536 "failed: %s (%d)", strerror(-err
->error
),
1541 hdr
= NLMSG_NEXT(hdr
, len
);
1552 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1553 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1554 while (RTA_OK(rta
, rtasize
))
1556 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1557 RTA_PAYLOAD(rta
) == sizeof(**replay
))
1559 *replay
= malloc(RTA_PAYLOAD(rta
));
1560 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1563 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
&&
1564 RTA_PAYLOAD(rta
) >= sizeof(**replay_esn
) + this->replay_bmp
)
1566 *replay_esn
= malloc(RTA_PAYLOAD(rta
));
1567 memcpy(*replay_esn
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1570 rta
= RTA_NEXT(rta
, rtasize
);
1576 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1577 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1578 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
1580 netlink_buf_t request
;
1581 struct nlmsghdr
*out
= NULL
, *hdr
;
1582 struct xfrm_usersa_id
*sa_id
;
1583 struct xfrm_usersa_info
*sa
= NULL
;
1584 status_t status
= FAILED
;
1587 memset(&request
, 0, sizeof(request
));
1591 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x (mark %u/0x%8x)",
1592 ntohl(spi
), mark
.value
, mark
.mask
);
1596 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1598 hdr
= (struct nlmsghdr
*)request
;
1599 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1600 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1601 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1603 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1604 host2xfrm(dst
, &sa_id
->daddr
);
1606 sa_id
->proto
= protocol
;
1607 sa_id
->family
= dst
->get_family(dst
);
1611 struct xfrm_mark
*mrk
;
1612 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_id
);
1614 rthdr
->rta_type
= XFRMA_MARK
;
1615 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1616 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1617 if (hdr
->nlmsg_len
> sizeof(request
))
1622 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1623 mrk
->v
= mark
.value
;
1627 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1630 while (NLMSG_OK(hdr
, len
))
1632 switch (hdr
->nlmsg_type
)
1634 case XFRM_MSG_NEWSA
:
1636 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1641 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1645 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1646 "(mark %u/0x%8x) failed: %s (%d)",
1647 ntohl(spi
), mark
.value
, mark
.mask
,
1648 strerror(-err
->error
), -err
->error
);
1652 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1653 "failed: %s (%d)", ntohl(spi
),
1654 strerror(-err
->error
), -err
->error
);
1659 hdr
= NLMSG_NEXT(hdr
, len
);
1670 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1674 *bytes
= sa
->curlft
.bytes
;
1682 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1683 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1684 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1686 netlink_buf_t request
;
1687 struct nlmsghdr
*hdr
;
1688 struct xfrm_usersa_id
*sa_id
;
1690 /* if IPComp was used, we first delete the additional IPComp SA */
1693 del_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0, mark
);
1696 memset(&request
, 0, sizeof(request
));
1700 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x (mark %u/0x%8x)",
1701 ntohl(spi
), mark
.value
, mark
.mask
);
1705 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1707 hdr
= (struct nlmsghdr
*)request
;
1708 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1709 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1710 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1712 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1713 host2xfrm(dst
, &sa_id
->daddr
);
1715 sa_id
->proto
= protocol
;
1716 sa_id
->family
= dst
->get_family(dst
);
1720 struct xfrm_mark
*mrk
;
1721 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_id
);
1723 rthdr
->rta_type
= XFRMA_MARK
;
1724 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1725 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1726 if (hdr
->nlmsg_len
> sizeof(request
))
1731 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1732 mrk
->v
= mark
.value
;
1736 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1740 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x "
1741 "(mark %u/0x%8x)", ntohl(spi
), mark
.value
, mark
.mask
);
1745 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x",
1752 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x (mark %u/0x%8x)",
1753 ntohl(spi
), mark
.value
, mark
.mask
);
1757 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1762 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1763 private_kernel_netlink_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1764 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1765 bool old_encap
, bool new_encap
, mark_t mark
)
1767 netlink_buf_t request
;
1769 struct nlmsghdr
*hdr
, *out
= NULL
;
1770 struct xfrm_usersa_id
*sa_id
;
1771 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1775 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1776 struct xfrm_replay_state
*replay
= NULL
;
1777 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
1778 status_t status
= FAILED
;
1780 /* if IPComp is used, we first update the IPComp SA */
1783 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1784 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
, mark
);
1787 memset(&request
, 0, sizeof(request
));
1789 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1791 /* query the existing SA first */
1792 hdr
= (struct nlmsghdr
*)request
;
1793 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1794 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1795 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1797 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1798 host2xfrm(dst
, &sa_id
->daddr
);
1800 sa_id
->proto
= protocol
;
1801 sa_id
->family
= dst
->get_family(dst
);
1803 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1806 while (NLMSG_OK(hdr
, len
))
1808 switch (hdr
->nlmsg_type
)
1810 case XFRM_MSG_NEWSA
:
1812 out_sa
= NLMSG_DATA(hdr
);
1817 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1818 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1819 strerror(-err
->error
), -err
->error
);
1823 hdr
= NLMSG_NEXT(hdr
, len
);
1833 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1837 get_replay_state(this, spi
, protocol
, dst
, &replay_esn
, &replay
);
1839 /* delete the old SA (without affecting the IPComp SA) */
1840 if (del_sa(this, src
, dst
, spi
, protocol
, 0, mark
) != SUCCESS
)
1842 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x",
1847 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1848 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1849 /* copy over the SA from out to request */
1850 hdr
= (struct nlmsghdr
*)request
;
1851 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1852 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1853 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1854 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1855 sa
= NLMSG_DATA(hdr
);
1856 sa
->family
= new_dst
->get_family(new_dst
);
1858 if (!src
->ip_equals(src
, new_src
))
1860 host2xfrm(new_src
, &sa
->saddr
);
1862 if (!dst
->ip_equals(dst
, new_dst
))
1864 host2xfrm(new_dst
, &sa
->id
.daddr
);
1867 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1868 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1869 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1870 while(RTA_OK(rta
, rtasize
))
1872 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1873 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1875 if (rta
->rta_type
== XFRMA_ENCAP
)
1876 { /* update encap tmpl */
1877 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1878 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1879 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1881 memcpy(pos
, rta
, rta
->rta_len
);
1882 pos
+= RTA_ALIGN(rta
->rta_len
);
1883 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1885 rta
= RTA_NEXT(rta
, rtasize
);
1888 rta
= (struct rtattr
*)pos
;
1889 if (tmpl
== NULL
&& new_encap
)
1890 { /* add tmpl if we are enabling it */
1891 rta
->rta_type
= XFRMA_ENCAP
;
1892 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1894 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1895 if (hdr
->nlmsg_len
> sizeof(request
))
1900 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1901 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1902 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1903 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1904 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1906 rta
= XFRM_RTA_NEXT(rta
);
1911 rta
->rta_type
= XFRMA_REPLAY_ESN_VAL
;
1912 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state_esn
) +
1915 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1916 if (hdr
->nlmsg_len
> sizeof(request
))
1920 memcpy(RTA_DATA(rta
), replay_esn
,
1921 sizeof(struct xfrm_replay_state_esn
) + this->replay_bmp
);
1923 rta
= XFRM_RTA_NEXT(rta
);
1927 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1928 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1930 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1931 if (hdr
->nlmsg_len
> sizeof(request
))
1935 memcpy(RTA_DATA(rta
), replay
, sizeof(replay
));
1937 rta
= XFRM_RTA_NEXT(rta
);
1941 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry "
1942 "with SPI %.8x", ntohl(spi
));
1945 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1947 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1961 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
1962 private_kernel_netlink_ipsec_t
*this)
1964 netlink_buf_t request
;
1965 struct nlmsghdr
*hdr
;
1966 struct xfrm_usersa_flush
*flush
;
1968 memset(&request
, 0, sizeof(request
));
1970 DBG2(DBG_KNL
, "flushing all SAD entries");
1972 hdr
= (struct nlmsghdr
*)request
;
1973 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1974 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
1975 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1977 flush
= (struct xfrm_usersa_flush
*)NLMSG_DATA(hdr
);
1978 flush
->proto
= IPSEC_PROTO_ANY
;
1980 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1982 DBG1(DBG_KNL
, "unable to flush SAD entries");
1989 * Add or update a policy in the kernel.
1991 * Note: The mutex has to be locked when entering this function.
1993 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
1994 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
1996 netlink_buf_t request
;
1997 policy_entry_t clone
;
1998 ipsec_sa_t
*ipsec
= mapping
->sa
;
1999 struct xfrm_userpolicy_info
*policy_info
;
2000 struct nlmsghdr
*hdr
;
2003 /* clone the policy so we are able to check it out again later */
2004 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
2006 memset(&request
, 0, sizeof(request
));
2007 hdr
= (struct nlmsghdr
*)request
;
2008 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2009 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
2010 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2012 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2013 policy_info
->sel
= policy
->sel
;
2014 policy_info
->dir
= policy
->direction
;
2016 /* calculate priority based on selector size, small size = high prio */
2017 policy_info
->priority
= mapping
->priority
;
2018 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
2019 : XFRM_POLICY_BLOCK
;
2020 policy_info
->share
= XFRM_SHARE_ANY
;
2022 /* policies don't expire */
2023 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2024 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2025 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2026 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2027 policy_info
->lft
.soft_add_expires_seconds
= 0;
2028 policy_info
->lft
.hard_add_expires_seconds
= 0;
2029 policy_info
->lft
.soft_use_expires_seconds
= 0;
2030 policy_info
->lft
.hard_use_expires_seconds
= 0;
2032 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2034 if (mapping
->type
== POLICY_IPSEC
)
2036 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2041 { IPPROTO_COMP
, ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2042 { IPPROTO_ESP
, ipsec
->cfg
.esp
.use
},
2043 { IPPROTO_AH
, ipsec
->cfg
.ah
.use
},
2045 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2047 rthdr
->rta_type
= XFRMA_TMPL
;
2048 rthdr
->rta_len
= 0; /* actual length is set below */
2050 for (i
= 0; i
< countof(protos
); i
++)
2057 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2058 hdr
->nlmsg_len
+= RTA_ALIGN(RTA_LENGTH(sizeof(struct xfrm_user_tmpl
)));
2059 if (hdr
->nlmsg_len
> sizeof(request
))
2064 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2065 tmpl
->id
.proto
= protos
[i
].proto
;
2066 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2067 tmpl
->mode
= mode2kernel(proto_mode
);
2068 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2069 policy
->direction
!= POLICY_OUT
;
2070 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2072 if (proto_mode
== MODE_TUNNEL
)
2073 { /* only for tunnel mode */
2074 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2075 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2080 /* use transport mode for other SAs */
2081 proto_mode
= MODE_TRANSPORT
;
2084 rthdr
= XFRM_RTA_NEXT(rthdr
);
2087 if (ipsec
->mark
.value
)
2089 struct xfrm_mark
*mrk
;
2091 rthdr
->rta_type
= XFRMA_MARK
;
2092 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2094 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2095 if (hdr
->nlmsg_len
> sizeof(request
))
2100 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2101 mrk
->v
= ipsec
->mark
.value
;
2102 mrk
->m
= ipsec
->mark
.mask
;
2104 this->mutex
->unlock(this->mutex
);
2106 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2111 /* find the policy again */
2112 this->mutex
->lock(this->mutex
);
2113 policy
= this->policies
->get(this->policies
, &clone
);
2115 policy
->used_by
->find_first(policy
->used_by
,
2116 NULL
, (void**)&mapping
) != SUCCESS
)
2117 { /* policy or mapping is already gone, ignore */
2118 this->mutex
->unlock(this->mutex
);
2122 /* install a route, if:
2123 * - this is a forward policy (to just get one for each child)
2124 * - we are in tunnel/BEET mode
2125 * - routing is not disabled via strongswan.conf
2127 if (policy
->direction
== POLICY_FWD
&&
2128 ipsec
->cfg
.mode
!= MODE_TRANSPORT
&& this->install_routes
)
2130 route_entry_t
*route
= malloc_thing(route_entry_t
);
2131 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)mapping
;
2133 if (hydra
->kernel_interface
->get_address_by_ts(hydra
->kernel_interface
,
2134 fwd
->dst_ts
, &route
->src_ip
) == SUCCESS
)
2136 /* get the nexthop to src (src as we are in POLICY_FWD) */
2137 route
->gateway
= hydra
->kernel_interface
->get_nexthop(
2138 hydra
->kernel_interface
, ipsec
->src
);
2139 /* install route via outgoing interface */
2140 route
->if_name
= hydra
->kernel_interface
->get_interface(
2141 hydra
->kernel_interface
, ipsec
->dst
);
2142 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2143 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
2144 route
->prefixlen
= policy
->sel
.prefixlen_s
;
2146 if (!route
->if_name
)
2148 this->mutex
->unlock(this->mutex
);
2149 route_entry_destroy(route
);
2155 route_entry_t
*old
= policy
->route
;
2156 if (route_entry_equals(old
, route
))
2157 { /* keep previously installed route */
2158 this->mutex
->unlock(this->mutex
);
2159 route_entry_destroy(route
);
2162 /* uninstall previously installed route */
2163 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2164 old
->dst_net
, old
->prefixlen
, old
->gateway
,
2165 old
->src_ip
, old
->if_name
) != SUCCESS
)
2167 DBG1(DBG_KNL
, "error uninstalling route installed with "
2168 "policy %R === %R %N", fwd
->src_ts
,
2169 fwd
->dst_ts
, policy_dir_names
,
2172 route_entry_destroy(old
);
2173 policy
->route
= NULL
;
2176 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2177 fwd
->src_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2178 switch (hydra
->kernel_interface
->add_route(
2179 hydra
->kernel_interface
, route
->dst_net
,
2180 route
->prefixlen
, route
->gateway
,
2181 route
->src_ip
, route
->if_name
))
2184 DBG1(DBG_KNL
, "unable to install source route for %H",
2188 /* route exists, do not uninstall */
2189 route_entry_destroy(route
);
2192 /* cache the installed route */
2193 policy
->route
= route
;
2202 this->mutex
->unlock(this->mutex
);
2206 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2207 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
2208 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
2209 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
2210 mark_t mark
, policy_priority_t priority
)
2212 policy_entry_t
*policy
, *current
;
2213 policy_sa_t
*assigned_sa
, *current_sa
;
2214 enumerator_t
*enumerator
;
2215 bool found
= FALSE
, update
= TRUE
;
2217 /* create a policy */
2219 .sel
= ts2selector(src_ts
, dst_ts
),
2220 .mark
= mark
.value
& mark
.mask
,
2221 .direction
= direction
,
2224 /* find the policy, which matches EXACTLY */
2225 this->mutex
->lock(this->mutex
);
2226 current
= this->policies
->get(this->policies
, policy
);
2229 /* use existing policy */
2232 DBG2(DBG_KNL
, "policy %R === %R %N (mark %u/0x%8x) "
2233 "already exists, increasing refcount",
2234 src_ts
, dst_ts
, policy_dir_names
, direction
,
2235 mark
.value
, mark
.mask
);
2239 DBG2(DBG_KNL
, "policy %R === %R %N "
2240 "already exists, increasing refcount",
2241 src_ts
, dst_ts
, policy_dir_names
, direction
);
2243 policy_entry_destroy(this, policy
);
2248 { /* use the new one, if we have no such policy */
2249 policy
->used_by
= linked_list_create();
2250 this->policies
->put(this->policies
, policy
, policy
);
2253 /* cache the assigned IPsec SA */
2254 assigned_sa
= policy_sa_create(this, direction
, type
, src
, dst
, src_ts
,
2256 assigned_sa
->priority
= get_priority(policy
, priority
);
2258 if (this->policy_history
)
2259 { /* insert the SA according to its priority */
2260 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2261 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2263 if (current_sa
->priority
>= assigned_sa
->priority
)
2269 policy
->used_by
->insert_before(policy
->used_by
, enumerator
,
2271 enumerator
->destroy(enumerator
);
2274 { /* simply insert it last and only update if it is not installed yet */
2275 policy
->used_by
->insert_last(policy
->used_by
, assigned_sa
);
2280 { /* we don't update the policy if the priority is lower than that of
2281 * the currently installed one */
2282 this->mutex
->unlock(this->mutex
);
2288 DBG2(DBG_KNL
, "%s policy %R === %R %N (mark %u/0x%8x)",
2289 found ?
"updating" : "adding", src_ts
, dst_ts
,
2290 policy_dir_names
, direction
, mark
.value
, mark
.mask
);
2294 DBG2(DBG_KNL
, "%s policy %R === %R %N",
2295 found ?
"updating" : "adding", src_ts
, dst_ts
,
2296 policy_dir_names
, direction
);
2299 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2301 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N",
2302 found ?
"update" : "add", src_ts
, dst_ts
,
2303 policy_dir_names
, direction
);
2309 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2310 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2311 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
2312 u_int32_t
*use_time
)
2314 netlink_buf_t request
;
2315 struct nlmsghdr
*out
= NULL
, *hdr
;
2316 struct xfrm_userpolicy_id
*policy_id
;
2317 struct xfrm_userpolicy_info
*policy
= NULL
;
2320 memset(&request
, 0, sizeof(request
));
2324 DBG2(DBG_KNL
, "querying policy %R === %R %N (mark %u/0x%8x)",
2325 src_ts
, dst_ts
, policy_dir_names
, direction
,
2326 mark
.value
, mark
.mask
);
2330 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
2331 policy_dir_names
, direction
);
2333 hdr
= (struct nlmsghdr
*)request
;
2334 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2335 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2336 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2338 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2339 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2340 policy_id
->dir
= direction
;
2344 struct xfrm_mark
*mrk
;
2345 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
2347 rthdr
->rta_type
= XFRMA_MARK
;
2348 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2350 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2351 if (hdr
->nlmsg_len
> sizeof(request
))
2356 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2357 mrk
->v
= mark
.value
;
2361 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2364 while (NLMSG_OK(hdr
, len
))
2366 switch (hdr
->nlmsg_type
)
2368 case XFRM_MSG_NEWPOLICY
:
2370 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2375 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2376 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2377 strerror(-err
->error
), -err
->error
);
2381 hdr
= NLMSG_NEXT(hdr
, len
);
2392 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
2393 policy_dir_names
, direction
);
2398 if (policy
->curlft
.use_time
)
2400 /* we need the monotonic time, but the kernel returns system time. */
2401 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
2412 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2413 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2414 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
2415 mark_t mark
, policy_priority_t prio
)
2417 policy_entry_t
*current
, policy
;
2418 enumerator_t
*enumerator
;
2419 policy_sa_t
*mapping
;
2420 netlink_buf_t request
;
2421 struct nlmsghdr
*hdr
;
2422 struct xfrm_userpolicy_id
*policy_id
;
2423 bool is_installed
= TRUE
;
2428 DBG2(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%8x)",
2429 src_ts
, dst_ts
, policy_dir_names
, direction
,
2430 mark
.value
, mark
.mask
);
2434 DBG2(DBG_KNL
, "deleting policy %R === %R %N",
2435 src_ts
, dst_ts
, policy_dir_names
, direction
);
2438 /* create a policy */
2439 memset(&policy
, 0, sizeof(policy_entry_t
));
2440 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2441 policy
.mark
= mark
.value
& mark
.mask
;
2442 policy
.direction
= direction
;
2444 /* find the policy */
2445 this->mutex
->lock(this->mutex
);
2446 current
= this->policies
->get(this->policies
, &policy
);
2451 DBG1(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%8x) "
2452 "failed, not found", src_ts
, dst_ts
, policy_dir_names
,
2453 direction
, mark
.value
, mark
.mask
);
2457 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found",
2458 src_ts
, dst_ts
, policy_dir_names
, direction
);
2460 this->mutex
->unlock(this->mutex
);
2464 if (this->policy_history
)
2465 { /* remove mapping to SA by reqid and priority */
2466 priority
= get_priority(current
, prio
);
2467 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
2468 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2470 if (reqid
== mapping
->sa
->cfg
.reqid
&&
2471 priority
== mapping
->priority
)
2473 current
->used_by
->remove_at(current
->used_by
, enumerator
);
2474 policy_sa_destroy(mapping
, &direction
, this);
2477 is_installed
= FALSE
;
2479 enumerator
->destroy(enumerator
);
2482 { /* remove one of the SAs but don't update the policy */
2483 current
->used_by
->remove_last(current
->used_by
, (void**)&mapping
);
2484 policy_sa_destroy(mapping
, &direction
, this);
2485 is_installed
= FALSE
;
2488 if (current
->used_by
->get_count(current
->used_by
) > 0)
2489 { /* policy is used by more SAs, keep in kernel */
2490 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2492 { /* no need to update as the policy was not installed for this SA */
2493 this->mutex
->unlock(this->mutex
);
2499 DBG2(DBG_KNL
, "updating policy %R === %R %N (mark %u/0x%8x)",
2500 src_ts
, dst_ts
, policy_dir_names
, direction
,
2501 mark
.value
, mark
.mask
);
2505 DBG2(DBG_KNL
, "updating policy %R === %R %N",
2506 src_ts
, dst_ts
, policy_dir_names
, direction
);
2509 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
2510 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
2512 DBG1(DBG_KNL
, "unable to update policy %R === %R %N",
2513 src_ts
, dst_ts
, policy_dir_names
, direction
);
2519 memset(&request
, 0, sizeof(request
));
2521 hdr
= (struct nlmsghdr
*)request
;
2522 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2523 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2524 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2526 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2527 policy_id
->sel
= current
->sel
;
2528 policy_id
->dir
= direction
;
2532 struct xfrm_mark
*mrk
;
2533 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
2535 rthdr
->rta_type
= XFRMA_MARK
;
2536 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2537 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2538 if (hdr
->nlmsg_len
> sizeof(request
))
2543 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2544 mrk
->v
= mark
.value
;
2550 route_entry_t
*route
= current
->route
;
2551 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2552 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2553 route
->src_ip
, route
->if_name
) != SUCCESS
)
2555 DBG1(DBG_KNL
, "error uninstalling route installed with "
2556 "policy %R === %R %N", src_ts
, dst_ts
,
2557 policy_dir_names
, direction
);
2561 this->policies
->remove(this->policies
, current
);
2562 policy_entry_destroy(this, current
);
2563 this->mutex
->unlock(this->mutex
);
2565 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2569 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N "
2570 "(mark %u/0x%8x)", src_ts
, dst_ts
, policy_dir_names
,
2571 direction
, mark
.value
, mark
.mask
);
2575 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N",
2576 src_ts
, dst_ts
, policy_dir_names
, direction
);
2583 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
2584 private_kernel_netlink_ipsec_t
*this)
2586 netlink_buf_t request
;
2587 struct nlmsghdr
*hdr
;
2589 memset(&request
, 0, sizeof(request
));
2591 DBG2(DBG_KNL
, "flushing all policies from SPD");
2593 hdr
= (struct nlmsghdr
*)request
;
2594 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2595 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
2596 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
2598 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2599 * to main or sub policies (default is main) */
2601 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2603 DBG1(DBG_KNL
, "unable to flush SPD entries");
2610 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2611 private_kernel_netlink_ipsec_t
*this, int fd
, int family
)
2613 struct xfrm_userpolicy_info policy
;
2614 u_int sol
, ipsec_policy
;
2620 ipsec_policy
= IP_XFRM_POLICY
;
2624 ipsec_policy
= IPV6_XFRM_POLICY
;
2630 memset(&policy
, 0, sizeof(policy
));
2631 policy
.action
= XFRM_POLICY_ALLOW
;
2632 policy
.sel
.family
= family
;
2634 policy
.dir
= XFRM_POLICY_OUT
;
2635 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2637 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2641 policy
.dir
= XFRM_POLICY_IN
;
2642 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2644 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2651 METHOD(kernel_ipsec_t
, destroy
, void,
2652 private_kernel_netlink_ipsec_t
*this)
2654 enumerator_t
*enumerator
;
2655 policy_entry_t
*policy
;
2659 this->job
->cancel(this->job
);
2661 if (this->socket_xfrm_events
> 0)
2663 close(this->socket_xfrm_events
);
2665 DESTROY_IF(this->socket_xfrm
);
2666 enumerator
= this->policies
->create_enumerator(this->policies
);
2667 while (enumerator
->enumerate(enumerator
, &policy
, &policy
))
2669 policy_entry_destroy(this, policy
);
2671 enumerator
->destroy(enumerator
);
2672 this->policies
->destroy(this->policies
);
2673 this->sas
->destroy(this->sas
);
2674 this->mutex
->destroy(this->mutex
);
2679 * Described in header.
2681 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
2683 private_kernel_netlink_ipsec_t
*this;
2684 struct sockaddr_nl addr
;
2690 .get_spi
= _get_spi
,
2691 .get_cpi
= _get_cpi
,
2693 .update_sa
= _update_sa
,
2694 .query_sa
= _query_sa
,
2696 .flush_sas
= _flush_sas
,
2697 .add_policy
= _add_policy
,
2698 .query_policy
= _query_policy
,
2699 .del_policy
= _del_policy
,
2700 .flush_policies
= _flush_policies
,
2701 .bypass_socket
= _bypass_socket
,
2702 .destroy
= _destroy
,
2705 .policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
2706 (hashtable_equals_t
)policy_equals
, 32),
2707 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
2708 (hashtable_equals_t
)ipsec_sa_equals
, 32),
2709 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2710 .policy_history
= TRUE
,
2711 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2712 "%s.install_routes", TRUE
, hydra
->daemon
),
2713 .replay_window
= lib
->settings
->get_int(lib
->settings
,
2714 "%s.replay_window", DEFAULT_REPLAY_WINDOW
, hydra
->daemon
),
2717 this->replay_bmp
= (this->replay_window
+ sizeof(u_int32_t
) * 8 - 1) /
2718 (sizeof(u_int32_t
) * 8);
2720 if (streq(hydra
->daemon
, "pluto"))
2721 { /* no routes for pluto, they are installed via updown script */
2722 this->install_routes
= FALSE
;
2723 /* no policy history for pluto */
2724 this->policy_history
= FALSE
;
2727 /* disable lifetimes for allocated SPIs in kernel */
2728 fd
= open("/proc/sys/net/core/xfrm_acq_expires", O_WRONLY
);
2731 ignore_result(write(fd
, "165", 3));
2735 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
2736 if (!this->socket_xfrm
)
2742 memset(&addr
, 0, sizeof(addr
));
2743 addr
.nl_family
= AF_NETLINK
;
2745 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
2746 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2747 if (this->socket_xfrm_events
<= 0)
2749 DBG1(DBG_KNL
, "unable to create XFRM event socket");
2753 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
2754 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
2755 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2757 DBG1(DBG_KNL
, "unable to bind XFRM event socket");
2761 this->job
= callback_job_create_with_prio((callback_job_cb_t
)receive_events
,
2762 this, NULL
, NULL
, JOB_PRIO_CRITICAL
);
2763 lib
->processor
->queue_job(lib
->processor
, (job_t
*)this->job
);
2765 return &this->public;