cosmetics
[strongswan.git] / src / charon / plugins / kernel_netlink / kernel_netlink_ipsec.c
1 /*
2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2008 Andreas Steffen
5 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
6 * Copyright (C) 2006 Daniel Roethlisberger
7 * Copyright (C) 2005 Jan Hutter
8 * Hochschule fuer Technik Rapperswil
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 */
20
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/time.h>
24 #include <stdint.h>
25 #include <linux/ipsec.h>
26 #include <linux/netlink.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/xfrm.h>
29 #include <linux/udp.h>
30 #include <pthread.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include "kernel_netlink_ipsec.h"
36 #include "kernel_netlink_shared.h"
37
38 #include <daemon.h>
39 #include <utils/mutex.h>
40 #include <utils/hashtable.h>
41 #include <processing/jobs/callback_job.h>
42 #include <processing/jobs/acquire_job.h>
43 #include <processing/jobs/migrate_job.h>
44 #include <processing/jobs/rekey_child_sa_job.h>
45 #include <processing/jobs/delete_child_sa_job.h>
46 #include <processing/jobs/update_sa_job.h>
47
48 /** required for Linux 2.6.26 kernel and later */
49 #ifndef XFRM_STATE_AF_UNSPEC
50 #define XFRM_STATE_AF_UNSPEC 32
51 #endif
52
53 /** from linux/in.h */
54 #ifndef IP_IPSEC_POLICY
55 #define IP_IPSEC_POLICY 16
56 #endif
57
58 /* missing on uclibc */
59 #ifndef IPV6_IPSEC_POLICY
60 #define IPV6_IPSEC_POLICY 34
61 #endif /*IPV6_IPSEC_POLICY*/
62
63 /** default priority of installed policies */
64 #define PRIO_LOW 3000
65 #define PRIO_HIGH 2000
66
67 /**
68 * Create ORable bitfield of XFRM NL groups
69 */
70 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
71
72 /**
73 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
74 * 'usual' netlink data x like 'struct xfrm_usersa_info'
75 */
76 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
77 /**
78 * returns a pointer to the next rtattr following rta.
79 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
80 */
81 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
82 /**
83 * returns the total size of attached rta data
84 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
85 */
86 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
87
88 typedef struct kernel_algorithm_t kernel_algorithm_t;
89
90 /**
91 * Mapping of IKEv2 kernel identifier to linux crypto API names
92 */
93 struct kernel_algorithm_t {
94 /**
95 * Identifier specified in IKEv2
96 */
97 int ikev2;
98
99 /**
100 * Name of the algorithm in linux crypto API
101 */
102 char *name;
103 };
104
105 ENUM(xfrm_msg_names, XFRM_MSG_NEWSA, XFRM_MSG_MAPPING,
106 "XFRM_MSG_NEWSA",
107 "XFRM_MSG_DELSA",
108 "XFRM_MSG_GETSA",
109 "XFRM_MSG_NEWPOLICY",
110 "XFRM_MSG_DELPOLICY",
111 "XFRM_MSG_GETPOLICY",
112 "XFRM_MSG_ALLOCSPI",
113 "XFRM_MSG_ACQUIRE",
114 "XFRM_MSG_EXPIRE",
115 "XFRM_MSG_UPDPOLICY",
116 "XFRM_MSG_UPDSA",
117 "XFRM_MSG_POLEXPIRE",
118 "XFRM_MSG_FLUSHSA",
119 "XFRM_MSG_FLUSHPOLICY",
120 "XFRM_MSG_NEWAE",
121 "XFRM_MSG_GETAE",
122 "XFRM_MSG_REPORT",
123 "XFRM_MSG_MIGRATE",
124 "XFRM_MSG_NEWSADINFO",
125 "XFRM_MSG_GETSADINFO",
126 "XFRM_MSG_NEWSPDINFO",
127 "XFRM_MSG_GETSPDINFO",
128 "XFRM_MSG_MAPPING"
129 );
130
131 ENUM(xfrm_attr_type_names, XFRMA_UNSPEC, XFRMA_KMADDRESS,
132 "XFRMA_UNSPEC",
133 "XFRMA_ALG_AUTH",
134 "XFRMA_ALG_CRYPT",
135 "XFRMA_ALG_COMP",
136 "XFRMA_ENCAP",
137 "XFRMA_TMPL",
138 "XFRMA_SA",
139 "XFRMA_POLICY",
140 "XFRMA_SEC_CTX",
141 "XFRMA_LTIME_VAL",
142 "XFRMA_REPLAY_VAL",
143 "XFRMA_REPLAY_THRESH",
144 "XFRMA_ETIMER_THRESH",
145 "XFRMA_SRCADDR",
146 "XFRMA_COADDR",
147 "XFRMA_LASTUSED",
148 "XFRMA_POLICY_TYPE",
149 "XFRMA_MIGRATE",
150 "XFRMA_ALG_AEAD",
151 "XFRMA_KMADDRESS"
152 );
153
154 #define END_OF_LIST -1
155
156 /**
157 * Algorithms for encryption
158 */
159 static kernel_algorithm_t encryption_algs[] = {
160 /* {ENCR_DES_IV64, "***" }, */
161 {ENCR_DES, "des" },
162 {ENCR_3DES, "des3_ede" },
163 /* {ENCR_RC5, "***" }, */
164 /* {ENCR_IDEA, "***" }, */
165 {ENCR_CAST, "cast128" },
166 {ENCR_BLOWFISH, "blowfish" },
167 /* {ENCR_3IDEA, "***" }, */
168 /* {ENCR_DES_IV32, "***" }, */
169 {ENCR_NULL, "cipher_null" },
170 {ENCR_AES_CBC, "aes" },
171 {ENCR_AES_CTR, "rfc3686(ctr(aes))" },
172 {ENCR_AES_CCM_ICV8, "rfc4309(ccm(aes))" },
173 {ENCR_AES_CCM_ICV12, "rfc4309(ccm(aes))" },
174 {ENCR_AES_CCM_ICV16, "rfc4309(ccm(aes))" },
175 {ENCR_AES_GCM_ICV8, "rfc4106(gcm(aes))" },
176 {ENCR_AES_GCM_ICV12, "rfc4106(gcm(aes))" },
177 {ENCR_AES_GCM_ICV16, "rfc4106(gcm(aes))" },
178 /* {ENCR_NULL_AUTH_AES_GMAC, "***" }, */
179 {ENCR_CAMELLIA_CBC, "cbc(camellia)" },
180 /* {ENCR_CAMELLIA_CTR, "***" }, */
181 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
182 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
183 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
184 {END_OF_LIST, NULL }
185 };
186
187 /**
188 * Algorithms for integrity protection
189 */
190 static kernel_algorithm_t integrity_algs[] = {
191 {AUTH_HMAC_MD5_96, "md5" },
192 {AUTH_HMAC_SHA1_96, "sha1" },
193 {AUTH_HMAC_SHA2_256_128, "sha256" },
194 {AUTH_HMAC_SHA2_384_192, "sha384" },
195 {AUTH_HMAC_SHA2_512_256, "sha512" },
196 /* {AUTH_DES_MAC, "***" }, */
197 /* {AUTH_KPDK_MD5, "***" }, */
198 {AUTH_AES_XCBC_96, "xcbc(aes)" },
199 {END_OF_LIST, NULL }
200 };
201
202 /**
203 * Algorithms for IPComp
204 */
205 static kernel_algorithm_t compression_algs[] = {
206 /* {IPCOMP_OUI, "***" }, */
207 {IPCOMP_DEFLATE, "deflate" },
208 {IPCOMP_LZS, "lzs" },
209 {IPCOMP_LZJH, "lzjh" },
210 {END_OF_LIST, NULL }
211 };
212
213 /**
214 * Look up a kernel algorithm name and its key size
215 */
216 static char* lookup_algorithm(kernel_algorithm_t *list, int ikev2)
217 {
218 while (list->ikev2 != END_OF_LIST)
219 {
220 if (list->ikev2 == ikev2)
221 {
222 return list->name;
223 }
224 list++;
225 }
226 return NULL;
227 }
228
229 typedef struct route_entry_t route_entry_t;
230
231 /**
232 * installed routing entry
233 */
234 struct route_entry_t {
235 /** Name of the interface the route is bound to */
236 char *if_name;
237
238 /** Source ip of the route */
239 host_t *src_ip;
240
241 /** gateway for this route */
242 host_t *gateway;
243
244 /** Destination net */
245 chunk_t dst_net;
246
247 /** Destination net prefixlen */
248 u_int8_t prefixlen;
249 };
250
251 /**
252 * destroy an route_entry_t object
253 */
254 static void route_entry_destroy(route_entry_t *this)
255 {
256 free(this->if_name);
257 this->src_ip->destroy(this->src_ip);
258 this->gateway->destroy(this->gateway);
259 chunk_free(&this->dst_net);
260 free(this);
261 }
262
263 typedef struct policy_entry_t policy_entry_t;
264
265 /**
266 * installed kernel policy.
267 */
268 struct policy_entry_t {
269
270 /** direction of this policy: in, out, forward */
271 u_int8_t direction;
272
273 /** parameters of installed policy */
274 struct xfrm_selector sel;
275
276 /** associated route installed for this policy */
277 route_entry_t *route;
278
279 /** by how many CHILD_SA's this policy is used */
280 u_int refcount;
281 };
282
283 /**
284 * Hash function for policy_entry_t objects
285 */
286 static u_int policy_hash(policy_entry_t *key)
287 {
288 chunk_t chunk = chunk_create((void*)&key->sel, sizeof(struct xfrm_selector));
289 return chunk_hash(chunk);
290 }
291
292 /**
293 * Equality function for policy_entry_t objects
294 */
295 static bool policy_equals(policy_entry_t *key, policy_entry_t *other_key)
296 {
297 return memeq(&key->sel, &other_key->sel, sizeof(struct xfrm_selector)) &&
298 key->direction == other_key->direction;
299 }
300
301 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t;
302
303 /**
304 * Private variables and functions of kernel_netlink class.
305 */
306 struct private_kernel_netlink_ipsec_t {
307 /**
308 * Public part of the kernel_netlink_t object.
309 */
310 kernel_netlink_ipsec_t public;
311
312 /**
313 * mutex to lock access to various lists
314 */
315 mutex_t *mutex;
316
317 /**
318 * Hash table of installed policies (policy_entry_t)
319 */
320 hashtable_t *policies;
321
322 /**
323 * job receiving netlink events
324 */
325 callback_job_t *job;
326
327 /**
328 * Netlink xfrm socket (IPsec)
329 */
330 netlink_socket_t *socket_xfrm;
331
332 /**
333 * netlink xfrm socket to receive acquire and expire events
334 */
335 int socket_xfrm_events;
336
337 /**
338 * whether to install routes along policies
339 */
340 bool install_routes;
341 };
342
343 /**
344 * convert a IKEv2 specific protocol identifier to the kernel one
345 */
346 static u_int8_t proto_ike2kernel(protocol_id_t proto)
347 {
348 switch (proto)
349 {
350 case PROTO_ESP:
351 return IPPROTO_ESP;
352 case PROTO_AH:
353 return IPPROTO_AH;
354 default:
355 return proto;
356 }
357 }
358
359 /**
360 * reverse of ike2kernel
361 */
362 static protocol_id_t proto_kernel2ike(u_int8_t proto)
363 {
364 switch (proto)
365 {
366 case IPPROTO_ESP:
367 return PROTO_ESP;
368 case IPPROTO_AH:
369 return PROTO_AH;
370 default:
371 return proto;
372 }
373 }
374
375 /**
376 * convert the general ipsec mode to the one defined in xfrm.h
377 */
378 static u_int8_t mode2kernel(ipsec_mode_t mode)
379 {
380 switch (mode)
381 {
382 case MODE_TRANSPORT:
383 return XFRM_MODE_TRANSPORT;
384 case MODE_TUNNEL:
385 return XFRM_MODE_TUNNEL;
386 case MODE_BEET:
387 return XFRM_MODE_BEET;
388 default:
389 return mode;
390 }
391 }
392
393 /**
394 * convert a host_t to a struct xfrm_address
395 */
396 static void host2xfrm(host_t *host, xfrm_address_t *xfrm)
397 {
398 chunk_t chunk = host->get_address(host);
399 memcpy(xfrm, chunk.ptr, min(chunk.len, sizeof(xfrm_address_t)));
400 }
401
402 /**
403 * convert a struct xfrm_address to a host_t
404 */
405 static host_t* xfrm2host(int family, xfrm_address_t *xfrm, u_int16_t port)
406 {
407 chunk_t chunk;
408
409 switch (family)
410 {
411 case AF_INET:
412 chunk = chunk_create((u_char*)&xfrm->a4, sizeof(xfrm->a4));
413 break;
414 case AF_INET6:
415 chunk = chunk_create((u_char*)&xfrm->a6, sizeof(xfrm->a6));
416 break;
417 default:
418 return NULL;
419 }
420 return host_create_from_chunk(family, chunk, ntohs(port));
421 }
422
423 /**
424 * convert a traffic selector address range to subnet and its mask.
425 */
426 static void ts2subnet(traffic_selector_t* ts,
427 xfrm_address_t *net, u_int8_t *mask)
428 {
429 host_t *net_host;
430 chunk_t net_chunk;
431
432 ts->to_subnet(ts, &net_host, mask);
433 net_chunk = net_host->get_address(net_host);
434 memcpy(net, net_chunk.ptr, net_chunk.len);
435 net_host->destroy(net_host);
436 }
437
438 /**
439 * convert a traffic selector port range to port/portmask
440 */
441 static void ts2ports(traffic_selector_t* ts,
442 u_int16_t *port, u_int16_t *mask)
443 {
444 /* linux does not seem to accept complex portmasks. Only
445 * any or a specific port is allowed. We set to any, if we have
446 * a port range, or to a specific, if we have one port only.
447 */
448 u_int16_t from, to;
449
450 from = ts->get_from_port(ts);
451 to = ts->get_to_port(ts);
452
453 if (from == to)
454 {
455 *port = htons(from);
456 *mask = ~0;
457 }
458 else
459 {
460 *port = 0;
461 *mask = 0;
462 }
463 }
464
465 /**
466 * convert a pair of traffic_selectors to a xfrm_selector
467 */
468 static struct xfrm_selector ts2selector(traffic_selector_t *src,
469 traffic_selector_t *dst)
470 {
471 struct xfrm_selector sel;
472
473 memset(&sel, 0, sizeof(sel));
474 sel.family = (src->get_type(src) == TS_IPV4_ADDR_RANGE) ? AF_INET : AF_INET6;
475 /* src or dest proto may be "any" (0), use more restrictive one */
476 sel.proto = max(src->get_protocol(src), dst->get_protocol(dst));
477 ts2subnet(dst, &sel.daddr, &sel.prefixlen_d);
478 ts2subnet(src, &sel.saddr, &sel.prefixlen_s);
479 ts2ports(dst, &sel.dport, &sel.dport_mask);
480 ts2ports(src, &sel.sport, &sel.sport_mask);
481 sel.ifindex = 0;
482 sel.user = 0;
483
484 return sel;
485 }
486
487 /**
488 * convert a xfrm_selector to a src|dst traffic_selector
489 */
490 static traffic_selector_t* selector2ts(struct xfrm_selector *sel, bool src)
491 {
492 u_char *addr;
493 u_int8_t prefixlen;
494 u_int16_t port = 0;
495 host_t *host = NULL;
496
497 if (src)
498 {
499 addr = (u_char*)&sel->saddr;
500 prefixlen = sel->prefixlen_s;
501 if (sel->sport_mask)
502 {
503 port = htons(sel->sport);
504 }
505 }
506 else
507 {
508 addr = (u_char*)&sel->daddr;
509 prefixlen = sel->prefixlen_d;
510 if (sel->dport_mask)
511 {
512 port = htons(sel->dport);
513 }
514 }
515
516 /* The Linux 2.6 kernel does not set the selector's family field,
517 * so as a kludge we additionally test the prefix length.
518 */
519 if (sel->family == AF_INET || sel->prefixlen_s == 32)
520 {
521 host = host_create_from_chunk(AF_INET, chunk_create(addr, 4), 0);
522 }
523 else if (sel->family == AF_INET6 || sel->prefixlen_s == 128)
524 {
525 host = host_create_from_chunk(AF_INET6, chunk_create(addr, 16), 0);
526 }
527
528 if (host)
529 {
530 return traffic_selector_create_from_subnet(host, prefixlen,
531 sel->proto, port);
532 }
533 return NULL;
534 }
535
536 /**
537 * process a XFRM_MSG_ACQUIRE from kernel
538 */
539 static void process_acquire(private_kernel_netlink_ipsec_t *this, struct nlmsghdr *hdr)
540 {
541 u_int32_t reqid = 0;
542 int proto = 0;
543 traffic_selector_t *src_ts, *dst_ts;
544 struct xfrm_user_acquire *acquire;
545 struct rtattr *rta;
546 size_t rtasize;
547 job_t *job;
548
549 acquire = (struct xfrm_user_acquire*)NLMSG_DATA(hdr);
550 rta = XFRM_RTA(hdr, struct xfrm_user_acquire);
551 rtasize = XFRM_PAYLOAD(hdr, struct xfrm_user_acquire);
552
553 DBG2(DBG_KNL, "received a XFRM_MSG_ACQUIRE");
554
555 while (RTA_OK(rta, rtasize))
556 {
557 DBG2(DBG_KNL, " %N", xfrm_attr_type_names, rta->rta_type);
558
559 if (rta->rta_type == XFRMA_TMPL)
560 {
561 struct xfrm_user_tmpl* tmpl;
562
563 tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rta);
564 reqid = tmpl->reqid;
565 proto = tmpl->id.proto;
566 }
567 rta = RTA_NEXT(rta, rtasize);
568 }
569 switch (proto)
570 {
571 case 0:
572 case IPPROTO_ESP:
573 case IPPROTO_AH:
574 break;
575 default:
576 /* acquire for AH/ESP only, not for IPCOMP */
577 return;
578 }
579 src_ts = selector2ts(&acquire->sel, TRUE);
580 dst_ts = selector2ts(&acquire->sel, FALSE);
581 DBG1(DBG_KNL, "creating acquire job for policy %R === %R with reqid {%u}",
582 src_ts, dst_ts, reqid);
583 job = (job_t*)acquire_job_create(reqid, src_ts, dst_ts);
584 charon->processor->queue_job(charon->processor, job);
585 }
586
587 /**
588 * process a XFRM_MSG_EXPIRE from kernel
589 */
590 static void process_expire(private_kernel_netlink_ipsec_t *this, struct nlmsghdr *hdr)
591 {
592 job_t *job;
593 protocol_id_t protocol;
594 u_int32_t spi, reqid;
595 struct xfrm_user_expire *expire;
596
597 expire = (struct xfrm_user_expire*)NLMSG_DATA(hdr);
598 protocol = proto_kernel2ike(expire->state.id.proto);
599 spi = expire->state.id.spi;
600 reqid = expire->state.reqid;
601
602 DBG2(DBG_KNL, "received a XFRM_MSG_EXPIRE");
603
604 if (protocol != PROTO_ESP && protocol != PROTO_AH)
605 {
606 DBG2(DBG_KNL, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and reqid {%u} "
607 "which is not a CHILD_SA", ntohl(spi), reqid);
608 return;
609 }
610
611 DBG1(DBG_KNL, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
612 expire->hard ? "delete" : "rekey", protocol_id_names,
613 protocol, ntohl(spi), reqid);
614 if (expire->hard)
615 {
616 job = (job_t*)delete_child_sa_job_create(reqid, protocol, spi);
617 }
618 else
619 {
620 job = (job_t*)rekey_child_sa_job_create(reqid, protocol, spi);
621 }
622 charon->processor->queue_job(charon->processor, job);
623 }
624
625 /**
626 * process a XFRM_MSG_MIGRATE from kernel
627 */
628 static void process_migrate(private_kernel_netlink_ipsec_t *this, struct nlmsghdr *hdr)
629 {
630 traffic_selector_t *src_ts, *dst_ts;
631 host_t *local = NULL, *remote = NULL;
632 host_t *old_src = NULL, *old_dst = NULL;
633 host_t *new_src = NULL, *new_dst = NULL;
634 struct xfrm_userpolicy_id *policy_id;
635 struct rtattr *rta;
636 size_t rtasize;
637 u_int32_t reqid = 0;
638 policy_dir_t dir;
639 job_t *job;
640
641 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
642 rta = XFRM_RTA(hdr, struct xfrm_userpolicy_id);
643 rtasize = XFRM_PAYLOAD(hdr, struct xfrm_userpolicy_id);
644
645 DBG2(DBG_KNL, "received a XFRM_MSG_MIGRATE");
646
647 src_ts = selector2ts(&policy_id->sel, TRUE);
648 dst_ts = selector2ts(&policy_id->sel, FALSE);
649 dir = (policy_dir_t)policy_id->dir;
650
651 DBG2(DBG_KNL, " policy: %R === %R %N", src_ts, dst_ts, policy_dir_names);
652
653 while (RTA_OK(rta, rtasize))
654 {
655 DBG2(DBG_KNL, " %N", xfrm_attr_type_names, rta->rta_type);
656 if (rta->rta_type == XFRMA_KMADDRESS)
657 {
658 struct xfrm_user_kmaddress *kmaddress;
659
660 kmaddress = (struct xfrm_user_kmaddress*)RTA_DATA(rta);
661 local = xfrm2host(kmaddress->family, &kmaddress->local, 0);
662 remote = xfrm2host(kmaddress->family, &kmaddress->remote, 0);
663 DBG2(DBG_KNL, " kmaddress: %H...%H", local, remote);
664 }
665 else if (rta->rta_type == XFRMA_MIGRATE)
666 {
667 struct xfrm_user_migrate *migrate;
668 protocol_id_t proto;
669
670 migrate = (struct xfrm_user_migrate*)RTA_DATA(rta);
671 old_src = xfrm2host(migrate->old_family, &migrate->old_saddr, 0);
672 old_dst = xfrm2host(migrate->old_family, &migrate->old_daddr, 0);
673 new_src = xfrm2host(migrate->new_family, &migrate->new_saddr, 0);
674 new_dst = xfrm2host(migrate->new_family, &migrate->new_daddr, 0);
675 proto = proto_kernel2ike(migrate->proto);
676 reqid = migrate->reqid;
677 DBG2(DBG_KNL, " migrate %N %H...%H to %H...%H, reqid {%u}",
678 protocol_id_names, proto, old_src, old_dst,
679 new_src, new_dst, reqid);
680 DESTROY_IF(old_src);
681 DESTROY_IF(old_dst);
682 DESTROY_IF(new_src);
683 DESTROY_IF(new_dst);
684 }
685 rta = RTA_NEXT(rta, rtasize);
686 }
687
688 if (src_ts && dst_ts && local && remote)
689 {
690 DBG1(DBG_KNL, "creating migrate job for policy %R === %R %N with reqid {%u}",
691 src_ts, dst_ts, policy_dir_names, dir, reqid, local);
692 job = (job_t*)migrate_job_create(reqid, src_ts, dst_ts, dir,
693 local, remote);
694 charon->processor->queue_job(charon->processor, job);
695 }
696 else
697 {
698 DESTROY_IF(src_ts);
699 DESTROY_IF(dst_ts);
700 DESTROY_IF(local);
701 DESTROY_IF(remote);
702 }
703 }
704
705 /**
706 * process a XFRM_MSG_MAPPING from kernel
707 */
708 static void process_mapping(private_kernel_netlink_ipsec_t *this,
709 struct nlmsghdr *hdr)
710 {
711 job_t *job;
712 u_int32_t spi, reqid;
713 struct xfrm_user_mapping *mapping;
714 host_t *host;
715
716 mapping = (struct xfrm_user_mapping*)NLMSG_DATA(hdr);
717 spi = mapping->id.spi;
718 reqid = mapping->reqid;
719
720 DBG2(DBG_KNL, "received a XFRM_MSG_MAPPING");
721
722 if (proto_kernel2ike(mapping->id.proto) == PROTO_ESP)
723 {
724 host = xfrm2host(mapping->id.family, &mapping->new_saddr,
725 mapping->new_sport);
726 if (host)
727 {
728 DBG1(DBG_KNL, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
729 "reqid {%u} changed, queuing update job", ntohl(spi), reqid);
730 job = (job_t*)update_sa_job_create(reqid, host);
731 charon->processor->queue_job(charon->processor, job);
732 }
733 }
734 }
735
736 /**
737 * Receives events from kernel
738 */
739 static job_requeue_t receive_events(private_kernel_netlink_ipsec_t *this)
740 {
741 char response[1024];
742 struct nlmsghdr *hdr = (struct nlmsghdr*)response;
743 struct sockaddr_nl addr;
744 socklen_t addr_len = sizeof(addr);
745 int len, oldstate;
746
747 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
748 len = recvfrom(this->socket_xfrm_events, response, sizeof(response), 0,
749 (struct sockaddr*)&addr, &addr_len);
750 pthread_setcancelstate(oldstate, NULL);
751
752 if (len < 0)
753 {
754 switch (errno)
755 {
756 case EINTR:
757 /* interrupted, try again */
758 return JOB_REQUEUE_DIRECT;
759 case EAGAIN:
760 /* no data ready, select again */
761 return JOB_REQUEUE_DIRECT;
762 default:
763 DBG1(DBG_KNL, "unable to receive from xfrm event socket");
764 sleep(1);
765 return JOB_REQUEUE_FAIR;
766 }
767 }
768
769 if (addr.nl_pid != 0)
770 { /* not from kernel. not interested, try another one */
771 return JOB_REQUEUE_DIRECT;
772 }
773
774 while (NLMSG_OK(hdr, len))
775 {
776 switch (hdr->nlmsg_type)
777 {
778 case XFRM_MSG_ACQUIRE:
779 process_acquire(this, hdr);
780 break;
781 case XFRM_MSG_EXPIRE:
782 process_expire(this, hdr);
783 break;
784 case XFRM_MSG_MIGRATE:
785 process_migrate(this, hdr);
786 break;
787 case XFRM_MSG_MAPPING:
788 process_mapping(this, hdr);
789 break;
790 default:
791 break;
792 }
793 hdr = NLMSG_NEXT(hdr, len);
794 }
795 return JOB_REQUEUE_DIRECT;
796 }
797
798 /**
799 * Get an SPI for a specific protocol from the kernel.
800 */
801 static status_t get_spi_internal(private_kernel_netlink_ipsec_t *this,
802 host_t *src, host_t *dst, u_int8_t proto, u_int32_t min, u_int32_t max,
803 u_int32_t reqid, u_int32_t *spi)
804 {
805 netlink_buf_t request;
806 struct nlmsghdr *hdr, *out;
807 struct xfrm_userspi_info *userspi;
808 u_int32_t received_spi = 0;
809 size_t len;
810
811 memset(&request, 0, sizeof(request));
812
813 hdr = (struct nlmsghdr*)request;
814 hdr->nlmsg_flags = NLM_F_REQUEST;
815 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
816 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
817
818 userspi = (struct xfrm_userspi_info*)NLMSG_DATA(hdr);
819 host2xfrm(src, &userspi->info.saddr);
820 host2xfrm(dst, &userspi->info.id.daddr);
821 userspi->info.id.proto = proto;
822 userspi->info.mode = XFRM_MODE_TUNNEL;
823 userspi->info.reqid = reqid;
824 userspi->info.family = src->get_family(src);
825 userspi->min = min;
826 userspi->max = max;
827
828 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
829 {
830 hdr = out;
831 while (NLMSG_OK(hdr, len))
832 {
833 switch (hdr->nlmsg_type)
834 {
835 case XFRM_MSG_NEWSA:
836 {
837 struct xfrm_usersa_info* usersa = NLMSG_DATA(hdr);
838 received_spi = usersa->id.spi;
839 break;
840 }
841 case NLMSG_ERROR:
842 {
843 struct nlmsgerr *err = NLMSG_DATA(hdr);
844
845 DBG1(DBG_KNL, "allocating SPI failed: %s (%d)",
846 strerror(-err->error), -err->error);
847 break;
848 }
849 default:
850 hdr = NLMSG_NEXT(hdr, len);
851 continue;
852 case NLMSG_DONE:
853 break;
854 }
855 break;
856 }
857 free(out);
858 }
859
860 if (received_spi == 0)
861 {
862 return FAILED;
863 }
864
865 *spi = received_spi;
866 return SUCCESS;
867 }
868
869 /**
870 * Implementation of kernel_interface_t.get_spi.
871 */
872 static status_t get_spi(private_kernel_netlink_ipsec_t *this,
873 host_t *src, host_t *dst,
874 protocol_id_t protocol, u_int32_t reqid,
875 u_int32_t *spi)
876 {
877 DBG2(DBG_KNL, "getting SPI for reqid {%u}", reqid);
878
879 if (get_spi_internal(this, src, dst, proto_ike2kernel(protocol),
880 0xc0000000, 0xcFFFFFFF, reqid, spi) != SUCCESS)
881 {
882 DBG1(DBG_KNL, "unable to get SPI for reqid {%u}", reqid);
883 return FAILED;
884 }
885
886 DBG2(DBG_KNL, "got SPI %.8x for reqid {%u}", ntohl(*spi), reqid);
887
888 return SUCCESS;
889 }
890
891 /**
892 * Implementation of kernel_interface_t.get_cpi.
893 */
894 static status_t get_cpi(private_kernel_netlink_ipsec_t *this,
895 host_t *src, host_t *dst,
896 u_int32_t reqid, u_int16_t *cpi)
897 {
898 u_int32_t received_spi = 0;
899
900 DBG2(DBG_KNL, "getting CPI for reqid {%u}", reqid);
901
902 if (get_spi_internal(this, src, dst,
903 IPPROTO_COMP, 0x100, 0xEFFF, reqid, &received_spi) != SUCCESS)
904 {
905 DBG1(DBG_KNL, "unable to get CPI for reqid {%u}", reqid);
906 return FAILED;
907 }
908
909 *cpi = htons((u_int16_t)ntohl(received_spi));
910
911 DBG2(DBG_KNL, "got CPI %.4x for reqid {%u}", ntohs(*cpi), reqid);
912
913 return SUCCESS;
914 }
915
916 /**
917 * Implementation of kernel_interface_t.add_sa.
918 */
919 static status_t add_sa(private_kernel_netlink_ipsec_t *this,
920 host_t *src, host_t *dst, u_int32_t spi,
921 protocol_id_t protocol, u_int32_t reqid,
922 u_int64_t expire_soft, u_int64_t expire_hard,
923 u_int16_t enc_alg, chunk_t enc_key,
924 u_int16_t int_alg, chunk_t int_key,
925 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
926 bool encap, bool inbound)
927 {
928 netlink_buf_t request;
929 char *alg_name;
930 struct nlmsghdr *hdr;
931 struct xfrm_usersa_info *sa;
932 u_int16_t icv_size = 64;
933
934 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
935 * we are in the recursive call below */
936 if (ipcomp != IPCOMP_NONE && cpi != 0)
937 {
938 add_sa(this, src, dst, htonl(ntohs(cpi)), IPPROTO_COMP, reqid, 0, 0,
939 ENCR_UNDEFINED, chunk_empty, AUTH_UNDEFINED, chunk_empty,
940 mode, ipcomp, 0, FALSE, inbound);
941 ipcomp = IPCOMP_NONE;
942 }
943
944 memset(&request, 0, sizeof(request));
945
946 DBG2(DBG_KNL, "adding SAD entry with SPI %.8x and reqid {%u}",
947 ntohl(spi), reqid);
948
949 hdr = (struct nlmsghdr*)request;
950 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
951 hdr->nlmsg_type = inbound ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
952 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
953
954 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
955 host2xfrm(src, &sa->saddr);
956 host2xfrm(dst, &sa->id.daddr);
957 sa->id.spi = spi;
958 sa->id.proto = proto_ike2kernel(protocol);
959 sa->family = src->get_family(src);
960 sa->mode = mode2kernel(mode);
961 if (mode == MODE_TUNNEL)
962 {
963 sa->flags |= XFRM_STATE_AF_UNSPEC;
964 }
965 sa->replay_window = (protocol == IPPROTO_COMP) ? 0 : 32;
966 sa->reqid = reqid;
967 /* we currently do not expire SAs by volume/packet count */
968 sa->lft.soft_byte_limit = XFRM_INF;
969 sa->lft.hard_byte_limit = XFRM_INF;
970 sa->lft.soft_packet_limit = XFRM_INF;
971 sa->lft.hard_packet_limit = XFRM_INF;
972 /* we use lifetimes since added, not since used */
973 sa->lft.soft_add_expires_seconds = expire_soft;
974 sa->lft.hard_add_expires_seconds = expire_hard;
975 sa->lft.soft_use_expires_seconds = 0;
976 sa->lft.hard_use_expires_seconds = 0;
977
978 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_usersa_info);
979
980 switch (enc_alg)
981 {
982 case ENCR_UNDEFINED:
983 /* no encryption */
984 break;
985 case ENCR_AES_CCM_ICV16:
986 case ENCR_AES_GCM_ICV16:
987 icv_size += 32;
988 /* FALL */
989 case ENCR_AES_CCM_ICV12:
990 case ENCR_AES_GCM_ICV12:
991 icv_size += 32;
992 /* FALL */
993 case ENCR_AES_CCM_ICV8:
994 case ENCR_AES_GCM_ICV8:
995 {
996 rthdr->rta_type = XFRMA_ALG_AEAD;
997 alg_name = lookup_algorithm(encryption_algs, enc_alg);
998 if (alg_name == NULL)
999 {
1000 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1001 encryption_algorithm_names, enc_alg);
1002 return FAILED;
1003 }
1004 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1005 encryption_algorithm_names, enc_alg, enc_key.len * 8);
1006
1007 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo_aead) + enc_key.len);
1008 hdr->nlmsg_len += rthdr->rta_len;
1009 if (hdr->nlmsg_len > sizeof(request))
1010 {
1011 return FAILED;
1012 }
1013
1014 struct xfrm_algo_aead* algo = (struct xfrm_algo_aead*)RTA_DATA(rthdr);
1015 algo->alg_key_len = enc_key.len * 8;
1016 algo->alg_icv_len = icv_size;
1017 strcpy(algo->alg_name, alg_name);
1018 memcpy(algo->alg_key, enc_key.ptr, enc_key.len);
1019
1020 rthdr = XFRM_RTA_NEXT(rthdr);
1021 break;
1022 }
1023 default:
1024 {
1025 rthdr->rta_type = XFRMA_ALG_CRYPT;
1026 alg_name = lookup_algorithm(encryption_algs, enc_alg);
1027 if (alg_name == NULL)
1028 {
1029 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1030 encryption_algorithm_names, enc_alg);
1031 return FAILED;
1032 }
1033 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1034 encryption_algorithm_names, enc_alg, enc_key.len * 8);
1035
1036 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + enc_key.len);
1037 hdr->nlmsg_len += rthdr->rta_len;
1038 if (hdr->nlmsg_len > sizeof(request))
1039 {
1040 return FAILED;
1041 }
1042
1043 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1044 algo->alg_key_len = enc_key.len * 8;
1045 strcpy(algo->alg_name, alg_name);
1046 memcpy(algo->alg_key, enc_key.ptr, enc_key.len);
1047
1048 rthdr = XFRM_RTA_NEXT(rthdr);
1049 break;
1050 }
1051 }
1052
1053 if (int_alg != AUTH_UNDEFINED)
1054 {
1055 rthdr->rta_type = XFRMA_ALG_AUTH;
1056 alg_name = lookup_algorithm(integrity_algs, int_alg);
1057 if (alg_name == NULL)
1058 {
1059 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1060 integrity_algorithm_names, int_alg);
1061 return FAILED;
1062 }
1063 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1064 integrity_algorithm_names, int_alg, int_key.len * 8);
1065
1066 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + int_key.len);
1067 hdr->nlmsg_len += rthdr->rta_len;
1068 if (hdr->nlmsg_len > sizeof(request))
1069 {
1070 return FAILED;
1071 }
1072
1073 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1074 algo->alg_key_len = int_key.len * 8;
1075 strcpy(algo->alg_name, alg_name);
1076 memcpy(algo->alg_key, int_key.ptr, int_key.len);
1077
1078 rthdr = XFRM_RTA_NEXT(rthdr);
1079 }
1080
1081 if (ipcomp != IPCOMP_NONE)
1082 {
1083 rthdr->rta_type = XFRMA_ALG_COMP;
1084 alg_name = lookup_algorithm(compression_algs, ipcomp);
1085 if (alg_name == NULL)
1086 {
1087 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1088 ipcomp_transform_names, ipcomp);
1089 return FAILED;
1090 }
1091 DBG2(DBG_KNL, " using compression algorithm %N",
1092 ipcomp_transform_names, ipcomp);
1093
1094 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo));
1095 hdr->nlmsg_len += rthdr->rta_len;
1096 if (hdr->nlmsg_len > sizeof(request))
1097 {
1098 return FAILED;
1099 }
1100
1101 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1102 algo->alg_key_len = 0;
1103 strcpy(algo->alg_name, alg_name);
1104
1105 rthdr = XFRM_RTA_NEXT(rthdr);
1106 }
1107
1108 if (encap)
1109 {
1110 rthdr->rta_type = XFRMA_ENCAP;
1111 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1112
1113 hdr->nlmsg_len += rthdr->rta_len;
1114 if (hdr->nlmsg_len > sizeof(request))
1115 {
1116 return FAILED;
1117 }
1118
1119 struct xfrm_encap_tmpl* tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
1120 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1121 tmpl->encap_sport = htons(src->get_port(src));
1122 tmpl->encap_dport = htons(dst->get_port(dst));
1123 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1124 /* encap_oa could probably be derived from the
1125 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1126 * pluto does the same as we do here but it uses encap_oa in the
1127 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1128 * the kernel ignores it anyway
1129 * -> does that mean that NAT-T encap doesn't work in transport mode?
1130 * No. The reason the kernel ignores NAT-OA is that it recomputes
1131 * (or, rather, just ignores) the checksum. If packets pass
1132 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1133 rthdr = XFRM_RTA_NEXT(rthdr);
1134 }
1135
1136 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1137 {
1138 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x", ntohl(spi));
1139 return FAILED;
1140 }
1141 return SUCCESS;
1142 }
1143
1144 /**
1145 * Get the replay state (i.e. sequence numbers) of an SA.
1146 */
1147 static status_t get_replay_state(private_kernel_netlink_ipsec_t *this,
1148 u_int32_t spi, protocol_id_t protocol, host_t *dst,
1149 struct xfrm_replay_state *replay)
1150 {
1151 netlink_buf_t request;
1152 struct nlmsghdr *hdr, *out = NULL;
1153 struct xfrm_aevent_id *out_aevent = NULL, *aevent_id;
1154 size_t len;
1155 struct rtattr *rta;
1156 size_t rtasize;
1157
1158 memset(&request, 0, sizeof(request));
1159
1160 DBG2(DBG_KNL, "querying replay state from SAD entry with SPI %.8x", ntohl(spi));
1161
1162 hdr = (struct nlmsghdr*)request;
1163 hdr->nlmsg_flags = NLM_F_REQUEST;
1164 hdr->nlmsg_type = XFRM_MSG_GETAE;
1165 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1166
1167 aevent_id = (struct xfrm_aevent_id*)NLMSG_DATA(hdr);
1168 aevent_id->flags = XFRM_AE_RVAL;
1169
1170 host2xfrm(dst, &aevent_id->sa_id.daddr);
1171 aevent_id->sa_id.spi = spi;
1172 aevent_id->sa_id.proto = proto_ike2kernel(protocol);
1173 aevent_id->sa_id.family = dst->get_family(dst);
1174
1175 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1176 {
1177 hdr = out;
1178 while (NLMSG_OK(hdr, len))
1179 {
1180 switch (hdr->nlmsg_type)
1181 {
1182 case XFRM_MSG_NEWAE:
1183 {
1184 out_aevent = NLMSG_DATA(hdr);
1185 break;
1186 }
1187 case NLMSG_ERROR:
1188 {
1189 struct nlmsgerr *err = NLMSG_DATA(hdr);
1190 DBG1(DBG_KNL, "querying replay state from SAD entry failed: %s (%d)",
1191 strerror(-err->error), -err->error);
1192 break;
1193 }
1194 default:
1195 hdr = NLMSG_NEXT(hdr, len);
1196 continue;
1197 case NLMSG_DONE:
1198 break;
1199 }
1200 break;
1201 }
1202 }
1203
1204 if (out_aevent == NULL)
1205 {
1206 DBG1(DBG_KNL, "unable to query replay state from SAD entry with SPI %.8x",
1207 ntohl(spi));
1208 free(out);
1209 return FAILED;
1210 }
1211
1212 rta = XFRM_RTA(out, struct xfrm_aevent_id);
1213 rtasize = XFRM_PAYLOAD(out, struct xfrm_aevent_id);
1214 while(RTA_OK(rta, rtasize))
1215 {
1216 if (rta->rta_type == XFRMA_REPLAY_VAL &&
1217 RTA_PAYLOAD(rta) == sizeof(struct xfrm_replay_state))
1218 {
1219 memcpy(replay, RTA_DATA(rta), RTA_PAYLOAD(rta));
1220 free(out);
1221 return SUCCESS;
1222 }
1223 rta = RTA_NEXT(rta, rtasize);
1224 }
1225
1226 DBG1(DBG_KNL, "unable to query replay state from SAD entry with SPI %.8x",
1227 ntohl(spi));
1228 free(out);
1229 return FAILED;
1230 }
1231
1232 /**
1233 * Implementation of kernel_interface_t.del_sa.
1234 */
1235 static status_t del_sa(private_kernel_netlink_ipsec_t *this, host_t *src,
1236 host_t *dst, u_int32_t spi, protocol_id_t protocol,
1237 u_int16_t cpi)
1238 {
1239 netlink_buf_t request;
1240 struct nlmsghdr *hdr;
1241 struct xfrm_usersa_id *sa_id;
1242
1243 /* if IPComp was used, we first delete the additional IPComp SA */
1244 if (cpi)
1245 {
1246 del_sa(this, src, dst, htonl(ntohs(cpi)), IPPROTO_COMP, 0);
1247 }
1248
1249 memset(&request, 0, sizeof(request));
1250
1251 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x", ntohl(spi));
1252
1253 hdr = (struct nlmsghdr*)request;
1254 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1255 hdr->nlmsg_type = XFRM_MSG_DELSA;
1256 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1257
1258 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1259 host2xfrm(dst, &sa_id->daddr);
1260 sa_id->spi = spi;
1261 sa_id->proto = proto_ike2kernel(protocol);
1262 sa_id->family = dst->get_family(dst);
1263
1264 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1265 {
1266 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x", ntohl(spi));
1267 return FAILED;
1268 }
1269 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x", ntohl(spi));
1270 return SUCCESS;
1271 }
1272
1273 /**
1274 * Implementation of kernel_interface_t.update_sa.
1275 */
1276 static status_t update_sa(private_kernel_netlink_ipsec_t *this,
1277 u_int32_t spi, protocol_id_t protocol, u_int16_t cpi,
1278 host_t *src, host_t *dst,
1279 host_t *new_src, host_t *new_dst,
1280 bool old_encap, bool new_encap)
1281 {
1282 netlink_buf_t request;
1283 u_char *pos;
1284 struct nlmsghdr *hdr, *out = NULL;
1285 struct xfrm_usersa_id *sa_id;
1286 struct xfrm_usersa_info *out_sa = NULL, *sa;
1287 size_t len;
1288 struct rtattr *rta;
1289 size_t rtasize;
1290 struct xfrm_encap_tmpl* tmpl = NULL;
1291 bool got_replay_state = FALSE;
1292 struct xfrm_replay_state replay;
1293
1294 /* if IPComp is used, we first update the IPComp SA */
1295 if (cpi)
1296 {
1297 update_sa(this, htonl(ntohs(cpi)), IPPROTO_COMP, 0,
1298 src, dst, new_src, new_dst, FALSE, FALSE);
1299 }
1300
1301 memset(&request, 0, sizeof(request));
1302
1303 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x for update", ntohl(spi));
1304
1305 /* query the existing SA first */
1306 hdr = (struct nlmsghdr*)request;
1307 hdr->nlmsg_flags = NLM_F_REQUEST;
1308 hdr->nlmsg_type = XFRM_MSG_GETSA;
1309 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1310
1311 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1312 host2xfrm(dst, &sa_id->daddr);
1313 sa_id->spi = spi;
1314 sa_id->proto = proto_ike2kernel(protocol);
1315 sa_id->family = dst->get_family(dst);
1316
1317 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1318 {
1319 hdr = out;
1320 while (NLMSG_OK(hdr, len))
1321 {
1322 switch (hdr->nlmsg_type)
1323 {
1324 case XFRM_MSG_NEWSA:
1325 {
1326 out_sa = NLMSG_DATA(hdr);
1327 break;
1328 }
1329 case NLMSG_ERROR:
1330 {
1331 struct nlmsgerr *err = NLMSG_DATA(hdr);
1332 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1333 strerror(-err->error), -err->error);
1334 break;
1335 }
1336 default:
1337 hdr = NLMSG_NEXT(hdr, len);
1338 continue;
1339 case NLMSG_DONE:
1340 break;
1341 }
1342 break;
1343 }
1344 }
1345 if (out_sa == NULL)
1346 {
1347 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1348 free(out);
1349 return FAILED;
1350 }
1351
1352 /* try to get the replay state */
1353 if (get_replay_state(this, spi, protocol, dst, &replay) == SUCCESS)
1354 {
1355 got_replay_state = TRUE;
1356 }
1357
1358 /* delete the old SA (without affecting the IPComp SA) */
1359 if (del_sa(this, src, dst, spi, protocol, 0) != SUCCESS)
1360 {
1361 DBG1(DBG_KNL, "unable to delete old SAD entry with SPI %.8x", ntohl(spi));
1362 free(out);
1363 return FAILED;
1364 }
1365
1366 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1367 ntohl(spi), src, dst, new_src, new_dst);
1368 /* copy over the SA from out to request */
1369 hdr = (struct nlmsghdr*)request;
1370 memcpy(hdr, out, min(out->nlmsg_len, sizeof(request)));
1371 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1372 hdr->nlmsg_type = XFRM_MSG_NEWSA;
1373 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1374 sa = NLMSG_DATA(hdr);
1375 sa->family = new_dst->get_family(new_dst);
1376
1377 if (!src->ip_equals(src, new_src))
1378 {
1379 host2xfrm(new_src, &sa->saddr);
1380 }
1381 if (!dst->ip_equals(dst, new_dst))
1382 {
1383 host2xfrm(new_dst, &sa->id.daddr);
1384 }
1385
1386 rta = XFRM_RTA(out, struct xfrm_usersa_info);
1387 rtasize = XFRM_PAYLOAD(out, struct xfrm_usersa_info);
1388 pos = (u_char*)XFRM_RTA(hdr, struct xfrm_usersa_info);
1389 while(RTA_OK(rta, rtasize))
1390 {
1391 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1392 if (rta->rta_type != XFRMA_ENCAP || new_encap)
1393 {
1394 if (rta->rta_type == XFRMA_ENCAP)
1395 { /* update encap tmpl */
1396 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1397 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1398 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1399 }
1400 memcpy(pos, rta, rta->rta_len);
1401 pos += RTA_ALIGN(rta->rta_len);
1402 hdr->nlmsg_len += RTA_ALIGN(rta->rta_len);
1403 }
1404 rta = RTA_NEXT(rta, rtasize);
1405 }
1406
1407 rta = (struct rtattr*)pos;
1408 if (tmpl == NULL && new_encap)
1409 { /* add tmpl if we are enabling it */
1410 rta->rta_type = XFRMA_ENCAP;
1411 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1412
1413 hdr->nlmsg_len += rta->rta_len;
1414 if (hdr->nlmsg_len > sizeof(request))
1415 {
1416 return FAILED;
1417 }
1418
1419 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1420 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1421 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1422 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1423 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1424
1425 rta = XFRM_RTA_NEXT(rta);
1426 }
1427
1428 if (got_replay_state)
1429 { /* copy the replay data if available */
1430 rta->rta_type = XFRMA_REPLAY_VAL;
1431 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_replay_state));
1432
1433 hdr->nlmsg_len += rta->rta_len;
1434 if (hdr->nlmsg_len > sizeof(request))
1435 {
1436 return FAILED;
1437 }
1438 memcpy(RTA_DATA(rta), &replay, sizeof(replay));
1439
1440 rta = XFRM_RTA_NEXT(rta);
1441 }
1442
1443 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1444 {
1445 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1446 free(out);
1447 return FAILED;
1448 }
1449 free(out);
1450
1451 return SUCCESS;
1452 }
1453
1454 /**
1455 * Implementation of kernel_interface_t.add_policy.
1456 */
1457 static status_t add_policy(private_kernel_netlink_ipsec_t *this,
1458 host_t *src, host_t *dst,
1459 traffic_selector_t *src_ts,
1460 traffic_selector_t *dst_ts,
1461 policy_dir_t direction, u_int32_t spi,
1462 protocol_id_t protocol, u_int32_t reqid,
1463 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
1464 bool routed)
1465 {
1466 policy_entry_t *current, *policy;
1467 bool found = FALSE;
1468 netlink_buf_t request;
1469 struct xfrm_userpolicy_info *policy_info;
1470 struct nlmsghdr *hdr;
1471
1472 /* create a policy */
1473 policy = malloc_thing(policy_entry_t);
1474 memset(policy, 0, sizeof(policy_entry_t));
1475 policy->sel = ts2selector(src_ts, dst_ts);
1476 policy->direction = direction;
1477
1478 /* find the policy, which matches EXACTLY */
1479 this->mutex->lock(this->mutex);
1480 current = this->policies->get(this->policies, policy);
1481 if (current)
1482 {
1483 /* use existing policy */
1484 current->refcount++;
1485 DBG2(DBG_KNL, "policy %R === %R %N already exists, increasing "
1486 "refcount", src_ts, dst_ts,
1487 policy_dir_names, direction);
1488 free(policy);
1489 policy = current;
1490 found = TRUE;
1491 }
1492 else
1493 { /* apply the new one, if we have no such policy */
1494 this->policies->put(this->policies, policy, policy);
1495 policy->refcount = 1;
1496 }
1497
1498 DBG2(DBG_KNL, "adding policy %R === %R %N", src_ts, dst_ts,
1499 policy_dir_names, direction);
1500
1501 memset(&request, 0, sizeof(request));
1502 hdr = (struct nlmsghdr*)request;
1503 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1504 hdr->nlmsg_type = found ? XFRM_MSG_UPDPOLICY : XFRM_MSG_NEWPOLICY;
1505 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
1506
1507 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1508 policy_info->sel = policy->sel;
1509 policy_info->dir = policy->direction;
1510 /* calculate priority based on source selector size, small size = high prio */
1511 policy_info->priority = routed ? PRIO_LOW : PRIO_HIGH;
1512 policy_info->priority -= policy->sel.prefixlen_s * 10;
1513 policy_info->priority -= policy->sel.proto ? 2 : 0;
1514 policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
1515 policy_info->action = XFRM_POLICY_ALLOW;
1516 policy_info->share = XFRM_SHARE_ANY;
1517 this->mutex->unlock(this->mutex);
1518
1519 /* policies don't expire */
1520 policy_info->lft.soft_byte_limit = XFRM_INF;
1521 policy_info->lft.soft_packet_limit = XFRM_INF;
1522 policy_info->lft.hard_byte_limit = XFRM_INF;
1523 policy_info->lft.hard_packet_limit = XFRM_INF;
1524 policy_info->lft.soft_add_expires_seconds = 0;
1525 policy_info->lft.hard_add_expires_seconds = 0;
1526 policy_info->lft.soft_use_expires_seconds = 0;
1527 policy_info->lft.hard_use_expires_seconds = 0;
1528
1529 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
1530 rthdr->rta_type = XFRMA_TMPL;
1531 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1532
1533 hdr->nlmsg_len += rthdr->rta_len;
1534 if (hdr->nlmsg_len > sizeof(request))
1535 {
1536 return FAILED;
1537 }
1538
1539 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
1540
1541 if (ipcomp != IPCOMP_NONE)
1542 {
1543 tmpl->reqid = reqid;
1544 tmpl->id.proto = IPPROTO_COMP;
1545 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1546 tmpl->mode = mode2kernel(mode);
1547 tmpl->optional = direction != POLICY_OUT;
1548 tmpl->family = src->get_family(src);
1549
1550 host2xfrm(src, &tmpl->saddr);
1551 host2xfrm(dst, &tmpl->id.daddr);
1552
1553 /* add an additional xfrm_user_tmpl */
1554 rthdr->rta_len += RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1555 hdr->nlmsg_len += RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1556 if (hdr->nlmsg_len > sizeof(request))
1557 {
1558 return FAILED;
1559 }
1560
1561 tmpl++;
1562 }
1563
1564 tmpl->reqid = reqid;
1565 tmpl->id.proto = proto_ike2kernel(protocol);
1566 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1567 tmpl->mode = mode2kernel(mode);
1568 tmpl->family = src->get_family(src);
1569
1570 host2xfrm(src, &tmpl->saddr);
1571 host2xfrm(dst, &tmpl->id.daddr);
1572
1573 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1574 {
1575 DBG1(DBG_KNL, "unable to add policy %R === %R %N", src_ts, dst_ts,
1576 policy_dir_names, direction);
1577 return FAILED;
1578 }
1579
1580 /* install a route, if:
1581 * - we are NOT updating a policy
1582 * - this is a forward policy (to just get one for each child)
1583 * - we are in tunnel mode
1584 * - we are not using IPv6 (does not work correctly yet!)
1585 * - routing is not disabled via strongswan.conf
1586 */
1587 if (policy->route == NULL && direction == POLICY_FWD &&
1588 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6 &&
1589 this->install_routes)
1590 {
1591 route_entry_t *route = malloc_thing(route_entry_t);
1592
1593 if (charon->kernel_interface->get_address_by_ts(charon->kernel_interface,
1594 dst_ts, &route->src_ip) == SUCCESS)
1595 {
1596 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1597 route->gateway = charon->kernel_interface->get_nexthop(
1598 charon->kernel_interface, src);
1599 route->if_name = charon->kernel_interface->get_interface(
1600 charon->kernel_interface, dst);
1601 route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
1602 memcpy(route->dst_net.ptr, &policy->sel.saddr, route->dst_net.len);
1603 route->prefixlen = policy->sel.prefixlen_s;
1604
1605 if (route->if_name)
1606 {
1607 switch (charon->kernel_interface->add_route(
1608 charon->kernel_interface, route->dst_net,
1609 route->prefixlen, route->gateway,
1610 route->src_ip, route->if_name))
1611 {
1612 default:
1613 DBG1(DBG_KNL, "unable to install source route for %H",
1614 route->src_ip);
1615 /* FALL */
1616 case ALREADY_DONE:
1617 /* route exists, do not uninstall */
1618 route_entry_destroy(route);
1619 break;
1620 case SUCCESS:
1621 /* cache the installed route */
1622 policy->route = route;
1623 break;
1624 }
1625 }
1626 else
1627 {
1628 route_entry_destroy(route);
1629 }
1630 }
1631 else
1632 {
1633 free(route);
1634 }
1635 }
1636 return SUCCESS;
1637 }
1638
1639 /**
1640 * Implementation of kernel_interface_t.query_policy.
1641 */
1642 static status_t query_policy(private_kernel_netlink_ipsec_t *this,
1643 traffic_selector_t *src_ts,
1644 traffic_selector_t *dst_ts,
1645 policy_dir_t direction, u_int32_t *use_time)
1646 {
1647 netlink_buf_t request;
1648 struct nlmsghdr *out = NULL, *hdr;
1649 struct xfrm_userpolicy_id *policy_id;
1650 struct xfrm_userpolicy_info *policy = NULL;
1651 size_t len;
1652
1653 memset(&request, 0, sizeof(request));
1654
1655 DBG2(DBG_KNL, "querying policy %R === %R %N", src_ts, dst_ts,
1656 policy_dir_names, direction);
1657
1658 hdr = (struct nlmsghdr*)request;
1659 hdr->nlmsg_flags = NLM_F_REQUEST;
1660 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
1661 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1662
1663 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1664 policy_id->sel = ts2selector(src_ts, dst_ts);
1665 policy_id->dir = direction;
1666
1667 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1668 {
1669 hdr = out;
1670 while (NLMSG_OK(hdr, len))
1671 {
1672 switch (hdr->nlmsg_type)
1673 {
1674 case XFRM_MSG_NEWPOLICY:
1675 {
1676 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1677 break;
1678 }
1679 case NLMSG_ERROR:
1680 {
1681 struct nlmsgerr *err = NLMSG_DATA(hdr);
1682 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
1683 strerror(-err->error), -err->error);
1684 break;
1685 }
1686 default:
1687 hdr = NLMSG_NEXT(hdr, len);
1688 continue;
1689 case NLMSG_DONE:
1690 break;
1691 }
1692 break;
1693 }
1694 }
1695
1696 if (policy == NULL)
1697 {
1698 DBG2(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
1699 policy_dir_names, direction);
1700 free(out);
1701 return FAILED;
1702 }
1703 *use_time = (time_t)policy->curlft.use_time;
1704
1705 free(out);
1706 return SUCCESS;
1707 }
1708
1709 /**
1710 * Implementation of kernel_interface_t.del_policy.
1711 */
1712 static status_t del_policy(private_kernel_netlink_ipsec_t *this,
1713 traffic_selector_t *src_ts,
1714 traffic_selector_t *dst_ts,
1715 policy_dir_t direction, bool unrouted)
1716 {
1717 policy_entry_t *current, policy, *to_delete = NULL;
1718 route_entry_t *route;
1719 netlink_buf_t request;
1720 struct nlmsghdr *hdr;
1721 struct xfrm_userpolicy_id *policy_id;
1722
1723 DBG2(DBG_KNL, "deleting policy %R === %R %N", src_ts, dst_ts,
1724 policy_dir_names, direction);
1725
1726 /* create a policy */
1727 memset(&policy, 0, sizeof(policy_entry_t));
1728 policy.sel = ts2selector(src_ts, dst_ts);
1729 policy.direction = direction;
1730
1731 /* find the policy */
1732 this->mutex->lock(this->mutex);
1733 current = this->policies->get(this->policies, &policy);
1734 if (current)
1735 {
1736 to_delete = current;
1737 if (--to_delete->refcount > 0)
1738 {
1739 /* is used by more SAs, keep in kernel */
1740 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
1741 this->mutex->unlock(this->mutex);
1742 return SUCCESS;
1743 }
1744 /* remove if last reference */
1745 this->policies->remove(this->policies, to_delete);
1746 }
1747 this->mutex->unlock(this->mutex);
1748 if (!to_delete)
1749 {
1750 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
1751 dst_ts, policy_dir_names, direction);
1752 return NOT_FOUND;
1753 }
1754
1755 memset(&request, 0, sizeof(request));
1756
1757 hdr = (struct nlmsghdr*)request;
1758 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1759 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
1760 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1761
1762 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1763 policy_id->sel = to_delete->sel;
1764 policy_id->dir = direction;
1765
1766 route = to_delete->route;
1767 free(to_delete);
1768
1769 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1770 {
1771 DBG1(DBG_KNL, "unable to delete policy %R === %R %N", src_ts, dst_ts,
1772 policy_dir_names, direction);
1773 return FAILED;
1774 }
1775
1776 if (route)
1777 {
1778 if (charon->kernel_interface->del_route(charon->kernel_interface,
1779 route->dst_net, route->prefixlen, route->gateway,
1780 route->src_ip, route->if_name) != SUCCESS)
1781 {
1782 DBG1(DBG_KNL, "error uninstalling route installed with "
1783 "policy %R === %R %N", src_ts, dst_ts,
1784 policy_dir_names, direction);
1785 }
1786 route_entry_destroy(route);
1787 }
1788 return SUCCESS;
1789 }
1790
1791 /**
1792 * Implementation of kernel_interface_t.destroy.
1793 */
1794 static void destroy(private_kernel_netlink_ipsec_t *this)
1795 {
1796 enumerator_t *enumerator;
1797 policy_entry_t *policy;
1798
1799 this->job->cancel(this->job);
1800 close(this->socket_xfrm_events);
1801 this->socket_xfrm->destroy(this->socket_xfrm);
1802 enumerator = this->policies->create_enumerator(this->policies);
1803 while (enumerator->enumerate(enumerator, &policy, &policy))
1804 {
1805 free(policy);
1806 }
1807 enumerator->destroy(enumerator);
1808 this->policies->destroy(this->policies);
1809 this->mutex->destroy(this->mutex);
1810 free(this);
1811 }
1812
1813 /**
1814 * Add bypass policies for IKE on the sockets used by charon
1815 */
1816 static bool add_bypass_policies()
1817 {
1818 int fd, family, port;
1819 enumerator_t *sockets;
1820 bool status = TRUE;
1821
1822 /* we open an AF_KEY socket to autoload the af_key module. Otherwise
1823 * setsockopt(IPSEC_POLICY) won't work. */
1824 fd = socket(AF_KEY, SOCK_RAW, PF_KEY_V2);
1825 if (fd == 0)
1826 {
1827 DBG1(DBG_KNL, "could not open AF_KEY socket");
1828 return FALSE;
1829 }
1830 close(fd);
1831
1832 sockets = charon->socket->create_enumerator(charon->socket);
1833 while (sockets->enumerate(sockets, &fd, &family, &port))
1834 {
1835 struct sadb_x_policy policy;
1836 u_int sol, ipsec_policy;
1837
1838 switch (family)
1839 {
1840 case AF_INET:
1841 sol = SOL_IP;
1842 ipsec_policy = IP_IPSEC_POLICY;
1843 break;
1844 case AF_INET6:
1845 sol = SOL_IPV6;
1846 ipsec_policy = IPV6_IPSEC_POLICY;
1847 break;
1848 default:
1849 continue;
1850 }
1851
1852 memset(&policy, 0, sizeof(policy));
1853 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);
1854 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1855 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;
1856
1857 policy.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;
1858 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
1859 {
1860 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
1861 strerror(errno));
1862 status = FALSE;
1863 break;
1864 }
1865 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;
1866 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
1867 {
1868 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
1869 strerror(errno));
1870 status = FALSE;
1871 break;
1872 }
1873 }
1874 sockets->destroy(sockets);
1875 return status;
1876 }
1877
1878 /*
1879 * Described in header.
1880 */
1881 kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
1882 {
1883 private_kernel_netlink_ipsec_t *this = malloc_thing(private_kernel_netlink_ipsec_t);
1884 struct sockaddr_nl addr;
1885
1886 /* public functions */
1887 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;
1888 this->public.interface.get_cpi = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,u_int32_t,u_int16_t*))get_cpi;
1889 this->public.interface.add_sa = (status_t(*)(kernel_ipsec_t *,host_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t,u_int64_t,u_int64_t,u_int16_t,chunk_t,u_int16_t,chunk_t,ipsec_mode_t,u_int16_t,u_int16_t,bool,bool))add_sa;
1890 this->public.interface.update_sa = (status_t(*)(kernel_ipsec_t*,u_int32_t,protocol_id_t,u_int16_t,host_t*,host_t*,host_t*,host_t*,bool,bool))update_sa;
1891 this->public.interface.del_sa = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,u_int32_t,protocol_id_t,u_int16_t))del_sa;
1892 this->public.interface.add_policy = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t,protocol_id_t,u_int32_t,ipsec_mode_t,u_int16_t,u_int16_t,bool))add_policy;
1893 this->public.interface.query_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
1894 this->public.interface.del_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,bool))del_policy;
1895 this->public.interface.destroy = (void(*)(kernel_ipsec_t*)) destroy;
1896
1897 /* private members */
1898 this->policies = hashtable_create((hashtable_hash_t)policy_hash,
1899 (hashtable_equals_t)policy_equals, 32);
1900 this->mutex = mutex_create(MUTEX_DEFAULT);
1901 this->install_routes = lib->settings->get_bool(lib->settings,
1902 "charon.install_routes", TRUE);
1903
1904 /* add bypass policies on the sockets used by charon */
1905 if (!add_bypass_policies())
1906 {
1907 charon->kill(charon, "unable to add bypass policies on sockets");
1908 }
1909
1910 this->socket_xfrm = netlink_socket_create(NETLINK_XFRM);
1911
1912 memset(&addr, 0, sizeof(addr));
1913 addr.nl_family = AF_NETLINK;
1914
1915 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
1916 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1917 if (this->socket_xfrm_events <= 0)
1918 {
1919 charon->kill(charon, "unable to create XFRM event socket");
1920 }
1921 addr.nl_groups = XFRMNLGRP(ACQUIRE) | XFRMNLGRP(EXPIRE) |
1922 XFRMNLGRP(MIGRATE) | XFRMNLGRP(MAPPING);
1923 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
1924 {
1925 charon->kill(charon, "unable to bind XFRM event socket");
1926 }
1927
1928 this->job = callback_job_create((callback_job_cb_t)receive_events,
1929 this, NULL, NULL);
1930 charon->processor->queue_job(charon->processor, (job_t*)this->job);
1931
1932 return &this->public;
1933 }