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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
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>
44 #include "kernel_vpp_ipsec.h"
45 #include "kernel_vpp_shared.h"
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <sys/ioctl.h>
55 #include <net/route.h>
58 #include <sys/types.h>
59 #include <net/if_arp.h>
68 * One and only instance of the daemon.
72 typedef struct private_kernel_vpp_ipsec_t private_kernel_vpp_ipsec_t;
75 * Private variables of kernel_vpp_ipsec class.
77 struct private_kernel_vpp_ipsec_t
83 kernel_vpp_ipsec_t public;
86 * Next security association database entry ID to allocate
88 refcount_t next_sad_id;
91 * Next security policy database entry ID to allocate
93 refcount_t next_spd_id;
96 * Mutex to lock access to installed policies
101 * Hash table of instaled SA, as kernel_ipsec_sa_id_t => sa_t
106 * Hash table of security policy databases, as nterface => spd_t
111 * Linked list of installed routes
113 linked_list_t *routes;
116 * Next SPI to allocate
121 * Mix value to distribute SPI allocation randomly
126 * Whether to install routes along policies
131 * Whether to install SAs with tunnel flag. Disabling this can be useful
132 * in some scenarios e.g. using SAs to "ipsec tunnel protect" for the
135 bool use_tunnel_mode_sa;
138 * Connections to VPP Stats
140 stat_client_main_t *sm;
144 * Security association entry
151 kernel_ipsec_sa_id_t *sa_id_p;
155 * Security policy database
161 /** Networking interface ID restricting policy */
162 uint32_t sw_if_index;
163 /** Policy count for this SPD */
164 refcount_t policy_num;
165 /** Name of the interface the SPD is bound to */
174 /** Name of the interface the route is bound to */
176 /** Gateway of route */
178 /** Destination network of route */
180 /** Prefix length of dst_net */
182 /** References for route */
187 ((1 == htonl (1)) ? \
189 ((uint64_t) htonl ((x) &0xFFFFFFFF) << 32) | htonl ((x) >> 32))
191 ((1 == ntohl (1)) ? \
193 ((uint64_t) ntohl ((x) &0xFFFFFFFF) << 32) | ntohl ((x) >> 32))
195 CALLBACK (route_equals, bool, route_entry_t *a, va_list args)
197 host_t *dst_net, *gateway;
201 VA_ARGS_VGET (args, if_name, gateway, dst_net, prefixlen);
203 return a->if_name && if_name && streq (a->if_name, if_name) &&
204 a->gateway->ip_equals (a->gateway, gateway) &&
205 a->dst_net->ip_equals (a->dst_net, dst_net) &&
206 a->prefixlen == *prefixlen;
210 * Clean up a route entry
213 route_destroy (route_entry_t *this)
215 this->dst_net->destroy (this->dst_net);
216 this->gateway->destroy (this->gateway);
217 free (this->if_name);
221 static uint32_t get_sw_if_index ();
224 set_arp (char *ipStr, char *if_name, bool add)
228 vl_api_ip_neighbor_add_del_t *mp = NULL;
229 vl_api_ip_neighbor_add_del_reply_t *rmp = NULL;
231 uint32_t sw_if_index = ~0;
238 char *file = "/proc/net/arp";
239 unsigned char mac[8] = {
244 if (if_name == NULL || ipStr == NULL)
246 DBG2 (DBG_KNL, "para is null\n");
250 DBG2 (DBG_KNL, "from kernel read mac\n");
252 mp = vl_msg_api_alloc (sizeof (*mp));
253 memset (mp, 0, sizeof (*mp));
254 sw_if_index = get_sw_if_index (if_name);
255 if (sw_if_index == ~0)
257 DBG1 (DBG_KNL, "sw_if_index for %s not found", if_name);
261 fp = fopen (file, "rb");
262 while (fp && ((nread = getline (&buffer, &len, fp)) != -1))
264 sscanf (buffer, "%s %*s %*s %s %*s %*s", &buf[0], &buf[1]);
265 inet_aton (&buf[0], &addr);
267 if (addr == *((u32 *) (ipStr)))
269 sscanf (buf[1], "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1],
270 &mac[2], &mac[3], &mac[4], &mac[5]);
272 vl_msg_api_get_msg_index ((u8 *) "ip_neighbor_add_del_0607c257");
273 mp->_vl_msg_id = htons (msg_id);
275 memcpy (mp->neighbor.ip_address.un.ip4, (u8 *) &addr, sizeof (addr));
276 mp->neighbor.ip_address.af = 0;
277 memcpy (mp->neighbor.mac_address, mac, 6);
278 mp->neighbor.sw_if_index = htonl (sw_if_index);
279 mp->neighbor.flags = 1;
281 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
283 DBG1 (DBG_KNL, "vac %s neighbor entry",
284 add ? "adding" : "removing");
291 DBG1 (DBG_KNL, "%s neighbor add rv:%d", add ? "add" : "remove",
292 ntohl (rmp->retval));
298 vl_msg_api_free (mp);
313 vl_msg_api_free (mp);
323 add_Route (char *ipAddr, int len, char *mask, char *gateWay)
327 struct sockaddr_in _sin;
328 struct sockaddr_in *sin = &_sin;
333 fd = socket (AF_INET, SOCK_DGRAM, 0);
336 DBG2 (DBG_KNL, "addRoute: socket error\n");
340 memset (&rt, 0, sizeof (struct rtentry));
341 memset (sin, 0, sizeof (struct sockaddr_in));
342 sin->sin_family = AF_INET;
345 if (inet_aton (gateWay, &sin->sin_addr) < 0)
350 memcpy (&rt.rt_gateway, sin, sizeof (struct sockaddr_in));
352 ((struct sockaddr_in *) &rt.rt_dst)->sin_family = AF_INET;
353 memcpy (&((struct sockaddr_in *) &rt.rt_dst)->sin_addr, ipAddr, len);
355 ((struct sockaddr_in *) &rt.rt_genmask)->sin_family = AF_INET;
357 &((struct sockaddr_in *) &rt.rt_genmask)->sin_addr) < 0)
362 rt.rt_flags = RTF_GATEWAY;
363 if (ioctl (fd, SIOCADDRT, &rt) < 0)
375 set_address (u32 ipAddr, u32 sw_if_index, bool add)
379 vl_api_sw_interface_add_del_address_t *mp;
380 vl_api_sw_interface_add_del_address_reply_t *rmp;
386 mp = vl_msg_api_alloc (sizeof (*mp));
387 memset (mp, 0, sizeof (*mp));
390 vl_msg_api_get_msg_index ((u8 *) "sw_interface_add_del_address_5463d73b");
391 mp->_vl_msg_id = htons (msg_id);
393 memcpy (mp->prefix.address.un.ip4, (u8 *) &ipAddr, sizeof (ipAddr));
395 mp->sw_if_index = sw_if_index;
397 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
399 DBG2 (DBG_KNL, "vac %s neighbor entry", add ? "adding" : "removing");
405 DBG2 (DBG_KNL, "%s neighbor add rv:%d", add ? "add" : "remove",
406 ntohl (rmp->retval));
413 vl_msg_api_free (mp);
418 * (Un)-install a single route
421 manage_route (private_kernel_vpp_ipsec_t *this, bool add,
422 traffic_selector_t *dst_ts, host_t *src, host_t *dst)
424 host_t *dst_net = NULL, *gateway = NULL;
426 char *if_name = NULL;
427 route_entry_t *route;
428 bool route_exist = FALSE;
430 char *netmask = "255.255.255.0";
431 char *tap_gateway = "1.1.1.1";
433 if (dst->is_anyaddr (dst))
438 charon->kernel->get_nexthop (charon->kernel, dst, -1, NULL, &if_name);
439 dst_ts->to_subnet (dst_ts, &dst_net, &prefixlen);
442 if (src->is_anyaddr (src))
446 if (!charon->kernel->get_interface (charon->kernel, src, &if_name))
452 this->routes->find_first (this->routes, route_equals, (void **) &route,
453 if_name, gateway, dst_net, &prefixlen);
456 DBG2 (DBG_KNL, "installing route: %H/%d via %H dev %s", dst_net,
457 prefixlen, gateway, if_name);
460 unsigned int refs_num = ref_get (&route->refs);
461 DBG2 (DBG_KNL, "add route but it exist %d", refs_num);
465 INIT (route, .if_name = strdup (if_name),
466 .gateway = gateway->clone (gateway),
467 .dst_net = dst_net->clone (dst_net), .prefixlen = prefixlen,
469 this->routes->insert_last (this->routes, route);
470 charon->kernel->add_route (charon->kernel,
471 dst_net->get_address (dst_net), prefixlen,
472 gateway, dst, if_name, 1);
475 add_Route (dst_net->get_address (dst_net).ptr,
476 dst_net->get_address (dst_net).len, netmask, tap_gateway);
478 arp_rc = set_arp (gateway->get_address (gateway).ptr, if_name, TRUE);
480 DBG2 (DBG_KNL, "arpGet success!\n");
484 DBG2 (DBG_KNL, "uninstalling route: %H/%d via %H dev %s", dst_net,
485 prefixlen, gateway, if_name);
488 DBG2 (DBG_KNL, "del route but it not exist");
491 if (ref_put (&route->refs))
493 this->routes->remove (this->routes, route, NULL);
494 route_destroy (route);
495 charon->kernel->del_route (charon->kernel,
496 dst_net->get_address (dst_net), prefixlen,
497 gateway, dst, if_name, 1);
502 gateway->destroy (gateway);
504 dst_net->destroy (dst_net);
511 * Hash function for IPsec SA
514 sa_hash (kernel_ipsec_sa_id_t *sa)
516 return chunk_hash_inc (
517 sa->src->get_address (sa->src),
519 sa->dst->get_address (sa->dst),
520 chunk_hash_inc (chunk_from_thing (sa->spi),
521 chunk_hash (chunk_from_thing (sa->proto)))));
525 * Equality function for IPsec SA
528 sa_equals (kernel_ipsec_sa_id_t *sa, kernel_ipsec_sa_id_t *other_sa)
530 return sa->src->ip_equals (sa->src, other_sa->src) &&
531 sa->dst->ip_equals (sa->dst, other_sa->dst) &&
532 sa->spi == other_sa->spi && sa->proto == other_sa->proto;
536 * Equality function for policy SPD
539 policy_equals (vl_api_ipsec_spd_entry_t *policy,
540 vl_api_ipsec_spd_entry_t *other_policy)
543 /* change protocol due to legacy implementation of ANY protocol inside VPP */
544 if (other_policy->protocol == 255)
545 other_policy->protocol = 0;
547 /* return true if both policies are equal */
548 return !memcmp (policy, other_policy, sizeof (*policy));
552 * Hash function for interface
555 interface_hash (char *interface)
557 return chunk_hash (chunk_from_str (interface));
561 * Equality function for interface
564 interface_equals (char *interface1, char *interface2)
566 return streq (interface1, interface2);
570 * Map an integer x with a one-to-one function using quadratic residues
573 permute (u_int x, u_int p)
578 qr = ((uint64_t) x * x) % p;
587 * Initialize seeds for SPI generation
590 init_spi (private_kernel_vpp_ipsec_t *this)
595 rng = lib->crypto->create_rng (lib->crypto, RNG_STRONG);
601 rng->get_bytes (rng, sizeof (this->nextspi), (uint8_t *) &this->nextspi);
605 rng->get_bytes (rng, sizeof (this->mixspi), (uint8_t *) &this->mixspi);
612 * Calculate policy priority
615 calculate_priority (policy_priority_t policy_priority, traffic_selector_t *src,
616 traffic_selector_t *dst)
618 uint32_t priority = PRIO_BASE;
623 switch (policy_priority)
625 case POLICY_PRIORITY_FALLBACK:
628 case POLICY_PRIORITY_ROUTED:
631 case POLICY_PRIORITY_DEFAULT:
634 case POLICY_PRIORITY_PASS:
637 /* calculate priority based on selector size, small size = high prio */
638 src->to_subnet (src, &net, &mask);
640 proto = src->get_protocol (src);
641 port = net->get_port (net);
644 dst->to_subnet (dst, &net, &mask);
646 proto = max (proto, dst->get_protocol (dst));
647 port = max (port, net->get_port (net));
650 priority <<= 2; /* make some room for the two flags */
651 priority += port ? 0 : 2;
652 priority += proto ? 0 : 1;
657 * Get sw_if_index from interface name
660 get_sw_if_index (char *interface)
663 int out_len, name_filter_len = 0, msg_len = 0;
665 vl_api_sw_interface_dump_t *mp = NULL;
666 vl_api_sw_interface_details_t *rmp = NULL;
667 uint32_t sw_if_index = ~0;
669 if (interface == NULL)
672 name_filter_len = strlen (interface);
673 msg_len = sizeof (*mp) + name_filter_len;
674 mp = vl_msg_api_alloc (msg_len);
675 clib_memset (mp, 0, msg_len);
676 u16 msg_id = vl_msg_api_get_msg_index ((u8 *) "sw_interface_dump_aa610c27");
677 mp->_vl_msg_id = htons (msg_id);
678 mp->name_filter_valid = TRUE;
679 mp->name_filter.length = htonl (name_filter_len);
680 memcpy ((char *) mp->name_filter.buf, interface, name_filter_len);
682 if (vac->send_dump (vac, (char *) mp, msg_len, &out, &out_len))
690 num = out_len / sizeof (*rmp);
691 rmp = (vl_api_sw_interface_details_t *) out;
692 for (i = 0; i < num; i++)
694 if (strlen (rmp->interface_name) &&
695 streq (interface, rmp->interface_name))
697 sw_if_index = ntohl (rmp->sw_if_index);
707 vl_msg_api_free (mp);
712 * (Un)-install a security policy database
715 spd_add_del (bool add, uint32_t spd_id)
719 vl_api_ipsec_spd_add_del_t *mp;
720 vl_api_ipsec_spd_add_del_reply_t *rmp;
721 status_t rv = FAILED;
723 mp = vl_msg_api_alloc (sizeof (*mp));
724 memset (mp, 0, sizeof (*mp));
726 u16 msg_id = vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_add_del_20e89a95");
727 mp->_vl_msg_id = htons (msg_id);
729 mp->spd_id = htonl (spd_id);
730 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
732 DBG1 (DBG_KNL, "vac %s SPD failed", add ? "adding" : "removing");
738 DBG1 (DBG_KNL, "%s SPD failed rv:%d", add ? "add" : "remove",
739 ntohl (rmp->retval));
746 vl_msg_api_free (mp);
751 * Enable or disable SPD on an insterface
754 interface_add_del_spd (bool add, uint32_t spd_id, uint32_t sw_if_index)
758 vl_api_ipsec_interface_add_del_spd_t *mp;
759 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
760 status_t rv = FAILED;
762 mp = vl_msg_api_alloc (sizeof (*mp));
763 memset (mp, 0, sizeof (*mp));
765 vl_msg_api_get_msg_index ((u8 *) "ipsec_interface_add_del_spd_80f80cbb");
766 mp->_vl_msg_id = htons (msg_id);
768 mp->spd_id = htonl (spd_id);
769 mp->sw_if_index = htonl (sw_if_index);
770 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
772 DBG1 (DBG_KNL, "vac %s interface SPD failed",
773 add ? "adding" : "removing");
779 DBG1 (DBG_KNL, "%s interface SPD failed rv:%d", add ? "add" : "remove",
780 ntohl (rmp->retval));
787 vl_msg_api_free (mp);
792 bypass_all (bool add, uint32_t spd_id, uint32_t sa_id)
794 vl_api_ipsec_spd_entry_add_del_t *mp;
795 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
798 status_t rv = FAILED;
800 DBG2 (DBG_KNL, "bypass_all [%s] spd_id %d sa_id %d", add ? "ADD" : "DEL",
803 mp = vl_msg_api_alloc (sizeof (*mp));
804 memset (mp, 0, sizeof (*mp));
807 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
808 mp->_vl_msg_id = ntohs (msg_id);
810 mp->entry.sa_id = ntohl (sa_id);
811 mp->entry.spd_id = ntohl (spd_id);
812 mp->entry.priority = ntohl (INT_MAX - POLICY_PRIORITY_PASS - 1);
813 mp->entry.is_outbound = 0;
814 mp->entry.policy = ntohl (IPSEC_API_SPD_ACTION_BYPASS);
815 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
816 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
817 mp->entry.remote_port_start = mp->entry.local_port_start = ntohs (0);
818 mp->entry.remote_port_stop = mp->entry.local_port_stop = ntohs (0xFFFF);
819 mp->entry.protocol = IP_API_PROTO_ESP;
820 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
822 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
828 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
829 ntohl (rmp->retval));
832 /* address "out" needs to be freed after vec->send */
838 mp->entry.is_outbound = 1;
839 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
841 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
847 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
848 ntohl (rmp->retval));
851 /* address "out" needs to be freed after vec->send */
857 mp->entry.is_outbound = 0;
858 mp->entry.protocol = IP_API_PROTO_AH;
859 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
861 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
867 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
868 ntohl (rmp->retval));
871 /* address "out" needs to be freed after vec->send */
877 mp->entry.is_outbound = 1;
878 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
880 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
886 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
887 ntohl (rmp->retval));
896 vl_msg_api_free (mp);
902 bypass_port (bool add, uint32_t spd_id, uint32_t sa_id, uint16_t port)
904 vl_api_ipsec_spd_entry_add_del_t *mp;
905 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
908 status_t rv = FAILED;
910 mp = vl_msg_api_alloc (sizeof (*mp));
911 memset (mp, 0, sizeof (*mp));
914 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
915 mp->_vl_msg_id = ntohs (msg_id);
917 mp->entry.sa_id = ntohl (sa_id);
918 mp->entry.spd_id = ntohl (spd_id);
919 mp->entry.priority = ntohl (INT_MAX - POLICY_PRIORITY_PASS - 1);
920 mp->entry.policy = ntohl (IPSEC_API_SPD_ACTION_BYPASS);
921 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
922 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
923 mp->entry.is_outbound = 0;
924 mp->entry.remote_port_start = mp->entry.local_port_start = ntohs (0);
925 mp->entry.remote_port_stop = mp->entry.local_port_stop = ntohs (0xFFFF);
926 mp->entry.protocol = IP_API_PROTO_HOPOPT;
928 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
930 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
936 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
937 ntohl (rmp->retval));
940 /* address "out" needs to be freed after vec->send */
946 mp->entry.is_outbound = 1;
947 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
949 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
955 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
956 ntohl (rmp->retval));
964 vl_msg_api_free (mp);
970 * Add or remove a bypass policy
973 manage_bypass (bool add, uint32_t spd_id, uint32_t sa_id)
978 bypass_all (add, spd_id, sa_id);
981 lib->settings->get_int (lib->settings, "%s.port", IKEV2_UDP_PORT, lib->ns);
985 rv = bypass_port (add, spd_id, sa_id, port);
992 port = lib->settings->get_int (lib->settings, "%s.port_nat_t",
993 IKEV2_NATT_PORT, lib->ns);
996 rv = bypass_port (add, spd_id, sa_id, port);
1007 * Add or remove a policy
1010 manage_policy (private_kernel_vpp_ipsec_t *this, bool add,
1011 kernel_ipsec_policy_id_t *id,
1012 kernel_ipsec_manage_policy_t *data)
1015 char *out = NULL, *interface = NULL;
1017 uint32_t sw_if_index, spd_id = ~0, sad_id = ~0;
1018 status_t rv = FAILED;
1019 uint32_t priority, auto_priority;
1020 chunk_t src_from, src_to, dst_from, dst_to;
1021 host_t *src = NULL, *dst = NULL, *addr = NULL;
1022 vl_api_ipsec_spd_entry_add_del_t *mp = NULL;
1023 vl_api_ipsec_spd_entry_add_del_reply_t *rmp = NULL;
1025 vl_api_ipsec_spd_dump_t *mp_dump = NULL;
1026 vl_api_ipsec_spd_details_t *rmp_dump = NULL, *tmp = NULL;
1028 mp = vl_msg_api_alloc (sizeof (*mp));
1029 memset (mp, 0, sizeof (*mp));
1031 this->mutex->lock (this->mutex);
1032 if (id->dir == POLICY_FWD)
1034 DBG1 (DBG_KNL, "policy FWD interface");
1038 addr = id->dir == POLICY_IN ? data->dst : data->src;
1039 for (int i = 0; i < N_RETRY_GET_IF; i++)
1041 if (!charon->kernel->get_interface (charon->kernel, addr, &interface))
1043 DBG1 (DBG_KNL, "policy no interface %H", addr);
1051 DBG1 (DBG_KNL, "policy have interface %H", addr);
1058 DBG2 (DBG_KNL, "manage policy [%s] interface [%s]", add ? "ADD" : "DEL",
1061 spd = this->spds->get (this->spds, interface);
1066 DBG1 (DBG_KNL, "SPD for %s not found, should not be deleted",
1070 sw_if_index = get_sw_if_index (interface);
1071 DBG1 (DBG_KNL, "firstly created, spd for %s found sw_if_index is %d",
1072 interface, sw_if_index);
1073 if (sw_if_index == ~0)
1075 DBG1 (DBG_KNL, "sw_if_index for %s not found", interface);
1078 spd_id = ref_get (&this->next_spd_id);
1079 if (spd_add_del (TRUE, spd_id))
1081 DBG1 (DBG_KNL, "spd_add_del %d failed!!!!!", spd_id);
1084 if (interface_add_del_spd (TRUE, spd_id, sw_if_index))
1086 DBG1 (DBG_KNL, "interface_add_del_spd %d %d failed!!!!!", spd_id,
1090 INIT (spd, .spd_id = spd_id, .sw_if_index = sw_if_index, .policy_num = 0,
1091 .if_name = strdup (interface), );
1092 this->spds->put (this->spds, spd->if_name, spd);
1096 auto_priority = calculate_priority (data->prio, id->src_ts, id->dst_ts);
1097 priority = data->manual_prio ? data->manual_prio : auto_priority;
1100 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
1101 mp->_vl_msg_id = htons (msg_id);
1103 mp->entry.spd_id = htonl (spd->spd_id);
1104 mp->entry.priority = htonl (INT_MAX - POLICY_PRIORITY_PASS);
1105 mp->entry.is_outbound = id->dir == POLICY_OUT;
1109 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_PROTECT);
1112 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_BYPASS);
1115 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_DISCARD);
1118 if ((data->type == POLICY_IPSEC) && data->sa)
1120 kernel_ipsec_sa_id_t id = {
1123 .proto = data->sa->esp.use ? IPPROTO_ESP : IPPROTO_AH,
1124 .spi = data->sa->esp.use ? data->sa->esp.spi : data->sa->ah.spi,
1127 sa = this->sas->get (this->sas, &id);
1130 DBG1 (DBG_KNL, "SA ID not found");
1136 if (manage_bypass (TRUE, spd_id, ~0))
1138 DBG1 (DBG_KNL, "manage_bypass %d failed!!!!", spd_id);
1144 mp->entry.sa_id = htonl (sad_id);
1146 bool is_ipv6 = false;
1147 if (id->src_ts->get_type (id->src_ts) == TS_IPV6_ADDR_RANGE)
1150 mp->entry.local_address_start.af = htonl (ADDRESS_IP6);
1151 mp->entry.local_address_stop.af = htonl (ADDRESS_IP6);
1152 mp->entry.remote_address_start.af = htonl (ADDRESS_IP6);
1153 mp->entry.remote_address_stop.af = htonl (ADDRESS_IP6);
1157 mp->entry.local_address_start.af = htonl (ADDRESS_IP4);
1158 mp->entry.local_address_stop.af = htonl (ADDRESS_IP4);
1159 mp->entry.remote_address_start.af = htonl (ADDRESS_IP4);
1160 mp->entry.remote_address_stop.af = htonl (ADDRESS_IP4);
1162 mp->entry.protocol = id->src_ts->get_protocol (id->src_ts);
1164 if (id->dir == POLICY_OUT)
1166 src_from = id->src_ts->get_from_address (id->src_ts);
1167 src_to = id->src_ts->get_to_address (id->src_ts);
1168 src = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, src_to, 0);
1169 dst_from = id->dst_ts->get_from_address (id->dst_ts);
1170 dst_to = id->dst_ts->get_to_address (id->dst_ts);
1171 dst = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, dst_to, 0);
1175 dst_from = id->src_ts->get_from_address (id->src_ts);
1176 dst_to = id->src_ts->get_to_address (id->src_ts);
1177 dst = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, dst_from, 0);
1178 src_from = id->dst_ts->get_from_address (id->dst_ts);
1179 src_to = id->dst_ts->get_to_address (id->dst_ts);
1180 src = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, src_from, 0);
1183 if (src->is_anyaddr (src) && dst->is_anyaddr (dst))
1185 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
1186 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
1190 memcpy (is_ipv6 ? mp->entry.local_address_start.un.ip6 :
1191 mp->entry.local_address_start.un.ip4,
1192 src_from.ptr, src_from.len);
1193 memcpy (is_ipv6 ? mp->entry.local_address_stop.un.ip6 :
1194 mp->entry.local_address_stop.un.ip4,
1195 src_to.ptr, src_to.len);
1196 memcpy (is_ipv6 ? mp->entry.remote_address_start.un.ip6 :
1197 mp->entry.remote_address_start.un.ip4,
1198 dst_from.ptr, dst_from.len);
1199 memcpy (is_ipv6 ? mp->entry.remote_address_stop.un.ip6 :
1200 mp->entry.remote_address_stop.un.ip4,
1201 dst_to.ptr, dst_to.len);
1203 mp->entry.local_port_start = htons (id->src_ts->get_from_port (id->src_ts));
1204 mp->entry.local_port_stop = htons (id->src_ts->get_to_port (id->src_ts));
1205 mp->entry.remote_port_start = htons (id->dst_ts->get_from_port (id->dst_ts));
1206 mp->entry.remote_port_stop = htons (id->dst_ts->get_to_port (id->dst_ts));
1208 /* check if policy exists in SPD */
1209 mp_dump = vl_msg_api_alloc (sizeof (*mp_dump));
1210 memset (mp_dump, 0, sizeof (*mp_dump));
1212 msg_id = vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_dump_afefbf7d");
1213 mp_dump->_vl_msg_id = htons (msg_id);
1214 mp_dump->spd_id = htonl (spd->spd_id);
1215 mp_dump->sa_id = htonl (sad_id);
1217 if (vac->send_dump (vac, (char *) mp_dump, sizeof (*mp_dump), &out,
1220 DBG1 (DBG_KNL, "vac %s SPD lookup failed", add ? "adding" : "removing");
1224 int num = out_len / sizeof (*rmp_dump);
1227 /* found existing policy */
1231 for (i = 0; i < num; i++)
1235 /* check if found entry equals the new one */
1236 if (policy_equals (&mp->entry, &rmp_dump->entry))
1240 else if (!add && num == 0)
1242 /* VPP doesn't have any policy to delete */
1248 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1250 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
1256 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
1257 ntohl (rmp->retval));
1264 ref_get (&spd->policy_num);
1268 if (ref_put (&spd->policy_num))
1272 "policy_num's ref is 0, delete spd_id %d sw_if_index %d sad_id %x",
1273 spd->spd_id, spd->sw_if_index, sad_id);
1274 interface_add_del_spd (FALSE, spd->spd_id, spd->sw_if_index);
1275 manage_bypass (FALSE, spd->spd_id, sad_id);
1276 spd_add_del (FALSE, spd->spd_id);
1277 this->spds->remove (this->spds, interface);
1280 free (spd->if_name);
1281 spd->if_name = NULL;
1291 if (this->install_routes && id->dir == POLICY_OUT && !mp->entry.protocol)
1293 if (data->type == POLICY_IPSEC && data->sa->mode != MODE_TRANSPORT)
1295 manage_route (this, add, id->dst_ts, data->src, data->dst);
1302 if (mp_dump != NULL)
1303 vl_msg_api_free (mp_dump);
1305 vl_msg_api_free (mp);
1310 if (interface != NULL)
1312 this->mutex->unlock (this->mutex);
1316 METHOD (kernel_ipsec_t, get_features, kernel_feature_t,
1317 private_kernel_vpp_ipsec_t *this)
1319 return KERNEL_ESP_V3_TFC;
1322 METHOD (kernel_ipsec_t, get_spi, status_t, private_kernel_vpp_ipsec_t *this,
1323 host_t *src, host_t *dst, uint8_t protocol, uint32_t *spi)
1325 static const u_int p = 268435399, offset = 0xc0000000;
1327 *spi = htonl (offset + permute (ref_get (&this->nextspi) ^ this->mixspi, p));
1331 METHOD (kernel_ipsec_t, get_cpi, status_t, private_kernel_vpp_ipsec_t *this,
1332 host_t *src, host_t *dst, uint16_t *cpi)
1334 DBG1 (DBG_KNL, "get_cpi is not supported!!!!!!!!!!!!!!!!!!!!!!!!");
1335 return NOT_SUPPORTED;
1339 * Helper struct for expiration events
1344 private_kernel_vpp_ipsec_t *manager;
1346 kernel_ipsec_sa_id_t *sa_id;
1349 * 0 if this is a hard expire, otherwise the offset in s (soft->hard)
1351 uint32_t hard_offset;
1356 * Clean up expire data
1359 expire_data_destroy (vpp_sa_expired_t *data)
1365 * Callback for expiration events
1367 static job_requeue_t
1368 sa_expired (vpp_sa_expired_t *expired)
1370 private_kernel_vpp_ipsec_t *this = expired->manager;
1372 kernel_ipsec_sa_id_t *id = expired->sa_id;
1374 this->mutex->lock (this->mutex);
1375 sa = this->sas->get (this->sas, id);
1379 charon->kernel->expire (charon->kernel, id->proto, id->spi, id->dst,
1384 id->src->destroy (id->src);
1386 id->dst->destroy (id->dst);
1388 this->mutex->unlock (this->mutex);
1389 return JOB_REQUEUE_NONE;
1393 * Schedule a job to handle IPsec SA expiration
1396 schedule_expiration (private_kernel_vpp_ipsec_t *this,
1397 kernel_ipsec_add_sa_t *entry,
1398 kernel_ipsec_sa_id_t *entry2)
1400 lifetime_cfg_t *lifetime = entry->lifetime;
1401 vpp_sa_expired_t *expired;
1402 callback_job_t *job;
1404 kernel_ipsec_sa_id_t *id;
1406 if (!lifetime->time.life)
1407 { /* no expiration at all */
1411 INIT (id, .src = entry2->src->clone (entry2->src),
1412 .dst = entry2->dst->clone (entry2->dst), .spi = entry2->spi,
1413 .proto = entry2->proto, );
1415 INIT (expired, .manager = this, .sa_id = id, );
1417 /* schedule a rekey first, a hard timeout will be scheduled then, if any */
1418 expired->hard_offset = lifetime->time.life - lifetime->time.rekey;
1419 timeout = lifetime->time.rekey;
1421 if (lifetime->time.life <= lifetime->time.rekey || lifetime->time.rekey == 0)
1422 { /* no rekey, schedule hard timeout */
1423 expired->hard_offset = 0;
1424 timeout = lifetime->time.life;
1428 callback_job_create ((callback_job_cb_t) sa_expired, expired,
1429 (callback_job_cleanup_t) expire_data_destroy, NULL);
1430 lib->scheduler->schedule_job (lib->scheduler, (job_t *) job, timeout);
1433 METHOD (kernel_ipsec_t, add_sa, status_t, private_kernel_vpp_ipsec_t *this,
1434 kernel_ipsec_sa_id_t *id, kernel_ipsec_add_sa_t *data)
1438 vl_api_ipsec_sad_entry_add_del_t *mp;
1439 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
1440 uint32_t sad_id = ref_get (&this->next_sad_id);
1441 uint8_t ca = 0, ia = 0;
1442 status_t rv = FAILED;
1444 kernel_ipsec_sa_id_t *sa_id;
1446 int key_len = data->enc_key.len;
1448 if ((data->enc_alg == ENCR_AES_CTR) ||
1449 (data->enc_alg == ENCR_AES_GCM_ICV8) ||
1450 (data->enc_alg == ENCR_AES_GCM_ICV12) ||
1451 (data->enc_alg == ENCR_AES_GCM_ICV16))
1453 static const int SALT_SIZE =
1454 4; /* See how enc_size is calculated at keymat_v2.derive_child_keys */
1455 key_len = key_len - SALT_SIZE;
1457 natt_port = lib->settings->get_int (
1458 lib->settings, "%s.plugins.socket-default.natt", IKEV2_NATT_PORT, lib->ns);
1459 mp = vl_msg_api_alloc (sizeof (*mp));
1460 memset (mp, 0, sizeof (*mp));
1462 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1463 mp->_vl_msg_id = htons (msg_id);
1465 mp->entry.sad_id = htonl (sad_id);
1466 mp->entry.spi = id->spi;
1467 mp->entry.protocol = id->proto == IPPROTO_ESP ? htonl (IPSEC_API_PROTO_ESP) :
1468 htonl (IPSEC_API_PROTO_AH);
1470 switch (data->enc_alg)
1473 ca = IPSEC_API_CRYPTO_ALG_NONE;
1476 switch (key_len * 8)
1479 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_128;
1482 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_192;
1485 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_256;
1488 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1494 switch (key_len * 8)
1497 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_128;
1500 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_192;
1503 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_256;
1506 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1511 case ENCR_AES_GCM_ICV8:
1512 case ENCR_AES_GCM_ICV12:
1513 case ENCR_AES_GCM_ICV16:
1514 switch (key_len * 8)
1517 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_128;
1520 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_192;
1523 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_256;
1526 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1532 ca = IPSEC_API_CRYPTO_ALG_DES_CBC;
1535 ca = IPSEC_API_CRYPTO_ALG_3DES_CBC;
1538 DBG1 (DBG_KNL, "algorithm %N not supported by VPP!",
1539 encryption_algorithm_names, data->enc_alg);
1542 mp->entry.crypto_algorithm = htonl (ca);
1543 mp->entry.crypto_key.length = key_len < 128 ? key_len : 128;
1544 memcpy (mp->entry.crypto_key.data, data->enc_key.ptr,
1545 mp->entry.crypto_key.length);
1547 /* copy salt for AEAD algorithms */
1548 if ((data->enc_alg == ENCR_AES_CTR) ||
1549 (data->enc_alg == ENCR_AES_GCM_ICV8) ||
1550 (data->enc_alg == ENCR_AES_GCM_ICV12) ||
1551 (data->enc_alg == ENCR_AES_GCM_ICV16))
1553 memcpy (&mp->entry.salt, data->enc_key.ptr + mp->entry.crypto_key.length,
1557 switch (data->int_alg)
1559 case AUTH_UNDEFINED:
1560 ia = IPSEC_API_INTEG_ALG_NONE;
1562 case AUTH_HMAC_MD5_96:
1563 ia = IPSEC_API_INTEG_ALG_MD5_96;
1565 case AUTH_HMAC_SHA1_96:
1566 ia = IPSEC_API_INTEG_ALG_SHA1_96;
1568 case AUTH_HMAC_SHA2_256_96:
1569 ia = IPSEC_API_INTEG_ALG_SHA_256_96;
1571 case AUTH_HMAC_SHA2_256_128:
1572 ia = IPSEC_API_INTEG_ALG_SHA_256_128;
1574 case AUTH_HMAC_SHA2_384_192:
1575 ia = IPSEC_API_INTEG_ALG_SHA_384_192;
1577 case AUTH_HMAC_SHA2_512_256:
1578 ia = IPSEC_API_INTEG_ALG_SHA_512_256;
1581 DBG1 (DBG_KNL, "algorithm %N not supported by VPP!",
1582 integrity_algorithm_names, data->int_alg);
1586 mp->entry.integrity_algorithm = htonl (ia);
1587 mp->entry.integrity_key.length =
1588 data->int_key.len < 128 ? data->int_key.len : 128;
1589 memcpy (mp->entry.integrity_key.data, data->int_key.ptr,
1590 mp->entry.integrity_key.length);
1592 int flags = IPSEC_API_SAD_FLAG_NONE;
1594 flags |= IPSEC_API_SAD_FLAG_IS_INBOUND;
1595 /* like the kernel-netlink plugin, anti-replay can be disabled with zero
1596 * replay_window, but window size cannot be customized for vpp */
1597 if (data->replay_window)
1598 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
1600 flags |= IPSEC_API_SAD_FLAG_USE_ESN;
1601 if (this->use_tunnel_mode_sa && data->mode == MODE_TUNNEL)
1603 if (id->src->get_family (id->src) == AF_INET6)
1604 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
1606 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
1610 DBG1 (DBG_KNL, "UDP encap");
1611 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
1612 mp->entry.udp_src_port = htons (natt_port);
1613 mp->entry.udp_dst_port = htons (natt_port);
1615 mp->entry.flags = htonl (flags);
1617 bool is_ipv6 = false;
1618 if (id->src->get_family (id->src) == AF_INET6)
1621 mp->entry.tunnel_src.af = htonl (ADDRESS_IP6);
1622 mp->entry.tunnel_dst.af = htonl (ADDRESS_IP6);
1626 mp->entry.tunnel_src.af = htonl (ADDRESS_IP4);
1627 mp->entry.tunnel_dst.af = htonl (ADDRESS_IP4);
1629 src = id->src->get_address (id->src);
1630 memcpy (is_ipv6 ? mp->entry.tunnel_src.un.ip6 : mp->entry.tunnel_src.un.ip4,
1632 dst = id->dst->get_address (id->dst);
1633 memcpy (is_ipv6 ? mp->entry.tunnel_dst.un.ip6 : mp->entry.tunnel_dst.un.ip4,
1635 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1637 DBG1 (DBG_KNL, "vac adding SA failed");
1643 DBG1 (DBG_KNL, "add SA failed rv:%d", ntohl (rmp->retval));
1647 this->mutex->lock (this->mutex);
1648 INIT (sa_id, .src = id->src->clone (id->src),
1649 .dst = id->dst->clone (id->dst), .spi = id->spi, .proto = id->proto, );
1650 INIT (sa, .sa_id = sad_id, .stat_index = ntohl (rmp->stat_index),
1651 .sa_id_p = sa_id, );
1652 DBG4 (DBG_KNL, "put sa by its sa_id %x !!!!!!", sad_id);
1653 this->sas->put (this->sas, sa_id, sa);
1654 schedule_expiration (this, data, id);
1655 this->mutex->unlock (this->mutex);
1660 vl_msg_api_free (mp);
1664 METHOD (kernel_ipsec_t, update_sa, status_t, private_kernel_vpp_ipsec_t *this,
1665 kernel_ipsec_sa_id_t *id, kernel_ipsec_update_sa_t *data)
1668 "update sa not supported!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
1669 return NOT_SUPPORTED;
1672 METHOD (kernel_ipsec_t, query_sa, status_t, private_kernel_vpp_ipsec_t *this,
1673 kernel_ipsec_sa_id_t *id, kernel_ipsec_query_sa_t *data,
1674 uint64_t *bytes, uint64_t *packets, time_t *time)
1676 status_t rv = FAILED;
1680 stat_segment_data_t *res = NULL;
1682 uint64_t res_bytes = 0;
1683 uint64_t res_packets = 0;
1685 this->mutex->lock (this->mutex);
1686 sa = this->sas->get (this->sas, id);
1689 this->mutex->unlock (this->mutex);
1690 DBG1 (DBG_KNL, "SA not found");
1694 if (this->sm == NULL)
1696 stat_client_main_t *sm = NULL;
1697 sm = stat_client_get ();
1701 DBG1 (DBG_KNL, "Not connecting with stats segmentation");
1702 this->mutex->unlock (this->mutex);
1706 int rv_stat = stat_segment_connect_r ("/run/vpp/stats.sock", this->sm);
1709 stat_client_free (this->sm);
1711 DBG1 (DBG_KNL, "Not connecting with stats segmentation");
1712 this->mutex->unlock (this->mutex);
1717 vec_add1 (pattern, (u8 *) "/net/ipsec/sa");
1718 dir = stat_segment_ls_r ((u8 **) pattern, this->sm);
1719 res = stat_segment_dump_r (dir, this->sm);
1720 /* i-loop for each results find by pattern - here two:
1722 * 2. /net/ipsec/sa/lost
1724 for (i = 0; i < vec_len (res); i++)
1726 switch (res[i].type)
1728 /* type for how many packets are lost */
1729 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
1730 if (res[i].simple_counter_vec == 0)
1733 /* type for counter for each SA */
1734 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
1735 if (res[i].combined_counter_vec == 0)
1737 /* k-loop for each threads - that you run VPP */
1738 for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
1740 if (sa->stat_index <= vec_len (res[i].combined_counter_vec[k]))
1742 DBG4 (DBG_KNL, "Thread: %d, Packets: %lu, Bytes: %lu", k,
1743 res[i].combined_counter_vec[k][sa->stat_index].packets,
1744 res[i].combined_counter_vec[k][sa->stat_index].bytes);
1746 res[i].combined_counter_vec[k][sa->stat_index].bytes;
1748 res[i].combined_counter_vec[k][sa->stat_index].packets;
1752 case STAT_DIR_TYPE_NAME_VECTOR:
1753 if (res[i].name_vector == 0)
1761 stat_segment_data_free (res);
1769 *packets = res_packets;
1776 this->mutex->unlock (this->mutex);
1781 METHOD (kernel_ipsec_t, del_sa, status_t, private_kernel_vpp_ipsec_t *this,
1782 kernel_ipsec_sa_id_t *id, kernel_ipsec_del_sa_t *data)
1786 vl_api_ipsec_sad_entry_add_del_t *mp = NULL;
1787 vl_api_ipsec_sad_entry_add_del_reply_t *rmp = NULL;
1788 status_t rv = FAILED;
1791 this->mutex->lock (this->mutex);
1792 sa = this->sas->get (this->sas, id);
1795 DBG1 (DBG_KNL, "SA not found");
1799 mp = vl_msg_api_alloc (sizeof (*mp));
1800 memset (mp, 0, sizeof (*mp));
1803 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1804 mp->_vl_msg_id = htons (msg_id);
1805 mp->entry.sad_id = htonl (sa->sa_id);
1807 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1809 DBG1 (DBG_KNL, "vac removing SA failed");
1815 DBG1 (DBG_KNL, "del SA failed rv:%d", ntohl (rmp->retval));
1819 void *temp = this->sas->remove (this->sas, id);
1822 if (sa->sa_id_p->src)
1823 sa->sa_id_p->src->destroy (sa->sa_id_p->src);
1824 if (sa->sa_id_p->dst)
1825 sa->sa_id_p->dst->destroy (sa->sa_id_p->dst);
1832 vl_msg_api_free (mp);
1833 this->mutex->unlock (this->mutex);
1837 METHOD (kernel_ipsec_t, flush_sas, status_t, private_kernel_vpp_ipsec_t *this)
1839 enumerator_t *enumerator;
1842 vl_api_ipsec_sad_entry_add_del_t *mp = NULL;
1843 vl_api_ipsec_sad_entry_add_del_reply_t *rmp = NULL;
1845 status_t rv = FAILED;
1847 this->mutex->lock (this->mutex);
1848 enumerator = this->sas->create_enumerator (this->sas);
1849 while (enumerator->enumerate (enumerator, &sa))
1851 mp = vl_msg_api_alloc (sizeof (*mp));
1852 memset (mp, 0, sizeof (*mp));
1854 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1855 mp->_vl_msg_id = htons (msg_id);
1856 mp->entry.sad_id = htonl (sa->sa_id);
1858 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1860 DBG1 (DBG_KNL, "flush_sas failed!!!!");
1866 DBG1 (DBG_KNL, "flush_sas failed!!!! rv: %d", ntohl (rmp->retval));
1871 if (sa->sa_id_p->src)
1872 sa->sa_id_p->src->destroy (sa->sa_id_p->src);
1873 if (sa->sa_id_p->dst)
1874 sa->sa_id_p->dst->destroy (sa->sa_id_p->dst);
1877 vl_msg_api_free (mp);
1878 this->sas->remove_at (this->sas, enumerator);
1887 vl_msg_api_free (mp);
1889 enumerator->destroy (enumerator);
1890 this->mutex->unlock (this->mutex);
1895 METHOD (kernel_ipsec_t, add_policy, status_t, private_kernel_vpp_ipsec_t *this,
1896 kernel_ipsec_policy_id_t *id, kernel_ipsec_manage_policy_t *data)
1898 return manage_policy (this, TRUE, id, data);
1901 METHOD (kernel_ipsec_t, query_policy, status_t,
1902 private_kernel_vpp_ipsec_t *this, kernel_ipsec_policy_id_t *id,
1903 kernel_ipsec_query_policy_t *data, time_t *use_time)
1905 return NOT_SUPPORTED;
1908 METHOD (kernel_ipsec_t, del_policy, status_t, private_kernel_vpp_ipsec_t *this,
1909 kernel_ipsec_policy_id_t *id, kernel_ipsec_manage_policy_t *data)
1911 return manage_policy (this, FALSE, id, data);
1914 METHOD (kernel_ipsec_t, flush_policies, status_t,
1915 private_kernel_vpp_ipsec_t *this)
1917 return NOT_SUPPORTED;
1920 METHOD (kernel_ipsec_t, bypass_socket, bool, private_kernel_vpp_ipsec_t *this,
1926 METHOD (kernel_ipsec_t, enable_udp_decap, bool,
1927 private_kernel_vpp_ipsec_t *this, int fd, int family, u_int16_t port)
1929 DBG1 (DBG_KNL, "enable_udp_decap not supported!!!!!!!!!!!!!!!!!!!!!!!!!");
1933 METHOD (kernel_ipsec_t, destroy, void, private_kernel_vpp_ipsec_t *this)
1935 this->mutex->destroy (this->mutex);
1936 this->sas->destroy (this->sas);
1937 this->spds->destroy (this->spds);
1938 this->routes->destroy (this->routes);
1941 stat_segment_disconnect_r (this->sm);
1942 stat_client_free (this->sm);
1948 kernel_vpp_ipsec_t *
1949 kernel_vpp_ipsec_create ()
1951 private_kernel_vpp_ipsec_t *this;
1956 .get_features = _get_features,
1957 .get_spi = _get_spi,
1958 .get_cpi = _get_cpi,
1960 .update_sa = _update_sa,
1961 .query_sa = _query_sa,
1963 .flush_sas = _flush_sas,
1964 .add_policy = _add_policy,
1965 .query_policy = _query_policy,
1966 .del_policy = _del_policy,
1967 .flush_policies = _flush_policies,
1968 .bypass_socket = _bypass_socket,
1969 .enable_udp_decap = _enable_udp_decap,
1970 .destroy = _destroy,
1975 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1976 .sas = hashtable_create((hashtable_hash_t)sa_hash,
1977 (hashtable_equals_t)sa_equals, 32),
1978 .spds = hashtable_create((hashtable_hash_t)interface_hash,
1979 (hashtable_equals_t)interface_equals, 4),
1980 .routes = linked_list_create(),
1981 .install_routes = lib->settings->get_bool(lib->settings,
1982 "%s.install_routes", TRUE, lib->ns),
1983 .use_tunnel_mode_sa = lib->settings->get_bool(lib->settings,
1984 "%s.plugins.kernel-vpp.use_tunnel_mode_sa",
1989 if (!init_spi (this))
1995 return &this->public;