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