1d5e3b2bd291bdd40f019f2a8c148c8271d09e19
[strongswan.git] / src / libcharon / plugins / kernel_netlink / kernel_netlink_ipsec.c
1 /*
2 * Copyright (C) 2006-2016 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2008-2016 Andreas Steffen
5 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
6 * Copyright (C) 2006 Daniel Roethlisberger
7 * Copyright (C) 2005 Jan Hutter
8 * HSR Hochschule fuer Technik Rapperswil
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 */
20
21 #define _GNU_SOURCE
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <stdint.h>
25 #include <linux/ipsec.h>
26 #include <linux/netlink.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/xfrm.h>
29 #include <linux/udp.h>
30 #include <net/if.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <dlfcn.h>
37
38 #include "kernel_netlink_ipsec.h"
39 #include "kernel_netlink_shared.h"
40
41 #include <daemon.h>
42 #include <utils/debug.h>
43 #include <threading/mutex.h>
44 #include <threading/condvar.h>
45 #include <collections/array.h>
46 #include <collections/hashtable.h>
47 #include <collections/linked_list.h>
48
49 /** Required for Linux 2.6.26 kernel and later */
50 #ifndef XFRM_STATE_AF_UNSPEC
51 #define XFRM_STATE_AF_UNSPEC 32
52 #endif
53
54 /** From linux/in.h */
55 #ifndef IP_XFRM_POLICY
56 #define IP_XFRM_POLICY 17
57 #endif
58
59 /** Missing on uclibc */
60 #ifndef IPV6_XFRM_POLICY
61 #define IPV6_XFRM_POLICY 34
62 #endif /*IPV6_XFRM_POLICY*/
63
64 /* from linux/udp.h */
65 #ifndef UDP_ENCAP
66 #define UDP_ENCAP 100
67 #endif
68
69 #ifndef UDP_ENCAP_ESPINUDP
70 #define UDP_ENCAP_ESPINUDP 2
71 #endif
72
73 /* this is not defined on some platforms */
74 #ifndef SOL_UDP
75 #define SOL_UDP IPPROTO_UDP
76 #endif
77
78 /** Base priority for installed policies */
79 #define PRIO_BASE 100000
80
81 /** Default lifetime of an acquire XFRM state (in seconds) */
82 #define DEFAULT_ACQUIRE_LIFETIME 165
83
84 /**
85 * Map the limit for bytes and packets to XFRM_INF by default
86 */
87 #define XFRM_LIMIT(x) ((x) == 0 ? XFRM_INF : (x))
88
89 /**
90 * Create ORable bitfield of XFRM NL groups
91 */
92 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
93
94 /**
95 * Returns a pointer to the first rtattr following the nlmsghdr *nlh and the
96 * 'usual' netlink data x like 'struct xfrm_usersa_info'
97 */
98 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + \
99 NLMSG_ALIGN(sizeof(x))))
100 /**
101 * Returns the total size of attached rta data
102 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
103 */
104 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
105
106 typedef struct kernel_algorithm_t kernel_algorithm_t;
107
108 /**
109 * Mapping of IKEv2 kernel identifier to linux crypto API names
110 */
111 struct kernel_algorithm_t {
112 /**
113 * Identifier specified in IKEv2
114 */
115 int ikev2;
116
117 /**
118 * Name of the algorithm in linux crypto API
119 */
120 const char *name;
121 };
122
123 ENUM(xfrm_msg_names, XFRM_MSG_NEWSA, XFRM_MSG_MAPPING,
124 "XFRM_MSG_NEWSA",
125 "XFRM_MSG_DELSA",
126 "XFRM_MSG_GETSA",
127 "XFRM_MSG_NEWPOLICY",
128 "XFRM_MSG_DELPOLICY",
129 "XFRM_MSG_GETPOLICY",
130 "XFRM_MSG_ALLOCSPI",
131 "XFRM_MSG_ACQUIRE",
132 "XFRM_MSG_EXPIRE",
133 "XFRM_MSG_UPDPOLICY",
134 "XFRM_MSG_UPDSA",
135 "XFRM_MSG_POLEXPIRE",
136 "XFRM_MSG_FLUSHSA",
137 "XFRM_MSG_FLUSHPOLICY",
138 "XFRM_MSG_NEWAE",
139 "XFRM_MSG_GETAE",
140 "XFRM_MSG_REPORT",
141 "XFRM_MSG_MIGRATE",
142 "XFRM_MSG_NEWSADINFO",
143 "XFRM_MSG_GETSADINFO",
144 "XFRM_MSG_NEWSPDINFO",
145 "XFRM_MSG_GETSPDINFO",
146 "XFRM_MSG_MAPPING"
147 );
148
149 ENUM(xfrm_attr_type_names, XFRMA_UNSPEC, XFRMA_REPLAY_ESN_VAL,
150 "XFRMA_UNSPEC",
151 "XFRMA_ALG_AUTH",
152 "XFRMA_ALG_CRYPT",
153 "XFRMA_ALG_COMP",
154 "XFRMA_ENCAP",
155 "XFRMA_TMPL",
156 "XFRMA_SA",
157 "XFRMA_POLICY",
158 "XFRMA_SEC_CTX",
159 "XFRMA_LTIME_VAL",
160 "XFRMA_REPLAY_VAL",
161 "XFRMA_REPLAY_THRESH",
162 "XFRMA_ETIMER_THRESH",
163 "XFRMA_SRCADDR",
164 "XFRMA_COADDR",
165 "XFRMA_LASTUSED",
166 "XFRMA_POLICY_TYPE",
167 "XFRMA_MIGRATE",
168 "XFRMA_ALG_AEAD",
169 "XFRMA_KMADDRESS",
170 "XFRMA_ALG_AUTH_TRUNC",
171 "XFRMA_MARK",
172 "XFRMA_TFCPAD",
173 "XFRMA_REPLAY_ESN_VAL",
174 );
175
176 /**
177 * Algorithms for encryption
178 */
179 static kernel_algorithm_t encryption_algs[] = {
180 /* {ENCR_DES_IV64, "***" }, */
181 {ENCR_DES, "des" },
182 {ENCR_3DES, "des3_ede" },
183 /* {ENCR_RC5, "***" }, */
184 /* {ENCR_IDEA, "***" }, */
185 {ENCR_CAST, "cast5" },
186 {ENCR_BLOWFISH, "blowfish" },
187 /* {ENCR_3IDEA, "***" }, */
188 /* {ENCR_DES_IV32, "***" }, */
189 {ENCR_NULL, "cipher_null" },
190 {ENCR_AES_CBC, "aes" },
191 {ENCR_AES_CTR, "rfc3686(ctr(aes))" },
192 {ENCR_AES_CCM_ICV8, "rfc4309(ccm(aes))" },
193 {ENCR_AES_CCM_ICV12, "rfc4309(ccm(aes))" },
194 {ENCR_AES_CCM_ICV16, "rfc4309(ccm(aes))" },
195 {ENCR_AES_GCM_ICV8, "rfc4106(gcm(aes))" },
196 {ENCR_AES_GCM_ICV12, "rfc4106(gcm(aes))" },
197 {ENCR_AES_GCM_ICV16, "rfc4106(gcm(aes))" },
198 {ENCR_NULL_AUTH_AES_GMAC, "rfc4543(gcm(aes))" },
199 {ENCR_CAMELLIA_CBC, "cbc(camellia)" },
200 /* {ENCR_CAMELLIA_CTR, "***" }, */
201 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
202 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
203 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
204 {ENCR_SERPENT_CBC, "serpent" },
205 {ENCR_TWOFISH_CBC, "twofish" },
206 {ENCR_CHACHA20_POLY1305, "rfc7539esp(chacha20,poly1305)"},
207 };
208
209 /**
210 * Algorithms for integrity protection
211 */
212 static kernel_algorithm_t integrity_algs[] = {
213 {AUTH_HMAC_MD5_96, "md5" },
214 {AUTH_HMAC_MD5_128, "hmac(md5)" },
215 {AUTH_HMAC_SHA1_96, "sha1" },
216 {AUTH_HMAC_SHA1_160, "hmac(sha1)" },
217 {AUTH_HMAC_SHA2_256_96, "sha256" },
218 {AUTH_HMAC_SHA2_256_128, "hmac(sha256)" },
219 {AUTH_HMAC_SHA2_384_192, "hmac(sha384)" },
220 {AUTH_HMAC_SHA2_512_256, "hmac(sha512)" },
221 /* {AUTH_DES_MAC, "***" }, */
222 /* {AUTH_KPDK_MD5, "***" }, */
223 {AUTH_AES_XCBC_96, "xcbc(aes)" },
224 {AUTH_AES_CMAC_96, "cmac(aes)" },
225 };
226
227 /**
228 * Algorithms for IPComp
229 */
230 static kernel_algorithm_t compression_algs[] = {
231 /* {IPCOMP_OUI, "***" }, */
232 {IPCOMP_DEFLATE, "deflate" },
233 {IPCOMP_LZS, "lzs" },
234 {IPCOMP_LZJH, "lzjh" },
235 };
236
237 /**
238 * Look up a kernel algorithm name and its key size
239 */
240 static const char* lookup_algorithm(transform_type_t type, int ikev2)
241 {
242 kernel_algorithm_t *list;
243 int i, count;
244 char *name;
245
246 switch (type)
247 {
248 case ENCRYPTION_ALGORITHM:
249 list = encryption_algs;
250 count = countof(encryption_algs);
251 break;
252 case INTEGRITY_ALGORITHM:
253 list = integrity_algs;
254 count = countof(integrity_algs);
255 break;
256 case COMPRESSION_ALGORITHM:
257 list = compression_algs;
258 count = countof(compression_algs);
259 break;
260 default:
261 return NULL;
262 }
263 for (i = 0; i < count; i++)
264 {
265 if (list[i].ikev2 == ikev2)
266 {
267 return list[i].name;
268 }
269 }
270 if (charon->kernel->lookup_algorithm(charon->kernel, ikev2, type, NULL,
271 &name))
272 {
273 return name;
274 }
275 return NULL;
276 }
277
278 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t;
279
280 /**
281 * Private variables and functions of kernel_netlink class.
282 */
283 struct private_kernel_netlink_ipsec_t {
284 /**
285 * Public part of the kernel_netlink_t object
286 */
287 kernel_netlink_ipsec_t public;
288
289 /**
290 * Mutex to lock access to installed policies
291 */
292 mutex_t *mutex;
293
294 /**
295 * Condvar to synchronize access to individual policies
296 */
297 condvar_t *condvar;
298
299 /**
300 * Hash table of installed policies (policy_entry_t)
301 */
302 hashtable_t *policies;
303
304 /**
305 * Hash table of IPsec SAs using policies (ipsec_sa_t)
306 */
307 hashtable_t *sas;
308
309 /**
310 * Netlink xfrm socket (IPsec)
311 */
312 netlink_socket_t *socket_xfrm;
313
314 /**
315 * Netlink xfrm socket to receive acquire and expire events
316 */
317 int socket_xfrm_events;
318
319 /**
320 * Whether to install routes along policies
321 */
322 bool install_routes;
323
324 /**
325 * Whether to set protocol and ports on selector installed with transport
326 * mode IPsec SAs
327 */
328 bool proto_port_transport;
329
330 /**
331 * Whether to always use UPDATE to install policies
332 */
333 bool policy_update;
334
335 /**
336 * Installed port based IKE bypass policies, as bypass_t
337 */
338 array_t *bypass;
339
340 /**
341 * Custom priority calculation function
342 */
343 uint32_t (*get_priority)(kernel_ipsec_policy_id_t *id,
344 kernel_ipsec_manage_policy_t *data);
345 };
346
347 typedef struct route_entry_t route_entry_t;
348
349 /**
350 * Installed routing entry
351 */
352 struct route_entry_t {
353 /** Name of the interface the route is bound to */
354 char *if_name;
355
356 /** Source ip of the route */
357 host_t *src_ip;
358
359 /** Gateway for this route */
360 host_t *gateway;
361
362 /** Destination net */
363 chunk_t dst_net;
364
365 /** Destination net prefixlen */
366 uint8_t prefixlen;
367 };
368
369 /**
370 * Destroy a route_entry_t object
371 */
372 static void route_entry_destroy(route_entry_t *this)
373 {
374 free(this->if_name);
375 this->src_ip->destroy(this->src_ip);
376 DESTROY_IF(this->gateway);
377 chunk_free(&this->dst_net);
378 free(this);
379 }
380
381 /**
382 * Compare two route_entry_t objects
383 */
384 static bool route_entry_equals(route_entry_t *a, route_entry_t *b)
385 {
386 return a->if_name && b->if_name && streq(a->if_name, b->if_name) &&
387 a->src_ip->ip_equals(a->src_ip, b->src_ip) &&
388 a->gateway->ip_equals(a->gateway, b->gateway) &&
389 chunk_equals(a->dst_net, b->dst_net) && a->prefixlen == b->prefixlen;
390 }
391
392 typedef struct ipsec_sa_t ipsec_sa_t;
393
394 /**
395 * IPsec SA assigned to a policy.
396 */
397 struct ipsec_sa_t {
398 /** Source address of this SA */
399 host_t *src;
400
401 /** Destination address of this SA */
402 host_t *dst;
403
404 /** Optional mark */
405 mark_t mark;
406
407 /** Description of this SA */
408 ipsec_sa_cfg_t cfg;
409
410 /** Reference count for this SA */
411 refcount_t refcount;
412 };
413
414 /**
415 * Hash function for ipsec_sa_t objects
416 */
417 static u_int ipsec_sa_hash(ipsec_sa_t *sa)
418 {
419 return chunk_hash_inc(sa->src->get_address(sa->src),
420 chunk_hash_inc(sa->dst->get_address(sa->dst),
421 chunk_hash_inc(chunk_from_thing(sa->mark),
422 chunk_hash(chunk_from_thing(sa->cfg)))));
423 }
424
425 /**
426 * Equality function for ipsec_sa_t objects
427 */
428 static bool ipsec_sa_equals(ipsec_sa_t *sa, ipsec_sa_t *other_sa)
429 {
430 return sa->src->ip_equals(sa->src, other_sa->src) &&
431 sa->dst->ip_equals(sa->dst, other_sa->dst) &&
432 sa->mark.value == other_sa->mark.value &&
433 sa->mark.mask == other_sa->mark.mask &&
434 ipsec_sa_cfg_equals(&sa->cfg, &other_sa->cfg);
435 }
436
437 /**
438 * Allocate or reference an IPsec SA object
439 */
440 static ipsec_sa_t *ipsec_sa_create(private_kernel_netlink_ipsec_t *this,
441 host_t *src, host_t *dst, mark_t mark,
442 ipsec_sa_cfg_t *cfg)
443 {
444 ipsec_sa_t *sa, *found;
445 INIT(sa,
446 .src = src,
447 .dst = dst,
448 .mark = mark,
449 .cfg = *cfg,
450 );
451 found = this->sas->get(this->sas, sa);
452 if (!found)
453 {
454 sa->src = src->clone(src);
455 sa->dst = dst->clone(dst);
456 this->sas->put(this->sas, sa, sa);
457 }
458 else
459 {
460 free(sa);
461 sa = found;
462 }
463 ref_get(&sa->refcount);
464 return sa;
465 }
466
467 /**
468 * Release and destroy an IPsec SA object
469 */
470 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t *this,
471 ipsec_sa_t *sa)
472 {
473 if (ref_put(&sa->refcount))
474 {
475 this->sas->remove(this->sas, sa);
476 DESTROY_IF(sa->src);
477 DESTROY_IF(sa->dst);
478 free(sa);
479 }
480 }
481
482 typedef struct policy_sa_t policy_sa_t;
483 typedef struct policy_sa_out_t policy_sa_out_t;
484
485 /**
486 * Mapping between a policy and an IPsec SA.
487 */
488 struct policy_sa_t {
489 /** Priority assigned to the policy when installed with this SA */
490 uint32_t priority;
491
492 /** Automatic priority assigned to the policy when installed with this SA */
493 uint32_t auto_priority;
494
495 /** Type of the policy */
496 policy_type_t type;
497
498 /** Assigned SA */
499 ipsec_sa_t *sa;
500 };
501
502 /**
503 * For outbound policies we also cache the traffic selectors in order to install
504 * the route.
505 */
506 struct policy_sa_out_t {
507 /** Generic interface */
508 policy_sa_t generic;
509
510 /** Source traffic selector of this policy */
511 traffic_selector_t *src_ts;
512
513 /** Destination traffic selector of this policy */
514 traffic_selector_t *dst_ts;
515 };
516
517 /**
518 * Create a policy_sa(_in)_t object
519 */
520 static policy_sa_t *policy_sa_create(private_kernel_netlink_ipsec_t *this,
521 policy_dir_t dir, policy_type_t type, host_t *src, host_t *dst,
522 traffic_selector_t *src_ts, traffic_selector_t *dst_ts, mark_t mark,
523 ipsec_sa_cfg_t *cfg)
524 {
525 policy_sa_t *policy;
526
527 if (dir == POLICY_OUT)
528 {
529 policy_sa_out_t *out;
530 INIT(out,
531 .src_ts = src_ts->clone(src_ts),
532 .dst_ts = dst_ts->clone(dst_ts),
533 );
534 policy = &out->generic;
535 }
536 else
537 {
538 INIT(policy, .priority = 0);
539 }
540 policy->type = type;
541 policy->sa = ipsec_sa_create(this, src, dst, mark, cfg);
542 return policy;
543 }
544
545 /**
546 * Destroy a policy_sa(_in)_t object
547 */
548 static void policy_sa_destroy(policy_sa_t *policy, policy_dir_t *dir,
549 private_kernel_netlink_ipsec_t *this)
550 {
551 if (*dir == POLICY_OUT)
552 {
553 policy_sa_out_t *out = (policy_sa_out_t*)policy;
554 out->src_ts->destroy(out->src_ts);
555 out->dst_ts->destroy(out->dst_ts);
556 }
557 ipsec_sa_destroy(this, policy->sa);
558 free(policy);
559 }
560
561 typedef struct policy_entry_t policy_entry_t;
562
563 /**
564 * Installed kernel policy.
565 */
566 struct policy_entry_t {
567
568 /** Direction of this policy: in, out, forward */
569 uint8_t direction;
570
571 /** Parameters of installed policy */
572 struct xfrm_selector sel;
573
574 /** Optional mark */
575 uint32_t mark;
576
577 /** Associated route installed for this policy */
578 route_entry_t *route;
579
580 /** List of SAs this policy is used by, ordered by priority */
581 linked_list_t *used_by;
582
583 /** reqid for this policy */
584 uint32_t reqid;
585
586 /** Number of threads waiting to work on this policy */
587 int waiting;
588
589 /** TRUE if a thread is working on this policy */
590 bool working;
591 };
592
593 /**
594 * Destroy a policy_entry_t object
595 */
596 static void policy_entry_destroy(private_kernel_netlink_ipsec_t *this,
597 policy_entry_t *policy)
598 {
599 if (policy->route)
600 {
601 route_entry_destroy(policy->route);
602 }
603 if (policy->used_by)
604 {
605 policy->used_by->invoke_function(policy->used_by,
606 (linked_list_invoke_t)policy_sa_destroy,
607 &policy->direction, this);
608 policy->used_by->destroy(policy->used_by);
609 }
610 free(policy);
611 }
612
613 /**
614 * Hash function for policy_entry_t objects
615 */
616 static u_int policy_hash(policy_entry_t *key)
617 {
618 chunk_t chunk = chunk_from_thing(key->sel);
619 return chunk_hash_inc(chunk, chunk_hash(chunk_from_thing(key->mark)));
620 }
621
622 /**
623 * Equality function for policy_entry_t objects
624 */
625 static bool policy_equals(policy_entry_t *key, policy_entry_t *other_key)
626 {
627 return memeq(&key->sel, &other_key->sel, sizeof(struct xfrm_selector)) &&
628 key->mark == other_key->mark &&
629 key->direction == other_key->direction;
630 }
631
632 /**
633 * Determine number of set bits in 16 bit port mask
634 */
635 static inline uint32_t port_mask_bits(uint16_t port_mask)
636 {
637 uint32_t bits;
638 uint16_t bit_mask = 0x8000;
639
640 port_mask = ntohs(port_mask);
641
642 for (bits = 0; bits < 16; bits++)
643 {
644 if (!(port_mask & bit_mask))
645 {
646 break;
647 }
648 bit_mask >>= 1;
649 }
650 return bits;
651 }
652
653 /**
654 * Calculate the priority of a policy
655 *
656 * bits 0-0: restriction to network interface (0..1) 1 bit
657 * bits 1-6: src + dst port mask bits (2 * 0..16) 6 bits
658 * bits 7-7: restriction to protocol (0..1) 1 bit
659 * bits 8-16: src + dst network mask bits (2 * 0..128) 9 bits
660 * 17 bits
661 *
662 * smallest value: 000000000 0 000000 0: 0, lowest priority = 100'000
663 * largest value : 100000000 1 100000 1: 65'729, highst priority = 34'271
664 */
665 static uint32_t get_priority(policy_entry_t *policy, policy_priority_t prio,
666 char *interface)
667 {
668 uint32_t priority = PRIO_BASE, sport_mask_bits, dport_mask_bits;
669
670 switch (prio)
671 {
672 case POLICY_PRIORITY_FALLBACK:
673 priority += PRIO_BASE;
674 /* fall-through to next case */
675 case POLICY_PRIORITY_ROUTED:
676 priority += PRIO_BASE;
677 /* fall-through to next case */
678 case POLICY_PRIORITY_DEFAULT:
679 priority += PRIO_BASE;
680 /* fall-through to next case */
681 case POLICY_PRIORITY_PASS:
682 break;
683 }
684 sport_mask_bits = port_mask_bits(policy->sel.sport_mask);
685 dport_mask_bits = port_mask_bits(policy->sel.dport_mask);
686
687 /* calculate priority */
688 priority -= (policy->sel.prefixlen_s + policy->sel.prefixlen_d) * 256;
689 priority -= policy->sel.proto ? 128 : 0;
690 priority -= (sport_mask_bits + dport_mask_bits) * 2;
691 priority -= (interface != NULL);
692
693 return priority;
694 }
695
696 /**
697 * Convert the general ipsec mode to the one defined in xfrm.h
698 */
699 static uint8_t mode2kernel(ipsec_mode_t mode)
700 {
701 switch (mode)
702 {
703 case MODE_TRANSPORT:
704 return XFRM_MODE_TRANSPORT;
705 case MODE_TUNNEL:
706 return XFRM_MODE_TUNNEL;
707 case MODE_BEET:
708 return XFRM_MODE_BEET;
709 default:
710 return mode;
711 }
712 }
713
714 /**
715 * Convert a host_t to a struct xfrm_address
716 */
717 static void host2xfrm(host_t *host, xfrm_address_t *xfrm)
718 {
719 chunk_t chunk = host->get_address(host);
720 memcpy(xfrm, chunk.ptr, min(chunk.len, sizeof(xfrm_address_t)));
721 }
722
723 /**
724 * Convert a struct xfrm_address to a host_t
725 */
726 static host_t* xfrm2host(int family, xfrm_address_t *xfrm, uint16_t port)
727 {
728 chunk_t chunk;
729
730 switch (family)
731 {
732 case AF_INET:
733 chunk = chunk_create((u_char*)&xfrm->a4, sizeof(xfrm->a4));
734 break;
735 case AF_INET6:
736 chunk = chunk_create((u_char*)&xfrm->a6, sizeof(xfrm->a6));
737 break;
738 default:
739 return NULL;
740 }
741 return host_create_from_chunk(family, chunk, ntohs(port));
742 }
743
744 /**
745 * Convert a traffic selector address range to subnet and its mask.
746 */
747 static void ts2subnet(traffic_selector_t* ts,
748 xfrm_address_t *net, uint8_t *mask)
749 {
750 host_t *net_host;
751 chunk_t net_chunk;
752
753 ts->to_subnet(ts, &net_host, mask);
754 net_chunk = net_host->get_address(net_host);
755 memcpy(net, net_chunk.ptr, net_chunk.len);
756 net_host->destroy(net_host);
757 }
758
759 /**
760 * Convert a traffic selector port range to port/portmask
761 */
762 static void ts2ports(traffic_selector_t* ts,
763 uint16_t *port, uint16_t *mask)
764 {
765 uint16_t from, to, bitmask;
766 int bit;
767
768 from = ts->get_from_port(ts);
769 to = ts->get_to_port(ts);
770
771 /* Quick check for a single port */
772 if (from == to)
773 {
774 *port = htons(from);
775 *mask = ~0;
776 }
777 else
778 {
779 /* Compute the port mask for port ranges */
780 *mask = 0;
781
782 for (bit = 15; bit >= 0; bit--)
783 {
784 bitmask = 1 << bit;
785
786 if ((bitmask & from) != (bitmask & to))
787 {
788 *port = htons(from & *mask);
789 *mask = htons(*mask);
790 return;
791 }
792 *mask |= bitmask;
793 }
794 }
795 return;
796 }
797
798 /**
799 * Convert a pair of traffic_selectors to an xfrm_selector
800 */
801 static struct xfrm_selector ts2selector(traffic_selector_t *src,
802 traffic_selector_t *dst,
803 char *interface)
804 {
805 struct xfrm_selector sel;
806 uint16_t port;
807
808 memset(&sel, 0, sizeof(sel));
809 sel.family = (src->get_type(src) == TS_IPV4_ADDR_RANGE) ? AF_INET : AF_INET6;
810 /* src or dest proto may be "any" (0), use more restrictive one */
811 sel.proto = max(src->get_protocol(src), dst->get_protocol(dst));
812 ts2subnet(dst, &sel.daddr, &sel.prefixlen_d);
813 ts2subnet(src, &sel.saddr, &sel.prefixlen_s);
814 ts2ports(dst, &sel.dport, &sel.dport_mask);
815 ts2ports(src, &sel.sport, &sel.sport_mask);
816 if ((sel.proto == IPPROTO_ICMP || sel.proto == IPPROTO_ICMPV6) &&
817 (sel.dport || sel.sport))
818 {
819 /* the kernel expects the ICMP type and code in the source and
820 * destination port fields, respectively. */
821 port = ntohs(max(sel.dport, sel.sport));
822 sel.sport = htons(traffic_selector_icmp_type(port));
823 sel.sport_mask = sel.sport ? ~0 : 0;
824 sel.dport = htons(traffic_selector_icmp_code(port));
825 sel.dport_mask = sel.dport ? ~0 : 0;
826 }
827 sel.ifindex = interface ? if_nametoindex(interface) : 0;
828 sel.user = 0;
829
830 return sel;
831 }
832
833 /**
834 * Convert an xfrm_selector to a src|dst traffic_selector
835 */
836 static traffic_selector_t* selector2ts(struct xfrm_selector *sel, bool src)
837 {
838 u_char *addr;
839 uint8_t prefixlen;
840 uint16_t port = 0;
841 host_t *host = NULL;
842
843 if (src)
844 {
845 addr = (u_char*)&sel->saddr;
846 prefixlen = sel->prefixlen_s;
847 if (sel->sport_mask)
848 {
849 port = ntohs(sel->sport);
850 }
851 }
852 else
853 {
854 addr = (u_char*)&sel->daddr;
855 prefixlen = sel->prefixlen_d;
856 if (sel->dport_mask)
857 {
858 port = ntohs(sel->dport);
859 }
860 }
861 if (sel->proto == IPPROTO_ICMP || sel->proto == IPPROTO_ICMPV6)
862 { /* convert ICMP[v6] message type and code as supplied by the kernel in
863 * source and destination ports (both in network order) */
864 port = (sel->sport >> 8) | (sel->dport & 0xff00);
865 port = ntohs(port);
866 }
867 /* The Linux 2.6 kernel does not set the selector's family field,
868 * so as a kludge we additionally test the prefix length.
869 */
870 if (sel->family == AF_INET || sel->prefixlen_s == 32)
871 {
872 host = host_create_from_chunk(AF_INET, chunk_create(addr, 4), 0);
873 }
874 else if (sel->family == AF_INET6 || sel->prefixlen_s == 128)
875 {
876 host = host_create_from_chunk(AF_INET6, chunk_create(addr, 16), 0);
877 }
878
879 if (host)
880 {
881 return traffic_selector_create_from_subnet(host, prefixlen,
882 sel->proto, port, port ?: 65535);
883 }
884 return NULL;
885 }
886
887 /**
888 * Process a XFRM_MSG_ACQUIRE from kernel
889 */
890 static void process_acquire(private_kernel_netlink_ipsec_t *this,
891 struct nlmsghdr *hdr)
892 {
893 struct xfrm_user_acquire *acquire;
894 struct rtattr *rta;
895 size_t rtasize;
896 traffic_selector_t *src_ts, *dst_ts;
897 uint32_t reqid = 0;
898 int proto = 0;
899
900 acquire = NLMSG_DATA(hdr);
901 rta = XFRM_RTA(hdr, struct xfrm_user_acquire);
902 rtasize = XFRM_PAYLOAD(hdr, struct xfrm_user_acquire);
903
904 DBG2(DBG_KNL, "received a XFRM_MSG_ACQUIRE");
905
906 while (RTA_OK(rta, rtasize))
907 {
908 DBG2(DBG_KNL, " %N", xfrm_attr_type_names, rta->rta_type);
909
910 if (rta->rta_type == XFRMA_TMPL)
911 {
912 struct xfrm_user_tmpl* tmpl;
913 tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rta);
914 reqid = tmpl->reqid;
915 proto = tmpl->id.proto;
916 }
917 rta = RTA_NEXT(rta, rtasize);
918 }
919 switch (proto)
920 {
921 case 0:
922 case IPPROTO_ESP:
923 case IPPROTO_AH:
924 break;
925 default:
926 /* acquire for AH/ESP only, not for IPCOMP */
927 return;
928 }
929 src_ts = selector2ts(&acquire->sel, TRUE);
930 dst_ts = selector2ts(&acquire->sel, FALSE);
931
932 charon->kernel->acquire(charon->kernel, reqid, src_ts, dst_ts);
933 }
934
935 /**
936 * Process a XFRM_MSG_EXPIRE from kernel
937 */
938 static void process_expire(private_kernel_netlink_ipsec_t *this,
939 struct nlmsghdr *hdr)
940 {
941 struct xfrm_user_expire *expire;
942 uint32_t spi;
943 uint8_t protocol;
944 host_t *dst;
945
946 expire = NLMSG_DATA(hdr);
947 protocol = expire->state.id.proto;
948 spi = expire->state.id.spi;
949
950 DBG2(DBG_KNL, "received a XFRM_MSG_EXPIRE");
951
952 if (protocol == IPPROTO_ESP || protocol == IPPROTO_AH)
953 {
954 dst = xfrm2host(expire->state.family, &expire->state.id.daddr, 0);
955 if (dst)
956 {
957 charon->kernel->expire(charon->kernel, protocol, spi, dst,
958 expire->hard != 0);
959 dst->destroy(dst);
960 }
961 }
962 }
963
964 /**
965 * Process a XFRM_MSG_MIGRATE from kernel
966 */
967 static void process_migrate(private_kernel_netlink_ipsec_t *this,
968 struct nlmsghdr *hdr)
969 {
970 struct xfrm_userpolicy_id *policy_id;
971 struct rtattr *rta;
972 size_t rtasize;
973 traffic_selector_t *src_ts, *dst_ts;
974 host_t *local = NULL, *remote = NULL;
975 host_t *old_src = NULL, *old_dst = NULL;
976 host_t *new_src = NULL, *new_dst = NULL;
977 uint32_t reqid = 0;
978 policy_dir_t dir;
979
980 policy_id = NLMSG_DATA(hdr);
981 rta = XFRM_RTA(hdr, struct xfrm_userpolicy_id);
982 rtasize = XFRM_PAYLOAD(hdr, struct xfrm_userpolicy_id);
983
984 DBG2(DBG_KNL, "received a XFRM_MSG_MIGRATE");
985
986 src_ts = selector2ts(&policy_id->sel, TRUE);
987 dst_ts = selector2ts(&policy_id->sel, FALSE);
988 dir = (policy_dir_t)policy_id->dir;
989
990 DBG2(DBG_KNL, " policy: %R === %R %N", src_ts, dst_ts, policy_dir_names);
991
992 while (RTA_OK(rta, rtasize))
993 {
994 DBG2(DBG_KNL, " %N", xfrm_attr_type_names, rta->rta_type);
995 if (rta->rta_type == XFRMA_KMADDRESS)
996 {
997 struct xfrm_user_kmaddress *kmaddress;
998
999 kmaddress = (struct xfrm_user_kmaddress*)RTA_DATA(rta);
1000 local = xfrm2host(kmaddress->family, &kmaddress->local, 0);
1001 remote = xfrm2host(kmaddress->family, &kmaddress->remote, 0);
1002 DBG2(DBG_KNL, " kmaddress: %H...%H", local, remote);
1003 }
1004 else if (rta->rta_type == XFRMA_MIGRATE)
1005 {
1006 struct xfrm_user_migrate *migrate;
1007
1008 migrate = (struct xfrm_user_migrate*)RTA_DATA(rta);
1009 old_src = xfrm2host(migrate->old_family, &migrate->old_saddr, 0);
1010 old_dst = xfrm2host(migrate->old_family, &migrate->old_daddr, 0);
1011 new_src = xfrm2host(migrate->new_family, &migrate->new_saddr, 0);
1012 new_dst = xfrm2host(migrate->new_family, &migrate->new_daddr, 0);
1013 reqid = migrate->reqid;
1014 DBG2(DBG_KNL, " migrate %H...%H to %H...%H, reqid {%u}",
1015 old_src, old_dst, new_src, new_dst, reqid);
1016 DESTROY_IF(old_src);
1017 DESTROY_IF(old_dst);
1018 DESTROY_IF(new_src);
1019 DESTROY_IF(new_dst);
1020 }
1021 rta = RTA_NEXT(rta, rtasize);
1022 }
1023
1024 if (src_ts && dst_ts && local && remote)
1025 {
1026 charon->kernel->migrate(charon->kernel, reqid, src_ts, dst_ts, dir,
1027 local, remote);
1028 }
1029 else
1030 {
1031 DESTROY_IF(src_ts);
1032 DESTROY_IF(dst_ts);
1033 DESTROY_IF(local);
1034 DESTROY_IF(remote);
1035 }
1036 }
1037
1038 /**
1039 * Process a XFRM_MSG_MAPPING from kernel
1040 */
1041 static void process_mapping(private_kernel_netlink_ipsec_t *this,
1042 struct nlmsghdr *hdr)
1043 {
1044 struct xfrm_user_mapping *mapping;
1045 uint32_t spi;
1046
1047 mapping = NLMSG_DATA(hdr);
1048 spi = mapping->id.spi;
1049
1050 DBG2(DBG_KNL, "received a XFRM_MSG_MAPPING");
1051
1052 if (mapping->id.proto == IPPROTO_ESP)
1053 {
1054 host_t *dst, *new;
1055
1056 dst = xfrm2host(mapping->id.family, &mapping->id.daddr, 0);
1057 if (dst)
1058 {
1059 new = xfrm2host(mapping->id.family, &mapping->new_saddr,
1060 mapping->new_sport);
1061 if (new)
1062 {
1063 charon->kernel->mapping(charon->kernel, IPPROTO_ESP, spi, dst,
1064 new);
1065 new->destroy(new);
1066 }
1067 dst->destroy(dst);
1068 }
1069 }
1070 }
1071
1072 /**
1073 * Receives events from kernel
1074 */
1075 static bool receive_events(private_kernel_netlink_ipsec_t *this, int fd,
1076 watcher_event_t event)
1077 {
1078 char response[1024];
1079 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
1080 struct sockaddr_nl addr;
1081 socklen_t addr_len = sizeof(addr);
1082 int len;
1083
1084 len = recvfrom(this->socket_xfrm_events, response, sizeof(response),
1085 MSG_DONTWAIT, (struct sockaddr*)&addr, &addr_len);
1086 if (len < 0)
1087 {
1088 switch (errno)
1089 {
1090 case EINTR:
1091 /* interrupted, try again */
1092 return TRUE;
1093 case EAGAIN:
1094 /* no data ready, select again */
1095 return TRUE;
1096 default:
1097 DBG1(DBG_KNL, "unable to receive from XFRM event socket: %s "
1098 "(%d)", strerror(errno), errno);
1099 sleep(1);
1100 return TRUE;
1101 }
1102 }
1103
1104 if (addr.nl_pid != 0)
1105 { /* not from kernel. not interested, try another one */
1106 return TRUE;
1107 }
1108
1109 while (NLMSG_OK(hdr, len))
1110 {
1111 switch (hdr->nlmsg_type)
1112 {
1113 case XFRM_MSG_ACQUIRE:
1114 process_acquire(this, hdr);
1115 break;
1116 case XFRM_MSG_EXPIRE:
1117 process_expire(this, hdr);
1118 break;
1119 case XFRM_MSG_MIGRATE:
1120 process_migrate(this, hdr);
1121 break;
1122 case XFRM_MSG_MAPPING:
1123 process_mapping(this, hdr);
1124 break;
1125 default:
1126 DBG1(DBG_KNL, "received unknown event from XFRM event "
1127 "socket: %d", hdr->nlmsg_type);
1128 break;
1129 }
1130 hdr = NLMSG_NEXT(hdr, len);
1131 }
1132 return TRUE;
1133 }
1134
1135 METHOD(kernel_ipsec_t, get_features, kernel_feature_t,
1136 private_kernel_netlink_ipsec_t *this)
1137 {
1138 return KERNEL_ESP_V3_TFC;
1139 }
1140
1141 /**
1142 * Get an SPI for a specific protocol from the kernel.
1143 */
1144 static status_t get_spi_internal(private_kernel_netlink_ipsec_t *this,
1145 host_t *src, host_t *dst, uint8_t proto, uint32_t min, uint32_t max,
1146 uint32_t *spi)
1147 {
1148 netlink_buf_t request;
1149 struct nlmsghdr *hdr, *out;
1150 struct xfrm_userspi_info *userspi;
1151 uint32_t received_spi = 0;
1152 size_t len;
1153
1154 memset(&request, 0, sizeof(request));
1155
1156 hdr = &request.hdr;
1157 hdr->nlmsg_flags = NLM_F_REQUEST;
1158 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
1159 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
1160
1161 userspi = NLMSG_DATA(hdr);
1162 host2xfrm(src, &userspi->info.saddr);
1163 host2xfrm(dst, &userspi->info.id.daddr);
1164 userspi->info.id.proto = proto;
1165 userspi->info.mode = XFRM_MODE_TUNNEL;
1166 userspi->info.family = src->get_family(src);
1167 userspi->min = min;
1168 userspi->max = max;
1169
1170 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1171 {
1172 hdr = out;
1173 while (NLMSG_OK(hdr, len))
1174 {
1175 switch (hdr->nlmsg_type)
1176 {
1177 case XFRM_MSG_NEWSA:
1178 {
1179 struct xfrm_usersa_info* usersa = NLMSG_DATA(hdr);
1180 received_spi = usersa->id.spi;
1181 break;
1182 }
1183 case NLMSG_ERROR:
1184 {
1185 struct nlmsgerr *err = NLMSG_DATA(hdr);
1186 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
1187 strerror(-err->error), -err->error);
1188 break;
1189 }
1190 default:
1191 hdr = NLMSG_NEXT(hdr, len);
1192 continue;
1193 case NLMSG_DONE:
1194 break;
1195 }
1196 break;
1197 }
1198 free(out);
1199 }
1200
1201 if (received_spi == 0)
1202 {
1203 return FAILED;
1204 }
1205
1206 *spi = received_spi;
1207 return SUCCESS;
1208 }
1209
1210 METHOD(kernel_ipsec_t, get_spi, status_t,
1211 private_kernel_netlink_ipsec_t *this, host_t *src, host_t *dst,
1212 uint8_t protocol, uint32_t *spi)
1213 {
1214 if (get_spi_internal(this, src, dst, protocol,
1215 0xc0000000, 0xcFFFFFFF, spi) != SUCCESS)
1216 {
1217 DBG1(DBG_KNL, "unable to get SPI");
1218 return FAILED;
1219 }
1220
1221 DBG2(DBG_KNL, "got SPI %.8x", ntohl(*spi));
1222 return SUCCESS;
1223 }
1224
1225 METHOD(kernel_ipsec_t, get_cpi, status_t,
1226 private_kernel_netlink_ipsec_t *this, host_t *src, host_t *dst,
1227 uint16_t *cpi)
1228 {
1229 uint32_t received_spi = 0;
1230
1231 if (get_spi_internal(this, src, dst, IPPROTO_COMP,
1232 0x100, 0xEFFF, &received_spi) != SUCCESS)
1233 {
1234 DBG1(DBG_KNL, "unable to get CPI");
1235 return FAILED;
1236 }
1237
1238 *cpi = htons((uint16_t)ntohl(received_spi));
1239
1240 DBG2(DBG_KNL, "got CPI %.4x", ntohs(*cpi));
1241 return SUCCESS;
1242 }
1243
1244 /**
1245 * Format the mark for debug messages
1246 */
1247 static void format_mark(char *buf, int buflen, mark_t mark)
1248 {
1249 if (mark.value | mark.mask)
1250 {
1251 snprintf(buf, buflen, " (mark %u/0x%08x)", mark.value, mark.mask);
1252 }
1253 }
1254
1255 /**
1256 * Add a XFRM mark to message if required
1257 */
1258 static bool add_mark(struct nlmsghdr *hdr, int buflen, mark_t mark)
1259 {
1260 if (mark.value | mark.mask)
1261 {
1262 struct xfrm_mark *xmrk;
1263
1264 xmrk = netlink_reserve(hdr, buflen, XFRMA_MARK, sizeof(*xmrk));
1265 if (!xmrk)
1266 {
1267 return FALSE;
1268 }
1269 xmrk->v = mark.value;
1270 xmrk->m = mark.mask;
1271 }
1272 return TRUE;
1273 }
1274
1275 METHOD(kernel_ipsec_t, add_sa, status_t,
1276 private_kernel_netlink_ipsec_t *this, kernel_ipsec_sa_id_t *id,
1277 kernel_ipsec_add_sa_t *data)
1278 {
1279 netlink_buf_t request;
1280 const char *alg_name;
1281 char markstr[32] = "";
1282 struct nlmsghdr *hdr;
1283 struct xfrm_usersa_info *sa;
1284 uint16_t icv_size = 64, ipcomp = data->ipcomp;
1285 ipsec_mode_t mode = data->mode, original_mode = data->mode;
1286 traffic_selector_t *first_src_ts, *first_dst_ts;
1287 status_t status = FAILED;
1288
1289 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1290 * we are in the recursive call below */
1291 if (ipcomp != IPCOMP_NONE && data->cpi != 0)
1292 {
1293 lifetime_cfg_t lft = {{0,0,0},{0,0,0},{0,0,0}};
1294 kernel_ipsec_sa_id_t ipcomp_id = {
1295 .src = id->src,
1296 .dst = id->dst,
1297 .spi = htonl(ntohs(data->cpi)),
1298 .proto = IPPROTO_COMP,
1299 .mark = id->mark,
1300 };
1301 kernel_ipsec_add_sa_t ipcomp_sa = {
1302 .reqid = data->reqid,
1303 .mode = data->mode,
1304 .src_ts = data->src_ts,
1305 .dst_ts = data->dst_ts,
1306 .lifetime = &lft,
1307 .enc_alg = ENCR_UNDEFINED,
1308 .int_alg = AUTH_UNDEFINED,
1309 .tfc = data->tfc,
1310 .ipcomp = data->ipcomp,
1311 .initiator = data->initiator,
1312 .inbound = data->inbound,
1313 .update = data->update,
1314 };
1315 add_sa(this, &ipcomp_id, &ipcomp_sa);
1316 ipcomp = IPCOMP_NONE;
1317 /* use transport mode ESP SA, IPComp uses tunnel mode */
1318 mode = MODE_TRANSPORT;
1319 }
1320
1321 memset(&request, 0, sizeof(request));
1322 format_mark(markstr, sizeof(markstr), id->mark);
1323
1324 DBG2(DBG_KNL, "adding SAD entry with SPI %.8x and reqid {%u}%s",
1325 ntohl(id->spi), data->reqid, markstr);
1326
1327 hdr = &request.hdr;
1328 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1329 hdr->nlmsg_type = data->update ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
1330 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1331
1332 sa = NLMSG_DATA(hdr);
1333 host2xfrm(id->src, &sa->saddr);
1334 host2xfrm(id->dst, &sa->id.daddr);
1335 sa->id.spi = id->spi;
1336 sa->id.proto = id->proto;
1337 sa->family = id->src->get_family(id->src);
1338 sa->mode = mode2kernel(mode);
1339 switch (mode)
1340 {
1341 case MODE_TUNNEL:
1342 sa->flags |= XFRM_STATE_AF_UNSPEC;
1343 break;
1344 case MODE_BEET:
1345 case MODE_TRANSPORT:
1346 if (original_mode == MODE_TUNNEL)
1347 { /* don't install selectors for switched SAs. because only one
1348 * selector can be installed other traffic would get dropped */
1349 break;
1350 }
1351 if (data->src_ts->get_first(data->src_ts,
1352 (void**)&first_src_ts) == SUCCESS &&
1353 data->dst_ts->get_first(data->dst_ts,
1354 (void**)&first_dst_ts) == SUCCESS)
1355 {
1356 sa->sel = ts2selector(first_src_ts, first_dst_ts,
1357 data->interface);
1358 if (!this->proto_port_transport)
1359 {
1360 /* don't install proto/port on SA. This would break
1361 * potential secondary SAs for the same address using a
1362 * different prot/port. */
1363 sa->sel.proto = 0;
1364 sa->sel.dport = sa->sel.dport_mask = 0;
1365 sa->sel.sport = sa->sel.sport_mask = 0;
1366 }
1367 }
1368 break;
1369 default:
1370 break;
1371 }
1372 if (id->proto == IPPROTO_AH && sa->family == AF_INET)
1373 { /* use alignment to 4 bytes for IPv4 instead of the incorrect 8 byte
1374 * alignment that's used by default but is only valid for IPv6 */
1375 sa->flags |= XFRM_STATE_ALIGN4;
1376 }
1377
1378 sa->reqid = data->reqid;
1379 sa->lft.soft_byte_limit = XFRM_LIMIT(data->lifetime->bytes.rekey);
1380 sa->lft.hard_byte_limit = XFRM_LIMIT(data->lifetime->bytes.life);
1381 sa->lft.soft_packet_limit = XFRM_LIMIT(data->lifetime->packets.rekey);
1382 sa->lft.hard_packet_limit = XFRM_LIMIT(data->lifetime->packets.life);
1383 /* we use lifetimes since added, not since used */
1384 sa->lft.soft_add_expires_seconds = data->lifetime->time.rekey;
1385 sa->lft.hard_add_expires_seconds = data->lifetime->time.life;
1386 sa->lft.soft_use_expires_seconds = 0;
1387 sa->lft.hard_use_expires_seconds = 0;
1388
1389 switch (data->enc_alg)
1390 {
1391 case ENCR_UNDEFINED:
1392 /* no encryption */
1393 break;
1394 case ENCR_AES_CCM_ICV16:
1395 case ENCR_AES_GCM_ICV16:
1396 case ENCR_NULL_AUTH_AES_GMAC:
1397 case ENCR_CAMELLIA_CCM_ICV16:
1398 case ENCR_CHACHA20_POLY1305:
1399 icv_size += 32;
1400 /* FALL */
1401 case ENCR_AES_CCM_ICV12:
1402 case ENCR_AES_GCM_ICV12:
1403 case ENCR_CAMELLIA_CCM_ICV12:
1404 icv_size += 32;
1405 /* FALL */
1406 case ENCR_AES_CCM_ICV8:
1407 case ENCR_AES_GCM_ICV8:
1408 case ENCR_CAMELLIA_CCM_ICV8:
1409 {
1410 struct xfrm_algo_aead *algo;
1411
1412 alg_name = lookup_algorithm(ENCRYPTION_ALGORITHM, data->enc_alg);
1413 if (alg_name == NULL)
1414 {
1415 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1416 encryption_algorithm_names, data->enc_alg);
1417 goto failed;
1418 }
1419 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1420 encryption_algorithm_names, data->enc_alg,
1421 data->enc_key.len * 8);
1422
1423 algo = netlink_reserve(hdr, sizeof(request), XFRMA_ALG_AEAD,
1424 sizeof(*algo) + data->enc_key.len);
1425 if (!algo)
1426 {
1427 goto failed;
1428 }
1429 algo->alg_key_len = data->enc_key.len * 8;
1430 algo->alg_icv_len = icv_size;
1431 strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
1432 algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
1433 memcpy(algo->alg_key, data->enc_key.ptr, data->enc_key.len);
1434 break;
1435 }
1436 default:
1437 {
1438 struct xfrm_algo *algo;
1439
1440 alg_name = lookup_algorithm(ENCRYPTION_ALGORITHM, data->enc_alg);
1441 if (alg_name == NULL)
1442 {
1443 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1444 encryption_algorithm_names, data->enc_alg);
1445 goto failed;
1446 }
1447 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1448 encryption_algorithm_names, data->enc_alg,
1449 data->enc_key.len * 8);
1450
1451 algo = netlink_reserve(hdr, sizeof(request), XFRMA_ALG_CRYPT,
1452 sizeof(*algo) + data->enc_key.len);
1453 if (!algo)
1454 {
1455 goto failed;
1456 }
1457 algo->alg_key_len = data->enc_key.len * 8;
1458 strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
1459 algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
1460 memcpy(algo->alg_key, data->enc_key.ptr, data->enc_key.len);
1461 }
1462 }
1463
1464 if (data->int_alg != AUTH_UNDEFINED)
1465 {
1466 u_int trunc_len = 0;
1467
1468 alg_name = lookup_algorithm(INTEGRITY_ALGORITHM, data->int_alg);
1469 if (alg_name == NULL)
1470 {
1471 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1472 integrity_algorithm_names, data->int_alg);
1473 goto failed;
1474 }
1475 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1476 integrity_algorithm_names, data->int_alg, data->int_key.len * 8);
1477
1478 switch (data->int_alg)
1479 {
1480 case AUTH_HMAC_MD5_128:
1481 case AUTH_HMAC_SHA2_256_128:
1482 trunc_len = 128;
1483 break;
1484 case AUTH_HMAC_SHA1_160:
1485 trunc_len = 160;
1486 break;
1487 default:
1488 break;
1489 }
1490
1491 if (trunc_len)
1492 {
1493 struct xfrm_algo_auth* algo;
1494
1495 /* the kernel uses SHA256 with 96 bit truncation by default,
1496 * use specified truncation size supported by newer kernels.
1497 * also use this for untruncated MD5 and SHA1. */
1498 algo = netlink_reserve(hdr, sizeof(request), XFRMA_ALG_AUTH_TRUNC,
1499 sizeof(*algo) + data->int_key.len);
1500 if (!algo)
1501 {
1502 goto failed;
1503 }
1504 algo->alg_key_len = data->int_key.len * 8;
1505 algo->alg_trunc_len = trunc_len;
1506 strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
1507 algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
1508 memcpy(algo->alg_key, data->int_key.ptr, data->int_key.len);
1509 }
1510 else
1511 {
1512 struct xfrm_algo* algo;
1513
1514 algo = netlink_reserve(hdr, sizeof(request), XFRMA_ALG_AUTH,
1515 sizeof(*algo) + data->int_key.len);
1516 if (!algo)
1517 {
1518 goto failed;
1519 }
1520 algo->alg_key_len = data->int_key.len * 8;
1521 strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
1522 algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
1523 memcpy(algo->alg_key, data->int_key.ptr, data->int_key.len);
1524 }
1525 }
1526
1527 if (ipcomp != IPCOMP_NONE)
1528 {
1529 struct xfrm_algo* algo;
1530
1531 alg_name = lookup_algorithm(COMPRESSION_ALGORITHM, ipcomp);
1532 if (alg_name == NULL)
1533 {
1534 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1535 ipcomp_transform_names, ipcomp);
1536 goto failed;
1537 }
1538 DBG2(DBG_KNL, " using compression algorithm %N",
1539 ipcomp_transform_names, ipcomp);
1540
1541 algo = netlink_reserve(hdr, sizeof(request), XFRMA_ALG_COMP,
1542 sizeof(*algo));
1543 if (!algo)
1544 {
1545 goto failed;
1546 }
1547 algo->alg_key_len = 0;
1548 strncpy(algo->alg_name, alg_name, sizeof(algo->alg_name));
1549 algo->alg_name[sizeof(algo->alg_name) - 1] = '\0';
1550 }
1551
1552 if (data->encap)
1553 {
1554 struct xfrm_encap_tmpl *tmpl;
1555
1556 tmpl = netlink_reserve(hdr, sizeof(request), XFRMA_ENCAP, sizeof(*tmpl));
1557 if (!tmpl)
1558 {
1559 goto failed;
1560 }
1561 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1562 tmpl->encap_sport = htons(id->src->get_port(id->src));
1563 tmpl->encap_dport = htons(id->dst->get_port(id->dst));
1564 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1565 /* encap_oa could probably be derived from the
1566 * traffic selectors [rfc4306, p39]. In the netlink kernel
1567 * implementation pluto does the same as we do here but it uses
1568 * encap_oa in the pfkey implementation.
1569 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1570 * it anyway
1571 * -> does that mean that NAT-T encap doesn't work in transport mode?
1572 * No. The reason the kernel ignores NAT-OA is that it recomputes
1573 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1574 * checks it marks them "checksum ok" so OA isn't needed. */
1575 }
1576
1577 if (!add_mark(hdr, sizeof(request), id->mark))
1578 {
1579 goto failed;
1580 }
1581
1582 if (data->tfc && id->proto == IPPROTO_ESP && mode == MODE_TUNNEL)
1583 { /* the kernel supports TFC padding only for tunnel mode ESP SAs */
1584 uint32_t *tfcpad;
1585
1586 tfcpad = netlink_reserve(hdr, sizeof(request), XFRMA_TFCPAD,
1587 sizeof(*tfcpad));
1588 if (!tfcpad)
1589 {
1590 goto failed;
1591 }
1592 *tfcpad = data->tfc;
1593 }
1594
1595 if (id->proto != IPPROTO_COMP)
1596 {
1597 /* generally, we don't need a replay window for outbound SAs, however,
1598 * when using ESN the kernel rejects the attribute if it is 0 */
1599 if (!data->inbound && data->replay_window)
1600 {
1601 data->replay_window = data->esn ? 1 : 0;
1602 }
1603 if (data->replay_window != 0 && (data->esn || data->replay_window > 32))
1604 {
1605 /* for ESN or larger replay windows we need the new
1606 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1607 struct xfrm_replay_state_esn *replay;
1608 uint32_t bmp_size;
1609
1610 bmp_size = round_up(data->replay_window, sizeof(uint32_t) * 8) / 8;
1611 replay = netlink_reserve(hdr, sizeof(request), XFRMA_REPLAY_ESN_VAL,
1612 sizeof(*replay) + bmp_size);
1613 if (!replay)
1614 {
1615 goto failed;
1616 }
1617 /* bmp_len contains number uf __u32's */
1618 replay->bmp_len = bmp_size / sizeof(uint32_t);
1619 replay->replay_window = data->replay_window;
1620 DBG2(DBG_KNL, " using replay window of %u packets",
1621 data->replay_window);
1622
1623 if (data->esn)
1624 {
1625 DBG2(DBG_KNL, " using extended sequence numbers (ESN)");
1626 sa->flags |= XFRM_STATE_ESN;
1627 }
1628 }
1629 else
1630 {
1631 DBG2(DBG_KNL, " using replay window of %u packets",
1632 data->replay_window);
1633 sa->replay_window = data->replay_window;
1634 }
1635 }
1636
1637 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1638 {
1639 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x%s", ntohl(id->spi),
1640 markstr);
1641 goto failed;
1642 }
1643
1644 status = SUCCESS;
1645
1646 failed:
1647 memwipe(&request, sizeof(request));
1648 return status;
1649 }
1650
1651 /**
1652 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1653 *
1654 * Allocates into one the replay state structure we get from the kernel.
1655 */
1656 static void get_replay_state(private_kernel_netlink_ipsec_t *this,
1657 kernel_ipsec_sa_id_t *sa,
1658 struct xfrm_replay_state_esn **replay_esn,
1659 uint32_t *replay_esn_len,
1660 struct xfrm_replay_state **replay,
1661 struct xfrm_lifetime_cur **lifetime)
1662 {
1663 netlink_buf_t request;
1664 struct nlmsghdr *hdr, *out = NULL;
1665 struct xfrm_aevent_id *out_aevent = NULL, *aevent_id;
1666 size_t len;
1667 struct rtattr *rta;
1668 size_t rtasize;
1669
1670 memset(&request, 0, sizeof(request));
1671
1672 DBG2(DBG_KNL, "querying replay state from SAD entry with SPI %.8x",
1673 ntohl(sa->spi));
1674
1675 hdr = &request.hdr;
1676 hdr->nlmsg_flags = NLM_F_REQUEST;
1677 hdr->nlmsg_type = XFRM_MSG_GETAE;
1678 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1679
1680 aevent_id = NLMSG_DATA(hdr);
1681 aevent_id->flags = XFRM_AE_RVAL;
1682
1683 host2xfrm(sa->dst, &aevent_id->sa_id.daddr);
1684 aevent_id->sa_id.spi = sa->spi;
1685 aevent_id->sa_id.proto = sa->proto;
1686 aevent_id->sa_id.family = sa->dst->get_family(sa->dst);
1687
1688 if (!add_mark(hdr, sizeof(request), sa->mark))
1689 {
1690 return;
1691 }
1692
1693 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1694 {
1695 hdr = out;
1696 while (NLMSG_OK(hdr, len))
1697 {
1698 switch (hdr->nlmsg_type)
1699 {
1700 case XFRM_MSG_NEWAE:
1701 {
1702 out_aevent = NLMSG_DATA(hdr);
1703 break;
1704 }
1705 case NLMSG_ERROR:
1706 {
1707 struct nlmsgerr *err = NLMSG_DATA(hdr);
1708 DBG1(DBG_KNL, "querying replay state from SAD entry "
1709 "failed: %s (%d)", strerror(-err->error), -err->error);
1710 break;
1711 }
1712 default:
1713 hdr = NLMSG_NEXT(hdr, len);
1714 continue;
1715 case NLMSG_DONE:
1716 break;
1717 }
1718 break;
1719 }
1720 }
1721
1722 if (out_aevent)
1723 {
1724 rta = XFRM_RTA(out, struct xfrm_aevent_id);
1725 rtasize = XFRM_PAYLOAD(out, struct xfrm_aevent_id);
1726 while (RTA_OK(rta, rtasize))
1727 {
1728 if (rta->rta_type == XFRMA_LTIME_VAL &&
1729 RTA_PAYLOAD(rta) == sizeof(**lifetime))
1730 {
1731 free(*lifetime);
1732 *lifetime = malloc(RTA_PAYLOAD(rta));
1733 memcpy(*lifetime, RTA_DATA(rta), RTA_PAYLOAD(rta));
1734 }
1735 if (rta->rta_type == XFRMA_REPLAY_VAL &&
1736 RTA_PAYLOAD(rta) == sizeof(**replay))
1737 {
1738 free(*replay);
1739 *replay = malloc(RTA_PAYLOAD(rta));
1740 memcpy(*replay, RTA_DATA(rta), RTA_PAYLOAD(rta));
1741 }
1742 if (rta->rta_type == XFRMA_REPLAY_ESN_VAL &&
1743 RTA_PAYLOAD(rta) >= sizeof(**replay_esn))
1744 {
1745 free(*replay_esn);
1746 *replay_esn = malloc(RTA_PAYLOAD(rta));
1747 *replay_esn_len = RTA_PAYLOAD(rta);
1748 memcpy(*replay_esn, RTA_DATA(rta), RTA_PAYLOAD(rta));
1749 }
1750 rta = RTA_NEXT(rta, rtasize);
1751 }
1752 }
1753 free(out);
1754 }
1755
1756 METHOD(kernel_ipsec_t, query_sa, status_t,
1757 private_kernel_netlink_ipsec_t *this, kernel_ipsec_sa_id_t *id,
1758 kernel_ipsec_query_sa_t *data, uint64_t *bytes, uint64_t *packets,
1759 time_t *time)
1760 {
1761 netlink_buf_t request;
1762 struct nlmsghdr *out = NULL, *hdr;
1763 struct xfrm_usersa_id *sa_id;
1764 struct xfrm_usersa_info *sa = NULL;
1765 status_t status = FAILED;
1766 size_t len;
1767 char markstr[32] = "";
1768
1769 memset(&request, 0, sizeof(request));
1770 format_mark(markstr, sizeof(markstr), id->mark);
1771
1772 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x%s", ntohl(id->spi),
1773 markstr);
1774
1775 hdr = &request.hdr;
1776 hdr->nlmsg_flags = NLM_F_REQUEST;
1777 hdr->nlmsg_type = XFRM_MSG_GETSA;
1778 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1779
1780 sa_id = NLMSG_DATA(hdr);
1781 host2xfrm(id->dst, &sa_id->daddr);
1782 sa_id->spi = id->spi;
1783 sa_id->proto = id->proto;
1784 sa_id->family = id->dst->get_family(id->dst);
1785
1786 if (!add_mark(hdr, sizeof(request), id->mark))
1787 {
1788 return FAILED;
1789 }
1790
1791 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1792 {
1793 hdr = out;
1794 while (NLMSG_OK(hdr, len))
1795 {
1796 switch (hdr->nlmsg_type)
1797 {
1798 case XFRM_MSG_NEWSA:
1799 {
1800 sa = NLMSG_DATA(hdr);
1801 break;
1802 }
1803 case NLMSG_ERROR:
1804 {
1805 struct nlmsgerr *err = NLMSG_DATA(hdr);
1806
1807 DBG1(DBG_KNL, "querying SAD entry with SPI %.8x%s failed: "
1808 "%s (%d)", ntohl(id->spi), markstr,
1809 strerror(-err->error), -err->error);
1810 break;
1811 }
1812 default:
1813 hdr = NLMSG_NEXT(hdr, len);
1814 continue;
1815 case NLMSG_DONE:
1816 break;
1817 }
1818 break;
1819 }
1820 }
1821
1822 if (sa == NULL)
1823 {
1824 DBG2(DBG_KNL, "unable to query SAD entry with SPI %.8x%s",
1825 ntohl(id->spi), markstr);
1826 }
1827 else
1828 {
1829 if (bytes)
1830 {
1831 *bytes = sa->curlft.bytes;
1832 }
1833 if (packets)
1834 {
1835 *packets = sa->curlft.packets;
1836 }
1837 if (time)
1838 { /* curlft contains an "use" time, but that contains a timestamp
1839 * of the first use, not the last. Last use time must be queried
1840 * on the policy on Linux */
1841 *time = 0;
1842 }
1843 status = SUCCESS;
1844 }
1845 memwipe(out, len);
1846 free(out);
1847 return status;
1848 }
1849
1850 METHOD(kernel_ipsec_t, del_sa, status_t,
1851 private_kernel_netlink_ipsec_t *this, kernel_ipsec_sa_id_t *id,
1852 kernel_ipsec_del_sa_t *data)
1853 {
1854 netlink_buf_t request;
1855 struct nlmsghdr *hdr;
1856 struct xfrm_usersa_id *sa_id;
1857 char markstr[32] = "";
1858
1859 /* if IPComp was used, we first delete the additional IPComp SA */
1860 if (data->cpi)
1861 {
1862 kernel_ipsec_sa_id_t ipcomp_id = {
1863 .src = id->src,
1864 .dst = id->dst,
1865 .spi = htonl(ntohs(data->cpi)),
1866 .proto = IPPROTO_COMP,
1867 .mark = id->mark,
1868 };
1869 kernel_ipsec_del_sa_t ipcomp = {};
1870 del_sa(this, &ipcomp_id, &ipcomp);
1871 }
1872
1873 memset(&request, 0, sizeof(request));
1874 format_mark(markstr, sizeof(markstr), id->mark);
1875
1876 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x%s", ntohl(id->spi),
1877 markstr);
1878
1879 hdr = &request.hdr;
1880 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1881 hdr->nlmsg_type = XFRM_MSG_DELSA;
1882 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1883
1884 sa_id = NLMSG_DATA(hdr);
1885 host2xfrm(id->dst, &sa_id->daddr);
1886 sa_id->spi = id->spi;
1887 sa_id->proto = id->proto;
1888 sa_id->family = id->dst->get_family(id->dst);
1889
1890 if (!add_mark(hdr, sizeof(request), id->mark))
1891 {
1892 return FAILED;
1893 }
1894
1895 switch (this->socket_xfrm->send_ack(this->socket_xfrm, hdr))
1896 {
1897 case SUCCESS:
1898 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x%s",
1899 ntohl(id->spi), markstr);
1900 return SUCCESS;
1901 case NOT_FOUND:
1902 return NOT_FOUND;
1903 default:
1904 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x%s",
1905 ntohl(id->spi), markstr);
1906 return FAILED;
1907 }
1908 }
1909
1910 METHOD(kernel_ipsec_t, update_sa, status_t,
1911 private_kernel_netlink_ipsec_t *this, kernel_ipsec_sa_id_t *id,
1912 kernel_ipsec_update_sa_t *data)
1913 {
1914 netlink_buf_t request;
1915 struct nlmsghdr *hdr, *out = NULL;
1916 struct xfrm_usersa_id *sa_id;
1917 struct xfrm_usersa_info *out_sa = NULL, *sa;
1918 size_t len;
1919 struct rtattr *rta;
1920 size_t rtasize;
1921 struct xfrm_encap_tmpl* tmpl = NULL;
1922 struct xfrm_replay_state *replay = NULL;
1923 struct xfrm_replay_state_esn *replay_esn = NULL;
1924 struct xfrm_lifetime_cur *lifetime = NULL;
1925 uint32_t replay_esn_len = 0;
1926 kernel_ipsec_del_sa_t del = { 0 };
1927 status_t status = FAILED;
1928 char markstr[32] = "";
1929
1930 /* if IPComp is used, we first update the IPComp SA */
1931 if (data->cpi)
1932 {
1933 kernel_ipsec_sa_id_t ipcomp_id = {
1934 .src = id->src,
1935 .dst = id->dst,
1936 .spi = htonl(ntohs(data->cpi)),
1937 .proto = IPPROTO_COMP,
1938 .mark = id->mark,
1939 };
1940 kernel_ipsec_update_sa_t ipcomp = {
1941 .new_src = data->new_src,
1942 .new_dst = data->new_dst,
1943 };
1944 update_sa(this, &ipcomp_id, &ipcomp);
1945 }
1946
1947 memset(&request, 0, sizeof(request));
1948 format_mark(markstr, sizeof(markstr), id->mark);
1949
1950 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x%s for update",
1951 ntohl(id->spi), markstr);
1952
1953 /* query the existing SA first */
1954 hdr = &request.hdr;
1955 hdr->nlmsg_flags = NLM_F_REQUEST;
1956 hdr->nlmsg_type = XFRM_MSG_GETSA;
1957 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1958
1959 sa_id = NLMSG_DATA(hdr);
1960 host2xfrm(id->dst, &sa_id->daddr);
1961 sa_id->spi = id->spi;
1962 sa_id->proto = id->proto;
1963 sa_id->family = id->dst->get_family(id->dst);
1964
1965 if (!add_mark(hdr, sizeof(request), id->mark))
1966 {
1967 return FAILED;
1968 }
1969
1970 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1971 {
1972 hdr = out;
1973 while (NLMSG_OK(hdr, len))
1974 {
1975 switch (hdr->nlmsg_type)
1976 {
1977 case XFRM_MSG_NEWSA:
1978 {
1979 out_sa = NLMSG_DATA(hdr);
1980 break;
1981 }
1982 case NLMSG_ERROR:
1983 {
1984 struct nlmsgerr *err = NLMSG_DATA(hdr);
1985 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1986 strerror(-err->error), -err->error);
1987 break;
1988 }
1989 default:
1990 hdr = NLMSG_NEXT(hdr, len);
1991 continue;
1992 case NLMSG_DONE:
1993 break;
1994 }
1995 break;
1996 }
1997 }
1998 if (out_sa == NULL)
1999 {
2000 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x%s",
2001 ntohl(id->spi), markstr);
2002 goto failed;
2003 }
2004
2005 get_replay_state(this, id, &replay_esn, &replay_esn_len, &replay,
2006 &lifetime);
2007
2008 /* delete the old SA (without affecting the IPComp SA) */
2009 if (del_sa(this, id, &del) != SUCCESS)
2010 {
2011 DBG1(DBG_KNL, "unable to delete old SAD entry with SPI %.8x%s",
2012 ntohl(id->spi), markstr);
2013 goto failed;
2014 }
2015
2016 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x%s from %#H..%#H to "
2017 "%#H..%#H", ntohl(id->spi), markstr, id->src, id->dst, data->new_src,
2018 data->new_dst);
2019 /* copy over the SA from out to request */
2020 hdr = &request.hdr;
2021 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2022 hdr->nlmsg_type = XFRM_MSG_NEWSA;
2023 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
2024 sa = NLMSG_DATA(hdr);
2025 memcpy(sa, NLMSG_DATA(out), sizeof(struct xfrm_usersa_info));
2026 sa->family = data->new_dst->get_family(data->new_dst);
2027
2028 if (!id->src->ip_equals(id->src, data->new_src))
2029 {
2030 host2xfrm(data->new_src, &sa->saddr);
2031 }
2032 if (!id->dst->ip_equals(id->dst, data->new_dst))
2033 {
2034 host2xfrm(data->new_dst, &sa->id.daddr);
2035 }
2036
2037 rta = XFRM_RTA(out, struct xfrm_usersa_info);
2038 rtasize = XFRM_PAYLOAD(out, struct xfrm_usersa_info);
2039 while (RTA_OK(rta, rtasize))
2040 {
2041 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
2042 if (rta->rta_type != XFRMA_ENCAP || data->new_encap)
2043 {
2044 if (rta->rta_type == XFRMA_ENCAP)
2045 { /* update encap tmpl */
2046 tmpl = RTA_DATA(rta);
2047 tmpl->encap_sport = ntohs(data->new_src->get_port(data->new_src));
2048 tmpl->encap_dport = ntohs(data->new_dst->get_port(data->new_dst));
2049 }
2050 netlink_add_attribute(hdr, rta->rta_type,
2051 chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)),
2052 sizeof(request));
2053 }
2054 rta = RTA_NEXT(rta, rtasize);
2055 }
2056
2057 if (tmpl == NULL && data->new_encap)
2058 { /* add tmpl if we are enabling it */
2059 tmpl = netlink_reserve(hdr, sizeof(request), XFRMA_ENCAP, sizeof(*tmpl));
2060 if (!tmpl)
2061 {
2062 goto failed;
2063 }
2064 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
2065 tmpl->encap_sport = ntohs(data->new_src->get_port(data->new_src));
2066 tmpl->encap_dport = ntohs(data->new_dst->get_port(data->new_dst));
2067 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
2068 }
2069
2070 if (replay_esn)
2071 {
2072 struct xfrm_replay_state_esn *state;
2073
2074 state = netlink_reserve(hdr, sizeof(request), XFRMA_REPLAY_ESN_VAL,
2075 replay_esn_len);
2076 if (!state)
2077 {
2078 goto failed;
2079 }
2080 memcpy(state, replay_esn, replay_esn_len);
2081 }
2082 else if (replay)
2083 {
2084 struct xfrm_replay_state *state;
2085
2086 state = netlink_reserve(hdr, sizeof(request), XFRMA_REPLAY_VAL,
2087 sizeof(*state));
2088 if (!state)
2089 {
2090 goto failed;
2091 }
2092 memcpy(state, replay, sizeof(*state));
2093 }
2094 else
2095 {
2096 DBG1(DBG_KNL, "unable to copy replay state from old SAD entry with "
2097 "SPI %.8x%s", ntohl(id->spi), markstr);
2098 }
2099 if (lifetime)
2100 {
2101 struct xfrm_lifetime_cur *state;
2102
2103 state = netlink_reserve(hdr, sizeof(request), XFRMA_LTIME_VAL,
2104 sizeof(*state));
2105 if (!state)
2106 {
2107 goto failed;
2108 }
2109 memcpy(state, lifetime, sizeof(*state));
2110 }
2111 else
2112 {
2113 DBG1(DBG_KNL, "unable to copy usage stats from old SAD entry with "
2114 "SPI %.8x%s", ntohl(id->spi), markstr);
2115 }
2116
2117 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
2118 {
2119 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x%s",
2120 ntohl(id->spi), markstr);
2121 goto failed;
2122 }
2123
2124 status = SUCCESS;
2125 failed:
2126 free(replay);
2127 free(replay_esn);
2128 free(lifetime);
2129 memwipe(out, len);
2130 memwipe(&request, sizeof(request));
2131 free(out);
2132
2133 return status;
2134 }
2135
2136 METHOD(kernel_ipsec_t, flush_sas, status_t,
2137 private_kernel_netlink_ipsec_t *this)
2138 {
2139 netlink_buf_t request;
2140 struct nlmsghdr *hdr;
2141 struct xfrm_usersa_flush *flush;
2142 struct {
2143 uint8_t proto;
2144 char *name;
2145 } protos[] = {
2146 { IPPROTO_AH, "AH" },
2147 { IPPROTO_ESP, "ESP" },
2148 { IPPROTO_COMP, "IPComp" },
2149 };
2150 int i;
2151
2152 memset(&request, 0, sizeof(request));
2153
2154 hdr = &request.hdr;
2155 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2156 hdr->nlmsg_type = XFRM_MSG_FLUSHSA;
2157 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
2158
2159 flush = NLMSG_DATA(hdr);
2160
2161 for (i = 0; i < countof(protos); i++)
2162 {
2163 DBG2(DBG_KNL, "flushing all %s SAD entries", protos[i].name);
2164
2165 flush->proto = protos[i].proto;
2166
2167 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
2168 {
2169 DBG1(DBG_KNL, "unable to flush %s SAD entries", protos[i].name);
2170 return FAILED;
2171 }
2172 }
2173 return SUCCESS;
2174 }
2175
2176 /**
2177 * Unlock the mutex and signal waiting threads
2178 */
2179 static void policy_change_done(private_kernel_netlink_ipsec_t *this,
2180 policy_entry_t *policy)
2181 {
2182 policy->working = FALSE;
2183 if (policy->waiting)
2184 { /* don't need to wake threads waiting for other policies */
2185 this->condvar->broadcast(this->condvar);
2186 }
2187 this->mutex->unlock(this->mutex);
2188 }
2189
2190 /**
2191 * Install a route for the given policy if enabled and required
2192 */
2193 static void install_route(private_kernel_netlink_ipsec_t *this,
2194 policy_entry_t *policy, policy_sa_t *mapping, ipsec_sa_t *ipsec)
2195 {
2196 policy_sa_out_t *out = (policy_sa_out_t*)mapping;
2197 route_entry_t *route;
2198 host_t *iface;
2199
2200 INIT(route,
2201 .prefixlen = policy->sel.prefixlen_d,
2202 );
2203
2204 if (charon->kernel->get_address_by_ts(charon->kernel, out->src_ts,
2205 &route->src_ip, NULL) == SUCCESS)
2206 {
2207 if (!ipsec->dst->is_anyaddr(ipsec->dst))
2208 {
2209 route->gateway = charon->kernel->get_nexthop(charon->kernel,
2210 ipsec->dst, -1, ipsec->src,
2211 &route->if_name);
2212 }
2213 else
2214 { /* for shunt policies */
2215 iface = xfrm2host(policy->sel.family, &policy->sel.daddr, 0);
2216 route->gateway = charon->kernel->get_nexthop(charon->kernel,
2217 iface, policy->sel.prefixlen_d,
2218 route->src_ip, &route->if_name);
2219 iface->destroy(iface);
2220 }
2221 route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
2222 memcpy(route->dst_net.ptr, &policy->sel.daddr, route->dst_net.len);
2223
2224 /* get the interface to install the route for, if we haven't one yet.
2225 * If we have a local address, use it. Otherwise (for shunt policies)
2226 * use the route's source address. */
2227 if (!route->if_name)
2228 {
2229 iface = ipsec->src;
2230 if (iface->is_anyaddr(iface))
2231 {
2232 iface = route->src_ip;
2233 }
2234 if (!charon->kernel->get_interface(charon->kernel, iface,
2235 &route->if_name))
2236 {
2237 route_entry_destroy(route);
2238 return;
2239 }
2240 }
2241 if (policy->route)
2242 {
2243 route_entry_t *old = policy->route;
2244 if (route_entry_equals(old, route))
2245 {
2246 route_entry_destroy(route);
2247 return;
2248 }
2249 /* uninstall previously installed route */
2250 if (charon->kernel->del_route(charon->kernel, old->dst_net,
2251 old->prefixlen, old->gateway,
2252 old->src_ip, old->if_name) != SUCCESS)
2253 {
2254 DBG1(DBG_KNL, "error uninstalling route installed with policy "
2255 "%R === %R %N", out->src_ts, out->dst_ts, policy_dir_names,
2256 policy->direction);
2257 }
2258 route_entry_destroy(old);
2259 policy->route = NULL;
2260 }
2261
2262 DBG2(DBG_KNL, "installing route: %R via %H src %H dev %s", out->dst_ts,
2263 route->gateway, route->src_ip, route->if_name);
2264 switch (charon->kernel->add_route(charon->kernel, route->dst_net,
2265 route->prefixlen, route->gateway,
2266 route->src_ip, route->if_name))
2267 {
2268 default:
2269 DBG1(DBG_KNL, "unable to install source route for %H",
2270 route->src_ip);
2271 /* FALL */
2272 case ALREADY_DONE:
2273 /* route exists, do not uninstall */
2274 route_entry_destroy(route);
2275 break;
2276 case SUCCESS:
2277 /* cache the installed route */
2278 policy->route = route;
2279 break;
2280 }
2281 }
2282 else
2283 {
2284 free(route);
2285 }
2286 }
2287
2288 /**
2289 * Add or update a policy in the kernel.
2290 *
2291 * Note: The mutex has to be locked when entering this function
2292 * and is unlocked here in any case.
2293 */
2294 static status_t add_policy_internal(private_kernel_netlink_ipsec_t *this,
2295 policy_entry_t *policy, policy_sa_t *mapping, bool update)
2296 {
2297 netlink_buf_t request;
2298 policy_entry_t clone;
2299 ipsec_sa_t *ipsec = mapping->sa;
2300 struct xfrm_userpolicy_info *policy_info;
2301 struct nlmsghdr *hdr;
2302 status_t status;
2303 int i;
2304
2305 /* clone the policy so we are able to check it out again later */
2306 memcpy(&clone, policy, sizeof(policy_entry_t));
2307
2308 memset(&request, 0, sizeof(request));
2309 hdr = &request.hdr;
2310 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2311 hdr->nlmsg_type = update ? XFRM_MSG_UPDPOLICY : XFRM_MSG_NEWPOLICY;
2312 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
2313
2314 policy_info = NLMSG_DATA(hdr);
2315 policy_info->sel = policy->sel;
2316 policy_info->dir = policy->direction;
2317
2318 /* calculate priority based on selector size, small size = high prio */
2319 policy_info->priority = mapping->priority;
2320 policy_info->action = mapping->type != POLICY_DROP ? XFRM_POLICY_ALLOW
2321 : XFRM_POLICY_BLOCK;
2322 policy_info->share = XFRM_SHARE_ANY;
2323
2324 /* policies don't expire */
2325 policy_info->lft.soft_byte_limit = XFRM_INF;
2326 policy_info->lft.soft_packet_limit = XFRM_INF;
2327 policy_info->lft.hard_byte_limit = XFRM_INF;
2328 policy_info->lft.hard_packet_limit = XFRM_INF;
2329 policy_info->lft.soft_add_expires_seconds = 0;
2330 policy_info->lft.hard_add_expires_seconds = 0;
2331 policy_info->lft.soft_use_expires_seconds = 0;
2332 policy_info->lft.hard_use_expires_seconds = 0;
2333
2334 if (mapping->type == POLICY_IPSEC && ipsec->cfg.reqid)
2335 {
2336 struct xfrm_user_tmpl *tmpl;
2337 struct {
2338 uint8_t proto;
2339 bool use;
2340 } protos[] = {
2341 { IPPROTO_COMP, ipsec->cfg.ipcomp.transform != IPCOMP_NONE },
2342 { IPPROTO_ESP, ipsec->cfg.esp.use },
2343 { IPPROTO_AH, ipsec->cfg.ah.use },
2344 };
2345 ipsec_mode_t proto_mode = ipsec->cfg.mode;
2346 int count = 0;
2347
2348 for (i = 0; i < countof(protos); i++)
2349 {
2350 if (protos[i].use)
2351 {
2352 count++;
2353 }
2354 }
2355 tmpl = netlink_reserve(hdr, sizeof(request), XFRMA_TMPL,
2356 count * sizeof(*tmpl));
2357 if (!tmpl)
2358 {
2359 policy_change_done(this, policy);
2360 return FAILED;
2361 }
2362
2363 for (i = 0; i < countof(protos); i++)
2364 {
2365 if (!protos[i].use)
2366 {
2367 continue;
2368 }
2369 tmpl->reqid = ipsec->cfg.reqid;
2370 tmpl->id.proto = protos[i].proto;
2371 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
2372 tmpl->mode = mode2kernel(proto_mode);
2373 tmpl->optional = protos[i].proto == IPPROTO_COMP &&
2374 policy->direction != POLICY_OUT;
2375 tmpl->family = ipsec->src->get_family(ipsec->src);
2376
2377 if (proto_mode == MODE_TUNNEL || proto_mode == MODE_BEET)
2378 { /* only for tunnel mode */
2379 host2xfrm(ipsec->src, &tmpl->saddr);
2380 host2xfrm(ipsec->dst, &tmpl->id.daddr);
2381 }
2382
2383 tmpl++;
2384
2385 /* use transport mode for other SAs */
2386 proto_mode = MODE_TRANSPORT;
2387 }
2388 }
2389
2390 if (!add_mark(hdr, sizeof(request), ipsec->mark))
2391 {
2392 policy_change_done(this, policy);
2393 return FAILED;
2394 }
2395 this->mutex->unlock(this->mutex);
2396
2397 status = this->socket_xfrm->send_ack(this->socket_xfrm, hdr);
2398 if (status == ALREADY_DONE && !update)
2399 {
2400 DBG1(DBG_KNL, "policy already exists, try to update it");
2401 hdr->nlmsg_type = XFRM_MSG_UPDPOLICY;
2402 status = this->socket_xfrm->send_ack(this->socket_xfrm, hdr);
2403 }
2404
2405 this->mutex->lock(this->mutex);
2406 if (status != SUCCESS)
2407 {
2408 policy_change_done(this, policy);
2409 return FAILED;
2410 }
2411 /* install a route, if:
2412 * - this is an outbound policy (to just get one for each child)
2413 * - routing is not disabled via strongswan.conf
2414 * - the selector is not for a specific protocol/port
2415 * - we are in tunnel/BEET mode or install a bypass policy
2416 */
2417 if (policy->direction == POLICY_OUT && this->install_routes &&
2418 !policy->sel.proto && !policy->sel.dport && !policy->sel.sport)
2419 {
2420 if (mapping->type == POLICY_PASS ||
2421 (mapping->type == POLICY_IPSEC && ipsec->cfg.mode != MODE_TRANSPORT))
2422 {
2423 install_route(this, policy, mapping, ipsec);
2424 }
2425 }
2426 policy_change_done(this, policy);
2427 return SUCCESS;
2428 }
2429
2430 METHOD(kernel_ipsec_t, add_policy, status_t,
2431 private_kernel_netlink_ipsec_t *this, kernel_ipsec_policy_id_t *id,
2432 kernel_ipsec_manage_policy_t *data)
2433 {
2434 policy_entry_t *policy, *current;
2435 policy_sa_t *assigned_sa, *current_sa;
2436 enumerator_t *enumerator;
2437 bool found = FALSE, update = TRUE;
2438 char markstr[32] = "";
2439 uint32_t cur_priority = 0;
2440 int use_count;
2441
2442 /* create a policy */
2443 INIT(policy,
2444 .sel = ts2selector(id->src_ts, id->dst_ts, id->interface),
2445 .mark = id->mark.value & id->mark.mask,
2446 .direction = id->dir,
2447 .reqid = data->sa->reqid,
2448 );
2449 format_mark(markstr, sizeof(markstr), id->mark);
2450
2451 /* find the policy, which matches EXACTLY */
2452 this->mutex->lock(this->mutex);
2453 current = this->policies->get(this->policies, policy);
2454 if (current)
2455 {
2456 if (current->reqid && data->sa->reqid &&
2457 current->reqid != data->sa->reqid)
2458 {
2459 DBG1(DBG_CFG, "unable to install policy %R === %R %N%s for reqid "
2460 "%u, the same policy for reqid %u exists",
2461 id->src_ts, id->dst_ts, policy_dir_names, id->dir, markstr,
2462 data->sa->reqid, current->reqid);
2463 policy_entry_destroy(this, policy);
2464 this->mutex->unlock(this->mutex);
2465 return INVALID_STATE;
2466 }
2467 /* use existing policy */
2468 DBG2(DBG_KNL, "policy %R === %R %N%s already exists, increasing "
2469 "refcount", id->src_ts, id->dst_ts, policy_dir_names, id->dir,
2470 markstr);
2471 policy_entry_destroy(this, policy);
2472 policy = current;
2473 found = TRUE;
2474
2475 policy->waiting++;
2476 while (policy->working)
2477 {
2478 this->condvar->wait(this->condvar, this->mutex);
2479 }
2480 policy->waiting--;
2481 policy->working = TRUE;
2482 }
2483 else
2484 { /* use the new one, if we have no such policy */
2485 policy->used_by = linked_list_create();
2486 this->policies->put(this->policies, policy, policy);
2487 }
2488
2489 /* cache the assigned IPsec SA */
2490 assigned_sa = policy_sa_create(this, id->dir, data->type, data->src,
2491 data->dst, id->src_ts, id->dst_ts, id->mark, data->sa);
2492 assigned_sa->auto_priority = get_priority(policy, data->prio, id->interface);
2493 assigned_sa->priority = this->get_priority ? this->get_priority(id, data)
2494 : data->manual_prio;
2495 assigned_sa->priority = assigned_sa->priority ?: assigned_sa->auto_priority;
2496
2497 /* insert the SA according to its priority */
2498 enumerator = policy->used_by->create_enumerator(policy->used_by);
2499 while (enumerator->enumerate(enumerator, (void**)&current_sa))
2500 {
2501 if (current_sa->priority > assigned_sa->priority)
2502 {
2503 break;
2504 }
2505 if (current_sa->priority == assigned_sa->priority)
2506 {
2507 /* in case of equal manual prios order SAs by automatic priority */
2508 if (current_sa->auto_priority > assigned_sa->auto_priority)
2509 {
2510 break;
2511 }
2512 /* prefer SAs with a reqid over those without */
2513 if (current_sa->auto_priority == assigned_sa->auto_priority &&
2514 (!current_sa->sa->cfg.reqid || assigned_sa->sa->cfg.reqid))
2515 {
2516 break;
2517 }
2518 }
2519 if (update)
2520 {
2521 cur_priority = current_sa->priority;
2522 update = FALSE;
2523 }
2524 }
2525 policy->used_by->insert_before(policy->used_by, enumerator, assigned_sa);
2526 enumerator->destroy(enumerator);
2527
2528 use_count = policy->used_by->get_count(policy->used_by);
2529 if (!update)
2530 { /* we don't update the policy if the priority is lower than that of
2531 * the currently installed one */
2532 policy_change_done(this, policy);
2533 DBG2(DBG_KNL, "not updating policy %R === %R %N%s [priority %u,"
2534 "refcount %d]", id->src_ts, id->dst_ts, policy_dir_names,
2535 id->dir, markstr, cur_priority, use_count);
2536 return SUCCESS;
2537 }
2538 policy->reqid = assigned_sa->sa->cfg.reqid;
2539
2540 if (this->policy_update)
2541 {
2542 found = TRUE;
2543 }
2544
2545 DBG2(DBG_KNL, "%s policy %R === %R %N%s [priority %u, refcount %d]",
2546 found ? "updating" : "adding", id->src_ts, id->dst_ts,
2547 policy_dir_names, id->dir, markstr, assigned_sa->priority, use_count);
2548
2549 if (add_policy_internal(this, policy, assigned_sa, found) != SUCCESS)
2550 {
2551 DBG1(DBG_KNL, "unable to %s policy %R === %R %N%s",
2552 found ? "update" : "add", id->src_ts, id->dst_ts,
2553 policy_dir_names, id->dir, markstr);
2554 return FAILED;
2555 }
2556 return SUCCESS;
2557 }
2558
2559 METHOD(kernel_ipsec_t, query_policy, status_t,
2560 private_kernel_netlink_ipsec_t *this, kernel_ipsec_policy_id_t *id,
2561 kernel_ipsec_query_policy_t *data, time_t *use_time)
2562 {
2563 netlink_buf_t request;
2564 struct nlmsghdr *out = NULL, *hdr;
2565 struct xfrm_userpolicy_id *policy_id;
2566 struct xfrm_userpolicy_info *policy = NULL;
2567 size_t len;
2568 char markstr[32] = "";
2569
2570 memset(&request, 0, sizeof(request));
2571 format_mark(markstr, sizeof(markstr), id->mark);
2572
2573 DBG2(DBG_KNL, "querying policy %R === %R %N%s", id->src_ts, id->dst_ts,
2574 policy_dir_names, id->dir, markstr);
2575
2576 hdr = &request.hdr;
2577 hdr->nlmsg_flags = NLM_F_REQUEST;
2578 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
2579 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2580
2581 policy_id = NLMSG_DATA(hdr);
2582 policy_id->sel = ts2selector(id->src_ts, id->dst_ts, id->interface);
2583 policy_id->dir = id->dir;
2584
2585 if (!add_mark(hdr, sizeof(request), id->mark))
2586 {
2587 return FAILED;
2588 }
2589
2590 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
2591 {
2592 hdr = out;
2593 while (NLMSG_OK(hdr, len))
2594 {
2595 switch (hdr->nlmsg_type)
2596 {
2597 case XFRM_MSG_NEWPOLICY:
2598 {
2599 policy = NLMSG_DATA(hdr);
2600 break;
2601 }
2602 case NLMSG_ERROR:
2603 {
2604 struct nlmsgerr *err = NLMSG_DATA(hdr);
2605 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
2606 strerror(-err->error), -err->error);
2607 break;
2608 }
2609 default:
2610 hdr = NLMSG_NEXT(hdr, len);
2611 continue;
2612 case NLMSG_DONE:
2613 break;
2614 }
2615 break;
2616 }
2617 }
2618
2619 if (policy == NULL)
2620 {
2621 DBG2(DBG_KNL, "unable to query policy %R === %R %N%s", id->src_ts,
2622 id->dst_ts, policy_dir_names, id->dir, markstr);
2623 free(out);
2624 return FAILED;
2625 }
2626
2627 if (policy->curlft.use_time)
2628 {
2629 /* we need the monotonic time, but the kernel returns system time. */
2630 *use_time = time_monotonic(NULL) - (time(NULL) - policy->curlft.use_time);
2631 }
2632 else
2633 {
2634 *use_time = 0;
2635 }
2636
2637 free(out);
2638 return SUCCESS;
2639 }
2640
2641 METHOD(kernel_ipsec_t, del_policy, status_t,
2642 private_kernel_netlink_ipsec_t *this, kernel_ipsec_policy_id_t *id,
2643 kernel_ipsec_manage_policy_t *data)
2644 {
2645 policy_entry_t *current, policy;
2646 enumerator_t *enumerator;
2647 policy_sa_t *mapping;
2648 netlink_buf_t request;
2649 struct nlmsghdr *hdr;
2650 struct xfrm_userpolicy_id *policy_id;
2651 bool is_installed = TRUE;
2652 uint32_t priority, auto_priority, cur_priority;
2653 ipsec_sa_t assigned_sa = {
2654 .src = data->src,
2655 .dst = data->dst,
2656 .mark = id->mark,
2657 .cfg = *data->sa,
2658 };
2659 char markstr[32] = "";
2660 int use_count;
2661 status_t status = SUCCESS;
2662
2663 format_mark(markstr, sizeof(markstr), id->mark);
2664
2665 DBG2(DBG_KNL, "deleting policy %R === %R %N%s", id->src_ts, id->dst_ts,
2666 policy_dir_names, id->dir, markstr);
2667
2668 /* create a policy */
2669 memset(&policy, 0, sizeof(policy_entry_t));
2670 policy.sel = ts2selector(id->src_ts, id->dst_ts, id->interface);
2671 policy.mark = id->mark.value & id->mark.mask;
2672 policy.direction = id->dir;
2673
2674 /* find the policy */
2675 this->mutex->lock(this->mutex);
2676 current = this->policies->get(this->policies, &policy);
2677 if (!current)
2678 {
2679 DBG1(DBG_KNL, "deleting policy %R === %R %N%s failed, not found",
2680 id->src_ts, id->dst_ts, policy_dir_names, id->dir, markstr);
2681 this->mutex->unlock(this->mutex);
2682 return NOT_FOUND;
2683 }
2684 current->waiting++;
2685 while (current->working)
2686 {
2687 this->condvar->wait(this->condvar, this->mutex);
2688 }
2689 current->working = TRUE;
2690 current->waiting--;
2691
2692 /* remove mapping to SA by reqid and priority */
2693 auto_priority = get_priority(current, data->prio,id->interface);
2694 priority = this->get_priority ? this->get_priority(id, data)
2695 : data->manual_prio;
2696 priority = priority ?: auto_priority;
2697
2698 enumerator = current->used_by->create_enumerator(current->used_by);
2699 while (enumerator->enumerate(enumerator, (void**)&mapping))
2700 {
2701 if (priority == mapping->priority &&
2702 auto_priority == mapping->auto_priority &&
2703 data->type == mapping->type &&
2704 ipsec_sa_equals(mapping->sa, &assigned_sa))
2705 {
2706 current->used_by->remove_at(current->used_by, enumerator);
2707 policy_sa_destroy(mapping, &id->dir, this);
2708 break;
2709 }
2710 if (is_installed)
2711 {
2712 cur_priority = mapping->priority;
2713 is_installed = FALSE;
2714 }
2715 }
2716 enumerator->destroy(enumerator);
2717
2718 use_count = current->used_by->get_count(current->used_by);
2719 if (use_count > 0)
2720 { /* policy is used by more SAs, keep in kernel */
2721 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
2722 if (!is_installed)
2723 { /* no need to update as the policy was not installed for this SA */
2724 policy_change_done(this, current);
2725 DBG2(DBG_KNL, "not updating policy %R === %R %N%s [priority %u, "
2726 "refcount %d]", id->src_ts, id->dst_ts, policy_dir_names,
2727 id->dir, markstr, cur_priority, use_count);
2728 return SUCCESS;
2729 }
2730 current->used_by->get_first(current->used_by, (void**)&mapping);
2731 current->reqid = mapping->sa->cfg.reqid;
2732
2733 DBG2(DBG_KNL, "updating policy %R === %R %N%s [priority %u, "
2734 "refcount %d]", id->src_ts, id->dst_ts, policy_dir_names, id->dir,
2735 markstr, mapping->priority, use_count);
2736
2737 if (add_policy_internal(this, current, mapping, TRUE) != SUCCESS)
2738 {
2739 DBG1(DBG_KNL, "unable to update policy %R === %R %N%s",
2740 id->src_ts, id->dst_ts, policy_dir_names, id->dir, markstr);
2741 return FAILED;
2742 }
2743 return SUCCESS;
2744 }
2745
2746 memset(&request, 0, sizeof(request));
2747
2748 hdr = &request.hdr;
2749 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2750 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
2751 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2752
2753 policy_id = NLMSG_DATA(hdr);
2754 policy_id->sel = current->sel;
2755 policy_id->dir = id->dir;
2756
2757 if (!add_mark(hdr, sizeof(request), id->mark))
2758 {
2759 policy_change_done(this, current);
2760 return FAILED;
2761 }
2762
2763 if (current->route)
2764 {
2765 route_entry_t *route = current->route;
2766 if (charon->kernel->del_route(charon->kernel, route->dst_net,
2767 route->prefixlen, route->gateway,
2768 route->src_ip, route->if_name) != SUCCESS)
2769 {
2770 DBG1(DBG_KNL, "error uninstalling route installed with policy "
2771 "%R === %R %N%s", id->src_ts, id->dst_ts, policy_dir_names,
2772 id->dir, markstr);
2773 }
2774 }
2775 this->mutex->unlock(this->mutex);
2776
2777 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
2778 {
2779 DBG1(DBG_KNL, "unable to delete policy %R === %R %N%s", id->src_ts,
2780 id->dst_ts, policy_dir_names, id->dir, markstr);
2781 status = FAILED;
2782 }
2783
2784 this->mutex->lock(this->mutex);
2785 if (!current->waiting)
2786 { /* only if no other thread still needs the policy */
2787 this->policies->remove(this->policies, current);
2788 policy_entry_destroy(this, current);
2789 this->mutex->unlock(this->mutex);
2790 }
2791 else
2792 {
2793 policy_change_done(this, current);
2794 }
2795 return status;
2796 }
2797
2798 METHOD(kernel_ipsec_t, flush_policies, status_t,
2799 private_kernel_netlink_ipsec_t *this)
2800 {
2801 netlink_buf_t request;
2802 struct nlmsghdr *hdr;
2803
2804 memset(&request, 0, sizeof(request));
2805
2806 DBG2(DBG_KNL, "flushing all policies from SPD");
2807
2808 hdr = &request.hdr;
2809 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2810 hdr->nlmsg_type = XFRM_MSG_FLUSHPOLICY;
2811 hdr->nlmsg_len = NLMSG_LENGTH(0); /* no data associated */
2812
2813 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2814 * to main or sub policies (default is main) */
2815
2816 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
2817 {
2818 DBG1(DBG_KNL, "unable to flush SPD entries");
2819 return FAILED;
2820 }
2821 return SUCCESS;
2822 }
2823
2824 /**
2825 * Bypass socket using a per-socket policy
2826 */
2827 static bool add_socket_bypass(private_kernel_netlink_ipsec_t *this,
2828 int fd, int family)
2829 {
2830 struct xfrm_userpolicy_info policy;
2831 u_int sol, ipsec_policy;
2832
2833 switch (family)
2834 {
2835 case AF_INET:
2836 sol = SOL_IP;
2837 ipsec_policy = IP_XFRM_POLICY;