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