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