2 * @file kernel_interface.c
4 * @brief Implementation of kernel_interface_t.
9 * Copyright (C) 2005-2007 Martin Willi
10 * Copyright (C) 2006-2007 Tobias Brunner
11 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
12 * Copyright (C) 2006 Daniel Roethlisberger
13 * Copyright (C) 2005 Jan Hutter
14 * Hochschule fuer Technik Rapperswil
15 * Copyright (C) 2003 Herbert Xu.
17 * Based on xfrm code from pluto.
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the
21 * Free Software Foundation; either version 2 of the License, or (at your
22 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/xfrm.h>
35 #include <linux/udp.h>
42 #include <sys/ioctl.h>
44 #include "kernel_interface.h"
47 #include <utils/linked_list.h>
48 #include <processing/jobs/delete_child_sa_job.h>
49 #include <processing/jobs/rekey_child_sa_job.h>
50 #include <processing/jobs/acquire_job.h>
51 #include <processing/jobs/callback_job.h>
53 /** kernel level protocol identifiers */
57 /** default priority of installed policies */
59 #define PRIO_HIGH 2000
61 #define BUFFER_SIZE 1024
64 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
65 * 'usual' netlink data x like 'struct xfrm_usersa_info'
67 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
69 * returns a pointer to the next rtattr following rta.
70 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
72 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
74 * returns the total size of attached rta data
75 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
77 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
79 typedef struct kernel_algorithm_t kernel_algorithm_t
;
82 * Mapping from the algorithms defined in IKEv2 to
83 * kernel level algorithm names and their key length
85 struct kernel_algorithm_t
{
87 * Identifier specified in IKEv2
92 * Name of the algorithm, as used as kernel identifier
97 * Key length in bits, if fixed size
101 #define END_OF_LIST -1
104 * Algorithms for encryption
106 kernel_algorithm_t encryption_algs
[] = {
107 /* {ENCR_DES_IV64, "***", 0}, */
108 {ENCR_DES
, "des", 64},
109 {ENCR_3DES
, "des3_ede", 192},
110 /* {ENCR_RC5, "***", 0}, */
111 /* {ENCR_IDEA, "***", 0}, */
112 {ENCR_CAST
, "cast128", 0},
113 {ENCR_BLOWFISH
, "blowfish", 0},
114 /* {ENCR_3IDEA, "***", 0}, */
115 /* {ENCR_DES_IV32, "***", 0}, */
116 {ENCR_NULL
, "cipher_null", 0},
117 {ENCR_AES_CBC
, "aes", 0},
118 /* {ENCR_AES_CTR, "***", 0}, */
119 {END_OF_LIST
, NULL
, 0},
123 * Algorithms for integrity protection
125 kernel_algorithm_t integrity_algs
[] = {
126 {AUTH_HMAC_MD5_96
, "md5", 128},
127 {AUTH_HMAC_SHA1_96
, "sha1", 160},
128 {AUTH_HMAC_SHA2_256_128
, "sha256", 256},
129 {AUTH_HMAC_SHA2_384_192
, "sha384", 384},
130 {AUTH_HMAC_SHA2_512_256
, "sha512", 512},
131 /* {AUTH_DES_MAC, "***", 0}, */
132 /* {AUTH_KPDK_MD5, "***", 0}, */
133 {AUTH_AES_XCBC_96
, "xcbc(aes)", 128},
134 {END_OF_LIST
, NULL
, 0},
138 * Look up a kernel algorithm name and its key size
140 char* lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
141 algorithm_t
*ikev2_algo
, u_int
*key_size
)
143 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
145 if (ikev2_algo
->algorithm
== kernel_algo
->ikev2_id
)
147 /* match, evaluate key length */
148 if (ikev2_algo
->key_size
)
149 { /* variable length */
150 *key_size
= ikev2_algo
->key_size
;
154 *key_size
= kernel_algo
->key_size
;
156 return kernel_algo
->name
;
163 typedef struct route_entry_t route_entry_t
;
166 * installed routing entry
168 struct route_entry_t
{
170 /** Index of the interface the route is bound to */
173 /** Source ip of the route */
176 /** gateway for this route */
179 /** Destination net */
182 /** Destination net prefixlen */
187 * destroy an route_entry_t object
189 static void route_entry_destroy(route_entry_t
*this)
191 this->src_ip
->destroy(this->src_ip
);
192 this->gateway
->destroy(this->gateway
);
193 chunk_free(&this->dst_net
);
197 typedef struct policy_entry_t policy_entry_t
;
200 * installed kernel policy.
202 struct policy_entry_t
{
204 /** direction of this policy: in, out, forward */
207 /** reqid of the policy */
210 /** parameters of installed policy */
211 struct xfrm_selector sel
;
213 /** associated route installed for this policy */
214 route_entry_t
*route
;
216 /** by how many CHILD_SA's this policy is used */
220 typedef struct vip_entry_t vip_entry_t
;
223 * Installed virtual ip
226 /** Index of the interface the ip is bound to */
229 /** The ip address */
232 /** Number of times this IP is used */
237 * destroy a vip_entry_t object
239 static void vip_entry_destroy(vip_entry_t
*this)
241 this->ip
->destroy(this->ip
);
245 typedef struct address_entry_t address_entry_t
;
248 * an address found on the system, containg address and interface info
250 struct address_entry_t
{
252 /** address of this entry */
255 /** interface index */
258 /** name of the index */
259 char ifname
[IFNAMSIZ
];
263 * destroy an address entry
265 static void address_entry_destroy(address_entry_t
*this)
267 this->host
->destroy(this->host
);
271 typedef struct private_kernel_interface_t private_kernel_interface_t
;
274 * Private variables and functions of kernel_interface class.
276 struct private_kernel_interface_t
{
278 * Public part of the kernel_interface_t object.
280 kernel_interface_t
public;
283 * List of installed policies (kernel_entry_t)
285 linked_list_t
*policies
;
288 * Mutex locks access to policies
290 pthread_mutex_t policies_mutex
;
293 * List of installed virtual IPs. (vip_entry_t)
298 * Mutex to lock access to vips.
300 pthread_mutex_t vips_mutex
;
303 * Cached list of IP adresses (address_entry_t)
305 linked_list_t
*addrs
;
308 * Mutex to lock access to addr.
310 pthread_mutex_t addrs_mutex
;
313 * job receiving xfrm events
318 * Netlink xfrm socket (IPsec)
323 * netlink xfrm socket to receive acquire and expire events
325 int socket_xfrm_events
;
328 * Netlink rt socket (routing)
333 * Netlink rt socket to receive address change events
335 int socket_rt_events
;
339 * convert a host_t to a struct xfrm_address
341 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
343 chunk_t chunk
= host
->get_address(host
);
344 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
348 * convert a traffic selector address range to subnet and its mask.
350 static void ts2subnet(traffic_selector_t
* ts
,
351 xfrm_address_t
*net
, u_int8_t
*mask
)
353 /* there is no way to do this cleanly, as the address range may
354 * be anything else but a subnet. We use from_addr as subnet
355 * and try to calculate a usable subnet mask.
360 size_t size
= (ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE
) ?
4 : 16;
362 from
= ts
->get_from_address(ts
);
363 to
= ts
->get_to_address(ts
);
366 /* go trough all bits of the addresses, beginning in the front.
367 * as long as they are equal, the subnet gets larger
369 for (byte
= 0; byte
< size
; byte
++)
371 for (bit
= 7; bit
>= 0; bit
--)
373 if ((1<<bit
& from
.ptr
[byte
]) != (1<<bit
& to
.ptr
[byte
]))
375 *mask
= ((7 - bit
) + (byte
* 8));
385 memcpy(net
, from
.ptr
, from
.len
);
391 * convert a traffic selector port range to port/portmask
393 static void ts2ports(traffic_selector_t
* ts
,
394 u_int16_t
*port
, u_int16_t
*mask
)
396 /* linux does not seem to accept complex portmasks. Only
397 * any or a specific port is allowed. We set to any, if we have
398 * a port range, or to a specific, if we have one port only.
402 from
= ts
->get_from_port(ts
);
403 to
= ts
->get_to_port(ts
);
418 * convert a pair of traffic_selectors to a xfrm_selector
420 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
421 traffic_selector_t
*dst
)
423 struct xfrm_selector sel
;
425 memset(&sel
, 0, sizeof(sel
));
426 sel
.family
= src
->get_type(src
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
427 /* src or dest proto may be "any" (0), use more restrictive one */
428 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
429 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
430 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
431 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
432 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
440 * Creates an rtattr and adds it to the netlink message
442 static void add_attribute(struct nlmsghdr
*hdr
, int rta_type
, chunk_t data
,
447 if (NLMSG_ALIGN(hdr
->nlmsg_len
) + RTA_ALIGN(data
.len
) > buflen
)
449 DBG1(DBG_KNL
, "unable to add attribute, buffer too small");
453 rta
= (struct rtattr
*)(((char*)hdr
) + NLMSG_ALIGN(hdr
->nlmsg_len
));
454 rta
->rta_type
= rta_type
;
455 rta
->rta_len
= RTA_LENGTH(data
.len
);
456 memcpy(RTA_DATA(rta
), data
.ptr
, data
.len
);
457 hdr
->nlmsg_len
= NLMSG_ALIGN(hdr
->nlmsg_len
) + rta
->rta_len
;
461 * process a XFRM_MSG_ACQUIRE from kernel
463 static void process_acquire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
467 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
468 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
470 if (RTA_OK(rtattr
, rtsize
))
472 if (rtattr
->rta_type
== XFRMA_TMPL
)
474 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rtattr
);
480 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
483 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
484 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid %d", reqid
);
485 job
= (job_t
*)acquire_job_create(reqid
);
486 charon
->processor
->queue_job(charon
->processor
, job
);
490 * process a XFRM_MSG_EXPIRE from kernel
492 static void process_expire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
495 protocol_id_t protocol
;
496 u_int32_t spi
, reqid
;
497 struct xfrm_user_expire
*expire
;
499 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
500 protocol
= expire
->state
.id
.proto
== KERNEL_ESP ? PROTO_ESP
: PROTO_AH
;
501 spi
= expire
->state
.id
.spi
;
502 reqid
= expire
->state
.reqid
;
504 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
505 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
506 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
507 protocol
, ntohl(spi
), reqid
);
510 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
514 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
516 charon
->processor
->queue_job(charon
->processor
, job
);
520 * process a RTM_NEWADDR from kernel
522 static void process_newaddr(private_kernel_interface_t
*this,
523 struct nlmsghdr
*hdr
, bool initial
)
525 struct ifaddrmsg
* msg
= (struct ifaddrmsg
*)(NLMSG_DATA(hdr
));
526 struct rtattr
*rta
= IFA_RTA(msg
);
527 size_t rtasize
= IFA_PAYLOAD (hdr
);
530 chunk_t local
= chunk_empty
, address
= chunk_empty
;
532 while(RTA_OK(rta
, rtasize
))
534 switch (rta
->rta_type
)
537 local
.ptr
= RTA_DATA(rta
);
538 local
.len
= RTA_PAYLOAD(rta
);
541 address
.ptr
= RTA_DATA(rta
);
542 address
.len
= RTA_PAYLOAD(rta
);
545 name
= RTA_DATA(rta
);
548 rta
= RTA_NEXT(rta
, rtasize
);
551 /* For PPP interfaces, we need the IFA_LOCAL address,
552 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
553 * not included in all cases, so fallback to IFA_ADDRESS. */
556 host
= host_create_from_chunk(msg
->ifa_family
, local
, 0);
558 else if (address
.ptr
)
560 host
= host_create_from_chunk(msg
->ifa_family
, address
, 0);
565 address_entry_t
*entry
;
567 entry
= malloc_thing(address_entry_t
);
569 entry
->ifindex
= msg
->ifa_index
;
572 memcpy(entry
->ifname
, name
, IFNAMSIZ
);
576 strcpy(entry
->ifname
, "(unknown)");
581 DBG1(DBG_KNL
, " %H on %s", host
, entry
->ifname
);
585 DBG1(DBG_KNL
, "%H appeared on %s", host
, entry
->ifname
);
588 pthread_mutex_lock(&this->addrs_mutex
);
589 this->addrs
->insert_last(this->addrs
, entry
);
590 pthread_mutex_unlock(&this->addrs_mutex
);
596 * process a RTM_DELADDR from kernel
598 static void process_deladdr(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
600 struct ifaddrmsg
* msg
= (struct ifaddrmsg
*)(NLMSG_DATA(hdr
));
601 struct rtattr
*rta
= IFA_RTA(msg
);
602 size_t rtasize
= IFA_PAYLOAD (hdr
);
604 chunk_t local
= chunk_empty
, address
= chunk_empty
;
606 while(RTA_OK(rta
, rtasize
))
608 switch (rta
->rta_type
)
611 local
.ptr
= RTA_DATA(rta
);
612 local
.len
= RTA_PAYLOAD(rta
);
615 address
.ptr
= RTA_DATA(rta
);
616 address
.len
= RTA_PAYLOAD(rta
);
619 rta
= RTA_NEXT(rta
, rtasize
);
622 /* same stuff as in newaddr */
625 host
= host_create_from_chunk(msg
->ifa_family
, local
, 0);
627 else if (address
.ptr
)
629 host
= host_create_from_chunk(msg
->ifa_family
, address
, 0);
634 address_entry_t
*entry
;
635 iterator_t
*iterator
;
637 iterator
= this->addrs
->create_iterator_locked(this->addrs
,
639 while (iterator
->iterate(iterator
, (void**)&entry
))
641 if (entry
->ifindex
== msg
->ifa_index
&&
642 host
->equals(host
, entry
->host
))
644 iterator
->remove(iterator
);
645 DBG1(DBG_KNL
, "%H disappeared from %s", host
, entry
->ifname
);
646 address_entry_destroy(entry
);
649 iterator
->destroy(iterator
);
655 * Receives events from kernel
657 static job_requeue_t
receive_events(private_kernel_interface_t
*this)
660 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
661 struct sockaddr_nl addr
;
662 socklen_t addr_len
= sizeof(addr
);
663 int len
, oldstate
, maxfd
, selected
;
667 FD_SET(this->socket_xfrm_events
, &rfds
);
668 FD_SET(this->socket_rt_events
, &rfds
);
669 maxfd
= max(this->socket_xfrm_events
, this->socket_rt_events
);
671 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
672 selected
= select(maxfd
+ 1, &rfds
, NULL
, NULL
, NULL
);
673 pthread_setcancelstate(oldstate
, NULL
);
676 DBG1(DBG_KNL
, "selecting on sockets failed: %s", strerror(errno
));
677 return JOB_REQUEUE_FAIR
;
679 if (FD_ISSET(this->socket_xfrm_events
, &rfds
))
681 selected
= this->socket_xfrm_events
;
683 else if (FD_ISSET(this->socket_rt_events
, &rfds
))
685 selected
= this->socket_rt_events
;
689 return JOB_REQUEUE_DIRECT
;
692 len
= recvfrom(selected
, response
, sizeof(response
), MSG_DONTWAIT
,
693 (struct sockaddr
*)&addr
, &addr_len
);
699 /* interrupted, try again */
700 return JOB_REQUEUE_DIRECT
;
702 /* no data ready, select again */
703 return JOB_REQUEUE_DIRECT
;
705 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
707 return JOB_REQUEUE_FAIR
;
710 if (addr
.nl_pid
!= 0)
711 { /* not from kernel. not interested, try another one */
712 return JOB_REQUEUE_DIRECT
;
715 while (NLMSG_OK(hdr
, len
))
717 /* looks good so far, dispatch netlink message */
718 if (selected
== this->socket_xfrm_events
)
720 switch (hdr
->nlmsg_type
)
722 case XFRM_MSG_ACQUIRE
:
723 process_acquire(this, hdr
);
725 case XFRM_MSG_EXPIRE
:
726 process_expire(this, hdr
);
732 else if (selected
== this->socket_rt_events
)
734 switch (hdr
->nlmsg_type
)
737 process_newaddr(this, hdr
, FALSE
);
740 process_deladdr(this, hdr
);
746 hdr
= NLMSG_NEXT(hdr
, len
);
749 return JOB_REQUEUE_DIRECT
;
753 * send a netlink message and wait for a reply
755 static status_t
netlink_send(int socket
, struct nlmsghdr
*in
,
756 struct nlmsghdr
**out
, size_t *out_len
)
759 struct sockaddr_nl addr
;
760 chunk_t result
= chunk_empty
, tmp
;
761 struct nlmsghdr
*msg
, peek
;
763 static int seq
= 200;
764 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
767 pthread_mutex_lock(&mutex
);
769 in
->nlmsg_seq
= ++seq
;
770 in
->nlmsg_pid
= getpid();
772 memset(&addr
, 0, sizeof(addr
));
773 addr
.nl_family
= AF_NETLINK
;
779 len
= sendto(socket
, in
, in
->nlmsg_len
, 0,
780 (struct sockaddr
*)&addr
, sizeof(addr
));
782 if (len
!= in
->nlmsg_len
)
786 /* interrupted, try again */
789 pthread_mutex_unlock(&mutex
);
790 DBG1(DBG_KNL
, "error sending to netlink socket: %s", strerror(errno
));
799 tmp
.len
= sizeof(buf
);
801 msg
= (struct nlmsghdr
*)tmp
.ptr
;
803 memset(&addr
, 0, sizeof(addr
));
804 addr
.nl_family
= AF_NETLINK
;
805 addr
.nl_pid
= getpid();
807 addr_len
= sizeof(addr
);
809 len
= recvfrom(socket
, tmp
.ptr
, tmp
.len
, 0,
810 (struct sockaddr
*)&addr
, &addr_len
);
816 DBG1(DBG_IKE
, "got interrupted");
817 /* interrupted, try again */
820 DBG1(DBG_IKE
, "error reading from netlink socket: %s", strerror(errno
));
821 pthread_mutex_unlock(&mutex
);
824 if (!NLMSG_OK(msg
, len
))
826 DBG1(DBG_IKE
, "received corrupted netlink message");
827 pthread_mutex_unlock(&mutex
);
830 if (msg
->nlmsg_seq
!= seq
)
832 DBG1(DBG_IKE
, "received invalid netlink sequence number");
833 if (msg
->nlmsg_seq
< seq
)
837 pthread_mutex_unlock(&mutex
);
842 result
= chunk_cata("cc", result
, tmp
);
844 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
845 * numbers to detect multi header messages */
846 len
= recvfrom(socket
, &peek
, sizeof(peek
), MSG_PEEK
| MSG_DONTWAIT
,
847 (struct sockaddr
*)&addr
, &addr_len
);
849 if (len
== sizeof(peek
) && peek
.nlmsg_seq
== seq
)
851 /* seems to be multipart */
857 *out_len
= result
.len
;
858 *out
= (struct nlmsghdr
*)clalloc(result
.ptr
, result
.len
);
860 pthread_mutex_unlock(&mutex
);
866 * send a netlink message and wait for its acknowlegde
868 static status_t
netlink_send_ack(int socket
, struct nlmsghdr
*in
)
870 struct nlmsghdr
*out
, *hdr
;
873 if (netlink_send(socket
, in
, &out
, &len
) != SUCCESS
)
878 while (NLMSG_OK(hdr
, len
))
880 switch (hdr
->nlmsg_type
)
884 struct nlmsgerr
* err
= (struct nlmsgerr
*)NLMSG_DATA(hdr
);
888 DBG1(DBG_KNL
, "received netlink error: %s (%d)",
889 strerror(-err
->error
), -err
->error
);
897 hdr
= NLMSG_NEXT(hdr
, len
);
904 DBG1(DBG_KNL
, "netlink request not acknowlegded");
910 * Initialize a list of local addresses.
912 static void init_address_list(private_kernel_interface_t
*this)
914 char request
[BUFFER_SIZE
];
915 struct nlmsghdr
*out
, *hdr
;
916 struct rtgenmsg
*msg
;
919 DBG1(DBG_IKE
, "listening on interfaces:");
921 memset(&request
, 0, sizeof(request
));
923 hdr
= (struct nlmsghdr
*)&request
;
924 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtgenmsg
));
925 hdr
->nlmsg_type
= RTM_GETADDR
;
926 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_MATCH
| NLM_F_ROOT
;
927 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
928 msg
->rtgen_family
= AF_UNSPEC
;
930 if (netlink_send(this->socket_rt
, hdr
, &out
, &len
) == SUCCESS
)
933 while (NLMSG_OK(hdr
, len
))
935 switch (hdr
->nlmsg_type
)
939 process_newaddr(this, hdr
, TRUE
);
940 hdr
= NLMSG_NEXT(hdr
, len
);
944 hdr
= NLMSG_NEXT(hdr
, len
);
955 DBG1(DBG_IKE
, "unable to get local address list");
960 * iterator hook to return address, not address_entry_t
962 bool address_iterator_hook(private_kernel_interface_t
*this,
963 address_entry_t
*in
, host_t
**out
)
970 * Implements kernel_interface_t.create_address_iterator.
972 static iterator_t
*create_address_iterator(private_kernel_interface_t
*this)
974 iterator_t
*iterator
;
976 iterator
= this->addrs
->create_iterator_locked(this->addrs
, &this->addrs_mutex
);
977 iterator
->set_iterator_hook(iterator
,
978 (iterator_hook_t
*)address_iterator_hook
, this);
983 * implementation of kernel_interface_t.get_interface_name
985 static char *get_interface_name(private_kernel_interface_t
*this, host_t
* ip
)
987 iterator_t
*iterator
;
988 address_entry_t
*entry
;
991 DBG2(DBG_IKE
, "getting interface name for %H", ip
);
993 iterator
= this->addrs
->create_iterator_locked(this->addrs
, &this->addrs_mutex
);
994 while (iterator
->iterate(iterator
, (void**)&entry
))
996 if (ip
->ip_equals(ip
, entry
->host
))
998 name
= strdup(entry
->ifname
);
1002 iterator
->destroy(iterator
);
1006 DBG2(DBG_IKE
, "%H is on interface %s", ip
, name
);
1010 DBG2(DBG_IKE
, "%H is not a local address", ip
);
1016 * Tries to find an ip address of a local interface that is included in the
1017 * supplied traffic selector.
1019 static status_t
get_address_by_ts(private_kernel_interface_t
*this,
1020 traffic_selector_t
*ts
, host_t
**ip
)
1022 iterator_t
*iterator
;
1023 address_entry_t
*entry
;
1028 DBG2(DBG_IKE
, "getting a local address in traffic selector %R", ts
);
1030 /* if we have a family which includes localhost, we do not
1031 * search for an IP, we use the default */
1032 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
1034 if (family
== AF_INET
)
1036 host
= host_create_from_string("127.0.0.1", 0);
1040 host
= host_create_from_string("::1", 0);
1043 if (ts
->includes(ts
, host
))
1045 *ip
= host_create_any(family
);
1046 host
->destroy(host
);
1047 DBG2(DBG_IKE
, "using host %H", *ip
);
1050 host
->destroy(host
);
1052 iterator
= this->addrs
->create_iterator_locked(this->addrs
, &this->addrs_mutex
);
1053 while (iterator
->iterate(iterator
, (void**)&entry
))
1055 if (ts
->includes(ts
, entry
->host
))
1058 *ip
= entry
->host
->clone(entry
->host
);
1062 iterator
->destroy(iterator
);
1066 DBG1(DBG_IKE
, "no local address found in traffic selector %R", ts
);
1069 DBG2(DBG_IKE
, "using host %H", *ip
);
1074 * get the interface of a local address
1076 static int get_interface_index(private_kernel_interface_t
*this, host_t
* ip
)
1078 iterator_t
*iterator
;
1079 address_entry_t
*entry
;
1082 DBG2(DBG_IKE
, "getting iface for %H", ip
);
1084 iterator
= this->addrs
->create_iterator_locked(this->addrs
, &this->addrs_mutex
);
1085 while (iterator
->iterate(iterator
, (void**)&entry
))
1087 if (ip
->ip_equals(ip
, entry
->host
))
1089 ifindex
= entry
->ifindex
;
1093 iterator
->destroy(iterator
);
1097 DBG1(DBG_IKE
, "unable to get interface for %H", ip
);
1103 * Manages the creation and deletion of ip addresses on an interface.
1104 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1106 static status_t
manage_ipaddr(private_kernel_interface_t
*this, int nlmsg_type
,
1107 int flags
, int if_index
, host_t
*ip
)
1109 unsigned char request
[BUFFER_SIZE
];
1110 struct nlmsghdr
*hdr
;
1111 struct ifaddrmsg
*msg
;
1114 memset(&request
, 0, sizeof(request
));
1116 chunk
= ip
->get_address(ip
);
1118 hdr
= (struct nlmsghdr
*)request
;
1119 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1120 hdr
->nlmsg_type
= nlmsg_type
;
1121 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1123 msg
= (struct ifaddrmsg
*)NLMSG_DATA(hdr
);
1124 msg
->ifa_family
= ip
->get_family(ip
);
1126 msg
->ifa_prefixlen
= 8 * chunk
.len
;
1127 msg
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1128 msg
->ifa_index
= if_index
;
1130 add_attribute(hdr
, IFA_LOCAL
, chunk
, sizeof(request
));
1132 return netlink_send_ack(this->socket_rt
, hdr
);
1136 * Manages source routes in the routing table.
1137 * By setting the appropriate nlmsg_type, the route added or r.
1139 static status_t
manage_srcroute(private_kernel_interface_t
*this, int nlmsg_type
,
1140 int flags
, route_entry_t
*route
)
1142 unsigned char request
[BUFFER_SIZE
];
1143 struct nlmsghdr
*hdr
;
1147 /* if route is 0.0.0.0/0, we can't install it, as it would
1148 * overwrite the default route. Instead, we add two routes:
1149 * 0.0.0.0/1 and 128.0.0.0/1
1150 * TODO: use metrics instead */
1151 if (route
->prefixlen
== 0)
1156 half
.dst_net
= chunk_alloca(route
->dst_net
.len
);
1157 memset(half
.dst_net
.ptr
, 0, half
.dst_net
.len
);
1158 half
.src_ip
= route
->src_ip
;
1159 half
.gateway
= route
->gateway
;
1160 half
.if_index
= route
->if_index
;
1163 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1164 half
.dst_net
.ptr
[0] |= 0x80;
1165 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1169 memset(&request
, 0, sizeof(request
));
1171 hdr
= (struct nlmsghdr
*)request
;
1172 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1173 hdr
->nlmsg_type
= nlmsg_type
;
1174 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1176 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1177 msg
->rtm_family
= route
->src_ip
->get_family(route
->src_ip
);
1178 msg
->rtm_dst_len
= route
->prefixlen
;
1179 msg
->rtm_table
= RT_TABLE_MAIN
;
1180 msg
->rtm_protocol
= RTPROT_STATIC
;
1181 msg
->rtm_type
= RTN_UNICAST
;
1182 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1184 add_attribute(hdr
, RTA_DST
, route
->dst_net
, sizeof(request
));
1185 chunk
= route
->src_ip
->get_address(route
->src_ip
);
1186 add_attribute(hdr
, RTA_PREFSRC
, chunk
, sizeof(request
));
1187 chunk
= route
->gateway
->get_address(route
->gateway
);
1188 add_attribute(hdr
, RTA_GATEWAY
, chunk
, sizeof(request
));
1189 chunk
.ptr
= (char*)&route
->if_index
;
1190 chunk
.len
= sizeof(route
->if_index
);
1191 add_attribute(hdr
, RTA_OIF
, chunk
, sizeof(request
));
1193 return netlink_send_ack(this->socket_rt
, hdr
);
1198 * Implementation of kernel_interface_t.add_ip.
1200 static status_t
add_ip(private_kernel_interface_t
*this,
1201 host_t
*virtual_ip
, host_t
*iface_ip
)
1204 vip_entry_t
*listed
;
1205 iterator_t
*iterator
;
1207 DBG2(DBG_KNL
, "adding virtual IP %H", virtual_ip
);
1209 targetif
= get_interface_index(this, iface_ip
);
1212 DBG1(DBG_KNL
, "unable to add virtual IP %H, no iface found for %H",
1213 virtual_ip
, iface_ip
);
1217 /* beware of deadlocks (e.g. send/receive packets while holding the lock) */
1218 iterator
= this->vips
->create_iterator_locked(this->vips
, &(this->vips_mutex
));
1219 while (iterator
->iterate(iterator
, (void**)&listed
))
1221 if (listed
->if_index
== targetif
&&
1222 virtual_ip
->ip_equals(virtual_ip
, listed
->ip
))
1225 iterator
->destroy(iterator
);
1226 DBG2(DBG_KNL
, "virtual IP %H already added to iface %d reusing it",
1227 virtual_ip
, targetif
);
1231 iterator
->destroy(iterator
);
1233 if (manage_ipaddr(this, RTM_NEWADDR
, NLM_F_CREATE
| NLM_F_EXCL
,
1234 targetif
, virtual_ip
) == SUCCESS
)
1236 listed
= malloc_thing(vip_entry_t
);
1237 listed
->ip
= virtual_ip
->clone(virtual_ip
);
1238 listed
->if_index
= targetif
;
1239 listed
->refcount
= 1;
1240 this->vips
->insert_last(this->vips
, listed
);
1241 DBG2(DBG_KNL
, "virtual IP %H added to iface %d",
1242 virtual_ip
, targetif
);
1246 DBG2(DBG_KNL
, "unable to add virtual IP %H to iface %d",
1247 virtual_ip
, targetif
);
1252 * Implementation of kernel_interface_t.del_ip.
1254 static status_t
del_ip(private_kernel_interface_t
*this,
1255 host_t
*virtual_ip
, host_t
*iface_ip
)
1258 vip_entry_t
*listed
;
1259 iterator_t
*iterator
;
1261 DBG2(DBG_KNL
, "deleting virtual IP %H", virtual_ip
);
1263 targetif
= get_interface_index(this, iface_ip
);
1266 DBG1(DBG_KNL
, "unable to delete virtual IP %H, no iface found for %H",
1267 virtual_ip
, iface_ip
);
1271 /* beware of deadlocks (e.g. send/receive packets while holding the lock) */
1272 iterator
= this->vips
->create_iterator_locked(this->vips
, &(this->vips_mutex
));
1273 while (iterator
->iterate(iterator
, (void**)&listed
))
1275 if (listed
->if_index
== targetif
&&
1276 virtual_ip
->ip_equals(virtual_ip
, listed
->ip
))
1279 if (listed
->refcount
== 0)
1281 iterator
->remove(iterator
);
1282 vip_entry_destroy(listed
);
1283 iterator
->destroy(iterator
);
1284 return manage_ipaddr(this, RTM_DELADDR
, 0, targetif
, virtual_ip
);
1286 iterator
->destroy(iterator
);
1287 DBG2(DBG_KNL
, "virtual IP %H used by other SAs, not deleting",
1292 iterator
->destroy(iterator
);
1294 DBG2(DBG_KNL
, "virtual IP %H not cached, unable to delete", virtual_ip
);
1299 * Implementation of kernel_interface_t.get_spi.
1301 static status_t
get_spi(private_kernel_interface_t
*this,
1302 host_t
*src
, host_t
*dst
,
1303 protocol_id_t protocol
, u_int32_t reqid
,
1306 unsigned char request
[BUFFER_SIZE
];
1307 struct nlmsghdr
*hdr
, *out
;
1308 struct xfrm_userspi_info
*userspi
;
1309 u_int32_t received_spi
= 0;
1312 memset(&request
, 0, sizeof(request
));
1314 DBG2(DBG_KNL
, "getting SPI for reqid %d", reqid
);
1316 hdr
= (struct nlmsghdr
*)request
;
1317 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1318 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1319 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1321 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1322 host2xfrm(src
, &userspi
->info
.saddr
);
1323 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1324 userspi
->info
.id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1325 userspi
->info
.mode
= TRUE
; /* tunnel mode */
1326 userspi
->info
.reqid
= reqid
;
1327 userspi
->info
.family
= src
->get_family(src
);
1328 userspi
->min
= 0xc0000000;
1329 userspi
->max
= 0xcFFFFFFF;
1331 if (netlink_send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1334 while (NLMSG_OK(hdr
, len
))
1336 switch (hdr
->nlmsg_type
)
1338 case XFRM_MSG_NEWSA
:
1340 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1341 received_spi
= usersa
->id
.spi
;
1346 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1348 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1349 strerror(-err
->error
), -err
->error
);
1353 hdr
= NLMSG_NEXT(hdr
, len
);
1363 if (received_spi
== 0)
1365 DBG1(DBG_KNL
, "unable to get SPI for reqid %d", reqid
);
1369 DBG2(DBG_KNL
, "got SPI 0x%x for reqid %d", received_spi
, reqid
);
1371 *spi
= received_spi
;
1376 * Implementation of kernel_interface_t.add_sa.
1378 static status_t
add_sa(private_kernel_interface_t
*this,
1379 host_t
*src
, host_t
*dst
, u_int32_t spi
,
1380 protocol_id_t protocol
, u_int32_t reqid
,
1381 u_int64_t expire_soft
, u_int64_t expire_hard
,
1382 algorithm_t
*enc_alg
, algorithm_t
*int_alg
,
1383 prf_plus_t
*prf_plus
, natt_conf_t
*natt
, mode_t mode
,
1386 unsigned char request
[BUFFER_SIZE
];
1389 struct nlmsghdr
*hdr
;
1390 struct xfrm_usersa_info
*sa
;
1392 memset(&request
, 0, sizeof(request
));
1394 DBG2(DBG_KNL
, "adding SAD entry with SPI 0x%x", spi
);
1396 hdr
= (struct nlmsghdr
*)request
;
1397 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1398 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1399 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1401 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1402 host2xfrm(src
, &sa
->saddr
);
1403 host2xfrm(dst
, &sa
->id
.daddr
);
1405 sa
->id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1406 sa
->family
= src
->get_family(src
);
1408 sa
->replay_window
= 32;
1410 /* we currently do not expire SAs by volume/packet count */
1411 sa
->lft
.soft_byte_limit
= XFRM_INF
;
1412 sa
->lft
.hard_byte_limit
= XFRM_INF
;
1413 sa
->lft
.soft_packet_limit
= XFRM_INF
;
1414 sa
->lft
.hard_packet_limit
= XFRM_INF
;
1415 /* we use lifetimes since added, not since used */
1416 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
1417 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
1418 sa
->lft
.soft_use_expires_seconds
= 0;
1419 sa
->lft
.hard_use_expires_seconds
= 0;
1421 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1423 if (enc_alg
->algorithm
!= ENCR_UNDEFINED
)
1425 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
1426 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &key_size
);
1427 if (alg_name
== NULL
)
1429 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1430 encryption_algorithm_names
, enc_alg
->algorithm
);
1433 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1434 encryption_algorithm_names
, enc_alg
->algorithm
, key_size
);
1436 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
1437 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1438 if (hdr
->nlmsg_len
> sizeof(request
))
1443 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1444 algo
->alg_key_len
= key_size
;
1445 strcpy(algo
->alg_name
, alg_name
);
1446 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
1448 rthdr
= XFRM_RTA_NEXT(rthdr
);
1451 if (int_alg
->algorithm
!= AUTH_UNDEFINED
)
1453 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1454 alg_name
= lookup_algorithm(integrity_algs
, int_alg
, &key_size
);
1455 if (alg_name
== NULL
)
1457 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1458 integrity_algorithm_names
, int_alg
->algorithm
);
1461 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1462 integrity_algorithm_names
, int_alg
->algorithm
, key_size
);
1464 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
1465 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1466 if (hdr
->nlmsg_len
> sizeof(request
))
1471 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1472 algo
->alg_key_len
= key_size
;
1473 strcpy(algo
->alg_name
, alg_name
);
1474 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
1476 rthdr
= XFRM_RTA_NEXT(rthdr
);
1479 /* TODO: add IPComp here */
1483 rthdr
->rta_type
= XFRMA_ENCAP
;
1484 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1486 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1487 if (hdr
->nlmsg_len
> sizeof(request
))
1492 struct xfrm_encap_tmpl
* encap
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1493 encap
->encap_type
= UDP_ENCAP_ESPINUDP
;
1494 encap
->encap_sport
= htons(natt
->sport
);
1495 encap
->encap_dport
= htons(natt
->dport
);
1496 memset(&encap
->encap_oa
, 0, sizeof (xfrm_address_t
));
1497 /* encap_oa could probably be derived from the
1498 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1499 * pluto does the same as we do here but it uses encap_oa in the
1500 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1501 * the kernel ignores it anyway
1502 * -> does that mean that NAT-T encap doesn't work in transport mode?
1503 * No. The reason the kernel ignores NAT-OA is that it recomputes
1504 * (or, rather, just ignores) the checksum. If packets pass
1505 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1506 rthdr
= XFRM_RTA_NEXT(rthdr
);
1509 if (netlink_send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1511 DBG1(DBG_KNL
, "unalbe to add SAD entry with SPI 0x%x", spi
);
1518 * Implementation of kernel_interface_t.update_sa.
1520 static status_t
update_sa(private_kernel_interface_t
*this,
1521 host_t
*src
, host_t
*dst
,
1522 host_t
*new_src
, host_t
*new_dst
,
1523 host_diff_t src_changes
, host_diff_t dst_changes
,
1524 u_int32_t spi
, protocol_id_t protocol
)
1526 unsigned char request
[BUFFER_SIZE
];
1527 struct nlmsghdr
*hdr
, *out
= NULL
;
1528 struct xfrm_usersa_id
*sa_id
;
1529 struct xfrm_usersa_info
*sa
= NULL
;
1532 memset(&request
, 0, sizeof(request
));
1534 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x", spi
);
1536 hdr
= (struct nlmsghdr
*)request
;
1537 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1538 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1539 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1541 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1542 host2xfrm(dst
, &sa_id
->daddr
);
1544 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1545 sa_id
->family
= dst
->get_family(dst
);
1547 if (netlink_send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1550 while (NLMSG_OK(hdr
, len
))
1552 switch (hdr
->nlmsg_type
)
1554 case XFRM_MSG_NEWSA
:
1556 sa
= NLMSG_DATA(hdr
);
1561 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1562 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1563 strerror(-err
->error
), -err
->error
);
1567 hdr
= NLMSG_NEXT(hdr
, len
);
1577 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
1582 DBG2(DBG_KNL
, "updating SAD entry with SPI 0x%x", spi
);
1585 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1586 hdr
->nlmsg_type
= XFRM_MSG_UPDSA
;
1588 if (src_changes
& HOST_DIFF_ADDR
)
1590 host2xfrm(new_src
, &sa
->saddr
);
1593 if (dst_changes
& HOST_DIFF_ADDR
)
1595 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1596 host2xfrm(new_dst
, &sa
->id
.daddr
);
1599 if (src_changes
& HOST_DIFF_PORT
|| dst_changes
& HOST_DIFF_PORT
)
1601 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1602 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_usersa_info
);
1603 while (RTA_OK(rtattr
, rtsize
))
1605 if (rtattr
->rta_type
== XFRMA_ENCAP
)
1607 struct xfrm_encap_tmpl
* encap
;
1608 encap
= (struct xfrm_encap_tmpl
*)RTA_DATA(rtattr
);
1609 encap
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1610 encap
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1613 rtattr
= RTA_NEXT(rtattr
, rtsize
);
1616 if (netlink_send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1618 DBG1(DBG_KNL
, "unalbe to update SAD entry with SPI 0x%x", spi
);
1624 if (dst_changes
& HOST_DIFF_ADDR
)
1626 return this->public.del_sa(&this->public, dst
, spi
, protocol
);
1632 * Implementation of kernel_interface_t.query_sa.
1634 static status_t
query_sa(private_kernel_interface_t
*this, host_t
*dst
,
1635 u_int32_t spi
, protocol_id_t protocol
,
1636 u_int32_t
*use_time
)
1638 unsigned char request
[BUFFER_SIZE
];
1639 struct nlmsghdr
*out
= NULL
, *hdr
;
1640 struct xfrm_usersa_id
*sa_id
;
1641 struct xfrm_usersa_info
*sa
= NULL
;
1644 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x", spi
);
1645 memset(&request
, 0, sizeof(request
));
1647 hdr
= (struct nlmsghdr
*)request
;
1648 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1649 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1650 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1652 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1653 host2xfrm(dst
, &sa_id
->daddr
);
1655 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1656 sa_id
->family
= dst
->get_family(dst
);
1658 if (netlink_send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1661 while (NLMSG_OK(hdr
, len
))
1663 switch (hdr
->nlmsg_type
)
1665 case XFRM_MSG_NEWSA
:
1667 sa
= NLMSG_DATA(hdr
);
1672 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1673 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1674 strerror(-err
->error
), -err
->error
);
1678 hdr
= NLMSG_NEXT(hdr
, len
);
1689 DBG1(DBG_KNL
, "unable to query SAD entry with SPI 0x%x", spi
);
1694 *use_time
= sa
->curlft
.use_time
;
1700 * Implementation of kernel_interface_t.del_sa.
1702 static status_t
del_sa(private_kernel_interface_t
*this, host_t
*dst
,
1703 u_int32_t spi
, protocol_id_t protocol
)
1705 unsigned char request
[BUFFER_SIZE
];
1706 struct nlmsghdr
*hdr
;
1707 struct xfrm_usersa_id
*sa_id
;
1709 memset(&request
, 0, sizeof(request
));
1711 DBG2(DBG_KNL
, "deleting SAD entry with SPI 0x%x", spi
);
1713 hdr
= (struct nlmsghdr
*)request
;
1714 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1715 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1716 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1718 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1719 host2xfrm(dst
, &sa_id
->daddr
);
1721 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1722 sa_id
->family
= dst
->get_family(dst
);
1724 if (netlink_send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1726 DBG1(DBG_KNL
, "unalbe to delete SAD entry with SPI 0x%x", spi
);
1729 DBG2(DBG_KNL
, "deleted SAD entry with SPI 0x%x", spi
);
1734 * Implementation of kernel_interface_t.add_policy.
1736 static status_t
add_policy(private_kernel_interface_t
*this,
1737 host_t
*src
, host_t
*dst
,
1738 traffic_selector_t
*src_ts
,
1739 traffic_selector_t
*dst_ts
,
1740 policy_dir_t direction
, protocol_id_t protocol
,
1741 u_int32_t reqid
, bool high_prio
, mode_t mode
,
1744 iterator_t
*iterator
;
1745 policy_entry_t
*current
, *policy
;
1747 unsigned char request
[BUFFER_SIZE
];
1748 struct xfrm_userpolicy_info
*policy_info
;
1749 struct nlmsghdr
*hdr
;
1751 /* create a policy */
1752 policy
= malloc_thing(policy_entry_t
);
1753 memset(policy
, 0, sizeof(policy_entry_t
));
1754 policy
->sel
= ts2selector(src_ts
, dst_ts
);
1755 policy
->direction
= direction
;
1757 /* find the policy, which matches EXACTLY */
1758 pthread_mutex_lock(&this->policies_mutex
);
1759 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
1760 while (iterator
->iterate(iterator
, (void**)¤t
))
1762 if (memcmp(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) == 0 &&
1763 policy
->direction
== current
->direction
)
1765 /* use existing policy */
1768 current
->refcount
++;
1769 DBG2(DBG_KNL
, "policy %R===%R already exists, increasing ",
1770 "refcount", src_ts
, dst_ts
);
1778 iterator
->destroy(iterator
);
1780 { /* apply the new one, if we have no such policy */
1781 this->policies
->insert_last(this->policies
, policy
);
1782 policy
->refcount
= 1;
1785 DBG2(DBG_KNL
, "adding policy %R===%R", src_ts
, dst_ts
);
1787 memset(&request
, 0, sizeof(request
));
1788 hdr
= (struct nlmsghdr
*)request
;
1789 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1790 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
1791 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1793 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1794 policy_info
->sel
= policy
->sel
;
1795 policy_info
->dir
= policy
->direction
;
1796 /* calculate priority based on source selector size, small size = high prio */
1797 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
1798 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
1799 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
1800 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
1801 policy_info
->action
= XFRM_POLICY_ALLOW
;
1802 policy_info
->share
= XFRM_SHARE_ANY
;
1803 pthread_mutex_unlock(&this->policies_mutex
);
1805 /* policies don't expire */
1806 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
1807 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
1808 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
1809 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
1810 policy_info
->lft
.soft_add_expires_seconds
= 0;
1811 policy_info
->lft
.hard_add_expires_seconds
= 0;
1812 policy_info
->lft
.soft_use_expires_seconds
= 0;
1813 policy_info
->lft
.hard_use_expires_seconds
= 0;
1815 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
1816 rthdr
->rta_type
= XFRMA_TMPL
;
1818 rthdr
->rta_len
= sizeof(struct xfrm_user_tmpl
);
1819 rthdr
->rta_len
= RTA_LENGTH(rthdr
->rta_len
);
1821 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1822 if (hdr
->nlmsg_len
> sizeof(request
))
1827 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
1828 tmpl
->reqid
= reqid
;
1829 tmpl
->id
.proto
= (protocol
== PROTO_AH
) ? KERNEL_AH
: KERNEL_ESP
;
1830 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1832 tmpl
->family
= src
->get_family(src
);
1834 host2xfrm(src
, &tmpl
->saddr
);
1835 host2xfrm(dst
, &tmpl
->id
.daddr
);
1837 if (netlink_send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1839 DBG1(DBG_KNL
, "unable to add policy %R===%R", src_ts
, dst_ts
);
1843 /* install a route, if:
1844 * - we are NOT updating a policy
1845 * - this is a forward policy (to just get one for each child)
1846 * - we are in tunnel mode
1847 * - we are not using IPv6 (does not work correctly yet!)
1849 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1850 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
)
1852 policy
->route
= malloc_thing(route_entry_t
);
1853 if (get_address_by_ts(this, dst_ts
, &policy
->route
->src_ip
) == SUCCESS
)
1855 policy
->route
->gateway
= dst
->clone(dst
);
1856 policy
->route
->if_index
= get_interface_index(this, dst
);
1857 policy
->route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
1858 memcpy(policy
->route
->dst_net
.ptr
, &policy
->sel
.saddr
, policy
->route
->dst_net
.len
);
1859 policy
->route
->prefixlen
= policy
->sel
.prefixlen_s
;
1861 if (manage_srcroute(this, RTM_NEWROUTE
, NLM_F_CREATE
| NLM_F_EXCL
,
1862 policy
->route
) != SUCCESS
)
1864 DBG1(DBG_KNL
, "unable to install source route for %H",
1865 policy
->route
->src_ip
);
1866 route_entry_destroy(policy
->route
);
1867 policy
->route
= NULL
;
1872 free(policy
->route
);
1873 policy
->route
= NULL
;
1881 * Implementation of kernel_interface_t.query_policy.
1883 static status_t
query_policy(private_kernel_interface_t
*this,
1884 traffic_selector_t
*src_ts
,
1885 traffic_selector_t
*dst_ts
,
1886 policy_dir_t direction
, u_int32_t
*use_time
)
1888 unsigned char request
[BUFFER_SIZE
];
1889 struct nlmsghdr
*out
= NULL
, *hdr
;
1890 struct xfrm_userpolicy_id
*policy_id
;
1891 struct xfrm_userpolicy_info
*policy
= NULL
;
1894 memset(&request
, 0, sizeof(request
));
1896 DBG2(DBG_KNL
, "querying policy %R===%R", src_ts
, dst_ts
);
1898 hdr
= (struct nlmsghdr
*)request
;
1899 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1900 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
1901 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1903 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1904 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
1905 policy_id
->dir
= direction
;
1907 if (netlink_send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1910 while (NLMSG_OK(hdr
, len
))
1912 switch (hdr
->nlmsg_type
)
1914 case XFRM_MSG_NEWPOLICY
:
1916 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1921 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1922 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
1923 strerror(-err
->error
), -err
->error
);
1927 hdr
= NLMSG_NEXT(hdr
, len
);
1938 DBG2(DBG_KNL
, "unable to query policy %R===%R", src_ts
, dst_ts
);
1942 *use_time
= (time_t)policy
->curlft
.use_time
;
1949 * Implementation of kernel_interface_t.del_policy.
1951 static status_t
del_policy(private_kernel_interface_t
*this,
1952 traffic_selector_t
*src_ts
,
1953 traffic_selector_t
*dst_ts
,
1954 policy_dir_t direction
)
1956 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
1957 route_entry_t
*route
;
1958 unsigned char request
[BUFFER_SIZE
];
1959 struct nlmsghdr
*hdr
;
1960 struct xfrm_userpolicy_id
*policy_id
;
1961 iterator_t
*iterator
;
1963 DBG2(DBG_KNL
, "deleting policy %R===%R", src_ts
, dst_ts
);
1965 /* create a policy */
1966 memset(&policy
, 0, sizeof(policy_entry_t
));
1967 policy
.sel
= ts2selector(src_ts
, dst_ts
);
1968 policy
.direction
= direction
;
1970 /* find the policy */
1971 pthread_mutex_lock(&this->policies_mutex
);
1972 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
1973 while (iterator
->iterate(iterator
, (void**)¤t
))
1975 if (memcmp(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) == 0 &&
1976 policy
.direction
== current
->direction
)
1978 to_delete
= current
;
1979 if (--to_delete
->refcount
> 0)
1981 /* is used by more SAs, keep in kernel */
1982 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1983 iterator
->destroy(iterator
);
1984 pthread_mutex_unlock(&this->policies_mutex
);
1987 /* remove if last reference */
1988 iterator
->remove(iterator
);
1992 iterator
->destroy(iterator
);
1993 pthread_mutex_unlock(&this->policies_mutex
);
1996 DBG1(DBG_KNL
, "deleting policy %R===%R failed, not found", src_ts
, dst_ts
);
2000 memset(&request
, 0, sizeof(request
));
2002 hdr
= (struct nlmsghdr
*)request
;
2003 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2004 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2005 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2007 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2008 policy_id
->sel
= to_delete
->sel
;
2009 policy_id
->dir
= direction
;
2011 route
= to_delete
->route
;
2014 if (netlink_send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2016 DBG1(DBG_KNL
, "unable to delete policy %R===%R", src_ts
, dst_ts
);
2022 if (manage_srcroute(this, RTM_DELROUTE
, 0, route
) != SUCCESS
)
2024 DBG1(DBG_KNL
, "error uninstalling route installed with "
2025 "policy %R===%R", src_ts
, dst_ts
);
2027 route_entry_destroy(route
);
2033 * Implementation of kernel_interface_t.destroy.
2035 static void destroy(private_kernel_interface_t
*this)
2037 this->job
->cancel(this->job
);
2038 close(this->socket_xfrm_events
);
2039 close(this->socket_xfrm
);
2040 close(this->socket_rt_events
);
2041 close(this->socket_rt
);
2042 this->vips
->destroy(this->vips
);
2043 this->policies
->destroy(this->policies
);
2044 this->addrs
->destroy_function(this->addrs
, (void*)address_entry_destroy
);
2049 * Described in header.
2051 kernel_interface_t
*kernel_interface_create()
2053 private_kernel_interface_t
*this = malloc_thing(private_kernel_interface_t
);
2054 struct sockaddr_nl addr
;
2056 /* public functions */
2057 this->public.get_spi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
2058 this->public.add_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
,u_int64_t
,u_int64_t
,algorithm_t
*,algorithm_t
*,prf_plus_t
*,natt_conf_t
*,mode_t
,bool))add_sa
;
2059 this->public.update_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,host_t
*,host_t
*,host_diff_t
,host_diff_t
))update_sa
;
2060 this->public.query_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
*))query_sa
;
2061 this->public.del_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
2062 this->public.add_policy
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,protocol_id_t
,u_int32_t
,bool,mode_t
,bool))add_policy
;
2063 this->public.query_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
2064 this->public.del_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
2066 this->public.get_interface
= (char*(*)(kernel_interface_t
*,host_t
*))get_interface_name
;
2067 this->public.create_address_iterator
= (iterator_t
*(*)(kernel_interface_t
*))create_address_iterator
;
2068 this->public.add_ip
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*)) add_ip
;
2069 this->public.del_ip
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*)) del_ip
;
2070 this->public.destroy
= (void(*)(kernel_interface_t
*)) destroy
;
2072 /* private members */
2073 this->vips
= linked_list_create();
2074 this->policies
= linked_list_create();
2075 this->addrs
= linked_list_create();
2076 pthread_mutex_init(&this->policies_mutex
,NULL
);
2077 pthread_mutex_init(&this->vips_mutex
,NULL
);
2078 pthread_mutex_init(&this->addrs_mutex
,NULL
);
2080 memset(&addr
, 0, sizeof(addr
));
2081 addr
.nl_family
= AF_NETLINK
;
2083 /* create and bind RT socket */
2084 this->socket_rt
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2085 if (this->socket_rt
<= 0)
2087 charon
->kill(charon
, "unable to create RT netlink socket");
2090 if (bind(this->socket_rt
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2092 charon
->kill(charon
, "unable to bind RT netlink socket");
2095 /* create and bind RT socket for events (address changes) */
2096 this->socket_rt_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2097 if (this->socket_rt_events
<= 0)
2099 charon
->kill(charon
, "unable to create RT event socket");
2101 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR
;
2102 if (bind(this->socket_rt_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2104 charon
->kill(charon
, "unable to bind RT event socket");
2107 /* create and bind XFRM socket */
2108 this->socket_xfrm
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2109 if (this->socket_xfrm
<= 0)
2111 charon
->kill(charon
, "unable to create XFRM netlink socket");
2114 if (bind(this->socket_xfrm
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2116 charon
->kill(charon
, "unable to bind XFRM netlink socket");
2119 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
2120 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2121 if (this->socket_xfrm_events
<= 0)
2123 charon
->kill(charon
, "unable to create XFRM event socket");
2125 addr
.nl_groups
= XFRMGRP_ACQUIRE
| XFRMGRP_EXPIRE
;
2126 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2128 charon
->kill(charon
, "unable to bind XFRM event socket");
2131 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
2133 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
2135 init_address_list(this);
2137 return &this->public;