Moved migrate job creation to kernel event handler.
[strongswan.git] / src / libcharon / plugins / kernel_pfkey / kernel_pfkey_ipsec.c
1 /*
2 * Copyright (C) 2008-2010 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
55 #include "kernel_pfkey_ipsec.h"
56
57 #include <hydra.h>
58 #include <daemon.h>
59 #include <utils/host.h>
60 #include <threading/thread.h>
61 #include <threading/mutex.h>
62 #include <processing/jobs/callback_job.h>
63
64 /** non linux specific */
65 #ifndef IPPROTO_COMP
66 #ifdef IPPROTO_IPCOMP
67 #define IPPROTO_COMP IPPROTO_IPCOMP
68 #endif
69 #endif
70
71 #ifndef SADB_X_AALG_SHA2_256HMAC
72 #define SADB_X_AALG_SHA2_256HMAC SADB_X_AALG_SHA2_256
73 #define SADB_X_AALG_SHA2_384HMAC SADB_X_AALG_SHA2_384
74 #define SADB_X_AALG_SHA2_512HMAC SADB_X_AALG_SHA2_512
75 #endif
76
77 #ifndef SADB_X_EALG_AESCBC
78 #define SADB_X_EALG_AESCBC SADB_X_EALG_AES
79 #endif
80
81 #ifndef SADB_X_EALG_CASTCBC
82 #define SADB_X_EALG_CASTCBC SADB_X_EALG_CAST128CBC
83 #endif
84
85 #ifndef SOL_IP
86 #define SOL_IP IPPROTO_IP
87 #define SOL_IPV6 IPPROTO_IPV6
88 #endif
89
90 /** from linux/in.h */
91 #ifndef IP_IPSEC_POLICY
92 #define IP_IPSEC_POLICY 16
93 #endif
94
95 /** missing on uclibc */
96 #ifndef IPV6_IPSEC_POLICY
97 #define IPV6_IPSEC_POLICY 34
98 #endif
99
100 /** default priority of installed policies */
101 #define PRIO_LOW 3000
102 #define PRIO_HIGH 2000
103
104 #ifdef __APPLE__
105 /** from xnu/bsd/net/pfkeyv2.h */
106 #define SADB_X_EXT_NATT 0x002
107 struct sadb_sa_2 {
108 struct sadb_sa sa;
109 u_int16_t sadb_sa_natt_port;
110 u_int16_t sadb_reserved0;
111 u_int32_t sadb_reserved1;
112 };
113 #endif
114
115 /** buffer size for PF_KEY messages */
116 #define PFKEY_BUFFER_SIZE 4096
117
118 /** PF_KEY messages are 64 bit aligned */
119 #define PFKEY_ALIGNMENT 8
120 /** aligns len to 64 bits */
121 #define PFKEY_ALIGN(len) (((len) + PFKEY_ALIGNMENT - 1) & ~(PFKEY_ALIGNMENT - 1))
122 /** calculates the properly padded length in 64 bit chunks */
123 #define PFKEY_LEN(len) ((PFKEY_ALIGN(len) / PFKEY_ALIGNMENT))
124 /** calculates user mode length i.e. in bytes */
125 #define PFKEY_USER_LEN(len) ((len) * PFKEY_ALIGNMENT)
126
127 /** given a PF_KEY message header and an extension this updates the length in the header */
128 #define PFKEY_EXT_ADD(msg, ext) ((msg)->sadb_msg_len += ((struct sadb_ext*)ext)->sadb_ext_len)
129 /** given a PF_KEY message header this returns a pointer to the next extension */
130 #define PFKEY_EXT_ADD_NEXT(msg) ((struct sadb_ext*)(((char*)(msg)) + PFKEY_USER_LEN((msg)->sadb_msg_len)))
131 /** copy an extension and append it to a PF_KEY message */
132 #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))))
133 /** given a PF_KEY extension this returns a pointer to the next extension */
134 #define PFKEY_EXT_NEXT(ext) ((struct sadb_ext*)(((char*)(ext)) + PFKEY_USER_LEN(((struct sadb_ext*)ext)->sadb_ext_len)))
135 /** given a PF_KEY extension this returns a pointer to the next extension also updates len (len in 64 bit words) */
136 #define PFKEY_EXT_NEXT_LEN(ext,len) ((len) -= (ext)->sadb_ext_len, PFKEY_EXT_NEXT(ext))
137 /** true if ext has a valid length and len is large enough to contain ext (assuming len in 64 bit words) */
138 #define PFKEY_EXT_OK(ext,len) ((len) >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
139 (ext)->sadb_ext_len >= PFKEY_LEN(sizeof(struct sadb_ext)) && \
140 (ext)->sadb_ext_len <= (len))
141
142 typedef struct private_kernel_pfkey_ipsec_t private_kernel_pfkey_ipsec_t;
143
144 /**
145 * Private variables and functions of kernel_pfkey class.
146 */
147 struct private_kernel_pfkey_ipsec_t
148 {
149 /**
150 * Public part of the kernel_pfkey_t object.
151 */
152 kernel_pfkey_ipsec_t public;
153
154 /**
155 * mutex to lock access to various lists
156 */
157 mutex_t *mutex;
158
159 /**
160 * List of installed policies (policy_entry_t)
161 */
162 linked_list_t *policies;
163
164 /**
165 * whether to install routes along policies
166 */
167 bool install_routes;
168
169 /**
170 * job receiving PF_KEY events
171 */
172 callback_job_t *job;
173
174 /**
175 * mutex to lock access to the PF_KEY socket
176 */
177 mutex_t *mutex_pfkey;
178
179 /**
180 * PF_KEY socket to communicate with the kernel
181 */
182 int socket;
183
184 /**
185 * PF_KEY socket to receive acquire and expire events
186 */
187 int socket_events;
188
189 /**
190 * sequence number for messages sent to the kernel
191 */
192 int seq;
193 };
194
195 typedef struct route_entry_t route_entry_t;
196
197 /**
198 * installed routing entry
199 */
200 struct route_entry_t {
201 /** Name of the interface the route is bound to */
202 char *if_name;
203
204 /** Source ip of the route */
205 host_t *src_ip;
206
207 /** gateway for this route */
208 host_t *gateway;
209
210 /** Destination net */
211 chunk_t dst_net;
212
213 /** Destination net prefixlen */
214 u_int8_t prefixlen;
215 };
216
217 /**
218 * destroy an route_entry_t object
219 */
220 static void route_entry_destroy(route_entry_t *this)
221 {
222 free(this->if_name);
223 DESTROY_IF(this->src_ip);
224 DESTROY_IF(this->gateway);
225 chunk_free(&this->dst_net);
226 free(this);
227 }
228
229 typedef struct policy_entry_t policy_entry_t;
230
231 /**
232 * installed kernel policy.
233 */
234 struct policy_entry_t {
235
236 /** reqid of this policy */
237 u_int32_t reqid;
238
239 /** index assigned by the kernel */
240 u_int32_t index;
241
242 /** direction of this policy: in, out, forward */
243 u_int8_t direction;
244
245 /** parameters of installed policy */
246 struct {
247 /** subnet and port */
248 host_t *net;
249 /** subnet mask */
250 u_int8_t mask;
251 /** protocol */
252 u_int8_t proto;
253 } src, dst;
254
255 /** associated route installed for this policy */
256 route_entry_t *route;
257
258 /** by how many CHILD_SA's this policy is used */
259 u_int refcount;
260 };
261
262 /**
263 * create a policy_entry_t object
264 */
265 static policy_entry_t *create_policy_entry(traffic_selector_t *src_ts,
266 traffic_selector_t *dst_ts, policy_dir_t dir, u_int32_t reqid)
267 {
268 policy_entry_t *policy = malloc_thing(policy_entry_t);
269 policy->reqid = reqid;
270 policy->index = 0;
271 policy->direction = dir;
272 policy->route = NULL;
273 policy->refcount = 0;
274
275 src_ts->to_subnet(src_ts, &policy->src.net, &policy->src.mask);
276 dst_ts->to_subnet(dst_ts, &policy->dst.net, &policy->dst.mask);
277
278 /* src or dest proto may be "any" (0), use more restrictive one */
279 policy->src.proto = max(src_ts->get_protocol(src_ts), dst_ts->get_protocol(dst_ts));
280 policy->src.proto = policy->src.proto ? policy->src.proto : IPSEC_PROTO_ANY;
281 policy->dst.proto = policy->src.proto;
282
283 return policy;
284 }
285
286 /**
287 * destroy a policy_entry_t object
288 */
289 static void policy_entry_destroy(policy_entry_t *this)
290 {
291 DESTROY_IF(this->src.net);
292 DESTROY_IF(this->dst.net);
293 if (this->route)
294 {
295 route_entry_destroy(this->route);
296 }
297 free(this);
298 }
299
300 /**
301 * compares two policy_entry_t
302 */
303 static inline bool policy_entry_equals(policy_entry_t *current, policy_entry_t *policy)
304 {
305 return current->direction == policy->direction &&
306 current->src.proto == policy->src.proto &&
307 current->dst.proto == policy->dst.proto &&
308 current->src.mask == policy->src.mask &&
309 current->dst.mask == policy->dst.mask &&
310 current->src.net->equals(current->src.net, policy->src.net) &&
311 current->dst.net->equals(current->dst.net, policy->dst.net);
312 }
313
314 /**
315 * compare the given kernel index with that of a policy
316 */
317 static inline bool policy_entry_match_byindex(policy_entry_t *current, u_int32_t *index)
318 {
319 return current->index == *index;
320 }
321
322 typedef struct pfkey_msg_t pfkey_msg_t;
323
324 struct pfkey_msg_t
325 {
326 /**
327 * PF_KEY message base
328 */
329 struct sadb_msg *msg;
330
331 /**
332 * PF_KEY message extensions
333 */
334 union {
335 struct sadb_ext *ext[SADB_EXT_MAX + 1];
336 struct {
337 struct sadb_ext *reserved; /* SADB_EXT_RESERVED */
338 struct sadb_sa *sa; /* SADB_EXT_SA */
339 struct sadb_lifetime *lft_current; /* SADB_EXT_LIFETIME_CURRENT */
340 struct sadb_lifetime *lft_hard; /* SADB_EXT_LIFETIME_HARD */
341 struct sadb_lifetime *lft_soft; /* SADB_EXT_LIFETIME_SOFT */
342 struct sadb_address *src; /* SADB_EXT_ADDRESS_SRC */
343 struct sadb_address *dst; /* SADB_EXT_ADDRESS_DST */
344 struct sadb_address *proxy; /* SADB_EXT_ADDRESS_PROXY */
345 struct sadb_key *key_auth; /* SADB_EXT_KEY_AUTH */
346 struct sadb_key *key_encr; /* SADB_EXT_KEY_ENCRYPT */
347 struct sadb_ident *id_src; /* SADB_EXT_IDENTITY_SRC */
348 struct sadb_ident *id_dst; /* SADB_EXT_IDENTITY_DST */
349 struct sadb_sens *sensitivity; /* SADB_EXT_SENSITIVITY */
350 struct sadb_prop *proposal; /* SADB_EXT_PROPOSAL */
351 struct sadb_supported *supported_auth; /* SADB_EXT_SUPPORTED_AUTH */
352 struct sadb_supported *supported_encr; /* SADB_EXT_SUPPORTED_ENCRYPT */
353 struct sadb_spirange *spirange; /* SADB_EXT_SPIRANGE */
354 struct sadb_x_kmprivate *x_kmprivate; /* SADB_X_EXT_KMPRIVATE */
355 struct sadb_x_policy *x_policy; /* SADB_X_EXT_POLICY */
356 struct sadb_x_sa2 *x_sa2; /* SADB_X_EXT_SA2 */
357 struct sadb_x_nat_t_type *x_natt_type; /* SADB_X_EXT_NAT_T_TYPE */
358 struct sadb_x_nat_t_port *x_natt_sport; /* SADB_X_EXT_NAT_T_SPORT */
359 struct sadb_x_nat_t_port *x_natt_dport; /* SADB_X_EXT_NAT_T_DPORT */
360 struct sadb_address *x_natt_oa; /* SADB_X_EXT_NAT_T_OA */
361 struct sadb_x_sec_ctx *x_sec_ctx; /* SADB_X_EXT_SEC_CTX */
362 struct sadb_x_kmaddress *x_kmaddress; /* SADB_X_EXT_KMADDRESS */
363 } __attribute__((__packed__));
364 };
365 };
366
367 ENUM(sadb_ext_type_names, SADB_EXT_RESERVED, SADB_EXT_MAX,
368 "SADB_EXT_RESERVED",
369 "SADB_EXT_SA",
370 "SADB_EXT_LIFETIME_CURRENT",
371 "SADB_EXT_LIFETIME_HARD",
372 "SADB_EXT_LIFETIME_SOFT",
373 "SADB_EXT_ADDRESS_SRC",
374 "SADB_EXT_ADDRESS_DST",
375 "SADB_EXT_ADDRESS_PROXY",
376 "SADB_EXT_KEY_AUTH",
377 "SADB_EXT_KEY_ENCRYPT",
378 "SADB_EXT_IDENTITY_SRC",
379 "SADB_EXT_IDENTITY_DST",
380 "SADB_EXT_SENSITIVITY",
381 "SADB_EXT_PROPOSAL",
382 "SADB_EXT_SUPPORTED_AUTH",
383 "SADB_EXT_SUPPORTED_ENCRYPT",
384 "SADB_EXT_SPIRANGE",
385 "SADB_X_EXT_KMPRIVATE",
386 "SADB_X_EXT_POLICY",
387 "SADB_X_EXT_SA2",
388 "SADB_X_EXT_NAT_T_TYPE",
389 "SADB_X_EXT_NAT_T_SPORT",
390 "SADB_X_EXT_NAT_T_DPORT",
391 "SADB_X_EXT_NAT_T_OA",
392 "SADB_X_EXT_SEC_CTX",
393 "SADB_X_EXT_KMADDRESS"
394 );
395
396 /**
397 * convert a IKEv2 specific protocol identifier to the PF_KEY sa type
398 */
399 static u_int8_t proto_ike2satype(protocol_id_t proto)
400 {
401 switch (proto)
402 {
403 case PROTO_ESP:
404 return SADB_SATYPE_ESP;
405 case PROTO_AH:
406 return SADB_SATYPE_AH;
407 case IPPROTO_COMP:
408 return SADB_X_SATYPE_IPCOMP;
409 default:
410 return proto;
411 }
412 }
413
414 /**
415 * convert a PF_KEY sa type to a IKEv2 specific protocol identifier
416 */
417 static protocol_id_t proto_satype2ike(u_int8_t proto)
418 {
419 switch (proto)
420 {
421 case SADB_SATYPE_ESP:
422 return PROTO_ESP;
423 case SADB_SATYPE_AH:
424 return PROTO_AH;
425 case SADB_X_SATYPE_IPCOMP:
426 return IPPROTO_COMP;
427 default:
428 return proto;
429 }
430 }
431
432 /**
433 * convert a IKEv2 specific protocol identifier to the IP protocol identifier
434 */
435 static u_int8_t proto_ike2ip(protocol_id_t proto)
436 {
437 switch (proto)
438 {
439 case PROTO_ESP:
440 return IPPROTO_ESP;
441 case PROTO_AH:
442 return IPPROTO_AH;
443 default:
444 return proto;
445 }
446 }
447
448 /**
449 * convert the general ipsec mode to the one defined in ipsec.h
450 */
451 static u_int8_t mode2kernel(ipsec_mode_t mode)
452 {
453 switch (mode)
454 {
455 case MODE_TRANSPORT:
456 return IPSEC_MODE_TRANSPORT;
457 case MODE_TUNNEL:
458 return IPSEC_MODE_TUNNEL;
459 #ifdef HAVE_IPSEC_MODE_BEET
460 case MODE_BEET:
461 return IPSEC_MODE_BEET;
462 #endif
463 default:
464 return mode;
465 }
466 }
467
468 /**
469 * convert the general policy direction to the one defined in ipsec.h
470 */
471 static u_int8_t dir2kernel(policy_dir_t dir)
472 {
473 switch (dir)
474 {
475 case POLICY_IN:
476 return IPSEC_DIR_INBOUND;
477 case POLICY_OUT:
478 return IPSEC_DIR_OUTBOUND;
479 #ifdef HAVE_IPSEC_DIR_FWD
480 case POLICY_FWD:
481 return IPSEC_DIR_FWD;
482 #endif
483 default:
484 return IPSEC_DIR_INVALID;
485 }
486 }
487
488 #ifdef SADB_X_MIGRATE
489 /**
490 * convert the policy direction in ipsec.h to the general one.
491 */
492 static policy_dir_t kernel2dir(u_int8_t dir)
493 {
494 switch (dir)
495 {
496 case IPSEC_DIR_INBOUND:
497 return POLICY_IN;
498 case IPSEC_DIR_OUTBOUND:
499 return POLICY_OUT;
500 #ifdef HAVE_IPSEC_DIR_FWD
501 case IPSEC_DIR_FWD:
502 return POLICY_FWD;
503 #endif
504 default:
505 return dir;
506 }
507 }
508 #endif /*SADB_X_MIGRATE*/
509
510 typedef struct kernel_algorithm_t kernel_algorithm_t;
511
512 /**
513 * Mapping of IKEv2 algorithms to PF_KEY algorithms
514 */
515 struct kernel_algorithm_t {
516 /**
517 * Identifier specified in IKEv2
518 */
519 int ikev2;
520
521 /**
522 * Identifier as defined in pfkeyv2.h
523 */
524 int kernel;
525 };
526
527 #define END_OF_LIST -1
528
529 /**
530 * Algorithms for encryption
531 */
532 static kernel_algorithm_t encryption_algs[] = {
533 /* {ENCR_DES_IV64, 0 }, */
534 {ENCR_DES, SADB_EALG_DESCBC },
535 {ENCR_3DES, SADB_EALG_3DESCBC },
536 /* {ENCR_RC5, 0 }, */
537 /* {ENCR_IDEA, 0 }, */
538 {ENCR_CAST, SADB_X_EALG_CASTCBC },
539 {ENCR_BLOWFISH, SADB_X_EALG_BLOWFISHCBC },
540 /* {ENCR_3IDEA, 0 }, */
541 /* {ENCR_DES_IV32, 0 }, */
542 {ENCR_NULL, SADB_EALG_NULL },
543 {ENCR_AES_CBC, SADB_X_EALG_AESCBC },
544 /* {ENCR_AES_CTR, SADB_X_EALG_AESCTR }, */
545 /* {ENCR_AES_CCM_ICV8, SADB_X_EALG_AES_CCM_ICV8 }, */
546 /* {ENCR_AES_CCM_ICV12, SADB_X_EALG_AES_CCM_ICV12 }, */
547 /* {ENCR_AES_CCM_ICV16, SADB_X_EALG_AES_CCM_ICV16 }, */
548 /* {ENCR_AES_GCM_ICV8, SADB_X_EALG_AES_GCM_ICV8 }, */
549 /* {ENCR_AES_GCM_ICV12, SADB_X_EALG_AES_GCM_ICV12 }, */
550 /* {ENCR_AES_GCM_ICV16, SADB_X_EALG_AES_GCM_ICV16 }, */
551 {END_OF_LIST, 0 },
552 };
553
554 /**
555 * Algorithms for integrity protection
556 */
557 static kernel_algorithm_t integrity_algs[] = {
558 {AUTH_HMAC_MD5_96, SADB_AALG_MD5HMAC },
559 {AUTH_HMAC_SHA1_96, SADB_AALG_SHA1HMAC },
560 {AUTH_HMAC_SHA2_256_128, SADB_X_AALG_SHA2_256HMAC },
561 {AUTH_HMAC_SHA2_384_192, SADB_X_AALG_SHA2_384HMAC },
562 {AUTH_HMAC_SHA2_512_256, SADB_X_AALG_SHA2_512HMAC },
563 /* {AUTH_DES_MAC, 0, }, */
564 /* {AUTH_KPDK_MD5, 0, }, */
565 #ifdef SADB_X_AALG_AES_XCBC_MAC
566 {AUTH_AES_XCBC_96, SADB_X_AALG_AES_XCBC_MAC, },
567 #endif
568 {END_OF_LIST, 0, },
569 };
570
571 #if 0
572 /**
573 * Algorithms for IPComp, unused yet
574 */
575 static kernel_algorithm_t compression_algs[] = {
576 /* {IPCOMP_OUI, 0 }, */
577 {IPCOMP_DEFLATE, SADB_X_CALG_DEFLATE },
578 {IPCOMP_LZS, SADB_X_CALG_LZS },
579 {IPCOMP_LZJH, SADB_X_CALG_LZJH },
580 {END_OF_LIST, 0 },
581 };
582 #endif
583
584 /**
585 * Look up a kernel algorithm ID and its key size
586 */
587 static int lookup_algorithm(kernel_algorithm_t *list, int ikev2)
588 {
589 while (list->ikev2 != END_OF_LIST)
590 {
591 if (ikev2 == list->ikev2)
592 {
593 return list->kernel;
594 }
595 list++;
596 }
597 return 0;
598 }
599
600 /**
601 * Copy a host_t as sockaddr_t to the given memory location. Ports are
602 * reset to zero as per RFC 2367.
603 * @return the number of bytes copied
604 */
605 static size_t hostcpy(void *dest, host_t *host)
606 {
607 sockaddr_t *addr = host->get_sockaddr(host), *dest_addr = dest;
608 socklen_t *len = host->get_sockaddr_len(host);
609 memcpy(dest, addr, *len);
610 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
611 dest_addr->sa_len = *len;
612 #endif
613 switch (dest_addr->sa_family)
614 {
615 case AF_INET:
616 {
617 struct sockaddr_in *sin = dest;
618 sin->sin_port = 0;
619 break;
620 }
621 case AF_INET6:
622 {
623 struct sockaddr_in6 *sin6 = dest;
624 sin6->sin6_port = 0;
625 break;
626 }
627 }
628 return *len;
629 }
630
631 /**
632 * add a host behind an sadb_address extension
633 */
634 static void host2ext(host_t *host, struct sadb_address *ext)
635 {
636 size_t len = hostcpy(ext + 1, host);
637 ext->sadb_address_len = PFKEY_LEN(sizeof(*ext) + len);
638 }
639
640 /**
641 * add a host to the given sadb_msg
642 */
643 static void add_addr_ext(struct sadb_msg *msg, host_t *host, u_int16_t type,
644 u_int8_t proto, u_int8_t prefixlen)
645 {
646 struct sadb_address *addr = (struct sadb_address*)PFKEY_EXT_ADD_NEXT(msg);
647 addr->sadb_address_exttype = type;
648 addr->sadb_address_proto = proto;
649 addr->sadb_address_prefixlen = prefixlen;
650 host2ext(host, addr);
651 PFKEY_EXT_ADD(msg, addr);
652 }
653
654 /**
655 * adds an empty address extension to the given sadb_msg
656 */
657 static void add_anyaddr_ext(struct sadb_msg *msg, int family, u_int8_t type)
658 {
659 socklen_t len = (family == AF_INET) ? sizeof(struct sockaddr_in) :
660 sizeof(struct sockaddr_in6);
661 struct sadb_address *addr = (struct sadb_address*)PFKEY_EXT_ADD_NEXT(msg);
662 addr->sadb_address_exttype = type;
663 sockaddr_t *saddr = (sockaddr_t*)(addr + 1);
664 saddr->sa_family = family;
665 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
666 saddr->sa_len = len;
667 #endif
668 addr->sadb_address_len = PFKEY_LEN(sizeof(*addr) + len);
669 PFKEY_EXT_ADD(msg, addr);
670 }
671
672 #ifdef HAVE_NATT
673 /**
674 * add udp encap extensions to a sadb_msg
675 */
676 static void add_encap_ext(struct sadb_msg *msg, host_t *src, host_t *dst)
677 {
678 struct sadb_x_nat_t_type* nat_type;
679 struct sadb_x_nat_t_port* nat_port;
680
681 nat_type = (struct sadb_x_nat_t_type*)PFKEY_EXT_ADD_NEXT(msg);
682 nat_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
683 nat_type->sadb_x_nat_t_type_len = PFKEY_LEN(sizeof(struct sadb_x_nat_t_type));
684 nat_type->sadb_x_nat_t_type_type = UDP_ENCAP_ESPINUDP;
685 PFKEY_EXT_ADD(msg, nat_type);
686
687 nat_port = (struct sadb_x_nat_t_port*)PFKEY_EXT_ADD_NEXT(msg);
688 nat_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
689 nat_port->sadb_x_nat_t_port_len = PFKEY_LEN(sizeof(struct sadb_x_nat_t_port));
690 nat_port->sadb_x_nat_t_port_port = htons(src->get_port(src));
691 PFKEY_EXT_ADD(msg, nat_port);
692
693 nat_port = (struct sadb_x_nat_t_port*)PFKEY_EXT_ADD_NEXT(msg);
694 nat_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
695 nat_port->sadb_x_nat_t_port_len = PFKEY_LEN(sizeof(struct sadb_x_nat_t_port));
696 nat_port->sadb_x_nat_t_port_port = htons(dst->get_port(dst));
697 PFKEY_EXT_ADD(msg, nat_port);
698 }
699 #endif /*HAVE_NATT*/
700
701 /**
702 * Convert a sadb_address to a traffic_selector
703 */
704 static traffic_selector_t* sadb_address2ts(struct sadb_address *address)
705 {
706 traffic_selector_t *ts;
707 host_t *host;
708
709 /* The Linux 2.6 kernel does not set the protocol and port information
710 * in the src and dst sadb_address extensions of the SADB_ACQUIRE message.
711 */
712 host = host_create_from_sockaddr((sockaddr_t*)&address[1]) ;
713 ts = traffic_selector_create_from_subnet(host, address->sadb_address_prefixlen,
714 address->sadb_address_proto, host->get_port(host));
715 return ts;
716 }
717
718 /**
719 * Parses a pfkey message received from the kernel
720 */
721 static status_t parse_pfkey_message(struct sadb_msg *msg, pfkey_msg_t *out)
722 {
723 struct sadb_ext* ext;
724 size_t len;
725
726 memset(out, 0, sizeof(pfkey_msg_t));
727 out->msg = msg;
728
729 len = msg->sadb_msg_len;
730 len -= PFKEY_LEN(sizeof(struct sadb_msg));
731
732 ext = (struct sadb_ext*)(((char*)msg) + sizeof(struct sadb_msg));
733
734 while (len >= PFKEY_LEN(sizeof(struct sadb_ext)))
735 {
736 DBG3(DBG_KNL, " %N", sadb_ext_type_names, ext->sadb_ext_type);
737 if (ext->sadb_ext_len < PFKEY_LEN(sizeof(struct sadb_ext)) ||
738 ext->sadb_ext_len > len)
739 {
740 DBG1(DBG_KNL, "length of %N extension is invalid",
741 sadb_ext_type_names, ext->sadb_ext_type);
742 break;
743 }
744
745 if ((ext->sadb_ext_type > SADB_EXT_MAX) || (!ext->sadb_ext_type))
746 {
747 DBG1(DBG_KNL, "type of PF_KEY extension (%d) is invalid", ext->sadb_ext_type);
748 break;
749 }
750
751 if (out->ext[ext->sadb_ext_type])
752 {
753 DBG1(DBG_KNL, "duplicate %N extension",
754 sadb_ext_type_names, ext->sadb_ext_type);
755 break;
756 }
757
758 out->ext[ext->sadb_ext_type] = ext;
759 ext = PFKEY_EXT_NEXT_LEN(ext, len);
760 }
761
762 if (len)
763 {
764 DBG1(DBG_KNL, "PF_KEY message length is invalid");
765 return FAILED;
766 }
767
768 return SUCCESS;
769 }
770
771 /**
772 * Send a message to a specific PF_KEY socket and handle the response.
773 */
774 static status_t pfkey_send_socket(private_kernel_pfkey_ipsec_t *this, int socket,
775 struct sadb_msg *in, struct sadb_msg **out, size_t *out_len)
776 {
777 unsigned char buf[PFKEY_BUFFER_SIZE];
778 struct sadb_msg *msg;
779 int in_len, len;
780
781 this->mutex_pfkey->lock(this->mutex_pfkey);
782
783 /* FIXME: our usage of sequence numbers is probably wrong. check RFC 2367,
784 * in particular the behavior in response to an SADB_ACQUIRE. */
785 in->sadb_msg_seq = ++this->seq;
786 in->sadb_msg_pid = getpid();
787
788 in_len = PFKEY_USER_LEN(in->sadb_msg_len);
789
790 while (TRUE)
791 {
792 len = send(socket, in, in_len, 0);
793
794 if (len != in_len)
795 {
796 if (errno == EINTR)
797 {
798 /* interrupted, try again */
799 continue;
800 }
801 this->mutex_pfkey->unlock(this->mutex_pfkey);
802 DBG1(DBG_KNL, "error sending to PF_KEY socket: %s", strerror(errno));
803 return FAILED;
804 }
805 break;
806 }
807
808 while (TRUE)
809 {
810 msg = (struct sadb_msg*)buf;
811
812 len = recv(socket, buf, sizeof(buf), 0);
813
814 if (len < 0)
815 {
816 if (errno == EINTR)
817 {
818 DBG1(DBG_KNL, "got interrupted");
819 /* interrupted, try again */
820 continue;
821 }
822 DBG1(DBG_KNL, "error reading from PF_KEY socket: %s", strerror(errno));
823 this->mutex_pfkey->unlock(this->mutex_pfkey);
824 return FAILED;
825 }
826 if (len < sizeof(struct sadb_msg) ||
827 msg->sadb_msg_len < PFKEY_LEN(sizeof(struct sadb_msg)))
828 {
829 DBG1(DBG_KNL, "received corrupted PF_KEY message");
830 this->mutex_pfkey->unlock(this->mutex_pfkey);
831 return FAILED;
832 }
833 if (msg->sadb_msg_len > len / PFKEY_ALIGNMENT)
834 {
835 DBG1(DBG_KNL, "buffer was too small to receive the complete PF_KEY message");
836 this->mutex_pfkey->unlock(this->mutex_pfkey);
837 return FAILED;
838 }
839 if (msg->sadb_msg_pid != in->sadb_msg_pid)
840 {
841 DBG2(DBG_KNL, "received PF_KEY message is not intended for us");
842 continue;
843 }
844 if (msg->sadb_msg_seq != this->seq)
845 {
846 DBG1(DBG_KNL, "received PF_KEY message with unexpected sequence "
847 "number, was %d expected %d", msg->sadb_msg_seq, this->seq);
848 if (msg->sadb_msg_seq == 0)
849 {
850 /* FreeBSD and Mac OS X do this for the response to
851 * SADB_X_SPDGET (but not for the response to SADB_GET).
852 * FreeBSD: 'key_spdget' in /usr/src/sys/netipsec/key.c. */
853 }
854 else if (msg->sadb_msg_seq < this->seq)
855 {
856 continue;
857 }
858 else
859 {
860 this->mutex_pfkey->unlock(this->mutex_pfkey);
861 return FAILED;
862 }
863 }
864 if (msg->sadb_msg_type != in->sadb_msg_type)
865 {
866 DBG2(DBG_KNL, "received PF_KEY message of wrong type, "
867 "was %d expected %d, ignoring",
868 msg->sadb_msg_type, in->sadb_msg_type);
869 }
870 break;
871 }
872
873 *out_len = len;
874 *out = (struct sadb_msg*)malloc(len);
875 memcpy(*out, buf, len);
876
877 this->mutex_pfkey->unlock(this->mutex_pfkey);
878
879 return SUCCESS;
880 }
881
882 /**
883 * Send a message to the default PF_KEY socket and handle the response.
884 */
885 static status_t pfkey_send(private_kernel_pfkey_ipsec_t *this,
886 struct sadb_msg *in, struct sadb_msg **out, size_t *out_len)
887 {
888 return pfkey_send_socket(this, this->socket, in, out, out_len);
889 }
890
891 /**
892 * Process a SADB_ACQUIRE message from the kernel
893 */
894 static void process_acquire(private_kernel_pfkey_ipsec_t *this, struct sadb_msg* msg)
895 {
896 pfkey_msg_t response;
897 u_int32_t index, reqid = 0;
898 traffic_selector_t *src_ts, *dst_ts;
899 policy_entry_t *policy;
900
901 switch (msg->sadb_msg_satype)
902 {
903 case SADB_SATYPE_UNSPEC:
904 case SADB_SATYPE_ESP:
905 case SADB_SATYPE_AH:
906 break;
907 default:
908 /* acquire for AH/ESP only */
909 return;
910 }
911 DBG2(DBG_KNL, "received an SADB_ACQUIRE");
912
913 if (parse_pfkey_message(msg, &response) != SUCCESS)
914 {
915 DBG1(DBG_KNL, "parsing SADB_ACQUIRE from kernel failed");
916 return;
917 }
918
919 index = response.x_policy->sadb_x_policy_id;
920 this->mutex->lock(this->mutex);
921 if (this->policies->find_first(this->policies,
922 (linked_list_match_t)policy_entry_match_byindex, (void**)&policy, &index) == SUCCESS)
923 {
924 reqid = policy->reqid;
925 }
926 else
927 {
928 DBG1(DBG_KNL, "received an SADB_ACQUIRE with policy id %d but no"
929 " matching policy found", index);
930 }
931 src_ts = sadb_address2ts(response.src);
932 dst_ts = sadb_address2ts(response.dst);
933 this->mutex->unlock(this->mutex);
934
935 charon->kernel_interface->acquire(charon->kernel_interface, reqid, src_ts,
936 dst_ts);
937 }
938
939 /**
940 * Process a SADB_EXPIRE message from the kernel
941 */
942 static void process_expire(private_kernel_pfkey_ipsec_t *this, struct sadb_msg* msg)
943 {
944 pfkey_msg_t response;
945 protocol_id_t protocol;
946 u_int32_t spi, reqid;
947 bool hard;
948
949 DBG2(DBG_KNL, "received an SADB_EXPIRE");
950
951 if (parse_pfkey_message(msg, &response) != SUCCESS)
952 {
953 DBG1(DBG_KNL, "parsing SADB_EXPIRE from kernel failed");
954 return;
955 }
956
957 protocol = proto_satype2ike(msg->sadb_msg_satype);
958 spi = response.sa->sadb_sa_spi;
959 reqid = response.x_sa2->sadb_x_sa2_reqid;
960 hard = response.lft_hard != NULL;
961
962 if (protocol != PROTO_ESP && protocol != PROTO_AH)
963 {
964 DBG2(DBG_KNL, "ignoring SADB_EXPIRE for SA with SPI %.8x and reqid {%u} "
965 "which is not a CHILD_SA", ntohl(spi), reqid);
966 return;
967 }
968
969 charon->kernel_interface->expire(charon->kernel_interface, reqid, protocol,
970 spi, hard);
971 }
972
973 #ifdef SADB_X_MIGRATE
974 /**
975 * Process a SADB_X_MIGRATE message from the kernel
976 */
977 static void process_migrate(private_kernel_pfkey_ipsec_t *this, struct sadb_msg* msg)
978 {
979 pfkey_msg_t response;
980 traffic_selector_t *src_ts, *dst_ts;
981 policy_dir_t dir;
982 u_int32_t reqid = 0;
983 host_t *local = NULL, *remote = NULL;
984
985 DBG2(DBG_KNL, "received an SADB_X_MIGRATE");
986
987 if (parse_pfkey_message(msg, &response) != SUCCESS)
988 {
989 DBG1(DBG_KNL, "parsing SADB_X_MIGRATE from kernel failed");
990 return;
991 }
992 src_ts = sadb_address2ts(response.src);
993 dst_ts = sadb_address2ts(response.dst);
994 dir = kernel2dir(response.x_policy->sadb_x_policy_dir);
995 DBG2(DBG_KNL, " policy %R === %R %N, id %u", src_ts, dst_ts,
996 policy_dir_names, dir);
997
998 /* SADB_X_EXT_KMADDRESS is not present in unpatched kernels < 2.6.28 */
999 if (response.x_kmaddress)
1000 {
1001 sockaddr_t *local_addr, *remote_addr;
1002 u_int32_t local_len;
1003
1004 local_addr = (sockaddr_t*)&response.x_kmaddress[1];
1005 local = host_create_from_sockaddr(local_addr);
1006 local_len = (local_addr->sa_family == AF_INET6)?
1007 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
1008 remote_addr = (sockaddr_t*)((u_int8_t*)local_addr + local_len);
1009 remote = host_create_from_sockaddr(remote_addr);
1010 DBG2(DBG_KNL, " kmaddress: %H...%H", local, remote);
1011 }
1012
1013 if (src_ts && dst_ts && local && remote)
1014 {
1015 charon->kernel_interface->migrate(charon->kernel_interface, reqid,
1016 src_ts, dst_ts, dir, local, remote);
1017 }
1018 else
1019 {
1020 DESTROY_IF(src_ts);
1021 DESTROY_IF(dst_ts);
1022 DESTROY_IF(local);
1023 DESTROY_IF(remote);
1024 }
1025 }
1026 #endif /*SADB_X_MIGRATE*/
1027
1028 #ifdef SADB_X_NAT_T_NEW_MAPPING
1029 /**
1030 * Process a SADB_X_NAT_T_NEW_MAPPING message from the kernel
1031 */
1032 static void process_mapping(private_kernel_pfkey_ipsec_t *this, struct sadb_msg* msg)
1033 {
1034 pfkey_msg_t response;
1035 u_int32_t spi, reqid;
1036 host_t *host;
1037
1038 DBG2(DBG_KNL, "received an SADB_X_NAT_T_NEW_MAPPING");
1039
1040 if (parse_pfkey_message(msg, &response) != SUCCESS)
1041 {
1042 DBG1(DBG_KNL, "parsing SADB_X_NAT_T_NEW_MAPPING from kernel failed");
1043 return;
1044 }
1045
1046 if (!response.x_sa2)
1047 {
1048 DBG1(DBG_KNL, "received SADB_X_NAT_T_NEW_MAPPING is missing required "
1049 "information");
1050 return;
1051 }
1052
1053 spi = response.sa->sadb_sa_spi;
1054 reqid = response.x_sa2->sadb_x_sa2_reqid;
1055
1056 if (proto_satype2ike(msg->sadb_msg_satype) == PROTO_ESP)
1057 {
1058 sockaddr_t *sa = (sockaddr_t*)(response.dst + 1);
1059 switch (sa->sa_family)
1060 {
1061 case AF_INET:
1062 {
1063 struct sockaddr_in *sin = (struct sockaddr_in*)sa;
1064 sin->sin_port = htons(response.x_natt_dport->sadb_x_nat_t_port_port);
1065 }
1066 case AF_INET6:
1067 {
1068 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
1069 sin6->sin6_port = htons(response.x_natt_dport->sadb_x_nat_t_port_port);
1070 }
1071 default:
1072 break;
1073 }
1074 host = host_create_from_sockaddr(sa);
1075 if (host)
1076 {
1077 charon->kernel_interface->mapping(charon->kernel_interface, reqid,
1078 spi, host);
1079 }
1080 }
1081 }
1082 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1083
1084 /**
1085 * Receives events from kernel
1086 */
1087 static job_requeue_t receive_events(private_kernel_pfkey_ipsec_t *this)
1088 {
1089 unsigned char buf[PFKEY_BUFFER_SIZE];
1090 struct sadb_msg *msg = (struct sadb_msg*)buf;
1091 int len;
1092 bool oldstate;
1093
1094 oldstate = thread_cancelability(TRUE);
1095 len = recvfrom(this->socket_events, buf, sizeof(buf), 0, NULL, 0);
1096 thread_cancelability(oldstate);
1097
1098 if (len < 0)
1099 {
1100 switch (errno)
1101 {
1102 case EINTR:
1103 /* interrupted, try again */
1104 return JOB_REQUEUE_DIRECT;
1105 case EAGAIN:
1106 /* no data ready, select again */
1107 return JOB_REQUEUE_DIRECT;
1108 default:
1109 DBG1(DBG_KNL, "unable to receive from PF_KEY event socket");
1110 sleep(1);
1111 return JOB_REQUEUE_FAIR;
1112 }
1113 }
1114
1115 if (len < sizeof(struct sadb_msg) ||
1116 msg->sadb_msg_len < PFKEY_LEN(sizeof(struct sadb_msg)))
1117 {
1118 DBG2(DBG_KNL, "received corrupted PF_KEY message");
1119 return JOB_REQUEUE_DIRECT;
1120 }
1121 if (msg->sadb_msg_pid != 0)
1122 { /* not from kernel. not interested, try another one */
1123 return JOB_REQUEUE_DIRECT;
1124 }
1125 if (msg->sadb_msg_len > len / PFKEY_ALIGNMENT)
1126 {
1127 DBG1(DBG_KNL, "buffer was too small to receive the complete PF_KEY message");
1128 return JOB_REQUEUE_DIRECT;
1129 }
1130
1131 switch (msg->sadb_msg_type)
1132 {
1133 case SADB_ACQUIRE:
1134 process_acquire(this, msg);
1135 break;
1136 case SADB_EXPIRE:
1137 process_expire(this, msg);
1138 break;
1139 #ifdef SADB_X_MIGRATE
1140 case SADB_X_MIGRATE:
1141 process_migrate(this, msg);
1142 break;
1143 #endif /*SADB_X_MIGRATE*/
1144 #ifdef SADB_X_NAT_T_NEW_MAPPING
1145 case SADB_X_NAT_T_NEW_MAPPING:
1146 process_mapping(this, msg);
1147 break;
1148 #endif /*SADB_X_NAT_T_NEW_MAPPING*/
1149 default:
1150 break;
1151 }
1152
1153 return JOB_REQUEUE_DIRECT;
1154 }
1155
1156 METHOD(kernel_ipsec_t, get_spi, status_t,
1157 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1158 protocol_id_t protocol, u_int32_t reqid, u_int32_t *spi)
1159 {
1160 unsigned char request[PFKEY_BUFFER_SIZE];
1161 struct sadb_msg *msg, *out;
1162 struct sadb_x_sa2 *sa2;
1163 struct sadb_spirange *range;
1164 pfkey_msg_t response;
1165 u_int32_t received_spi = 0;
1166 size_t len;
1167
1168 memset(&request, 0, sizeof(request));
1169
1170 msg = (struct sadb_msg*)request;
1171 msg->sadb_msg_version = PF_KEY_V2;
1172 msg->sadb_msg_type = SADB_GETSPI;
1173 msg->sadb_msg_satype = proto_ike2satype(protocol);
1174 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1175
1176 sa2 = (struct sadb_x_sa2*)PFKEY_EXT_ADD_NEXT(msg);
1177 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
1178 sa2->sadb_x_sa2_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1179 sa2->sadb_x_sa2_reqid = reqid;
1180 PFKEY_EXT_ADD(msg, sa2);
1181
1182 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0);
1183 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0);
1184
1185 range = (struct sadb_spirange*)PFKEY_EXT_ADD_NEXT(msg);
1186 range->sadb_spirange_exttype = SADB_EXT_SPIRANGE;
1187 range->sadb_spirange_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1188 range->sadb_spirange_min = 0xc0000000;
1189 range->sadb_spirange_max = 0xcFFFFFFF;
1190 PFKEY_EXT_ADD(msg, range);
1191
1192 if (pfkey_send(this, msg, &out, &len) == SUCCESS)
1193 {
1194 if (out->sadb_msg_errno)
1195 {
1196 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
1197 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1198 }
1199 else if (parse_pfkey_message(out, &response) == SUCCESS)
1200 {
1201 received_spi = response.sa->sadb_sa_spi;
1202 }
1203 free(out);
1204 }
1205
1206 if (received_spi == 0)
1207 {
1208 return FAILED;
1209 }
1210
1211 *spi = received_spi;
1212 return SUCCESS;
1213 }
1214
1215 METHOD(kernel_ipsec_t, get_cpi, status_t,
1216 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1217 u_int32_t reqid, u_int16_t *cpi)
1218 {
1219 return FAILED;
1220 }
1221
1222 METHOD(kernel_ipsec_t, add_sa, status_t,
1223 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst, u_int32_t spi,
1224 protocol_id_t protocol, u_int32_t reqid, mark_t mark,
1225 lifetime_cfg_t *lifetime, u_int16_t enc_alg, chunk_t enc_key,
1226 u_int16_t int_alg, chunk_t int_key, ipsec_mode_t mode,
1227 u_int16_t ipcomp, u_int16_t cpi, bool encap, bool inbound,
1228 traffic_selector_t *src_ts, traffic_selector_t *dst_ts)
1229 {
1230 unsigned char request[PFKEY_BUFFER_SIZE];
1231 struct sadb_msg *msg, *out;
1232 struct sadb_sa *sa;
1233 struct sadb_x_sa2 *sa2;
1234 struct sadb_lifetime *lft;
1235 struct sadb_key *key;
1236 size_t len;
1237
1238 memset(&request, 0, sizeof(request));
1239
1240 DBG2(DBG_KNL, "adding SAD entry with SPI %.8x and reqid {%u}", ntohl(spi), reqid);
1241
1242 msg = (struct sadb_msg*)request;
1243 msg->sadb_msg_version = PF_KEY_V2;
1244 msg->sadb_msg_type = inbound ? SADB_UPDATE : SADB_ADD;
1245 msg->sadb_msg_satype = proto_ike2satype(protocol);
1246 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1247
1248 #ifdef __APPLE__
1249 if (encap)
1250 {
1251 struct sadb_sa_2 *sa_2;
1252 sa_2 = (struct sadb_sa_2*)PFKEY_EXT_ADD_NEXT(msg);
1253 sa_2->sadb_sa_natt_port = dst->get_port(dst);
1254 sa = &sa_2->sa;
1255 sa->sadb_sa_flags |= SADB_X_EXT_NATT;
1256 len = sizeof(struct sadb_sa_2);
1257 }
1258 else
1259 #endif
1260 {
1261 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1262 len = sizeof(struct sadb_sa);
1263 }
1264 sa->sadb_sa_exttype = SADB_EXT_SA;
1265 sa->sadb_sa_len = PFKEY_LEN(len);
1266 sa->sadb_sa_spi = spi;
1267 sa->sadb_sa_replay = (protocol == IPPROTO_COMP) ? 0 : 32;
1268 sa->sadb_sa_auth = lookup_algorithm(integrity_algs, int_alg);
1269 sa->sadb_sa_encrypt = lookup_algorithm(encryption_algs, enc_alg);
1270 PFKEY_EXT_ADD(msg, sa);
1271
1272 sa2 = (struct sadb_x_sa2*)PFKEY_EXT_ADD_NEXT(msg);
1273 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
1274 sa2->sadb_x_sa2_len = PFKEY_LEN(sizeof(struct sadb_spirange));
1275 sa2->sadb_x_sa2_mode = mode2kernel(mode);
1276 sa2->sadb_x_sa2_reqid = reqid;
1277 PFKEY_EXT_ADD(msg, sa2);
1278
1279 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0);
1280 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0);
1281
1282 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1283 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1284 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1285 lft->sadb_lifetime_allocations = lifetime->packets.rekey;
1286 lft->sadb_lifetime_bytes = lifetime->bytes.rekey;
1287 lft->sadb_lifetime_addtime = lifetime->time.rekey;
1288 lft->sadb_lifetime_usetime = 0; /* we only use addtime */
1289 PFKEY_EXT_ADD(msg, lft);
1290
1291 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1292 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1293 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1294 lft->sadb_lifetime_allocations = lifetime->packets.life;
1295 lft->sadb_lifetime_bytes = lifetime->bytes.life;
1296 lft->sadb_lifetime_addtime = lifetime->time.life;
1297 lft->sadb_lifetime_usetime = 0; /* we only use addtime */
1298 PFKEY_EXT_ADD(msg, lft);
1299
1300 if (enc_alg != ENCR_UNDEFINED)
1301 {
1302 if (!sa->sadb_sa_encrypt)
1303 {
1304 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1305 encryption_algorithm_names, enc_alg);
1306 return FAILED;
1307 }
1308 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1309 encryption_algorithm_names, enc_alg, enc_key.len * 8);
1310
1311 key = (struct sadb_key*)PFKEY_EXT_ADD_NEXT(msg);
1312 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
1313 key->sadb_key_bits = enc_key.len * 8;
1314 key->sadb_key_len = PFKEY_LEN(sizeof(struct sadb_key) + enc_key.len);
1315 memcpy(key + 1, enc_key.ptr, enc_key.len);
1316
1317 PFKEY_EXT_ADD(msg, key);
1318 }
1319
1320 if (int_alg != AUTH_UNDEFINED)
1321 {
1322 if (!sa->sadb_sa_auth)
1323 {
1324 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1325 integrity_algorithm_names, int_alg);
1326 return FAILED;
1327 }
1328 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1329 integrity_algorithm_names, int_alg, int_key.len * 8);
1330
1331 key = (struct sadb_key*)PFKEY_EXT_ADD_NEXT(msg);
1332 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
1333 key->sadb_key_bits = int_key.len * 8;
1334 key->sadb_key_len = PFKEY_LEN(sizeof(struct sadb_key) + int_key.len);
1335 memcpy(key + 1, int_key.ptr, int_key.len);
1336
1337 PFKEY_EXT_ADD(msg, key);
1338 }
1339
1340 if (ipcomp != IPCOMP_NONE)
1341 {
1342 /*TODO*/
1343 }
1344
1345 #ifdef HAVE_NATT
1346 if (encap)
1347 {
1348 add_encap_ext(msg, src, dst);
1349 }
1350 #endif /*HAVE_NATT*/
1351
1352 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1353 {
1354 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x", ntohl(spi));
1355 return FAILED;
1356 }
1357 else if (out->sadb_msg_errno)
1358 {
1359 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x: %s (%d)",
1360 ntohl(spi), strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1361 free(out);
1362 return FAILED;
1363 }
1364
1365 free(out);
1366 return SUCCESS;
1367 }
1368
1369 METHOD(kernel_ipsec_t, update_sa, status_t,
1370 private_kernel_pfkey_ipsec_t *this, u_int32_t spi, protocol_id_t protocol,
1371 u_int16_t cpi, host_t *src, host_t *dst, host_t *new_src, host_t *new_dst,
1372 bool encap, bool new_encap, mark_t mark)
1373 {
1374 unsigned char request[PFKEY_BUFFER_SIZE];
1375 struct sadb_msg *msg, *out;
1376 struct sadb_sa *sa;
1377 pfkey_msg_t response;
1378 size_t len;
1379
1380 /* we can't update the SA if any of the ip addresses have changed.
1381 * that's because we can't use SADB_UPDATE and by deleting and readding the
1382 * SA the sequence numbers would get lost */
1383 if (!src->ip_equals(src, new_src) ||
1384 !dst->ip_equals(dst, new_dst))
1385 {
1386 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x: address changes"
1387 " are not supported", ntohl(spi));
1388 return NOT_SUPPORTED;
1389 }
1390
1391 memset(&request, 0, sizeof(request));
1392
1393 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x", ntohl(spi));
1394
1395 msg = (struct sadb_msg*)request;
1396 msg->sadb_msg_version = PF_KEY_V2;
1397 msg->sadb_msg_type = SADB_GET;
1398 msg->sadb_msg_satype = proto_ike2satype(protocol);
1399 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1400
1401 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1402 sa->sadb_sa_exttype = SADB_EXT_SA;
1403 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1404 sa->sadb_sa_spi = spi;
1405 PFKEY_EXT_ADD(msg, sa);
1406
1407 /* the kernel wants a SADB_EXT_ADDRESS_SRC to be present even though
1408 * it is not used for anything. */
1409 add_anyaddr_ext(msg, dst->get_family(dst), SADB_EXT_ADDRESS_SRC);
1410 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0);
1411
1412 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1413 {
1414 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x",
1415 ntohl(spi));
1416 return FAILED;
1417 }
1418 else if (out->sadb_msg_errno)
1419 {
1420 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: %s (%d)",
1421 ntohl(spi), strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1422 free(out);
1423 return FAILED;
1424 }
1425 else if (parse_pfkey_message(out, &response) != SUCCESS)
1426 {
1427 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: parsing response "
1428 "from kernel failed", ntohl(spi));
1429 free(out);
1430 return FAILED;
1431 }
1432
1433 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1434 ntohl(spi), src, dst, new_src, new_dst);
1435
1436 memset(&request, 0, sizeof(request));
1437
1438 msg = (struct sadb_msg*)request;
1439 msg->sadb_msg_version = PF_KEY_V2;
1440 msg->sadb_msg_type = SADB_UPDATE;
1441 msg->sadb_msg_satype = proto_ike2satype(protocol);
1442 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1443
1444 #ifdef __APPLE__
1445 {
1446 struct sadb_sa_2 *sa_2;
1447 sa_2 = (struct sadb_sa_2*)PFKEY_EXT_ADD_NEXT(msg);
1448 sa_2->sa.sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa_2));
1449 memcpy(&sa_2->sa, response.sa, sizeof(struct sadb_sa));
1450 if (encap)
1451 {
1452 sa_2->sadb_sa_natt_port = new_dst->get_port(new_dst);
1453 sa_2->sa.sadb_sa_flags |= SADB_X_EXT_NATT;
1454 }
1455 }
1456 #else
1457 PFKEY_EXT_COPY(msg, response.sa);
1458 #endif
1459 PFKEY_EXT_COPY(msg, response.x_sa2);
1460
1461 PFKEY_EXT_COPY(msg, response.src);
1462 PFKEY_EXT_COPY(msg, response.dst);
1463
1464 PFKEY_EXT_COPY(msg, response.lft_soft);
1465 PFKEY_EXT_COPY(msg, response.lft_hard);
1466
1467 if (response.key_encr)
1468 {
1469 PFKEY_EXT_COPY(msg, response.key_encr);
1470 }
1471
1472 if (response.key_auth)
1473 {
1474 PFKEY_EXT_COPY(msg, response.key_auth);
1475 }
1476
1477 #ifdef HAVE_NATT
1478 if (new_encap)
1479 {
1480 add_encap_ext(msg, new_src, new_dst);
1481 }
1482 #endif /*HAVE_NATT*/
1483
1484 free(out);
1485
1486 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1487 {
1488 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1489 return FAILED;
1490 }
1491 else if (out->sadb_msg_errno)
1492 {
1493 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x: %s (%d)",
1494 ntohl(spi), strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1495 free(out);
1496 return FAILED;
1497 }
1498 free(out);
1499
1500 return SUCCESS;
1501 }
1502
1503 METHOD(kernel_ipsec_t, query_sa, status_t,
1504 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1505 u_int32_t spi, protocol_id_t protocol, mark_t mark, u_int64_t *bytes)
1506 {
1507 unsigned char request[PFKEY_BUFFER_SIZE];
1508 struct sadb_msg *msg, *out;
1509 struct sadb_sa *sa;
1510 pfkey_msg_t response;
1511 size_t len;
1512
1513 memset(&request, 0, sizeof(request));
1514
1515 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x", ntohl(spi));
1516
1517 msg = (struct sadb_msg*)request;
1518 msg->sadb_msg_version = PF_KEY_V2;
1519 msg->sadb_msg_type = SADB_GET;
1520 msg->sadb_msg_satype = proto_ike2satype(protocol);
1521 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1522
1523 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1524 sa->sadb_sa_exttype = SADB_EXT_SA;
1525 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1526 sa->sadb_sa_spi = spi;
1527 PFKEY_EXT_ADD(msg, sa);
1528
1529 /* the Linux Kernel doesn't care for the src address, but other systems do
1530 * (e.g. FreeBSD)
1531 */
1532 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0);
1533 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0);
1534
1535 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1536 {
1537 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1538 return FAILED;
1539 }
1540 else if (out->sadb_msg_errno)
1541 {
1542 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x: %s (%d)",
1543 ntohl(spi), strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1544 free(out);
1545 return FAILED;
1546 }
1547 else if (parse_pfkey_message(out, &response) != SUCCESS)
1548 {
1549 DBG1(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1550 free(out);
1551 return FAILED;
1552 }
1553 *bytes = response.lft_current->sadb_lifetime_bytes;
1554
1555 free(out);
1556 return SUCCESS;
1557 }
1558
1559 METHOD(kernel_ipsec_t, del_sa, status_t,
1560 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1561 u_int32_t spi, protocol_id_t protocol, u_int16_t cpi, mark_t mark)
1562 {
1563 unsigned char request[PFKEY_BUFFER_SIZE];
1564 struct sadb_msg *msg, *out;
1565 struct sadb_sa *sa;
1566 size_t len;
1567
1568 memset(&request, 0, sizeof(request));
1569
1570 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x", ntohl(spi));
1571
1572 msg = (struct sadb_msg*)request;
1573 msg->sadb_msg_version = PF_KEY_V2;
1574 msg->sadb_msg_type = SADB_DELETE;
1575 msg->sadb_msg_satype = proto_ike2satype(protocol);
1576 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1577
1578 sa = (struct sadb_sa*)PFKEY_EXT_ADD_NEXT(msg);
1579 sa->sadb_sa_exttype = SADB_EXT_SA;
1580 sa->sadb_sa_len = PFKEY_LEN(sizeof(struct sadb_sa));
1581 sa->sadb_sa_spi = spi;
1582 PFKEY_EXT_ADD(msg, sa);
1583
1584 /* the Linux Kernel doesn't care for the src address, but other systems do
1585 * (e.g. FreeBSD)
1586 */
1587 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0);
1588 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0);
1589
1590 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1591 {
1592 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x", ntohl(spi));
1593 return FAILED;
1594 }
1595 else if (out->sadb_msg_errno)
1596 {
1597 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1598 ntohl(spi), strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1599 free(out);
1600 return FAILED;
1601 }
1602
1603 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x", ntohl(spi));
1604 free(out);
1605 return SUCCESS;
1606 }
1607
1608 METHOD(kernel_ipsec_t, add_policy, status_t,
1609 private_kernel_pfkey_ipsec_t *this, host_t *src, host_t *dst,
1610 traffic_selector_t *src_ts, traffic_selector_t *dst_ts,
1611 policy_dir_t direction, u_int32_t spi, protocol_id_t protocol,
1612 u_int32_t reqid, mark_t mark, ipsec_mode_t mode, u_int16_t ipcomp,
1613 u_int16_t cpi, bool routed)
1614 {
1615 unsigned char request[PFKEY_BUFFER_SIZE];
1616 struct sadb_msg *msg, *out;
1617 struct sadb_x_policy *pol;
1618 struct sadb_x_ipsecrequest *req;
1619 policy_entry_t *policy, *found = NULL;
1620 pfkey_msg_t response;
1621 size_t len;
1622
1623 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
1624 {
1625 /* FWD policies are not supported on all platforms */
1626 return SUCCESS;
1627 }
1628
1629 /* create a policy */
1630 policy = create_policy_entry(src_ts, dst_ts, direction, reqid);
1631
1632 /* find a matching policy */
1633 this->mutex->lock(this->mutex);
1634 if (this->policies->find_first(this->policies,
1635 (linked_list_match_t)policy_entry_equals, (void**)&found, policy) == SUCCESS)
1636 {
1637 /* use existing policy */
1638 found->refcount++;
1639 DBG2(DBG_KNL, "policy %R === %R %N already exists, increasing "
1640 "refcount", src_ts, dst_ts,
1641 policy_dir_names, direction);
1642 policy_entry_destroy(policy);
1643 policy = found;
1644 }
1645 else
1646 {
1647 /* apply the new one, if we have no such policy */
1648 this->policies->insert_last(this->policies, policy);
1649 policy->refcount = 1;
1650 }
1651
1652 memset(&request, 0, sizeof(request));
1653
1654 DBG2(DBG_KNL, "adding policy %R === %R %N", src_ts, dst_ts,
1655 policy_dir_names, direction);
1656
1657 msg = (struct sadb_msg*)request;
1658 msg->sadb_msg_version = PF_KEY_V2;
1659 msg->sadb_msg_type = found ? SADB_X_SPDUPDATE : SADB_X_SPDADD;
1660 msg->sadb_msg_satype = 0;
1661 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1662
1663 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
1664 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1665 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
1666 pol->sadb_x_policy_id = 0;
1667 pol->sadb_x_policy_dir = dir2kernel(direction);
1668 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1669 #ifdef HAVE_STRUCT_SADB_X_POLICY_SADB_X_POLICY_PRIORITY
1670 /* calculate priority based on source selector size, small size = high prio */
1671 pol->sadb_x_policy_priority = routed ? PRIO_LOW : PRIO_HIGH;
1672 pol->sadb_x_policy_priority -= policy->src.mask * 10;
1673 pol->sadb_x_policy_priority -= policy->src.proto != IPSEC_PROTO_ANY ? 2 : 0;
1674 pol->sadb_x_policy_priority -= policy->src.net->get_port(policy->src.net) ? 1 : 0;
1675 #endif
1676
1677 /* one or more sadb_x_ipsecrequest extensions are added to the sadb_x_policy extension */
1678 req = (struct sadb_x_ipsecrequest*)(pol + 1);
1679 req->sadb_x_ipsecrequest_proto = proto_ike2ip(protocol);
1680 /* !!! the length of this struct MUST be in octets instead of 64 bit words */
1681 req->sadb_x_ipsecrequest_len = sizeof(struct sadb_x_ipsecrequest);
1682 req->sadb_x_ipsecrequest_mode = mode2kernel(mode);
1683 req->sadb_x_ipsecrequest_reqid = reqid;
1684 req->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1685 if (mode == MODE_TUNNEL)
1686 {
1687 len = hostcpy(req + 1, src);
1688 req->sadb_x_ipsecrequest_len += len;
1689 len = hostcpy((char*)(req + 1) + len, dst);
1690 req->sadb_x_ipsecrequest_len += len;
1691 }
1692
1693 pol->sadb_x_policy_len += PFKEY_LEN(req->sadb_x_ipsecrequest_len);
1694 PFKEY_EXT_ADD(msg, pol);
1695
1696 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
1697 policy->src.mask);
1698 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
1699 policy->dst.mask);
1700
1701 #ifdef __FreeBSD__
1702 { /* on FreeBSD a lifetime has to be defined to be able to later query
1703 * the current use time. */
1704 struct sadb_lifetime *lft;
1705 lft = (struct sadb_lifetime*)PFKEY_EXT_ADD_NEXT(msg);
1706 lft->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1707 lft->sadb_lifetime_len = PFKEY_LEN(sizeof(struct sadb_lifetime));
1708 lft->sadb_lifetime_addtime = LONG_MAX;
1709 PFKEY_EXT_ADD(msg, lft);
1710 }
1711 #endif
1712
1713 this->mutex->unlock(this->mutex);
1714
1715 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1716 {
1717 DBG1(DBG_KNL, "unable to add policy %R === %R %N", src_ts, dst_ts,
1718 policy_dir_names, direction);
1719 return FAILED;
1720 }
1721 else if (out->sadb_msg_errno)
1722 {
1723 DBG1(DBG_KNL, "unable to add policy %R === %R %N: %s (%d)", src_ts, dst_ts,
1724 policy_dir_names, direction,
1725 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1726 free(out);
1727 return FAILED;
1728 }
1729 else if (parse_pfkey_message(out, &response) != SUCCESS)
1730 {
1731 DBG1(DBG_KNL, "unable to add policy %R === %R %N: parsing response "
1732 "from kernel failed", src_ts, dst_ts, policy_dir_names, direction);
1733 free(out);
1734 return FAILED;
1735 }
1736
1737 this->mutex->lock(this->mutex);
1738
1739 /* we try to find the policy again and update the kernel index */
1740 if (this->policies->find_last(this->policies, NULL, (void**)&policy) != SUCCESS)
1741 {
1742 DBG2(DBG_KNL, "unable to update index, the policy %R === %R %N is "
1743 "already gone, ignoring", src_ts, dst_ts, policy_dir_names, direction);
1744 this->mutex->unlock(this->mutex);
1745 free(out);
1746 return SUCCESS;
1747 }
1748 policy->index = response.x_policy->sadb_x_policy_id;
1749 free(out);
1750
1751 /* install a route, if:
1752 * - we are NOT updating a policy
1753 * - this is a forward policy (to just get one for each child)
1754 * - we are in tunnel mode
1755 * - we are not using IPv6 (does not work correctly yet!)
1756 * - routing is not disabled via strongswan.conf
1757 */
1758 if (policy->route == NULL && direction == POLICY_FWD &&
1759 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6 &&
1760 this->install_routes)
1761 {
1762 route_entry_t *route = malloc_thing(route_entry_t);
1763
1764 if (charon->kernel_interface->get_address_by_ts(charon->kernel_interface,
1765 dst_ts, &route->src_ip) == SUCCESS)
1766 {
1767 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1768 route->gateway = charon->kernel_interface->get_nexthop(
1769 charon->kernel_interface, src);
1770 route->if_name = charon->kernel_interface->get_interface(
1771 charon->kernel_interface, dst);
1772 route->dst_net = chunk_clone(policy->src.net->get_address(policy->src.net));
1773 route->prefixlen = policy->src.mask;
1774
1775 if (route->if_name)
1776 {
1777 switch (charon->kernel_interface->add_route(
1778 charon->kernel_interface, route->dst_net,
1779 route->prefixlen, route->gateway,
1780 route->src_ip, route->if_name))
1781 {
1782 default:
1783 DBG1(DBG_KNL, "unable to install source route for %H",
1784 route->src_ip);
1785 /* FALL */
1786 case ALREADY_DONE:
1787 /* route exists, do not uninstall */
1788 route_entry_destroy(route);
1789 break;
1790 case SUCCESS:
1791 /* cache the installed route */
1792 policy->route = route;
1793 break;
1794 }
1795 }
1796 else
1797 {
1798 route_entry_destroy(route);
1799 }
1800 }
1801 else
1802 {
1803 free(route);
1804 }
1805 }
1806
1807 this->mutex->unlock(this->mutex);
1808
1809 return SUCCESS;
1810 }
1811
1812 METHOD(kernel_ipsec_t, query_policy, status_t,
1813 private_kernel_pfkey_ipsec_t *this, traffic_selector_t *src_ts,
1814 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
1815 u_int32_t *use_time)
1816 {
1817 unsigned char request[PFKEY_BUFFER_SIZE];
1818 struct sadb_msg *msg, *out;
1819 struct sadb_x_policy *pol;
1820 policy_entry_t *policy, *found = NULL;
1821 pfkey_msg_t response;
1822 size_t len;
1823
1824 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
1825 {
1826 /* FWD policies are not supported on all platforms */
1827 return NOT_FOUND;
1828 }
1829
1830 DBG2(DBG_KNL, "querying policy %R === %R %N", src_ts, dst_ts,
1831 policy_dir_names, direction);
1832
1833 /* create a policy */
1834 policy = create_policy_entry(src_ts, dst_ts, direction, 0);
1835
1836 /* find a matching policy */
1837 this->mutex->lock(this->mutex);
1838 if (this->policies->find_first(this->policies,
1839 (linked_list_match_t)policy_entry_equals, (void**)&found, policy) != SUCCESS)
1840 {
1841 DBG1(DBG_KNL, "querying policy %R === %R %N failed, not found", src_ts,
1842 dst_ts, policy_dir_names, direction);
1843 policy_entry_destroy(policy);
1844 this->mutex->unlock(this->mutex);
1845 return NOT_FOUND;
1846 }
1847 policy_entry_destroy(policy);
1848 policy = found;
1849
1850 memset(&request, 0, sizeof(request));
1851
1852 msg = (struct sadb_msg*)request;
1853 msg->sadb_msg_version = PF_KEY_V2;
1854 msg->sadb_msg_type = SADB_X_SPDGET;
1855 msg->sadb_msg_satype = 0;
1856 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1857
1858 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
1859 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1860 pol->sadb_x_policy_id = policy->index;
1861 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
1862 pol->sadb_x_policy_dir = dir2kernel(direction);
1863 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1864 PFKEY_EXT_ADD(msg, pol);
1865
1866 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
1867 policy->src.mask);
1868 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
1869 policy->dst.mask);
1870
1871 this->mutex->unlock(this->mutex);
1872
1873 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1874 {
1875 DBG1(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
1876 policy_dir_names, direction);
1877 return FAILED;
1878 }
1879 else if (out->sadb_msg_errno)
1880 {
1881 DBG1(DBG_KNL, "unable to query policy %R === %R %N: %s (%d)", src_ts,
1882 dst_ts, policy_dir_names, direction,
1883 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1884 free(out);
1885 return FAILED;
1886 }
1887 else if (parse_pfkey_message(out, &response) != SUCCESS)
1888 {
1889 DBG1(DBG_KNL, "unable to query policy %R === %R %N: parsing response "
1890 "from kernel failed", src_ts, dst_ts, policy_dir_names, direction);
1891 free(out);
1892 return FAILED;
1893 }
1894 else if (response.lft_current == NULL)
1895 {
1896 DBG1(DBG_KNL, "unable to query policy %R === %R %N: kernel reports no "
1897 "use time", src_ts, dst_ts, policy_dir_names, direction);
1898 free(out);
1899 return FAILED;
1900 }
1901 /* we need the monotonic time, but the kernel returns system time. */
1902 if (response.lft_current->sadb_lifetime_usetime)
1903 {
1904 *use_time = time_monotonic(NULL) -
1905 (time(NULL) - response.lft_current->sadb_lifetime_usetime);
1906 }
1907 else
1908 {
1909 *use_time = 0;
1910 }
1911 free(out);
1912
1913 return SUCCESS;
1914 }
1915
1916 METHOD(kernel_ipsec_t, del_policy, status_t,
1917 private_kernel_pfkey_ipsec_t *this, traffic_selector_t *src_ts,
1918 traffic_selector_t *dst_ts, policy_dir_t direction, mark_t mark,
1919 bool unrouted)
1920 {
1921 unsigned char request[PFKEY_BUFFER_SIZE];
1922 struct sadb_msg *msg, *out;
1923 struct sadb_x_policy *pol;
1924 policy_entry_t *policy, *found = NULL;
1925 route_entry_t *route;
1926 size_t len;
1927
1928 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
1929 {
1930 /* FWD policies are not supported on all platforms */
1931 return SUCCESS;
1932 }
1933
1934 DBG2(DBG_KNL, "deleting policy %R === %R %N", src_ts, dst_ts,
1935 policy_dir_names, direction);
1936
1937 /* create a policy */
1938 policy = create_policy_entry(src_ts, dst_ts, direction, 0);
1939
1940 /* find a matching policy */
1941 this->mutex->lock(this->mutex);
1942 if (this->policies->find_first(this->policies,
1943 (linked_list_match_t)policy_entry_equals, (void**)&found, policy) == SUCCESS)
1944 {
1945 if (--found->refcount > 0)
1946 {
1947 /* is used by more SAs, keep in kernel */
1948 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
1949 policy_entry_destroy(policy);
1950 this->mutex->unlock(this->mutex);
1951 return SUCCESS;
1952 }
1953 /* remove if last reference */
1954 this->policies->remove(this->policies, found, NULL);
1955 policy_entry_destroy(policy);
1956 policy = found;
1957 }
1958 else
1959 {
1960 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
1961 dst_ts, policy_dir_names, direction);
1962 policy_entry_destroy(policy);
1963 this->mutex->unlock(this->mutex);
1964 return NOT_FOUND;
1965 }
1966 this->mutex->unlock(this->mutex);
1967
1968 memset(&request, 0, sizeof(request));
1969
1970 msg = (struct sadb_msg*)request;
1971 msg->sadb_msg_version = PF_KEY_V2;
1972 msg->sadb_msg_type = SADB_X_SPDDELETE;
1973 msg->sadb_msg_satype = 0;
1974 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1975
1976 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
1977 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1978 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
1979 pol->sadb_x_policy_dir = dir2kernel(direction);
1980 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1981 PFKEY_EXT_ADD(msg, pol);
1982
1983 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
1984 policy->src.mask);
1985 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
1986 policy->dst.mask);
1987
1988 route = policy->route;
1989 policy->route = NULL;
1990 policy_entry_destroy(policy);
1991
1992 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1993 {
1994 DBG1(DBG_KNL, "unable to delete policy %R === %R %N", src_ts, dst_ts,
1995 policy_dir_names, direction);
1996 return FAILED;
1997 }
1998 else if (out->sadb_msg_errno)
1999 {
2000 DBG1(DBG_KNL, "unable to delete policy %R === %R %N: %s (%d)", src_ts,
2001 dst_ts, policy_dir_names, direction,
2002 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2003 free(out);
2004 return FAILED;
2005 }
2006 free(out);
2007
2008 if (route)
2009 {
2010 if (charon->kernel_interface->del_route(charon->kernel_interface,
2011 route->dst_net, route->prefixlen, route->gateway,
2012 route->src_ip, route->if_name) != SUCCESS)
2013 {
2014 DBG1(DBG_KNL, "error uninstalling route installed with "
2015 "policy %R === %R %N", src_ts, dst_ts,
2016 policy_dir_names, direction);
2017 }
2018 route_entry_destroy(route);
2019 }
2020
2021 return SUCCESS;
2022 }
2023
2024 /**
2025 * Register a socket for AQUIRE/EXPIRE messages
2026 */
2027 static status_t register_pfkey_socket(private_kernel_pfkey_ipsec_t *this,
2028 u_int8_t satype)
2029 {
2030 unsigned char request[PFKEY_BUFFER_SIZE];
2031 struct sadb_msg *msg, *out;
2032 size_t len;
2033
2034 memset(&request, 0, sizeof(request));
2035
2036 msg = (struct sadb_msg*)request;
2037 msg->sadb_msg_version = PF_KEY_V2;
2038 msg->sadb_msg_type = SADB_REGISTER;
2039 msg->sadb_msg_satype = satype;
2040 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
2041
2042 if (pfkey_send_socket(this, this->socket_events, msg, &out, &len) != SUCCESS)
2043 {
2044 DBG1(DBG_KNL, "unable to register PF_KEY socket");
2045 return FAILED;
2046 }
2047 else if (out->sadb_msg_errno)
2048 {
2049 DBG1(DBG_KNL, "unable to register PF_KEY socket: %s (%d)",
2050 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
2051 free(out);
2052 return FAILED;
2053 }
2054 free(out);
2055 return SUCCESS;
2056 }
2057
2058 METHOD(kernel_ipsec_t, bypass_socket, bool,
2059 private_kernel_pfkey_ipsec_t *this, int fd, int family)
2060 {
2061 struct sadb_x_policy policy;
2062 u_int sol, ipsec_policy;
2063
2064 switch (family)
2065 {
2066 case AF_INET:
2067 {
2068 sol = SOL_IP;
2069 ipsec_policy = IP_IPSEC_POLICY;
2070 break;
2071 }
2072 case AF_INET6:
2073 {
2074 sol = SOL_IPV6;
2075 ipsec_policy = IPV6_IPSEC_POLICY;
2076 break;
2077 }
2078 default:
2079 return FALSE;
2080 }
2081
2082 memset(&policy, 0, sizeof(policy));
2083 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);
2084 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2085 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;
2086
2087 policy.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;
2088 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2089 {
2090 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2091 strerror(errno));
2092 return FALSE;
2093 }
2094 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;
2095 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2096 {
2097 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2098 strerror(errno));
2099 return FALSE;
2100 }
2101 return TRUE;
2102 }
2103
2104 METHOD(kernel_ipsec_t, destroy, void,
2105 private_kernel_pfkey_ipsec_t *this)
2106 {
2107 if (this->job)
2108 {
2109 this->job->cancel(this->job);
2110 }
2111 if (this->socket > 0)
2112 {
2113 close(this->socket);
2114 }
2115 if (this->socket_events > 0)
2116 {
2117 close(this->socket_events);
2118 }
2119 this->policies->destroy_function(this->policies, (void*)policy_entry_destroy);
2120 this->mutex->destroy(this->mutex);
2121 this->mutex_pfkey->destroy(this->mutex_pfkey);
2122 free(this);
2123 }
2124
2125 /*
2126 * Described in header.
2127 */
2128 kernel_pfkey_ipsec_t *kernel_pfkey_ipsec_create()
2129 {
2130 private_kernel_pfkey_ipsec_t *this;
2131
2132 INIT(this,
2133 .public = {
2134 .interface = {
2135 .get_spi = _get_spi,
2136 .get_cpi = _get_cpi,
2137 .add_sa = _add_sa,
2138 .update_sa = _update_sa,
2139 .query_sa = _query_sa,
2140 .del_sa = _del_sa,
2141 .add_policy = _add_policy,
2142 .query_policy = _query_policy,
2143 .del_policy = _del_policy,
2144 .bypass_socket = _bypass_socket,
2145 .destroy = _destroy,
2146 },
2147 },
2148 .policies = linked_list_create(),
2149 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
2150 .mutex_pfkey = mutex_create(MUTEX_TYPE_DEFAULT),
2151 .install_routes = lib->settings->get_bool(lib->settings,
2152 "charon.install_routes", TRUE),
2153 );
2154
2155 /* create a PF_KEY socket to communicate with the kernel */
2156 this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
2157 if (this->socket <= 0)
2158 {
2159 DBG1(DBG_KNL, "unable to create PF_KEY socket");
2160 destroy(this);
2161 return NULL;
2162 }
2163
2164 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
2165 this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
2166 if (this->socket_events <= 0)
2167 {
2168 DBG1(DBG_KNL, "unable to create PF_KEY event socket");
2169 destroy(this);
2170 return NULL;
2171 }
2172
2173 /* register the event socket */
2174 if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS ||
2175 register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS)
2176 {
2177 DBG1(DBG_KNL, "unable to register PF_KEY event socket");
2178 destroy(this);
2179 return NULL;
2180 }
2181
2182 this->job = callback_job_create((callback_job_cb_t)receive_events,
2183 this, NULL, NULL);
2184 hydra->processor->queue_job(hydra->processor, (job_t*)this->job);
2185
2186 return &this->public;
2187 }
2188