Use source address in get_nexthop() call
[strongswan.git] / src / libhydra / plugins / kernel_pfkey / kernel_pfkey_ipsec.c
1 /*
2 * Copyright (C) 2008-2012 Tobias Brunner
3 * Copyright (C) 2008 Andreas Steffen
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <sys/types.h>
18 #include <sys/socket.h>
19
20 #ifdef __FreeBSD__
21 #include <limits.h> /* for LONG_MAX */
22 #endif
23
24 #ifdef HAVE_NET_PFKEYV2_H
25 #include <net/pfkeyv2.h>
26 #else
27 #include <stdint.h>
28 #include <linux/pfkeyv2.h>
29 #endif
30
31 #ifdef SADB_X_EXT_NAT_T_TYPE
32 #define HAVE_NATT
33 #endif
34
35 #ifdef HAVE_NETIPSEC_IPSEC_H
36 #include <netipsec/ipsec.h>
37 #elif defined(HAVE_NETINET6_IPSEC_H)
38 #include <netinet6/ipsec.h>
39 #else
40 #include <linux/ipsec.h>
41 #endif
42
43 #ifdef HAVE_NATT
44 #ifdef HAVE_LINUX_UDP_H
45 #include <linux/udp.h>
46 #else
47 #include <netinet/udp.h>
48 #endif /*HAVE_LINUX_UDP_H*/
49 #endif /*HAVE_NATT*/
50
51 #include <unistd.h>
52 #include <time.h>
53 #include <errno.h>
54 #ifdef __APPLE__
55 #include <sys/sysctl.h>
56 #endif
57
58 #include "kernel_pfkey_ipsec.h"
59
60 #include <hydra.h>
61 #include <debug.h>
62 #include <utils/host.h>
63 #include <utils/linked_list.h>
64 #include <utils/hashtable.h>
65 #include <threading/thread.h>
66 #include <threading/mutex.h>
67 #include <processing/jobs/callback_job.h>
68
69 /** non linux specific */
70 #ifndef IPPROTO_COMP
71 #ifdef IPPROTO_IPCOMP
72 #define IPPROTO_COMP IPPROTO_IPCOMP
73 #endif
74 #endif
75
76 #ifndef SADB_X_AALG_SHA2_256HMAC
77 #define SADB_X_AALG_SHA2_256HMAC SADB_X_AALG_SHA2_256
78 #define SADB_X_AALG_SHA2_384HMAC SADB_X_AALG_SHA2_384
79 #define SADB_X_AALG_SHA2_512HMAC SADB_X_AALG_SHA2_512
80 #endif
81
82 #ifndef SADB_X_EALG_AESCBC
83 #define SADB_X_EALG_AESCBC SADB_X_EALG_AES
84 #endif
85
86 #ifndef SADB_X_EALG_CASTCBC
87 #define SADB_X_EALG_CASTCBC SADB_X_EALG_CAST128CBC
88 #endif
89
90 #ifndef SOL_IP
91 #define SOL_IP IPPROTO_IP
92 #define SOL_IPV6 IPPROTO_IPV6
93 #endif
94
95 /** from linux/in.h */
96 #ifndef IP_IPSEC_POLICY
97 #define IP_IPSEC_POLICY 16
98 #endif
99
100 /** missing on uclibc */
101 #ifndef IPV6_IPSEC_POLICY
102 #define IPV6_IPSEC_POLICY 34
103 #endif
104
105 /* from linux/udp.h */
106 #ifndef UDP_ENCAP
107 #define UDP_ENCAP 100
108 #endif
109
110 #ifndef UDP_ENCAP_ESPINUDP
111 #define UDP_ENCAP_ESPINUDP 2
112 #endif
113
114 /* this is not defined on some platforms */
115 #ifndef SOL_UDP
116 #define SOL_UDP IPPROTO_UDP
117 #endif
118
119 /** default priority of installed policies */
120 #define PRIO_BASE 512
121
122 #ifdef __APPLE__
123 /** from xnu/bsd/net/pfkeyv2.h */
124 #define SADB_X_EXT_NATT 0x002
125 struct sadb_sa_2 {
126 struct sadb_sa sa;
127 u_int16_t sadb_sa_natt_port;
128 u_int16_t sadb_reserved0;
129 u_int32_t sadb_reserved1;
130 };
131 #endif
132
133 /** buffer size for PF_KEY messages */
134 #define PFKEY_BUFFER_SIZE 4096
135
136 /** PF_KEY messages are 64 bit aligned */
137 #define PFKEY_ALIGNMENT 8
138 /** aligns len to 64 bits */
139 #define PFKEY_ALIGN(len) (((len) + PFKEY_ALIGNMENT - 1) & ~(PFKEY_ALIGNMENT - 1))
140 /** calculates the properly padded length in 64 bit chunks */
141 #define PFKEY_LEN(len) ((PFKEY_ALIGN(len) / PFKEY_ALIGNMENT))
142 /** calculates user mode length i.e. in bytes */
143 #define PFKEY_USER_LEN(len) ((len) * PFKEY_ALIGNMENT)
144
145 /** given a PF_KEY message header and an extension this updates the length in the header */
146 #define PFKEY_EXT_ADD(msg, ext) ((msg)->sadb_msg_len += ((struct sadb_ext*)ext)->sadb_ext_len)
147 /** given a PF_KEY message header this returns a pointer to the next extension */
148 #define PFKEY_EXT_ADD_NEXT(msg) ((struct sadb_ext*)(((char*)(msg)) + PFKEY_USER_LEN((msg)->sadb_msg_len)))
149 /** copy an extension and append it to a PF_KEY message */
150 #define PFKEY_EXT_COPY(msg, ext) (PFKEY_EXT_ADD(msg, memcpy(PFKEY_EXT_ADD_NEXT(msg), ext, PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len))))
151 /** given a PF_KEY extension this returns a pointer to the next extension */
152 #define PFKEY_EXT_NEXT(ext) ((struct sadb_ext*)(((char*)(ext)) + PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len)))
153 /** given a PF_KEY extension this returns a pointer to the next extension also updates len (len in 64 bit words) */
154 #define PFKEY_EXT_NEXT_LEN(ext,len) ((len) -= (ext)->sadb_ext_len, PFKEY_EXT_NEXT(ext))
155 /** true if ext has a valid length and len is large enough to contain ext (assuming len in 64 bit words) */
156 #define PFKEY_EXT_OK(ext,len) ((len) >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
157 (ext)->sadb_ext_len >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
158 (ext)->sadb_ext_len <= (len))
159
160 typedef struct private_kernel_pfkey_ipsec_t private_kernel_pfkey_ipsec_t;
161
162 /**
163 * Private variables and functions of kernel_pfkey class.
164 */
165 struct private_kernel_pfkey_ipsec_t
166 {
167 /**
168 * Public part of the kernel_pfkey_t object.
169 */
170 kernel_pfkey_ipsec_t public;
171
172 /**
173 * mutex to lock access to various lists
174 */
175 mutex_t *mutex;
176
177 /**
178 * List of installed policies (policy_entry_t)
179 */
180 linked_list_t *policies;
181
182 /**
183 * Hash table of IPsec SAs using policies (ipsec_sa_t)
184 */
185 hashtable_t *sas;
186
187 /**
188 * whether to install routes along policies
189 */
190 bool install_routes;
191
192 /**
193 * mutex to lock access to the PF_KEY socket
194 */
195 mutex_t *mutex_pfkey;
196
197 /**
198 * PF_KEY socket to communicate with the kernel
199 */
200 int socket;
201
202 /**
203 * PF_KEY socket to receive acquire and expire events
204 */
205 int socket_events;
206
207 /**
208 * sequence number for messages sent to the kernel
209 */
210 int seq;
211 };
212
213 typedef struct route_entry_t route_entry_t;
214
215 /**
216 * installed routing entry
217 */
218 struct route_entry_t {
219 /** name of the interface the route is bound to */
220 char *if_name;
221
222 /** source ip of the route */
223 host_t *src_ip;
224
225 /** gateway for this route */
226 host_t *gateway;
227
228 /** destination net */
229 chunk_t dst_net;
230
231 /** destination net prefixlen */
232 u_int8_t prefixlen;
233 };
234
235 /**
236 * destroy an route_entry_t object
237 */
238 static void route_entry_destroy(route_entry_t *this)
239 {
240 free(this->if_name);
241 DESTROY_IF(this->src_ip);
242 DESTROY_IF(this->gateway);
243 chunk_free(&this->dst_net);
244 free(this);
245 }
246
247 /**
248 * compare two route_entry_t objects
249 */
250 static bool route_entry_equals(route_entry_t *a, route_entry_t *b)
251 {
252 return a->if_name && b->if_name && streq(a->if_name, b->if_name) &&
253 a->src_ip->ip_equals(a->src_ip, b->src_ip) &&
254 a->gateway->ip_equals(a->gateway, b->gateway) &&
255 chunk_equals(a->dst_net, b->dst_net) && a->prefixlen == b->prefixlen;
256 }
257
258 typedef struct ipsec_sa_t ipsec_sa_t;
259
260 /**
261 * IPsec SA assigned to a policy.
262 */
263 struct ipsec_sa_t {
264 /** Source address of this SA */
265 host_t *src;
266
267 /** Destination address of this SA */
268 host_t *dst;
269
270 /** Description of this SA */
271 ipsec_sa_cfg_t cfg;
272
273 /** Reference count for this SA */
274 refcount_t refcount;
275 };
276
277 /**
278 * Hash function for ipsec_sa_t objects
279 */
280 static u_int ipsec_sa_hash(ipsec_sa_t *sa)
281 {
282 return chunk_hash_inc(sa->src->get_address(sa->src),
283 chunk_hash_inc(sa->dst->get_address(sa->dst),
284 chunk_hash(chunk_from_thing(sa->cfg))));
285 }
286
287 /**
288 * Equality function for ipsec_sa_t objects
289 */
290 static bool ipsec_sa_equals(ipsec_sa_t *sa, ipsec_sa_t *other_sa)
291 {
292 return sa->src->ip_equals(sa->src, other_sa->src) &&
293 sa->dst->ip_equals(sa->dst, other_sa->dst) &&
294 memeq(&sa->cfg, &other_sa->cfg, sizeof(ipsec_sa_cfg_t));
295 }
296
297 /**
298 * Allocate or reference an IPsec SA object
299 */
300 static ipsec_sa_t *ipsec_sa_create(private_kernel_pfkey_ipsec_t *this,
301 host_t *src, host_t *dst,
302 ipsec_sa_cfg_t *cfg)
303 {
304 ipsec_sa_t *sa, *found;
305 INIT(sa,
306 .src = src,
307 .dst = dst,
308 .cfg = *cfg,
309 );
310 found = this->sas->get(this->sas, sa);
311 if (!found)
312 {
313 sa->src = src->clone(src);
314 sa->dst = dst->clone(dst);
315 this->sas->put(this->sas, sa, sa);
316 }
317 else
318 {
319 free(sa);
320 sa = found;
321 }
322 ref_get(&sa->refcount);
323 return sa;
324 }
325
326 /**
327 * Release and destroy an IPsec SA object
328 */
329 static void ipsec_sa_destroy(private_kernel_pfkey_ipsec_t *this,
330 ipsec_sa_t *sa)
331 {
332 if (ref_put(&sa->refcount))
333 {
334 this->sas->remove(this->sas, sa);
335 DESTROY_IF(sa->src);
336 DESTROY_IF(sa->dst);
337 free(sa);
338 }
339 }
340
341 typedef struct policy_sa_t policy_sa_t;
342 typedef struct policy_sa_fwd_t policy_sa_fwd_t;
343
344 /**
345 * Mapping between a policy and an IPsec SA.
346 */
347 struct policy_sa_t {
348 /** Priority assigned to the policy when installed with this SA */
349 u_int32_t priority;
350
351 /** Type of the policy */
352 policy_type_t type;
353
354 /** Assigned SA */
355 ipsec_sa_t *sa;
356 };
357
358 /**
359 * For forward policies we also cache the traffic selectors in order to install
360 * the route.
361 */
362 struct policy_sa_fwd_t {
363 /** Generic interface */
364 policy_sa_t generic;
365
366 /** Source traffic selector of this policy */
367 traffic_selector_t *src_ts;
368
369 /** Destination traffic selector of this policy */
370 traffic_selector_t *dst_ts;
371 };
372
373 /**
374 * Create a policy_sa(_fwd)_t object
375 */
376 static policy_sa_t *policy_sa_create(private_kernel_pfkey_ipsec_t *this,
377 policy_dir_t dir, policy_type_t type, host_t *src, host_t *dst,
378 traffic_selector_t *src_ts, traffic_selector_t *dst_ts, ipsec_sa_cfg_t *cfg)
379 {
380 policy_sa_t *policy;
381
382 if (dir == POLICY_FWD)
383 {
384 policy_sa_fwd_t *fwd;
385 INIT(fwd,
386 .src_ts = src_ts->clone(src_ts),
387 .dst_ts = dst_ts->clone(dst_ts),
388 );
389 policy = &fwd->generic;
390 }
391 else
392 {
393 INIT(policy, .priority = 0);
394 }
395 policy->type = type;
396 policy->sa = ipsec_sa_create(this, src, dst, cfg);
397 return policy;
398 }
399
400 /**
401 * Destroy a policy_sa(_fwd)_t object
402 */
403 static void policy_sa_destroy(policy_sa_t *policy, policy_dir_t *dir,
404 private_kernel_pfkey_ipsec_t *this)
405 {
406 if (*dir == POLICY_FWD)
407 {
408 policy_sa_fwd_t *fwd = (policy_sa_fwd_t*)policy;
409 fwd->src_ts->destroy(fwd->src_ts);
410 fwd->dst_ts->destroy(fwd->dst_ts);
411 }
412 ipsec_sa_destroy(this, policy->sa);
413 free(policy);
414 }
415
416 typedef struct policy_entry_t policy_entry_t;
417
418 /**
419 * installed kernel policy.
420 */
421 struct policy_entry_t {
422 /** Index assigned by the kernel */
423 u_int32_t index;
424
425 /** Direction of this policy: in, out, forward */
426 u_int8_t direction;
427
428 /** Parameters of installed policy */
429 struct {
430 /** Subnet and port */
431 host_t *net;
432 /** Subnet mask */
433 u_int8_t mask;
434 /** Protocol */
435 u_int8_t proto;
436 } src, dst;
437
438 /** Associated route installed for this policy */
439 route_entry_t *route;
440
441 /** List of SAs this policy is used by, ordered by priority */
442 linked_list_t *used_by;
443 };
444
445 /**
446 * Create a policy_entry_t object
447 */
448 static policy_entry_t *create_policy_entry(traffic_selector_t *src_ts,
449 traffic_selector_t *dst_ts,
450 policy_dir_t dir)
451 {
452 policy_entry_t *policy;
453 INIT(policy,
454 .direction = dir,
455 );
456
457 src_ts->to_subnet(src_ts, &policy->src.net, &policy->src.mask);
458 dst_ts->to_subnet(dst_ts, &policy->dst.net, &policy->dst.mask);
459
460 /* src or dest proto may be "any" (0), use more restrictive one */
461 policy->src.proto = max(src_ts->get_protocol(src_ts),
462 dst_ts->get_protocol(dst_ts));
463 policy->src.proto = policy->src.proto ? policy->src.proto : IPSEC_PROTO_ANY;
464 policy->dst.proto = policy->src.proto;
465
466 return policy;
467 }
468
469 /**
470 * Destroy a policy_entry_t object
471 */
472 static void policy_entry_destroy(policy_entry_t *policy,
473 private_kernel_pfkey_ipsec_t *this)
474 {
475 if (policy->route)
476 {
477 route_entry_destroy(policy->route);
478 }
479 if (policy->used_by)
480 {
481 policy->used_by->invoke_function(policy->used_by,
482 (linked_list_invoke_t)policy_sa_destroy,
483 &policy->direction, this);
484 policy->used_by->destroy(policy->used_by);
485 }
486 DESTROY_IF(policy->src.net);
487 DESTROY_IF(policy->dst.net);
488 free(policy);
489 }
490
491 /**
492 * compares two policy_entry_t
493 */
494 static inline bool policy_entry_equals(policy_entry_t *current,
495 policy_entry_t *policy)
496 {
497 return current->direction == policy->direction &&
498 current->src.proto == policy->src.proto &&
499 current->dst.proto == policy->dst.proto &&
500 current->src.mask == policy->src.mask &&
501 current->dst.mask == policy->dst.mask &&
502 current->src.net->equals(current->src.net, policy->src.net) &&
503 current->dst.net->equals(current->dst.net, policy->dst.net);
504 }
505
506 /**
507 * compare the given kernel index with that of a policy
508 */
509 static inline bool policy_entry_match_byindex(policy_entry_t *current,
510 u_int32_t *index)
511 {
512 return current->index == *index;
513 }
514
515 /**
516 * Calculate the priority of a policy
517 */
518 static inline u_int32_t get_priority(policy_entry_t *policy,
519 policy_priority_t prio)
520 {
521 u_int32_t priority = PRIO_BASE;
522 switch (prio)
523 {
524 case POLICY_PRIORITY_FALLBACK:
525 priority <<= 1;
526 /* fall-through */
527 case POLICY_PRIORITY_ROUTED:
528 priority <<= 1;
529 /* fall-through */
530 case POLICY_PRIORITY_DEFAULT:
531 break;
532 }
533 /* calculate priority based on selector size, small size = high prio */
534 priority -= policy->src.mask;
535 priority -= policy->dst.mask;
536 priority <<= 2; /* make some room for the two flags */
537 priority += policy->src.net->get_port(policy->src.net) ||
538 policy->dst.net->get_port(policy->dst.net) ?
539 0 : 2;
540 priority += policy->src.proto != IPSEC_PROTO_ANY ? 0 : 1;
541 return priority;
542 }
543
544 typedef struct pfkey_msg_t pfkey_msg_t;
545
546 struct pfkey_msg_t
547 {
548 /**
549 * PF_KEY message base
550 */
551 struct sadb_msg *msg;
552
553 /**
554 * PF_KEY message extensions
555 */
556 union {
557 struct sadb_ext *ext[SADB_EXT_MAX + 1];
558 struct {
559 struct sadb_ext *reserved; /* SADB_EXT_RESERVED */
560 struct sadb_sa *sa; /* SADB_EXT_SA */
561 struct sadb_lifetime *lft_current; /* SADB_EXT_LIFETIME_CURRENT */
562 struct sadb_lifetime *lft_hard; /* SADB_EXT_LIFETIME_HARD */
563 struct sadb_lifetime *lft_soft; /* SADB_EXT_LIFETIME_SOFT */
564 struct sadb_address *src; /* SADB_EXT_ADDRESS_SRC */
565 struct sadb_address *dst; /* SADB_EXT_ADDRESS_DST */
566 struct sadb_address *proxy; /* SADB_EXT_ADDRESS_PROXY */
567 struct sadb_key *key_auth; /* SADB_EXT_KEY_AUTH */
568 struct sadb_key *key_encr; /* SADB_EXT_KEY_ENCRYPT */
569 struct sadb_ident *id_src; /* SADB_EXT_IDENTITY_SRC */
570 struct sadb_ident *id_dst; /* SADB_EXT_IDENTITY_DST */
571 struct sadb_sens *sensitivity; /* SADB_EXT_SENSITIVITY */
572 struct sadb_prop *proposal; /* SADB_EXT_PROPOSAL */
573 struct sadb_supported *supported_auth; /* SADB_EXT_SUPPORTED_AUTH */
574 struct sadb_supported *supported_encr; /* SADB_EXT_SUPPORTED_ENCRYPT */
575 struct sadb_spirange *spirange; /* SADB_EXT_SPIRANGE */
576 struct sadb_x_kmprivate *x_kmprivate; /* SADB_X_EXT_KMPRIVATE */
577 struct sadb_x_policy *x_policy; /* SADB_X_EXT_POLICY */
578 struct sadb_x_sa2 *x_sa2; /* SADB_X_EXT_SA2 */
579 struct sadb_x_nat_t_type *x_natt_type; /* SADB_X_EXT_NAT_T_TYPE */
580 struct sadb_x_nat_t_port *x_natt_sport; /* SADB_X_EXT_NAT_T_SPORT */
581 struct sadb_x_nat_t_port *x_natt_dport; /* SADB_X_EXT_NAT_T_DPORT */
582 struct sadb_address *x_natt_oa; /* SADB_X_EXT_NAT_T_OA */
583 struct sadb_x_sec_ctx *x_sec_ctx; /* SADB_X_EXT_SEC_CTX */
584 struct sadb_x_kmaddress *x_kmaddress; /* SADB_X_EXT_KMADDRESS */
585 } __attribute__((__packed__));
586 };
587 };
588
589 ENUM(sadb_ext_type_names, SADB_EXT_RESERVED, SADB_EXT_MAX,
590 "SADB_EXT_RESERVED",
591 "SADB_EXT_SA",
592 "SADB_EXT_LIFETIME_CURRENT",
593 "SADB_EXT_LIFETIME_HARD",
594 "SADB_EXT_LIFETIME_SOFT",
595 "SADB_EXT_ADDRESS_SRC",
596 "SADB_EXT_ADDRESS_DST",
597 "SADB_EXT_ADDRESS_PROXY",
598 "SADB_EXT_KEY_AUTH",
599 "SADB_EXT_KEY_ENCRYPT",
600 "SADB_EXT_IDENTITY_SRC",
601 "SADB_EXT_IDENTITY_DST",
602 "SADB_EXT_SENSITIVITY",
603 "SADB_EXT_PROPOSAL",
604 "SADB_EXT_SUPPORTED_AUTH",
605 "SADB_EXT_SUPPORTED_ENCRYPT",
606 "SADB_EXT_SPIRANGE",
607 "SADB_X_EXT_KMPRIVATE",
608 "SADB_X_EXT_POLICY",
609 "SADB_X_EXT_SA2",
610 "SADB_X_EXT_NAT_T_TYPE",
611 "SADB_X_EXT_NAT_T_SPORT",
612 "SADB_X_EXT_NAT_T_DPORT",
613 "SADB_X_EXT_NAT_T_OA",
614 "SADB_X_EXT_SEC_CTX",
615 "SADB_X_EXT_KMADDRESS"
616 );
617
618 /**
619 * convert a protocol identifier to the PF_KEY sa type
620 */
621 static u_int8_t proto2satype(u_int8_t proto)
622 {
623 switch (proto)
624 {
625 case IPPROTO_ESP:
626 return SADB_SATYPE_ESP;
627 case IPPROTO_AH:
628 return SADB_SATYPE_AH;
629 case IPPROTO_COMP:
630 return SADB_X_SATYPE_IPCOMP;
631 default:
632 return proto;
633 }
634 }
635
636 /**
637 * convert a PF_KEY sa type to a protocol identifier
638 */
639 static u_int8_t satype2proto(u_int8_t satype)
640 {
641 switch (satype)
642 {
643 case SADB_SATYPE_ESP:
644 return IPPROTO_ESP;
645 case SADB_SATYPE_AH:
646 return IPPROTO_AH;
647 case SADB_X_SATYPE_IPCOMP:
648 return IPPROTO_COMP;
649 default:
650 return satype;
651 }
652 }
653
654 /**
655 * convert the general ipsec mode to the one defined in ipsec.h
656 */
657 static u_int8_t mode2kernel(ipsec_mode_t mode)
658 {
659 switch (mode)
660 {
661 case MODE_TRANSPORT:
662 return IPSEC_MODE_TRANSPORT;
663 case MODE_TUNNEL:
664 return IPSEC_MODE_TUNNEL;
665 #ifdef HAVE_IPSEC_MODE_BEET
666 case MODE_BEET:
667 return IPSEC_MODE_BEET;
668 #endif
669 default:
670 return mode;
671 }
672 }
673
674 /**
675 * convert the general policy direction to the one defined in ipsec.h
676 */
677 static u_int8_t dir2kernel(policy_dir_t dir)
678 {
679 switch (dir)
680 {
681 case POLICY_IN:
682 return IPSEC_DIR_INBOUND;
683 case POLICY_OUT:
684 return IPSEC_DIR_OUTBOUND;
685 #ifdef HAVE_IPSEC_DIR_FWD
686 case POLICY_FWD:
687 return IPSEC_DIR_FWD;
688 #endif
689 default:
690 return IPSEC_DIR_INVALID;
691 }
692 }
693
694 /**
695 * convert the policy type to the one defined in ipsec.h
696 */
697 static inline u_int16_t type2kernel(policy_type_t type)
698 {
699 switch (type)
700 {
701 case POLICY_IPSEC:
702 return IPSEC_POLICY_IPSEC;
703 case POLICY_PASS:
704 return IPSEC_POLICY_NONE;
705 case POLICY_DROP:
706 return IPSEC_POLICY_DISCARD;
707 }
708 return type;
709 }
710
711 #ifdef SADB_X_MIGRATE
712 /**
713 * convert the policy direction in ipsec.h to the general one.
714 */
715 static policy_dir_t kernel2dir(u_int8_t dir)
716 {
717 switch (dir)
718 {
719 case IPSEC_DIR_INBOUND:
720 return POLICY_IN;
721 case IPSEC_DIR_OUTBOUND:
722 return POLICY_OUT;
723 #ifdef HAVE_IPSEC_DIR_FWD
724 case IPSEC_DIR_FWD:
725 return POLICY_FWD;
726 #endif
727 default:
728 return dir;
729 }
730 }
731 #endif /*SADB_X_MIGRATE*/
732
733 typedef struct kernel_algorithm_t kernel_algorithm_t;
734
735 /**
736 * Mapping of IKEv2 algorithms to PF_KEY algorithms
737 */
738 struct kernel_algorithm_t {
739 /**
740 * Identifier specified in IKEv2
741 */
742 int ikev2;
743
744 /**
745 * Identifier as defined in pfkeyv2.h
746 */
747 int kernel;
748 };
749
750 #define END_OF_LIST -1
751
752 /**
753 * Algorithms for encryption
754 */
755 static kernel_algorithm_t encryption_algs[] = {
756 /* {ENCR_DES_IV64, 0 }, */
757 {ENCR_DES, SADB_EALG_DESCBC },
758 {ENCR_3DES, SADB_EALG_3DESCBC },
759 /* {ENCR_RC5, 0 }, */
760 /* {ENCR_IDEA, 0 }, */
761 {ENCR_CAST, SADB_X_EALG_CASTCBC },
762 {ENCR_BLOWFISH, SADB_X_EALG_BLOWFISHCBC },
763 /* {ENCR_3IDEA, 0 }, */
764 /* {ENCR_DES_IV32, 0 }, */
765 {ENCR_NULL, SADB_EALG_NULL },
766 {ENCR_AES_CBC, SADB_X_EALG_AESCBC },
767 /* {ENCR_AES_CTR, SADB_X_EALG_AESCTR }, */
768 /* {ENCR_AES_CCM_ICV8, SADB_X_EALG_AES_CCM_ICV8 }, */
769 /* {ENCR_AES_CCM_ICV12, SADB_X_EALG_AES_CCM_ICV12 }, */
770 /* {ENCR_AES_CCM_ICV16, SADB_X_EALG_AES_CCM_ICV16 }, */
771 /* {ENCR_AES_GCM_ICV8, SADB_X_EALG_AES_GCM_ICV8 }, */
772 /* {ENCR_AES_GCM_ICV12, SADB_X_EALG_AES_GCM_ICV12 }, */
773 /* {ENCR_AES_GCM_ICV16, SADB_X_EALG_AES_GCM_ICV16 }, */
774 {END_OF_LIST, 0 },
775 };
776
777 /**
778 * Algorithms for integrity protection
779 */
780 static kernel_algorithm_t integrity_algs[] = {
781 {AUTH_HMAC_MD5_96, SADB_AALG_MD5HMAC },
782 {AUTH_HMAC_SHA1_96, SADB_AALG_SHA1HMAC },
783 {AUTH_HMAC_SHA2_256_128, SADB_X_AALG_SHA2_256HMAC },
784 {AUTH_HMAC_SHA2_384_192, SADB_X_AALG_SHA2_384HMAC },
785 {AUTH_HMAC_SHA2_512_256, SADB_X_AALG_SHA2_512HMAC },
786 /* {AUTH_DES_MAC, 0, }, */
787 /* {AUTH_KPDK_MD5, 0, }, */
788 #ifdef SADB_X_AALG_AES_XCBC_MAC
789 {AUTH_AES_XCBC_96, SADB_X_AALG_AES_XCBC_MAC, },
790 #endif
791 {END_OF_LIST, 0, },
792 };
793
794 #if 0
795 /**
796 * Algorithms for IPComp, unused yet
797 */
798 static kernel_algorithm_t compression_algs[] = {
799 /* {IPCOMP_OUI, 0 }, */
800 {IPCOMP_DEFLATE, SADB_X_CALG_DEFLATE },
801 {IPCOMP_LZS, SADB_X_CALG_LZS },
802 {IPCOMP_LZJH, SADB_X_CALG_LZJH },
803 {END_OF_LIST, 0 },
804 };
805 #endif
806
807 /**
808 * Look up a kernel algorithm ID and its key size
809 */
810 static int lookup_algorithm(transform_type_t type, int ikev2)
811 {
812 kernel_algorithm_t *list;
813 int alg = 0;
814
815 switch (type)
816 {
817 case ENCRYPTION_ALGORITHM:
818 list = encryption_algs;
819 break;
820 case INTEGRITY_ALGORITHM:
821 list = integrity_algs;
822 break;
823 default:
824 return 0;
825 }
826 while (list->ikev2 != END_OF_LIST)
827 {
828 if (ikev2 == list->ikev2)
829 {
830 return list->kernel;
831 }
832 list++;
833 }
834 hydra->kernel_interface->lookup_algorithm(hydra->kernel_interface, ikev2,
835 type, &alg, NULL);
836 return alg;
837 }
838
839 /**
840 * Copy a host_t as sockaddr_t to the given memory location.
841 * @return the number of bytes copied
842 */
843 static size_t hostcpy(void *dest, host_t *host, bool include_port)
844 {
845 sockaddr_t *addr = host->get_sockaddr(host), *dest_addr = dest;
846 socklen_t *len = host->get_sockaddr_len(host);
847 u_int16_t port = htons(host->get_port(host));
848
849 memcpy(dest, addr, *len);
850 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
851 dest_addr->sa_len = *len;
852 #endif
853 switch (dest_addr->sa_family)
854 {
855 case AF_INET:
856 {
857 struct sockaddr_in *sin = dest;
858 sin->sin_port = include_port ? port : 0;
859 break;
860 }
861 case AF_INET6:
862 {
863 struct sockaddr_in6 *sin6 = dest;
864 sin6->sin6_port = include_port ? port : 0;
865 break;
866 }
867 }
868 return *len;
869 }
870
871 /**
872 * add a host behind an sadb_address extension
873 */
874 static void host2ext(host_t *host, struct sadb_address *ext, bool include_port)
875 {
876 size_t len = hostcpy(ext + 1, host, include_port);
877 ext->sadb_address_len = PFKEY_LEN(sizeof(*ext) + len);
878 }
879
880 /**
881 * add a host to the given sadb_msg
882 */
883 static void add_addr_ext(struct sadb_msg *msg, host_t *host, u_int16_t type,
884 u_int8_t proto, u_int8_t prefixlen, bool include_port)
885 {
886 struct sadb_address *addr = (struct sadb_address*)PFKEY_EXT_ADD_NEXT(msg);
887 addr->sadb_address_exttype = type;
888 addr->sadb_address_proto = proto;
889 addr->sadb_address_prefixlen = prefixlen;
890 host2ext(host, addr, include_port);
891 PFKEY_EXT_ADD(msg, addr);
892 }
893
894 /**
895 * adds an empty address extension to the given sadb_msg
896 */
897 static void add_anyaddr_ext(struct sadb_msg *msg, int family, u_int8_t type)
898 {
899 socklen_t len = (family == AF_INET) ? sizeof(struct sockaddr_in) :
900 sizeof(struct sockaddr_in6);
901 struct sadb_address *addr = (struct sadb_address*)PFKEY_EXT_ADD_NEXT(msg);
902 addr->sadb_address_exttype = type;
903 sockaddr_t *saddr = (sockaddr_t*)(addr + 1);
904 saddr->sa_family = family;
905 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
906 saddr->sa_len = len;
907 #endif
908 addr->sadb_address_len = PFKEY_LEN(sizeof(*addr) + len);
909 PFKEY_EXT_ADD(msg, addr);
910 }
911
912 #ifdef HAVE_NATT
913 /**
914 * add udp encap extensions to a sadb_msg
915 */
916 static void add_encap_ext(struct sadb_msg *msg, host_t *src, host_t *dst)
917 {
918 struct sadb_x_nat_t_type* nat_type;
919 struct sadb_x_nat_t_port* nat_port;
920
921 nat_type = (struct sadb_x_nat_t_type*)PFKEY_EXT_ADD_NEXT(msg);
922 nat_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
923 nat_type->sadb_x_nat_t_type_len = PFKEY_LEN(sizeof(*nat_type));
924 nat_type->sadb_x_nat_t_type_type = UDP_ENCAP_ESPINUDP;
925 PFKEY_EXT_ADD(msg, nat_type);
926
927 nat_port = (struct sadb_x_nat_t_port*)PFKEY_EXT_ADD_NEXT(msg);
928 nat_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
929 nat_port->sadb_x_nat_t_port_len = PFKEY_LEN(sizeof(*nat_port));
930 nat_port->sadb_x_nat_t_port_port = htons(src->get_port(src));
931 PFKEY_EXT_ADD(msg, nat_port);
932
933 nat_port = (struct sadb_x_nat_t_port*)PFKEY_EXT_ADD_NEXT(msg);
934 nat_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
935 nat_port->sadb_x_nat_t_port_len = PFKEY_LEN(sizeof(*nat_port));
936 nat_port->sadb_x_nat_t_port_port = htons(dst->get_port(dst));
937 PFKEY_EXT_ADD(msg, nat_port);
938 }
939 #endif /*HAVE_NATT*/
940
941 /**
942 * Convert a sadb_address to a traffic_selector
943 */
944 static traffic_selector_t* sadb_address2ts(struct sadb_address *address)
945 {
946 traffic_selector_t *ts;
947 host_t *host;
948
949 /* The Linux 2.6 kernel does not set the protocol and port information
950 * in the src and dst sadb_address extensions of the SADB_ACQUIRE message.
951 */
952 host = host_create_from_sockaddr((sockaddr_t*)&address[1]);
953 ts = traffic_selector_create_from_subnet(host,
954 address->sadb_address_prefixlen,
955 address->sadb_address_proto,
956 host->get_port(host));
957 return ts;
958 }
959
960 /**
961 * Parses a pfkey message received from the kernel
962 */
963 static status_t parse_pfkey_message(struct sadb_msg *msg, pfkey_msg_t *out)
964 {
965 struct sadb_ext* ext;
966 size_t len;
967
968 memset(out, 0, sizeof(pfkey_msg_t));
969 out->msg = msg;
970
971 len = msg->sadb_msg_len;
972 len -= PFKEY_LEN(sizeof(struct sadb_msg));
973
974 ext = (struct sadb_ext*)(((char*)msg) + sizeof(struct sadb_msg));
975
976 while (len >= PFKEY_LEN(sizeof(struct sadb_ext)))
977 {
978 DBG3(DBG_KNL, " %N", sadb_ext_type_names, ext->sadb_ext_type);
979 if (ext->sadb_ext_len < PFKEY_LEN(sizeof(struct sadb_ext)) ||
980 ext->sadb_ext_len > len)
981 {
982 DBG1(DBG_KNL, "length of %N extension is invalid",
983 sadb_ext_type_names, ext->sadb_ext_type);
984 break;
985 }
986
987 if ((ext->sadb_ext_type > SADB_EXT_MAX) || (!ext->sadb_ext_type))
988 {
989 DBG1(DBG_KNL, "type of PF_KEY extension (%d) is invalid",
990 ext->sadb_ext_type);
991 break;
992 }
993
994 if (out->ext[ext->sadb_ext_type])
995 {
996 DBG1(DBG_KNL, "duplicate %N extension",
997 sadb_ext_type_names, ext->sadb_ext_type);
998 break;
999 }
1000
1001 out->ext[ext->sadb_ext_type] = ext;
1002 ext = PFKEY_EXT_NEXT_LEN(ext, len);
1003 }
1004
1005 if (len)
1006 {
1007 DBG1(DBG_KNL, "PF_KEY message length is invalid");
1008 return FAILED;
1009 }
1010
1011 return SUCCESS;
1012 }
1013
1014 /**
1015 * Send a message to a specific PF_KEY socket and handle the response.
1016 */
1017 static status_t pfkey_send_socket(private_kernel_pfkey_ipsec_t *this, int socket,
1018 struct sadb_msg *in, struct sadb_msg **out, size_t *out_len)
1019 {
1020 unsigned char buf[PFKEY_BUFFER_SIZE];
1021 struct sadb_msg *msg;
1022 int in_len, len;
1023
1024 this->mutex_pfkey->lock(this->mutex_pfkey);
1025
1026 /* FIXME: our usage of sequence numbers is probably wrong. check RFC 2367,
1027 * in particular the behavior in response to an SADB_ACQUIRE. */
1028 in->sadb_msg_seq = ++this->seq;
1029 in->sadb_msg_pid = getpid();
1030
1031 in_len = PFKEY_USER_LEN(in->sadb_msg_len);
1032
1033 while (TRUE)
1034 {
1035 len = send(socket, in, in_len, 0);
1036
1037 if (len != in_len)
1038 {
1039 if (errno == EINTR)
1040 {
1041 /* interrupted, try again */
1042 continue;
1043 }
1044 this->mutex_pfkey->unlock(this->mutex_pfkey);
1045 DBG1(DBG_KNL, "error sending to PF_KEY socket: %s",
1046 strerror(errno));
1047 return FAILED;
1048 }
1049 break;
1050 }
1051
1052 while (TRUE)
1053 {
1054 msg = (struct sadb_msg*)buf;
1055
1056 len = recv(socket, buf, sizeof(buf), 0);
1057
1058 if (len < 0)
1059 {
1060 if (errno == EINTR)
1061 {
1062 DBG1(DBG_KNL, "got interrupted");
1063 /* interrupted, try again */
1064 continue;
1065 }
1066 DBG1(DBG_KNL, "error reading from PF_KEY socket: %s",
1067 strerror(errno));
1068 this->mutex_pfkey->unlock(this->mutex_pfkey);
1069 return FAILED;
1070 }
1071 if (len < sizeof(struct sadb_msg) ||
1072 msg->sadb_msg_len < PFKEY_LEN(sizeof(struct sadb_msg)))
1073 {
1074 DBG1(DBG_KNL, "received corrupted PF_KEY message");
1075 this->mutex_pfkey->unlock(this->mutex_pfkey);
1076 return FAILED;
1077 }
1078 if (msg->sadb_msg_len > len / PFKEY_ALIGNMENT)
1079 {
1080 DBG1(DBG_KNL, "buffer was too small to receive the complete PF_KEY "
1081 "message");
1082 this->mutex_pfkey->unlock(this->mutex_pfkey);
1083 return FAILED;
1084 }
1085 if (msg->sadb_msg_pid != in->sadb_msg_pid)
1086 {
1087 DBG2(DBG_KNL, "received PF_KEY message is not intended for us");
1088 continue;
1089 }
1090 if (msg->sadb_msg_seq != this->seq)
1091 {
1092 DBG1(DBG_KNL, "received PF_KEY message with unexpected sequence "
1093 "number, was %d expected %d", msg->sadb_msg_seq,
1094 this->seq);
1095 if (msg->sadb_msg_seq == 0)
1096 {
1097 /* FreeBSD and Mac OS X do this for the response to
1098 * SADB_X_SPDGET (but not for the response to SADB_GET).
1099 * FreeBSD: 'key_spdget' in /usr/src/sys/netipsec/key.c. */
1100 }
1101 else if (msg->sadb_msg_seq < this->seq)
1102 {
1103 continue;
1104 }
1105 else
1106 {
1107 this->mutex_pfkey->unlock(this->mutex_pfkey);
1108 return FAILED;
1109 }
1110 }
1111 if (msg->sadb_msg_type != in->sadb_msg_type)
1112 {
1113 DBG2(DBG_KNL, "received PF_KEY message of wrong type, "
1114 "was %d expected %d, ignoring", msg->sadb_msg_type,
1115 in->sadb_msg_type);
1116 }
1117 break;
1118 }
1119
1120 *out_len = len;
1121 *out = (struct sadb_msg*)malloc(len);
1122 memcpy(*out, buf, len);
1123
1124 this->mutex_pfkey->unlock(this->mutex_pfkey);
1125 return SUCCESS;
1126 }
1127
1128 /**
1129 * Send a message to the default PF_KEY socket and handle the response.
1130 */
1131 static status_t pfkey_send(private_kernel_pfkey_ipsec_t *this,
1132 struct sadb_msg *in, struct sadb_msg **out,
1133 size_t *out_len)
1134 {
1135 return pfkey_send_socket(this, this->socket, in, out, out_len);
1136 }
1137
1138 /**
1139 * Process a SADB_ACQUIRE message from the kernel
1140 */
1141 static void process_acquire(private_kernel_pfkey_ipsec_t *this,
1142 struct sadb_msg* msg)
1143 {
1144 pfkey_msg_t response;
1145 u_int32_t index, reqid = 0;
1146 traffic_selector_t *src_ts, *dst_ts;
1147 policy_entry_t *policy;
1148 policy_sa_t *sa;
1149
1150 switch (msg->sadb_msg_satype)
1151 {
1152 case SADB_SATYPE_UNSPEC:
1153 case SADB_SATYPE_ESP:
1154 case SADB_SATYPE_AH:
1155 break;
1156 default:
1157 /* acquire for AH/ESP only */
1158 return;
1159 }
1160 DBG2(DBG_KNL, "received an SADB_ACQUIRE");
1161
1162 if (parse_pfkey_message(msg, &response) != SUCCESS)
1163 {
1164 DBG1(DBG_KNL, "parsing SADB_ACQUIRE from kernel failed");
1165 return;
1166 }
1167
1168 index = response.x_policy->sadb_x_policy_id;
1169 this->mutex->lock(this->mutex);
1170 if (this->policies->find_first(this->policies,
1171 (linked_list_match_t)policy_entry_match_byindex,
1172 (void**)&policy, &index) == SUCCESS &&
1173 policy->used_by->get_first(policy->used_by, (void**)&sa) == SUCCESS)
1174 {
1175 reqid = sa->sa->cfg.reqid;
1176 }
1177 else
1178 {
1179 DBG1(DBG_KNL, "received an SADB_ACQUIRE with policy id %d but no "
1180 "matching policy found", index);
1181 }
1182 this->mutex->unlock(this->mutex);
1183
1184 src_ts = sadb_address2ts(response.src);
1185 dst_ts = sadb_address2ts(response.dst);
1186
1187 hydra->kernel_interface->acquire(hydra->kernel_interface, reqid, src_ts,
1188 dst_ts);
1189 }
1190
1191 /**
1192 * Process a SADB_EXPIRE message from the kernel
1193 */
1194 static void process_expire(private_kernel_pfkey_ipsec_t *this,
1195 struct sadb_msg* msg)
1196 {
1197 pfkey_msg_t response;
1198 u_int8_t protocol;
1199 u_int32_t spi, reqid;
1200 bool hard;
1201
1202 DBG2(DBG_KNL, "received an SADB_EXPIRE");
1203
1204 if (parse_pfkey_message(msg, &response) != SUCCESS)
1205 {
1206 DBG1(DBG_KNL, "parsing SADB_EXPIRE from kernel failed");
1207 return;
1208 }
1209
1210 protocol = satype2proto(msg->sadb_msg_satype);
1211 spi = response.sa->sadb_sa_spi;
1212 reqid = response.x_sa2->sadb_x_sa2_reqid;
1213 hard = response.lft_hard != NULL;
1214
1215 if (protocol != IPPROTO_ESP && protocol != IPPROTO_AH)
1216 {
1217 DBG2(DBG_KNL, "ignoring SADB_EXPIRE for SA with SPI %.8x and "
1218 "reqid {%u} which is not a CHILD_SA", ntohl(spi), reqid);
1219 return;
1220 }
1221
1222 hydra->kernel_interface->expire(hydra->kernel_interface, reqid, protocol,
1223 spi, hard);
1224 }
1225
1226 #ifdef SADB_X_MIGRATE
1227 /**
1228 * Process a SADB_X_MIGRATE message from the kernel
1229 */
1230 static void process_migrate(private_kernel_pfkey_ipsec_t *this,
1231 struct sadb_msg* msg)
1232 {
1233 pfkey_msg_t response;
1234 traffic_selector_t *src_ts, *dst_ts;
1235 policy_dir_t dir;
1236 u_int32_t reqid = 0;
1237 host_t *local = NULL, *remote = NULL;
1238
1239 DBG2(DBG_KNL, "received an SADB_X_MIGRATE");
1240
1241 if (parse_pfkey_message(msg, &response) != SUCCESS)
1242 {
1243 DBG1(DBG_KNL, "parsing SADB_X_MIGRATE from kernel failed");
1244 return;
1245 }
1246 src_ts = sadb_address2ts(response.src);
1247 dst_ts = sadb_address2ts(response.dst);
1248 dir = kernel2dir(response.x_policy->sadb_x_policy_dir);
1249 DBG2(DBG_KNL, " policy %R === %R %N, id %u", src_ts, dst_ts,
1250 policy_dir_names, dir);
1251
1252 /* SADB_X_EXT_KMADDRESS is not present in unpatched kernels < 2.6.28 */
1253 if (response.x_kmaddress)
1254 {
1255 sockaddr_t *local_addr, *remote_addr;
1256 u_int32_t local_len;
1257
1258 local_addr = (sockaddr_t*)&response.x_kmaddress[1];
1259 local = host_create_from_sockaddr(local_addr);
1260 local_len = (local_addr->sa_family == AF_INET6)?
1261 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
1262 remote_addr = (sockaddr_t*)((u_int8_t*)local_addr + local_len);
1263 remote = host_create_from_sockaddr(remote_addr);
1264 DBG2(DBG_KNL, " kmaddress: %H...%H", local, remote);
1265 }
1266
1267 if (src_ts && dst_ts && local && remote)
1268 {
1269 hydra->kernel_interface->migrate(hydra->kernel_interface, reqid,
1270 src_ts, dst_ts, dir, local, remote);
1271 }
1272 else
1273 {
1274 DESTROY_IF(src_ts);
1275 DESTROY_IF(dst_ts);
1276 DESTROY_IF(local);
1277 DESTROY_IF(remote);
1278 }
1279 }
1280 #endif /*SADB_X_MIGRATE*/
1281
1282 #ifdef SADB_X_NAT_T_NEW_MAPPING
1283 /**
1284 * Process a SADB_X_NAT_T_NEW_MAPPING message from the kernel
1285 */
1286 static void process_mapping(private_kernel_pfkey_ipsec_t *this,
1287 struct sadb_msg* msg)
1288 {
1289 pfkey_msg_t response;
1290 u_int32_t spi, reqid;
1291 sockaddr_t *sa;
1292 host_t *host;
1293
1294 DBG2(DBG_KNL, "received an SADB_X_NAT_T_NEW_MAPPING");
1295
1296 if (parse_pfkey_message(msg, &response) != SUCCESS)
1297 {
1298 DBG1(DBG_KNL, "parsing SADB_X_NAT_T_NEW_MAPPING from kernel failed");
1299 return;
1300 }
1301
1302 if (!response.x_sa2)
1303 {
1304 DBG1(DBG_KNL, "received SADB_X_NAT_T_NEW_MAPPING is missing required "
1305 "information");
1306 return;
1307 }
1308
1309 spi = response.sa->sadb_sa_spi;
1310 reqid = response.x_sa2->sadb_x_sa2_reqid;
1311
1312 if (satype2proto(msg->sadb_msg_satype) != IPPROTO_ESP)
1313 {
1314 return;
1315 }
1316
1317 sa = (sockaddr_t*)(response.dst + 1);
1318 switch (sa->sa_family)
1319 {
1320 case AF_INET:
1321 {
1322 struct sockaddr_in *sin = (struct sockaddr_in*)sa;
1323 sin->sin_port = htons(response.x_natt_dport->sadb_x_nat_t_port_port);
1324 }
1325 case AF_INET6:
1326 {
1327 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
1328 sin6->sin6_port = htons(response.x_natt_dport->sadb_x_nat_t_port_port);
1329 }
1330 default:
1331 break;
1332 }
1333
1334 host = host_create_from_sockaddr(sa);
1335 if (host)
1336 {
1337 hydra->kernel_interface->mapping(hydra->kernel_interface, reqid,
1338 spi, host);
1339 }
1340 }
1341 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1342
1343 /**
1344 * Receives events from kernel
1345 */
1346 static job_requeue_t receive_events(private_kernel_pfkey_ipsec_t *this)
1347 {
1348 unsigned char buf[PFKEY_BUFFER_SIZE];
1349 struct sadb_msg *msg = (struct sadb_msg*)buf;
1350 bool oldstate;
1351 int len;
1352
1353 oldstate = thread_cancelability(TRUE);
1354 len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0);
1355 thread_cancelability(oldstate);
1356
1357 if (len < 0)
1358 {
1359 switch (errno)
1360 {
1361 case EINTR:
1362 /* interrupted, try again */
1363 return JOB_REQUEUE_DIRECT;
1364 case EAGAIN:
1365 /* no data ready, select again */
1366 return JOB_REQUEUE_DIRECT;
1367 default:
1368 DBG1(DBG_KNL, "unable to receive from PF_KEY event socket");
1369 sleep(1);
1370 return JOB_REQUEUE_FAIR;
1371 }
1372 }
1373
1374 if (len < sizeof(struct sadb_msg) ||
1375 msg->sadb_msg_len < PFKEY_LEN(sizeof(struct sadb_msg)))
1376 {
1377 DBG2(DBG_KNL, "received corrupted PF_KEY message");
1378 return JOB_REQUEUE_DIRECT;
1379 }
1380 if (msg->sadb_msg_pid != 0)
1381 { /* not from kernel. not interested, try another one */
1382 return JOB_REQUEUE_DIRECT;
1383 }
1384 if (msg->sadb_msg_len > len / PFKEY_ALIGNMENT)
1385 {
1386 DBG1(DBG_KNL, "buffer was too small to receive the complete "
1387 "PF_KEY message");
1388 return JOB_REQUEUE_DIRECT;
1389 }
1390
1391 switch (msg->sadb_msg_type)
1392 {
1393 case SADB_ACQUIRE:
1394 process_acquire(this, msg);
1395 break;
1396 case SADB_EXPIRE:
1397 process_expire(this, msg);
1398 break;
1399 #ifdef SADB_X_MIGRATE
1400 case SADB_X_MIGRATE:
1401 process_migrate(this, msg);
1402 break;
1403 #endif /*SADB_X_MIGRATE*/
1404 #ifdef SADB_X_NAT_T_NEW_MAPPING
1405 case SADB_X_NAT_T_NEW_MAPPING:
1406 process_mapping(this, msg);
1407 break;
1408 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1409 default:
1410 break;
1411 }
1412
1413 return JOB_REQUEUE_DIRECT;
1414 }
1415
1416 METHOD(kernel_ipsec_t, get_spi, status_t,
1417 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1418 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi)
1419 {
1420 unsigned char request[PFKEY_BUFFER_SIZE];
1421 struct sadb_msg *msg, *out;
1422 struct sadb_x_sa2 *sa2;
1423 struct sadb_spirange *range;
1424 pfkey_msg_t response;
1425 u_int32_t received_spi = 0;
1426 size_t len;
1427
1428 memset(&request, 0, sizeof(request));
1429
1430 msg = (struct sadb_msg*)request;
1431 msg->sadb_msg_version = PF_KEY_V2;
1432 msg->sadb_msg_type = SADB_GETSPI;
1433 msg->sadb_msg_satype = proto2satype(protocol);
1434 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1435
1436 sa2 = (struct sadb_x_sa2*)PFKEY_EXT_ADD_NEXT(msg);
1437 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
1438 sa2->sadb_x_sa2_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1439 sa2->sadb_x_sa2_reqid = reqid;
1440 PFKEY_EXT_ADD(msg, sa2);
1441
1442 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0, FALSE);
1443 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1444
1445 range = (struct sadb_spirange*)PFKEY_EXT_ADD_NEXT(msg);
1446 range->sadb_spirange_exttype = SADB_EXT_SPIRANGE;
1447 range->sadb_spirange_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1448 range->sadb_spirange_min = 0xc0000000;
1449 range->sadb_spirange_max = 0xcFFFFFFF;
1450 PFKEY_EXT_ADD(msg, range);
1451
1452 if (pfkey_send(this, msg, &out, &len) == SUCCESS)
1453 {
1454 if (out->sadb_msg_errno)
1455 {
1456 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
1457 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1458 }
1459 else if (parse_pfkey_message(out, &response) == SUCCESS)
1460 {
1461 received_spi = response.sa->sadb_sa_spi;
1462 }
1463 free(out);
1464 }
1465
1466 if (received_spi == 0)
1467 {
1468 return FAILED;
1469 }
1470
1471 *spi = received_spi;
1472 return SUCCESS;
1473 }
1474
1475 METHOD(kernel_ipsec_t, get_cpi, status_t,
1476 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1477 u_int32_t reqid, u_int16_t *cpi)
1478 {
1479 return FAILED;
1480 }
1481
1482 METHOD(kernel_ipsec_t, add_sa, status_t,
1483 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst, u_int32_t spi,
1484 u_int8_t protocol, u_int32_t reqid, mark_t mark, u_int32_t tfc,
1485 lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
1486 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode,
1487 u_int16_t ipcomp, u_int16_t cpi, bool encap, bool esn, bool inbound,
1488 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
1489 {
1490 unsigned char request[PFKEY_BUFFER_SIZE];
1491 struct sadb_msg *msg, *out;
1492 struct sadb_sa *sa;
1493 struct sadb_x_sa2 *sa2;
1494 struct sadb_lifetime *lft;
1495 struct sadb_key *key;
1496 size_t len;
1497
1498 memset(&request, 0, sizeof(request));
1499
1500 DBG2(DBG_KNL, "adding SAD entry with SPI %.8x and reqid {%u}",
1501 ntohl(spi), reqid);
1502
1503 msg = (struct sadb_msg*)request;
1504 msg->sadb_msg_version = PF_KEY_V2;
1505 msg->sadb_msg_type = inbound ? SADB_UPDATE : SADB_ADD;
1506 msg->sadb_msg_satype = proto2satype(protocol);
1507 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1508
1509 #ifdef __APPLE__
1510 if (encap)
1511 {
1512 struct sadb_sa_2 *sa_2;
1513 sa_2 = (struct sadb_sa_2*)PFKEY_EXT_ADD_NEXT(msg);
1514 sa_2->sadb_sa_natt_port = dst->get_port(dst);
1515 sa = &sa_2->sa;
1516 sa->sadb_sa_flags |= SADB_X_EXT_NATT;
1517 len = sizeof(struct sadb_sa_2);
1518 }
1519 else
1520 #endif
1521 {
1522 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1523 len = sizeof(struct sadb_sa);
1524 }
1525 sa->sadb_sa_exttype = SADB_EXT_SA;
1526 sa->sadb_sa_len = PFKEY_LEN(len);
1527 sa->sadb_sa_spi = spi;
1528 sa->sadb_sa_replay = (protocol == IPPROTO_COMP) ? 0 : 32;
1529 sa->sadb_sa_auth = lookup_algorithm(INTEGRITY_ALGORITHM, int_alg);
1530 sa->sadb_sa_encrypt = lookup_algorithm(ENCRYPTION_ALGORITHM, enc_alg);
1531 PFKEY_EXT_ADD(msg, sa);
1532
1533 sa2 = (struct sadb_x_sa2*)PFKEY_EXT_ADD_NEXT(msg);
1534 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
1535 sa2->sadb_x_sa2_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1536 sa2->sadb_x_sa2_mode = mode2kernel(mode);
1537 sa2->sadb_x_sa2_reqid = reqid;
1538 PFKEY_EXT_ADD(msg, sa2);
1539
1540 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0, FALSE);
1541 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1542
1543 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1544 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1545 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1546 lft->sadb_lifetime_allocations = lifetime->packets.rekey;
1547 lft->sadb_lifetime_bytes = lifetime->bytes.rekey;
1548 lft->sadb_lifetime_addtime = lifetime->time.rekey;
1549 lft->sadb_lifetime_usetime = 0; /* we only use addtime */
1550 PFKEY_EXT_ADD(msg, lft);
1551
1552 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1553 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1554 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1555 lft->sadb_lifetime_allocations = lifetime->packets.life;
1556 lft->sadb_lifetime_bytes = lifetime->bytes.life;
1557 lft->sadb_lifetime_addtime = lifetime->time.life;
1558 lft->sadb_lifetime_usetime = 0; /* we only use addtime */
1559 PFKEY_EXT_ADD(msg, lft);
1560
1561 if (enc_alg != ENCR_UNDEFINED)
1562 {
1563 if (!sa->sadb_sa_encrypt)
1564 {
1565 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1566 encryption_algorithm_names, enc_alg);
1567 return FAILED;
1568 }
1569 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1570 encryption_algorithm_names, enc_alg, enc_key.len * 8);
1571
1572 key = (struct sadb_key*)PFKEY_EXT_ADD_NEXT(msg);
1573 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
1574 key->sadb_key_bits = enc_key.len * 8;
1575 key->sadb_key_len = PFKEY_LEN(sizeof(struct sadb_key) + enc_key.len);
1576 memcpy(key + 1, enc_key.ptr, enc_key.len);
1577
1578 PFKEY_EXT_ADD(msg, key);
1579 }
1580
1581 if (int_alg != AUTH_UNDEFINED)
1582 {
1583 if (!sa->sadb_sa_auth)
1584 {
1585 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1586 integrity_algorithm_names, int_alg);
1587 return FAILED;
1588 }
1589 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1590 integrity_algorithm_names, int_alg, int_key.len * 8);
1591
1592 key = (struct sadb_key*)PFKEY_EXT_ADD_NEXT(msg);
1593 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
1594 key->sadb_key_bits = int_key.len * 8;
1595 key->sadb_key_len = PFKEY_LEN(sizeof(struct sadb_key) + int_key.len);
1596 memcpy(key + 1, int_key.ptr, int_key.len);
1597
1598 PFKEY_EXT_ADD(msg, key);
1599 }
1600
1601 if (ipcomp != IPCOMP_NONE)
1602 {
1603 /*TODO*/
1604 }
1605
1606 #ifdef HAVE_NATT
1607 if (encap)
1608 {
1609 add_encap_ext(msg, src, dst);
1610 }
1611 #endif /*HAVE_NATT*/
1612
1613 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1614 {
1615 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x", ntohl(spi));
1616 return FAILED;
1617 }
1618 else if (out->sadb_msg_errno)
1619 {
1620 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x: %s (%d)",
1621 ntohl(spi), strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1622 free(out);
1623 return FAILED;
1624 }
1625
1626 free(out);
1627 return SUCCESS;
1628 }
1629
1630 METHOD(kernel_ipsec_t, update_sa, status_t,
1631 private_kernel_pfkey_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
1632 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
1633 bool encap, bool new_encap, mark_t mark)
1634 {
1635 unsigned char request[PFKEY_BUFFER_SIZE];
1636 struct sadb_msg *msg, *out;
1637 struct sadb_sa *sa;
1638 pfkey_msg_t response;
1639 size_t len;
1640
1641 /* we can't update the SA if any of the ip addresses have changed.
1642 * that's because we can't use SADB_UPDATE and by deleting and readding the
1643 * SA the sequence numbers would get lost */
1644 if (!src->ip_equals(src, new_src) ||
1645 !dst->ip_equals(dst, new_dst))
1646 {
1647 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x: address "
1648 "changes are not supported", ntohl(spi));
1649 return NOT_SUPPORTED;
1650 }
1651
1652 memset(&request, 0, sizeof(request));
1653
1654 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x", ntohl(spi));
1655
1656 msg = (struct sadb_msg*)request;
1657 msg->sadb_msg_version = PF_KEY_V2;
1658 msg->sadb_msg_type = SADB_GET;
1659 msg->sadb_msg_satype = proto2satype(protocol);
1660 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1661
1662 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1663 sa->sadb_sa_exttype = SADB_EXT_SA;
1664 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1665 sa->sadb_sa_spi = spi;
1666 PFKEY_EXT_ADD(msg, sa);
1667
1668 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1669 * it is not used for anything. */
1670 add_anyaddr_ext(msg, dst->get_family(dst), SADB_EXT_ADDRESS_SRC);
1671 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1672
1673 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1674 {
1675 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1676 return FAILED;
1677 }
1678 else if (out->sadb_msg_errno)
1679 {
1680 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: %s (%d)",
1681 ntohl(spi), strerror(out->sadb_msg_errno),
1682 out->sadb_msg_errno);
1683 free(out);
1684 return FAILED;
1685 }
1686 else if (parse_pfkey_message(out, &response) != SUCCESS)
1687 {
1688 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: parsing "
1689 "response from kernel failed", ntohl(spi));
1690 free(out);
1691 return FAILED;
1692 }
1693
1694 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1695 ntohl(spi), src, dst, new_src, new_dst);
1696
1697 memset(&request, 0, sizeof(request));
1698
1699 msg = (struct sadb_msg*)request;
1700 msg->sadb_msg_version = PF_KEY_V2;
1701 msg->sadb_msg_type = SADB_UPDATE;
1702 msg->sadb_msg_satype = proto2satype(protocol);
1703 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1704
1705 #ifdef __APPLE__
1706 {
1707 struct sadb_sa_2 *sa_2;
1708 sa_2 = (struct sadb_sa_2*)PFKEY_EXT_ADD_NEXT(msg);
1709 sa_2->sa.sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa_2));
1710 memcpy(&sa_2->sa, response.sa, sizeof(struct sadb_sa));
1711 if (encap)
1712 {
1713 sa_2->sadb_sa_natt_port = new_dst->get_port(new_dst);
1714 sa_2->sa.sadb_sa_flags |= SADB_X_EXT_NATT;
1715 }
1716 }
1717 #else
1718 PFKEY_EXT_COPY(msg, response.sa);
1719 #endif
1720 PFKEY_EXT_COPY(msg, response.x_sa2);
1721
1722 PFKEY_EXT_COPY(msg, response.src);
1723 PFKEY_EXT_COPY(msg, response.dst);
1724
1725 PFKEY_EXT_COPY(msg, response.lft_soft);
1726 PFKEY_EXT_COPY(msg, response.lft_hard);
1727
1728 if (response.key_encr)
1729 {
1730 PFKEY_EXT_COPY(msg, response.key_encr);
1731 }
1732
1733 if (response.key_auth)
1734 {
1735 PFKEY_EXT_COPY(msg, response.key_auth);
1736 }
1737
1738 #ifdef HAVE_NATT
1739 if (new_encap)
1740 {
1741 add_encap_ext(msg, new_src, new_dst);
1742 }
1743 #endif /*HAVE_NATT*/
1744
1745 free(out);
1746
1747 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1748 {
1749 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1750 return FAILED;
1751 }
1752 else if (out->sadb_msg_errno)
1753 {
1754 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x: %s (%d)",
1755 ntohl(spi), strerror(out->sadb_msg_errno),
1756 out->sadb_msg_errno);
1757 free(out);
1758 return FAILED;
1759 }
1760 free(out);
1761
1762 return SUCCESS;
1763 }
1764
1765 METHOD(kernel_ipsec_t, query_sa, status_t,
1766 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1767 u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes)
1768 {
1769 unsigned char request[PFKEY_BUFFER_SIZE];
1770 struct sadb_msg *msg, *out;
1771 struct sadb_sa *sa;
1772 pfkey_msg_t response;
1773 size_t len;
1774
1775 memset(&request, 0, sizeof(request));
1776
1777 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x", ntohl(spi));
1778
1779 msg = (struct sadb_msg*)request;
1780 msg->sadb_msg_version = PF_KEY_V2;
1781 msg->sadb_msg_type = SADB_GET;
1782 msg->sadb_msg_satype = proto2satype(protocol);
1783 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1784
1785 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1786 sa->sadb_sa_exttype = SADB_EXT_SA;
1787 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1788 sa->sadb_sa_spi = spi;
1789 PFKEY_EXT_ADD(msg, sa);
1790
1791 /* the Linux Kernel doesn't care for the src address, but other systems do
1792 * (e.g. FreeBSD)
1793 */
1794 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0, FALSE);
1795 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1796
1797 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1798 {
1799 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1800 return FAILED;
1801 }
1802 else if (out->sadb_msg_errno)
1803 {
1804 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: %s (%d)",
1805 ntohl(spi), strerror(out->sadb_msg_errno),
1806 out->sadb_msg_errno);
1807 free(out);
1808 return FAILED;
1809 }
1810 else if (parse_pfkey_message(out, &response) != SUCCESS)
1811 {
1812 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1813 free(out);
1814 return FAILED;
1815 }
1816 *bytes = response.lft_current->sadb_lifetime_bytes;
1817
1818 free(out);
1819 return SUCCESS;
1820 }
1821
1822 METHOD(kernel_ipsec_t, del_sa, status_t,
1823 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1824 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
1825 {
1826 unsigned char request[PFKEY_BUFFER_SIZE];
1827 struct sadb_msg *msg, *out;
1828 struct sadb_sa *sa;
1829 size_t len;
1830
1831 memset(&request, 0, sizeof(request));
1832
1833 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x", ntohl(spi));
1834
1835 msg = (struct sadb_msg*)request;
1836 msg->sadb_msg_version = PF_KEY_V2;
1837 msg->sadb_msg_type = SADB_DELETE;
1838 msg->sadb_msg_satype = proto2satype(protocol);
1839 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1840
1841 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1842 sa->sadb_sa_exttype = SADB_EXT_SA;
1843 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1844 sa->sadb_sa_spi = spi;
1845 PFKEY_EXT_ADD(msg, sa);
1846
1847 /* the Linux Kernel doesn't care for the src address, but other systems do
1848 * (e.g. FreeBSD)
1849 */
1850 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0, FALSE);
1851 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1852
1853 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1854 {
1855 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x", ntohl(spi));
1856 return FAILED;
1857 }
1858 else if (out->sadb_msg_errno)
1859 {
1860 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1861 ntohl(spi), strerror(out->sadb_msg_errno),
1862 out->sadb_msg_errno);
1863 free(out);
1864 return FAILED;
1865 }
1866
1867 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x", ntohl(spi));
1868 free(out);
1869 return SUCCESS;
1870 }
1871
1872 METHOD(kernel_ipsec_t, flush_sas, status_t,
1873 private_kernel_pfkey_ipsec_t *this)
1874 {
1875 unsigned char request[PFKEY_BUFFER_SIZE];
1876 struct sadb_msg *msg, *out;
1877 size_t len;
1878
1879 memset(&request, 0, sizeof(request));
1880
1881 DBG2(DBG_KNL, "flushing all SAD entries");
1882
1883 msg = (struct sadb_msg*)request;
1884 msg->sadb_msg_version = PF_KEY_V2;
1885 msg->sadb_msg_type = SADB_FLUSH;
1886 msg->sadb_msg_satype = SADB_SATYPE_UNSPEC;
1887 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1888
1889 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1890 {
1891 DBG1(DBG_KNL, "unable to flush SAD entries");
1892 return FAILED;
1893 }
1894 else if (out->sadb_msg_errno)
1895 {
1896 DBG1(DBG_KNL, "unable to flush SAD entries: %s (%d)",
1897 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1898 free(out);
1899 return FAILED;
1900 }
1901 free(out);
1902 return SUCCESS;
1903 }
1904
1905 /**
1906 * Add or update a policy in the kernel.
1907 *
1908 * Note: The mutex has to be locked when entering this function.
1909 */
1910 static status_t add_policy_internal(private_kernel_pfkey_ipsec_t *this,
1911 policy_entry_t *policy, policy_sa_t *mapping, bool update)
1912 {
1913 unsigned char request[PFKEY_BUFFER_SIZE];
1914 struct sadb_msg *msg, *out;
1915 struct sadb_x_policy *pol;
1916 struct sadb_x_ipsecrequest *req;
1917 ipsec_sa_t *ipsec = mapping->sa;
1918 pfkey_msg_t response;
1919 size_t len;
1920
1921 memset(&request, 0, sizeof(request));
1922
1923 msg = (struct sadb_msg*)request;
1924 msg->sadb_msg_version = PF_KEY_V2;
1925 msg->sadb_msg_type = update ? SADB_X_SPDUPDATE : SADB_X_SPDADD;
1926 msg->sadb_msg_satype = 0;
1927 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1928
1929 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
1930 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1931 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
1932 pol->sadb_x_policy_id = 0;
1933 pol->sadb_x_policy_dir = dir2kernel(policy->direction);
1934 pol->sadb_x_policy_type = type2kernel(mapping->type);
1935 #ifdef HAVE_STRUCT_SADB_X_POLICY_SADB_X_POLICY_PRIORITY
1936 pol->sadb_x_policy_priority = mapping->priority;
1937 #endif
1938
1939 /* one or more sadb_x_ipsecrequest extensions are added to the
1940 * sadb_x_policy extension */
1941 req = (struct sadb_x_ipsecrequest*)(pol + 1);
1942 req->sadb_x_ipsecrequest_proto = ipsec->cfg.esp.use ? IPPROTO_ESP
1943 : IPPROTO_AH;
1944 /* !!! the length here MUST be in octets instead of 64 bit words */
1945 req->sadb_x_ipsecrequest_len = sizeof(struct sadb_x_ipsecrequest);
1946 req->sadb_x_ipsecrequest_mode = mode2kernel(ipsec->cfg.mode);
1947 req->sadb_x_ipsecrequest_reqid = ipsec->cfg.reqid;
1948 req->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1949 if (ipsec->cfg.mode == MODE_TUNNEL)
1950 {
1951 len = hostcpy(req + 1, ipsec->src, FALSE);
1952 req->sadb_x_ipsecrequest_len += len;
1953 len = hostcpy((char*)(req + 1) + len, ipsec->dst, FALSE);
1954 req->sadb_x_ipsecrequest_len += len;
1955 }
1956
1957 pol->sadb_x_policy_len += PFKEY_LEN(req->sadb_x_ipsecrequest_len);
1958 PFKEY_EXT_ADD(msg, pol);
1959
1960 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
1961 policy->src.mask, TRUE);
1962 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
1963 policy->dst.mask, TRUE);
1964
1965 #ifdef __FreeBSD__
1966 { /* on FreeBSD a lifetime has to be defined to be able to later query
1967 * the current use time. */
1968 struct sadb_lifetime *lft;
1969 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1970 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1971 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1972 lft->sadb_lifetime_addtime = LONG_MAX;
1973 PFKEY_EXT_ADD(msg, lft);
1974 }
1975 #endif
1976
1977 this->mutex->unlock(this->mutex);
1978
1979 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1980 {
1981 return FAILED;
1982 }
1983 else if (out->sadb_msg_errno)
1984 {
1985 DBG1(DBG_KNL, "unable to %s policy: %s (%d)",
1986 update ? "update" : "add", strerror(out->sadb_msg_errno),
1987 out->sadb_msg_errno);
1988 free(out);
1989 return FAILED;
1990 }
1991 else if (parse_pfkey_message(out, &response) != SUCCESS)
1992 {
1993 DBG1(DBG_KNL, "unable to %s policy: parsing response from kernel "
1994 "failed", update ? "update" : "add");
1995 free(out);
1996 return FAILED;
1997 }
1998
1999 /* we try to find the policy again and update the kernel index */
2000 this->mutex->lock(this->mutex);
2001 if (this->policies->find_last(this->policies, NULL,
2002 (void**)&policy) != SUCCESS)
2003 {
2004 DBG2(DBG_KNL, "unable to update index, the policy is already gone, "
2005 "ignoring");
2006 this->mutex->unlock(this->mutex);
2007 free(out);
2008 return SUCCESS;
2009 }
2010 policy->index = response.x_policy->sadb_x_policy_id;
2011 free(out);
2012
2013 /* install a route, if:
2014 * - this is a forward policy (to just get one for each child)
2015 * - we are in tunnel mode
2016 * - routing is not disabled via strongswan.conf
2017 */
2018 if (policy->direction == POLICY_FWD &&
2019 ipsec->cfg.mode != MODE_TRANSPORT && this->install_routes)
2020 {
2021 route_entry_t *route = malloc_thing(route_entry_t);
2022 policy_sa_fwd_t *fwd = (policy_sa_fwd_t*)mapping;
2023
2024 if (hydra->kernel_interface->get_address_by_ts(hydra->kernel_interface,
2025 fwd->dst_ts, &route->src_ip) == SUCCESS)
2026 {
2027 /* get the nexthop to src (src as we are in POLICY_FWD).*/
2028 route->gateway = hydra->kernel_interface->get_nexthop(
2029 hydra->kernel_interface, ipsec->src,
2030 ipsec->dst);
2031 /* install route via outgoing interface */
2032 route->if_name = hydra->kernel_interface->get_interface(
2033 hydra->kernel_interface, ipsec->dst);
2034 route->dst_net = chunk_clone(policy->src.net->get_address(
2035 policy->src.net));
2036 route->prefixlen = policy->src.mask;
2037
2038 if (!route->if_name)
2039 {
2040 this->mutex->unlock(this->mutex);
2041 route_entry_destroy(route);
2042 return SUCCESS;
2043 }
2044
2045 if (policy->route)
2046 {
2047 route_entry_t *old = policy->route;
2048 if (route_entry_equals(old, route))
2049 {
2050 this->mutex->unlock(this->mutex);
2051 route_entry_destroy(route);
2052 return SUCCESS;
2053 }
2054 /* uninstall previously installed route */
2055 if (hydra->kernel_interface->del_route(hydra->kernel_interface,
2056 old->dst_net, old->prefixlen, old->gateway,
2057 old->src_ip, old->if_name) != SUCCESS)
2058 {
2059 DBG1(DBG_KNL, "error uninstalling route installed with "
2060 "policy %R === %R %N", fwd->src_ts,
2061 fwd->dst_ts, policy_dir_names,
2062 policy->direction);
2063 }
2064 route_entry_destroy(old);
2065 policy->route = NULL;
2066 }
2067
2068 DBG2(DBG_KNL, "installing route: %R via %H src %H dev %s",
2069 fwd->src_ts, route->gateway, route->src_ip, route->if_name);
2070 switch (hydra->kernel_interface->add_route(
2071 hydra->kernel_interface, route->dst_net,
2072 route->prefixlen, route->gateway,
2073 route->src_ip, route->if_name))
2074 {
2075 default:
2076 DBG1(DBG_KNL, "unable to install source route for %H",
2077 route->src_ip);
2078 /* FALL */
2079 case ALREADY_DONE:
2080 /* route exists, do not uninstall */
2081 route_entry_destroy(route);
2082 break;
2083 case SUCCESS:
2084 /* cache the installed route */
2085 policy->route = route;
2086 break;
2087 }
2088 }
2089 else
2090 {
2091 free(route);
2092 }
2093 }
2094 this->mutex->unlock(this->mutex);
2095 return SUCCESS;
2096 }
2097
2098 METHOD(kernel_ipsec_t, add_policy, status_t,
2099 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
2100 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
2101 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
2102 mark_t mark, policy_priority_t priority)
2103 {
2104 policy_entry_t *policy, *found = NULL;
2105 policy_sa_t *assigned_sa, *current_sa;
2106 enumerator_t *enumerator;
2107 bool update = TRUE;
2108
2109 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
2110 { /* FWD policies are not supported on all platforms */
2111 return SUCCESS;
2112 }
2113
2114 /* create a policy */
2115 policy = create_policy_entry(src_ts, dst_ts, direction);
2116
2117 /* find a matching policy */
2118 this->mutex->lock(this->mutex);
2119 if (this->policies->find_first(this->policies,
2120 (linked_list_match_t)policy_entry_equals,
2121 (void**)&found, policy) == SUCCESS)
2122 { /* use existing policy */
2123 DBG2(DBG_KNL, "policy %R === %R %N already exists, increasing "
2124 "refcount", src_ts, dst_ts, policy_dir_names, direction);
2125 policy_entry_destroy(policy, this);
2126 policy = found;
2127 }
2128 else
2129 { /* use the new one, if we have no such policy */
2130 this->policies->insert_last(this->policies, policy);
2131 policy->used_by = linked_list_create();
2132 }
2133
2134 /* cache the assigned IPsec SA */
2135 assigned_sa = policy_sa_create(this, direction, type, src, dst, src_ts,
2136 dst_ts, sa);
2137 assigned_sa->priority = get_priority(policy, priority);
2138
2139 /* insert the SA according to its priority */
2140 enumerator = policy->used_by->create_enumerator(policy->used_by);
2141 while (enumerator->enumerate(enumerator, (void**)&current_sa))
2142 {
2143 if (current_sa->priority >= assigned_sa->priority)
2144 {
2145 break;
2146 }
2147 update = FALSE;
2148 }
2149 policy->used_by->insert_before(policy->used_by, enumerator, assigned_sa);
2150 enumerator->destroy(enumerator);
2151
2152 if (!update)
2153 { /* we don't update the policy if the priority is lower than that of the
2154 * currently installed one */
2155 this->mutex->unlock(this->mutex);
2156 return SUCCESS;
2157 }
2158
2159 DBG2(DBG_KNL, "%s policy %R === %R %N",
2160 found ? "updating" : "adding", src_ts, dst_ts,
2161 policy_dir_names, direction);
2162
2163 if (add_policy_internal(this, policy, assigned_sa, found) != SUCCESS)
2164 {
2165 DBG1(DBG_KNL, "unable to %s policy %R === %R %N",
2166 found ? "update" : "add", src_ts, dst_ts,
2167 policy_dir_names, direction);
2168 return FAILED;
2169 }
2170 return SUCCESS;
2171 }
2172
2173 METHOD(kernel_ipsec_t, query_policy, status_t,
2174 private_kernel_pfkey_ipsec_t *this, traffic_selector_t *src_ts,
2175 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
2176 u_int32_t *use_time)
2177 {
2178 unsigned char request[PFKEY_BUFFER_SIZE];
2179 struct sadb_msg *msg, *out;
2180 struct sadb_x_policy *pol;
2181 policy_entry_t *policy, *found = NULL;
2182 pfkey_msg_t response;
2183 size_t len;
2184
2185 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
2186 { /* FWD policies are not supported on all platforms */
2187 return NOT_FOUND;
2188 }
2189
2190 DBG2(DBG_KNL, "querying policy %R === %R %N", src_ts, dst_ts,
2191 policy_dir_names, direction);
2192
2193 /* create a policy */
2194 policy = create_policy_entry(src_ts, dst_ts, direction);
2195
2196 /* find a matching policy */
2197 this->mutex->lock(this->mutex);
2198 if (this->policies->find_first(this->policies,
2199 (linked_list_match_t)policy_entry_equals,
2200 (void**)&found, policy) != SUCCESS)
2201 {
2202 DBG1(DBG_KNL, "querying policy %R === %R %N failed, not found", src_ts,
2203 dst_ts, policy_dir_names, direction);
2204 policy_entry_destroy(policy, this);
2205 this->mutex->unlock(this->mutex);
2206 return NOT_FOUND;
2207 }
2208 policy_entry_destroy(policy, this);
2209 policy = found;
2210
2211 memset(&request, 0, sizeof(request));
2212
2213 msg = (struct sadb_msg*)request;
2214 msg->sadb_msg_version = PF_KEY_V2;
2215 msg->sadb_msg_type = SADB_X_SPDGET;
2216 msg->sadb_msg_satype = 0;
2217 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2218
2219 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
2220 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2221 pol->sadb_x_policy_id = policy->index;
2222 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
2223 pol->sadb_x_policy_dir = dir2kernel(direction);
2224 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2225 PFKEY_EXT_ADD(msg, pol);
2226
2227 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
2228 policy->src.mask, TRUE);
2229 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
2230 policy->dst.mask, TRUE);
2231
2232 this->mutex->unlock(this->mutex);
2233
2234 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
2235 {
2236 DBG1(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
2237 policy_dir_names, direction);
2238 return FAILED;
2239 }
2240 else if (out->sadb_msg_errno)
2241 {
2242 DBG1(DBG_KNL, "unable to query policy %R === %R %N: %s (%d)", src_ts,
2243 dst_ts, policy_dir_names, direction,
2244 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2245 free(out);
2246 return FAILED;
2247 }
2248 else if (parse_pfkey_message(out, &response) != SUCCESS)
2249 {
2250 DBG1(DBG_KNL, "unable to query policy %R === %R %N: parsing response "
2251 "from kernel failed", src_ts, dst_ts, policy_dir_names,
2252 direction);
2253 free(out);
2254 return FAILED;
2255 }
2256 else if (response.lft_current == NULL)
2257 {
2258 DBG1(DBG_KNL, "unable to query policy %R === %R %N: kernel reports no "
2259 "use time", src_ts, dst_ts, policy_dir_names, direction);
2260 free(out);
2261 return FAILED;
2262 }
2263
2264 /* we need the monotonic time, but the kernel returns system time. */
2265 if (response.lft_current->sadb_lifetime_usetime)
2266 {
2267 *use_time = time_monotonic(NULL) -
2268 (time(NULL) - response.lft_current->sadb_lifetime_usetime);
2269 }
2270 else
2271 {
2272 *use_time = 0;
2273 }
2274 free(out);
2275 return SUCCESS;
2276 }
2277
2278 METHOD(kernel_ipsec_t, del_policy, status_t,
2279 private_kernel_pfkey_ipsec_t *this, traffic_selector_t *src_ts,
2280 traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
2281 mark_t mark, policy_priority_t prio)
2282 {
2283 unsigned char request[PFKEY_BUFFER_SIZE];
2284 struct sadb_msg *msg, *out;
2285 struct sadb_x_policy *pol;
2286 policy_entry_t *policy, *found = NULL;
2287 policy_sa_t *mapping;
2288 enumerator_t *enumerator;
2289 bool is_installed = TRUE;
2290 u_int32_t priority;
2291 size_t len;
2292
2293 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
2294 { /* FWD policies are not supported on all platforms */
2295 return SUCCESS;
2296 }
2297
2298 DBG2(DBG_KNL, "deleting policy %R === %R %N", src_ts, dst_ts,
2299 policy_dir_names, direction);
2300
2301 /* create a policy */
2302 policy = create_policy_entry(src_ts, dst_ts, direction);
2303
2304 /* find a matching policy */
2305 this->mutex->lock(this->mutex);
2306 if (this->policies->find_first(this->policies,
2307 (linked_list_match_t)policy_entry_equals,
2308 (void**)&found, policy) != SUCCESS)
2309 {
2310 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
2311 dst_ts, policy_dir_names, direction);
2312 policy_entry_destroy(policy, this);
2313 this->mutex->unlock(this->mutex);
2314 return NOT_FOUND;
2315 }
2316 policy_entry_destroy(policy, this);
2317 policy = found;
2318
2319 /* remove mapping to SA by reqid and priority */
2320 priority = get_priority(policy, prio);
2321 enumerator = policy->used_by->create_enumerator(policy->used_by);
2322 while (enumerator->enumerate(enumerator, (void**)&mapping))
2323 {
2324 if (reqid == mapping->sa->cfg.reqid && priority == mapping->priority)
2325 {
2326 policy->used_by->remove_at(policy->used_by, enumerator);
2327 break;
2328 }
2329 is_installed = FALSE;
2330 }
2331 enumerator->destroy(enumerator);
2332
2333 if (policy->used_by->get_count(policy->used_by) > 0)
2334 { /* policy is used by more SAs, keep in kernel */
2335 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
2336 policy_sa_destroy(mapping, &direction, this);
2337
2338 if (!is_installed)
2339 { /* no need to update as the policy was not installed for this SA */
2340 this->mutex->unlock(this->mutex);
2341 return SUCCESS;
2342 }
2343
2344 DBG2(DBG_KNL, "updating policy %R === %R %N", src_ts, dst_ts,
2345 policy_dir_names, direction);
2346 policy->used_by->get_first(policy->used_by, (void**)&mapping);
2347 if (add_policy_internal(this, policy, mapping, TRUE) != SUCCESS)
2348 {
2349 DBG1(DBG_KNL, "unable to update policy %R === %R %N",
2350 src_ts, dst_ts, policy_dir_names, direction);
2351 return FAILED;
2352 }
2353 return SUCCESS;
2354 }
2355
2356 memset(&request, 0, sizeof(request));
2357
2358 msg = (struct sadb_msg*)request;
2359 msg->sadb_msg_version = PF_KEY_V2;
2360 msg->sadb_msg_type = SADB_X_SPDDELETE;
2361 msg->sadb_msg_satype = 0;
2362 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2363
2364 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
2365 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2366 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
2367 pol->sadb_x_policy_dir = dir2kernel(direction);
2368 pol->sadb_x_policy_type = type2kernel(mapping->type);
2369 PFKEY_EXT_ADD(msg, pol);
2370
2371 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
2372 policy->src.mask, TRUE);
2373 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
2374 policy->dst.mask, TRUE);
2375
2376 if (policy->route)
2377 {
2378 route_entry_t *route = policy->route;
2379 if (hydra->kernel_interface->del_route(hydra->kernel_interface,
2380 route->dst_net, route->prefixlen, route->gateway,
2381 route->src_ip, route->if_name) != SUCCESS)
2382 {
2383 DBG1(DBG_KNL, "error uninstalling route installed with "
2384 "policy %R === %R %N", src_ts, dst_ts,
2385 policy_dir_names, direction);
2386 }
2387 }
2388
2389 this->policies->remove(this->policies, found, NULL);
2390 policy_sa_destroy(mapping, &direction, this);
2391 policy_entry_destroy(policy, this);
2392 this->mutex->unlock(this->mutex);
2393
2394 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
2395 {
2396 DBG1(DBG_KNL, "unable to delete policy %R === %R %N", src_ts, dst_ts,
2397 policy_dir_names, direction);
2398 return FAILED;
2399 }
2400 else if (out->sadb_msg_errno)
2401 {
2402 DBG1(DBG_KNL, "unable to delete policy %R === %R %N: %s (%d)", src_ts,
2403 dst_ts, policy_dir_names, direction,
2404 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2405 free(out);
2406 return FAILED;
2407 }
2408 free(out);
2409 return SUCCESS;
2410 }
2411
2412 METHOD(kernel_ipsec_t, flush_policies, status_t,
2413 private_kernel_pfkey_ipsec_t *this)
2414 {
2415 unsigned char request[PFKEY_BUFFER_SIZE];
2416 struct sadb_msg *msg, *out;
2417 size_t len;
2418
2419 memset(&request, 0, sizeof(request));
2420
2421 DBG2(DBG_KNL, "flushing all policies from SPD");
2422
2423 msg = (struct sadb_msg*)request;
2424 msg->sadb_msg_version = PF_KEY_V2;
2425 msg->sadb_msg_type = SADB_X_SPDFLUSH;
2426 msg->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2427 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2428
2429 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
2430 {
2431 DBG1(DBG_KNL, "unable to flush SPD entries");
2432 return FAILED;
2433 }
2434 else if (out->sadb_msg_errno)
2435 {
2436 DBG1(DBG_KNL, "unable to flush SPD entries: %s (%d)",
2437 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2438 free(out);
2439 return FAILED;
2440 }
2441 free(out);
2442 return SUCCESS;
2443 }
2444
2445 /**
2446 * Register a socket for ACQUIRE/EXPIRE messages
2447 */
2448 static status_t register_pfkey_socket(private_kernel_pfkey_ipsec_t *this,
2449 u_int8_t satype)
2450 {
2451 unsigned char request[PFKEY_BUFFER_SIZE];
2452 struct sadb_msg *msg, *out;
2453 size_t len;
2454
2455 memset(&request, 0, sizeof(request));
2456
2457 msg = (struct sadb_msg*)request;
2458 msg->sadb_msg_version = PF_KEY_V2;
2459 msg->sadb_msg_type = SADB_REGISTER;
2460 msg->sadb_msg_satype = satype;
2461 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2462
2463 if (pfkey_send_socket(this, this->socket_events, msg, &out, &len) != SUCCESS)
2464 {
2465 DBG1(DBG_KNL, "unable to register PF_KEY socket");
2466 return FAILED;
2467 }
2468 else if (out->sadb_msg_errno)
2469 {
2470 DBG1(DBG_KNL, "unable to register PF_KEY socket: %s (%d)",
2471 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2472 free(out);
2473 return FAILED;
2474 }
2475 free(out);
2476 return SUCCESS;
2477 }
2478
2479 METHOD(kernel_ipsec_t, bypass_socket, bool,
2480 private_kernel_pfkey_ipsec_t *this, int fd, int family)
2481 {
2482 struct sadb_x_policy policy;
2483 u_int sol, ipsec_policy;
2484
2485 switch (family)
2486 {
2487 case AF_INET:
2488 {
2489 sol = SOL_IP;
2490 ipsec_policy = IP_IPSEC_POLICY;
2491 break;
2492 }
2493 case AF_INET6:
2494 {
2495 sol = SOL_IPV6;
2496 ipsec_policy = IPV6_IPSEC_POLICY;
2497 break;
2498 }
2499 default:
2500 return FALSE;
2501 }
2502
2503 memset(&policy, 0, sizeof(policy));
2504 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);
2505 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2506 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;
2507
2508 policy.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;
2509 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2510 {
2511 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2512 strerror(errno));
2513 return FALSE;
2514 }
2515 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;
2516 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2517 {
2518 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2519 strerror(errno));
2520 return FALSE;
2521 }
2522 return TRUE;
2523 }
2524
2525 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
2526 private_kernel_pfkey_ipsec_t *this, int fd, int family, u_int16_t port)
2527 {
2528 #ifndef __APPLE__
2529 int type = UDP_ENCAP_ESPINUDP;
2530
2531 if (setsockopt(fd, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
2532 {
2533 DBG1(DBG_KNL, "unable to set UDP_ENCAP: %s", strerror(errno));
2534 return FALSE;
2535 }
2536 #else /* __APPLE__ */
2537 if (sysctlbyname("net.inet.ipsec.esp_port", NULL, NULL, &port,
2538 sizeof(port)) != 0)
2539 {
2540 DBG1(DBG_KNL, "could not set net.inet.ipsec.esp_port to %d: %s",
2541 port, strerror(errno));
2542 return FALSE;
2543 }
2544 #endif /* __APPLE__ */
2545
2546 return TRUE;
2547 }
2548
2549 METHOD(kernel_ipsec_t, destroy, void,
2550 private_kernel_pfkey_ipsec_t *this)
2551 {
2552 if (this->socket > 0)
2553 {
2554 close(this->socket);
2555 }
2556 if (this->socket_events > 0)
2557 {
2558 close(this->socket_events);
2559 }
2560 this->policies->invoke_function(this->policies,
2561 (linked_list_invoke_t)policy_entry_destroy,
2562 this);
2563 this->policies->destroy(this->policies);
2564 this->sas->destroy(this->sas);
2565 this->mutex->destroy(this->mutex);
2566 this->mutex_pfkey->destroy(this->mutex_pfkey);
2567 free(this);
2568 }
2569
2570 /*
2571 * Described in header.
2572 */
2573 kernel_pfkey_ipsec_t *kernel_pfkey_ipsec_create()
2574 {
2575 private_kernel_pfkey_ipsec_t *this;
2576 bool register_for_events = TRUE;
2577
2578 INIT(this,
2579 .public = {
2580 .interface = {
2581 .get_spi = _get_spi,
2582 .get_cpi = _get_cpi,
2583 .add_sa = _add_sa,
2584 .update_sa = _update_sa,
2585 .query_sa = _query_sa,
2586 .del_sa = _del_sa,
2587 .flush_sas = _flush_sas,
2588 .add_policy = _add_policy,
2589 .query_policy = _query_policy,
2590 .del_policy = _del_policy,
2591 .flush_policies = _flush_policies,
2592 .bypass_socket = _bypass_socket,
2593 .enable_udp_decap = _enable_udp_decap,
2594 .destroy = _destroy,
2595 },
2596 },
2597 .policies = linked_list_create(),
2598 .sas = hashtable_create((hashtable_hash_t)ipsec_sa_hash,
2599 (hashtable_equals_t)ipsec_sa_equals, 32),
2600 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
2601 .mutex_pfkey = mutex_create(MUTEX_TYPE_DEFAULT),
2602 .install_routes = lib->settings->get_bool(lib->settings,
2603 "%s.install_routes", TRUE,
2604 hydra->daemon),
2605 );
2606
2607 if (streq(hydra->daemon, "pluto"))
2608 { /* no routes for pluto, they are installed via updown script */
2609 this->install_routes = FALSE;
2610 }
2611 else if (streq(hydra->daemon, "starter"))
2612 { /* starter has no threads, so we do not register for kernel events */
2613 register_for_events = FALSE;
2614 }
2615
2616 /* create a PF_KEY socket to communicate with the kernel */
2617 this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
2618 if (this->socket <= 0)
2619 {
2620 DBG1(DBG_KNL, "unable to create PF_KEY socket");
2621 destroy(this);
2622 return NULL;
2623 }
2624
2625 if (register_for_events)
2626 {
2627 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
2628 this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
2629 if (this->socket_events <= 0)
2630 {
2631 DBG1(DBG_KNL, "unable to create PF_KEY event socket");
2632 destroy(this);
2633 return NULL;
2634 }
2635
2636 /* register the event socket */
2637 if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS ||
2638 register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS)
2639 {
2640 DBG1(DBG_KNL, "unable to register PF_KEY event socket");
2641 destroy(this);
2642 return NULL;
2643 }
2644
2645 lib->processor->queue_job(lib->processor,
2646 (job_t*)callback_job_create_with_prio(
2647 (callback_job_cb_t)receive_events, this, NULL,
2648 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
2649 }
2650
2651 return &this->public;
2652 }
2653