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