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