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