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