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