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