using correct nexthop for inserted route
[strongswan.git] / src / charon / kernel / kernel_interface.c
1 /**
2 * @file kernel_interface.c
3 *
4 * @brief Implementation of kernel_interface_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2007 Martin Willi
10 * Copyright (C) 2006-2007 Tobias Brunner
11 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
12 * Copyright (C) 2006 Daniel Roethlisberger
13 * Copyright (C) 2005 Jan Hutter
14 * Hochschule fuer Technik Rapperswil
15 * Copyright (C) 2003 Herbert Xu.
16 *
17 * Based on xfrm code from pluto.
18 *
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the
21 * Free Software Foundation; either version 2 of the License, or (at your
22 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 * for more details.
28 */
29
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/xfrm.h>
35 #include <linux/udp.h>
36 #include <pthread.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <net/if.h>
42 #include <sys/ioctl.h>
43
44 #include "kernel_interface.h"
45
46 #include <daemon.h>
47 #include <utils/linked_list.h>
48 #include <processing/jobs/delete_child_sa_job.h>
49 #include <processing/jobs/rekey_child_sa_job.h>
50 #include <processing/jobs/acquire_job.h>
51 #include <processing/jobs/callback_job.h>
52 #include <processing/jobs/roam_job.h>
53
54 /** kernel level protocol identifiers */
55 #define KERNEL_ESP 50
56 #define KERNEL_AH 51
57
58 /** default priority of installed policies */
59 #define PRIO_LOW 3000
60 #define PRIO_HIGH 2000
61
62 #define BUFFER_SIZE 1024
63
64 /**
65 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
66 * 'usual' netlink data x like 'struct xfrm_usersa_info'
67 */
68 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
69 /**
70 * returns a pointer to the next rtattr following rta.
71 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
72 */
73 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
74 /**
75 * returns the total size of attached rta data
76 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
77 */
78 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
79
80 typedef struct kernel_algorithm_t kernel_algorithm_t;
81
82 /**
83 * Mapping from the algorithms defined in IKEv2 to
84 * kernel level algorithm names and their key length
85 */
86 struct kernel_algorithm_t {
87 /**
88 * Identifier specified in IKEv2
89 */
90 int ikev2_id;
91
92 /**
93 * Name of the algorithm, as used as kernel identifier
94 */
95 char *name;
96
97 /**
98 * Key length in bits, if fixed size
99 */
100 u_int key_size;
101 };
102 #define END_OF_LIST -1
103
104 /**
105 * Algorithms for encryption
106 */
107 kernel_algorithm_t encryption_algs[] = {
108 /* {ENCR_DES_IV64, "***", 0}, */
109 {ENCR_DES, "des", 64},
110 {ENCR_3DES, "des3_ede", 192},
111 /* {ENCR_RC5, "***", 0}, */
112 /* {ENCR_IDEA, "***", 0}, */
113 {ENCR_CAST, "cast128", 0},
114 {ENCR_BLOWFISH, "blowfish", 0},
115 /* {ENCR_3IDEA, "***", 0}, */
116 /* {ENCR_DES_IV32, "***", 0}, */
117 {ENCR_NULL, "cipher_null", 0},
118 {ENCR_AES_CBC, "aes", 0},
119 /* {ENCR_AES_CTR, "***", 0}, */
120 {END_OF_LIST, NULL, 0},
121 };
122
123 /**
124 * Algorithms for integrity protection
125 */
126 kernel_algorithm_t integrity_algs[] = {
127 {AUTH_HMAC_MD5_96, "md5", 128},
128 {AUTH_HMAC_SHA1_96, "sha1", 160},
129 {AUTH_HMAC_SHA2_256_128, "sha256", 256},
130 {AUTH_HMAC_SHA2_384_192, "sha384", 384},
131 {AUTH_HMAC_SHA2_512_256, "sha512", 512},
132 /* {AUTH_DES_MAC, "***", 0}, */
133 /* {AUTH_KPDK_MD5, "***", 0}, */
134 {AUTH_AES_XCBC_96, "xcbc(aes)", 128},
135 {END_OF_LIST, NULL, 0},
136 };
137
138 /**
139 * Look up a kernel algorithm name and its key size
140 */
141 char* lookup_algorithm(kernel_algorithm_t *kernel_algo,
142 algorithm_t *ikev2_algo, u_int *key_size)
143 {
144 while (kernel_algo->ikev2_id != END_OF_LIST)
145 {
146 if (ikev2_algo->algorithm == kernel_algo->ikev2_id)
147 {
148 /* match, evaluate key length */
149 if (ikev2_algo->key_size)
150 { /* variable length */
151 *key_size = ikev2_algo->key_size;
152 }
153 else
154 { /* fixed length */
155 *key_size = kernel_algo->key_size;
156 }
157 return kernel_algo->name;
158 }
159 kernel_algo++;
160 }
161 return NULL;
162 }
163
164 typedef struct route_entry_t route_entry_t;
165
166 /**
167 * installed routing entry
168 */
169 struct route_entry_t {
170
171 /** Index of the interface the route is bound to */
172 int if_index;
173
174 /** Source ip of the route */
175 host_t *src_ip;
176
177 /** gateway for this route */
178 host_t *gateway;
179
180 /** Destination net */
181 chunk_t dst_net;
182
183 /** Destination net prefixlen */
184 u_int8_t prefixlen;
185 };
186
187 /**
188 * destroy an route_entry_t object
189 */
190 static void route_entry_destroy(route_entry_t *this)
191 {
192 this->src_ip->destroy(this->src_ip);
193 this->gateway->destroy(this->gateway);
194 chunk_free(&this->dst_net);
195 free(this);
196 }
197
198 typedef struct policy_entry_t policy_entry_t;
199
200 /**
201 * installed kernel policy.
202 */
203 struct policy_entry_t {
204
205 /** direction of this policy: in, out, forward */
206 u_int8_t direction;
207
208 /** reqid of the policy */
209 u_int32_t reqid;
210
211 /** parameters of installed policy */
212 struct xfrm_selector sel;
213
214 /** associated route installed for this policy */
215 route_entry_t *route;
216
217 /** by how many CHILD_SA's this policy is used */
218 u_int refcount;
219 };
220
221 typedef struct addr_entry_t addr_entry_t;
222
223 /**
224 * IP address in an inface_entry_t
225 */
226 struct addr_entry_t {
227
228 /** The ip address */
229 host_t *ip;
230
231 /** virtual IP managed by us */
232 bool virtual;
233
234 /** scope of the address */
235 u_char scope;
236
237 /** Number of times this IP is used, if virtual */
238 u_int refcount;
239 };
240
241 /**
242 * destroy a addr_entry_t object
243 */
244 static void addr_entry_destroy(addr_entry_t *this)
245 {
246 this->ip->destroy(this->ip);
247 free(this);
248 }
249
250 typedef struct iface_entry_t iface_entry_t;
251
252 /**
253 * A network interface on this system, containing addr_entry_t's
254 */
255 struct iface_entry_t {
256
257 /** interface index */
258 int ifindex;
259
260 /** name of the interface */
261 char ifname[IFNAMSIZ];
262
263 /** interface flags, as in netdevice(7) SIOCGIFFLAGS */
264 u_int flags;
265
266 /** list of addresses as host_t */
267 linked_list_t *addrs;
268 };
269
270 /**
271 * destroy an interface entry
272 */
273 static void iface_entry_destroy(iface_entry_t *this)
274 {
275 this->addrs->destroy_function(this->addrs, (void*)addr_entry_destroy);
276 free(this);
277 }
278
279 typedef struct private_kernel_interface_t private_kernel_interface_t;
280
281 /**
282 * Private variables and functions of kernel_interface class.
283 */
284 struct private_kernel_interface_t {
285 /**
286 * Public part of the kernel_interface_t object.
287 */
288 kernel_interface_t public;
289
290 /**
291 * mutex to lock access to the various lists
292 */
293 pthread_mutex_t mutex;
294
295 /**
296 * List of installed policies (policy_entry_t)
297 */
298 linked_list_t *policies;
299
300 /**
301 * Cached list of interfaces and its adresses (iface_entry_t)
302 */
303 linked_list_t *ifaces;
304
305 /**
306 * iterator used in hook()
307 */
308 iterator_t *hiter;
309
310 /**
311 * job receiving netlink events
312 */
313 callback_job_t *job;
314
315 /**
316 * current sequence number for netlink request
317 */
318 int seq;
319
320 /**
321 * Netlink xfrm socket (IPsec)
322 */
323 int socket_xfrm;
324
325 /**
326 * netlink xfrm socket to receive acquire and expire events
327 */
328 int socket_xfrm_events;
329
330 /**
331 * Netlink rt socket (routing)
332 */
333 int socket_rt;
334
335 /**
336 * Netlink rt socket to receive address change events
337 */
338 int socket_rt_events;
339 };
340
341 /**
342 * convert a host_t to a struct xfrm_address
343 */
344 static void host2xfrm(host_t *host, xfrm_address_t *xfrm)
345 {
346 chunk_t chunk = host->get_address(host);
347 memcpy(xfrm, chunk.ptr, min(chunk.len, sizeof(xfrm_address_t)));
348 }
349
350 /**
351 * convert a traffic selector address range to subnet and its mask.
352 */
353 static void ts2subnet(traffic_selector_t* ts,
354 xfrm_address_t *net, u_int8_t *mask)
355 {
356 /* there is no way to do this cleanly, as the address range may
357 * be anything else but a subnet. We use from_addr as subnet
358 * and try to calculate a usable subnet mask.
359 */
360 int byte, bit;
361 bool found = FALSE;
362 chunk_t from, to;
363 size_t size = (ts->get_type(ts) == TS_IPV4_ADDR_RANGE) ? 4 : 16;
364
365 from = ts->get_from_address(ts);
366 to = ts->get_to_address(ts);
367
368 *mask = (size * 8);
369 /* go trough all bits of the addresses, beginning in the front.
370 * as long as they are equal, the subnet gets larger
371 */
372 for (byte = 0; byte < size; byte++)
373 {
374 for (bit = 7; bit >= 0; bit--)
375 {
376 if ((1<<bit & from.ptr[byte]) != (1<<bit & to.ptr[byte]))
377 {
378 *mask = ((7 - bit) + (byte * 8));
379 found = TRUE;
380 break;
381 }
382 }
383 if (found)
384 {
385 break;
386 }
387 }
388 memcpy(net, from.ptr, from.len);
389 chunk_free(&from);
390 chunk_free(&to);
391 }
392
393 /**
394 * convert a traffic selector port range to port/portmask
395 */
396 static void ts2ports(traffic_selector_t* ts,
397 u_int16_t *port, u_int16_t *mask)
398 {
399 /* linux does not seem to accept complex portmasks. Only
400 * any or a specific port is allowed. We set to any, if we have
401 * a port range, or to a specific, if we have one port only.
402 */
403 u_int16_t from, to;
404
405 from = ts->get_from_port(ts);
406 to = ts->get_to_port(ts);
407
408 if (from == to)
409 {
410 *port = htons(from);
411 *mask = ~0;
412 }
413 else
414 {
415 *port = 0;
416 *mask = 0;
417 }
418 }
419
420 /**
421 * convert a pair of traffic_selectors to a xfrm_selector
422 */
423 static struct xfrm_selector ts2selector(traffic_selector_t *src,
424 traffic_selector_t *dst)
425 {
426 struct xfrm_selector sel;
427
428 memset(&sel, 0, sizeof(sel));
429 sel.family = src->get_type(src) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
430 /* src or dest proto may be "any" (0), use more restrictive one */
431 sel.proto = max(src->get_protocol(src), dst->get_protocol(dst));
432 ts2subnet(dst, &sel.daddr, &sel.prefixlen_d);
433 ts2subnet(src, &sel.saddr, &sel.prefixlen_s);
434 ts2ports(dst, &sel.dport, &sel.dport_mask);
435 ts2ports(src, &sel.sport, &sel.sport_mask);
436 sel.ifindex = 0;
437 sel.user = 0;
438
439 return sel;
440 }
441
442 /**
443 * Creates an rtattr and adds it to the netlink message
444 */
445 static void add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
446 size_t buflen)
447 {
448 struct rtattr *rta;
449
450 if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_ALIGN(data.len) > buflen)
451 {
452 DBG1(DBG_KNL, "unable to add attribute, buffer too small");
453 return;
454 }
455
456 rta = (struct rtattr*)(((char*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len));
457 rta->rta_type = rta_type;
458 rta->rta_len = RTA_LENGTH(data.len);
459 memcpy(RTA_DATA(rta), data.ptr, data.len);
460 hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
461 }
462
463 /**
464 * process a XFRM_MSG_ACQUIRE from kernel
465 */
466 static void process_acquire(private_kernel_interface_t *this, struct nlmsghdr *hdr)
467 {
468 u_int32_t reqid = 0;
469 job_t *job;
470 struct rtattr *rtattr = XFRM_RTA(hdr, struct xfrm_user_acquire);
471 size_t rtsize = XFRM_PAYLOAD(hdr, struct xfrm_user_tmpl);
472
473 if (RTA_OK(rtattr, rtsize))
474 {
475 if (rtattr->rta_type == XFRMA_TMPL)
476 {
477 struct xfrm_user_tmpl* tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rtattr);
478 reqid = tmpl->reqid;
479 }
480 }
481 if (reqid == 0)
482 {
483 DBG1(DBG_KNL, "received a XFRM_MSG_ACQUIRE, but no reqid found");
484 return;
485 }
486 DBG2(DBG_KNL, "received a XFRM_MSG_ACQUIRE");
487 DBG1(DBG_KNL, "creating acquire job for CHILD_SA with reqid %d", reqid);
488 job = (job_t*)acquire_job_create(reqid);
489 charon->processor->queue_job(charon->processor, job);
490 }
491
492 /**
493 * process a XFRM_MSG_EXPIRE from kernel
494 */
495 static void process_expire(private_kernel_interface_t *this, struct nlmsghdr *hdr)
496 {
497 job_t *job;
498 protocol_id_t protocol;
499 u_int32_t spi, reqid;
500 struct xfrm_user_expire *expire;
501
502 expire = (struct xfrm_user_expire*)NLMSG_DATA(hdr);
503 protocol = expire->state.id.proto == KERNEL_ESP ? PROTO_ESP : PROTO_AH;
504 spi = expire->state.id.spi;
505 reqid = expire->state.reqid;
506
507 DBG2(DBG_KNL, "received a XFRM_MSG_EXPIRE");
508 DBG1(DBG_KNL, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
509 expire->hard ? "delete" : "rekey", protocol_id_names,
510 protocol, ntohl(spi), reqid);
511 if (expire->hard)
512 {
513 job = (job_t*)delete_child_sa_job_create(reqid, protocol, spi);
514 }
515 else
516 {
517 job = (job_t*)rekey_child_sa_job_create(reqid, protocol, spi);
518 }
519 charon->processor->queue_job(charon->processor, job);
520 }
521
522 /**
523 * process RTM_NEWLINK/RTM_DELLINK from kernel
524 */
525 static void process_link(private_kernel_interface_t *this,
526 struct nlmsghdr *hdr, bool event)
527 {
528 struct ifinfomsg* msg = (struct ifinfomsg*)(NLMSG_DATA(hdr));
529 struct rtattr *rta = IFLA_RTA(msg);
530 size_t rtasize = IFLA_PAYLOAD (hdr);
531 iterator_t *iterator;
532 iface_entry_t *current, *entry = NULL;
533 char *name = NULL;
534 bool update = FALSE;
535
536 while(RTA_OK(rta, rtasize))
537 {
538 switch (rta->rta_type)
539 {
540 case IFLA_IFNAME:
541 name = RTA_DATA(rta);
542 break;
543 }
544 rta = RTA_NEXT(rta, rtasize);
545 }
546 if (!name)
547 {
548 name = "(unknown)";
549 }
550
551 switch (hdr->nlmsg_type)
552 {
553 case RTM_NEWLINK:
554 {
555 if (msg->ifi_flags & IFF_LOOPBACK)
556 { /* ignore loopback interfaces */
557 break;
558 }
559 iterator = this->ifaces->create_iterator_locked(this->ifaces,
560 &this->mutex);
561 while (iterator->iterate(iterator, (void**)&current))
562 {
563 if (current->ifindex == msg->ifi_index)
564 {
565 entry = current;
566 break;
567 }
568 }
569 if (!entry)
570 {
571 entry = malloc_thing(iface_entry_t);
572 entry->ifindex = msg->ifi_index;
573 entry->flags = 0;
574 entry->addrs = linked_list_create();
575 this->ifaces->insert_last(this->ifaces, entry);
576 }
577 memcpy(entry->ifname, name, IFNAMSIZ);
578 entry->ifname[IFNAMSIZ-1] = '\0';
579 if (event)
580 {
581 if (!(entry->flags & IFF_UP) && (msg->ifi_flags & IFF_UP))
582 {
583 update = TRUE;
584 DBG1(DBG_KNL, "interface %s activated", name);
585 }
586 if ((entry->flags & IFF_UP) && !(msg->ifi_flags & IFF_UP))
587 {
588 update = TRUE;
589 DBG1(DBG_KNL, "interface %s deactivated", name);
590 }
591 }
592 entry->flags = msg->ifi_flags;
593 iterator->destroy(iterator);
594 break;
595 }
596 case RTM_DELLINK:
597 {
598 iterator = this->ifaces->create_iterator_locked(this->ifaces,
599 &this->mutex);
600 while (iterator->iterate(iterator, (void**)&current))
601 {
602 if (current->ifindex == msg->ifi_index)
603 {
604 /* we do not remove it, as an address may be added to a
605 * "down" interface and we wan't to know that. */
606 current->flags = msg->ifi_flags;
607 break;
608 }
609 }
610 iterator->destroy(iterator);
611 break;
612 }
613 }
614
615 /* send an update to all IKE_SAs */
616 if (update && event)
617 {
618 charon->processor->queue_job(charon->processor,
619 (job_t*)roam_job_create(TRUE));
620 }
621 }
622
623 /**
624 * process RTM_NEWADDR/RTM_DELADDR from kernel
625 */
626 static void process_addr(private_kernel_interface_t *this,
627 struct nlmsghdr *hdr, bool event)
628 {
629 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
630 struct rtattr *rta = IFA_RTA(msg);
631 size_t rtasize = IFA_PAYLOAD (hdr);
632 host_t *host = NULL;
633 iterator_t *ifaces, *addrs;
634 iface_entry_t *iface;
635 addr_entry_t *addr;
636 chunk_t local = chunk_empty, address = chunk_empty;
637 bool update = FALSE, found = FALSE, changed = FALSE;
638
639 while(RTA_OK(rta, rtasize))
640 {
641 switch (rta->rta_type)
642 {
643 case IFA_LOCAL:
644 local.ptr = RTA_DATA(rta);
645 local.len = RTA_PAYLOAD(rta);
646 break;
647 case IFA_ADDRESS:
648 address.ptr = RTA_DATA(rta);
649 address.len = RTA_PAYLOAD(rta);
650 break;
651 }
652 rta = RTA_NEXT(rta, rtasize);
653 }
654
655 /* For PPP interfaces, we need the IFA_LOCAL address,
656 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
657 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
658 if (local.ptr)
659 {
660 host = host_create_from_chunk(msg->ifa_family, local, 0);
661 }
662 else if (address.ptr)
663 {
664 host = host_create_from_chunk(msg->ifa_family, address, 0);
665 }
666
667 if (host == NULL)
668 { /* bad family? */
669 return;
670 }
671
672 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
673 while (ifaces->iterate(ifaces, (void**)&iface))
674 {
675 if (iface->ifindex == msg->ifa_index)
676 {
677 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
678 while (addrs->iterate(addrs, (void**)&addr))
679 {
680 if (host->ip_equals(host, addr->ip))
681 {
682 found = TRUE;
683 if (hdr->nlmsg_type == RTM_DELADDR)
684 {
685 changed = TRUE;
686 addrs->remove(addrs);
687 addr_entry_destroy(addr);
688 DBG1(DBG_KNL, "%H disappeared from %s", host, iface->ifname);
689 }
690 }
691 }
692 addrs->destroy(addrs);
693
694 if (hdr->nlmsg_type == RTM_NEWADDR)
695 {
696 if (!found)
697 {
698 found = TRUE;
699 changed = TRUE;
700 addr = malloc_thing(addr_entry_t);
701 addr->ip = host->clone(host);
702 addr->virtual = FALSE;
703 addr->refcount = 1;
704 addr->scope = msg->ifa_scope;
705
706 iface->addrs->insert_last(iface->addrs, addr);
707 if (event)
708 {
709 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
710 }
711 }
712 }
713 if (found && (iface->flags & IFF_UP))
714 {
715 update = TRUE;
716 }
717 break;
718 }
719 }
720 ifaces->destroy(ifaces);
721 host->destroy(host);
722
723 /* send an update to all IKE_SAs */
724 if (update && event && changed)
725 {
726 charon->processor->queue_job(charon->processor,
727 (job_t*)roam_job_create(TRUE));
728 }
729 }
730
731 /**
732 * Receives events from kernel
733 */
734 static job_requeue_t receive_events(private_kernel_interface_t *this)
735 {
736 char response[1024];
737 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
738 struct sockaddr_nl addr;
739 socklen_t addr_len = sizeof(addr);
740 int len, oldstate, maxfd, selected;
741 fd_set rfds;
742
743 FD_ZERO(&rfds);
744 FD_SET(this->socket_xfrm_events, &rfds);
745 FD_SET(this->socket_rt_events, &rfds);
746 maxfd = max(this->socket_xfrm_events, this->socket_rt_events);
747
748 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
749 selected = select(maxfd + 1, &rfds, NULL, NULL, NULL);
750 pthread_setcancelstate(oldstate, NULL);
751 if (selected <= 0)
752 {
753 DBG1(DBG_KNL, "selecting on sockets failed: %s", strerror(errno));
754 return JOB_REQUEUE_FAIR;
755 }
756 if (FD_ISSET(this->socket_xfrm_events, &rfds))
757 {
758 selected = this->socket_xfrm_events;
759 }
760 else if (FD_ISSET(this->socket_rt_events, &rfds))
761 {
762 selected = this->socket_rt_events;
763 }
764 else
765 {
766 return JOB_REQUEUE_DIRECT;
767 }
768
769 len = recvfrom(selected, response, sizeof(response), MSG_DONTWAIT,
770 (struct sockaddr*)&addr, &addr_len);
771 if (len < 0)
772 {
773 switch (errno)
774 {
775 case EINTR:
776 /* interrupted, try again */
777 return JOB_REQUEUE_DIRECT;
778 case EAGAIN:
779 /* no data ready, select again */
780 return JOB_REQUEUE_DIRECT;
781 default:
782 DBG1(DBG_KNL, "unable to receive from xfrm event socket");
783 sleep(1);
784 return JOB_REQUEUE_FAIR;
785 }
786 }
787 if (addr.nl_pid != 0)
788 { /* not from kernel. not interested, try another one */
789 return JOB_REQUEUE_DIRECT;
790 }
791
792 while (NLMSG_OK(hdr, len))
793 {
794 /* looks good so far, dispatch netlink message */
795 if (selected == this->socket_xfrm_events)
796 {
797 switch (hdr->nlmsg_type)
798 {
799 case XFRM_MSG_ACQUIRE:
800 process_acquire(this, hdr);
801 break;
802 case XFRM_MSG_EXPIRE:
803 process_expire(this, hdr);
804 break;
805 default:
806 break;
807 }
808 }
809 else if (selected == this->socket_rt_events)
810 {
811 switch (hdr->nlmsg_type)
812 {
813 case RTM_NEWADDR:
814 case RTM_DELADDR:
815 process_addr(this, hdr, TRUE);
816 break;
817 case RTM_NEWLINK:
818 case RTM_DELLINK:
819 process_link(this, hdr, TRUE);
820 break;
821 case RTM_NEWROUTE:
822 case RTM_DELROUTE:
823 charon->processor->queue_job(charon->processor,
824 (job_t*)roam_job_create(FALSE));
825 break;
826 default:
827 break;
828 }
829 }
830 hdr = NLMSG_NEXT(hdr, len);
831 }
832 return JOB_REQUEUE_DIRECT;
833 }
834
835 /**
836 * send a netlink message and wait for a reply
837 */
838 static status_t netlink_send(private_kernel_interface_t *this,
839 int socket, struct nlmsghdr *in,
840 struct nlmsghdr **out, size_t *out_len)
841 {
842 int len, addr_len;
843 struct sockaddr_nl addr;
844 chunk_t result = chunk_empty, tmp;
845 struct nlmsghdr *msg, peek;
846
847 pthread_mutex_lock(&this->mutex);
848
849 in->nlmsg_seq = ++this->seq;
850 in->nlmsg_pid = getpid();
851
852 memset(&addr, 0, sizeof(addr));
853 addr.nl_family = AF_NETLINK;
854 addr.nl_pid = 0;
855 addr.nl_groups = 0;
856
857 while (TRUE)
858 {
859 len = sendto(socket, in, in->nlmsg_len, 0,
860 (struct sockaddr*)&addr, sizeof(addr));
861
862 if (len != in->nlmsg_len)
863 {
864 if (errno == EINTR)
865 {
866 /* interrupted, try again */
867 continue;
868 }
869 pthread_mutex_unlock(&this->mutex);
870 DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
871 return FAILED;
872 }
873 break;
874 }
875
876 while (TRUE)
877 {
878 char buf[4096];
879 tmp.len = sizeof(buf);
880 tmp.ptr = buf;
881 msg = (struct nlmsghdr*)tmp.ptr;
882
883 memset(&addr, 0, sizeof(addr));
884 addr.nl_family = AF_NETLINK;
885 addr.nl_pid = getpid();
886 addr.nl_groups = 0;
887 addr_len = sizeof(addr);
888
889 len = recvfrom(socket, tmp.ptr, tmp.len, 0,
890 (struct sockaddr*)&addr, &addr_len);
891
892 if (len < 0)
893 {
894 if (errno == EINTR)
895 {
896 DBG1(DBG_KNL, "got interrupted");
897 /* interrupted, try again */
898 continue;
899 }
900 DBG1(DBG_KNL, "error reading from netlink socket: %s", strerror(errno));
901 pthread_mutex_unlock(&this->mutex);
902 return FAILED;
903 }
904 if (!NLMSG_OK(msg, len))
905 {
906 DBG1(DBG_KNL, "received corrupted netlink message");
907 pthread_mutex_unlock(&this->mutex);
908 return FAILED;
909 }
910 if (msg->nlmsg_seq != this->seq)
911 {
912 DBG1(DBG_KNL, "received invalid netlink sequence number");
913 if (msg->nlmsg_seq < this->seq)
914 {
915 continue;
916 }
917 pthread_mutex_unlock(&this->mutex);
918 return FAILED;
919 }
920
921 tmp.len = len;
922 result = chunk_cata("cc", result, tmp);
923
924 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
925 * numbers to detect multi header messages */
926 len = recvfrom(socket, &peek, sizeof(peek), MSG_PEEK | MSG_DONTWAIT,
927 (struct sockaddr*)&addr, &addr_len);
928
929 if (len == sizeof(peek) && peek.nlmsg_seq == this->seq)
930 {
931 /* seems to be multipart */
932 continue;
933 }
934 break;
935 }
936
937 *out_len = result.len;
938 *out = (struct nlmsghdr*)clalloc(result.ptr, result.len);
939
940 pthread_mutex_unlock(&this->mutex);
941
942 return SUCCESS;
943 }
944
945 /**
946 * send a netlink message and wait for its acknowlegde
947 */
948 static status_t netlink_send_ack(private_kernel_interface_t *this,
949 int socket, struct nlmsghdr *in)
950 {
951 struct nlmsghdr *out, *hdr;
952 size_t len;
953
954 if (netlink_send(this, socket, in, &out, &len) != SUCCESS)
955 {
956 return FAILED;
957 }
958 hdr = out;
959 while (NLMSG_OK(hdr, len))
960 {
961 switch (hdr->nlmsg_type)
962 {
963 case NLMSG_ERROR:
964 {
965 struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(hdr);
966
967 if (err->error)
968 {
969 DBG1(DBG_KNL, "received netlink error: %s (%d)",
970 strerror(-err->error), -err->error);
971 free(out);
972 return FAILED;
973 }
974 free(out);
975 return SUCCESS;
976 }
977 default:
978 hdr = NLMSG_NEXT(hdr, len);
979 continue;
980 case NLMSG_DONE:
981 break;
982 }
983 break;
984 }
985 DBG1(DBG_KNL, "netlink request not acknowlegded");
986 free(out);
987 return FAILED;
988 }
989
990 /**
991 * Initialize a list of local addresses.
992 */
993 static status_t init_address_list(private_kernel_interface_t *this)
994 {
995 char request[BUFFER_SIZE];
996 struct nlmsghdr *out, *current, *in;
997 struct rtgenmsg *msg;
998 size_t len;
999 iterator_t *ifaces, *addrs;
1000 iface_entry_t *iface;
1001 addr_entry_t *addr;
1002
1003 DBG1(DBG_KNL, "listening on interfaces:");
1004
1005 memset(&request, 0, sizeof(request));
1006
1007 in = (struct nlmsghdr*)&request;
1008 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1009 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1010 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1011 msg->rtgen_family = AF_UNSPEC;
1012
1013 /* get all links */
1014 in->nlmsg_type = RTM_GETLINK;
1015 if (netlink_send(this, this->socket_rt, in, &out, &len) != SUCCESS)
1016 {
1017 return FAILED;
1018 }
1019 current = out;
1020 while (NLMSG_OK(current, len))
1021 {
1022 switch (current->nlmsg_type)
1023 {
1024 case NLMSG_DONE:
1025 break;
1026 case RTM_NEWLINK:
1027 process_link(this, current, FALSE);
1028 /* fall through */
1029 default:
1030 current = NLMSG_NEXT(current, len);
1031 continue;
1032 }
1033 break;
1034 }
1035 free(out);
1036
1037 /* get all interface addresses */
1038 in->nlmsg_type = RTM_GETADDR;
1039 if (netlink_send(this, this->socket_rt, in, &out, &len) != SUCCESS)
1040 {
1041 return FAILED;
1042 }
1043 current = out;
1044 while (NLMSG_OK(current, len))
1045 {
1046 switch (current->nlmsg_type)
1047 {
1048 case NLMSG_DONE:
1049 break;
1050 case RTM_NEWADDR:
1051 process_addr(this, current, FALSE);
1052 /* fall through */
1053 default:
1054 current = NLMSG_NEXT(current, len);
1055 continue;
1056 }
1057 break;
1058 }
1059 free(out);
1060
1061 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1062 while (ifaces->iterate(ifaces, (void**)&iface))
1063 {
1064 if (iface->flags & IFF_UP)
1065 {
1066 DBG1(DBG_KNL, " %s", iface->ifname);
1067 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1068 while (addrs->iterate(addrs, (void**)&addr))
1069 {
1070 DBG1(DBG_KNL, " %H", addr->ip);
1071 }
1072 addrs->destroy(addrs);
1073 }
1074 }
1075 ifaces->destroy(ifaces);
1076 return SUCCESS;
1077 }
1078
1079 /**
1080 * iterator hook to iterate over addrs
1081 */
1082 static hook_result_t addr_hook(private_kernel_interface_t *this,
1083 addr_entry_t *in, host_t **out)
1084 {
1085 if (in->virtual)
1086 { /* skip virtual interfaces added by us */
1087 return HOOK_SKIP;
1088 }
1089 if (in->scope >= RT_SCOPE_LINK)
1090 { /* skip addresses with a unusable scope */
1091 return HOOK_SKIP;
1092 }
1093 *out = in->ip;
1094 return HOOK_NEXT;
1095 }
1096
1097 /**
1098 * iterator hook to iterate over ifaces
1099 */
1100 static hook_result_t iface_hook(private_kernel_interface_t *this,
1101 iface_entry_t *in, host_t **out)
1102 {
1103 if (!(in->flags & IFF_UP))
1104 { /* skip interfaces not up */
1105 return HOOK_SKIP;
1106 }
1107
1108 if (this->hiter == NULL)
1109 {
1110 this->hiter = in->addrs->create_iterator(in->addrs, TRUE);
1111 this->hiter->set_iterator_hook(this->hiter,
1112 (iterator_hook_t*)addr_hook, this);
1113 }
1114 while (this->hiter->iterate(this->hiter, (void**)out))
1115 {
1116 return HOOK_AGAIN;
1117 }
1118 this->hiter->destroy(this->hiter);
1119 this->hiter = NULL;
1120 return HOOK_SKIP;
1121 }
1122
1123 /**
1124 * Implements kernel_interface_t.create_address_iterator.
1125 */
1126 static iterator_t *create_address_iterator(private_kernel_interface_t *this)
1127 {
1128 iterator_t *iterator;
1129
1130 /* This iterator is not only hooked, is is double-hooked. As we have stored
1131 * our addresses in iface_entry->addr_entry->ip, we need to iterate the
1132 * entries in each interface we iterate. This does the iface_hook. The
1133 * addr_hook returns the ip instead of the addr_entry. */
1134
1135 iterator = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1136 iterator->set_iterator_hook(iterator, (iterator_hook_t*)iface_hook, this);
1137 return iterator;
1138 }
1139
1140 /**
1141 * implementation of kernel_interface_t.get_interface_name
1142 */
1143 static char *get_interface_name(private_kernel_interface_t *this, host_t* ip)
1144 {
1145 iterator_t *ifaces, *addrs;
1146 iface_entry_t *iface;
1147 addr_entry_t *addr;
1148 char *name = NULL;
1149
1150 DBG2(DBG_KNL, "getting interface name for %H", ip);
1151
1152 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1153 while (ifaces->iterate(ifaces, (void**)&iface))
1154 {
1155 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1156 while (addrs->iterate(addrs, (void**)&addr))
1157 {
1158 if (ip->ip_equals(ip, addr->ip))
1159 {
1160 name = strdup(iface->ifname);
1161 break;
1162 }
1163 }
1164 addrs->destroy(addrs);
1165 if (name)
1166 {
1167 break;
1168 }
1169 }
1170 ifaces->destroy(ifaces);
1171
1172 if (name)
1173 {
1174 DBG2(DBG_KNL, "%H is on interface %s", ip, name);
1175 }
1176 else
1177 {
1178 DBG2(DBG_KNL, "%H is not a local address", ip);
1179 }
1180 return name;
1181 }
1182
1183 /**
1184 * Tries to find an ip address of a local interface that is included in the
1185 * supplied traffic selector.
1186 */
1187 static status_t get_address_by_ts(private_kernel_interface_t *this,
1188 traffic_selector_t *ts, host_t **ip)
1189 {
1190 iterator_t *ifaces, *addrs;
1191 iface_entry_t *iface;
1192 addr_entry_t *addr;
1193 host_t *host;
1194 int family;
1195 bool found = FALSE;
1196
1197 DBG2(DBG_KNL, "getting a local address in traffic selector %R", ts);
1198
1199 /* if we have a family which includes localhost, we do not
1200 * search for an IP, we use the default */
1201 family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
1202
1203 if (family == AF_INET)
1204 {
1205 host = host_create_from_string("127.0.0.1", 0);
1206 }
1207 else
1208 {
1209 host = host_create_from_string("::1", 0);
1210 }
1211
1212 if (ts->includes(ts, host))
1213 {
1214 *ip = host_create_any(family);
1215 host->destroy(host);
1216 DBG2(DBG_KNL, "using host %H", *ip);
1217 return SUCCESS;
1218 }
1219 host->destroy(host);
1220
1221 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1222 while (ifaces->iterate(ifaces, (void**)&iface))
1223 {
1224 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1225 while (addrs->iterate(addrs, (void**)&addr))
1226 {
1227 if (ts->includes(ts, addr->ip))
1228 {
1229 found = TRUE;
1230 *ip = addr->ip->clone(addr->ip);
1231 break;
1232 }
1233 }
1234 addrs->destroy(addrs);
1235 if (found)
1236 {
1237 break;
1238 }
1239 }
1240 ifaces->destroy(ifaces);
1241
1242 if (!found)
1243 {
1244 DBG1(DBG_KNL, "no local address found in traffic selector %R", ts);
1245 return FAILED;
1246 }
1247 DBG2(DBG_KNL, "using host %H", *ip);
1248 return SUCCESS;
1249 }
1250
1251 /**
1252 * get the interface of a local address
1253 */
1254 static int get_interface_index(private_kernel_interface_t *this, host_t* ip)
1255 {
1256 iterator_t *ifaces, *addrs;
1257 iface_entry_t *iface;
1258 addr_entry_t *addr;
1259 int ifindex = 0;
1260
1261 DBG2(DBG_KNL, "getting iface for %H", ip);
1262
1263 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1264 while (ifaces->iterate(ifaces, (void**)&iface))
1265 {
1266 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1267 while (addrs->iterate(addrs, (void**)&addr))
1268 {
1269 if (ip->ip_equals(ip, addr->ip))
1270 {
1271 ifindex = iface->ifindex;
1272 break;
1273 }
1274 }
1275 addrs->destroy(addrs);
1276 if (ifindex)
1277 {
1278 break;
1279 }
1280 }
1281 ifaces->destroy(ifaces);
1282
1283 if (ifindex == 0)
1284 {
1285 DBG1(DBG_KNL, "unable to get interface for %H", ip);
1286 }
1287 return ifindex;
1288 }
1289
1290 /**
1291 * Manages the creation and deletion of ip addresses on an interface.
1292 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1293 */
1294 static status_t manage_ipaddr(private_kernel_interface_t *this, int nlmsg_type,
1295 int flags, int if_index, host_t *ip)
1296 {
1297 unsigned char request[BUFFER_SIZE];
1298 struct nlmsghdr *hdr;
1299 struct ifaddrmsg *msg;
1300 chunk_t chunk;
1301
1302 memset(&request, 0, sizeof(request));
1303
1304 chunk = ip->get_address(ip);
1305
1306 hdr = (struct nlmsghdr*)request;
1307 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1308 hdr->nlmsg_type = nlmsg_type;
1309 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1310
1311 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1312 msg->ifa_family = ip->get_family(ip);
1313 msg->ifa_flags = 0;
1314 msg->ifa_prefixlen = 8 * chunk.len;
1315 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1316 msg->ifa_index = if_index;
1317
1318 add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1319
1320 return netlink_send_ack(this, this->socket_rt, hdr);
1321 }
1322
1323 /**
1324 * Manages source routes in the routing table.
1325 * By setting the appropriate nlmsg_type, the route added or r.
1326 */
1327 static status_t manage_srcroute(private_kernel_interface_t *this, int nlmsg_type,
1328 int flags, route_entry_t *route)
1329 {
1330 unsigned char request[BUFFER_SIZE];
1331 struct nlmsghdr *hdr;
1332 struct rtmsg *msg;
1333 chunk_t chunk;
1334
1335 /* if route is 0.0.0.0/0, we can't install it, as it would
1336 * overwrite the default route. Instead, we add two routes:
1337 * 0.0.0.0/1 and 128.0.0.0/1
1338 * TODO: use metrics instead */
1339 if (route->prefixlen == 0)
1340 {
1341 route_entry_t half;
1342 status_t status;
1343
1344 half.dst_net = chunk_alloca(route->dst_net.len);
1345 memset(half.dst_net.ptr, 0, half.dst_net.len);
1346 half.src_ip = route->src_ip;
1347 half.gateway = route->gateway;
1348 half.if_index = route->if_index;
1349 half.prefixlen = 1;
1350
1351 status = manage_srcroute(this, nlmsg_type, flags, &half);
1352 half.dst_net.ptr[0] |= 0x80;
1353 status = manage_srcroute(this, nlmsg_type, flags, &half);
1354 return status;
1355 }
1356
1357 memset(&request, 0, sizeof(request));
1358
1359 hdr = (struct nlmsghdr*)request;
1360 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1361 hdr->nlmsg_type = nlmsg_type;
1362 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1363
1364 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1365 msg->rtm_family = route->src_ip->get_family(route->src_ip);
1366 msg->rtm_dst_len = route->prefixlen;
1367 msg->rtm_table = RT_TABLE_MAIN;
1368 msg->rtm_protocol = RTPROT_STATIC;
1369 msg->rtm_type = RTN_UNICAST;
1370 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1371
1372 add_attribute(hdr, RTA_DST, route->dst_net, sizeof(request));
1373 chunk = route->src_ip->get_address(route->src_ip);
1374 add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1375 chunk = route->gateway->get_address(route->gateway);
1376 add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1377 chunk.ptr = (char*)&route->if_index;
1378 chunk.len = sizeof(route->if_index);
1379 add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1380
1381 return netlink_send_ack(this, this->socket_rt, hdr);
1382 }
1383
1384 /**
1385 * Get the nexthop gateway for dest; or the source addr if gateway = FALSE
1386 */
1387 static host_t* get_addr(private_kernel_interface_t *this,
1388 host_t *dest, bool gateway)
1389 {
1390 unsigned char request[BUFFER_SIZE];
1391 struct nlmsghdr *hdr, *out, *current;
1392 struct rtmsg *msg;
1393 chunk_t chunk;
1394 size_t len;
1395 host_t *addr = NULL;
1396
1397 DBG2(DBG_KNL, "getting address to reach %H", dest);
1398
1399 memset(&request, 0, sizeof(request));
1400
1401 hdr = (struct nlmsghdr*)request;
1402 hdr->nlmsg_flags = NLM_F_REQUEST;
1403 hdr->nlmsg_type = RTM_GETROUTE;
1404 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1405
1406 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1407 msg->rtm_family = dest->get_family(dest);
1408 msg->rtm_dst_len = msg->rtm_family == AF_INET ? 32 : 128;
1409 msg->rtm_table = RT_TABLE_MAIN;
1410 msg->rtm_protocol = RTPROT_STATIC;
1411 msg->rtm_type = RTN_UNICAST;
1412 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1413
1414 chunk = dest->get_address(dest);
1415 add_attribute(hdr, RTA_DST, chunk, sizeof(request));
1416
1417 if (netlink_send(this, this->socket_rt, hdr, &out, &len) != SUCCESS)
1418 {
1419 DBG1(DBG_KNL, "getting address to %H failed", dest);
1420 return NULL;
1421 }
1422 current = out;
1423 while (NLMSG_OK(current, len))
1424 {
1425 switch (current->nlmsg_type)
1426 {
1427 case NLMSG_DONE:
1428 break;
1429 case RTM_NEWROUTE:
1430 {
1431 struct rtattr *rta;
1432 size_t rtasize;
1433
1434 msg = (struct rtmsg*)(NLMSG_DATA(current));
1435 rta = RTM_RTA(msg);
1436 rtasize = RTM_PAYLOAD(current);
1437 while(RTA_OK(rta, rtasize))
1438 {
1439 if ((rta->rta_type == RTA_PREFSRC && !gateway) ||
1440 (rta->rta_type == RTA_GATEWAY && gateway))
1441 {
1442 chunk.ptr = RTA_DATA(rta);
1443 chunk.len = RTA_PAYLOAD(rta);
1444 addr = host_create_from_chunk(msg->rtm_family,
1445 chunk, 0);
1446 break;
1447 }
1448 rta = RTA_NEXT(rta, rtasize);
1449 }
1450 break;
1451 }
1452 default:
1453 current = NLMSG_NEXT(current, len);
1454 continue;
1455 }
1456 break;
1457 }
1458 free(out);
1459 if (addr == NULL)
1460 {
1461 DBG2(DBG_KNL, "no route found to %H", dest);
1462 }
1463 return addr;
1464 }
1465
1466 /**
1467 * Implementation of kernel_interface_t.get_source_addr.
1468 */
1469 static host_t* get_source_addr(private_kernel_interface_t *this, host_t *dest)
1470 {
1471 return get_addr(this, dest, FALSE);
1472 }
1473
1474 /**
1475 * Implementation of kernel_interface_t.add_ip.
1476 */
1477 static status_t add_ip(private_kernel_interface_t *this,
1478 host_t *virtual_ip, host_t *iface_ip)
1479 {
1480 iface_entry_t *iface;
1481 addr_entry_t *addr;
1482 iterator_t *addrs, *ifaces;
1483
1484 DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
1485
1486 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1487 while (ifaces->iterate(ifaces, (void**)&iface))
1488 {
1489 bool iface_found = FALSE;
1490
1491 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1492 while (addrs->iterate(addrs, (void**)&addr))
1493 {
1494 if (iface_ip->ip_equals(iface_ip, addr->ip))
1495 {
1496 iface_found = TRUE;
1497 }
1498 else if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1499 {
1500 addr->refcount++;
1501 DBG2(DBG_KNL, "virtual IP %H already installed on %s",
1502 virtual_ip, iface->ifname);
1503 addrs->destroy(addrs);
1504 ifaces->destroy(ifaces);
1505 return SUCCESS;
1506 }
1507 }
1508 addrs->destroy(addrs);
1509
1510 if (iface_found)
1511 {
1512 int ifindex = iface->ifindex;
1513 ifaces->destroy(ifaces);
1514 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1515 ifindex, virtual_ip) == SUCCESS)
1516 {
1517 addr = malloc_thing(addr_entry_t);
1518 addr->ip = virtual_ip->clone(virtual_ip);
1519 addr->refcount = 1;
1520 addr->virtual = TRUE;
1521 addr->scope = RT_SCOPE_UNIVERSE;
1522 pthread_mutex_lock(&this->mutex);
1523 iface->addrs->insert_last(iface->addrs, addr);
1524 pthread_mutex_unlock(&this->mutex);
1525 return SUCCESS;
1526 }
1527 DBG2(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1528 return FAILED;
1529
1530 }
1531
1532 }
1533 ifaces->destroy(ifaces);
1534
1535 DBG2(DBG_KNL, "interface address %H not found, unable to install"
1536 "virtual IP %H", iface_ip, virtual_ip);
1537 return FAILED;
1538 }
1539
1540 /**
1541 * Implementation of kernel_interface_t.del_ip.
1542 */
1543 static status_t del_ip(private_kernel_interface_t *this, host_t *virtual_ip)
1544 {
1545 iface_entry_t *iface;
1546 addr_entry_t *addr;
1547 iterator_t *addrs, *ifaces;
1548
1549 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1550
1551 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1552 while (ifaces->iterate(ifaces, (void**)&iface))
1553 {
1554 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1555 while (addrs->iterate(addrs, (void**)&addr))
1556 {
1557 if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1558 {
1559 int ifindex = iface->ifindex;
1560 addr->refcount--;
1561 if (addr->refcount == 0)
1562 {
1563 addrs->remove(addrs);
1564 addrs->destroy(addrs);
1565 ifaces->destroy(ifaces);
1566 addr_entry_destroy(addr);
1567 return manage_ipaddr(this, RTM_DELADDR, 0,
1568 ifindex, virtual_ip);
1569 }
1570 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1571 virtual_ip);
1572 addrs->destroy(addrs);
1573 ifaces->destroy(ifaces);
1574 return SUCCESS;
1575 }
1576 }
1577 addrs->destroy(addrs);
1578 }
1579 ifaces->destroy(ifaces);
1580
1581 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1582 return FAILED;
1583 }
1584
1585 /**
1586 * Implementation of kernel_interface_t.get_spi.
1587 */
1588 static status_t get_spi(private_kernel_interface_t *this,
1589 host_t *src, host_t *dst,
1590 protocol_id_t protocol, u_int32_t reqid,
1591 u_int32_t *spi)
1592 {
1593 unsigned char request[BUFFER_SIZE];
1594 struct nlmsghdr *hdr, *out;
1595 struct xfrm_userspi_info *userspi;
1596 u_int32_t received_spi = 0;
1597 size_t len;
1598
1599 memset(&request, 0, sizeof(request));
1600
1601 DBG2(DBG_KNL, "getting SPI for reqid %d", reqid);
1602
1603 hdr = (struct nlmsghdr*)request;
1604 hdr->nlmsg_flags = NLM_F_REQUEST;
1605 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
1606 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
1607
1608 userspi = (struct xfrm_userspi_info*)NLMSG_DATA(hdr);
1609 host2xfrm(src, &userspi->info.saddr);
1610 host2xfrm(dst, &userspi->info.id.daddr);
1611 userspi->info.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1612 userspi->info.mode = TRUE; /* tunnel mode */
1613 userspi->info.reqid = reqid;
1614 userspi->info.family = src->get_family(src);
1615 userspi->min = 0xc0000000;
1616 userspi->max = 0xcFFFFFFF;
1617
1618 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1619 {
1620 hdr = out;
1621 while (NLMSG_OK(hdr, len))
1622 {
1623 switch (hdr->nlmsg_type)
1624 {
1625 case XFRM_MSG_NEWSA:
1626 {
1627 struct xfrm_usersa_info* usersa = NLMSG_DATA(hdr);
1628 received_spi = usersa->id.spi;
1629 break;
1630 }
1631 case NLMSG_ERROR:
1632 {
1633 struct nlmsgerr *err = NLMSG_DATA(hdr);
1634
1635 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
1636 strerror(-err->error), -err->error);
1637 break;
1638 }
1639 default:
1640 hdr = NLMSG_NEXT(hdr, len);
1641 continue;
1642 case NLMSG_DONE:
1643 break;
1644 }
1645 break;
1646 }
1647 free(out);
1648 }
1649
1650 if (received_spi == 0)
1651 {
1652 DBG1(DBG_KNL, "unable to get SPI for reqid %d", reqid);
1653 return FAILED;
1654 }
1655
1656 DBG2(DBG_KNL, "got SPI 0x%x for reqid %d", received_spi, reqid);
1657
1658 *spi = received_spi;
1659 return SUCCESS;
1660 }
1661
1662 /**
1663 * Implementation of kernel_interface_t.add_sa.
1664 */
1665 static status_t add_sa(private_kernel_interface_t *this,
1666 host_t *src, host_t *dst, u_int32_t spi,
1667 protocol_id_t protocol, u_int32_t reqid,
1668 u_int64_t expire_soft, u_int64_t expire_hard,
1669 algorithm_t *enc_alg, algorithm_t *int_alg,
1670 prf_plus_t *prf_plus, mode_t mode, bool encap,
1671 bool replace)
1672 {
1673 unsigned char request[BUFFER_SIZE];
1674 char *alg_name;
1675 u_int key_size;
1676 struct nlmsghdr *hdr;
1677 struct xfrm_usersa_info *sa;
1678
1679 memset(&request, 0, sizeof(request));
1680
1681 DBG2(DBG_KNL, "adding SAD entry with SPI 0x%x", spi);
1682
1683 hdr = (struct nlmsghdr*)request;
1684 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1685 hdr->nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
1686 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1687
1688 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
1689 host2xfrm(src, &sa->saddr);
1690 host2xfrm(dst, &sa->id.daddr);
1691 sa->id.spi = spi;
1692 sa->id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1693 sa->family = src->get_family(src);
1694 sa->mode = mode;
1695 sa->replay_window = 32;
1696 sa->reqid = reqid;
1697 /* we currently do not expire SAs by volume/packet count */
1698 sa->lft.soft_byte_limit = XFRM_INF;
1699 sa->lft.hard_byte_limit = XFRM_INF;
1700 sa->lft.soft_packet_limit = XFRM_INF;
1701 sa->lft.hard_packet_limit = XFRM_INF;
1702 /* we use lifetimes since added, not since used */
1703 sa->lft.soft_add_expires_seconds = expire_soft;
1704 sa->lft.hard_add_expires_seconds = expire_hard;
1705 sa->lft.soft_use_expires_seconds = 0;
1706 sa->lft.hard_use_expires_seconds = 0;
1707
1708 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_usersa_info);
1709
1710 if (enc_alg->algorithm != ENCR_UNDEFINED)
1711 {
1712 rthdr->rta_type = XFRMA_ALG_CRYPT;
1713 alg_name = lookup_algorithm(encryption_algs, enc_alg, &key_size);
1714 if (alg_name == NULL)
1715 {
1716 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1717 encryption_algorithm_names, enc_alg->algorithm);
1718 return FAILED;
1719 }
1720 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1721 encryption_algorithm_names, enc_alg->algorithm, key_size);
1722
1723 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
1724 hdr->nlmsg_len += rthdr->rta_len;
1725 if (hdr->nlmsg_len > sizeof(request))
1726 {
1727 return FAILED;
1728 }
1729
1730 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1731 algo->alg_key_len = key_size;
1732 strcpy(algo->alg_name, alg_name);
1733 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
1734
1735 rthdr = XFRM_RTA_NEXT(rthdr);
1736 }
1737
1738 if (int_alg->algorithm != AUTH_UNDEFINED)
1739 {
1740 rthdr->rta_type = XFRMA_ALG_AUTH;
1741 alg_name = lookup_algorithm(integrity_algs, int_alg, &key_size);
1742 if (alg_name == NULL)
1743 {
1744 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1745 integrity_algorithm_names, int_alg->algorithm);
1746 return FAILED;
1747 }
1748 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1749 integrity_algorithm_names, int_alg->algorithm, key_size);
1750
1751 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
1752 hdr->nlmsg_len += rthdr->rta_len;
1753 if (hdr->nlmsg_len > sizeof(request))
1754 {
1755 return FAILED;
1756 }
1757
1758 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1759 algo->alg_key_len = key_size;
1760 strcpy(algo->alg_name, alg_name);
1761 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
1762
1763 rthdr = XFRM_RTA_NEXT(rthdr);
1764 }
1765
1766 /* TODO: add IPComp here */
1767
1768 if (encap)
1769 {
1770 rthdr->rta_type = XFRMA_ENCAP;
1771 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1772
1773 hdr->nlmsg_len += rthdr->rta_len;
1774 if (hdr->nlmsg_len > sizeof(request))
1775 {
1776 return FAILED;
1777 }
1778
1779 struct xfrm_encap_tmpl* tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
1780 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1781 tmpl->encap_sport = htons(src->get_port(src));
1782 tmpl->encap_dport = htons(dst->get_port(dst));
1783 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1784 /* encap_oa could probably be derived from the
1785 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1786 * pluto does the same as we do here but it uses encap_oa in the
1787 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1788 * the kernel ignores it anyway
1789 * -> does that mean that NAT-T encap doesn't work in transport mode?
1790 * No. The reason the kernel ignores NAT-OA is that it recomputes
1791 * (or, rather, just ignores) the checksum. If packets pass
1792 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1793 rthdr = XFRM_RTA_NEXT(rthdr);
1794 }
1795
1796 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
1797 {
1798 DBG1(DBG_KNL, "unable to add SAD entry with SPI 0x%x", spi);
1799 return FAILED;
1800 }
1801 return SUCCESS;
1802 }
1803
1804 /**
1805 * Implementation of kernel_interface_t.update_sa.
1806 */
1807 static status_t update_sa(private_kernel_interface_t *this,
1808 u_int32_t spi, protocol_id_t protocol,
1809 host_t *src, host_t *dst,
1810 host_t *new_src, host_t *new_dst, bool encap)
1811 {
1812 unsigned char request[BUFFER_SIZE], *pos;
1813 struct nlmsghdr *hdr, *out = NULL;
1814 struct xfrm_usersa_id *sa_id;
1815 struct xfrm_usersa_info *out_sa = NULL, *sa;
1816 size_t len;
1817 struct rtattr *rta;
1818 size_t rtasize;
1819 struct xfrm_encap_tmpl* tmpl = NULL;
1820
1821 memset(&request, 0, sizeof(request));
1822
1823 DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x for update", spi);
1824
1825 /* query the exisiting SA first */
1826 hdr = (struct nlmsghdr*)request;
1827 hdr->nlmsg_flags = NLM_F_REQUEST;
1828 hdr->nlmsg_type = XFRM_MSG_GETSA;
1829 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1830
1831 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1832 host2xfrm(dst, &sa_id->daddr);
1833 sa_id->spi = spi;
1834 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1835 sa_id->family = dst->get_family(dst);
1836
1837 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1838 {
1839 hdr = out;
1840 while (NLMSG_OK(hdr, len))
1841 {
1842 switch (hdr->nlmsg_type)
1843 {
1844 case XFRM_MSG_NEWSA:
1845 {
1846 out_sa = NLMSG_DATA(hdr);
1847 break;
1848 }
1849 case NLMSG_ERROR:
1850 {
1851 struct nlmsgerr *err = NLMSG_DATA(hdr);
1852 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1853 strerror(-err->error), -err->error);
1854 break;
1855 }
1856 default:
1857 hdr = NLMSG_NEXT(hdr, len);
1858 continue;
1859 case NLMSG_DONE:
1860 break;
1861 }
1862 break;
1863 }
1864 }
1865 if (out_sa == NULL ||
1866 this->public.del_sa(&this->public, dst, spi, protocol) != SUCCESS)
1867 {
1868 DBG1(DBG_KNL, "unable to update SAD entry with SPI 0x%x", spi);
1869 free(out);
1870 return FAILED;
1871 }
1872
1873 DBG2(DBG_KNL, "updating SAD entry with SPI 0x%x from %#H..%#H to %#H..%#H",
1874 spi, src, dst, new_src, new_dst);
1875
1876 /* copy over the SA from out to request */
1877 hdr = (struct nlmsghdr*)request;
1878 memcpy(hdr, out, min(out->nlmsg_len, sizeof(request)));
1879 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1880 hdr->nlmsg_type = XFRM_MSG_NEWSA;
1881 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1882 sa = NLMSG_DATA(hdr);
1883 sa->family = new_dst->get_family(new_dst);
1884
1885 if (!src->ip_equals(src, new_src))
1886 {
1887 host2xfrm(new_src, &sa->saddr);
1888 }
1889 if (!dst->ip_equals(dst, new_dst))
1890 {
1891 host2xfrm(new_dst, &sa->id.daddr);
1892 }
1893
1894 rta = XFRM_RTA(out, struct xfrm_usersa_info);
1895 rtasize = XFRM_PAYLOAD(out, struct xfrm_usersa_info);
1896 pos = (u_char*)XFRM_RTA(hdr, struct xfrm_usersa_info);
1897 while(RTA_OK(rta, rtasize))
1898 {
1899 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1900 if (rta->rta_type != XFRMA_ENCAP || encap)
1901 {
1902 if (rta->rta_type == XFRMA_ENCAP)
1903 { /* update encap tmpl */
1904 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1905 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1906 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1907 }
1908 memcpy(pos, rta, rta->rta_len);
1909 pos += rta->rta_len;
1910 hdr->nlmsg_len += rta->rta_len;
1911 }
1912 rta = RTA_NEXT(rta, rtasize);
1913 }
1914 if (tmpl == NULL && encap)
1915 { /* add tmpl if we are enabling it */
1916 rta = (struct rtattr*)pos;
1917 rta->rta_type = XFRMA_ENCAP;
1918 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1919 hdr->nlmsg_len += rta->rta_len;
1920 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1921 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1922 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1923 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1924 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1925 }
1926
1927 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
1928 {
1929 DBG1(DBG_KNL, "unable to update SAD entry with SPI 0x%x", spi);
1930 free(out);
1931 return FAILED;
1932 }
1933 free(out);
1934
1935 return SUCCESS;
1936 }
1937
1938 /**
1939 * Implementation of kernel_interface_t.query_sa.
1940 */
1941 static status_t query_sa(private_kernel_interface_t *this, host_t *dst,
1942 u_int32_t spi, protocol_id_t protocol,
1943 u_int32_t *use_time)
1944 {
1945 unsigned char request[BUFFER_SIZE];
1946 struct nlmsghdr *out = NULL, *hdr;
1947 struct xfrm_usersa_id *sa_id;
1948 struct xfrm_usersa_info *sa = NULL;
1949 size_t len;
1950
1951 DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x", spi);
1952 memset(&request, 0, sizeof(request));
1953
1954 hdr = (struct nlmsghdr*)request;
1955 hdr->nlmsg_flags = NLM_F_REQUEST;
1956 hdr->nlmsg_type = XFRM_MSG_GETSA;
1957 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1958
1959 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1960 host2xfrm(dst, &sa_id->daddr);
1961 sa_id->spi = spi;
1962 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1963 sa_id->family = dst->get_family(dst);
1964
1965 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1966 {
1967 hdr = out;
1968 while (NLMSG_OK(hdr, len))
1969 {
1970 switch (hdr->nlmsg_type)
1971 {
1972 case XFRM_MSG_NEWSA:
1973 {
1974 sa = NLMSG_DATA(hdr);
1975 break;
1976 }
1977 case NLMSG_ERROR:
1978 {
1979 struct nlmsgerr *err = NLMSG_DATA(hdr);
1980 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1981 strerror(-err->error), -err->error);
1982 break;
1983 }
1984 default:
1985 hdr = NLMSG_NEXT(hdr, len);
1986 continue;
1987 case NLMSG_DONE:
1988 break;
1989 }
1990 break;
1991 }
1992 }
1993
1994 if (sa == NULL)
1995 {
1996 DBG1(DBG_KNL, "unable to query SAD entry with SPI 0x%x", spi);
1997 free(out);
1998 return FAILED;
1999 }
2000
2001 *use_time = sa->curlft.use_time;
2002 free (out);
2003 return SUCCESS;
2004 }
2005
2006 /**
2007 * Implementation of kernel_interface_t.del_sa.
2008 */
2009 static status_t del_sa(private_kernel_interface_t *this, host_t *dst,
2010 u_int32_t spi, protocol_id_t protocol)
2011 {
2012 unsigned char request[BUFFER_SIZE];
2013 struct nlmsghdr *hdr;
2014 struct xfrm_usersa_id *sa_id;
2015
2016 memset(&request, 0, sizeof(request));
2017
2018 DBG2(DBG_KNL, "deleting SAD entry with SPI 0x%x", spi);
2019
2020 hdr = (struct nlmsghdr*)request;
2021 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2022 hdr->nlmsg_type = XFRM_MSG_DELSA;
2023 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
2024
2025 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
2026 host2xfrm(dst, &sa_id->daddr);
2027 sa_id->spi = spi;
2028 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
2029 sa_id->family = dst->get_family(dst);
2030
2031 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2032 {
2033 DBG1(DBG_KNL, "unable to delete SAD entry with SPI 0x%x", spi);
2034 return FAILED;
2035 }
2036 DBG2(DBG_KNL, "deleted SAD entry with SPI 0x%x", spi);
2037 return SUCCESS;
2038 }
2039
2040 /**
2041 * Implementation of kernel_interface_t.add_policy.
2042 */
2043 static status_t add_policy(private_kernel_interface_t *this,
2044 host_t *src, host_t *dst,
2045 traffic_selector_t *src_ts,
2046 traffic_selector_t *dst_ts,
2047 policy_dir_t direction, protocol_id_t protocol,
2048 u_int32_t reqid, bool high_prio, mode_t mode)
2049 {
2050 iterator_t *iterator;
2051 policy_entry_t *current, *policy;
2052 bool found = FALSE;
2053 unsigned char request[BUFFER_SIZE];
2054 struct xfrm_userpolicy_info *policy_info;
2055 struct nlmsghdr *hdr;
2056
2057 /* create a policy */
2058 policy = malloc_thing(policy_entry_t);
2059 memset(policy, 0, sizeof(policy_entry_t));
2060 policy->sel = ts2selector(src_ts, dst_ts);
2061 policy->direction = direction;
2062
2063 /* find the policy, which matches EXACTLY */
2064 pthread_mutex_lock(&this->mutex);
2065 iterator = this->policies->create_iterator(this->policies, TRUE);
2066 while (iterator->iterate(iterator, (void**)&current))
2067 {
2068 if (memcmp(&current->sel, &policy->sel, sizeof(struct xfrm_selector)) == 0 &&
2069 policy->direction == current->direction)
2070 {
2071 /* use existing policy */
2072 current->refcount++;
2073 DBG2(DBG_KNL, "policy %R===%R already exists, increasing ",
2074 "refcount", src_ts, dst_ts);
2075 free(policy);
2076 policy = current;
2077 found = TRUE;
2078 break;
2079 }
2080 }
2081 iterator->destroy(iterator);
2082 if (!found)
2083 { /* apply the new one, if we have no such policy */
2084 this->policies->insert_last(this->policies, policy);
2085 policy->refcount = 1;
2086 }
2087
2088 DBG2(DBG_KNL, "adding policy %R===%R", src_ts, dst_ts);
2089
2090 memset(&request, 0, sizeof(request));
2091 hdr = (struct nlmsghdr*)request;
2092 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2093 hdr->nlmsg_type = XFRM_MSG_UPDPOLICY;
2094 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
2095
2096 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
2097 policy_info->sel = policy->sel;
2098 policy_info->dir = policy->direction;
2099 /* calculate priority based on source selector size, small size = high prio */
2100 policy_info->priority = high_prio ? PRIO_HIGH : PRIO_LOW;
2101 policy_info->priority -= policy->sel.prefixlen_s * 10;
2102 policy_info->priority -= policy->sel.proto ? 2 : 0;
2103 policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
2104 policy_info->action = XFRM_POLICY_ALLOW;
2105 policy_info->share = XFRM_SHARE_ANY;
2106 pthread_mutex_unlock(&this->mutex);
2107
2108 /* policies don't expire */
2109 policy_info->lft.soft_byte_limit = XFRM_INF;
2110 policy_info->lft.soft_packet_limit = XFRM_INF;
2111 policy_info->lft.hard_byte_limit = XFRM_INF;
2112 policy_info->lft.hard_packet_limit = XFRM_INF;
2113 policy_info->lft.soft_add_expires_seconds = 0;
2114 policy_info->lft.hard_add_expires_seconds = 0;
2115 policy_info->lft.soft_use_expires_seconds = 0;
2116 policy_info->lft.hard_use_expires_seconds = 0;
2117
2118 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
2119 rthdr->rta_type = XFRMA_TMPL;
2120
2121 rthdr->rta_len = sizeof(struct xfrm_user_tmpl);
2122 rthdr->rta_len = RTA_LENGTH(rthdr->rta_len);
2123
2124 hdr->nlmsg_len += rthdr->rta_len;
2125 if (hdr->nlmsg_len > sizeof(request))
2126 {
2127 return FAILED;
2128 }
2129
2130 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
2131 tmpl->reqid = reqid;
2132 tmpl->id.proto = (protocol == PROTO_AH) ? KERNEL_AH : KERNEL_ESP;
2133 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
2134 tmpl->mode = mode;
2135 tmpl->family = src->get_family(src);
2136
2137 host2xfrm(src, &tmpl->saddr);
2138 host2xfrm(dst, &tmpl->id.daddr);
2139
2140 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2141 {
2142 DBG1(DBG_KNL, "unable to add policy %R===%R", src_ts, dst_ts);
2143 return FAILED;
2144 }
2145
2146 /* install a route, if:
2147 * - we are NOT updating a policy
2148 * - this is a forward policy (to just get one for each child)
2149 * - we are in tunnel mode
2150 * - we are not using IPv6 (does not work correctly yet!)
2151 */
2152 if (policy->route == NULL && direction == POLICY_FWD &&
2153 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6)
2154 {
2155 policy->route = malloc_thing(route_entry_t);
2156 if (get_address_by_ts(this, dst_ts, &policy->route->src_ip) == SUCCESS)
2157 {
2158 /* if we have a gateway (via), we use it. If it's direct, we
2159 * use the peers address (which is src, as we are in POLICY_FWD).*/
2160 policy->route->gateway = get_addr(this, src, TRUE);
2161 if (policy->route->gateway == NULL)
2162 {
2163 policy->route->gateway = src->clone(src);
2164 }
2165 policy->route->if_index = get_interface_index(this, dst);
2166 policy->route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
2167 memcpy(policy->route->dst_net.ptr, &policy->sel.saddr, policy->route->dst_net.len);
2168 policy->route->prefixlen = policy->sel.prefixlen_s;
2169
2170 if (manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
2171 policy->route) != SUCCESS)
2172 {
2173 DBG1(DBG_KNL, "unable to install source route for %H",
2174 policy->route->src_ip);
2175 route_entry_destroy(policy->route);
2176 policy->route = NULL;
2177 }
2178 }
2179 else
2180 {
2181 free(policy->route);
2182 policy->route = NULL;
2183 }
2184 }
2185
2186 return SUCCESS;
2187 }
2188
2189 /**
2190 * Implementation of kernel_interface_t.query_policy.
2191 */
2192 static status_t query_policy(private_kernel_interface_t *this,
2193 traffic_selector_t *src_ts,
2194 traffic_selector_t *dst_ts,
2195 policy_dir_t direction, u_int32_t *use_time)
2196 {
2197 unsigned char request[BUFFER_SIZE];
2198 struct nlmsghdr *out = NULL, *hdr;
2199 struct xfrm_userpolicy_id *policy_id;
2200 struct xfrm_userpolicy_info *policy = NULL;
2201 size_t len;
2202
2203 memset(&request, 0, sizeof(request));
2204
2205 DBG2(DBG_KNL, "querying policy %R===%R", src_ts, dst_ts);
2206
2207 hdr = (struct nlmsghdr*)request;
2208 hdr->nlmsg_flags = NLM_F_REQUEST;
2209 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
2210 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2211
2212 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
2213 policy_id->sel = ts2selector(src_ts, dst_ts);
2214 policy_id->dir = direction;
2215
2216 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
2217 {
2218 hdr = out;
2219 while (NLMSG_OK(hdr, len))
2220 {
2221 switch (hdr->nlmsg_type)
2222 {
2223 case XFRM_MSG_NEWPOLICY:
2224 {
2225 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
2226 break;
2227 }
2228 case NLMSG_ERROR:
2229 {
2230 struct nlmsgerr *err = NLMSG_DATA(hdr);
2231 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
2232 strerror(-err->error), -err->error);
2233 break;
2234 }
2235 default:
2236 hdr = NLMSG_NEXT(hdr, len);
2237 continue;
2238 case NLMSG_DONE:
2239 break;
2240 }
2241 break;
2242 }
2243 }
2244
2245 if (policy == NULL)
2246 {
2247 DBG2(DBG_KNL, "unable to query policy %R===%R", src_ts, dst_ts);
2248 free(out);
2249 return FAILED;
2250 }
2251 *use_time = (time_t)policy->curlft.use_time;
2252
2253 free(out);
2254 return SUCCESS;
2255 }
2256
2257 /**
2258 * Implementation of kernel_interface_t.del_policy.
2259 */
2260 static status_t del_policy(private_kernel_interface_t *this,
2261 traffic_selector_t *src_ts,
2262 traffic_selector_t *dst_ts,
2263 policy_dir_t direction)
2264 {
2265 policy_entry_t *current, policy, *to_delete = NULL;
2266 route_entry_t *route;
2267 unsigned char request[BUFFER_SIZE];
2268 struct nlmsghdr *hdr;
2269 struct xfrm_userpolicy_id *policy_id;
2270 iterator_t *iterator;
2271
2272 DBG2(DBG_KNL, "deleting policy %R===%R", src_ts, dst_ts);
2273
2274 /* create a policy */
2275 memset(&policy, 0, sizeof(policy_entry_t));
2276 policy.sel = ts2selector(src_ts, dst_ts);
2277 policy.direction = direction;
2278
2279 /* find the policy */
2280 iterator = this->policies->create_iterator_locked(this->policies, &this->mutex);
2281 while (iterator->iterate(iterator, (void**)&current))
2282 {
2283 if (memcmp(&current->sel, &policy.sel, sizeof(struct xfrm_selector)) == 0 &&
2284 policy.direction == current->direction)
2285 {
2286 to_delete = current;
2287 if (--to_delete->refcount > 0)
2288 {
2289 /* is used by more SAs, keep in kernel */
2290 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
2291 iterator->destroy(iterator);
2292 return SUCCESS;
2293 }
2294 /* remove if last reference */
2295 iterator->remove(iterator);
2296 break;
2297 }
2298 }
2299 iterator->destroy(iterator);
2300 if (!to_delete)
2301 {
2302 DBG1(DBG_KNL, "deleting policy %R===%R failed, not found", src_ts, dst_ts);
2303 return NOT_FOUND;
2304 }
2305
2306 memset(&request, 0, sizeof(request));
2307
2308 hdr = (struct nlmsghdr*)request;
2309 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2310 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
2311 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2312
2313 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
2314 policy_id->sel = to_delete->sel;
2315 policy_id->dir = direction;
2316
2317 route = to_delete->route;
2318 free(to_delete);
2319
2320 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2321 {
2322 DBG1(DBG_KNL, "unable to delete policy %R===%R", src_ts, dst_ts);
2323 return FAILED;
2324 }
2325
2326 if (route)
2327 {
2328 if (manage_srcroute(this, RTM_DELROUTE, 0, route) != SUCCESS)
2329 {
2330 DBG1(DBG_KNL, "error uninstalling route installed with "
2331 "policy %R===%R", src_ts, dst_ts);
2332 }
2333 route_entry_destroy(route);
2334 }
2335 return SUCCESS;
2336 }
2337
2338 /**
2339 * Implementation of kernel_interface_t.destroy.
2340 */
2341 static void destroy(private_kernel_interface_t *this)
2342 {
2343 this->job->cancel(this->job);
2344 close(this->socket_xfrm_events);
2345 close(this->socket_xfrm);
2346 close(this->socket_rt_events);
2347 close(this->socket_rt);
2348 this->policies->destroy(this->policies);
2349 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
2350 free(this);
2351 }
2352
2353 /*
2354 * Described in header.
2355 */
2356 kernel_interface_t *kernel_interface_create()
2357 {
2358 private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
2359 struct sockaddr_nl addr;
2360
2361 /* public functions */
2362 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
2363 this->public.add_sa = (status_t(*)(kernel_interface_t *,host_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t,u_int64_t,u_int64_t,algorithm_t*,algorithm_t*,prf_plus_t*,mode_t,bool,bool))add_sa;
2364 this->public.update_sa = (status_t(*)(kernel_interface_t*,u_int32_t,protocol_id_t,host_t*,host_t*,host_t*,host_t*,bool))update_sa;
2365 this->public.query_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t*))query_sa;
2366 this->public.del_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
2367 this->public.add_policy = (status_t(*)(kernel_interface_t*,host_t*,host_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,protocol_id_t,u_int32_t,bool,mode_t))add_policy;
2368 this->public.query_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
2369 this->public.del_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t))del_policy;
2370 this->public.get_interface = (char*(*)(kernel_interface_t*,host_t*))get_interface_name;
2371 this->public.create_address_iterator = (iterator_t*(*)(kernel_interface_t*))create_address_iterator;
2372 this->public.get_source_addr = (host_t*(*)(kernel_interface_t*, host_t *dest))get_source_addr;
2373 this->public.add_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) add_ip;
2374 this->public.del_ip = (status_t(*)(kernel_interface_t*,host_t*)) del_ip;
2375 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
2376
2377 /* private members */
2378 this->policies = linked_list_create();
2379 this->ifaces = linked_list_create();
2380 this->hiter = NULL;
2381 this->seq = 200;
2382 pthread_mutex_init(&this->mutex,NULL);
2383
2384 memset(&addr, 0, sizeof(addr));
2385 addr.nl_family = AF_NETLINK;
2386
2387 /* create and bind RT socket */
2388 this->socket_rt = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2389 if (this->socket_rt <= 0)
2390 {
2391 charon->kill(charon, "unable to create RT netlink socket");
2392 }
2393 addr.nl_groups = 0;
2394 if (bind(this->socket_rt, (struct sockaddr*)&addr, sizeof(addr)))
2395 {
2396 charon->kill(charon, "unable to bind RT netlink socket");
2397 }
2398
2399 /* create and bind RT socket for events (address/interface/route changes) */
2400 this->socket_rt_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2401 if (this->socket_rt_events <= 0)
2402 {
2403 charon->kill(charon, "unable to create RT event socket");
2404 }
2405 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
2406 RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_LINK;
2407 if (bind(this->socket_rt_events, (struct sockaddr*)&addr, sizeof(addr)))
2408 {
2409 charon->kill(charon, "unable to bind RT event socket");
2410 }
2411
2412 /* create and bind XFRM socket */
2413 this->socket_xfrm = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
2414 if (this->socket_xfrm <= 0)
2415 {
2416 charon->kill(charon, "unable to create XFRM netlink socket");
2417 }
2418 addr.nl_groups = 0;
2419 if (bind(this->socket_xfrm, (struct sockaddr*)&addr, sizeof(addr)))
2420 {
2421 charon->kill(charon, "unable to bind XFRM netlink socket");
2422 }
2423
2424 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
2425 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
2426 if (this->socket_xfrm_events <= 0)
2427 {
2428 charon->kill(charon, "unable to create XFRM event socket");
2429 }
2430 addr.nl_groups = XFRMGRP_ACQUIRE | XFRMGRP_EXPIRE;
2431 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
2432 {
2433 charon->kill(charon, "unable to bind XFRM event socket");
2434 }
2435
2436 this->job = callback_job_create((callback_job_cb_t)receive_events,
2437 this, NULL, NULL);
2438 charon->processor->queue_job(charon->processor, (job_t*)this->job);
2439
2440 if (init_address_list(this) != SUCCESS)
2441 {
2442 charon->kill(charon, "unable to get interface list");
2443 }
2444
2445 return &this->public;
2446 }
2447