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