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