Removed strayed code fragment
[strongswan.git] / src / charon / plugins / kernel_pfroute / kernel_pfroute_net.c
1 /*
2 * Copyright (C) 2009 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <net/if.h>
19 #include <ifaddrs.h>
20 #include <net/route.h>
21 #include <unistd.h>
22 #include <errno.h>
23
24 #include "kernel_pfroute_net.h"
25
26 #include <daemon.h>
27 #include <utils/host.h>
28 #include <threading/thread.h>
29 #include <threading/mutex.h>
30 #include <utils/linked_list.h>
31 #include <processing/jobs/callback_job.h>
32 #include <processing/jobs/roam_job.h>
33
34 #ifndef HAVE_STRUCT_SOCKADDR_SA_LEN
35 #error Cannot compile this plugin on systems where 'struct sockaddr' has no sa_len member.
36 #endif
37
38 /** delay before firing roam jobs (ms) */
39 #define ROAM_DELAY 100
40
41 /** buffer size for PF_ROUTE messages */
42 #define PFROUTE_BUFFER_SIZE 4096
43
44 typedef struct addr_entry_t addr_entry_t;
45
46 /**
47 * IP address in an inface_entry_t
48 */
49 struct addr_entry_t {
50
51 /** The ip address */
52 host_t *ip;
53
54 /** virtual IP managed by us */
55 bool virtual;
56
57 /** Number of times this IP is used, if virtual */
58 u_int refcount;
59 };
60
61 /**
62 * destroy a addr_entry_t object
63 */
64 static void addr_entry_destroy(addr_entry_t *this)
65 {
66 this->ip->destroy(this->ip);
67 free(this);
68 }
69
70 typedef struct iface_entry_t iface_entry_t;
71
72 /**
73 * A network interface on this system, containing addr_entry_t's
74 */
75 struct iface_entry_t {
76
77 /** interface index */
78 int ifindex;
79
80 /** name of the interface */
81 char ifname[IFNAMSIZ];
82
83 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
84 u_int flags;
85
86 /** list of addresses as host_t */
87 linked_list_t *addrs;
88 };
89
90 /**
91 * destroy an interface entry
92 */
93 static void iface_entry_destroy(iface_entry_t *this)
94 {
95 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
96 free(this);
97 }
98
99
100 typedef struct private_kernel_pfroute_net_t private_kernel_pfroute_net_t;
101
102 /**
103 * Private variables and functions of kernel_pfroute class.
104 */
105 struct private_kernel_pfroute_net_t
106 {
107 /**
108 * Public part of the kernel_pfroute_t object.
109 */
110 kernel_pfroute_net_t public;
111
112 /**
113 * mutex to lock access to various lists
114 */
115 mutex_t *mutex;
116
117 /**
118 * Cached list of interfaces and their addresses (iface_entry_t)
119 */
120 linked_list_t *ifaces;
121
122 /**
123 * job receiving PF_ROUTE events
124 */
125 callback_job_t *job;
126
127 /**
128 * mutex to lock access to the PF_ROUTE socket
129 */
130 mutex_t *mutex_pfroute;
131
132 /**
133 * PF_ROUTE socket to communicate with the kernel
134 */
135 int socket;
136
137 /**
138 * PF_ROUTE socket to receive events
139 */
140 int socket_events;
141
142 /**
143 * sequence number for messages sent to the kernel
144 */
145 int seq;
146
147 /**
148 * time of last roam job
149 */
150 timeval_t last_roam;
151 };
152
153 /**
154 * Start a roaming job. We delay it a bit and fire only one job
155 * for multiple events. Otherwise we would create too many jobs.
156 */
157 static void fire_roam_job(private_kernel_pfroute_net_t *this, bool address)
158 {
159 timeval_t now;
160
161 time_monotonic(&now);
162 if (timercmp(&now, &this->last_roam, >))
163 {
164 now.tv_usec += ROAM_DELAY * 1000;
165 while (now.tv_usec > 1000000)
166 {
167 now.tv_sec++;
168 now.tv_usec -= 1000000;
169 }
170 this->last_roam = now;
171 charon->scheduler->schedule_job_ms(charon->scheduler,
172 (job_t*)roam_job_create(address), ROAM_DELAY);
173 }
174 }
175
176 /**
177 * Process an RTM_*ADDR message from the kernel
178 */
179 static void process_addr(private_kernel_pfroute_net_t *this,
180 struct rt_msghdr *msg)
181 {
182 struct ifa_msghdr *ifa = (struct ifa_msghdr*)msg;
183 sockaddr_t *sockaddr = (sockaddr_t*)(ifa + 1);
184 host_t *host = NULL;
185 enumerator_t *ifaces, *addrs;
186 iface_entry_t *iface;
187 addr_entry_t *addr;
188 bool found = FALSE, changed = FALSE, roam = FALSE;
189 int i;
190
191 for (i = 1; i < (1 << RTAX_MAX); i <<= 1)
192 {
193 if (ifa->ifam_addrs & i)
194 {
195 if (RTA_IFA & i)
196 {
197 host = host_create_from_sockaddr(sockaddr);
198 break;
199 }
200 sockaddr = (sockaddr_t*)((char*)sockaddr + sockaddr->sa_len);
201 }
202 }
203
204 if (!host)
205 {
206 return;
207 }
208
209 this->mutex->lock(this->mutex);
210 ifaces = this->ifaces->create_enumerator(this->ifaces);
211 while (ifaces->enumerate(ifaces, &iface))
212 {
213 if (iface->ifindex == ifa->ifam_index)
214 {
215 addrs = iface->addrs->create_enumerator(iface->addrs);
216 while (addrs->enumerate(addrs, &addr))
217 {
218 if (host->ip_equals(host, addr->ip))
219 {
220 found = TRUE;
221 if (ifa->ifam_type == RTM_DELADDR)
222 {
223 iface->addrs->remove_at(iface->addrs, addrs);
224 if (!addr->virtual)
225 {
226 changed = TRUE;
227 DBG1(DBG_KNL, "%H disappeared from %s",
228 host, iface->ifname);
229 }
230 addr_entry_destroy(addr);
231 }
232 else if (ifa->ifam_type == RTM_NEWADDR && addr->virtual)
233 {
234 addr->refcount = 1;
235 }
236 }
237 }
238 addrs->destroy(addrs);
239
240 if (!found && ifa->ifam_type == RTM_NEWADDR)
241 {
242 changed = TRUE;
243 addr = malloc_thing(addr_entry_t);
244 addr->ip = host->clone(host);
245 addr->virtual = FALSE;
246 addr->refcount = 1;
247 iface->addrs->insert_last(iface->addrs, addr);
248 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
249 }
250
251 if (changed && (iface->flags & IFF_UP))
252 {
253 roam = TRUE;
254 }
255 break;
256 }
257 }
258 ifaces->destroy(ifaces);
259 this->mutex->unlock(this->mutex);
260 host->destroy(host);
261
262 if (roam)
263 {
264 fire_roam_job(this, TRUE);
265 }
266 }
267
268 /**
269 * Process an RTM_IFINFO message from the kernel
270 */
271 static void process_link(private_kernel_pfroute_net_t *this,
272 struct rt_msghdr *hdr)
273 {
274 struct if_msghdr *msg = (struct if_msghdr*)hdr;
275 enumerator_t *enumerator;
276 iface_entry_t *iface;
277 bool roam = FALSE;
278
279 if (msg->ifm_flags & IFF_LOOPBACK)
280 { /* ignore loopback interfaces */
281 return;
282 }
283
284 this->mutex->lock(this->mutex);
285 enumerator = this->ifaces->create_enumerator(this->ifaces);
286 while (enumerator->enumerate(enumerator, &iface))
287 {
288 if (iface->ifindex == msg->ifm_index)
289 {
290 if (!(iface->flags & IFF_UP) && (msg->ifm_flags & IFF_UP))
291 {
292 roam = TRUE;
293 DBG1(DBG_KNL, "interface %s activated", iface->ifname);
294 }
295 else if ((iface->flags & IFF_UP) && !(msg->ifm_flags & IFF_UP))
296 {
297 roam = TRUE;
298 DBG1(DBG_KNL, "interface %s deactivated", iface->ifname);
299 }
300 iface->flags = msg->ifm_flags;
301 break;
302 }
303 }
304 enumerator->destroy(enumerator);
305 this->mutex->unlock(this->mutex);
306
307 if (roam)
308 {
309 fire_roam_job(this, TRUE);
310 }
311 }
312
313 /**
314 * Process an RTM_*ROUTE message from the kernel
315 */
316 static void process_route(private_kernel_pfroute_net_t *this,
317 struct rt_msghdr *msg)
318 {
319
320 }
321
322 /**
323 * Receives events from kernel
324 */
325 static job_requeue_t receive_events(private_kernel_pfroute_net_t *this)
326 {
327 unsigned char buf[PFROUTE_BUFFER_SIZE];
328 struct rt_msghdr *msg = (struct rt_msghdr*)buf;
329 int len;
330 bool oldstate;
331
332 oldstate = thread_cancelability(TRUE);
333 len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0);
334 thread_cancelability(oldstate);
335
336 if (len < 0)
337 {
338 switch (errno)
339 {
340 case EINTR:
341 /* interrupted, try again */
342 return JOB_REQUEUE_DIRECT;
343 case EAGAIN:
344 /* no data ready, select again */
345 return JOB_REQUEUE_DIRECT;
346 default:
347 DBG1(DBG_KNL, "unable to receive from PF_ROUTE event socket");
348 sleep(1);
349 return JOB_REQUEUE_FAIR;
350 }
351 }
352
353 if (len < sizeof(msg->rtm_msglen) || len < msg->rtm_msglen ||
354 msg->rtm_version != RTM_VERSION)
355 {
356 DBG2(DBG_KNL, "received corrupted PF_ROUTE message");
357 return JOB_REQUEUE_DIRECT;
358 }
359
360 switch (msg->rtm_type)
361 {
362 case RTM_NEWADDR:
363 case RTM_DELADDR:
364 process_addr(this, msg);
365 break;
366 case RTM_IFINFO:
367 /*case RTM_IFANNOUNCE <- what about this*/
368 process_link(this, msg);
369 break;
370 case RTM_ADD:
371 case RTM_DELETE:
372 process_route(this, msg);
373 default:
374 break;
375 }
376
377 return JOB_REQUEUE_DIRECT;
378 }
379
380
381 /** enumerator over addresses */
382 typedef struct {
383 private_kernel_pfroute_net_t* this;
384 /** whether to enumerate down interfaces */
385 bool include_down_ifaces;
386 /** whether to enumerate virtual ip addresses */
387 bool include_virtual_ips;
388 } address_enumerator_t;
389
390 /**
391 * cleanup function for address enumerator
392 */
393 static void address_enumerator_destroy(address_enumerator_t *data)
394 {
395 data->this->mutex->unlock(data->this->mutex);
396 free(data);
397 }
398
399 /**
400 * filter for addresses
401 */
402 static bool filter_addresses(address_enumerator_t *data, addr_entry_t** in, host_t** out)
403 {
404 host_t *ip;
405 if (!data->include_virtual_ips && (*in)->virtual)
406 { /* skip virtual interfaces added by us */
407 return FALSE;
408 }
409 ip = (*in)->ip;
410 if (ip->get_family(ip) == AF_INET6)
411 {
412 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ip->get_sockaddr(ip);
413 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
414 { /* skip addresses with a unusable scope */
415 return FALSE;
416 }
417 }
418 *out = ip;
419 return TRUE;
420 }
421
422 /**
423 * enumerator constructor for interfaces
424 */
425 static enumerator_t *create_iface_enumerator(iface_entry_t *iface, address_enumerator_t *data)
426 {
427 return enumerator_create_filter(iface->addrs->create_enumerator(iface->addrs),
428 (void*)filter_addresses, data, NULL);
429 }
430
431 /**
432 * filter for interfaces
433 */
434 static bool filter_interfaces(address_enumerator_t *data, iface_entry_t** in, iface_entry_t** out)
435 {
436 if (!data->include_down_ifaces && !((*in)->flags & IFF_UP))
437 { /* skip interfaces not up */
438 return FALSE;
439 }
440 *out = *in;
441 return TRUE;
442 }
443
444 /**
445 * implementation of kernel_net_t.create_address_enumerator
446 */
447 static enumerator_t *create_address_enumerator(private_kernel_pfroute_net_t *this,
448 bool include_down_ifaces, bool include_virtual_ips)
449 {
450 address_enumerator_t *data = malloc_thing(address_enumerator_t);
451 data->this = this;
452 data->include_down_ifaces = include_down_ifaces;
453 data->include_virtual_ips = include_virtual_ips;
454
455 this->mutex->lock(this->mutex);
456 return enumerator_create_nested(
457 enumerator_create_filter(this->ifaces->create_enumerator(this->ifaces),
458 (void*)filter_interfaces, data, NULL),
459 (void*)create_iface_enumerator, data, (void*)address_enumerator_destroy);
460 }
461
462 /**
463 * implementation of kernel_net_t.get_interface_name
464 */
465 static char *get_interface_name(private_kernel_pfroute_net_t *this, host_t* ip)
466 {
467 enumerator_t *ifaces, *addrs;
468 iface_entry_t *iface;
469 addr_entry_t *addr;
470 char *name = NULL;
471
472 DBG2(DBG_KNL, "getting interface name for %H", ip);
473
474 this->mutex->lock(this->mutex);
475 ifaces = this->ifaces->create_enumerator(this->ifaces);
476 while (ifaces->enumerate(ifaces, &iface))
477 {
478 addrs = iface->addrs->create_enumerator(iface->addrs);
479 while (addrs->enumerate(addrs, &addr))
480 {
481 if (ip->ip_equals(ip, addr->ip))
482 {
483 name = strdup(iface->ifname);
484 break;
485 }
486 }
487 addrs->destroy(addrs);
488 if (name)
489 {
490 break;
491 }
492 }
493 ifaces->destroy(ifaces);
494 this->mutex->unlock(this->mutex);
495
496 if (name)
497 {
498 DBG2(DBG_KNL, "%H is on interface %s", ip, name);
499 }
500 else
501 {
502 DBG2(DBG_KNL, "%H is not a local address", ip);
503 }
504 return name;
505 }
506
507 /**
508 * Implementation of kernel_net_t.get_source_addr.
509 */
510 static host_t* get_source_addr(private_kernel_pfroute_net_t *this,
511 host_t *dest, host_t *src)
512 {
513 return NULL;
514 }
515
516 /**
517 * Implementation of kernel_net_t.get_nexthop.
518 */
519 static host_t* get_nexthop(private_kernel_pfroute_net_t *this, host_t *dest)
520 {
521 return NULL;
522 }
523
524 /**
525 * Implementation of kernel_net_t.add_ip.
526 */
527 static status_t add_ip(private_kernel_pfroute_net_t *this,
528 host_t *virtual_ip, host_t *iface_ip)
529 {
530 return FAILED;
531 }
532
533 /**
534 * Implementation of kernel_net_t.del_ip.
535 */
536 static status_t del_ip(private_kernel_pfroute_net_t *this, host_t *virtual_ip)
537 {
538 return FAILED;
539 }
540
541 /**
542 * Implementation of kernel_net_t.add_route.
543 */
544 static status_t add_route(private_kernel_pfroute_net_t *this, chunk_t dst_net,
545 u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
546 {
547 return FAILED;
548 }
549
550 /**
551 * Implementation of kernel_net_t.del_route.
552 */
553 static status_t del_route(private_kernel_pfroute_net_t *this, chunk_t dst_net,
554 u_int8_t prefixlen, host_t *gateway, host_t *src_ip, char *if_name)
555 {
556 return FAILED;
557 }
558
559 /**
560 * Initialize a list of local addresses.
561 */
562 static status_t init_address_list(private_kernel_pfroute_net_t *this)
563 {
564 struct ifaddrs *ifap, *ifa;
565 iface_entry_t *iface, *current;
566 addr_entry_t *addr;
567 enumerator_t *ifaces, *addrs;
568
569 DBG1(DBG_KNL, "listening on interfaces:");
570
571 if (getifaddrs(&ifap) < 0)
572 {
573 DBG1(DBG_KNL, " failed to get interfaces!");
574 return FAILED;
575 }
576
577 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
578 {
579 if (ifa->ifa_addr == NULL)
580 {
581 continue;
582 }
583 switch(ifa->ifa_addr->sa_family)
584 {
585 case AF_LINK:
586 case AF_INET:
587 case AF_INET6:
588 {
589 if (ifa->ifa_flags & IFF_LOOPBACK)
590 { /* ignore loopback interfaces */
591 continue;
592 }
593
594 iface = NULL;
595 ifaces = this->ifaces->create_enumerator(this->ifaces);
596 while (ifaces->enumerate(ifaces, &current))
597 {
598 if (streq(current->ifname, ifa->ifa_name))
599 {
600 iface = current;
601 break;
602 }
603 }
604 ifaces->destroy(ifaces);
605
606 if (!iface)
607 {
608 iface = malloc_thing(iface_entry_t);
609 memcpy(iface->ifname, ifa->ifa_name, IFNAMSIZ);
610 iface->ifindex = if_nametoindex(ifa->ifa_name);
611 iface->flags = ifa->ifa_flags;
612 iface->addrs = linked_list_create();
613 this->ifaces->insert_last(this->ifaces, iface);
614 }
615
616 if (ifa->ifa_addr->sa_family != AF_LINK)
617 {
618 addr = malloc_thing(addr_entry_t);
619 addr->ip = host_create_from_sockaddr(ifa->ifa_addr);
620 addr->virtual = FALSE;
621 addr->refcount = 1;
622 iface->addrs->insert_last(iface->addrs, addr);
623 }
624 }
625 }
626 }
627 freeifaddrs(ifap);
628
629 ifaces = this->ifaces->create_enumerator(this->ifaces);
630 while (ifaces->enumerate(ifaces, &iface))
631 {
632 if (iface->flags & IFF_UP)
633 {
634 DBG1(DBG_KNL, " %s", iface->ifname);
635 addrs = iface->addrs->create_enumerator(iface->addrs);
636 while (addrs->enumerate(addrs, (void**)&addr))
637 {
638 DBG1(DBG_KNL, " %H", addr->ip);
639 }
640 addrs->destroy(addrs);
641 }
642 }
643 ifaces->destroy(ifaces);
644
645 return SUCCESS;
646 }
647
648 /**
649 * Implementation of kernel_netlink_net_t.destroy.
650 */
651 static void destroy(private_kernel_pfroute_net_t *this)
652 {
653 if (this->job)
654 {
655 this->job->cancel(this->job);
656 }
657 if (this->socket > 0)
658 {
659 close(this->socket);
660 }
661 if (this->socket_events)
662 {
663 close(this->socket_events);
664 }
665 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
666 this->mutex->destroy(this->mutex);
667 this->mutex_pfroute->destroy(this->mutex_pfroute);
668 free(this);
669 }
670
671 /*
672 * Described in header.
673 */
674 kernel_pfroute_net_t *kernel_pfroute_net_create()
675 {
676 private_kernel_pfroute_net_t *this = malloc_thing(private_kernel_pfroute_net_t);
677
678 /* public functions */
679 this->public.interface.get_interface = (char*(*)(kernel_net_t*,host_t*))get_interface_name;
680 this->public.interface.create_address_enumerator = (enumerator_t*(*)(kernel_net_t*,bool,bool))create_address_enumerator;
681 this->public.interface.get_source_addr = (host_t*(*)(kernel_net_t*, host_t *dest, host_t *src))get_source_addr;
682 this->public.interface.get_nexthop = (host_t*(*)(kernel_net_t*, host_t *dest))get_nexthop;
683 this->public.interface.add_ip = (status_t(*)(kernel_net_t*,host_t*,host_t*)) add_ip;
684 this->public.interface.del_ip = (status_t(*)(kernel_net_t*,host_t*)) del_ip;
685 this->public.interface.add_route = (status_t(*)(kernel_net_t*,chunk_t,u_int8_t,host_t*,host_t*,char*)) add_route;
686 this->public.interface.del_route = (status_t(*)(kernel_net_t*,chunk_t,u_int8_t,host_t*,host_t*,char*)) del_route;
687
688 this->public.interface.destroy = (void(*)(kernel_net_t*)) destroy;
689
690 /* private members */
691 this->ifaces = linked_list_create();
692 this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
693 this->mutex_pfroute = mutex_create(MUTEX_TYPE_DEFAULT);
694
695 this->seq = 0;
696 this->socket_events = 0;
697 this->job = NULL;
698
699 /* create a PF_ROUTE socket to communicate with the kernel */
700 this->socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
701 if (this->socket < 0)
702 {
703 DBG1(DBG_KNL, "unable to create PF_ROUTE socket");
704 destroy(this);
705 return NULL;
706 }
707
708 /* create a PF_ROUTE socket to receive events */
709 this->socket_events = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
710 if (this->socket_events < 0)
711 {
712 DBG1(DBG_KNL, "unable to create PF_ROUTE event socket");
713 destroy(this);
714 return NULL;
715 }
716
717 this->job = callback_job_create((callback_job_cb_t)receive_events,
718 this, NULL, NULL);
719 charon->processor->queue_job(charon->processor, (job_t*)this->job);
720
721 if (init_address_list(this) != SUCCESS)
722 {
723 DBG1(DBG_KNL, "unable to get interface list");
724 destroy(this);
725 return NULL;
726 }
727
728 return &this->public;
729 }