2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
5 * Copyright (C) 2006 Daniel Roethlisberger
6 * Copyright (C) 2005 Jan Hutter
7 * Hochschule fuer Technik Rapperswil
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <linux/netlink.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/xfrm.h>
28 #include <linux/udp.h>
29 #include <netinet/in.h>
35 #include "kernel_netlink_ipsec.h"
36 #include "kernel_netlink_shared.h"
39 #include <utils/linked_list.h>
40 #include <processing/jobs/callback_job.h>
41 #include <processing/jobs/acquire_job.h>
42 #include <processing/jobs/rekey_child_sa_job.h>
43 #include <processing/jobs/delete_child_sa_job.h>
44 #include <processing/jobs/update_sa_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 /** default priority of installed policies */
53 #define PRIO_HIGH 2000
56 * Create ORable bitfield of XFRM NL groups
58 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
61 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
62 * 'usual' netlink data x like 'struct xfrm_usersa_info'
64 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
66 * returns a pointer to the next rtattr following rta.
67 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
69 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
71 * returns the total size of attached rta data
72 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
74 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
76 typedef struct kernel_algorithm_t kernel_algorithm_t
;
79 * Mapping of IKEv2 kernel identifier to linux crypto API names
81 struct kernel_algorithm_t
{
83 * Identifier specified in IKEv2
88 * Name of the algorithm in linux crypto API
93 #define END_OF_LIST -1
96 * Algorithms for encryption
98 static kernel_algorithm_t encryption_algs
[] = {
99 /* {ENCR_DES_IV64, "***" }, */
101 {ENCR_3DES
, "des3_ede" },
102 /* {ENCR_RC5, "***" }, */
103 /* {ENCR_IDEA, "***" }, */
104 {ENCR_CAST
, "cast128" },
105 {ENCR_BLOWFISH
, "blowfish" },
106 /* {ENCR_3IDEA, "***" }, */
107 /* {ENCR_DES_IV32, "***" }, */
108 {ENCR_NULL
, "cipher_null" },
109 {ENCR_AES_CBC
, "aes" },
110 /* {ENCR_AES_CTR, "***" }, */
111 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
112 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
113 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
114 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
115 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
116 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
117 {END_OF_LIST
, NULL
},
121 * Algorithms for integrity protection
123 static kernel_algorithm_t integrity_algs
[] = {
124 {AUTH_HMAC_MD5_96
, "md5" },
125 {AUTH_HMAC_SHA1_96
, "sha1" },
126 {AUTH_HMAC_SHA2_256_128
, "sha256" },
127 {AUTH_HMAC_SHA2_384_192
, "sha384" },
128 {AUTH_HMAC_SHA2_512_256
, "sha512" },
129 /* {AUTH_DES_MAC, "***" }, */
130 /* {AUTH_KPDK_MD5, "***" }, */
131 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
132 {END_OF_LIST
, NULL
},
136 * Algorithms for IPComp
138 static kernel_algorithm_t compression_algs
[] = {
139 /* {IPCOMP_OUI, "***" }, */
140 {IPCOMP_DEFLATE
, "deflate" },
141 {IPCOMP_LZS
, "lzs" },
142 {IPCOMP_LZJH
, "lzjh" },
143 {END_OF_LIST
, NULL
},
147 * Look up a kernel algorithm name and its key size
149 static char* lookup_algorithm(kernel_algorithm_t
*list
, int ikev2
)
151 while (list
->ikev2
!= END_OF_LIST
)
153 if (list
->ikev2
== ikev2
)
162 typedef struct route_entry_t route_entry_t
;
165 * installed routing entry
167 struct route_entry_t
{
168 /** Name of the interface the route is bound to */
171 /** Source ip of the route */
174 /** gateway for this route */
177 /** Destination net */
180 /** Destination net prefixlen */
185 * destroy an route_entry_t object
187 static void route_entry_destroy(route_entry_t
*this)
190 this->src_ip
->destroy(this->src_ip
);
191 this->gateway
->destroy(this->gateway
);
192 chunk_free(&this->dst_net
);
196 typedef struct policy_entry_t policy_entry_t
;
199 * installed kernel policy.
201 struct policy_entry_t
{
203 /** direction of this policy: in, out, forward */
206 /** parameters of installed policy */
207 struct xfrm_selector sel
;
209 /** associated route installed for this policy */
210 route_entry_t
*route
;
212 /** by how many CHILD_SA's this policy is used */
216 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
219 * Private variables and functions of kernel_netlink class.
221 struct private_kernel_netlink_ipsec_t
{
223 * Public part of the kernel_netlink_t object.
225 kernel_netlink_ipsec_t
public;
228 * mutex to lock access to various lists
230 pthread_mutex_t mutex
;
233 * List of installed policies (policy_entry_t)
235 linked_list_t
*policies
;
238 * job receiving netlink events
243 * Netlink xfrm socket (IPsec)
245 netlink_socket_t
*socket_xfrm
;
248 * netlink xfrm socket to receive acquire and expire events
250 int socket_xfrm_events
;
253 * whether to install routes along policies
259 * convert a IKEv2 specific protocol identifier to the kernel one
261 static u_int8_t
proto_ike2kernel(protocol_id_t proto
)
275 * reverse of ike2kernel
277 static protocol_id_t
proto_kernel2ike(u_int8_t proto
)
291 * convert a host_t to a struct xfrm_address
293 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
295 chunk_t chunk
= host
->get_address(host
);
296 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
300 * convert a struct xfrm_address to a host_t
302 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
309 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
312 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
317 return host_create_from_chunk(family
, chunk
, ntohs(port
));
321 * convert a traffic selector address range to subnet and its mask.
323 static void ts2subnet(traffic_selector_t
* ts
,
324 xfrm_address_t
*net
, u_int8_t
*mask
)
329 ts
->to_subnet(ts
, &net_host
, mask
);
330 net_chunk
= net_host
->get_address(net_host
);
331 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
332 net_host
->destroy(net_host
);
336 * convert a traffic selector port range to port/portmask
338 static void ts2ports(traffic_selector_t
* ts
,
339 u_int16_t
*port
, u_int16_t
*mask
)
341 /* linux does not seem to accept complex portmasks. Only
342 * any or a specific port is allowed. We set to any, if we have
343 * a port range, or to a specific, if we have one port only.
347 from
= ts
->get_from_port(ts
);
348 to
= ts
->get_to_port(ts
);
363 * convert a pair of traffic_selectors to a xfrm_selector
365 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
366 traffic_selector_t
*dst
)
368 struct xfrm_selector sel
;
370 memset(&sel
, 0, sizeof(sel
));
371 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
372 /* src or dest proto may be "any" (0), use more restrictive one */
373 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
374 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
375 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
376 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
377 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
386 * process a XFRM_MSG_ACQUIRE from kernel
388 static void process_acquire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
393 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
394 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
396 if (RTA_OK(rtattr
, rtsize
))
398 if (rtattr
->rta_type
== XFRMA_TMPL
)
400 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rtattr
);
402 proto
= tmpl
->id
.proto
;
412 /* acquire for AH/ESP only, not for IPCOMP */
417 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
420 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
421 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid {%d}", reqid
);
422 job
= (job_t
*)acquire_job_create(reqid
);
423 charon
->processor
->queue_job(charon
->processor
, job
);
427 * process a XFRM_MSG_EXPIRE from kernel
429 static void process_expire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
432 protocol_id_t protocol
;
433 u_int32_t spi
, reqid
;
434 struct xfrm_user_expire
*expire
;
436 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
437 protocol
= proto_kernel2ike(expire
->state
.id
.proto
);
438 spi
= expire
->state
.id
.spi
;
439 reqid
= expire
->state
.reqid
;
441 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
443 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
445 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and reqid {%d} "
446 "which is not a CHILD_SA", ntohl(spi
), reqid
);
450 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
451 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
452 protocol
, ntohl(spi
), reqid
);
455 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
459 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
461 charon
->processor
->queue_job(charon
->processor
, job
);
465 * process a XFRM_MSG_MAPPING from kernel
467 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
468 struct nlmsghdr
*hdr
)
471 u_int32_t spi
, reqid
;
472 struct xfrm_user_mapping
*mapping
;
475 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
476 spi
= mapping
->id
.spi
;
477 reqid
= mapping
->reqid
;
479 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
481 if (proto_kernel2ike(mapping
->id
.proto
) == PROTO_ESP
)
483 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
487 DBG1(DBG_KNL
, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
488 "reqid {%d} changed, queuing update job", ntohl(spi
), reqid
);
489 job
= (job_t
*)update_sa_job_create(reqid
, host
);
490 charon
->processor
->queue_job(charon
->processor
, job
);
496 * Receives events from kernel
498 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
501 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
502 struct sockaddr_nl addr
;
503 socklen_t addr_len
= sizeof(addr
);
506 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
507 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
508 (struct sockaddr
*)&addr
, &addr_len
);
509 pthread_setcancelstate(oldstate
, NULL
);
516 /* interrupted, try again */
517 return JOB_REQUEUE_DIRECT
;
519 /* no data ready, select again */
520 return JOB_REQUEUE_DIRECT
;
522 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
524 return JOB_REQUEUE_FAIR
;
528 if (addr
.nl_pid
!= 0)
529 { /* not from kernel. not interested, try another one */
530 return JOB_REQUEUE_DIRECT
;
533 while (NLMSG_OK(hdr
, len
))
535 switch (hdr
->nlmsg_type
)
537 case XFRM_MSG_ACQUIRE
:
538 process_acquire(this, hdr
);
540 case XFRM_MSG_EXPIRE
:
541 process_expire(this, hdr
);
543 case XFRM_MSG_MAPPING
:
544 process_mapping(this, hdr
);
549 hdr
= NLMSG_NEXT(hdr
, len
);
551 return JOB_REQUEUE_DIRECT
;
555 * Get an SPI for a specific protocol from the kernel.
557 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
558 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
559 u_int32_t reqid
, u_int32_t
*spi
)
561 unsigned char request
[NETLINK_BUFFER_SIZE
];
562 struct nlmsghdr
*hdr
, *out
;
563 struct xfrm_userspi_info
*userspi
;
564 u_int32_t received_spi
= 0;
567 memset(&request
, 0, sizeof(request
));
569 hdr
= (struct nlmsghdr
*)request
;
570 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
571 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
572 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
574 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
575 host2xfrm(src
, &userspi
->info
.saddr
);
576 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
577 userspi
->info
.id
.proto
= proto
;
578 userspi
->info
.mode
= TRUE
; /* tunnel mode */
579 userspi
->info
.reqid
= reqid
;
580 userspi
->info
.family
= src
->get_family(src
);
584 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
587 while (NLMSG_OK(hdr
, len
))
589 switch (hdr
->nlmsg_type
)
593 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
594 received_spi
= usersa
->id
.spi
;
599 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
601 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
602 strerror(-err
->error
), -err
->error
);
606 hdr
= NLMSG_NEXT(hdr
, len
);
616 if (received_spi
== 0)
626 * Implementation of kernel_interface_t.get_spi.
628 static status_t
get_spi(private_kernel_netlink_ipsec_t
*this,
629 host_t
*src
, host_t
*dst
,
630 protocol_id_t protocol
, u_int32_t reqid
,
633 DBG2(DBG_KNL
, "getting SPI for reqid {%d}", reqid
);
635 if (get_spi_internal(this, src
, dst
, proto_ike2kernel(protocol
),
636 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
638 DBG1(DBG_KNL
, "unable to get SPI for reqid {%d}", reqid
);
642 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%d}", ntohl(*spi
), reqid
);
648 * Implementation of kernel_interface_t.get_cpi.
650 static status_t
get_cpi(private_kernel_netlink_ipsec_t
*this,
651 host_t
*src
, host_t
*dst
,
652 u_int32_t reqid
, u_int16_t
*cpi
)
654 u_int32_t received_spi
= 0;
656 DBG2(DBG_KNL
, "getting CPI for reqid {%d}", reqid
);
658 if (get_spi_internal(this, src
, dst
,
659 IPPROTO_COMP
, 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
661 DBG1(DBG_KNL
, "unable to get CPI for reqid {%d}", reqid
);
665 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
667 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%d}", ntohs(*cpi
), reqid
);
673 * Implementation of kernel_interface_t.add_sa.
675 static status_t
add_sa(private_kernel_netlink_ipsec_t
*this,
676 host_t
*src
, host_t
*dst
, u_int32_t spi
,
677 protocol_id_t protocol
, u_int32_t reqid
,
678 u_int64_t expire_soft
, u_int64_t expire_hard
,
679 u_int16_t enc_alg
, chunk_t enc_key
,
680 u_int16_t int_alg
, chunk_t int_key
,
681 ipsec_mode_t mode
, u_int16_t ipcomp
, bool encap
,
684 unsigned char request
[NETLINK_BUFFER_SIZE
];
686 struct nlmsghdr
*hdr
;
687 struct xfrm_usersa_info
*sa
;
688 u_int16_t icv_size
= 64;
690 memset(&request
, 0, sizeof(request
));
692 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%d}",
695 hdr
= (struct nlmsghdr
*)request
;
696 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
697 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
698 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
700 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
701 host2xfrm(src
, &sa
->saddr
);
702 host2xfrm(dst
, &sa
->id
.daddr
);
704 sa
->id
.proto
= proto_ike2kernel(protocol
);
705 sa
->family
= src
->get_family(src
);
707 if (mode
== MODE_TUNNEL
)
709 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
711 sa
->replay_window
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
713 /* we currently do not expire SAs by volume/packet count */
714 sa
->lft
.soft_byte_limit
= XFRM_INF
;
715 sa
->lft
.hard_byte_limit
= XFRM_INF
;
716 sa
->lft
.soft_packet_limit
= XFRM_INF
;
717 sa
->lft
.hard_packet_limit
= XFRM_INF
;
718 /* we use lifetimes since added, not since used */
719 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
720 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
721 sa
->lft
.soft_use_expires_seconds
= 0;
722 sa
->lft
.hard_use_expires_seconds
= 0;
724 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
731 case ENCR_AES_CCM_ICV16
:
732 case ENCR_AES_GCM_ICV16
:
735 case ENCR_AES_CCM_ICV12
:
736 case ENCR_AES_GCM_ICV12
:
739 case ENCR_AES_CCM_ICV8
:
740 case ENCR_AES_GCM_ICV8
:
742 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
743 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
744 if (alg_name
== NULL
)
746 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
747 encryption_algorithm_names
, enc_alg
);
750 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
751 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
753 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) + enc_key
.len
);
754 hdr
->nlmsg_len
+= rthdr
->rta_len
;
755 if (hdr
->nlmsg_len
> sizeof(request
))
760 struct xfrm_algo_aead
* algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
761 algo
->alg_key_len
= enc_key
.len
* 8;
762 algo
->alg_icv_len
= icv_size
;
763 strcpy(algo
->alg_name
, alg_name
);
764 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
766 rthdr
= XFRM_RTA_NEXT(rthdr
);
771 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
772 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
773 if (alg_name
== NULL
)
775 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
776 encryption_algorithm_names
, enc_alg
);
779 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
780 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
782 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_key
.len
);
783 hdr
->nlmsg_len
+= rthdr
->rta_len
;
784 if (hdr
->nlmsg_len
> sizeof(request
))
789 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
790 algo
->alg_key_len
= enc_key
.len
* 8;
791 strcpy(algo
->alg_name
, alg_name
);
792 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
794 rthdr
= XFRM_RTA_NEXT(rthdr
);
799 if (int_alg
!= AUTH_UNDEFINED
)
801 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
802 alg_name
= lookup_algorithm(integrity_algs
, int_alg
);
803 if (alg_name
== NULL
)
805 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
806 integrity_algorithm_names
, int_alg
);
809 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
810 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
812 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_key
.len
);
813 hdr
->nlmsg_len
+= rthdr
->rta_len
;
814 if (hdr
->nlmsg_len
> sizeof(request
))
819 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
820 algo
->alg_key_len
= int_key
.len
* 8;
821 strcpy(algo
->alg_name
, alg_name
);
822 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
824 rthdr
= XFRM_RTA_NEXT(rthdr
);
827 if (ipcomp
!= IPCOMP_NONE
)
829 rthdr
->rta_type
= XFRMA_ALG_COMP
;
830 alg_name
= lookup_algorithm(compression_algs
, ipcomp
);
831 if (alg_name
== NULL
)
833 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
834 ipcomp_transform_names
, ipcomp
);
837 DBG2(DBG_KNL
, " using compression algorithm %N",
838 ipcomp_transform_names
, ipcomp
);
840 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
841 hdr
->nlmsg_len
+= rthdr
->rta_len
;
842 if (hdr
->nlmsg_len
> sizeof(request
))
847 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
848 algo
->alg_key_len
= 0;
849 strcpy(algo
->alg_name
, alg_name
);
851 rthdr
= XFRM_RTA_NEXT(rthdr
);
856 rthdr
->rta_type
= XFRMA_ENCAP
;
857 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
859 hdr
->nlmsg_len
+= rthdr
->rta_len
;
860 if (hdr
->nlmsg_len
> sizeof(request
))
865 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
866 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
867 tmpl
->encap_sport
= htons(src
->get_port(src
));
868 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
869 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
870 /* encap_oa could probably be derived from the
871 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
872 * pluto does the same as we do here but it uses encap_oa in the
873 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
874 * the kernel ignores it anyway
875 * -> does that mean that NAT-T encap doesn't work in transport mode?
876 * No. The reason the kernel ignores NAT-OA is that it recomputes
877 * (or, rather, just ignores) the checksum. If packets pass
878 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
879 rthdr
= XFRM_RTA_NEXT(rthdr
);
882 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
884 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
891 * Get the replay state (i.e. sequence numbers) of an SA.
893 static status_t
get_replay_state(private_kernel_netlink_ipsec_t
*this,
894 u_int32_t spi
, protocol_id_t protocol
, host_t
*dst
,
895 struct xfrm_replay_state
*replay
)
897 unsigned char request
[NETLINK_BUFFER_SIZE
];
898 struct nlmsghdr
*hdr
, *out
= NULL
;
899 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
904 memset(&request
, 0, sizeof(request
));
906 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x", ntohl(spi
));
908 hdr
= (struct nlmsghdr
*)request
;
909 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
910 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
911 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
913 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
914 aevent_id
->flags
= XFRM_AE_RVAL
;
916 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
917 aevent_id
->sa_id
.spi
= spi
;
918 aevent_id
->sa_id
.proto
= proto_ike2kernel(protocol
);
919 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
921 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
924 while (NLMSG_OK(hdr
, len
))
926 switch (hdr
->nlmsg_type
)
930 out_aevent
= NLMSG_DATA(hdr
);
935 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
936 DBG1(DBG_KNL
, "querying replay state from SAD entry failed: %s (%d)",
937 strerror(-err
->error
), -err
->error
);
941 hdr
= NLMSG_NEXT(hdr
, len
);
950 if (out_aevent
== NULL
)
952 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
958 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
959 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
960 while(RTA_OK(rta
, rtasize
))
962 if (rta
->rta_type
== XFRMA_REPLAY_VAL
)
964 memcpy(replay
, RTA_DATA(rta
), rta
->rta_len
);
968 rta
= RTA_NEXT(rta
, rtasize
);
971 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
978 * Implementation of kernel_interface_t.update_sa.
980 static status_t
update_sa(private_kernel_netlink_ipsec_t
*this,
981 u_int32_t spi
, protocol_id_t protocol
,
982 host_t
*src
, host_t
*dst
,
983 host_t
*new_src
, host_t
*new_dst
, bool encap
)
985 unsigned char request
[NETLINK_BUFFER_SIZE
], *pos
;
986 struct nlmsghdr
*hdr
, *out
= NULL
;
987 struct xfrm_usersa_id
*sa_id
;
988 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
992 struct xfrm_encap_tmpl
* tmpl
= NULL
;
993 bool got_replay_state
;
994 struct xfrm_replay_state replay
;
996 memset(&request
, 0, sizeof(request
));
998 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1000 /* query the existing SA first */
1001 hdr
= (struct nlmsghdr
*)request
;
1002 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1003 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1004 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1006 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1007 host2xfrm(dst
, &sa_id
->daddr
);
1009 sa_id
->proto
= proto_ike2kernel(protocol
);
1010 sa_id
->family
= dst
->get_family(dst
);
1012 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1015 while (NLMSG_OK(hdr
, len
))
1017 switch (hdr
->nlmsg_type
)
1019 case XFRM_MSG_NEWSA
:
1021 out_sa
= NLMSG_DATA(hdr
);
1026 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1027 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1028 strerror(-err
->error
), -err
->error
);
1032 hdr
= NLMSG_NEXT(hdr
, len
);
1042 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1047 /* try to get the replay state */
1048 got_replay_state
= (get_replay_state(
1049 this, spi
, protocol
, dst
, &replay
) == SUCCESS
);
1051 /* delete the old SA */
1052 if (this->public.interface
.del_sa(&this->public.interface
, dst
, spi
, protocol
) != SUCCESS
)
1054 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x", ntohl(spi
));
1059 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1060 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1062 /* copy over the SA from out to request */
1063 hdr
= (struct nlmsghdr
*)request
;
1064 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1065 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1066 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1067 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1068 sa
= NLMSG_DATA(hdr
);
1069 sa
->family
= new_dst
->get_family(new_dst
);
1071 if (!src
->ip_equals(src
, new_src
))
1073 host2xfrm(new_src
, &sa
->saddr
);
1075 if (!dst
->ip_equals(dst
, new_dst
))
1077 host2xfrm(new_dst
, &sa
->id
.daddr
);
1080 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1081 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1082 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1083 while(RTA_OK(rta
, rtasize
))
1085 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1086 if (rta
->rta_type
!= XFRMA_ENCAP
|| encap
)
1088 if (rta
->rta_type
== XFRMA_ENCAP
)
1089 { /* update encap tmpl */
1090 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1091 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1092 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1094 memcpy(pos
, rta
, rta
->rta_len
);
1095 pos
+= RTA_ALIGN(rta
->rta_len
);
1096 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1098 rta
= RTA_NEXT(rta
, rtasize
);
1101 rta
= (struct rtattr
*)pos
;
1102 if (tmpl
== NULL
&& encap
)
1103 { /* add tmpl if we are enabling it */
1104 rta
->rta_type
= XFRMA_ENCAP
;
1105 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1107 hdr
->nlmsg_len
+= rta
->rta_len
;
1108 if (hdr
->nlmsg_len
> sizeof(request
))
1113 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1114 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1115 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1116 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1117 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1119 rta
= XFRM_RTA_NEXT(rta
);
1122 if (got_replay_state
)
1123 { /* copy the replay data if available */
1124 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1125 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1127 hdr
->nlmsg_len
+= rta
->rta_len
;
1128 if (hdr
->nlmsg_len
> sizeof(request
))
1132 memcpy(RTA_DATA(rta
), &replay
, sizeof(replay
));
1134 rta
= XFRM_RTA_NEXT(rta
);
1137 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1139 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1149 * Implementation of kernel_interface_t.del_sa.
1151 static status_t
del_sa(private_kernel_netlink_ipsec_t
*this, host_t
*dst
,
1152 u_int32_t spi
, protocol_id_t protocol
)
1154 unsigned char request
[NETLINK_BUFFER_SIZE
];
1155 struct nlmsghdr
*hdr
;
1156 struct xfrm_usersa_id
*sa_id
;
1158 memset(&request
, 0, sizeof(request
));
1160 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1162 hdr
= (struct nlmsghdr
*)request
;
1163 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1164 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1165 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1167 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1168 host2xfrm(dst
, &sa_id
->daddr
);
1170 sa_id
->proto
= proto_ike2kernel(protocol
);
1171 sa_id
->family
= dst
->get_family(dst
);
1173 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1175 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1178 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1183 * Implementation of kernel_interface_t.add_policy.
1185 static status_t
add_policy(private_kernel_netlink_ipsec_t
*this,
1186 host_t
*src
, host_t
*dst
,
1187 traffic_selector_t
*src_ts
,
1188 traffic_selector_t
*dst_ts
,
1189 policy_dir_t direction
, protocol_id_t protocol
,
1190 u_int32_t reqid
, bool high_prio
, ipsec_mode_t mode
,
1193 iterator_t
*iterator
;
1194 policy_entry_t
*current
, *policy
;
1196 unsigned char request
[NETLINK_BUFFER_SIZE
];
1197 struct xfrm_userpolicy_info
*policy_info
;
1198 struct nlmsghdr
*hdr
;
1200 /* create a policy */
1201 policy
= malloc_thing(policy_entry_t
);
1202 memset(policy
, 0, sizeof(policy_entry_t
));
1203 policy
->sel
= ts2selector(src_ts
, dst_ts
);
1204 policy
->direction
= direction
;
1206 /* find the policy, which matches EXACTLY */
1207 pthread_mutex_lock(&this->mutex
);
1208 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
1209 while (iterator
->iterate(iterator
, (void**)¤t
))
1211 if (memeq(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) &&
1212 policy
->direction
== current
->direction
)
1214 /* use existing policy */
1215 current
->refcount
++;
1216 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
1217 "refcount", src_ts
, dst_ts
,
1218 policy_dir_names
, direction
);
1225 iterator
->destroy(iterator
);
1227 { /* apply the new one, if we have no such policy */
1228 this->policies
->insert_last(this->policies
, policy
);
1229 policy
->refcount
= 1;
1232 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
1233 policy_dir_names
, direction
);
1235 memset(&request
, 0, sizeof(request
));
1236 hdr
= (struct nlmsghdr
*)request
;
1237 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1238 hdr
->nlmsg_type
= found ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
1239 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1241 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1242 policy_info
->sel
= policy
->sel
;
1243 policy_info
->dir
= policy
->direction
;
1244 /* calculate priority based on source selector size, small size = high prio */
1245 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
1246 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
1247 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
1248 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
1249 policy_info
->action
= XFRM_POLICY_ALLOW
;
1250 policy_info
->share
= XFRM_SHARE_ANY
;
1251 pthread_mutex_unlock(&this->mutex
);
1253 /* policies don't expire */
1254 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
1255 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
1256 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
1257 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
1258 policy_info
->lft
.soft_add_expires_seconds
= 0;
1259 policy_info
->lft
.hard_add_expires_seconds
= 0;
1260 policy_info
->lft
.soft_use_expires_seconds
= 0;
1261 policy_info
->lft
.hard_use_expires_seconds
= 0;
1263 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
1264 rthdr
->rta_type
= XFRMA_TMPL
;
1265 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1267 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1268 if (hdr
->nlmsg_len
> sizeof(request
))
1273 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
1275 if (ipcomp
!= IPCOMP_NONE
)
1277 tmpl
->reqid
= reqid
;
1278 tmpl
->id
.proto
= IPPROTO_COMP
;
1279 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1281 tmpl
->optional
= direction
!= POLICY_OUT
;
1282 tmpl
->family
= src
->get_family(src
);
1284 host2xfrm(src
, &tmpl
->saddr
);
1285 host2xfrm(dst
, &tmpl
->id
.daddr
);
1287 /* add an additional xfrm_user_tmpl */
1288 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1289 hdr
->nlmsg_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1290 if (hdr
->nlmsg_len
> sizeof(request
))
1298 tmpl
->reqid
= reqid
;
1299 tmpl
->id
.proto
= proto_ike2kernel(protocol
);
1300 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1302 tmpl
->family
= src
->get_family(src
);
1304 host2xfrm(src
, &tmpl
->saddr
);
1305 host2xfrm(dst
, &tmpl
->id
.daddr
);
1307 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1309 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
1310 policy_dir_names
, direction
);
1314 /* install a route, if:
1315 * - we are NOT updating a policy
1316 * - this is a forward policy (to just get one for each child)
1317 * - we are in tunnel mode
1318 * - we are not using IPv6 (does not work correctly yet!)
1319 * - routing is not disabled via strongswan.conf
1321 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1322 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
1323 this->install_routes
)
1325 route_entry_t
*route
= malloc_thing(route_entry_t
);
1327 if (charon
->kernel_interface
->get_address_by_ts(charon
->kernel_interface
,
1328 dst_ts
, &route
->src_ip
) == SUCCESS
)
1330 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1331 route
->gateway
= charon
->kernel_interface
->get_nexthop(
1332 charon
->kernel_interface
, src
);
1333 route
->if_name
= charon
->kernel_interface
->get_interface(
1334 charon
->kernel_interface
, dst
);
1335 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
1336 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
1337 route
->prefixlen
= policy
->sel
.prefixlen_s
;
1339 switch (charon
->kernel_interface
->add_route(charon
->kernel_interface
,
1340 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1341 route
->src_ip
, route
->if_name
))
1344 DBG1(DBG_KNL
, "unable to install source route for %H",
1348 /* route exists, do not uninstall */
1349 route_entry_destroy(route
);
1352 /* cache the installed route */
1353 policy
->route
= route
;
1367 * Implementation of kernel_interface_t.query_policy.
1369 static status_t
query_policy(private_kernel_netlink_ipsec_t
*this,
1370 traffic_selector_t
*src_ts
,
1371 traffic_selector_t
*dst_ts
,
1372 policy_dir_t direction
, u_int32_t
*use_time
)
1374 unsigned char request
[NETLINK_BUFFER_SIZE
];
1375 struct nlmsghdr
*out
= NULL
, *hdr
;
1376 struct xfrm_userpolicy_id
*policy_id
;
1377 struct xfrm_userpolicy_info
*policy
= NULL
;
1380 memset(&request
, 0, sizeof(request
));
1382 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
1383 policy_dir_names
, direction
);
1385 hdr
= (struct nlmsghdr
*)request
;
1386 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1387 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
1388 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1390 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1391 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
1392 policy_id
->dir
= direction
;
1394 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1397 while (NLMSG_OK(hdr
, len
))
1399 switch (hdr
->nlmsg_type
)
1401 case XFRM_MSG_NEWPOLICY
:
1403 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1408 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1409 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
1410 strerror(-err
->error
), -err
->error
);
1414 hdr
= NLMSG_NEXT(hdr
, len
);
1425 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
1426 policy_dir_names
, direction
);
1430 *use_time
= (time_t)policy
->curlft
.use_time
;
1437 * Implementation of kernel_interface_t.del_policy.
1439 static status_t
del_policy(private_kernel_netlink_ipsec_t
*this,
1440 traffic_selector_t
*src_ts
,
1441 traffic_selector_t
*dst_ts
,
1442 policy_dir_t direction
)
1444 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
1445 route_entry_t
*route
;
1446 unsigned char request
[NETLINK_BUFFER_SIZE
];
1447 struct nlmsghdr
*hdr
;
1448 struct xfrm_userpolicy_id
*policy_id
;
1449 iterator_t
*iterator
;
1451 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
1452 policy_dir_names
, direction
);
1454 /* create a policy */
1455 memset(&policy
, 0, sizeof(policy_entry_t
));
1456 policy
.sel
= ts2selector(src_ts
, dst_ts
);
1457 policy
.direction
= direction
;
1459 /* find the policy */
1460 iterator
= this->policies
->create_iterator_locked(this->policies
, &this->mutex
);
1461 while (iterator
->iterate(iterator
, (void**)¤t
))
1463 if (memeq(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) &&
1464 policy
.direction
== current
->direction
)
1466 to_delete
= current
;
1467 if (--to_delete
->refcount
> 0)
1469 /* is used by more SAs, keep in kernel */
1470 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1471 iterator
->destroy(iterator
);
1474 /* remove if last reference */
1475 iterator
->remove(iterator
);
1479 iterator
->destroy(iterator
);
1482 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
1483 dst_ts
, policy_dir_names
, direction
);
1487 memset(&request
, 0, sizeof(request
));
1489 hdr
= (struct nlmsghdr
*)request
;
1490 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1491 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
1492 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1494 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1495 policy_id
->sel
= to_delete
->sel
;
1496 policy_id
->dir
= direction
;
1498 route
= to_delete
->route
;
1501 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1503 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
1504 policy_dir_names
, direction
);
1510 if (charon
->kernel_interface
->del_route(charon
->kernel_interface
,
1511 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1512 route
->src_ip
, route
->if_name
) != SUCCESS
)
1514 DBG1(DBG_KNL
, "error uninstalling route installed with "
1515 "policy %R === %R %N", src_ts
, dst_ts
,
1516 policy_dir_names
, direction
);
1518 route_entry_destroy(route
);
1524 * Implementation of kernel_interface_t.destroy.
1526 static void destroy(private_kernel_netlink_ipsec_t
*this)
1528 this->job
->cancel(this->job
);
1529 close(this->socket_xfrm_events
);
1530 this->socket_xfrm
->destroy(this->socket_xfrm
);
1531 this->policies
->destroy(this->policies
);
1536 * Described in header.
1538 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
1540 private_kernel_netlink_ipsec_t
*this = malloc_thing(private_kernel_netlink_ipsec_t
);
1541 struct sockaddr_nl addr
;
1543 /* public functions */
1544 this->public.interface
.get_spi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
1545 this->public.interface
.get_cpi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
1546 this->public.interface
.add_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
,u_int64_t
,u_int64_t
,u_int16_t
,chunk_t
,u_int16_t
,chunk_t
,ipsec_mode_t
,u_int16_t
,bool,bool))add_sa
;
1547 this->public.interface
.update_sa
= (status_t(*)(kernel_ipsec_t
*,u_int32_t
,protocol_id_t
,host_t
*,host_t
*,host_t
*,host_t
*,bool))update_sa
;
1548 this->public.interface
.del_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
1549 this->public.interface
.add_policy
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,protocol_id_t
,u_int32_t
,bool,ipsec_mode_t
,u_int16_t
))add_policy
;
1550 this->public.interface
.query_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
1551 this->public.interface
.del_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
1552 this->public.interface
.destroy
= (void(*)(kernel_ipsec_t
*)) destroy
;
1554 /* private members */
1555 this->policies
= linked_list_create();
1556 pthread_mutex_init(&this->mutex
, NULL
);
1557 this->install_routes
= lib
->settings
->get_bool(lib
->settings
,
1558 "charon.install_routes", TRUE
);
1560 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
1562 memset(&addr
, 0, sizeof(addr
));
1563 addr
.nl_family
= AF_NETLINK
;
1565 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
1566 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
1567 if (this->socket_xfrm_events
<= 0)
1569 charon
->kill(charon
, "unable to create XFRM event socket");
1571 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) | XFRMNLGRP(MAPPING
);
1572 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1574 charon
->kill(charon
, "unable to bind XFRM event socket");
1577 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
1579 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
1581 return &this->public;