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