0a94a8badddfcee6221e66b7491373c9e340e80e
[strongswan.git] / src / charon / plugins / kernel_netlink / kernel_netlink_net.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <sys/socket.h>
20 #include <linux/netlink.h>
21 #include <linux/rtnetlink.h>
22 #include <sys/time.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <net/if.h>
27
28 #include "kernel_netlink_net.h"
29 #include "kernel_netlink_shared.h"
30
31 #include <daemon.h>
32 #include <utils/linked_list.h>
33 #include <processing/jobs/callback_job.h>
34 #include <processing/jobs/roam_job.h>
35
36 /** delay before firing roam jobs (ms) */
37 #define ROAM_DELAY 100
38
39 /** routing table for routes installed by us */
40 #ifndef IPSEC_ROUTING_TABLE
41 #define IPSEC_ROUTING_TABLE 100
42 #endif
43 #ifndef IPSEC_ROUTING_TABLE_PRIO
44 #define IPSEC_ROUTING_TABLE_PRIO 100
45 #endif
46
47 typedef struct addr_entry_t addr_entry_t;
48
49 /**
50 * IP address in an inface_entry_t
51 */
52 struct addr_entry_t {
53
54 /** The ip address */
55 host_t *ip;
56
57 /** virtual IP managed by us */
58 bool virtual;
59
60 /** scope of the address */
61 u_char scope;
62
63 /** Number of times this IP is used, if virtual */
64 u_int refcount;
65 };
66
67 /**
68 * destroy a addr_entry_t object
69 */
70 static void addr_entry_destroy(addr_entry_t *this)
71 {
72 this->ip->destroy(this->ip);
73 free(this);
74 }
75
76 typedef struct iface_entry_t iface_entry_t;
77
78 /**
79 * A network interface on this system, containing addr_entry_t's
80 */
81 struct iface_entry_t {
82
83 /** interface index */
84 int ifindex;
85
86 /** name of the interface */
87 char ifname[IFNAMSIZ];
88
89 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
90 u_int flags;
91
92 /** list of addresses as host_t */
93 linked_list_t *addrs;
94 };
95
96 /**
97 * destroy an interface entry
98 */
99 static void iface_entry_destroy(iface_entry_t *this)
100 {
101 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
102 free(this);
103 }
104
105 typedef struct private_kernel_netlink_net_t private_kernel_netlink_net_t;
106
107 /**
108 * Private variables and functions of kernel_netlink_net class.
109 */
110 struct private_kernel_netlink_net_t {
111 /**
112 * Public part of the kernel_netlink_net_t object.
113 */
114 kernel_netlink_net_t public;
115
116 /**
117 * mutex to lock access to various lists
118 */
119 pthread_mutex_t mutex;
120
121 /**
122 * condition variable to signal virtual IP add/removal
123 */
124 pthread_cond_t cond;
125
126 /**
127 * Cached list of interfaces and its addresses (iface_entry_t)
128 */
129 linked_list_t *ifaces;
130
131 /**
132 * job receiving netlink events
133 */
134 callback_job_t *job;
135
136 /**
137 * netlink rt socket (routing)
138 */
139 netlink_socket_t *socket;
140
141 /**
142 * Netlink rt socket to receive address change events
143 */
144 int socket_events;
145
146 /**
147 * time of the last roam_job
148 */
149 struct timeval last_roam;
150
151 /**
152 * routing table to install routes
153 */
154 int routing_table;
155
156 /**
157 * priority of used routing table
158 */
159 int routing_table_prio;
160
161 /**
162 * whether to react to RTM_NEWROUTE or RTM_DELROUTE events
163 */
164 bool process_route;
165
166 };
167
168 /**
169 * get the refcount of a virtual ip
170 */
171 static int get_vip_refcount(private_kernel_netlink_net_t *this, host_t* ip)
172 {
173 iterator_t *ifaces, *addrs;
174 iface_entry_t *iface;
175 addr_entry_t *addr;
176 int refcount = 0;
177
178 ifaces = this->ifaces->create_iterator(this->ifaces, TRUE);
179 while (ifaces->iterate(ifaces, (void**)&iface))
180 {
181 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
182 while (addrs->iterate(addrs, (void**)&addr))
183 {
184 if (addr->virtual && (iface->flags & IFF_UP) &&
185 ip->ip_equals(ip, addr->ip))
186 {
187 refcount = addr->refcount;
188 break;
189 }
190 }
191 addrs->destroy(addrs);
192 if (refcount)
193 {
194 break;
195 }
196 }
197 ifaces->destroy(ifaces);
198
199 return refcount;
200 }
201
202 /**
203 * start a roaming job. We delay it for a second and fire only one job
204 * for multiple events. Otherwise we would create two many jobs.
205 */
206 static void fire_roam_job(private_kernel_netlink_net_t *this, bool address)
207 {
208 struct timeval now;
209
210 if (gettimeofday(&now, NULL) == 0)
211 {
212 if (timercmp(&now, &this->last_roam, >))
213 {
214 now.tv_usec += ROAM_DELAY * 1000;
215 while (now.tv_usec > 1000000)
216 {
217 now.tv_sec++;
218 now.tv_usec -= 1000000;
219 }
220 this->last_roam = now;
221 charon->scheduler->schedule_job(charon->scheduler,
222 (job_t*)roam_job_create(address), ROAM_DELAY);
223 }
224 }
225 }
226
227 /**
228 * process RTM_NEWLINK/RTM_DELLINK from kernel
229 */
230 static void process_link(private_kernel_netlink_net_t *this,
231 struct nlmsghdr *hdr, bool event)
232 {
233 struct ifinfomsg* msg = (struct ifinfomsg*)(NLMSG_DATA(hdr));
234 struct rtattr *rta = IFLA_RTA(msg);
235 size_t rtasize = IFLA_PAYLOAD (hdr);
236 iterator_t *iterator;
237 iface_entry_t *current, *entry = NULL;
238 char *name = NULL;
239 bool update = FALSE;
240
241 while(RTA_OK(rta, rtasize))
242 {
243 switch (rta->rta_type)
244 {
245 case IFLA_IFNAME:
246 name = RTA_DATA(rta);
247 break;
248 }
249 rta = RTA_NEXT(rta, rtasize);
250 }
251 if (!name)
252 {
253 name = "(unknown)";
254 }
255
256 switch (hdr->nlmsg_type)
257 {
258 case RTM_NEWLINK:
259 {
260 if (msg->ifi_flags & IFF_LOOPBACK)
261 { /* ignore loopback interfaces */
262 break;
263 }
264 iterator = this->ifaces->create_iterator_locked(this->ifaces,
265 &this->mutex);
266 while (iterator->iterate(iterator, (void**)&current))
267 {
268 if (current->ifindex == msg->ifi_index)
269 {
270 entry = current;
271 break;
272 }
273 }
274 if (!entry)
275 {
276 entry = malloc_thing(iface_entry_t);
277 entry->ifindex = msg->ifi_index;
278 entry->flags = 0;
279 entry->addrs = linked_list_create();
280 this->ifaces->insert_last(this->ifaces, entry);
281 }
282 memcpy(entry->ifname, name, IFNAMSIZ);
283 entry->ifname[IFNAMSIZ-1] = '\0';
284 if (event)
285 {
286 if (!(entry->flags & IFF_UP) && (msg->ifi_flags & IFF_UP))
287 {
288 update = TRUE;
289 DBG1(DBG_KNL, "interface %s activated", name);
290 }
291 if ((entry->flags & IFF_UP) && !(msg->ifi_flags & IFF_UP))
292 {
293 update = TRUE;
294 DBG1(DBG_KNL, "interface %s deactivated", name);
295 }
296 }
297 entry->flags = msg->ifi_flags;
298 iterator->destroy(iterator);
299 break;
300 }
301 case RTM_DELLINK:
302 {
303 iterator = this->ifaces->create_iterator_locked(this->ifaces,
304 &this->mutex);
305 while (iterator->iterate(iterator, (void**)&current))
306 {
307 if (current->ifindex == msg->ifi_index)
308 {
309 /* we do not remove it, as an address may be added to a
310 * "down" interface and we wan't to know that. */
311 current->flags = msg->ifi_flags;
312 break;
313 }
314 }
315 iterator->destroy(iterator);
316 break;
317 }
318 }
319
320 /* send an update to all IKE_SAs */
321 if (update && event)
322 {
323 fire_roam_job(this, TRUE);
324 }
325 }
326
327 /**
328 * process RTM_NEWADDR/RTM_DELADDR from kernel
329 */
330 static void process_addr(private_kernel_netlink_net_t *this,
331 struct nlmsghdr *hdr, bool event)
332 {
333 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
334 struct rtattr *rta = IFA_RTA(msg);
335 size_t rtasize = IFA_PAYLOAD (hdr);
336 host_t *host = NULL;
337 iterator_t *ifaces, *addrs;
338 iface_entry_t *iface;
339 addr_entry_t *addr;
340 chunk_t local = chunk_empty, address = chunk_empty;
341 bool update = FALSE, found = FALSE, changed = FALSE;
342
343 while(RTA_OK(rta, rtasize))
344 {
345 switch (rta->rta_type)
346 {
347 case IFA_LOCAL:
348 local.ptr = RTA_DATA(rta);
349 local.len = RTA_PAYLOAD(rta);
350 break;
351 case IFA_ADDRESS:
352 address.ptr = RTA_DATA(rta);
353 address.len = RTA_PAYLOAD(rta);
354 break;
355 }
356 rta = RTA_NEXT(rta, rtasize);
357 }
358
359 /* For PPP interfaces, we need the IFA_LOCAL address,
360 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
361 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
362 if (local.ptr)
363 {
364 host = host_create_from_chunk(msg->ifa_family, local, 0);
365 }
366 else if (address.ptr)
367 {
368 host = host_create_from_chunk(msg->ifa_family, address, 0);
369 }
370
371 if (host == NULL)
372 { /* bad family? */
373 return;
374 }
375
376 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
377 while (ifaces->iterate(ifaces, (void**)&iface))
378 {
379 if (iface->ifindex == msg->ifa_index)
380 {
381 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
382 while (addrs->iterate(addrs, (void**)&addr))
383 {
384 if (host->ip_equals(host, addr->ip))
385 {
386 found = TRUE;
387 if (hdr->nlmsg_type == RTM_DELADDR)
388 {
389 addrs->remove(addrs);
390 if (!addr->virtual)
391 {
392 changed = TRUE;
393 DBG1(DBG_KNL, "%H disappeared from %s",
394 host, iface->ifname);
395 }
396 addr_entry_destroy(addr);
397 }
398 else if (hdr->nlmsg_type == RTM_NEWADDR && addr->virtual)
399 {
400 addr->refcount = 1;
401 }
402 }
403 }
404 addrs->destroy(addrs);
405
406 if (hdr->nlmsg_type == RTM_NEWADDR)
407 {
408 if (!found)
409 {
410 found = TRUE;
411 changed = TRUE;
412 addr = malloc_thing(addr_entry_t);
413 addr->ip = host->clone(host);
414 addr->virtual = FALSE;
415 addr->refcount = 1;
416 addr->scope = msg->ifa_scope;
417
418 iface->addrs->insert_last(iface->addrs, addr);
419 if (event)
420 {
421 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
422 }
423 }
424 }
425 if (found && (iface->flags & IFF_UP))
426 {
427 update = TRUE;
428 }
429 break;
430 }
431 }
432 ifaces->destroy(ifaces);
433 host->destroy(host);
434
435 /* send an update to all IKE_SAs */
436 if (update && event && changed)
437 {
438 fire_roam_job(this, TRUE);
439 }
440 }
441
442 /**
443 * process RTM_NEWROUTE and RTM_DELROUTE from kernel
444 */
445 static void process_route(private_kernel_netlink_net_t *this, struct nlmsghdr *hdr)
446 {
447 struct rtmsg* msg = (struct rtmsg*)(NLMSG_DATA(hdr));
448 struct rtattr *rta = RTM_RTA(msg);
449 size_t rtasize = RTM_PAYLOAD(hdr);
450 host_t *host = NULL;
451
452 /* ignore routes added by us */
453 if (msg->rtm_table && msg->rtm_table == this->routing_table)
454 {
455 return;
456 }
457
458 while (RTA_OK(rta, rtasize))
459 {
460 switch (rta->rta_type)
461 {
462 case RTA_PREFSRC:
463 host = host_create_from_chunk(msg->rtm_family,
464 chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)), 0);
465 break;
466 }
467 rta = RTA_NEXT(rta, rtasize);
468 }
469 if (host)
470 {
471 if (!get_vip_refcount(this, host))
472 { /* ignore routes added for virtual IPs */
473 fire_roam_job(this, FALSE);
474 }
475 host->destroy(host);
476 }
477 }
478
479 /**
480 * Receives events from kernel
481 */
482 static job_requeue_t receive_events(private_kernel_netlink_net_t *this)
483 {
484 char response[1024];
485 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
486 struct sockaddr_nl addr;
487 socklen_t addr_len = sizeof(addr);
488 int len, oldstate;
489
490 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
491 len = recvfrom(this->socket_events, response, sizeof(response), 0,
492 (struct sockaddr*)&addr, &addr_len);
493 pthread_setcancelstate(oldstate, NULL);
494
495 if (len < 0)
496 {
497 switch (errno)
498 {
499 case EINTR:
500 /* interrupted, try again */
501 return JOB_REQUEUE_DIRECT;
502 case EAGAIN:
503 /* no data ready, select again */
504 return JOB_REQUEUE_DIRECT;
505 default:
506 DBG1(DBG_KNL, "unable to receive from rt event socket");
507 sleep(1);
508 return JOB_REQUEUE_FAIR;
509 }
510 }
511
512 if (addr.nl_pid != 0)
513 { /* not from kernel. not interested, try another one */
514 return JOB_REQUEUE_DIRECT;
515 }
516
517 while (NLMSG_OK(hdr, len))
518 {
519 /* looks good so far, dispatch netlink message */
520 switch (hdr->nlmsg_type)
521 {
522 case RTM_NEWADDR:
523 case RTM_DELADDR:
524 process_addr(this, hdr, TRUE);
525 pthread_cond_broadcast(&this->cond);
526 break;
527 case RTM_NEWLINK:
528 case RTM_DELLINK:
529 process_link(this, hdr, TRUE);
530 pthread_cond_broadcast(&this->cond);
531 break;
532 case RTM_NEWROUTE:
533 case RTM_DELROUTE:
534 if (this->process_route)
535 {
536 process_route(this, hdr);
537 }
538 break;
539 default:
540 break;
541 }
542 hdr = NLMSG_NEXT(hdr, len);
543 }
544 return JOB_REQUEUE_DIRECT;
545 }
546
547 /** enumerator over addresses */
548 typedef struct {
549 private_kernel_netlink_net_t* this;
550 /** whether to enumerate down interfaces */
551 bool include_down_ifaces;
552 /** whether to enumerate virtual ip addresses */
553 bool include_virtual_ips;
554 } address_enumerator_t;
555
556 /**
557 * cleanup function for address enumerator
558 */
559 static void address_enumerator_destroy(address_enumerator_t *data)
560 {
561 pthread_mutex_unlock(&data->this->mutex);
562 free(data);
563 }
564
565 /**
566 * filter for addresses
567 */
568 static bool filter_addresses(address_enumerator_t *data, addr_entry_t** in, host_t** out)
569 {
570 if (!data->include_virtual_ips && (*in)->virtual)
571 { /* skip virtual interfaces added by us */
572 return FALSE;
573 }
574 if ((*in)->scope >= RT_SCOPE_LINK)
575 { /* skip addresses with a unusable scope */
576 return FALSE;
577 }
578 *out = (*in)->ip;
579 return TRUE;
580 }
581
582 /**
583 * enumerator constructor for interfaces
584 */
585 static enumerator_t *create_iface_enumerator(iface_entry_t *iface, address_enumerator_t *data)
586 {
587 return enumerator_create_filter(iface->addrs->create_enumerator(iface->addrs),
588 (void*)filter_addresses, data, NULL);
589 }
590
591 /**
592 * filter for interfaces
593 */
594 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in, iface_entry_t** out)
595 {
596 if (!data->include_down_ifaces && !((*in)->flags & IFF_UP))
597 { /* skip interfaces not up */
598 return FALSE;
599 }
600 *out = *in;
601 return TRUE;
602 }
603
604 /**
605 * implementation of kernel_net_t.create_address_enumerator
606 */
607 static enumerator_t *create_address_enumerator(private_kernel_netlink_net_t *this,
608 bool include_down_ifaces, bool include_virtual_ips)
609 {
610 address_enumerator_t *data = malloc_thing(address_enumerator_t);
611 data->this = this;
612 data->include_down_ifaces = include_down_ifaces;
613 data->include_virtual_ips = include_virtual_ips;
614
615 pthread_mutex_lock(&this->mutex);
616 return enumerator_create_nested(
617 enumerator_create_filter(this->ifaces->create_enumerator(this->ifaces),
618 (void*)filter_interfaces, data, NULL),
619 (void*)create_iface_enumerator, data, (void*)address_enumerator_destroy);
620 }
621
622 /**
623 * implementation of kernel_net_t.get_interface_name
624 */
625 static char *get_interface_name(private_kernel_netlink_net_t *this, host_t* ip)
626 {
627 iterator_t *ifaces, *addrs;
628 iface_entry_t *iface;
629 addr_entry_t *addr;
630 char *name = NULL;
631
632 DBG2(DBG_KNL, "getting interface name for %H", ip);
633
634 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
635 while (ifaces->iterate(ifaces, (void**)&iface))
636 {
637 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
638 while (addrs->iterate(addrs, (void**)&addr))
639 {
640 if (ip->ip_equals(ip, addr->ip))
641 {
642 name = strdup(iface->ifname);
643 break;
644 }
645 }
646 addrs->destroy(addrs);
647 if (name)
648 {
649 break;
650 }
651 }
652 ifaces->destroy(ifaces);
653
654 if (name)
655 {
656 DBG2(DBG_KNL, "%H is on interface %s", ip, name);
657 }
658 else
659 {
660 DBG2(DBG_KNL, "%H is not a local address", ip);
661 }
662 return name;
663 }
664
665 /**
666 * get the index of an interface by name
667 */
668 static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
669 {
670 iterator_t *ifaces;
671 iface_entry_t *iface;
672 int ifindex = 0;
673
674 DBG2(DBG_KNL, "getting iface index for %s", name);
675
676 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
677 while (ifaces->iterate(ifaces, (void**)&iface))
678 {
679 if (streq(name, iface->ifname))
680 {
681 ifindex = iface->ifindex;
682 break;
683 }
684 }
685 ifaces->destroy(ifaces);
686
687 if (ifindex == 0)
688 {
689 DBG1(DBG_KNL, "unable to get interface index for %s", name);
690 }
691 return ifindex;
692 }
693
694 /**
695 * check if an address (chunk) addr is in subnet (net with net_len net bits)
696 */
697 static bool addr_in_subnet(chunk_t addr, chunk_t net, int net_len)
698 {
699 int bit, byte;
700
701 if (addr.len != net.len)
702 {
703 return FALSE;
704 }
705 /* scan through all bits, beginning at the front */
706 for (byte = 0; byte < addr.len; byte++)
707 {
708 for (bit = 0; bit < 8; bit++)
709 {
710 u_char bitpos = 1 << (7-bit);
711
712 /* check if bits are equal (or we reached the end of the net) */
713 if (bit + byte * 8 >= net_len)
714 {
715 return TRUE;
716 }
717 if ((bitpos & addr.ptr[byte]) != (bitpos & net.ptr[byte]))
718 {
719 return FALSE;
720 }
721 }
722 }
723 return TRUE;
724 }
725
726 /**
727 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
728 */
729 static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
730 bool nexthop, host_t *candidate)
731 {
732 unsigned char request[NETLINK_BUFFER_SIZE];
733 struct nlmsghdr *hdr, *out, *current;
734 struct rtmsg *msg;
735 chunk_t chunk;
736 size_t len;
737 int best = -1;
738 host_t *src = NULL, *gtw = NULL;
739
740 DBG2(DBG_KNL, "getting address to reach %H", dest);
741
742 memset(&request, 0, sizeof(request));
743
744 hdr = (struct nlmsghdr*)request;
745 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP | NLM_F_ROOT;
746 hdr->nlmsg_type = RTM_GETROUTE;
747 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
748
749 msg = (struct rtmsg*)NLMSG_DATA(hdr);
750 msg->rtm_family = dest->get_family(dest);
751
752 chunk = dest->get_address(dest);
753 netlink_add_attribute(hdr, RTA_DST, chunk, sizeof(request));
754 if (candidate)
755 {
756 chunk = candidate->get_address(candidate);
757 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
758 }
759
760 if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
761 {
762 DBG1(DBG_KNL, "getting address to %H failed", dest);
763 return NULL;
764 }
765 current = out;
766 while (NLMSG_OK(current, len))
767 {
768 switch (current->nlmsg_type)
769 {
770 case NLMSG_DONE:
771 break;
772 case RTM_NEWROUTE:
773 {
774 struct rtattr *rta;
775 size_t rtasize;
776 chunk_t rta_gtw, rta_src, rta_dst;
777 u_int32_t rta_oif = 0;
778
779 rta_gtw = rta_src = rta_dst = chunk_empty;
780 msg = (struct rtmsg*)(NLMSG_DATA(current));
781 rta = RTM_RTA(msg);
782 rtasize = RTM_PAYLOAD(current);
783 while (RTA_OK(rta, rtasize))
784 {
785 switch (rta->rta_type)
786 {
787 case RTA_PREFSRC:
788 rta_src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
789 break;
790 case RTA_GATEWAY:
791 rta_gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
792 break;
793 case RTA_DST:
794 rta_dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
795 break;
796 case RTA_OIF:
797 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
798 {
799 rta_oif = *(u_int32_t*)RTA_DATA(rta);
800 }
801 break;
802 }
803 rta = RTA_NEXT(rta, rtasize);
804 }
805
806 /* apply the route if:
807 * - it is not from our own ipsec routing table
808 * - is better than a previous one
809 * - is the default route or
810 * - its destination net contains our destination
811 */
812 if ((this->routing_table == 0 ||msg->rtm_table != this->routing_table)
813 && msg->rtm_dst_len > best
814 && (msg->rtm_dst_len == 0 || /* default route */
815 (rta_dst.ptr && addr_in_subnet(chunk, rta_dst, msg->rtm_dst_len))))
816 {
817 iterator_t *ifaces, *addrs;
818 iface_entry_t *iface;
819 addr_entry_t *addr;
820
821 best = msg->rtm_dst_len;
822 if (nexthop)
823 {
824 DESTROY_IF(gtw);
825 gtw = host_create_from_chunk(msg->rtm_family, rta_gtw, 0);
826 }
827 else if (rta_src.ptr)
828 {
829 DESTROY_IF(src);
830 src = host_create_from_chunk(msg->rtm_family, rta_src, 0);
831 if (get_vip_refcount(this, src))
832 { /* skip source address if it is installed by us */
833 DESTROY_IF(src);
834 src = NULL;
835 current = NLMSG_NEXT(current, len);
836 continue;
837 }
838 }
839 else
840 {
841 /* no source addr, get one from the interfaces */
842 ifaces = this->ifaces->create_iterator_locked(
843 this->ifaces, &this->mutex);
844 while (ifaces->iterate(ifaces, (void**)&iface))
845 {
846 if (iface->ifindex == rta_oif)
847 {
848 addrs = iface->addrs->create_iterator(
849 iface->addrs, TRUE);
850 while (addrs->iterate(addrs, (void**)&addr))
851 {
852 chunk_t ip = addr->ip->get_address(addr->ip);
853 if ((msg->rtm_dst_len == 0 &&
854 addr->ip->get_family(addr->ip) ==
855 dest->get_family(dest)) ||
856 addr_in_subnet(ip, rta_dst, msg->rtm_dst_len))
857 {
858 DESTROY_IF(src);
859 src = addr->ip->clone(addr->ip);
860 break;
861 }
862 }
863 addrs->destroy(addrs);
864 }
865 }
866 ifaces->destroy(ifaces);
867 }
868 }
869 /* FALL through */
870 }
871 default:
872 current = NLMSG_NEXT(current, len);
873 continue;
874 }
875 break;
876 }
877 free(out);
878
879 if (nexthop)
880 {
881 if (gtw)
882 {
883 return gtw;
884 }
885 return dest->clone(dest);
886 }
887 return src;
888 }
889
890 /**
891 * Implementation of kernel_net_t.get_source_addr.
892 */
893 static host_t* get_source_addr(private_kernel_netlink_net_t *this,
894 host_t *dest, host_t *src)
895 {
896 return get_route(this, dest, FALSE, src);
897 }
898
899 /**
900 * Implementation of kernel_net_t.get_nexthop.
901 */
902 static host_t* get_nexthop(private_kernel_netlink_net_t *this, host_t *dest)
903 {
904 return get_route(this, dest, TRUE, NULL);
905 }
906
907 /**
908 * Manages the creation and deletion of ip addresses on an interface.
909 * By setting the appropriate nlmsg_type, the ip will be set or unset.
910 */
911 static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
912 int flags, int if_index, host_t *ip)
913 {
914 unsigned char request[NETLINK_BUFFER_SIZE];
915 struct nlmsghdr *hdr;
916 struct ifaddrmsg *msg;
917 chunk_t chunk;
918
919 memset(&request, 0, sizeof(request));
920
921 chunk = ip->get_address(ip);
922
923 hdr = (struct nlmsghdr*)request;
924 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
925 hdr->nlmsg_type = nlmsg_type;
926 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
927
928 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
929 msg->ifa_family = ip->get_family(ip);
930 msg->ifa_flags = 0;
931 msg->ifa_prefixlen = 8 * chunk.len;
932 msg->ifa_scope = RT_SCOPE_UNIVERSE;
933 msg->ifa_index = if_index;
934
935 netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
936
937 return this->socket->send_ack(this->socket, hdr);
938 }
939
940 /**
941 * Implementation of kernel_net_t.add_ip.
942 */
943 static status_t add_ip(private_kernel_netlink_net_t *this,
944 host_t *virtual_ip, host_t *iface_ip)
945 {
946 iface_entry_t *iface;
947 addr_entry_t *addr;
948 iterator_t *addrs, *ifaces;
949 int ifindex;
950
951 DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
952
953 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
954 while (ifaces->iterate(ifaces, (void**)&iface))
955 {
956 bool iface_found = FALSE;
957
958 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
959 while (addrs->iterate(addrs, (void**)&addr))
960 {
961 if (iface_ip->ip_equals(iface_ip, addr->ip))
962 {
963 iface_found = TRUE;
964 }
965 else if (virtual_ip->ip_equals(virtual_ip, addr->ip))
966 {
967 addr->refcount++;
968 DBG2(DBG_KNL, "virtual IP %H already installed on %s",
969 virtual_ip, iface->ifname);
970 addrs->destroy(addrs);
971 ifaces->destroy(ifaces);
972 return SUCCESS;
973 }
974 }
975 addrs->destroy(addrs);
976
977 if (iface_found)
978 {
979 ifindex = iface->ifindex;
980 addr = malloc_thing(addr_entry_t);
981 addr->ip = virtual_ip->clone(virtual_ip);
982 addr->refcount = 0;
983 addr->virtual = TRUE;
984 addr->scope = RT_SCOPE_UNIVERSE;
985 iface->addrs->insert_last(iface->addrs, addr);
986
987 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
988 ifindex, virtual_ip) == SUCCESS)
989 {
990 while (get_vip_refcount(this, virtual_ip) == 0)
991 { /* wait until address appears */
992 pthread_cond_wait(&this->cond, &this->mutex);
993 }
994 ifaces->destroy(ifaces);
995 return SUCCESS;
996 }
997 ifaces->destroy(ifaces);
998 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
999 return FAILED;
1000 }
1001 }
1002 ifaces->destroy(ifaces);
1003
1004 DBG1(DBG_KNL, "interface address %H not found, unable to install"
1005 "virtual IP %H", iface_ip, virtual_ip);
1006 return FAILED;
1007 }
1008
1009 /**
1010 * Implementation of kernel_net_t.del_ip.
1011 */
1012 static status_t del_ip(private_kernel_netlink_net_t *this, host_t *virtual_ip)
1013 {
1014 iface_entry_t *iface;
1015 addr_entry_t *addr;
1016 iterator_t *addrs, *ifaces;
1017 status_t status;
1018 int ifindex;
1019
1020 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1021
1022 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1023 while (ifaces->iterate(ifaces, (void**)&iface))
1024 {
1025 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1026 while (addrs->iterate(addrs, (void**)&addr))
1027 {
1028 if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1029 {
1030 ifindex = iface->ifindex;
1031 if (addr->refcount == 1)
1032 {
1033 status = manage_ipaddr(this, RTM_DELADDR, 0,
1034 ifindex, virtual_ip);
1035 if (status == SUCCESS)
1036 { /* wait until the address is really gone */
1037 while (get_vip_refcount(this, virtual_ip) > 0)
1038 {
1039 pthread_cond_wait(&this->cond, &this->mutex);
1040 }
1041 }
1042 addrs->destroy(addrs);
1043 ifaces->destroy(ifaces);
1044 return status;
1045 }
1046 else
1047 {
1048 addr->refcount--;
1049 }
1050 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1051 virtual_ip);
1052 addrs->destroy(addrs);
1053 ifaces->destroy(ifaces);
1054 return SUCCESS;
1055 }
1056 }
1057 addrs->destroy(addrs);
1058 }
1059 ifaces->destroy(ifaces);
1060
1061 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1062 return FAILED;
1063 }
1064
1065 /**
1066 * Manages source routes in the routing table.
1067 * By setting the appropriate nlmsg_type, the route gets added or removed.
1068 */
1069 static status_t manage_srcroute(private_kernel_netlink_net_t *this, int nlmsg_type,
1070 int flags, chunk_t dst_net, u_int8_t prefixlen,
1071 host_t *gateway, host_t *src_ip, char *if_name)
1072 {
1073 unsigned char request[NETLINK_BUFFER_SIZE];
1074 struct nlmsghdr *hdr;
1075 struct rtmsg *msg;
1076 int ifindex;
1077 chunk_t chunk;
1078
1079 /* if route is 0.0.0.0/0, we can't install it, as it would
1080 * overwrite the default route. Instead, we add two routes:
1081 * 0.0.0.0/1 and 128.0.0.0/1 */
1082 if (this->routing_table == 0 && prefixlen == 0)
1083 {
1084 chunk_t half_net;
1085 u_int8_t half_prefixlen;
1086 status_t status;
1087
1088 half_net = chunk_alloca(dst_net.len);
1089 memset(half_net.ptr, 0, half_net.len);
1090 half_prefixlen = 1;
1091
1092 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1093 gateway, src_ip, if_name);
1094 half_net.ptr[0] |= 0x80;
1095 status = manage_srcroute(this, nlmsg_type, flags, half_net, half_prefixlen,
1096 gateway, src_ip, if_name);
1097 return status;
1098 }
1099
1100 memset(&request, 0, sizeof(request));
1101
1102 hdr = (struct nlmsghdr*)request;
1103 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1104 hdr->nlmsg_type = nlmsg_type;
1105 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1106
1107 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1108 msg->rtm_family = src_ip->get_family(src_ip);
1109 msg->rtm_dst_len = prefixlen;
1110 msg->rtm_table = this->routing_table;
1111 msg->rtm_protocol = RTPROT_STATIC;
1112 msg->rtm_type = RTN_UNICAST;
1113 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1114
1115 netlink_add_attribute(hdr, RTA_DST, dst_net, sizeof(request));
1116 chunk = src_ip->get_address(src_ip);
1117 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1118 chunk = gateway->get_address(gateway);
1119 netlink_add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1120 ifindex = get_interface_index(this, if_name);
1121 chunk.ptr = (char*)&ifindex;
1122 chunk.len = sizeof(ifindex);
1123 netlink_add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1124
1125 return this->socket->send_ack(this->socket, hdr);
1126 }
1127
1128 /**
1129 * Implementation of kernel_net_t.add_route.
1130 */
1131 status_t add_route(private_kernel_netlink_net_t *this, chunk_t dst_net,
1132 u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
1133 {
1134 return manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
1135 dst_net, prefixlen, gateway, src_ip, if_name);
1136 }
1137
1138 /**
1139 * Implementation of kernel_net_t.del_route.
1140 */
1141 status_t del_route(private_kernel_netlink_net_t *this, chunk_t dst_net,
1142 u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
1143 {
1144 return manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
1145 gateway, src_ip, if_name);
1146 }
1147
1148 /**
1149 * Initialize a list of local addresses.
1150 */
1151 static status_t init_address_list(private_kernel_netlink_net_t *this)
1152 {
1153 char request[NETLINK_BUFFER_SIZE];
1154 struct nlmsghdr *out, *current, *in;
1155 struct rtgenmsg *msg;
1156 size_t len;
1157 iterator_t *ifaces, *addrs;
1158 iface_entry_t *iface;
1159 addr_entry_t *addr;
1160
1161 DBG1(DBG_KNL, "listening on interfaces:");
1162
1163 memset(&request, 0, sizeof(request));
1164
1165 in = (struct nlmsghdr*)&request;
1166 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1167 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1168 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1169 msg->rtgen_family = AF_UNSPEC;
1170
1171 /* get all links */
1172 in->nlmsg_type = RTM_GETLINK;
1173 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1174 {
1175 return FAILED;
1176 }
1177 current = out;
1178 while (NLMSG_OK(current, len))
1179 {
1180 switch (current->nlmsg_type)
1181 {
1182 case NLMSG_DONE:
1183 break;
1184 case RTM_NEWLINK:
1185 process_link(this, current, FALSE);
1186 /* fall through */
1187 default:
1188 current = NLMSG_NEXT(current, len);
1189 continue;
1190 }
1191 break;
1192 }
1193 free(out);
1194
1195 /* get all interface addresses */
1196 in->nlmsg_type = RTM_GETADDR;
1197 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
1198 {
1199 return FAILED;
1200 }
1201 current = out;
1202 while (NLMSG_OK(current, len))
1203 {
1204 switch (current->nlmsg_type)
1205 {
1206 case NLMSG_DONE:
1207 break;
1208 case RTM_NEWADDR:
1209 process_addr(this, current, FALSE);
1210 /* fall through */
1211 default:
1212 current = NLMSG_NEXT(current, len);
1213 continue;
1214 }
1215 break;
1216 }
1217 free(out);
1218
1219 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1220 while (ifaces->iterate(ifaces, (void**)&iface))
1221 {
1222 if (iface->flags & IFF_UP)
1223 {
1224 DBG1(DBG_KNL, " %s", iface->ifname);
1225 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1226 while (addrs->iterate(addrs, (void**)&addr))
1227 {
1228 DBG1(DBG_KNL, " %H", addr->ip);
1229 }
1230 addrs->destroy(addrs);
1231 }
1232 }
1233 ifaces->destroy(ifaces);
1234 return SUCCESS;
1235 }
1236
1237 /**
1238 * create or delete a rule to use our routing table
1239 */
1240 static status_t manage_rule(private_kernel_netlink_net_t *this, int nlmsg_type,
1241 u_int32_t table, u_int32_t prio)
1242 {
1243 unsigned char request[NETLINK_BUFFER_SIZE];
1244 struct nlmsghdr *hdr;
1245 struct rtmsg *msg;
1246 chunk_t chunk;
1247
1248 memset(&request, 0, sizeof(request));
1249 hdr = (struct nlmsghdr*)request;
1250 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1251 hdr->nlmsg_type = nlmsg_type;
1252 if (nlmsg_type == RTM_NEWRULE)
1253 {
1254 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
1255 }
1256 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1257
1258 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1259 msg->rtm_table = table;
1260 msg->rtm_family = AF_INET;
1261 msg->rtm_protocol = RTPROT_BOOT;
1262 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1263 msg->rtm_type = RTN_UNICAST;
1264
1265 chunk = chunk_from_thing(prio);
1266 netlink_add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
1267
1268 return this->socket->send_ack(this->socket, hdr);
1269 }
1270
1271 /**
1272 * Implementation of kernel_netlink_net_t.destroy.
1273 */
1274 static void destroy(private_kernel_netlink_net_t *this)
1275 {
1276 if (this->routing_table)
1277 {
1278 manage_rule(this, RTM_DELRULE, this->routing_table,
1279 this->routing_table_prio);
1280 }
1281
1282 this->job->cancel(this->job);
1283 close(this->socket_events);
1284 this->socket->destroy(this->socket);
1285 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
1286 free(this);
1287 }
1288
1289 /*
1290 * Described in header.
1291 */
1292 kernel_netlink_net_t *kernel_netlink_net_create()
1293 {
1294 private_kernel_netlink_net_t *this = malloc_thing(private_kernel_netlink_net_t);
1295 struct sockaddr_nl addr;
1296
1297 /* public functions */
1298 this->public.interface.get_interface = (char*(*)(kernel_net_t*,host_t*))get_interface_name;
1299 this->public.interface.create_address_enumerator = (enumerator_t*(*)(kernel_net_t*,bool,bool))create_address_enumerator;
1300 this->public.interface.get_source_addr = (host_t*(*)(kernel_net_t*, host_t *dest, host_t *src))get_source_addr;
1301 this->public.interface.get_nexthop = (host_t*(*)(kernel_net_t*, host_t *dest))get_nexthop;
1302 this->public.interface.add_ip = (status_t(*)(kernel_net_t*,host_t*,host_t*)) add_ip;
1303 this->public.interface.del_ip = (status_t(*)(kernel_net_t*,host_t*)) del_ip;
1304 this->public.interface.add_route = (status_t(*)(kernel_net_t*,chunk_t,u_int8_t,host_t*,host_t*,char*)) add_route;
1305 this->public.interface.del_route = (status_t(*)(kernel_net_t*,chunk_t,u_int8_t,host_t*,host_t*,char*)) del_route;
1306 this->public.interface.destroy = (void(*)(kernel_net_t*)) destroy;
1307
1308 /* private members */
1309 this->ifaces = linked_list_create();
1310 pthread_mutex_init(&this->mutex, NULL);
1311 pthread_cond_init(&this->cond, NULL);
1312 timerclear(&this->last_roam);
1313 this->routing_table = lib->settings->get_int(lib->settings,
1314 "charon.routing_table", IPSEC_ROUTING_TABLE);
1315 this->routing_table_prio = lib->settings->get_int(lib->settings,
1316 "charon.routing_table_prio", IPSEC_ROUTING_TABLE_PRIO);
1317 this->process_route = lib->settings->get_bool(lib->settings,
1318 "charon.process_route", TRUE);
1319
1320 this->socket = netlink_socket_create(NETLINK_ROUTE);
1321
1322 memset(&addr, 0, sizeof(addr));
1323 addr.nl_family = AF_NETLINK;
1324
1325 /* create and bind RT socket for events (address/interface/route changes) */
1326 this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1327 if (this->socket_events <= 0)
1328 {
1329 charon->kill(charon, "unable to create RT event socket");
1330 }
1331 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
1332 RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_LINK;
1333 if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
1334 {
1335 charon->kill(charon, "unable to bind RT event socket");
1336 }
1337
1338 this->job = callback_job_create((callback_job_cb_t)receive_events,
1339 this, NULL, NULL);
1340 charon->processor->queue_job(charon->processor, (job_t*)this->job);
1341
1342 if (init_address_list(this) != SUCCESS)
1343 {
1344 charon->kill(charon, "unable to get interface list");
1345 }
1346
1347 if (this->routing_table)
1348 {
1349 if (manage_rule(this, RTM_NEWRULE, this->routing_table,
1350 this->routing_table_prio) != SUCCESS)
1351 {
1352 DBG1(DBG_KNL, "unable to create routing table rule");
1353 }
1354 }
1355
1356 return &this->public;
1357 }