88df4f3d26a59daa5365b4532cc8294bda35d987
[strongswan.git] / src / charon / kernel / kernel_interface.c
1 /*
2 * Copyright (C) 2005-2007 Martin Willi
3 * Copyright (C) 2006-2007 Tobias Brunner
4 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
5 * Copyright (C) 2006 Daniel Roethlisberger
6 * Copyright (C) 2005 Jan Hutter
7 * Hochschule fuer Technik Rapperswil
8 * Copyright (C) 2003 Herbert Xu.
9 *
10 * Based on xfrm code from pluto.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * $Id$
23 */
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/time.h>
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/xfrm.h>
31 #include <linux/udp.h>
32 #include <pthread.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <net/if.h>
38 #include <sys/ioctl.h>
39
40 #include "kernel_interface.h"
41
42 #include <daemon.h>
43 #include <utils/linked_list.h>
44 #include <processing/jobs/delete_child_sa_job.h>
45 #include <processing/jobs/rekey_child_sa_job.h>
46 #include <processing/jobs/acquire_job.h>
47 #include <processing/jobs/callback_job.h>
48 #include <processing/jobs/roam_job.h>
49
50 /** routing table for routes installed by us */
51 #ifndef IPSEC_ROUTING_TABLE
52 #define IPSEC_ROUTING_TABLE 100
53 #endif
54 #ifndef IPSEC_ROUTING_TABLE_PRIO
55 #define IPSEC_ROUTING_TABLE_PRIO 100
56 #endif
57
58 /** kernel level protocol identifiers */
59 #define KERNEL_ESP 50
60 #define KERNEL_AH 51
61
62 /** default priority of installed policies */
63 #define PRIO_LOW 3000
64 #define PRIO_HIGH 2000
65
66 /** delay before firing roam jobs (ms) */
67 #define ROAM_DELAY 100
68
69 #define BUFFER_SIZE 1024
70
71 /**
72 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
73 * 'usual' netlink data x like 'struct xfrm_usersa_info'
74 */
75 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
76 /**
77 * returns a pointer to the next rtattr following rta.
78 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
79 */
80 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
81 /**
82 * returns the total size of attached rta data
83 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
84 */
85 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
86
87 typedef struct kernel_algorithm_t kernel_algorithm_t;
88
89 /**
90 * Mapping from the algorithms defined in IKEv2 to
91 * kernel level algorithm names and their key length
92 */
93 struct kernel_algorithm_t {
94 /**
95 * Identifier specified in IKEv2
96 */
97 int ikev2_id;
98
99 /**
100 * Name of the algorithm, as used as kernel identifier
101 */
102 char *name;
103
104 /**
105 * Key length in bits, if fixed size
106 */
107 u_int key_size;
108 };
109 #define END_OF_LIST -1
110
111 /**
112 * Algorithms for encryption
113 */
114 kernel_algorithm_t encryption_algs[] = {
115 /* {ENCR_DES_IV64, "***", 0}, */
116 {ENCR_DES, "des", 64},
117 {ENCR_3DES, "des3_ede", 192},
118 /* {ENCR_RC5, "***", 0}, */
119 /* {ENCR_IDEA, "***", 0}, */
120 {ENCR_CAST, "cast128", 0},
121 {ENCR_BLOWFISH, "blowfish", 0},
122 /* {ENCR_3IDEA, "***", 0}, */
123 /* {ENCR_DES_IV32, "***", 0}, */
124 {ENCR_NULL, "cipher_null", 0},
125 {ENCR_AES_CBC, "aes", 0},
126 /* {ENCR_AES_CTR, "***", 0}, */
127 {END_OF_LIST, NULL, 0},
128 };
129
130 /**
131 * Algorithms for integrity protection
132 */
133 kernel_algorithm_t integrity_algs[] = {
134 {AUTH_HMAC_MD5_96, "md5", 128},
135 {AUTH_HMAC_SHA1_96, "sha1", 160},
136 {AUTH_HMAC_SHA2_256_128, "sha256", 256},
137 {AUTH_HMAC_SHA2_384_192, "sha384", 384},
138 {AUTH_HMAC_SHA2_512_256, "sha512", 512},
139 /* {AUTH_DES_MAC, "***", 0}, */
140 /* {AUTH_KPDK_MD5, "***", 0}, */
141 {AUTH_AES_XCBC_96, "xcbc(aes)", 128},
142 {END_OF_LIST, NULL, 0},
143 };
144
145 /**
146 * Look up a kernel algorithm name and its key size
147 */
148 char* lookup_algorithm(kernel_algorithm_t *kernel_algo,
149 algorithm_t *ikev2_algo, u_int *key_size)
150 {
151 while (kernel_algo->ikev2_id != END_OF_LIST)
152 {
153 if (ikev2_algo->algorithm == kernel_algo->ikev2_id)
154 {
155 /* match, evaluate key length */
156 if (ikev2_algo->key_size)
157 { /* variable length */
158 *key_size = ikev2_algo->key_size;
159 }
160 else
161 { /* fixed length */
162 *key_size = kernel_algo->key_size;
163 }
164 return kernel_algo->name;
165 }
166 kernel_algo++;
167 }
168 return NULL;
169 }
170
171 typedef struct route_entry_t route_entry_t;
172
173 /**
174 * installed routing entry
175 */
176 struct route_entry_t {
177
178 /** Index of the interface the route is bound to */
179 int if_index;
180
181 /** Source ip of the route */
182 host_t *src_ip;
183
184 /** gateway for this route */
185 host_t *gateway;
186
187 /** Destination net */
188 chunk_t dst_net;
189
190 /** Destination net prefixlen */
191 u_int8_t prefixlen;
192 };
193
194 /**
195 * destroy an route_entry_t object
196 */
197 static void route_entry_destroy(route_entry_t *this)
198 {
199 this->src_ip->destroy(this->src_ip);
200 this->gateway->destroy(this->gateway);
201 chunk_free(&this->dst_net);
202 free(this);
203 }
204
205 typedef struct policy_entry_t policy_entry_t;
206
207 /**
208 * installed kernel policy.
209 */
210 struct policy_entry_t {
211
212 /** direction of this policy: in, out, forward */
213 u_int8_t direction;
214
215 /** reqid of the policy */
216 u_int32_t reqid;
217
218 /** parameters of installed policy */
219 struct xfrm_selector sel;
220
221 /** associated route installed for this policy */
222 route_entry_t *route;
223
224 /** by how many CHILD_SA's this policy is used */
225 u_int refcount;
226 };
227
228 typedef struct addr_entry_t addr_entry_t;
229
230 /**
231 * IP address in an inface_entry_t
232 */
233 struct addr_entry_t {
234
235 /** The ip address */
236 host_t *ip;
237
238 /** virtual IP managed by us */
239 bool virtual;
240
241 /** scope of the address */
242 u_char scope;
243
244 /** Number of times this IP is used, if virtual */
245 u_int refcount;
246 };
247
248 /**
249 * destroy a addr_entry_t object
250 */
251 static void addr_entry_destroy(addr_entry_t *this)
252 {
253 this->ip->destroy(this->ip);
254 free(this);
255 }
256
257 typedef struct iface_entry_t iface_entry_t;
258
259 /**
260 * A network interface on this system, containing addr_entry_t's
261 */
262 struct iface_entry_t {
263
264 /** interface index */
265 int ifindex;
266
267 /** name of the interface */
268 char ifname[IFNAMSIZ];
269
270 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
271 u_int flags;
272
273 /** list of addresses as host_t */
274 linked_list_t *addrs;
275 };
276
277 /**
278 * destroy an interface entry
279 */
280 static void iface_entry_destroy(iface_entry_t *this)
281 {
282 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
283 free(this);
284 }
285
286 typedef struct private_kernel_interface_t private_kernel_interface_t;
287
288 /**
289 * Private variables and functions of kernel_interface class.
290 */
291 struct private_kernel_interface_t {
292 /**
293 * Public part of the kernel_interface_t object.
294 */
295 kernel_interface_t public;
296
297 /**
298 * mutex to lock access to netlink socket
299 */
300 pthread_mutex_t nl_mutex;
301
302 /**
303 * mutex to lock access to various lists
304 */
305 pthread_mutex_t mutex;
306
307 /**
308 * condition variable to signal virtual IP add/removal
309 */
310 pthread_cond_t cond;
311
312 /**
313 * List of installed policies (policy_entry_t)
314 */
315 linked_list_t *policies;
316
317 /**
318 * Cached list of interfaces and its adresses (iface_entry_t)
319 */
320 linked_list_t *ifaces;
321
322 /**
323 * iterator used in hook()
324 */
325 iterator_t *hiter;
326
327 /**
328 * job receiving netlink events
329 */
330 callback_job_t *job;
331
332 /**
333 * current sequence number for netlink request
334 */
335 int seq;
336
337 /**
338 * Netlink xfrm socket (IPsec)
339 */
340 int socket_xfrm;
341
342 /**
343 * netlink xfrm socket to receive acquire and expire events
344 */
345 int socket_xfrm_events;
346
347 /**
348 * Netlink rt socket (routing)
349 */
350 int socket_rt;
351
352 /**
353 * Netlink rt socket to receive address change events
354 */
355 int socket_rt_events;
356
357 /**
358 * time of the last roam_job
359 */
360 struct timeval last_roam;
361 };
362
363 /**
364 * convert a host_t to a struct xfrm_address
365 */
366 static void host2xfrm(host_t *host, xfrm_address_t *xfrm)
367 {
368 chunk_t chunk = host->get_address(host);
369 memcpy(xfrm, chunk.ptr, min(chunk.len, sizeof(xfrm_address_t)));
370 }
371
372 /**
373 * convert a traffic selector address range to subnet and its mask.
374 */
375 static void ts2subnet(traffic_selector_t* ts,
376 xfrm_address_t *net, u_int8_t *mask)
377 {
378 /* there is no way to do this cleanly, as the address range may
379 * be anything else but a subnet. We use from_addr as subnet
380 * and try to calculate a usable subnet mask.
381 */
382 int byte, bit;
383 bool found = FALSE;
384 chunk_t from, to;
385 size_t size = (ts->get_type(ts) == TS_IPV4_ADDR_RANGE) ? 4 : 16;
386
387 from = ts->get_from_address(ts);
388 to = ts->get_to_address(ts);
389
390 *mask = (size * 8);
391 /* go trough all bits of the addresses, beginning in the front.
392 * as long as they are equal, the subnet gets larger
393 */
394 for (byte = 0; byte < size; byte++)
395 {
396 for (bit = 7; bit >= 0; bit--)
397 {
398 if ((1<<bit & from.ptr[byte]) != (1<<bit & to.ptr[byte]))
399 {
400 *mask = ((7 - bit) + (byte * 8));
401 found = TRUE;
402 break;
403 }
404 }
405 if (found)
406 {
407 break;
408 }
409 }
410 memcpy(net, from.ptr, from.len);
411 chunk_free(&from);
412 chunk_free(&to);
413 }
414
415 /**
416 * convert a traffic selector port range to port/portmask
417 */
418 static void ts2ports(traffic_selector_t* ts,
419 u_int16_t *port, u_int16_t *mask)
420 {
421 /* linux does not seem to accept complex portmasks. Only
422 * any or a specific port is allowed. We set to any, if we have
423 * a port range, or to a specific, if we have one port only.
424 */
425 u_int16_t from, to;
426
427 from = ts->get_from_port(ts);
428 to = ts->get_to_port(ts);
429
430 if (from == to)
431 {
432 *port = htons(from);
433 *mask = ~0;
434 }
435 else
436 {
437 *port = 0;
438 *mask = 0;
439 }
440 }
441
442 /**
443 * convert a pair of traffic_selectors to a xfrm_selector
444 */
445 static struct xfrm_selector ts2selector(traffic_selector_t *src,
446 traffic_selector_t *dst)
447 {
448 struct xfrm_selector sel;
449
450 memset(&sel, 0, sizeof(sel));
451 sel.family = src->get_type(src) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
452 /* src or dest proto may be "any" (0), use more restrictive one */
453 sel.proto = max(src->get_protocol(src), dst->get_protocol(dst));
454 ts2subnet(dst, &sel.daddr, &sel.prefixlen_d);
455 ts2subnet(src, &sel.saddr, &sel.prefixlen_s);
456 ts2ports(dst, &sel.dport, &sel.dport_mask);
457 ts2ports(src, &sel.sport, &sel.sport_mask);
458 sel.ifindex = 0;
459 sel.user = 0;
460
461 return sel;
462 }
463
464 /**
465 * Creates an rtattr and adds it to the netlink message
466 */
467 static void add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
468 size_t buflen)
469 {
470 struct rtattr *rta;
471
472 if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_ALIGN(data.len) > buflen)
473 {
474 DBG1(DBG_KNL, "unable to add attribute, buffer too small");
475 return;
476 }
477
478 rta = (struct rtattr*)(((char*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len));
479 rta->rta_type = rta_type;
480 rta->rta_len = RTA_LENGTH(data.len);
481 memcpy(RTA_DATA(rta), data.ptr, data.len);
482 hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
483 }
484
485 /**
486 * process a XFRM_MSG_ACQUIRE from kernel
487 */
488 static void process_acquire(private_kernel_interface_t *this, struct nlmsghdr *hdr)
489 {
490 u_int32_t reqid = 0;
491 job_t *job;
492 struct rtattr *rtattr = XFRM_RTA(hdr, struct xfrm_user_acquire);
493 size_t rtsize = XFRM_PAYLOAD(hdr, struct xfrm_user_tmpl);
494
495 if (RTA_OK(rtattr, rtsize))
496 {
497 if (rtattr->rta_type == XFRMA_TMPL)
498 {
499 struct xfrm_user_tmpl* tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rtattr);
500 reqid = tmpl->reqid;
501 }
502 }
503 if (reqid == 0)
504 {
505 DBG1(DBG_KNL, "received a XFRM_MSG_ACQUIRE, but no reqid found");
506 return;
507 }
508 DBG2(DBG_KNL, "received a XFRM_MSG_ACQUIRE");
509 DBG1(DBG_KNL, "creating acquire job for CHILD_SA with reqid %d", reqid);
510 job = (job_t*)acquire_job_create(reqid);
511 charon->processor->queue_job(charon->processor, job);
512 }
513
514 /**
515 * process a XFRM_MSG_EXPIRE from kernel
516 */
517 static void process_expire(private_kernel_interface_t *this, struct nlmsghdr *hdr)
518 {
519 job_t *job;
520 protocol_id_t protocol;
521 u_int32_t spi, reqid;
522 struct xfrm_user_expire *expire;
523
524 expire = (struct xfrm_user_expire*)NLMSG_DATA(hdr);
525 protocol = expire->state.id.proto == KERNEL_ESP ? PROTO_ESP : PROTO_AH;
526 spi = expire->state.id.spi;
527 reqid = expire->state.reqid;
528
529 DBG2(DBG_KNL, "received a XFRM_MSG_EXPIRE");
530 DBG1(DBG_KNL, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
531 expire->hard ? "delete" : "rekey", protocol_id_names,
532 protocol, ntohl(spi), reqid);
533 if (expire->hard)
534 {
535 job = (job_t*)delete_child_sa_job_create(reqid, protocol, spi);
536 }
537 else
538 {
539 job = (job_t*)rekey_child_sa_job_create(reqid, protocol, spi);
540 }
541 charon->processor->queue_job(charon->processor, job);
542 }
543
544 /**
545 * start a roaming job. We delay it for a second and fire only one job
546 * for multiple events. Otherwise we would create two many jobs.
547 */
548 static void fire_roam_job(private_kernel_interface_t *this, bool address)
549 {
550 struct timeval now;
551
552 if (gettimeofday(&now, NULL) == 0)
553 {
554 if (timercmp(&now, &this->last_roam, >))
555 {
556 now.tv_usec += ROAM_DELAY * 1000;
557 while (now.tv_usec > 1000000)
558 {
559 now.tv_sec++;
560 now.tv_usec -= 1000000;
561 }
562 this->last_roam = now;
563 charon->scheduler->schedule_job(charon->scheduler,
564 (job_t*)roam_job_create(address), ROAM_DELAY);
565 }
566 }
567 }
568
569 /**
570 * process RTM_NEWLINK/RTM_DELLINK from kernel
571 */
572 static void process_link(private_kernel_interface_t *this,
573 struct nlmsghdr *hdr, bool event)
574 {
575 struct ifinfomsg* msg = (struct ifinfomsg*)(NLMSG_DATA(hdr));
576 struct rtattr *rta = IFLA_RTA(msg);
577 size_t rtasize = IFLA_PAYLOAD (hdr);
578 iterator_t *iterator;
579 iface_entry_t *current, *entry = NULL;
580 char *name = NULL;
581 bool update = FALSE;
582
583 while(RTA_OK(rta, rtasize))
584 {
585 switch (rta->rta_type)
586 {
587 case IFLA_IFNAME:
588 name = RTA_DATA(rta);
589 break;
590 }
591 rta = RTA_NEXT(rta, rtasize);
592 }
593 if (!name)
594 {
595 name = "(unknown)";
596 }
597
598 switch (hdr->nlmsg_type)
599 {
600 case RTM_NEWLINK:
601 {
602 if (msg->ifi_flags & IFF_LOOPBACK)
603 { /* ignore loopback interfaces */
604 break;
605 }
606 iterator = this->ifaces->create_iterator_locked(this->ifaces,
607 &this->mutex);
608 while (iterator->iterate(iterator, (void**)&current))
609 {
610 if (current->ifindex == msg->ifi_index)
611 {
612 entry = current;
613 break;
614 }
615 }
616 if (!entry)
617 {
618 entry = malloc_thing(iface_entry_t);
619 entry->ifindex = msg->ifi_index;
620 entry->flags = 0;
621 entry->addrs = linked_list_create();
622 this->ifaces->insert_last(this->ifaces, entry);
623 }
624 memcpy(entry->ifname, name, IFNAMSIZ);
625 entry->ifname[IFNAMSIZ-1] = '\0';
626 if (event)
627 {
628 if (!(entry->flags & IFF_UP) && (msg->ifi_flags & IFF_UP))
629 {
630 update = TRUE;
631 DBG1(DBG_KNL, "interface %s activated", name);
632 }
633 if ((entry->flags & IFF_UP) && !(msg->ifi_flags & IFF_UP))
634 {
635 update = TRUE;
636 DBG1(DBG_KNL, "interface %s deactivated", name);
637 }
638 }
639 entry->flags = msg->ifi_flags;
640 iterator->destroy(iterator);
641 break;
642 }
643 case RTM_DELLINK:
644 {
645 iterator = this->ifaces->create_iterator_locked(this->ifaces,
646 &this->mutex);
647 while (iterator->iterate(iterator, (void**)&current))
648 {
649 if (current->ifindex == msg->ifi_index)
650 {
651 /* we do not remove it, as an address may be added to a
652 * "down" interface and we wan't to know that. */
653 current->flags = msg->ifi_flags;
654 break;
655 }
656 }
657 iterator->destroy(iterator);
658 break;
659 }
660 }
661
662 /* send an update to all IKE_SAs */
663 if (update && event)
664 {
665 fire_roam_job(this, TRUE);
666 }
667 }
668
669 /**
670 * process RTM_NEWADDR/RTM_DELADDR from kernel
671 */
672 static void process_addr(private_kernel_interface_t *this,
673 struct nlmsghdr *hdr, bool event)
674 {
675 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
676 struct rtattr *rta = IFA_RTA(msg);
677 size_t rtasize = IFA_PAYLOAD (hdr);
678 host_t *host = NULL;
679 iterator_t *ifaces, *addrs;
680 iface_entry_t *iface;
681 addr_entry_t *addr;
682 chunk_t local = chunk_empty, address = chunk_empty;
683 bool update = FALSE, found = FALSE, changed = FALSE;
684
685 while(RTA_OK(rta, rtasize))
686 {
687 switch (rta->rta_type)
688 {
689 case IFA_LOCAL:
690 local.ptr = RTA_DATA(rta);
691 local.len = RTA_PAYLOAD(rta);
692 break;
693 case IFA_ADDRESS:
694 address.ptr = RTA_DATA(rta);
695 address.len = RTA_PAYLOAD(rta);
696 break;
697 }
698 rta = RTA_NEXT(rta, rtasize);
699 }
700
701 /* For PPP interfaces, we need the IFA_LOCAL address,
702 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
703 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
704 if (local.ptr)
705 {
706 host = host_create_from_chunk(msg->ifa_family, local, 0);
707 }
708 else if (address.ptr)
709 {
710 host = host_create_from_chunk(msg->ifa_family, address, 0);
711 }
712
713 if (host == NULL)
714 { /* bad family? */
715 return;
716 }
717
718 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
719 while (ifaces->iterate(ifaces, (void**)&iface))
720 {
721 if (iface->ifindex == msg->ifa_index)
722 {
723 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
724 while (addrs->iterate(addrs, (void**)&addr))
725 {
726 if (host->ip_equals(host, addr->ip))
727 {
728 found = TRUE;
729 if (hdr->nlmsg_type == RTM_DELADDR)
730 {
731 changed = TRUE;
732 addrs->remove(addrs);
733 if (!addr->virtual)
734 {
735 DBG1(DBG_KNL, "%H disappeared from %s",
736 host, iface->ifname);
737 }
738 addr_entry_destroy(addr);
739 }
740 else if (hdr->nlmsg_type == RTM_NEWADDR && addr->virtual)
741 {
742 addr->refcount = 1;
743 }
744 }
745 }
746 addrs->destroy(addrs);
747
748 if (hdr->nlmsg_type == RTM_NEWADDR)
749 {
750 if (!found)
751 {
752 found = TRUE;
753 changed = TRUE;
754 addr = malloc_thing(addr_entry_t);
755 addr->ip = host->clone(host);
756 addr->virtual = FALSE;
757 addr->refcount = 1;
758 addr->scope = msg->ifa_scope;
759
760 iface->addrs->insert_last(iface->addrs, addr);
761 if (event)
762 {
763 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
764 }
765 }
766 }
767 if (found && (iface->flags & IFF_UP))
768 {
769 update = TRUE;
770 }
771 break;
772 }
773 }
774 ifaces->destroy(ifaces);
775 host->destroy(host);
776
777 /* send an update to all IKE_SAs */
778 if (update && event && changed)
779 {
780 fire_roam_job(this, TRUE);
781 }
782 }
783
784 /**
785 * Receives events from kernel
786 */
787 static job_requeue_t receive_events(private_kernel_interface_t *this)
788 {
789 char response[1024];
790 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
791 struct sockaddr_nl addr;
792 socklen_t addr_len = sizeof(addr);
793 int len, oldstate, maxfd, selected;
794 fd_set rfds;
795
796 FD_ZERO(&rfds);
797 FD_SET(this->socket_xfrm_events, &rfds);
798 FD_SET(this->socket_rt_events, &rfds);
799 maxfd = max(this->socket_xfrm_events, this->socket_rt_events);
800
801 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
802 selected = select(maxfd + 1, &rfds, NULL, NULL, NULL);
803 pthread_setcancelstate(oldstate, NULL);
804 if (selected <= 0)
805 {
806 DBG1(DBG_KNL, "selecting on sockets failed: %s", strerror(errno));
807 return JOB_REQUEUE_FAIR;
808 }
809 if (FD_ISSET(this->socket_xfrm_events, &rfds))
810 {
811 selected = this->socket_xfrm_events;
812 }
813 else if (FD_ISSET(this->socket_rt_events, &rfds))
814 {
815 selected = this->socket_rt_events;
816 }
817 else
818 {
819 return JOB_REQUEUE_DIRECT;
820 }
821
822 len = recvfrom(selected, response, sizeof(response), MSG_DONTWAIT,
823 (struct sockaddr*)&addr, &addr_len);
824 if (len < 0)
825 {
826 switch (errno)
827 {
828 case EINTR:
829 /* interrupted, try again */
830 return JOB_REQUEUE_DIRECT;
831 case EAGAIN:
832 /* no data ready, select again */
833 return JOB_REQUEUE_DIRECT;
834 default:
835 DBG1(DBG_KNL, "unable to receive from xfrm event socket");
836 sleep(1);
837 return JOB_REQUEUE_FAIR;
838 }
839 }
840 if (addr.nl_pid != 0)
841 { /* not from kernel. not interested, try another one */
842 return JOB_REQUEUE_DIRECT;
843 }
844
845 while (NLMSG_OK(hdr, len))
846 {
847 /* looks good so far, dispatch netlink message */
848 if (selected == this->socket_xfrm_events)
849 {
850 switch (hdr->nlmsg_type)
851 {
852 case XFRM_MSG_ACQUIRE:
853 process_acquire(this, hdr);
854 break;
855 case XFRM_MSG_EXPIRE:
856 process_expire(this, hdr);
857 break;
858 default:
859 break;
860 }
861 }
862 else if (selected == this->socket_rt_events)
863 {
864 switch (hdr->nlmsg_type)
865 {
866 case RTM_NEWADDR:
867 case RTM_DELADDR:
868 process_addr(this, hdr, TRUE);
869 pthread_cond_signal(&this->cond);
870 break;
871 case RTM_NEWLINK:
872 case RTM_DELLINK:
873 process_link(this, hdr, TRUE);
874 pthread_cond_signal(&this->cond);
875 break;
876 case RTM_NEWROUTE:
877 case RTM_DELROUTE:
878 fire_roam_job(this, FALSE);
879 break;
880 default:
881 break;
882 }
883 }
884 hdr = NLMSG_NEXT(hdr, len);
885 }
886 return JOB_REQUEUE_DIRECT;
887 }
888
889 /**
890 * send a netlink message and wait for a reply
891 */
892 static status_t netlink_send(private_kernel_interface_t *this,
893 int socket, struct nlmsghdr *in,
894 struct nlmsghdr **out, size_t *out_len)
895 {
896 int len, addr_len;
897 struct sockaddr_nl addr;
898 chunk_t result = chunk_empty, tmp;
899 struct nlmsghdr *msg, peek;
900
901 pthread_mutex_lock(&this->nl_mutex);
902
903 in->nlmsg_seq = ++this->seq;
904 in->nlmsg_pid = getpid();
905
906 memset(&addr, 0, sizeof(addr));
907 addr.nl_family = AF_NETLINK;
908 addr.nl_pid = 0;
909 addr.nl_groups = 0;
910
911 while (TRUE)
912 {
913 len = sendto(socket, in, in->nlmsg_len, 0,
914 (struct sockaddr*)&addr, sizeof(addr));
915
916 if (len != in->nlmsg_len)
917 {
918 if (errno == EINTR)
919 {
920 /* interrupted, try again */
921 continue;
922 }
923 pthread_mutex_unlock(&this->nl_mutex);
924 DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
925 return FAILED;
926 }
927 break;
928 }
929
930 while (TRUE)
931 {
932 char buf[4096];
933 tmp.len = sizeof(buf);
934 tmp.ptr = buf;
935 msg = (struct nlmsghdr*)tmp.ptr;
936
937 memset(&addr, 0, sizeof(addr));
938 addr.nl_family = AF_NETLINK;
939 addr.nl_pid = getpid();
940 addr.nl_groups = 0;
941 addr_len = sizeof(addr);
942
943 len = recvfrom(socket, tmp.ptr, tmp.len, 0,
944 (struct sockaddr*)&addr, &addr_len);
945
946 if (len < 0)
947 {
948 if (errno == EINTR)
949 {
950 DBG1(DBG_KNL, "got interrupted");
951 /* interrupted, try again */
952 continue;
953 }
954 DBG1(DBG_KNL, "error reading from netlink socket: %s", strerror(errno));
955 pthread_mutex_unlock(&this->nl_mutex);
956 return FAILED;
957 }
958 if (!NLMSG_OK(msg, len))
959 {
960 DBG1(DBG_KNL, "received corrupted netlink message");
961 pthread_mutex_unlock(&this->nl_mutex);
962 return FAILED;
963 }
964 if (msg->nlmsg_seq != this->seq)
965 {
966 DBG1(DBG_KNL, "received invalid netlink sequence number");
967 if (msg->nlmsg_seq < this->seq)
968 {
969 continue;
970 }
971 pthread_mutex_unlock(&this->nl_mutex);
972 return FAILED;
973 }
974
975 tmp.len = len;
976 result = chunk_cata("cc", result, tmp);
977
978 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
979 * numbers to detect multi header messages */
980 len = recvfrom(socket, &peek, sizeof(peek), MSG_PEEK | MSG_DONTWAIT,
981 (struct sockaddr*)&addr, &addr_len);
982
983 if (len == sizeof(peek) && peek.nlmsg_seq == this->seq)
984 {
985 /* seems to be multipart */
986 continue;
987 }
988 break;
989 }
990
991 *out_len = result.len;
992 *out = (struct nlmsghdr*)clalloc(result.ptr, result.len);
993
994 pthread_mutex_unlock(&this->nl_mutex);
995
996 return SUCCESS;
997 }
998
999 /**
1000 * send a netlink message and wait for its acknowlegde
1001 */
1002 static status_t netlink_send_ack(private_kernel_interface_t *this,
1003 int socket, struct nlmsghdr *in)
1004 {
1005 struct nlmsghdr *out, *hdr;
1006 size_t len;
1007
1008 if (netlink_send(this, socket, in, &out, &len) != SUCCESS)
1009 {
1010 return FAILED;
1011 }
1012 hdr = out;
1013 while (NLMSG_OK(hdr, len))
1014 {
1015 switch (hdr->nlmsg_type)
1016 {
1017 case NLMSG_ERROR:
1018 {
1019 struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(hdr);
1020
1021 if (err->error)
1022 {
1023 DBG1(DBG_KNL, "received netlink error: %s (%d)",
1024 strerror(-err->error), -err->error);
1025 free(out);
1026 return FAILED;
1027 }
1028 free(out);
1029 return SUCCESS;
1030 }
1031 default:
1032 hdr = NLMSG_NEXT(hdr, len);
1033 continue;
1034 case NLMSG_DONE:
1035 break;
1036 }
1037 break;
1038 }
1039 DBG1(DBG_KNL, "netlink request not acknowlegded");
1040 free(out);
1041 return FAILED;
1042 }
1043
1044 /**
1045 * Initialize a list of local addresses.
1046 */
1047 static status_t init_address_list(private_kernel_interface_t *this)
1048 {
1049 char request[BUFFER_SIZE];
1050 struct nlmsghdr *out, *current, *in;
1051 struct rtgenmsg *msg;
1052 size_t len;
1053 iterator_t *ifaces, *addrs;
1054 iface_entry_t *iface;
1055 addr_entry_t *addr;
1056
1057 DBG1(DBG_KNL, "listening on interfaces:");
1058
1059 memset(&request, 0, sizeof(request));
1060
1061 in = (struct nlmsghdr*)&request;
1062 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1063 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1064 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1065 msg->rtgen_family = AF_UNSPEC;
1066
1067 /* get all links */
1068 in->nlmsg_type = RTM_GETLINK;
1069 if (netlink_send(this, this->socket_rt, in, &out, &len) != SUCCESS)
1070 {
1071 return FAILED;
1072 }
1073 current = out;
1074 while (NLMSG_OK(current, len))
1075 {
1076 switch (current->nlmsg_type)
1077 {
1078 case NLMSG_DONE:
1079 break;
1080 case RTM_NEWLINK:
1081 process_link(this, current, FALSE);
1082 /* fall through */
1083 default:
1084 current = NLMSG_NEXT(current, len);
1085 continue;
1086 }
1087 break;
1088 }
1089 free(out);
1090
1091 /* get all interface addresses */
1092 in->nlmsg_type = RTM_GETADDR;
1093 if (netlink_send(this, this->socket_rt, in, &out, &len) != SUCCESS)
1094 {
1095 return FAILED;
1096 }
1097 current = out;
1098 while (NLMSG_OK(current, len))
1099 {
1100 switch (current->nlmsg_type)
1101 {
1102 case NLMSG_DONE:
1103 break;
1104 case RTM_NEWADDR:
1105 process_addr(this, current, FALSE);
1106 /* fall through */
1107 default:
1108 current = NLMSG_NEXT(current, len);
1109 continue;
1110 }
1111 break;
1112 }
1113 free(out);
1114
1115 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1116 while (ifaces->iterate(ifaces, (void**)&iface))
1117 {
1118 if (iface->flags & IFF_UP)
1119 {
1120 DBG1(DBG_KNL, " %s", iface->ifname);
1121 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1122 while (addrs->iterate(addrs, (void**)&addr))
1123 {
1124 DBG1(DBG_KNL, " %H", addr->ip);
1125 }
1126 addrs->destroy(addrs);
1127 }
1128 }
1129 ifaces->destroy(ifaces);
1130 return SUCCESS;
1131 }
1132
1133 /**
1134 * iterator hook to iterate over addrs
1135 */
1136 static hook_result_t addr_hook(private_kernel_interface_t *this,
1137 addr_entry_t *in, host_t **out)
1138 {
1139 if (in->virtual)
1140 { /* skip virtual interfaces added by us */
1141 return HOOK_SKIP;
1142 }
1143 if (in->scope >= RT_SCOPE_LINK)
1144 { /* skip addresses with a unusable scope */
1145 return HOOK_SKIP;
1146 }
1147 *out = in->ip;
1148 return HOOK_NEXT;
1149 }
1150
1151 /**
1152 * iterator hook to iterate over ifaces
1153 */
1154 static hook_result_t iface_hook(private_kernel_interface_t *this,
1155 iface_entry_t *in, host_t **out)
1156 {
1157 if (!(in->flags & IFF_UP))
1158 { /* skip interfaces not up */
1159 return HOOK_SKIP;
1160 }
1161
1162 if (this->hiter == NULL)
1163 {
1164 this->hiter = in->addrs->create_iterator(in->addrs, TRUE);
1165 this->hiter->set_iterator_hook(this->hiter,
1166 (iterator_hook_t*)addr_hook, this);
1167 }
1168 while (this->hiter->iterate(this->hiter, (void**)out))
1169 {
1170 return HOOK_AGAIN;
1171 }
1172 this->hiter->destroy(this->hiter);
1173 this->hiter = NULL;
1174 return HOOK_SKIP;
1175 }
1176
1177 /**
1178 * Implements kernel_interface_t.create_address_iterator.
1179 */
1180 static iterator_t *create_address_iterator(private_kernel_interface_t *this)
1181 {
1182 iterator_t *iterator;
1183
1184 /* This iterator is not only hooked, is is double-hooked. As we have stored
1185 * our addresses in iface_entry->addr_entry->ip, we need to iterate the
1186 * entries in each interface we iterate. This does the iface_hook. The
1187 * addr_hook returns the ip instead of the addr_entry. */
1188
1189 iterator = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1190 iterator->set_iterator_hook(iterator, (iterator_hook_t*)iface_hook, this);
1191 return iterator;
1192 }
1193
1194 /**
1195 * implementation of kernel_interface_t.get_interface_name
1196 */
1197 static char *get_interface_name(private_kernel_interface_t *this, host_t* ip)
1198 {
1199 iterator_t *ifaces, *addrs;
1200 iface_entry_t *iface;
1201 addr_entry_t *addr;
1202 char *name = NULL;
1203
1204 DBG2(DBG_KNL, "getting interface name for %H", ip);
1205
1206 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1207 while (ifaces->iterate(ifaces, (void**)&iface))
1208 {
1209 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1210 while (addrs->iterate(addrs, (void**)&addr))
1211 {
1212 if (ip->ip_equals(ip, addr->ip))
1213 {
1214 name = strdup(iface->ifname);
1215 break;
1216 }
1217 }
1218 addrs->destroy(addrs);
1219 if (name)
1220 {
1221 break;
1222 }
1223 }
1224 ifaces->destroy(ifaces);
1225
1226 if (name)
1227 {
1228 DBG2(DBG_KNL, "%H is on interface %s", ip, name);
1229 }
1230 else
1231 {
1232 DBG2(DBG_KNL, "%H is not a local address", ip);
1233 }
1234 return name;
1235 }
1236
1237 /**
1238 * Tries to find an ip address of a local interface that is included in the
1239 * supplied traffic selector.
1240 */
1241 static status_t get_address_by_ts(private_kernel_interface_t *this,
1242 traffic_selector_t *ts, host_t **ip)
1243 {
1244 iterator_t *ifaces, *addrs;
1245 iface_entry_t *iface;
1246 addr_entry_t *addr;
1247 host_t *host;
1248 int family;
1249 bool found = FALSE;
1250
1251 DBG2(DBG_KNL, "getting a local address in traffic selector %R", ts);
1252
1253 /* if we have a family which includes localhost, we do not
1254 * search for an IP, we use the default */
1255 family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
1256
1257 if (family == AF_INET)
1258 {
1259 host = host_create_from_string("127.0.0.1", 0);
1260 }
1261 else
1262 {
1263 host = host_create_from_string("::1", 0);
1264 }
1265
1266 if (ts->includes(ts, host))
1267 {
1268 *ip = host_create_any(family);
1269 host->destroy(host);
1270 DBG2(DBG_KNL, "using host %H", *ip);
1271 return SUCCESS;
1272 }
1273 host->destroy(host);
1274
1275 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1276 while (ifaces->iterate(ifaces, (void**)&iface))
1277 {
1278 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1279 while (addrs->iterate(addrs, (void**)&addr))
1280 {
1281 if (ts->includes(ts, addr->ip))
1282 {
1283 found = TRUE;
1284 *ip = addr->ip->clone(addr->ip);
1285 break;
1286 }
1287 }
1288 addrs->destroy(addrs);
1289 if (found)
1290 {
1291 break;
1292 }
1293 }
1294 ifaces->destroy(ifaces);
1295
1296 if (!found)
1297 {
1298 DBG1(DBG_KNL, "no local address found in traffic selector %R", ts);
1299 return FAILED;
1300 }
1301 DBG2(DBG_KNL, "using host %H", *ip);
1302 return SUCCESS;
1303 }
1304
1305 /**
1306 * get the interface of a local address
1307 */
1308 static int get_interface_index(private_kernel_interface_t *this, host_t* ip)
1309 {
1310 iterator_t *ifaces, *addrs;
1311 iface_entry_t *iface;
1312 addr_entry_t *addr;
1313 int ifindex = 0;
1314
1315 DBG2(DBG_KNL, "getting iface for %H", ip);
1316
1317 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1318 while (ifaces->iterate(ifaces, (void**)&iface))
1319 {
1320 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1321 while (addrs->iterate(addrs, (void**)&addr))
1322 {
1323 if (ip->ip_equals(ip, addr->ip))
1324 {
1325 ifindex = iface->ifindex;
1326 break;
1327 }
1328 }
1329 addrs->destroy(addrs);
1330 if (ifindex)
1331 {
1332 break;
1333 }
1334 }
1335 ifaces->destroy(ifaces);
1336
1337 if (ifindex == 0)
1338 {
1339 DBG1(DBG_KNL, "unable to get interface for %H", ip);
1340 }
1341 return ifindex;
1342 }
1343
1344 /**
1345 * get the refcount of a virtual ip
1346 */
1347 static int get_vip_refcount(private_kernel_interface_t *this, host_t* ip)
1348 {
1349 iterator_t *ifaces, *addrs;
1350 iface_entry_t *iface;
1351 addr_entry_t *addr;
1352 int refcount = 0;
1353
1354 ifaces = this->ifaces->create_iterator(this->ifaces, TRUE);
1355 while (ifaces->iterate(ifaces, (void**)&iface))
1356 {
1357 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1358 while (addrs->iterate(addrs, (void**)&addr))
1359 {
1360 if (addr->virtual && (iface->flags & IFF_UP) &&
1361 ip->ip_equals(ip, addr->ip))
1362 {
1363 refcount = addr->refcount;
1364 break;
1365 }
1366 }
1367 addrs->destroy(addrs);
1368 if (refcount)
1369 {
1370 break;
1371 }
1372 }
1373 ifaces->destroy(ifaces);
1374
1375 return refcount;
1376 }
1377
1378 /**
1379 * Manages the creation and deletion of ip addresses on an interface.
1380 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1381 */
1382 static status_t manage_ipaddr(private_kernel_interface_t *this, int nlmsg_type,
1383 int flags, int if_index, host_t *ip)
1384 {
1385 unsigned char request[BUFFER_SIZE];
1386 struct nlmsghdr *hdr;
1387 struct ifaddrmsg *msg;
1388 chunk_t chunk;
1389
1390 memset(&request, 0, sizeof(request));
1391
1392 chunk = ip->get_address(ip);
1393
1394 hdr = (struct nlmsghdr*)request;
1395 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1396 hdr->nlmsg_type = nlmsg_type;
1397 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1398
1399 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1400 msg->ifa_family = ip->get_family(ip);
1401 msg->ifa_flags = 0;
1402 msg->ifa_prefixlen = 8 * chunk.len;
1403 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1404 msg->ifa_index = if_index;
1405
1406 add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1407
1408 return netlink_send_ack(this, this->socket_rt, hdr);
1409 }
1410
1411 /**
1412 * Manages source routes in the routing table.
1413 * By setting the appropriate nlmsg_type, the route added or r.
1414 */
1415 static status_t manage_srcroute(private_kernel_interface_t *this, int nlmsg_type,
1416 int flags, route_entry_t *route)
1417 {
1418 unsigned char request[BUFFER_SIZE];
1419 struct nlmsghdr *hdr;
1420 struct rtmsg *msg;
1421 chunk_t chunk;
1422
1423 #if IPSEC_ROUTING_TABLE == 0
1424 /* if route is 0.0.0.0/0, we can't install it, as it would
1425 * overwrite the default route. Instead, we add two routes:
1426 * 0.0.0.0/1 and 128.0.0.0/1 */
1427 if (route->prefixlen == 0)
1428 {
1429 route_entry_t half;
1430 status_t status;
1431
1432 half.dst_net = chunk_alloca(route->dst_net.len);
1433 memset(half.dst_net.ptr, 0, half.dst_net.len);
1434 half.src_ip = route->src_ip;
1435 half.gateway = route->gateway;
1436 half.if_index = route->if_index;
1437 half.prefixlen = 1;
1438
1439 status = manage_srcroute(this, nlmsg_type, flags, &half);
1440 half.dst_net.ptr[0] |= 0x80;
1441 status = manage_srcroute(this, nlmsg_type, flags, &half);
1442 return status;
1443 }
1444 #endif
1445
1446 memset(&request, 0, sizeof(request));
1447
1448 hdr = (struct nlmsghdr*)request;
1449 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1450 hdr->nlmsg_type = nlmsg_type;
1451 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1452
1453 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1454 msg->rtm_family = route->src_ip->get_family(route->src_ip);
1455 msg->rtm_dst_len = route->prefixlen;
1456 msg->rtm_table = IPSEC_ROUTING_TABLE;
1457 msg->rtm_protocol = RTPROT_STATIC;
1458 msg->rtm_type = RTN_UNICAST;
1459 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1460
1461 add_attribute(hdr, RTA_DST, route->dst_net, sizeof(request));
1462 chunk = route->src_ip->get_address(route->src_ip);
1463 add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1464 chunk = route->gateway->get_address(route->gateway);
1465 add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1466 chunk.ptr = (char*)&route->if_index;
1467 chunk.len = sizeof(route->if_index);
1468 add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1469
1470 return netlink_send_ack(this, this->socket_rt, hdr);
1471 }
1472
1473 /**
1474 * create or delete an rule to use our routing table
1475 */
1476 static status_t manage_rule(private_kernel_interface_t *this, int nlmsg_type,
1477 u_int32_t table, u_int32_t prio)
1478 {
1479 unsigned char request[BUFFER_SIZE];
1480 struct nlmsghdr *hdr;
1481 struct rtmsg *msg;
1482 chunk_t chunk;
1483
1484 memset(&request, 0, sizeof(request));
1485 hdr = (struct nlmsghdr*)request;
1486 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1487 hdr->nlmsg_type = nlmsg_type;
1488 if (nlmsg_type == RTM_NEWRULE)
1489 {
1490 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
1491 }
1492 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1493
1494 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1495 msg->rtm_table = table;
1496 msg->rtm_family = AF_INET;
1497 msg->rtm_protocol = RTPROT_BOOT;
1498 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1499 msg->rtm_type = RTN_UNICAST;
1500
1501 chunk = chunk_from_thing(prio);
1502 add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
1503
1504 return netlink_send_ack(this, this->socket_rt, hdr);
1505 }
1506
1507 /**
1508 * check if an address (chunk) addr is in subnet (net with net_len net bits)
1509 */
1510 static bool addr_in_subnet(chunk_t addr, chunk_t net, int net_len)
1511 {
1512 int bit, byte;
1513
1514 if (addr.len != net.len)
1515 {
1516 return FALSE;
1517 }
1518 /* scan through all bits, beginning in the front */
1519 for (byte = 0; byte < addr.len; byte++)
1520 {
1521 for (bit = 7; bit >= 0; bit--)
1522 {
1523 /* check if bits are equal (or we reached the end of the net) */
1524 if (bit + byte * 8 > net_len)
1525 {
1526 return TRUE;
1527 }
1528 if (((1<<bit) & addr.ptr[byte]) != ((1<<bit) & net.ptr[byte]))
1529 {
1530 return FALSE;
1531 }
1532 }
1533 }
1534 return TRUE;
1535 }
1536
1537 /**
1538 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1539 */
1540 static host_t *get_route(private_kernel_interface_t *this, host_t *dest,
1541 bool nexthop)
1542 {
1543 unsigned char request[BUFFER_SIZE];
1544 struct nlmsghdr *hdr, *out, *current;
1545 struct rtmsg *msg;
1546 chunk_t chunk;
1547 size_t len;
1548 int best = -1;
1549 host_t *src = NULL, *gtw = NULL;
1550
1551 DBG2(DBG_KNL, "getting address to reach %H", dest);
1552
1553 memset(&request, 0, sizeof(request));
1554
1555 hdr = (struct nlmsghdr*)request;
1556 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP | NLM_F_ROOT;
1557 hdr->nlmsg_type = RTM_GETROUTE;
1558 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1559
1560 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1561 msg->rtm_family = dest->get_family(dest);
1562
1563 chunk = dest->get_address(dest);
1564 add_attribute(hdr, RTA_DST, chunk, sizeof(request));
1565
1566 if (netlink_send(this, this->socket_rt, hdr, &out, &len) != SUCCESS)
1567 {
1568 DBG1(DBG_KNL, "getting address to %H failed", dest);
1569 return NULL;
1570 }
1571 current = out;
1572 while (NLMSG_OK(current, len))
1573 {
1574 switch (current->nlmsg_type)
1575 {
1576 case NLMSG_DONE:
1577 break;
1578 case RTM_NEWROUTE:
1579 {
1580 struct rtattr *rta;
1581 size_t rtasize;
1582 chunk_t rta_gtw, rta_src, rta_dst;
1583 u_int32_t rta_oif = 0;
1584
1585 rta_gtw = rta_src = rta_dst = chunk_empty;
1586 msg = (struct rtmsg*)(NLMSG_DATA(current));
1587 rta = RTM_RTA(msg);
1588 rtasize = RTM_PAYLOAD(current);
1589 while (RTA_OK(rta, rtasize))
1590 {
1591 switch (rta->rta_type)
1592 {
1593 case RTA_PREFSRC:
1594 rta_src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1595 break;
1596 case RTA_GATEWAY:
1597 rta_gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1598 break;
1599 case RTA_DST:
1600 rta_dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1601 break;
1602 case RTA_OIF:
1603 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
1604 {
1605 rta_oif = *(u_int32_t*)RTA_DATA(rta);
1606 }
1607 break;
1608 }
1609 rta = RTA_NEXT(rta, rtasize);
1610 }
1611
1612 /* apply the route if:
1613 * - it is not from our own ipsec routing table
1614 * - is better than a previous one
1615 * - is the default route or
1616 * - its destination net contains our destination
1617 */
1618 if (msg->rtm_table != IPSEC_ROUTING_TABLE
1619 && msg->rtm_dst_len > best
1620 && (msg->rtm_dst_len == 0 || /* default route */
1621 (rta_dst.ptr && addr_in_subnet(chunk, rta_dst, msg->rtm_dst_len))))
1622 {
1623 iterator_t *ifaces, *addrs;
1624 iface_entry_t *iface;
1625 addr_entry_t *addr;
1626
1627 best = msg->rtm_dst_len;
1628 if (nexthop)
1629 {
1630 DESTROY_IF(gtw);
1631 gtw = host_create_from_chunk(msg->rtm_family, rta_gtw, 0);
1632 }
1633 else if (rta_src.ptr)
1634 {
1635 DESTROY_IF(src);
1636 src = host_create_from_chunk(msg->rtm_family, rta_src, 0);
1637 }
1638 else
1639 {
1640 /* no source addr, get one from the interfaces */
1641 ifaces = this->ifaces->create_iterator_locked(
1642 this->ifaces, &this->mutex);
1643 while (ifaces->iterate(ifaces, (void**)&iface))
1644 {
1645 if (iface->ifindex == rta_oif)
1646 {
1647 addrs = iface->addrs->create_iterator(
1648 iface->addrs, TRUE);
1649 while (addrs->iterate(addrs, (void**)&addr))
1650 {
1651 chunk_t ip = addr->ip->get_address(addr->ip);
1652 if (msg->rtm_dst_len == 0
1653 || addr_in_subnet(ip, rta_dst, msg->rtm_dst_len))
1654 {
1655 DESTROY_IF(src);
1656 src = addr->ip->clone(addr->ip);
1657 break;
1658 }
1659 }
1660 addrs->destroy(addrs);
1661 }
1662 }
1663 ifaces->destroy(ifaces);
1664 }
1665 }
1666 /* FALL through */
1667 }
1668 default:
1669 current = NLMSG_NEXT(current, len);
1670 continue;
1671 }
1672 break;
1673 }
1674 free(out);
1675
1676 if (nexthop)
1677 {
1678 if (gtw)
1679 {
1680 return gtw;
1681 }
1682 return dest->clone(dest);
1683 }
1684 return src;
1685 }
1686
1687 /**
1688 * Implementation of kernel_interface_t.get_source_addr.
1689 */
1690 static host_t* get_source_addr(private_kernel_interface_t *this, host_t *dest)
1691 {
1692 return get_route(this, dest, FALSE);
1693 }
1694
1695 /**
1696 * Implementation of kernel_interface_t.add_ip.
1697 */
1698 static status_t add_ip(private_kernel_interface_t *this,
1699 host_t *virtual_ip, host_t *iface_ip)
1700 {
1701 iface_entry_t *iface;
1702 addr_entry_t *addr;
1703 iterator_t *addrs, *ifaces;
1704 int ifindex;
1705
1706 DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
1707
1708 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1709 while (ifaces->iterate(ifaces, (void**)&iface))
1710 {
1711 bool iface_found = FALSE;
1712
1713 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1714 while (addrs->iterate(addrs, (void**)&addr))
1715 {
1716 if (iface_ip->ip_equals(iface_ip, addr->ip))
1717 {
1718 iface_found = TRUE;
1719 }
1720 else if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1721 {
1722 addr->refcount++;
1723 DBG2(DBG_KNL, "virtual IP %H already installed on %s",
1724 virtual_ip, iface->ifname);
1725 addrs->destroy(addrs);
1726 ifaces->destroy(ifaces);
1727 return SUCCESS;
1728 }
1729 }
1730 addrs->destroy(addrs);
1731
1732 if (iface_found)
1733 {
1734 ifindex = iface->ifindex;
1735 addr = malloc_thing(addr_entry_t);
1736 addr->ip = virtual_ip->clone(virtual_ip);
1737 addr->refcount = 0;
1738 addr->virtual = TRUE;
1739 addr->scope = RT_SCOPE_UNIVERSE;
1740 iface->addrs->insert_last(iface->addrs, addr);
1741
1742 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1743 ifindex, virtual_ip) == SUCCESS)
1744 {
1745 while (get_vip_refcount(this, virtual_ip) == 0)
1746 { /* wait until address appears */
1747 pthread_cond_wait(&this->cond, &this->mutex);
1748 }
1749 ifaces->destroy(ifaces);
1750 return SUCCESS;
1751 }
1752 ifaces->destroy(ifaces);
1753 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1754 return FAILED;
1755 }
1756 }
1757 ifaces->destroy(ifaces);
1758
1759 DBG1(DBG_KNL, "interface address %H not found, unable to install"
1760 "virtual IP %H", iface_ip, virtual_ip);
1761 return FAILED;
1762 }
1763
1764 /**
1765 * Implementation of kernel_interface_t.del_ip.
1766 */
1767 static status_t del_ip(private_kernel_interface_t *this, host_t *virtual_ip)
1768 {
1769 iface_entry_t *iface;
1770 addr_entry_t *addr;
1771 iterator_t *addrs, *ifaces;
1772 status_t status;
1773 int ifindex;
1774
1775 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1776
1777 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1778 while (ifaces->iterate(ifaces, (void**)&iface))
1779 {
1780 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1781 while (addrs->iterate(addrs, (void**)&addr))
1782 {
1783 if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1784 {
1785 ifindex = iface->ifindex;
1786 if (addr->refcount == 1)
1787 {
1788 status = manage_ipaddr(this, RTM_DELADDR, 0,
1789 ifindex, virtual_ip);
1790 if (status == SUCCESS)
1791 { /* wait until the address is really gone */
1792 while (get_vip_refcount(this, virtual_ip) > 0)
1793 {
1794 pthread_cond_wait(&this->cond, &this->mutex);
1795 }
1796 }
1797 addrs->destroy(addrs);
1798 ifaces->destroy(ifaces);
1799 return status;
1800 }
1801 else
1802 {
1803 addr->refcount--;
1804 }
1805 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1806 virtual_ip);
1807 addrs->destroy(addrs);
1808 ifaces->destroy(ifaces);
1809 return SUCCESS;
1810 }
1811 }
1812 addrs->destroy(addrs);
1813 }
1814 ifaces->destroy(ifaces);
1815
1816 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1817 return FAILED;
1818 }
1819
1820 /**
1821 * Implementation of kernel_interface_t.get_spi.
1822 */
1823 static status_t get_spi(private_kernel_interface_t *this,
1824 host_t *src, host_t *dst,
1825 protocol_id_t protocol, u_int32_t reqid,
1826 u_int32_t *spi)
1827 {
1828 unsigned char request[BUFFER_SIZE];
1829 struct nlmsghdr *hdr, *out;
1830 struct xfrm_userspi_info *userspi;
1831 u_int32_t received_spi = 0;
1832 size_t len;
1833
1834 memset(&request, 0, sizeof(request));
1835
1836 DBG2(DBG_KNL, "getting SPI for reqid %d", reqid);
1837
1838 hdr = (struct nlmsghdr*)request;
1839 hdr->nlmsg_flags = NLM_F_REQUEST;
1840 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
1841 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
1842
1843 userspi = (struct xfrm_userspi_info*)NLMSG_DATA(hdr);
1844 host2xfrm(src, &userspi->info.saddr);
1845 host2xfrm(dst, &userspi->info.id.daddr);
1846 userspi->info.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1847 userspi->info.mode = TRUE; /* tunnel mode */
1848 userspi->info.reqid = reqid;
1849 userspi->info.family = src->get_family(src);
1850 userspi->min = 0xc0000000;
1851 userspi->max = 0xcFFFFFFF;
1852
1853 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1854 {
1855 hdr = out;
1856 while (NLMSG_OK(hdr, len))
1857 {
1858 switch (hdr->nlmsg_type)
1859 {
1860 case XFRM_MSG_NEWSA:
1861 {
1862 struct xfrm_usersa_info* usersa = NLMSG_DATA(hdr);
1863 received_spi = usersa->id.spi;
1864 break;
1865 }
1866 case NLMSG_ERROR:
1867 {
1868 struct nlmsgerr *err = NLMSG_DATA(hdr);
1869
1870 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
1871 strerror(-err->error), -err->error);
1872 break;
1873 }
1874 default:
1875 hdr = NLMSG_NEXT(hdr, len);
1876 continue;
1877 case NLMSG_DONE:
1878 break;
1879 }
1880 break;
1881 }
1882 free(out);
1883 }
1884
1885 if (received_spi == 0)
1886 {
1887 DBG1(DBG_KNL, "unable to get SPI for reqid %d", reqid);
1888 return FAILED;
1889 }
1890
1891 DBG2(DBG_KNL, "got SPI 0x%x for reqid %d", received_spi, reqid);
1892
1893 *spi = received_spi;
1894 return SUCCESS;
1895 }
1896
1897 /**
1898 * Implementation of kernel_interface_t.add_sa.
1899 */
1900 static status_t add_sa(private_kernel_interface_t *this,
1901 host_t *src, host_t *dst, u_int32_t spi,
1902 protocol_id_t protocol, u_int32_t reqid,
1903 u_int64_t expire_soft, u_int64_t expire_hard,
1904 algorithm_t *enc_alg, algorithm_t *int_alg,
1905 prf_plus_t *prf_plus, mode_t mode, bool encap,
1906 bool replace)
1907 {
1908 unsigned char request[BUFFER_SIZE];
1909 char *alg_name;
1910 u_int key_size;
1911 struct nlmsghdr *hdr;
1912 struct xfrm_usersa_info *sa;
1913
1914 memset(&request, 0, sizeof(request));
1915
1916 DBG2(DBG_KNL, "adding SAD entry with SPI 0x%x", spi);
1917
1918 hdr = (struct nlmsghdr*)request;
1919 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1920 hdr->nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
1921 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1922
1923 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
1924 host2xfrm(src, &sa->saddr);
1925 host2xfrm(dst, &sa->id.daddr);
1926 sa->id.spi = spi;
1927 sa->id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1928 sa->family = src->get_family(src);
1929 sa->mode = mode;
1930 sa->replay_window = 32;
1931 sa->reqid = reqid;
1932 /* we currently do not expire SAs by volume/packet count */
1933 sa->lft.soft_byte_limit = XFRM_INF;
1934 sa->lft.hard_byte_limit = XFRM_INF;
1935 sa->lft.soft_packet_limit = XFRM_INF;
1936 sa->lft.hard_packet_limit = XFRM_INF;
1937 /* we use lifetimes since added, not since used */
1938 sa->lft.soft_add_expires_seconds = expire_soft;
1939 sa->lft.hard_add_expires_seconds = expire_hard;
1940 sa->lft.soft_use_expires_seconds = 0;
1941 sa->lft.hard_use_expires_seconds = 0;
1942
1943 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_usersa_info);
1944
1945 if (enc_alg->algorithm != ENCR_UNDEFINED)
1946 {
1947 rthdr->rta_type = XFRMA_ALG_CRYPT;
1948 alg_name = lookup_algorithm(encryption_algs, enc_alg, &key_size);
1949 if (alg_name == NULL)
1950 {
1951 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1952 encryption_algorithm_names, enc_alg->algorithm);
1953 return FAILED;
1954 }
1955 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1956 encryption_algorithm_names, enc_alg->algorithm, key_size);
1957
1958 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
1959 hdr->nlmsg_len += rthdr->rta_len;
1960 if (hdr->nlmsg_len > sizeof(request))
1961 {
1962 return FAILED;
1963 }
1964
1965 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1966 algo->alg_key_len = key_size;
1967 strcpy(algo->alg_name, alg_name);
1968 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
1969
1970 rthdr = XFRM_RTA_NEXT(rthdr);
1971 }
1972
1973 if (int_alg->algorithm != AUTH_UNDEFINED)
1974 {
1975 rthdr->rta_type = XFRMA_ALG_AUTH;
1976 alg_name = lookup_algorithm(integrity_algs, int_alg, &key_size);
1977 if (alg_name == NULL)
1978 {
1979 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1980 integrity_algorithm_names, int_alg->algorithm);
1981 return FAILED;
1982 }
1983 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1984 integrity_algorithm_names, int_alg->algorithm, key_size);
1985
1986 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
1987 hdr->nlmsg_len += rthdr->rta_len;
1988 if (hdr->nlmsg_len > sizeof(request))
1989 {
1990 return FAILED;
1991 }
1992
1993 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1994 algo->alg_key_len = key_size;
1995 strcpy(algo->alg_name, alg_name);
1996 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
1997
1998 rthdr = XFRM_RTA_NEXT(rthdr);
1999 }
2000
2001 /* TODO: add IPComp here */
2002
2003 if (encap)
2004 {
2005 rthdr->rta_type = XFRMA_ENCAP;
2006 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
2007
2008 hdr->nlmsg_len += rthdr->rta_len;
2009 if (hdr->nlmsg_len > sizeof(request))
2010 {
2011 return FAILED;
2012 }
2013
2014 struct xfrm_encap_tmpl* tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
2015 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
2016 tmpl->encap_sport = htons(src->get_port(src));
2017 tmpl->encap_dport = htons(dst->get_port(dst));
2018 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
2019 /* encap_oa could probably be derived from the
2020 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
2021 * pluto does the same as we do here but it uses encap_oa in the
2022 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
2023 * the kernel ignores it anyway
2024 * -> does that mean that NAT-T encap doesn't work in transport mode?
2025 * No. The reason the kernel ignores NAT-OA is that it recomputes
2026 * (or, rather, just ignores) the checksum. If packets pass
2027 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
2028 rthdr = XFRM_RTA_NEXT(rthdr);
2029 }
2030
2031 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2032 {
2033 DBG1(DBG_KNL, "unable to add SAD entry with SPI 0x%x", spi);
2034 return FAILED;
2035 }
2036 return SUCCESS;
2037 }
2038
2039 /**
2040 * Implementation of kernel_interface_t.update_sa.
2041 */
2042 static status_t update_sa(private_kernel_interface_t *this,
2043 u_int32_t spi, protocol_id_t protocol,
2044 host_t *src, host_t *dst,
2045 host_t *new_src, host_t *new_dst, bool encap)
2046 {
2047 unsigned char request[BUFFER_SIZE], *pos;
2048 struct nlmsghdr *hdr, *out = NULL;
2049 struct xfrm_usersa_id *sa_id;
2050 struct xfrm_usersa_info *out_sa = NULL, *sa;
2051 size_t len;
2052 struct rtattr *rta;
2053 size_t rtasize;
2054 struct xfrm_encap_tmpl* tmpl = NULL;
2055
2056 memset(&request, 0, sizeof(request));
2057
2058 DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x for update", spi);
2059
2060 /* query the exisiting SA first */
2061 hdr = (struct nlmsghdr*)request;
2062 hdr->nlmsg_flags = NLM_F_REQUEST;
2063 hdr->nlmsg_type = XFRM_MSG_GETSA;
2064 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
2065
2066 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
2067 host2xfrm(dst, &sa_id->daddr);
2068 sa_id->spi = spi;
2069 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
2070 sa_id->family = dst->get_family(dst);
2071
2072 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
2073 {
2074 hdr = out;
2075 while (NLMSG_OK(hdr, len))
2076 {
2077 switch (hdr->nlmsg_type)
2078 {
2079 case XFRM_MSG_NEWSA:
2080 {
2081 out_sa = NLMSG_DATA(hdr);
2082 break;
2083 }
2084 case NLMSG_ERROR:
2085 {
2086 struct nlmsgerr *err = NLMSG_DATA(hdr);
2087 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
2088 strerror(-err->error), -err->error);
2089 break;
2090 }
2091 default:
2092 hdr = NLMSG_NEXT(hdr, len);
2093 continue;
2094 case NLMSG_DONE:
2095 break;
2096 }
2097 break;
2098 }
2099 }
2100 if (out_sa == NULL ||
2101 this->public.del_sa(&this->public, dst, spi, protocol) != SUCCESS)
2102 {
2103 DBG1(DBG_KNL, "unable to update SAD entry with SPI 0x%x", spi);
2104 free(out);
2105 return FAILED;
2106 }
2107
2108 DBG2(DBG_KNL, "updating SAD entry with SPI 0x%x from %#H..%#H to %#H..%#H",
2109 spi, src, dst, new_src, new_dst);
2110
2111 /* copy over the SA from out to request */
2112 hdr = (struct nlmsghdr*)request;
2113 memcpy(hdr, out, min(out->nlmsg_len, sizeof(request)));
2114 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2115 hdr->nlmsg_type = XFRM_MSG_NEWSA;
2116 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
2117 sa = NLMSG_DATA(hdr);
2118 sa->family = new_dst->get_family(new_dst);
2119
2120 if (!src->ip_equals(src, new_src))
2121 {
2122 host2xfrm(new_src, &sa->saddr);
2123 }
2124 if (!dst->ip_equals(dst, new_dst))
2125 {
2126 host2xfrm(new_dst, &sa->id.daddr);
2127 }
2128
2129 rta = XFRM_RTA(out, struct xfrm_usersa_info);
2130 rtasize = XFRM_PAYLOAD(out, struct xfrm_usersa_info);
2131 pos = (u_char*)XFRM_RTA(hdr, struct xfrm_usersa_info);
2132 while(RTA_OK(rta, rtasize))
2133 {
2134 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2135 if (rta->rta_type != XFRMA_ENCAP || encap)
2136 {
2137 if (rta->rta_type == XFRMA_ENCAP)
2138 { /* update encap tmpl */
2139 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
2140 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
2141 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
2142 }
2143 memcpy(pos, rta, rta->rta_len);
2144 pos += rta->rta_len;
2145 hdr->nlmsg_len += rta->rta_len;
2146 }
2147 rta = RTA_NEXT(rta, rtasize);
2148 }
2149 if (tmpl == NULL && encap)
2150 { /* add tmpl if we are enabling it */
2151 rta = (struct rtattr*)pos;
2152 rta->rta_type = XFRMA_ENCAP;
2153 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
2154 hdr->nlmsg_len += rta->rta_len;
2155 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
2156 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
2157 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
2158 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
2159 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
2160 }
2161
2162 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2163 {
2164 DBG1(DBG_KNL, "unable to update SAD entry with SPI 0x%x", spi);
2165 free(out);
2166 return FAILED;
2167 }
2168 free(out);
2169
2170 return SUCCESS;
2171 }
2172
2173 /**
2174 * Implementation of kernel_interface_t.query_sa.
2175 */
2176 static status_t query_sa(private_kernel_interface_t *this, host_t *dst,
2177 u_int32_t spi, protocol_id_t protocol,
2178 u_int32_t *use_time)
2179 {
2180 unsigned char request[BUFFER_SIZE];
2181 struct nlmsghdr *out = NULL, *hdr;
2182 struct xfrm_usersa_id *sa_id;
2183 struct xfrm_usersa_info *sa = NULL;
2184 size_t len;
2185
2186 DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x", spi);
2187 memset(&request, 0, sizeof(request));
2188
2189 hdr = (struct nlmsghdr*)request;
2190 hdr->nlmsg_flags = NLM_F_REQUEST;
2191 hdr->nlmsg_type = XFRM_MSG_GETSA;
2192 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
2193
2194 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
2195 host2xfrm(dst, &sa_id->daddr);
2196 sa_id->spi = spi;
2197 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
2198 sa_id->family = dst->get_family(dst);
2199
2200 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
2201 {
2202 hdr = out;
2203 while (NLMSG_OK(hdr, len))
2204 {
2205 switch (hdr->nlmsg_type)
2206 {
2207 case XFRM_MSG_NEWSA:
2208 {
2209 sa = NLMSG_DATA(hdr);
2210 break;
2211 }
2212 case NLMSG_ERROR:
2213 {
2214 struct nlmsgerr *err = NLMSG_DATA(hdr);
2215 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
2216 strerror(-err->error), -err->error);
2217 break;
2218 }
2219 default:
2220 hdr = NLMSG_NEXT(hdr, len);
2221 continue;
2222 case NLMSG_DONE:
2223 break;
2224 }
2225 break;
2226 }
2227 }
2228
2229 if (sa == NULL)
2230 {
2231 DBG1(DBG_KNL, "unable to query SAD entry with SPI 0x%x", spi);
2232 free(out);
2233 return FAILED;
2234 }
2235
2236 *use_time = sa->curlft.use_time;
2237 free (out);
2238 return SUCCESS;
2239 }
2240
2241 /**
2242 * Implementation of kernel_interface_t.del_sa.
2243 */
2244 static status_t del_sa(private_kernel_interface_t *this, host_t *dst,
2245 u_int32_t spi, protocol_id_t protocol)
2246 {
2247 unsigned char request[BUFFER_SIZE];
2248 struct nlmsghdr *hdr;
2249 struct xfrm_usersa_id *sa_id;
2250
2251 memset(&request, 0, sizeof(request));
2252
2253 DBG2(DBG_KNL, "deleting SAD entry with SPI 0x%x", spi);
2254
2255 hdr = (struct nlmsghdr*)request;
2256 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2257 hdr->nlmsg_type = XFRM_MSG_DELSA;
2258 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
2259
2260 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
2261 host2xfrm(dst, &sa_id->daddr);
2262 sa_id->spi = spi;
2263 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
2264 sa_id->family = dst->get_family(dst);
2265
2266 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2267 {
2268 DBG1(DBG_KNL, "unable to delete SAD entry with SPI 0x%x", spi);
2269 return FAILED;
2270 }
2271 DBG2(DBG_KNL, "deleted SAD entry with SPI 0x%x", spi);
2272 return SUCCESS;
2273 }
2274
2275 /**
2276 * Implementation of kernel_interface_t.add_policy.
2277 */
2278 static status_t add_policy(private_kernel_interface_t *this,
2279 host_t *src, host_t *dst,
2280 traffic_selector_t *src_ts,
2281 traffic_selector_t *dst_ts,
2282 policy_dir_t direction, protocol_id_t protocol,
2283 u_int32_t reqid, bool high_prio, mode_t mode)
2284 {
2285 iterator_t *iterator;
2286 policy_entry_t *current, *policy;
2287 bool found = FALSE;
2288 unsigned char request[BUFFER_SIZE];
2289 struct xfrm_userpolicy_info *policy_info;
2290 struct nlmsghdr *hdr;
2291
2292 /* create a policy */
2293 policy = malloc_thing(policy_entry_t);
2294 memset(policy, 0, sizeof(policy_entry_t));
2295 policy->sel = ts2selector(src_ts, dst_ts);
2296 policy->direction = direction;
2297
2298 /* find the policy, which matches EXACTLY */
2299 pthread_mutex_lock(&this->mutex);
2300 iterator = this->policies->create_iterator(this->policies, TRUE);
2301 while (iterator->iterate(iterator, (void**)&current))
2302 {
2303 if (memcmp(&current->sel, &policy->sel, sizeof(struct xfrm_selector)) == 0 &&
2304 policy->direction == current->direction)
2305 {
2306 /* use existing policy */
2307 current->refcount++;
2308 DBG2(DBG_KNL, "policy %R===%R already exists, increasing ",
2309 "refcount", src_ts, dst_ts);
2310 free(policy);
2311 policy = current;
2312 found = TRUE;
2313 break;
2314 }
2315 }
2316 iterator->destroy(iterator);
2317 if (!found)
2318 { /* apply the new one, if we have no such policy */
2319 this->policies->insert_last(this->policies, policy);
2320 policy->refcount = 1;
2321 }
2322
2323 DBG2(DBG_KNL, "adding policy %R===%R", src_ts, dst_ts);
2324
2325 memset(&request, 0, sizeof(request));
2326 hdr = (struct nlmsghdr*)request;
2327 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2328 hdr->nlmsg_type = XFRM_MSG_UPDPOLICY;
2329 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
2330
2331 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
2332 policy_info->sel = policy->sel;
2333 policy_info->dir = policy->direction;
2334 /* calculate priority based on source selector size, small size = high prio */
2335 policy_info->priority = high_prio ? PRIO_HIGH : PRIO_LOW;
2336 policy_info->priority -= policy->sel.prefixlen_s * 10;
2337 policy_info->priority -= policy->sel.proto ? 2 : 0;
2338 policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
2339 policy_info->action = XFRM_POLICY_ALLOW;
2340 policy_info->share = XFRM_SHARE_ANY;
2341 pthread_mutex_unlock(&this->mutex);
2342
2343 /* policies don't expire */
2344 policy_info->lft.soft_byte_limit = XFRM_INF;
2345 policy_info->lft.soft_packet_limit = XFRM_INF;
2346 policy_info->lft.hard_byte_limit = XFRM_INF;
2347 policy_info->lft.hard_packet_limit = XFRM_INF;
2348 policy_info->lft.soft_add_expires_seconds = 0;
2349 policy_info->lft.hard_add_expires_seconds = 0;
2350 policy_info->lft.soft_use_expires_seconds = 0;
2351 policy_info->lft.hard_use_expires_seconds = 0;
2352
2353 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
2354 rthdr->rta_type = XFRMA_TMPL;
2355
2356 rthdr->rta_len = sizeof(struct xfrm_user_tmpl);
2357 rthdr->rta_len = RTA_LENGTH(rthdr->rta_len);
2358
2359 hdr->nlmsg_len += rthdr->rta_len;
2360 if (hdr->nlmsg_len > sizeof(request))
2361 {
2362 return FAILED;
2363 }
2364
2365 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
2366 tmpl->reqid = reqid;
2367 tmpl->id.proto = (protocol == PROTO_AH) ? KERNEL_AH : KERNEL_ESP;
2368 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
2369 tmpl->mode = mode;
2370 tmpl->family = src->get_family(src);
2371
2372 host2xfrm(src, &tmpl->saddr);
2373 host2xfrm(dst, &tmpl->id.daddr);
2374
2375 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2376 {
2377 DBG1(DBG_KNL, "unable to add policy %R===%R", src_ts, dst_ts);
2378 return FAILED;
2379 }
2380
2381 /* install a route, if:
2382 * - we are NOT updating a policy
2383 * - this is a forward policy (to just get one for each child)
2384 * - we are in tunnel mode
2385 * - we are not using IPv6 (does not work correctly yet!)
2386 */
2387 if (policy->route == NULL && direction == POLICY_FWD &&
2388 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6)
2389 {
2390 policy->route = malloc_thing(route_entry_t);
2391 if (get_address_by_ts(this, dst_ts, &policy->route->src_ip) == SUCCESS)
2392 {
2393 /* get the nexthop to src (src as we are in POLICY_FWD).*/
2394 policy->route->gateway = get_route(this, src, TRUE);
2395 policy->route->if_index = get_interface_index(this, dst);
2396 policy->route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
2397 memcpy(policy->route->dst_net.ptr, &policy->sel.saddr, policy->route->dst_net.len);
2398 policy->route->prefixlen = policy->sel.prefixlen_s;
2399
2400 if (manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
2401 policy->route) != SUCCESS)
2402 {
2403 DBG1(DBG_KNL, "unable to install source route for %H",
2404 policy->route->src_ip);
2405 route_entry_destroy(policy->route);
2406 policy->route = NULL;
2407 }
2408 }
2409 else
2410 {
2411 free(policy->route);
2412 policy->route = NULL;
2413 }
2414 }
2415
2416 return SUCCESS;
2417 }
2418
2419 /**
2420 * Implementation of kernel_interface_t.query_policy.
2421 */
2422 static status_t query_policy(private_kernel_interface_t *this,
2423 traffic_selector_t *src_ts,
2424 traffic_selector_t *dst_ts,
2425 policy_dir_t direction, u_int32_t *use_time)
2426 {
2427 unsigned char request[BUFFER_SIZE];
2428 struct nlmsghdr *out = NULL, *hdr;
2429 struct xfrm_userpolicy_id *policy_id;
2430 struct xfrm_userpolicy_info *policy = NULL;
2431 size_t len;
2432
2433 memset(&request, 0, sizeof(request));
2434
2435 DBG2(DBG_KNL, "querying policy %R===%R", src_ts, dst_ts);
2436
2437 hdr = (struct nlmsghdr*)request;
2438 hdr->nlmsg_flags = NLM_F_REQUEST;
2439 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
2440 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2441
2442 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
2443 policy_id->sel = ts2selector(src_ts, dst_ts);
2444 policy_id->dir = direction;
2445
2446 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
2447 {
2448 hdr = out;
2449 while (NLMSG_OK(hdr, len))
2450 {
2451 switch (hdr->nlmsg_type)
2452 {
2453 case XFRM_MSG_NEWPOLICY:
2454 {
2455 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
2456 break;
2457 }
2458 case NLMSG_ERROR:
2459 {
2460 struct nlmsgerr *err = NLMSG_DATA(hdr);
2461 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
2462 strerror(-err->error), -err->error);
2463 break;
2464 }
2465 default:
2466 hdr = NLMSG_NEXT(hdr, len);
2467 continue;
2468 case NLMSG_DONE:
2469 break;
2470 }
2471 break;
2472 }
2473 }
2474
2475 if (policy == NULL)
2476 {
2477 DBG2(DBG_KNL, "unable to query policy %R===%R", src_ts, dst_ts);
2478 free(out);
2479 return FAILED;
2480 }
2481 *use_time = (time_t)policy->curlft.use_time;
2482
2483 free(out);
2484 return SUCCESS;
2485 }
2486
2487 /**
2488 * Implementation of kernel_interface_t.del_policy.
2489 */
2490 static status_t del_policy(private_kernel_interface_t *this,
2491 traffic_selector_t *src_ts,
2492 traffic_selector_t *dst_ts,
2493 policy_dir_t direction)
2494 {
2495 policy_entry_t *current, policy, *to_delete = NULL;
2496 route_entry_t *route;
2497 unsigned char request[BUFFER_SIZE];
2498 struct nlmsghdr *hdr;
2499 struct xfrm_userpolicy_id *policy_id;
2500 iterator_t *iterator;
2501
2502 DBG2(DBG_KNL, "deleting policy %R===%R", src_ts, dst_ts);
2503
2504 /* create a policy */
2505 memset(&policy, 0, sizeof(policy_entry_t));
2506 policy.sel = ts2selector(src_ts, dst_ts);
2507 policy.direction = direction;
2508
2509 /* find the policy */
2510 iterator = this->policies->create_iterator_locked(this->policies, &this->mutex);
2511 while (iterator->iterate(iterator, (void**)&current))
2512 {
2513 if (memcmp(&current->sel, &policy.sel, sizeof(struct xfrm_selector)) == 0 &&
2514 policy.direction == current->direction)
2515 {
2516 to_delete = current;
2517 if (--to_delete->refcount > 0)
2518 {
2519 /* is used by more SAs, keep in kernel */
2520 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
2521 iterator->destroy(iterator);
2522 return SUCCESS;
2523 }
2524 /* remove if last reference */
2525 iterator->remove(iterator);
2526 break;
2527 }
2528 }
2529 iterator->destroy(iterator);
2530 if (!to_delete)
2531 {
2532 DBG1(DBG_KNL, "deleting policy %R===%R failed, not found", src_ts, dst_ts);
2533 return NOT_FOUND;
2534 }
2535
2536 memset(&request, 0, sizeof(request));
2537
2538 hdr = (struct nlmsghdr*)request;
2539 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2540 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
2541 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2542
2543 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
2544 policy_id->sel = to_delete->sel;
2545 policy_id->dir = direction;
2546
2547 route = to_delete->route;
2548 free(to_delete);
2549
2550 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2551 {
2552 DBG1(DBG_KNL, "unable to delete policy %R===%R", src_ts, dst_ts);
2553 return FAILED;
2554 }
2555
2556 if (route)
2557 {
2558 if (manage_srcroute(this, RTM_DELROUTE, 0, route) != SUCCESS)
2559 {
2560 DBG1(DBG_KNL, "error uninstalling route installed with "
2561 "policy %R===%R", src_ts, dst_ts);
2562 }
2563 route_entry_destroy(route);
2564 }
2565 return SUCCESS;
2566 }
2567
2568 /**
2569 * Implementation of kernel_interface_t.destroy.
2570 */
2571 static void destroy(private_kernel_interface_t *this)
2572 {
2573 manage_rule(this, RTM_DELRULE, IPSEC_ROUTING_TABLE, IPSEC_ROUTING_TABLE_PRIO);
2574
2575 this->job->cancel(this->job);
2576 close(this->socket_xfrm_events);
2577 close(this->socket_xfrm);
2578 close(this->socket_rt_events);
2579 close(this->socket_rt);
2580 this->policies->destroy(this->policies);
2581 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
2582 free(this);
2583 }
2584
2585 /*
2586 * Described in header.
2587 */
2588 kernel_interface_t *kernel_interface_create()
2589 {
2590 private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
2591 struct sockaddr_nl addr;
2592
2593 /* public functions */
2594 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
2595 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;
2596 this->public.update_sa = (status_t(*)(kernel_interface_t*,u_int32_t,protocol_id_t,host_t*,host_t*,host_t*,host_t*,bool))update_sa;
2597 this->public.query_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t*))query_sa;
2598 this->public.del_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
2599 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;
2600 this->public.query_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
2601 this->public.del_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t))del_policy;
2602 this->public.get_interface = (char*(*)(kernel_interface_t*,host_t*))get_interface_name;
2603 this->public.create_address_iterator = (iterator_t*(*)(kernel_interface_t*))create_address_iterator;
2604 this->public.get_source_addr = (host_t*(*)(kernel_interface_t*, host_t *dest))get_source_addr;
2605 this->public.add_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) add_ip;
2606 this->public.del_ip = (status_t(*)(kernel_interface_t*,host_t*)) del_ip;
2607 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
2608
2609 /* private members */
2610 this->policies = linked_list_create();
2611 this->ifaces = linked_list_create();
2612 this->hiter = NULL;
2613 this->seq = 200;
2614 pthread_mutex_init(&this->mutex, NULL);
2615 pthread_mutex_init(&this->nl_mutex, NULL);
2616 pthread_cond_init(&this->cond, NULL);
2617 timerclear(&this->last_roam);
2618
2619 memset(&addr, 0, sizeof(addr));
2620 addr.nl_family = AF_NETLINK;
2621
2622 /* create and bind RT socket */
2623 this->socket_rt = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2624 if (this->socket_rt <= 0)
2625 {
2626 charon->kill(charon, "unable to create RT netlink socket");
2627 }
2628 addr.nl_groups = 0;
2629 if (bind(this->socket_rt, (struct sockaddr*)&addr, sizeof(addr)))
2630 {
2631 charon->kill(charon, "unable to bind RT netlink socket");
2632 }
2633
2634 /* create and bind RT socket for events (address/interface/route changes) */
2635 this->socket_rt_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2636 if (this->socket_rt_events <= 0)
2637 {
2638 charon->kill(charon, "unable to create RT event socket");
2639 }
2640 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
2641 RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_LINK;
2642 if (bind(this->socket_rt_events, (struct sockaddr*)&addr, sizeof(addr)))
2643 {
2644 charon->kill(charon, "unable to bind RT event socket");
2645 }
2646
2647 /* create and bind XFRM socket */
2648 this->socket_xfrm = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
2649 if (this->socket_xfrm <= 0)
2650 {
2651 charon->kill(charon, "unable to create XFRM netlink socket");
2652 }
2653 addr.nl_groups = 0;
2654 if (bind(this->socket_xfrm, (struct sockaddr*)&addr, sizeof(addr)))
2655 {
2656 charon->kill(charon, "unable to bind XFRM netlink socket");
2657 }
2658
2659 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
2660 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
2661 if (this->socket_xfrm_events <= 0)
2662 {
2663 charon->kill(charon, "unable to create XFRM event socket");
2664 }
2665 addr.nl_groups = XFRMGRP_ACQUIRE | XFRMGRP_EXPIRE;
2666 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
2667 {
2668 charon->kill(charon, "unable to bind XFRM event socket");
2669 }
2670
2671 this->job = callback_job_create((callback_job_cb_t)receive_events,
2672 this, NULL, NULL);
2673 charon->processor->queue_job(charon->processor, (job_t*)this->job);
2674
2675 if (init_address_list(this) != SUCCESS)
2676 {
2677 charon->kill(charon, "unable to get interface list");
2678 }
2679
2680 if (manage_rule(this, RTM_NEWRULE, IPSEC_ROUTING_TABLE,
2681 IPSEC_ROUTING_TABLE_PRIO) != SUCCESS)
2682 {
2683 DBG1(DBG_KNL, "unable to create routing table rule");
2684 }
2685
2686 return &this->public;
2687 }
2688