display transmitted bytes per SA
[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 IPSEC_DIR_INVALID;
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.query_sa.
1493 */
1494 static status_t query_sa(private_kernel_pfkey_ipsec_t *this, host_t *src,
1495 host_t *dst, u_int32_t spi, protocol_id_t protocol,
1496 u_int64_t *bytes)
1497 {
1498 return NOT_SUPPORTED; /* TODO */
1499 }
1500
1501 /**
1502 * Implementation of kernel_interface_t.del_sa.
1503 */
1504 static status_t del_sa(private_kernel_pfkey_ipsec_t *this, host_t *src,
1505 host_t *dst, u_int32_t spi, protocol_id_t protocol,
1506 u_int16_t cpi)
1507 {
1508 unsigned char request[PFKEY_BUFFER_SIZE];
1509 struct sadb_msg *msg, *out;
1510 struct sadb_sa *sa;
1511 size_t len;
1512
1513 memset(&request, 0, sizeof(request));
1514
1515 DBG2(DBG_KNL, "deleting 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_DELETE;
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 (e.g. FreeBSD) */
1530 add_addr_ext(msg, src, SADB_EXT_ADDRESS_SRC, 0, 0);
1531 add_addr_ext(msg, dst, SADB_EXT_ADDRESS_DST, 0, 0);
1532
1533 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1534 {
1535 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x", ntohl(spi));
1536 return FAILED;
1537 }
1538 else if (out->sadb_msg_errno)
1539 {
1540 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x: %s (%d)",
1541 ntohl(spi), strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1542 free(out);
1543 return FAILED;
1544 }
1545
1546 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x", ntohl(spi));
1547 free(out);
1548 return SUCCESS;
1549 }
1550
1551 /**
1552 * Implementation of kernel_interface_t.add_policy.
1553 */
1554 static status_t add_policy(private_kernel_pfkey_ipsec_t *this,
1555 host_t *src, host_t *dst,
1556 traffic_selector_t *src_ts,
1557 traffic_selector_t *dst_ts,
1558 policy_dir_t direction, u_int32_t spi,
1559 protocol_id_t protocol, u_int32_t reqid,
1560 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
1561 bool routed)
1562 {
1563 unsigned char request[PFKEY_BUFFER_SIZE];
1564 struct sadb_msg *msg, *out;
1565 struct sadb_x_policy *pol;
1566 struct sadb_x_ipsecrequest *req;
1567 policy_entry_t *policy, *found = NULL;
1568 pfkey_msg_t response;
1569 size_t len;
1570
1571 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
1572 {
1573 /* FWD policies are not supported on all platforms */
1574 return SUCCESS;
1575 }
1576
1577 /* create a policy */
1578 policy = create_policy_entry(src_ts, dst_ts, direction, reqid);
1579
1580 /* find a matching policy */
1581 this->mutex->lock(this->mutex);
1582 if (this->policies->find_first(this->policies,
1583 (linked_list_match_t)policy_entry_equals, (void**)&found, policy) == SUCCESS)
1584 {
1585 /* use existing policy */
1586 found->refcount++;
1587 DBG2(DBG_KNL, "policy %R === %R %N already exists, increasing "
1588 "refcount", src_ts, dst_ts,
1589 policy_dir_names, direction);
1590 policy_entry_destroy(policy);
1591 policy = found;
1592 }
1593 else
1594 {
1595 /* apply the new one, if we have no such policy */
1596 this->policies->insert_last(this->policies, policy);
1597 policy->refcount = 1;
1598 }
1599
1600 memset(&request, 0, sizeof(request));
1601
1602 DBG2(DBG_KNL, "adding policy %R === %R %N", src_ts, dst_ts,
1603 policy_dir_names, direction);
1604
1605 msg = (struct sadb_msg*)request;
1606 msg->sadb_msg_version = PF_KEY_V2;
1607 msg->sadb_msg_type = found ? SADB_X_SPDUPDATE : SADB_X_SPDADD;
1608 msg->sadb_msg_satype = 0;
1609 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1610
1611 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
1612 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1613 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
1614 pol->sadb_x_policy_id = 0;
1615 pol->sadb_x_policy_dir = dir2kernel(direction);
1616 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1617 #ifdef HAVE_STRUCT_SADB_X_POLICY_SADB_X_POLICY_PRIORITY
1618 /* calculate priority based on source selector size, small size = high prio */
1619 pol->sadb_x_policy_priority = routed ? PRIO_LOW : PRIO_HIGH;
1620 pol->sadb_x_policy_priority -= policy->src.mask * 10;
1621 pol->sadb_x_policy_priority -= policy->src.proto != IPSEC_PROTO_ANY ? 2 : 0;
1622 pol->sadb_x_policy_priority -= policy->src.net->get_port(policy->src.net) ? 1 : 0;
1623 #endif
1624
1625 /* one or more sadb_x_ipsecrequest extensions are added to the sadb_x_policy extension */
1626 req = (struct sadb_x_ipsecrequest*)(pol + 1);
1627 req->sadb_x_ipsecrequest_proto = proto_ike2ip(protocol);
1628 /* !!! the length of this struct MUST be in octets instead of 64 bit words */
1629 req->sadb_x_ipsecrequest_len = sizeof(struct sadb_x_ipsecrequest);
1630 req->sadb_x_ipsecrequest_mode = mode2kernel(mode);
1631 req->sadb_x_ipsecrequest_reqid = reqid;
1632 req->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1633 if (mode == MODE_TUNNEL)
1634 {
1635 sockaddr_t *sa;
1636 socklen_t sl;
1637 sa = src->get_sockaddr(src);
1638 sl = *src->get_sockaddr_len(src);
1639 memcpy(req + 1, sa, sl);
1640 sa = dst->get_sockaddr(dst);
1641 memcpy((u_int8_t*)(req + 1) + sl, sa, sl);
1642 req->sadb_x_ipsecrequest_len += sl * 2;
1643 }
1644
1645 pol->sadb_x_policy_len += PFKEY_LEN(req->sadb_x_ipsecrequest_len);
1646 PFKEY_EXT_ADD(msg, pol);
1647
1648 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
1649 policy->src.mask);
1650 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
1651 policy->dst.mask);
1652
1653 this->mutex->unlock(this->mutex);
1654
1655 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1656 {
1657 DBG1(DBG_KNL, "unable to add policy %R === %R %N", src_ts, dst_ts,
1658 policy_dir_names, direction);
1659 return FAILED;
1660 }
1661 else if (out->sadb_msg_errno)
1662 {
1663 DBG1(DBG_KNL, "unable to add policy %R === %R %N: %s (%d)", src_ts, dst_ts,
1664 policy_dir_names, direction,
1665 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1666 free(out);
1667 return FAILED;
1668 }
1669 else if (parse_pfkey_message(out, &response) != SUCCESS)
1670 {
1671 DBG1(DBG_KNL, "unable to add policy %R === %R %N: parsing response "
1672 "from kernel failed", src_ts, dst_ts, policy_dir_names, direction);
1673 free(out);
1674 return FAILED;
1675 }
1676
1677 this->mutex->lock(this->mutex);
1678
1679 /* we try to find the policy again and update the kernel index */
1680 if (this->policies->find_last(this->policies, NULL, (void**)&policy) != SUCCESS)
1681 {
1682 DBG2(DBG_KNL, "unable to update index, the policy %R === %R %N is "
1683 "already gone, ignoring", src_ts, dst_ts, policy_dir_names, direction);
1684 this->mutex->unlock(this->mutex);
1685 free(out);
1686 return SUCCESS;
1687 }
1688 policy->index = response.x_policy->sadb_x_policy_id;
1689 free(out);
1690
1691 /* install a route, if:
1692 * - we are NOT updating a policy
1693 * - this is a forward policy (to just get one for each child)
1694 * - we are in tunnel mode
1695 * - we are not using IPv6 (does not work correctly yet!)
1696 * - routing is not disabled via strongswan.conf
1697 */
1698 if (policy->route == NULL && direction == POLICY_FWD &&
1699 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6 &&
1700 this->install_routes)
1701 {
1702 route_entry_t *route = malloc_thing(route_entry_t);
1703
1704 if (charon->kernel_interface->get_address_by_ts(charon->kernel_interface,
1705 dst_ts, &route->src_ip) == SUCCESS)
1706 {
1707 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1708 route->gateway = charon->kernel_interface->get_nexthop(
1709 charon->kernel_interface, src);
1710 route->if_name = charon->kernel_interface->get_interface(
1711 charon->kernel_interface, dst);
1712 route->dst_net = chunk_clone(policy->src.net->get_address(policy->src.net));
1713 route->prefixlen = policy->src.mask;
1714
1715 switch (charon->kernel_interface->add_route(charon->kernel_interface,
1716 route->dst_net, route->prefixlen, route->gateway,
1717 route->src_ip, route->if_name))
1718 {
1719 default:
1720 DBG1(DBG_KNL, "unable to install source route for %H",
1721 route->src_ip);
1722 /* FALL */
1723 case ALREADY_DONE:
1724 /* route exists, do not uninstall */
1725 route_entry_destroy(route);
1726 break;
1727 case SUCCESS:
1728 /* cache the installed route */
1729 policy->route = route;
1730 break;
1731 }
1732 }
1733 else
1734 {
1735 free(route);
1736 }
1737 }
1738
1739 this->mutex->unlock(this->mutex);
1740
1741 return SUCCESS;
1742 }
1743
1744 /**
1745 * Implementation of kernel_interface_t.query_policy.
1746 */
1747 static status_t query_policy(private_kernel_pfkey_ipsec_t *this,
1748 traffic_selector_t *src_ts,
1749 traffic_selector_t *dst_ts,
1750 policy_dir_t direction, u_int32_t *use_time)
1751 {
1752 unsigned char request[PFKEY_BUFFER_SIZE];
1753 struct sadb_msg *msg, *out;
1754 struct sadb_x_policy *pol;
1755 policy_entry_t *policy, *found = NULL;
1756 pfkey_msg_t response;
1757 size_t len;
1758
1759 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
1760 {
1761 /* FWD policies are not supported on all platforms */
1762 return NOT_FOUND;
1763 }
1764
1765 DBG2(DBG_KNL, "querying policy %R === %R %N", src_ts, dst_ts,
1766 policy_dir_names, direction);
1767
1768 /* create a policy */
1769 policy = create_policy_entry(src_ts, dst_ts, direction, 0);
1770
1771 /* find a matching policy */
1772 this->mutex->lock(this->mutex);
1773 if (this->policies->find_first(this->policies,
1774 (linked_list_match_t)policy_entry_equals, (void**)&found, policy) != SUCCESS)
1775 {
1776 DBG1(DBG_KNL, "querying policy %R === %R %N failed, not found", src_ts,
1777 dst_ts, policy_dir_names, direction);
1778 policy_entry_destroy(policy);
1779 this->mutex->unlock(this->mutex);
1780 return NOT_FOUND;
1781 }
1782 policy_entry_destroy(policy);
1783 policy = found;
1784
1785 memset(&request, 0, sizeof(request));
1786
1787 msg = (struct sadb_msg*)request;
1788 msg->sadb_msg_version = PF_KEY_V2;
1789 msg->sadb_msg_type = SADB_X_SPDGET;
1790 msg->sadb_msg_satype = 0;
1791 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1792
1793 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
1794 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1795 pol->sadb_x_policy_id = policy->index;
1796 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
1797 pol->sadb_x_policy_dir = dir2kernel(direction);
1798 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1799 PFKEY_EXT_ADD(msg, pol);
1800
1801 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
1802 policy->src.mask);
1803 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
1804 policy->dst.mask);
1805
1806 this->mutex->unlock(this->mutex);
1807
1808 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1809 {
1810 DBG1(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
1811 policy_dir_names, direction);
1812 return FAILED;
1813 }
1814 else if (out->sadb_msg_errno)
1815 {
1816 DBG1(DBG_KNL, "unable to query policy %R === %R %N: %s (%d)", src_ts,
1817 dst_ts, policy_dir_names, direction,
1818 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1819 free(out);
1820 return FAILED;
1821 }
1822 else if (parse_pfkey_message(out, &response) != SUCCESS)
1823 {
1824 DBG1(DBG_KNL, "unable to query policy %R === %R %N: parsing response "
1825 "from kernel failed", src_ts, dst_ts, policy_dir_names, direction);
1826 free(out);
1827 return FAILED;
1828 }
1829
1830 *use_time = response.lft_current->sadb_lifetime_usetime;
1831
1832 free(out);
1833
1834 return SUCCESS;
1835 }
1836
1837 /**
1838 * Implementation of kernel_interface_t.del_policy.
1839 */
1840 static status_t del_policy(private_kernel_pfkey_ipsec_t *this,
1841 traffic_selector_t *src_ts,
1842 traffic_selector_t *dst_ts,
1843 policy_dir_t direction, bool unrouted)
1844 {
1845 unsigned char request[PFKEY_BUFFER_SIZE];
1846 struct sadb_msg *msg, *out;
1847 struct sadb_x_policy *pol;
1848 policy_entry_t *policy, *found = NULL;
1849 route_entry_t *route;
1850 size_t len;
1851
1852 if (dir2kernel(direction) == IPSEC_DIR_INVALID)
1853 {
1854 /* FWD policies are not supported on all platforms */
1855 return SUCCESS;
1856 }
1857
1858 DBG2(DBG_KNL, "deleting policy %R === %R %N", src_ts, dst_ts,
1859 policy_dir_names, direction);
1860
1861 /* create a policy */
1862 policy = create_policy_entry(src_ts, dst_ts, direction, 0);
1863
1864 /* find a matching policy */
1865 this->mutex->lock(this->mutex);
1866 if (this->policies->find_first(this->policies,
1867 (linked_list_match_t)policy_entry_equals, (void**)&found, policy) == SUCCESS)
1868 {
1869 if (--found->refcount > 0)
1870 {
1871 /* is used by more SAs, keep in kernel */
1872 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
1873 policy_entry_destroy(policy);
1874 this->mutex->unlock(this->mutex);
1875 return SUCCESS;
1876 }
1877 /* remove if last reference */
1878 this->policies->remove(this->policies, found, NULL);
1879 policy_entry_destroy(policy);
1880 policy = found;
1881 }
1882 else
1883 {
1884 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
1885 dst_ts, policy_dir_names, direction);
1886 policy_entry_destroy(policy);
1887 this->mutex->unlock(this->mutex);
1888 return NOT_FOUND;
1889 }
1890 this->mutex->unlock(this->mutex);
1891
1892 memset(&request, 0, sizeof(request));
1893
1894 msg = (struct sadb_msg*)request;
1895 msg->sadb_msg_version = PF_KEY_V2;
1896 msg->sadb_msg_type = SADB_X_SPDDELETE;
1897 msg->sadb_msg_satype = 0;
1898 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1899
1900 pol = (struct sadb_x_policy*)PFKEY_EXT_ADD_NEXT(msg);
1901 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1902 pol->sadb_x_policy_len = PFKEY_LEN(sizeof(struct sadb_x_policy));
1903 pol->sadb_x_policy_dir = dir2kernel(direction);
1904 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1905 PFKEY_EXT_ADD(msg, pol);
1906
1907 add_addr_ext(msg, policy->src.net, SADB_EXT_ADDRESS_SRC, policy->src.proto,
1908 policy->src.mask);
1909 add_addr_ext(msg, policy->dst.net, SADB_EXT_ADDRESS_DST, policy->dst.proto,
1910 policy->dst.mask);
1911
1912 route = policy->route;
1913 policy->route = NULL;
1914 policy_entry_destroy(policy);
1915
1916 if (pfkey_send(this, msg, &out, &len) != SUCCESS)
1917 {
1918 DBG1(DBG_KNL, "unable to delete policy %R === %R %N", src_ts, dst_ts,
1919 policy_dir_names, direction);
1920 return FAILED;
1921 }
1922 else if (out->sadb_msg_errno)
1923 {
1924 DBG1(DBG_KNL, "unable to delete policy %R === %R %N: %s (%d)", src_ts,
1925 dst_ts, policy_dir_names, direction,
1926 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1927 free(out);
1928 return FAILED;
1929 }
1930 free(out);
1931
1932 if (route)
1933 {
1934 if (charon->kernel_interface->del_route(charon->kernel_interface,
1935 route->dst_net, route->prefixlen, route->gateway,
1936 route->src_ip, route->if_name) != SUCCESS)
1937 {
1938 DBG1(DBG_KNL, "error uninstalling route installed with "
1939 "policy %R === %R %N", src_ts, dst_ts,
1940 policy_dir_names, direction);
1941 }
1942 route_entry_destroy(route);
1943 }
1944
1945 return SUCCESS;
1946 }
1947
1948 /**
1949 * Register a socket for AQUIRE/EXPIRE messages
1950 */
1951 static status_t register_pfkey_socket(private_kernel_pfkey_ipsec_t *this, u_int8_t satype)
1952 {
1953 unsigned char request[PFKEY_BUFFER_SIZE];
1954 struct sadb_msg *msg, *out;
1955 size_t len;
1956
1957 memset(&request, 0, sizeof(request));
1958
1959 msg = (struct sadb_msg*)request;
1960 msg->sadb_msg_version = PF_KEY_V2;
1961 msg->sadb_msg_type = SADB_REGISTER;
1962 msg->sadb_msg_satype = satype;
1963 msg->sadb_msg_len = PFKEY_LEN(sizeof(struct sadb_msg));
1964
1965 if (pfkey_send_socket(this, this->socket_events, msg, &out, &len) != SUCCESS)
1966 {
1967 DBG1(DBG_KNL, "unable to register PF_KEY socket");
1968 return FAILED;
1969 }
1970 else if (out->sadb_msg_errno)
1971 {
1972 DBG1(DBG_KNL, "unable to register PF_KEY socket: %s (%d)",
1973 strerror(out->sadb_msg_errno), out->sadb_msg_errno);
1974 free(out);
1975 return FAILED;
1976 }
1977 free(out);
1978 return SUCCESS;
1979 }
1980
1981 /**
1982 * Implementation of kernel_interface_t.destroy.
1983 */
1984 static void destroy(private_kernel_pfkey_ipsec_t *this)
1985 {
1986 this->job->cancel(this->job);
1987 close(this->socket);
1988 close(this->socket_events);
1989 this->policies->destroy_function(this->policies, (void*)policy_entry_destroy);
1990 this->mutex->destroy(this->mutex);
1991 this->mutex_pfkey->destroy(this->mutex_pfkey);
1992 free(this);
1993 }
1994
1995 /**
1996 * Add bypass policies for IKE on the sockets of charon
1997 */
1998 static bool add_bypass_policies(private_kernel_pfkey_ipsec_t *this)
1999 {
2000 int fd, family, port;
2001 enumerator_t *sockets;
2002 bool status = TRUE;
2003
2004 sockets = charon->socket->create_enumerator(charon->socket);
2005 while (sockets->enumerate(sockets, &fd, &family, &port))
2006 {
2007 struct sadb_x_policy policy;
2008 u_int sol, ipsec_policy;
2009
2010 switch (family)
2011 {
2012 case AF_INET:
2013 {
2014 sol = SOL_IP;
2015 ipsec_policy = IP_IPSEC_POLICY;
2016 break;
2017 }
2018 case AF_INET6:
2019 {
2020 sol = SOL_IPV6;
2021 ipsec_policy = IPV6_IPSEC_POLICY;
2022 break;
2023 }
2024 default:
2025 continue;
2026 }
2027
2028 memset(&policy, 0, sizeof(policy));
2029 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);
2030 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2031 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;
2032
2033 policy.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;
2034 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2035 {
2036 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2037 strerror(errno));
2038 status = FALSE;
2039 break;
2040 }
2041 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;
2042 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
2043 {
2044 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
2045 strerror(errno));
2046 status = FALSE;
2047 break;
2048 }
2049 }
2050 sockets->destroy(sockets);
2051 return status;
2052 }
2053
2054 /*
2055 * Described in header.
2056 */
2057 kernel_pfkey_ipsec_t *kernel_pfkey_ipsec_create()
2058 {
2059 private_kernel_pfkey_ipsec_t *this = malloc_thing(private_kernel_pfkey_ipsec_t);
2060
2061 /* public functions */
2062 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;
2063 this->public.interface.get_cpi = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,u_int32_t,u_int16_t*))get_cpi;
2064 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;
2065 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;
2066 this->public.interface.query_sa = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,u_int32_t,protocol_id_t,u_int64_t*))query_sa;
2067 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;
2068 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;
2069 this->public.interface.query_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
2070 this->public.interface.del_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,bool))del_policy;
2071
2072 this->public.interface.destroy = (void(*)(kernel_ipsec_t*)) destroy;
2073
2074 /* private members */
2075 this->policies = linked_list_create();
2076 this->mutex = mutex_create(MUTEX_DEFAULT);
2077 this->mutex_pfkey = mutex_create(MUTEX_DEFAULT);
2078 this->install_routes = lib->settings->get_bool(lib->settings,
2079 "charon.install_routes", TRUE);
2080 this->seq = 0;
2081
2082 /* create a PF_KEY socket to communicate with the kernel */
2083 this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
2084 if (this->socket <= 0)
2085 {
2086 charon->kill(charon, "unable to create PF_KEY socket");
2087 }
2088
2089 /* create a PF_KEY socket for ACQUIRE & EXPIRE */
2090 this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
2091 if (this->socket_events <= 0)
2092 {
2093 charon->kill(charon, "unable to create PF_KEY event socket");
2094 }
2095
2096 /* add bypass policies on the sockets used by charon */
2097 if (!add_bypass_policies(this))
2098 {
2099 charon->kill(charon, "unable to add bypass policies on sockets");
2100 }
2101
2102 /* register the event socket */
2103 if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS ||
2104 register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS)
2105 {
2106 charon->kill(charon, "unable to register PF_KEY event socket");
2107 }
2108
2109 this->job = callback_job_create((callback_job_cb_t)receive_events,
2110 this, NULL, NULL);
2111 charon->processor->queue_job(charon->processor, (job_t*)this->job);
2112
2113 return &this->public;
2114 }