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>
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.
71 #define N_RETRY_GET_IF 5
76 * One and only instance of the daemon.
80 typedef struct private_kernel_vpp_ipsec_t private_kernel_vpp_ipsec_t;
83 * Private variables of kernel_vpp_ipsec class.
85 struct private_kernel_vpp_ipsec_t
91 kernel_vpp_ipsec_t public;
94 * Next security association database entry ID to allocate
96 refcount_t next_sad_id;
99 * Next security policy database entry ID to allocate
101 refcount_t next_spd_id;
104 * Mutex to lock access to installed policies
109 * Hash table of instaled SA, as kernel_ipsec_sa_id_t => sa_t
114 * Hash table of security policy databases, as nterface => spd_t
119 * Linked list of installed routes
121 linked_list_t *routes;
124 * Next SPI to allocate
129 * Mix value to distribute SPI allocation randomly
134 * Whether to install routes along policies
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
143 bool use_tunnel_mode_sa;
146 * Connections to VPP Stats
148 stat_client_main_t *sm;
152 * Security association entry
159 kernel_ipsec_sa_id_t *sa_id_p;
163 * Security policy database
169 /** Networking interface ID restricting policy */
170 uint32_t sw_if_index;
171 /** Policy count for this SPD */
172 refcount_t policy_num;
173 /** Name of the interface the SPD is bound to */
182 /** Name of the interface the route is bound to */
184 /** Gateway of route */
186 /** Destination network of route */
188 /** Prefix length of dst_net */
190 /** References for route */
195 ((1 == htonl (1)) ? \
197 ((uint64_t) htonl ((x) &0xFFFFFFFF) << 32) | htonl ((x) >> 32))
199 ((1 == ntohl (1)) ? \
201 ((uint64_t) ntohl ((x) &0xFFFFFFFF) << 32) | ntohl ((x) >> 32))
203 CALLBACK (route_equals, bool, route_entry_t *a, va_list args)
205 host_t *dst_net, *gateway;
209 VA_ARGS_VGET (args, if_name, gateway, dst_net, prefixlen);
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;
218 * Clean up a route entry
221 route_destroy (route_entry_t *this)
223 this->dst_net->destroy (this->dst_net);
224 this->gateway->destroy (this->gateway);
225 free (this->if_name);
229 static uint32_t get_sw_if_index ();
232 set_arp (char *ipStr, char *if_name, bool add)
236 vl_api_ip_neighbor_add_del_t *mp = NULL;
237 vl_api_ip_neighbor_add_del_reply_t *rmp = NULL;
239 uint32_t sw_if_index = ~0;
246 char *file = "/proc/net/arp";
247 unsigned char mac[8] = {
252 if (if_name == NULL || ipStr == NULL)
254 DBG2 (DBG_KNL, "para is null\n");
258 DBG2 (DBG_KNL, "from kernel read mac\n");
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)
265 DBG1 (DBG_KNL, "sw_if_index for %s not found", if_name);
269 fp = fopen (file, "rb");
270 while (fp && ((nread = getline (&buffer, &len, fp)) != -1))
272 sscanf (buffer, "%s %*s %*s %s %*s %*s", &buf[0], &buf[1]);
273 inet_aton (&buf[0], &addr);
275 if (addr == *((u32 *) (ipStr)))
277 sscanf (buf[1], "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1],
278 &mac[2], &mac[3], &mac[4], &mac[5]);
280 vl_msg_api_get_msg_index ((u8 *) "ip_neighbor_add_del_0607c257");
281 mp->_vl_msg_id = htons (msg_id);
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;
289 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
291 DBG1 (DBG_KNL, "vac %s neighbor entry",
292 add ? "adding" : "removing");
299 DBG1 (DBG_KNL, "%s neighbor add rv:%d", add ? "add" : "remove",
300 ntohl (rmp->retval));
306 vl_msg_api_free (mp);
321 vl_msg_api_free (mp);
331 add_Route (char *ipAddr, int len, char *mask, char *gateWay)
335 struct sockaddr_in _sin;
336 struct sockaddr_in *sin = &_sin;
341 fd = socket (AF_INET, SOCK_DGRAM, 0);
344 DBG2 (DBG_KNL, "addRoute: socket error\n");
348 memset (&rt, 0, sizeof (struct rtentry));
349 memset (sin, 0, sizeof (struct sockaddr_in));
350 sin->sin_family = AF_INET;
353 if (inet_aton (gateWay, &sin->sin_addr) < 0)
358 memcpy (&rt.rt_gateway, sin, sizeof (struct sockaddr_in));
360 ((struct sockaddr_in *) &rt.rt_dst)->sin_family = AF_INET;
361 memcpy (&((struct sockaddr_in *) &rt.rt_dst)->sin_addr, ipAddr, len);
363 ((struct sockaddr_in *) &rt.rt_genmask)->sin_family = AF_INET;
365 &((struct sockaddr_in *) &rt.rt_genmask)->sin_addr) < 0)
370 rt.rt_flags = RTF_GATEWAY;
371 if (ioctl (fd, SIOCADDRT, &rt) < 0)
383 set_address (u32 ipAddr, u32 sw_if_index, bool add)
387 vl_api_sw_interface_add_del_address_t *mp;
388 vl_api_sw_interface_add_del_address_reply_t *rmp;
394 mp = vl_msg_api_alloc (sizeof (*mp));
395 memset (mp, 0, sizeof (*mp));
398 vl_msg_api_get_msg_index ((u8 *) "sw_interface_add_del_address_5463d73b");
399 mp->_vl_msg_id = htons (msg_id);
401 memcpy (mp->prefix.address.un.ip4, (u8 *) &ipAddr, sizeof (ipAddr));
403 mp->sw_if_index = sw_if_index;
405 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
407 DBG2 (DBG_KNL, "vac %s neighbor entry", add ? "adding" : "removing");
413 DBG2 (DBG_KNL, "%s neighbor add rv:%d", add ? "add" : "remove",
414 ntohl (rmp->retval));
421 vl_msg_api_free (mp);
426 * (Un)-install a single route
429 manage_route (private_kernel_vpp_ipsec_t *this, bool add,
430 traffic_selector_t *dst_ts, host_t *src, host_t *dst)
432 host_t *dst_net = NULL, *gateway = NULL;
434 char *if_name = NULL;
435 route_entry_t *route;
436 bool route_exist = FALSE;
438 char *netmask = "255.255.255.0";
439 char *tap_gateway = "1.1.1.1";
441 if (dst->is_anyaddr (dst))
446 charon->kernel->get_nexthop (charon->kernel, dst, -1, NULL, &if_name);
447 dst_ts->to_subnet (dst_ts, &dst_net, &prefixlen);
450 if (src->is_anyaddr (src))
454 if (!charon->kernel->get_interface (charon->kernel, src, &if_name))
460 this->routes->find_first (this->routes, route_equals, (void **) &route,
461 if_name, gateway, dst_net, &prefixlen);
464 DBG2 (DBG_KNL, "installing route: %H/%d via %H dev %s", dst_net,
465 prefixlen, gateway, if_name);
468 unsigned int refs_num = ref_get (&route->refs);
469 DBG2 (DBG_KNL, "add route but it exist %d", refs_num);
473 INIT (route, .if_name = strdup (if_name),
474 .gateway = gateway->clone (gateway),
475 .dst_net = dst_net->clone (dst_net), .prefixlen = prefixlen,
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);
483 add_Route (dst_net->get_address (dst_net).ptr,
484 dst_net->get_address (dst_net).len, netmask, tap_gateway);
486 arp_rc = set_arp (gateway->get_address (gateway).ptr, if_name, TRUE);
488 DBG2 (DBG_KNL, "arpGet success!\n");
492 DBG2 (DBG_KNL, "uninstalling route: %H/%d via %H dev %s", dst_net,
493 prefixlen, gateway, if_name);
496 DBG2 (DBG_KNL, "del route but it not exist");
499 if (ref_put (&route->refs))
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);
510 gateway->destroy (gateway);
512 dst_net->destroy (dst_net);
519 * Hash function for IPsec SA
522 sa_hash (kernel_ipsec_sa_id_t *sa)
524 return chunk_hash_inc (
525 sa->src->get_address (sa->src),
527 sa->dst->get_address (sa->dst),
528 chunk_hash_inc (chunk_from_thing (sa->spi),
529 chunk_hash (chunk_from_thing (sa->proto)))));
533 * Equality function for IPsec SA
536 sa_equals (kernel_ipsec_sa_id_t *sa, kernel_ipsec_sa_id_t *other_sa)
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;
544 * Equality function for policy SPD
547 policy_equals (vl_api_ipsec_spd_entry_t *policy,
548 vl_api_ipsec_spd_entry_t *other_policy)
551 /* change protocol due to legacy implementation of ANY protocol inside VPP */
552 if (other_policy->protocol == 255)
553 other_policy->protocol = 0;
555 /* return true if both policies are equal */
556 return !memcmp (policy, other_policy, sizeof (*policy));
560 * Hash function for interface
563 interface_hash (char *interface)
565 return chunk_hash (chunk_from_str (interface));
569 * Equality function for interface
572 interface_equals (char *interface1, char *interface2)
574 return streq (interface1, interface2);
578 * Map an integer x with a one-to-one function using quadratic residues
581 permute (u_int x, u_int p)
586 qr = ((uint64_t) x * x) % p;
595 * Initialize seeds for SPI generation
598 init_spi (private_kernel_vpp_ipsec_t *this)
603 rng = lib->crypto->create_rng (lib->crypto, RNG_STRONG);
609 rng->get_bytes (rng, sizeof (this->nextspi), (uint8_t *) &this->nextspi);
613 rng->get_bytes (rng, sizeof (this->mixspi), (uint8_t *) &this->mixspi);
620 * Calculate policy priority
623 calculate_priority (policy_priority_t policy_priority, traffic_selector_t *src,
624 traffic_selector_t *dst)
626 uint32_t priority = PRIO_BASE;
631 switch (policy_priority)
633 case POLICY_PRIORITY_FALLBACK:
636 case POLICY_PRIORITY_ROUTED:
639 case POLICY_PRIORITY_DEFAULT:
642 case POLICY_PRIORITY_PASS:
645 /* calculate priority based on selector size, small size = high prio */
646 src->to_subnet (src, &net, &mask);
648 proto = src->get_protocol (src);
649 port = net->get_port (net);
652 dst->to_subnet (dst, &net, &mask);
654 proto = max (proto, dst->get_protocol (dst));
655 port = max (port, net->get_port (net));
658 priority <<= 2; /* make some room for the two flags */
659 priority += port ? 0 : 2;
660 priority += proto ? 0 : 1;
665 * Get sw_if_index from interface name
668 get_sw_if_index (char *interface)
671 int out_len, name_filter_len = 0, msg_len = 0;
673 vl_api_sw_interface_dump_t *mp = NULL;
674 vl_api_sw_interface_details_t *rmp = NULL;
675 uint32_t sw_if_index = ~0;
677 if (interface == NULL)
680 name_filter_len = strlen (interface);
681 msg_len = sizeof (*mp) + name_filter_len;
682 mp = vl_msg_api_alloc (msg_len);
683 clib_memset (mp, 0, msg_len);
684 u16 msg_id = vl_msg_api_get_msg_index ((u8 *) "sw_interface_dump_aa610c27");
685 mp->_vl_msg_id = htons (msg_id);
686 mp->name_filter_valid = TRUE;
687 mp->name_filter.length = htonl (name_filter_len);
688 memcpy ((char *) mp->name_filter.buf, interface, name_filter_len);
690 if (vac->send_dump (vac, (char *) mp, msg_len, &out, &out_len))
698 num = out_len / sizeof (*rmp);
699 rmp = (vl_api_sw_interface_details_t *) out;
700 for (i = 0; i < num; i++)
702 if (strlen (rmp->interface_name) &&
703 streq (interface, rmp->interface_name))
705 sw_if_index = ntohl (rmp->sw_if_index);
715 vl_msg_api_free (mp);
720 * (Un)-install a security policy database
723 spd_add_del (bool add, uint32_t spd_id)
727 vl_api_ipsec_spd_add_del_t *mp;
728 vl_api_ipsec_spd_add_del_reply_t *rmp;
729 status_t rv = FAILED;
731 mp = vl_msg_api_alloc (sizeof (*mp));
732 memset (mp, 0, sizeof (*mp));
734 u16 msg_id = vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_add_del_20e89a95");
735 mp->_vl_msg_id = htons (msg_id);
737 mp->spd_id = htonl (spd_id);
738 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
740 DBG1 (DBG_KNL, "vac %s SPD failed", add ? "adding" : "removing");
746 DBG1 (DBG_KNL, "%s SPD failed rv:%d", add ? "add" : "remove",
747 ntohl (rmp->retval));
754 vl_msg_api_free (mp);
759 * Enable or disable SPD on an insterface
762 interface_add_del_spd (bool add, uint32_t spd_id, uint32_t sw_if_index)
766 vl_api_ipsec_interface_add_del_spd_t *mp;
767 vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
768 status_t rv = FAILED;
770 mp = vl_msg_api_alloc (sizeof (*mp));
771 memset (mp, 0, sizeof (*mp));
773 vl_msg_api_get_msg_index ((u8 *) "ipsec_interface_add_del_spd_80f80cbb");
774 mp->_vl_msg_id = htons (msg_id);
776 mp->spd_id = htonl (spd_id);
777 mp->sw_if_index = htonl (sw_if_index);
778 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
780 DBG1 (DBG_KNL, "vac %s interface SPD failed",
781 add ? "adding" : "removing");
787 DBG1 (DBG_KNL, "%s interface SPD failed rv:%d", add ? "add" : "remove",
788 ntohl (rmp->retval));
795 vl_msg_api_free (mp);
800 bypass_all (bool add, uint32_t spd_id, uint32_t sa_id)
802 vl_api_ipsec_spd_entry_add_del_t *mp;
803 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
806 status_t rv = FAILED;
808 DBG2 (DBG_KNL, "bypass_all [%s] spd_id %d sa_id %d", add ? "ADD" : "DEL",
811 mp = vl_msg_api_alloc (sizeof (*mp));
812 memset (mp, 0, sizeof (*mp));
815 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
816 mp->_vl_msg_id = ntohs (msg_id);
818 mp->entry.sa_id = ntohl (sa_id);
819 mp->entry.spd_id = ntohl (spd_id);
820 mp->entry.priority = ntohl (INT_MAX - POLICY_PRIORITY_PASS - 1);
821 mp->entry.is_outbound = 0;
822 mp->entry.policy = ntohl (IPSEC_API_SPD_ACTION_BYPASS);
823 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
824 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
825 mp->entry.remote_port_start = mp->entry.local_port_start = ntohs (0);
826 mp->entry.remote_port_stop = mp->entry.local_port_stop = ntohs (0xFFFF);
827 mp->entry.protocol = IP_API_PROTO_ESP;
828 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
830 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
836 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
837 ntohl (rmp->retval));
840 /* address "out" needs to be freed after vec->send */
846 mp->entry.is_outbound = 1;
847 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
849 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
855 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
856 ntohl (rmp->retval));
859 /* address "out" needs to be freed after vec->send */
865 mp->entry.is_outbound = 0;
866 mp->entry.protocol = IP_API_PROTO_AH;
867 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
869 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
875 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
876 ntohl (rmp->retval));
879 /* address "out" needs to be freed after vec->send */
885 mp->entry.is_outbound = 1;
886 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
888 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
894 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
895 ntohl (rmp->retval));
904 vl_msg_api_free (mp);
910 bypass_port (bool add, uint32_t spd_id, uint32_t sa_id, uint16_t port)
912 vl_api_ipsec_spd_entry_add_del_t *mp;
913 vl_api_ipsec_spd_entry_add_del_reply_t *rmp;
916 status_t rv = FAILED;
918 mp = vl_msg_api_alloc (sizeof (*mp));
919 memset (mp, 0, sizeof (*mp));
922 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
923 mp->_vl_msg_id = ntohs (msg_id);
925 mp->entry.sa_id = ntohl (sa_id);
926 mp->entry.spd_id = ntohl (spd_id);
927 mp->entry.priority = ntohl (INT_MAX - POLICY_PRIORITY_PASS - 1);
928 mp->entry.policy = ntohl (IPSEC_API_SPD_ACTION_BYPASS);
929 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
930 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
931 mp->entry.is_outbound = 0;
932 mp->entry.remote_port_start = mp->entry.local_port_start = ntohs (0);
933 mp->entry.remote_port_stop = mp->entry.local_port_stop = ntohs (0xFFFF);
934 mp->entry.protocol = IP_API_PROTO_HOPOPT;
936 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
938 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
944 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
945 ntohl (rmp->retval));
948 /* address "out" needs to be freed after vec->send */
954 mp->entry.is_outbound = 1;
955 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
957 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
963 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
964 ntohl (rmp->retval));
972 vl_msg_api_free (mp);
978 * Add or remove a bypass policy
981 manage_bypass (bool add, uint32_t spd_id, uint32_t sa_id)
986 bypass_all (add, spd_id, sa_id);
989 lib->settings->get_int (lib->settings, "%s.port", IKEV2_UDP_PORT, lib->ns);
993 rv = bypass_port (add, spd_id, sa_id, port);
1000 port = lib->settings->get_int (lib->settings, "%s.port_nat_t",
1001 IKEV2_NATT_PORT, lib->ns);
1004 rv = bypass_port (add, spd_id, sa_id, port);
1015 * Add or remove a policy
1018 manage_policy (private_kernel_vpp_ipsec_t *this, bool add,
1019 kernel_ipsec_policy_id_t *id,
1020 kernel_ipsec_manage_policy_t *data)
1023 char *out = NULL, *interface = NULL;
1025 uint32_t sw_if_index, spd_id = ~0, sad_id = ~0;
1026 status_t rv = FAILED;
1027 uint32_t priority, auto_priority;
1028 chunk_t src_from, src_to, dst_from, dst_to;
1029 host_t *src = NULL, *dst = NULL, *addr = NULL;
1030 vl_api_ipsec_spd_entry_add_del_t *mp = NULL;
1031 vl_api_ipsec_spd_entry_add_del_reply_t *rmp = NULL;
1033 vl_api_ipsec_spd_dump_t *mp_dump = NULL;
1034 vl_api_ipsec_spd_details_t *rmp_dump = NULL, *tmp = NULL;
1036 mp = vl_msg_api_alloc (sizeof (*mp));
1037 memset (mp, 0, sizeof (*mp));
1039 this->mutex->lock (this->mutex);
1040 if (id->dir == POLICY_FWD)
1042 DBG1 (DBG_KNL, "policy FWD interface");
1046 addr = id->dir == POLICY_IN ? data->dst : data->src;
1047 for (int i = 0; i < N_RETRY_GET_IF; i++)
1049 if (!charon->kernel->get_interface (charon->kernel, addr, &interface))
1051 DBG1 (DBG_KNL, "policy no interface %H", addr);
1059 DBG1 (DBG_KNL, "policy have interface %H", addr);
1066 DBG2 (DBG_KNL, "manage policy [%s] interface [%s]", add ? "ADD" : "DEL",
1069 spd = this->spds->get (this->spds, interface);
1074 DBG1 (DBG_KNL, "SPD for %s not found, should not be deleted",
1078 sw_if_index = get_sw_if_index (interface);
1079 DBG1 (DBG_KNL, "firstly created, spd for %s found sw_if_index is %d",
1080 interface, sw_if_index);
1081 if (sw_if_index == ~0)
1083 DBG1 (DBG_KNL, "sw_if_index for %s not found", interface);
1086 spd_id = ref_get (&this->next_spd_id);
1087 if (spd_add_del (TRUE, spd_id))
1089 DBG1 (DBG_KNL, "spd_add_del %d failed!!!!!", spd_id);
1092 if (interface_add_del_spd (TRUE, spd_id, sw_if_index))
1094 DBG1 (DBG_KNL, "interface_add_del_spd %d %d failed!!!!!", spd_id,
1098 INIT (spd, .spd_id = spd_id, .sw_if_index = sw_if_index, .policy_num = 0,
1099 .if_name = strdup (interface), );
1100 this->spds->put (this->spds, spd->if_name, spd);
1104 auto_priority = calculate_priority (data->prio, id->src_ts, id->dst_ts);
1105 priority = data->manual_prio ? data->manual_prio : auto_priority;
1108 vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411");
1109 mp->_vl_msg_id = htons (msg_id);
1111 mp->entry.spd_id = htonl (spd->spd_id);
1112 mp->entry.priority = htonl (INT_MAX - POLICY_PRIORITY_PASS);
1113 mp->entry.is_outbound = id->dir == POLICY_OUT;
1117 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_PROTECT);
1120 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_BYPASS);
1123 mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_DISCARD);
1126 if ((data->type == POLICY_IPSEC) && data->sa)
1128 kernel_ipsec_sa_id_t id = {
1131 .proto = data->sa->esp.use ? IPPROTO_ESP : IPPROTO_AH,
1132 .spi = data->sa->esp.use ? data->sa->esp.spi : data->sa->ah.spi,
1135 sa = this->sas->get (this->sas, &id);
1138 DBG1 (DBG_KNL, "SA ID not found");
1144 if (manage_bypass (TRUE, spd_id, ~0))
1146 DBG1 (DBG_KNL, "manage_bypass %d failed!!!!", spd_id);
1152 mp->entry.sa_id = htonl (sad_id);
1154 bool is_ipv6 = false;
1155 if (id->src_ts->get_type (id->src_ts) == TS_IPV6_ADDR_RANGE)
1158 mp->entry.local_address_start.af = htonl (ADDRESS_IP6);
1159 mp->entry.local_address_stop.af = htonl (ADDRESS_IP6);
1160 mp->entry.remote_address_start.af = htonl (ADDRESS_IP6);
1161 mp->entry.remote_address_stop.af = htonl (ADDRESS_IP6);
1165 mp->entry.local_address_start.af = htonl (ADDRESS_IP4);
1166 mp->entry.local_address_stop.af = htonl (ADDRESS_IP4);
1167 mp->entry.remote_address_start.af = htonl (ADDRESS_IP4);
1168 mp->entry.remote_address_stop.af = htonl (ADDRESS_IP4);
1170 mp->entry.protocol = id->src_ts->get_protocol (id->src_ts);
1172 if (id->dir == POLICY_OUT)
1174 src_from = id->src_ts->get_from_address (id->src_ts);
1175 src_to = id->src_ts->get_to_address (id->src_ts);
1176 src = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, src_to, 0);
1177 dst_from = id->dst_ts->get_from_address (id->dst_ts);
1178 dst_to = id->dst_ts->get_to_address (id->dst_ts);
1179 dst = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, dst_to, 0);
1183 dst_from = id->src_ts->get_from_address (id->src_ts);
1184 dst_to = id->src_ts->get_to_address (id->src_ts);
1185 dst = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, dst_from, 0);
1186 src_from = id->dst_ts->get_from_address (id->dst_ts);
1187 src_to = id->dst_ts->get_to_address (id->dst_ts);
1188 src = host_create_from_chunk (is_ipv6 ? AF_INET6 : AF_INET, src_from, 0);
1191 if (src->is_anyaddr (src) && dst->is_anyaddr (dst))
1193 memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16);
1194 memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16);
1198 memcpy (is_ipv6 ? mp->entry.local_address_start.un.ip6 :
1199 mp->entry.local_address_start.un.ip4,
1200 src_from.ptr, src_from.len);
1201 memcpy (is_ipv6 ? mp->entry.local_address_stop.un.ip6 :
1202 mp->entry.local_address_stop.un.ip4,
1203 src_to.ptr, src_to.len);
1204 memcpy (is_ipv6 ? mp->entry.remote_address_start.un.ip6 :
1205 mp->entry.remote_address_start.un.ip4,
1206 dst_from.ptr, dst_from.len);
1207 memcpy (is_ipv6 ? mp->entry.remote_address_stop.un.ip6 :
1208 mp->entry.remote_address_stop.un.ip4,
1209 dst_to.ptr, dst_to.len);
1211 mp->entry.local_port_start = htons (id->src_ts->get_from_port (id->src_ts));
1212 mp->entry.local_port_stop = htons (id->src_ts->get_to_port (id->src_ts));
1213 mp->entry.remote_port_start = htons (id->dst_ts->get_from_port (id->dst_ts));
1214 mp->entry.remote_port_stop = htons (id->dst_ts->get_to_port (id->dst_ts));
1216 /* check if policy exists in SPD */
1217 mp_dump = vl_msg_api_alloc (sizeof (*mp_dump));
1218 memset (mp_dump, 0, sizeof (*mp_dump));
1220 msg_id = vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_dump_afefbf7d");
1221 mp_dump->_vl_msg_id = htons (msg_id);
1222 mp_dump->spd_id = htonl (spd->spd_id);
1223 mp_dump->sa_id = htonl (sad_id);
1225 if (vac->send_dump (vac, (char *) mp_dump, sizeof (*mp_dump), &out,
1228 DBG1 (DBG_KNL, "vac %s SPD lookup failed", add ? "adding" : "removing");
1232 int num = out_len / sizeof (*rmp_dump);
1235 /* found existing policy */
1239 for (i = 0; i < num; i++)
1243 /* check if found entry equals the new one */
1244 if (policy_equals (&mp->entry, &rmp_dump->entry))
1248 else if (!add && num == 0)
1250 /* VPP doesn't have any policy to delete */
1256 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1258 DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing");
1264 DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove",
1265 ntohl (rmp->retval));
1272 ref_get (&spd->policy_num);
1276 if (ref_put (&spd->policy_num))
1280 "policy_num's ref is 0, delete spd_id %d sw_if_index %d sad_id %x",
1281 spd->spd_id, spd->sw_if_index, sad_id);
1282 interface_add_del_spd (FALSE, spd->spd_id, spd->sw_if_index);
1283 manage_bypass (FALSE, spd->spd_id, sad_id);
1284 spd_add_del (FALSE, spd->spd_id);
1285 this->spds->remove (this->spds, interface);
1288 free (spd->if_name);
1289 spd->if_name = NULL;
1299 if (this->install_routes && id->dir == POLICY_OUT && !mp->entry.protocol)
1301 if (data->type == POLICY_IPSEC && data->sa->mode != MODE_TRANSPORT)
1303 manage_route (this, add, id->dst_ts, data->src, data->dst);
1310 if (mp_dump != NULL)
1311 vl_msg_api_free (mp_dump);
1313 vl_msg_api_free (mp);
1318 if (interface != NULL)
1320 this->mutex->unlock (this->mutex);
1324 METHOD (kernel_ipsec_t, get_features, kernel_feature_t,
1325 private_kernel_vpp_ipsec_t *this)
1327 return KERNEL_ESP_V3_TFC;
1330 METHOD (kernel_ipsec_t, get_spi, status_t, private_kernel_vpp_ipsec_t *this,
1331 host_t *src, host_t *dst, uint8_t protocol, uint32_t *spi)
1333 static const u_int p = 268435399, offset = 0xc0000000;
1335 *spi = htonl (offset + permute (ref_get (&this->nextspi) ^ this->mixspi, p));
1339 METHOD (kernel_ipsec_t, get_cpi, status_t, private_kernel_vpp_ipsec_t *this,
1340 host_t *src, host_t *dst, uint16_t *cpi)
1342 DBG1 (DBG_KNL, "get_cpi is not supported!!!!!!!!!!!!!!!!!!!!!!!!");
1343 return NOT_SUPPORTED;
1347 * Helper struct for expiration events
1352 private_kernel_vpp_ipsec_t *manager;
1354 kernel_ipsec_sa_id_t *sa_id;
1357 * 0 if this is a hard expire, otherwise the offset in s (soft->hard)
1359 uint32_t hard_offset;
1364 * Clean up expire data
1367 expire_data_destroy (vpp_sa_expired_t *data)
1373 * Callback for expiration events
1375 static job_requeue_t
1376 sa_expired (vpp_sa_expired_t *expired)
1378 private_kernel_vpp_ipsec_t *this = expired->manager;
1380 kernel_ipsec_sa_id_t *id = expired->sa_id;
1382 this->mutex->lock (this->mutex);
1383 sa = this->sas->get (this->sas, id);
1387 charon->kernel->expire (charon->kernel, id->proto, id->spi, id->dst,
1392 id->src->destroy (id->src);
1394 id->dst->destroy (id->dst);
1396 this->mutex->unlock (this->mutex);
1397 return JOB_REQUEUE_NONE;
1401 * Schedule a job to handle IPsec SA expiration
1404 schedule_expiration (private_kernel_vpp_ipsec_t *this,
1405 kernel_ipsec_add_sa_t *entry,
1406 kernel_ipsec_sa_id_t *entry2)
1408 lifetime_cfg_t *lifetime = entry->lifetime;
1409 vpp_sa_expired_t *expired;
1410 callback_job_t *job;
1412 kernel_ipsec_sa_id_t *id;
1414 if (!lifetime->time.life)
1415 { /* no expiration at all */
1419 INIT (id, .src = entry2->src->clone (entry2->src),
1420 .dst = entry2->dst->clone (entry2->dst), .spi = entry2->spi,
1421 .proto = entry2->proto, );
1423 INIT (expired, .manager = this, .sa_id = id, );
1425 /* schedule a rekey first, a hard timeout will be scheduled then, if any */
1426 expired->hard_offset = lifetime->time.life - lifetime->time.rekey;
1427 timeout = lifetime->time.rekey;
1429 if (lifetime->time.life <= lifetime->time.rekey || lifetime->time.rekey == 0)
1430 { /* no rekey, schedule hard timeout */
1431 expired->hard_offset = 0;
1432 timeout = lifetime->time.life;
1436 callback_job_create ((callback_job_cb_t) sa_expired, expired,
1437 (callback_job_cleanup_t) expire_data_destroy, NULL);
1438 lib->scheduler->schedule_job (lib->scheduler, (job_t *) job, timeout);
1441 METHOD (kernel_ipsec_t, add_sa, status_t, private_kernel_vpp_ipsec_t *this,
1442 kernel_ipsec_sa_id_t *id, kernel_ipsec_add_sa_t *data)
1446 vl_api_ipsec_sad_entry_add_del_t *mp;
1447 vl_api_ipsec_sad_entry_add_del_reply_t *rmp;
1448 uint32_t sad_id = ref_get (&this->next_sad_id);
1449 uint8_t ca = 0, ia = 0;
1450 status_t rv = FAILED;
1452 kernel_ipsec_sa_id_t *sa_id;
1454 int key_len = data->enc_key.len;
1456 if ((data->enc_alg == ENCR_AES_CTR) ||
1457 (data->enc_alg == ENCR_AES_GCM_ICV8) ||
1458 (data->enc_alg == ENCR_AES_GCM_ICV12) ||
1459 (data->enc_alg == ENCR_AES_GCM_ICV16))
1461 static const int SALT_SIZE =
1462 4; /* See how enc_size is calculated at keymat_v2.derive_child_keys */
1463 key_len = key_len - SALT_SIZE;
1465 natt_port = lib->settings->get_int (
1466 lib->settings, "%s.plugins.socket-default.natt", IKEV2_NATT_PORT, lib->ns);
1467 mp = vl_msg_api_alloc (sizeof (*mp));
1468 memset (mp, 0, sizeof (*mp));
1470 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1471 mp->_vl_msg_id = htons (msg_id);
1473 mp->entry.sad_id = htonl (sad_id);
1474 mp->entry.spi = id->spi;
1475 mp->entry.protocol = id->proto == IPPROTO_ESP ? htonl (IPSEC_API_PROTO_ESP) :
1476 htonl (IPSEC_API_PROTO_AH);
1478 switch (data->enc_alg)
1481 ca = IPSEC_API_CRYPTO_ALG_NONE;
1484 switch (key_len * 8)
1487 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_128;
1490 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_192;
1493 ca = IPSEC_API_CRYPTO_ALG_AES_CBC_256;
1496 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1502 switch (key_len * 8)
1505 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_128;
1508 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_192;
1511 ca = IPSEC_API_CRYPTO_ALG_AES_CTR_256;
1514 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1519 case ENCR_AES_GCM_ICV8:
1520 case ENCR_AES_GCM_ICV12:
1521 case ENCR_AES_GCM_ICV16:
1522 switch (key_len * 8)
1525 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_128;
1528 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_192;
1531 ca = IPSEC_API_CRYPTO_ALG_AES_GCM_256;
1534 DBG1 (DBG_KNL, "Key length %d is not supported by VPP!",
1540 ca = IPSEC_API_CRYPTO_ALG_DES_CBC;
1543 ca = IPSEC_API_CRYPTO_ALG_3DES_CBC;
1546 DBG1 (DBG_KNL, "algorithm %N not supported by VPP!",
1547 encryption_algorithm_names, data->enc_alg);
1550 mp->entry.crypto_algorithm = htonl (ca);
1551 mp->entry.crypto_key.length = key_len < 128 ? key_len : 128;
1552 memcpy (mp->entry.crypto_key.data, data->enc_key.ptr,
1553 mp->entry.crypto_key.length);
1555 /* copy salt for AEAD algorithms */
1556 if ((data->enc_alg == ENCR_AES_CTR) ||
1557 (data->enc_alg == ENCR_AES_GCM_ICV8) ||
1558 (data->enc_alg == ENCR_AES_GCM_ICV12) ||
1559 (data->enc_alg == ENCR_AES_GCM_ICV16))
1561 memcpy (&mp->entry.salt, data->enc_key.ptr + mp->entry.crypto_key.length,
1565 switch (data->int_alg)
1567 case AUTH_UNDEFINED:
1568 ia = IPSEC_API_INTEG_ALG_NONE;
1570 case AUTH_HMAC_MD5_96:
1571 ia = IPSEC_API_INTEG_ALG_MD5_96;
1573 case AUTH_HMAC_SHA1_96:
1574 ia = IPSEC_API_INTEG_ALG_SHA1_96;
1576 case AUTH_HMAC_SHA2_256_96:
1577 ia = IPSEC_API_INTEG_ALG_SHA_256_96;
1579 case AUTH_HMAC_SHA2_256_128:
1580 ia = IPSEC_API_INTEG_ALG_SHA_256_128;
1582 case AUTH_HMAC_SHA2_384_192:
1583 ia = IPSEC_API_INTEG_ALG_SHA_384_192;
1585 case AUTH_HMAC_SHA2_512_256:
1586 ia = IPSEC_API_INTEG_ALG_SHA_512_256;
1589 DBG1 (DBG_KNL, "algorithm %N not supported by VPP!",
1590 integrity_algorithm_names, data->int_alg);
1594 mp->entry.integrity_algorithm = htonl (ia);
1595 mp->entry.integrity_key.length =
1596 data->int_key.len < 128 ? data->int_key.len : 128;
1597 memcpy (mp->entry.integrity_key.data, data->int_key.ptr,
1598 mp->entry.integrity_key.length);
1600 int flags = IPSEC_API_SAD_FLAG_NONE;
1602 flags |= IPSEC_API_SAD_FLAG_IS_INBOUND;
1603 /* like the kernel-netlink plugin, anti-replay can be disabled with zero
1604 * replay_window, but window size cannot be customized for vpp */
1605 if (data->replay_window)
1606 flags |= IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY;
1608 flags |= IPSEC_API_SAD_FLAG_USE_ESN;
1609 if (this->use_tunnel_mode_sa && data->mode == MODE_TUNNEL)
1611 if (id->src->get_family (id->src) == AF_INET6)
1612 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6;
1614 flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL;
1618 DBG1 (DBG_KNL, "UDP encap");
1619 flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP;
1620 mp->entry.udp_src_port = htons (natt_port);
1621 mp->entry.udp_dst_port = htons (natt_port);
1623 mp->entry.flags = htonl (flags);
1625 bool is_ipv6 = false;
1626 if (id->src->get_family (id->src) == AF_INET6)
1629 mp->entry.tunnel_src.af = htonl (ADDRESS_IP6);
1630 mp->entry.tunnel_dst.af = htonl (ADDRESS_IP6);
1634 mp->entry.tunnel_src.af = htonl (ADDRESS_IP4);
1635 mp->entry.tunnel_dst.af = htonl (ADDRESS_IP4);
1637 src = id->src->get_address (id->src);
1638 memcpy (is_ipv6 ? mp->entry.tunnel_src.un.ip6 : mp->entry.tunnel_src.un.ip4,
1640 dst = id->dst->get_address (id->dst);
1641 memcpy (is_ipv6 ? mp->entry.tunnel_dst.un.ip6 : mp->entry.tunnel_dst.un.ip4,
1643 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1645 DBG1 (DBG_KNL, "vac adding SA failed");
1651 DBG1 (DBG_KNL, "add SA failed rv:%d", ntohl (rmp->retval));
1655 this->mutex->lock (this->mutex);
1656 INIT (sa_id, .src = id->src->clone (id->src),
1657 .dst = id->dst->clone (id->dst), .spi = id->spi, .proto = id->proto, );
1658 INIT (sa, .sa_id = sad_id, .stat_index = ntohl (rmp->stat_index),
1659 .sa_id_p = sa_id, );
1660 DBG4 (DBG_KNL, "put sa by its sa_id %x !!!!!!", sad_id);
1661 this->sas->put (this->sas, sa_id, sa);
1662 schedule_expiration (this, data, id);
1663 this->mutex->unlock (this->mutex);
1668 vl_msg_api_free (mp);
1672 METHOD (kernel_ipsec_t, update_sa, status_t, private_kernel_vpp_ipsec_t *this,
1673 kernel_ipsec_sa_id_t *id, kernel_ipsec_update_sa_t *data)
1676 "update sa not supported!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
1677 return NOT_SUPPORTED;
1680 METHOD (kernel_ipsec_t, query_sa, status_t, private_kernel_vpp_ipsec_t *this,
1681 kernel_ipsec_sa_id_t *id, kernel_ipsec_query_sa_t *data,
1682 uint64_t *bytes, uint64_t *packets, time_t *time)
1684 status_t rv = FAILED;
1688 stat_segment_data_t *res = NULL;
1690 uint64_t res_bytes = 0;
1691 uint64_t res_packets = 0;
1693 this->mutex->lock (this->mutex);
1694 sa = this->sas->get (this->sas, id);
1697 this->mutex->unlock (this->mutex);
1698 DBG1 (DBG_KNL, "SA not found");
1702 if (this->sm == NULL)
1704 stat_client_main_t *sm = NULL;
1705 sm = stat_client_get ();
1709 DBG1 (DBG_KNL, "Not connecting with stats segmentation");
1710 this->mutex->unlock (this->mutex);
1714 int rv_stat = stat_segment_connect_r ("/run/vpp/stats.sock", this->sm);
1717 stat_client_free (this->sm);
1719 DBG1 (DBG_KNL, "Not connecting with stats segmentation");
1720 this->mutex->unlock (this->mutex);
1725 vec_add1 (pattern, (u8 *) "/net/ipsec/sa");
1726 dir = stat_segment_ls_r ((u8 **) pattern, this->sm);
1727 res = stat_segment_dump_r (dir, this->sm);
1728 /* i-loop for each results find by pattern - here two:
1730 * 2. /net/ipsec/sa/lost
1732 for (i = 0; i < vec_len (res); i++)
1734 switch (res[i].type)
1736 /* type for how many packets are lost */
1737 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
1738 if (res[i].simple_counter_vec == 0)
1741 /* type for counter for each SA */
1742 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
1743 if (res[i].combined_counter_vec == 0)
1745 /* k-loop for each threads - that you run VPP */
1746 for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
1748 if (sa->stat_index <= vec_len (res[i].combined_counter_vec[k]))
1750 DBG4 (DBG_KNL, "Thread: %d, Packets: %lu, Bytes: %lu", k,
1751 res[i].combined_counter_vec[k][sa->stat_index].packets,
1752 res[i].combined_counter_vec[k][sa->stat_index].bytes);
1754 res[i].combined_counter_vec[k][sa->stat_index].bytes;
1756 res[i].combined_counter_vec[k][sa->stat_index].packets;
1760 case STAT_DIR_TYPE_NAME_VECTOR:
1761 if (res[i].name_vector == 0)
1769 stat_segment_data_free (res);
1777 *packets = res_packets;
1784 this->mutex->unlock (this->mutex);
1789 METHOD (kernel_ipsec_t, del_sa, status_t, private_kernel_vpp_ipsec_t *this,
1790 kernel_ipsec_sa_id_t *id, kernel_ipsec_del_sa_t *data)
1794 vl_api_ipsec_sad_entry_add_del_t *mp = NULL;
1795 vl_api_ipsec_sad_entry_add_del_reply_t *rmp = NULL;
1796 status_t rv = FAILED;
1799 this->mutex->lock (this->mutex);
1800 sa = this->sas->get (this->sas, id);
1803 DBG1 (DBG_KNL, "SA not found");
1807 mp = vl_msg_api_alloc (sizeof (*mp));
1808 memset (mp, 0, sizeof (*mp));
1811 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1812 mp->_vl_msg_id = htons (msg_id);
1813 mp->entry.sad_id = htonl (sa->sa_id);
1815 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1817 DBG1 (DBG_KNL, "vac removing SA failed");
1823 DBG1 (DBG_KNL, "del SA failed rv:%d", ntohl (rmp->retval));
1827 void *temp = this->sas->remove (this->sas, id);
1830 if (sa->sa_id_p->src)
1831 sa->sa_id_p->src->destroy (sa->sa_id_p->src);
1832 if (sa->sa_id_p->dst)
1833 sa->sa_id_p->dst->destroy (sa->sa_id_p->dst);
1840 vl_msg_api_free (mp);
1841 this->mutex->unlock (this->mutex);
1845 METHOD (kernel_ipsec_t, flush_sas, status_t, private_kernel_vpp_ipsec_t *this)
1847 enumerator_t *enumerator;
1850 vl_api_ipsec_sad_entry_add_del_t *mp = NULL;
1851 vl_api_ipsec_sad_entry_add_del_reply_t *rmp = NULL;
1853 status_t rv = FAILED;
1855 this->mutex->lock (this->mutex);
1856 enumerator = this->sas->create_enumerator (this->sas);
1857 while (enumerator->enumerate (enumerator, &sa))
1859 mp = vl_msg_api_alloc (sizeof (*mp));
1860 memset (mp, 0, sizeof (*mp));
1862 vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6");
1863 mp->_vl_msg_id = htons (msg_id);
1864 mp->entry.sad_id = htonl (sa->sa_id);
1866 if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len))
1868 DBG1 (DBG_KNL, "flush_sas failed!!!!");
1874 DBG1 (DBG_KNL, "flush_sas failed!!!! rv: %d", ntohl (rmp->retval));
1879 if (sa->sa_id_p->src)
1880 sa->sa_id_p->src->destroy (sa->sa_id_p->src);
1881 if (sa->sa_id_p->dst)
1882 sa->sa_id_p->dst->destroy (sa->sa_id_p->dst);
1885 vl_msg_api_free (mp);
1886 this->sas->remove_at (this->sas, enumerator);
1895 vl_msg_api_free (mp);
1897 enumerator->destroy (enumerator);
1898 this->mutex->unlock (this->mutex);
1903 METHOD (kernel_ipsec_t, add_policy, status_t, private_kernel_vpp_ipsec_t *this,
1904 kernel_ipsec_policy_id_t *id, kernel_ipsec_manage_policy_t *data)
1906 return manage_policy (this, TRUE, id, data);
1909 METHOD (kernel_ipsec_t, query_policy, status_t,
1910 private_kernel_vpp_ipsec_t *this, kernel_ipsec_policy_id_t *id,
1911 kernel_ipsec_query_policy_t *data, time_t *use_time)
1913 return NOT_SUPPORTED;
1916 METHOD (kernel_ipsec_t, del_policy, status_t, private_kernel_vpp_ipsec_t *this,
1917 kernel_ipsec_policy_id_t *id, kernel_ipsec_manage_policy_t *data)
1919 return manage_policy (this, FALSE, id, data);
1922 METHOD (kernel_ipsec_t, flush_policies, status_t,
1923 private_kernel_vpp_ipsec_t *this)
1925 return NOT_SUPPORTED;
1928 METHOD (kernel_ipsec_t, bypass_socket, bool, private_kernel_vpp_ipsec_t *this,
1934 METHOD (kernel_ipsec_t, enable_udp_decap, bool,
1935 private_kernel_vpp_ipsec_t *this, int fd, int family, u_int16_t port)
1937 DBG1 (DBG_KNL, "enable_udp_decap not supported!!!!!!!!!!!!!!!!!!!!!!!!!");
1941 METHOD (kernel_ipsec_t, destroy, void, private_kernel_vpp_ipsec_t *this)
1943 this->mutex->destroy (this->mutex);
1944 this->sas->destroy (this->sas);
1945 this->spds->destroy (this->spds);
1946 this->routes->destroy (this->routes);
1949 stat_segment_disconnect_r (this->sm);
1950 stat_client_free (this->sm);
1956 kernel_vpp_ipsec_t *
1957 kernel_vpp_ipsec_create ()
1959 private_kernel_vpp_ipsec_t *this;
1964 .get_features = _get_features,
1965 .get_spi = _get_spi,
1966 .get_cpi = _get_cpi,
1968 .update_sa = _update_sa,
1969 .query_sa = _query_sa,
1971 .flush_sas = _flush_sas,
1972 .add_policy = _add_policy,
1973 .query_policy = _query_policy,
1974 .del_policy = _del_policy,
1975 .flush_policies = _flush_policies,
1976 .bypass_socket = _bypass_socket,
1977 .enable_udp_decap = _enable_udp_decap,
1978 .destroy = _destroy,
1983 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
1984 .sas = hashtable_create((hashtable_hash_t)sa_hash,
1985 (hashtable_equals_t)sa_equals, 32),
1986 .spds = hashtable_create((hashtable_hash_t)interface_hash,
1987 (hashtable_equals_t)interface_equals, 4),
1988 .routes = linked_list_create(),
1989 .install_routes = lib->settings->get_bool(lib->settings,
1990 "%s.install_routes", TRUE, lib->ns),
1991 .use_tunnel_mode_sa = lib->settings->get_bool(lib->settings,
1992 "%s.plugins.kernel-vpp.use_tunnel_mode_sa",
1997 if (!init_spi (this))
2003 return &this->public;