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