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