fixed crash when using 0.0.0.0/0 subnets
[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 charon->drop_capabilities(charon, TRUE);
450
451 while(TRUE)
452 {
453 unsigned char response[512];
454 struct nlmsghdr *hdr;
455 struct sockaddr_nl addr;
456 socklen_t addr_len = sizeof(addr);
457 int len;
458
459 hdr = (struct nlmsghdr*)response;
460 len = recvfrom(this->socket_xfrm_events, response, sizeof(response),
461 0, (struct sockaddr*)&addr, &addr_len);
462 if (len < 0)
463 {
464 if (errno == EINTR)
465 {
466 /* interrupted, try again */
467 continue;
468 }
469 charon->kill(charon, "unable to receive netlink events");
470 }
471
472 if (!NLMSG_OK(hdr, len))
473 {
474 /* bad netlink message */
475 continue;
476 }
477
478 if (addr.nl_pid != 0)
479 {
480 /* not from kernel. not interested, try another one */
481 continue;
482 }
483
484 /* we handle ACQUIRE and EXPIRE messages directly */
485 if (hdr->nlmsg_type == XFRM_MSG_ACQUIRE)
486 {
487 u_int32_t reqid = 0;
488 job_t *job;
489 struct rtattr *rtattr = XFRM_RTA(hdr, struct xfrm_user_acquire);
490 size_t rtsize = XFRM_PAYLOAD(hdr, struct xfrm_user_tmpl);
491 if (RTA_OK(rtattr, rtsize))
492 {
493 if (rtattr->rta_type == XFRMA_TMPL)
494 {
495 struct xfrm_user_tmpl* tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rtattr);
496 reqid = tmpl->reqid;
497 }
498 }
499 if (reqid == 0)
500 {
501 DBG1(DBG_KNL, "received a XFRM_MSG_ACQUIRE, but no reqid found");
502 }
503 else
504 {
505 DBG2(DBG_KNL, "received a XFRM_MSG_ACQUIRE");
506 DBG1(DBG_KNL, "creating acquire job for CHILD_SA with reqid %d",
507 reqid);
508 job = (job_t*)acquire_job_create(reqid);
509 charon->job_queue->add(charon->job_queue, job);
510 }
511 }
512 else if (hdr->nlmsg_type == XFRM_MSG_EXPIRE)
513 {
514 job_t *job;
515 protocol_id_t protocol;
516 u_int32_t spi, reqid;
517 struct xfrm_user_expire *expire;
518
519 expire = (struct xfrm_user_expire*)NLMSG_DATA(hdr);
520 protocol = expire->state.id.proto == KERNEL_ESP ?
521 PROTO_ESP : PROTO_AH;
522 spi = expire->state.id.spi;
523 reqid = expire->state.reqid;
524
525 DBG2(DBG_KNL, "received a XFRM_MSG_EXPIRE");
526 DBG1(DBG_KNL, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
527 expire->hard ? "delete" : "rekey", protocol_id_names,
528 protocol, ntohl(spi), reqid);
529 if (expire->hard)
530 {
531 job = (job_t*)delete_child_sa_job_create(reqid, protocol, spi);
532 }
533 else
534 {
535 job = (job_t*)rekey_child_sa_job_create(reqid, protocol, spi);
536 }
537 charon->job_queue->add(charon->job_queue, job);
538 }
539 }
540 }
541
542 /**
543 * send a netlink message and wait for a reply
544 */
545 static status_t netlink_send(int socket, struct nlmsghdr *in,
546 struct nlmsghdr **out, size_t *out_len)
547 {
548 int len, addr_len;
549 struct sockaddr_nl addr;
550 chunk_t result = chunk_empty, tmp;
551 struct nlmsghdr *msg, peek;
552
553 static int seq = 200;
554 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
555
556
557 pthread_mutex_lock(&mutex);
558
559 in->nlmsg_seq = ++seq;
560 in->nlmsg_pid = getpid();
561
562 memset(&addr, 0, sizeof(addr));
563 addr.nl_family = AF_NETLINK;
564 addr.nl_pid = 0;
565 addr.nl_groups = 0;
566
567 while (TRUE)
568 {
569 len = sendto(socket, in, in->nlmsg_len, 0,
570 (struct sockaddr*)&addr, sizeof(addr));
571
572 if (len != in->nlmsg_len)
573 {
574 if (errno == EINTR)
575 {
576 /* interrupted, try again */
577 continue;
578 }
579 pthread_mutex_unlock(&mutex);
580 DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
581 return FAILED;
582 }
583 break;
584 }
585
586 while (TRUE)
587 {
588 char buf[1024];
589 tmp.len = sizeof(buf);
590 tmp.ptr = buf;
591 msg = (struct nlmsghdr*)tmp.ptr;
592
593 memset(&addr, 0, sizeof(addr));
594 addr.nl_family = AF_NETLINK;
595 addr.nl_pid = getpid();
596 addr.nl_groups = 0;
597 addr_len = sizeof(addr);
598
599 len = recvfrom(socket, tmp.ptr, tmp.len, 0,
600 (struct sockaddr*)&addr, &addr_len);
601
602 if (len < 0)
603 {
604 if (errno == EINTR)
605 {
606 DBG1(DBG_IKE, "got interrupted");
607 /* interrupted, try again */
608 continue;
609 }
610 DBG1(DBG_IKE, "error reading from netlink socket: %s", strerror(errno));
611 pthread_mutex_unlock(&mutex);
612 return FAILED;
613 }
614 if (!NLMSG_OK(msg, len))
615 {
616 DBG1(DBG_IKE, "received corrupted netlink message");
617 pthread_mutex_unlock(&mutex);
618 return FAILED;
619 }
620 if (msg->nlmsg_seq != seq)
621 {
622 DBG1(DBG_IKE, "received invalid netlink sequence number");
623 if (msg->nlmsg_seq < seq)
624 {
625 continue;
626 }
627 pthread_mutex_unlock(&mutex);
628 return FAILED;
629 }
630
631 tmp.len = len;
632 result = chunk_cata("cc", result, tmp);
633
634 /* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
635 * numbers to detect multi header messages */
636 len = recvfrom(socket, &peek, sizeof(peek), MSG_PEEK | MSG_DONTWAIT,
637 (struct sockaddr*)&addr, &addr_len);
638
639 if (len == sizeof(peek) && peek.nlmsg_seq == seq)
640 {
641 /* seems to be multipart */
642 continue;
643 }
644 break;
645 }
646
647 *out_len = result.len;
648 *out = (struct nlmsghdr*)clalloc(result.ptr, result.len);
649
650 pthread_mutex_unlock(&mutex);
651
652 return SUCCESS;
653 }
654
655 /**
656 * send a netlink message and wait for its acknowlegde
657 */
658 static status_t netlink_send_ack(int socket, struct nlmsghdr *in)
659 {
660 struct nlmsghdr *out, *hdr;
661 size_t len;
662
663 if (netlink_send(socket, in, &out, &len) != SUCCESS)
664 {
665 return FAILED;
666 }
667 hdr = out;
668 while (NLMSG_OK(hdr, len))
669 {
670 switch (hdr->nlmsg_type)
671 {
672 case NLMSG_ERROR:
673 {
674 struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(hdr);
675
676 if (err->error)
677 {
678 DBG1(DBG_KNL, "received netlink error: %s (%d)",
679 strerror(-err->error), -err->error);
680 free(out);
681 return FAILED;
682 }
683 free(out);
684 return SUCCESS;
685 }
686 default:
687 hdr = NLMSG_NEXT(hdr, len);
688 continue;
689 case NLMSG_DONE:
690 break;
691 }
692 break;
693 }
694 DBG1(DBG_KNL, "netlink request not acknowlegded");
695 free(out);
696 return FAILED;
697 }
698
699 /**
700 * Create a list of local addresses.
701 */
702 static linked_list_t *create_address_list(private_kernel_interface_t *this)
703 {
704 char request[BUFFER_SIZE];
705 struct nlmsghdr *out, *hdr;
706 struct rtgenmsg *msg;
707 size_t len;
708 linked_list_t *list;
709
710 DBG2(DBG_IKE, "getting local address list");
711
712 list = linked_list_create();
713
714 memset(&request, 0, sizeof(request));
715
716 hdr = (struct nlmsghdr*)&request;
717 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
718 hdr->nlmsg_type = RTM_GETADDR;
719 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH | NLM_F_ROOT;
720 msg = (struct rtgenmsg*)NLMSG_DATA(hdr);
721 msg->rtgen_family = AF_UNSPEC;
722
723 if (netlink_send(this->socket_rt, hdr, &out, &len) == SUCCESS)
724 {
725 hdr = out;
726 while (NLMSG_OK(hdr, len))
727 {
728 switch (hdr->nlmsg_type)
729 {
730 case RTM_NEWADDR:
731 {
732 struct ifaddrmsg* msg = (struct ifaddrmsg*)(NLMSG_DATA(hdr));
733 struct rtattr *rta = IFA_RTA(msg);
734 size_t rtasize = IFA_PAYLOAD (hdr);
735 host_t *host = NULL;
736 char *name = NULL;
737 chunk_t local = chunk_empty, address = chunk_empty;
738
739 while(RTA_OK(rta, rtasize))
740 {
741 switch (rta->rta_type)
742 {
743 case IFA_LOCAL:
744 local.ptr = RTA_DATA(rta);
745 local.len = RTA_PAYLOAD(rta);
746 break;
747 case IFA_ADDRESS:
748 address.ptr = RTA_DATA(rta);
749 address.len = RTA_PAYLOAD(rta);
750 break;
751 case IFA_LABEL:
752 name = RTA_DATA(rta);
753 break;
754 }
755 rta = RTA_NEXT(rta, rtasize);
756 }
757
758 /* For PPP interfaces, we need the IFA_LOCAL address,
759 * IFA_ADDRESS is the peers address. But IFA_LOCAL is
760 * not included in all cases, so fallback to IFA_ADDRESS. */
761 if (local.ptr)
762 {
763 host = host_create_from_chunk(msg->ifa_family, local, 0);
764 }
765 else if (address.ptr)
766 {
767 host = host_create_from_chunk(msg->ifa_family, address, 0);
768 }
769
770 if (host)
771 {
772 address_entry_t *entry;
773
774 entry = malloc_thing(address_entry_t);
775 entry->host = host;
776 entry->ifindex = msg->ifa_index;
777 if (name)
778 {
779 memcpy(entry->ifname, name, IFNAMSIZ);
780 }
781 else
782 {
783 strcpy(entry->ifname, "(unknown)");
784 }
785 list->insert_last(list, entry);
786 }
787 hdr = NLMSG_NEXT(hdr, len);
788 continue;
789 }
790 default:
791 hdr = NLMSG_NEXT(hdr, len);
792 continue;
793 case NLMSG_DONE:
794 break;
795 }
796 break;
797 }
798 free(out);
799 }
800 else
801 {
802 DBG1(DBG_IKE, "unable to get local address list");
803 }
804
805 return list;
806 }
807
808 /**
809 * Implements kernel_interface_t.create_address_list.
810 */
811 static linked_list_t *create_address_list_public(private_kernel_interface_t *this)
812 {
813 linked_list_t *result, *list;
814 address_entry_t *entry;
815
816 result = linked_list_create();
817 list = create_address_list(this);
818 while (list->remove_last(list, (void**)&entry) == SUCCESS)
819 {
820 result->insert_last(result, entry->host);
821 free(entry);
822 }
823 list->destroy(list);
824
825 return result;
826 }
827
828 /**
829 * implementation of kernel_interface_t.get_interface_name
830 */
831 static char *get_interface_name(private_kernel_interface_t *this, host_t* ip)
832 {
833 linked_list_t *list;
834 address_entry_t *entry;
835 char *name = NULL;
836
837 DBG2(DBG_IKE, "getting interface name for %H", ip);
838
839 list = create_address_list(this);
840 while (!name && list->remove_last(list, (void**)&entry) == SUCCESS)
841 {
842 if (ip->ip_equals(ip, entry->host))
843 {
844 name = strdup(entry->ifname);
845 }
846 address_entry_destroy(entry);
847 }
848 list->destroy_function(list, (void*)address_entry_destroy);
849
850 if (name)
851 {
852 DBG2(DBG_IKE, "%H is on interface %s", ip, name);
853 }
854 else
855 {
856 DBG2(DBG_IKE, "%H is not a local address", ip);
857 }
858 return name;
859 }
860
861 /**
862 * Tries to find an ip address of a local interface that is included in the
863 * supplied traffic selector.
864 */
865 static status_t get_address_by_ts(private_kernel_interface_t *this,
866 traffic_selector_t *ts, host_t **ip)
867 {
868 address_entry_t *entry;
869 host_t *host;
870 int family;
871 linked_list_t *list;
872 bool found = FALSE;
873
874 DBG2(DBG_IKE, "getting a local address in traffic selector %R", ts);
875
876 /* if we have a family which includes localhost, we do not
877 * search for an IP, we use the default */
878 family = ts->get_type(ts) == TS_IPV4_ADDR_RANGE ? AF_INET : AF_INET6;
879
880 if (family == AF_INET)
881 {
882 host = host_create_from_string("127.0.0.1", 0);
883 }
884 else
885 {
886 host = host_create_from_string("::1", 0);
887 }
888
889 if (ts->includes(ts, host))
890 {
891 *ip = host_create_any(family);
892 host->destroy(host);
893 DBG2(DBG_IKE, "using host %H", *ip);
894 return SUCCESS;
895 }
896 host->destroy(host);
897
898 list = create_address_list(this);
899 while (!found && list->remove_last(list, (void**)&entry) == SUCCESS)
900 {
901 if (ts->includes(ts, entry->host))
902 {
903 found = TRUE;
904 *ip = entry->host->clone(entry->host);
905 }
906 address_entry_destroy(entry);
907 }
908 list->destroy_function(list, (void*)address_entry_destroy);
909
910 if (!found)
911 {
912 DBG1(DBG_IKE, "no local address found in traffic selector %R", ts);
913 return FAILED;
914 }
915 DBG2(DBG_IKE, "using host %H", *ip);
916 return SUCCESS;
917 }
918
919 /**
920 * get the interface of a local address
921 */
922 static int get_interface_index(private_kernel_interface_t *this, host_t* ip)
923 {
924 linked_list_t *list;
925 address_entry_t *entry;
926 int ifindex = 0;
927
928 DBG2(DBG_IKE, "getting iface for %H", ip);
929
930 list = create_address_list(this);
931 while (!ifindex && list->remove_last(list, (void**)&entry) == SUCCESS)
932 {
933 if (ip->ip_equals(ip, entry->host))
934 {
935 ifindex = entry->ifindex;
936 }
937 address_entry_destroy(entry);
938 }
939 list->destroy_function(list, (void*)address_entry_destroy);
940
941 if (ifindex == 0)
942 {
943 DBG1(DBG_IKE, "unable to get interface for %H", ip);
944 }
945 return ifindex;
946 }
947
948 /**
949 * Manages the creation and deletion of ip addresses on an interface.
950 * By setting the appropriate nlmsg_type, the ip will be set or unset.
951 */
952 static status_t manage_ipaddr(private_kernel_interface_t *this, int nlmsg_type,
953 int flags, int if_index, host_t *ip)
954 {
955 unsigned char request[BUFFER_SIZE];
956 struct nlmsghdr *hdr;
957 struct ifaddrmsg *msg;
958 chunk_t chunk;
959
960 memset(&request, 0, sizeof(request));
961
962 chunk = ip->get_address(ip);
963
964 hdr = (struct nlmsghdr*)request;
965 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
966 hdr->nlmsg_type = nlmsg_type;
967 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
968
969 msg = (struct ifaddrmsg*)NLMSG_DATA(hdr);
970 msg->ifa_family = ip->get_family(ip);
971 msg->ifa_flags = 0;
972 msg->ifa_prefixlen = 8 * chunk.len;
973 msg->ifa_scope = RT_SCOPE_UNIVERSE;
974 msg->ifa_index = if_index;
975
976 add_attribute(hdr, IFA_LOCAL, chunk, sizeof(request));
977
978 return netlink_send_ack(this->socket_rt, hdr);
979 }
980
981 /**
982 * Manages source routes in the routing table.
983 * By setting the appropriate nlmsg_type, the route added or r.
984 */
985 static status_t manage_srcroute(private_kernel_interface_t *this, int nlmsg_type,
986 int flags, route_entry_t *route)
987 {
988 unsigned char request[BUFFER_SIZE];
989 struct nlmsghdr *hdr;
990 struct rtmsg *msg;
991 chunk_t chunk;
992
993 /* if route is 0.0.0.0/0, we can't install it, as it would
994 * overwrite the default route. Instead, we add two routes:
995 * 0.0.0.0/1 and 128.0.0.0/1
996 * TODO: use metrics instead */
997 if (route->prefixlen == 0)
998 {
999 route_entry_t half;
1000 status_t status;
1001
1002 half.dst_net = chunk_alloca(route->dst_net.len);
1003 memset(half.dst_net.ptr, 0, half.dst_net.len);
1004 half.src_ip = route->src_ip;
1005 half.gateway = route->gateway;
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 = dst->clone(dst);
1702 policy->route->if_index = get_interface_index(this, dst);
1703 policy->route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
1704 memcpy(policy->route->dst_net.ptr, &policy->sel.saddr, policy->route->dst_net.len);
1705 policy->route->prefixlen = policy->sel.prefixlen_s;
1706
1707 if (manage_srcroute(this, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL,
1708 policy->route) != SUCCESS)
1709 {
1710 DBG1(DBG_KNL, "unable to install source route for %H",
1711 policy->route->src_ip);
1712 route_entry_destroy(policy->route);
1713 policy->route = NULL;
1714 }
1715 }
1716 else
1717 {
1718 free(policy->route);
1719 policy->route = NULL;
1720 }
1721 }
1722
1723 return SUCCESS;
1724 }
1725
1726 /**
1727 * Implementation of kernel_interface_t.query_policy.
1728 */
1729 static status_t query_policy(private_kernel_interface_t *this,
1730 traffic_selector_t *src_ts,
1731 traffic_selector_t *dst_ts,
1732 policy_dir_t direction, u_int32_t *use_time)
1733 {
1734 unsigned char request[BUFFER_SIZE];
1735 struct nlmsghdr *out = NULL, *hdr;
1736 struct xfrm_userpolicy_id *policy_id;
1737 struct xfrm_userpolicy_info *policy = NULL;
1738 size_t len;
1739
1740 memset(&request, 0, sizeof(request));
1741
1742 DBG2(DBG_KNL, "querying policy %R===%R", src_ts, dst_ts);
1743
1744 hdr = (struct nlmsghdr*)request;
1745 hdr->nlmsg_flags = NLM_F_REQUEST;
1746 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
1747 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1748
1749 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1750 policy_id->sel = ts2selector(src_ts, dst_ts);
1751 policy_id->dir = direction;
1752
1753 if (netlink_send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1754 {
1755 hdr = out;
1756 while (NLMSG_OK(hdr, len))
1757 {
1758 switch (hdr->nlmsg_type)
1759 {
1760 case XFRM_MSG_NEWPOLICY:
1761 {
1762 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1763 break;
1764 }
1765 case NLMSG_ERROR:
1766 {
1767 struct nlmsgerr *err = NLMSG_DATA(hdr);
1768 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
1769 strerror(-err->error), -err->error);
1770 break;
1771 }
1772 default:
1773 hdr = NLMSG_NEXT(hdr, len);
1774 continue;
1775 case NLMSG_DONE:
1776 break;
1777 }
1778 break;
1779 }
1780 }
1781
1782 if (policy == NULL)
1783 {
1784 DBG2(DBG_KNL, "unable to query policy %R===%R", src_ts, dst_ts);
1785 free(out);
1786 return FAILED;
1787 }
1788 *use_time = (time_t)policy->curlft.use_time;
1789
1790 free(out);
1791 return SUCCESS;
1792 }
1793
1794 /**
1795 * Implementation of kernel_interface_t.del_policy.
1796 */
1797 static status_t del_policy(private_kernel_interface_t *this,
1798 traffic_selector_t *src_ts,
1799 traffic_selector_t *dst_ts,
1800 policy_dir_t direction)
1801 {
1802 policy_entry_t *current, policy, *to_delete = NULL;
1803 route_entry_t *route;
1804 unsigned char request[BUFFER_SIZE];
1805 struct nlmsghdr *hdr;
1806 struct xfrm_userpolicy_id *policy_id;
1807 iterator_t *iterator;
1808
1809 DBG2(DBG_KNL, "deleting policy %R===%R", src_ts, dst_ts);
1810
1811 /* create a policy */
1812 memset(&policy, 0, sizeof(policy_entry_t));
1813 policy.sel = ts2selector(src_ts, dst_ts);
1814 policy.direction = direction;
1815
1816 /* find the policy */
1817 pthread_mutex_lock(&this->policies_mutex);
1818 iterator = this->policies->create_iterator(this->policies, TRUE);
1819 while (iterator->iterate(iterator, (void**)&current))
1820 {
1821 if (memcmp(&current->sel, &policy.sel, sizeof(struct xfrm_selector)) == 0 &&
1822 policy.direction == current->direction)
1823 {
1824 to_delete = current;
1825 if (--to_delete->refcount > 0)
1826 {
1827 /* is used by more SAs, keep in kernel */
1828 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
1829 iterator->destroy(iterator);
1830 pthread_mutex_unlock(&this->policies_mutex);
1831 return SUCCESS;
1832 }
1833 /* remove if last reference */
1834 iterator->remove(iterator);
1835 break;
1836 }
1837 }
1838 iterator->destroy(iterator);
1839 pthread_mutex_unlock(&this->policies_mutex);
1840 if (!to_delete)
1841 {
1842 DBG1(DBG_KNL, "deleting policy %R===%R failed, not found", src_ts, dst_ts);
1843 return NOT_FOUND;
1844 }
1845
1846 memset(&request, 0, sizeof(request));
1847
1848 hdr = (struct nlmsghdr*)request;
1849 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1850 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
1851 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1852
1853 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1854 policy_id->sel = to_delete->sel;
1855 policy_id->dir = direction;
1856
1857 route = to_delete->route;
1858 free(to_delete);
1859
1860 if (netlink_send_ack(this->socket_xfrm, hdr) != SUCCESS)
1861 {
1862 DBG1(DBG_KNL, "unable to delete policy %R===%R", src_ts, dst_ts);
1863 return FAILED;
1864 }
1865
1866 if (route)
1867 {
1868 if (manage_srcroute(this, RTM_DELROUTE, 0, route) != SUCCESS)
1869 {
1870 DBG1(DBG_KNL, "error uninstalling route installed with "
1871 "policy %R===%R", src_ts, dst_ts);
1872 }
1873 route_entry_destroy(route);
1874 }
1875 return SUCCESS;
1876 }
1877
1878 /**
1879 * Implementation of kernel_interface_t.destroy.
1880 */
1881 static void destroy(private_kernel_interface_t *this)
1882 {
1883 pthread_cancel(this->event_thread);
1884 pthread_join(this->event_thread, NULL);
1885 close(this->socket_xfrm_events);
1886 close(this->socket_xfrm);
1887 close(this->socket_rt);
1888 this->vips->destroy(this->vips);
1889 this->policies->destroy(this->policies);
1890 free(this);
1891 }
1892
1893 /*
1894 * Described in header.
1895 */
1896 kernel_interface_t *kernel_interface_create()
1897 {
1898 private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
1899 struct sockaddr_nl addr;
1900
1901 /* public functions */
1902 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
1903 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;
1904 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;
1905 this->public.query_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t*))query_sa;
1906 this->public.del_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
1907 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;
1908 this->public.query_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
1909 this->public.del_policy = (status_t(*)(kernel_interface_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t))del_policy;
1910
1911 this->public.get_interface = (char*(*)(kernel_interface_t*,host_t*))get_interface_name;
1912 this->public.create_address_list = (linked_list_t*(*)(kernel_interface_t*))create_address_list_public;
1913 this->public.add_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) add_ip;
1914 this->public.del_ip = (status_t(*)(kernel_interface_t*,host_t*,host_t*)) del_ip;
1915 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
1916
1917 /* private members */
1918 this->vips = linked_list_create();
1919 this->policies = linked_list_create();
1920 pthread_mutex_init(&this->policies_mutex,NULL);
1921 pthread_mutex_init(&this->vips_mutex,NULL);
1922
1923 addr.nl_family = AF_NETLINK;
1924 addr.nl_pid = 0;
1925 addr.nl_groups = 0;
1926
1927 /* create and bind XFRM socket */
1928 this->socket_xfrm = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1929 if (this->socket_xfrm <= 0)
1930 {
1931 charon->kill(charon, "unable to create XFRM netlink socket");
1932 }
1933
1934 if (bind(this->socket_xfrm, (struct sockaddr*)&addr, sizeof(addr)))
1935 {
1936 charon->kill(charon, "unable to bind XFRM netlink socket");
1937 }
1938
1939 /* create and bind RT socket */
1940 this->socket_rt = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1941 if (this->socket_rt <= 0)
1942 {
1943 charon->kill(charon, "unable to create RT netlink socket");
1944 }
1945
1946 if (bind(this->socket_rt, (struct sockaddr*)&addr, sizeof(addr)))
1947 {
1948 charon->kill(charon, "unable to bind RT netlink socket");
1949 }
1950
1951 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
1952 addr.nl_groups = XFRMGRP_ACQUIRE | XFRMGRP_EXPIRE;
1953 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1954 if (this->socket_xfrm_events <= 0)
1955 {
1956 charon->kill(charon, "unable to create XFRM event socket");
1957 }
1958
1959 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
1960 {
1961 charon->kill(charon, "unable to bind XFRM event socket");
1962 }
1963
1964 /* create a thread receiving ACQUIRE & EXPIRE events */
1965 if (pthread_create(&this->event_thread, NULL,
1966 (void*(*)(void*))receive_events, this))
1967 {
1968 charon->kill(charon, "unable to create xfrm event dispatcher thread");
1969 }
1970
1971 return &this->public;
1972 }
1973
1974 /* vim: set ts=4 sw=4 noet: */