cdf9c1e992a1da1df156caa21f0e49101c0735fd
[strongswan.git] / src / charon / plugins / kernel_netlink / kernel_netlink_ipsec.c
1 /*
2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
5 * Copyright (C) 2006 Daniel Roethlisberger
6 * Copyright (C) 2005 Jan Hutter
7 * Hochschule fuer Technik Rapperswil
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * $Id$
20 */
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 #include <linux/netlink.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/xfrm.h>
28 #include <linux/udp.h>
29 #include <netinet/in.h>
30 #include <pthread.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include "kernel_netlink_ipsec.h"
36 #include "kernel_netlink_shared.h"
37
38 #include <daemon.h>
39 #include <utils/linked_list.h>
40 #include <processing/jobs/callback_job.h>
41 #include <processing/jobs/acquire_job.h>
42 #include <processing/jobs/migrate_job.h>
43 #include <processing/jobs/rekey_child_sa_job.h>
44 #include <processing/jobs/delete_child_sa_job.h>
45 #include <processing/jobs/update_sa_job.h>
46
47 /** required for Linux 2.6.26 kernel and later */
48 #ifndef XFRM_STATE_AF_UNSPEC
49 #define XFRM_STATE_AF_UNSPEC 32
50 #endif
51
52 /** default priority of installed policies */
53 #define PRIO_LOW 3000
54 #define PRIO_HIGH 2000
55
56 /**
57 * Create ORable bitfield of XFRM NL groups
58 */
59 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
60
61 /**
62 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
63 * 'usual' netlink data x like 'struct xfrm_usersa_info'
64 */
65 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
66 /**
67 * returns a pointer to the next rtattr following rta.
68 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
69 */
70 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
71 /**
72 * returns the total size of attached rta data
73 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
74 */
75 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
76
77 typedef struct kernel_algorithm_t kernel_algorithm_t;
78
79 /**
80 * Mapping of IKEv2 kernel identifier to linux crypto API names
81 */
82 struct kernel_algorithm_t {
83 /**
84 * Identifier specified in IKEv2
85 */
86 int ikev2;
87
88 /**
89 * Name of the algorithm in linux crypto API
90 */
91 char *name;
92 };
93
94 ENUM(xfrm_attr_type_names, XFRMA_UNSPEC, XFRMA_KMADDRESS,
95 "XFRMA_UNSPEC",
96 "XFRMA_ALG_AUTH",
97 "XFRMA_ALG_CRYPT",
98 "XFRMA_ALG_COMP",
99 "XFRMA_ENCAP",
100 "XFRMA_TMPL",
101 "XFRMA_SA",
102 "XFRMA_POLICY",
103 "XFRMA_SEC_CTX",
104 "XFRMA_LTIME_VAL",
105 "XFRMA_REPLAY_VAL",
106 "XFRMA_REPLAY_THRESH",
107 "XFRMA_ETIMER_THRESH",
108 "XFRMA_SRCADDR",
109 "XFRMA_COADDR",
110 "XFRMA_LASTUSED",
111 "XFRMA_POLICY_TYPE",
112 "XFRMA_MIGRATE",
113 "XFRMA_ALG_AEAD",
114 "XFRMA_KMADDRESS"
115 );
116
117 #define END_OF_LIST -1
118
119 /**
120 * Algorithms for encryption
121 */
122 static kernel_algorithm_t encryption_algs[] = {
123 /* {ENCR_DES_IV64, "***" }, */
124 {ENCR_DES, "des" },
125 {ENCR_3DES, "des3_ede" },
126 /* {ENCR_RC5, "***" }, */
127 /* {ENCR_IDEA, "***" }, */
128 {ENCR_CAST, "cast128" },
129 {ENCR_BLOWFISH, "blowfish" },
130 /* {ENCR_3IDEA, "***" }, */
131 /* {ENCR_DES_IV32, "***" }, */
132 {ENCR_NULL, "cipher_null" },
133 {ENCR_AES_CBC, "aes" },
134 /* {ENCR_AES_CTR, "***" }, */
135 {ENCR_AES_CCM_ICV8, "rfc4309(ccm(aes))" },
136 {ENCR_AES_CCM_ICV12, "rfc4309(ccm(aes))" },
137 {ENCR_AES_CCM_ICV16, "rfc4309(ccm(aes))" },
138 {ENCR_AES_GCM_ICV8, "rfc4106(gcm(aes))" },
139 {ENCR_AES_GCM_ICV12, "rfc4106(gcm(aes))" },
140 {ENCR_AES_GCM_ICV16, "rfc4106(gcm(aes))" },
141 {END_OF_LIST, NULL },
142 };
143
144 /**
145 * Algorithms for integrity protection
146 */
147 static kernel_algorithm_t integrity_algs[] = {
148 {AUTH_HMAC_MD5_96, "md5" },
149 {AUTH_HMAC_SHA1_96, "sha1" },
150 {AUTH_HMAC_SHA2_256_128, "sha256" },
151 {AUTH_HMAC_SHA2_384_192, "sha384" },
152 {AUTH_HMAC_SHA2_512_256, "sha512" },
153 /* {AUTH_DES_MAC, "***" }, */
154 /* {AUTH_KPDK_MD5, "***" }, */
155 {AUTH_AES_XCBC_96, "xcbc(aes)" },
156 {END_OF_LIST, NULL },
157 };
158
159 /**
160 * Algorithms for IPComp
161 */
162 static kernel_algorithm_t compression_algs[] = {
163 /* {IPCOMP_OUI, "***" }, */
164 {IPCOMP_DEFLATE, "deflate" },
165 {IPCOMP_LZS, "lzs" },
166 {IPCOMP_LZJH, "lzjh" },
167 {END_OF_LIST, NULL },
168 };
169
170 /**
171 * Look up a kernel algorithm name and its key size
172 */
173 static char* lookup_algorithm(kernel_algorithm_t *list, int ikev2)
174 {
175 while (list->ikev2 != END_OF_LIST)
176 {
177 if (list->ikev2 == ikev2)
178 {
179 return list->name;
180 }
181 list++;
182 }
183 return NULL;
184 }
185
186 typedef struct route_entry_t route_entry_t;
187
188 /**
189 * installed routing entry
190 */
191 struct route_entry_t {
192 /** Name of the interface the route is bound to */
193 char *if_name;
194
195 /** Source ip of the route */
196 host_t *src_ip;
197
198 /** gateway for this route */
199 host_t *gateway;
200
201 /** Destination net */
202 chunk_t dst_net;
203
204 /** Destination net prefixlen */
205 u_int8_t prefixlen;
206 };
207
208 /**
209 * destroy an route_entry_t object
210 */
211 static void route_entry_destroy(route_entry_t *this)
212 {
213 free(this->if_name);
214 this->src_ip->destroy(this->src_ip);
215 this->gateway->destroy(this->gateway);
216 chunk_free(&this->dst_net);
217 free(this);
218 }
219
220 typedef struct policy_entry_t policy_entry_t;
221
222 /**
223 * installed kernel policy.
224 */
225 struct policy_entry_t {
226
227 /** direction of this policy: in, out, forward */
228 u_int8_t direction;
229
230 /** parameters of installed policy */
231 struct xfrm_selector sel;
232
233 /** associated route installed for this policy */
234 route_entry_t *route;
235
236 /** by how many CHILD_SA's this policy is used */
237 u_int refcount;
238 };
239
240 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t;
241
242 /**
243 * Private variables and functions of kernel_netlink class.
244 */
245 struct private_kernel_netlink_ipsec_t {
246 /**
247 * Public part of the kernel_netlink_t object.
248 */
249 kernel_netlink_ipsec_t public;
250
251 /**
252 * mutex to lock access to various lists
253 */
254 pthread_mutex_t mutex;
255
256 /**
257 * List of installed policies (policy_entry_t)
258 */
259 linked_list_t *policies;
260
261 /**
262 * job receiving netlink events
263 */
264 callback_job_t *job;
265
266 /**
267 * Netlink xfrm socket (IPsec)
268 */
269 netlink_socket_t *socket_xfrm;
270
271 /**
272 * netlink xfrm socket to receive acquire and expire events
273 */
274 int socket_xfrm_events;
275
276 /**
277 * whether to install routes along policies
278 */
279 bool install_routes;
280 };
281
282 /**
283 * convert a IKEv2 specific protocol identifier to the kernel one
284 */
285 static u_int8_t proto_ike2kernel(protocol_id_t proto)
286 {
287 switch (proto)
288 {
289 case PROTO_ESP:
290 return IPPROTO_ESP;
291 case PROTO_AH:
292 return IPPROTO_AH;
293 default:
294 return proto;
295 }
296 }
297
298 /**
299 * reverse of ike2kernel
300 */
301 static protocol_id_t proto_kernel2ike(u_int8_t proto)
302 {
303 switch (proto)
304 {
305 case IPPROTO_ESP:
306 return PROTO_ESP;
307 case IPPROTO_AH:
308 return PROTO_AH;
309 default:
310 return proto;
311 }
312 }
313
314 /**
315 * convert a host_t to a struct xfrm_address
316 */
317 static void host2xfrm(host_t *host, xfrm_address_t *xfrm)
318 {
319 chunk_t chunk = host->get_address(host);
320 memcpy(xfrm, chunk.ptr, min(chunk.len, sizeof(xfrm_address_t)));
321 }
322
323 /**
324 * convert a struct xfrm_address to a host_t
325 */
326 static host_t* xfrm2host(int family, xfrm_address_t *xfrm, u_int16_t port)
327 {
328 chunk_t chunk;
329
330 switch (family)
331 {
332 case AF_INET:
333 chunk = chunk_create((u_char*)&xfrm->a4, sizeof(xfrm->a4));
334 break;
335 case AF_INET6:
336 chunk = chunk_create((u_char*)&xfrm->a6, sizeof(xfrm->a6));
337 break;
338 default:
339 return NULL;
340 }
341 return host_create_from_chunk(family, chunk, ntohs(port));
342 }
343
344 /**
345 * convert a traffic selector address range to subnet and its mask.
346 */
347 static void ts2subnet(traffic_selector_t* ts,
348 xfrm_address_t *net, u_int8_t *mask)
349 {
350 host_t *net_host;
351 chunk_t net_chunk;
352
353 ts->to_subnet(ts, &net_host, mask);
354 net_chunk = net_host->get_address(net_host);
355 memcpy(net, net_chunk.ptr, net_chunk.len);
356 net_host->destroy(net_host);
357 }
358
359 /**
360 * convert a traffic selector port range to port/portmask
361 */
362 static void ts2ports(traffic_selector_t* ts,
363 u_int16_t *port, u_int16_t *mask)
364 {
365 /* linux does not seem to accept complex portmasks. Only
366 * any or a specific port is allowed. We set to any, if we have
367 * a port range, or to a specific, if we have one port only.
368 */
369 u_int16_t from, to;
370
371 from = ts->get_from_port(ts);
372 to = ts->get_to_port(ts);
373
374 if (from == to)
375 {
376 *port = htons(from);
377 *mask = ~0;
378 }
379 else
380 {
381 *port = 0;
382 *mask = 0;
383 }
384 }
385
386 /**
387 * convert a pair of traffic_selectors to a xfrm_selector
388 */
389 static struct xfrm_selector ts2selector(traffic_selector_t *src,
390 traffic_selector_t *dst)
391 {
392 struct xfrm_selector sel;
393
394 memset(&sel, 0, sizeof(sel));
395 sel.family = (src->get_type(src) == TS_IPV4_ADDR_RANGE) ? AF_INET : AF_INET6;
396 /* src or dest proto may be "any" (0), use more restrictive one */
397 sel.proto = max(src->get_protocol(src), dst->get_protocol(dst));
398 ts2subnet(dst, &sel.daddr, &sel.prefixlen_d);
399 ts2subnet(src, &sel.saddr, &sel.prefixlen_s);
400 ts2ports(dst, &sel.dport, &sel.dport_mask);
401 ts2ports(src, &sel.sport, &sel.sport_mask);
402 sel.ifindex = 0;
403 sel.user = 0;
404
405 return sel;
406 }
407
408 /**
409 * convert a xfrm_selector to a src|dst traffic_selector
410 */
411 static traffic_selector_t* selector2ts(struct xfrm_selector *sel, bool src)
412 {
413 int family;
414 chunk_t addr;
415 u_int8_t prefixlen;
416 u_int16_t port, port_mask;
417 host_t *host;
418 traffic_selector_t *ts;
419
420 if (src)
421 {
422 addr.ptr = (u_char*)&sel->saddr;
423 prefixlen = sel->prefixlen_s;
424 port = sel->sport;
425 port_mask = sel->sport_mask;
426 }
427 else
428 {
429 addr.ptr = (u_char*)&sel->daddr;
430 prefixlen = sel->prefixlen_d;
431 port = sel->dport;
432 port_mask = sel->dport_mask;
433 }
434
435 /* The Linux 2.6 kernel does not set the selector's family field,
436 * so as a kludge we additionally test the prefix length.
437 */
438 if (sel->family == AF_INET || sel->prefixlen_s == 32)
439 {
440 family = AF_INET;
441 addr.len = 4;
442 }
443 else if (sel->family == AF_INET6 || sel->prefixlen_s == 128)
444 {
445 family = AF_INET6;
446 addr.len = 16;
447 }
448 else
449 {
450 return NULL;
451 }
452 host = host_create_from_chunk(family, addr, 0);
453 port = (port_mask == 0) ? 0 : ntohs(port);
454
455 ts = traffic_selector_create_from_subnet(host, prefixlen, sel->proto, port);
456 host->destroy(host);
457 return ts;
458 }
459
460 /**
461 * process a XFRM_MSG_ACQUIRE from kernel
462 */
463 static void process_acquire(private_kernel_netlink_ipsec_t *this, struct nlmsghdr *hdr)
464 {
465 u_int32_t reqid = 0;
466 int proto = 0;
467 traffic_selector_t *src_ts, *dst_ts;
468 struct xfrm_user_acquire *acquire;
469 struct rtattr *rta;
470 size_t rtasize;
471 job_t *job;
472
473 acquire = (struct xfrm_user_acquire*)NLMSG_DATA(hdr);
474 rta = XFRM_RTA(hdr, struct xfrm_user_acquire);
475 rtasize = XFRM_PAYLOAD(hdr, struct xfrm_user_acquire);
476
477 DBG2(DBG_KNL, "received a XFRM_MSG_ACQUIRE");
478
479 while (RTA_OK(rta, rtasize))
480 {
481 DBG2(DBG_KNL, " %N", xfrm_attr_type_names, rta->rta_type);
482
483 if (rta->rta_type == XFRMA_TMPL)
484 {
485 struct xfrm_user_tmpl* tmpl;
486
487 tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rta);
488 reqid = tmpl->reqid;
489 proto = tmpl->id.proto;
490 }
491 rta = RTA_NEXT(rta, rtasize);
492 }
493 switch (proto)
494 {
495 case 0:
496 case IPPROTO_ESP:
497 case IPPROTO_AH:
498 break;
499 default:
500 /* acquire for AH/ESP only, not for IPCOMP */
501 return;
502 }
503 src_ts = selector2ts(&acquire->sel, TRUE);
504 dst_ts = selector2ts(&acquire->sel, FALSE);
505 DBG1(DBG_KNL, "creating acquire job for policy %R === %R with reqid {%u}",
506 src_ts, dst_ts, reqid);
507 job = (job_t*)acquire_job_create(reqid, src_ts, dst_ts);
508 charon->processor->queue_job(charon->processor, job);
509 }
510
511 /**
512 * process a XFRM_MSG_EXPIRE from kernel
513 */
514 static void process_expire(private_kernel_netlink_ipsec_t *this, struct nlmsghdr *hdr)
515 {
516 job_t *job;
517 protocol_id_t protocol;
518 u_int32_t spi, reqid;
519 struct xfrm_user_expire *expire;
520
521 expire = (struct xfrm_user_expire*)NLMSG_DATA(hdr);
522 protocol = proto_kernel2ike(expire->state.id.proto);
523 spi = expire->state.id.spi;
524 reqid = expire->state.reqid;
525
526 DBG2(DBG_KNL, "received a XFRM_MSG_EXPIRE");
527
528 if (protocol != PROTO_ESP && protocol != PROTO_AH)
529 {
530 DBG2(DBG_KNL, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and reqid {%u} "
531 "which is not a CHILD_SA", ntohl(spi), reqid);
532 return;
533 }
534
535 DBG1(DBG_KNL, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
536 expire->hard ? "delete" : "rekey", protocol_id_names,
537 protocol, ntohl(spi), reqid);
538 if (expire->hard)
539 {
540 job = (job_t*)delete_child_sa_job_create(reqid, protocol, spi);
541 }
542 else
543 {
544 job = (job_t*)rekey_child_sa_job_create(reqid, protocol, spi);
545 }
546 charon->processor->queue_job(charon->processor, job);
547 }
548
549 /**
550 * process a XFRM_MSG_MIGRATE from kernel
551 */
552 static void process_migrate(private_kernel_netlink_ipsec_t *this, struct nlmsghdr *hdr)
553 {
554 traffic_selector_t *src_ts, *dst_ts;
555 host_t *local = NULL, *remote = NULL;
556 host_t *old_src = NULL, *old_dst = NULL;
557 host_t *new_src = NULL, *new_dst = NULL;
558 struct xfrm_userpolicy_id *policy_id;
559 struct rtattr *rta;
560 size_t rtasize;
561 u_int32_t reqid = 0;
562 policy_dir_t dir;
563 job_t *job;
564
565 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
566 rta = XFRM_RTA(hdr, struct xfrm_userpolicy_id);
567 rtasize = XFRM_PAYLOAD(hdr, struct xfrm_userpolicy_id);
568
569 DBG2(DBG_KNL, "received a XFRM_MSG_MIGRATE");
570
571 src_ts = selector2ts(&policy_id->sel, TRUE);
572 dst_ts = selector2ts(&policy_id->sel, FALSE);
573 dir = (policy_dir_t)policy_id->dir;
574
575 DBG2(DBG_KNL, " policy: %R === %R %N, index %u", src_ts, dst_ts,
576 policy_dir_names, dir, policy_id->index);
577
578 while (RTA_OK(rta, rtasize))
579 {
580 DBG2(DBG_KNL, " %N", xfrm_attr_type_names, rta->rta_type);
581 if (rta->rta_type == XFRMA_KMADDRESS)
582 {
583 struct xfrm_user_kmaddress *kmaddress;
584
585 kmaddress = (struct xfrm_user_kmaddress*)RTA_DATA(rta);
586 local = xfrm2host(kmaddress->family, &kmaddress->local, 0);
587 remote = xfrm2host(kmaddress->family, &kmaddress->remote, 0);
588 DBG2(DBG_KNL, " %H...%H", local, remote);
589 DESTROY_IF(remote); }
590 else if (rta->rta_type == XFRMA_MIGRATE)
591 {
592 struct xfrm_user_migrate *migrate;
593 protocol_id_t proto;
594
595 migrate = (struct xfrm_user_migrate*)RTA_DATA(rta);
596 old_src = xfrm2host(migrate->old_family, &migrate->old_saddr, 0);
597 old_dst = xfrm2host(migrate->old_family, &migrate->old_daddr, 0);
598 new_src = xfrm2host(migrate->new_family, &migrate->new_saddr, 0);
599 new_dst = xfrm2host(migrate->new_family, &migrate->new_daddr, 0);
600 proto = proto_kernel2ike(migrate->proto);
601 reqid = migrate->reqid;
602 DBG2(DBG_KNL, " migrate %N %H...%H to %H...%H, reqid {%u}",
603 protocol_id_names, proto, old_src, old_dst,
604 new_src, new_src, reqid);
605 DESTROY_IF(old_src);
606 DESTROY_IF(old_dst);
607 DESTROY_IF(new_src);
608 DESTROY_IF(new_dst);
609 }
610 rta = RTA_NEXT(rta, rtasize);
611 }
612
613 if (src_ts && dst_ts)
614 {
615 DBG1(DBG_KNL, "creating migrate job for policy %R === %R %N "
616 "with reqid {%u}, kmaddress = %H",
617 src_ts, dst_ts, policy_dir_names, dir, reqid, local);
618 job = (job_t*)migrate_job_create(reqid, src_ts, dst_ts, dir, local);
619 charon->processor->queue_job(charon->processor, job);
620 }
621 else
622 {
623 DESTROY_IF(src_ts);
624 DESTROY_IF(dst_ts);
625 DESTROY_IF(local);
626 }
627 }
628
629 /**
630 * process a XFRM_MSG_MAPPING from kernel
631 */
632 static void process_mapping(private_kernel_netlink_ipsec_t *this,
633 struct nlmsghdr *hdr)
634 {
635 job_t *job;
636 u_int32_t spi, reqid;
637 struct xfrm_user_mapping *mapping;
638 host_t *host;
639
640 mapping = (struct xfrm_user_mapping*)NLMSG_DATA(hdr);
641 spi = mapping->id.spi;
642 reqid = mapping->reqid;
643
644 DBG2(DBG_KNL, "received a XFRM_MSG_MAPPING");
645
646 if (proto_kernel2ike(mapping->id.proto) == PROTO_ESP)
647 {
648 host = xfrm2host(mapping->id.family, &mapping->new_saddr,
649 mapping->new_sport);
650 if (host)
651 {
652 DBG1(DBG_KNL, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
653 "reqid {%u} changed, queuing update job", ntohl(spi), reqid);
654 job = (job_t*)update_sa_job_create(reqid, host);
655 charon->processor->queue_job(charon->processor, job);
656 }
657 }
658 }
659
660 /**
661 * Receives events from kernel
662 */
663 static job_requeue_t receive_events(private_kernel_netlink_ipsec_t *this)
664 {
665 char response[1024];
666 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
667 struct sockaddr_nl addr;
668 socklen_t addr_len = sizeof(addr);
669 int len, oldstate;
670
671 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
672 len = recvfrom(this->socket_xfrm_events, response, sizeof(response), 0,
673 (struct sockaddr*)&addr, &addr_len);
674 pthread_setcancelstate(oldstate, NULL);
675
676 if (len < 0)
677 {
678 switch (errno)
679 {
680 case EINTR:
681 /* interrupted, try again */
682 return JOB_REQUEUE_DIRECT;
683 case EAGAIN:
684 /* no data ready, select again */
685 return JOB_REQUEUE_DIRECT;
686 default:
687 DBG1(DBG_KNL, "unable to receive from xfrm event socket");
688 sleep(1);
689 return JOB_REQUEUE_FAIR;
690 }
691 }
692
693 if (addr.nl_pid != 0)
694 { /* not from kernel. not interested, try another one */
695 return JOB_REQUEUE_DIRECT;
696 }
697
698 while (NLMSG_OK(hdr, len))
699 {
700 switch (hdr->nlmsg_type)
701 {
702 case XFRM_MSG_ACQUIRE:
703 process_acquire(this, hdr);
704 break;
705 case XFRM_MSG_EXPIRE:
706 process_expire(this, hdr);
707 break;
708 case XFRM_MSG_MIGRATE:
709 process_migrate(this, hdr);
710 break;
711 case XFRM_MSG_MAPPING:
712 process_mapping(this, hdr);
713 break;
714 default:
715 break;
716 }
717 hdr = NLMSG_NEXT(hdr, len);
718 }
719 return JOB_REQUEUE_DIRECT;
720 }
721
722 /**
723 * Get an SPI for a specific protocol from the kernel.
724 */
725 static status_t get_spi_internal(private_kernel_netlink_ipsec_t *this,
726 host_t *src, host_t *dst, u_int8_t proto, u_int32_t min, u_int32_t max,
727 u_int32_t reqid, u_int32_t *spi)
728 {
729 unsigned char request[NETLINK_BUFFER_SIZE];
730 struct nlmsghdr *hdr, *out;
731 struct xfrm_userspi_info *userspi;
732 u_int32_t received_spi = 0;
733 size_t len;
734
735 memset(&request, 0, sizeof(request));
736
737 hdr = (struct nlmsghdr*)request;
738 hdr->nlmsg_flags = NLM_F_REQUEST;
739 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
740 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
741
742 userspi = (struct xfrm_userspi_info*)NLMSG_DATA(hdr);
743 host2xfrm(src, &userspi->info.saddr);
744 host2xfrm(dst, &userspi->info.id.daddr);
745 userspi->info.id.proto = proto;
746 userspi->info.mode = TRUE; /* tunnel mode */
747 userspi->info.reqid = reqid;
748 userspi->info.family = src->get_family(src);
749 userspi->min = min;
750 userspi->max = max;
751
752 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
753 {
754 hdr = out;
755 while (NLMSG_OK(hdr, len))
756 {
757 switch (hdr->nlmsg_type)
758 {
759 case XFRM_MSG_NEWSA:
760 {
761 struct xfrm_usersa_info* usersa = NLMSG_DATA(hdr);
762 received_spi = usersa->id.spi;
763 break;
764 }
765 case NLMSG_ERROR:
766 {
767 struct nlmsgerr *err = NLMSG_DATA(hdr);
768
769 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
770 strerror(-err->error), -err->error);
771 break;
772 }
773 default:
774 hdr = NLMSG_NEXT(hdr, len);
775 continue;
776 case NLMSG_DONE:
777 break;
778 }
779 break;
780 }
781 free(out);
782 }
783
784 if (received_spi == 0)
785 {
786 return FAILED;
787 }
788
789 *spi = received_spi;
790 return SUCCESS;
791 }
792
793 /**
794 * Implementation of kernel_interface_t.get_spi.
795 */
796 static status_t get_spi(private_kernel_netlink_ipsec_t *this,
797 host_t *src, host_t *dst,
798 protocol_id_t protocol, u_int32_t reqid,
799 u_int32_t *spi)
800 {
801 DBG2(DBG_KNL, "getting SPI for reqid {%u}", reqid);
802
803 if (get_spi_internal(this, src, dst, proto_ike2kernel(protocol),
804 0xc0000000, 0xcFFFFFFF, reqid, spi) != SUCCESS)
805 {
806 DBG1(DBG_KNL, "unable to get SPI for reqid {%u}", reqid);
807 return FAILED;
808 }
809
810 DBG2(DBG_KNL, "got SPI %.8x for reqid {%u}", ntohl(*spi), reqid);
811
812 return SUCCESS;
813 }
814
815 /**
816 * Implementation of kernel_interface_t.get_cpi.
817 */
818 static status_t get_cpi(private_kernel_netlink_ipsec_t *this,
819 host_t *src, host_t *dst,
820 u_int32_t reqid, u_int16_t *cpi)
821 {
822 u_int32_t received_spi = 0;
823
824 DBG2(DBG_KNL, "getting CPI for reqid {%u}", reqid);
825
826 if (get_spi_internal(this, src, dst,
827 IPPROTO_COMP, 0x100, 0xEFFF, reqid, &received_spi) != SUCCESS)
828 {
829 DBG1(DBG_KNL, "unable to get CPI for reqid {%u}", reqid);
830 return FAILED;
831 }
832
833 *cpi = htons((u_int16_t)ntohl(received_spi));
834
835 DBG2(DBG_KNL, "got CPI %.4x for reqid {%u}", ntohs(*cpi), reqid);
836
837 return SUCCESS;
838 }
839
840 /**
841 * Implementation of kernel_interface_t.add_sa.
842 */
843 static status_t add_sa(private_kernel_netlink_ipsec_t *this,
844 host_t *src, host_t *dst, u_int32_t spi,
845 protocol_id_t protocol, u_int32_t reqid,
846 u_int64_t expire_soft, u_int64_t expire_hard,
847 u_int16_t enc_alg, chunk_t enc_key,
848 u_int16_t int_alg, chunk_t int_key,
849 ipsec_mode_t mode, u_int16_t ipcomp, bool encap,
850 bool replace)
851 {
852 unsigned char request[NETLINK_BUFFER_SIZE];
853 char *alg_name;
854 struct nlmsghdr *hdr;
855 struct xfrm_usersa_info *sa;
856 u_int16_t icv_size = 64;
857
858 memset(&request, 0, sizeof(request));
859
860 DBG2(DBG_KNL, "adding SAD entry with SPI %.8x and reqid {%u}",
861 ntohl(spi), reqid);
862
863 hdr = (struct nlmsghdr*)request;
864 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
865 hdr->nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
866 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
867
868 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
869 host2xfrm(src, &sa->saddr);
870 host2xfrm(dst, &sa->id.daddr);
871 sa->id.spi = spi;
872 sa->id.proto = proto_ike2kernel(protocol);
873 sa->family = src->get_family(src);
874 sa->mode = mode;
875 if (mode == MODE_TUNNEL)
876 {
877 sa->flags |= XFRM_STATE_AF_UNSPEC;
878 }
879 sa->replay_window = (protocol == IPPROTO_COMP) ? 0 : 32;
880 sa->reqid = reqid;
881 /* we currently do not expire SAs by volume/packet count */
882 sa->lft.soft_byte_limit = XFRM_INF;
883 sa->lft.hard_byte_limit = XFRM_INF;
884 sa->lft.soft_packet_limit = XFRM_INF;
885 sa->lft.hard_packet_limit = XFRM_INF;
886 /* we use lifetimes since added, not since used */
887 sa->lft.soft_add_expires_seconds = expire_soft;
888 sa->lft.hard_add_expires_seconds = expire_hard;
889 sa->lft.soft_use_expires_seconds = 0;
890 sa->lft.hard_use_expires_seconds = 0;
891
892 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_usersa_info);
893
894 switch (enc_alg)
895 {
896 case ENCR_UNDEFINED:
897 /* no encryption */
898 break;
899 case ENCR_AES_CCM_ICV16:
900 case ENCR_AES_GCM_ICV16:
901 icv_size += 32;
902 /* FALL */
903 case ENCR_AES_CCM_ICV12:
904 case ENCR_AES_GCM_ICV12:
905 icv_size += 32;
906 /* FALL */
907 case ENCR_AES_CCM_ICV8:
908 case ENCR_AES_GCM_ICV8:
909 {
910 rthdr->rta_type = XFRMA_ALG_AEAD;
911 alg_name = lookup_algorithm(encryption_algs, enc_alg);
912 if (alg_name == NULL)
913 {
914 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
915 encryption_algorithm_names, enc_alg);
916 return FAILED;
917 }
918 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
919 encryption_algorithm_names, enc_alg, enc_key.len * 8);
920
921 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo_aead) + enc_key.len);
922 hdr->nlmsg_len += rthdr->rta_len;
923 if (hdr->nlmsg_len > sizeof(request))
924 {
925 return FAILED;
926 }
927
928 struct xfrm_algo_aead* algo = (struct xfrm_algo_aead*)RTA_DATA(rthdr);
929 algo->alg_key_len = enc_key.len * 8;
930 algo->alg_icv_len = icv_size;
931 strcpy(algo->alg_name, alg_name);
932 memcpy(algo->alg_key, enc_key.ptr, enc_key.len);
933
934 rthdr = XFRM_RTA_NEXT(rthdr);
935 break;
936 }
937 default:
938 {
939 rthdr->rta_type = XFRMA_ALG_CRYPT;
940 alg_name = lookup_algorithm(encryption_algs, enc_alg);
941 if (alg_name == NULL)
942 {
943 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
944 encryption_algorithm_names, enc_alg);
945 return FAILED;
946 }
947 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
948 encryption_algorithm_names, enc_alg, enc_key.len * 8);
949
950 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + enc_key.len);
951 hdr->nlmsg_len += rthdr->rta_len;
952 if (hdr->nlmsg_len > sizeof(request))
953 {
954 return FAILED;
955 }
956
957 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
958 algo->alg_key_len = enc_key.len * 8;
959 strcpy(algo->alg_name, alg_name);
960 memcpy(algo->alg_key, enc_key.ptr, enc_key.len);
961
962 rthdr = XFRM_RTA_NEXT(rthdr);
963 break;
964 }
965 }
966
967 if (int_alg != AUTH_UNDEFINED)
968 {
969 rthdr->rta_type = XFRMA_ALG_AUTH;
970 alg_name = lookup_algorithm(integrity_algs, int_alg);
971 if (alg_name == NULL)
972 {
973 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
974 integrity_algorithm_names, int_alg);
975 return FAILED;
976 }
977 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
978 integrity_algorithm_names, int_alg, int_key.len * 8);
979
980 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + int_key.len);
981 hdr->nlmsg_len += rthdr->rta_len;
982 if (hdr->nlmsg_len > sizeof(request))
983 {
984 return FAILED;
985 }
986
987 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
988 algo->alg_key_len = int_key.len * 8;
989 strcpy(algo->alg_name, alg_name);
990 memcpy(algo->alg_key, int_key.ptr, int_key.len);
991
992 rthdr = XFRM_RTA_NEXT(rthdr);
993 }
994
995 if (ipcomp != IPCOMP_NONE)
996 {
997 rthdr->rta_type = XFRMA_ALG_COMP;
998 alg_name = lookup_algorithm(compression_algs, ipcomp);
999 if (alg_name == NULL)
1000 {
1001 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1002 ipcomp_transform_names, ipcomp);
1003 return FAILED;
1004 }
1005 DBG2(DBG_KNL, " using compression algorithm %N",
1006 ipcomp_transform_names, ipcomp);
1007
1008 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo));
1009 hdr->nlmsg_len += rthdr->rta_len;
1010 if (hdr->nlmsg_len > sizeof(request))
1011 {
1012 return FAILED;
1013 }
1014
1015 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1016 algo->alg_key_len = 0;
1017 strcpy(algo->alg_name, alg_name);
1018
1019 rthdr = XFRM_RTA_NEXT(rthdr);
1020 }
1021
1022 if (encap)
1023 {
1024 rthdr->rta_type = XFRMA_ENCAP;
1025 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1026
1027 hdr->nlmsg_len += rthdr->rta_len;
1028 if (hdr->nlmsg_len > sizeof(request))
1029 {
1030 return FAILED;
1031 }
1032
1033 struct xfrm_encap_tmpl* tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
1034 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1035 tmpl->encap_sport = htons(src->get_port(src));
1036 tmpl->encap_dport = htons(dst->get_port(dst));
1037 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1038 /* encap_oa could probably be derived from the
1039 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1040 * pluto does the same as we do here but it uses encap_oa in the
1041 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1042 * the kernel ignores it anyway
1043 * -> does that mean that NAT-T encap doesn't work in transport mode?
1044 * No. The reason the kernel ignores NAT-OA is that it recomputes
1045 * (or, rather, just ignores) the checksum. If packets pass
1046 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1047 rthdr = XFRM_RTA_NEXT(rthdr);
1048 }
1049
1050 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1051 {
1052 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x", ntohl(spi));
1053 return FAILED;
1054 }
1055 return SUCCESS;
1056 }
1057
1058 /**
1059 * Get the replay state (i.e. sequence numbers) of an SA.
1060 */
1061 static status_t get_replay_state(private_kernel_netlink_ipsec_t *this,
1062 u_int32_t spi, protocol_id_t protocol, host_t *dst,
1063 struct xfrm_replay_state *replay)
1064 {
1065 unsigned char request[NETLINK_BUFFER_SIZE];
1066 struct nlmsghdr *hdr, *out = NULL;
1067 struct xfrm_aevent_id *out_aevent = NULL, *aevent_id;
1068 size_t len;
1069 struct rtattr *rta;
1070 size_t rtasize;
1071
1072 memset(&request, 0, sizeof(request));
1073
1074 DBG2(DBG_KNL, "querying replay state from SAD entry with SPI %.8x", ntohl(spi));
1075
1076 hdr = (struct nlmsghdr*)request;
1077 hdr->nlmsg_flags = NLM_F_REQUEST;
1078 hdr->nlmsg_type = XFRM_MSG_GETAE;
1079 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1080
1081 aevent_id = (struct xfrm_aevent_id*)NLMSG_DATA(hdr);
1082 aevent_id->flags = XFRM_AE_RVAL;
1083
1084 host2xfrm(dst, &aevent_id->sa_id.daddr);
1085 aevent_id->sa_id.spi = spi;
1086 aevent_id->sa_id.proto = proto_ike2kernel(protocol);
1087 aevent_id->sa_id.family = dst->get_family(dst);
1088
1089 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1090 {
1091 hdr = out;
1092 while (NLMSG_OK(hdr, len))
1093 {
1094 switch (hdr->nlmsg_type)
1095 {
1096 case XFRM_MSG_NEWAE:
1097 {
1098 out_aevent = NLMSG_DATA(hdr);
1099 break;
1100 }
1101 case NLMSG_ERROR:
1102 {
1103 struct nlmsgerr *err = NLMSG_DATA(hdr);
1104 DBG1(DBG_KNL, "querying replay state from SAD entry failed: %s (%d)",
1105 strerror(-err->error), -err->error);
1106 break;
1107 }
1108 default:
1109 hdr = NLMSG_NEXT(hdr, len);
1110 continue;
1111 case NLMSG_DONE:
1112 break;
1113 }
1114 break;
1115 }
1116 }
1117
1118 if (out_aevent == NULL)
1119 {
1120 DBG1(DBG_KNL, "unable to query replay state from SAD entry with SPI %.8x",
1121 ntohl(spi));
1122 free(out);
1123 return FAILED;
1124 }
1125
1126 rta = XFRM_RTA(out, struct xfrm_aevent_id);
1127 rtasize = XFRM_PAYLOAD(out, struct xfrm_aevent_id);
1128 while(RTA_OK(rta, rtasize))
1129 {
1130 if (rta->rta_type == XFRMA_REPLAY_VAL)
1131 {
1132 memcpy(replay, RTA_DATA(rta), rta->rta_len);
1133 free(out);
1134 return SUCCESS;
1135 }
1136 rta = RTA_NEXT(rta, rtasize);
1137 }
1138
1139 DBG1(DBG_KNL, "unable to query replay state from SAD entry with SPI %.8x",
1140 ntohl(spi));
1141 free(out);
1142 return FAILED;
1143 }
1144
1145 /**
1146 * Implementation of kernel_interface_t.update_sa.
1147 */
1148 static status_t update_sa(private_kernel_netlink_ipsec_t *this,
1149 u_int32_t spi, protocol_id_t protocol,
1150 host_t *src, host_t *dst,
1151 host_t *new_src, host_t *new_dst, bool encap)
1152 {
1153 unsigned char request[NETLINK_BUFFER_SIZE], *pos;
1154 struct nlmsghdr *hdr, *out = NULL;
1155 struct xfrm_usersa_id *sa_id;
1156 struct xfrm_usersa_info *out_sa = NULL, *sa;
1157 size_t len;
1158 struct rtattr *rta;
1159 size_t rtasize;
1160 struct xfrm_encap_tmpl* tmpl = NULL;
1161 bool got_replay_state;
1162 struct xfrm_replay_state replay;
1163
1164 memset(&request, 0, sizeof(request));
1165
1166 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x for update", ntohl(spi));
1167
1168 /* query the existing SA first */
1169 hdr = (struct nlmsghdr*)request;
1170 hdr->nlmsg_flags = NLM_F_REQUEST;
1171 hdr->nlmsg_type = XFRM_MSG_GETSA;
1172 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1173
1174 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1175 host2xfrm(dst, &sa_id->daddr);
1176 sa_id->spi = spi;
1177 sa_id->proto = proto_ike2kernel(protocol);
1178 sa_id->family = dst->get_family(dst);
1179
1180 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1181 {
1182 hdr = out;
1183 while (NLMSG_OK(hdr, len))
1184 {
1185 switch (hdr->nlmsg_type)
1186 {
1187 case XFRM_MSG_NEWSA:
1188 {
1189 out_sa = NLMSG_DATA(hdr);
1190 break;
1191 }
1192 case NLMSG_ERROR:
1193 {
1194 struct nlmsgerr *err = NLMSG_DATA(hdr);
1195 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1196 strerror(-err->error), -err->error);
1197 break;
1198 }
1199 default:
1200 hdr = NLMSG_NEXT(hdr, len);
1201 continue;
1202 case NLMSG_DONE:
1203 break;
1204 }
1205 break;
1206 }
1207 }
1208 if (out_sa == NULL)
1209 {
1210 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1211 free(out);
1212 return FAILED;
1213 }
1214
1215 /* try to get the replay state */
1216 got_replay_state = (get_replay_state(
1217 this, spi, protocol, dst, &replay) == SUCCESS);
1218
1219 /* delete the old SA */
1220 if (this->public.interface.del_sa(&this->public.interface, dst, spi, protocol) != SUCCESS)
1221 {
1222 DBG1(DBG_KNL, "unable to delete old SAD entry with SPI %.8x", ntohl(spi));
1223 free(out);
1224 return FAILED;
1225 }
1226
1227 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1228 ntohl(spi), src, dst, new_src, new_dst);
1229
1230 /* copy over the SA from out to request */
1231 hdr = (struct nlmsghdr*)request;
1232 memcpy(hdr, out, min(out->nlmsg_len, sizeof(request)));
1233 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1234 hdr->nlmsg_type = XFRM_MSG_NEWSA;
1235 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1236 sa = NLMSG_DATA(hdr);
1237 sa->family = new_dst->get_family(new_dst);
1238
1239 if (!src->ip_equals(src, new_src))
1240 {
1241 host2xfrm(new_src, &sa->saddr);
1242 }
1243 if (!dst->ip_equals(dst, new_dst))
1244 {
1245 host2xfrm(new_dst, &sa->id.daddr);
1246 }
1247
1248 rta = XFRM_RTA(out, struct xfrm_usersa_info);
1249 rtasize = XFRM_PAYLOAD(out, struct xfrm_usersa_info);
1250 pos = (u_char*)XFRM_RTA(hdr, struct xfrm_usersa_info);
1251 while(RTA_OK(rta, rtasize))
1252 {
1253 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1254 if (rta->rta_type != XFRMA_ENCAP || encap)
1255 {
1256 if (rta->rta_type == XFRMA_ENCAP)
1257 { /* update encap tmpl */
1258 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1259 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1260 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1261 }
1262 memcpy(pos, rta, rta->rta_len);
1263 pos += RTA_ALIGN(rta->rta_len);
1264 hdr->nlmsg_len += RTA_ALIGN(rta->rta_len);
1265 }
1266 rta = RTA_NEXT(rta, rtasize);
1267 }
1268
1269 rta = (struct rtattr*)pos;
1270 if (tmpl == NULL && encap)
1271 { /* add tmpl if we are enabling it */
1272 rta->rta_type = XFRMA_ENCAP;
1273 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1274
1275 hdr->nlmsg_len += rta->rta_len;
1276 if (hdr->nlmsg_len > sizeof(request))
1277 {
1278 return FAILED;
1279 }
1280
1281 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1282 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1283 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1284 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1285 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1286
1287 rta = XFRM_RTA_NEXT(rta);
1288 }
1289
1290 if (got_replay_state)
1291 { /* copy the replay data if available */
1292 rta->rta_type = XFRMA_REPLAY_VAL;
1293 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_replay_state));
1294
1295 hdr->nlmsg_len += rta->rta_len;
1296 if (hdr->nlmsg_len > sizeof(request))
1297 {
1298 return FAILED;
1299 }
1300 memcpy(RTA_DATA(rta), &replay, sizeof(replay));
1301
1302 rta = XFRM_RTA_NEXT(rta);
1303 }
1304
1305 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1306 {
1307 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1308 free(out);
1309 return FAILED;
1310 }
1311 free(out);
1312
1313 return SUCCESS;
1314 }
1315
1316 /**
1317 * Implementation of kernel_interface_t.del_sa.
1318 */
1319 static status_t del_sa(private_kernel_netlink_ipsec_t *this, host_t *dst,
1320 u_int32_t spi, protocol_id_t protocol)
1321 {
1322 unsigned char request[NETLINK_BUFFER_SIZE];
1323 struct nlmsghdr *hdr;
1324 struct xfrm_usersa_id *sa_id;
1325
1326 memset(&request, 0, sizeof(request));
1327
1328 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x", ntohl(spi));
1329
1330 hdr = (struct nlmsghdr*)request;
1331 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1332 hdr->nlmsg_type = XFRM_MSG_DELSA;
1333 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1334
1335 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1336 host2xfrm(dst, &sa_id->daddr);
1337 sa_id->spi = spi;
1338 sa_id->proto = proto_ike2kernel(protocol);
1339 sa_id->family = dst->get_family(dst);
1340
1341 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1342 {
1343 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x", ntohl(spi));
1344 return FAILED;
1345 }
1346 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x", ntohl(spi));
1347 return SUCCESS;
1348 }
1349
1350 /**
1351 * Implementation of kernel_interface_t.add_policy.
1352 */
1353 static status_t add_policy(private_kernel_netlink_ipsec_t *this,
1354 host_t *src, host_t *dst,
1355 traffic_selector_t *src_ts,
1356 traffic_selector_t *dst_ts,
1357 policy_dir_t direction, protocol_id_t protocol,
1358 u_int32_t reqid, bool high_prio, ipsec_mode_t mode,
1359 u_int16_t ipcomp)
1360 {
1361 iterator_t *iterator;
1362 policy_entry_t *current, *policy;
1363 bool found = FALSE;
1364 unsigned char request[NETLINK_BUFFER_SIZE];
1365 struct xfrm_userpolicy_info *policy_info;
1366 struct nlmsghdr *hdr;
1367
1368 /* create a policy */
1369 policy = malloc_thing(policy_entry_t);
1370 memset(policy, 0, sizeof(policy_entry_t));
1371 policy->sel = ts2selector(src_ts, dst_ts);
1372 policy->direction = direction;
1373
1374 /* find the policy, which matches EXACTLY */
1375 pthread_mutex_lock(&this->mutex);
1376 iterator = this->policies->create_iterator(this->policies, TRUE);
1377 while (iterator->iterate(iterator, (void**)&current))
1378 {
1379 if (memeq(&current->sel, &policy->sel, sizeof(struct xfrm_selector)) &&
1380 policy->direction == current->direction)
1381 {
1382 /* use existing policy */
1383 current->refcount++;
1384 DBG2(DBG_KNL, "policy %R === %R %N already exists, increasing "
1385 "refcount", src_ts, dst_ts,
1386 policy_dir_names, direction);
1387 free(policy);
1388 policy = current;
1389 found = TRUE;
1390 break;
1391 }
1392 }
1393 iterator->destroy(iterator);
1394 if (!found)
1395 { /* apply the new one, if we have no such policy */
1396 this->policies->insert_last(this->policies, policy);
1397 policy->refcount = 1;
1398 }
1399
1400 DBG2(DBG_KNL, "adding policy %R === %R %N", src_ts, dst_ts,
1401 policy_dir_names, direction);
1402
1403 memset(&request, 0, sizeof(request));
1404 hdr = (struct nlmsghdr*)request;
1405 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1406 hdr->nlmsg_type = found ? XFRM_MSG_UPDPOLICY : XFRM_MSG_NEWPOLICY;
1407 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
1408
1409 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1410 policy_info->sel = policy->sel;
1411 policy_info->dir = policy->direction;
1412 /* calculate priority based on source selector size, small size = high prio */
1413 policy_info->priority = high_prio ? PRIO_HIGH : PRIO_LOW;
1414 policy_info->priority -= policy->sel.prefixlen_s * 10;
1415 policy_info->priority -= policy->sel.proto ? 2 : 0;
1416 policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
1417 policy_info->action = XFRM_POLICY_ALLOW;
1418 policy_info->share = XFRM_SHARE_ANY;
1419 pthread_mutex_unlock(&this->mutex);
1420
1421 /* policies don't expire */
1422 policy_info->lft.soft_byte_limit = XFRM_INF;
1423 policy_info->lft.soft_packet_limit = XFRM_INF;
1424 policy_info->lft.hard_byte_limit = XFRM_INF;
1425 policy_info->lft.hard_packet_limit = XFRM_INF;
1426 policy_info->lft.soft_add_expires_seconds = 0;
1427 policy_info->lft.hard_add_expires_seconds = 0;
1428 policy_info->lft.soft_use_expires_seconds = 0;
1429 policy_info->lft.hard_use_expires_seconds = 0;
1430
1431 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
1432 rthdr->rta_type = XFRMA_TMPL;
1433 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1434
1435 hdr->nlmsg_len += rthdr->rta_len;
1436 if (hdr->nlmsg_len > sizeof(request))
1437 {
1438 return FAILED;
1439 }
1440
1441 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
1442
1443 if (ipcomp != IPCOMP_NONE)
1444 {
1445 tmpl->reqid = reqid;
1446 tmpl->id.proto = IPPROTO_COMP;
1447 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1448 tmpl->mode = mode;
1449 tmpl->optional = direction != POLICY_OUT;
1450 tmpl->family = src->get_family(src);
1451
1452 host2xfrm(src, &tmpl->saddr);
1453 host2xfrm(dst, &tmpl->id.daddr);
1454
1455 /* add an additional xfrm_user_tmpl */
1456 rthdr->rta_len += RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1457 hdr->nlmsg_len += RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1458 if (hdr->nlmsg_len > sizeof(request))
1459 {
1460 return FAILED;
1461 }
1462
1463 tmpl++;
1464 }
1465
1466 tmpl->reqid = reqid;
1467 tmpl->id.proto = proto_ike2kernel(protocol);
1468 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1469 tmpl->mode = mode;
1470 tmpl->family = src->get_family(src);
1471
1472 host2xfrm(src, &tmpl->saddr);
1473 host2xfrm(dst, &tmpl->id.daddr);
1474
1475 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1476 {
1477 DBG1(DBG_KNL, "unable to add policy %R === %R %N", src_ts, dst_ts,
1478 policy_dir_names, direction);
1479 return FAILED;
1480 }
1481
1482 /* install a route, if:
1483 * - we are NOT updating a policy
1484 * - this is a forward policy (to just get one for each child)
1485 * - we are in tunnel mode
1486 * - we are not using IPv6 (does not work correctly yet!)
1487 * - routing is not disabled via strongswan.conf
1488 */
1489 if (policy->route == NULL && direction == POLICY_FWD &&
1490 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6 &&
1491 this->install_routes)
1492 {
1493 route_entry_t *route = malloc_thing(route_entry_t);
1494
1495 if (charon->kernel_interface->get_address_by_ts(charon->kernel_interface,
1496 dst_ts, &route->src_ip) == SUCCESS)
1497 {
1498 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1499 route->gateway = charon->kernel_interface->get_nexthop(
1500 charon->kernel_interface, src);
1501 route->if_name = charon->kernel_interface->get_interface(
1502 charon->kernel_interface, dst);
1503 route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
1504 memcpy(route->dst_net.ptr, &policy->sel.saddr, route->dst_net.len);
1505 route->prefixlen = policy->sel.prefixlen_s;
1506
1507 switch (charon->kernel_interface->add_route(charon->kernel_interface,
1508 route->dst_net, route->prefixlen, route->gateway,
1509 route->src_ip, route->if_name))
1510 {
1511 default:
1512 DBG1(DBG_KNL, "unable to install source route for %H",
1513 route->src_ip);
1514 /* FALL */
1515 case ALREADY_DONE:
1516 /* route exists, do not uninstall */
1517 route_entry_destroy(route);
1518 break;
1519 case SUCCESS:
1520 /* cache the installed route */
1521 policy->route = route;
1522 break;
1523 }
1524 }
1525 else
1526 {
1527 free(route);
1528 }
1529 }
1530
1531 return SUCCESS;
1532 }
1533
1534 /**
1535 * Implementation of kernel_interface_t.query_policy.
1536 */
1537 static status_t query_policy(private_kernel_netlink_ipsec_t *this,
1538 traffic_selector_t *src_ts,
1539 traffic_selector_t *dst_ts,
1540 policy_dir_t direction, u_int32_t *use_time)
1541 {
1542 unsigned char request[NETLINK_BUFFER_SIZE];
1543 struct nlmsghdr *out = NULL, *hdr;
1544 struct xfrm_userpolicy_id *policy_id;
1545 struct xfrm_userpolicy_info *policy = NULL;
1546 size_t len;
1547
1548 memset(&request, 0, sizeof(request));
1549
1550 DBG2(DBG_KNL, "querying policy %R === %R %N", src_ts, dst_ts,
1551 policy_dir_names, direction);
1552
1553 hdr = (struct nlmsghdr*)request;
1554 hdr->nlmsg_flags = NLM_F_REQUEST;
1555 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
1556 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1557
1558 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1559 policy_id->sel = ts2selector(src_ts, dst_ts);
1560 policy_id->dir = direction;
1561
1562 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1563 {
1564 hdr = out;
1565 while (NLMSG_OK(hdr, len))
1566 {
1567 switch (hdr->nlmsg_type)
1568 {
1569 case XFRM_MSG_NEWPOLICY:
1570 {
1571 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1572 break;
1573 }
1574 case NLMSG_ERROR:
1575 {
1576 struct nlmsgerr *err = NLMSG_DATA(hdr);
1577 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
1578 strerror(-err->error), -err->error);
1579 break;
1580 }
1581 default:
1582 hdr = NLMSG_NEXT(hdr, len);
1583 continue;
1584 case NLMSG_DONE:
1585 break;
1586 }
1587 break;
1588 }
1589 }
1590
1591 if (policy == NULL)
1592 {
1593 DBG2(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
1594 policy_dir_names, direction);
1595 free(out);
1596 return FAILED;
1597 }
1598 *use_time = (time_t)policy->curlft.use_time;
1599
1600 free(out);
1601 return SUCCESS;
1602 }
1603
1604 /**
1605 * Implementation of kernel_interface_t.del_policy.
1606 */
1607 static status_t del_policy(private_kernel_netlink_ipsec_t *this,
1608 traffic_selector_t *src_ts,
1609 traffic_selector_t *dst_ts,
1610 policy_dir_t direction)
1611 {
1612 policy_entry_t *current, policy, *to_delete = NULL;
1613 route_entry_t *route;
1614 unsigned char request[NETLINK_BUFFER_SIZE];
1615 struct nlmsghdr *hdr;
1616 struct xfrm_userpolicy_id *policy_id;
1617 iterator_t *iterator;
1618
1619 DBG2(DBG_KNL, "deleting policy %R === %R %N", src_ts, dst_ts,
1620 policy_dir_names, direction);
1621
1622 /* create a policy */
1623 memset(&policy, 0, sizeof(policy_entry_t));
1624 policy.sel = ts2selector(src_ts, dst_ts);
1625 policy.direction = direction;
1626
1627 /* find the policy */
1628 iterator = this->policies->create_iterator_locked(this->policies, &this->mutex);
1629 while (iterator->iterate(iterator, (void**)&current))
1630 {
1631 if (memeq(&current->sel, &policy.sel, sizeof(struct xfrm_selector)) &&
1632 policy.direction == current->direction)
1633 {
1634 to_delete = current;
1635 if (--to_delete->refcount > 0)
1636 {
1637 /* is used by more SAs, keep in kernel */
1638 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
1639 iterator->destroy(iterator);
1640 return SUCCESS;
1641 }
1642 /* remove if last reference */
1643 iterator->remove(iterator);
1644 break;
1645 }
1646 }
1647 iterator->destroy(iterator);
1648 if (!to_delete)
1649 {
1650 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
1651 dst_ts, policy_dir_names, direction);
1652 return NOT_FOUND;
1653 }
1654
1655 memset(&request, 0, sizeof(request));
1656
1657 hdr = (struct nlmsghdr*)request;
1658 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1659 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
1660 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1661
1662 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1663 policy_id->sel = to_delete->sel;
1664 policy_id->dir = direction;
1665
1666 route = to_delete->route;
1667 free(to_delete);
1668
1669 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1670 {
1671 DBG1(DBG_KNL, "unable to delete policy %R === %R %N", src_ts, dst_ts,
1672 policy_dir_names, direction);
1673 return FAILED;
1674 }
1675
1676 if (route)
1677 {
1678 if (charon->kernel_interface->del_route(charon->kernel_interface,
1679 route->dst_net, route->prefixlen, route->gateway,
1680 route->src_ip, route->if_name) != SUCCESS)
1681 {
1682 DBG1(DBG_KNL, "error uninstalling route installed with "
1683 "policy %R === %R %N", src_ts, dst_ts,
1684 policy_dir_names, direction);
1685 }
1686 route_entry_destroy(route);
1687 }
1688 return SUCCESS;
1689 }
1690
1691 /**
1692 * Implementation of kernel_interface_t.destroy.
1693 */
1694 static void destroy(private_kernel_netlink_ipsec_t *this)
1695 {
1696 this->job->cancel(this->job);
1697 close(this->socket_xfrm_events);
1698 this->socket_xfrm->destroy(this->socket_xfrm);
1699 this->policies->destroy(this->policies);
1700 free(this);
1701 }
1702
1703 /*
1704 * Described in header.
1705 */
1706 kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
1707 {
1708 private_kernel_netlink_ipsec_t *this = malloc_thing(private_kernel_netlink_ipsec_t);
1709 struct sockaddr_nl addr;
1710
1711 /* public functions */
1712 this->public.interface.get_spi = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
1713 this->public.interface.get_cpi = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,u_int32_t,u_int16_t*))get_cpi;
1714 this->public.interface.add_sa = (status_t(*)(kernel_ipsec_t *,host_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t,u_int64_t,u_int64_t,u_int16_t,chunk_t,u_int16_t,chunk_t,ipsec_mode_t,u_int16_t,bool,bool))add_sa;
1715 this->public.interface.update_sa = (status_t(*)(kernel_ipsec_t*,u_int32_t,protocol_id_t,host_t*,host_t*,host_t*,host_t*,bool))update_sa;
1716 this->public.interface.del_sa = (status_t(*)(kernel_ipsec_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
1717 this->public.interface.add_policy = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,protocol_id_t,u_int32_t,bool,ipsec_mode_t,u_int16_t))add_policy;
1718 this->public.interface.query_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
1719 this->public.interface.del_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t))del_policy;
1720 this->public.interface.destroy = (void(*)(kernel_ipsec_t*)) destroy;
1721
1722 /* private members */
1723 this->policies = linked_list_create();
1724 pthread_mutex_init(&this->mutex, NULL);
1725 this->install_routes = lib->settings->get_bool(lib->settings,
1726 "charon.install_routes", TRUE);
1727
1728 this->socket_xfrm = netlink_socket_create(NETLINK_XFRM);
1729
1730 memset(&addr, 0, sizeof(addr));
1731 addr.nl_family = AF_NETLINK;
1732
1733 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
1734 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1735 if (this->socket_xfrm_events <= 0)
1736 {
1737 charon->kill(charon, "unable to create XFRM event socket");
1738 }
1739 addr.nl_groups = XFRMNLGRP(ACQUIRE) | XFRMNLGRP(EXPIRE) |
1740 XFRMNLGRP(MIGRATE) | XFRMNLGRP(MAPPING);
1741 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
1742 {
1743 charon->kill(charon, "unable to bind XFRM event socket");
1744 }
1745
1746 this->job = callback_job_create((callback_job_cb_t)receive_events,
1747 this, NULL, NULL);
1748 charon->processor->queue_job(charon->processor, (job_t*)this->job);
1749
1750 return &this->public;
1751 }