c290df2ea110d649a09207e3dd317d50a813516c
[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, (job_t*)roam_job_create());
619 }
620 }
621
622 /**
623 * process RTM_NEWADDR/RTM_DELADDR from kernel
624 */
625 static void process_addr(private_kernel_interface_t *this,
626 struct nlmsghdr *hdr, bool event)
627 {
628 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
629 struct rtattr *rta = IFA_RTA(msg);
630 size_t rtasize = IFA_PAYLOAD (hdr);
631 host_t *host = NULL;
632 iterator_t *ifaces, *addrs;
633 iface_entry_t *iface;
634 addr_entry_t *addr;
635 chunk_t local = chunk_empty, address = chunk_empty;
636 bool update = FALSE, found = FALSE;
637
638 while(RTA_OK(rta, rtasize))
639 {
640 switch (rta->rta_type)
641 {
642 case IFA_LOCAL:
643 local.ptr = RTA_DATA(rta);
644 local.len = RTA_PAYLOAD(rta);
645 break;
646 case IFA_ADDRESS:
647 address.ptr = RTA_DATA(rta);
648 address.len = RTA_PAYLOAD(rta);
649 break;
650 }
651 rta = RTA_NEXT(rta, rtasize);
652 }
653
654 /* For PPP interfaces, we need the IFA_LOCAL address,
655 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
656 * not included in all cases (IPv6?), so fallback to IFA_ADDRESS. */
657 if (local.ptr)
658 {
659 host = host_create_from_chunk(msg->ifa_family, local, 0);
660 }
661 else if (address.ptr)
662 {
663 host = host_create_from_chunk(msg->ifa_family, address, 0);
664 }
665
666 if (host == NULL)
667 { /* bad family? */
668 return;
669 }
670
671 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
672 while (ifaces->iterate(ifaces, (void**)&iface))
673 {
674 if (iface->ifindex == msg->ifa_index)
675 {
676 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
677 while (addrs->iterate(addrs, (void**)&addr))
678 {
679 if (host->ip_equals(host, addr->ip))
680 {
681 found = TRUE;
682 if (hdr->nlmsg_type == RTM_DELADDR)
683 {
684 addrs->remove(addrs);
685 addr_entry_destroy(addr);
686 DBG1(DBG_KNL, "%H disappeared from %s", host, iface->ifname);
687 }
688 }
689 }
690 addrs->destroy(addrs);
691
692 if (hdr->nlmsg_type == RTM_NEWADDR)
693 {
694 if (!found)
695 {
696 found = TRUE;
697 addr = malloc_thing(addr_entry_t);
698 addr->ip = host->clone(host);
699 addr->virtual = FALSE;
700 addr->refcount = 1;
701 addr->scope = msg->ifa_scope;
702
703 iface->addrs->insert_last(iface->addrs, addr);
704 if (event)
705 {
706 DBG1(DBG_KNL, "%H appeared on %s", host, iface->ifname);
707 }
708 }
709 }
710 if (found && (iface->flags & IFF_UP))
711 {
712 update = TRUE;
713 }
714 break;
715 }
716 }
717 ifaces->destroy(ifaces);
718 host->destroy(host);
719
720 /* send an update to all IKE_SAs */
721 if (update && event)
722 {
723 charon->processor->queue_job(charon->processor, (job_t*)roam_job_create());
724 }
725 }
726
727 /**
728 * Receives events from kernel
729 */
730 static job_requeue_t receive_events(private_kernel_interface_t *this)
731 {
732 char response[1024];
733 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
734 struct sockaddr_nl addr;
735 socklen_t addr_len = sizeof(addr);
736 int len, oldstate, maxfd, selected;
737 fd_set rfds;
738
739 FD_ZERO(&rfds);
740 FD_SET(this->socket_xfrm_events, &rfds);
741 FD_SET(this->socket_rt_events, &rfds);
742 maxfd = max(this->socket_xfrm_events, this->socket_rt_events);
743
744 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
745 selected = select(maxfd + 1, &rfds, NULL, NULL, NULL);
746 pthread_setcancelstate(oldstate, NULL);
747 if (selected <= 0)
748 {
749 DBG1(DBG_KNL, "selecting on sockets failed: %s", strerror(errno));
750 return JOB_REQUEUE_FAIR;
751 }
752 if (FD_ISSET(this->socket_xfrm_events, &rfds))
753 {
754 selected = this->socket_xfrm_events;
755 }
756 else if (FD_ISSET(this->socket_rt_events, &rfds))
757 {
758 selected = this->socket_rt_events;
759 }
760 else
761 {
762 return JOB_REQUEUE_DIRECT;
763 }
764
765 len = recvfrom(selected, response, sizeof(response), MSG_DONTWAIT,
766 (struct sockaddr*)&addr, &addr_len);
767 if (len < 0)
768 {
769 switch (errno)
770 {
771 case EINTR:
772 /* interrupted, try again */
773 return JOB_REQUEUE_DIRECT;
774 case EAGAIN:
775 /* no data ready, select again */
776 return JOB_REQUEUE_DIRECT;
777 default:
778 DBG1(DBG_KNL, "unable to receive from xfrm event socket");
779 sleep(1);
780 return JOB_REQUEUE_FAIR;
781 }
782 }
783 if (addr.nl_pid != 0)
784 { /* not from kernel. not interested, try another one */
785 return JOB_REQUEUE_DIRECT;
786 }
787
788 while (NLMSG_OK(hdr, len))
789 {
790 /* looks good so far, dispatch netlink message */
791 if (selected == this->socket_xfrm_events)
792 {
793 switch (hdr->nlmsg_type)
794 {
795 case XFRM_MSG_ACQUIRE:
796 process_acquire(this, hdr);
797 break;
798 case XFRM_MSG_EXPIRE:
799 process_expire(this, hdr);
800 break;
801 default:
802 break;
803 }
804 }
805 else if (selected == this->socket_rt_events)
806 {
807 switch (hdr->nlmsg_type)
808 {
809 case RTM_NEWADDR:
810 case RTM_DELADDR:
811 process_addr(this, hdr, TRUE);
812 break;
813 case RTM_NEWLINK:
814 case RTM_DELLINK:
815 process_link(this, hdr, TRUE);
816 break;
817 case RTM_NEWROUTE:
818 case RTM_DELROUTE:
819 charon->processor->queue_job(charon->processor,
820 (job_t*)roam_job_create());
821 break;
822 default:
823 break;
824 }
825 }
826 hdr = NLMSG_NEXT(hdr, len);
827 }
828 return JOB_REQUEUE_DIRECT;
829 }
830
831 /**
832 * send a netlink message and wait for a reply
833 */
834 static status_t netlink_send(private_kernel_interface_t *this,
835 int socket, struct nlmsghdr *in,
836 struct nlmsghdr **out, size_t *out_len)
837 {
838 int len, addr_len;
839 struct sockaddr_nl addr;
840 chunk_t result = chunk_empty, tmp;
841 struct nlmsghdr *msg, peek;
842
843 pthread_mutex_lock(&this->mutex);
844
845 in->nlmsg_seq = ++this->seq;
846 in->nlmsg_pid = getpid();
847
848 memset(&addr, 0, sizeof(addr));
849 addr.nl_family = AF_NETLINK;
850 addr.nl_pid = 0;
851 addr.nl_groups = 0;
852
853 while (TRUE)
854 {
855 len = sendto(socket, in, in->nlmsg_len, 0,
856 (struct sockaddr*)&addr, sizeof(addr));
857
858 if (len != in->nlmsg_len)
859 {
860 if (errno == EINTR)
861 {
862 /* interrupted, try again */
863 continue;
864 }
865 pthread_mutex_unlock(&this->mutex);
866 DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
867 return FAILED;
868 }
869 break;
870 }
871
872 while (TRUE)
873 {
874 char buf[4096];
875 tmp.len = sizeof(buf);
876 tmp.ptr = buf;
877 msg = (struct nlmsghdr*)tmp.ptr;
878
879 memset(&addr, 0, sizeof(addr));
880 addr.nl_family = AF_NETLINK;
881 addr.nl_pid = getpid();
882 addr.nl_groups = 0;
883 addr_len = sizeof(addr);
884
885 len = recvfrom(socket, tmp.ptr, tmp.len, 0,
886 (struct sockaddr*)&addr, &addr_len);
887
888 if (len < 0)
889 {
890 if (errno == EINTR)
891 {
892 DBG1(DBG_KNL, "got interrupted");
893 /* interrupted, try again */
894 continue;
895 }
896 DBG1(DBG_KNL, "error reading from netlink socket: %s", strerror(errno));
897 pthread_mutex_unlock(&this->mutex);
898 return FAILED;
899 }
900 if (!NLMSG_OK(msg, len))
901 {
902 DBG1(DBG_KNL, "received corrupted netlink message");
903 pthread_mutex_unlock(&this->mutex);
904 return FAILED;
905 }
906 if (msg->nlmsg_seq != this->seq)
907 {
908 DBG1(DBG_KNL, "received invalid netlink sequence number");
909 if (msg->nlmsg_seq < this->seq)
910 {
911 continue;
912 }
913 pthread_mutex_unlock(&this->mutex);
914 return FAILED;
915 }
916
917 tmp.len = len;
918 result = chunk_cata("cc", result, tmp);
919
920 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
921 * numbers to detect multi header messages */
922 len = recvfrom(socket, &peek, sizeof(peek), MSG_PEEK | MSG_DONTWAIT,
923 (struct sockaddr*)&addr, &addr_len);
924
925 if (len == sizeof(peek) && peek.nlmsg_seq == this->seq)
926 {
927 /* seems to be multipart */
928 continue;
929 }
930 break;
931 }
932
933 *out_len = result.len;
934 *out = (struct nlmsghdr*)clalloc(result.ptr, result.len);
935
936 pthread_mutex_unlock(&this->mutex);
937
938 return SUCCESS;
939 }
940
941 /**
942 * send a netlink message and wait for its acknowlegde
943 */
944 static status_t netlink_send_ack(private_kernel_interface_t *this,
945 int socket, struct nlmsghdr *in)
946 {
947 struct nlmsghdr *out, *hdr;
948 size_t len;
949
950 if (netlink_send(this, socket, in, &out, &len) != SUCCESS)
951 {
952 return FAILED;
953 }
954 hdr = out;
955 while (NLMSG_OK(hdr, len))
956 {
957 switch (hdr->nlmsg_type)
958 {
959 case NLMSG_ERROR:
960 {
961 struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(hdr);
962
963 if (err->error)
964 {
965 DBG1(DBG_KNL, "received netlink error: %s (%d)",
966 strerror(-err->error), -err->error);
967 free(out);
968 return FAILED;
969 }
970 free(out);
971 return SUCCESS;
972 }
973 default:
974 hdr = NLMSG_NEXT(hdr, len);
975 continue;
976 case NLMSG_DONE:
977 break;
978 }
979 break;
980 }
981 DBG1(DBG_KNL, "netlink request not acknowlegded");
982 free(out);
983 return FAILED;
984 }
985
986 /**
987 * Initialize a list of local addresses.
988 */
989 static status_t init_address_list(private_kernel_interface_t *this)
990 {
991 char request[BUFFER_SIZE];
992 struct nlmsghdr *out, *current, *in;
993 struct rtgenmsg *msg;
994 size_t len;
995 iterator_t *ifaces, *addrs;
996 iface_entry_t *iface;
997 addr_entry_t *addr;
998
999 DBG1(DBG_KNL, "listening on interfaces:");
1000
1001 memset(&request, 0, sizeof(request));
1002
1003 in = (struct nlmsghdr*)&request;
1004 in->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
1005 in->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
1006 msg = (struct rtgenmsg*)NLMSG_DATA(in);
1007 msg->rtgen_family = AF_UNSPEC;
1008
1009 /* get all links */
1010 in->nlmsg_type = RTM_GETLINK;
1011 if (netlink_send(this, this->socket_rt, in, &out, &len) != SUCCESS)
1012 {
1013 return FAILED;
1014 }
1015 current = out;
1016 while (NLMSG_OK(current, len))
1017 {
1018 switch (current->nlmsg_type)
1019 {
1020 case NLMSG_DONE:
1021 break;
1022 case RTM_NEWLINK:
1023 process_link(this, current, FALSE);
1024 /* fall through */
1025 default:
1026 current = NLMSG_NEXT(current, len);
1027 continue;
1028 }
1029 break;
1030 }
1031 free(out);
1032
1033 /* get all interface addresses */
1034 in->nlmsg_type = RTM_GETADDR;
1035 if (netlink_send(this, this->socket_rt, in, &out, &len) != SUCCESS)
1036 {
1037 return FAILED;
1038 }
1039 current = out;
1040 while (NLMSG_OK(current, len))
1041 {
1042 switch (current->nlmsg_type)
1043 {
1044 case NLMSG_DONE:
1045 break;
1046 case RTM_NEWADDR:
1047 process_addr(this, current, FALSE);
1048 /* fall through */
1049 default:
1050 current = NLMSG_NEXT(current, len);
1051 continue;
1052 }
1053 break;
1054 }
1055 free(out);
1056
1057 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1058 while (ifaces->iterate(ifaces, (void**)&iface))
1059 {
1060 if (iface->flags & IFF_UP)
1061 {
1062 DBG1(DBG_KNL, " %s", iface->ifname);
1063 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1064 while (addrs->iterate(addrs, (void**)&addr))
1065 {
1066 DBG1(DBG_KNL, " %H", addr->ip);
1067 }
1068 addrs->destroy(addrs);
1069 }
1070 }
1071 ifaces->destroy(ifaces);
1072 return SUCCESS;
1073 }
1074
1075 /**
1076 * iterator hook to iterate over addrs
1077 */
1078 static hook_result_t addr_hook(private_kernel_interface_t *this,
1079 addr_entry_t *in, host_t **out)
1080 {
1081 if (in->virtual)
1082 { /* skip virtual interfaces added by us */
1083 return HOOK_SKIP;
1084 }
1085 if (in->scope >= RT_SCOPE_LINK)
1086 { /* skip addresses with a unusable scope */
1087 return HOOK_SKIP;
1088 }
1089 *out = in->ip;
1090 return HOOK_NEXT;
1091 }
1092
1093 /**
1094 * iterator hook to iterate over ifaces
1095 */
1096 static hook_result_t iface_hook(private_kernel_interface_t *this,
1097 iface_entry_t *in, host_t **out)
1098 {
1099 if (!(in->flags & IFF_UP))
1100 { /* skip interfaces not up */
1101 return HOOK_SKIP;
1102 }
1103
1104 if (this->hiter == NULL)
1105 {
1106 this->hiter = in->addrs->create_iterator(in->addrs, TRUE);
1107 this->hiter->set_iterator_hook(this->hiter,
1108 (iterator_hook_t*)addr_hook, this);
1109 }
1110 while (this->hiter->iterate(this->hiter, (void**)out))
1111 {
1112 return HOOK_AGAIN;
1113 }
1114 this->hiter->destroy(this->hiter);
1115 this->hiter = NULL;
1116 return HOOK_SKIP;
1117 }
1118
1119 /**
1120 * Implements kernel_interface_t.create_address_iterator.
1121 */
1122 static iterator_t *create_address_iterator(private_kernel_interface_t *this)
1123 {
1124 iterator_t *iterator;
1125
1126 /* This iterator is not only hooked, is is double-hooked. As we have stored
1127 * our addresses in iface_entry->addr_entry->ip, we need to iterate the
1128 * entries in each interface we iterate. This does the iface_hook. The
1129 * addr_hook returns the ip instead of the addr_entry. */
1130
1131 iterator = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1132 iterator->set_iterator_hook(iterator, (iterator_hook_t*)iface_hook, this);
1133 return iterator;
1134 }
1135
1136 /**
1137 * implementation of kernel_interface_t.get_interface_name
1138 */
1139 static char *get_interface_name(private_kernel_interface_t *this, host_t* ip)
1140 {
1141 iterator_t *ifaces, *addrs;
1142 iface_entry_t *iface;
1143 addr_entry_t *addr;
1144 char *name = NULL;
1145
1146 DBG2(DBG_KNL, "getting interface name for %H", ip);
1147
1148 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1149 while (ifaces->iterate(ifaces, (void**)&iface))
1150 {
1151 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1152 while (addrs->iterate(addrs, (void**)&addr))
1153 {
1154 if (ip->ip_equals(ip, addr->ip))
1155 {
1156 name = strdup(iface->ifname);
1157 break;
1158 }
1159 }
1160 addrs->destroy(addrs);
1161 if (name)
1162 {
1163 break;
1164 }
1165 }
1166 ifaces->destroy(ifaces);
1167
1168 if (name)
1169 {
1170 DBG2(DBG_KNL, "%H is on interface %s", ip, name);
1171 }
1172 else
1173 {
1174 DBG2(DBG_KNL, "%H is not a local address", ip);
1175 }
1176 return name;
1177 }
1178
1179 /**
1180 * Tries to find an ip address of a local interface that is included in the
1181 * supplied traffic selector.
1182 */
1183 static status_t get_address_by_ts(private_kernel_interface_t *this,
1184 traffic_selector_t *ts, host_t **ip)
1185 {
1186 iterator_t *ifaces, *addrs;
1187 iface_entry_t *iface;
1188 addr_entry_t *addr;
1189 host_t *host;
1190 int family;
1191 bool found = FALSE;
1192
1193 DBG2(DBG_KNL, "getting a local address in traffic selector %R", ts);
1194
1195 /* if we have a family which includes localhost, we do not
1196 * search for an IP, we use the default */
1197 family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
1198
1199 if (family == AF_INET)
1200 {
1201 host = host_create_from_string("127.0.0.1", 0);
1202 }
1203 else
1204 {
1205 host = host_create_from_string("::1", 0);
1206 }
1207
1208 if (ts->includes(ts, host))
1209 {
1210 *ip = host_create_any(family);
1211 host->destroy(host);
1212 DBG2(DBG_KNL, "using host %H", *ip);
1213 return SUCCESS;
1214 }
1215 host->destroy(host);
1216
1217 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1218 while (ifaces->iterate(ifaces, (void**)&iface))
1219 {
1220 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1221 while (addrs->iterate(addrs, (void**)&addr))
1222 {
1223 if (ts->includes(ts, addr->ip))
1224 {
1225 found = TRUE;
1226 *ip = addr->ip->clone(addr->ip);
1227 break;
1228 }
1229 }
1230 addrs->destroy(addrs);
1231 if (found)
1232 {
1233 break;
1234 }
1235 }
1236 ifaces->destroy(ifaces);
1237
1238 if (!found)
1239 {
1240 DBG1(DBG_KNL, "no local address found in traffic selector %R", ts);
1241 return FAILED;
1242 }
1243 DBG2(DBG_KNL, "using host %H", *ip);
1244 return SUCCESS;
1245 }
1246
1247 /**
1248 * get the interface of a local address
1249 */
1250 static int get_interface_index(private_kernel_interface_t *this, host_t* ip)
1251 {
1252 iterator_t *ifaces, *addrs;
1253 iface_entry_t *iface;
1254 addr_entry_t *addr;
1255 int ifindex = 0;
1256
1257 DBG2(DBG_KNL, "getting iface for %H", ip);
1258
1259 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1260 while (ifaces->iterate(ifaces, (void**)&iface))
1261 {
1262 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1263 while (addrs->iterate(addrs, (void**)&addr))
1264 {
1265 if (ip->ip_equals(ip, addr->ip))
1266 {
1267 ifindex = iface->ifindex;
1268 break;
1269 }
1270 }
1271 addrs->destroy(addrs);
1272 if (ifindex)
1273 {
1274 break;
1275 }
1276 }
1277 ifaces->destroy(ifaces);
1278
1279 if (ifindex == 0)
1280 {
1281 DBG1(DBG_KNL, "unable to get interface for %H", ip);
1282 }
1283 return ifindex;
1284 }
1285
1286 /**
1287 * Manages the creation and deletion of ip addresses on an interface.
1288 * By setting the appropriate nlmsg_type, the ip will be set or unset.
1289 */
1290 static status_t manage_ipaddr(private_kernel_interface_t *this, int nlmsg_type,
1291 int flags, int if_index, host_t *ip)
1292 {
1293 unsigned char request[BUFFER_SIZE];
1294 struct nlmsghdr *hdr;
1295 struct ifaddrmsg *msg;
1296 chunk_t chunk;
1297
1298 memset(&request, 0, sizeof(request));
1299
1300 chunk = ip->get_address(ip);
1301
1302 hdr = (struct nlmsghdr*)request;
1303 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1304 hdr->nlmsg_type = nlmsg_type;
1305 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1306
1307 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
1308 msg->ifa_family = ip->get_family(ip);
1309 msg->ifa_flags = 0;
1310 msg->ifa_prefixlen = 8 * chunk.len;
1311 msg->ifa_scope = RT_SCOPE_UNIVERSE;
1312 msg->ifa_index = if_index;
1313
1314 add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
1315
1316 return netlink_send_ack(this, this->socket_rt, hdr);
1317 }
1318
1319 /**
1320 * Manages source routes in the routing table.
1321 * By setting the appropriate nlmsg_type, the route added or r.
1322 */
1323 static status_t manage_srcroute(private_kernel_interface_t *this, int nlmsg_type,
1324 int flags, route_entry_t *route)
1325 {
1326 unsigned char request[BUFFER_SIZE];
1327 struct nlmsghdr *hdr;
1328 struct rtmsg *msg;
1329 chunk_t chunk;
1330
1331 /* if route is 0.0.0.0/0, we can't install it, as it would
1332 * overwrite the default route. Instead, we add two routes:
1333 * 0.0.0.0/1 and 128.0.0.0/1
1334 * TODO: use metrics instead */
1335 if (route->prefixlen == 0)
1336 {
1337 route_entry_t half;
1338 status_t status;
1339
1340 half.dst_net = chunk_alloca(route->dst_net.len);
1341 memset(half.dst_net.ptr, 0, half.dst_net.len);
1342 half.src_ip = route->src_ip;
1343 half.gateway = route->gateway;
1344 half.if_index = route->if_index;
1345 half.prefixlen = 1;
1346
1347 status = manage_srcroute(this, nlmsg_type, flags, &half);
1348 half.dst_net.ptr[0] |= 0x80;
1349 status = manage_srcroute(this, nlmsg_type, flags, &half);
1350 return status;
1351 }
1352
1353 memset(&request, 0, sizeof(request));
1354
1355 hdr = (struct nlmsghdr*)request;
1356 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1357 hdr->nlmsg_type = nlmsg_type;
1358 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1359
1360 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1361 msg->rtm_family = route->src_ip->get_family(route->src_ip);
1362 msg->rtm_dst_len = route->prefixlen;
1363 msg->rtm_table = RT_TABLE_MAIN;
1364 msg->rtm_protocol = RTPROT_STATIC;
1365 msg->rtm_type = RTN_UNICAST;
1366 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1367
1368 add_attribute(hdr, RTA_DST, route->dst_net, sizeof(request));
1369 chunk = route->src_ip->get_address(route->src_ip);
1370 add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1371 chunk = route->gateway->get_address(route->gateway);
1372 add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1373 chunk.ptr = (char*)&route->if_index;
1374 chunk.len = sizeof(route->if_index);
1375 add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1376
1377 return netlink_send_ack(this, this->socket_rt, hdr);
1378 }
1379
1380 /**
1381 * Implementation of kernel_interface_t.get_source_addr.
1382 */
1383 static host_t* get_source_addr(private_kernel_interface_t *this, host_t *dest)
1384 {
1385 unsigned char request[BUFFER_SIZE];
1386 struct nlmsghdr *hdr, *out, *current;
1387 struct rtmsg *msg;
1388 chunk_t chunk;
1389 size_t len;
1390 host_t *source = NULL;
1391
1392 DBG2(DBG_KNL, "getting source address to reach %H", dest);
1393
1394 memset(&request, 0, sizeof(request));
1395
1396 hdr = (struct nlmsghdr*)request;
1397 hdr->nlmsg_flags = NLM_F_REQUEST;
1398 hdr->nlmsg_type = RTM_GETROUTE;
1399 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1400
1401 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1402 msg->rtm_family = dest->get_family(dest);
1403 msg->rtm_dst_len = msg->rtm_family == AF_INET ? 32 : 128;
1404 msg->rtm_table = RT_TABLE_MAIN;
1405 msg->rtm_protocol = RTPROT_STATIC;
1406 msg->rtm_type = RTN_UNICAST;
1407 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1408
1409 chunk = dest->get_address(dest);
1410 add_attribute(hdr, RTA_DST, chunk, sizeof(request));
1411
1412 if (netlink_send(this, this->socket_rt, hdr, &out, &len) != SUCCESS)
1413 {
1414 DBG1(DBG_KNL, "getting source address to %H failed", dest);
1415 return NULL;
1416 }
1417 current = out;
1418 while (NLMSG_OK(current, len))
1419 {
1420 switch (current->nlmsg_type)
1421 {
1422 case NLMSG_DONE:
1423 break;
1424 case RTM_NEWROUTE:
1425 {
1426 struct rtattr *rta;
1427 size_t rtasize;
1428
1429 msg = (struct rtmsg*)(NLMSG_DATA(current));
1430 rta = RTM_RTA(msg);
1431 rtasize = RTM_PAYLOAD(current);
1432 while(RTA_OK(rta, rtasize))
1433 {
1434 switch (rta->rta_type)
1435 {
1436 case RTA_PREFSRC:
1437 chunk.ptr = RTA_DATA(rta);
1438 chunk.len = RTA_PAYLOAD(rta);
1439 source = host_create_from_chunk(msg->rtm_family,
1440 chunk, 0);
1441 break;
1442 }
1443 rta = RTA_NEXT(rta, rtasize);
1444 }
1445 break;
1446 }
1447 default:
1448 current = NLMSG_NEXT(current, len);
1449 continue;
1450 }
1451 break;
1452 }
1453 free(out);
1454 if (source == NULL)
1455 {
1456 DBG2(DBG_KNL, "no route found to %H", dest);
1457 }
1458 return source;
1459 }
1460
1461 /**
1462 * Implementation of kernel_interface_t.add_ip.
1463 */
1464 static status_t add_ip(private_kernel_interface_t *this,
1465 host_t *virtual_ip, host_t *iface_ip)
1466 {
1467 iface_entry_t *iface;
1468 addr_entry_t *addr;
1469 iterator_t *addrs, *ifaces;
1470
1471 DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
1472
1473 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1474 while (ifaces->iterate(ifaces, (void**)&iface))
1475 {
1476 bool iface_found = FALSE;
1477
1478 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1479 while (addrs->iterate(addrs, (void**)&addr))
1480 {
1481 if (iface_ip->ip_equals(iface_ip, addr->ip))
1482 {
1483 iface_found = TRUE;
1484 }
1485 else if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1486 {
1487 addr->refcount++;
1488 DBG2(DBG_KNL, "virtual IP %H already installed on %s",
1489 virtual_ip, iface->ifname);
1490 addrs->destroy(addrs);
1491 ifaces->destroy(ifaces);
1492 return SUCCESS;
1493 }
1494 }
1495 addrs->destroy(addrs);
1496
1497 if (iface_found)
1498 {
1499 int ifindex = iface->ifindex;
1500 ifaces->destroy(ifaces);
1501 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1502 ifindex, virtual_ip) == SUCCESS)
1503 {
1504 addr = malloc_thing(addr_entry_t);
1505 addr->ip = virtual_ip->clone(virtual_ip);
1506 addr->refcount = 1;
1507 addr->virtual = TRUE;
1508 addr->scope = RT_SCOPE_UNIVERSE;
1509 pthread_mutex_lock(&this->mutex);
1510 iface->addrs->insert_last(iface->addrs, addr);
1511 pthread_mutex_unlock(&this->mutex);
1512 return SUCCESS;
1513 }
1514 DBG2(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
1515 return FAILED;
1516
1517 }
1518
1519 }
1520 ifaces->destroy(ifaces);
1521
1522 DBG2(DBG_KNL, "interface address %H not found, unable to install"
1523 "virtual IP %H", iface_ip, virtual_ip);
1524 return FAILED;
1525 }
1526
1527 /**
1528 * Implementation of kernel_interface_t.del_ip.
1529 */
1530 static status_t del_ip(private_kernel_interface_t *this, host_t *virtual_ip)
1531 {
1532 iface_entry_t *iface;
1533 addr_entry_t *addr;
1534 iterator_t *addrs, *ifaces;
1535
1536 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1537
1538 ifaces = this->ifaces->create_iterator_locked(this->ifaces, &this->mutex);
1539 while (ifaces->iterate(ifaces, (void**)&iface))
1540 {
1541 addrs = iface->addrs->create_iterator(iface->addrs, TRUE);
1542 while (addrs->iterate(addrs, (void**)&addr))
1543 {
1544 if (virtual_ip->ip_equals(virtual_ip, addr->ip))
1545 {
1546 int ifindex = iface->ifindex;
1547 addr->refcount--;
1548 if (addr->refcount == 0)
1549 {
1550 addrs->remove(addrs);
1551 addrs->destroy(addrs);
1552 ifaces->destroy(ifaces);
1553 addr_entry_destroy(addr);
1554 return manage_ipaddr(this, RTM_DELADDR, 0,
1555 ifindex, virtual_ip);
1556 }
1557 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1558 virtual_ip);
1559 addrs->destroy(addrs);
1560 ifaces->destroy(ifaces);
1561 return SUCCESS;
1562 }
1563 }
1564 addrs->destroy(addrs);
1565 }
1566 ifaces->destroy(ifaces);
1567
1568 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1569 return FAILED;
1570 }
1571
1572 /**
1573 * Implementation of kernel_interface_t.get_spi.
1574 */
1575 static status_t get_spi(private_kernel_interface_t *this,
1576 host_t *src, host_t *dst,
1577 protocol_id_t protocol, u_int32_t reqid,
1578 u_int32_t *spi)
1579 {
1580 unsigned char request[BUFFER_SIZE];
1581 struct nlmsghdr *hdr, *out;
1582 struct xfrm_userspi_info *userspi;
1583 u_int32_t received_spi = 0;
1584 size_t len;
1585
1586 memset(&request, 0, sizeof(request));
1587
1588 DBG2(DBG_KNL, "getting SPI for reqid %d", reqid);
1589
1590 hdr = (struct nlmsghdr*)request;
1591 hdr->nlmsg_flags = NLM_F_REQUEST;
1592 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
1593 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
1594
1595 userspi = (struct xfrm_userspi_info*)NLMSG_DATA(hdr);
1596 host2xfrm(src, &userspi->info.saddr);
1597 host2xfrm(dst, &userspi->info.id.daddr);
1598 userspi->info.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1599 userspi->info.mode = TRUE; /* tunnel mode */
1600 userspi->info.reqid = reqid;
1601 userspi->info.family = src->get_family(src);
1602 userspi->min = 0xc0000000;
1603 userspi->max = 0xcFFFFFFF;
1604
1605 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1606 {
1607 hdr = out;
1608 while (NLMSG_OK(hdr, len))
1609 {
1610 switch (hdr->nlmsg_type)
1611 {
1612 case XFRM_MSG_NEWSA:
1613 {
1614 struct xfrm_usersa_info* usersa = NLMSG_DATA(hdr);
1615 received_spi = usersa->id.spi;
1616 break;
1617 }
1618 case NLMSG_ERROR:
1619 {
1620 struct nlmsgerr *err = NLMSG_DATA(hdr);
1621
1622 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
1623 strerror(-err->error), -err->error);
1624 break;
1625 }
1626 default:
1627 hdr = NLMSG_NEXT(hdr, len);
1628 continue;
1629 case NLMSG_DONE:
1630 break;
1631 }
1632 break;
1633 }
1634 free(out);
1635 }
1636
1637 if (received_spi == 0)
1638 {
1639 DBG1(DBG_KNL, "unable to get SPI for reqid %d", reqid);
1640 return FAILED;
1641 }
1642
1643 DBG2(DBG_KNL, "got SPI 0x%x for reqid %d", received_spi, reqid);
1644
1645 *spi = received_spi;
1646 return SUCCESS;
1647 }
1648
1649 /**
1650 * Implementation of kernel_interface_t.add_sa.
1651 */
1652 static status_t add_sa(private_kernel_interface_t *this,
1653 host_t *src, host_t *dst, u_int32_t spi,
1654 protocol_id_t protocol, u_int32_t reqid,
1655 u_int64_t expire_soft, u_int64_t expire_hard,
1656 algorithm_t *enc_alg, algorithm_t *int_alg,
1657 prf_plus_t *prf_plus, mode_t mode, bool encap,
1658 bool replace)
1659 {
1660 unsigned char request[BUFFER_SIZE];
1661 char *alg_name;
1662 u_int key_size;
1663 struct nlmsghdr *hdr;
1664 struct xfrm_usersa_info *sa;
1665
1666 memset(&request, 0, sizeof(request));
1667
1668 DBG2(DBG_KNL, "adding SAD entry with SPI 0x%x", spi);
1669
1670 hdr = (struct nlmsghdr*)request;
1671 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1672 hdr->nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
1673 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1674
1675 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
1676 host2xfrm(src, &sa->saddr);
1677 host2xfrm(dst, &sa->id.daddr);
1678 sa->id.spi = spi;
1679 sa->id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1680 sa->family = src->get_family(src);
1681 sa->mode = mode;
1682 sa->replay_window = 32;
1683 sa->reqid = reqid;
1684 /* we currently do not expire SAs by volume/packet count */
1685 sa->lft.soft_byte_limit = XFRM_INF;
1686 sa->lft.hard_byte_limit = XFRM_INF;
1687 sa->lft.soft_packet_limit = XFRM_INF;
1688 sa->lft.hard_packet_limit = XFRM_INF;
1689 /* we use lifetimes since added, not since used */
1690 sa->lft.soft_add_expires_seconds = expire_soft;
1691 sa->lft.hard_add_expires_seconds = expire_hard;
1692 sa->lft.soft_use_expires_seconds = 0;
1693 sa->lft.hard_use_expires_seconds = 0;
1694
1695 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_usersa_info);
1696
1697 if (enc_alg->algorithm != ENCR_UNDEFINED)
1698 {
1699 rthdr->rta_type = XFRMA_ALG_CRYPT;
1700 alg_name = lookup_algorithm(encryption_algs, enc_alg, &key_size);
1701 if (alg_name == NULL)
1702 {
1703 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1704 encryption_algorithm_names, enc_alg->algorithm);
1705 return FAILED;
1706 }
1707 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1708 encryption_algorithm_names, enc_alg->algorithm, key_size);
1709
1710 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
1711 hdr->nlmsg_len += rthdr->rta_len;
1712 if (hdr->nlmsg_len > sizeof(request))
1713 {
1714 return FAILED;
1715 }
1716
1717 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1718 algo->alg_key_len = key_size;
1719 strcpy(algo->alg_name, alg_name);
1720 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
1721
1722 rthdr = XFRM_RTA_NEXT(rthdr);
1723 }
1724
1725 if (int_alg->algorithm != AUTH_UNDEFINED)
1726 {
1727 rthdr->rta_type = XFRMA_ALG_AUTH;
1728 alg_name = lookup_algorithm(integrity_algs, int_alg, &key_size);
1729 if (alg_name == NULL)
1730 {
1731 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1732 integrity_algorithm_names, int_alg->algorithm);
1733 return FAILED;
1734 }
1735 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1736 integrity_algorithm_names, int_alg->algorithm, key_size);
1737
1738 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
1739 hdr->nlmsg_len += rthdr->rta_len;
1740 if (hdr->nlmsg_len > sizeof(request))
1741 {
1742 return FAILED;
1743 }
1744
1745 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1746 algo->alg_key_len = key_size;
1747 strcpy(algo->alg_name, alg_name);
1748 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
1749
1750 rthdr = XFRM_RTA_NEXT(rthdr);
1751 }
1752
1753 /* TODO: add IPComp here */
1754
1755 if (encap)
1756 {
1757 rthdr->rta_type = XFRMA_ENCAP;
1758 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1759
1760 hdr->nlmsg_len += rthdr->rta_len;
1761 if (hdr->nlmsg_len > sizeof(request))
1762 {
1763 return FAILED;
1764 }
1765
1766 struct xfrm_encap_tmpl* encap = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
1767 encap->encap_type = UDP_ENCAP_ESPINUDP;
1768 encap->encap_sport = htons(src->get_port(src));
1769 encap->encap_dport = htons(dst->get_port(dst));
1770 memset(&encap->encap_oa, 0, sizeof (xfrm_address_t));
1771 /* encap_oa could probably be derived from the
1772 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1773 * pluto does the same as we do here but it uses encap_oa in the
1774 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1775 * the kernel ignores it anyway
1776 * -> does that mean that NAT-T encap doesn't work in transport mode?
1777 * No. The reason the kernel ignores NAT-OA is that it recomputes
1778 * (or, rather, just ignores) the checksum. If packets pass
1779 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1780 rthdr = XFRM_RTA_NEXT(rthdr);
1781 }
1782
1783 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
1784 {
1785 DBG1(DBG_KNL, "unalbe to add SAD entry with SPI 0x%x", spi);
1786 return FAILED;
1787 }
1788 return SUCCESS;
1789 }
1790
1791 /**
1792 * Implementation of kernel_interface_t.update_sa.
1793 */
1794 static status_t update_sa(private_kernel_interface_t *this,
1795 u_int32_t spi, protocol_id_t protocol,
1796 host_t *src, host_t *dst,
1797 host_t *new_src, host_t *new_dst)
1798 {
1799 unsigned char request[BUFFER_SIZE];
1800 struct nlmsghdr *hdr, *out = NULL;
1801 struct xfrm_usersa_id *sa_id;
1802 struct xfrm_usersa_info *sa = NULL;
1803 size_t len;
1804
1805 memset(&request, 0, sizeof(request));
1806
1807 DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x for update", spi);
1808
1809 /* query the exisiting SA first */
1810 hdr = (struct nlmsghdr*)request;
1811 hdr->nlmsg_flags = NLM_F_REQUEST;
1812 hdr->nlmsg_type = XFRM_MSG_GETSA;
1813 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1814
1815 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1816 host2xfrm(dst, &sa_id->daddr);
1817 sa_id->spi = spi;
1818 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1819 sa_id->family = dst->get_family(dst);
1820
1821 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1822 {
1823 hdr = out;
1824 while (NLMSG_OK(hdr, len))
1825 {
1826 switch (hdr->nlmsg_type)
1827 {
1828 case XFRM_MSG_NEWSA:
1829 {
1830 sa = NLMSG_DATA(hdr);
1831 break;
1832 }
1833 case NLMSG_ERROR:
1834 {
1835 struct nlmsgerr *err = NLMSG_DATA(hdr);
1836 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1837 strerror(-err->error), -err->error);
1838 break;
1839 }
1840 default:
1841 hdr = NLMSG_NEXT(hdr, len);
1842 continue;
1843 case NLMSG_DONE:
1844 break;
1845 }
1846 break;
1847 }
1848 }
1849 if (sa == NULL ||
1850 this->public.del_sa(&this->public, dst, spi, protocol) != SUCCESS)
1851 {
1852 DBG1(DBG_KNL, "unable to update SAD entry with SPI 0x%x", spi);
1853 free(out);
1854 return FAILED;
1855 }
1856
1857 DBG2(DBG_KNL, "updating SAD entry with SPI 0x%x from %#H..%#H to %#H..%#H",
1858 spi, src, dst, new_src, new_dst);
1859
1860 /* update the values in the queried SA */
1861 hdr = out;
1862 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1863 hdr->nlmsg_type = XFRM_MSG_NEWSA;
1864
1865 if (!src->ip_equals(src, new_src))
1866 {
1867 host2xfrm(new_src, &sa->saddr);
1868 }
1869 if (!dst->ip_equals(dst, new_dst))
1870 {
1871 host2xfrm(new_dst, &sa->id.daddr);
1872 }
1873
1874 if (src->get_port(src) != new_src->get_port(new_src) ||
1875 dst->get_port(dst) != new_dst->get_port(new_dst))
1876 {
1877 struct rtattr *rtattr = XFRM_RTA(hdr, struct xfrm_usersa_info);
1878 size_t rtsize = XFRM_PAYLOAD(hdr, struct xfrm_usersa_info);
1879 while (RTA_OK(rtattr, rtsize))
1880 {
1881 if (rtattr->rta_type == XFRMA_ENCAP)
1882 {
1883 struct xfrm_encap_tmpl* encap;
1884 encap = (struct xfrm_encap_tmpl*)RTA_DATA(rtattr);
1885 encap->encap_sport = ntohs(new_src->get_port(new_src));
1886 encap->encap_dport = ntohs(new_dst->get_port(new_dst));
1887 break;
1888 }
1889 rtattr = RTA_NEXT(rtattr, rtsize);
1890 }
1891 }
1892 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
1893 {
1894 DBG1(DBG_KNL, "unalbe to update SAD entry with SPI 0x%x", spi);
1895 free(out);
1896 return FAILED;
1897 }
1898 free(out);
1899
1900 return SUCCESS;
1901 }
1902
1903 /**
1904 * Implementation of kernel_interface_t.query_sa.
1905 */
1906 static status_t query_sa(private_kernel_interface_t *this, host_t *dst,
1907 u_int32_t spi, protocol_id_t protocol,
1908 u_int32_t *use_time)
1909 {
1910 unsigned char request[BUFFER_SIZE];
1911 struct nlmsghdr *out = NULL, *hdr;
1912 struct xfrm_usersa_id *sa_id;
1913 struct xfrm_usersa_info *sa = NULL;
1914 size_t len;
1915
1916 DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x", spi);
1917 memset(&request, 0, sizeof(request));
1918
1919 hdr = (struct nlmsghdr*)request;
1920 hdr->nlmsg_flags = NLM_F_REQUEST;
1921 hdr->nlmsg_type = XFRM_MSG_GETSA;
1922 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1923
1924 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1925 host2xfrm(dst, &sa_id->daddr);
1926 sa_id->spi = spi;
1927 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1928 sa_id->family = dst->get_family(dst);
1929
1930 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1931 {
1932 hdr = out;
1933 while (NLMSG_OK(hdr, len))
1934 {
1935 switch (hdr->nlmsg_type)
1936 {
1937 case XFRM_MSG_NEWSA:
1938 {
1939 sa = NLMSG_DATA(hdr);
1940 break;
1941 }
1942 case NLMSG_ERROR:
1943 {
1944 struct nlmsgerr *err = NLMSG_DATA(hdr);
1945 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1946 strerror(-err->error), -err->error);
1947 break;
1948 }
1949 default:
1950 hdr = NLMSG_NEXT(hdr, len);
1951 continue;
1952 case NLMSG_DONE:
1953 break;
1954 }
1955 break;
1956 }
1957 }
1958
1959 if (sa == NULL)
1960 {
1961 DBG1(DBG_KNL, "unable to query SAD entry with SPI 0x%x", spi);
1962 free(out);
1963 return FAILED;
1964 }
1965
1966 *use_time = sa->curlft.use_time;
1967 free (out);
1968 return SUCCESS;
1969 }
1970
1971 /**
1972 * Implementation of kernel_interface_t.del_sa.
1973 */
1974 static status_t del_sa(private_kernel_interface_t *this, host_t *dst,
1975 u_int32_t spi, protocol_id_t protocol)
1976 {
1977 unsigned char request[BUFFER_SIZE];
1978 struct nlmsghdr *hdr;
1979 struct xfrm_usersa_id *sa_id;
1980
1981 memset(&request, 0, sizeof(request));
1982
1983 DBG2(DBG_KNL, "deleting SAD entry with SPI 0x%x", spi);
1984
1985 hdr = (struct nlmsghdr*)request;
1986 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1987 hdr->nlmsg_type = XFRM_MSG_DELSA;
1988 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1989
1990 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1991 host2xfrm(dst, &sa_id->daddr);
1992 sa_id->spi = spi;
1993 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1994 sa_id->family = dst->get_family(dst);
1995
1996 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
1997 {
1998 DBG1(DBG_KNL, "unalbe to delete SAD entry with SPI 0x%x", spi);
1999 return FAILED;
2000 }
2001 DBG2(DBG_KNL, "deleted SAD entry with SPI 0x%x", spi);
2002 return SUCCESS;
2003 }
2004
2005 /**
2006 * Implementation of kernel_interface_t.add_policy.
2007 */
2008 static status_t add_policy(private_kernel_interface_t *this,
2009 host_t *src, host_t *dst,
2010 traffic_selector_t *src_ts,
2011 traffic_selector_t *dst_ts,
2012 policy_dir_t direction, protocol_id_t protocol,
2013 u_int32_t reqid, bool high_prio, mode_t mode)
2014 {
2015 iterator_t *iterator;
2016 policy_entry_t *current, *policy;
2017 bool found = FALSE;
2018 unsigned char request[BUFFER_SIZE];
2019 struct xfrm_userpolicy_info *policy_info;
2020 struct nlmsghdr *hdr;
2021
2022 /* create a policy */
2023 policy = malloc_thing(policy_entry_t);
2024 memset(policy, 0, sizeof(policy_entry_t));
2025 policy->sel = ts2selector(src_ts, dst_ts);
2026 policy->direction = direction;
2027
2028 /* find the policy, which matches EXACTLY */
2029 pthread_mutex_lock(&this->mutex);
2030 iterator = this->policies->create_iterator(this->policies, TRUE);
2031 while (iterator->iterate(iterator, (void**)&current))
2032 {
2033 if (memcmp(&current->sel, &policy->sel, sizeof(struct xfrm_selector)) == 0 &&
2034 policy->direction == current->direction)
2035 {
2036 /* use existing policy */
2037 current->refcount++;
2038 DBG2(DBG_KNL, "policy %R===%R already exists, increasing ",
2039 "refcount", src_ts, dst_ts);
2040 free(policy);
2041 policy = current;
2042 found = TRUE;
2043 break;
2044 }
2045 }
2046 iterator->destroy(iterator);
2047 if (!found)
2048 { /* apply the new one, if we have no such policy */
2049 this->policies->insert_last(this->policies, policy);
2050 policy->refcount = 1;
2051 }
2052
2053 DBG2(DBG_KNL, "adding policy %R===%R", src_ts, dst_ts);
2054
2055 memset(&request, 0, sizeof(request));
2056 hdr = (struct nlmsghdr*)request;
2057 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2058 hdr->nlmsg_type = XFRM_MSG_UPDPOLICY;
2059 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
2060
2061 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
2062 policy_info->sel = policy->sel;
2063 policy_info->dir = policy->direction;
2064 /* calculate priority based on source selector size, small size = high prio */
2065 policy_info->priority = high_prio ? PRIO_HIGH : PRIO_LOW;
2066 policy_info->priority -= policy->sel.prefixlen_s * 10;
2067 policy_info->priority -= policy->sel.proto ? 2 : 0;
2068 policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
2069 policy_info->action = XFRM_POLICY_ALLOW;
2070 policy_info->share = XFRM_SHARE_ANY;
2071 pthread_mutex_unlock(&this->mutex);
2072
2073 /* policies don't expire */
2074 policy_info->lft.soft_byte_limit = XFRM_INF;
2075 policy_info->lft.soft_packet_limit = XFRM_INF;
2076 policy_info->lft.hard_byte_limit = XFRM_INF;
2077 policy_info->lft.hard_packet_limit = XFRM_INF;
2078 policy_info->lft.soft_add_expires_seconds = 0;
2079 policy_info->lft.hard_add_expires_seconds = 0;
2080 policy_info->lft.soft_use_expires_seconds = 0;
2081 policy_info->lft.hard_use_expires_seconds = 0;
2082
2083 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
2084 rthdr->rta_type = XFRMA_TMPL;
2085
2086 rthdr->rta_len = sizeof(struct xfrm_user_tmpl);
2087 rthdr->rta_len = RTA_LENGTH(rthdr->rta_len);
2088
2089 hdr->nlmsg_len += rthdr->rta_len;
2090 if (hdr->nlmsg_len > sizeof(request))
2091 {
2092 return FAILED;
2093 }
2094
2095 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
2096 tmpl->reqid = reqid;
2097 tmpl->id.proto = (protocol == PROTO_AH) ? KERNEL_AH : KERNEL_ESP;
2098 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
2099 tmpl->mode = mode;
2100 tmpl->family = src->get_family(src);
2101
2102 host2xfrm(src, &tmpl->saddr);
2103 host2xfrm(dst, &tmpl->id.daddr);
2104
2105 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2106 {
2107 DBG1(DBG_KNL, "unable to add policy %R===%R", src_ts, dst_ts);
2108 return FAILED;
2109 }
2110
2111 /* install a route, if:
2112 * - we are NOT updating a policy
2113 * - this is a forward policy (to just get one for each child)
2114 * - we are in tunnel mode
2115 * - we are not using IPv6 (does not work correctly yet!)
2116 */
2117 if (policy->route == NULL && direction == POLICY_FWD &&
2118 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6)
2119 {
2120 policy->route = malloc_thing(route_entry_t);
2121 if (get_address_by_ts(this, dst_ts, &policy->route->src_ip) == SUCCESS)
2122 {
2123 policy->route->gateway = dst->clone(dst);
2124 policy->route->if_index = get_interface_index(this, dst);
2125 policy->route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
2126 memcpy(policy->route->dst_net.ptr, &policy->sel.saddr, policy->route->dst_net.len);
2127 policy->route->prefixlen = policy->sel.prefixlen_s;
2128
2129 if (manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
2130 policy->route) != SUCCESS)
2131 {
2132 DBG1(DBG_KNL, "unable to install source route for %H",
2133 policy->route->src_ip);
2134 route_entry_destroy(policy->route);
2135 policy->route = NULL;
2136 }
2137 }
2138 else
2139 {
2140 free(policy->route);
2141 policy->route = NULL;
2142 }
2143 }
2144
2145 return SUCCESS;
2146 }
2147
2148 /**
2149 * Implementation of kernel_interface_t.query_policy.
2150 */
2151 static status_t query_policy(private_kernel_interface_t *this,
2152 traffic_selector_t *src_ts,
2153 traffic_selector_t *dst_ts,
2154 policy_dir_t direction, u_int32_t *use_time)
2155 {
2156 unsigned char request[BUFFER_SIZE];
2157 struct nlmsghdr *out = NULL, *hdr;
2158 struct xfrm_userpolicy_id *policy_id;
2159 struct xfrm_userpolicy_info *policy = NULL;
2160 size_t len;
2161
2162 memset(&request, 0, sizeof(request));
2163
2164 DBG2(DBG_KNL, "querying policy %R===%R", src_ts, dst_ts);
2165
2166 hdr = (struct nlmsghdr*)request;
2167 hdr->nlmsg_flags = NLM_F_REQUEST;
2168 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
2169 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2170
2171 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
2172 policy_id->sel = ts2selector(src_ts, dst_ts);
2173 policy_id->dir = direction;
2174
2175 if (netlink_send(this, this->socket_xfrm, hdr, &out, &len) == SUCCESS)
2176 {
2177 hdr = out;
2178 while (NLMSG_OK(hdr, len))
2179 {
2180 switch (hdr->nlmsg_type)
2181 {
2182 case XFRM_MSG_NEWPOLICY:
2183 {
2184 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
2185 break;
2186 }
2187 case NLMSG_ERROR:
2188 {
2189 struct nlmsgerr *err = NLMSG_DATA(hdr);
2190 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
2191 strerror(-err->error), -err->error);
2192 break;
2193 }
2194 default:
2195 hdr = NLMSG_NEXT(hdr, len);
2196 continue;
2197 case NLMSG_DONE:
2198 break;
2199 }
2200 break;
2201 }
2202 }
2203
2204 if (policy == NULL)
2205 {
2206 DBG2(DBG_KNL, "unable to query policy %R===%R", src_ts, dst_ts);
2207 free(out);
2208 return FAILED;
2209 }
2210 *use_time = (time_t)policy->curlft.use_time;
2211
2212 free(out);
2213 return SUCCESS;
2214 }
2215
2216 /**
2217 * Implementation of kernel_interface_t.del_policy.
2218 */
2219 static status_t del_policy(private_kernel_interface_t *this,
2220 traffic_selector_t *src_ts,
2221 traffic_selector_t *dst_ts,
2222 policy_dir_t direction)
2223 {
2224 policy_entry_t *current, policy, *to_delete = NULL;
2225 route_entry_t *route;
2226 unsigned char request[BUFFER_SIZE];
2227 struct nlmsghdr *hdr;
2228 struct xfrm_userpolicy_id *policy_id;
2229 iterator_t *iterator;
2230
2231 DBG2(DBG_KNL, "deleting policy %R===%R", src_ts, dst_ts);
2232
2233 /* create a policy */
2234 memset(&policy, 0, sizeof(policy_entry_t));
2235 policy.sel = ts2selector(src_ts, dst_ts);
2236 policy.direction = direction;
2237
2238 /* find the policy */
2239 iterator = this->policies->create_iterator_locked(this->policies, &this->mutex);
2240 while (iterator->iterate(iterator, (void**)&current))
2241 {
2242 if (memcmp(&current->sel, &policy.sel, sizeof(struct xfrm_selector)) == 0 &&
2243 policy.direction == current->direction)
2244 {
2245 to_delete = current;
2246 if (--to_delete->refcount > 0)
2247 {
2248 /* is used by more SAs, keep in kernel */
2249 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
2250 iterator->destroy(iterator);
2251 return SUCCESS;
2252 }
2253 /* remove if last reference */
2254 iterator->remove(iterator);
2255 break;
2256 }
2257 }
2258 iterator->destroy(iterator);
2259 if (!to_delete)
2260 {
2261 DBG1(DBG_KNL, "deleting policy %R===%R failed, not found", src_ts, dst_ts);
2262 return NOT_FOUND;
2263 }
2264
2265 memset(&request, 0, sizeof(request));
2266
2267 hdr = (struct nlmsghdr*)request;
2268 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2269 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
2270 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2271
2272 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
2273 policy_id->sel = to_delete->sel;
2274 policy_id->dir = direction;
2275
2276 route = to_delete->route;
2277 free(to_delete);
2278
2279 if (netlink_send_ack(this, this->socket_xfrm, hdr) != SUCCESS)
2280 {
2281 DBG1(DBG_KNL, "unable to delete policy %R===%R", src_ts, dst_ts);
2282 return FAILED;
2283 }
2284
2285 if (route)
2286 {
2287 if (manage_srcroute(this, RTM_DELROUTE, 0, route) != SUCCESS)
2288 {
2289 DBG1(DBG_KNL, "error uninstalling route installed with "
2290 "policy %R===%R", src_ts, dst_ts);
2291 }
2292 route_entry_destroy(route);
2293 }
2294 return SUCCESS;
2295 }
2296
2297 /**
2298 * Implementation of kernel_interface_t.destroy.
2299 */
2300 static void destroy(private_kernel_interface_t *this)
2301 {
2302 this->job->cancel(this->job);
2303 close(this->socket_xfrm_events);
2304 close(this->socket_xfrm);
2305 close(this->socket_rt_events);
2306 close(this->socket_rt);
2307 this->policies->destroy(this->policies);
2308 this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
2309 free(this);
2310 }
2311
2312 /*
2313 * Described in header.
2314 */
2315 kernel_interface_t *kernel_interface_create()
2316 {
2317 private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
2318 struct sockaddr_nl addr;
2319
2320 /* public functions */
2321 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
2322 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;
2323 this->public.update_sa = (status_t(*)(kernel_interface_t*,u_int32_t,protocol_id_t,host_t*,host_t*,host_t*,host_t*))update_sa;
2324 this->public.query_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t*))query_sa;
2325 this->public.del_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
2326 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;
2327 this->public.query_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
2328 this->public.del_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t))del_policy;
2329 this->public.get_interface = (char*(*)(kernel_interface_t*,host_t*))get_interface_name;
2330 this->public.create_address_iterator = (iterator_t*(*)(kernel_interface_t*))create_address_iterator;
2331 this->public.get_source_addr = (host_t*(*)(kernel_interface_t*, host_t *dest))get_source_addr;
2332 this->public.add_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) add_ip;
2333 this->public.del_ip = (status_t(*)(kernel_interface_t*,host_t*)) del_ip;
2334 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
2335
2336 /* private members */
2337 this->policies = linked_list_create();
2338 this->ifaces = linked_list_create();
2339 this->hiter = NULL;
2340 this->seq = 200;
2341 pthread_mutex_init(&this->mutex,NULL);
2342
2343 memset(&addr, 0, sizeof(addr));
2344 addr.nl_family = AF_NETLINK;
2345
2346 /* create and bind RT socket */
2347 this->socket_rt = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2348 if (this->socket_rt <= 0)
2349 {
2350 charon->kill(charon, "unable to create RT netlink socket");
2351 }
2352 addr.nl_groups = 0;
2353 if (bind(this->socket_rt, (struct sockaddr*)&addr, sizeof(addr)))
2354 {
2355 charon->kill(charon, "unable to bind RT netlink socket");
2356 }
2357
2358 /* create and bind RT socket for events (address/interface/route changes) */
2359 this->socket_rt_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2360 if (this->socket_rt_events <= 0)
2361 {
2362 charon->kill(charon, "unable to create RT event socket");
2363 }
2364 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
2365 RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_LINK;
2366 if (bind(this->socket_rt_events, (struct sockaddr*)&addr, sizeof(addr)))
2367 {
2368 charon->kill(charon, "unable to bind RT event socket");
2369 }
2370
2371 /* create and bind XFRM socket */
2372 this->socket_xfrm = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
2373 if (this->socket_xfrm <= 0)
2374 {
2375 charon->kill(charon, "unable to create XFRM netlink socket");
2376 }
2377 addr.nl_groups = 0;
2378 if (bind(this->socket_xfrm, (struct sockaddr*)&addr, sizeof(addr)))
2379 {
2380 charon->kill(charon, "unable to bind XFRM netlink socket");
2381 }
2382
2383 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
2384 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
2385 if (this->socket_xfrm_events <= 0)
2386 {
2387 charon->kill(charon, "unable to create XFRM event socket");
2388 }
2389 addr.nl_groups = XFRMGRP_ACQUIRE | XFRMGRP_EXPIRE;
2390 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
2391 {
2392 charon->kill(charon, "unable to bind XFRM event socket");
2393 }
2394
2395 this->job = callback_job_create((callback_job_cb_t)receive_events,
2396 this, NULL, NULL);
2397 charon->processor->queue_job(charon->processor, (job_t*)this->job);
2398
2399 if (init_address_list(this) != SUCCESS)
2400 {
2401 charon->kill(charon, "unable to get interface list");
2402 }
2403
2404 return &this->public;
2405 }
2406