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