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