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