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