reducing capabilities of the threads to a minimum
[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
52 /** kernel level protocol identifiers */
53 #define KERNEL_ESP 50
54 #define KERNEL_AH 51
55
56 /** default priority of installed policies */
57 #define PRIO_LOW 3000
58 #define PRIO_HIGH 2000
59
60 #define BUFFER_SIZE 1024
61
62 /**
63 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
64 * 'usual' netlink data x like 'struct xfrm_usersa_info'
65 */
66 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
67 /**
68 * returns a pointer to the next rtattr following rta.
69 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
70 */
71 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
72 /**
73 * returns the total size of attached rta data
74 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
75 */
76 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
77
78 typedef struct kernel_algorithm_t kernel_algorithm_t;
79
80 /**
81 * Mapping from the algorithms defined in IKEv2 to
82 * kernel level algorithm names and their key length
83 */
84 struct kernel_algorithm_t {
85 /**
86 * Identifier specified in IKEv2
87 */
88 int ikev2_id;
89
90 /**
91 * Name of the algorithm, as used as kernel identifier
92 */
93 char *name;
94
95 /**
96 * Key length in bits, if fixed size
97 */
98 u_int key_size;
99 };
100 #define END_OF_LIST -1
101
102 /**
103 * Algorithms for encryption
104 */
105 kernel_algorithm_t encryption_algs[] = {
106 /* {ENCR_DES_IV64, "***", 0}, */
107 {ENCR_DES, "des", 64},
108 {ENCR_3DES, "des3_ede", 192},
109 /* {ENCR_RC5, "***", 0}, */
110 /* {ENCR_IDEA, "***", 0}, */
111 {ENCR_CAST, "cast128", 0},
112 {ENCR_BLOWFISH, "blowfish", 0},
113 /* {ENCR_3IDEA, "***", 0}, */
114 /* {ENCR_DES_IV32, "***", 0}, */
115 {ENCR_NULL, "cipher_null", 0},
116 {ENCR_AES_CBC, "aes", 0},
117 /* {ENCR_AES_CTR, "***", 0}, */
118 {END_OF_LIST, NULL, 0},
119 };
120
121 /**
122 * Algorithms for integrity protection
123 */
124 kernel_algorithm_t integrity_algs[] = {
125 {AUTH_HMAC_MD5_96, "md5", 128},
126 {AUTH_HMAC_SHA1_96, "sha1", 160},
127 {AUTH_HMAC_SHA2_256_128, "sha256", 256},
128 {AUTH_HMAC_SHA2_384_192, "sha384", 384},
129 {AUTH_HMAC_SHA2_512_256, "sha512", 512},
130 /* {AUTH_DES_MAC, "***", 0}, */
131 /* {AUTH_KPDK_MD5, "***", 0}, */
132 {AUTH_AES_XCBC_96, "xcbc(aes)", 128},
133 {END_OF_LIST, NULL, 0},
134 };
135
136 /**
137 * Look up a kernel algorithm name and its key size
138 */
139 char* lookup_algorithm(kernel_algorithm_t *kernel_algo,
140 algorithm_t *ikev2_algo, u_int *key_size)
141 {
142 while (kernel_algo->ikev2_id != END_OF_LIST)
143 {
144 if (ikev2_algo->algorithm == kernel_algo->ikev2_id)
145 {
146 /* match, evaluate key length */
147 if (ikev2_algo->key_size)
148 { /* variable length */
149 *key_size = ikev2_algo->key_size;
150 }
151 else
152 { /* fixed length */
153 *key_size = kernel_algo->key_size;
154 }
155 return kernel_algo->name;
156 }
157 kernel_algo++;
158 }
159 return NULL;
160 }
161
162 typedef struct route_entry_t route_entry_t;
163
164 /**
165 * installed routing entry
166 */
167 struct route_entry_t {
168
169 /** Index of the interface the route is bound to */
170 int if_index;
171
172 /** Source ip of the route */
173 host_t *src_ip;
174
175 /** gateway for this route */
176 host_t *gateway;
177
178 /** Destination net */
179 chunk_t dst_net;
180
181 /** Destination net prefixlen */
182 u_int8_t prefixlen;
183 };
184
185 /**
186 * destroy an route_entry_t object
187 */
188 static void route_entry_destroy(route_entry_t *this)
189 {
190 this->src_ip->destroy(this->src_ip);
191 this->gateway->destroy(this->gateway);
192 chunk_free(&this->dst_net);
193 free(this);
194 }
195
196 typedef struct policy_entry_t policy_entry_t;
197
198 /**
199 * installed kernel policy.
200 */
201 struct policy_entry_t {
202
203 /** direction of this policy: in, out, forward */
204 u_int8_t direction;
205
206 /** reqid of the policy */
207 u_int32_t reqid;
208
209 /** parameters of installed policy */
210 struct xfrm_selector sel;
211
212 /** associated route installed for this policy */
213 route_entry_t *route;
214
215 /** by how many CHILD_SA's this policy is used */
216 u_int refcount;
217 };
218
219 typedef struct vip_entry_t vip_entry_t;
220
221 /**
222 * Installed virtual ip
223 */
224 struct vip_entry_t {
225 /** Index of the interface the ip is bound to */
226 u_int8_t if_index;
227
228 /** The ip address */
229 host_t *ip;
230
231 /** Number of times this IP is used */
232 u_int refcount;
233 };
234
235 /**
236 * destroy a vip_entry_t object
237 */
238 static void vip_entry_destroy(vip_entry_t *this)
239 {
240 this->ip->destroy(this->ip);
241 free(this);
242 }
243
244 typedef struct address_entry_t address_entry_t;
245
246 /**
247 * an address found on the system, containg address and interface info
248 */
249 struct address_entry_t {
250
251 /** address of this entry */
252 host_t *host;
253
254 /** interface index */
255 int ifindex;
256
257 /** name of the index */
258 char ifname[IFNAMSIZ];
259 };
260
261 /**
262 * destroy an address entry
263 */
264 static void address_entry_destroy(address_entry_t *this)
265 {
266 this->host->destroy(this->host);
267 free(this);
268 }
269
270 typedef struct private_kernel_interface_t private_kernel_interface_t;
271
272 /**
273 * Private variables and functions of kernel_interface class.
274 */
275 struct private_kernel_interface_t {
276 /**
277 * Public part of the kernel_interface_t object.
278 */
279 kernel_interface_t public;
280
281 /**
282 * List of installed policies (kernel_entry_t)
283 */
284 linked_list_t *policies;
285
286 /**
287 * Mutex locks access to policies
288 */
289 pthread_mutex_t policies_mutex;
290
291 /**
292 * List of installed virtual IPs. (vip_entry_t)
293 */
294 linked_list_t *vips;
295
296 /**
297 * Mutex to lock access to vips.
298 */
299 pthread_mutex_t vips_mutex;
300
301 /**
302 * netlink xfrm socket to receive acquire and expire events
303 */
304 int socket_xfrm_events;
305
306 /**
307 * Netlink xfrm socket (IPsec)
308 */
309 int socket_xfrm;
310
311 /**
312 * Netlink rt socket (routing)
313 */
314 int socket_rt;
315
316 /**
317 * Thread receiving events from kernel
318 */
319 pthread_t event_thread;
320 };
321
322 /**
323 * convert a host_t to a struct xfrm_address
324 */
325 static void host2xfrm(host_t *host, xfrm_address_t *xfrm)
326 {
327 chunk_t chunk = host->get_address(host);
328 memcpy(xfrm, chunk.ptr, min(chunk.len, sizeof(xfrm_address_t)));
329 }
330
331 /**
332 * convert a traffic selector address range to subnet and its mask.
333 */
334 static void ts2subnet(traffic_selector_t* ts,
335 xfrm_address_t *net, u_int8_t *mask)
336 {
337 /* there is no way to do this cleanly, as the address range may
338 * be anything else but a subnet. We use from_addr as subnet
339 * and try to calculate a usable subnet mask.
340 */
341 int byte, bit;
342 bool found = FALSE;
343 chunk_t from, to;
344 size_t size = (ts->get_type(ts) == TS_IPV4_ADDR_RANGE) ? 4 : 16;
345
346 from = ts->get_from_address(ts);
347 to = ts->get_to_address(ts);
348
349 *mask = (size * 8);
350 /* go trough all bits of the addresses, beginning in the front.
351 * as long as they are equal, the subnet gets larger
352 */
353 for (byte = 0; byte < size; byte++)
354 {
355 for (bit = 7; bit >= 0; bit--)
356 {
357 if ((1<<bit & from.ptr[byte]) != (1<<bit & to.ptr[byte]))
358 {
359 *mask = ((7 - bit) + (byte * 8));
360 found = TRUE;
361 break;
362 }
363 }
364 if (found)
365 {
366 break;
367 }
368 }
369 memcpy(net, from.ptr, from.len);
370 chunk_free(&from);
371 chunk_free(&to);
372 }
373
374 /**
375 * convert a traffic selector port range to port/portmask
376 */
377 static void ts2ports(traffic_selector_t* ts,
378 u_int16_t *port, u_int16_t *mask)
379 {
380 /* linux does not seem to accept complex portmasks. Only
381 * any or a specific port is allowed. We set to any, if we have
382 * a port range, or to a specific, if we have one port only.
383 */
384 u_int16_t from, to;
385
386 from = ts->get_from_port(ts);
387 to = ts->get_to_port(ts);
388
389 if (from == to)
390 {
391 *port = htons(from);
392 *mask = ~0;
393 }
394 else
395 {
396 *port = 0;
397 *mask = 0;
398 }
399 }
400
401 /**
402 * convert a pair of traffic_selectors to a xfrm_selector
403 */
404 static struct xfrm_selector ts2selector(traffic_selector_t *src,
405 traffic_selector_t *dst)
406 {
407 struct xfrm_selector sel;
408
409 memset(&sel, 0, sizeof(sel));
410 sel.family = src->get_type(src) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
411 /* src or dest proto may be "any" (0), use more restrictive one */
412 sel.proto = max(src->get_protocol(src), dst->get_protocol(dst));
413 ts2subnet(dst, &sel.daddr, &sel.prefixlen_d);
414 ts2subnet(src, &sel.saddr, &sel.prefixlen_s);
415 ts2ports(dst, &sel.dport, &sel.dport_mask);
416 ts2ports(src, &sel.sport, &sel.sport_mask);
417 sel.ifindex = 0;
418 sel.user = 0;
419
420 return sel;
421 }
422
423 /**
424 * Creates an rtattr and adds it to the netlink message
425 */
426 static void add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
427 size_t buflen)
428 {
429 struct rtattr *rta;
430
431 if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_ALIGN(data.len) > buflen)
432 {
433 DBG1(DBG_KNL, "unable to add attribute, buffer too small");
434 return;
435 }
436
437 rta = (struct rtattr*)(((char*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len));
438 rta->rta_type = rta_type;
439 rta->rta_len = RTA_LENGTH(data.len);
440 memcpy(RTA_DATA(rta), data.ptr, data.len);
441 hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
442 }
443
444 /**
445 * Receives events from kernel
446 */
447 static void receive_events(private_kernel_interface_t *this)
448 {
449 /* keep netlink capabilities only */
450 charon->drop_capabilities(charon, TRUE, FALSE);
451
452 while(TRUE)
453 {
454 unsigned char response[512];
455 struct nlmsghdr *hdr;
456 struct sockaddr_nl addr;
457 socklen_t addr_len = sizeof(addr);
458 int len;
459
460 hdr = (struct nlmsghdr*)response;
461 len = recvfrom(this->socket_xfrm_events, response, sizeof(response),
462 0, (struct sockaddr*)&addr, &addr_len);
463 if (len < 0)
464 {
465 if (errno == EINTR)
466 {
467 /* interrupted, try again */
468 continue;
469 }
470 charon->kill(charon, "unable to receive netlink events");
471 }
472
473 if (!NLMSG_OK(hdr, len))
474 {
475 /* bad netlink message */
476 continue;
477 }
478
479 if (addr.nl_pid != 0)
480 {
481 /* not from kernel. not interested, try another one */
482 continue;
483 }
484
485 /* we handle ACQUIRE and EXPIRE messages directly */
486 if (hdr->nlmsg_type == XFRM_MSG_ACQUIRE)
487 {
488 u_int32_t reqid = 0;
489 job_t *job;
490 struct rtattr *rtattr = XFRM_RTA(hdr, struct xfrm_user_acquire);
491 size_t rtsize = XFRM_PAYLOAD(hdr, struct xfrm_user_tmpl);
492 if (RTA_OK(rtattr, rtsize))
493 {
494 if (rtattr->rta_type == XFRMA_TMPL)
495 {
496 struct xfrm_user_tmpl* tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rtattr);
497 reqid = tmpl->reqid;
498 }
499 }
500 if (reqid == 0)
501 {
502 DBG1(DBG_KNL, "received a XFRM_MSG_ACQUIRE, but no reqid found");
503 }
504 else
505 {
506 DBG2(DBG_KNL, "received a XFRM_MSG_ACQUIRE");
507 DBG1(DBG_KNL, "creating acquire job for CHILD_SA with reqid %d",
508 reqid);
509 job = (job_t*)acquire_job_create(reqid);
510 charon->job_queue->add(charon->job_queue, job);
511 }
512 }
513 else if (hdr->nlmsg_type == XFRM_MSG_EXPIRE)
514 {
515 job_t *job;
516 protocol_id_t protocol;
517 u_int32_t spi, reqid;
518 struct xfrm_user_expire *expire;
519
520 expire = (struct xfrm_user_expire*)NLMSG_DATA(hdr);
521 protocol = expire->state.id.proto == KERNEL_ESP ?
522 PROTO_ESP : PROTO_AH;
523 spi = expire->state.id.spi;
524 reqid = expire->state.reqid;
525
526 DBG2(DBG_KNL, "received a XFRM_MSG_EXPIRE");
527 DBG1(DBG_KNL, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
528 expire->hard ? "delete" : "rekey", protocol_id_names,
529 protocol, ntohl(spi), reqid);
530 if (expire->hard)
531 {
532 job = (job_t*)delete_child_sa_job_create(reqid, protocol, spi);
533 }
534 else
535 {
536 job = (job_t*)rekey_child_sa_job_create(reqid, protocol, spi);
537 }
538 charon->job_queue->add(charon->job_queue, job);
539 }
540 }
541 }
542
543 /**
544 * send a netlink message and wait for a reply
545 */
546 static status_t netlink_send(int socket, struct nlmsghdr *in,
547 struct nlmsghdr **out, size_t *out_len)
548 {
549 int len, addr_len;
550 struct sockaddr_nl addr;
551 chunk_t result = chunk_empty, tmp;
552 struct nlmsghdr *msg, peek;
553
554 static int seq = 200;
555 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
556
557
558 pthread_mutex_lock(&mutex);
559
560 in->nlmsg_seq = ++seq;
561 in->nlmsg_pid = getpid();
562
563 memset(&addr, 0, sizeof(addr));
564 addr.nl_family = AF_NETLINK;
565 addr.nl_pid = 0;
566 addr.nl_groups = 0;
567
568 while (TRUE)
569 {
570 len = sendto(socket, in, in->nlmsg_len, 0,
571 (struct sockaddr*)&addr, sizeof(addr));
572
573 if (len != in->nlmsg_len)
574 {
575 if (errno == EINTR)
576 {
577 /* interrupted, try again */
578 continue;
579 }
580 pthread_mutex_unlock(&mutex);
581 DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
582 return FAILED;
583 }
584 break;
585 }
586
587 while (TRUE)
588 {
589 char buf[1024];
590 tmp.len = sizeof(buf);
591 tmp.ptr = buf;
592 msg = (struct nlmsghdr*)tmp.ptr;
593
594 memset(&addr, 0, sizeof(addr));
595 addr.nl_family = AF_NETLINK;
596 addr.nl_pid = getpid();
597 addr.nl_groups = 0;
598 addr_len = sizeof(addr);
599
600 len = recvfrom(socket, tmp.ptr, tmp.len, 0,
601 (struct sockaddr*)&addr, &addr_len);
602
603 if (len < 0)
604 {
605 if (errno == EINTR)
606 {
607 DBG1(DBG_IKE, "got interrupted");
608 /* interrupted, try again */
609 continue;
610 }
611 DBG1(DBG_IKE, "error reading from netlink socket: %s", strerror(errno));
612 pthread_mutex_unlock(&mutex);
613 return FAILED;
614 }
615 if (!NLMSG_OK(msg, len))
616 {
617 DBG1(DBG_IKE, "received corrupted netlink message");
618 pthread_mutex_unlock(&mutex);
619 return FAILED;
620 }
621 if (msg->nlmsg_seq != seq)
622 {
623 DBG1(DBG_IKE, "received invalid netlink sequence number");
624 if (msg->nlmsg_seq < seq)
625 {
626 continue;
627 }
628 pthread_mutex_unlock(&mutex);
629 return FAILED;
630 }
631
632 tmp.len = len;
633 result = chunk_cata("cc", result, tmp);
634
635 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
636 * numbers to detect multi header messages */
637 len = recvfrom(socket, &peek, sizeof(peek), MSG_PEEK | MSG_DONTWAIT,
638 (struct sockaddr*)&addr, &addr_len);
639
640 if (len == sizeof(peek) && peek.nlmsg_seq == seq)
641 {
642 /* seems to be multipart */
643 continue;
644 }
645 break;
646 }
647
648 *out_len = result.len;
649 *out = (struct nlmsghdr*)clalloc(result.ptr, result.len);
650
651 pthread_mutex_unlock(&mutex);
652
653 return SUCCESS;
654 }
655
656 /**
657 * send a netlink message and wait for its acknowlegde
658 */
659 static status_t netlink_send_ack(int socket, struct nlmsghdr *in)
660 {
661 struct nlmsghdr *out, *hdr;
662 size_t len;
663
664 if (netlink_send(socket, in, &out, &len) != SUCCESS)
665 {
666 return FAILED;
667 }
668 hdr = out;
669 while (NLMSG_OK(hdr, len))
670 {
671 switch (hdr->nlmsg_type)
672 {
673 case NLMSG_ERROR:
674 {
675 struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(hdr);
676
677 if (err->error)
678 {
679 DBG1(DBG_KNL, "received netlink error: %s (%d)",
680 strerror(-err->error), -err->error);
681 free(out);
682 return FAILED;
683 }
684 free(out);
685 return SUCCESS;
686 }
687 default:
688 hdr = NLMSG_NEXT(hdr, len);
689 continue;
690 case NLMSG_DONE:
691 break;
692 }
693 break;
694 }
695 DBG1(DBG_KNL, "netlink request not acknowlegded");
696 free(out);
697 return FAILED;
698 }
699
700 /**
701 * Create a list of local addresses.
702 */
703 static linked_list_t *create_address_list(private_kernel_interface_t *this)
704 {
705 char request[BUFFER_SIZE];
706 struct nlmsghdr *out, *hdr;
707 struct rtgenmsg *msg;
708 size_t len;
709 linked_list_t *list;
710
711 DBG2(DBG_IKE, "getting local address list");
712
713 list = linked_list_create();
714
715 memset(&request, 0, sizeof(request));
716
717 hdr = (struct nlmsghdr*)&request;
718 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
719 hdr->nlmsg_type = RTM_GETADDR;
720 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
721 msg = (struct rtgenmsg*)NLMSG_DATA(hdr);
722 msg->rtgen_family = AF_UNSPEC;
723
724 if (netlink_send(this->socket_rt, hdr, &out, &len) == SUCCESS)
725 {
726 hdr = out;
727 while (NLMSG_OK(hdr, len))
728 {
729 switch (hdr->nlmsg_type)
730 {
731 case RTM_NEWADDR:
732 {
733 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
734 struct rtattr *rta = IFA_RTA(msg);
735 size_t rtasize = IFA_PAYLOAD (hdr);
736 host_t *host = NULL;
737 char *name = NULL;
738 chunk_t local = chunk_empty, address = chunk_empty;
739
740 while(RTA_OK(rta, rtasize))
741 {
742 switch (rta->rta_type)
743 {
744 case IFA_LOCAL:
745 local.ptr = RTA_DATA(rta);
746 local.len = RTA_PAYLOAD(rta);
747 break;
748 case IFA_ADDRESS:
749 address.ptr = RTA_DATA(rta);
750 address.len = RTA_PAYLOAD(rta);
751 break;
752 case IFA_LABEL:
753 name = RTA_DATA(rta);
754 break;
755 }
756 rta = RTA_NEXT(rta, rtasize);
757 }
758
759 /* For PPP interfaces, we need the IFA_LOCAL address,
760 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
761 * not included in all cases, so fallback to IFA_ADDRESS. */
762 if (local.ptr)
763 {
764 host = host_create_from_chunk(msg->ifa_family, local, 0);
765 }
766 else if (address.ptr)
767 {
768 host = host_create_from_chunk(msg->ifa_family, address, 0);
769 }
770
771 if (host)
772 {
773 address_entry_t *entry;
774
775 entry = malloc_thing(address_entry_t);
776 entry->host = host;
777 entry->ifindex = msg->ifa_index;
778 if (name)
779 {
780 memcpy(entry->ifname, name, IFNAMSIZ);
781 }
782 else
783 {
784 strcpy(entry->ifname, "(unknown)");
785 }
786 list->insert_last(list, entry);
787 }
788 hdr = NLMSG_NEXT(hdr, len);
789 continue;
790 }
791 default:
792 hdr = NLMSG_NEXT(hdr, len);
793 continue;
794 case NLMSG_DONE:
795 break;
796 }
797 break;
798 }
799 free(out);
800 }
801 else
802 {
803 DBG1(DBG_IKE, "unable to get local address list");
804 }
805
806 return list;
807 }
808
809 /**
810 * Implements kernel_interface_t.create_address_list.
811 */
812 static linked_list_t *create_address_list_public(private_kernel_interface_t *this)
813 {
814 linked_list_t *result, *list;
815 address_entry_t *entry;
816
817 result = linked_list_create();
818 list = create_address_list(this);
819 while (list->remove_last(list, (void**)&entry) == SUCCESS)
820 {
821 result->insert_last(result, entry->host);
822 free(entry);
823 }
824 list->destroy(list);
825
826 return result;
827 }
828
829 /**
830 * implementation of kernel_interface_t.get_interface_name
831 */
832 static char *get_interface_name(private_kernel_interface_t *this, host_t* ip)
833 {
834 linked_list_t *list;
835 address_entry_t *entry;
836 char *name = NULL;
837
838 DBG2(DBG_IKE, "getting interface name for %H", ip);
839
840 list = create_address_list(this);
841 while (!name && list->remove_last(list, (void**)&entry) == SUCCESS)
842 {
843 if (ip->ip_equals(ip, entry->host))
844 {
845 name = strdup(entry->ifname);
846 }
847 address_entry_destroy(entry);
848 }
849 list->destroy_function(list, (void*)address_entry_destroy);
850
851 if (name)
852 {
853 DBG2(DBG_IKE, "%H is on interface %s", ip, name);
854 }
855 else
856 {
857 DBG2(DBG_IKE, "%H is not a local address", ip);
858 }
859 return name;
860 }
861
862 /**
863 * Tries to find an ip address of a local interface that is included in the
864 * supplied traffic selector.
865 */
866 static status_t get_address_by_ts(private_kernel_interface_t *this,
867 traffic_selector_t *ts, host_t **ip)
868 {
869 address_entry_t *entry;
870 host_t *host;
871 int family;
872 linked_list_t *list;
873 bool found = FALSE;
874
875 DBG2(DBG_IKE, "getting a local address in traffic selector %R", ts);
876
877 /* if we have a family which includes localhost, we do not
878 * search for an IP, we use the default */
879 family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
880
881 if (family == AF_INET)
882 {
883 host = host_create_from_string("127.0.0.1", 0);
884 }
885 else
886 {
887 host = host_create_from_string("::1", 0);
888 }
889
890 if (ts->includes(ts, host))
891 {
892 *ip = host_create_any(family);
893 host->destroy(host);
894 DBG2(DBG_IKE, "using host %H", *ip);
895 return SUCCESS;
896 }
897 host->destroy(host);
898
899 list = create_address_list(this);
900 while (!found && list->remove_last(list, (void**)&entry) == SUCCESS)
901 {
902 if (ts->includes(ts, entry->host))
903 {
904 found = TRUE;
905 *ip = entry->host->clone(entry->host);
906 }
907 address_entry_destroy(entry);
908 }
909 list->destroy_function(list, (void*)address_entry_destroy);
910
911 if (!found)
912 {
913 DBG1(DBG_IKE, "no local address found in traffic selector %R", ts);
914 return FAILED;
915 }
916 DBG2(DBG_IKE, "using host %H", *ip);
917 return SUCCESS;
918 }
919
920 /**
921 * get the interface of a local address
922 */
923 static int get_interface_index(private_kernel_interface_t *this, host_t* ip)
924 {
925 linked_list_t *list;
926 address_entry_t *entry;
927 int ifindex = 0;
928
929 DBG2(DBG_IKE, "getting iface for %H", ip);
930
931 list = create_address_list(this);
932 while (!ifindex && list->remove_last(list, (void**)&entry) == SUCCESS)
933 {
934 if (ip->ip_equals(ip, entry->host))
935 {
936 ifindex = entry->ifindex;
937 }
938 address_entry_destroy(entry);
939 }
940 list->destroy_function(list, (void*)address_entry_destroy);
941
942 if (ifindex == 0)
943 {
944 DBG1(DBG_IKE, "unable to get interface for %H", ip);
945 }
946 return ifindex;
947 }
948
949 /**
950 * Manages the creation and deletion of ip addresses on an interface.
951 * By setting the appropriate nlmsg_type, the ip will be set or unset.
952 */
953 static status_t manage_ipaddr(private_kernel_interface_t *this, int nlmsg_type,
954 int flags, int if_index, host_t *ip)
955 {
956 unsigned char request[BUFFER_SIZE];
957 struct nlmsghdr *hdr;
958 struct ifaddrmsg *msg;
959 chunk_t chunk;
960
961 memset(&request, 0, sizeof(request));
962
963 chunk = ip->get_address(ip);
964
965 hdr = (struct nlmsghdr*)request;
966 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
967 hdr->nlmsg_type = nlmsg_type;
968 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
969
970 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
971 msg->ifa_family = ip->get_family(ip);
972 msg->ifa_flags = 0;
973 msg->ifa_prefixlen = 8 * chunk.len;
974 msg->ifa_scope = RT_SCOPE_UNIVERSE;
975 msg->ifa_index = if_index;
976
977 add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
978
979 return netlink_send_ack(this->socket_rt, hdr);
980 }
981
982 /**
983 * Manages source routes in the routing table.
984 * By setting the appropriate nlmsg_type, the route added or r.
985 */
986 static status_t manage_srcroute(private_kernel_interface_t *this, int nlmsg_type,
987 int flags, route_entry_t *route)
988 {
989 unsigned char request[BUFFER_SIZE];
990 struct nlmsghdr *hdr;
991 struct rtmsg *msg;
992 chunk_t chunk;
993
994 /* if route is 0.0.0.0/0, we can't install it, as it would
995 * overwrite the default route. Instead, we add two routes:
996 * 0.0.0.0/1 and 128.0.0.0/1
997 * TODO: use metrics instead */
998 if (route->prefixlen == 0)
999 {
1000 route_entry_t half;
1001 status_t status;
1002
1003 half.dst_net = chunk_alloca(route->dst_net.len);
1004 memset(half.dst_net.ptr, 0, half.dst_net.len);
1005 half.src_ip = route->src_ip;
1006 half.if_index = route->if_index;
1007 half.prefixlen = 1;
1008
1009 status = manage_srcroute(this, nlmsg_type, flags, &half);
1010 half.dst_net.ptr[0] |= 0x80;
1011 status = manage_srcroute(this, nlmsg_type, flags, &half);
1012 return status;
1013 }
1014
1015 memset(&request, 0, sizeof(request));
1016
1017 hdr = (struct nlmsghdr*)request;
1018 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
1019 hdr->nlmsg_type = nlmsg_type;
1020 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1021
1022 msg = (struct rtmsg*)NLMSG_DATA(hdr);
1023 msg->rtm_family = route->src_ip->get_family(route->src_ip);
1024 msg->rtm_dst_len = route->prefixlen;
1025 msg->rtm_table = RT_TABLE_MAIN;
1026 msg->rtm_protocol = RTPROT_STATIC;
1027 msg->rtm_type = RTN_UNICAST;
1028 msg->rtm_scope = RT_SCOPE_UNIVERSE;
1029
1030 add_attribute(hdr, RTA_DST, route->dst_net, sizeof(request));
1031 chunk = route->src_ip->get_address(route->src_ip);
1032 add_attribute(hdr, RTA_PREFSRC, chunk, sizeof(request));
1033 chunk = route->gateway->get_address(route->gateway);
1034 add_attribute(hdr, RTA_GATEWAY, chunk, sizeof(request));
1035 chunk.ptr = (char*)&route->if_index;
1036 chunk.len = sizeof(route->if_index);
1037 add_attribute(hdr, RTA_OIF, chunk, sizeof(request));
1038
1039 return netlink_send_ack(this->socket_rt, hdr);
1040 }
1041
1042
1043 /**
1044 * Implementation of kernel_interface_t.add_ip.
1045 */
1046 static status_t add_ip(private_kernel_interface_t *this,
1047 host_t *virtual_ip, host_t *iface_ip)
1048 {
1049 int targetif;
1050 vip_entry_t *listed;
1051 iterator_t *iterator;
1052
1053 DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
1054
1055 targetif = get_interface_index(this, iface_ip);
1056 if (targetif == 0)
1057 {
1058 DBG1(DBG_KNL, "unable to add virtual IP %H, no iface found for %H",
1059 virtual_ip, iface_ip);
1060 return FAILED;
1061 }
1062
1063 /* beware of deadlocks (e.g. send/receive packets while holding the lock) */
1064 iterator = this->vips->create_iterator_locked(this->vips, &(this->vips_mutex));
1065 while (iterator->iterate(iterator, (void**)&listed))
1066 {
1067 if (listed->if_index == targetif &&
1068 virtual_ip->ip_equals(virtual_ip, listed->ip))
1069 {
1070 listed->refcount++;
1071 iterator->destroy(iterator);
1072 DBG2(DBG_KNL, "virtual IP %H already added to iface %d reusing it",
1073 virtual_ip, targetif);
1074 return SUCCESS;
1075 }
1076 }
1077 iterator->destroy(iterator);
1078
1079 if (manage_ipaddr(this, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL,
1080 targetif, virtual_ip) == SUCCESS)
1081 {
1082 listed = malloc_thing(vip_entry_t);
1083 listed->ip = virtual_ip->clone(virtual_ip);
1084 listed->if_index = targetif;
1085 listed->refcount = 1;
1086 this->vips->insert_last(this->vips, listed);
1087 DBG2(DBG_KNL, "virtual IP %H added to iface %d",
1088 virtual_ip, targetif);
1089 return SUCCESS;
1090 }
1091
1092 DBG2(DBG_KNL, "unable to add virtual IP %H to iface %d",
1093 virtual_ip, targetif);
1094 return FAILED;
1095 }
1096
1097 /**
1098 * Implementation of kernel_interface_t.del_ip.
1099 */
1100 static status_t del_ip(private_kernel_interface_t *this,
1101 host_t *virtual_ip, host_t *iface_ip)
1102 {
1103 int targetif;
1104 vip_entry_t *listed;
1105 iterator_t *iterator;
1106
1107 DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
1108
1109 targetif = get_interface_index(this, iface_ip);
1110 if (targetif == 0)
1111 {
1112 DBG1(DBG_KNL, "unable to delete virtual IP %H, no iface found for %H",
1113 virtual_ip, iface_ip);
1114 return FAILED;
1115 }
1116
1117 /* beware of deadlocks (e.g. send/receive packets while holding the lock) */
1118 iterator = this->vips->create_iterator_locked(this->vips, &(this->vips_mutex));
1119 while (iterator->iterate(iterator, (void**)&listed))
1120 {
1121 if (listed->if_index == targetif &&
1122 virtual_ip->ip_equals(virtual_ip, listed->ip))
1123 {
1124 listed->refcount--;
1125 if (listed->refcount == 0)
1126 {
1127 iterator->remove(iterator);
1128 vip_entry_destroy(listed);
1129 iterator->destroy(iterator);
1130 return manage_ipaddr(this, RTM_DELADDR, 0, targetif, virtual_ip);
1131 }
1132 iterator->destroy(iterator);
1133 DBG2(DBG_KNL, "virtual IP %H used by other SAs, not deleting",
1134 virtual_ip);
1135 return SUCCESS;
1136 }
1137 }
1138 iterator->destroy(iterator);
1139
1140 DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
1141 return FAILED;
1142 }
1143
1144 /**
1145 * Implementation of kernel_interface_t.get_spi.
1146 */
1147 static status_t get_spi(private_kernel_interface_t *this,
1148 host_t *src, host_t *dst,
1149 protocol_id_t protocol, u_int32_t reqid,
1150 u_int32_t *spi)
1151 {
1152 unsigned char request[BUFFER_SIZE];
1153 struct nlmsghdr *hdr, *out;
1154 struct xfrm_userspi_info *userspi;
1155 u_int32_t received_spi = 0;
1156 size_t len;
1157
1158 memset(&request, 0, sizeof(request));
1159
1160 DBG2(DBG_KNL, "getting SPI for reqid %d", reqid);
1161
1162 hdr = (struct nlmsghdr*)request;
1163 hdr->nlmsg_flags = NLM_F_REQUEST;
1164 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
1165 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
1166
1167 userspi = (struct xfrm_userspi_info*)NLMSG_DATA(hdr);
1168 host2xfrm(src, &userspi->info.saddr);
1169 host2xfrm(dst, &userspi->info.id.daddr);
1170 userspi->info.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1171 userspi->info.mode = TRUE; /* tunnel mode */
1172 userspi->info.reqid = reqid;
1173 userspi->info.family = src->get_family(src);
1174 userspi->min = 0xc0000000;
1175 userspi->max = 0xcFFFFFFF;
1176
1177 if (netlink_send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1178 {
1179 hdr = out;
1180 while (NLMSG_OK(hdr, len))
1181 {
1182 switch (hdr->nlmsg_type)
1183 {
1184 case XFRM_MSG_NEWSA:
1185 {
1186 struct xfrm_usersa_info* usersa = NLMSG_DATA(hdr);
1187 received_spi = usersa->id.spi;
1188 break;
1189 }
1190 case NLMSG_ERROR:
1191 {
1192 struct nlmsgerr *err = NLMSG_DATA(hdr);
1193
1194 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
1195 strerror(-err->error), -err->error);
1196 break;
1197 }
1198 default:
1199 hdr = NLMSG_NEXT(hdr, len);
1200 continue;
1201 case NLMSG_DONE:
1202 break;
1203 }
1204 break;
1205 }
1206 free(out);
1207 }
1208
1209 if (received_spi == 0)
1210 {
1211 DBG1(DBG_KNL, "unable to get SPI for reqid %d", reqid);
1212 return FAILED;
1213 }
1214
1215 DBG2(DBG_KNL, "got SPI 0x%x for reqid %d", received_spi, reqid);
1216
1217 *spi = received_spi;
1218 return SUCCESS;
1219 }
1220
1221 /**
1222 * Implementation of kernel_interface_t.add_sa.
1223 */
1224 static status_t add_sa(private_kernel_interface_t *this,
1225 host_t *src, host_t *dst, u_int32_t spi,
1226 protocol_id_t protocol, u_int32_t reqid,
1227 u_int64_t expire_soft, u_int64_t expire_hard,
1228 algorithm_t *enc_alg, algorithm_t *int_alg,
1229 prf_plus_t *prf_plus, natt_conf_t *natt, mode_t mode,
1230 bool replace)
1231 {
1232 unsigned char request[BUFFER_SIZE];
1233 char *alg_name;
1234 u_int key_size;
1235 struct nlmsghdr *hdr;
1236 struct xfrm_usersa_info *sa;
1237
1238 memset(&request, 0, sizeof(request));
1239
1240 DBG2(DBG_KNL, "adding SAD entry with SPI 0x%x", spi);
1241
1242 hdr = (struct nlmsghdr*)request;
1243 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1244 hdr->nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
1245 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1246
1247 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
1248 host2xfrm(src, &sa->saddr);
1249 host2xfrm(dst, &sa->id.daddr);
1250 sa->id.spi = spi;
1251 sa->id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1252 sa->family = src->get_family(src);
1253 sa->mode = mode;
1254 sa->replay_window = 32;
1255 sa->reqid = reqid;
1256 /* we currently do not expire SAs by volume/packet count */
1257 sa->lft.soft_byte_limit = XFRM_INF;
1258 sa->lft.hard_byte_limit = XFRM_INF;
1259 sa->lft.soft_packet_limit = XFRM_INF;
1260 sa->lft.hard_packet_limit = XFRM_INF;
1261 /* we use lifetimes since added, not since used */
1262 sa->lft.soft_add_expires_seconds = expire_soft;
1263 sa->lft.hard_add_expires_seconds = expire_hard;
1264 sa->lft.soft_use_expires_seconds = 0;
1265 sa->lft.hard_use_expires_seconds = 0;
1266
1267 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_usersa_info);
1268
1269 if (enc_alg->algorithm != ENCR_UNDEFINED)
1270 {
1271 rthdr->rta_type = XFRMA_ALG_CRYPT;
1272 alg_name = lookup_algorithm(encryption_algs, enc_alg, &key_size);
1273 if (alg_name == NULL)
1274 {
1275 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1276 encryption_algorithm_names, enc_alg->algorithm);
1277 return FAILED;
1278 }
1279 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1280 encryption_algorithm_names, enc_alg->algorithm, key_size);
1281
1282 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
1283 hdr->nlmsg_len += rthdr->rta_len;
1284 if (hdr->nlmsg_len > sizeof(request))
1285 {
1286 return FAILED;
1287 }
1288
1289 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1290 algo->alg_key_len = key_size;
1291 strcpy(algo->alg_name, alg_name);
1292 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
1293
1294 rthdr = XFRM_RTA_NEXT(rthdr);
1295 }
1296
1297 if (int_alg->algorithm != AUTH_UNDEFINED)
1298 {
1299 rthdr->rta_type = XFRMA_ALG_AUTH;
1300 alg_name = lookup_algorithm(integrity_algs, int_alg, &key_size);
1301 if (alg_name == NULL)
1302 {
1303 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1304 integrity_algorithm_names, int_alg->algorithm);
1305 return FAILED;
1306 }
1307 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1308 integrity_algorithm_names, int_alg->algorithm, key_size);
1309
1310 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
1311 hdr->nlmsg_len += rthdr->rta_len;
1312 if (hdr->nlmsg_len > sizeof(request))
1313 {
1314 return FAILED;
1315 }
1316
1317 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1318 algo->alg_key_len = key_size;
1319 strcpy(algo->alg_name, alg_name);
1320 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
1321
1322 rthdr = XFRM_RTA_NEXT(rthdr);
1323 }
1324
1325 /* TODO: add IPComp here */
1326
1327 if (natt)
1328 {
1329 rthdr->rta_type = XFRMA_ENCAP;
1330 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1331
1332 hdr->nlmsg_len += rthdr->rta_len;
1333 if (hdr->nlmsg_len > sizeof(request))
1334 {
1335 return FAILED;
1336 }
1337
1338 struct xfrm_encap_tmpl* encap = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
1339 encap->encap_type = UDP_ENCAP_ESPINUDP;
1340 encap->encap_sport = htons(natt->sport);
1341 encap->encap_dport = htons(natt->dport);
1342 memset(&encap->encap_oa, 0, sizeof (xfrm_address_t));
1343 /* encap_oa could probably be derived from the
1344 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1345 * pluto does the same as we do here but it uses encap_oa in the
1346 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1347 * the kernel ignores it anyway
1348 * -> does that mean that NAT-T encap doesn't work in transport mode?
1349 * No. The reason the kernel ignores NAT-OA is that it recomputes
1350 * (or, rather, just ignores) the checksum. If packets pass
1351 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1352 rthdr = XFRM_RTA_NEXT(rthdr);
1353 }
1354
1355 if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
1356 {
1357 DBG1(DBG_KNL, "unalbe to add SAD entry with SPI 0x%x", spi);
1358 return FAILED;
1359 }
1360 return SUCCESS;
1361 }
1362
1363 /**
1364 * Implementation of kernel_interface_t.update_sa.
1365 */
1366 static status_t update_sa(private_kernel_interface_t *this,
1367 host_t *src, host_t *dst,
1368 host_t *new_src, host_t *new_dst,
1369 host_diff_t src_changes, host_diff_t dst_changes,
1370 u_int32_t spi, protocol_id_t protocol)
1371 {
1372 unsigned char request[BUFFER_SIZE];
1373 struct nlmsghdr *hdr, *out = NULL;
1374 struct xfrm_usersa_id *sa_id;
1375 struct xfrm_usersa_info *sa = NULL;
1376 size_t len;
1377
1378 memset(&request, 0, sizeof(request));
1379
1380 DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x", spi);
1381
1382 hdr = (struct nlmsghdr*)request;
1383 hdr->nlmsg_flags = NLM_F_REQUEST;
1384 hdr->nlmsg_type = XFRM_MSG_GETSA;
1385 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1386
1387 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1388 host2xfrm(dst, &sa_id->daddr);
1389 sa_id->spi = spi;
1390 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1391 sa_id->family = dst->get_family(dst);
1392
1393 if (netlink_send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1394 {
1395 hdr = out;
1396 while (NLMSG_OK(hdr, len))
1397 {
1398 switch (hdr->nlmsg_type)
1399 {
1400 case XFRM_MSG_NEWSA:
1401 {
1402 sa = NLMSG_DATA(hdr);
1403 break;
1404 }
1405 case NLMSG_ERROR:
1406 {
1407 struct nlmsgerr *err = NLMSG_DATA(hdr);
1408 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1409 strerror(-err->error), -err->error);
1410 break;
1411 }
1412 default:
1413 hdr = NLMSG_NEXT(hdr, len);
1414 continue;
1415 case NLMSG_DONE:
1416 break;
1417 }
1418 break;
1419 }
1420 }
1421 if (sa == NULL)
1422 {
1423 DBG1(DBG_KNL, "unable to update SAD entry with SPI 0x%x", spi);
1424 free(out);
1425 return FAILED;
1426 }
1427
1428 DBG2(DBG_KNL, "updating SAD entry with SPI 0x%x", spi);
1429
1430 hdr = out;
1431 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1432 hdr->nlmsg_type = XFRM_MSG_UPDSA;
1433
1434 if (src_changes & HOST_DIFF_ADDR)
1435 {
1436 host2xfrm(new_src, &sa->saddr);
1437 }
1438
1439 if (dst_changes & HOST_DIFF_ADDR)
1440 {
1441 hdr->nlmsg_type = XFRM_MSG_NEWSA;
1442 host2xfrm(new_dst, &sa->id.daddr);
1443 }
1444
1445 if (src_changes & HOST_DIFF_PORT || dst_changes & HOST_DIFF_PORT)
1446 {
1447 struct rtattr *rtattr = XFRM_RTA(hdr, struct xfrm_usersa_info);
1448 size_t rtsize = XFRM_PAYLOAD(hdr, struct xfrm_usersa_info);
1449 while (RTA_OK(rtattr, rtsize))
1450 {
1451 if (rtattr->rta_type == XFRMA_ENCAP)
1452 {
1453 struct xfrm_encap_tmpl* encap;
1454 encap = (struct xfrm_encap_tmpl*)RTA_DATA(rtattr);
1455 encap->encap_sport = ntohs(new_src->get_port(new_src));
1456 encap->encap_dport = ntohs(new_dst->get_port(new_dst));
1457 break;
1458 }
1459 rtattr = RTA_NEXT(rtattr, rtsize);
1460 }
1461 }
1462 if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
1463 {
1464 DBG1(DBG_KNL, "unalbe to update SAD entry with SPI 0x%x", spi);
1465 free(out);
1466 return FAILED;
1467 }
1468 free(out);
1469
1470 if (dst_changes & HOST_DIFF_ADDR)
1471 {
1472 return this->public.del_sa(&this->public, dst, spi, protocol);
1473 }
1474 return SUCCESS;
1475 }
1476
1477 /**
1478 * Implementation of kernel_interface_t.query_sa.
1479 */
1480 static status_t query_sa(private_kernel_interface_t *this, host_t *dst,
1481 u_int32_t spi, protocol_id_t protocol,
1482 u_int32_t *use_time)
1483 {
1484 unsigned char request[BUFFER_SIZE];
1485 struct nlmsghdr *out = NULL, *hdr;
1486 struct xfrm_usersa_id *sa_id;
1487 struct xfrm_usersa_info *sa = NULL;
1488 size_t len;
1489
1490 DBG2(DBG_KNL, "querying SAD entry with SPI 0x%x", spi);
1491 memset(&request, 0, sizeof(request));
1492
1493 hdr = (struct nlmsghdr*)request;
1494 hdr->nlmsg_flags = NLM_F_REQUEST;
1495 hdr->nlmsg_type = XFRM_MSG_GETSA;
1496 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1497
1498 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1499 host2xfrm(dst, &sa_id->daddr);
1500 sa_id->spi = spi;
1501 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1502 sa_id->family = dst->get_family(dst);
1503
1504 if (netlink_send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1505 {
1506 hdr = out;
1507 while (NLMSG_OK(hdr, len))
1508 {
1509 switch (hdr->nlmsg_type)
1510 {
1511 case XFRM_MSG_NEWSA:
1512 {
1513 sa = NLMSG_DATA(hdr);
1514 break;
1515 }
1516 case NLMSG_ERROR:
1517 {
1518 struct nlmsgerr *err = NLMSG_DATA(hdr);
1519 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1520 strerror(-err->error), -err->error);
1521 break;
1522 }
1523 default:
1524 hdr = NLMSG_NEXT(hdr, len);
1525 continue;
1526 case NLMSG_DONE:
1527 break;
1528 }
1529 break;
1530 }
1531 }
1532
1533 if (sa == NULL)
1534 {
1535 DBG1(DBG_KNL, "unable to query SAD entry with SPI 0x%x", spi);
1536 free(out);
1537 return FAILED;
1538 }
1539
1540 *use_time = sa->curlft.use_time;
1541 free (out);
1542 return SUCCESS;
1543 }
1544
1545 /**
1546 * Implementation of kernel_interface_t.del_sa.
1547 */
1548 static status_t del_sa(private_kernel_interface_t *this, host_t *dst,
1549 u_int32_t spi, protocol_id_t protocol)
1550 {
1551 unsigned char request[BUFFER_SIZE];
1552 struct nlmsghdr *hdr;
1553 struct xfrm_usersa_id *sa_id;
1554
1555 memset(&request, 0, sizeof(request));
1556
1557 DBG2(DBG_KNL, "deleting SAD entry with SPI 0x%x", spi);
1558
1559 hdr = (struct nlmsghdr*)request;
1560 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1561 hdr->nlmsg_type = XFRM_MSG_DELSA;
1562 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1563
1564 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1565 host2xfrm(dst, &sa_id->daddr);
1566 sa_id->spi = spi;
1567 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
1568 sa_id->family = dst->get_family(dst);
1569
1570 if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
1571 {
1572 DBG1(DBG_KNL, "unalbe to delete SAD entry with SPI 0x%x", spi);
1573 return FAILED;
1574 }
1575 DBG2(DBG_KNL, "deleted SAD entry with SPI 0x%x", spi);
1576 return SUCCESS;
1577 }
1578
1579 /**
1580 * Implementation of kernel_interface_t.add_policy.
1581 */
1582 static status_t add_policy(private_kernel_interface_t *this,
1583 host_t *src, host_t *dst,
1584 traffic_selector_t *src_ts,
1585 traffic_selector_t *dst_ts,
1586 policy_dir_t direction, protocol_id_t protocol,
1587 u_int32_t reqid, bool high_prio, mode_t mode,
1588 bool update)
1589 {
1590 iterator_t *iterator;
1591 policy_entry_t *current, *policy;
1592 bool found = FALSE;
1593 unsigned char request[BUFFER_SIZE];
1594 struct xfrm_userpolicy_info *policy_info;
1595 struct nlmsghdr *hdr;
1596
1597 /* create a policy */
1598 policy = malloc_thing(policy_entry_t);
1599 memset(policy, 0, sizeof(policy_entry_t));
1600 policy->sel = ts2selector(src_ts, dst_ts);
1601 policy->direction = direction;
1602
1603 /* find the policy, which matches EXACTLY */
1604 pthread_mutex_lock(&this->policies_mutex);
1605 iterator = this->policies->create_iterator(this->policies, TRUE);
1606 while (iterator->iterate(iterator, (void**)&current))
1607 {
1608 if (memcmp(&current->sel, &policy->sel, sizeof(struct xfrm_selector)) == 0 &&
1609 policy->direction == current->direction)
1610 {
1611 /* use existing policy */
1612 if (!update)
1613 {
1614 current->refcount++;
1615 DBG2(DBG_KNL, "policy %R===%R already exists, increasing ",
1616 "refcount", src_ts, dst_ts);
1617 }
1618 free(policy);
1619 policy = current;
1620 found = TRUE;
1621 break;
1622 }
1623 }
1624 iterator->destroy(iterator);
1625 if (!found)
1626 { /* apply the new one, if we have no such policy */
1627 this->policies->insert_last(this->policies, policy);
1628 policy->refcount = 1;
1629 }
1630
1631 DBG2(DBG_KNL, "adding policy %R===%R", src_ts, dst_ts);
1632
1633 memset(&request, 0, sizeof(request));
1634 hdr = (struct nlmsghdr*)request;
1635 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1636 hdr->nlmsg_type = XFRM_MSG_UPDPOLICY;
1637 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
1638
1639 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1640 policy_info->sel = policy->sel;
1641 policy_info->dir = policy->direction;
1642 /* calculate priority based on source selector size, small size = high prio */
1643 policy_info->priority = high_prio ? PRIO_HIGH : PRIO_LOW;
1644 policy_info->priority -= policy->sel.prefixlen_s * 10;
1645 policy_info->priority -= policy->sel.proto ? 2 : 0;
1646 policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
1647 policy_info->action = XFRM_POLICY_ALLOW;
1648 policy_info->share = XFRM_SHARE_ANY;
1649 pthread_mutex_unlock(&this->policies_mutex);
1650
1651 /* policies don't expire */
1652 policy_info->lft.soft_byte_limit = XFRM_INF;
1653 policy_info->lft.soft_packet_limit = XFRM_INF;
1654 policy_info->lft.hard_byte_limit = XFRM_INF;
1655 policy_info->lft.hard_packet_limit = XFRM_INF;
1656 policy_info->lft.soft_add_expires_seconds = 0;
1657 policy_info->lft.hard_add_expires_seconds = 0;
1658 policy_info->lft.soft_use_expires_seconds = 0;
1659 policy_info->lft.hard_use_expires_seconds = 0;
1660
1661 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
1662 rthdr->rta_type = XFRMA_TMPL;
1663
1664 rthdr->rta_len = sizeof(struct xfrm_user_tmpl);
1665 rthdr->rta_len = RTA_LENGTH(rthdr->rta_len);
1666
1667 hdr->nlmsg_len += rthdr->rta_len;
1668 if (hdr->nlmsg_len > sizeof(request))
1669 {
1670 return FAILED;
1671 }
1672
1673 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
1674 tmpl->reqid = reqid;
1675 tmpl->id.proto = (protocol == PROTO_AH) ? KERNEL_AH : KERNEL_ESP;
1676 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1677 tmpl->mode = mode;
1678 tmpl->family = src->get_family(src);
1679
1680 host2xfrm(src, &tmpl->saddr);
1681 host2xfrm(dst, &tmpl->id.daddr);
1682
1683 if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
1684 {
1685 DBG1(DBG_KNL, "unable to add policy %R===%R", src_ts, dst_ts);
1686 return FAILED;
1687 }
1688
1689 /* install a route, if:
1690 * - we are NOT updating a policy
1691 * - this is a forward policy (to just get one for each child)
1692 * - we are in tunnel mode
1693 * - we are not using IPv6 (does not work correctly yet!)
1694 */
1695 if (policy->route == NULL && direction == POLICY_FWD &&
1696 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6)
1697 {
1698 policy->route = malloc_thing(route_entry_t);
1699 if (get_address_by_ts(this, dst_ts, &policy->route->src_ip) == SUCCESS)
1700 {
1701 policy->route->gateway = (direction == POLICY_IN) ?
1702 dst->clone(dst) : src->clone(src);
1703 policy->route->if_index = get_interface_index(this, dst);
1704 policy->route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
1705 memcpy(policy->route->dst_net.ptr, &policy->sel.saddr, policy->route->dst_net.len);
1706 policy->route->prefixlen = policy->sel.prefixlen_s;
1707
1708 if (manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
1709 policy->route) != SUCCESS)
1710 {
1711 DBG1(DBG_KNL, "unable to install source route for %H",
1712 policy->route->src_ip);
1713 route_entry_destroy(policy->route);
1714 policy->route = NULL;
1715 }
1716 }
1717 else
1718 {
1719 free(policy->route);
1720 policy->route = NULL;
1721 }
1722 }
1723
1724 return SUCCESS;
1725 }
1726
1727 /**
1728 * Implementation of kernel_interface_t.query_policy.
1729 */
1730 static status_t query_policy(private_kernel_interface_t *this,
1731 traffic_selector_t *src_ts,
1732 traffic_selector_t *dst_ts,
1733 policy_dir_t direction, u_int32_t *use_time)
1734 {
1735 unsigned char request[BUFFER_SIZE];
1736 struct nlmsghdr *out = NULL, *hdr;
1737 struct xfrm_userpolicy_id *policy_id;
1738 struct xfrm_userpolicy_info *policy = NULL;
1739 size_t len;
1740
1741 memset(&request, 0, sizeof(request));
1742
1743 DBG2(DBG_KNL, "querying policy %R===%R", src_ts, dst_ts);
1744
1745 hdr = (struct nlmsghdr*)request;
1746 hdr->nlmsg_flags = NLM_F_REQUEST;
1747 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
1748 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1749
1750 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1751 policy_id->sel = ts2selector(src_ts, dst_ts);
1752 policy_id->dir = direction;
1753
1754 if (netlink_send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1755 {
1756 hdr = out;
1757 while (NLMSG_OK(hdr, len))
1758 {
1759 switch (hdr->nlmsg_type)
1760 {
1761 case XFRM_MSG_NEWPOLICY:
1762 {
1763 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1764 break;
1765 }
1766 case NLMSG_ERROR:
1767 {
1768 struct nlmsgerr *err = NLMSG_DATA(hdr);
1769 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
1770 strerror(-err->error), -err->error);
1771 break;
1772 }
1773 default:
1774 hdr = NLMSG_NEXT(hdr, len);
1775 continue;
1776 case NLMSG_DONE:
1777 break;
1778 }
1779 break;
1780 }
1781 }
1782
1783 if (policy == NULL)
1784 {
1785 DBG2(DBG_KNL, "unable to query policy %R===%R", src_ts, dst_ts);
1786 free(out);
1787 return FAILED;
1788 }
1789 *use_time = (time_t)policy->curlft.use_time;
1790
1791 free(out);
1792 return SUCCESS;
1793 }
1794
1795 /**
1796 * Implementation of kernel_interface_t.del_policy.
1797 */
1798 static status_t del_policy(private_kernel_interface_t *this,
1799 traffic_selector_t *src_ts,
1800 traffic_selector_t *dst_ts,
1801 policy_dir_t direction)
1802 {
1803 policy_entry_t *current, policy, *to_delete = NULL;
1804 route_entry_t *route;
1805 unsigned char request[BUFFER_SIZE];
1806 struct nlmsghdr *hdr;
1807 struct xfrm_userpolicy_id *policy_id;
1808 iterator_t *iterator;
1809
1810 DBG2(DBG_KNL, "deleting policy %R===%R", src_ts, dst_ts);
1811
1812 /* create a policy */
1813 memset(&policy, 0, sizeof(policy_entry_t));
1814 policy.sel = ts2selector(src_ts, dst_ts);
1815 policy.direction = direction;
1816
1817 /* find the policy */
1818 pthread_mutex_lock(&this->policies_mutex);
1819 iterator = this->policies->create_iterator(this->policies, TRUE);
1820 while (iterator->iterate(iterator, (void**)&current))
1821 {
1822 if (memcmp(&current->sel, &policy.sel, sizeof(struct xfrm_selector)) == 0 &&
1823 policy.direction == current->direction)
1824 {
1825 to_delete = current;
1826 if (--to_delete->refcount > 0)
1827 {
1828 /* is used by more SAs, keep in kernel */
1829 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
1830 iterator->destroy(iterator);
1831 pthread_mutex_unlock(&this->policies_mutex);
1832 return SUCCESS;
1833 }
1834 /* remove if last reference */
1835 iterator->remove(iterator);
1836 break;
1837 }
1838 }
1839 iterator->destroy(iterator);
1840 pthread_mutex_unlock(&this->policies_mutex);
1841 if (!to_delete)
1842 {
1843 DBG1(DBG_KNL, "deleting policy %R===%R failed, not found", src_ts, dst_ts);
1844 return NOT_FOUND;
1845 }
1846
1847 memset(&request, 0, sizeof(request));
1848
1849 hdr = (struct nlmsghdr*)request;
1850 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1851 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
1852 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1853
1854 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1855 policy_id->sel = to_delete->sel;
1856 policy_id->dir = direction;
1857
1858 route = to_delete->route;
1859 free(to_delete);
1860
1861 if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
1862 {
1863 DBG1(DBG_KNL, "unable to delete policy %R===%R", src_ts, dst_ts);
1864 return FAILED;
1865 }
1866
1867 if (route)
1868 {
1869 if (manage_srcroute(this, RTM_DELROUTE, 0, route) != SUCCESS)
1870 {
1871 DBG1(DBG_KNL, "error uninstalling route installed with "
1872 "policy %R===%R", src_ts, dst_ts);
1873 }
1874 route_entry_destroy(route);
1875 }
1876 return SUCCESS;
1877 }
1878
1879 /**
1880 * Implementation of kernel_interface_t.destroy.
1881 */
1882 static void destroy(private_kernel_interface_t *this)
1883 {
1884 pthread_cancel(this->event_thread);
1885 pthread_join(this->event_thread, NULL);
1886 close(this->socket_xfrm_events);
1887 close(this->socket_xfrm);
1888 close(this->socket_rt);
1889 this->vips->destroy(this->vips);
1890 this->policies->destroy(this->policies);
1891 free(this);
1892 }
1893
1894 /*
1895 * Described in header.
1896 */
1897 kernel_interface_t *kernel_interface_create()
1898 {
1899 private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
1900 struct sockaddr_nl addr;
1901
1902 /* public functions */
1903 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
1904 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;
1905 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;
1906 this->public.query_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t*))query_sa;
1907 this->public.del_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
1908 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;
1909 this->public.query_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
1910 this->public.del_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t))del_policy;
1911
1912 this->public.get_interface = (char*(*)(kernel_interface_t*,host_t*))get_interface_name;
1913 this->public.create_address_list = (linked_list_t*(*)(kernel_interface_t*))create_address_list_public;
1914 this->public.add_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) add_ip;
1915 this->public.del_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) del_ip;
1916 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
1917
1918 /* private members */
1919 this->vips = linked_list_create();
1920 this->policies = linked_list_create();
1921 pthread_mutex_init(&this->policies_mutex,NULL);
1922 pthread_mutex_init(&this->vips_mutex,NULL);
1923
1924 addr.nl_family = AF_NETLINK;
1925 addr.nl_pid = 0;
1926 addr.nl_groups = 0;
1927
1928 /* create and bind XFRM socket */
1929 this->socket_xfrm = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1930 if (this->socket_xfrm <= 0)
1931 {
1932 charon->kill(charon, "unable to create XFRM netlink socket");
1933 }
1934
1935 if (bind(this->socket_xfrm, (struct sockaddr*)&addr, sizeof(addr)))
1936 {
1937 charon->kill(charon, "unable to bind XFRM netlink socket");
1938 }
1939
1940 /* create and bind RT socket */
1941 this->socket_rt = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1942 if (this->socket_rt <= 0)
1943 {
1944 charon->kill(charon, "unable to create RT netlink socket");
1945 }
1946
1947 if (bind(this->socket_rt, (struct sockaddr*)&addr, sizeof(addr)))
1948 {
1949 charon->kill(charon, "unable to bind RT netlink socket");
1950 }
1951
1952 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
1953 addr.nl_groups = XFRMGRP_ACQUIRE | XFRMGRP_EXPIRE;
1954 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1955 if (this->socket_xfrm_events <= 0)
1956 {
1957 charon->kill(charon, "unable to create XFRM event socket");
1958 }
1959
1960 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
1961 {
1962 charon->kill(charon, "unable to bind XFRM event socket");
1963 }
1964
1965 /* create a thread receiving ACQUIRE & EXPIRE events */
1966 if (pthread_create(&this->event_thread, NULL,
1967 (void*(*)(void*))receive_events, this))
1968 {
1969 charon->kill(charon, "unable to create xfrm event dispatcher thread");
1970 }
1971
1972 return &this->public;
1973 }
1974
1975 /* vim: set ts=4 sw=4 noet: */