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