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>
52 #include <processing/jobs/roam_job.h>
54 /** kernel level protocol identifiers */
58 /** default priority of installed policies */
60 #define PRIO_HIGH 2000
62 #define BUFFER_SIZE 1024
65 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
66 * 'usual' netlink data x like 'struct xfrm_usersa_info'
68 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
70 * returns a pointer to the next rtattr following rta.
71 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
73 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
75 * returns the total size of attached rta data
76 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
78 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
80 typedef struct kernel_algorithm_t kernel_algorithm_t
;
83 * Mapping from the algorithms defined in IKEv2 to
84 * kernel level algorithm names and their key length
86 struct kernel_algorithm_t
{
88 * Identifier specified in IKEv2
93 * Name of the algorithm, as used as kernel identifier
98 * Key length in bits, if fixed size
102 #define END_OF_LIST -1
105 * Algorithms for encryption
107 kernel_algorithm_t encryption_algs
[] = {
108 /* {ENCR_DES_IV64, "***", 0}, */
109 {ENCR_DES
, "des", 64},
110 {ENCR_3DES
, "des3_ede", 192},
111 /* {ENCR_RC5, "***", 0}, */
112 /* {ENCR_IDEA, "***", 0}, */
113 {ENCR_CAST
, "cast128", 0},
114 {ENCR_BLOWFISH
, "blowfish", 0},
115 /* {ENCR_3IDEA, "***", 0}, */
116 /* {ENCR_DES_IV32, "***", 0}, */
117 {ENCR_NULL
, "cipher_null", 0},
118 {ENCR_AES_CBC
, "aes", 0},
119 /* {ENCR_AES_CTR, "***", 0}, */
120 {END_OF_LIST
, NULL
, 0},
124 * Algorithms for integrity protection
126 kernel_algorithm_t integrity_algs
[] = {
127 {AUTH_HMAC_MD5_96
, "md5", 128},
128 {AUTH_HMAC_SHA1_96
, "sha1", 160},
129 {AUTH_HMAC_SHA2_256_128
, "sha256", 256},
130 {AUTH_HMAC_SHA2_384_192
, "sha384", 384},
131 {AUTH_HMAC_SHA2_512_256
, "sha512", 512},
132 /* {AUTH_DES_MAC, "***", 0}, */
133 /* {AUTH_KPDK_MD5, "***", 0}, */
134 {AUTH_AES_XCBC_96
, "xcbc(aes)", 128},
135 {END_OF_LIST
, NULL
, 0},
139 * Look up a kernel algorithm name and its key size
141 char* lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
142 algorithm_t
*ikev2_algo
, u_int
*key_size
)
144 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
146 if (ikev2_algo
->algorithm
== kernel_algo
->ikev2_id
)
148 /* match, evaluate key length */
149 if (ikev2_algo
->key_size
)
150 { /* variable length */
151 *key_size
= ikev2_algo
->key_size
;
155 *key_size
= kernel_algo
->key_size
;
157 return kernel_algo
->name
;
164 typedef struct route_entry_t route_entry_t
;
167 * installed routing entry
169 struct route_entry_t
{
171 /** Index of the interface the route is bound to */
174 /** Source ip of the route */
177 /** gateway for this route */
180 /** Destination net */
183 /** Destination net prefixlen */
188 * destroy an route_entry_t object
190 static void route_entry_destroy(route_entry_t
*this)
192 this->src_ip
->destroy(this->src_ip
);
193 this->gateway
->destroy(this->gateway
);
194 chunk_free(&this->dst_net
);
198 typedef struct policy_entry_t policy_entry_t
;
201 * installed kernel policy.
203 struct policy_entry_t
{
205 /** direction of this policy: in, out, forward */
208 /** reqid of the policy */
211 /** parameters of installed policy */
212 struct xfrm_selector sel
;
214 /** associated route installed for this policy */
215 route_entry_t
*route
;
217 /** by how many CHILD_SA's this policy is used */
221 typedef struct addr_entry_t addr_entry_t
;
224 * IP address in an inface_entry_t
226 struct addr_entry_t
{
228 /** The ip address */
231 /** virtual IP managed by us */
234 /** scope of the address */
237 /** Number of times this IP is used, if virtual */
242 * destroy a addr_entry_t object
244 static void addr_entry_destroy(addr_entry_t
*this)
246 this->ip
->destroy(this->ip
);
250 typedef struct iface_entry_t iface_entry_t
;
253 * A network interface on this system, containing addr_entry_t's
255 struct iface_entry_t
{
257 /** interface index */
260 /** name of the interface */
261 char ifname
[IFNAMSIZ
];
263 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
266 /** list of addresses as host_t */
267 linked_list_t
*addrs
;
271 * destroy an interface entry
273 static void iface_entry_destroy(iface_entry_t
*this)
275 this->addrs
->destroy_function(this->addrs
, (void*)addr_entry_destroy
);
279 typedef struct private_kernel_interface_t private_kernel_interface_t
;
282 * Private variables and functions of kernel_interface class.
284 struct private_kernel_interface_t
{
286 * Public part of the kernel_interface_t object.
288 kernel_interface_t
public;
291 * mutex to lock access to the various lists
293 pthread_mutex_t mutex
;
296 * List of installed policies (policy_entry_t)
298 linked_list_t
*policies
;
301 * Cached list of interfaces and its adresses (iface_entry_t)
303 linked_list_t
*ifaces
;
306 * iterator used in hook()
311 * job receiving netlink events
316 * current sequence number for netlink request
321 * Netlink xfrm socket (IPsec)
326 * netlink xfrm socket to receive acquire and expire events
328 int socket_xfrm_events
;
331 * Netlink rt socket (routing)
336 * Netlink rt socket to receive address change events
338 int socket_rt_events
;
342 * convert a host_t to a struct xfrm_address
344 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
346 chunk_t chunk
= host
->get_address(host
);
347 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
351 * convert a traffic selector address range to subnet and its mask.
353 static void ts2subnet(traffic_selector_t
* ts
,
354 xfrm_address_t
*net
, u_int8_t
*mask
)
356 /* there is no way to do this cleanly, as the address range may
357 * be anything else but a subnet. We use from_addr as subnet
358 * and try to calculate a usable subnet mask.
363 size_t size
= (ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE
) ?
4 : 16;
365 from
= ts
->get_from_address(ts
);
366 to
= ts
->get_to_address(ts
);
369 /* go trough all bits of the addresses, beginning in the front.
370 * as long as they are equal, the subnet gets larger
372 for (byte
= 0; byte
< size
; byte
++)
374 for (bit
= 7; bit
>= 0; bit
--)
376 if ((1<<bit
& from
.ptr
[byte
]) != (1<<bit
& to
.ptr
[byte
]))
378 *mask
= ((7 - bit
) + (byte
* 8));
388 memcpy(net
, from
.ptr
, from
.len
);
394 * convert a traffic selector port range to port/portmask
396 static void ts2ports(traffic_selector_t
* ts
,
397 u_int16_t
*port
, u_int16_t
*mask
)
399 /* linux does not seem to accept complex portmasks. Only
400 * any or a specific port is allowed. We set to any, if we have
401 * a port range, or to a specific, if we have one port only.
405 from
= ts
->get_from_port(ts
);
406 to
= ts
->get_to_port(ts
);
421 * convert a pair of traffic_selectors to a xfrm_selector
423 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
424 traffic_selector_t
*dst
)
426 struct xfrm_selector sel
;
428 memset(&sel
, 0, sizeof(sel
));
429 sel
.family
= src
->get_type(src
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
430 /* src or dest proto may be "any" (0), use more restrictive one */
431 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
432 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
433 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
434 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
435 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
443 * Creates an rtattr and adds it to the netlink message
445 static void add_attribute(struct nlmsghdr
*hdr
, int rta_type
, chunk_t data
,
450 if (NLMSG_ALIGN(hdr
->nlmsg_len
) + RTA_ALIGN(data
.len
) > buflen
)
452 DBG1(DBG_KNL
, "unable to add attribute, buffer too small");
456 rta
= (struct rtattr
*)(((char*)hdr
) + NLMSG_ALIGN(hdr
->nlmsg_len
));
457 rta
->rta_type
= rta_type
;
458 rta
->rta_len
= RTA_LENGTH(data
.len
);
459 memcpy(RTA_DATA(rta
), data
.ptr
, data
.len
);
460 hdr
->nlmsg_len
= NLMSG_ALIGN(hdr
->nlmsg_len
) + rta
->rta_len
;
464 * process a XFRM_MSG_ACQUIRE from kernel
466 static void process_acquire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
470 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
471 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
473 if (RTA_OK(rtattr
, rtsize
))
475 if (rtattr
->rta_type
== XFRMA_TMPL
)
477 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rtattr
);
483 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
486 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
487 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid %d", reqid
);
488 job
= (job_t
*)acquire_job_create(reqid
);
489 charon
->processor
->queue_job(charon
->processor
, job
);
493 * process a XFRM_MSG_EXPIRE from kernel
495 static void process_expire(private_kernel_interface_t
*this, struct nlmsghdr
*hdr
)
498 protocol_id_t protocol
;
499 u_int32_t spi
, reqid
;
500 struct xfrm_user_expire
*expire
;
502 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
503 protocol
= expire
->state
.id
.proto
== KERNEL_ESP ? PROTO_ESP
: PROTO_AH
;
504 spi
= expire
->state
.id
.spi
;
505 reqid
= expire
->state
.reqid
;
507 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
508 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
509 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
510 protocol
, ntohl(spi
), reqid
);
513 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
517 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
519 charon
->processor
->queue_job(charon
->processor
, job
);
523 * process RTM_NEWLINK/RTM_DELLINK from kernel
525 static void process_link(private_kernel_interface_t
*this,
526 struct nlmsghdr
*hdr
, bool event
)
528 struct ifinfomsg
* msg
= (struct ifinfomsg
*)(NLMSG_DATA(hdr
));
529 struct rtattr
*rta
= IFLA_RTA(msg
);
530 size_t rtasize
= IFLA_PAYLOAD (hdr
);
531 iterator_t
*iterator
;
532 iface_entry_t
*current
, *entry
= NULL
;
536 while(RTA_OK(rta
, rtasize
))
538 switch (rta
->rta_type
)
541 name
= RTA_DATA(rta
);
544 rta
= RTA_NEXT(rta
, rtasize
);
551 switch (hdr
->nlmsg_type
)
555 if (msg
->ifi_flags
& IFF_LOOPBACK
)
556 { /* ignore loopback interfaces */
559 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
561 while (iterator
->iterate(iterator
, (void**)¤t
))
563 if (current
->ifindex
== msg
->ifi_index
)
571 entry
= malloc_thing(iface_entry_t
);
572 entry
->ifindex
= msg
->ifi_index
;
574 entry
->addrs
= linked_list_create();
575 this->ifaces
->insert_last(this->ifaces
, entry
);
577 memcpy(entry
->ifname
, name
, IFNAMSIZ
);
578 entry
->ifname
[IFNAMSIZ
-1] = '\0';
581 if (!(entry
->flags
& IFF_UP
) && (msg
->ifi_flags
& IFF_UP
))
584 DBG1(DBG_KNL
, "interface %s activated", name
);
586 if ((entry
->flags
& IFF_UP
) && !(msg
->ifi_flags
& IFF_UP
))
589 DBG1(DBG_KNL
, "interface %s deactivated", name
);
592 entry
->flags
= msg
->ifi_flags
;
593 iterator
->destroy(iterator
);
598 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
,
600 while (iterator
->iterate(iterator
, (void**)¤t
))
602 if (current
->ifindex
== msg
->ifi_index
)
604 /* we do not remove it, as an address may be added to a
605 * "down" interface and we wan't to know that. */
606 current
->flags
= msg
->ifi_flags
;
610 iterator
->destroy(iterator
);
615 /* send an update to all IKE_SAs */
618 charon
->processor
->queue_job(charon
->processor
, (job_t
*)roam_job_create());
623 * process RTM_NEWADDR/RTM_DELADDR from kernel
625 static void process_addr(private_kernel_interface_t
*this,
626 struct nlmsghdr
*hdr
, bool event
)
628 struct ifaddrmsg
* msg
= (struct ifaddrmsg
*)(NLMSG_DATA(hdr
));
629 struct rtattr
*rta
= IFA_RTA(msg
);
630 size_t rtasize
= IFA_PAYLOAD (hdr
);
632 iterator_t
*ifaces
, *addrs
;
633 iface_entry_t
*iface
;
635 chunk_t local
= chunk_empty
, address
= chunk_empty
;
636 bool update
= FALSE
, found
= FALSE
;
638 while(RTA_OK(rta
, rtasize
))
640 switch (rta
->rta_type
)
643 local
.ptr
= RTA_DATA(rta
);
644 local
.len
= RTA_PAYLOAD(rta
);
647 address
.ptr
= RTA_DATA(rta
);
648 address
.len
= RTA_PAYLOAD(rta
);
651 rta
= RTA_NEXT(rta
, rtasize
);
654 /* For PPP interfaces, we need the IFA_LOCAL address,
655 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
656 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
659 host
= host_create_from_chunk(msg
->ifa_family
, local
, 0);
661 else if (address
.ptr
)
663 host
= host_create_from_chunk(msg
->ifa_family
, address
, 0);
671 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
672 while (ifaces
->iterate(ifaces
, (void**)&iface
))
674 if (iface
->ifindex
== msg
->ifa_index
)
676 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
677 while (addrs
->iterate(addrs
, (void**)&addr
))
679 if (host
->ip_equals(host
, addr
->ip
))
682 if (hdr
->nlmsg_type
== RTM_DELADDR
)
684 addrs
->remove(addrs
);
685 addr_entry_destroy(addr
);
686 DBG1(DBG_KNL
, "%H disappeared from %s", host
, iface
->ifname
);
690 addrs
->destroy(addrs
);
692 if (hdr
->nlmsg_type
== RTM_NEWADDR
)
697 addr
= malloc_thing(addr_entry_t
);
698 addr
->ip
= host
->clone(host
);
699 addr
->virtual = FALSE
;
701 addr
->scope
= msg
->ifa_scope
;
703 iface
->addrs
->insert_last(iface
->addrs
, addr
);
706 DBG1(DBG_KNL
, "%H appeared on %s", host
, iface
->ifname
);
710 if (found
&& (iface
->flags
& IFF_UP
))
717 ifaces
->destroy(ifaces
);
720 /* send an update to all IKE_SAs */
723 charon
->processor
->queue_job(charon
->processor
, (job_t
*)roam_job_create());
728 * Receives events from kernel
730 static job_requeue_t
receive_events(private_kernel_interface_t
*this)
733 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
734 struct sockaddr_nl addr
;
735 socklen_t addr_len
= sizeof(addr
);
736 int len
, oldstate
, maxfd
, selected
;
740 FD_SET(this->socket_xfrm_events
, &rfds
);
741 FD_SET(this->socket_rt_events
, &rfds
);
742 maxfd
= max(this->socket_xfrm_events
, this->socket_rt_events
);
744 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
745 selected
= select(maxfd
+ 1, &rfds
, NULL
, NULL
, NULL
);
746 pthread_setcancelstate(oldstate
, NULL
);
749 DBG1(DBG_KNL
, "selecting on sockets failed: %s", strerror(errno
));
750 return JOB_REQUEUE_FAIR
;
752 if (FD_ISSET(this->socket_xfrm_events
, &rfds
))
754 selected
= this->socket_xfrm_events
;
756 else if (FD_ISSET(this->socket_rt_events
, &rfds
))
758 selected
= this->socket_rt_events
;
762 return JOB_REQUEUE_DIRECT
;
765 len
= recvfrom(selected
, response
, sizeof(response
), MSG_DONTWAIT
,
766 (struct sockaddr
*)&addr
, &addr_len
);
772 /* interrupted, try again */
773 return JOB_REQUEUE_DIRECT
;
775 /* no data ready, select again */
776 return JOB_REQUEUE_DIRECT
;
778 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
780 return JOB_REQUEUE_FAIR
;
783 if (addr
.nl_pid
!= 0)
784 { /* not from kernel. not interested, try another one */
785 return JOB_REQUEUE_DIRECT
;
788 while (NLMSG_OK(hdr
, len
))
790 /* looks good so far, dispatch netlink message */
791 if (selected
== this->socket_xfrm_events
)
793 switch (hdr
->nlmsg_type
)
795 case XFRM_MSG_ACQUIRE
:
796 process_acquire(this, hdr
);
798 case XFRM_MSG_EXPIRE
:
799 process_expire(this, hdr
);
805 else if (selected
== this->socket_rt_events
)
807 switch (hdr
->nlmsg_type
)
811 process_addr(this, hdr
, TRUE
);
815 process_link(this, hdr
, TRUE
);
819 charon
->processor
->queue_job(charon
->processor
,
820 (job_t
*)roam_job_create());
826 hdr
= NLMSG_NEXT(hdr
, len
);
828 return JOB_REQUEUE_DIRECT
;
832 * send a netlink message and wait for a reply
834 static status_t
netlink_send(private_kernel_interface_t
*this,
835 int socket
, struct nlmsghdr
*in
,
836 struct nlmsghdr
**out
, size_t *out_len
)
839 struct sockaddr_nl addr
;
840 chunk_t result
= chunk_empty
, tmp
;
841 struct nlmsghdr
*msg
, peek
;
843 pthread_mutex_lock(&this->mutex
);
845 in
->nlmsg_seq
= ++this->seq
;
846 in
->nlmsg_pid
= getpid();
848 memset(&addr
, 0, sizeof(addr
));
849 addr
.nl_family
= AF_NETLINK
;
855 len
= sendto(socket
, in
, in
->nlmsg_len
, 0,
856 (struct sockaddr
*)&addr
, sizeof(addr
));
858 if (len
!= in
->nlmsg_len
)
862 /* interrupted, try again */
865 pthread_mutex_unlock(&this->mutex
);
866 DBG1(DBG_KNL
, "error sending to netlink socket: %s", strerror(errno
));
875 tmp
.len
= sizeof(buf
);
877 msg
= (struct nlmsghdr
*)tmp
.ptr
;
879 memset(&addr
, 0, sizeof(addr
));
880 addr
.nl_family
= AF_NETLINK
;
881 addr
.nl_pid
= getpid();
883 addr_len
= sizeof(addr
);
885 len
= recvfrom(socket
, tmp
.ptr
, tmp
.len
, 0,
886 (struct sockaddr
*)&addr
, &addr_len
);
892 DBG1(DBG_KNL
, "got interrupted");
893 /* interrupted, try again */
896 DBG1(DBG_KNL
, "error reading from netlink socket: %s", strerror(errno
));
897 pthread_mutex_unlock(&this->mutex
);
900 if (!NLMSG_OK(msg
, len
))
902 DBG1(DBG_KNL
, "received corrupted netlink message");
903 pthread_mutex_unlock(&this->mutex
);
906 if (msg
->nlmsg_seq
!= this->seq
)
908 DBG1(DBG_KNL
, "received invalid netlink sequence number");
909 if (msg
->nlmsg_seq
< this->seq
)
913 pthread_mutex_unlock(&this->mutex
);
918 result
= chunk_cata("cc", result
, tmp
);
920 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
921 * numbers to detect multi header messages */
922 len
= recvfrom(socket
, &peek
, sizeof(peek
), MSG_PEEK
| MSG_DONTWAIT
,
923 (struct sockaddr
*)&addr
, &addr_len
);
925 if (len
== sizeof(peek
) && peek
.nlmsg_seq
== this->seq
)
927 /* seems to be multipart */
933 *out_len
= result
.len
;
934 *out
= (struct nlmsghdr
*)clalloc(result
.ptr
, result
.len
);
936 pthread_mutex_unlock(&this->mutex
);
942 * send a netlink message and wait for its acknowlegde
944 static status_t
netlink_send_ack(private_kernel_interface_t
*this,
945 int socket
, struct nlmsghdr
*in
)
947 struct nlmsghdr
*out
, *hdr
;
950 if (netlink_send(this, socket
, in
, &out
, &len
) != SUCCESS
)
955 while (NLMSG_OK(hdr
, len
))
957 switch (hdr
->nlmsg_type
)
961 struct nlmsgerr
* err
= (struct nlmsgerr
*)NLMSG_DATA(hdr
);
965 DBG1(DBG_KNL
, "received netlink error: %s (%d)",
966 strerror(-err
->error
), -err
->error
);
974 hdr
= NLMSG_NEXT(hdr
, len
);
981 DBG1(DBG_KNL
, "netlink request not acknowlegded");
987 * Initialize a list of local addresses.
989 static status_t
init_address_list(private_kernel_interface_t
*this)
991 char request
[BUFFER_SIZE
];
992 struct nlmsghdr
*out
, *current
, *in
;
993 struct rtgenmsg
*msg
;
995 iterator_t
*ifaces
, *addrs
;
996 iface_entry_t
*iface
;
999 DBG1(DBG_KNL
, "listening on interfaces:");
1001 memset(&request
, 0, sizeof(request
));
1003 in
= (struct nlmsghdr
*)&request
;
1004 in
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtgenmsg
));
1005 in
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_MATCH
| NLM_F_ROOT
;
1006 msg
= (struct rtgenmsg
*)NLMSG_DATA(in
);
1007 msg
->rtgen_family
= AF_UNSPEC
;
1010 in
->nlmsg_type
= RTM_GETLINK
;
1011 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1016 while (NLMSG_OK(current
, len
))
1018 switch (current
->nlmsg_type
)
1023 process_link(this, current
, FALSE
);
1026 current
= NLMSG_NEXT(current
, len
);
1033 /* get all interface addresses */
1034 in
->nlmsg_type
= RTM_GETADDR
;
1035 if (netlink_send(this, this->socket_rt
, in
, &out
, &len
) != SUCCESS
)
1040 while (NLMSG_OK(current
, len
))
1042 switch (current
->nlmsg_type
)
1047 process_addr(this, current
, FALSE
);
1050 current
= NLMSG_NEXT(current
, len
);
1057 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1058 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1060 if (iface
->flags
& IFF_UP
)
1062 DBG1(DBG_KNL
, " %s", iface
->ifname
);
1063 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1064 while (addrs
->iterate(addrs
, (void**)&addr
))
1066 DBG1(DBG_KNL
, " %H", addr
->ip
);
1068 addrs
->destroy(addrs
);
1071 ifaces
->destroy(ifaces
);
1076 * iterator hook to iterate over addrs
1078 static hook_result_t
addr_hook(private_kernel_interface_t
*this,
1079 addr_entry_t
*in
, host_t
**out
)
1082 { /* skip virtual interfaces added by us */
1085 if (in
->scope
>= RT_SCOPE_LINK
)
1086 { /* skip addresses with a unusable scope */
1094 * iterator hook to iterate over ifaces
1096 static hook_result_t
iface_hook(private_kernel_interface_t
*this,
1097 iface_entry_t
*in
, host_t
**out
)
1099 if (!(in
->flags
& IFF_UP
))
1100 { /* skip interfaces not up */
1104 if (this->hiter
== NULL
)
1106 this->hiter
= in
->addrs
->create_iterator(in
->addrs
, TRUE
);
1107 this->hiter
->set_iterator_hook(this->hiter
,
1108 (iterator_hook_t
*)addr_hook
, this);
1110 while (this->hiter
->iterate(this->hiter
, (void**)out
))
1114 this->hiter
->destroy(this->hiter
);
1120 * Implements kernel_interface_t.create_address_iterator.
1122 static iterator_t
*create_address_iterator(private_kernel_interface_t
*this)
1124 iterator_t
*iterator
;
1126 /* This iterator is not only hooked, is is double-hooked. As we have stored
1127 * our addresses in iface_entry->addr_entry->ip, we need to iterate the
1128 * entries in each interface we iterate. This does the iface_hook. The
1129 * addr_hook returns the ip instead of the addr_entry. */
1131 iterator
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1132 iterator
->set_iterator_hook(iterator
, (iterator_hook_t
*)iface_hook
, this);
1137 * implementation of kernel_interface_t.get_interface_name
1139 static char *get_interface_name(private_kernel_interface_t
*this, host_t
* ip
)
1141 iterator_t
*ifaces
, *addrs
;
1142 iface_entry_t
*iface
;
1146 DBG2(DBG_KNL
, "getting interface name for %H", ip
);
1148 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1149 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1151 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1152 while (addrs
->iterate(addrs
, (void**)&addr
))
1154 if (ip
->ip_equals(ip
, addr
->ip
))
1156 name
= strdup(iface
->ifname
);
1160 addrs
->destroy(addrs
);
1166 ifaces
->destroy(ifaces
);
1170 DBG2(DBG_KNL
, "%H is on interface %s", ip
, name
);
1174 DBG2(DBG_KNL
, "%H is not a local address", ip
);
1180 * Tries to find an ip address of a local interface that is included in the
1181 * supplied traffic selector.
1183 static status_t
get_address_by_ts(private_kernel_interface_t
*this,
1184 traffic_selector_t
*ts
, host_t
**ip
)
1186 iterator_t
*ifaces
, *addrs
;
1187 iface_entry_t
*iface
;
1193 DBG2(DBG_KNL
, "getting a local address in traffic selector %R", ts
);
1195 /* if we have a family which includes localhost, we do not
1196 * search for an IP, we use the default */
1197 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
1199 if (family
== AF_INET
)
1201 host
= host_create_from_string("127.0.0.1", 0);
1205 host
= host_create_from_string("::1", 0);
1208 if (ts
->includes(ts
, host
))
1210 *ip
= host_create_any(family
);
1211 host
->destroy(host
);
1212 DBG2(DBG_KNL
, "using host %H", *ip
);
1215 host
->destroy(host
);
1217 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1218 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1220 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1221 while (addrs
->iterate(addrs
, (void**)&addr
))
1223 if (ts
->includes(ts
, addr
->ip
))
1226 *ip
= addr
->ip
->clone(addr
->ip
);
1230 addrs
->destroy(addrs
);
1236 ifaces
->destroy(ifaces
);
1240 DBG1(DBG_KNL
, "no local address found in traffic selector %R", ts
);
1243 DBG2(DBG_KNL
, "using host %H", *ip
);
1248 * get the interface of a local address
1250 static int get_interface_index(private_kernel_interface_t
*this, host_t
* ip
)
1252 iterator_t
*ifaces
, *addrs
;
1253 iface_entry_t
*iface
;
1257 DBG2(DBG_KNL
, "getting iface for %H", ip
);
1259 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1260 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1262 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1263 while (addrs
->iterate(addrs
, (void**)&addr
))
1265 if (ip
->ip_equals(ip
, addr
->ip
))
1267 ifindex
= iface
->ifindex
;
1271 addrs
->destroy(addrs
);
1277 ifaces
->destroy(ifaces
);
1281 DBG1(DBG_KNL
, "unable to get interface for %H", ip
);
1287 * Manages the creation and deletion of ip addresses on an interface.
1288 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1290 static status_t
manage_ipaddr(private_kernel_interface_t
*this, int nlmsg_type
,
1291 int flags
, int if_index
, host_t
*ip
)
1293 unsigned char request
[BUFFER_SIZE
];
1294 struct nlmsghdr
*hdr
;
1295 struct ifaddrmsg
*msg
;
1298 memset(&request
, 0, sizeof(request
));
1300 chunk
= ip
->get_address(ip
);
1302 hdr
= (struct nlmsghdr
*)request
;
1303 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1304 hdr
->nlmsg_type
= nlmsg_type
;
1305 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1307 msg
= (struct ifaddrmsg
*)NLMSG_DATA(hdr
);
1308 msg
->ifa_family
= ip
->get_family(ip
);
1310 msg
->ifa_prefixlen
= 8 * chunk
.len
;
1311 msg
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1312 msg
->ifa_index
= if_index
;
1314 add_attribute(hdr
, IFA_LOCAL
, chunk
, sizeof(request
));
1316 return netlink_send_ack(this, this->socket_rt
, hdr
);
1320 * Manages source routes in the routing table.
1321 * By setting the appropriate nlmsg_type, the route added or r.
1323 static status_t
manage_srcroute(private_kernel_interface_t
*this, int nlmsg_type
,
1324 int flags
, route_entry_t
*route
)
1326 unsigned char request
[BUFFER_SIZE
];
1327 struct nlmsghdr
*hdr
;
1331 /* if route is 0.0.0.0/0, we can't install it, as it would
1332 * overwrite the default route. Instead, we add two routes:
1333 * 0.0.0.0/1 and 128.0.0.0/1
1334 * TODO: use metrics instead */
1335 if (route
->prefixlen
== 0)
1340 half
.dst_net
= chunk_alloca(route
->dst_net
.len
);
1341 memset(half
.dst_net
.ptr
, 0, half
.dst_net
.len
);
1342 half
.src_ip
= route
->src_ip
;
1343 half
.gateway
= route
->gateway
;
1344 half
.if_index
= route
->if_index
;
1347 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1348 half
.dst_net
.ptr
[0] |= 0x80;
1349 status
= manage_srcroute(this, nlmsg_type
, flags
, &half
);
1353 memset(&request
, 0, sizeof(request
));
1355 hdr
= (struct nlmsghdr
*)request
;
1356 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
| flags
;
1357 hdr
->nlmsg_type
= nlmsg_type
;
1358 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1360 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1361 msg
->rtm_family
= route
->src_ip
->get_family(route
->src_ip
);
1362 msg
->rtm_dst_len
= route
->prefixlen
;
1363 msg
->rtm_table
= RT_TABLE_MAIN
;
1364 msg
->rtm_protocol
= RTPROT_STATIC
;
1365 msg
->rtm_type
= RTN_UNICAST
;
1366 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1368 add_attribute(hdr
, RTA_DST
, route
->dst_net
, sizeof(request
));
1369 chunk
= route
->src_ip
->get_address(route
->src_ip
);
1370 add_attribute(hdr
, RTA_PREFSRC
, chunk
, sizeof(request
));
1371 chunk
= route
->gateway
->get_address(route
->gateway
);
1372 add_attribute(hdr
, RTA_GATEWAY
, chunk
, sizeof(request
));
1373 chunk
.ptr
= (char*)&route
->if_index
;
1374 chunk
.len
= sizeof(route
->if_index
);
1375 add_attribute(hdr
, RTA_OIF
, chunk
, sizeof(request
));
1377 return netlink_send_ack(this, this->socket_rt
, hdr
);
1381 * Implementation of kernel_interface_t.get_source_addr.
1383 static host_t
* get_source_addr(private_kernel_interface_t
*this, host_t
*dest
)
1385 unsigned char request
[BUFFER_SIZE
];
1386 struct nlmsghdr
*hdr
, *out
, *current
;
1390 host_t
*source
= NULL
;
1392 DBG2(DBG_KNL
, "getting source address to reach %H", dest
);
1394 memset(&request
, 0, sizeof(request
));
1396 hdr
= (struct nlmsghdr
*)request
;
1397 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1398 hdr
->nlmsg_type
= RTM_GETROUTE
;
1399 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
1401 msg
= (struct rtmsg
*)NLMSG_DATA(hdr
);
1402 msg
->rtm_family
= dest
->get_family(dest
);
1403 msg
->rtm_dst_len
= msg
->rtm_family
== AF_INET ?
32 : 128;
1404 msg
->rtm_table
= RT_TABLE_MAIN
;
1405 msg
->rtm_protocol
= RTPROT_STATIC
;
1406 msg
->rtm_type
= RTN_UNICAST
;
1407 msg
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1409 chunk
= dest
->get_address(dest
);
1410 add_attribute(hdr
, RTA_DST
, chunk
, sizeof(request
));
1412 if (netlink_send(this, this->socket_rt
, hdr
, &out
, &len
) != SUCCESS
)
1414 DBG1(DBG_KNL
, "getting source address to %H failed", dest
);
1418 while (NLMSG_OK(current
, len
))
1420 switch (current
->nlmsg_type
)
1429 msg
= (struct rtmsg
*)(NLMSG_DATA(current
));
1431 rtasize
= RTM_PAYLOAD(current
);
1432 while(RTA_OK(rta
, rtasize
))
1434 switch (rta
->rta_type
)
1437 chunk
.ptr
= RTA_DATA(rta
);
1438 chunk
.len
= RTA_PAYLOAD(rta
);
1439 source
= host_create_from_chunk(msg
->rtm_family
,
1443 rta
= RTA_NEXT(rta
, rtasize
);
1448 current
= NLMSG_NEXT(current
, len
);
1456 DBG2(DBG_KNL
, "no route found to %H", dest
);
1462 * Implementation of kernel_interface_t.add_ip.
1464 static status_t
add_ip(private_kernel_interface_t
*this,
1465 host_t
*virtual_ip
, host_t
*iface_ip
)
1467 iface_entry_t
*iface
;
1469 iterator_t
*addrs
, *ifaces
;
1471 DBG2(DBG_KNL
, "adding virtual IP %H", virtual_ip
);
1473 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1474 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1476 bool iface_found
= FALSE
;
1478 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1479 while (addrs
->iterate(addrs
, (void**)&addr
))
1481 if (iface_ip
->ip_equals(iface_ip
, addr
->ip
))
1485 else if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1488 DBG2(DBG_KNL
, "virtual IP %H already installed on %s",
1489 virtual_ip
, iface
->ifname
);
1490 addrs
->destroy(addrs
);
1491 ifaces
->destroy(ifaces
);
1495 addrs
->destroy(addrs
);
1499 int ifindex
= iface
->ifindex
;
1500 ifaces
->destroy(ifaces
);
1501 if (manage_ipaddr(this, RTM_NEWADDR
, NLM_F_CREATE
| NLM_F_EXCL
,
1502 ifindex
, virtual_ip
) == SUCCESS
)
1504 addr
= malloc_thing(addr_entry_t
);
1505 addr
->ip
= virtual_ip
->clone(virtual_ip
);
1507 addr
->virtual = TRUE
;
1508 addr
->scope
= RT_SCOPE_UNIVERSE
;
1509 pthread_mutex_lock(&this->mutex
);
1510 iface
->addrs
->insert_last(iface
->addrs
, addr
);
1511 pthread_mutex_unlock(&this->mutex
);
1514 DBG2(DBG_KNL
, "adding virtual IP %H failed", virtual_ip
);
1520 ifaces
->destroy(ifaces
);
1522 DBG2(DBG_KNL
, "interface address %H not found, unable to install"
1523 "virtual IP %H", iface_ip
, virtual_ip
);
1528 * Implementation of kernel_interface_t.del_ip.
1530 static status_t
del_ip(private_kernel_interface_t
*this, host_t
*virtual_ip
)
1532 iface_entry_t
*iface
;
1534 iterator_t
*addrs
, *ifaces
;
1536 DBG2(DBG_KNL
, "deleting virtual IP %H", virtual_ip
);
1538 ifaces
= this->ifaces
->create_iterator_locked(this->ifaces
, &this->mutex
);
1539 while (ifaces
->iterate(ifaces
, (void**)&iface
))
1541 addrs
= iface
->addrs
->create_iterator(iface
->addrs
, TRUE
);
1542 while (addrs
->iterate(addrs
, (void**)&addr
))
1544 if (virtual_ip
->ip_equals(virtual_ip
, addr
->ip
))
1546 int ifindex
= iface
->ifindex
;
1548 if (addr
->refcount
== 0)
1550 addrs
->remove(addrs
);
1551 addrs
->destroy(addrs
);
1552 ifaces
->destroy(ifaces
);
1553 addr_entry_destroy(addr
);
1554 return manage_ipaddr(this, RTM_DELADDR
, 0,
1555 ifindex
, virtual_ip
);
1557 DBG2(DBG_KNL
, "virtual IP %H used by other SAs, not deleting",
1559 addrs
->destroy(addrs
);
1560 ifaces
->destroy(ifaces
);
1564 addrs
->destroy(addrs
);
1566 ifaces
->destroy(ifaces
);
1568 DBG2(DBG_KNL
, "virtual IP %H not cached, unable to delete", virtual_ip
);
1573 * Implementation of kernel_interface_t.get_spi.
1575 static status_t
get_spi(private_kernel_interface_t
*this,
1576 host_t
*src
, host_t
*dst
,
1577 protocol_id_t protocol
, u_int32_t reqid
,
1580 unsigned char request
[BUFFER_SIZE
];
1581 struct nlmsghdr
*hdr
, *out
;
1582 struct xfrm_userspi_info
*userspi
;
1583 u_int32_t received_spi
= 0;
1586 memset(&request
, 0, sizeof(request
));
1588 DBG2(DBG_KNL
, "getting SPI for reqid %d", reqid
);
1590 hdr
= (struct nlmsghdr
*)request
;
1591 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1592 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1593 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1595 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1596 host2xfrm(src
, &userspi
->info
.saddr
);
1597 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1598 userspi
->info
.id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1599 userspi
->info
.mode
= TRUE
; /* tunnel mode */
1600 userspi
->info
.reqid
= reqid
;
1601 userspi
->info
.family
= src
->get_family(src
);
1602 userspi
->min
= 0xc0000000;
1603 userspi
->max
= 0xcFFFFFFF;
1605 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1608 while (NLMSG_OK(hdr
, len
))
1610 switch (hdr
->nlmsg_type
)
1612 case XFRM_MSG_NEWSA
:
1614 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1615 received_spi
= usersa
->id
.spi
;
1620 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1622 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1623 strerror(-err
->error
), -err
->error
);
1627 hdr
= NLMSG_NEXT(hdr
, len
);
1637 if (received_spi
== 0)
1639 DBG1(DBG_KNL
, "unable to get SPI for reqid %d", reqid
);
1643 DBG2(DBG_KNL
, "got SPI 0x%x for reqid %d", received_spi
, reqid
);
1645 *spi
= received_spi
;
1650 * Implementation of kernel_interface_t.add_sa.
1652 static status_t
add_sa(private_kernel_interface_t
*this,
1653 host_t
*src
, host_t
*dst
, u_int32_t spi
,
1654 protocol_id_t protocol
, u_int32_t reqid
,
1655 u_int64_t expire_soft
, u_int64_t expire_hard
,
1656 algorithm_t
*enc_alg
, algorithm_t
*int_alg
,
1657 prf_plus_t
*prf_plus
, mode_t mode
, bool encap
,
1660 unsigned char request
[BUFFER_SIZE
];
1663 struct nlmsghdr
*hdr
;
1664 struct xfrm_usersa_info
*sa
;
1666 memset(&request
, 0, sizeof(request
));
1668 DBG2(DBG_KNL
, "adding SAD entry with SPI 0x%x", spi
);
1670 hdr
= (struct nlmsghdr
*)request
;
1671 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1672 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1673 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1675 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1676 host2xfrm(src
, &sa
->saddr
);
1677 host2xfrm(dst
, &sa
->id
.daddr
);
1679 sa
->id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1680 sa
->family
= src
->get_family(src
);
1682 sa
->replay_window
= 32;
1684 /* we currently do not expire SAs by volume/packet count */
1685 sa
->lft
.soft_byte_limit
= XFRM_INF
;
1686 sa
->lft
.hard_byte_limit
= XFRM_INF
;
1687 sa
->lft
.soft_packet_limit
= XFRM_INF
;
1688 sa
->lft
.hard_packet_limit
= XFRM_INF
;
1689 /* we use lifetimes since added, not since used */
1690 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
1691 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
1692 sa
->lft
.soft_use_expires_seconds
= 0;
1693 sa
->lft
.hard_use_expires_seconds
= 0;
1695 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1697 if (enc_alg
->algorithm
!= ENCR_UNDEFINED
)
1699 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
1700 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &key_size
);
1701 if (alg_name
== NULL
)
1703 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1704 encryption_algorithm_names
, enc_alg
->algorithm
);
1707 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1708 encryption_algorithm_names
, enc_alg
->algorithm
, key_size
);
1710 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
1711 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1712 if (hdr
->nlmsg_len
> sizeof(request
))
1717 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1718 algo
->alg_key_len
= key_size
;
1719 strcpy(algo
->alg_name
, alg_name
);
1720 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
1722 rthdr
= XFRM_RTA_NEXT(rthdr
);
1725 if (int_alg
->algorithm
!= AUTH_UNDEFINED
)
1727 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1728 alg_name
= lookup_algorithm(integrity_algs
, int_alg
, &key_size
);
1729 if (alg_name
== NULL
)
1731 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1732 integrity_algorithm_names
, int_alg
->algorithm
);
1735 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1736 integrity_algorithm_names
, int_alg
->algorithm
, key_size
);
1738 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
1739 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1740 if (hdr
->nlmsg_len
> sizeof(request
))
1745 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1746 algo
->alg_key_len
= key_size
;
1747 strcpy(algo
->alg_name
, alg_name
);
1748 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
1750 rthdr
= XFRM_RTA_NEXT(rthdr
);
1753 /* TODO: add IPComp here */
1757 rthdr
->rta_type
= XFRMA_ENCAP
;
1758 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1760 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1761 if (hdr
->nlmsg_len
> sizeof(request
))
1766 struct xfrm_encap_tmpl
* encap
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1767 encap
->encap_type
= UDP_ENCAP_ESPINUDP
;
1768 encap
->encap_sport
= htons(src
->get_port(src
));
1769 encap
->encap_dport
= htons(dst
->get_port(dst
));
1770 memset(&encap
->encap_oa
, 0, sizeof (xfrm_address_t
));
1771 /* encap_oa could probably be derived from the
1772 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1773 * pluto does the same as we do here but it uses encap_oa in the
1774 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1775 * the kernel ignores it anyway
1776 * -> does that mean that NAT-T encap doesn't work in transport mode?
1777 * No. The reason the kernel ignores NAT-OA is that it recomputes
1778 * (or, rather, just ignores) the checksum. If packets pass
1779 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1780 rthdr
= XFRM_RTA_NEXT(rthdr
);
1783 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
1785 DBG1(DBG_KNL
, "unalbe to add SAD entry with SPI 0x%x", spi
);
1792 * Implementation of kernel_interface_t.update_sa.
1794 static status_t
update_sa(private_kernel_interface_t
*this,
1795 u_int32_t spi
, protocol_id_t protocol
,
1796 host_t
*src
, host_t
*dst
,
1797 host_t
*new_src
, host_t
*new_dst
)
1799 unsigned char request
[BUFFER_SIZE
];
1800 struct nlmsghdr
*hdr
, *out
= NULL
;
1801 struct xfrm_usersa_id
*sa_id
;
1802 struct xfrm_usersa_info
*sa
= NULL
;
1805 memset(&request
, 0, sizeof(request
));
1807 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x for update", spi
);
1809 /* query the exisiting SA first */
1810 hdr
= (struct nlmsghdr
*)request
;
1811 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1812 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1813 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1815 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1816 host2xfrm(dst
, &sa_id
->daddr
);
1818 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1819 sa_id
->family
= dst
->get_family(dst
);
1821 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1824 while (NLMSG_OK(hdr
, len
))
1826 switch (hdr
->nlmsg_type
)
1828 case XFRM_MSG_NEWSA
:
1830 sa
= NLMSG_DATA(hdr
);
1835 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1836 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1837 strerror(-err
->error
), -err
->error
);
1841 hdr
= NLMSG_NEXT(hdr
, len
);
1850 this->public.del_sa(&this->public, dst
, spi
, protocol
) != SUCCESS
)
1852 DBG1(DBG_KNL
, "unable to update SAD entry with SPI 0x%x", spi
);
1857 DBG2(DBG_KNL
, "updating SAD entry with SPI 0x%x from %#H..%#H to %#H..%#H",
1858 spi
, src
, dst
, new_src
, new_dst
);
1860 /* update the values in the queried SA */
1862 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1863 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1865 if (!src
->ip_equals(src
, new_src
))
1867 host2xfrm(new_src
, &sa
->saddr
);
1869 if (!dst
->ip_equals(dst
, new_dst
))
1871 host2xfrm(new_dst
, &sa
->id
.daddr
);
1874 if (src
->get_port(src
) != new_src
->get_port(new_src
) ||
1875 dst
->get_port(dst
) != new_dst
->get_port(new_dst
))
1877 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1878 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_usersa_info
);
1879 while (RTA_OK(rtattr
, rtsize
))
1881 if (rtattr
->rta_type
== XFRMA_ENCAP
)
1883 struct xfrm_encap_tmpl
* encap
;
1884 encap
= (struct xfrm_encap_tmpl
*)RTA_DATA(rtattr
);
1885 encap
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1886 encap
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1889 rtattr
= RTA_NEXT(rtattr
, rtsize
);
1892 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
1894 DBG1(DBG_KNL
, "unalbe to update SAD entry with SPI 0x%x", spi
);
1904 * Implementation of kernel_interface_t.query_sa.
1906 static status_t
query_sa(private_kernel_interface_t
*this, host_t
*dst
,
1907 u_int32_t spi
, protocol_id_t protocol
,
1908 u_int32_t
*use_time
)
1910 unsigned char request
[BUFFER_SIZE
];
1911 struct nlmsghdr
*out
= NULL
, *hdr
;
1912 struct xfrm_usersa_id
*sa_id
;
1913 struct xfrm_usersa_info
*sa
= NULL
;
1916 DBG2(DBG_KNL
, "querying SAD entry with SPI 0x%x", spi
);
1917 memset(&request
, 0, sizeof(request
));
1919 hdr
= (struct nlmsghdr
*)request
;
1920 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1921 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1922 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1924 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1925 host2xfrm(dst
, &sa_id
->daddr
);
1927 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1928 sa_id
->family
= dst
->get_family(dst
);
1930 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1933 while (NLMSG_OK(hdr
, len
))
1935 switch (hdr
->nlmsg_type
)
1937 case XFRM_MSG_NEWSA
:
1939 sa
= NLMSG_DATA(hdr
);
1944 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1945 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1946 strerror(-err
->error
), -err
->error
);
1950 hdr
= NLMSG_NEXT(hdr
, len
);
1961 DBG1(DBG_KNL
, "unable to query SAD entry with SPI 0x%x", spi
);
1966 *use_time
= sa
->curlft
.use_time
;
1972 * Implementation of kernel_interface_t.del_sa.
1974 static status_t
del_sa(private_kernel_interface_t
*this, host_t
*dst
,
1975 u_int32_t spi
, protocol_id_t protocol
)
1977 unsigned char request
[BUFFER_SIZE
];
1978 struct nlmsghdr
*hdr
;
1979 struct xfrm_usersa_id
*sa_id
;
1981 memset(&request
, 0, sizeof(request
));
1983 DBG2(DBG_KNL
, "deleting SAD entry with SPI 0x%x", spi
);
1985 hdr
= (struct nlmsghdr
*)request
;
1986 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1987 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1988 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1990 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1991 host2xfrm(dst
, &sa_id
->daddr
);
1993 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
1994 sa_id
->family
= dst
->get_family(dst
);
1996 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
1998 DBG1(DBG_KNL
, "unalbe to delete SAD entry with SPI 0x%x", spi
);
2001 DBG2(DBG_KNL
, "deleted SAD entry with SPI 0x%x", spi
);
2006 * Implementation of kernel_interface_t.add_policy.
2008 static status_t
add_policy(private_kernel_interface_t
*this,
2009 host_t
*src
, host_t
*dst
,
2010 traffic_selector_t
*src_ts
,
2011 traffic_selector_t
*dst_ts
,
2012 policy_dir_t direction
, protocol_id_t protocol
,
2013 u_int32_t reqid
, bool high_prio
, mode_t mode
)
2015 iterator_t
*iterator
;
2016 policy_entry_t
*current
, *policy
;
2018 unsigned char request
[BUFFER_SIZE
];
2019 struct xfrm_userpolicy_info
*policy_info
;
2020 struct nlmsghdr
*hdr
;
2022 /* create a policy */
2023 policy
= malloc_thing(policy_entry_t
);
2024 memset(policy
, 0, sizeof(policy_entry_t
));
2025 policy
->sel
= ts2selector(src_ts
, dst_ts
);
2026 policy
->direction
= direction
;
2028 /* find the policy, which matches EXACTLY */
2029 pthread_mutex_lock(&this->mutex
);
2030 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
2031 while (iterator
->iterate(iterator
, (void**)¤t
))
2033 if (memcmp(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) == 0 &&
2034 policy
->direction
== current
->direction
)
2036 /* use existing policy */
2037 current
->refcount
++;
2038 DBG2(DBG_KNL
, "policy %R===%R already exists, increasing ",
2039 "refcount", src_ts
, dst_ts
);
2046 iterator
->destroy(iterator
);
2048 { /* apply the new one, if we have no such policy */
2049 this->policies
->insert_last(this->policies
, policy
);
2050 policy
->refcount
= 1;
2053 DBG2(DBG_KNL
, "adding policy %R===%R", src_ts
, dst_ts
);
2055 memset(&request
, 0, sizeof(request
));
2056 hdr
= (struct nlmsghdr
*)request
;
2057 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2058 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
2059 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2061 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2062 policy_info
->sel
= policy
->sel
;
2063 policy_info
->dir
= policy
->direction
;
2064 /* calculate priority based on source selector size, small size = high prio */
2065 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
2066 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
2067 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
2068 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
2069 policy_info
->action
= XFRM_POLICY_ALLOW
;
2070 policy_info
->share
= XFRM_SHARE_ANY
;
2071 pthread_mutex_unlock(&this->mutex
);
2073 /* policies don't expire */
2074 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2075 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2076 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2077 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2078 policy_info
->lft
.soft_add_expires_seconds
= 0;
2079 policy_info
->lft
.hard_add_expires_seconds
= 0;
2080 policy_info
->lft
.soft_use_expires_seconds
= 0;
2081 policy_info
->lft
.hard_use_expires_seconds
= 0;
2083 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2084 rthdr
->rta_type
= XFRMA_TMPL
;
2086 rthdr
->rta_len
= sizeof(struct xfrm_user_tmpl
);
2087 rthdr
->rta_len
= RTA_LENGTH(rthdr
->rta_len
);
2089 hdr
->nlmsg_len
+= rthdr
->rta_len
;
2090 if (hdr
->nlmsg_len
> sizeof(request
))
2095 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2096 tmpl
->reqid
= reqid
;
2097 tmpl
->id
.proto
= (protocol
== PROTO_AH
) ? KERNEL_AH
: KERNEL_ESP
;
2098 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2100 tmpl
->family
= src
->get_family(src
);
2102 host2xfrm(src
, &tmpl
->saddr
);
2103 host2xfrm(dst
, &tmpl
->id
.daddr
);
2105 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2107 DBG1(DBG_KNL
, "unable to add policy %R===%R", src_ts
, dst_ts
);
2111 /* install a route, if:
2112 * - we are NOT updating a policy
2113 * - this is a forward policy (to just get one for each child)
2114 * - we are in tunnel mode
2115 * - we are not using IPv6 (does not work correctly yet!)
2117 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
2118 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
)
2120 policy
->route
= malloc_thing(route_entry_t
);
2121 if (get_address_by_ts(this, dst_ts
, &policy
->route
->src_ip
) == SUCCESS
)
2123 policy
->route
->gateway
= dst
->clone(dst
);
2124 policy
->route
->if_index
= get_interface_index(this, dst
);
2125 policy
->route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2126 memcpy(policy
->route
->dst_net
.ptr
, &policy
->sel
.saddr
, policy
->route
->dst_net
.len
);
2127 policy
->route
->prefixlen
= policy
->sel
.prefixlen_s
;
2129 if (manage_srcroute(this, RTM_NEWROUTE
, NLM_F_CREATE
| NLM_F_EXCL
,
2130 policy
->route
) != SUCCESS
)
2132 DBG1(DBG_KNL
, "unable to install source route for %H",
2133 policy
->route
->src_ip
);
2134 route_entry_destroy(policy
->route
);
2135 policy
->route
= NULL
;
2140 free(policy
->route
);
2141 policy
->route
= NULL
;
2149 * Implementation of kernel_interface_t.query_policy.
2151 static status_t
query_policy(private_kernel_interface_t
*this,
2152 traffic_selector_t
*src_ts
,
2153 traffic_selector_t
*dst_ts
,
2154 policy_dir_t direction
, u_int32_t
*use_time
)
2156 unsigned char request
[BUFFER_SIZE
];
2157 struct nlmsghdr
*out
= NULL
, *hdr
;
2158 struct xfrm_userpolicy_id
*policy_id
;
2159 struct xfrm_userpolicy_info
*policy
= NULL
;
2162 memset(&request
, 0, sizeof(request
));
2164 DBG2(DBG_KNL
, "querying policy %R===%R", src_ts
, dst_ts
);
2166 hdr
= (struct nlmsghdr
*)request
;
2167 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2168 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2169 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2171 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2172 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2173 policy_id
->dir
= direction
;
2175 if (netlink_send(this, this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2178 while (NLMSG_OK(hdr
, len
))
2180 switch (hdr
->nlmsg_type
)
2182 case XFRM_MSG_NEWPOLICY
:
2184 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2189 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2190 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2191 strerror(-err
->error
), -err
->error
);
2195 hdr
= NLMSG_NEXT(hdr
, len
);
2206 DBG2(DBG_KNL
, "unable to query policy %R===%R", src_ts
, dst_ts
);
2210 *use_time
= (time_t)policy
->curlft
.use_time
;
2217 * Implementation of kernel_interface_t.del_policy.
2219 static status_t
del_policy(private_kernel_interface_t
*this,
2220 traffic_selector_t
*src_ts
,
2221 traffic_selector_t
*dst_ts
,
2222 policy_dir_t direction
)
2224 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
2225 route_entry_t
*route
;
2226 unsigned char request
[BUFFER_SIZE
];
2227 struct nlmsghdr
*hdr
;
2228 struct xfrm_userpolicy_id
*policy_id
;
2229 iterator_t
*iterator
;
2231 DBG2(DBG_KNL
, "deleting policy %R===%R", src_ts
, dst_ts
);
2233 /* create a policy */
2234 memset(&policy
, 0, sizeof(policy_entry_t
));
2235 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2236 policy
.direction
= direction
;
2238 /* find the policy */
2239 iterator
= this->policies
->create_iterator_locked(this->policies
, &this->mutex
);
2240 while (iterator
->iterate(iterator
, (void**)¤t
))
2242 if (memcmp(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) == 0 &&
2243 policy
.direction
== current
->direction
)
2245 to_delete
= current
;
2246 if (--to_delete
->refcount
> 0)
2248 /* is used by more SAs, keep in kernel */
2249 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2250 iterator
->destroy(iterator
);
2253 /* remove if last reference */
2254 iterator
->remove(iterator
);
2258 iterator
->destroy(iterator
);
2261 DBG1(DBG_KNL
, "deleting policy %R===%R failed, not found", src_ts
, dst_ts
);
2265 memset(&request
, 0, sizeof(request
));
2267 hdr
= (struct nlmsghdr
*)request
;
2268 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2269 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2270 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2272 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2273 policy_id
->sel
= to_delete
->sel
;
2274 policy_id
->dir
= direction
;
2276 route
= to_delete
->route
;
2279 if (netlink_send_ack(this, this->socket_xfrm
, hdr
) != SUCCESS
)
2281 DBG1(DBG_KNL
, "unable to delete policy %R===%R", src_ts
, dst_ts
);
2287 if (manage_srcroute(this, RTM_DELROUTE
, 0, route
) != SUCCESS
)
2289 DBG1(DBG_KNL
, "error uninstalling route installed with "
2290 "policy %R===%R", src_ts
, dst_ts
);
2292 route_entry_destroy(route
);
2298 * Implementation of kernel_interface_t.destroy.
2300 static void destroy(private_kernel_interface_t
*this)
2302 this->job
->cancel(this->job
);
2303 close(this->socket_xfrm_events
);
2304 close(this->socket_xfrm
);
2305 close(this->socket_rt_events
);
2306 close(this->socket_rt
);
2307 this->policies
->destroy(this->policies
);
2308 this->ifaces
->destroy_function(this->ifaces
, (void*)iface_entry_destroy
);
2313 * Described in header.
2315 kernel_interface_t
*kernel_interface_create()
2317 private_kernel_interface_t
*this = malloc_thing(private_kernel_interface_t
);
2318 struct sockaddr_nl addr
;
2320 /* public functions */
2321 this->public.get_spi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
2322 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
*,mode_t
,bool,bool))add_sa
;
2323 this->public.update_sa
= (status_t(*)(kernel_interface_t
*,u_int32_t
,protocol_id_t
,host_t
*,host_t
*,host_t
*,host_t
*))update_sa
;
2324 this->public.query_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
*))query_sa
;
2325 this->public.del_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
2326 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
))add_policy
;
2327 this->public.query_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
2328 this->public.del_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
2329 this->public.get_interface
= (char*(*)(kernel_interface_t
*,host_t
*))get_interface_name
;
2330 this->public.create_address_iterator
= (iterator_t
*(*)(kernel_interface_t
*))create_address_iterator
;
2331 this->public.get_source_addr
= (host_t
*(*)(kernel_interface_t
*, host_t
*dest
))get_source_addr
;
2332 this->public.add_ip
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*)) add_ip
;
2333 this->public.del_ip
= (status_t(*)(kernel_interface_t
*,host_t
*)) del_ip
;
2334 this->public.destroy
= (void(*)(kernel_interface_t
*)) destroy
;
2336 /* private members */
2337 this->policies
= linked_list_create();
2338 this->ifaces
= linked_list_create();
2341 pthread_mutex_init(&this->mutex
,NULL
);
2343 memset(&addr
, 0, sizeof(addr
));
2344 addr
.nl_family
= AF_NETLINK
;
2346 /* create and bind RT socket */
2347 this->socket_rt
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2348 if (this->socket_rt
<= 0)
2350 charon
->kill(charon
, "unable to create RT netlink socket");
2353 if (bind(this->socket_rt
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2355 charon
->kill(charon
, "unable to bind RT netlink socket");
2358 /* create and bind RT socket for events (address/interface/route changes) */
2359 this->socket_rt_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
2360 if (this->socket_rt_events
<= 0)
2362 charon
->kill(charon
, "unable to create RT event socket");
2364 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR
|
2365 RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_ROUTE
| RTMGRP_LINK
;
2366 if (bind(this->socket_rt_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2368 charon
->kill(charon
, "unable to bind RT event socket");
2371 /* create and bind XFRM socket */
2372 this->socket_xfrm
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2373 if (this->socket_xfrm
<= 0)
2375 charon
->kill(charon
, "unable to create XFRM netlink socket");
2378 if (bind(this->socket_xfrm
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2380 charon
->kill(charon
, "unable to bind XFRM netlink socket");
2383 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
2384 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2385 if (this->socket_xfrm_events
<= 0)
2387 charon
->kill(charon
, "unable to create XFRM event socket");
2389 addr
.nl_groups
= XFRMGRP_ACQUIRE
| XFRMGRP_EXPIRE
;
2390 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2392 charon
->kill(charon
, "unable to bind XFRM event socket");
2395 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
2397 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
2399 if (init_address_list(this) != SUCCESS
)
2401 charon
->kill(charon
, "unable to get interface list");
2404 return &this->public;