blob: 652de6552f702f67ed2fcaa9fe54ff2f7bdaeff8 [file] [log] [blame]
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001/*
2 * Copyright (c) 2022 Intel and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <daemon.h>
17#include <utils/debug.h>
18#include <vlibapi/api.h>
19#include <vlibmemory/api.h>
20#include <vnet/ipsec/ipsec.h>
21#include <vnet/vnet.h>
22#include <collections/hashtable.h>
23#include <threading/mutex.h>
24#include <processing/jobs/callback_job.h>
25#include <vpp-api/client/stat_client.h>
26
27#define vl_typedefs
28#define vl_endianfun
29/* Include the (first) vlib-api API definition layer */
30#include <vlibmemory/vl_memory_api_h.h>
31/* Include the current layer (third) vpp API definition layer */
32#include <vpp/api/vpe_types.api.h>
33#include <vpp/api/vpe.api.h>
34
35#include <vnet/ip-neighbor/ip_neighbor.api_enum.h>
36#include <vnet/ip-neighbor/ip_neighbor.api_types.h>
37#include <vnet/ipsec/ipsec.api_enum.h>
38#include <vnet/ipsec/ipsec.api_types.h>
39#include <vnet/interface.api_enum.h>
40#include <vnet/interface.api_types.h>
41#undef vl_typedefs
42#undef vl_endianfun
43
44#include "kernel_vpp_ipsec.h"
45#include "kernel_vpp_shared.h"
46
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <sys/socket.h>
51#include <netinet/in.h>
52#include <arpa/inet.h>
53#include <sys/ioctl.h>
54#include <net/if.h>
55#include <net/route.h>
56#include <unistd.h>
57#include <fcntl.h>
58#include <sys/types.h>
59#include <net/if_arp.h>
60#include <sys/stat.h>
61#include <dirent.h>
62
63#define PRIO_BASE 384
64
Gabriel Oginski2da99e52023-02-14 08:46:36 +000065/**
66 * Every 2 seconds, the thread responsible for collecting the available
67 * interfaces will be executed.
68 * Retrying 5 times every 1 second ensures that there is enough time to check
69 * if the interface will be available.
70 */
71#define N_RETRY_GET_IF 5
72
Gabriel Oginski4e88e042022-06-29 12:54:30 +000073u32 natt_port;
74
75/**
76 * One and only instance of the daemon.
77 */
78daemon_t *charon;
79
80typedef struct private_kernel_vpp_ipsec_t private_kernel_vpp_ipsec_t;
81
82/**
83 * Private variables of kernel_vpp_ipsec class.
84 */
85struct private_kernel_vpp_ipsec_t
86{
87
88 /**
89 * Public interface
90 */
91 kernel_vpp_ipsec_t public;
92
93 /**
94 * Next security association database entry ID to allocate
95 */
96 refcount_t next_sad_id;
97
98 /**
99 * Next security policy database entry ID to allocate
100 */
101 refcount_t next_spd_id;
102
103 /**
104 * Mutex to lock access to installed policies
105 */
106 mutex_t *mutex;
107
108 /**
109 * Hash table of instaled SA, as kernel_ipsec_sa_id_t => sa_t
110 */
111 hashtable_t *sas;
112
113 /**
114 * Hash table of security policy databases, as nterface => spd_t
115 */
116 hashtable_t *spds;
117
118 /**
119 * Linked list of installed routes
120 */
121 linked_list_t *routes;
122
123 /**
124 * Next SPI to allocate
125 */
126 refcount_t nextspi;
127
128 /**
129 * Mix value to distribute SPI allocation randomly
130 */
131 uint32_t mixspi;
132
133 /**
134 * Whether to install routes along policies
135 */
136 bool install_routes;
Atzm Watanabefc2d95d2023-01-25 14:11:10 +0900137
138 /**
139 * Whether to install SAs with tunnel flag. Disabling this can be useful
140 * in some scenarios e.g. using SAs to "ipsec tunnel protect" for the
141 * route-based IPsec
142 */
143 bool use_tunnel_mode_sa;
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000144
145 /**
146 * Connections to VPP Stats
147 */
148 stat_client_main_t *sm;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000149};
150
151/**
152 * Security association entry
153 */
154typedef struct
155{
156 /** VPP SA ID */
157 uint32_t sa_id;
158 uint32_t stat_index;
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000159 kernel_ipsec_sa_id_t *sa_id_p;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000160} sa_t;
161
162/**
163 * Security policy database
164 */
165typedef struct
166{
167 /** VPP SPD ID */
168 uint32_t spd_id;
169 /** Networking interface ID restricting policy */
170 uint32_t sw_if_index;
171 /** Policy count for this SPD */
172 refcount_t policy_num;
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000173 /** Name of the interface the SPD is bound to */
174 char *if_name;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000175} spd_t;
176
177/**
178 * Installed route
179 */
180typedef struct
181{
182 /** Name of the interface the route is bound to */
183 char *if_name;
184 /** Gateway of route */
185 host_t *gateway;
186 /** Destination network of route */
187 host_t *dst_net;
188 /** Prefix length of dst_net */
189 uint8_t prefixlen;
190 /** References for route */
191 refcount_t refs;
192} route_entry_t;
193
194#define htonll(x) \
195 ((1 == htonl (1)) ? \
196 (x) : \
197 ((uint64_t) htonl ((x) &0xFFFFFFFF) << 32) | htonl ((x) >> 32))
198#define ntohll(x) \
199 ((1 == ntohl (1)) ? \
200 (x) : \
201 ((uint64_t) ntohl ((x) &0xFFFFFFFF) << 32) | ntohl ((x) >> 32))
202
203CALLBACK (route_equals, bool, route_entry_t *a, va_list args)
204{
205 host_t *dst_net, *gateway;
206 uint8_t *prefixlen;
207 char *if_name;
208
209 VA_ARGS_VGET (args, if_name, gateway, dst_net, prefixlen);
210
211 return a->if_name && if_name && streq (a->if_name, if_name) &&
212 a->gateway->ip_equals (a->gateway, gateway) &&
213 a->dst_net->ip_equals (a->dst_net, dst_net) &&
214 a->prefixlen == *prefixlen;
215}
216
217/**
218 * Clean up a route entry
219 */
220static void
221route_destroy (route_entry_t *this)
222{
223 this->dst_net->destroy (this->dst_net);
224 this->gateway->destroy (this->gateway);
225 free (this->if_name);
226 free (this);
227}
228
229static uint32_t get_sw_if_index ();
230
231static int
232set_arp (char *ipStr, char *if_name, bool add)
233{
234 char *out = NULL;
235 int out_len = 0;
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000236 vl_api_ip_neighbor_add_del_t *mp = NULL;
237 vl_api_ip_neighbor_add_del_reply_t *rmp = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000238 int rc = SUCCESS;
239 uint32_t sw_if_index = ~0;
240
241 FILE *fp;
242 int nread = 0;
243 ssize_t len = 0;
244 char *buffer = NULL;
245 char buf[2][20];
246 char *file = "/proc/net/arp";
247 unsigned char mac[8] = {
248 0,
249 };
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000250 uint32_t addr = 0;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000251
252 if (if_name == NULL || ipStr == NULL)
253 {
254 DBG2 (DBG_KNL, "para is null\n");
255 rc = FAILED;
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000256 goto error;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000257 }
258 DBG2 (DBG_KNL, "from kernel read mac\n");
259
260 mp = vl_msg_api_alloc (sizeof (*mp));
261 memset (mp, 0, sizeof (*mp));
262 sw_if_index = get_sw_if_index (if_name);
263 if (sw_if_index == ~0)
264 {
265 DBG1 (DBG_KNL, "sw_if_index for %s not found", if_name);
266 goto error;
267 }
268
269 fp = fopen (file, "rb");
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000270 while (fp && ((nread = getline (&buffer, &len, fp)) != -1))
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000271 {
272 sscanf (buffer, "%s %*s %*s %s %*s %*s", &buf[0], &buf[1]);
273 inet_aton (&buf[0], &addr);
274
275 if (addr == *((u32 *) (ipStr)))
276 {
277 sscanf (buf[1], "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1],
278 &mac[2], &mac[3], &mac[4], &mac[5]);
279 u16 msg_id =
280 vl_msg_api_get_msg_index ((u8 *) "ip_neighbor_add_del_0607c257");
281 mp->_vl_msg_id = htons (msg_id);
282 mp->is_add = add;
283 memcpy (mp->neighbor.ip_address.un.ip4, (u8 *) &addr, sizeof (addr));
284 mp->neighbor.ip_address.af = 0;
285 memcpy (mp->neighbor.mac_address, mac, 6);
286 mp->neighbor.sw_if_index = htonl (sw_if_index);
287 mp->neighbor.flags = 1;
288
289 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
290 {
291 DBG1 (DBG_KNL, "vac %s neighbor entry",
292 add ? "adding" : "removing");
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000293 fclose (fp);
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000294 goto error;
295 }
296 rmp = (void *) out;
297 if (rmp->retval)
298 {
299 DBG1 (DBG_KNL, "%s neighbor add rv:%d", add ? "add" : "remove",
300 ntohl (rmp->retval));
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000301 fclose (fp);
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000302 goto error;
303 }
304 fclose (fp);
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000305 free (out);
306 vl_msg_api_free (mp);
307 free (buffer);
308
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000309 return rc;
310 }
311 }
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000312
313 if (fp != NULL)
314 {
315 fclose (fp);
316 fp = NULL;
317 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000318
319error:
320 free (out);
321 vl_msg_api_free (mp);
Gabriel Oginski9cb3e152023-02-14 08:41:07 +0000322 if (buffer != NULL)
323 {
324 free (buffer);
325 buffer = NULL;
326 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000327 return rc;
328}
329
330static int
331add_Route (char *ipAddr, int len, char *mask, char *gateWay)
332{
333 int fd;
334 int rc = SUCCESS;
335 struct sockaddr_in _sin;
336 struct sockaddr_in *sin = &_sin;
337 struct rtentry rt;
338
339 do
340 {
341 fd = socket (AF_INET, SOCK_DGRAM, 0);
342 if (fd < 0)
343 {
344 DBG2 (DBG_KNL, "addRoute: socket error\n");
345 rc = FAILED;
346 break;
347 }
348 memset (&rt, 0, sizeof (struct rtentry));
349 memset (sin, 0, sizeof (struct sockaddr_in));
350 sin->sin_family = AF_INET;
351 sin->sin_port = 0;
352
353 if (inet_aton (gateWay, &sin->sin_addr) < 0)
354 {
355 rc = FAILED;
356 break;
357 }
358 memcpy (&rt.rt_gateway, sin, sizeof (struct sockaddr_in));
359
360 ((struct sockaddr_in *) &rt.rt_dst)->sin_family = AF_INET;
361 memcpy (&((struct sockaddr_in *) &rt.rt_dst)->sin_addr, ipAddr, len);
362
363 ((struct sockaddr_in *) &rt.rt_genmask)->sin_family = AF_INET;
364 if (inet_aton (mask,
365 &((struct sockaddr_in *) &rt.rt_genmask)->sin_addr) < 0)
366 {
367 rc = FAILED;
368 break;
369 }
370 rt.rt_flags = RTF_GATEWAY;
371 if (ioctl (fd, SIOCADDRT, &rt) < 0)
372 {
373 rc = FAILED;
374 }
375 }
376 while (0);
377
378 close (fd);
379 return rc;
380}
381
382static int
383set_address (u32 ipAddr, u32 sw_if_index, bool add)
384{
385 char *out = NULL;
386 int out_len = 0;
387 vl_api_sw_interface_add_del_address_t *mp;
388 vl_api_sw_interface_add_del_address_reply_t *rmp;
389
390 int rc = SUCCESS;
391
392 uint32_t addr;
393
394 mp = vl_msg_api_alloc (sizeof (*mp));
395 memset (mp, 0, sizeof (*mp));
396
397 u16 msg_id =
398 vl_msg_api_get_msg_index ((u8 *) "sw_interface_add_del_address_5463d73b");
399 mp->_vl_msg_id = htons (msg_id);
400 mp->is_add = add;
401 memcpy (mp->prefix.address.un.ip4, (u8 *) &ipAddr, sizeof (ipAddr));
402 mp->prefix.len = 24;
403 mp->sw_if_index = sw_if_index;
404
405 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
406 {
407 DBG2 (DBG_KNL, "vac %s neighbor entry", add ? "adding" : "removing");
408 goto error;
409 }
410 rmp = (void *) out;
411 if (rmp->retval)
412 {
413 DBG2 (DBG_KNL, "%s neighbor add rv:%d", add ? "add" : "remove",
414 ntohl (rmp->retval));
415 goto error;
416 }
417 return rc;
418
419error:
420 free (out);
421 vl_msg_api_free (mp);
422 return rc;
423}
424
425/**
426 * (Un)-install a single route
427 */
428static void
429manage_route (private_kernel_vpp_ipsec_t *this, bool add,
430 traffic_selector_t *dst_ts, host_t *src, host_t *dst)
431{
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000432 host_t *dst_net = NULL, *gateway = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000433 uint8_t prefixlen;
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000434 char *if_name = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000435 route_entry_t *route;
436 bool route_exist = FALSE;
437
438 char *netmask = "255.255.255.0";
439 char *tap_gateway = "1.1.1.1";
440 int arp_rc = 0;
441 if (dst->is_anyaddr (dst))
442 {
443 return;
444 }
445 gateway =
446 charon->kernel->get_nexthop (charon->kernel, dst, -1, NULL, &if_name);
447 dst_ts->to_subnet (dst_ts, &dst_net, &prefixlen);
448 if (!if_name)
449 {
450 if (src->is_anyaddr (src))
451 {
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000452 goto error;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000453 }
454 if (!charon->kernel->get_interface (charon->kernel, src, &if_name))
455 {
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000456 goto error;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000457 }
458 }
459 route_exist =
460 this->routes->find_first (this->routes, route_equals, (void **) &route,
461 if_name, gateway, dst_net, &prefixlen);
462 if (add)
463 {
464 DBG2 (DBG_KNL, "installing route: %H/%d via %H dev %s", dst_net,
465 prefixlen, gateway, if_name);
466 if (route_exist)
467 {
468 unsigned int refs_num = ref_get (&route->refs);
469 DBG2 (DBG_KNL, "add route but it exist %d", refs_num);
470 }
471 else
472 {
473 INIT (route, .if_name = strdup (if_name),
474 .gateway = gateway->clone (gateway),
475 .dst_net = dst_net->clone (dst_net), .prefixlen = prefixlen,
476 .refs = 1, );
477 this->routes->insert_last (this->routes, route);
478 charon->kernel->add_route (charon->kernel,
479 dst_net->get_address (dst_net), prefixlen,
480 gateway, dst, if_name, 1);
481 }
482
483 add_Route (dst_net->get_address (dst_net).ptr,
484 dst_net->get_address (dst_net).len, netmask, tap_gateway);
485
486 arp_rc = set_arp (gateway->get_address (gateway).ptr, if_name, TRUE);
487 if (arp_rc)
488 DBG2 (DBG_KNL, "arpGet success!\n");
489 }
490 else
491 {
492 DBG2 (DBG_KNL, "uninstalling route: %H/%d via %H dev %s", dst_net,
493 prefixlen, gateway, if_name);
494 if (!route_exist)
495 {
496 DBG2 (DBG_KNL, "del route but it not exist");
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000497 goto error;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000498 }
499 if (ref_put (&route->refs))
500 {
501 this->routes->remove (this->routes, route, NULL);
502 route_destroy (route);
503 charon->kernel->del_route (charon->kernel,
504 dst_net->get_address (dst_net), prefixlen,
505 gateway, dst, if_name, 1);
506 }
507 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000508error:
509 if (gateway != NULL)
510 gateway->destroy (gateway);
511 if (dst_net != NULL)
512 dst_net->destroy (dst_net);
513 if (if_name != NULL)
514 free (if_name);
515 return;
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000516}
517
518/**
519 * Hash function for IPsec SA
520 */
521static u_int
522sa_hash (kernel_ipsec_sa_id_t *sa)
523{
524 return chunk_hash_inc (
525 sa->src->get_address (sa->src),
526 chunk_hash_inc (
527 sa->dst->get_address (sa->dst),
528 chunk_hash_inc (chunk_from_thing (sa->spi),
529 chunk_hash (chunk_from_thing (sa->proto)))));
530}
531
532/**
533 * Equality function for IPsec SA
534 */
535static bool
536sa_equals (kernel_ipsec_sa_id_t *sa, kernel_ipsec_sa_id_t *other_sa)
537{
538 return sa->src->ip_equals (sa->src, other_sa->src) &&
539 sa->dst->ip_equals (sa->dst, other_sa->dst) &&
540 sa->spi == other_sa->spi && sa->proto == other_sa->proto;
541}
542
543/**
Gabriel Oginski8de66c02023-02-03 08:12:36 +0000544 * Equality function for policy SPD
545 */
546static bool
547policy_equals (vl_api_ipsec_spd_entry_t *policy,
548 vl_api_ipsec_spd_entry_t *other_policy)
549{
550
551 /* change protocol due to legacy implementation of ANY protocol inside VPP */
552 if (other_policy->protocol == 255)
553 other_policy->protocol = 0;
554
555 /* return true if both policies are equal */
556 return !memcmp (policy, other_policy, sizeof (*policy));
557}
558
559/**
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000560 * Hash function for interface
561 */
562static u_int
563interface_hash (char *interface)
564{
565 return chunk_hash (chunk_from_str (interface));
566}
567
568/**
569 * Equality function for interface
570 */
571static bool
572interface_equals (char *interface1, char *interface2)
573{
574 return streq (interface1, interface2);
575}
576
577/**
578 * Map an integer x with a one-to-one function using quadratic residues
579 */
580static u_int
581permute (u_int x, u_int p)
582{
583 u_int qr;
584
585 x = x % p;
586 qr = ((uint64_t) x * x) % p;
587 if (x <= p / 2)
588 {
589 return qr;
590 }
591 return p - qr;
592}
593
594/**
595 * Initialize seeds for SPI generation
596 */
597static bool
598init_spi (private_kernel_vpp_ipsec_t *this)
599{
600 bool ok = TRUE;
601 rng_t *rng;
602
603 rng = lib->crypto->create_rng (lib->crypto, RNG_STRONG);
604 if (!rng)
605 {
606 return FALSE;
607 }
608 ok =
609 rng->get_bytes (rng, sizeof (this->nextspi), (uint8_t *) &this->nextspi);
610 if (ok)
611 {
612 ok =
613 rng->get_bytes (rng, sizeof (this->mixspi), (uint8_t *) &this->mixspi);
614 }
615 rng->destroy (rng);
616 return ok;
617}
618
619/**
620 * Calculate policy priority
621 */
622static uint32_t
623calculate_priority (policy_priority_t policy_priority, traffic_selector_t *src,
624 traffic_selector_t *dst)
625{
626 uint32_t priority = PRIO_BASE;
627 uint16_t port;
628 uint8_t mask, proto;
629 host_t *net;
630
631 switch (policy_priority)
632 {
633 case POLICY_PRIORITY_FALLBACK:
634 priority <<= 1;
635 /* fall-through */
636 case POLICY_PRIORITY_ROUTED:
637 priority <<= 1;
638 /* fall-through */
639 case POLICY_PRIORITY_DEFAULT:
640 priority <<= 1;
641 /* fall-through */
642 case POLICY_PRIORITY_PASS:
643 break;
644 }
645 /* calculate priority based on selector size, small size = high prio */
646 src->to_subnet (src, &net, &mask);
647 priority -= mask;
648 proto = src->get_protocol (src);
649 port = net->get_port (net);
650 net->destroy (net);
651
652 dst->to_subnet (dst, &net, &mask);
653 priority -= mask;
654 proto = max (proto, dst->get_protocol (dst));
655 port = max (port, net->get_port (net));
656 net->destroy (net);
657
658 priority <<= 2; /* make some room for the two flags */
659 priority += port ? 0 : 2;
660 priority += proto ? 0 : 1;
661 return priority;
662}
663
664/**
665 * Get sw_if_index from interface name
666 */
667static uint32_t
668get_sw_if_index (char *interface)
669{
670 char *out = NULL;
671 int out_len, name_filter_len = 0, msg_len = 0;
672 vl_api_sw_interface_dump_t *mp;
673 vl_api_sw_interface_details_t *rmp;
674 uint32_t sw_if_index = ~0;
675
676 name_filter_len = strlen (interface);
677 msg_len = sizeof (*mp) + name_filter_len;
678 mp = vl_msg_api_alloc (msg_len);
679 clib_memset (mp, 0, msg_len);
680 u16 msg_id = vl_msg_api_get_msg_index ((u8 *) "sw_interface_dump_aa610c27");
681 mp->_vl_msg_id = htons (msg_id);
682 mp->name_filter_valid = TRUE;
683 mp->name_filter.length = htonl (name_filter_len);
684 memcpy ((char *) mp->name_filter.buf, interface, name_filter_len);
685
686 if (vac->send (vac, (char *) mp, msg_len, &out, &out_len))
687 {
688 goto error;
689 }
690 if (!out_len)
691 {
692 goto error;
693 }
694 rmp = (vl_api_sw_interface_details_t *) out;
695 sw_if_index = ntohl (rmp->sw_if_index);
696
697error:
698 free (out);
699 vl_msg_api_free (mp);
700 return sw_if_index;
701}
702/**
703 * (Un)-install a security policy database
704 */
705static status_t
706spd_add_del (bool add, uint32_t spd_id)
707{
708 char *out = NULL;
709 int out_len;
710 vl_api_ipsec_spd_add_del_t *mp;
711 vl_api_ipsec_spd_add_del_reply_t *rmp;
712 status_t rv = FAILED;
713
714 mp = vl_msg_api_alloc (sizeof (*mp));
715 memset (mp, 0, sizeof (*mp));
716
717 u16 msg_id = vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_add_del_20e89a95");
718 mp->_vl_msg_id = htons (msg_id);
719 mp->is_add = add;
720 mp->spd_id = htonl (spd_id);
721 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
722 {
723 DBG1 (DBG_KNL, "vac %s SPD failed", add ? "adding" : "removing");
724 goto error;
725 }
726 rmp = (void *) out;
727 if (rmp->retval)
728 {
729 DBG1 (DBG_KNL, "%s SPD failed rv:%d", add ? "add" : "remove",
730 ntohl (rmp->retval));
731 goto error;
732 }
733 rv = SUCCESS;
734
735error:
736 free (out);
737 vl_msg_api_free (mp);
738 return rv;
739}
740
741/**
742 * Enable or disable SPD on an insterface
743 */
744static status_t
745interface_add_del_spd (bool add, uint32_t spd_id, uint32_t sw_if_index)
746{
747 char *out = NULL;
748 int out_len;
749 vl_api_ipsec_interface_add_del_spd_t *mp;
750 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
751 status_t rv = FAILED;
752
753 mp = vl_msg_api_alloc (sizeof (*mp));
754 memset (mp, 0, sizeof (*mp));
755 u16 msg_id =
756 vl_msg_api_get_msg_index ((u8 *) "ipsec_interface_add_del_spd_80f80cbb");
757 mp->_vl_msg_id = htons (msg_id);
758 mp->is_add = add;
759 mp->spd_id = htonl (spd_id);
760 mp->sw_if_index = htonl (sw_if_index);
761 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
762 {
763 DBG1 (DBG_KNL, "vac %s interface SPD failed",
764 add ? "adding" : "removing");
765 goto error;
766 }
767 rmp = (void *) out;
768 if (rmp->retval)
769 {
770 DBG1 (DBG_KNL, "%s interface SPD failed rv:%d", add ? "add" : "remove",
771 ntohl (rmp->retval));
772 goto error;
773 }
774 rv = SUCCESS;
775
776error:
777 free (out);
778 vl_msg_api_free (mp);
779 return rv;
780}
781
782static int
783bypass_all (bool add, uint32_t spd_id, uint32_t sa_id)
784{
785 vl_api_ipsec_spd_entry_add_del_t *mp;
786 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
787 char *out = NULL;
788 int out_len;
789 status_t rv = FAILED;
790
791 DBG2 (DBG_KNL, "bypass_all [%s] spd_id %d sa_id %d", add ? "ADD" : "DEL",
792 spd_id, sa_id);
793
794 mp = vl_msg_api_alloc (sizeof (*mp));
795 memset (mp, 0, sizeof (*mp));
796
797 u16 msg_id =
798 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
799 mp->_vl_msg_id = ntohs (msg_id);
800 mp->is_add = add;
801 mp->entry.sa_id = ntohl (sa_id);
802 mp->entry.spd_id = ntohl (spd_id);
803 mp->entry.priority = ntohl (INT_MAX - POLICY_PRIORITY_PASS - 1);
804 mp->entry.is_outbound = 0;
805 mp->entry.policy = ntohl (IPSEC_API_SPD_ACTION_BYPASS);
806 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
807 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
808 mp->entry.remote_port_start = mp->entry.local_port_start = ntohs (0);
809 mp->entry.remote_port_stop = mp->entry.local_port_stop = ntohs (0xFFFF);
810 mp->entry.protocol = IP_API_PROTO_ESP;
811 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
812 {
813 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
814 goto error;
815 }
816 rmp = (void *) out;
817 if (rmp->retval)
818 {
819 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
820 ntohl (rmp->retval));
821 goto error;
822 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000823 /* address "out" needs to be freed after vec->send */
824 if (out != NULL)
825 {
826 free (out);
827 out = NULL;
828 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000829 mp->entry.is_outbound = 1;
830 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
831 {
832 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
833 goto error;
834 }
835 rmp = (void *) out;
836 if (rmp->retval)
837 {
838 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
839 ntohl (rmp->retval));
840 goto error;
841 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000842 /* address "out" needs to be freed after vec->send */
843 if (out != NULL)
844 {
845 free (out);
846 out = NULL;
847 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000848 mp->entry.is_outbound = 0;
849 mp->entry.protocol = IP_API_PROTO_AH;
850 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
851 {
852 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
853 goto error;
854 }
855 rmp = (void *) out;
856 if (rmp->retval)
857 {
858 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
859 ntohl (rmp->retval));
860 goto error;
861 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000862 /* address "out" needs to be freed after vec->send */
863 if (out != NULL)
864 {
865 free (out);
866 out = NULL;
867 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000868 mp->entry.is_outbound = 1;
869 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
870 {
871 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
872 goto error;
873 }
874 rmp = (void *) out;
875 if (rmp->retval)
876 {
877 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
878 ntohl (rmp->retval));
879 goto error;
880 }
881
882 rv = SUCCESS;
883
884error:
885 if (out)
886 free (out);
887 vl_msg_api_free (mp);
888
889 return rv;
890}
891
892static int
893bypass_port (bool add, uint32_t spd_id, uint32_t sa_id, uint16_t port)
894{
895 vl_api_ipsec_spd_entry_add_del_t *mp;
896 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
897 char *out = NULL;
898 int out_len;
899 status_t rv = FAILED;
900
901 mp = vl_msg_api_alloc (sizeof (*mp));
902 memset (mp, 0, sizeof (*mp));
903
904 u16 msg_id =
905 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
906 mp->_vl_msg_id = ntohs (msg_id);
907 mp->is_add = add;
908 mp->entry.sa_id = ntohl (sa_id);
909 mp->entry.spd_id = ntohl (spd_id);
910 mp->entry.priority = ntohl (INT_MAX - POLICY_PRIORITY_PASS - 1);
911 mp->entry.policy = ntohl (IPSEC_API_SPD_ACTION_BYPASS);
912 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
913 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
914 mp->entry.is_outbound = 0;
915 mp->entry.remote_port_start = mp->entry.local_port_start = ntohs (0);
916 mp->entry.remote_port_stop = mp->entry.local_port_stop = ntohs (0xFFFF);
917 mp->entry.protocol = IP_API_PROTO_HOPOPT;
918
919 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
920 {
921 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
922 goto error;
923 }
924 rmp = (void *) out;
925 if (rmp->retval)
926 {
927 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
928 ntohl (rmp->retval));
929 goto error;
930 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +0000931 /* address "out" needs to be freed after vec->send */
932 if (out != NULL)
933 {
934 free (out);
935 out = NULL;
936 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +0000937 mp->entry.is_outbound = 1;
938 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
939 {
940 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
941 goto error;
942 }
943 rmp = (void *) out;
944 if (rmp->retval)
945 {
946 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
947 ntohl (rmp->retval));
948 goto error;
949 }
950 rv = SUCCESS;
951
952error:
953 if (out)
954 free (out);
955 vl_msg_api_free (mp);
956
957 return rv;
958}
959
960/**
961 * Add or remove a bypass policy
962 */
963static status_t
964manage_bypass (bool add, uint32_t spd_id, uint32_t sa_id)
965{
966 uint16_t port;
967 status_t rv;
968
969 bypass_all (add, spd_id, sa_id);
970
971 port =
972 lib->settings->get_int (lib->settings, "%s.port", IKEV2_UDP_PORT, lib->ns);
973
974 if (port)
975 {
976 rv = bypass_port (add, spd_id, sa_id, port);
977 if (rv != SUCCESS)
978 {
979 return rv;
980 }
981 }
982
983 port = lib->settings->get_int (lib->settings, "%s.port_nat_t",
984 IKEV2_NATT_PORT, lib->ns);
985 if (port)
986 {
987 rv = bypass_port (add, spd_id, sa_id, port);
988 if (rv != SUCCESS)
989 {
990 return rv;
991 }
992 }
993
994 return SUCCESS;
995}
996
997/**
998 * Add or remove a policy
999 */
1000static status_t
1001manage_policy (private_kernel_vpp_ipsec_t *this, bool add,
1002 kernel_ipsec_policy_id_t *id,
1003 kernel_ipsec_manage_policy_t *data)
1004{
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001005 spd_t *spd = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001006 char *out = NULL, *interface = NULL;
1007 int out_len;
1008 uint32_t sw_if_index, spd_id = ~0, sad_id = ~0;
1009 status_t rv = FAILED;
1010 uint32_t priority, auto_priority;
1011 chunk_t src_from, src_to, dst_from, dst_to;
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001012 host_t *src = NULL, *dst = NULL, *addr = NULL;
1013 vl_api_ipsec_spd_entry_add_del_t *mp = NULL;
1014 vl_api_ipsec_spd_entry_add_del_reply_t *rmp = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001015 bool n_spd = false;
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001016 vl_api_ipsec_spd_dump_t *mp_dump = NULL;
1017 vl_api_ipsec_spd_details_t *rmp_dump = NULL, *tmp = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001018
1019 mp = vl_msg_api_alloc (sizeof (*mp));
1020 memset (mp, 0, sizeof (*mp));
1021
1022 this->mutex->lock (this->mutex);
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001023 if (id->dir == POLICY_FWD)
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001024 {
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001025 DBG1 (DBG_KNL, "policy FWD interface");
1026 rv = SUCCESS;
1027 goto error;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001028 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001029 addr = id->dir == POLICY_IN ? data->dst : data->src;
1030 for (int i = 0; i < N_RETRY_GET_IF; i++)
1031 {
1032 if (!charon->kernel->get_interface (charon->kernel, addr, &interface))
1033 {
1034 DBG1 (DBG_KNL, "policy no interface %H", addr);
1035 free (interface);
1036 interface = NULL;
1037 sleep (1);
1038 }
1039
1040 if (interface)
1041 {
1042 DBG1 (DBG_KNL, "policy have interface %H", addr);
1043 break;
1044 }
1045 }
1046 if (!interface)
1047 goto error;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001048
1049 DBG2 (DBG_KNL, "manage policy [%s] interface [%s]", add ? "ADD" : "DEL",
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001050 interface);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001051
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001052 spd = this->spds->get (this->spds, interface);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001053 if (!spd)
1054 {
1055 if (!add)
1056 {
1057 DBG1 (DBG_KNL, "SPD for %s not found, should not be deleted",
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001058 interface);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001059 goto error;
1060 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001061 sw_if_index = get_sw_if_index (interface);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001062 DBG1 (DBG_KNL, "firstly created, spd for %s found sw_if_index is %d",
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001063 interface, sw_if_index);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001064 if (sw_if_index == ~0)
1065 {
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001066 DBG1 (DBG_KNL, "sw_if_index for %s not found", interface);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001067 goto error;
1068 }
1069 spd_id = ref_get (&this->next_spd_id);
1070 if (spd_add_del (TRUE, spd_id))
1071 {
1072 DBG1 (DBG_KNL, "spd_add_del %d failed!!!!!", spd_id);
1073 goto error;
1074 }
1075 if (interface_add_del_spd (TRUE, spd_id, sw_if_index))
1076 {
1077 DBG1 (DBG_KNL, "interface_add_del_spd %d %d failed!!!!!", spd_id,
1078 sw_if_index);
1079 goto error;
1080 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001081 INIT (spd, .spd_id = spd_id, .sw_if_index = sw_if_index, .policy_num = 0,
1082 .if_name = strdup (interface), );
1083 this->spds->put (this->spds, spd->if_name, spd);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001084 n_spd = true;
1085 }
1086
1087 auto_priority = calculate_priority (data->prio, id->src_ts, id->dst_ts);
1088 priority = data->manual_prio ? data->manual_prio : auto_priority;
1089
1090 u16 msg_id =
1091 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
1092 mp->_vl_msg_id = htons (msg_id);
1093 mp->is_add = add;
1094 mp->entry.spd_id = htonl (spd->spd_id);
1095 mp->entry.priority = htonl (INT_MAX - POLICY_PRIORITY_PASS);
1096 mp->entry.is_outbound = id->dir == POLICY_OUT;
1097 switch (data->type)
1098 {
1099 case POLICY_IPSEC:
1100 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_PROTECT);
1101 break;
1102 case POLICY_PASS:
1103 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_BYPASS);
1104 break;
1105 case POLICY_DROP:
1106 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_DISCARD);
1107 break;
1108 }
1109 if ((data->type == POLICY_IPSEC) && data->sa)
1110 {
1111 kernel_ipsec_sa_id_t id = {
1112 .src = data->src,
1113 .dst = data->dst,
1114 .proto = data->sa->esp.use ? IPPROTO_ESP : IPPROTO_AH,
1115 .spi = data->sa->esp.use ? data->sa->esp.spi : data->sa->ah.spi,
1116 };
1117 sa_t *sa = NULL;
1118 sa = this->sas->get (this->sas, &id);
1119 if (!sa)
1120 {
1121 DBG1 (DBG_KNL, "SA ID not found");
1122 goto error;
1123 }
1124 sad_id = sa->sa_id;
1125 if (n_spd)
1126 {
1127 if (manage_bypass (TRUE, spd_id, ~0))
1128 {
1129 DBG1 (DBG_KNL, "manage_bypass %d failed!!!!", spd_id);
1130 goto error;
1131 }
1132 }
1133 }
1134
1135 mp->entry.sa_id = htonl (sad_id);
1136
1137 bool is_ipv6 = false;
1138 if (id->src_ts->get_type (id->src_ts) == TS_IPV6_ADDR_RANGE)
1139 {
1140 is_ipv6 = true;
1141 mp->entry.local_address_start.af = htonl (ADDRESS_IP6);
1142 mp->entry.local_address_stop.af = htonl (ADDRESS_IP6);
1143 mp->entry.remote_address_start.af = htonl (ADDRESS_IP6);
1144 mp->entry.remote_address_stop.af = htonl (ADDRESS_IP6);
1145 }
1146 else
1147 {
1148 mp->entry.local_address_start.af = htonl (ADDRESS_IP4);
1149 mp->entry.local_address_stop.af = htonl (ADDRESS_IP4);
1150 mp->entry.remote_address_start.af = htonl (ADDRESS_IP4);
1151 mp->entry.remote_address_stop.af = htonl (ADDRESS_IP4);
1152 }
1153 mp->entry.protocol = id->src_ts->get_protocol (id->src_ts);
1154
1155 if (id->dir == POLICY_OUT)
1156 {
1157 src_from = id->src_ts->get_from_address (id->src_ts);
1158 src_to = id->src_ts->get_to_address (id->src_ts);
1159 src = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, src_to, 0);
1160 dst_from = id->dst_ts->get_from_address (id->dst_ts);
1161 dst_to = id->dst_ts->get_to_address (id->dst_ts);
1162 dst = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, dst_to, 0);
1163 }
1164 else
1165 {
1166 dst_from = id->src_ts->get_from_address (id->src_ts);
1167 dst_to = id->src_ts->get_to_address (id->src_ts);
1168 dst = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, dst_from, 0);
1169 src_from = id->dst_ts->get_from_address (id->dst_ts);
1170 src_to = id->dst_ts->get_to_address (id->dst_ts);
1171 src = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, src_from, 0);
1172 }
1173
1174 if (src->is_anyaddr (src) && dst->is_anyaddr (dst))
1175 {
1176 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
1177 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
1178 }
1179 else
1180 {
1181 memcpy (is_ipv6 ? mp->entry.local_address_start.un.ip6 :
1182 mp->entry.local_address_start.un.ip4,
1183 src_from.ptr, src_from.len);
1184 memcpy (is_ipv6 ? mp->entry.local_address_stop.un.ip6 :
1185 mp->entry.local_address_stop.un.ip4,
1186 src_to.ptr, src_to.len);
1187 memcpy (is_ipv6 ? mp->entry.remote_address_start.un.ip6 :
1188 mp->entry.remote_address_start.un.ip4,
1189 dst_from.ptr, dst_from.len);
1190 memcpy (is_ipv6 ? mp->entry.remote_address_stop.un.ip6 :
1191 mp->entry.remote_address_stop.un.ip4,
1192 dst_to.ptr, dst_to.len);
1193 }
1194 mp->entry.local_port_start = htons (id->src_ts->get_from_port (id->src_ts));
1195 mp->entry.local_port_stop = htons (id->src_ts->get_to_port (id->src_ts));
1196 mp->entry.remote_port_start = htons (id->dst_ts->get_from_port (id->dst_ts));
1197 mp->entry.remote_port_stop = htons (id->dst_ts->get_to_port (id->dst_ts));
1198
Gabriel Oginski8de66c02023-02-03 08:12:36 +00001199 /* check if policy exists in SPD */
Gabriel Oginski8de66c02023-02-03 08:12:36 +00001200 mp_dump = vl_msg_api_alloc (sizeof (*mp_dump));
1201 memset (mp_dump, 0, sizeof (*mp_dump));
1202
1203 msg_id = vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_dump_afefbf7d");
1204 mp_dump->_vl_msg_id = htons (msg_id);
1205 mp_dump->spd_id = htonl (spd->spd_id);
1206 mp_dump->sa_id = htonl (sad_id);
1207
1208 if (vac->send_dump (vac, (char *) mp_dump, sizeof (*mp_dump), &out,
1209 &out_len))
1210 {
1211 DBG1 (DBG_KNL, "vac %s SPD lookup failed", add ? "adding" : "removing");
1212 goto error;
1213 }
1214
1215 int num = out_len / sizeof (*rmp_dump);
1216 tmp = (void *) out;
1217
1218 /* found existing policy */
1219 if (add && num)
1220 {
1221 int i;
1222 for (i = 0; i < num; i++)
1223 {
1224 rmp_dump = tmp;
1225 tmp += 1;
1226 /* check if found entry equals the new one */
1227 if (policy_equals (&mp->entry, &rmp_dump->entry))
1228 goto next;
1229 }
1230 }
1231 else if (!add && num == 0)
1232 {
1233 /* VPP doesn't have any policy to delete */
1234 goto next;
1235 }
1236
1237 free (out);
1238
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001239 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1240 {
1241 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
1242 goto error;
1243 }
1244 rmp = (void *) out;
1245 if (rmp->retval)
1246 {
1247 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
1248 ntohl (rmp->retval));
1249 goto error;
1250 }
1251
Gabriel Oginski8de66c02023-02-03 08:12:36 +00001252next:
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001253 if (add)
1254 {
1255 ref_get (&spd->policy_num);
1256 }
1257 else
1258 {
1259 if (ref_put (&spd->policy_num))
1260 {
1261 DBG1 (
1262 DBG_KNL,
1263 "policy_num's ref is 0, delete spd_id %d sw_if_index %d sad_id %x",
1264 spd->spd_id, spd->sw_if_index, sad_id);
1265 interface_add_del_spd (FALSE, spd->spd_id, spd->sw_if_index);
1266 manage_bypass (FALSE, spd->spd_id, sad_id);
1267 spd_add_del (FALSE, spd->spd_id);
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001268 this->spds->remove (this->spds, interface);
1269 if (spd->if_name)
1270 {
1271 free (spd->if_name);
1272 spd->if_name = NULL;
1273 }
1274 if (spd)
1275 {
1276 free (spd);
1277 spd = NULL;
1278 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001279 }
1280 }
1281
1282 if (this->install_routes && id->dir == POLICY_OUT && !mp->entry.protocol)
1283 {
1284 if (data->type == POLICY_IPSEC && data->sa->mode != MODE_TRANSPORT)
1285 {
1286 manage_route (this, add, id->dst_ts, data->src, data->dst);
1287 }
1288 }
1289 rv = SUCCESS;
1290error:
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001291 if (out != NULL)
1292 free (out);
1293 if (mp_dump != NULL)
1294 vl_msg_api_free (mp_dump);
1295 if (mp != NULL)
1296 vl_msg_api_free (mp);
1297 if (src != NULL)
1298 src->destroy (src);
1299 if (dst != NULL)
1300 dst->destroy (dst);
1301 if (interface != NULL)
1302 free (interface);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001303 this->mutex->unlock (this->mutex);
1304 return rv;
1305}
1306
1307METHOD (kernel_ipsec_t, get_features, kernel_feature_t,
1308 private_kernel_vpp_ipsec_t *this)
1309{
1310 return KERNEL_ESP_V3_TFC;
1311}
1312
1313METHOD (kernel_ipsec_t, get_spi, status_t, private_kernel_vpp_ipsec_t *this,
1314 host_t *src, host_t *dst, uint8_t protocol, uint32_t *spi)
1315{
1316 static const u_int p = 268435399, offset = 0xc0000000;
1317
1318 *spi = htonl (offset + permute (ref_get (&this->nextspi) ^ this->mixspi, p));
1319 return SUCCESS;
1320}
1321
1322METHOD (kernel_ipsec_t, get_cpi, status_t, private_kernel_vpp_ipsec_t *this,
1323 host_t *src, host_t *dst, uint16_t *cpi)
1324{
1325 DBG1 (DBG_KNL, "get_cpi is not supported!!!!!!!!!!!!!!!!!!!!!!!!");
1326 return NOT_SUPPORTED;
1327}
1328
1329/**
1330 * Helper struct for expiration events
1331 */
1332typedef struct
1333{
1334
1335 private_kernel_vpp_ipsec_t *manager;
1336
1337 kernel_ipsec_sa_id_t *sa_id;
1338
1339 /**
1340 * 0 if this is a hard expire, otherwise the offset in s (soft->hard)
1341 */
1342 uint32_t hard_offset;
1343
1344} vpp_sa_expired_t;
1345
1346/**
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001347 * Clean up expire data
1348 */
1349static void
1350expire_data_destroy (vpp_sa_expired_t *data)
1351{
1352 free (data);
1353}
1354
1355/**
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001356 * Callback for expiration events
1357 */
1358static job_requeue_t
1359sa_expired (vpp_sa_expired_t *expired)
1360{
1361 private_kernel_vpp_ipsec_t *this = expired->manager;
1362 sa_t *sa;
1363 kernel_ipsec_sa_id_t *id = expired->sa_id;
1364
1365 this->mutex->lock (this->mutex);
1366 sa = this->sas->get (this->sas, id);
1367
1368 if (sa)
1369 {
1370 charon->kernel->expire (charon->kernel, id->proto, id->spi, id->dst,
1371 FALSE);
1372 }
1373
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001374 if (id->src)
1375 id->src->destroy (id->src);
1376 if (id->dst)
1377 id->dst->destroy (id->dst);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001378 free (id);
1379 this->mutex->unlock (this->mutex);
1380 return JOB_REQUEUE_NONE;
1381}
1382
1383/**
1384 * Schedule a job to handle IPsec SA expiration
1385 */
1386static void
1387schedule_expiration (private_kernel_vpp_ipsec_t *this,
1388 kernel_ipsec_add_sa_t *entry,
1389 kernel_ipsec_sa_id_t *entry2)
1390{
1391 lifetime_cfg_t *lifetime = entry->lifetime;
1392 vpp_sa_expired_t *expired;
1393 callback_job_t *job;
1394 uint32_t timeout;
1395 kernel_ipsec_sa_id_t *id;
1396
1397 if (!lifetime->time.life)
1398 { /* no expiration at all */
1399 return;
1400 }
1401
1402 INIT (id, .src = entry2->src->clone (entry2->src),
1403 .dst = entry2->dst->clone (entry2->dst), .spi = entry2->spi,
1404 .proto = entry2->proto, );
1405
1406 INIT (expired, .manager = this, .sa_id = id, );
1407
1408 /* schedule a rekey first, a hard timeout will be scheduled then, if any */
1409 expired->hard_offset = lifetime->time.life - lifetime->time.rekey;
1410 timeout = lifetime->time.rekey;
1411
1412 if (lifetime->time.life <= lifetime->time.rekey || lifetime->time.rekey == 0)
1413 { /* no rekey, schedule hard timeout */
1414 expired->hard_offset = 0;
1415 timeout = lifetime->time.life;
1416 }
1417
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001418 job =
1419 callback_job_create ((callback_job_cb_t) sa_expired, expired,
1420 (callback_job_cleanup_t) expire_data_destroy, NULL);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001421 lib->scheduler->schedule_job (lib->scheduler, (job_t *) job, timeout);
1422}
1423
1424METHOD (kernel_ipsec_t, add_sa, status_t, private_kernel_vpp_ipsec_t *this,
1425 kernel_ipsec_sa_id_t *id, kernel_ipsec_add_sa_t *data)
1426{
1427 char *out = NULL;
1428 int out_len;
1429 vl_api_ipsec_sad_entry_add_del_t *mp;
1430 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
1431 uint32_t sad_id = ref_get (&this->next_sad_id);
1432 uint8_t ca = 0, ia = 0;
1433 status_t rv = FAILED;
1434 chunk_t src, dst;
1435 kernel_ipsec_sa_id_t *sa_id;
1436 sa_t *sa;
1437 int key_len = data->enc_key.len;
1438
1439 if ((data->enc_alg == ENCR_AES_CTR) ||
1440 (data->enc_alg == ENCR_AES_GCM_ICV8) ||
1441 (data->enc_alg == ENCR_AES_GCM_ICV12) ||
1442 (data->enc_alg == ENCR_AES_GCM_ICV16))
1443 {
1444 static const int SALT_SIZE =
1445 4; /* See how enc_size is calculated at keymat_v2.derive_child_keys */
1446 key_len = key_len - SALT_SIZE;
1447 }
1448 natt_port = lib->settings->get_int (
1449 lib->settings, "%s.plugins.socket-default.natt", IKEV2_NATT_PORT, lib->ns);
1450 mp = vl_msg_api_alloc (sizeof (*mp));
1451 memset (mp, 0, sizeof (*mp));
1452 u16 msg_id =
1453 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1454 mp->_vl_msg_id = htons (msg_id);
1455 mp->is_add = 1;
1456 mp->entry.sad_id = htonl (sad_id);
1457 mp->entry.spi = id->spi;
1458 mp->entry.protocol = id->proto == IPPROTO_ESP ? htonl (IPSEC_API_PROTO_ESP) :
1459 htonl (IPSEC_API_PROTO_AH);
1460
1461 switch (data->enc_alg)
1462 {
1463 case ENCR_NULL:
1464 ca = IPSEC_API_CRYPTO_ALG_NONE;
1465 break;
1466 case ENCR_AES_CBC:
1467 switch (key_len * 8)
1468 {
1469 case 128:
1470 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_128;
1471 break;
1472 case 192:
1473 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_192;
1474 break;
1475 case 256:
1476 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_256;
1477 break;
1478 default:
1479 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1480 key_len * 8);
1481 goto error;
1482 }
1483 break;
1484 case ENCR_AES_CTR:
1485 switch (key_len * 8)
1486 {
1487 case 128:
1488 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_128;
1489 break;
1490 case 192:
1491 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_192;
1492 break;
1493 case 256:
1494 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_256;
1495 break;
1496 default:
1497 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1498 key_len * 8);
1499 goto error;
1500 }
1501 break;
1502 case ENCR_AES_GCM_ICV8:
1503 case ENCR_AES_GCM_ICV12:
1504 case ENCR_AES_GCM_ICV16:
1505 switch (key_len * 8)
1506 {
1507 case 128:
1508 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_128;
1509 break;
1510 case 192:
1511 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_192;
1512 break;
1513 case 256:
1514 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_256;
1515 break;
1516 default:
1517 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1518 key_len * 8);
1519 goto error;
1520 }
1521 break;
1522 case ENCR_DES:
1523 ca = IPSEC_API_CRYPTO_ALG_DES_CBC;
1524 break;
1525 case ENCR_3DES:
1526 ca = IPSEC_API_CRYPTO_ALG_3DES_CBC;
1527 break;
1528 default:
1529 DBG1 (DBG_KNL, "algorithm %N not supported by VPP!",
1530 encryption_algorithm_names, data->enc_alg);
1531 goto error;
1532 }
1533 mp->entry.crypto_algorithm = htonl (ca);
1534 mp->entry.crypto_key.length = key_len < 128 ? key_len : 128;
1535 memcpy (mp->entry.crypto_key.data, data->enc_key.ptr,
1536 mp->entry.crypto_key.length);
1537
1538 /* copy salt for AEAD algorithms */
1539 if ((data->enc_alg == ENCR_AES_CTR) ||
1540 (data->enc_alg == ENCR_AES_GCM_ICV8) ||
1541 (data->enc_alg == ENCR_AES_GCM_ICV12) ||
1542 (data->enc_alg == ENCR_AES_GCM_ICV16))
1543 {
1544 memcpy (&mp->entry.salt, data->enc_key.ptr + mp->entry.crypto_key.length,
1545 4);
1546 }
1547
1548 switch (data->int_alg)
1549 {
1550 case AUTH_UNDEFINED:
1551 ia = IPSEC_API_INTEG_ALG_NONE;
1552 break;
1553 case AUTH_HMAC_MD5_96:
1554 ia = IPSEC_API_INTEG_ALG_MD5_96;
1555 break;
1556 case AUTH_HMAC_SHA1_96:
1557 ia = IPSEC_API_INTEG_ALG_SHA1_96;
1558 break;
1559 case AUTH_HMAC_SHA2_256_96:
1560 ia = IPSEC_API_INTEG_ALG_SHA_256_96;
1561 break;
1562 case AUTH_HMAC_SHA2_256_128:
1563 ia = IPSEC_API_INTEG_ALG_SHA_256_128;
1564 break;
1565 case AUTH_HMAC_SHA2_384_192:
1566 ia = IPSEC_API_INTEG_ALG_SHA_384_192;
1567 break;
1568 case AUTH_HMAC_SHA2_512_256:
1569 ia = IPSEC_API_INTEG_ALG_SHA_512_256;
1570 break;
1571 default:
1572 DBG1 (DBG_KNL, "algorithm %N not supported by VPP!",
1573 integrity_algorithm_names, data->int_alg);
1574 goto error;
1575 break;
1576 }
1577 mp->entry.integrity_algorithm = htonl (ia);
1578 mp->entry.integrity_key.length =
1579 data->int_key.len < 128 ? data->int_key.len : 128;
1580 memcpy (mp->entry.integrity_key.data, data->int_key.ptr,
1581 mp->entry.integrity_key.length);
1582
1583 int flags = IPSEC_API_SAD_FLAG_NONE;
Atzm Watanabefc2d95d2023-01-25 14:11:10 +09001584 if (data->inbound)
1585 flags |= IPSEC_API_SAD_FLAG_IS_INBOUND;
1586 /* like the kernel-netlink plugin, anti-replay can be disabled with zero
1587 * replay_window, but window size cannot be customized for vpp */
1588 if (data->replay_window)
1589 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001590 if (data->esn)
1591 flags |= IPSEC_API_SAD_FLAG_USE_ESN;
Atzm Watanabefc2d95d2023-01-25 14:11:10 +09001592 if (this->use_tunnel_mode_sa && data->mode == MODE_TUNNEL)
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001593 {
1594 if (id->src->get_family (id->src) == AF_INET6)
1595 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
1596 else
1597 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
1598 }
1599 if (data->encap)
1600 {
1601 DBG1 (DBG_KNL, "UDP encap!!!!!!!!!!!!!!!!!!!!");
1602 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
1603 }
1604 mp->entry.flags = htonl (flags);
1605
1606 bool is_ipv6 = false;
1607 if (id->src->get_family (id->src) == AF_INET6)
1608 {
1609 is_ipv6 = true;
1610 mp->entry.tunnel_src.af = htonl (ADDRESS_IP6);
1611 mp->entry.tunnel_dst.af = htonl (ADDRESS_IP6);
1612 }
1613 else
1614 {
1615 mp->entry.tunnel_src.af = htonl (ADDRESS_IP4);
1616 mp->entry.tunnel_dst.af = htonl (ADDRESS_IP4);
1617 }
1618 src = id->src->get_address (id->src);
1619 memcpy (is_ipv6 ? mp->entry.tunnel_src.un.ip6 : mp->entry.tunnel_src.un.ip4,
1620 src.ptr, src.len);
1621 dst = id->dst->get_address (id->dst);
1622 memcpy (is_ipv6 ? mp->entry.tunnel_dst.un.ip6 : mp->entry.tunnel_dst.un.ip4,
1623 dst.ptr, dst.len);
1624 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1625 {
1626 DBG1 (DBG_KNL, "vac adding SA failed");
1627 goto error;
1628 }
1629 rmp = (void *) out;
1630 if (rmp->retval)
1631 {
1632 DBG1 (DBG_KNL, "add SA failed rv:%d", ntohl (rmp->retval));
1633 goto error;
1634 }
1635
1636 this->mutex->lock (this->mutex);
1637 INIT (sa_id, .src = id->src->clone (id->src),
1638 .dst = id->dst->clone (id->dst), .spi = id->spi, .proto = id->proto, );
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001639 INIT (sa, .sa_id = sad_id, .stat_index = ntohl (rmp->stat_index),
1640 .sa_id_p = sa_id, );
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001641 DBG4 (DBG_KNL, "put sa by its sa_id %x !!!!!!", sad_id);
1642 this->sas->put (this->sas, sa_id, sa);
1643 schedule_expiration (this, data, id);
1644 this->mutex->unlock (this->mutex);
1645 rv = SUCCESS;
1646
1647error:
1648 free (out);
1649 vl_msg_api_free (mp);
1650 return rv;
1651}
1652
1653METHOD (kernel_ipsec_t, update_sa, status_t, private_kernel_vpp_ipsec_t *this,
1654 kernel_ipsec_sa_id_t *id, kernel_ipsec_update_sa_t *data)
1655{
1656 DBG1 (DBG_KNL,
1657 "update sa not supported!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
1658 return NOT_SUPPORTED;
1659}
1660
1661METHOD (kernel_ipsec_t, query_sa, status_t, private_kernel_vpp_ipsec_t *this,
1662 kernel_ipsec_sa_id_t *id, kernel_ipsec_query_sa_t *data,
1663 uint64_t *bytes, uint64_t *packets, time_t *time)
1664{
1665 status_t rv = FAILED;
1666 sa_t *sa;
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001667 u32 *dir = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001668 int i, k;
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001669 stat_segment_data_t *res = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001670 u8 **pattern = 0;
1671 uint64_t res_bytes = 0;
1672 uint64_t res_packets = 0;
1673
1674 this->mutex->lock (this->mutex);
1675 sa = this->sas->get (this->sas, id);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001676 if (!sa)
1677 {
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001678 this->mutex->unlock (this->mutex);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001679 DBG1 (DBG_KNL, "SA not found");
1680 return NOT_FOUND;
1681 }
1682
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001683 if (this->sm == NULL)
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001684 {
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001685 stat_client_main_t *sm = NULL;
1686 sm = stat_client_get ();
1687
1688 if (!sm)
1689 {
1690 DBG1 (DBG_KNL, "Not connecting with stats segmentation");
1691 this->mutex->unlock (this->mutex);
1692 return NOT_FOUND;
1693 }
1694 this->sm = sm;
1695 int rv_stat = stat_segment_connect_r ("/run/vpp/stats.sock", this->sm);
1696 if (rv_stat != 0)
1697 {
1698 stat_client_free (this->sm);
1699 this->sm = NULL;
1700 DBG1 (DBG_KNL, "Not connecting with stats segmentation");
1701 this->mutex->unlock (this->mutex);
1702 return NOT_FOUND;
1703 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001704 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001705
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001706 vec_add1 (pattern, (u8 *) "/net/ipsec/sa");
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001707 dir = stat_segment_ls_r ((u8 **) pattern, this->sm);
1708 res = stat_segment_dump_r (dir, this->sm);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001709 /* i-loop for each results find by pattern - here two:
1710 * 1. /net/ipsec/sa
1711 * 2. /net/ipsec/sa/lost
1712 */
1713 for (i = 0; i < vec_len (res); i++)
1714 {
1715 switch (res[i].type)
1716 {
1717 /* type for how many packets are lost */
1718 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
1719 if (res[i].simple_counter_vec == 0)
1720 continue;
1721 break;
1722 /* type for counter for each SA */
1723 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
1724 if (res[i].combined_counter_vec == 0)
1725 continue;
1726 /* k-loop for each threads - that you run VPP */
1727 for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
1728 {
1729 if (sa->stat_index <= vec_len (res[i].combined_counter_vec[k]))
1730 {
1731 DBG4 (DBG_KNL, "Thread: %d, Packets: %lu, Bytes: %lu", k,
1732 res[i].combined_counter_vec[k][sa->stat_index].packets,
1733 res[i].combined_counter_vec[k][sa->stat_index].bytes);
1734 res_bytes +=
1735 res[i].combined_counter_vec[k][sa->stat_index].bytes;
1736 res_packets +=
1737 res[i].combined_counter_vec[k][sa->stat_index].packets;
1738 }
1739 }
1740 break;
1741 case STAT_DIR_TYPE_NAME_VECTOR:
1742 if (res[i].name_vector == 0)
1743 continue;
1744 break;
1745 }
1746 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001747
1748 vec_free (pattern);
1749 vec_free (dir);
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001750 stat_segment_data_free (res);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001751
1752 if (bytes)
1753 {
1754 *bytes = res_bytes;
1755 }
1756 if (packets)
1757 {
1758 *packets = res_packets;
1759 }
1760 if (time)
1761 {
1762 *time = 0;
1763 }
1764
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001765 this->mutex->unlock (this->mutex);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001766 rv = SUCCESS;
1767 return rv;
1768}
1769
1770METHOD (kernel_ipsec_t, del_sa, status_t, private_kernel_vpp_ipsec_t *this,
1771 kernel_ipsec_sa_id_t *id, kernel_ipsec_del_sa_t *data)
1772{
1773 char *out = NULL;
1774 int out_len;
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001775 vl_api_ipsec_sad_entry_add_del_t *mp = NULL;
1776 vl_api_ipsec_sad_entry_add_del_reply_t *rmp = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001777 status_t rv = FAILED;
1778 sa_t *sa;
1779
1780 this->mutex->lock (this->mutex);
1781 sa = this->sas->get (this->sas, id);
1782 if (!sa)
1783 {
1784 DBG1 (DBG_KNL, "SA not found");
1785 rv = NOT_FOUND;
1786 goto error;
1787 }
1788 mp = vl_msg_api_alloc (sizeof (*mp));
1789 memset (mp, 0, sizeof (*mp));
1790 mp->is_add = 0;
1791 u16 msg_id =
1792 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1793 mp->_vl_msg_id = htons (msg_id);
1794 mp->entry.sad_id = htonl (sa->sa_id);
1795
1796 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1797 {
1798 DBG1 (DBG_KNL, "vac removing SA failed");
1799 goto error;
1800 }
1801 rmp = (void *) out;
1802 if (rmp->retval)
1803 {
1804 DBG1 (DBG_KNL, "del SA failed rv:%d", ntohl (rmp->retval));
1805 goto error;
1806 }
1807
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001808 void *temp = this->sas->remove (this->sas, id);
1809 if (sa->sa_id_p)
1810 {
1811 if (sa->sa_id_p->src)
1812 sa->sa_id_p->src->destroy (sa->sa_id_p->src);
1813 if (sa->sa_id_p->dst)
1814 sa->sa_id_p->dst->destroy (sa->sa_id_p->dst);
1815 free (sa->sa_id_p);
1816 }
1817 free (sa);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001818 rv = SUCCESS;
1819error:
1820 free (out);
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001821 vl_msg_api_free (mp);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001822 this->mutex->unlock (this->mutex);
1823 return rv;
1824}
1825
1826METHOD (kernel_ipsec_t, flush_sas, status_t, private_kernel_vpp_ipsec_t *this)
1827{
1828 enumerator_t *enumerator;
1829 int out_len;
1830 char *out;
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001831 vl_api_ipsec_sad_entry_add_del_t *mp = NULL;
1832 vl_api_ipsec_sad_entry_add_del_reply_t *rmp = NULL;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001833 sa_t *sa = NULL;
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001834 status_t rv = FAILED;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001835
1836 this->mutex->lock (this->mutex);
1837 enumerator = this->sas->create_enumerator (this->sas);
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001838 while (enumerator->enumerate (enumerator, &sa))
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001839 {
1840 mp = vl_msg_api_alloc (sizeof (*mp));
1841 memset (mp, 0, sizeof (*mp));
1842 u16 msg_id =
1843 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1844 mp->_vl_msg_id = htons (msg_id);
1845 mp->entry.sad_id = htonl (sa->sa_id);
1846 mp->is_add = 0;
1847 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1848 {
1849 DBG1 (DBG_KNL, "flush_sas failed!!!!");
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001850 goto error;
1851 }
1852 rmp = (void *) out;
1853 if (rmp->retval)
1854 {
1855 DBG1 (DBG_KNL, "flush_sas failed!!!! rv: %d", ntohl (rmp->retval));
1856 goto error;
1857 }
1858 if (sa->sa_id_p)
1859 {
1860 if (sa->sa_id_p->src)
1861 sa->sa_id_p->src->destroy (sa->sa_id_p->src);
1862 if (sa->sa_id_p->dst)
1863 sa->sa_id_p->dst->destroy (sa->sa_id_p->dst);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001864 }
1865 free (out);
1866 vl_msg_api_free (mp);
1867 this->sas->remove_at (this->sas, enumerator);
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001868 free (sa->sa_id_p);
1869 free (sa);
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001870 }
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001871 rv = SUCCESS;
1872error:
1873 if (out != NULL)
1874 free (out);
1875 if (mp != NULL)
1876 vl_msg_api_free (mp);
1877
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001878 enumerator->destroy (enumerator);
1879 this->mutex->unlock (this->mutex);
1880
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001881 return rv;
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001882}
1883
1884METHOD (kernel_ipsec_t, add_policy, status_t, private_kernel_vpp_ipsec_t *this,
1885 kernel_ipsec_policy_id_t *id, kernel_ipsec_manage_policy_t *data)
1886{
1887 return manage_policy (this, TRUE, id, data);
1888}
1889
1890METHOD (kernel_ipsec_t, query_policy, status_t,
1891 private_kernel_vpp_ipsec_t *this, kernel_ipsec_policy_id_t *id,
1892 kernel_ipsec_query_policy_t *data, time_t *use_time)
1893{
1894 return NOT_SUPPORTED;
1895}
1896
1897METHOD (kernel_ipsec_t, del_policy, status_t, private_kernel_vpp_ipsec_t *this,
1898 kernel_ipsec_policy_id_t *id, kernel_ipsec_manage_policy_t *data)
1899{
1900 return manage_policy (this, FALSE, id, data);
1901}
1902
1903METHOD (kernel_ipsec_t, flush_policies, status_t,
1904 private_kernel_vpp_ipsec_t *this)
1905{
1906 return NOT_SUPPORTED;
1907}
1908
1909METHOD (kernel_ipsec_t, bypass_socket, bool, private_kernel_vpp_ipsec_t *this,
1910 int fd, int family)
1911{
1912 return FALSE;
1913}
1914
1915METHOD (kernel_ipsec_t, enable_udp_decap, bool,
1916 private_kernel_vpp_ipsec_t *this, int fd, int family, u_int16_t port)
1917{
1918 DBG1 (DBG_KNL, "enable_udp_decap not supported!!!!!!!!!!!!!!!!!!!!!!!!!");
1919 return FALSE;
1920}
1921
1922METHOD (kernel_ipsec_t, destroy, void, private_kernel_vpp_ipsec_t *this)
1923{
1924 this->mutex->destroy (this->mutex);
1925 this->sas->destroy (this->sas);
1926 this->spds->destroy (this->spds);
1927 this->routes->destroy (this->routes);
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001928 if (this->sm)
1929 {
1930 stat_segment_disconnect_r (this->sm);
1931 stat_client_free (this->sm);
1932 this->sm = NULL;
1933 }
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001934 free (this);
1935}
1936
1937kernel_vpp_ipsec_t *
1938kernel_vpp_ipsec_create ()
1939{
1940 private_kernel_vpp_ipsec_t *this;
1941
1942 INIT(this,
1943 .public = {
1944 .interface = {
1945 .get_features = _get_features,
1946 .get_spi = _get_spi,
1947 .get_cpi = _get_cpi,
1948 .add_sa = _add_sa,
1949 .update_sa = _update_sa,
1950 .query_sa = _query_sa,
1951 .del_sa = _del_sa,
1952 .flush_sas = _flush_sas,
1953 .add_policy = _add_policy,
1954 .query_policy = _query_policy,
1955 .del_policy = _del_policy,
1956 .flush_policies = _flush_policies,
1957 .bypass_socket = _bypass_socket,
1958 .enable_udp_decap = _enable_udp_decap,
1959 .destroy = _destroy,
1960 },
1961 },
1962 .next_sad_id = 0,
1963 .next_spd_id = 0,
1964 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1965 .sas = hashtable_create((hashtable_hash_t)sa_hash,
1966 (hashtable_equals_t)sa_equals, 32),
1967 .spds = hashtable_create((hashtable_hash_t)interface_hash,
1968 (hashtable_equals_t)interface_equals, 4),
1969 .routes = linked_list_create(),
1970 .install_routes = lib->settings->get_bool(lib->settings,
1971 "%s.install_routes", TRUE, lib->ns),
Atzm Watanabefc2d95d2023-01-25 14:11:10 +09001972 .use_tunnel_mode_sa = lib->settings->get_bool(lib->settings,
1973 "%s.plugins.kernel-vpp.use_tunnel_mode_sa",
1974 TRUE, lib->ns),
Gabriel Oginski2da99e52023-02-14 08:46:36 +00001975 .sm = NULL,
Gabriel Oginski4e88e042022-06-29 12:54:30 +00001976 );
1977
1978 if (!init_spi (this))
1979 {
1980 destroy (this);
1981 return NULL;
1982 }
1983
1984 return &this->public;
1985}