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