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