Moved data structures to new collections subfolder
[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 <networking/host.h>
63 #include <collections/linked_list.h>
64 #include <collections/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 break;
1325 }
1326 case AF_INET6:
1327 {
1328 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
1329 sin6->sin6_port = htons(response.x_natt_dport->sadb_x_nat_t_port_port);
1330 break;
1331 }
1332 default:
1333 break;
1334 }
1335
1336 host = host_create_from_sockaddr(sa);
1337 if (host)
1338 {
1339 hydra->kernel_interface->mapping(hydra->kernel_interface, reqid,
1340 spi, host);
1341 }
1342 }
1343 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1344
1345 /**
1346 * Receives events from kernel
1347 */
1348 static job_requeue_t receive_events(private_kernel_pfkey_ipsec_t *this)
1349 {
1350 unsigned char buf[PFKEY_BUFFER_SIZE];
1351 struct sadb_msg *msg = (struct sadb_msg*)buf;
1352 bool oldstate;
1353 int len;
1354
1355 oldstate = thread_cancelability(TRUE);
1356 len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0);
1357 thread_cancelability(oldstate);
1358
1359 if (len < 0)
1360 {
1361 switch (errno)
1362 {
1363 case EINTR:
1364 /* interrupted, try again */
1365 return JOB_REQUEUE_DIRECT;
1366 case EAGAIN:
1367 /* no data ready, select again */
1368 return JOB_REQUEUE_DIRECT;
1369 default:
1370 DBG1(DBG_KNL, "unable to receive from PF_KEY event socket");
1371 sleep(1);
1372 return JOB_REQUEUE_FAIR;
1373 }
1374 }
1375
1376 if (len < sizeof(struct sadb_msg) ||
1377 msg->sadb_msg_len < PFKEY_LEN(sizeof(struct sadb_msg)))
1378 {
1379 DBG2(DBG_KNL, "received corrupted PF_KEY message");
1380 return JOB_REQUEUE_DIRECT;
1381 }
1382 if (msg->sadb_msg_pid != 0)
1383 { /* not from kernel. not interested, try another one */
1384 return JOB_REQUEUE_DIRECT;
1385 }
1386 if (msg->sadb_msg_len > len / PFKEY_ALIGNMENT)
1387 {
1388 DBG1(DBG_KNL, "buffer was too small to receive the complete "
1389 "PF_KEY message");
1390 return JOB_REQUEUE_DIRECT;
1391 }
1392
1393 switch (msg->sadb_msg_type)
1394 {
1395 case SADB_ACQUIRE:
1396 process_acquire(this, msg);
1397 break;
1398 case SADB_EXPIRE:
1399 process_expire(this, msg);
1400 break;
1401 #ifdef SADB_X_MIGRATE
1402 case SADB_X_MIGRATE:
1403 process_migrate(this, msg);
1404 break;
1405 #endif /*SADB_X_MIGRATE*/
1406 #ifdef SADB_X_NAT_T_NEW_MAPPING
1407 case SADB_X_NAT_T_NEW_MAPPING:
1408 process_mapping(this, msg);
1409 break;
1410 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1411 default:
1412 break;
1413 }
1414
1415 return JOB_REQUEUE_DIRECT;
1416 }
1417
1418 METHOD(kernel_ipsec_t, get_spi, status_t,
1419 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1420 u_int8_t protocol, u_int32_t reqid, u_int32_t *spi)
1421 {
1422 unsigned char request[PFKEY_BUFFER_SIZE];
1423 struct sadb_msg *msg, *out;
1424 struct sadb_x_sa2 *sa2;
1425 struct sadb_spirange *range;
1426 pfkey_msg_t response;
1427 u_int32_t received_spi = 0;
1428 size_t len;
1429
1430 memset(&request, 0, sizeof(request));
1431
1432 msg = (struct sadb_msg*)request;
1433 msg->sadb_msg_version = PF_KEY_V2;
1434 msg->sadb_msg_type = SADB_GETSPI;
1435 msg->sadb_msg_satype = proto2satype(protocol);
1436 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1437
1438 sa2 = (struct sadb_x_sa2*)PFKEY_EXT_ADD_NEXT(msg);
1439 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
1440 sa2->sadb_x_sa2_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1441 sa2->sadb_x_sa2_reqid = reqid;
1442 PFKEY_EXT_ADD(msg, sa2);
1443
1444 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0, FALSE);
1445 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1446
1447 range = (struct sadb_spirange*)PFKEY_EXT_ADD_NEXT(msg);
1448 range->sadb_spirange_exttype = SADB_EXT_SPIRANGE;
1449 range->sadb_spirange_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1450 range->sadb_spirange_min = 0xc0000000;
1451 range->sadb_spirange_max = 0xcFFFFFFF;
1452 PFKEY_EXT_ADD(msg, range);
1453
1454 if (pfkey_send(this, msg, &out, &len) == SUCCESS)
1455 {
1456 if (out->sadb_msg_errno)
1457 {
1458 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
1459 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1460 }
1461 else if (parse_pfkey_message(out, &response) == SUCCESS)
1462 {
1463 received_spi = response.sa->sadb_sa_spi;
1464 }
1465 free(out);
1466 }
1467
1468 if (received_spi == 0)
1469 {
1470 return FAILED;
1471 }
1472
1473 *spi = received_spi;
1474 return SUCCESS;
1475 }
1476
1477 METHOD(kernel_ipsec_t, get_cpi, status_t,
1478 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1479 u_int32_t reqid, u_int16_t *cpi)
1480 {
1481 return FAILED;
1482 }
1483
1484 METHOD(kernel_ipsec_t, add_sa, status_t,
1485 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst, u_int32_t spi,
1486 u_int8_t protocol, u_int32_t reqid, mark_t mark, u_int32_t tfc,
1487 lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
1488 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode,
1489 u_int16_t ipcomp, u_int16_t cpi, bool encap, bool esn, bool inbound,
1490 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
1491 {
1492 unsigned char request[PFKEY_BUFFER_SIZE];
1493 struct sadb_msg *msg, *out;
1494 struct sadb_sa *sa;
1495 struct sadb_x_sa2 *sa2;
1496 struct sadb_lifetime *lft;
1497 struct sadb_key *key;
1498 size_t len;
1499
1500 memset(&request, 0, sizeof(request));
1501
1502 DBG2(DBG_KNL, "adding SAD entry with SPI %.8x and reqid {%u}",
1503 ntohl(spi), reqid);
1504
1505 msg = (struct sadb_msg*)request;
1506 msg->sadb_msg_version = PF_KEY_V2;
1507 msg->sadb_msg_type = inbound ? SADB_UPDATE : SADB_ADD;
1508 msg->sadb_msg_satype = proto2satype(protocol);
1509 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1510
1511 #ifdef __APPLE__
1512 if (encap)
1513 {
1514 struct sadb_sa_2 *sa_2;
1515 sa_2 = (struct sadb_sa_2*)PFKEY_EXT_ADD_NEXT(msg);
1516 sa_2->sadb_sa_natt_port = dst->get_port(dst);
1517 sa = &sa_2->sa;
1518 sa->sadb_sa_flags |= SADB_X_EXT_NATT;
1519 len = sizeof(struct sadb_sa_2);
1520 }
1521 else
1522 #endif
1523 {
1524 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1525 len = sizeof(struct sadb_sa);
1526 }
1527 sa->sadb_sa_exttype = SADB_EXT_SA;
1528 sa->sadb_sa_len = PFKEY_LEN(len);
1529 sa->sadb_sa_spi = spi;
1530 sa->sadb_sa_replay = (protocol == IPPROTO_COMP) ? 0 : 32;
1531 sa->sadb_sa_auth = lookup_algorithm(INTEGRITY_ALGORITHM, int_alg);
1532 sa->sadb_sa_encrypt = lookup_algorithm(ENCRYPTION_ALGORITHM, enc_alg);
1533 PFKEY_EXT_ADD(msg, sa);
1534
1535 sa2 = (struct sadb_x_sa2*)PFKEY_EXT_ADD_NEXT(msg);
1536 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
1537 sa2->sadb_x_sa2_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1538 sa2->sadb_x_sa2_mode = mode2kernel(mode);
1539 sa2->sadb_x_sa2_reqid = reqid;
1540 PFKEY_EXT_ADD(msg, sa2);
1541
1542 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0, FALSE);
1543 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1544
1545 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1546 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1547 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1548 lft->sadb_lifetime_allocations = lifetime->packets.rekey;
1549 lft->sadb_lifetime_bytes = lifetime->bytes.rekey;
1550 lft->sadb_lifetime_addtime = lifetime->time.rekey;
1551 lft->sadb_lifetime_usetime = 0; /* we only use addtime */
1552 PFKEY_EXT_ADD(msg, lft);
1553
1554 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1555 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1556 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1557 lft->sadb_lifetime_allocations = lifetime->packets.life;
1558 lft->sadb_lifetime_bytes = lifetime->bytes.life;
1559 lft->sadb_lifetime_addtime = lifetime->time.life;
1560 lft->sadb_lifetime_usetime = 0; /* we only use addtime */
1561 PFKEY_EXT_ADD(msg, lft);
1562
1563 if (enc_alg != ENCR_UNDEFINED)
1564 {
1565 if (!sa->sadb_sa_encrypt)
1566 {
1567 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1568 encryption_algorithm_names, enc_alg);
1569 return FAILED;
1570 }
1571 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1572 encryption_algorithm_names, enc_alg, enc_key.len * 8);
1573
1574 key = (struct sadb_key*)PFKEY_EXT_ADD_NEXT(msg);
1575 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
1576 key->sadb_key_bits = enc_key.len * 8;
1577 key->sadb_key_len = PFKEY_LEN(sizeof(struct sadb_key) + enc_key.len);
1578 memcpy(key + 1, enc_key.ptr, enc_key.len);
1579
1580 PFKEY_EXT_ADD(msg, key);
1581 }
1582
1583 if (int_alg != AUTH_UNDEFINED)
1584 {
1585 if (!sa->sadb_sa_auth)
1586 {
1587 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1588 integrity_algorithm_names, int_alg);
1589 return FAILED;
1590 }
1591 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1592 integrity_algorithm_names, int_alg, int_key.len * 8);
1593
1594 key = (struct sadb_key*)PFKEY_EXT_ADD_NEXT(msg);
1595 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
1596 key->sadb_key_bits = int_key.len * 8;
1597 key->sadb_key_len = PFKEY_LEN(sizeof(struct sadb_key) + int_key.len);
1598 memcpy(key + 1, int_key.ptr, int_key.len);
1599
1600 PFKEY_EXT_ADD(msg, key);
1601 }
1602
1603 if (ipcomp != IPCOMP_NONE)
1604 {
1605 /*TODO*/
1606 }
1607
1608 #ifdef HAVE_NATT
1609 if (encap)
1610 {
1611 add_encap_ext(msg, src, dst);
1612 }
1613 #endif /*HAVE_NATT*/
1614
1615 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1616 {
1617 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x", ntohl(spi));
1618 return FAILED;
1619 }
1620 else if (out->sadb_msg_errno)
1621 {
1622 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x: %s (%d)",
1623 ntohl(spi), strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1624 free(out);
1625 return FAILED;
1626 }
1627
1628 free(out);
1629 return SUCCESS;
1630 }
1631
1632 METHOD(kernel_ipsec_t, update_sa, status_t,
1633 private_kernel_pfkey_ipsec_t *this, u_int32_t spi, u_int8_t protocol,
1634 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
1635 bool encap, bool new_encap, mark_t mark)
1636 {
1637 unsigned char request[PFKEY_BUFFER_SIZE];
1638 struct sadb_msg *msg, *out;
1639 struct sadb_sa *sa;
1640 pfkey_msg_t response;
1641 size_t len;
1642
1643 /* we can't update the SA if any of the ip addresses have changed.
1644 * that's because we can't use SADB_UPDATE and by deleting and readding the
1645 * SA the sequence numbers would get lost */
1646 if (!src->ip_equals(src, new_src) ||
1647 !dst->ip_equals(dst, new_dst))
1648 {
1649 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x: address "
1650 "changes are not supported", ntohl(spi));
1651 return NOT_SUPPORTED;
1652 }
1653
1654 memset(&request, 0, sizeof(request));
1655
1656 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x", ntohl(spi));
1657
1658 msg = (struct sadb_msg*)request;
1659 msg->sadb_msg_version = PF_KEY_V2;
1660 msg->sadb_msg_type = SADB_GET;
1661 msg->sadb_msg_satype = proto2satype(protocol);
1662 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1663
1664 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1665 sa->sadb_sa_exttype = SADB_EXT_SA;
1666 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1667 sa->sadb_sa_spi = spi;
1668 PFKEY_EXT_ADD(msg, sa);
1669
1670 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1671 * it is not used for anything. */
1672 add_anyaddr_ext(msg, dst->get_family(dst), SADB_EXT_ADDRESS_SRC);
1673 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1674
1675 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1676 {
1677 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1678 return FAILED;
1679 }
1680 else if (out->sadb_msg_errno)
1681 {
1682 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: %s (%d)",
1683 ntohl(spi), strerror(out->sadb_msg_errno),
1684 out->sadb_msg_errno);
1685 free(out);
1686 return FAILED;
1687 }
1688 else if (parse_pfkey_message(out, &response) != SUCCESS)
1689 {
1690 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: parsing "
1691 "response from kernel failed", ntohl(spi));
1692 free(out);
1693 return FAILED;
1694 }
1695
1696 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1697 ntohl(spi), src, dst, new_src, new_dst);
1698
1699 memset(&request, 0, sizeof(request));
1700
1701 msg = (struct sadb_msg*)request;
1702 msg->sadb_msg_version = PF_KEY_V2;
1703 msg->sadb_msg_type = SADB_UPDATE;
1704 msg->sadb_msg_satype = proto2satype(protocol);
1705 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1706
1707 #ifdef __APPLE__
1708 {
1709 struct sadb_sa_2 *sa_2;
1710 sa_2 = (struct sadb_sa_2*)PFKEY_EXT_ADD_NEXT(msg);
1711 sa_2->sa.sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa_2));
1712 memcpy(&sa_2->sa, response.sa, sizeof(struct sadb_sa));
1713 if (encap)
1714 {
1715 sa_2->sadb_sa_natt_port = new_dst->get_port(new_dst);
1716 sa_2->sa.sadb_sa_flags |= SADB_X_EXT_NATT;
1717 }
1718 }
1719 #else
1720 PFKEY_EXT_COPY(msg, response.sa);
1721 #endif
1722 PFKEY_EXT_COPY(msg, response.x_sa2);
1723
1724 PFKEY_EXT_COPY(msg, response.src);
1725 PFKEY_EXT_COPY(msg, response.dst);
1726
1727 PFKEY_EXT_COPY(msg, response.lft_soft);
1728 PFKEY_EXT_COPY(msg, response.lft_hard);
1729
1730 if (response.key_encr)
1731 {
1732 PFKEY_EXT_COPY(msg, response.key_encr);
1733 }
1734
1735 if (response.key_auth)
1736 {
1737 PFKEY_EXT_COPY(msg, response.key_auth);
1738 }
1739
1740 #ifdef HAVE_NATT
1741 if (new_encap)
1742 {
1743 add_encap_ext(msg, new_src, new_dst);
1744 }
1745 #endif /*HAVE_NATT*/
1746
1747 free(out);
1748
1749 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1750 {
1751 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1752 return FAILED;
1753 }
1754 else if (out->sadb_msg_errno)
1755 {
1756 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x: %s (%d)",
1757 ntohl(spi), strerror(out->sadb_msg_errno),
1758 out->sadb_msg_errno);
1759 free(out);
1760 return FAILED;
1761 }
1762 free(out);
1763
1764 return SUCCESS;
1765 }
1766
1767 METHOD(kernel_ipsec_t, query_sa, status_t,
1768 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1769 u_int32_t spi, u_int8_t protocol, mark_t mark, u_int64_t *bytes)
1770 {
1771 unsigned char request[PFKEY_BUFFER_SIZE];
1772 struct sadb_msg *msg, *out;
1773 struct sadb_sa *sa;
1774 pfkey_msg_t response;
1775 size_t len;
1776
1777 memset(&request, 0, sizeof(request));
1778
1779 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x", ntohl(spi));
1780
1781 msg = (struct sadb_msg*)request;
1782 msg->sadb_msg_version = PF_KEY_V2;
1783 msg->sadb_msg_type = SADB_GET;
1784 msg->sadb_msg_satype = proto2satype(protocol);
1785 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1786
1787 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1788 sa->sadb_sa_exttype = SADB_EXT_SA;
1789 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1790 sa->sadb_sa_spi = spi;
1791 PFKEY_EXT_ADD(msg, sa);
1792
1793 /* the Linux Kernel doesn't care for the src address, but other systems do
1794 * (e.g. FreeBSD)
1795 */
1796 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0, FALSE);
1797 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1798
1799 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1800 {
1801 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1802 return FAILED;
1803 }
1804 else if (out->sadb_msg_errno)
1805 {
1806 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: %s (%d)",
1807 ntohl(spi), strerror(out->sadb_msg_errno),
1808 out->sadb_msg_errno);
1809 free(out);
1810 return FAILED;
1811 }
1812 else if (parse_pfkey_message(out, &response) != SUCCESS)
1813 {
1814 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1815 free(out);
1816 return FAILED;
1817 }
1818 *bytes = response.lft_current->sadb_lifetime_bytes;
1819
1820 free(out);
1821 return SUCCESS;
1822 }
1823
1824 METHOD(kernel_ipsec_t, del_sa, status_t,
1825 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1826 u_int32_t spi, u_int8_t protocol, u_int16_t cpi, mark_t mark)
1827 {
1828 unsigned char request[PFKEY_BUFFER_SIZE];
1829 struct sadb_msg *msg, *out;
1830 struct sadb_sa *sa;
1831 size_t len;
1832
1833 memset(&request, 0, sizeof(request));
1834
1835 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x", ntohl(spi));
1836
1837 msg = (struct sadb_msg*)request;
1838 msg->sadb_msg_version = PF_KEY_V2;
1839 msg->sadb_msg_type = SADB_DELETE;
1840 msg->sadb_msg_satype = proto2satype(protocol);
1841 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1842
1843 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1844 sa->sadb_sa_exttype = SADB_EXT_SA;
1845 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1846 sa->sadb_sa_spi = spi;
1847 PFKEY_EXT_ADD(msg, sa);
1848
1849 /* the Linux Kernel doesn't care for the src address, but other systems do
1850 * (e.g. FreeBSD)
1851 */
1852 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0, FALSE);
1853 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0, FALSE);
1854
1855 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1856 {
1857 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x", ntohl(spi));
1858 return FAILED;
1859 }
1860 else if (out->sadb_msg_errno)
1861 {
1862 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1863 ntohl(spi), strerror(out->sadb_msg_errno),
1864 out->sadb_msg_errno);
1865 free(out);
1866 return FAILED;
1867 }
1868
1869 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x", ntohl(spi));
1870 free(out);
1871 return SUCCESS;
1872 }
1873
1874 METHOD(kernel_ipsec_t, flush_sas, status_t,
1875 private_kernel_pfkey_ipsec_t *this)
1876 {
1877 unsigned char request[PFKEY_BUFFER_SIZE];
1878 struct sadb_msg *msg, *out;
1879 size_t len;
1880
1881 memset(&request, 0, sizeof(request));
1882
1883 DBG2(DBG_KNL, "flushing all SAD entries");
1884
1885 msg = (struct sadb_msg*)request;
1886 msg->sadb_msg_version = PF_KEY_V2;
1887 msg->sadb_msg_type = SADB_FLUSH;
1888 msg->sadb_msg_satype = SADB_SATYPE_UNSPEC;
1889 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1890
1891 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1892 {
1893 DBG1(DBG_KNL, "unable to flush SAD entries");
1894 return FAILED;
1895 }
1896 else if (out->sadb_msg_errno)
1897 {
1898 DBG1(DBG_KNL, "unable to flush SAD entries: %s (%d)",
1899 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1900 free(out);
1901 return FAILED;
1902 }
1903 free(out);
1904 return SUCCESS;
1905 }
1906
1907 /**
1908 * Add or update a policy in the kernel.
1909 *
1910 * Note: The mutex has to be locked when entering this function.
1911 */
1912 static status_t add_policy_internal(private_kernel_pfkey_ipsec_t *this,
1913 policy_entry_t *policy, policy_sa_t *mapping, bool update)
1914 {
1915 unsigned char request[PFKEY_BUFFER_SIZE];
1916 struct sadb_msg *msg, *out;
1917 struct sadb_x_policy *pol;
1918 struct sadb_x_ipsecrequest *req;
1919 ipsec_sa_t *ipsec = mapping->sa;
1920 pfkey_msg_t response;
1921 size_t len;
1922
1923 memset(&request, 0, sizeof(request));
1924
1925 msg = (struct sadb_msg*)request;
1926 msg->sadb_msg_version = PF_KEY_V2;
1927 msg->sadb_msg_type = update ? SADB_X_SPDUPDATE : SADB_X_SPDADD;
1928 msg->sadb_msg_satype = 0;
1929 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1930
1931 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
1932 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1933 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
1934 pol->sadb_x_policy_id = 0;
1935 pol->sadb_x_policy_dir = dir2kernel(policy->direction);
1936 pol->sadb_x_policy_type = type2kernel(mapping->type);
1937 #ifdef HAVE_STRUCT_SADB_X_POLICY_SADB_X_POLICY_PRIORITY
1938 pol->sadb_x_policy_priority = mapping->priority;
1939 #endif
1940
1941 /* one or more sadb_x_ipsecrequest extensions are added to the
1942 * sadb_x_policy extension */
1943 req = (struct sadb_x_ipsecrequest*)(pol + 1);
1944 req->sadb_x_ipsecrequest_proto = ipsec->cfg.esp.use ? IPPROTO_ESP
1945 : IPPROTO_AH;
1946 /* !!! the length here MUST be in octets instead of 64 bit words */
1947 req->sadb_x_ipsecrequest_len = sizeof(struct sadb_x_ipsecrequest);
1948 req->sadb_x_ipsecrequest_mode = mode2kernel(ipsec->cfg.mode);
1949 req->sadb_x_ipsecrequest_reqid = ipsec->cfg.reqid;
1950 req->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1951 if (ipsec->cfg.mode == MODE_TUNNEL)
1952 {
1953 len = hostcpy(req + 1, ipsec->src, FALSE);
1954 req->sadb_x_ipsecrequest_len += len;
1955 len = hostcpy((char*)(req + 1) + len, ipsec->dst, FALSE);
1956 req->sadb_x_ipsecrequest_len += len;
1957 }
1958
1959 pol->sadb_x_policy_len += PFKEY_LEN(req->sadb_x_ipsecrequest_len);
1960 PFKEY_EXT_ADD(msg, pol);
1961
1962 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
1963 policy->src.mask, TRUE);
1964 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
1965 policy->dst.mask, TRUE);
1966
1967 #ifdef __FreeBSD__
1968 { /* on FreeBSD a lifetime has to be defined to be able to later query
1969 * the current use time. */
1970 struct sadb_lifetime *lft;
1971 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1972 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1973 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1974 lft->sadb_lifetime_addtime = LONG_MAX;
1975 PFKEY_EXT_ADD(msg, lft);
1976 }
1977 #endif
1978
1979 this->mutex->unlock(this->mutex);
1980
1981 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1982 {
1983 return FAILED;
1984 }
1985 else if (out->sadb_msg_errno)
1986 {
1987 DBG1(DBG_KNL, "unable to %s policy: %s (%d)",
1988 update ? "update" : "add", strerror(out->sadb_msg_errno),
1989 out->sadb_msg_errno);
1990 free(out);
1991 return FAILED;
1992 }
1993 else if (parse_pfkey_message(out, &response) != SUCCESS)
1994 {
1995 DBG1(DBG_KNL, "unable to %s policy: parsing response from kernel "
1996 "failed", update ? "update" : "add");
1997 free(out);
1998 return FAILED;
1999 }
2000
2001 /* we try to find the policy again and update the kernel index */
2002 this->mutex->lock(this->mutex);
2003 if (this->policies->find_last(this->policies, NULL,
2004 (void**)&policy) != SUCCESS)
2005 {
2006 DBG2(DBG_KNL, "unable to update index, the policy is already gone, "
2007 "ignoring");
2008 this->mutex->unlock(this->mutex);
2009 free(out);
2010 return SUCCESS;
2011 }
2012 policy->index = response.x_policy->sadb_x_policy_id;
2013 free(out);
2014
2015 /* install a route, if:
2016 * - this is a forward policy (to just get one for each child)
2017 * - we are in tunnel mode
2018 * - routing is not disabled via strongswan.conf
2019 */
2020 if (policy->direction == POLICY_FWD &&
2021 ipsec->cfg.mode != MODE_TRANSPORT && this->install_routes)
2022 {
2023 policy_sa_fwd_t *fwd = (policy_sa_fwd_t*)mapping;
2024 route_entry_t *route;
2025
2026 INIT(route,
2027 .prefixlen = policy->src.mask,
2028 );
2029
2030 if (hydra->kernel_interface->get_address_by_ts(hydra->kernel_interface,
2031 fwd->dst_ts, &route->src_ip) == SUCCESS)
2032 {
2033 /* get the nexthop to src (src as we are in POLICY_FWD).*/
2034 route->gateway = hydra->kernel_interface->get_nexthop(
2035 hydra->kernel_interface, ipsec->src,
2036 ipsec->dst);
2037 route->dst_net = chunk_clone(policy->src.net->get_address(
2038 policy->src.net));
2039
2040 /* install route via outgoing interface */
2041 if (!hydra->kernel_interface->get_interface(hydra->kernel_interface,
2042 ipsec->dst, &route->if_name))
2043 {
2044 this->mutex->unlock(this->mutex);
2045 route_entry_destroy(route);
2046 return SUCCESS;
2047 }
2048
2049 if (policy->route)
2050 {
2051 route_entry_t *old = policy->route;
2052 if (route_entry_equals(old, route))
2053 {
2054 this->mutex->unlock(this->mutex);
2055 route_entry_destroy(route);
2056 return SUCCESS;
2057 }
2058 /* uninstall previously installed route */
2059 if (hydra->kernel_interface->del_route(hydra->kernel_interface,
2060 old->dst_net, old->prefixlen, old->gateway,
2061 old->src_ip, old->if_name) != SUCCESS)
2062 {
2063 DBG1(DBG_KNL, "error uninstalling route installed with "
2064 "policy %R === %R %N", fwd->src_ts,
2065 fwd->dst_ts, policy_dir_names,
2066 policy->direction);
2067 }
2068 route_entry_destroy(old);
2069 policy->route = NULL;
2070 }
2071
2072 DBG2(DBG_KNL, "installing route: %R via %H src %H dev %s",
2073 fwd->src_ts, route->gateway, route->src_ip, route->if_name);
2074 switch (hydra->kernel_interface->add_route(
2075 hydra->kernel_interface, route->dst_net,
2076 route->prefixlen, route->gateway,
2077 route->src_ip, route->if_name))
2078 {
2079 default:
2080 DBG1(DBG_KNL, "unable to install source route for %H",
2081 route->src_ip);
2082 /* FALL */
2083 case ALREADY_DONE:
2084 /* route exists, do not uninstall */
2085 route_entry_destroy(route);
2086 break;
2087 case SUCCESS:
2088 /* cache the installed route */
2089 policy->route = route;
2090 break;
2091 }
2092 }
2093 else
2094 {
2095 free(route);
2096 }
2097 }
2098 this->mutex->unlock(this->mutex);
2099 return SUCCESS;
2100 }
2101
2102 METHOD(kernel_ipsec_t, add_policy, status_t,
2103 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
2104 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
2105 policy_dir_t direction, policy_type_t type, ipsec_sa_cfg_t *sa,
2106 mark_t mark, policy_priority_t priority)
2107 {
2108 policy_entry_t *policy, *found = NULL;
2109 policy_sa_t *assigned_sa, *current_sa;
2110 enumerator_t *enumerator;
2111 bool update = TRUE;
2112
2113 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
2114 { /* FWD policies are not supported on all platforms */
2115 return SUCCESS;
2116 }
2117
2118 /* create a policy */
2119 policy = create_policy_entry(src_ts, dst_ts, direction);
2120
2121 /* find a matching policy */
2122 this->mutex->lock(this->mutex);
2123 if (this->policies->find_first(this->policies,
2124 (linked_list_match_t)policy_entry_equals,
2125 (void**)&found, policy) == SUCCESS)
2126 { /* use existing policy */
2127 DBG2(DBG_KNL, "policy %R === %R %N already exists, increasing "
2128 "refcount", src_ts, dst_ts, policy_dir_names, direction);
2129 policy_entry_destroy(policy, this);
2130 policy = found;
2131 }
2132 else
2133 { /* use the new one, if we have no such policy */
2134 this->policies->insert_last(this->policies, policy);
2135 policy->used_by = linked_list_create();
2136 }
2137
2138 /* cache the assigned IPsec SA */
2139 assigned_sa = policy_sa_create(this, direction, type, src, dst, src_ts,
2140 dst_ts, sa);
2141 assigned_sa->priority = get_priority(policy, priority);
2142
2143 /* insert the SA according to its priority */
2144 enumerator = policy->used_by->create_enumerator(policy->used_by);
2145 while (enumerator->enumerate(enumerator, (void**)&current_sa))
2146 {
2147 if (current_sa->priority >= assigned_sa->priority)
2148 {
2149 break;
2150 }
2151 update = FALSE;
2152 }
2153 policy->used_by->insert_before(policy->used_by, enumerator, assigned_sa);
2154 enumerator->destroy(enumerator);
2155
2156 if (!update)
2157 { /* we don't update the policy if the priority is lower than that of the
2158 * currently installed one */
2159 this->mutex->unlock(this->mutex);
2160 return SUCCESS;
2161 }
2162
2163 DBG2(DBG_KNL, "%s policy %R === %R %N",
2164 found ? "updating" : "adding", src_ts, dst_ts,
2165 policy_dir_names, direction);
2166
2167 if (add_policy_internal(this, policy, assigned_sa, found) != SUCCESS)
2168 {
2169 DBG1(DBG_KNL, "unable to %s policy %R === %R %N",
2170 found ? "update" : "add", src_ts, dst_ts,
2171 policy_dir_names, direction);
2172 return FAILED;
2173 }
2174 return SUCCESS;
2175 }
2176
2177 METHOD(kernel_ipsec_t, query_policy, status_t,
2178 private_kernel_pfkey_ipsec_t *this, traffic_selector_t *src_ts,
2179 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
2180 u_int32_t *use_time)
2181 {
2182 unsigned char request[PFKEY_BUFFER_SIZE];
2183 struct sadb_msg *msg, *out;
2184 struct sadb_x_policy *pol;
2185 policy_entry_t *policy, *found = NULL;
2186 pfkey_msg_t response;
2187 size_t len;
2188
2189 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
2190 { /* FWD policies are not supported on all platforms */
2191 return NOT_FOUND;
2192 }
2193
2194 DBG2(DBG_KNL, "querying policy %R === %R %N", src_ts, dst_ts,
2195 policy_dir_names, direction);
2196
2197 /* create a policy */
2198 policy = create_policy_entry(src_ts, dst_ts, direction);
2199
2200 /* find a matching policy */
2201 this->mutex->lock(this->mutex);
2202 if (this->policies->find_first(this->policies,
2203 (linked_list_match_t)policy_entry_equals,
2204 (void**)&found, policy) != SUCCESS)
2205 {
2206 DBG1(DBG_KNL, "querying policy %R === %R %N failed, not found", src_ts,
2207 dst_ts, policy_dir_names, direction);
2208 policy_entry_destroy(policy, this);
2209 this->mutex->unlock(this->mutex);
2210 return NOT_FOUND;
2211 }
2212 policy_entry_destroy(policy, this);
2213 policy = found;
2214
2215 memset(&request, 0, sizeof(request));
2216
2217 msg = (struct sadb_msg*)request;
2218 msg->sadb_msg_version = PF_KEY_V2;
2219 msg->sadb_msg_type = SADB_X_SPDGET;
2220 msg->sadb_msg_satype = 0;
2221 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2222
2223 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
2224 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2225 pol->sadb_x_policy_id = policy->index;
2226 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
2227 pol->sadb_x_policy_dir = dir2kernel(direction);
2228 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2229 PFKEY_EXT_ADD(msg, pol);
2230
2231 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
2232 policy->src.mask, TRUE);
2233 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
2234 policy->dst.mask, TRUE);
2235
2236 this->mutex->unlock(this->mutex);
2237
2238 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
2239 {
2240 DBG1(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
2241 policy_dir_names, direction);
2242 return FAILED;
2243 }
2244 else if (out->sadb_msg_errno)
2245 {
2246 DBG1(DBG_KNL, "unable to query policy %R === %R %N: %s (%d)", src_ts,
2247 dst_ts, policy_dir_names, direction,
2248 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2249 free(out);
2250 return FAILED;
2251 }
2252 else if (parse_pfkey_message(out, &response) != SUCCESS)
2253 {
2254 DBG1(DBG_KNL, "unable to query policy %R === %R %N: parsing response "
2255 "from kernel failed", src_ts, dst_ts, policy_dir_names,
2256 direction);
2257 free(out);
2258 return FAILED;
2259 }
2260 else if (response.lft_current == NULL)
2261 {
2262 DBG1(DBG_KNL, "unable to query policy %R === %R %N: kernel reports no "
2263 "use time", src_ts, dst_ts, policy_dir_names, direction);
2264 free(out);
2265 return FAILED;
2266 }
2267
2268 /* we need the monotonic time, but the kernel returns system time. */
2269 if (response.lft_current->sadb_lifetime_usetime)
2270 {
2271 *use_time = time_monotonic(NULL) -
2272 (time(NULL) - response.lft_current->sadb_lifetime_usetime);
2273 }
2274 else
2275 {
2276 *use_time = 0;
2277 }
2278 free(out);
2279 return SUCCESS;
2280 }
2281
2282 METHOD(kernel_ipsec_t, del_policy, status_t,
2283 private_kernel_pfkey_ipsec_t *this, traffic_selector_t *src_ts,
2284 traffic_selector_t *dst_ts, policy_dir_t direction, u_int32_t reqid,
2285 mark_t mark, policy_priority_t prio)
2286 {
2287 unsigned char request[PFKEY_BUFFER_SIZE];
2288 struct sadb_msg *msg, *out;
2289 struct sadb_x_policy *pol;
2290 policy_entry_t *policy, *found = NULL;
2291 policy_sa_t *mapping;
2292 enumerator_t *enumerator;
2293 bool is_installed = TRUE;
2294 u_int32_t priority;
2295 size_t len;
2296
2297 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
2298 { /* FWD policies are not supported on all platforms */
2299 return SUCCESS;
2300 }
2301
2302 DBG2(DBG_KNL, "deleting policy %R === %R %N", src_ts, dst_ts,
2303 policy_dir_names, direction);
2304
2305 /* create a policy */
2306 policy = create_policy_entry(src_ts, dst_ts, direction);
2307
2308 /* find a matching policy */
2309 this->mutex->lock(this->mutex);
2310 if (this->policies->find_first(this->policies,
2311 (linked_list_match_t)policy_entry_equals,
2312 (void**)&found, policy) != SUCCESS)
2313 {
2314 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
2315 dst_ts, policy_dir_names, direction);
2316 policy_entry_destroy(policy, this);
2317 this->mutex->unlock(this->mutex);
2318 return NOT_FOUND;
2319 }
2320 policy_entry_destroy(policy, this);
2321 policy = found;
2322
2323 /* remove mapping to SA by reqid and priority */
2324 priority = get_priority(policy, prio);
2325 enumerator = policy->used_by->create_enumerator(policy->used_by);
2326 while (enumerator->enumerate(enumerator, (void**)&mapping))
2327 {
2328 if (reqid == mapping->sa->cfg.reqid && priority == mapping->priority)
2329 {
2330 policy->used_by->remove_at(policy->used_by, enumerator);
2331 break;
2332 }
2333 is_installed = FALSE;
2334 }
2335 enumerator->destroy(enumerator);
2336
2337 if (policy->used_by->get_count(policy->used_by) > 0)
2338 { /* policy is used by more SAs, keep in kernel */
2339 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
2340 policy_sa_destroy(mapping, &direction, this);
2341
2342 if (!is_installed)
2343 { /* no need to update as the policy was not installed for this SA */
2344 this->mutex->unlock(this->mutex);
2345 return SUCCESS;
2346 }
2347
2348 DBG2(DBG_KNL, "updating policy %R === %R %N", src_ts, dst_ts,
2349 policy_dir_names, direction);
2350 policy->used_by->get_first(policy->used_by, (void**)&mapping);
2351 if (add_policy_internal(this, policy, mapping, TRUE) != SUCCESS)
2352 {
2353 DBG1(DBG_KNL, "unable to update policy %R === %R %N",
2354 src_ts, dst_ts, policy_dir_names, direction);
2355 return FAILED;
2356 }
2357 return SUCCESS;
2358 }
2359
2360 memset(&request, 0, sizeof(request));
2361
2362 msg = (struct sadb_msg*)request;
2363 msg->sadb_msg_version = PF_KEY_V2;
2364 msg->sadb_msg_type = SADB_X_SPDDELETE;
2365 msg->sadb_msg_satype = 0;
2366 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2367
2368 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
2369 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2370 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
2371 pol->sadb_x_policy_dir = dir2kernel(direction);
2372 pol->sadb_x_policy_type = type2kernel(mapping->type);
2373 PFKEY_EXT_ADD(msg, pol);
2374
2375 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
2376 policy->src.mask, TRUE);
2377 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
2378 policy->dst.mask, TRUE);
2379
2380 if (policy->route)
2381 {
2382 route_entry_t *route = policy->route;
2383 if (hydra->kernel_interface->del_route(hydra->kernel_interface,
2384 route->dst_net, route->prefixlen, route->gateway,
2385 route->src_ip, route->if_name) != SUCCESS)
2386 {
2387 DBG1(DBG_KNL, "error uninstalling route installed with "
2388 "policy %R === %R %N", src_ts, dst_ts,
2389 policy_dir_names, direction);
2390 }
2391 }
2392
2393 this->policies->remove(this->policies, found, NULL);
2394 policy_sa_destroy(mapping, &direction, this);
2395 policy_entry_destroy(policy, this);
2396 this->mutex->unlock(this->mutex);
2397
2398 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
2399 {
2400 DBG1(DBG_KNL, "unable to delete policy %R === %R %N", src_ts, dst_ts,
2401 policy_dir_names, direction);
2402 return FAILED;
2403 }
2404 else if (out->sadb_msg_errno)
2405 {
2406 DBG1(DBG_KNL, "unable to delete policy %R === %R %N: %s (%d)", src_ts,
2407 dst_ts, policy_dir_names, direction,
2408 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2409 free(out);
2410 return FAILED;
2411 }
2412 free(out);
2413 return SUCCESS;
2414 }
2415
2416 METHOD(kernel_ipsec_t, flush_policies, status_t,
2417 private_kernel_pfkey_ipsec_t *this)
2418 {
2419 unsigned char request[PFKEY_BUFFER_SIZE];
2420 struct sadb_msg *msg, *out;
2421 size_t len;
2422
2423 memset(&request, 0, sizeof(request));
2424
2425 DBG2(DBG_KNL, "flushing all policies from SPD");
2426
2427 msg = (struct sadb_msg*)request;
2428 msg->sadb_msg_version = PF_KEY_V2;
2429 msg->sadb_msg_type = SADB_X_SPDFLUSH;
2430 msg->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2431 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2432
2433 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
2434 {
2435 DBG1(DBG_KNL, "unable to flush SPD entries");
2436 return FAILED;
2437 }
2438 else if (out->sadb_msg_errno)
2439 {
2440 DBG1(DBG_KNL, "unable to flush SPD entries: %s (%d)",
2441 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2442 free(out);
2443 return FAILED;
2444 }
2445 free(out);
2446 return SUCCESS;
2447 }
2448
2449 /**
2450 * Register a socket for ACQUIRE/EXPIRE messages
2451 */
2452 static status_t register_pfkey_socket(private_kernel_pfkey_ipsec_t *this,
2453 u_int8_t satype)
2454 {
2455 unsigned char request[PFKEY_BUFFER_SIZE];
2456 struct sadb_msg *msg, *out;
2457 size_t len;
2458
2459 memset(&request, 0, sizeof(request));
2460
2461 msg = (struct sadb_msg*)request;
2462 msg->sadb_msg_version = PF_KEY_V2;
2463 msg->sadb_msg_type = SADB_REGISTER;
2464 msg->sadb_msg_satype = satype;
2465 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2466
2467 if (pfkey_send_socket(this, this->socket_events, msg, &out, &len) != SUCCESS)
2468 {
2469 DBG1(DBG_KNL, "unable to register PF_KEY socket");
2470 return FAILED;
2471 }
2472 else if (out->sadb_msg_errno)
2473 {
2474 DBG1(DBG_KNL, "unable to register PF_KEY socket: %s (%d)",
2475 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2476 free(out);
2477 return FAILED;
2478 }
2479 free(out);
2480 return SUCCESS;
2481 }
2482
2483 METHOD(kernel_ipsec_t, bypass_socket, bool,
2484 private_kernel_pfkey_ipsec_t *this, int fd, int family)
2485 {
2486 struct sadb_x_policy policy;
2487 u_int sol, ipsec_policy;
2488
2489 switch (family)
2490 {
2491 case AF_INET:
2492 {
2493 sol = SOL_IP;
2494 ipsec_policy = IP_IPSEC_POLICY;
2495 break;
2496 }
2497 case AF_INET6:
2498 {
2499 sol = SOL_IPV6;
2500 ipsec_policy = IPV6_IPSEC_POLICY;
2501 break;
2502 }
2503 default:
2504 return FALSE;
2505 }
2506
2507 memset(&policy, 0, sizeof(policy));
2508 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);
2509 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2510 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;
2511
2512 policy.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;
2513 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2514 {
2515 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2516 strerror(errno));
2517 return FALSE;
2518 }
2519 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;
2520 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2521 {
2522 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2523 strerror(errno));
2524 return FALSE;
2525 }
2526 return TRUE;
2527 }
2528
2529 METHOD(kernel_ipsec_t, enable_udp_decap, bool,
2530 private_kernel_pfkey_ipsec_t *this, int fd, int family, u_int16_t port)
2531 {
2532 #ifndef __APPLE__
2533 int type = UDP_ENCAP_ESPINUDP;
2534
2535 if (setsockopt(fd, SOL_UDP, UDP_ENCAP, &type, sizeof(type)) < 0)
2536 {
2537 DBG1(DBG_KNL, "unable to set UDP_ENCAP: %s", strerror(errno));
2538 return FALSE;
2539 }
2540 #else /* __APPLE__ */
2541 if (sysctlbyname("net.inet.ipsec.esp_port", NULL, NULL, &port,
2542 sizeof(port)) != 0)
2543 {
2544 DBG1(DBG_KNL, "could not set net.inet.ipsec.esp_port to %d: %s",
2545 port, strerror(errno));
2546 return FALSE;
2547 }
2548 #endif /* __APPLE__ */
2549
2550 return TRUE;
2551 }
2552
2553 METHOD(kernel_ipsec_t, destroy, void,
2554 private_kernel_pfkey_ipsec_t *this)
2555 {
2556 if (this->socket > 0)
2557 {
2558 close(this->socket);
2559 }
2560 if (this->socket_events > 0)
2561 {
2562 close(this->socket_events);
2563 }
2564 this->policies->invoke_function(this->policies,
2565 (linked_list_invoke_t)policy_entry_destroy,
2566 this);
2567 this->policies->destroy(this->policies);
2568 this->sas->destroy(this->sas);
2569 this->mutex->destroy(this->mutex);
2570 this->mutex_pfkey->destroy(this->mutex_pfkey);
2571 free(this);
2572 }
2573
2574 /*
2575 * Described in header.
2576 */
2577 kernel_pfkey_ipsec_t *kernel_pfkey_ipsec_create()
2578 {
2579 private_kernel_pfkey_ipsec_t *this;
2580 bool register_for_events = TRUE;
2581
2582 INIT(this,
2583 .public = {
2584 .interface = {
2585 .get_spi = _get_spi,
2586 .get_cpi = _get_cpi,
2587 .add_sa = _add_sa,
2588 .update_sa = _update_sa,
2589 .query_sa = _query_sa,
2590 .del_sa = _del_sa,
2591 .flush_sas = _flush_sas,
2592 .add_policy = _add_policy,
2593 .query_policy = _query_policy,
2594 .del_policy = _del_policy,
2595 .flush_policies = _flush_policies,
2596 .bypass_socket = _bypass_socket,
2597 .enable_udp_decap = _enable_udp_decap,
2598 .destroy = _destroy,
2599 },
2600 },
2601 .policies = linked_list_create(),
2602 .sas = hashtable_create((hashtable_hash_t)ipsec_sa_hash,
2603 (hashtable_equals_t)ipsec_sa_equals, 32),
2604 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
2605 .mutex_pfkey = mutex_create(MUTEX_TYPE_DEFAULT),
2606 .install_routes = lib->settings->get_bool(lib->settings,
2607 "%s.install_routes", TRUE,
2608 hydra->daemon),
2609 );
2610
2611 if (streq(hydra->daemon, "pluto"))
2612 { /* no routes for pluto, they are installed via updown script */
2613 this->install_routes = FALSE;
2614 }
2615 else if (streq(hydra->daemon, "starter"))
2616 { /* starter has no threads, so we do not register for kernel events */
2617 register_for_events = FALSE;
2618 }
2619
2620 /* create a PF_KEY socket to communicate with the kernel */
2621 this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
2622 if (this->socket <= 0)
2623 {
2624 DBG1(DBG_KNL, "unable to create PF_KEY socket");
2625 destroy(this);
2626 return NULL;
2627 }
2628
2629 if (register_for_events)
2630 {
2631 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
2632 this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
2633 if (this->socket_events <= 0)
2634 {
2635 DBG1(DBG_KNL, "unable to create PF_KEY event socket");
2636 destroy(this);
2637 return NULL;
2638 }
2639
2640 /* register the event socket */
2641 if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS ||
2642 register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS)
2643 {
2644 DBG1(DBG_KNL, "unable to register PF_KEY event socket");
2645 destroy(this);
2646 return NULL;
2647 }
2648
2649 lib->processor->queue_job(lib->processor,
2650 (job_t*)callback_job_create_with_prio(
2651 (callback_job_cb_t)receive_events, this, NULL,
2652 (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
2653 }
2654
2655 return &this->public;
2656 }
2657