]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/plugins/kernel_netlink/kernel_netlink_net.c
416ae78116738d95ce4ee12e08214fb9192ed7e3
[thirdparty/strongswan.git] / src / libcharon / plugins / kernel_netlink / kernel_netlink_net.c
1 /*
2 * Copyright (C) 2008-2019 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 *
5 * Copyright (C) secunet Security Networks AG
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /*
19 * Copyright (C) 2010 Thomas Egerer
20 *
21 * Permission is hereby granted, free of charge, to any person obtaining a copy
22 * of this software and associated documentation files (the "Software"), to deal
23 * in the Software without restriction, including without limitation the rights
24 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 * copies of the Software, and to permit persons to whom the Software is
26 * furnished to do so, subject to the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 * THE SOFTWARE.
38 */
39
40 #include <sys/socket.h>
41 #include <sys/utsname.h>
42 #include <linux/netlink.h>
43 #include <linux/rtnetlink.h>
44 #include <linux/if_addrlabel.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <net/if.h>
48 #ifdef HAVE_LINUX_FIB_RULES_H
49 #include <linux/fib_rules.h>
50 #endif
51
52 #include "kernel_netlink_net.h"
53 #include "kernel_netlink_shared.h"
54
55 #include <daemon.h>
56 #include <utils/debug.h>
57 #include <threading/mutex.h>
58 #include <threading/rwlock.h>
59 #include <threading/rwlock_condvar.h>
60 #include <threading/spinlock.h>
61 #include <collections/hashtable.h>
62 #include <collections/linked_list.h>
63 #include <processing/jobs/callback_job.h>
64
65 /** delay before firing roam events (ms) */
66 #define ROAM_DELAY 100
67
68 /** delay before reinstalling routes (ms) */
69 #define ROUTE_DELAY 100
70
71 /** maximum recursion when searching for addresses in get_route() */
72 #define MAX_ROUTE_RECURSION 2
73
74 #ifndef ROUTING_TABLE
75 #define ROUTING_TABLE 0
76 #endif
77
78 #ifndef ROUTING_TABLE_PRIO
79 #define ROUTING_TABLE_PRIO 0
80 #endif
81
82 ENUM(rt_msg_names, RTM_NEWLINK, RTM_GETRULE,
83 "RTM_NEWLINK",
84 "RTM_DELLINK",
85 "RTM_GETLINK",
86 "RTM_SETLINK",
87 "RTM_NEWADDR",
88 "RTM_DELADDR",
89 "RTM_GETADDR",
90 "23",
91 "RTM_NEWROUTE",
92 "RTM_DELROUTE",
93 "RTM_GETROUTE",
94 "27",
95 "RTM_NEWNEIGH",
96 "RTM_DELNEIGH",
97 "RTM_GETNEIGH",
98 "31",
99 "RTM_NEWRULE",
100 "RTM_DELRULE",
101 "RTM_GETRULE",
102 );
103
104 typedef struct addr_entry_t addr_entry_t;
105
106 /**
107 * IP address in an iface_entry_t
108 */
109 struct addr_entry_t {
110
111 /** the ip address */
112 host_t *ip;
113
114 /** address flags */
115 u_char flags;
116
117 /** scope of the address */
118 u_char scope;
119
120 /** number of times this IP is used, if virtual (i.e. managed by us) */
121 u_int refcount;
122
123 /** TRUE once it is installed, if virtual */
124 bool installed;
125 };
126
127 /**
128 * destroy a addr_entry_t object
129 */
130 static void addr_entry_destroy(addr_entry_t *this)
131 {
132 this->ip->destroy(this->ip);
133 free(this);
134 }
135
136 typedef struct iface_entry_t iface_entry_t;
137
138 /**
139 * A network interface on this system, containing addr_entry_t's
140 */
141 struct iface_entry_t {
142
143 /** interface index */
144 int ifindex;
145
146 /** name of the interface */
147 char ifname[IFNAMSIZ];
148
149 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
150 u_int flags;
151
152 /** list of addresses as host_t */
153 linked_list_t *addrs;
154
155 /** TRUE if usable by config */
156 bool usable;
157 };
158
159 /**
160 * destroy an interface entry
161 */
162 static void iface_entry_destroy(iface_entry_t *this)
163 {
164 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
165 free(this);
166 }
167
168 CALLBACK(iface_entry_by_index, bool,
169 iface_entry_t *this, va_list args)
170 {
171 int ifindex;
172
173 VA_ARGS_VGET(args, ifindex);
174 return this->ifindex == ifindex;
175 }
176
177 CALLBACK(iface_entry_by_name, bool,
178 iface_entry_t *this, va_list args)
179 {
180 char *ifname;
181
182 VA_ARGS_VGET(args, ifname);
183 return streq(this->ifname, ifname);
184 }
185
186 /**
187 * check if an interface is up
188 */
189 static inline bool iface_entry_up(iface_entry_t *iface)
190 {
191 return (iface->flags & IFF_UP) == IFF_UP;
192 }
193
194 /**
195 * check if an interface is up and usable
196 */
197 static inline bool iface_entry_up_and_usable(iface_entry_t *iface)
198 {
199 return iface->usable && iface_entry_up(iface);
200 }
201
202 typedef struct addr_map_entry_t addr_map_entry_t;
203
204 /**
205 * Entry that maps an IP address to an interface entry
206 */
207 struct addr_map_entry_t {
208 /** The IP address */
209 host_t *ip;
210
211 /** The address entry for this IP address */
212 addr_entry_t *addr;
213
214 /** The interface this address is installed on */
215 iface_entry_t *iface;
216 };
217
218 /**
219 * Hash a addr_map_entry_t object, all entries with the same IP address
220 * are stored in the same bucket
221 */
222 static u_int addr_map_entry_hash(addr_map_entry_t *this)
223 {
224 return chunk_hash(this->ip->get_address(this->ip));
225 }
226
227 /**
228 * Compare two addr_map_entry_t objects, two entries are equal if they are
229 * installed on the same interface
230 */
231 static bool addr_map_entry_equals(addr_map_entry_t *a, addr_map_entry_t *b)
232 {
233 return a->iface->ifindex == b->iface->ifindex &&
234 a->ip->ip_equals(a->ip, b->ip);
235 }
236
237 /**
238 * Used with get_match this finds an address entry if it is installed on
239 * an up and usable interface
240 */
241 static bool addr_map_entry_match_up_and_usable(addr_map_entry_t *a,
242 addr_map_entry_t *b)
243 {
244 return iface_entry_up_and_usable(b->iface) &&
245 a->ip->ip_equals(a->ip, b->ip);
246 }
247
248 /**
249 * Used with get_match this finds an address entry if it is installed on
250 * any active local interface
251 */
252 static bool addr_map_entry_match_up(addr_map_entry_t *a, addr_map_entry_t *b)
253 {
254 return iface_entry_up(b->iface) && a->ip->ip_equals(a->ip, b->ip);
255 }
256
257 /**
258 * Used with get_match this finds an address entry if it is installed on
259 * any local interface
260 */
261 static bool addr_map_entry_match(addr_map_entry_t *a, addr_map_entry_t *b)
262 {
263 return a->ip->ip_equals(a->ip, b->ip);
264 }
265
266 typedef struct net_change_t net_change_t;
267
268 /**
269 * Queued network changes
270 */
271 struct net_change_t {
272 /** Name of the interface that got activated (or an IP appeared on) */
273 char *if_name;
274 };
275
276 /**
277 * Destroy a net_change_t object
278 */
279 static void net_change_destroy(net_change_t *this)
280 {
281 free(this->if_name);
282 free(this);
283 }
284
285 /**
286 * Hash a net_change_t object
287 */
288 static u_int net_change_hash(net_change_t *this)
289 {
290 return chunk_hash(chunk_create(this->if_name, strlen(this->if_name)));
291 }
292
293 /**
294 * Compare two net_change_t objects
295 */
296 static bool net_change_equals(net_change_t *a, net_change_t *b)
297 {
298 return streq(a->if_name, b->if_name);
299 }
300
301 typedef struct private_kernel_netlink_net_t private_kernel_netlink_net_t;
302
303 /**
304 * Private variables and functions of kernel_netlink_net class.
305 */
306 struct private_kernel_netlink_net_t {
307 /**
308 * Public part of the kernel_netlink_net_t object.
309 */
310 kernel_netlink_net_t public;
311
312 /**
313 * lock to access various lists and maps
314 */
315 rwlock_t *lock;
316
317 /**
318 * condition variable to signal virtual IP add/removal
319 */
320 rwlock_condvar_t *condvar;
321
322 /**
323 * Cached list of interfaces and its addresses (iface_entry_t)
324 */
325 linked_list_t *ifaces;
326
327 /**
328 * Map for IP addresses to iface_entry_t objects (addr_map_entry_t)
329 */
330 hashlist_t *addrs;
331
332 /**
333 * Map for virtual IP addresses to iface_entry_t objects (addr_map_entry_t)
334 */
335 hashlist_t *vips;
336
337 /**
338 * netlink rt socket (routing)
339 */
340 netlink_socket_t *socket;
341
342 /**
343 * Netlink rt event socket
344 */
345 netlink_event_socket_t *socket_events;
346
347 /**
348 * earliest time of the next roam event
349 */
350 timeval_t next_roam;
351
352 /**
353 * roam event due to address change
354 */
355 bool roam_address;
356
357 /**
358 * lock to check and update roam event time
359 */
360 spinlock_t *roam_lock;
361
362 /**
363 * routing table to install routes
364 */
365 uint32_t routing_table;
366
367 /**
368 * priority of used routing table
369 */
370 uint32_t routing_table_prio;
371
372 /**
373 * installed routes
374 */
375 hashlist_t *routes;
376
377 /**
378 * mutex for routes
379 */
380 mutex_t *routes_lock;
381
382 /**
383 * interface changes which may trigger route reinstallation
384 */
385 hashtable_t *net_changes;
386
387 /**
388 * mutex for route reinstallation triggers
389 */
390 mutex_t *net_changes_lock;
391
392 /**
393 * time of last route reinstallation
394 */
395 timeval_t last_route_reinstall;
396
397 /**
398 * whether to react to RTM_NEWROUTE or RTM_DELROUTE events
399 */
400 bool process_route;
401
402 /**
403 * whether to react to RTM_NEWRULE or RTM_DELRULE events
404 */
405 bool process_rules;
406
407 /**
408 * whether to trigger roam events
409 */
410 bool roam_events;
411
412 /**
413 * whether to install IPsec policy routes
414 */
415 bool install_routes;
416
417 /**
418 * whether to actually install virtual IPs
419 */
420 bool install_virtual_ip;
421
422 /**
423 * the name of the interface virtual IP addresses are installed on
424 */
425 char *install_virtual_ip_on;
426
427 /**
428 * whether preferred source addresses can be specified for IPv6 routes
429 */
430 bool rta_prefsrc_for_ipv6;
431
432 /**
433 * whether marks can be used in route lookups
434 */
435 bool rta_mark;
436
437 /**
438 * the mark excluded from the routing rule used for virtual IPs
439 */
440 mark_t routing_mark;
441
442 /**
443 * whether to prefer temporary IPv6 addresses over public ones
444 */
445 bool prefer_temporary_addrs;
446
447 /**
448 * list with routing tables to be excluded from route lookup
449 */
450 linked_list_t *rt_exclude;
451
452 /**
453 * MTU to set on installed routes
454 */
455 uint32_t mtu;
456
457 /**
458 * MSS to set on installed routes
459 */
460 uint32_t mss;
461 };
462
463 /**
464 * Forward declaration
465 */
466 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
467 int nlmsg_type, int flags, chunk_t dst_net,
468 uint8_t prefixlen, host_t *gateway,
469 host_t *src_ip, char *if_name, bool pass);
470
471 /**
472 * Clear the queued network changes.
473 */
474 static void net_changes_clear(private_kernel_netlink_net_t *this)
475 {
476 enumerator_t *enumerator;
477 net_change_t *change;
478
479 enumerator = this->net_changes->create_enumerator(this->net_changes);
480 while (enumerator->enumerate(enumerator, NULL, (void**)&change))
481 {
482 this->net_changes->remove_at(this->net_changes, enumerator);
483 net_change_destroy(change);
484 }
485 enumerator->destroy(enumerator);
486 }
487
488 /**
489 * Act upon queued network changes.
490 */
491 static job_requeue_t reinstall_routes(private_kernel_netlink_net_t *this)
492 {
493 enumerator_t *enumerator;
494 route_entry_t *route;
495
496 this->net_changes_lock->lock(this->net_changes_lock);
497 this->routes_lock->lock(this->routes_lock);
498
499 enumerator = this->routes->ht.create_enumerator(&this->routes->ht);
500 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
501 {
502 net_change_t *change, lookup = {
503 .if_name = route->if_name,
504 };
505 if (route->pass || !route->if_name)
506 { /* no need to reinstall these, they don't reference interfaces */
507 continue;
508 }
509 /* check if a change for the outgoing interface is queued */
510 change = this->net_changes->get(this->net_changes, &lookup);
511 if (!change)
512 { /* in case src_ip is not on the outgoing interface */
513 if (this->public.interface.get_interface(&this->public.interface,
514 route->src_ip, &lookup.if_name))
515 {
516 if (!streq(lookup.if_name, route->if_name))
517 {
518 change = this->net_changes->get(this->net_changes, &lookup);
519 }
520 free(lookup.if_name);
521 }
522 }
523 if (change)
524 {
525 manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
526 route->dst_net, route->prefixlen, route->gateway,
527 route->src_ip, route->if_name, route->pass);
528 }
529 }
530 enumerator->destroy(enumerator);
531 this->routes_lock->unlock(this->routes_lock);
532
533 net_changes_clear(this);
534 this->net_changes_lock->unlock(this->net_changes_lock);
535 return JOB_REQUEUE_NONE;
536 }
537
538 /**
539 * Queue route reinstallation caused by network changes for a given interface.
540 *
541 * The route reinstallation is delayed for a while and only done once for
542 * several calls during this delay, in order to avoid doing it too often.
543 * The interface name is freed.
544 */
545 static void queue_route_reinstall(private_kernel_netlink_net_t *this,
546 char *if_name)
547 {
548 net_change_t *update, *found;
549 timeval_t now;
550 job_t *job;
551
552 INIT(update,
553 .if_name = if_name
554 );
555
556 this->net_changes_lock->lock(this->net_changes_lock);
557 found = this->net_changes->put(this->net_changes, update, update);
558 if (found)
559 {
560 net_change_destroy(found);
561 }
562 time_monotonic(&now);
563 if (timercmp(&now, &this->last_route_reinstall, >))
564 {
565 timeval_add_ms(&now, ROUTE_DELAY);
566 this->last_route_reinstall = now;
567
568 job = (job_t*)callback_job_create((callback_job_cb_t)reinstall_routes,
569 this, NULL, NULL);
570 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROUTE_DELAY);
571 }
572 this->net_changes_lock->unlock(this->net_changes_lock);
573 }
574
575 /**
576 * check if the given IP is known as virtual IP and currently installed
577 *
578 * this function will also return TRUE if the virtual IP entry disappeared.
579 * in that case the returned entry will be NULL.
580 *
581 * this->lock must be held when calling this function
582 */
583 static bool is_vip_installed_or_gone(private_kernel_netlink_net_t *this,
584 host_t *ip, addr_map_entry_t **entry)
585 {
586 addr_map_entry_t lookup = {
587 .ip = ip,
588 };
589
590 *entry = this->vips->get_match(this->vips, &lookup,
591 (void*)addr_map_entry_match);
592 if (*entry == NULL)
593 { /* the virtual IP disappeared */
594 return TRUE;
595 }
596 return (*entry)->addr->installed;
597 }
598
599 /**
600 * check if the given IP is known as virtual IP
601 *
602 * this->lock must be held when calling this function
603 */
604 static bool is_known_vip(private_kernel_netlink_net_t *this, host_t *ip)
605 {
606 addr_map_entry_t lookup = {
607 .ip = ip,
608 };
609
610 return this->vips->get_match(this->vips, &lookup,
611 (void*)addr_map_entry_match) != NULL;
612 }
613
614 /**
615 * Add an address map entry
616 */
617 static void addr_map_entry_add(hashlist_t *map, addr_entry_t *addr,
618 iface_entry_t *iface)
619 {
620 addr_map_entry_t *entry;
621
622 INIT(entry,
623 .ip = addr->ip,
624 .addr = addr,
625 .iface = iface,
626 );
627 entry = map->ht.put(&map->ht, entry, entry);
628 free(entry);
629 }
630
631 /**
632 * Remove an address map entry
633 */
634 static void addr_map_entry_remove(hashlist_t *map, addr_entry_t *addr,
635 iface_entry_t *iface)
636 {
637 addr_map_entry_t *entry, lookup = {
638 .ip = addr->ip,
639 .addr = addr,
640 .iface = iface,
641 };
642
643 entry = map->ht.remove(&map->ht, &lookup);
644 free(entry);
645 }
646
647 /**
648 * Check if an address or net (addr with prefix net bits) is in
649 * subnet (net with net_len net bits)
650 */
651 static bool addr_in_subnet(chunk_t addr, int prefix, chunk_t net, int net_len)
652 {
653 static const u_char mask[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
654 int byte = 0;
655
656 if (net_len == 0)
657 { /* any address matches a /0 network */
658 return TRUE;
659 }
660 if (addr.len != net.len || net_len > 8 * net.len || prefix < net_len)
661 {
662 return FALSE;
663 }
664 /* scan through all bytes in network order */
665 while (net_len > 0)
666 {
667 if (net_len < 8)
668 {
669 return (mask[net_len] & addr.ptr[byte]) == (mask[net_len] & net.ptr[byte]);
670 }
671 else
672 {
673 if (addr.ptr[byte] != net.ptr[byte])
674 {
675 return FALSE;
676 }
677 byte++;
678 net_len -= 8;
679 }
680 }
681 return TRUE;
682 }
683
684 /**
685 * Check if the given address is in subnet (net with net_len net bits)
686 */
687 static bool host_in_subnet(host_t *host, chunk_t net, int net_len)
688 {
689 chunk_t addr;
690
691 addr = host->get_address(host);
692 return addr_in_subnet(addr, addr.len * 8, net, net_len);
693 }
694
695 /**
696 * Determine the type or scope of the given unicast IP address. This is not
697 * the same thing returned in rtm_scope/ifa_scope.
698 *
699 * We use return values as defined in RFC 6724 (referring to RFC 4291).
700 */
701 static u_char get_scope(host_t *ip)
702 {
703 chunk_t addr;
704
705 addr = ip->get_address(ip);
706 switch (addr.len)
707 {
708 case 4:
709 /* we use the mapping defined in RFC 6724, 3.2 */
710 if (addr.ptr[0] == 127)
711 { /* link-local, same as the IPv6 loopback address */
712 return 2;
713 }
714 if (addr.ptr[0] == 169 && addr.ptr[1] == 254)
715 { /* link-local */
716 return 2;
717 }
718 break;
719 case 16:
720 if (IN6_IS_ADDR_LOOPBACK((struct in6_addr*)addr.ptr))
721 { /* link-local, according to RFC 4291, 2.5.3 */
722 return 2;
723 }
724 if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr*)addr.ptr))
725 {
726 return 2;
727 }
728 if (IN6_IS_ADDR_SITELOCAL((struct in6_addr*)addr.ptr))
729 { /* deprecated, according to RFC 4291, 2.5.7 */
730 return 5;
731 }
732 break;
733 default:
734 break;
735 }
736 /* global */
737 return 14;
738 }
739
740 /**
741 * Determine the label of the given unicast IP address.
742 *
743 * We currently only support the default table given in RFC 6724:
744 *
745 * Prefix Precedence Label
746 * ::1/128 50 0
747 * ::/0 40 1
748 * ::ffff:0:0/96 35 4
749 * 2002::/16 30 2
750 * 2001::/32 5 5
751 * fc00::/7 3 13
752 * ::/96 1 3
753 * fec0::/10 1 11
754 * 3ffe::/16 1 12
755 */
756 static u_char get_label(host_t *ip)
757 {
758 struct {
759 chunk_t net;
760 u_char prefix;
761 u_char label;
762 } priorities[] = {
763 /* priority table ordered by prefix */
764 /* ::1/128 */
765 { chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
766 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01), 128, 0 },
767 /* ::ffff:0:0/96 */
768 { chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
769 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00), 96, 4 },
770 /* ::/96 */
771 { chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
772 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 96, 3 },
773 /* 2001::/32 */
774 { chunk_from_chars(0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
775 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 32, 5 },
776 /* 2002::/16 */
777 { chunk_from_chars(0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
778 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 16, 2 },
779 /* 3ffe::/16 */
780 { chunk_from_chars(0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
781 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 16, 12 },
782 /* fec0::/10 */
783 { chunk_from_chars(0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
784 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 10, 11 },
785 /* fc00::/7 */
786 { chunk_from_chars(0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
787 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), 7, 13 },
788 };
789 int i;
790
791 for (i = 0; i < countof(priorities); i++)
792 {
793 if (host_in_subnet(ip, priorities[i].net, priorities[i].prefix))
794 {
795 return priorities[i].label;
796 }
797 }
798 /* ::/0 */
799 return 1;
800 }
801
802 /**
803 * Returns the length of the common prefix in bits up to the length of a's
804 * prefix, defined by RFC 6724 as the portion of the address not including the
805 * interface ID, which is 64-bit for most unicast addresses (see RFC 4291).
806 */
807 static u_char common_prefix(host_t *a, host_t *b)
808 {
809 chunk_t aa, ba;
810 u_char byte, bits = 0, match;
811
812 aa = a->get_address(a);
813 ba = b->get_address(b);
814 for (byte = 0; byte < 8; byte++)
815 {
816 if (aa.ptr[byte] != ba.ptr[byte])
817 {
818 match = aa.ptr[byte] ^ ba.ptr[byte];
819 for (bits = 8; match; match >>= 1)
820 {
821 bits--;
822 }
823 break;
824 }
825 }
826 return byte * 8 + bits;
827 }
828
829 /**
830 * Compare two IP addresses and return TRUE if the second address is the better
831 * choice of the two to reach the destination.
832 * For IPv6 we approximately follow RFC 6724.
833 */
834 static bool is_address_better(private_kernel_netlink_net_t *this,
835 addr_entry_t *a, addr_entry_t *b, host_t *d)
836 {
837 u_char sa, sb, sd, la, lb, ld, pa, pb;
838
839 /* rule 2: prefer appropriate scope */
840 if (d)
841 {
842 sa = get_scope(a->ip);
843 sb = get_scope(b->ip);
844 sd = get_scope(d);
845 if (sa < sb)
846 {
847 return sa < sd;
848 }
849 else if (sb < sa)
850 {
851 return sb >= sd;
852 }
853 }
854 if (a->ip->get_family(a->ip) == AF_INET)
855 { /* stop here for IPv4, default to addresses found earlier */
856 return FALSE;
857 }
858 /* rule 3: avoid deprecated addresses (RFC 4862) */
859 if ((a->flags & IFA_F_DEPRECATED) != (b->flags & IFA_F_DEPRECATED))
860 {
861 return a->flags & IFA_F_DEPRECATED;
862 }
863 /* rule 4 is not applicable as we don't know if an address is a home or
864 * care-of addresses.
865 * rule 5 does not apply as we only compare addresses from one interface
866 */
867 /* rule 6: prefer matching label */
868 if (d)
869 {
870 la = get_label(a->ip);
871 lb = get_label(b->ip);
872 ld = get_label(d);
873 if (la == ld && lb != ld)
874 {
875 return FALSE;
876 }
877 else if (lb == ld && la != ld)
878 {
879 return TRUE;
880 }
881 }
882 /* rule 7: prefer temporary addresses (WE REVERSE THIS BY DEFAULT!) */
883 if ((a->flags & IFA_F_TEMPORARY) != (b->flags & IFA_F_TEMPORARY))
884 {
885 if (this->prefer_temporary_addrs)
886 {
887 return b->flags & IFA_F_TEMPORARY;
888 }
889 return a->flags & IFA_F_TEMPORARY;
890 }
891 /* rule 8: use longest matching prefix */
892 if (d)
893 {
894 pa = common_prefix(a->ip, d);
895 pb = common_prefix(b->ip, d);
896 if (pa != pb)
897 {
898 return pb > pa;
899 }
900 }
901 /* default to addresses found earlier */
902 return FALSE;
903 }
904
905 /**
906 * Get a non-virtual IP address on the given interfaces and optionally in a
907 * given subnet.
908 *
909 * If a candidate address is given, we first search for that address and if not
910 * found return the address as above.
911 * Returned host is a clone, has to be freed by caller.
912 *
913 * this->lock must be held when calling this function.
914 */
915 static host_t *get_matching_address(private_kernel_netlink_net_t *this,
916 int *ifindex, int family, chunk_t net,
917 uint8_t mask, host_t *dest,
918 host_t *candidate)
919 {
920 enumerator_t *ifaces, *addrs;
921 iface_entry_t *iface;
922 addr_entry_t *addr, *best = NULL;
923 bool candidate_matched = FALSE;
924
925 ifaces = this->ifaces->create_enumerator(this->ifaces);
926 while (ifaces->enumerate(ifaces, &iface))
927 {
928 if (iface->usable && (!ifindex || iface->ifindex == *ifindex))
929 { /* only use matching interfaces not excluded by config */
930 addrs = iface->addrs->create_enumerator(iface->addrs);
931 while (addrs->enumerate(addrs, &addr))
932 {
933 if (addr->refcount ||
934 addr->ip->get_family(addr->ip) != family)
935 { /* ignore virtual IP addresses and ensure family matches */
936 continue;
937 }
938 if (net.ptr && !host_in_subnet(addr->ip, net, mask))
939 { /* optionally match a subnet */
940 continue;
941 }
942 if (candidate && candidate->ip_equals(candidate, addr->ip) &&
943 !(addr->flags & IFA_F_DEPRECATED))
944 { /* stop if we find the candidate and it's not deprecated */
945 best = addr;
946 candidate_matched = TRUE;
947 break;
948 }
949 else if (!best || is_address_better(this, best, addr, dest))
950 {
951 best = addr;
952 }
953 }
954 addrs->destroy(addrs);
955 if (ifindex || candidate_matched)
956 {
957 break;
958 }
959 }
960 }
961 ifaces->destroy(ifaces);
962 return best ? best->ip->clone(best->ip) : NULL;
963 }
964
965 /**
966 * Get a non-virtual IP address on the given interface.
967 *
968 * If a candidate address is given, we first search for that address and if not
969 * found return the address as above.
970 * Returned host is a clone, has to be freed by caller.
971 *
972 * this->lock must be held when calling this function.
973 */
974 static host_t *get_interface_address(private_kernel_netlink_net_t *this,
975 int ifindex, int family, host_t *dest,
976 host_t *candidate)
977 {
978 return get_matching_address(this, &ifindex, family, chunk_empty, 0, dest,
979 candidate);
980 }
981
982 /**
983 * Get a non-virtual IP address in the given subnet.
984 *
985 * If a candidate address is given, we first search for that address and if not
986 * found return the address as above.
987 * Returned host is a clone, has to be freed by caller.
988 *
989 * this->lock must be held when calling this function.
990 */
991 static host_t *get_subnet_address(private_kernel_netlink_net_t *this,
992 int family, chunk_t net, uint8_t mask,
993 host_t *dest, host_t *candidate)
994 {
995 return get_matching_address(this, NULL, family, net, mask, dest, candidate);
996 }
997
998 /**
999 * callback function that raises the delayed roam event
1000 */
1001 static job_requeue_t roam_event(private_kernel_netlink_net_t *this)
1002 {
1003 bool address;
1004
1005 this->roam_lock->lock(this->roam_lock);
1006 address = this->roam_address;
1007 this->roam_address = FALSE;
1008 this->roam_lock->unlock(this->roam_lock);
1009 charon->kernel->roam(charon->kernel, address);
1010 return JOB_REQUEUE_NONE;
1011 }
1012
1013 /**
1014 * fire a roaming event. we delay it for a bit and fire only one event
1015 * for multiple calls. otherwise we would create too many events.
1016 */
1017 static void fire_roam_event(private_kernel_netlink_net_t *this, bool address)
1018 {
1019 timeval_t now;
1020 job_t *job;
1021
1022 if (!this->roam_events)
1023 {
1024 return;
1025 }
1026
1027 time_monotonic(&now);
1028 this->roam_lock->lock(this->roam_lock);
1029 this->roam_address |= address;
1030 if (!timercmp(&now, &this->next_roam, >))
1031 {
1032 this->roam_lock->unlock(this->roam_lock);
1033 return;
1034 }
1035 timeval_add_ms(&now, ROAM_DELAY);
1036 this->next_roam = now;
1037 this->roam_lock->unlock(this->roam_lock);
1038
1039 job = (job_t*)callback_job_create((callback_job_cb_t)roam_event,
1040 this, NULL, NULL);
1041 lib->scheduler->schedule_job_ms(lib->scheduler, job, ROAM_DELAY);
1042 }
1043
1044 /**
1045 * check if an interface with a given index is up and usable
1046 *
1047 * this->lock must be locked when calling this function
1048 */
1049 static bool is_interface_up_and_usable(private_kernel_netlink_net_t *this,
1050 int index)
1051 {
1052 iface_entry_t *iface;
1053
1054 if (this->ifaces->find_first(this->ifaces, iface_entry_by_index,
1055 (void**)&iface, index))
1056 {
1057 return iface_entry_up_and_usable(iface);
1058 }
1059 return FALSE;
1060 }
1061
1062 /**
1063 * unregister the current addr_entry_t from the hashtable it is stored in
1064 *
1065 * this->lock must be locked when calling this function
1066 */
1067 CALLBACK(addr_entry_unregister, void,
1068 addr_entry_t *addr, va_list args)
1069 {
1070 private_kernel_netlink_net_t *this;
1071 iface_entry_t *iface;
1072
1073 VA_ARGS_VGET(args, iface, this);
1074 if (addr->refcount)
1075 {
1076 addr_map_entry_remove(this->vips, addr, iface);
1077 this->condvar->broadcast(this->condvar);
1078 return;
1079 }
1080 addr_map_entry_remove(this->addrs, addr, iface);
1081 }
1082
1083 /**
1084 * process RTM_NEWLINK/RTM_DELLINK from kernel
1085 */
1086 static void process_link(private_kernel_netlink_net_t *this,
1087 struct nlmsghdr *hdr, bool event)
1088 {
1089 struct ifinfomsg* msg = NLMSG_DATA(hdr);
1090 struct rtattr *rta = IFLA_RTA(msg);
1091 size_t rtasize = IFLA_PAYLOAD (hdr);
1092 enumerator_t *enumerator;
1093 iface_entry_t *current, *entry = NULL;
1094 char *name = NULL;
1095 bool update = FALSE, update_routes = FALSE;
1096
1097 while (RTA_OK(rta, rtasize))
1098 {
1099 switch (rta->rta_type)
1100 {
1101 case IFLA_IFNAME:
1102 name = RTA_DATA(rta);
1103 break;
1104 }
1105 rta = RTA_NEXT(rta, rtasize);
1106 }
1107 if (!name)
1108 {
1109 name = "(unknown)";
1110 }
1111
1112 this->lock->write_lock(this->lock);
1113 switch (hdr->nlmsg_type)
1114 {
1115 case RTM_NEWLINK:
1116 {
1117 if (!this->ifaces->find_first(this->ifaces, iface_entry_by_index,
1118 (void**)&entry, msg->ifi_index))
1119 {
1120 INIT(entry,
1121 .ifindex = msg->ifi_index,
1122 .addrs = linked_list_create(),
1123 );
1124 this->ifaces->insert_last(this->ifaces, entry);
1125 }
1126 strncpy(entry->ifname, name, IFNAMSIZ-1);
1127 entry->ifname[IFNAMSIZ-1] = '\0';
1128 entry->usable = charon->kernel->is_interface_usable(charon->kernel,
1129 name);
1130 if (event && entry->usable)
1131 {
1132 if (!(entry->flags & IFF_UP) && (msg->ifi_flags & IFF_UP))
1133 {
1134 update = update_routes = TRUE;
1135 DBG1(DBG_KNL, "interface %s activated", name);
1136 }
1137 if ((entry->flags & IFF_UP) && !(msg->ifi_flags & IFF_UP))
1138 {
1139 update = TRUE;
1140 DBG1(DBG_KNL, "interface %s deactivated", name);
1141 }
1142 }
1143 entry->flags = msg->ifi_flags;
1144 break;
1145 }
1146 case RTM_DELLINK:
1147 {
1148 enumerator = this->ifaces->create_enumerator(this->ifaces);
1149 while (enumerator->enumerate(enumerator, &current))
1150 {
1151 if (current->ifindex == msg->ifi_index)
1152 {
1153 if (event && current->usable)
1154 {
1155 update = TRUE;
1156 DBG1(DBG_KNL, "interface %s deleted", current->ifname);
1157 }
1158 /* TODO: move virtual IPs installed on this interface to
1159 * another interface? */
1160 this->ifaces->remove_at(this->ifaces, enumerator);
1161 current->addrs->invoke_function(current->addrs,
1162 addr_entry_unregister, current, this);
1163 iface_entry_destroy(current);
1164 break;
1165 }
1166 }
1167 enumerator->destroy(enumerator);
1168 break;
1169 }
1170 }
1171 this->lock->unlock(this->lock);
1172
1173 if (update_routes && event)
1174 {
1175 queue_route_reinstall(this, strdup(name));
1176 }
1177
1178 if (update && event)
1179 {
1180 fire_roam_event(this, TRUE);
1181 }
1182 }
1183
1184 /**
1185 * process RTM_NEWADDR/RTM_DELADDR from kernel
1186 */
1187 static void process_addr(private_kernel_netlink_net_t *this,
1188 struct nlmsghdr *hdr, bool event)
1189 {
1190 struct ifaddrmsg* msg = NLMSG_DATA(hdr);
1191 struct rtattr *rta = IFA_RTA(msg);
1192 size_t rtasize = IFA_PAYLOAD (hdr);
1193 host_t *host = NULL;
1194 iface_entry_t *iface;
1195 chunk_t local = chunk_empty, address = chunk_empty;
1196 char *route_ifname = NULL;
1197 bool update = FALSE, found = FALSE, changed = FALSE;
1198
1199 while (RTA_OK(rta, rtasize))
1200 {
1201 switch (rta->rta_type)
1202 {
1203 case IFA_LOCAL:
1204 local.ptr = RTA_DATA(rta);
1205 local.len = RTA_PAYLOAD(rta);
1206 break;
1207 case IFA_ADDRESS:
1208 address.ptr = RTA_DATA(rta);
1209 address.len = RTA_PAYLOAD(rta);
1210 break;
1211 }
1212 rta = RTA_NEXT(rta, rtasize);
1213 }
1214
1215 /* For PPP interfaces, we need the IFA_LOCAL address,
1216 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
1217 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
1218 if (local.ptr)
1219 {
1220 host = host_create_from_chunk(msg->ifa_family, local, 0);
1221 }
1222 else if (address.ptr)
1223 {
1224 host = host_create_from_chunk(msg->ifa_family, address, 0);
1225 }
1226
1227 if (host == NULL)
1228 { /* bad family? */
1229 return;
1230 }
1231
1232 this->lock->write_lock(this->lock);
1233 if (this->ifaces->find_first(this->ifaces, iface_entry_by_index,
1234 (void**)&iface, msg->ifa_index))
1235 {
1236 addr_map_entry_t *entry, lookup = {
1237 .ip = host,
1238 .iface = iface,
1239 };
1240 addr_entry_t *addr;
1241
1242 entry = this->vips->ht.get(&this->vips->ht, &lookup);
1243 if (entry)
1244 {
1245 if (hdr->nlmsg_type == RTM_NEWADDR)
1246 { /* mark as installed and signal waiting threads */
1247 entry->addr->installed = TRUE;
1248 }
1249 else
1250 { /* the address was already marked as uninstalled */
1251 addr = entry->addr;
1252 iface->addrs->remove(iface->addrs, addr, NULL);
1253 addr_map_entry_remove(this->vips, addr, iface);
1254 addr_entry_destroy(addr);
1255 }
1256 /* no roam events etc. for virtual IPs */
1257 this->condvar->broadcast(this->condvar);
1258 this->lock->unlock(this->lock);
1259 host->destroy(host);
1260 return;
1261 }
1262 entry = this->addrs->ht.get(&this->addrs->ht, &lookup);
1263 if (entry)
1264 {
1265 if (hdr->nlmsg_type == RTM_DELADDR)
1266 {
1267 found = TRUE;
1268 addr = entry->addr;
1269 iface->addrs->remove(iface->addrs, addr, NULL);
1270 if (iface->usable)
1271 {
1272 changed = TRUE;
1273 DBG1(DBG_KNL, "%H disappeared from %s", host,
1274 iface->ifname);
1275 }
1276 addr_map_entry_remove(this->addrs, addr, iface);
1277 addr_entry_destroy(addr);
1278 }
1279 else if (entry->addr->flags != msg->ifa_flags)
1280 {
1281 found = TRUE;
1282 entry->addr->flags = msg->ifa_flags;
1283 if (event && iface->usable)
1284 {
1285 changed = TRUE;
1286 DBG1(DBG_KNL, "flags changed for %H on %s", host,
1287 iface->ifname);
1288 }
1289 }
1290 }
1291 else
1292 {
1293 if (hdr->nlmsg_type == RTM_NEWADDR)
1294 {
1295 found = TRUE;
1296 changed = TRUE;
1297 route_ifname = strdup(iface->ifname);
1298 INIT(addr,
1299 .ip = host->clone(host),
1300 .flags = msg->ifa_flags,
1301 .scope = msg->ifa_scope,
1302 );
1303 iface->addrs->insert_last(iface->addrs, addr);
1304 addr_map_entry_add(this->addrs, addr, iface);
1305 if (event && iface->usable)
1306 {
1307 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
1308 }
1309 }
1310 }
1311 if (found && (iface->flags & IFF_UP))
1312 {
1313 update = TRUE;
1314 }
1315 if (!iface->usable)
1316 { /* ignore events for interfaces excluded by config */
1317 update = changed = FALSE;
1318 }
1319 }
1320 this->lock->unlock(this->lock);
1321
1322 if (update && event && route_ifname)
1323 {
1324 queue_route_reinstall(this, route_ifname);
1325 }
1326 else
1327 {
1328 free(route_ifname);
1329 }
1330 host->destroy(host);
1331
1332 /* send an update to all IKE_SAs */
1333 if (update && event && changed)
1334 {
1335 fire_roam_event(this, TRUE);
1336 }
1337 }
1338
1339 /**
1340 * process RTM_NEWROUTE and RTM_DELROUTE from kernel
1341 */
1342 static void process_route(private_kernel_netlink_net_t *this,
1343 struct nlmsghdr *hdr)
1344 {
1345 struct rtmsg* msg = NLMSG_DATA(hdr);
1346 struct rtattr *rta = RTM_RTA(msg);
1347 size_t rtasize = RTM_PAYLOAD(hdr);
1348 uint32_t rta_oif = 0;
1349 host_t *host = NULL;
1350
1351 /* ignore routes added by us or in the local routing table (local addrs) */
1352 if (msg->rtm_table && (msg->rtm_table == this->routing_table ||
1353 msg->rtm_table == RT_TABLE_LOCAL))
1354 {
1355 return;
1356 }
1357 else if (msg->rtm_flags & RTM_F_CLONED)
1358 { /* ignore cached routes, seem to be created a lot for IPv6 */
1359 return;
1360 }
1361
1362 while (RTA_OK(rta, rtasize))
1363 {
1364 switch (rta->rta_type)
1365 {
1366 #ifdef HAVE_RTA_TABLE
1367 case RTA_TABLE:
1368 /* also check against extended table ID */
1369 if (RTA_PAYLOAD(rta) == sizeof(uint32_t) &&
1370 this->routing_table == *(uint32_t*)RTA_DATA(rta))
1371 {
1372 DESTROY_IF(host);
1373 return;
1374 }
1375 break;
1376 #endif /* HAVE_RTA_TABLE */
1377 case RTA_PREFSRC:
1378 DESTROY_IF(host);
1379 host = host_create_from_chunk(msg->rtm_family,
1380 chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)), 0);
1381 break;
1382 case RTA_OIF:
1383 if (RTA_PAYLOAD(rta) == sizeof(rta_oif))
1384 {
1385 rta_oif = *(uint32_t*)RTA_DATA(rta);
1386 }
1387 break;
1388 }
1389 rta = RTA_NEXT(rta, rtasize);
1390 }
1391 this->lock->read_lock(this->lock);
1392 if (rta_oif && !is_interface_up_and_usable(this, rta_oif))
1393 { /* ignore route changes for interfaces that are ignored or down */
1394 this->lock->unlock(this->lock);
1395 DESTROY_IF(host);
1396 return;
1397 }
1398 if (!host && rta_oif)
1399 {
1400 host = get_interface_address(this, rta_oif, msg->rtm_family,
1401 NULL, NULL);
1402 }
1403 if (!host || is_known_vip(this, host))
1404 { /* ignore routes added for virtual IPs */
1405 this->lock->unlock(this->lock);
1406 DESTROY_IF(host);
1407 return;
1408 }
1409 this->lock->unlock(this->lock);
1410 fire_roam_event(this, FALSE);
1411 host->destroy(host);
1412 }
1413
1414 /**
1415 * process RTM_NEW|DELRULE from kernel
1416 */
1417 static void process_rule(private_kernel_netlink_net_t *this,
1418 struct nlmsghdr *hdr)
1419 {
1420 #ifdef HAVE_LINUX_FIB_RULES_H
1421 struct rtmsg* msg = NLMSG_DATA(hdr);
1422 struct rtattr *rta = RTM_RTA(msg);
1423 size_t rtasize = RTM_PAYLOAD(hdr);
1424
1425 /* ignore rules added by us or in the local routing table (local addrs) */
1426 if (msg->rtm_table && (msg->rtm_table == this->routing_table ||
1427 msg->rtm_table == RT_TABLE_LOCAL))
1428 {
1429 return;
1430 }
1431
1432 while (RTA_OK(rta, rtasize))
1433 {
1434 switch (rta->rta_type)
1435 {
1436 case FRA_TABLE:
1437 /* also check against extended table ID */
1438 if (RTA_PAYLOAD(rta) == sizeof(uint32_t) &&
1439 this->routing_table == *(uint32_t*)RTA_DATA(rta))
1440 {
1441 return;
1442 }
1443 break;
1444 }
1445 rta = RTA_NEXT(rta, rtasize);
1446 }
1447 fire_roam_event(this, FALSE);
1448 #endif
1449 }
1450
1451 CALLBACK(receive_events, void,
1452 private_kernel_netlink_net_t *this, struct nlmsghdr *hdr)
1453 {
1454 switch (hdr->nlmsg_type)
1455 {
1456 case RTM_NEWADDR:
1457 case RTM_DELADDR:
1458 process_addr(this, hdr, TRUE);
1459 break;
1460 case RTM_NEWLINK:
1461 case RTM_DELLINK:
1462 process_link(this, hdr, TRUE);
1463 break;
1464 case RTM_NEWROUTE:
1465 case RTM_DELROUTE:
1466 if (this->process_route)
1467 {
1468 process_route(this, hdr);
1469 }
1470 break;
1471 case RTM_NEWRULE:
1472 case RTM_DELRULE:
1473 if (this->process_rules)
1474 {
1475 process_rule(this, hdr);
1476 }
1477 break;
1478 default:
1479 break;
1480 }
1481 }
1482
1483 /** enumerator over addresses */
1484 typedef struct {
1485 private_kernel_netlink_net_t* this;
1486 /** which addresses to enumerate */
1487 kernel_address_type_t which;
1488 } address_enumerator_t;
1489
1490 CALLBACK(address_enumerator_destroy, void,
1491 address_enumerator_t *data)
1492 {
1493 data->this->lock->unlock(data->this->lock);
1494 free(data);
1495 }
1496
1497 CALLBACK(filter_addresses, bool,
1498 address_enumerator_t *data, enumerator_t *orig, va_list args)
1499 {
1500 addr_entry_t *addr;
1501 host_t **out;
1502
1503 VA_ARGS_VGET(args, out);
1504
1505 while (orig->enumerate(orig, &addr))
1506 {
1507 if (!(data->which & ADDR_TYPE_VIRTUAL) && addr->refcount)
1508 { /* skip virtual interfaces added by us */
1509 continue;
1510 }
1511 if (!(data->which & ADDR_TYPE_REGULAR) && !addr->refcount)
1512 { /* address is regular, but not requested */
1513 continue;
1514 }
1515 if (addr->flags & IFA_F_DEPRECATED ||
1516 addr->scope >= RT_SCOPE_LINK)
1517 { /* skip deprecated addresses or those with an unusable scope */
1518 continue;
1519 }
1520 if (!addr->refcount && addr->ip->get_family(addr->ip) == AF_INET6)
1521 { /* handle non-VIP temporary IPv6 addresses according to config */
1522 bool temporary = (addr->flags & IFA_F_TEMPORARY) == IFA_F_TEMPORARY;
1523 if (data->this->prefer_temporary_addrs != temporary)
1524 {
1525 continue;
1526 }
1527 }
1528 *out = addr->ip;
1529 return TRUE;
1530 }
1531 return FALSE;
1532 }
1533
1534 /**
1535 * enumerator constructor for interfaces
1536 */
1537 static enumerator_t *create_iface_enumerator(iface_entry_t *iface,
1538 address_enumerator_t *data)
1539 {
1540 return enumerator_create_filter(
1541 iface->addrs->create_enumerator(iface->addrs),
1542 filter_addresses, data, NULL);
1543 }
1544
1545 CALLBACK(filter_interfaces, bool,
1546 address_enumerator_t *data, enumerator_t *orig, va_list args)
1547 {
1548 iface_entry_t *iface, **out;
1549
1550 VA_ARGS_VGET(args, out);
1551
1552 while (orig->enumerate(orig, &iface))
1553 {
1554 if (!(data->which & ADDR_TYPE_IGNORED) && !iface->usable)
1555 { /* skip interfaces excluded by config */
1556 continue;
1557 }
1558 if (!(data->which & ADDR_TYPE_LOOPBACK) && (iface->flags & IFF_LOOPBACK))
1559 { /* ignore loopback devices */
1560 continue;
1561 }
1562 if (!(data->which & ADDR_TYPE_DOWN) && !(iface->flags & IFF_UP))
1563 { /* skip interfaces not up */
1564 continue;
1565 }
1566 *out = iface;
1567 return TRUE;
1568 }
1569 return FALSE;
1570 }
1571
1572 METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
1573 private_kernel_netlink_net_t *this, kernel_address_type_t which)
1574 {
1575 address_enumerator_t *data;
1576
1577 INIT(data,
1578 .this = this,
1579 .which = which,
1580 );
1581
1582 this->lock->read_lock(this->lock);
1583 return enumerator_create_nested(
1584 enumerator_create_filter(
1585 this->ifaces->create_enumerator(this->ifaces),
1586 filter_interfaces, data, NULL),
1587 (void*)create_iface_enumerator, data,
1588 address_enumerator_destroy);
1589 }
1590
1591 METHOD(kernel_net_t, get_interface_name, bool,
1592 private_kernel_netlink_net_t *this, host_t* ip, char **name)
1593 {
1594 addr_map_entry_t *entry, lookup = {
1595 .ip = ip,
1596 };
1597
1598 if (ip->is_anyaddr(ip))
1599 {
1600 return FALSE;
1601 }
1602 this->lock->read_lock(this->lock);
1603 /* first try to find it on an up and usable interface */
1604 entry = this->addrs->get_match(this->addrs, &lookup,
1605 (void*)addr_map_entry_match_up_and_usable);
1606 if (entry)
1607 {
1608 if (name)
1609 {
1610 *name = strdup(entry->iface->ifname);
1611 DBG2(DBG_KNL, "%H is on interface %s", ip, *name);
1612 }
1613 this->lock->unlock(this->lock);
1614 return TRUE;
1615 }
1616 /* in a second step, consider virtual IPs installed by us */
1617 entry = this->vips->get_match(this->vips, &lookup,
1618 (void*)addr_map_entry_match_up_and_usable);
1619 if (entry)
1620 {
1621 if (name)
1622 {
1623 *name = strdup(entry->iface->ifname);
1624 DBG2(DBG_KNL, "virtual IP %H is on interface %s", ip, *name);
1625 }
1626 this->lock->unlock(this->lock);
1627 return TRUE;
1628 }
1629 /* maybe it is installed on an ignored interface */
1630 entry = this->addrs->get_match(this->addrs, &lookup,
1631 (void*)addr_map_entry_match_up);
1632 if (!entry)
1633 {
1634 DBG2(DBG_KNL, "%H is not a local address or the interface is down", ip);
1635 }
1636 this->lock->unlock(this->lock);
1637 return FALSE;
1638 }
1639
1640 /**
1641 * get the index of an interface by name
1642 */
1643 static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
1644 {
1645 iface_entry_t *iface;
1646 int ifindex = 0;
1647
1648 DBG2(DBG_KNL, "getting iface index for %s", name);
1649
1650 this->lock->read_lock(this->lock);
1651 if (this->ifaces->find_first(this->ifaces, iface_entry_by_name,
1652 (void**)&iface, name))
1653 {
1654 ifindex = iface->ifindex;
1655 }
1656 this->lock->unlock(this->lock);
1657
1658 if (ifindex == 0)
1659 {
1660 DBG1(DBG_KNL, "unable to get interface index for %s", name);
1661 }
1662 return ifindex;
1663 }
1664
1665 /**
1666 * get the name of an interface by index (allocated)
1667 */
1668 static char *get_interface_name_by_index(private_kernel_netlink_net_t *this,
1669 int index)
1670 {
1671 iface_entry_t *iface;
1672 char *name = NULL;
1673
1674 DBG2(DBG_KNL, "getting iface name for index %d", index);
1675
1676 this->lock->read_lock(this->lock);
1677 if (this->ifaces->find_first(this->ifaces, iface_entry_by_index,
1678 (void**)&iface, index))
1679 {
1680 name = strdup(iface->ifname);
1681 }
1682 this->lock->unlock(this->lock);
1683
1684 if (!name)
1685 {
1686 DBG1(DBG_KNL, "unable to get interface name for %d", index);
1687 }
1688 return name;
1689 }
1690
1691 /**
1692 * Store information about a route retrieved via RTNETLINK
1693 */
1694 typedef struct {
1695 chunk_t gtw;
1696 chunk_t pref_src;
1697 chunk_t dst;
1698 chunk_t src;
1699 host_t *src_host;
1700 uint8_t dst_len;
1701 uint8_t src_len;
1702 uint32_t table;
1703 uint32_t oif;
1704 uint32_t priority;
1705 } rt_entry_t;
1706
1707 /**
1708 * Free a route entry
1709 */
1710 static void rt_entry_destroy(rt_entry_t *this)
1711 {
1712 DESTROY_IF(this->src_host);
1713 free(this);
1714 }
1715
1716 /**
1717 * Check if the route received with RTM_NEWROUTE is usable based on its type.
1718 */
1719 static bool route_usable(struct nlmsghdr *hdr, bool allow_local)
1720 {
1721 struct rtmsg *msg;
1722
1723 msg = NLMSG_DATA(hdr);
1724 switch (msg->rtm_type)
1725 {
1726 case RTN_BLACKHOLE:
1727 case RTN_UNREACHABLE:
1728 case RTN_PROHIBIT:
1729 case RTN_THROW:
1730 return FALSE;
1731 case RTN_LOCAL:
1732 return allow_local;
1733 default:
1734 return TRUE;
1735 }
1736 }
1737
1738 /**
1739 * Parse route received with RTM_NEWROUTE. The given rt_entry_t object will be
1740 * reused if not NULL.
1741 *
1742 * Returned chunks point to internal data of the Netlink message.
1743 */
1744 static rt_entry_t *parse_route(struct nlmsghdr *hdr, rt_entry_t *route)
1745 {
1746 struct rtattr *rta;
1747 struct rtmsg *msg;
1748 size_t rtasize;
1749
1750 msg = NLMSG_DATA(hdr);
1751 rta = RTM_RTA(msg);
1752 rtasize = RTM_PAYLOAD(hdr);
1753
1754 if (route)
1755 {
1756 *route = (rt_entry_t){
1757 .dst_len = msg->rtm_dst_len,
1758 .src_len = msg->rtm_src_len,
1759 .table = msg->rtm_table,
1760 };
1761 }
1762 else
1763 {
1764 INIT(route,
1765 .dst_len = msg->rtm_dst_len,
1766 .src_len = msg->rtm_src_len,
1767 .table = msg->rtm_table,
1768 );
1769 }
1770
1771 while (RTA_OK(rta, rtasize))
1772 {
1773 switch (rta->rta_type)
1774 {
1775 case RTA_PREFSRC:
1776 route->pref_src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1777 break;
1778 case RTA_GATEWAY:
1779 route->gtw = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1780 break;
1781 case RTA_DST:
1782 route->dst = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1783 break;
1784 case RTA_SRC:
1785 route->src = chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta));
1786 break;
1787 case RTA_OIF:
1788 if (RTA_PAYLOAD(rta) == sizeof(route->oif))
1789 {
1790 route->oif = *(uint32_t*)RTA_DATA(rta);
1791 }
1792 break;
1793 case RTA_PRIORITY:
1794 if (RTA_PAYLOAD(rta) == sizeof(route->priority))
1795 {
1796 route->priority = *(uint32_t*)RTA_DATA(rta);
1797 }
1798 break;
1799 #ifdef HAVE_RTA_TABLE
1800 case RTA_TABLE:
1801 if (RTA_PAYLOAD(rta) == sizeof(route->table))
1802 {
1803 route->table = *(uint32_t*)RTA_DATA(rta);
1804 }
1805 break;
1806 #endif /* HAVE_RTA_TABLE*/
1807 }
1808 rta = RTA_NEXT(rta, rtasize);
1809 }
1810 return route;
1811 }
1812
1813 /**
1814 * Get a route: If "nexthop", the nexthop is returned. source addr otherwise.
1815 */
1816 static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
1817 int prefix, bool nexthop, host_t *candidate,
1818 char **iface, u_int recursion)
1819 {
1820 netlink_buf_t request;
1821 struct nlmsghdr *hdr, *out, *current;
1822 struct rtmsg *msg;
1823 chunk_t chunk;
1824 size_t len;
1825 linked_list_t *routes;
1826 rt_entry_t *route = NULL, *best = NULL;
1827 enumerator_t *enumerator;
1828 host_t *addr = NULL;
1829 bool match_net;
1830 int family;
1831
1832 if (recursion > MAX_ROUTE_RECURSION)
1833 {
1834 return NULL;
1835 }
1836 chunk = dest->get_address(dest);
1837 len = chunk.len * 8;
1838 prefix = prefix < 0 ? len : min(prefix, len);
1839 match_net = prefix != len;
1840
1841 memset(&request, 0, sizeof(request));
1842
1843 family = dest->get_family(dest);
1844 hdr = &request.hdr;
1845 hdr->nlmsg_flags = NLM_F_REQUEST;
1846 hdr->nlmsg_type = RTM_GETROUTE;
1847 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1848
1849 msg = NLMSG_DATA(hdr);
1850 msg->rtm_family = family;
1851 if (!match_net && this->rta_mark && this->routing_mark.value)
1852 {
1853 /* if our routing rule excludes packets with a certain mark we can
1854 * get the preferred route without having to dump all routes */
1855 chunk = chunk_from_thing(this->routing_mark.value);
1856 netlink_add_attribute(hdr, RTA_MARK, chunk, sizeof(request));
1857 }
1858 else if (family == AF_INET || this->rta_prefsrc_for_ipv6 ||
1859 this->routing_table || match_net)
1860 { /* kernels prior to 3.0 do not support RTA_PREFSRC for IPv6 routes.
1861 * as we want to ignore routes with virtual IPs we cannot use DUMP
1862 * if these routes are not installed in a separate table */
1863 if (this->install_routes)
1864 {
1865 hdr->nlmsg_flags |= NLM_F_DUMP;
1866 }
1867 }
1868 if (candidate)
1869 {
1870 chunk = candidate->get_address(candidate);
1871 if (hdr->nlmsg_flags & NLM_F_DUMP)
1872 {
1873 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1874 }
1875 else
1876 {
1877 netlink_add_attribute(hdr, RTA_SRC, chunk, sizeof(request));
1878 }
1879 }
1880 /* we use this below to match against the routes */
1881 chunk = dest->get_address(dest);
1882 if (!match_net)
1883 {
1884 netlink_add_attribute(hdr, RTA_DST, chunk, sizeof(request));
1885 }
1886
1887 if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
1888 {
1889 DBG2(DBG_KNL, "getting %s to reach %H/%d failed",
1890 nexthop ? "nexthop" : "address", dest, prefix);
1891 return NULL;
1892 }
1893 routes = linked_list_create();
1894 this->lock->read_lock(this->lock);
1895
1896 for (current = out; NLMSG_OK(current, len);
1897 current = NLMSG_NEXT(current, len))
1898 {
1899 switch (current->nlmsg_type)
1900 {
1901 case NLMSG_DONE:
1902 break;
1903 case RTM_NEWROUTE:
1904 {
1905 rt_entry_t *other;
1906 uintptr_t table;
1907
1908 if (!route_usable(current, TRUE))
1909 {
1910 continue;
1911 }
1912 route = parse_route(current, route);
1913
1914 table = (uintptr_t)route->table;
1915 if (this->rt_exclude->find_first(this->rt_exclude, NULL,
1916 (void**)&table))
1917 { /* route is from an excluded routing table */
1918 continue;
1919 }
1920 if (this->routing_table != 0 &&
1921 route->table == this->routing_table)
1922 { /* route is from our own ipsec routing table */
1923 continue;
1924 }
1925 if (route->oif && !is_interface_up_and_usable(this, route->oif))
1926 { /* interface is down */
1927 continue;
1928 }
1929 if (!addr_in_subnet(chunk, prefix, route->dst, route->dst_len))
1930 { /* route destination does not contain dest */
1931 continue;
1932 }
1933 if (route->pref_src.ptr)
1934 { /* verify source address, if any */
1935 host_t *src = host_create_from_chunk(msg->rtm_family,
1936 route->pref_src, 0);
1937 if (src && is_known_vip(this, src))
1938 { /* ignore routes installed by us */
1939 src->destroy(src);
1940 continue;
1941 }
1942 route->src_host = src;
1943 }
1944 /* insert route, sorted by network prefix and priority */
1945 enumerator = routes->create_enumerator(routes);
1946 while (enumerator->enumerate(enumerator, &other))
1947 {
1948 if (route->dst_len > other->dst_len)
1949 {
1950 break;
1951 }
1952 if (route->dst_len == other->dst_len &&
1953 route->priority < other->priority)
1954 {
1955 break;
1956 }
1957 }
1958 routes->insert_before(routes, enumerator, route);
1959 enumerator->destroy(enumerator);
1960 route = NULL;
1961 continue;
1962 }
1963 default:
1964 continue;
1965 }
1966 break;
1967 }
1968 if (route)
1969 {
1970 rt_entry_destroy(route);
1971 }
1972
1973 /* now we have a list of routes matching dest, sorted by net prefix.
1974 * we will look for source addresses for these routes and select the one
1975 * with the preferred source address, if possible */
1976 enumerator = routes->create_enumerator(routes);
1977 while (enumerator->enumerate(enumerator, &route))
1978 {
1979 if (route->src_host)
1980 { /* got a source address with the route, if no preferred source
1981 * is given or it matches we are done, as this is the best route */
1982 if (!candidate || candidate->ip_equals(candidate, route->src_host))
1983 {
1984 best = route;
1985 break;
1986 }
1987 else if (route->oif)
1988 { /* no match yet, maybe it is assigned to the same interface */
1989 host_t *src = get_interface_address(this, route->oif,
1990 msg->rtm_family, dest, candidate);
1991 if (src && src->ip_equals(src, candidate))
1992 {
1993 route->src_host->destroy(route->src_host);
1994 route->src_host = src;
1995 best = route;
1996 break;
1997 }
1998 DESTROY_IF(src);
1999 }
2000 /* no luck yet with the source address. if this is the best (first)
2001 * route we store it as fallback in case we don't find a route with
2002 * the preferred source */
2003 best = best ?: route;
2004 continue;
2005 }
2006 if (route->src.ptr)
2007 { /* no src, but a source selector, try to find a matching address */
2008 route->src_host = get_subnet_address(this, msg->rtm_family,
2009 route->src, route->src_len, dest,
2010 candidate);
2011 if (route->src_host)
2012 { /* we handle this address the same as the one above */
2013 if (!candidate ||
2014 candidate->ip_equals(candidate, route->src_host))
2015 {
2016 best = route;
2017 break;
2018 }
2019 best = best ?: route;
2020 continue;
2021 }
2022 }
2023 if (route->oif)
2024 { /* no src, but an interface - get address from it */
2025 route->src_host = get_interface_address(this, route->oif,
2026 msg->rtm_family, dest, candidate);
2027 if (route->src_host)
2028 { /* more of the same */
2029 if (!candidate ||
2030 candidate->ip_equals(candidate, route->src_host))
2031 {
2032 best = route;
2033 break;
2034 }
2035 best = best ?: route;
2036 continue;
2037 }
2038 }
2039 if (route->gtw.ptr)
2040 { /* no src, no iface, but a gateway - lookup src to reach gtw */
2041 host_t *gtw;
2042
2043 gtw = host_create_from_chunk(msg->rtm_family, route->gtw, 0);
2044 if (gtw && !gtw->ip_equals(gtw, dest))
2045 {
2046 route->src_host = get_route(this, gtw, -1, FALSE, candidate,
2047 iface, recursion + 1);
2048 }
2049 DESTROY_IF(gtw);
2050 if (route->src_host)
2051 { /* more of the same */
2052 if (!candidate ||
2053 candidate->ip_equals(candidate, route->src_host))
2054 {
2055 best = route;
2056 break;
2057 }
2058 best = best ?: route;
2059 }
2060 }
2061 }
2062 enumerator->destroy(enumerator);
2063
2064 if (nexthop)
2065 { /* nexthop lookup, return gateway and oif if any */
2066 if (iface)
2067 {
2068 *iface = NULL;
2069 }
2070 if (best || routes->get_first(routes, (void**)&best) == SUCCESS)
2071 {
2072 addr = host_create_from_chunk(msg->rtm_family, best->gtw, 0);
2073 if (iface && best->oif)
2074 {
2075 *iface = get_interface_name_by_index(this, best->oif);
2076 }
2077 }
2078 if (!addr && !match_net)
2079 { /* fallback to destination address */
2080 addr = dest->clone(dest);
2081 }
2082 }
2083 else
2084 {
2085 if (best)
2086 {
2087 addr = best->src_host->clone(best->src_host);
2088 }
2089 }
2090 this->lock->unlock(this->lock);
2091 routes->destroy_function(routes, (void*)rt_entry_destroy);
2092 free(out);
2093
2094 if (addr)
2095 {
2096 if (nexthop && iface && *iface)
2097 {
2098 DBG2(DBG_KNL, "using %H as nexthop and %s as dev to reach %H/%d",
2099 addr, *iface, dest, prefix);
2100 }
2101 else
2102 {
2103 DBG2(DBG_KNL, "using %H as %s to reach %H/%d", addr,
2104 nexthop ? "nexthop" : "address", dest, prefix);
2105 }
2106 }
2107 else if (!recursion)
2108 {
2109 DBG2(DBG_KNL, "no %s found to reach %H/%d",
2110 nexthop ? "nexthop" : "address", dest, prefix);
2111 }
2112 return addr;
2113 }
2114
2115 METHOD(kernel_net_t, get_source_addr, host_t*,
2116 private_kernel_netlink_net_t *this, host_t *dest, host_t *src)
2117 {
2118 return get_route(this, dest, -1, FALSE, src, NULL, 0);
2119 }
2120
2121 METHOD(kernel_net_t, get_nexthop, host_t*,
2122 private_kernel_netlink_net_t *this, host_t *dest, int prefix, host_t *src,
2123 char **iface)
2124 {
2125 return get_route(this, dest, prefix, TRUE, src, iface, 0);
2126 }
2127
2128 /** enumerator over subnets */
2129 typedef struct {
2130 enumerator_t public;
2131 private_kernel_netlink_net_t *private;
2132 /** message from the kernel */
2133 struct nlmsghdr *msg;
2134 /** current message from the kernel */
2135 struct nlmsghdr *current;
2136 /** remaining length */
2137 size_t len;
2138 /** last subnet enumerated */
2139 host_t *net;
2140 /** interface of current net */
2141 char ifname[IFNAMSIZ];
2142 } subnet_enumerator_t;
2143
2144 METHOD(enumerator_t, destroy_subnet_enumerator, void,
2145 subnet_enumerator_t *this)
2146 {
2147 DESTROY_IF(this->net);
2148 free(this->msg);
2149 free(this);
2150 }
2151
2152 METHOD(enumerator_t, enumerate_subnets, bool,
2153 subnet_enumerator_t *this, va_list args)
2154 {
2155 host_t **net;
2156 uint8_t *mask;
2157 char **ifname;
2158
2159 VA_ARGS_VGET(args, net, mask, ifname);
2160
2161 if (!this->current)
2162 {
2163 this->current = this->msg;
2164 }
2165 else
2166 {
2167 this->current = NLMSG_NEXT(this->current, this->len);
2168 DESTROY_IF(this->net);
2169 this->net = NULL;
2170 }
2171
2172 while (NLMSG_OK(this->current, this->len))
2173 {
2174 switch (this->current->nlmsg_type)
2175 {
2176 case NLMSG_DONE:
2177 break;
2178 case RTM_NEWROUTE:
2179 {
2180 rt_entry_t route;
2181
2182 if (!route_usable(this->current, FALSE))
2183 {
2184 break;
2185 }
2186 parse_route(this->current, &route);
2187
2188 if (route.table && (
2189 route.table == RT_TABLE_LOCAL ||
2190 route.table == this->private->routing_table))
2191 { /* ignore our own and the local routing tables */
2192 break;
2193 }
2194 else if (route.gtw.ptr)
2195 { /* ignore routes via gateway/next hop */
2196 break;
2197 }
2198
2199 if (route.dst.ptr && route.oif &&
2200 if_indextoname(route.oif, this->ifname))
2201 {
2202 this->net = host_create_from_chunk(AF_UNSPEC, route.dst, 0);
2203 *net = this->net;
2204 *mask = route.dst_len;
2205 *ifname = this->ifname;
2206 return TRUE;
2207 }
2208 break;
2209 }
2210 default:
2211 break;
2212 }
2213 this->current = NLMSG_NEXT(this->current, this->len);
2214 }
2215 return FALSE;
2216 }
2217
2218 METHOD(kernel_net_t, create_local_subnet_enumerator, enumerator_t*,
2219 private_kernel_netlink_net_t *this)
2220 {
2221 netlink_buf_t request;
2222 struct nlmsghdr *hdr, *out;
2223 struct rtmsg *msg;
2224 size_t len;
2225 subnet_enumerator_t *enumerator;
2226
2227 memset(&request, 0, sizeof(request));
2228
2229 hdr = &request.hdr;
2230 hdr->nlmsg_flags = NLM_F_REQUEST;
2231 hdr->nlmsg_type = RTM_GETROUTE;
2232 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2233 hdr->nlmsg_flags |= NLM_F_DUMP;
2234
2235 msg = NLMSG_DATA(hdr);
2236 msg->rtm_scope = RT_SCOPE_LINK;
2237
2238 if (this->socket->send(this->socket, hdr, &out, &len) != SUCCESS)
2239 {
2240 DBG2(DBG_KNL, "enumerating local subnets failed");
2241 return enumerator_create_empty();
2242 }
2243
2244 INIT(enumerator,
2245 .public = {
2246 .enumerate = enumerator_enumerate_default,
2247 .venumerate = _enumerate_subnets,
2248 .destroy = _destroy_subnet_enumerator,
2249 },
2250 .private = this,
2251 .msg = out,
2252 .len = len,
2253 );
2254 return &enumerator->public;
2255 }
2256
2257 /**
2258 * Manages the creation and deletion of IPv6 address labels for virtual IPs.
2259 * By setting the appropriate nlmsg_type the label is either added or removed.
2260 */
2261 static status_t manage_addrlabel(private_kernel_netlink_net_t *this,
2262 int nlmsg_type, host_t *ip)
2263 {
2264 netlink_buf_t request;
2265 struct nlmsghdr *hdr;
2266 struct ifaddrlblmsg *msg;
2267 chunk_t chunk;
2268 uint32_t label;
2269
2270 memset(&request, 0, sizeof(request));
2271
2272 chunk = ip->get_address(ip);
2273
2274 hdr = &request.hdr;
2275 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2276 if (nlmsg_type == RTM_NEWADDRLABEL)
2277 {
2278 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
2279 }
2280 hdr->nlmsg_type = nlmsg_type;
2281 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg));
2282
2283 msg = NLMSG_DATA(hdr);
2284 msg->ifal_family = ip->get_family(ip);
2285 msg->ifal_prefixlen = chunk.len * 8;
2286
2287 netlink_add_attribute(hdr, IFAL_ADDRESS, chunk, sizeof(request));
2288 /* doesn't really matter as default labels are < 20 but this makes it kinda
2289 * recognizable */
2290 label = 220;
2291 netlink_add_attribute(hdr, IFAL_LABEL, chunk_from_thing(label),
2292 sizeof(request));
2293
2294 return this->socket->send_ack(this->socket, hdr);
2295 }
2296
2297 /**
2298 * Manages the creation and deletion of ip addresses on an interface.
2299 * By setting the appropriate nlmsg_type, the ip will be set or unset.
2300 */
2301 static status_t manage_ipaddr(private_kernel_netlink_net_t *this, int nlmsg_type,
2302 int flags, int if_index, host_t *ip, int prefix)
2303 {
2304 netlink_buf_t request;
2305 struct nlmsghdr *hdr;
2306 struct ifaddrmsg *msg;
2307 chunk_t chunk;
2308
2309 memset(&request, 0, sizeof(request));
2310
2311 chunk = ip->get_address(ip);
2312
2313 hdr = &request.hdr;
2314 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
2315 hdr->nlmsg_type = nlmsg_type;
2316 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
2317
2318 msg = NLMSG_DATA(hdr);
2319 msg->ifa_family = ip->get_family(ip);
2320 msg->ifa_flags = 0;
2321 msg->ifa_prefixlen = prefix < 0 ? chunk.len * 8 : prefix;
2322 msg->ifa_scope = RT_SCOPE_UNIVERSE;
2323 msg->ifa_index = if_index;
2324
2325 netlink_add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
2326
2327 if (ip->get_family(ip) == AF_INET6)
2328 {
2329 #ifdef IFA_F_NODAD
2330 msg->ifa_flags |= IFA_F_NODAD;
2331 #endif
2332 if (this->rta_prefsrc_for_ipv6)
2333 {
2334 /* if source routes are possible we set a label for this virtual IP
2335 * so it gets only used if forced by our route, and not by the
2336 * default IPv6 address selection */
2337 int labelop = nlmsg_type == RTM_NEWADDR ? RTM_NEWADDRLABEL
2338 : RTM_DELADDRLABEL;
2339 if (manage_addrlabel(this, labelop, ip) != SUCCESS)
2340 {
2341 /* if we can't use address labels we let the virtual IP get
2342 * deprecated immediately (but mark it as valid forever), which
2343 * should also avoid that it gets used by the default address
2344 * selection */
2345 struct ifa_cacheinfo cache = {
2346 .ifa_valid = 0xFFFFFFFF,
2347 .ifa_prefered = 0,
2348 };
2349 netlink_add_attribute(hdr, IFA_CACHEINFO,
2350 chunk_from_thing(cache), sizeof(request));
2351 }
2352 }
2353 }
2354 return this->socket->send_ack(this->socket, hdr);
2355 }
2356
2357 METHOD(kernel_net_t, add_ip, status_t,
2358 private_kernel_netlink_net_t *this, host_t *virtual_ip, int prefix,
2359 char *iface_name)
2360 {
2361 addr_map_entry_t *entry, lookup = {
2362 .ip = virtual_ip,
2363 };
2364 iface_entry_t *iface = NULL;
2365
2366 if (!this->install_virtual_ip)
2367 { /* disabled by config */
2368 return SUCCESS;
2369 }
2370
2371 this->lock->write_lock(this->lock);
2372 /* the virtual IP might actually be installed as regular IP, in which case
2373 * we don't track it as virtual IP */
2374 entry = this->addrs->get_match(this->addrs, &lookup,
2375 (void*)addr_map_entry_match);
2376 if (!entry)
2377 { /* otherwise it might already be installed as virtual IP */
2378 entry = this->vips->get_match(this->vips, &lookup,
2379 (void*)addr_map_entry_match);
2380 if (entry)
2381 { /* the vip we found can be in one of three states: 1) installed and
2382 * ready, 2) just added by another thread, but not yet confirmed to
2383 * be installed by the kernel, 3) just deleted, but not yet gone.
2384 * Then while we wait below, several things could happen (as we
2385 * release the lock). For instance, the interface could disappear,
2386 * or the IP is finally deleted, and it reappears on a different
2387 * interface. All these cases are handled by the call below. */
2388 while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
2389 {
2390 this->condvar->wait(this->condvar, this->lock);
2391 }
2392 if (entry)
2393 {
2394 entry->addr->refcount++;
2395 }
2396 }
2397 }
2398 if (entry)
2399 {
2400 DBG2(DBG_KNL, "virtual IP %H is already installed on %s", virtual_ip,
2401 entry->iface->ifname);
2402 this->lock->unlock(this->lock);
2403 return SUCCESS;
2404 }
2405 /* try to find the target interface, either by config or via src ip */
2406 if (!this->install_virtual_ip_on ||
2407 !this->ifaces->find_first(this->ifaces, iface_entry_by_name,
2408 (void**)&iface, this->install_virtual_ip_on))
2409 {
2410 if (!this->ifaces->find_first(this->ifaces, iface_entry_by_name,
2411 (void**)&iface, iface_name))
2412 { /* if we don't find the requested interface we just use the first */
2413 this->ifaces->get_first(this->ifaces, (void**)&iface);
2414 }
2415 }
2416 if (iface)
2417 {
2418 addr_entry_t *addr;
2419 char *ifname;
2420 int ifi;
2421
2422 INIT(addr,
2423 .ip = virtual_ip->clone(virtual_ip),
2424 .refcount = 1,
2425 .scope = RT_SCOPE_UNIVERSE,
2426 );
2427 iface->addrs->insert_last(iface->addrs, addr);
2428 addr_map_entry_add(this->vips, addr, iface);
2429 ifi = iface->ifindex;
2430 this->lock->unlock(this->lock);
2431 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
2432 ifi, virtual_ip, prefix) == SUCCESS)
2433 {
2434 this->lock->write_lock(this->lock);
2435 while (!is_vip_installed_or_gone(this, virtual_ip, &entry))
2436 { /* wait until address appears */
2437 this->condvar->wait(this->condvar, this->lock);
2438 }
2439 if (entry)
2440 { /* we fail if the interface got deleted in the meantime */
2441 ifname = strdup(entry->iface->ifname);
2442 this->lock->unlock(this->lock);
2443 DBG2(DBG_KNL, "virtual IP %H installed on %s",
2444 virtual_ip, ifname);
2445 /* during IKEv1 reauthentication, children get moved from
2446 * old the new SA before the virtual IP is available. This
2447 * kills the route for our virtual IP, reinstall. */
2448 queue_route_reinstall(this, ifname);
2449 return SUCCESS;
2450 }
2451 this->lock->unlock(this->lock);
2452 }
2453 DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
2454 return FAILED;
2455 }
2456 this->lock->unlock(this->lock);
2457 DBG1(DBG_KNL, "no interface available, unable to install virtual IP %H",
2458 virtual_ip);
2459 return FAILED;
2460 }
2461
2462 METHOD(kernel_net_t, del_ip, status_t,
2463 private_kernel_netlink_net_t *this, host_t *virtual_ip, int prefix,
2464 bool wait)
2465 {
2466 addr_map_entry_t *entry, lookup = {
2467 .ip = virtual_ip,
2468 };
2469
2470 if (!this->install_virtual_ip)
2471 { /* disabled by config */
2472 return SUCCESS;
2473 }
2474
2475 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
2476
2477 this->lock->write_lock(this->lock);
2478 entry = this->vips->get_match(this->vips, &lookup,
2479 (void*)addr_map_entry_match);
2480 if (!entry)
2481 { /* we didn't install this IP as virtual IP */
2482 entry = this->addrs->get_match(this->addrs, &lookup,
2483 (void*)addr_map_entry_match);
2484 if (entry)
2485 {
2486 DBG2(DBG_KNL, "not deleting existing IP %H on %s", virtual_ip,
2487 entry->iface->ifname);
2488 this->lock->unlock(this->lock);
2489 return SUCCESS;
2490 }
2491 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
2492 this->lock->unlock(this->lock);
2493 return FAILED;
2494 }
2495 if (entry->addr->refcount == 1)
2496 {
2497 status_t status;
2498 int ifi;
2499
2500 /* we set this flag so that threads calling add_ip will block and wait
2501 * until the entry is gone, also so we can wait below */
2502 entry->addr->installed = FALSE;
2503 ifi = entry->iface->ifindex;
2504 this->lock->unlock(this->lock);
2505 status = manage_ipaddr(this, RTM_DELADDR, 0, ifi, virtual_ip, prefix);
2506 if (status == SUCCESS && wait)
2507 { /* wait until the address is really gone */
2508 this->lock->write_lock(this->lock);
2509 while (is_known_vip(this, virtual_ip) &&
2510 lib->watcher->get_state(lib->watcher) != WATCHER_STOPPED)
2511 { /* don't wait during deinit when we can't get notified,
2512 * re-evaluate watcher state if we have to wait longer */
2513 this->condvar->timed_wait(this->condvar, this->lock, 1000);
2514 }
2515 this->lock->unlock(this->lock);
2516 }
2517 return status;
2518 }
2519 else
2520 {
2521 entry->addr->refcount--;
2522 }
2523 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
2524 virtual_ip);
2525 this->lock->unlock(this->lock);
2526 return SUCCESS;
2527 }
2528
2529 /**
2530 * Manages source routes in the routing table.
2531 * By setting the appropriate nlmsg_type, the route gets added or removed.
2532 */
2533 static status_t manage_srcroute(private_kernel_netlink_net_t *this,
2534 int nlmsg_type, int flags, chunk_t dst_net,
2535 uint8_t prefixlen, host_t *gateway,
2536 host_t *src_ip, char *if_name, bool pass)
2537 {
2538 netlink_buf_t request;
2539 struct nlmsghdr *hdr;
2540 struct rtmsg *msg;
2541 struct rtattr *rta;
2542 int ifindex;
2543 chunk_t chunk;
2544
2545 /* if route is 0.0.0.0/0, we can't install it, as it would
2546 * overwrite the default route. Instead, we add two routes:
2547 * 0.0.0.0/1 and 128.0.0.0/1 */
2548 if (this->routing_table == 0 && prefixlen == 0)
2549 {
2550 chunk_t half_net;
2551 uint8_t half_prefixlen;
2552 status_t status;
2553
2554 half_net = chunk_alloca(dst_net.len);
2555 memset(half_net.ptr, 0, half_net.len);
2556 half_prefixlen = 1;
2557 /* no throw routes in the main table */
2558 status = manage_srcroute(this, nlmsg_type, flags, half_net,
2559 half_prefixlen, gateway, src_ip, if_name, FALSE);
2560 half_net.ptr[0] |= 0x80;
2561 status |= manage_srcroute(this, nlmsg_type, flags, half_net,
2562 half_prefixlen, gateway, src_ip, if_name, FALSE);
2563 return status;
2564 }
2565
2566 memset(&request, 0, sizeof(request));
2567
2568 hdr = &request.hdr;
2569 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
2570 hdr->nlmsg_type = nlmsg_type;
2571 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2572
2573 msg = NLMSG_DATA(hdr);
2574 msg->rtm_family = (dst_net.len == 4) ? AF_INET : AF_INET6;
2575 msg->rtm_dst_len = prefixlen;
2576 msg->rtm_protocol = RTPROT_STATIC;
2577 msg->rtm_type = pass ? RTN_THROW : RTN_UNICAST;
2578 msg->rtm_scope = RT_SCOPE_UNIVERSE;
2579
2580 if (this->routing_table < 256)
2581 {
2582 msg->rtm_table = this->routing_table;
2583 }
2584 else
2585 {
2586 #ifdef HAVE_RTA_TABLE
2587 chunk = chunk_from_thing(this->routing_table);
2588 netlink_add_attribute(hdr, RTA_TABLE, chunk, sizeof(request));
2589 #else
2590 DBG1(DBG_KNL, "routing table IDs > 255 are not supported");
2591 return FAILED;
2592 #endif /* HAVE_RTA_TABLE */
2593 }
2594 netlink_add_attribute(hdr, RTA_DST, dst_net, sizeof(request));
2595
2596 /* only when installing regular routes do we need all the parameters,
2597 * deletes are done by destination net (except if metrics are used, which
2598 * we don't support), for throw routes we don't need any of them either */
2599 if (nlmsg_type == RTM_NEWROUTE && !pass)
2600 {
2601 chunk = src_ip->get_address(src_ip);
2602 netlink_add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
2603 if (gateway && gateway->get_family(gateway) == src_ip->get_family(src_ip))
2604 {
2605 chunk = gateway->get_address(gateway);
2606 netlink_add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
2607 }
2608 ifindex = get_interface_index(this, if_name);
2609 chunk.ptr = (char*)&ifindex;
2610 chunk.len = sizeof(ifindex);
2611 netlink_add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
2612
2613 if (this->mtu || this->mss)
2614 {
2615 chunk = chunk_alloca(RTA_LENGTH((sizeof(struct rtattr) +
2616 sizeof(uint32_t)) * 2));
2617 chunk.len = 0;
2618 rta = (struct rtattr*)chunk.ptr;
2619 if (this->mtu)
2620 {
2621 rta->rta_type = RTAX_MTU;
2622 rta->rta_len = RTA_LENGTH(sizeof(uint32_t));
2623 memcpy(RTA_DATA(rta), &this->mtu, sizeof(uint32_t));
2624 chunk.len = rta->rta_len;
2625 }
2626 if (this->mss)
2627 {
2628 rta = (struct rtattr*)(chunk.ptr + RTA_ALIGN(chunk.len));
2629 rta->rta_type = RTAX_ADVMSS;
2630 rta->rta_len = RTA_LENGTH(sizeof(uint32_t));
2631 memcpy(RTA_DATA(rta), &this->mss, sizeof(uint32_t));
2632 chunk.len = RTA_ALIGN(chunk.len) + rta->rta_len;
2633 }
2634 netlink_add_attribute(hdr, RTA_METRICS, chunk, sizeof(request));
2635 }
2636 }
2637 return this->socket->send_ack(this->socket, hdr);
2638 }
2639
2640 /**
2641 * Helper struct used to check routes
2642 */
2643 typedef struct {
2644 /** the entry we look for */
2645 route_entry_t route;
2646 /** kernel interface */
2647 private_kernel_netlink_net_t *this;
2648 } route_entry_lookup_t;
2649
2650 /**
2651 * Check if a matching route entry has a VIP associated
2652 */
2653 static bool route_with_vip(route_entry_lookup_t *a, route_entry_t *b)
2654 {
2655 if (chunk_equals(a->route.dst_net, b->dst_net) &&
2656 a->route.prefixlen == b->prefixlen &&
2657 is_known_vip(a->this, b->src_ip))
2658 {
2659 return TRUE;
2660 }
2661 return FALSE;
2662 }
2663
2664 /**
2665 * Check if there is any route entry with a matching destination
2666 */
2667 static bool route_with_dst(route_entry_lookup_t *a, route_entry_t *b)
2668 {
2669 if (chunk_equals(a->route.dst_net, b->dst_net) &&
2670 a->route.prefixlen == b->prefixlen)
2671 {
2672 return TRUE;
2673 }
2674 return FALSE;
2675 }
2676
2677 METHOD(kernel_net_t, add_route, status_t,
2678 private_kernel_netlink_net_t *this, chunk_t dst_net, uint8_t prefixlen,
2679 host_t *gateway, host_t *src_ip, char *if_name, bool pass)
2680 {
2681 status_t status;
2682 route_entry_t *found;
2683 route_entry_lookup_t lookup = {
2684 .route = {
2685 .dst_net = dst_net,
2686 .prefixlen = prefixlen,
2687 .gateway = gateway,
2688 .src_ip = src_ip,
2689 .if_name = if_name,
2690 .pass = pass,
2691 },
2692 .this = this,
2693 };
2694
2695 if (!this->routing_table)
2696 { /* treat these as regular routes if installing in the main table */
2697 pass = lookup.route.pass = FALSE;
2698 }
2699
2700 this->routes_lock->lock(this->routes_lock);
2701 found = this->routes->ht.get(&this->routes->ht, &lookup.route);
2702 if (found)
2703 {
2704 this->routes_lock->unlock(this->routes_lock);
2705 return ALREADY_DONE;
2706 }
2707
2708 /* don't replace the route if we already have one with a VIP installed,
2709 * but keep track of it in case that other route is uninstalled */
2710 this->lock->read_lock(this->lock);
2711 if (!is_known_vip(this, src_ip))
2712 {
2713 found = this->routes->get_match(this->routes, &lookup,
2714 (void*)route_with_vip);
2715 }
2716 this->lock->unlock(this->lock);
2717 if (found)
2718 {
2719 status = SUCCESS;
2720 }
2721 else
2722 {
2723 status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
2724 dst_net, prefixlen, gateway, src_ip, if_name,
2725 pass);
2726 }
2727 if (status == SUCCESS)
2728 {
2729 found = route_entry_clone(&lookup.route);
2730 this->routes->ht.put(&this->routes->ht, found, found);
2731 }
2732 this->routes_lock->unlock(this->routes_lock);
2733 return status;
2734 }
2735
2736 METHOD(kernel_net_t, del_route, status_t,
2737 private_kernel_netlink_net_t *this, chunk_t dst_net, uint8_t prefixlen,
2738 host_t *gateway, host_t *src_ip, char *if_name, bool pass)
2739 {
2740 status_t status;
2741 route_entry_t *found;
2742 route_entry_lookup_t lookup = {
2743 .route = {
2744 .dst_net = dst_net,
2745 .prefixlen = prefixlen,
2746 .gateway = gateway,
2747 .src_ip = src_ip,
2748 .if_name = if_name,
2749 .pass = pass,
2750 },
2751 .this = this,
2752 };
2753
2754 if (!this->routing_table)
2755 { /* treat these as regular routes if installing in the main table */
2756 pass = lookup.route.pass = FALSE;
2757 }
2758
2759 this->routes_lock->lock(this->routes_lock);
2760 found = this->routes->ht.remove(&this->routes->ht, &lookup.route);
2761 if (!found)
2762 {
2763 this->routes_lock->unlock(this->routes_lock);
2764 return NOT_FOUND;
2765 }
2766 route_entry_destroy(found);
2767
2768 /* check if there are any other routes for the same destination and if
2769 * so update the route, otherwise uninstall it */
2770 this->lock->read_lock(this->lock);
2771 found = this->routes->get_match(this->routes, &lookup,
2772 (void*)route_with_vip);
2773 this->lock->unlock(this->lock);
2774 if (!found)
2775 {
2776 found = this->routes->get_match(this->routes, &lookup,
2777 (void*)route_with_dst);
2778 }
2779 if (found)
2780 {
2781 status = manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
2782 found->dst_net, found->prefixlen, found->gateway,
2783 found->src_ip, found->if_name, found->pass);
2784 }
2785 else
2786 {
2787 status = manage_srcroute(this, RTM_DELROUTE, 0, dst_net, prefixlen,
2788 gateway, src_ip, if_name, pass);
2789 }
2790 this->routes_lock->unlock(this->routes_lock);
2791 return status;
2792 }
2793
2794 /**
2795 * Initialize a list of local addresses.
2796 */
2797 static status_t init_address_list(private_kernel_netlink_net_t *this)
2798 {
2799 netlink_buf_t request;
2800 struct nlmsghdr *out, *current, *in;
2801 struct rtgenmsg *msg;
2802 size_t len;
2803 enumerator_t *ifaces, *addrs;
2804 iface_entry_t *iface;
2805 addr_entry_t *addr;
2806
2807 DBG2(DBG_KNL, "known interfaces and IP addresses:");
2808
2809 memset(&request, 0, sizeof(request));
2810
2811 in = &request.hdr;
2812 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
2813 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
2814 msg = NLMSG_DATA(in);
2815 msg->rtgen_family = AF_UNSPEC;
2816
2817 /* get all links */
2818 in->nlmsg_type = RTM_GETLINK;
2819 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
2820 {
2821 return FAILED;
2822 }
2823 current = out;
2824 while (NLMSG_OK(current, len))
2825 {
2826 switch (current->nlmsg_type)
2827 {
2828 case NLMSG_DONE:
2829 break;
2830 case RTM_NEWLINK:
2831 process_link(this, current, FALSE);
2832 /* fall through */
2833 default:
2834 current = NLMSG_NEXT(current, len);
2835 continue;
2836 }
2837 break;
2838 }
2839 free(out);
2840
2841 /* get all interface addresses */
2842 in->nlmsg_type = RTM_GETADDR;
2843 if (this->socket->send(this->socket, in, &out, &len) != SUCCESS)
2844 {
2845 return FAILED;
2846 }
2847 current = out;
2848 while (NLMSG_OK(current, len))
2849 {
2850 switch (current->nlmsg_type)
2851 {
2852 case NLMSG_DONE:
2853 break;
2854 case RTM_NEWADDR:
2855 process_addr(this, current, FALSE);
2856 /* fall through */
2857 default:
2858 current = NLMSG_NEXT(current, len);
2859 continue;
2860 }
2861 break;
2862 }
2863 free(out);
2864
2865 this->lock->read_lock(this->lock);
2866 ifaces = this->ifaces->create_enumerator(this->ifaces);
2867 while (ifaces->enumerate(ifaces, &iface))
2868 {
2869 if (iface_entry_up_and_usable(iface))
2870 {
2871 DBG2(DBG_KNL, " %s", iface->ifname);
2872 addrs = iface->addrs->create_enumerator(iface->addrs);
2873 while (addrs->enumerate(addrs, (void**)&addr))
2874 {
2875 DBG2(DBG_KNL, " %H", addr->ip);
2876 }
2877 addrs->destroy(addrs);
2878 }
2879 else
2880 {
2881 DBG3(DBG_KNL, " %s (ignored, %s)", iface->ifname,
2882 iface->usable ? "down" : "configuration");
2883 }
2884 }
2885 ifaces->destroy(ifaces);
2886 this->lock->unlock(this->lock);
2887 return SUCCESS;
2888 }
2889
2890 /**
2891 * create or delete a rule to use our routing table
2892 */
2893 static status_t manage_rule(private_kernel_netlink_net_t *this, int nlmsg_type,
2894 int family, uint32_t table, uint32_t prio)
2895 {
2896 netlink_buf_t request;
2897 struct nlmsghdr *hdr;
2898 struct rtmsg *msg;
2899 chunk_t chunk;
2900 char *fwmark;
2901
2902 memset(&request, 0, sizeof(request));
2903 hdr = &request.hdr;
2904 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2905 hdr->nlmsg_type = nlmsg_type;
2906 if (nlmsg_type == RTM_NEWRULE)
2907 {
2908 hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
2909 }
2910 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2911
2912 msg = NLMSG_DATA(hdr);
2913 msg->rtm_family = family;
2914 msg->rtm_protocol = RTPROT_BOOT;
2915 msg->rtm_scope = RT_SCOPE_UNIVERSE;
2916 msg->rtm_type = RTN_UNICAST;
2917
2918 if (this->routing_table < 256)
2919 {
2920 msg->rtm_table = table;
2921 }
2922 else
2923 {
2924 #ifdef HAVE_LINUX_FIB_RULES_H
2925 chunk = chunk_from_thing(table);
2926 netlink_add_attribute(hdr, FRA_TABLE, chunk, sizeof(request));
2927 #else
2928 DBG1(DBG_KNL, "routing table IDs > 255 are not supported");
2929 return FAILED;
2930 #endif /* HAVE_LINUX_FIB_RULES_H */
2931 }
2932 chunk = chunk_from_thing(prio);
2933 netlink_add_attribute(hdr, RTA_PRIORITY, chunk, sizeof(request));
2934
2935 fwmark = lib->settings->get_str(lib->settings,
2936 "%s.plugins.kernel-netlink.fwmark", NULL, lib->ns);
2937 if (fwmark)
2938 {
2939 #ifdef HAVE_LINUX_FIB_RULES_H
2940 mark_t mark;
2941
2942 if (fwmark[0] == '!')
2943 {
2944 msg->rtm_flags |= FIB_RULE_INVERT;
2945 fwmark++;
2946 }
2947 if (mark_from_string(fwmark, MARK_OP_NONE, &mark))
2948 {
2949 chunk = chunk_from_thing(mark.value);
2950 netlink_add_attribute(hdr, FRA_FWMARK, chunk, sizeof(request));
2951 chunk = chunk_from_thing(mark.mask);
2952 netlink_add_attribute(hdr, FRA_FWMASK, chunk, sizeof(request));
2953 if (msg->rtm_flags & FIB_RULE_INVERT)
2954 {
2955 this->routing_mark = mark;
2956 }
2957 }
2958 #else
2959 DBG1(DBG_KNL, "setting firewall mark on routing rule is not supported");
2960 #endif /* HAVE_LINUX_FIB_RULES_H */
2961 }
2962 return this->socket->send_ack(this->socket, hdr);
2963 }
2964
2965 /**
2966 * check for kernel features (currently only via version number)
2967 */
2968 static void check_kernel_features(private_kernel_netlink_net_t *this)
2969 {
2970 struct utsname utsname;
2971 int a, b, c;
2972
2973 if (uname(&utsname) == 0)
2974 {
2975 switch(sscanf(utsname.release, "%d.%d.%d", &a, &b, &c))
2976 {
2977 case 3:
2978 if (a == 2)
2979 {
2980 if (b == 6 && c >= 36)
2981 {
2982 this->rta_mark = TRUE;
2983 }
2984 DBG2(DBG_KNL, "detected Linux %d.%d.%d, no support for "
2985 "RTA_PREFSRC for IPv6 routes", a, b, c);
2986 break;
2987 }
2988 /* fall-through */
2989 case 2:
2990 /* only 3.x+ uses two part version numbers */
2991 this->rta_prefsrc_for_ipv6 = TRUE;
2992 this->rta_mark = TRUE;
2993 break;
2994 default:
2995 break;
2996 }
2997 }
2998 }
2999
3000 /**
3001 * Destroy an address to iface map
3002 */
3003 static void addr_map_destroy(hashlist_t *map)
3004 {
3005 map->ht.destroy_function(&map->ht, (void*)free);
3006 }
3007
3008 METHOD(kernel_net_t, destroy, void,
3009 private_kernel_netlink_net_t *this)
3010 {
3011 enumerator_t *enumerator;
3012 route_entry_t *route;
3013
3014 if (this->routing_table && this->socket)
3015 {
3016 manage_rule(this, RTM_DELRULE, AF_INET, this->routing_table,
3017 this->routing_table_prio);
3018 manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
3019 this->routing_table_prio);
3020 }
3021 DESTROY_IF(this->socket_events);
3022 enumerator = this->routes->ht.create_enumerator(&this->routes->ht);
3023 while (enumerator->enumerate(enumerator, NULL, (void**)&route))
3024 {
3025 manage_srcroute(this, RTM_DELROUTE, 0, route->dst_net, route->prefixlen,
3026 route->gateway, route->src_ip, route->if_name,
3027 route->pass);
3028 route_entry_destroy(route);
3029 }
3030 enumerator->destroy(enumerator);
3031 this->routes->destroy(this->routes);
3032 this->routes_lock->destroy(this->routes_lock);
3033 DESTROY_IF(this->socket);
3034
3035 net_changes_clear(this);
3036 this->net_changes->destroy(this->net_changes);
3037 this->net_changes_lock->destroy(this->net_changes_lock);
3038
3039 addr_map_destroy(this->addrs);
3040 addr_map_destroy(this->vips);
3041
3042 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
3043 this->rt_exclude->destroy(this->rt_exclude);
3044 this->roam_lock->destroy(this->roam_lock);
3045 this->condvar->destroy(this->condvar);
3046 this->lock->destroy(this->lock);
3047 free(this);
3048 }
3049
3050 /*
3051 * Described in header.
3052 */
3053 kernel_netlink_net_t *kernel_netlink_net_create()
3054 {
3055 private_kernel_netlink_net_t *this;
3056 enumerator_t *enumerator;
3057 uint32_t groups;
3058 char *exclude;
3059
3060 INIT(this,
3061 .public = {
3062 .interface = {
3063 .get_interface = _get_interface_name,
3064 .create_address_enumerator = _create_address_enumerator,
3065 .create_local_subnet_enumerator = _create_local_subnet_enumerator,
3066 .get_source_addr = _get_source_addr,
3067 .get_nexthop = _get_nexthop,
3068 .add_ip = _add_ip,
3069 .del_ip = _del_ip,
3070 .add_route = _add_route,
3071 .del_route = _del_route,
3072 .destroy = _destroy,
3073 },
3074 },
3075 .socket = netlink_socket_create(NETLINK_ROUTE, rt_msg_names,
3076 lib->settings->get_bool(lib->settings,
3077 "%s.plugins.kernel-netlink.parallel_route", FALSE, lib->ns)),
3078 .rt_exclude = linked_list_create(),
3079 .routes = hashlist_create((hashtable_hash_t)route_entry_hash,
3080 (hashtable_equals_t)route_entry_equals, 16),
3081 .net_changes = hashtable_create(
3082 (hashtable_hash_t)net_change_hash,
3083 (hashtable_equals_t)net_change_equals, 16),
3084 .addrs = hashlist_create(
3085 (hashtable_hash_t)addr_map_entry_hash,
3086 (hashtable_equals_t)addr_map_entry_equals, 16),
3087 .vips = hashlist_create((hashtable_hash_t)addr_map_entry_hash,
3088 (hashtable_equals_t)addr_map_entry_equals, 16),
3089 .routes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
3090 .net_changes_lock = mutex_create(MUTEX_TYPE_DEFAULT),
3091 .ifaces = linked_list_create(),
3092 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
3093 .condvar = rwlock_condvar_create(),
3094 .roam_lock = spinlock_create(),
3095 .routing_table = lib->settings->get_int(lib->settings,
3096 "%s.routing_table", ROUTING_TABLE, lib->ns),
3097 .routing_table_prio = lib->settings->get_int(lib->settings,
3098 "%s.routing_table_prio", ROUTING_TABLE_PRIO, lib->ns),
3099 .process_route = lib->settings->get_bool(lib->settings,
3100 "%s.process_route", TRUE, lib->ns),
3101 .install_routes = lib->settings->get_bool(lib->settings,
3102 "%s.install_routes", TRUE, lib->ns),
3103 .install_virtual_ip = lib->settings->get_bool(lib->settings,
3104 "%s.install_virtual_ip", TRUE, lib->ns),
3105 .install_virtual_ip_on = lib->settings->get_str(lib->settings,
3106 "%s.install_virtual_ip_on", NULL, lib->ns),
3107 .prefer_temporary_addrs = lib->settings->get_bool(lib->settings,
3108 "%s.prefer_temporary_addrs", FALSE, lib->ns),
3109 .roam_events = lib->settings->get_bool(lib->settings,
3110 "%s.plugins.kernel-netlink.roam_events", TRUE, lib->ns),
3111 .process_rules = lib->settings->get_bool(lib->settings,
3112 "%s.plugins.kernel-netlink.process_rules", FALSE, lib->ns),
3113 .mtu = lib->settings->get_int(lib->settings,
3114 "%s.plugins.kernel-netlink.mtu", 0, lib->ns),
3115 .mss = lib->settings->get_int(lib->settings,
3116 "%s.plugins.kernel-netlink.mss", 0, lib->ns),
3117 );
3118 timerclear(&this->last_route_reinstall);
3119 timerclear(&this->next_roam);
3120
3121 check_kernel_features(this);
3122
3123 if (!this->socket)
3124 {
3125 destroy(this);
3126 return NULL;
3127 }
3128
3129 exclude = lib->settings->get_str(lib->settings,
3130 "%s.ignore_routing_tables", NULL, lib->ns);
3131 if (exclude)
3132 {
3133 char *token;
3134 uintptr_t table;
3135
3136 enumerator = enumerator_create_token(exclude, " ", " ");
3137 while (enumerator->enumerate(enumerator, &token))
3138 {
3139 errno = 0;
3140 table = strtoul(token, NULL, 10);
3141
3142 if (errno == 0)
3143 {
3144 this->rt_exclude->insert_last(this->rt_exclude, (void*)table);
3145 }
3146 }
3147 enumerator->destroy(enumerator);
3148 }
3149
3150 groups = nl_group(RTNLGRP_IPV4_IFADDR) |
3151 nl_group(RTNLGRP_IPV6_IFADDR) |
3152 nl_group(RTNLGRP_LINK);
3153 if (this->process_route)
3154 {
3155 groups |= nl_group(RTNLGRP_IPV4_ROUTE) |
3156 nl_group(RTNLGRP_IPV6_ROUTE);
3157 }
3158 if (this->process_rules)
3159 {
3160 groups |= nl_group(RTNLGRP_IPV4_RULE) |
3161 nl_group(RTNLGRP_IPV6_RULE);
3162 }
3163 this->socket_events = netlink_event_socket_create(NETLINK_ROUTE, groups,
3164 receive_events, this);
3165 if (!this->socket_events)
3166 {
3167 destroy(this);
3168 return NULL;
3169 }
3170
3171 if (init_address_list(this) != SUCCESS)
3172 {
3173 DBG1(DBG_KNL, "unable to get interface list");
3174 destroy(this);
3175 return NULL;
3176 }
3177
3178 if (this->routing_table)
3179 {
3180 if (manage_rule(this, RTM_NEWRULE, AF_INET, this->routing_table,
3181 this->routing_table_prio) != SUCCESS)
3182 {
3183 DBG1(DBG_KNL, "unable to create IPv4 routing table rule");
3184 }
3185 if (manage_rule(this, RTM_NEWRULE, AF_INET6, this->routing_table,
3186 this->routing_table_prio) != SUCCESS)
3187 {
3188 DBG1(DBG_KNL, "unable to create IPv6 routing table rule");
3189 }
3190 }
3191
3192 return &this->public;
3193 }