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