Merge branch 'radius-ext'
[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,
1598 u_int64_t *bytes, u_int64_t *packets)
1599 {
1600 netlink_buf_t request;
1601 struct nlmsghdr *out = NULL, *hdr;
1602 struct xfrm_usersa_id *sa_id;
1603 struct xfrm_usersa_info *sa = NULL;
1604 status_t status = FAILED;
1605 size_t len;
1606
1607 memset(&request, 0, sizeof(request));
1608
1609 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x (mark %u/0x%08x)",
1610 ntohl(spi), mark.value, mark.mask);
1611
1612 hdr = (struct nlmsghdr*)request;
1613 hdr->nlmsg_flags = NLM_F_REQUEST;
1614 hdr->nlmsg_type = XFRM_MSG_GETSA;
1615 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1616
1617 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1618 host2xfrm(dst, &sa_id->daddr);
1619 sa_id->spi = spi;
1620 sa_id->proto = protocol;
1621 sa_id->family = dst->get_family(dst);
1622
1623 if (!add_mark(hdr, sizeof(request), mark))
1624 {
1625 return FAILED;
1626 }
1627
1628 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1629 {
1630 hdr = out;
1631 while (NLMSG_OK(hdr, len))
1632 {
1633 switch (hdr->nlmsg_type)
1634 {
1635 case XFRM_MSG_NEWSA:
1636 {
1637 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
1638 break;
1639 }
1640 case NLMSG_ERROR:
1641 {
1642 struct nlmsgerr *err = NLMSG_DATA(hdr);
1643
1644 if (mark.value)
1645 {
1646 DBG1(DBG_KNL, "querying SAD entry with SPI %.8x "
1647 "(mark %u/0x%08x) failed: %s (%d)",
1648 ntohl(spi), mark.value, mark.mask,
1649 strerror(-err->error), -err->error);
1650 }
1651 else
1652 {
1653 DBG1(DBG_KNL, "querying SAD entry with SPI %.8x "
1654 "failed: %s (%d)", ntohl(spi),
1655 strerror(-err->error), -err->error);
1656 }
1657 break;
1658 }
1659 default:
1660 hdr = NLMSG_NEXT(hdr, len);
1661 continue;
1662 case NLMSG_DONE:
1663 break;
1664 }
1665 break;
1666 }
1667 }
1668
1669 if (sa == NULL)
1670 {
1671 DBG2(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1672 }
1673 else
1674 {
1675 if (bytes)
1676 {
1677 *bytes = sa->curlft.bytes;
1678 }
1679 if (packets)
1680 {
1681 *packets = sa->curlft.packets;
1682 }
1683 status = SUCCESS;
1684 }
1685 memwipe(out, len);
1686 free(out);
1687 return status;
1688 }
1689
1690 METHOD(kernel_ipsec_t, del_sa, status_t,
1691 private_kernel_netlink_ipsec_t *this, host_t *src, host_t *dst,
1692 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
1693 {
1694 netlink_buf_t request;
1695 struct nlmsghdr *hdr;
1696 struct xfrm_usersa_id *sa_id;
1697
1698 /* if IPComp was used, we first delete the additional IPComp SA */
1699 if (cpi)
1700 {
1701 del_sa(this, src, dst, htonl(ntohs(cpi)), IPPROTO_COMP, 0, mark);
1702 }
1703
1704 memset(&request, 0, sizeof(request));
1705
1706 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x (mark %u/0x%08x)",
1707 ntohl(spi), mark.value, mark.mask);
1708
1709 hdr = (struct nlmsghdr*)request;
1710 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1711 hdr->nlmsg_type = XFRM_MSG_DELSA;
1712 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1713
1714 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1715 host2xfrm(dst, &sa_id->daddr);
1716 sa_id->spi = spi;
1717 sa_id->proto = protocol;
1718 sa_id->family = dst->get_family(dst);
1719
1720 if (!add_mark(hdr, sizeof(request), mark))
1721 {
1722 return FAILED;
1723 }
1724
1725 switch (this->socket_xfrm->send_ack(this->socket_xfrm, hdr))
1726 {
1727 case SUCCESS:
1728 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x (mark %u/0x%08x)",
1729 ntohl(spi), mark.value, mark.mask);
1730 return SUCCESS;
1731 case NOT_FOUND:
1732 return NOT_FOUND;
1733 default:
1734 if (mark.value)
1735 {
1736 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x "
1737 "(mark %u/0x%08x)", ntohl(spi), mark.value, mark.mask);
1738 }
1739 else
1740 {
1741 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x",
1742 ntohl(spi));
1743 }
1744 return FAILED;
1745 }
1746 }
1747
1748 METHOD(kernel_ipsec_t, update_sa, status_t,
1749 private_kernel_netlink_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
1750 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
1751 bool old_encap, bool new_encap, mark_t mark)
1752 {
1753 netlink_buf_t request;
1754 struct nlmsghdr *hdr, *out = NULL;
1755 struct xfrm_usersa_id *sa_id;
1756 struct xfrm_usersa_info *out_sa = NULL, *sa;
1757 size_t len;
1758 struct rtattr *rta;
1759 size_t rtasize;
1760 struct xfrm_encap_tmpl* tmpl = NULL;
1761 struct xfrm_replay_state *replay = NULL;
1762 struct xfrm_replay_state_esn *replay_esn = NULL;
1763 status_t status = FAILED;
1764
1765 /* if IPComp is used, we first update the IPComp SA */
1766 if (cpi)
1767 {
1768 update_sa(this, htonl(ntohs(cpi)), IPPROTO_COMP, 0,
1769 src, dst, new_src, new_dst, FALSE, FALSE, mark);
1770 }
1771
1772 memset(&request, 0, sizeof(request));
1773
1774 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x for update", ntohl(spi));
1775
1776 /* query the existing SA first */
1777 hdr = (struct nlmsghdr*)request;
1778 hdr->nlmsg_flags = NLM_F_REQUEST;
1779 hdr->nlmsg_type = XFRM_MSG_GETSA;
1780 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1781
1782 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1783 host2xfrm(dst, &sa_id->daddr);
1784 sa_id->spi = spi;
1785 sa_id->proto = protocol;
1786 sa_id->family = dst->get_family(dst);
1787
1788 if (!add_mark(hdr, sizeof(request), mark))
1789 {
1790 return FAILED;
1791 }
1792
1793 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1794 {
1795 hdr = out;
1796 while (NLMSG_OK(hdr, len))
1797 {
1798 switch (hdr->nlmsg_type)
1799 {
1800 case XFRM_MSG_NEWSA:
1801 {
1802 out_sa = NLMSG_DATA(hdr);
1803 break;
1804 }
1805 case NLMSG_ERROR:
1806 {
1807 struct nlmsgerr *err = NLMSG_DATA(hdr);
1808 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1809 strerror(-err->error), -err->error);
1810 break;
1811 }
1812 default:
1813 hdr = NLMSG_NEXT(hdr, len);
1814 continue;
1815 case NLMSG_DONE:
1816 break;
1817 }
1818 break;
1819 }
1820 }
1821 if (out_sa == NULL)
1822 {
1823 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1824 goto failed;
1825 }
1826
1827 get_replay_state(this, spi, protocol, dst, mark, &replay_esn, &replay);
1828
1829 /* delete the old SA (without affecting the IPComp SA) */
1830 if (del_sa(this, src, dst, spi, protocol, 0, mark) != SUCCESS)
1831 {
1832 DBG1(DBG_KNL, "unable to delete old SAD entry with SPI %.8x",
1833 ntohl(spi));
1834 goto failed;
1835 }
1836
1837 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1838 ntohl(spi), src, dst, new_src, new_dst);
1839 /* copy over the SA from out to request */
1840 hdr = (struct nlmsghdr*)request;
1841 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1842 hdr->nlmsg_type = XFRM_MSG_NEWSA;
1843 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1844 sa = NLMSG_DATA(hdr);
1845 memcpy(sa, NLMSG_DATA(out), sizeof(struct xfrm_usersa_info));
1846 sa->family = new_dst->get_family(new_dst);
1847
1848 if (!src->ip_equals(src, new_src))
1849 {
1850 host2xfrm(new_src, &sa->saddr);
1851 }
1852 if (!dst->ip_equals(dst, new_dst))
1853 {
1854 host2xfrm(new_dst, &sa->id.daddr);
1855 }
1856
1857 rta = XFRM_RTA(out, struct xfrm_usersa_info);
1858 rtasize = XFRM_PAYLOAD(out, struct xfrm_usersa_info);
1859 while (RTA_OK(rta, rtasize))
1860 {
1861 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1862 if (rta->rta_type != XFRMA_ENCAP || new_encap)
1863 {
1864 if (rta->rta_type == XFRMA_ENCAP)
1865 { /* update encap tmpl */
1866 tmpl = RTA_DATA(rta);
1867 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1868 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1869 }
1870 netlink_add_attribute(hdr, rta->rta_type,
1871 chunk_create(RTA_DATA(rta), RTA_PAYLOAD(rta)),
1872 sizeof(request));
1873 }
1874 rta = RTA_NEXT(rta, rtasize);
1875 }
1876
1877 if (tmpl == NULL && new_encap)
1878 { /* add tmpl if we are enabling it */
1879 tmpl = netlink_reserve(hdr, sizeof(request), XFRMA_ENCAP, sizeof(*tmpl));
1880 if (!tmpl)
1881 {
1882 goto failed;
1883 }
1884 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1885 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1886 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1887 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1888 }
1889
1890 if (replay_esn)
1891 {
1892 struct xfrm_replay_state_esn *state;
1893
1894 state = netlink_reserve(hdr, sizeof(request), XFRMA_REPLAY_ESN_VAL,
1895 sizeof(*state) + this->replay_bmp);
1896 if (!state)
1897 {
1898 goto failed;
1899 }
1900 memcpy(state, replay_esn, sizeof(*state) + this->replay_bmp);
1901 }
1902 else if (replay)
1903 {
1904 struct xfrm_replay_state *state;
1905
1906 state = netlink_reserve(hdr, sizeof(request), XFRMA_REPLAY_VAL,
1907 sizeof(*state));
1908 if (!state)
1909 {
1910 goto failed;
1911 }
1912 memcpy(state, replay, sizeof(*state));
1913 }
1914 else
1915 {
1916 DBG1(DBG_KNL, "unable to copy replay state from old SAD entry "
1917 "with SPI %.8x", ntohl(spi));
1918 }
1919
1920 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1921 {
1922 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1923 goto failed;
1924 }
1925
1926 status = SUCCESS;
1927 failed:
1928 free(replay);
1929 free(replay_esn);
1930 memwipe(out, len);
1931 memwipe(request, sizeof(request));
1932 free(out);
1933
1934 return status;
1935 }
1936
1937 METHOD(kernel_ipsec_t, flush_sas, status_t,
1938 private_kernel_netlink_ipsec_t *this)
1939 {
1940 netlink_buf_t request;
1941 struct nlmsghdr *hdr;
1942 struct xfrm_usersa_flush *flush;
1943
1944 memset(&request, 0, sizeof(request));
1945
1946 DBG2(DBG_KNL, "flushing all SAD entries");
1947
1948 hdr = (struct nlmsghdr*)request;
1949 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1950 hdr->nlmsg_type = XFRM_MSG_FLUSHSA;
1951 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1952
1953 flush = (struct xfrm_usersa_flush*)NLMSG_DATA(hdr);
1954 flush->proto = IPSEC_PROTO_ANY;
1955
1956 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1957 {
1958 DBG1(DBG_KNL, "unable to flush SAD entries");
1959 return FAILED;
1960 }
1961 return SUCCESS;
1962 }
1963
1964 /**
1965 * Add or update a policy in the kernel.
1966 *
1967 * Note: The mutex has to be locked when entering this function
1968 * and is unlocked here in any case.
1969 */
1970 static status_t add_policy_internal(private_kernel_netlink_ipsec_t *this,
1971 policy_entry_t *policy, policy_sa_t *mapping, bool update)
1972 {
1973 netlink_buf_t request;
1974 policy_entry_t clone;
1975 ipsec_sa_t *ipsec = mapping->sa;
1976 struct xfrm_userpolicy_info *policy_info;
1977 struct nlmsghdr *hdr;
1978 int i;
1979
1980 /* clone the policy so we are able to check it out again later */
1981 memcpy(&clone, policy, sizeof(policy_entry_t));
1982
1983 memset(&request, 0, sizeof(request));
1984 hdr = (struct nlmsghdr*)request;
1985 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1986 hdr->nlmsg_type = update ? XFRM_MSG_UPDPOLICY : XFRM_MSG_NEWPOLICY;
1987 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
1988
1989 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1990 policy_info->sel = policy->sel;
1991 policy_info->dir = policy->direction;
1992
1993 /* calculate priority based on selector size, small size = high prio */
1994 policy_info->priority = mapping->priority;
1995 policy_info->action = mapping->type != POLICY_DROP ? XFRM_POLICY_ALLOW
1996 : XFRM_POLICY_BLOCK;
1997 policy_info->share = XFRM_SHARE_ANY;
1998
1999 /* policies don't expire */
2000 policy_info->lft.soft_byte_limit = XFRM_INF;
2001 policy_info->lft.soft_packet_limit = XFRM_INF;
2002 policy_info->lft.hard_byte_limit = XFRM_INF;
2003 policy_info->lft.hard_packet_limit = XFRM_INF;
2004 policy_info->lft.soft_add_expires_seconds = 0;
2005 policy_info->lft.hard_add_expires_seconds = 0;
2006 policy_info->lft.soft_use_expires_seconds = 0;
2007 policy_info->lft.hard_use_expires_seconds = 0;
2008
2009 if (mapping->type == POLICY_IPSEC)
2010 {
2011 struct xfrm_user_tmpl *tmpl;
2012 struct {
2013 u_int8_t proto;
2014 bool use;
2015 } protos[] = {
2016 { IPPROTO_COMP, ipsec->cfg.ipcomp.transform != IPCOMP_NONE },
2017 { IPPROTO_ESP, ipsec->cfg.esp.use },
2018 { IPPROTO_AH, ipsec->cfg.ah.use },
2019 };
2020 ipsec_mode_t proto_mode = ipsec->cfg.mode;
2021 int count = 0;
2022
2023 for (i = 0; i < countof(protos); i++)
2024 {
2025 if (protos[i].use)
2026 {
2027 count++;
2028 }
2029 }
2030 tmpl = netlink_reserve(hdr, sizeof(request), XFRMA_TMPL,
2031 count * sizeof(*tmpl));
2032 if (!tmpl)
2033 {
2034 this->mutex->unlock(this->mutex);
2035 return FAILED;
2036 }
2037
2038 for (i = 0; i < countof(protos); i++)
2039 {
2040 if (!protos[i].use)
2041 {
2042 continue;
2043 }
2044 tmpl->reqid = ipsec->cfg.reqid;
2045 tmpl->id.proto = protos[i].proto;
2046 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
2047 tmpl->mode = mode2kernel(proto_mode);
2048 tmpl->optional = protos[i].proto == IPPROTO_COMP &&
2049 policy->direction != POLICY_OUT;
2050 tmpl->family = ipsec->src->get_family(ipsec->src);
2051
2052 if (proto_mode == MODE_TUNNEL)
2053 { /* only for tunnel mode */
2054 host2xfrm(ipsec->src, &tmpl->saddr);
2055 host2xfrm(ipsec->dst, &tmpl->id.daddr);
2056 }
2057
2058 tmpl++;
2059
2060 /* use transport mode for other SAs */
2061 proto_mode = MODE_TRANSPORT;
2062 }
2063 }
2064
2065 if (!add_mark(hdr, sizeof(request), ipsec->mark))
2066 {
2067 this->mutex->unlock(this->mutex);
2068 return FAILED;
2069 }
2070 this->mutex->unlock(this->mutex);
2071
2072 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
2073 {
2074 return FAILED;
2075 }
2076
2077 /* find the policy again */
2078 this->mutex->lock(this->mutex);
2079 policy = this->policies->get(this->policies, &clone);
2080 if (!policy ||
2081 policy->used_by->find_first(policy->used_by,
2082 NULL, (void**)&mapping) != SUCCESS)
2083 { /* policy or mapping is already gone, ignore */
2084 this->mutex->unlock(this->mutex);
2085 return SUCCESS;
2086 }
2087
2088 /* install a route, if:
2089 * - this is a forward policy (to just get one for each child)
2090 * - we are in tunnel/BEET mode or install a bypass policy
2091 * - routing is not disabled via strongswan.conf
2092 */
2093 if (policy->direction == POLICY_FWD && this->install_routes &&
2094 (mapping->type != POLICY_IPSEC || ipsec->cfg.mode != MODE_TRANSPORT))
2095 {
2096 policy_sa_fwd_t *fwd = (policy_sa_fwd_t*)mapping;
2097 route_entry_t *route;
2098 host_t *iface;
2099
2100 INIT(route,
2101 .prefixlen = policy->sel.prefixlen_s,
2102 );
2103
2104 if (hydra->kernel_interface->get_address_by_ts(hydra->kernel_interface,
2105 fwd->dst_ts, &route->src_ip) == SUCCESS)
2106 {
2107 /* get the nexthop to src (src as we are in POLICY_FWD) */
2108 route->gateway = hydra->kernel_interface->get_nexthop(
2109 hydra->kernel_interface, ipsec->src,
2110 ipsec->dst);
2111 route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
2112 memcpy(route->dst_net.ptr, &policy->sel.saddr, route->dst_net.len);
2113
2114 /* get the interface to install the route for. If we have a local
2115 * address, use it. Otherwise (for shunt policies) use the
2116 * routes source address. */
2117 iface = ipsec->dst;
2118 if (iface->is_anyaddr(iface))
2119 {
2120 iface = route->src_ip;
2121 }
2122 /* install route via outgoing interface */
2123 if (!hydra->kernel_interface->get_interface(hydra->kernel_interface,
2124 iface, &route->if_name))
2125 {
2126 this->mutex->unlock(this->mutex);
2127 route_entry_destroy(route);
2128 return SUCCESS;
2129 }
2130
2131 if (policy->route)
2132 {
2133 route_entry_t *old = policy->route;
2134 if (route_entry_equals(old, route))
2135 {
2136 this->mutex->unlock(this->mutex);
2137 route_entry_destroy(route);
2138 return SUCCESS;
2139 }
2140 /* uninstall previously installed route */
2141 if (hydra->kernel_interface->del_route(hydra->kernel_interface,
2142 old->dst_net, old->prefixlen, old->gateway,
2143 old->src_ip, old->if_name) != SUCCESS)
2144 {
2145 DBG1(DBG_KNL, "error uninstalling route installed with "
2146 "policy %R === %R %N", fwd->src_ts,
2147 fwd->dst_ts, policy_dir_names,
2148 policy->direction);
2149 }
2150 route_entry_destroy(old);
2151 policy->route = NULL;
2152 }
2153
2154 DBG2(DBG_KNL, "installing route: %R via %H src %H dev %s",
2155 fwd->src_ts, route->gateway, route->src_ip, route->if_name);
2156 switch (hydra->kernel_interface->add_route(
2157 hydra->kernel_interface, route->dst_net,
2158 route->prefixlen, route->gateway,
2159 route->src_ip, route->if_name))
2160 {
2161 default:
2162 DBG1(DBG_KNL, "unable to install source route for %H",
2163 route->src_ip);
2164 /* FALL */
2165 case ALREADY_DONE:
2166 /* route exists, do not uninstall */
2167 route_entry_destroy(route);
2168 break;
2169 case SUCCESS:
2170 /* cache the installed route */
2171 policy->route = route;
2172 break;
2173 }
2174 }
2175 else
2176 {
2177 free(route);
2178 }
2179 }
2180 this->mutex->unlock(this->mutex);
2181 return SUCCESS;
2182 }
2183
2184 METHOD(kernel_ipsec_t, add_policy, status_t,
2185 private_kernel_netlink_ipsec_t *this, host_t *src, host_t *dst,
2186 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
2187 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
2188 mark_t mark, policy_priority_t priority)
2189 {
2190 policy_entry_t *policy, *current;
2191 policy_sa_t *assigned_sa, *current_sa;
2192 enumerator_t *enumerator;
2193 bool found = FALSE, update = TRUE;
2194
2195 /* create a policy */
2196 INIT(policy,
2197 .sel = ts2selector(src_ts, dst_ts),
2198 .mark = mark.value & mark.mask,
2199 .direction = direction,
2200 );
2201
2202 /* find the policy, which matches EXACTLY */
2203 this->mutex->lock(this->mutex);
2204 current = this->policies->get(this->policies, policy);
2205 if (current)
2206 {
2207 /* use existing policy */
2208 DBG2(DBG_KNL, "policy %R === %R %N (mark %u/0x%08x) "
2209 "already exists, increasing refcount",
2210 src_ts, dst_ts, policy_dir_names, direction,
2211 mark.value, mark.mask);
2212 policy_entry_destroy(this, policy);
2213 policy = current;
2214 found = TRUE;
2215 }
2216 else
2217 { /* use the new one, if we have no such policy */
2218 policy->used_by = linked_list_create();
2219 this->policies->put(this->policies, policy, policy);
2220 }
2221
2222 /* cache the assigned IPsec SA */
2223 assigned_sa = policy_sa_create(this, direction, type, src, dst, src_ts,
2224 dst_ts, mark, sa);
2225 assigned_sa->priority = get_priority(policy, priority);
2226
2227 if (this->policy_history)
2228 { /* insert the SA according to its priority */
2229 enumerator = policy->used_by->create_enumerator(policy->used_by);
2230 while (enumerator->enumerate(enumerator, (void**)&current_sa))
2231 {
2232 if (current_sa->priority >= assigned_sa->priority)
2233 {
2234 break;
2235 }
2236 update = FALSE;
2237 }
2238 policy->used_by->insert_before(policy->used_by, enumerator,
2239 assigned_sa);
2240 enumerator->destroy(enumerator);
2241 }
2242 else
2243 { /* simply insert it last and only update if it is not installed yet */
2244 policy->used_by->insert_last(policy->used_by, assigned_sa);
2245 update = !found;
2246 }
2247
2248 if (!update)
2249 { /* we don't update the policy if the priority is lower than that of
2250 * the currently installed one */
2251 this->mutex->unlock(this->mutex);
2252 return SUCCESS;
2253 }
2254
2255 DBG2(DBG_KNL, "%s policy %R === %R %N (mark %u/0x%08x)",
2256 found ? "updating" : "adding", src_ts, dst_ts,
2257 policy_dir_names, direction, mark.value, mark.mask);
2258
2259 if (add_policy_internal(this, policy, assigned_sa, found) != SUCCESS)
2260 {
2261 DBG1(DBG_KNL, "unable to %s policy %R === %R %N",
2262 found ? "update" : "add", src_ts, dst_ts,
2263 policy_dir_names, direction);
2264 return FAILED;
2265 }
2266 return SUCCESS;
2267 }
2268
2269 METHOD(kernel_ipsec_t, query_policy, status_t,
2270 private_kernel_netlink_ipsec_t *this, traffic_selector_t *src_ts,
2271 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
2272 u_int32_t *use_time)
2273 {
2274 netlink_buf_t request;
2275 struct nlmsghdr *out = NULL, *hdr;
2276 struct xfrm_userpolicy_id *policy_id;
2277 struct xfrm_userpolicy_info *policy = NULL;
2278 size_t len;
2279
2280 memset(&request, 0, sizeof(request));
2281
2282 DBG2(DBG_KNL, "querying policy %R === %R %N (mark %u/0x%08x)",
2283 src_ts, dst_ts, policy_dir_names, direction,
2284 mark.value, mark.mask);
2285
2286 hdr = (struct nlmsghdr*)request;
2287 hdr->nlmsg_flags = NLM_F_REQUEST;
2288 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
2289 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2290
2291 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
2292 policy_id->sel = ts2selector(src_ts, dst_ts);
2293 policy_id->dir = direction;
2294
2295 if (!add_mark(hdr, sizeof(request), mark))
2296 {
2297 return FAILED;
2298 }
2299
2300 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
2301 {
2302 hdr = out;
2303 while (NLMSG_OK(hdr, len))
2304 {
2305 switch (hdr->nlmsg_type)
2306 {
2307 case XFRM_MSG_NEWPOLICY:
2308 {
2309 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
2310 break;
2311 }
2312 case NLMSG_ERROR:
2313 {
2314 struct nlmsgerr *err = NLMSG_DATA(hdr);
2315 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
2316 strerror(-err->error), -err->error);
2317 break;
2318 }
2319 default:
2320 hdr = NLMSG_NEXT(hdr, len);
2321 continue;
2322 case NLMSG_DONE:
2323 break;
2324 }
2325 break;
2326 }
2327 }
2328
2329 if (policy == NULL)
2330 {
2331 DBG2(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
2332 policy_dir_names, direction);
2333 free(out);
2334 return FAILED;
2335 }
2336
2337 if (policy->curlft.use_time)
2338 {
2339 /* we need the monotonic time, but the kernel returns system time. */
2340 *use_time = time_monotonic(NULL) - (time(NULL) - policy->curlft.use_time);
2341 }
2342 else
2343 {
2344 *use_time = 0;
2345 }
2346
2347 free(out);
2348 return SUCCESS;
2349 }
2350
2351 METHOD(kernel_ipsec_t, del_policy, status_t,
2352 private_kernel_netlink_ipsec_t *this, traffic_selector_t *src_ts,
2353 traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
2354 mark_t mark, policy_priority_t prio)
2355 {
2356 policy_entry_t *current, policy;
2357 enumerator_t *enumerator;
2358 policy_sa_t *mapping;
2359 netlink_buf_t request;
2360 struct nlmsghdr *hdr;
2361 struct xfrm_userpolicy_id *policy_id;
2362 bool is_installed = TRUE;
2363 u_int32_t priority;
2364
2365 DBG2(DBG_KNL, "deleting policy %R === %R %N (mark %u/0x%08x)",
2366 src_ts, dst_ts, policy_dir_names, direction,
2367 mark.value, mark.mask);
2368
2369 /* create a policy */
2370 memset(&policy, 0, sizeof(policy_entry_t));
2371 policy.sel = ts2selector(src_ts, dst_ts);
2372 policy.mark = mark.value & mark.mask;
2373 policy.direction = direction;
2374
2375 /* find the policy */
2376 this->mutex->lock(this->mutex);
2377 current = this->policies->get(this->policies, &policy);
2378 if (!current)
2379 {
2380 if (mark.value)
2381 {
2382 DBG1(DBG_KNL, "deleting policy %R === %R %N (mark %u/0x%08x) "
2383 "failed, not found", src_ts, dst_ts, policy_dir_names,
2384 direction, mark.value, mark.mask);
2385 }
2386 else
2387 {
2388 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found",
2389 src_ts, dst_ts, policy_dir_names, direction);
2390 }
2391 this->mutex->unlock(this->mutex);
2392 return NOT_FOUND;
2393 }
2394
2395 if (this->policy_history)
2396 { /* remove mapping to SA by reqid and priority */
2397 priority = get_priority(current, prio);
2398 enumerator = current->used_by->create_enumerator(current->used_by);
2399 while (enumerator->enumerate(enumerator, (void**)&mapping))
2400 {
2401 if (reqid == mapping->sa->cfg.reqid &&
2402 priority == mapping->priority)
2403 {
2404 current->used_by->remove_at(current->used_by, enumerator);
2405 policy_sa_destroy(mapping, &direction, this);
2406 break;
2407 }
2408 is_installed = FALSE;
2409 }
2410 enumerator->destroy(enumerator);
2411 }
2412 else
2413 { /* remove one of the SAs but don't update the policy */
2414 current->used_by->remove_last(current->used_by, (void**)&mapping);
2415 policy_sa_destroy(mapping, &direction, this);
2416 is_installed = FALSE;
2417 }
2418
2419 if (current->used_by->get_count(current->used_by) > 0)
2420 { /* policy is used by more SAs, keep in kernel */
2421 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
2422 if (!is_installed)
2423 { /* no need to update as the policy was not installed for this SA */
2424 this->mutex->unlock(this->mutex);
2425 return SUCCESS;
2426 }
2427
2428 DBG2(DBG_KNL, "updating policy %R === %R %N (mark %u/0x%08x)",
2429 src_ts, dst_ts, policy_dir_names, direction,
2430 mark.value, mark.mask);
2431
2432 current->used_by->get_first(current->used_by, (void**)&mapping);
2433 if (add_policy_internal(this, current, mapping, TRUE) != SUCCESS)
2434 {
2435 DBG1(DBG_KNL, "unable to update policy %R === %R %N",
2436 src_ts, dst_ts, policy_dir_names, direction);
2437 return FAILED;
2438 }
2439 return SUCCESS;
2440 }
2441
2442 memset(&request, 0, sizeof(request));
2443
2444 hdr = (struct nlmsghdr*)request;
2445 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2446 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
2447 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
2448
2449 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
2450 policy_id->sel = current->sel;
2451 policy_id->dir = direction;
2452
2453 if (!add_mark(hdr, sizeof(request), mark))
2454 {
2455 return FAILED;
2456 }
2457
2458 if (current->route)
2459 {
2460 route_entry_t *route = current->route;
2461 if (hydra->kernel_interface->del_route(hydra->kernel_interface,
2462 route->dst_net, route->prefixlen, route->gateway,
2463 route->src_ip, route->if_name) != SUCCESS)
2464 {
2465 DBG1(DBG_KNL, "error uninstalling route installed with "
2466 "policy %R === %R %N", src_ts, dst_ts,
2467 policy_dir_names, direction);
2468 }
2469 }
2470
2471 this->policies->remove(this->policies, current);
2472 policy_entry_destroy(this, current);
2473 this->mutex->unlock(this->mutex);
2474
2475 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
2476 {
2477 if (mark.value)
2478 {
2479 DBG1(DBG_KNL, "unable to delete policy %R === %R %N "
2480 "(mark %u/0x%08x)", src_ts, dst_ts, policy_dir_names,
2481 direction, mark.value, mark.mask);
2482 }
2483 else
2484 {
2485 DBG1(DBG_KNL, "unable to delete policy %R === %R %N",
2486 src_ts, dst_ts, policy_dir_names, direction);
2487 }
2488 return FAILED;
2489 }
2490 return SUCCESS;
2491 }
2492
2493 METHOD(kernel_ipsec_t, flush_policies, status_t,
2494 private_kernel_netlink_ipsec_t *this)
2495 {
2496 netlink_buf_t request;
2497 struct nlmsghdr *hdr;
2498
2499 memset(&request, 0, sizeof(request));
2500
2501 DBG2(DBG_KNL, "flushing all policies from SPD");
2502
2503 hdr = (struct nlmsghdr*)request;
2504 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
2505 hdr->nlmsg_type = XFRM_MSG_FLUSHPOLICY;
2506 hdr->nlmsg_len = NLMSG_LENGTH(0); /* no data associated */
2507
2508 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2509 * to main or sub policies (default is main) */
2510
2511 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
2512 {
2513 DBG1(DBG_KNL, "unable to flush SPD entries");
2514 return FAILED;
2515 }
2516 return SUCCESS;
2517 }
2518
2519
2520 METHOD(kernel_ipsec_t, bypass_socket, bool,
2521 private_kernel_netlink_ipsec_t *this, int fd, int family)
2522 {
2523 struct xfrm_userpolicy_info policy;
2524 u_int sol, ipsec_policy;
2525
2526 switch (family)
2527 {
2528 case AF_INET:
2529 sol = SOL_IP;
2530 ipsec_policy = IP_XFRM_POLICY;
2531 break;
2532 case AF_INET6:
2533 sol = SOL_IPV6;
2534 ipsec_policy = IPV6_XFRM_POLICY;
2535 break;
2536 default:
2537 return FALSE;
2538 }
2539
2540 memset(&policy, 0, sizeof(policy));
2541 policy.action = XFRM_POLICY_ALLOW;
2542 policy.sel.family = family;
2543
2544 policy.dir = XFRM_POLICY_OUT;
2545 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2546 {
2547 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2548 strerror(errno));
2549 return FALSE;
2550 }
2551 policy.dir = XFRM_POLICY_IN;
2552 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2553 {
2554 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2555 strerror(errno));
2556 return FALSE;
2557 }
2558 return TRUE;
2559 }
2560
2561 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
2562 private_kernel_netlink_ipsec_t *this, int fd, int family, u_int16_t port)
2563 {
2564 int type = UDP_ENCAP_ESPINUDP;
2565
2566 if (setsockopt(fd, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
2567 {
2568 DBG1(DBG_KNL, "unable to set UDP_ENCAP: %s", strerror(errno));
2569 return FALSE;
2570 }
2571 return TRUE;
2572 }
2573
2574 METHOD(kernel_ipsec_t, destroy, void,
2575 private_kernel_netlink_ipsec_t *this)
2576 {
2577 enumerator_t *enumerator;
2578 policy_entry_t *policy;
2579
2580 if (this->socket_xfrm_events > 0)
2581 {
2582 close(this->socket_xfrm_events);
2583 }
2584 DESTROY_IF(this->socket_xfrm);
2585 enumerator = this->policies->create_enumerator(this->policies);
2586 while (enumerator->enumerate(enumerator, &policy, &policy))
2587 {
2588 policy_entry_destroy(this, policy);
2589 }
2590 enumerator->destroy(enumerator);
2591 this->policies->destroy(this->policies);
2592 this->sas->destroy(this->sas);
2593 this->mutex->destroy(this->mutex);
2594 free(this);
2595 }
2596
2597 /*
2598 * Described in header.
2599 */
2600 kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
2601 {
2602 private_kernel_netlink_ipsec_t *this;
2603 bool register_for_events = TRUE;
2604 int fd;
2605
2606 INIT(this,
2607 .public = {
2608 .interface = {
2609 .get_features = _get_features,
2610 .get_spi = _get_spi,
2611 .get_cpi = _get_cpi,
2612 .add_sa = _add_sa,
2613 .update_sa = _update_sa,
2614 .query_sa = _query_sa,
2615 .del_sa = _del_sa,
2616 .flush_sas = _flush_sas,
2617 .add_policy = _add_policy,
2618 .query_policy = _query_policy,
2619 .del_policy = _del_policy,
2620 .flush_policies = _flush_policies,
2621 .bypass_socket = _bypass_socket,
2622 .enable_udp_decap = _enable_udp_decap,
2623 .destroy = _destroy,
2624 },
2625 },
2626 .policies = hashtable_create((hashtable_hash_t)policy_hash,
2627 (hashtable_equals_t)policy_equals, 32),
2628 .sas = hashtable_create((hashtable_hash_t)ipsec_sa_hash,
2629 (hashtable_equals_t)ipsec_sa_equals, 32),
2630 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
2631 .policy_history = TRUE,
2632 .install_routes = lib->settings->get_bool(lib->settings,
2633 "%s.install_routes", TRUE, hydra->daemon),
2634 .replay_window = lib->settings->get_int(lib->settings,
2635 "%s.replay_window", DEFAULT_REPLAY_WINDOW, hydra->daemon),
2636 );
2637
2638 this->replay_bmp = (this->replay_window + sizeof(u_int32_t) * 8 - 1) /
2639 (sizeof(u_int32_t) * 8);
2640
2641 if (streq(hydra->daemon, "pluto"))
2642 { /* no routes for pluto, they are installed via updown script */
2643 this->install_routes = FALSE;
2644 /* no policy history for pluto */
2645 this->policy_history = FALSE;
2646 }
2647 else if (streq(hydra->daemon, "starter"))
2648 { /* starter has no threads, so we do not register for kernel events */
2649 register_for_events = FALSE;
2650 }
2651
2652 /* disable lifetimes for allocated SPIs in kernel */
2653 fd = open("/proc/sys/net/core/xfrm_acq_expires", O_WRONLY);
2654 if (fd > 0)
2655 {
2656 ignore_result(write(fd, "165", 3));
2657 close(fd);
2658 }
2659
2660 this->socket_xfrm = netlink_socket_create(NETLINK_XFRM);
2661 if (!this->socket_xfrm)
2662 {
2663 destroy(this);
2664 return NULL;
2665 }
2666
2667 if (register_for_events)
2668 {
2669 struct sockaddr_nl addr;
2670
2671 memset(&addr, 0, sizeof(addr));
2672 addr.nl_family = AF_NETLINK;
2673
2674 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
2675 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
2676 if (this->socket_xfrm_events <= 0)
2677 {
2678 DBG1(DBG_KNL, "unable to create XFRM event socket");
2679 destroy(this);
2680 return NULL;
2681 }
2682 addr.nl_groups = XFRMNLGRP(ACQUIRE) | XFRMNLGRP(EXPIRE) |
2683 XFRMNLGRP(MIGRATE) | XFRMNLGRP(MAPPING);
2684 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
2685 {
2686 DBG1(DBG_KNL, "unable to bind XFRM event socket");
2687 destroy(this);
2688 return NULL;
2689 }
2690 lib->processor->queue_job(lib->processor,
2691 (job_t*)callback_job_create_with_prio(
2692 (callback_job_cb_t)receive_events, this, NULL,
2693 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
2694 }
2695
2696 return &this->public;
2697 }