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