added support for xfrm remote kmaddress
[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 }
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 with reqid {%u}",
616 src_ts, dst_ts, policy_dir_names, dir, reqid, local);
617 job = (job_t*)migrate_job_create(reqid, src_ts, dst_ts, dir,
618 local, remote);
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 DESTROY_IF(remote);
627 }
628 }
629
630 /**
631 * process a XFRM_MSG_MAPPING from kernel
632 */
633 static void process_mapping(private_kernel_netlink_ipsec_t *this,
634 struct nlmsghdr *hdr)
635 {
636 job_t *job;
637 u_int32_t spi, reqid;
638 struct xfrm_user_mapping *mapping;
639 host_t *host;
640
641 mapping = (struct xfrm_user_mapping*)NLMSG_DATA(hdr);
642 spi = mapping->id.spi;
643 reqid = mapping->reqid;
644
645 DBG2(DBG_KNL, "received a XFRM_MSG_MAPPING");
646
647 if (proto_kernel2ike(mapping->id.proto) == PROTO_ESP)
648 {
649 host = xfrm2host(mapping->id.family, &mapping->new_saddr,
650 mapping->new_sport);
651 if (host)
652 {
653 DBG1(DBG_KNL, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
654 "reqid {%u} changed, queuing update job", ntohl(spi), reqid);
655 job = (job_t*)update_sa_job_create(reqid, host);
656 charon->processor->queue_job(charon->processor, job);
657 }
658 }
659 }
660
661 /**
662 * Receives events from kernel
663 */
664 static job_requeue_t receive_events(private_kernel_netlink_ipsec_t *this)
665 {
666 char response[1024];
667 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
668 struct sockaddr_nl addr;
669 socklen_t addr_len = sizeof(addr);
670 int len, oldstate;
671
672 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
673 len = recvfrom(this->socket_xfrm_events, response, sizeof(response), 0,
674 (struct sockaddr*)&addr, &addr_len);
675 pthread_setcancelstate(oldstate, NULL);
676
677 if (len < 0)
678 {
679 switch (errno)
680 {
681 case EINTR:
682 /* interrupted, try again */
683 return JOB_REQUEUE_DIRECT;
684 case EAGAIN:
685 /* no data ready, select again */
686 return JOB_REQUEUE_DIRECT;
687 default:
688 DBG1(DBG_KNL, "unable to receive from xfrm event socket");
689 sleep(1);
690 return JOB_REQUEUE_FAIR;
691 }
692 }
693
694 if (addr.nl_pid != 0)
695 { /* not from kernel. not interested, try another one */
696 return JOB_REQUEUE_DIRECT;
697 }
698
699 while (NLMSG_OK(hdr, len))
700 {
701 switch (hdr->nlmsg_type)
702 {
703 case XFRM_MSG_ACQUIRE:
704 process_acquire(this, hdr);
705 break;
706 case XFRM_MSG_EXPIRE:
707 process_expire(this, hdr);
708 break;
709 case XFRM_MSG_MIGRATE:
710 process_migrate(this, hdr);
711 break;
712 case XFRM_MSG_MAPPING:
713 process_mapping(this, hdr);
714 break;
715 default:
716 break;
717 }
718 hdr = NLMSG_NEXT(hdr, len);
719 }
720 return JOB_REQUEUE_DIRECT;
721 }
722
723 /**
724 * Get an SPI for a specific protocol from the kernel.
725 */
726 static status_t get_spi_internal(private_kernel_netlink_ipsec_t *this,
727 host_t *src, host_t *dst, u_int8_t proto, u_int32_t min, u_int32_t max,
728 u_int32_t reqid, u_int32_t *spi)
729 {
730 unsigned char request[NETLINK_BUFFER_SIZE];
731 struct nlmsghdr *hdr, *out;
732 struct xfrm_userspi_info *userspi;
733 u_int32_t received_spi = 0;
734 size_t len;
735
736 memset(&request, 0, sizeof(request));
737
738 hdr = (struct nlmsghdr*)request;
739 hdr->nlmsg_flags = NLM_F_REQUEST;
740 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
741 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
742
743 userspi = (struct xfrm_userspi_info*)NLMSG_DATA(hdr);
744 host2xfrm(src, &userspi->info.saddr);
745 host2xfrm(dst, &userspi->info.id.daddr);
746 userspi->info.id.proto = proto;
747 userspi->info.mode = TRUE; /* tunnel mode */
748 userspi->info.reqid = reqid;
749 userspi->info.family = src->get_family(src);
750 userspi->min = min;
751 userspi->max = max;
752
753 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
754 {
755 hdr = out;
756 while (NLMSG_OK(hdr, len))
757 {
758 switch (hdr->nlmsg_type)
759 {
760 case XFRM_MSG_NEWSA:
761 {
762 struct xfrm_usersa_info* usersa = NLMSG_DATA(hdr);
763 received_spi = usersa->id.spi;
764 break;
765 }
766 case NLMSG_ERROR:
767 {
768 struct nlmsgerr *err = NLMSG_DATA(hdr);
769
770 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
771 strerror(-err->error), -err->error);
772 break;
773 }
774 default:
775 hdr = NLMSG_NEXT(hdr, len);
776 continue;
777 case NLMSG_DONE:
778 break;
779 }
780 break;
781 }
782 free(out);
783 }
784
785 if (received_spi == 0)
786 {
787 return FAILED;
788 }
789
790 *spi = received_spi;
791 return SUCCESS;
792 }
793
794 /**
795 * Implementation of kernel_interface_t.get_spi.
796 */
797 static status_t get_spi(private_kernel_netlink_ipsec_t *this,
798 host_t *src, host_t *dst,
799 protocol_id_t protocol, u_int32_t reqid,
800 u_int32_t *spi)
801 {
802 DBG2(DBG_KNL, "getting SPI for reqid {%u}", reqid);
803
804 if (get_spi_internal(this, src, dst, proto_ike2kernel(protocol),
805 0xc0000000, 0xcFFFFFFF, reqid, spi) != SUCCESS)
806 {
807 DBG1(DBG_KNL, "unable to get SPI for reqid {%u}", reqid);
808 return FAILED;
809 }
810
811 DBG2(DBG_KNL, "got SPI %.8x for reqid {%u}", ntohl(*spi), reqid);
812
813 return SUCCESS;
814 }
815
816 /**
817 * Implementation of kernel_interface_t.get_cpi.
818 */
819 static status_t get_cpi(private_kernel_netlink_ipsec_t *this,
820 host_t *src, host_t *dst,
821 u_int32_t reqid, u_int16_t *cpi)
822 {
823 u_int32_t received_spi = 0;
824
825 DBG2(DBG_KNL, "getting CPI for reqid {%u}", reqid);
826
827 if (get_spi_internal(this, src, dst,
828 IPPROTO_COMP, 0x100, 0xEFFF, reqid, &received_spi) != SUCCESS)
829 {
830 DBG1(DBG_KNL, "unable to get CPI for reqid {%u}", reqid);
831 return FAILED;
832 }
833
834 *cpi = htons((u_int16_t)ntohl(received_spi));
835
836 DBG2(DBG_KNL, "got CPI %.4x for reqid {%u}", ntohs(*cpi), reqid);
837
838 return SUCCESS;
839 }
840
841 /**
842 * Implementation of kernel_interface_t.add_sa.
843 */
844 static status_t add_sa(private_kernel_netlink_ipsec_t *this,
845 host_t *src, host_t *dst, u_int32_t spi,
846 protocol_id_t protocol, u_int32_t reqid,
847 u_int64_t expire_soft, u_int64_t expire_hard,
848 u_int16_t enc_alg, chunk_t enc_key,
849 u_int16_t int_alg, chunk_t int_key,
850 ipsec_mode_t mode, u_int16_t ipcomp, bool encap,
851 bool replace)
852 {
853 unsigned char request[NETLINK_BUFFER_SIZE];
854 char *alg_name;
855 struct nlmsghdr *hdr;
856 struct xfrm_usersa_info *sa;
857 u_int16_t icv_size = 64;
858
859 memset(&request, 0, sizeof(request));
860
861 DBG2(DBG_KNL, "adding SAD entry with SPI %.8x and reqid {%u}",
862 ntohl(spi), reqid);
863
864 hdr = (struct nlmsghdr*)request;
865 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
866 hdr->nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
867 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
868
869 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
870 host2xfrm(src, &sa->saddr);
871 host2xfrm(dst, &sa->id.daddr);
872 sa->id.spi = spi;
873 sa->id.proto = proto_ike2kernel(protocol);
874 sa->family = src->get_family(src);
875 sa->mode = mode;
876 if (mode == MODE_TUNNEL)
877 {
878 sa->flags |= XFRM_STATE_AF_UNSPEC;
879 }
880 sa->replay_window = (protocol == IPPROTO_COMP) ? 0 : 32;
881 sa->reqid = reqid;
882 /* we currently do not expire SAs by volume/packet count */
883 sa->lft.soft_byte_limit = XFRM_INF;
884 sa->lft.hard_byte_limit = XFRM_INF;
885 sa->lft.soft_packet_limit = XFRM_INF;
886 sa->lft.hard_packet_limit = XFRM_INF;
887 /* we use lifetimes since added, not since used */
888 sa->lft.soft_add_expires_seconds = expire_soft;
889 sa->lft.hard_add_expires_seconds = expire_hard;
890 sa->lft.soft_use_expires_seconds = 0;
891 sa->lft.hard_use_expires_seconds = 0;
892
893 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_usersa_info);
894
895 switch (enc_alg)
896 {
897 case ENCR_UNDEFINED:
898 /* no encryption */
899 break;
900 case ENCR_AES_CCM_ICV16:
901 case ENCR_AES_GCM_ICV16:
902 icv_size += 32;
903 /* FALL */
904 case ENCR_AES_CCM_ICV12:
905 case ENCR_AES_GCM_ICV12:
906 icv_size += 32;
907 /* FALL */
908 case ENCR_AES_CCM_ICV8:
909 case ENCR_AES_GCM_ICV8:
910 {
911 rthdr->rta_type = XFRMA_ALG_AEAD;
912 alg_name = lookup_algorithm(encryption_algs, enc_alg);
913 if (alg_name == NULL)
914 {
915 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
916 encryption_algorithm_names, enc_alg);
917 return FAILED;
918 }
919 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
920 encryption_algorithm_names, enc_alg, enc_key.len * 8);
921
922 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo_aead) + enc_key.len);
923 hdr->nlmsg_len += rthdr->rta_len;
924 if (hdr->nlmsg_len > sizeof(request))
925 {
926 return FAILED;
927 }
928
929 struct xfrm_algo_aead* algo = (struct xfrm_algo_aead*)RTA_DATA(rthdr);
930 algo->alg_key_len = enc_key.len * 8;
931 algo->alg_icv_len = icv_size;
932 strcpy(algo->alg_name, alg_name);
933 memcpy(algo->alg_key, enc_key.ptr, enc_key.len);
934
935 rthdr = XFRM_RTA_NEXT(rthdr);
936 break;
937 }
938 default:
939 {
940 rthdr->rta_type = XFRMA_ALG_CRYPT;
941 alg_name = lookup_algorithm(encryption_algs, enc_alg);
942 if (alg_name == NULL)
943 {
944 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
945 encryption_algorithm_names, enc_alg);
946 return FAILED;
947 }
948 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
949 encryption_algorithm_names, enc_alg, enc_key.len * 8);
950
951 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + enc_key.len);
952 hdr->nlmsg_len += rthdr->rta_len;
953 if (hdr->nlmsg_len > sizeof(request))
954 {
955 return FAILED;
956 }
957
958 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
959 algo->alg_key_len = enc_key.len * 8;
960 strcpy(algo->alg_name, alg_name);
961 memcpy(algo->alg_key, enc_key.ptr, enc_key.len);
962
963 rthdr = XFRM_RTA_NEXT(rthdr);
964 break;
965 }
966 }
967
968 if (int_alg != AUTH_UNDEFINED)
969 {
970 rthdr->rta_type = XFRMA_ALG_AUTH;
971 alg_name = lookup_algorithm(integrity_algs, int_alg);
972 if (alg_name == NULL)
973 {
974 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
975 integrity_algorithm_names, int_alg);
976 return FAILED;
977 }
978 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
979 integrity_algorithm_names, int_alg, int_key.len * 8);
980
981 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + int_key.len);
982 hdr->nlmsg_len += rthdr->rta_len;
983 if (hdr->nlmsg_len > sizeof(request))
984 {
985 return FAILED;
986 }
987
988 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
989 algo->alg_key_len = int_key.len * 8;
990 strcpy(algo->alg_name, alg_name);
991 memcpy(algo->alg_key, int_key.ptr, int_key.len);
992
993 rthdr = XFRM_RTA_NEXT(rthdr);
994 }
995
996 if (ipcomp != IPCOMP_NONE)
997 {
998 rthdr->rta_type = XFRMA_ALG_COMP;
999 alg_name = lookup_algorithm(compression_algs, ipcomp);
1000 if (alg_name == NULL)
1001 {
1002 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1003 ipcomp_transform_names, ipcomp);
1004 return FAILED;
1005 }
1006 DBG2(DBG_KNL, " using compression algorithm %N",
1007 ipcomp_transform_names, ipcomp);
1008
1009 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo));
1010 hdr->nlmsg_len += rthdr->rta_len;
1011 if (hdr->nlmsg_len > sizeof(request))
1012 {
1013 return FAILED;
1014 }
1015
1016 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1017 algo->alg_key_len = 0;
1018 strcpy(algo->alg_name, alg_name);
1019
1020 rthdr = XFRM_RTA_NEXT(rthdr);
1021 }
1022
1023 if (encap)
1024 {
1025 rthdr->rta_type = XFRMA_ENCAP;
1026 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1027
1028 hdr->nlmsg_len += rthdr->rta_len;
1029 if (hdr->nlmsg_len > sizeof(request))
1030 {
1031 return FAILED;
1032 }
1033
1034 struct xfrm_encap_tmpl* tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
1035 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1036 tmpl->encap_sport = htons(src->get_port(src));
1037 tmpl->encap_dport = htons(dst->get_port(dst));
1038 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1039 /* encap_oa could probably be derived from the
1040 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1041 * pluto does the same as we do here but it uses encap_oa in the
1042 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1043 * the kernel ignores it anyway
1044 * -> does that mean that NAT-T encap doesn't work in transport mode?
1045 * No. The reason the kernel ignores NAT-OA is that it recomputes
1046 * (or, rather, just ignores) the checksum. If packets pass
1047 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1048 rthdr = XFRM_RTA_NEXT(rthdr);
1049 }
1050
1051 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1052 {
1053 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x", ntohl(spi));
1054 return FAILED;
1055 }
1056 return SUCCESS;
1057 }
1058
1059 /**
1060 * Get the replay state (i.e. sequence numbers) of an SA.
1061 */
1062 static status_t get_replay_state(private_kernel_netlink_ipsec_t *this,
1063 u_int32_t spi, protocol_id_t protocol, host_t *dst,
1064 struct xfrm_replay_state *replay)
1065 {
1066 unsigned char request[NETLINK_BUFFER_SIZE];
1067 struct nlmsghdr *hdr, *out = NULL;
1068 struct xfrm_aevent_id *out_aevent = NULL, *aevent_id;
1069 size_t len;
1070 struct rtattr *rta;
1071 size_t rtasize;
1072
1073 memset(&request, 0, sizeof(request));
1074
1075 DBG2(DBG_KNL, "querying replay state from SAD entry with SPI %.8x", ntohl(spi));
1076
1077 hdr = (struct nlmsghdr*)request;
1078 hdr->nlmsg_flags = NLM_F_REQUEST;
1079 hdr->nlmsg_type = XFRM_MSG_GETAE;
1080 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1081
1082 aevent_id = (struct xfrm_aevent_id*)NLMSG_DATA(hdr);
1083 aevent_id->flags = XFRM_AE_RVAL;
1084
1085 host2xfrm(dst, &aevent_id->sa_id.daddr);
1086 aevent_id->sa_id.spi = spi;
1087 aevent_id->sa_id.proto = proto_ike2kernel(protocol);
1088 aevent_id->sa_id.family = dst->get_family(dst);
1089
1090 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1091 {
1092 hdr = out;
1093 while (NLMSG_OK(hdr, len))
1094 {
1095 switch (hdr->nlmsg_type)
1096 {
1097 case XFRM_MSG_NEWAE:
1098 {
1099 out_aevent = NLMSG_DATA(hdr);
1100 break;
1101 }
1102 case NLMSG_ERROR:
1103 {
1104 struct nlmsgerr *err = NLMSG_DATA(hdr);
1105 DBG1(DBG_KNL, "querying replay state from SAD entry failed: %s (%d)",
1106 strerror(-err->error), -err->error);
1107 break;
1108 }
1109 default:
1110 hdr = NLMSG_NEXT(hdr, len);
1111 continue;
1112 case NLMSG_DONE:
1113 break;
1114 }
1115 break;
1116 }
1117 }
1118
1119 if (out_aevent == NULL)
1120 {
1121 DBG1(DBG_KNL, "unable to query replay state from SAD entry with SPI %.8x",
1122 ntohl(spi));
1123 free(out);
1124 return FAILED;
1125 }
1126
1127 rta = XFRM_RTA(out, struct xfrm_aevent_id);
1128 rtasize = XFRM_PAYLOAD(out, struct xfrm_aevent_id);
1129 while(RTA_OK(rta, rtasize))
1130 {
1131 if (rta->rta_type == XFRMA_REPLAY_VAL)
1132 {
1133 memcpy(replay, RTA_DATA(rta), rta->rta_len);
1134 free(out);
1135 return SUCCESS;
1136 }
1137 rta = RTA_NEXT(rta, rtasize);
1138 }
1139
1140 DBG1(DBG_KNL, "unable to query replay state from SAD entry with SPI %.8x",
1141 ntohl(spi));
1142 free(out);
1143 return FAILED;
1144 }
1145
1146 /**
1147 * Implementation of kernel_interface_t.update_sa.
1148 */
1149 static status_t update_sa(private_kernel_netlink_ipsec_t *this,
1150 u_int32_t spi, protocol_id_t protocol,
1151 host_t *src, host_t *dst,
1152 host_t *new_src, host_t *new_dst, bool encap)
1153 {
1154 unsigned char request[NETLINK_BUFFER_SIZE], *pos;
1155 struct nlmsghdr *hdr, *out = NULL;
1156 struct xfrm_usersa_id *sa_id;
1157 struct xfrm_usersa_info *out_sa = NULL, *sa;
1158 size_t len;
1159 struct rtattr *rta;
1160 size_t rtasize;
1161 struct xfrm_encap_tmpl* tmpl = NULL;
1162 bool got_replay_state;
1163 struct xfrm_replay_state replay;
1164
1165 memset(&request, 0, sizeof(request));
1166
1167 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x for update", ntohl(spi));
1168
1169 /* query the existing SA first */
1170 hdr = (struct nlmsghdr*)request;
1171 hdr->nlmsg_flags = NLM_F_REQUEST;
1172 hdr->nlmsg_type = XFRM_MSG_GETSA;
1173 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1174
1175 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1176 host2xfrm(dst, &sa_id->daddr);
1177 sa_id->spi = spi;
1178 sa_id->proto = proto_ike2kernel(protocol);
1179 sa_id->family = dst->get_family(dst);
1180
1181 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1182 {
1183 hdr = out;
1184 while (NLMSG_OK(hdr, len))
1185 {
1186 switch (hdr->nlmsg_type)
1187 {
1188 case XFRM_MSG_NEWSA:
1189 {
1190 out_sa = NLMSG_DATA(hdr);
1191 break;
1192 }
1193 case NLMSG_ERROR:
1194 {
1195 struct nlmsgerr *err = NLMSG_DATA(hdr);
1196 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1197 strerror(-err->error), -err->error);
1198 break;
1199 }
1200 default:
1201 hdr = NLMSG_NEXT(hdr, len);
1202 continue;
1203 case NLMSG_DONE:
1204 break;
1205 }
1206 break;
1207 }
1208 }
1209 if (out_sa == NULL)
1210 {
1211 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1212 free(out);
1213 return FAILED;
1214 }
1215
1216 /* try to get the replay state */
1217 got_replay_state = (get_replay_state(
1218 this, spi, protocol, dst, &replay) == SUCCESS);
1219
1220 /* delete the old SA */
1221 if (this->public.interface.del_sa(&this->public.interface, dst, spi, protocol) != SUCCESS)
1222 {
1223 DBG1(DBG_KNL, "unable to delete old SAD entry with SPI %.8x", ntohl(spi));
1224 free(out);
1225 return FAILED;
1226 }
1227
1228 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1229 ntohl(spi), src, dst, new_src, new_dst);
1230
1231 /* copy over the SA from out to request */
1232 hdr = (struct nlmsghdr*)request;
1233 memcpy(hdr, out, min(out->nlmsg_len, sizeof(request)));
1234 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1235 hdr->nlmsg_type = XFRM_MSG_NEWSA;
1236 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1237 sa = NLMSG_DATA(hdr);
1238 sa->family = new_dst->get_family(new_dst);
1239
1240 if (!src->ip_equals(src, new_src))
1241 {
1242 host2xfrm(new_src, &sa->saddr);
1243 }
1244 if (!dst->ip_equals(dst, new_dst))
1245 {
1246 host2xfrm(new_dst, &sa->id.daddr);
1247 }
1248
1249 rta = XFRM_RTA(out, struct xfrm_usersa_info);
1250 rtasize = XFRM_PAYLOAD(out, struct xfrm_usersa_info);
1251 pos = (u_char*)XFRM_RTA(hdr, struct xfrm_usersa_info);
1252 while(RTA_OK(rta, rtasize))
1253 {
1254 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1255 if (rta->rta_type != XFRMA_ENCAP || encap)
1256 {
1257 if (rta->rta_type == XFRMA_ENCAP)
1258 { /* update encap tmpl */
1259 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1260 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1261 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1262 }
1263 memcpy(pos, rta, rta->rta_len);
1264 pos += RTA_ALIGN(rta->rta_len);
1265 hdr->nlmsg_len += RTA_ALIGN(rta->rta_len);
1266 }
1267 rta = RTA_NEXT(rta, rtasize);
1268 }
1269
1270 rta = (struct rtattr*)pos;
1271 if (tmpl == NULL && encap)
1272 { /* add tmpl if we are enabling it */
1273 rta->rta_type = XFRMA_ENCAP;
1274 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1275
1276 hdr->nlmsg_len += rta->rta_len;
1277 if (hdr->nlmsg_len > sizeof(request))
1278 {
1279 return FAILED;
1280 }
1281
1282 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1283 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1284 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1285 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1286 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1287
1288 rta = XFRM_RTA_NEXT(rta);
1289 }
1290
1291 if (got_replay_state)
1292 { /* copy the replay data if available */
1293 rta->rta_type = XFRMA_REPLAY_VAL;
1294 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_replay_state));
1295
1296 hdr->nlmsg_len += rta->rta_len;
1297 if (hdr->nlmsg_len > sizeof(request))
1298 {
1299 return FAILED;
1300 }
1301 memcpy(RTA_DATA(rta), &replay, sizeof(replay));
1302
1303 rta = XFRM_RTA_NEXT(rta);
1304 }
1305
1306 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1307 {
1308 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1309 free(out);
1310 return FAILED;
1311 }
1312 free(out);
1313
1314 return SUCCESS;
1315 }
1316
1317 /**
1318 * Implementation of kernel_interface_t.del_sa.
1319 */
1320 static status_t del_sa(private_kernel_netlink_ipsec_t *this, host_t *dst,
1321 u_int32_t spi, protocol_id_t protocol)
1322 {
1323 unsigned char request[NETLINK_BUFFER_SIZE];
1324 struct nlmsghdr *hdr;
1325 struct xfrm_usersa_id *sa_id;
1326
1327 memset(&request, 0, sizeof(request));
1328
1329 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x", ntohl(spi));
1330
1331 hdr = (struct nlmsghdr*)request;
1332 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1333 hdr->nlmsg_type = XFRM_MSG_DELSA;
1334 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1335
1336 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1337 host2xfrm(dst, &sa_id->daddr);
1338 sa_id->spi = spi;
1339 sa_id->proto = proto_ike2kernel(protocol);
1340 sa_id->family = dst->get_family(dst);
1341
1342 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1343 {
1344 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x", ntohl(spi));
1345 return FAILED;
1346 }
1347 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x", ntohl(spi));
1348 return SUCCESS;
1349 }
1350
1351 /**
1352 * Implementation of kernel_interface_t.add_policy.
1353 */
1354 static status_t add_policy(private_kernel_netlink_ipsec_t *this,
1355 host_t *src, host_t *dst,
1356 traffic_selector_t *src_ts,
1357 traffic_selector_t *dst_ts,
1358 policy_dir_t direction, protocol_id_t protocol,
1359 u_int32_t reqid, bool high_prio, ipsec_mode_t mode,
1360 u_int16_t ipcomp)
1361 {
1362 iterator_t *iterator;
1363 policy_entry_t *current, *policy;
1364 bool found = FALSE;
1365 unsigned char request[NETLINK_BUFFER_SIZE];
1366 struct xfrm_userpolicy_info *policy_info;
1367 struct nlmsghdr *hdr;
1368
1369 /* create a policy */
1370 policy = malloc_thing(policy_entry_t);
1371 memset(policy, 0, sizeof(policy_entry_t));
1372 policy->sel = ts2selector(src_ts, dst_ts);
1373 policy->direction = direction;
1374
1375 /* find the policy, which matches EXACTLY */
1376 pthread_mutex_lock(&this->mutex);
1377 iterator = this->policies->create_iterator(this->policies, TRUE);
1378 while (iterator->iterate(iterator, (void**)&current))
1379 {
1380 if (memeq(&current->sel, &policy->sel, sizeof(struct xfrm_selector)) &&
1381 policy->direction == current->direction)
1382 {
1383 /* use existing policy */
1384 current->refcount++;
1385 DBG2(DBG_KNL, "policy %R === %R %N already exists, increasing "
1386 "refcount", src_ts, dst_ts,
1387 policy_dir_names, direction);
1388 free(policy);
1389 policy = current;
1390 found = TRUE;
1391 break;
1392 }
1393 }
1394 iterator->destroy(iterator);
1395 if (!found)
1396 { /* apply the new one, if we have no such policy */
1397 this->policies->insert_last(this->policies, policy);
1398 policy->refcount = 1;
1399 }
1400
1401 DBG2(DBG_KNL, "adding policy %R === %R %N", src_ts, dst_ts,
1402 policy_dir_names, direction);
1403
1404 memset(&request, 0, sizeof(request));
1405 hdr = (struct nlmsghdr*)request;
1406 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1407 hdr->nlmsg_type = found ? XFRM_MSG_UPDPOLICY : XFRM_MSG_NEWPOLICY;
1408 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
1409
1410 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1411 policy_info->sel = policy->sel;
1412 policy_info->dir = policy->direction;
1413 /* calculate priority based on source selector size, small size = high prio */
1414 policy_info->priority = high_prio ? PRIO_HIGH : PRIO_LOW;
1415 policy_info->priority -= policy->sel.prefixlen_s * 10;
1416 policy_info->priority -= policy->sel.proto ? 2 : 0;
1417 policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
1418 policy_info->action = XFRM_POLICY_ALLOW;
1419 policy_info->share = XFRM_SHARE_ANY;
1420 pthread_mutex_unlock(&this->mutex);
1421
1422 /* policies don't expire */
1423 policy_info->lft.soft_byte_limit = XFRM_INF;
1424 policy_info->lft.soft_packet_limit = XFRM_INF;
1425 policy_info->lft.hard_byte_limit = XFRM_INF;
1426 policy_info->lft.hard_packet_limit = XFRM_INF;
1427 policy_info->lft.soft_add_expires_seconds = 0;
1428 policy_info->lft.hard_add_expires_seconds = 0;
1429 policy_info->lft.soft_use_expires_seconds = 0;
1430 policy_info->lft.hard_use_expires_seconds = 0;
1431
1432 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
1433 rthdr->rta_type = XFRMA_TMPL;
1434 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1435
1436 hdr->nlmsg_len += rthdr->rta_len;
1437 if (hdr->nlmsg_len > sizeof(request))
1438 {
1439 return FAILED;
1440 }
1441
1442 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
1443
1444 if (ipcomp != IPCOMP_NONE)
1445 {
1446 tmpl->reqid = reqid;
1447 tmpl->id.proto = IPPROTO_COMP;
1448 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1449 tmpl->mode = mode;
1450 tmpl->optional = direction != POLICY_OUT;
1451 tmpl->family = src->get_family(src);
1452
1453 host2xfrm(src, &tmpl->saddr);
1454 host2xfrm(dst, &tmpl->id.daddr);
1455
1456 /* add an additional xfrm_user_tmpl */
1457 rthdr->rta_len += RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1458 hdr->nlmsg_len += RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1459 if (hdr->nlmsg_len > sizeof(request))
1460 {
1461 return FAILED;
1462 }
1463
1464 tmpl++;
1465 }
1466
1467 tmpl->reqid = reqid;
1468 tmpl->id.proto = proto_ike2kernel(protocol);
1469 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1470 tmpl->mode = mode;
1471 tmpl->family = src->get_family(src);
1472
1473 host2xfrm(src, &tmpl->saddr);
1474 host2xfrm(dst, &tmpl->id.daddr);
1475
1476 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1477 {
1478 DBG1(DBG_KNL, "unable to add policy %R === %R %N", src_ts, dst_ts,
1479 policy_dir_names, direction);
1480 return FAILED;
1481 }
1482
1483 /* install a route, if:
1484 * - we are NOT updating a policy
1485 * - this is a forward policy (to just get one for each child)
1486 * - we are in tunnel mode
1487 * - we are not using IPv6 (does not work correctly yet!)
1488 * - routing is not disabled via strongswan.conf
1489 */
1490 if (policy->route == NULL && direction == POLICY_FWD &&
1491 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6 &&
1492 this->install_routes)
1493 {
1494 route_entry_t *route = malloc_thing(route_entry_t);
1495
1496 if (charon->kernel_interface->get_address_by_ts(charon->kernel_interface,
1497 dst_ts, &route->src_ip) == SUCCESS)
1498 {
1499 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1500 route->gateway = charon->kernel_interface->get_nexthop(
1501 charon->kernel_interface, src);
1502 route->if_name = charon->kernel_interface->get_interface(
1503 charon->kernel_interface, dst);
1504 route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
1505 memcpy(route->dst_net.ptr, &policy->sel.saddr, route->dst_net.len);
1506 route->prefixlen = policy->sel.prefixlen_s;
1507
1508 switch (charon->kernel_interface->add_route(charon->kernel_interface,
1509 route->dst_net, route->prefixlen, route->gateway,
1510 route->src_ip, route->if_name))
1511 {
1512 default:
1513 DBG1(DBG_KNL, "unable to install source route for %H",
1514 route->src_ip);
1515 /* FALL */
1516 case ALREADY_DONE:
1517 /* route exists, do not uninstall */
1518 route_entry_destroy(route);
1519 break;
1520 case SUCCESS:
1521 /* cache the installed route */
1522 policy->route = route;
1523 break;
1524 }
1525 }
1526 else
1527 {
1528 free(route);
1529 }
1530 }
1531
1532 return SUCCESS;
1533 }
1534
1535 /**
1536 * Implementation of kernel_interface_t.query_policy.
1537 */
1538 static status_t query_policy(private_kernel_netlink_ipsec_t *this,
1539 traffic_selector_t *src_ts,
1540 traffic_selector_t *dst_ts,
1541 policy_dir_t direction, u_int32_t *use_time)
1542 {
1543 unsigned char request[NETLINK_BUFFER_SIZE];
1544 struct nlmsghdr *out = NULL, *hdr;
1545 struct xfrm_userpolicy_id *policy_id;
1546 struct xfrm_userpolicy_info *policy = NULL;
1547 size_t len;
1548
1549 memset(&request, 0, sizeof(request));
1550
1551 DBG2(DBG_KNL, "querying policy %R === %R %N", src_ts, dst_ts,
1552 policy_dir_names, direction);
1553
1554 hdr = (struct nlmsghdr*)request;
1555 hdr->nlmsg_flags = NLM_F_REQUEST;
1556 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
1557 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1558
1559 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1560 policy_id->sel = ts2selector(src_ts, dst_ts);
1561 policy_id->dir = direction;
1562
1563 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1564 {
1565 hdr = out;
1566 while (NLMSG_OK(hdr, len))
1567 {
1568 switch (hdr->nlmsg_type)
1569 {
1570 case XFRM_MSG_NEWPOLICY:
1571 {
1572 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1573 break;
1574 }
1575 case NLMSG_ERROR:
1576 {
1577 struct nlmsgerr *err = NLMSG_DATA(hdr);
1578 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
1579 strerror(-err->error), -err->error);
1580 break;
1581 }
1582 default:
1583 hdr = NLMSG_NEXT(hdr, len);
1584 continue;
1585 case NLMSG_DONE:
1586 break;
1587 }
1588 break;
1589 }
1590 }
1591
1592 if (policy == NULL)
1593 {
1594 DBG2(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
1595 policy_dir_names, direction);
1596 free(out);
1597 return FAILED;
1598 }
1599 *use_time = (time_t)policy->curlft.use_time;
1600
1601 free(out);
1602 return SUCCESS;
1603 }
1604
1605 /**
1606 * Implementation of kernel_interface_t.del_policy.
1607 */
1608 static status_t del_policy(private_kernel_netlink_ipsec_t *this,
1609 traffic_selector_t *src_ts,
1610 traffic_selector_t *dst_ts,
1611 policy_dir_t direction)
1612 {
1613 policy_entry_t *current, policy, *to_delete = NULL;
1614 route_entry_t *route;
1615 unsigned char request[NETLINK_BUFFER_SIZE];
1616 struct nlmsghdr *hdr;
1617 struct xfrm_userpolicy_id *policy_id;
1618 iterator_t *iterator;
1619
1620 DBG2(DBG_KNL, "deleting policy %R === %R %N", src_ts, dst_ts,
1621 policy_dir_names, direction);
1622
1623 /* create a policy */
1624 memset(&policy, 0, sizeof(policy_entry_t));
1625 policy.sel = ts2selector(src_ts, dst_ts);
1626 policy.direction = direction;
1627
1628 /* find the policy */
1629 iterator = this->policies->create_iterator_locked(this->policies, &this->mutex);
1630 while (iterator->iterate(iterator, (void**)&current))
1631 {
1632 if (memeq(&current->sel, &policy.sel, sizeof(struct xfrm_selector)) &&
1633 policy.direction == current->direction)
1634 {
1635 to_delete = current;
1636 if (--to_delete->refcount > 0)
1637 {
1638 /* is used by more SAs, keep in kernel */
1639 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
1640 iterator->destroy(iterator);
1641 return SUCCESS;
1642 }
1643 /* remove if last reference */
1644 iterator->remove(iterator);
1645 break;
1646 }
1647 }
1648 iterator->destroy(iterator);
1649 if (!to_delete)
1650 {
1651 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
1652 dst_ts, policy_dir_names, direction);
1653 return NOT_FOUND;
1654 }
1655
1656 memset(&request, 0, sizeof(request));
1657
1658 hdr = (struct nlmsghdr*)request;
1659 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1660 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
1661 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1662
1663 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1664 policy_id->sel = to_delete->sel;
1665 policy_id->dir = direction;
1666
1667 route = to_delete->route;
1668 free(to_delete);
1669
1670 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1671 {
1672 DBG1(DBG_KNL, "unable to delete policy %R === %R %N", src_ts, dst_ts,
1673 policy_dir_names, direction);
1674 return FAILED;
1675 }
1676
1677 if (route)
1678 {
1679 if (charon->kernel_interface->del_route(charon->kernel_interface,
1680 route->dst_net, route->prefixlen, route->gateway,
1681 route->src_ip, route->if_name) != SUCCESS)
1682 {
1683 DBG1(DBG_KNL, "error uninstalling route installed with "
1684 "policy %R === %R %N", src_ts, dst_ts,
1685 policy_dir_names, direction);
1686 }
1687 route_entry_destroy(route);
1688 }
1689 return SUCCESS;
1690 }
1691
1692 /**
1693 * Implementation of kernel_interface_t.destroy.
1694 */
1695 static void destroy(private_kernel_netlink_ipsec_t *this)
1696 {
1697 this->job->cancel(this->job);
1698 close(this->socket_xfrm_events);
1699 this->socket_xfrm->destroy(this->socket_xfrm);
1700 this->policies->destroy(this->policies);
1701 free(this);
1702 }
1703
1704 /*
1705 * Described in header.
1706 */
1707 kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
1708 {
1709 private_kernel_netlink_ipsec_t *this = malloc_thing(private_kernel_netlink_ipsec_t);
1710 struct sockaddr_nl addr;
1711
1712 /* public functions */
1713 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;
1714 this->public.interface.get_cpi = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,u_int32_t,u_int16_t*))get_cpi;
1715 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;
1716 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;
1717 this->public.interface.del_sa = (status_t(*)(kernel_ipsec_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
1718 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;
1719 this->public.interface.query_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
1720 this->public.interface.del_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t))del_policy;
1721 this->public.interface.destroy = (void(*)(kernel_ipsec_t*)) destroy;
1722
1723 /* private members */
1724 this->policies = linked_list_create();
1725 pthread_mutex_init(&this->mutex, NULL);
1726 this->install_routes = lib->settings->get_bool(lib->settings,
1727 "charon.install_routes", TRUE);
1728
1729 this->socket_xfrm = netlink_socket_create(NETLINK_XFRM);
1730
1731 memset(&addr, 0, sizeof(addr));
1732 addr.nl_family = AF_NETLINK;
1733
1734 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
1735 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1736 if (this->socket_xfrm_events <= 0)
1737 {
1738 charon->kill(charon, "unable to create XFRM event socket");
1739 }
1740 addr.nl_groups = XFRMNLGRP(ACQUIRE) | XFRMNLGRP(EXPIRE) |
1741 XFRMNLGRP(MIGRATE) | XFRMNLGRP(MAPPING);
1742 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
1743 {
1744 charon->kill(charon, "unable to bind XFRM event socket");
1745 }
1746
1747 this->job = callback_job_create((callback_job_cb_t)receive_events,
1748 this, NULL, NULL);
1749 charon->processor->queue_job(charon->processor, (job_t*)this->job);
1750
1751 return &this->public;
1752 }