display transmitted bytes per SA
[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 struct xfrm_algo_aead *algo;
997
998 alg_name = lookup_algorithm(encryption_algs, enc_alg);
999 if (alg_name == NULL)
1000 {
1001 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1002 encryption_algorithm_names, enc_alg);
1003 return FAILED;
1004 }
1005 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1006 encryption_algorithm_names, enc_alg, enc_key.len * 8);
1007
1008 rthdr->rta_type = XFRMA_ALG_AEAD;
1009 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo_aead) + enc_key.len);
1010 hdr->nlmsg_len += rthdr->rta_len;
1011 if (hdr->nlmsg_len > sizeof(request))
1012 {
1013 return FAILED;
1014 }
1015
1016 algo = (struct xfrm_algo_aead*)RTA_DATA(rthdr);
1017 algo->alg_key_len = enc_key.len * 8;
1018 algo->alg_icv_len = icv_size;
1019 strcpy(algo->alg_name, alg_name);
1020 memcpy(algo->alg_key, enc_key.ptr, enc_key.len);
1021
1022 rthdr = XFRM_RTA_NEXT(rthdr);
1023 break;
1024 }
1025 default:
1026 {
1027 struct xfrm_algo *algo;
1028
1029 alg_name = lookup_algorithm(encryption_algs, enc_alg);
1030 if (alg_name == NULL)
1031 {
1032 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1033 encryption_algorithm_names, enc_alg);
1034 return FAILED;
1035 }
1036 DBG2(DBG_KNL, " using encryption algorithm %N with key size %d",
1037 encryption_algorithm_names, enc_alg, enc_key.len * 8);
1038
1039 rthdr->rta_type = XFRMA_ALG_CRYPT;
1040 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + enc_key.len);
1041 hdr->nlmsg_len += rthdr->rta_len;
1042 if (hdr->nlmsg_len > sizeof(request))
1043 {
1044 return FAILED;
1045 }
1046
1047 algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1048 algo->alg_key_len = enc_key.len * 8;
1049 strcpy(algo->alg_name, alg_name);
1050 memcpy(algo->alg_key, enc_key.ptr, enc_key.len);
1051
1052 rthdr = XFRM_RTA_NEXT(rthdr);
1053 }
1054 }
1055
1056 if (int_alg != AUTH_UNDEFINED)
1057 {
1058 rthdr->rta_type = XFRMA_ALG_AUTH;
1059 alg_name = lookup_algorithm(integrity_algs, int_alg);
1060 if (alg_name == NULL)
1061 {
1062 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1063 integrity_algorithm_names, int_alg);
1064 return FAILED;
1065 }
1066 DBG2(DBG_KNL, " using integrity algorithm %N with key size %d",
1067 integrity_algorithm_names, int_alg, int_key.len * 8);
1068
1069 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + int_key.len);
1070 hdr->nlmsg_len += rthdr->rta_len;
1071 if (hdr->nlmsg_len > sizeof(request))
1072 {
1073 return FAILED;
1074 }
1075
1076 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1077 algo->alg_key_len = int_key.len * 8;
1078 strcpy(algo->alg_name, alg_name);
1079 memcpy(algo->alg_key, int_key.ptr, int_key.len);
1080
1081 rthdr = XFRM_RTA_NEXT(rthdr);
1082 }
1083
1084 if (ipcomp != IPCOMP_NONE)
1085 {
1086 rthdr->rta_type = XFRMA_ALG_COMP;
1087 alg_name = lookup_algorithm(compression_algs, ipcomp);
1088 if (alg_name == NULL)
1089 {
1090 DBG1(DBG_KNL, "algorithm %N not supported by kernel!",
1091 ipcomp_transform_names, ipcomp);
1092 return FAILED;
1093 }
1094 DBG2(DBG_KNL, " using compression algorithm %N",
1095 ipcomp_transform_names, ipcomp);
1096
1097 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo));
1098 hdr->nlmsg_len += rthdr->rta_len;
1099 if (hdr->nlmsg_len > sizeof(request))
1100 {
1101 return FAILED;
1102 }
1103
1104 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
1105 algo->alg_key_len = 0;
1106 strcpy(algo->alg_name, alg_name);
1107
1108 rthdr = XFRM_RTA_NEXT(rthdr);
1109 }
1110
1111 if (encap)
1112 {
1113 rthdr->rta_type = XFRMA_ENCAP;
1114 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1115
1116 hdr->nlmsg_len += rthdr->rta_len;
1117 if (hdr->nlmsg_len > sizeof(request))
1118 {
1119 return FAILED;
1120 }
1121
1122 struct xfrm_encap_tmpl* tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
1123 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1124 tmpl->encap_sport = htons(src->get_port(src));
1125 tmpl->encap_dport = htons(dst->get_port(dst));
1126 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1127 /* encap_oa could probably be derived from the
1128 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1129 * pluto does the same as we do here but it uses encap_oa in the
1130 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1131 * the kernel ignores it anyway
1132 * -> does that mean that NAT-T encap doesn't work in transport mode?
1133 * No. The reason the kernel ignores NAT-OA is that it recomputes
1134 * (or, rather, just ignores) the checksum. If packets pass
1135 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1136 rthdr = XFRM_RTA_NEXT(rthdr);
1137 }
1138
1139 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1140 {
1141 DBG1(DBG_KNL, "unable to add SAD entry with SPI %.8x", ntohl(spi));
1142 return FAILED;
1143 }
1144 return SUCCESS;
1145 }
1146
1147 /**
1148 * Get the replay state (i.e. sequence numbers) of an SA.
1149 */
1150 static status_t get_replay_state(private_kernel_netlink_ipsec_t *this,
1151 u_int32_t spi, protocol_id_t protocol, host_t *dst,
1152 struct xfrm_replay_state *replay)
1153 {
1154 netlink_buf_t request;
1155 struct nlmsghdr *hdr, *out = NULL;
1156 struct xfrm_aevent_id *out_aevent = NULL, *aevent_id;
1157 size_t len;
1158 struct rtattr *rta;
1159 size_t rtasize;
1160
1161 memset(&request, 0, sizeof(request));
1162
1163 DBG2(DBG_KNL, "querying replay state from SAD entry with SPI %.8x", ntohl(spi));
1164
1165 hdr = (struct nlmsghdr*)request;
1166 hdr->nlmsg_flags = NLM_F_REQUEST;
1167 hdr->nlmsg_type = XFRM_MSG_GETAE;
1168 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1169
1170 aevent_id = (struct xfrm_aevent_id*)NLMSG_DATA(hdr);
1171 aevent_id->flags = XFRM_AE_RVAL;
1172
1173 host2xfrm(dst, &aevent_id->sa_id.daddr);
1174 aevent_id->sa_id.spi = spi;
1175 aevent_id->sa_id.proto = proto_ike2kernel(protocol);
1176 aevent_id->sa_id.family = dst->get_family(dst);
1177
1178 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1179 {
1180 hdr = out;
1181 while (NLMSG_OK(hdr, len))
1182 {
1183 switch (hdr->nlmsg_type)
1184 {
1185 case XFRM_MSG_NEWAE:
1186 {
1187 out_aevent = NLMSG_DATA(hdr);
1188 break;
1189 }
1190 case NLMSG_ERROR:
1191 {
1192 struct nlmsgerr *err = NLMSG_DATA(hdr);
1193 DBG1(DBG_KNL, "querying replay state from SAD entry failed: %s (%d)",
1194 strerror(-err->error), -err->error);
1195 break;
1196 }
1197 default:
1198 hdr = NLMSG_NEXT(hdr, len);
1199 continue;
1200 case NLMSG_DONE:
1201 break;
1202 }
1203 break;
1204 }
1205 }
1206
1207 if (out_aevent == NULL)
1208 {
1209 DBG1(DBG_KNL, "unable to query replay state from SAD entry with SPI %.8x",
1210 ntohl(spi));
1211 free(out);
1212 return FAILED;
1213 }
1214
1215 rta = XFRM_RTA(out, struct xfrm_aevent_id);
1216 rtasize = XFRM_PAYLOAD(out, struct xfrm_aevent_id);
1217 while(RTA_OK(rta, rtasize))
1218 {
1219 if (rta->rta_type == XFRMA_REPLAY_VAL &&
1220 RTA_PAYLOAD(rta) == sizeof(struct xfrm_replay_state))
1221 {
1222 memcpy(replay, RTA_DATA(rta), RTA_PAYLOAD(rta));
1223 free(out);
1224 return SUCCESS;
1225 }
1226 rta = RTA_NEXT(rta, rtasize);
1227 }
1228
1229 DBG1(DBG_KNL, "unable to query replay state from SAD entry with SPI %.8x",
1230 ntohl(spi));
1231 free(out);
1232 return FAILED;
1233 }
1234
1235 /**
1236 * Implementation of kernel_interface_t.query_sa.
1237 */
1238 static status_t query_sa(private_kernel_netlink_ipsec_t *this, host_t *src,
1239 host_t *dst, u_int32_t spi, protocol_id_t protocol,
1240 u_int64_t *bytes)
1241 {
1242 netlink_buf_t request;
1243 struct nlmsghdr *out = NULL, *hdr;
1244 struct xfrm_usersa_id *sa_id;
1245 struct xfrm_usersa_info *sa = NULL;
1246 size_t len;
1247
1248 memset(&request, 0, sizeof(request));
1249
1250 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x", ntohl(spi));
1251
1252 hdr = (struct nlmsghdr*)request;
1253 hdr->nlmsg_flags = NLM_F_REQUEST;
1254 hdr->nlmsg_type = XFRM_MSG_GETSA;
1255 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1256
1257 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1258 host2xfrm(dst, &sa_id->daddr);
1259 sa_id->spi = spi;
1260 sa_id->proto = proto_ike2kernel(protocol);
1261 sa_id->family = dst->get_family(dst);
1262
1263 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1264 {
1265 hdr = out;
1266 while (NLMSG_OK(hdr, len))
1267 {
1268 switch (hdr->nlmsg_type)
1269 {
1270 case XFRM_MSG_NEWSA:
1271 {
1272 sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
1273 break;
1274 }
1275 case NLMSG_ERROR:
1276 {
1277 struct nlmsgerr *err = NLMSG_DATA(hdr);
1278 DBG1(DBG_KNL, "querying SAD entry with SPI %.8x failed: %s (%d)",
1279 ntohl(spi), strerror(-err->error), -err->error);
1280 break;
1281 }
1282 default:
1283 hdr = NLMSG_NEXT(hdr, len);
1284 continue;
1285 case NLMSG_DONE:
1286 break;
1287 }
1288 break;
1289 }
1290 }
1291
1292 if (sa == NULL)
1293 {
1294 DBG2(DBG_KNL, "unable to query SAD entry with SPI %.8x", ntohl(spi));
1295 free(out);
1296 return FAILED;
1297 }
1298 *bytes = sa->curlft.bytes;
1299
1300 free(out);
1301 return SUCCESS;
1302 }
1303 /**
1304 * Implementation of kernel_interface_t.del_sa.
1305 */
1306 static status_t del_sa(private_kernel_netlink_ipsec_t *this, host_t *src,
1307 host_t *dst, u_int32_t spi, protocol_id_t protocol,
1308 u_int16_t cpi)
1309 {
1310 netlink_buf_t request;
1311 struct nlmsghdr *hdr;
1312 struct xfrm_usersa_id *sa_id;
1313
1314 /* if IPComp was used, we first delete the additional IPComp SA */
1315 if (cpi)
1316 {
1317 del_sa(this, src, dst, htonl(ntohs(cpi)), IPPROTO_COMP, 0);
1318 }
1319
1320 memset(&request, 0, sizeof(request));
1321
1322 DBG2(DBG_KNL, "deleting SAD entry with SPI %.8x", ntohl(spi));
1323
1324 hdr = (struct nlmsghdr*)request;
1325 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1326 hdr->nlmsg_type = XFRM_MSG_DELSA;
1327 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1328
1329 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1330 host2xfrm(dst, &sa_id->daddr);
1331 sa_id->spi = spi;
1332 sa_id->proto = proto_ike2kernel(protocol);
1333 sa_id->family = dst->get_family(dst);
1334
1335 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1336 {
1337 DBG1(DBG_KNL, "unable to delete SAD entry with SPI %.8x", ntohl(spi));
1338 return FAILED;
1339 }
1340 DBG2(DBG_KNL, "deleted SAD entry with SPI %.8x", ntohl(spi));
1341 return SUCCESS;
1342 }
1343
1344 /**
1345 * Implementation of kernel_interface_t.update_sa.
1346 */
1347 static status_t update_sa(private_kernel_netlink_ipsec_t *this,
1348 u_int32_t spi, protocol_id_t protocol, u_int16_t cpi,
1349 host_t *src, host_t *dst,
1350 host_t *new_src, host_t *new_dst,
1351 bool old_encap, bool new_encap)
1352 {
1353 netlink_buf_t request;
1354 u_char *pos;
1355 struct nlmsghdr *hdr, *out = NULL;
1356 struct xfrm_usersa_id *sa_id;
1357 struct xfrm_usersa_info *out_sa = NULL, *sa;
1358 size_t len;
1359 struct rtattr *rta;
1360 size_t rtasize;
1361 struct xfrm_encap_tmpl* tmpl = NULL;
1362 bool got_replay_state = FALSE;
1363 struct xfrm_replay_state replay;
1364
1365 /* if IPComp is used, we first update the IPComp SA */
1366 if (cpi)
1367 {
1368 update_sa(this, htonl(ntohs(cpi)), IPPROTO_COMP, 0,
1369 src, dst, new_src, new_dst, FALSE, FALSE);
1370 }
1371
1372 memset(&request, 0, sizeof(request));
1373
1374 DBG2(DBG_KNL, "querying SAD entry with SPI %.8x for update", ntohl(spi));
1375
1376 /* query the existing SA first */
1377 hdr = (struct nlmsghdr*)request;
1378 hdr->nlmsg_flags = NLM_F_REQUEST;
1379 hdr->nlmsg_type = XFRM_MSG_GETSA;
1380 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
1381
1382 sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
1383 host2xfrm(dst, &sa_id->daddr);
1384 sa_id->spi = spi;
1385 sa_id->proto = proto_ike2kernel(protocol);
1386 sa_id->family = dst->get_family(dst);
1387
1388 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1389 {
1390 hdr = out;
1391 while (NLMSG_OK(hdr, len))
1392 {
1393 switch (hdr->nlmsg_type)
1394 {
1395 case XFRM_MSG_NEWSA:
1396 {
1397 out_sa = NLMSG_DATA(hdr);
1398 break;
1399 }
1400 case NLMSG_ERROR:
1401 {
1402 struct nlmsgerr *err = NLMSG_DATA(hdr);
1403 DBG1(DBG_KNL, "querying SAD entry failed: %s (%d)",
1404 strerror(-err->error), -err->error);
1405 break;
1406 }
1407 default:
1408 hdr = NLMSG_NEXT(hdr, len);
1409 continue;
1410 case NLMSG_DONE:
1411 break;
1412 }
1413 break;
1414 }
1415 }
1416 if (out_sa == NULL)
1417 {
1418 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1419 free(out);
1420 return FAILED;
1421 }
1422
1423 /* try to get the replay state */
1424 if (get_replay_state(this, spi, protocol, dst, &replay) == SUCCESS)
1425 {
1426 got_replay_state = TRUE;
1427 }
1428
1429 /* delete the old SA (without affecting the IPComp SA) */
1430 if (del_sa(this, src, dst, spi, protocol, 0) != SUCCESS)
1431 {
1432 DBG1(DBG_KNL, "unable to delete old SAD entry with SPI %.8x", ntohl(spi));
1433 free(out);
1434 return FAILED;
1435 }
1436
1437 DBG2(DBG_KNL, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1438 ntohl(spi), src, dst, new_src, new_dst);
1439 /* copy over the SA from out to request */
1440 hdr = (struct nlmsghdr*)request;
1441 memcpy(hdr, out, min(out->nlmsg_len, sizeof(request)));
1442 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1443 hdr->nlmsg_type = XFRM_MSG_NEWSA;
1444 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
1445 sa = NLMSG_DATA(hdr);
1446 sa->family = new_dst->get_family(new_dst);
1447
1448 if (!src->ip_equals(src, new_src))
1449 {
1450 host2xfrm(new_src, &sa->saddr);
1451 }
1452 if (!dst->ip_equals(dst, new_dst))
1453 {
1454 host2xfrm(new_dst, &sa->id.daddr);
1455 }
1456
1457 rta = XFRM_RTA(out, struct xfrm_usersa_info);
1458 rtasize = XFRM_PAYLOAD(out, struct xfrm_usersa_info);
1459 pos = (u_char*)XFRM_RTA(hdr, struct xfrm_usersa_info);
1460 while(RTA_OK(rta, rtasize))
1461 {
1462 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1463 if (rta->rta_type != XFRMA_ENCAP || new_encap)
1464 {
1465 if (rta->rta_type == XFRMA_ENCAP)
1466 { /* update encap tmpl */
1467 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1468 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1469 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1470 }
1471 memcpy(pos, rta, rta->rta_len);
1472 pos += RTA_ALIGN(rta->rta_len);
1473 hdr->nlmsg_len += RTA_ALIGN(rta->rta_len);
1474 }
1475 rta = RTA_NEXT(rta, rtasize);
1476 }
1477
1478 rta = (struct rtattr*)pos;
1479 if (tmpl == NULL && new_encap)
1480 { /* add tmpl if we are enabling it */
1481 rta->rta_type = XFRMA_ENCAP;
1482 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
1483
1484 hdr->nlmsg_len += rta->rta_len;
1485 if (hdr->nlmsg_len > sizeof(request))
1486 {
1487 return FAILED;
1488 }
1489
1490 tmpl = (struct xfrm_encap_tmpl*)RTA_DATA(rta);
1491 tmpl->encap_type = UDP_ENCAP_ESPINUDP;
1492 tmpl->encap_sport = ntohs(new_src->get_port(new_src));
1493 tmpl->encap_dport = ntohs(new_dst->get_port(new_dst));
1494 memset(&tmpl->encap_oa, 0, sizeof (xfrm_address_t));
1495
1496 rta = XFRM_RTA_NEXT(rta);
1497 }
1498
1499 if (got_replay_state)
1500 { /* copy the replay data if available */
1501 rta->rta_type = XFRMA_REPLAY_VAL;
1502 rta->rta_len = RTA_LENGTH(sizeof(struct xfrm_replay_state));
1503
1504 hdr->nlmsg_len += rta->rta_len;
1505 if (hdr->nlmsg_len > sizeof(request))
1506 {
1507 return FAILED;
1508 }
1509 memcpy(RTA_DATA(rta), &replay, sizeof(replay));
1510
1511 rta = XFRM_RTA_NEXT(rta);
1512 }
1513
1514 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1515 {
1516 DBG1(DBG_KNL, "unable to update SAD entry with SPI %.8x", ntohl(spi));
1517 free(out);
1518 return FAILED;
1519 }
1520 free(out);
1521
1522 return SUCCESS;
1523 }
1524
1525 /**
1526 * Implementation of kernel_interface_t.add_policy.
1527 */
1528 static status_t add_policy(private_kernel_netlink_ipsec_t *this,
1529 host_t *src, host_t *dst,
1530 traffic_selector_t *src_ts,
1531 traffic_selector_t *dst_ts,
1532 policy_dir_t direction, u_int32_t spi,
1533 protocol_id_t protocol, u_int32_t reqid,
1534 ipsec_mode_t mode, u_int16_t ipcomp, u_int16_t cpi,
1535 bool routed)
1536 {
1537 policy_entry_t *current, *policy;
1538 bool found = FALSE;
1539 netlink_buf_t request;
1540 struct xfrm_userpolicy_info *policy_info;
1541 struct nlmsghdr *hdr;
1542
1543 /* create a policy */
1544 policy = malloc_thing(policy_entry_t);
1545 memset(policy, 0, sizeof(policy_entry_t));
1546 policy->sel = ts2selector(src_ts, dst_ts);
1547 policy->direction = direction;
1548
1549 /* find the policy, which matches EXACTLY */
1550 this->mutex->lock(this->mutex);
1551 current = this->policies->get(this->policies, policy);
1552 if (current)
1553 {
1554 /* use existing policy */
1555 current->refcount++;
1556 DBG2(DBG_KNL, "policy %R === %R %N already exists, increasing "
1557 "refcount", src_ts, dst_ts,
1558 policy_dir_names, direction);
1559 free(policy);
1560 policy = current;
1561 found = TRUE;
1562 }
1563 else
1564 { /* apply the new one, if we have no such policy */
1565 this->policies->put(this->policies, policy, policy);
1566 policy->refcount = 1;
1567 }
1568
1569 DBG2(DBG_KNL, "adding policy %R === %R %N", src_ts, dst_ts,
1570 policy_dir_names, direction);
1571
1572 memset(&request, 0, sizeof(request));
1573 hdr = (struct nlmsghdr*)request;
1574 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1575 hdr->nlmsg_type = found ? XFRM_MSG_UPDPOLICY : XFRM_MSG_NEWPOLICY;
1576 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
1577
1578 policy_info = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1579 policy_info->sel = policy->sel;
1580 policy_info->dir = policy->direction;
1581 /* calculate priority based on source selector size, small size = high prio */
1582 policy_info->priority = routed ? PRIO_LOW : PRIO_HIGH;
1583 policy_info->priority -= policy->sel.prefixlen_s * 10;
1584 policy_info->priority -= policy->sel.proto ? 2 : 0;
1585 policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
1586 policy_info->action = XFRM_POLICY_ALLOW;
1587 policy_info->share = XFRM_SHARE_ANY;
1588 this->mutex->unlock(this->mutex);
1589
1590 /* policies don't expire */
1591 policy_info->lft.soft_byte_limit = XFRM_INF;
1592 policy_info->lft.soft_packet_limit = XFRM_INF;
1593 policy_info->lft.hard_byte_limit = XFRM_INF;
1594 policy_info->lft.hard_packet_limit = XFRM_INF;
1595 policy_info->lft.soft_add_expires_seconds = 0;
1596 policy_info->lft.hard_add_expires_seconds = 0;
1597 policy_info->lft.soft_use_expires_seconds = 0;
1598 policy_info->lft.hard_use_expires_seconds = 0;
1599
1600 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
1601 rthdr->rta_type = XFRMA_TMPL;
1602 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1603
1604 hdr->nlmsg_len += rthdr->rta_len;
1605 if (hdr->nlmsg_len > sizeof(request))
1606 {
1607 return FAILED;
1608 }
1609
1610 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
1611
1612 if (ipcomp != IPCOMP_NONE)
1613 {
1614 tmpl->reqid = reqid;
1615 tmpl->id.proto = IPPROTO_COMP;
1616 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1617 tmpl->mode = mode2kernel(mode);
1618 tmpl->optional = direction != POLICY_OUT;
1619 tmpl->family = src->get_family(src);
1620
1621 host2xfrm(src, &tmpl->saddr);
1622 host2xfrm(dst, &tmpl->id.daddr);
1623
1624 /* add an additional xfrm_user_tmpl */
1625 rthdr->rta_len += RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1626 hdr->nlmsg_len += RTA_LENGTH(sizeof(struct xfrm_user_tmpl));
1627 if (hdr->nlmsg_len > sizeof(request))
1628 {
1629 return FAILED;
1630 }
1631
1632 tmpl++;
1633 }
1634
1635 tmpl->reqid = reqid;
1636 tmpl->id.proto = proto_ike2kernel(protocol);
1637 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
1638 tmpl->mode = mode2kernel(mode);
1639 tmpl->family = src->get_family(src);
1640
1641 host2xfrm(src, &tmpl->saddr);
1642 host2xfrm(dst, &tmpl->id.daddr);
1643
1644 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1645 {
1646 DBG1(DBG_KNL, "unable to add policy %R === %R %N", src_ts, dst_ts,
1647 policy_dir_names, direction);
1648 return FAILED;
1649 }
1650
1651 /* install a route, if:
1652 * - we are NOT updating a policy
1653 * - this is a forward policy (to just get one for each child)
1654 * - we are in tunnel mode
1655 * - we are not using IPv6 (does not work correctly yet!)
1656 * - routing is not disabled via strongswan.conf
1657 */
1658 if (policy->route == NULL && direction == POLICY_FWD &&
1659 mode != MODE_TRANSPORT && src->get_family(src) != AF_INET6 &&
1660 this->install_routes)
1661 {
1662 route_entry_t *route = malloc_thing(route_entry_t);
1663
1664 if (charon->kernel_interface->get_address_by_ts(charon->kernel_interface,
1665 dst_ts, &route->src_ip) == SUCCESS)
1666 {
1667 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1668 route->gateway = charon->kernel_interface->get_nexthop(
1669 charon->kernel_interface, src);
1670 route->if_name = charon->kernel_interface->get_interface(
1671 charon->kernel_interface, dst);
1672 route->dst_net = chunk_alloc(policy->sel.family == AF_INET ? 4 : 16);
1673 memcpy(route->dst_net.ptr, &policy->sel.saddr, route->dst_net.len);
1674 route->prefixlen = policy->sel.prefixlen_s;
1675
1676 if (route->if_name)
1677 {
1678 switch (charon->kernel_interface->add_route(
1679 charon->kernel_interface, route->dst_net,
1680 route->prefixlen, route->gateway,
1681 route->src_ip, route->if_name))
1682 {
1683 default:
1684 DBG1(DBG_KNL, "unable to install source route for %H",
1685 route->src_ip);
1686 /* FALL */
1687 case ALREADY_DONE:
1688 /* route exists, do not uninstall */
1689 route_entry_destroy(route);
1690 break;
1691 case SUCCESS:
1692 /* cache the installed route */
1693 policy->route = route;
1694 break;
1695 }
1696 }
1697 else
1698 {
1699 route_entry_destroy(route);
1700 }
1701 }
1702 else
1703 {
1704 free(route);
1705 }
1706 }
1707 return SUCCESS;
1708 }
1709
1710 /**
1711 * Implementation of kernel_interface_t.query_policy.
1712 */
1713 static status_t query_policy(private_kernel_netlink_ipsec_t *this,
1714 traffic_selector_t *src_ts,
1715 traffic_selector_t *dst_ts,
1716 policy_dir_t direction, u_int32_t *use_time)
1717 {
1718 netlink_buf_t request;
1719 struct nlmsghdr *out = NULL, *hdr;
1720 struct xfrm_userpolicy_id *policy_id;
1721 struct xfrm_userpolicy_info *policy = NULL;
1722 size_t len;
1723
1724 memset(&request, 0, sizeof(request));
1725
1726 DBG2(DBG_KNL, "querying policy %R === %R %N", src_ts, dst_ts,
1727 policy_dir_names, direction);
1728
1729 hdr = (struct nlmsghdr*)request;
1730 hdr->nlmsg_flags = NLM_F_REQUEST;
1731 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
1732 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1733
1734 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1735 policy_id->sel = ts2selector(src_ts, dst_ts);
1736 policy_id->dir = direction;
1737
1738 if (this->socket_xfrm->send(this->socket_xfrm, hdr, &out, &len) == SUCCESS)
1739 {
1740 hdr = out;
1741 while (NLMSG_OK(hdr, len))
1742 {
1743 switch (hdr->nlmsg_type)
1744 {
1745 case XFRM_MSG_NEWPOLICY:
1746 {
1747 policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
1748 break;
1749 }
1750 case NLMSG_ERROR:
1751 {
1752 struct nlmsgerr *err = NLMSG_DATA(hdr);
1753 DBG1(DBG_KNL, "querying policy failed: %s (%d)",
1754 strerror(-err->error), -err->error);
1755 break;
1756 }
1757 default:
1758 hdr = NLMSG_NEXT(hdr, len);
1759 continue;
1760 case NLMSG_DONE:
1761 break;
1762 }
1763 break;
1764 }
1765 }
1766
1767 if (policy == NULL)
1768 {
1769 DBG2(DBG_KNL, "unable to query policy %R === %R %N", src_ts, dst_ts,
1770 policy_dir_names, direction);
1771 free(out);
1772 return FAILED;
1773 }
1774 *use_time = (time_t)policy->curlft.use_time;
1775
1776 free(out);
1777 return SUCCESS;
1778 }
1779
1780 /**
1781 * Implementation of kernel_interface_t.del_policy.
1782 */
1783 static status_t del_policy(private_kernel_netlink_ipsec_t *this,
1784 traffic_selector_t *src_ts,
1785 traffic_selector_t *dst_ts,
1786 policy_dir_t direction, bool unrouted)
1787 {
1788 policy_entry_t *current, policy, *to_delete = NULL;
1789 route_entry_t *route;
1790 netlink_buf_t request;
1791 struct nlmsghdr *hdr;
1792 struct xfrm_userpolicy_id *policy_id;
1793
1794 DBG2(DBG_KNL, "deleting policy %R === %R %N", src_ts, dst_ts,
1795 policy_dir_names, direction);
1796
1797 /* create a policy */
1798 memset(&policy, 0, sizeof(policy_entry_t));
1799 policy.sel = ts2selector(src_ts, dst_ts);
1800 policy.direction = direction;
1801
1802 /* find the policy */
1803 this->mutex->lock(this->mutex);
1804 current = this->policies->get(this->policies, &policy);
1805 if (current)
1806 {
1807 to_delete = current;
1808 if (--to_delete->refcount > 0)
1809 {
1810 /* is used by more SAs, keep in kernel */
1811 DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
1812 this->mutex->unlock(this->mutex);
1813 return SUCCESS;
1814 }
1815 /* remove if last reference */
1816 this->policies->remove(this->policies, to_delete);
1817 }
1818 this->mutex->unlock(this->mutex);
1819 if (!to_delete)
1820 {
1821 DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
1822 dst_ts, policy_dir_names, direction);
1823 return NOT_FOUND;
1824 }
1825
1826 memset(&request, 0, sizeof(request));
1827
1828 hdr = (struct nlmsghdr*)request;
1829 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1830 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
1831 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
1832
1833 policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
1834 policy_id->sel = to_delete->sel;
1835 policy_id->dir = direction;
1836
1837 route = to_delete->route;
1838 free(to_delete);
1839
1840 if (this->socket_xfrm->send_ack(this->socket_xfrm, hdr) != SUCCESS)
1841 {
1842 DBG1(DBG_KNL, "unable to delete policy %R === %R %N", src_ts, dst_ts,
1843 policy_dir_names, direction);
1844 return FAILED;
1845 }
1846
1847 if (route)
1848 {
1849 if (charon->kernel_interface->del_route(charon->kernel_interface,
1850 route->dst_net, route->prefixlen, route->gateway,
1851 route->src_ip, route->if_name) != SUCCESS)
1852 {
1853 DBG1(DBG_KNL, "error uninstalling route installed with "
1854 "policy %R === %R %N", src_ts, dst_ts,
1855 policy_dir_names, direction);
1856 }
1857 route_entry_destroy(route);
1858 }
1859 return SUCCESS;
1860 }
1861
1862 /**
1863 * Implementation of kernel_interface_t.destroy.
1864 */
1865 static void destroy(private_kernel_netlink_ipsec_t *this)
1866 {
1867 enumerator_t *enumerator;
1868 policy_entry_t *policy;
1869
1870 this->job->cancel(this->job);
1871 close(this->socket_xfrm_events);
1872 this->socket_xfrm->destroy(this->socket_xfrm);
1873 enumerator = this->policies->create_enumerator(this->policies);
1874 while (enumerator->enumerate(enumerator, &policy, &policy))
1875 {
1876 free(policy);
1877 }
1878 enumerator->destroy(enumerator);
1879 this->policies->destroy(this->policies);
1880 this->mutex->destroy(this->mutex);
1881 free(this);
1882 }
1883
1884 /**
1885 * Add bypass policies for IKE on the sockets used by charon
1886 */
1887 static bool add_bypass_policies()
1888 {
1889 int fd, family, port;
1890 enumerator_t *sockets;
1891 bool status = TRUE;
1892
1893 /* we open an AF_KEY socket to autoload the af_key module. Otherwise
1894 * setsockopt(IPSEC_POLICY) won't work. */
1895 fd = socket(AF_KEY, SOCK_RAW, PF_KEY_V2);
1896 if (fd == 0)
1897 {
1898 DBG1(DBG_KNL, "could not open AF_KEY socket");
1899 return FALSE;
1900 }
1901 close(fd);
1902
1903 sockets = charon->socket->create_enumerator(charon->socket);
1904 while (sockets->enumerate(sockets, &fd, &family, &port))
1905 {
1906 struct sadb_x_policy policy;
1907 u_int sol, ipsec_policy;
1908
1909 switch (family)
1910 {
1911 case AF_INET:
1912 sol = SOL_IP;
1913 ipsec_policy = IP_IPSEC_POLICY;
1914 break;
1915 case AF_INET6:
1916 sol = SOL_IPV6;
1917 ipsec_policy = IPV6_IPSEC_POLICY;
1918 break;
1919 default:
1920 continue;
1921 }
1922
1923 memset(&policy, 0, sizeof(policy));
1924 policy.sadb_x_policy_len = sizeof(policy) / sizeof(u_int64_t);
1925 policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1926 policy.sadb_x_policy_type = IPSEC_POLICY_BYPASS;
1927
1928 policy.sadb_x_policy_dir = IPSEC_DIR_OUTBOUND;
1929 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
1930 {
1931 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
1932 strerror(errno));
1933 status = FALSE;
1934 break;
1935 }
1936 policy.sadb_x_policy_dir = IPSEC_DIR_INBOUND;
1937 if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
1938 {
1939 DBG1(DBG_KNL, "unable to set IPSEC_POLICY on socket: %s",
1940 strerror(errno));
1941 status = FALSE;
1942 break;
1943 }
1944 }
1945 sockets->destroy(sockets);
1946 return status;
1947 }
1948
1949 /*
1950 * Described in header.
1951 */
1952 kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
1953 {
1954 private_kernel_netlink_ipsec_t *this = malloc_thing(private_kernel_netlink_ipsec_t);
1955 struct sockaddr_nl addr;
1956
1957 /* public functions */
1958 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;
1959 this->public.interface.get_cpi = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,u_int32_t,u_int16_t*))get_cpi;
1960 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;
1961 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;
1962 this->public.interface.query_sa = (status_t(*)(kernel_ipsec_t*,host_t*,host_t*,u_int32_t,protocol_id_t,u_int64_t*))query_sa;
1963 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;
1964 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;
1965 this->public.interface.query_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,u_int32_t*))query_policy;
1966 this->public.interface.del_policy = (status_t(*)(kernel_ipsec_t*,traffic_selector_t*,traffic_selector_t*,policy_dir_t,bool))del_policy;
1967 this->public.interface.destroy = (void(*)(kernel_ipsec_t*)) destroy;
1968
1969 /* private members */
1970 this->policies = hashtable_create((hashtable_hash_t)policy_hash,
1971 (hashtable_equals_t)policy_equals, 32);
1972 this->mutex = mutex_create(MUTEX_DEFAULT);
1973 this->install_routes = lib->settings->get_bool(lib->settings,
1974 "charon.install_routes", TRUE);
1975
1976 /* add bypass policies on the sockets used by charon */
1977 if (!add_bypass_policies())
1978 {
1979 charon->kill(charon, "unable to add bypass policies on sockets");
1980 }
1981
1982 this->socket_xfrm = netlink_socket_create(NETLINK_XFRM);
1983
1984 memset(&addr, 0, sizeof(addr));
1985 addr.nl_family = AF_NETLINK;
1986
1987 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
1988 this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1989 if (this->socket_xfrm_events <= 0)
1990 {
1991 charon->kill(charon, "unable to create XFRM event socket");
1992 }
1993 addr.nl_groups = XFRMNLGRP(ACQUIRE) | XFRMNLGRP(EXPIRE) |
1994 XFRMNLGRP(MIGRATE) | XFRMNLGRP(MAPPING);
1995 if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
1996 {
1997 charon->kill(charon, "unable to bind XFRM event socket");
1998 }
1999
2000 this->job = callback_job_create((callback_job_cb_t)receive_events,
2001 this, NULL, NULL);
2002 charon->processor->queue_job(charon->processor, (job_t*)this->job);
2003
2004 return &this->public;
2005 }