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