Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 65 | u32 natt_port; |
| 66 | |
| 67 | /** |
| 68 | * One and only instance of the daemon. |
| 69 | */ |
| 70 | daemon_t *charon; |
| 71 | |
| 72 | typedef struct private_kernel_vpp_ipsec_t private_kernel_vpp_ipsec_t; |
| 73 | |
| 74 | /** |
| 75 | * Private variables of kernel_vpp_ipsec class. |
| 76 | */ |
| 77 | struct private_kernel_vpp_ipsec_t |
| 78 | { |
| 79 | |
| 80 | /** |
| 81 | * Public interface |
| 82 | */ |
| 83 | kernel_vpp_ipsec_t public; |
| 84 | |
| 85 | /** |
| 86 | * Next security association database entry ID to allocate |
| 87 | */ |
| 88 | refcount_t next_sad_id; |
| 89 | |
| 90 | /** |
| 91 | * Next security policy database entry ID to allocate |
| 92 | */ |
| 93 | refcount_t next_spd_id; |
| 94 | |
| 95 | /** |
| 96 | * Mutex to lock access to installed policies |
| 97 | */ |
| 98 | mutex_t *mutex; |
| 99 | |
| 100 | /** |
| 101 | * Hash table of instaled SA, as kernel_ipsec_sa_id_t => sa_t |
| 102 | */ |
| 103 | hashtable_t *sas; |
| 104 | |
| 105 | /** |
| 106 | * Hash table of security policy databases, as nterface => spd_t |
| 107 | */ |
| 108 | hashtable_t *spds; |
| 109 | |
| 110 | /** |
| 111 | * Linked list of installed routes |
| 112 | */ |
| 113 | linked_list_t *routes; |
| 114 | |
| 115 | /** |
| 116 | * Next SPI to allocate |
| 117 | */ |
| 118 | refcount_t nextspi; |
| 119 | |
| 120 | /** |
| 121 | * Mix value to distribute SPI allocation randomly |
| 122 | */ |
| 123 | uint32_t mixspi; |
| 124 | |
| 125 | /** |
| 126 | * Whether to install routes along policies |
| 127 | */ |
| 128 | bool install_routes; |
Atzm Watanabe | fc2d95d | 2023-01-25 14:11:10 +0900 | [diff] [blame] | 129 | |
| 130 | /** |
| 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 |
| 133 | * route-based IPsec |
| 134 | */ |
| 135 | bool use_tunnel_mode_sa; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Connections to VPP Stats |
| 139 | */ |
| 140 | stat_client_main_t *sm; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * Security association entry |
| 145 | */ |
| 146 | typedef struct |
| 147 | { |
| 148 | /** VPP SA ID */ |
| 149 | uint32_t sa_id; |
| 150 | uint32_t stat_index; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 151 | kernel_ipsec_sa_id_t *sa_id_p; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 152 | } sa_t; |
| 153 | |
| 154 | /** |
| 155 | * Security policy database |
| 156 | */ |
| 157 | typedef struct |
| 158 | { |
| 159 | /** VPP SPD ID */ |
| 160 | uint32_t spd_id; |
| 161 | /** Networking interface ID restricting policy */ |
| 162 | uint32_t sw_if_index; |
| 163 | /** Policy count for this SPD */ |
| 164 | refcount_t policy_num; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 165 | /** Name of the interface the SPD is bound to */ |
| 166 | char *if_name; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 167 | } spd_t; |
| 168 | |
| 169 | /** |
| 170 | * Installed route |
| 171 | */ |
| 172 | typedef struct |
| 173 | { |
| 174 | /** Name of the interface the route is bound to */ |
| 175 | char *if_name; |
| 176 | /** Gateway of route */ |
| 177 | host_t *gateway; |
| 178 | /** Destination network of route */ |
| 179 | host_t *dst_net; |
| 180 | /** Prefix length of dst_net */ |
| 181 | uint8_t prefixlen; |
| 182 | /** References for route */ |
| 183 | refcount_t refs; |
| 184 | } route_entry_t; |
| 185 | |
| 186 | #define htonll(x) \ |
| 187 | ((1 == htonl (1)) ? \ |
| 188 | (x) : \ |
| 189 | ((uint64_t) htonl ((x) &0xFFFFFFFF) << 32) | htonl ((x) >> 32)) |
| 190 | #define ntohll(x) \ |
| 191 | ((1 == ntohl (1)) ? \ |
| 192 | (x) : \ |
| 193 | ((uint64_t) ntohl ((x) &0xFFFFFFFF) << 32) | ntohl ((x) >> 32)) |
| 194 | |
| 195 | CALLBACK (route_equals, bool, route_entry_t *a, va_list args) |
| 196 | { |
| 197 | host_t *dst_net, *gateway; |
| 198 | uint8_t *prefixlen; |
| 199 | char *if_name; |
| 200 | |
| 201 | VA_ARGS_VGET (args, if_name, gateway, dst_net, prefixlen); |
| 202 | |
| 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; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Clean up a route entry |
| 211 | */ |
| 212 | static void |
| 213 | route_destroy (route_entry_t *this) |
| 214 | { |
| 215 | this->dst_net->destroy (this->dst_net); |
| 216 | this->gateway->destroy (this->gateway); |
| 217 | free (this->if_name); |
| 218 | free (this); |
| 219 | } |
| 220 | |
| 221 | static uint32_t get_sw_if_index (); |
| 222 | |
| 223 | static int |
| 224 | set_arp (char *ipStr, char *if_name, bool add) |
| 225 | { |
| 226 | char *out = NULL; |
| 227 | int out_len = 0; |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 228 | vl_api_ip_neighbor_add_del_t *mp = NULL; |
| 229 | vl_api_ip_neighbor_add_del_reply_t *rmp = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 230 | int rc = SUCCESS; |
| 231 | uint32_t sw_if_index = ~0; |
| 232 | |
| 233 | FILE *fp; |
| 234 | int nread = 0; |
| 235 | ssize_t len = 0; |
| 236 | char *buffer = NULL; |
| 237 | char buf[2][20]; |
| 238 | char *file = "/proc/net/arp"; |
| 239 | unsigned char mac[8] = { |
| 240 | 0, |
| 241 | }; |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 242 | uint32_t addr = 0; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 243 | |
| 244 | if (if_name == NULL || ipStr == NULL) |
| 245 | { |
| 246 | DBG2 (DBG_KNL, "para is null\n"); |
| 247 | rc = FAILED; |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 248 | goto error; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 249 | } |
| 250 | DBG2 (DBG_KNL, "from kernel read mac\n"); |
| 251 | |
| 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) |
| 256 | { |
| 257 | DBG1 (DBG_KNL, "sw_if_index for %s not found", if_name); |
| 258 | goto error; |
| 259 | } |
| 260 | |
| 261 | fp = fopen (file, "rb"); |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 262 | while (fp && ((nread = getline (&buffer, &len, fp)) != -1)) |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 263 | { |
| 264 | sscanf (buffer, "%s %*s %*s %s %*s %*s", &buf[0], &buf[1]); |
| 265 | inet_aton (&buf[0], &addr); |
| 266 | |
| 267 | if (addr == *((u32 *) (ipStr))) |
| 268 | { |
| 269 | sscanf (buf[1], "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1], |
| 270 | &mac[2], &mac[3], &mac[4], &mac[5]); |
| 271 | u16 msg_id = |
| 272 | vl_msg_api_get_msg_index ((u8 *) "ip_neighbor_add_del_0607c257"); |
| 273 | mp->_vl_msg_id = htons (msg_id); |
| 274 | mp->is_add = add; |
| 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; |
| 280 | |
| 281 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 282 | { |
| 283 | DBG1 (DBG_KNL, "vac %s neighbor entry", |
| 284 | add ? "adding" : "removing"); |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 285 | fclose (fp); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 286 | goto error; |
| 287 | } |
| 288 | rmp = (void *) out; |
| 289 | if (rmp->retval) |
| 290 | { |
| 291 | DBG1 (DBG_KNL, "%s neighbor add rv:%d", add ? "add" : "remove", |
| 292 | ntohl (rmp->retval)); |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 293 | fclose (fp); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 294 | goto error; |
| 295 | } |
| 296 | fclose (fp); |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 297 | free (out); |
| 298 | vl_msg_api_free (mp); |
| 299 | free (buffer); |
| 300 | |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 301 | return rc; |
| 302 | } |
| 303 | } |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 304 | |
| 305 | if (fp != NULL) |
| 306 | { |
| 307 | fclose (fp); |
| 308 | fp = NULL; |
| 309 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 310 | |
| 311 | error: |
| 312 | free (out); |
| 313 | vl_msg_api_free (mp); |
Gabriel Oginski | 9cb3e15 | 2023-02-14 08:41:07 +0000 | [diff] [blame] | 314 | if (buffer != NULL) |
| 315 | { |
| 316 | free (buffer); |
| 317 | buffer = NULL; |
| 318 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 319 | return rc; |
| 320 | } |
| 321 | |
| 322 | static int |
| 323 | add_Route (char *ipAddr, int len, char *mask, char *gateWay) |
| 324 | { |
| 325 | int fd; |
| 326 | int rc = SUCCESS; |
| 327 | struct sockaddr_in _sin; |
| 328 | struct sockaddr_in *sin = &_sin; |
| 329 | struct rtentry rt; |
| 330 | |
| 331 | do |
| 332 | { |
| 333 | fd = socket (AF_INET, SOCK_DGRAM, 0); |
| 334 | if (fd < 0) |
| 335 | { |
| 336 | DBG2 (DBG_KNL, "addRoute: socket error\n"); |
| 337 | rc = FAILED; |
| 338 | break; |
| 339 | } |
| 340 | memset (&rt, 0, sizeof (struct rtentry)); |
| 341 | memset (sin, 0, sizeof (struct sockaddr_in)); |
| 342 | sin->sin_family = AF_INET; |
| 343 | sin->sin_port = 0; |
| 344 | |
| 345 | if (inet_aton (gateWay, &sin->sin_addr) < 0) |
| 346 | { |
| 347 | rc = FAILED; |
| 348 | break; |
| 349 | } |
| 350 | memcpy (&rt.rt_gateway, sin, sizeof (struct sockaddr_in)); |
| 351 | |
| 352 | ((struct sockaddr_in *) &rt.rt_dst)->sin_family = AF_INET; |
| 353 | memcpy (&((struct sockaddr_in *) &rt.rt_dst)->sin_addr, ipAddr, len); |
| 354 | |
| 355 | ((struct sockaddr_in *) &rt.rt_genmask)->sin_family = AF_INET; |
| 356 | if (inet_aton (mask, |
| 357 | &((struct sockaddr_in *) &rt.rt_genmask)->sin_addr) < 0) |
| 358 | { |
| 359 | rc = FAILED; |
| 360 | break; |
| 361 | } |
| 362 | rt.rt_flags = RTF_GATEWAY; |
| 363 | if (ioctl (fd, SIOCADDRT, &rt) < 0) |
| 364 | { |
| 365 | rc = FAILED; |
| 366 | } |
| 367 | } |
| 368 | while (0); |
| 369 | |
| 370 | close (fd); |
| 371 | return rc; |
| 372 | } |
| 373 | |
| 374 | static int |
| 375 | set_address (u32 ipAddr, u32 sw_if_index, bool add) |
| 376 | { |
| 377 | char *out = NULL; |
| 378 | int out_len = 0; |
| 379 | vl_api_sw_interface_add_del_address_t *mp; |
| 380 | vl_api_sw_interface_add_del_address_reply_t *rmp; |
| 381 | |
| 382 | int rc = SUCCESS; |
| 383 | |
| 384 | uint32_t addr; |
| 385 | |
| 386 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 387 | memset (mp, 0, sizeof (*mp)); |
| 388 | |
| 389 | u16 msg_id = |
| 390 | vl_msg_api_get_msg_index ((u8 *) "sw_interface_add_del_address_5463d73b"); |
| 391 | mp->_vl_msg_id = htons (msg_id); |
| 392 | mp->is_add = add; |
| 393 | memcpy (mp->prefix.address.un.ip4, (u8 *) &ipAddr, sizeof (ipAddr)); |
| 394 | mp->prefix.len = 24; |
| 395 | mp->sw_if_index = sw_if_index; |
| 396 | |
| 397 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 398 | { |
| 399 | DBG2 (DBG_KNL, "vac %s neighbor entry", add ? "adding" : "removing"); |
| 400 | goto error; |
| 401 | } |
| 402 | rmp = (void *) out; |
| 403 | if (rmp->retval) |
| 404 | { |
| 405 | DBG2 (DBG_KNL, "%s neighbor add rv:%d", add ? "add" : "remove", |
| 406 | ntohl (rmp->retval)); |
| 407 | goto error; |
| 408 | } |
| 409 | return rc; |
| 410 | |
| 411 | error: |
| 412 | free (out); |
| 413 | vl_msg_api_free (mp); |
| 414 | return rc; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * (Un)-install a single route |
| 419 | */ |
| 420 | static void |
| 421 | manage_route (private_kernel_vpp_ipsec_t *this, bool add, |
| 422 | traffic_selector_t *dst_ts, host_t *src, host_t *dst) |
| 423 | { |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 424 | host_t *dst_net = NULL, *gateway = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 425 | uint8_t prefixlen; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 426 | char *if_name = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 427 | route_entry_t *route; |
| 428 | bool route_exist = FALSE; |
| 429 | |
| 430 | char *netmask = "255.255.255.0"; |
| 431 | char *tap_gateway = "1.1.1.1"; |
| 432 | int arp_rc = 0; |
| 433 | if (dst->is_anyaddr (dst)) |
| 434 | { |
| 435 | return; |
| 436 | } |
| 437 | gateway = |
| 438 | charon->kernel->get_nexthop (charon->kernel, dst, -1, NULL, &if_name); |
| 439 | dst_ts->to_subnet (dst_ts, &dst_net, &prefixlen); |
| 440 | if (!if_name) |
| 441 | { |
| 442 | if (src->is_anyaddr (src)) |
| 443 | { |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 444 | goto error; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 445 | } |
| 446 | if (!charon->kernel->get_interface (charon->kernel, src, &if_name)) |
| 447 | { |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 448 | goto error; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | route_exist = |
| 452 | this->routes->find_first (this->routes, route_equals, (void **) &route, |
| 453 | if_name, gateway, dst_net, &prefixlen); |
| 454 | if (add) |
| 455 | { |
| 456 | DBG2 (DBG_KNL, "installing route: %H/%d via %H dev %s", dst_net, |
| 457 | prefixlen, gateway, if_name); |
| 458 | if (route_exist) |
| 459 | { |
| 460 | unsigned int refs_num = ref_get (&route->refs); |
| 461 | DBG2 (DBG_KNL, "add route but it exist %d", refs_num); |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | INIT (route, .if_name = strdup (if_name), |
| 466 | .gateway = gateway->clone (gateway), |
| 467 | .dst_net = dst_net->clone (dst_net), .prefixlen = prefixlen, |
| 468 | .refs = 1, ); |
| 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); |
| 473 | } |
| 474 | |
| 475 | add_Route (dst_net->get_address (dst_net).ptr, |
| 476 | dst_net->get_address (dst_net).len, netmask, tap_gateway); |
| 477 | |
| 478 | arp_rc = set_arp (gateway->get_address (gateway).ptr, if_name, TRUE); |
| 479 | if (arp_rc) |
| 480 | DBG2 (DBG_KNL, "arpGet success!\n"); |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | DBG2 (DBG_KNL, "uninstalling route: %H/%d via %H dev %s", dst_net, |
| 485 | prefixlen, gateway, if_name); |
| 486 | if (!route_exist) |
| 487 | { |
| 488 | DBG2 (DBG_KNL, "del route but it not exist"); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 489 | goto error; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 490 | } |
| 491 | if (ref_put (&route->refs)) |
| 492 | { |
| 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); |
| 498 | } |
| 499 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 500 | error: |
| 501 | if (gateway != NULL) |
| 502 | gateway->destroy (gateway); |
| 503 | if (dst_net != NULL) |
| 504 | dst_net->destroy (dst_net); |
| 505 | if (if_name != NULL) |
| 506 | free (if_name); |
| 507 | return; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Hash function for IPsec SA |
| 512 | */ |
| 513 | static u_int |
| 514 | sa_hash (kernel_ipsec_sa_id_t *sa) |
| 515 | { |
| 516 | return chunk_hash_inc ( |
| 517 | sa->src->get_address (sa->src), |
| 518 | chunk_hash_inc ( |
| 519 | sa->dst->get_address (sa->dst), |
| 520 | chunk_hash_inc (chunk_from_thing (sa->spi), |
| 521 | chunk_hash (chunk_from_thing (sa->proto))))); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Equality function for IPsec SA |
| 526 | */ |
| 527 | static bool |
| 528 | sa_equals (kernel_ipsec_sa_id_t *sa, kernel_ipsec_sa_id_t *other_sa) |
| 529 | { |
| 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; |
| 533 | } |
| 534 | |
| 535 | /** |
Gabriel Oginski | 8de66c0 | 2023-02-03 08:12:36 +0000 | [diff] [blame] | 536 | * Equality function for policy SPD |
| 537 | */ |
| 538 | static bool |
| 539 | policy_equals (vl_api_ipsec_spd_entry_t *policy, |
| 540 | vl_api_ipsec_spd_entry_t *other_policy) |
| 541 | { |
| 542 | |
| 543 | /* change protocol due to legacy implementation of ANY protocol inside VPP */ |
| 544 | if (other_policy->protocol == 255) |
| 545 | other_policy->protocol = 0; |
| 546 | |
| 547 | /* return true if both policies are equal */ |
| 548 | return !memcmp (policy, other_policy, sizeof (*policy)); |
| 549 | } |
| 550 | |
| 551 | /** |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 552 | * Hash function for interface |
| 553 | */ |
| 554 | static u_int |
| 555 | interface_hash (char *interface) |
| 556 | { |
| 557 | return chunk_hash (chunk_from_str (interface)); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Equality function for interface |
| 562 | */ |
| 563 | static bool |
| 564 | interface_equals (char *interface1, char *interface2) |
| 565 | { |
| 566 | return streq (interface1, interface2); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Map an integer x with a one-to-one function using quadratic residues |
| 571 | */ |
| 572 | static u_int |
| 573 | permute (u_int x, u_int p) |
| 574 | { |
| 575 | u_int qr; |
| 576 | |
| 577 | x = x % p; |
| 578 | qr = ((uint64_t) x * x) % p; |
| 579 | if (x <= p / 2) |
| 580 | { |
| 581 | return qr; |
| 582 | } |
| 583 | return p - qr; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Initialize seeds for SPI generation |
| 588 | */ |
| 589 | static bool |
| 590 | init_spi (private_kernel_vpp_ipsec_t *this) |
| 591 | { |
| 592 | bool ok = TRUE; |
| 593 | rng_t *rng; |
| 594 | |
| 595 | rng = lib->crypto->create_rng (lib->crypto, RNG_STRONG); |
| 596 | if (!rng) |
| 597 | { |
| 598 | return FALSE; |
| 599 | } |
| 600 | ok = |
| 601 | rng->get_bytes (rng, sizeof (this->nextspi), (uint8_t *) &this->nextspi); |
| 602 | if (ok) |
| 603 | { |
| 604 | ok = |
| 605 | rng->get_bytes (rng, sizeof (this->mixspi), (uint8_t *) &this->mixspi); |
| 606 | } |
| 607 | rng->destroy (rng); |
| 608 | return ok; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Calculate policy priority |
| 613 | */ |
| 614 | static uint32_t |
| 615 | calculate_priority (policy_priority_t policy_priority, traffic_selector_t *src, |
| 616 | traffic_selector_t *dst) |
| 617 | { |
| 618 | uint32_t priority = PRIO_BASE; |
| 619 | uint16_t port; |
| 620 | uint8_t mask, proto; |
| 621 | host_t *net; |
| 622 | |
| 623 | switch (policy_priority) |
| 624 | { |
| 625 | case POLICY_PRIORITY_FALLBACK: |
| 626 | priority <<= 1; |
| 627 | /* fall-through */ |
| 628 | case POLICY_PRIORITY_ROUTED: |
| 629 | priority <<= 1; |
| 630 | /* fall-through */ |
| 631 | case POLICY_PRIORITY_DEFAULT: |
| 632 | priority <<= 1; |
| 633 | /* fall-through */ |
| 634 | case POLICY_PRIORITY_PASS: |
| 635 | break; |
| 636 | } |
| 637 | /* calculate priority based on selector size, small size = high prio */ |
| 638 | src->to_subnet (src, &net, &mask); |
| 639 | priority -= mask; |
| 640 | proto = src->get_protocol (src); |
| 641 | port = net->get_port (net); |
| 642 | net->destroy (net); |
| 643 | |
| 644 | dst->to_subnet (dst, &net, &mask); |
| 645 | priority -= mask; |
| 646 | proto = max (proto, dst->get_protocol (dst)); |
| 647 | port = max (port, net->get_port (net)); |
| 648 | net->destroy (net); |
| 649 | |
| 650 | priority <<= 2; /* make some room for the two flags */ |
| 651 | priority += port ? 0 : 2; |
| 652 | priority += proto ? 0 : 1; |
| 653 | return priority; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Get sw_if_index from interface name |
| 658 | */ |
| 659 | static uint32_t |
| 660 | get_sw_if_index (char *interface) |
| 661 | { |
| 662 | char *out = NULL; |
| 663 | int out_len, name_filter_len = 0, msg_len = 0; |
Gabriel Oginski | 73d82d1 | 2023-07-06 09:18:45 +0000 | [diff] [blame] | 664 | int num, i; |
| 665 | vl_api_sw_interface_dump_t *mp = NULL; |
| 666 | vl_api_sw_interface_details_t *rmp = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 667 | uint32_t sw_if_index = ~0; |
| 668 | |
Gabriel Oginski | 73d82d1 | 2023-07-06 09:18:45 +0000 | [diff] [blame] | 669 | if (interface == NULL) |
| 670 | goto error; |
| 671 | |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 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); |
| 681 | |
Gabriel Oginski | 73d82d1 | 2023-07-06 09:18:45 +0000 | [diff] [blame] | 682 | if (vac->send_dump (vac, (char *) mp, msg_len, &out, &out_len)) |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 683 | { |
| 684 | goto error; |
| 685 | } |
| 686 | if (!out_len) |
| 687 | { |
| 688 | goto error; |
| 689 | } |
Gabriel Oginski | 73d82d1 | 2023-07-06 09:18:45 +0000 | [diff] [blame] | 690 | num = out_len / sizeof (*rmp); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 691 | rmp = (vl_api_sw_interface_details_t *) out; |
Gabriel Oginski | 73d82d1 | 2023-07-06 09:18:45 +0000 | [diff] [blame] | 692 | for (i = 0; i < num; i++) |
| 693 | { |
| 694 | if (strlen (rmp->interface_name) && |
| 695 | streq (interface, rmp->interface_name)) |
| 696 | { |
| 697 | sw_if_index = ntohl (rmp->sw_if_index); |
| 698 | break; |
| 699 | } |
| 700 | rmp += 1; |
| 701 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 702 | |
| 703 | error: |
Gabriel Oginski | 73d82d1 | 2023-07-06 09:18:45 +0000 | [diff] [blame] | 704 | if (out) |
| 705 | free (out); |
| 706 | if (mp) |
| 707 | vl_msg_api_free (mp); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 708 | return sw_if_index; |
| 709 | } |
Gabriel Oginski | 73d82d1 | 2023-07-06 09:18:45 +0000 | [diff] [blame] | 710 | |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 711 | /** |
| 712 | * (Un)-install a security policy database |
| 713 | */ |
| 714 | static status_t |
| 715 | spd_add_del (bool add, uint32_t spd_id) |
| 716 | { |
| 717 | char *out = NULL; |
| 718 | int out_len; |
| 719 | vl_api_ipsec_spd_add_del_t *mp; |
| 720 | vl_api_ipsec_spd_add_del_reply_t *rmp; |
| 721 | status_t rv = FAILED; |
| 722 | |
| 723 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 724 | memset (mp, 0, sizeof (*mp)); |
| 725 | |
| 726 | u16 msg_id = vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_add_del_20e89a95"); |
| 727 | mp->_vl_msg_id = htons (msg_id); |
| 728 | mp->is_add = add; |
| 729 | mp->spd_id = htonl (spd_id); |
| 730 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 731 | { |
| 732 | DBG1 (DBG_KNL, "vac %s SPD failed", add ? "adding" : "removing"); |
| 733 | goto error; |
| 734 | } |
| 735 | rmp = (void *) out; |
| 736 | if (rmp->retval) |
| 737 | { |
| 738 | DBG1 (DBG_KNL, "%s SPD failed rv:%d", add ? "add" : "remove", |
| 739 | ntohl (rmp->retval)); |
| 740 | goto error; |
| 741 | } |
| 742 | rv = SUCCESS; |
| 743 | |
| 744 | error: |
| 745 | free (out); |
| 746 | vl_msg_api_free (mp); |
| 747 | return rv; |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Enable or disable SPD on an insterface |
| 752 | */ |
| 753 | static status_t |
| 754 | interface_add_del_spd (bool add, uint32_t spd_id, uint32_t sw_if_index) |
| 755 | { |
| 756 | char *out = NULL; |
| 757 | int out_len; |
| 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; |
| 761 | |
| 762 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 763 | memset (mp, 0, sizeof (*mp)); |
| 764 | u16 msg_id = |
| 765 | vl_msg_api_get_msg_index ((u8 *) "ipsec_interface_add_del_spd_80f80cbb"); |
| 766 | mp->_vl_msg_id = htons (msg_id); |
| 767 | mp->is_add = add; |
| 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)) |
| 771 | { |
| 772 | DBG1 (DBG_KNL, "vac %s interface SPD failed", |
| 773 | add ? "adding" : "removing"); |
| 774 | goto error; |
| 775 | } |
| 776 | rmp = (void *) out; |
| 777 | if (rmp->retval) |
| 778 | { |
| 779 | DBG1 (DBG_KNL, "%s interface SPD failed rv:%d", add ? "add" : "remove", |
| 780 | ntohl (rmp->retval)); |
| 781 | goto error; |
| 782 | } |
| 783 | rv = SUCCESS; |
| 784 | |
| 785 | error: |
| 786 | free (out); |
| 787 | vl_msg_api_free (mp); |
| 788 | return rv; |
| 789 | } |
| 790 | |
| 791 | static int |
| 792 | bypass_all (bool add, uint32_t spd_id, uint32_t sa_id) |
| 793 | { |
| 794 | vl_api_ipsec_spd_entry_add_del_t *mp; |
| 795 | vl_api_ipsec_spd_entry_add_del_reply_t *rmp; |
| 796 | char *out = NULL; |
| 797 | int out_len; |
| 798 | status_t rv = FAILED; |
| 799 | |
| 800 | DBG2 (DBG_KNL, "bypass_all [%s] spd_id %d sa_id %d", add ? "ADD" : "DEL", |
| 801 | spd_id, sa_id); |
| 802 | |
| 803 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 804 | memset (mp, 0, sizeof (*mp)); |
| 805 | |
| 806 | u16 msg_id = |
| 807 | vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411"); |
| 808 | mp->_vl_msg_id = ntohs (msg_id); |
| 809 | mp->is_add = add; |
| 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)) |
| 821 | { |
| 822 | DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing"); |
| 823 | goto error; |
| 824 | } |
| 825 | rmp = (void *) out; |
| 826 | if (rmp->retval) |
| 827 | { |
| 828 | DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove", |
| 829 | ntohl (rmp->retval)); |
| 830 | goto error; |
| 831 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 832 | /* address "out" needs to be freed after vec->send */ |
| 833 | if (out != NULL) |
| 834 | { |
| 835 | free (out); |
| 836 | out = NULL; |
| 837 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 838 | mp->entry.is_outbound = 1; |
| 839 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 840 | { |
| 841 | DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing"); |
| 842 | goto error; |
| 843 | } |
| 844 | rmp = (void *) out; |
| 845 | if (rmp->retval) |
| 846 | { |
| 847 | DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove", |
| 848 | ntohl (rmp->retval)); |
| 849 | goto error; |
| 850 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 851 | /* address "out" needs to be freed after vec->send */ |
| 852 | if (out != NULL) |
| 853 | { |
| 854 | free (out); |
| 855 | out = NULL; |
| 856 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 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)) |
| 860 | { |
| 861 | DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing"); |
| 862 | goto error; |
| 863 | } |
| 864 | rmp = (void *) out; |
| 865 | if (rmp->retval) |
| 866 | { |
| 867 | DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove", |
| 868 | ntohl (rmp->retval)); |
| 869 | goto error; |
| 870 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 871 | /* address "out" needs to be freed after vec->send */ |
| 872 | if (out != NULL) |
| 873 | { |
| 874 | free (out); |
| 875 | out = NULL; |
| 876 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 877 | mp->entry.is_outbound = 1; |
| 878 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 879 | { |
| 880 | DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing"); |
| 881 | goto error; |
| 882 | } |
| 883 | rmp = (void *) out; |
| 884 | if (rmp->retval) |
| 885 | { |
| 886 | DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove", |
| 887 | ntohl (rmp->retval)); |
| 888 | goto error; |
| 889 | } |
| 890 | |
| 891 | rv = SUCCESS; |
| 892 | |
| 893 | error: |
| 894 | if (out) |
| 895 | free (out); |
| 896 | vl_msg_api_free (mp); |
| 897 | |
| 898 | return rv; |
| 899 | } |
| 900 | |
| 901 | static int |
| 902 | bypass_port (bool add, uint32_t spd_id, uint32_t sa_id, uint16_t port) |
| 903 | { |
| 904 | vl_api_ipsec_spd_entry_add_del_t *mp; |
| 905 | vl_api_ipsec_spd_entry_add_del_reply_t *rmp; |
| 906 | char *out = NULL; |
| 907 | int out_len; |
| 908 | status_t rv = FAILED; |
| 909 | |
| 910 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 911 | memset (mp, 0, sizeof (*mp)); |
| 912 | |
| 913 | u16 msg_id = |
| 914 | vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411"); |
| 915 | mp->_vl_msg_id = ntohs (msg_id); |
| 916 | mp->is_add = add; |
| 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; |
| 927 | |
| 928 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 929 | { |
| 930 | DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing"); |
| 931 | goto error; |
| 932 | } |
| 933 | rmp = (void *) out; |
| 934 | if (rmp->retval) |
| 935 | { |
| 936 | DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove", |
| 937 | ntohl (rmp->retval)); |
| 938 | goto error; |
| 939 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 940 | /* address "out" needs to be freed after vec->send */ |
| 941 | if (out != NULL) |
| 942 | { |
| 943 | free (out); |
| 944 | out = NULL; |
| 945 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 946 | mp->entry.is_outbound = 1; |
| 947 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 948 | { |
| 949 | DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing"); |
| 950 | goto error; |
| 951 | } |
| 952 | rmp = (void *) out; |
| 953 | if (rmp->retval) |
| 954 | { |
| 955 | DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove", |
| 956 | ntohl (rmp->retval)); |
| 957 | goto error; |
| 958 | } |
| 959 | rv = SUCCESS; |
| 960 | |
| 961 | error: |
| 962 | if (out) |
| 963 | free (out); |
| 964 | vl_msg_api_free (mp); |
| 965 | |
| 966 | return rv; |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * Add or remove a bypass policy |
| 971 | */ |
| 972 | static status_t |
| 973 | manage_bypass (bool add, uint32_t spd_id, uint32_t sa_id) |
| 974 | { |
| 975 | uint16_t port; |
| 976 | status_t rv; |
| 977 | |
| 978 | bypass_all (add, spd_id, sa_id); |
| 979 | |
| 980 | port = |
| 981 | lib->settings->get_int (lib->settings, "%s.port", IKEV2_UDP_PORT, lib->ns); |
| 982 | |
| 983 | if (port) |
| 984 | { |
| 985 | rv = bypass_port (add, spd_id, sa_id, port); |
| 986 | if (rv != SUCCESS) |
| 987 | { |
| 988 | return rv; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | port = lib->settings->get_int (lib->settings, "%s.port_nat_t", |
| 993 | IKEV2_NATT_PORT, lib->ns); |
| 994 | if (port) |
| 995 | { |
| 996 | rv = bypass_port (add, spd_id, sa_id, port); |
| 997 | if (rv != SUCCESS) |
| 998 | { |
| 999 | return rv; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | return SUCCESS; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Add or remove a policy |
| 1008 | */ |
| 1009 | static status_t |
| 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) |
| 1013 | { |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1014 | spd_t *spd = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1015 | char *out = NULL, *interface = NULL; |
| 1016 | int out_len; |
| 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; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 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; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1024 | bool n_spd = false; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1025 | vl_api_ipsec_spd_dump_t *mp_dump = NULL; |
| 1026 | vl_api_ipsec_spd_details_t *rmp_dump = NULL, *tmp = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1027 | |
| 1028 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 1029 | memset (mp, 0, sizeof (*mp)); |
| 1030 | |
| 1031 | this->mutex->lock (this->mutex); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1032 | if (id->dir == POLICY_FWD) |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1033 | { |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1034 | DBG1 (DBG_KNL, "policy FWD interface"); |
| 1035 | rv = SUCCESS; |
| 1036 | goto error; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1037 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1038 | addr = id->dir == POLICY_IN ? data->dst : data->src; |
| 1039 | for (int i = 0; i < N_RETRY_GET_IF; i++) |
| 1040 | { |
| 1041 | if (!charon->kernel->get_interface (charon->kernel, addr, &interface)) |
| 1042 | { |
| 1043 | DBG1 (DBG_KNL, "policy no interface %H", addr); |
| 1044 | free (interface); |
| 1045 | interface = NULL; |
| 1046 | sleep (1); |
| 1047 | } |
| 1048 | |
| 1049 | if (interface) |
| 1050 | { |
| 1051 | DBG1 (DBG_KNL, "policy have interface %H", addr); |
| 1052 | break; |
| 1053 | } |
| 1054 | } |
| 1055 | if (!interface) |
| 1056 | goto error; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1057 | |
| 1058 | DBG2 (DBG_KNL, "manage policy [%s] interface [%s]", add ? "ADD" : "DEL", |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1059 | interface); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1060 | |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1061 | spd = this->spds->get (this->spds, interface); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1062 | if (!spd) |
| 1063 | { |
| 1064 | if (!add) |
| 1065 | { |
| 1066 | DBG1 (DBG_KNL, "SPD for %s not found, should not be deleted", |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1067 | interface); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1068 | goto error; |
| 1069 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1070 | sw_if_index = get_sw_if_index (interface); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1071 | DBG1 (DBG_KNL, "firstly created, spd for %s found sw_if_index is %d", |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1072 | interface, sw_if_index); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1073 | if (sw_if_index == ~0) |
| 1074 | { |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1075 | DBG1 (DBG_KNL, "sw_if_index for %s not found", interface); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1076 | goto error; |
| 1077 | } |
| 1078 | spd_id = ref_get (&this->next_spd_id); |
| 1079 | if (spd_add_del (TRUE, spd_id)) |
| 1080 | { |
| 1081 | DBG1 (DBG_KNL, "spd_add_del %d failed!!!!!", spd_id); |
| 1082 | goto error; |
| 1083 | } |
| 1084 | if (interface_add_del_spd (TRUE, spd_id, sw_if_index)) |
| 1085 | { |
| 1086 | DBG1 (DBG_KNL, "interface_add_del_spd %d %d failed!!!!!", spd_id, |
| 1087 | sw_if_index); |
| 1088 | goto error; |
| 1089 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 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); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1093 | n_spd = true; |
| 1094 | } |
| 1095 | |
| 1096 | auto_priority = calculate_priority (data->prio, id->src_ts, id->dst_ts); |
| 1097 | priority = data->manual_prio ? data->manual_prio : auto_priority; |
| 1098 | |
| 1099 | u16 msg_id = |
| 1100 | vl_msg_api_get_msg_index ((u8 *) "ipsec_spd_entry_add_del_338b7411"); |
| 1101 | mp->_vl_msg_id = htons (msg_id); |
| 1102 | mp->is_add = add; |
| 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; |
| 1106 | switch (data->type) |
| 1107 | { |
| 1108 | case POLICY_IPSEC: |
| 1109 | mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_PROTECT); |
| 1110 | break; |
| 1111 | case POLICY_PASS: |
| 1112 | mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_BYPASS); |
| 1113 | break; |
| 1114 | case POLICY_DROP: |
| 1115 | mp->entry.policy = htonl (IPSEC_API_SPD_ACTION_DISCARD); |
| 1116 | break; |
| 1117 | } |
| 1118 | if ((data->type == POLICY_IPSEC) && data->sa) |
| 1119 | { |
| 1120 | kernel_ipsec_sa_id_t id = { |
| 1121 | .src = data->src, |
| 1122 | .dst = data->dst, |
| 1123 | .proto = data->sa->esp.use ? IPPROTO_ESP : IPPROTO_AH, |
| 1124 | .spi = data->sa->esp.use ? data->sa->esp.spi : data->sa->ah.spi, |
| 1125 | }; |
| 1126 | sa_t *sa = NULL; |
| 1127 | sa = this->sas->get (this->sas, &id); |
| 1128 | if (!sa) |
| 1129 | { |
| 1130 | DBG1 (DBG_KNL, "SA ID not found"); |
| 1131 | goto error; |
| 1132 | } |
| 1133 | sad_id = sa->sa_id; |
| 1134 | if (n_spd) |
| 1135 | { |
| 1136 | if (manage_bypass (TRUE, spd_id, ~0)) |
| 1137 | { |
| 1138 | DBG1 (DBG_KNL, "manage_bypass %d failed!!!!", spd_id); |
| 1139 | goto error; |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | mp->entry.sa_id = htonl (sad_id); |
| 1145 | |
| 1146 | bool is_ipv6 = false; |
| 1147 | if (id->src_ts->get_type (id->src_ts) == TS_IPV6_ADDR_RANGE) |
| 1148 | { |
| 1149 | is_ipv6 = true; |
| 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); |
| 1154 | } |
| 1155 | else |
| 1156 | { |
| 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); |
| 1161 | } |
| 1162 | mp->entry.protocol = id->src_ts->get_protocol (id->src_ts); |
| 1163 | |
| 1164 | if (id->dir == POLICY_OUT) |
| 1165 | { |
| 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); |
| 1172 | } |
| 1173 | else |
| 1174 | { |
| 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); |
| 1181 | } |
| 1182 | |
| 1183 | if (src->is_anyaddr (src) && dst->is_anyaddr (dst)) |
| 1184 | { |
| 1185 | memset (mp->entry.local_address_stop.un.ip6, 0xFF, 16); |
| 1186 | memset (mp->entry.remote_address_stop.un.ip6, 0xFF, 16); |
| 1187 | } |
| 1188 | else |
| 1189 | { |
| 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); |
| 1202 | } |
| 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)); |
| 1207 | |
Gabriel Oginski | 8de66c0 | 2023-02-03 08:12:36 +0000 | [diff] [blame] | 1208 | /* check if policy exists in SPD */ |
Gabriel Oginski | 8de66c0 | 2023-02-03 08:12:36 +0000 | [diff] [blame] | 1209 | mp_dump = vl_msg_api_alloc (sizeof (*mp_dump)); |
| 1210 | memset (mp_dump, 0, sizeof (*mp_dump)); |
| 1211 | |
| 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); |
| 1216 | |
| 1217 | if (vac->send_dump (vac, (char *) mp_dump, sizeof (*mp_dump), &out, |
| 1218 | &out_len)) |
| 1219 | { |
| 1220 | DBG1 (DBG_KNL, "vac %s SPD lookup failed", add ? "adding" : "removing"); |
| 1221 | goto error; |
| 1222 | } |
| 1223 | |
| 1224 | int num = out_len / sizeof (*rmp_dump); |
| 1225 | tmp = (void *) out; |
| 1226 | |
| 1227 | /* found existing policy */ |
| 1228 | if (add && num) |
| 1229 | { |
| 1230 | int i; |
| 1231 | for (i = 0; i < num; i++) |
| 1232 | { |
| 1233 | rmp_dump = tmp; |
| 1234 | tmp += 1; |
| 1235 | /* check if found entry equals the new one */ |
| 1236 | if (policy_equals (&mp->entry, &rmp_dump->entry)) |
| 1237 | goto next; |
| 1238 | } |
| 1239 | } |
| 1240 | else if (!add && num == 0) |
| 1241 | { |
| 1242 | /* VPP doesn't have any policy to delete */ |
| 1243 | goto next; |
| 1244 | } |
| 1245 | |
| 1246 | free (out); |
| 1247 | |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1248 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 1249 | { |
| 1250 | DBG1 (DBG_KNL, "vac %s SPD entry failed", add ? "adding" : "removing"); |
| 1251 | goto error; |
| 1252 | } |
| 1253 | rmp = (void *) out; |
| 1254 | if (rmp->retval) |
| 1255 | { |
| 1256 | DBG1 (DBG_KNL, "%s SPD entry failed rv:%d", add ? "add" : "remove", |
| 1257 | ntohl (rmp->retval)); |
| 1258 | goto error; |
| 1259 | } |
| 1260 | |
Gabriel Oginski | 8de66c0 | 2023-02-03 08:12:36 +0000 | [diff] [blame] | 1261 | next: |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1262 | if (add) |
| 1263 | { |
| 1264 | ref_get (&spd->policy_num); |
| 1265 | } |
| 1266 | else |
| 1267 | { |
| 1268 | if (ref_put (&spd->policy_num)) |
| 1269 | { |
| 1270 | DBG1 ( |
| 1271 | DBG_KNL, |
| 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); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1277 | this->spds->remove (this->spds, interface); |
| 1278 | if (spd->if_name) |
| 1279 | { |
| 1280 | free (spd->if_name); |
| 1281 | spd->if_name = NULL; |
| 1282 | } |
| 1283 | if (spd) |
| 1284 | { |
| 1285 | free (spd); |
| 1286 | spd = NULL; |
| 1287 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | if (this->install_routes && id->dir == POLICY_OUT && !mp->entry.protocol) |
| 1292 | { |
| 1293 | if (data->type == POLICY_IPSEC && data->sa->mode != MODE_TRANSPORT) |
| 1294 | { |
| 1295 | manage_route (this, add, id->dst_ts, data->src, data->dst); |
| 1296 | } |
| 1297 | } |
| 1298 | rv = SUCCESS; |
| 1299 | error: |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1300 | if (out != NULL) |
| 1301 | free (out); |
| 1302 | if (mp_dump != NULL) |
| 1303 | vl_msg_api_free (mp_dump); |
| 1304 | if (mp != NULL) |
| 1305 | vl_msg_api_free (mp); |
| 1306 | if (src != NULL) |
| 1307 | src->destroy (src); |
| 1308 | if (dst != NULL) |
| 1309 | dst->destroy (dst); |
| 1310 | if (interface != NULL) |
| 1311 | free (interface); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1312 | this->mutex->unlock (this->mutex); |
| 1313 | return rv; |
| 1314 | } |
| 1315 | |
| 1316 | METHOD (kernel_ipsec_t, get_features, kernel_feature_t, |
| 1317 | private_kernel_vpp_ipsec_t *this) |
| 1318 | { |
| 1319 | return KERNEL_ESP_V3_TFC; |
| 1320 | } |
| 1321 | |
| 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) |
| 1324 | { |
| 1325 | static const u_int p = 268435399, offset = 0xc0000000; |
| 1326 | |
| 1327 | *spi = htonl (offset + permute (ref_get (&this->nextspi) ^ this->mixspi, p)); |
| 1328 | return SUCCESS; |
| 1329 | } |
| 1330 | |
| 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) |
| 1333 | { |
| 1334 | DBG1 (DBG_KNL, "get_cpi is not supported!!!!!!!!!!!!!!!!!!!!!!!!"); |
| 1335 | return NOT_SUPPORTED; |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * Helper struct for expiration events |
| 1340 | */ |
| 1341 | typedef struct |
| 1342 | { |
| 1343 | |
| 1344 | private_kernel_vpp_ipsec_t *manager; |
| 1345 | |
| 1346 | kernel_ipsec_sa_id_t *sa_id; |
| 1347 | |
| 1348 | /** |
| 1349 | * 0 if this is a hard expire, otherwise the offset in s (soft->hard) |
| 1350 | */ |
| 1351 | uint32_t hard_offset; |
| 1352 | |
| 1353 | } vpp_sa_expired_t; |
| 1354 | |
| 1355 | /** |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1356 | * Clean up expire data |
| 1357 | */ |
| 1358 | static void |
| 1359 | expire_data_destroy (vpp_sa_expired_t *data) |
| 1360 | { |
| 1361 | free (data); |
| 1362 | } |
| 1363 | |
| 1364 | /** |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1365 | * Callback for expiration events |
| 1366 | */ |
| 1367 | static job_requeue_t |
| 1368 | sa_expired (vpp_sa_expired_t *expired) |
| 1369 | { |
| 1370 | private_kernel_vpp_ipsec_t *this = expired->manager; |
| 1371 | sa_t *sa; |
| 1372 | kernel_ipsec_sa_id_t *id = expired->sa_id; |
| 1373 | |
| 1374 | this->mutex->lock (this->mutex); |
| 1375 | sa = this->sas->get (this->sas, id); |
| 1376 | |
| 1377 | if (sa) |
| 1378 | { |
| 1379 | charon->kernel->expire (charon->kernel, id->proto, id->spi, id->dst, |
| 1380 | FALSE); |
| 1381 | } |
| 1382 | |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1383 | if (id->src) |
| 1384 | id->src->destroy (id->src); |
| 1385 | if (id->dst) |
| 1386 | id->dst->destroy (id->dst); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1387 | free (id); |
| 1388 | this->mutex->unlock (this->mutex); |
| 1389 | return JOB_REQUEUE_NONE; |
| 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * Schedule a job to handle IPsec SA expiration |
| 1394 | */ |
| 1395 | static void |
| 1396 | schedule_expiration (private_kernel_vpp_ipsec_t *this, |
| 1397 | kernel_ipsec_add_sa_t *entry, |
| 1398 | kernel_ipsec_sa_id_t *entry2) |
| 1399 | { |
| 1400 | lifetime_cfg_t *lifetime = entry->lifetime; |
| 1401 | vpp_sa_expired_t *expired; |
| 1402 | callback_job_t *job; |
| 1403 | uint32_t timeout; |
| 1404 | kernel_ipsec_sa_id_t *id; |
| 1405 | |
| 1406 | if (!lifetime->time.life) |
| 1407 | { /* no expiration at all */ |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | INIT (id, .src = entry2->src->clone (entry2->src), |
| 1412 | .dst = entry2->dst->clone (entry2->dst), .spi = entry2->spi, |
| 1413 | .proto = entry2->proto, ); |
| 1414 | |
| 1415 | INIT (expired, .manager = this, .sa_id = id, ); |
| 1416 | |
| 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; |
| 1420 | |
| 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; |
| 1425 | } |
| 1426 | |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1427 | job = |
| 1428 | callback_job_create ((callback_job_cb_t) sa_expired, expired, |
| 1429 | (callback_job_cleanup_t) expire_data_destroy, NULL); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1430 | lib->scheduler->schedule_job (lib->scheduler, (job_t *) job, timeout); |
| 1431 | } |
| 1432 | |
| 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) |
| 1435 | { |
| 1436 | char *out = NULL; |
| 1437 | int out_len; |
| 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; |
| 1443 | chunk_t src, dst; |
| 1444 | kernel_ipsec_sa_id_t *sa_id; |
| 1445 | sa_t *sa; |
| 1446 | int key_len = data->enc_key.len; |
| 1447 | |
| 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)) |
| 1452 | { |
| 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; |
| 1456 | } |
| 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)); |
| 1461 | u16 msg_id = |
| 1462 | vl_msg_api_get_msg_index ((u8 *) "ipsec_sad_entry_add_del_ab64b5c6"); |
| 1463 | mp->_vl_msg_id = htons (msg_id); |
| 1464 | mp->is_add = 1; |
| 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); |
| 1469 | |
| 1470 | switch (data->enc_alg) |
| 1471 | { |
| 1472 | case ENCR_NULL: |
| 1473 | ca = IPSEC_API_CRYPTO_ALG_NONE; |
| 1474 | break; |
| 1475 | case ENCR_AES_CBC: |
| 1476 | switch (key_len * 8) |
| 1477 | { |
| 1478 | case 128: |
| 1479 | ca = IPSEC_API_CRYPTO_ALG_AES_CBC_128; |
| 1480 | break; |
| 1481 | case 192: |
| 1482 | ca = IPSEC_API_CRYPTO_ALG_AES_CBC_192; |
| 1483 | break; |
| 1484 | case 256: |
| 1485 | ca = IPSEC_API_CRYPTO_ALG_AES_CBC_256; |
| 1486 | break; |
| 1487 | default: |
| 1488 | DBG1 (DBG_KNL, "Key length %d is not supported by VPP!", |
| 1489 | key_len * 8); |
| 1490 | goto error; |
| 1491 | } |
| 1492 | break; |
| 1493 | case ENCR_AES_CTR: |
| 1494 | switch (key_len * 8) |
| 1495 | { |
| 1496 | case 128: |
| 1497 | ca = IPSEC_API_CRYPTO_ALG_AES_CTR_128; |
| 1498 | break; |
| 1499 | case 192: |
| 1500 | ca = IPSEC_API_CRYPTO_ALG_AES_CTR_192; |
| 1501 | break; |
| 1502 | case 256: |
| 1503 | ca = IPSEC_API_CRYPTO_ALG_AES_CTR_256; |
| 1504 | break; |
| 1505 | default: |
| 1506 | DBG1 (DBG_KNL, "Key length %d is not supported by VPP!", |
| 1507 | key_len * 8); |
| 1508 | goto error; |
| 1509 | } |
| 1510 | break; |
| 1511 | case ENCR_AES_GCM_ICV8: |
| 1512 | case ENCR_AES_GCM_ICV12: |
| 1513 | case ENCR_AES_GCM_ICV16: |
| 1514 | switch (key_len * 8) |
| 1515 | { |
| 1516 | case 128: |
| 1517 | ca = IPSEC_API_CRYPTO_ALG_AES_GCM_128; |
| 1518 | break; |
| 1519 | case 192: |
| 1520 | ca = IPSEC_API_CRYPTO_ALG_AES_GCM_192; |
| 1521 | break; |
| 1522 | case 256: |
| 1523 | ca = IPSEC_API_CRYPTO_ALG_AES_GCM_256; |
| 1524 | break; |
| 1525 | default: |
| 1526 | DBG1 (DBG_KNL, "Key length %d is not supported by VPP!", |
| 1527 | key_len * 8); |
| 1528 | goto error; |
| 1529 | } |
| 1530 | break; |
| 1531 | case ENCR_DES: |
| 1532 | ca = IPSEC_API_CRYPTO_ALG_DES_CBC; |
| 1533 | break; |
| 1534 | case ENCR_3DES: |
| 1535 | ca = IPSEC_API_CRYPTO_ALG_3DES_CBC; |
| 1536 | break; |
| 1537 | default: |
| 1538 | DBG1 (DBG_KNL, "algorithm %N not supported by VPP!", |
| 1539 | encryption_algorithm_names, data->enc_alg); |
| 1540 | goto error; |
| 1541 | } |
| 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); |
| 1546 | |
| 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)) |
| 1552 | { |
| 1553 | memcpy (&mp->entry.salt, data->enc_key.ptr + mp->entry.crypto_key.length, |
| 1554 | 4); |
| 1555 | } |
| 1556 | |
| 1557 | switch (data->int_alg) |
| 1558 | { |
| 1559 | case AUTH_UNDEFINED: |
| 1560 | ia = IPSEC_API_INTEG_ALG_NONE; |
| 1561 | break; |
| 1562 | case AUTH_HMAC_MD5_96: |
| 1563 | ia = IPSEC_API_INTEG_ALG_MD5_96; |
| 1564 | break; |
| 1565 | case AUTH_HMAC_SHA1_96: |
| 1566 | ia = IPSEC_API_INTEG_ALG_SHA1_96; |
| 1567 | break; |
| 1568 | case AUTH_HMAC_SHA2_256_96: |
| 1569 | ia = IPSEC_API_INTEG_ALG_SHA_256_96; |
| 1570 | break; |
| 1571 | case AUTH_HMAC_SHA2_256_128: |
| 1572 | ia = IPSEC_API_INTEG_ALG_SHA_256_128; |
| 1573 | break; |
| 1574 | case AUTH_HMAC_SHA2_384_192: |
| 1575 | ia = IPSEC_API_INTEG_ALG_SHA_384_192; |
| 1576 | break; |
| 1577 | case AUTH_HMAC_SHA2_512_256: |
| 1578 | ia = IPSEC_API_INTEG_ALG_SHA_512_256; |
| 1579 | break; |
| 1580 | default: |
| 1581 | DBG1 (DBG_KNL, "algorithm %N not supported by VPP!", |
| 1582 | integrity_algorithm_names, data->int_alg); |
| 1583 | goto error; |
| 1584 | break; |
| 1585 | } |
| 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); |
| 1591 | |
| 1592 | int flags = IPSEC_API_SAD_FLAG_NONE; |
Atzm Watanabe | fc2d95d | 2023-01-25 14:11:10 +0900 | [diff] [blame] | 1593 | if (data->inbound) |
| 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; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1599 | if (data->esn) |
| 1600 | flags |= IPSEC_API_SAD_FLAG_USE_ESN; |
Atzm Watanabe | fc2d95d | 2023-01-25 14:11:10 +0900 | [diff] [blame] | 1601 | if (this->use_tunnel_mode_sa && data->mode == MODE_TUNNEL) |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1602 | { |
| 1603 | if (id->src->get_family (id->src) == AF_INET6) |
| 1604 | flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL_V6; |
| 1605 | else |
| 1606 | flags |= IPSEC_API_SAD_FLAG_IS_TUNNEL; |
| 1607 | } |
| 1608 | if (data->encap) |
| 1609 | { |
Gabriel Oginski | 980f3fb | 2023-04-17 07:00:24 +0000 | [diff] [blame] | 1610 | DBG1 (DBG_KNL, "UDP encap"); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1611 | flags |= IPSEC_API_SAD_FLAG_UDP_ENCAP; |
Gabriel Oginski | 980f3fb | 2023-04-17 07:00:24 +0000 | [diff] [blame] | 1612 | mp->entry.udp_src_port = htons (natt_port); |
| 1613 | mp->entry.udp_dst_port = htons (natt_port); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1614 | } |
| 1615 | mp->entry.flags = htonl (flags); |
| 1616 | |
| 1617 | bool is_ipv6 = false; |
| 1618 | if (id->src->get_family (id->src) == AF_INET6) |
| 1619 | { |
| 1620 | is_ipv6 = true; |
| 1621 | mp->entry.tunnel_src.af = htonl (ADDRESS_IP6); |
| 1622 | mp->entry.tunnel_dst.af = htonl (ADDRESS_IP6); |
| 1623 | } |
| 1624 | else |
| 1625 | { |
| 1626 | mp->entry.tunnel_src.af = htonl (ADDRESS_IP4); |
| 1627 | mp->entry.tunnel_dst.af = htonl (ADDRESS_IP4); |
| 1628 | } |
| 1629 | src = id->src->get_address (id->src); |
| 1630 | memcpy (is_ipv6 ? mp->entry.tunnel_src.un.ip6 : mp->entry.tunnel_src.un.ip4, |
| 1631 | src.ptr, src.len); |
| 1632 | dst = id->dst->get_address (id->dst); |
| 1633 | memcpy (is_ipv6 ? mp->entry.tunnel_dst.un.ip6 : mp->entry.tunnel_dst.un.ip4, |
| 1634 | dst.ptr, dst.len); |
| 1635 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 1636 | { |
| 1637 | DBG1 (DBG_KNL, "vac adding SA failed"); |
| 1638 | goto error; |
| 1639 | } |
| 1640 | rmp = (void *) out; |
| 1641 | if (rmp->retval) |
| 1642 | { |
| 1643 | DBG1 (DBG_KNL, "add SA failed rv:%d", ntohl (rmp->retval)); |
| 1644 | goto error; |
| 1645 | } |
| 1646 | |
| 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, ); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1650 | INIT (sa, .sa_id = sad_id, .stat_index = ntohl (rmp->stat_index), |
| 1651 | .sa_id_p = sa_id, ); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 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); |
| 1656 | rv = SUCCESS; |
| 1657 | |
| 1658 | error: |
| 1659 | free (out); |
| 1660 | vl_msg_api_free (mp); |
| 1661 | return rv; |
| 1662 | } |
| 1663 | |
| 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) |
| 1666 | { |
| 1667 | DBG1 (DBG_KNL, |
| 1668 | "update sa not supported!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); |
| 1669 | return NOT_SUPPORTED; |
| 1670 | } |
| 1671 | |
| 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) |
| 1675 | { |
| 1676 | status_t rv = FAILED; |
| 1677 | sa_t *sa; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1678 | u32 *dir = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1679 | int i, k; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1680 | stat_segment_data_t *res = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1681 | u8 **pattern = 0; |
| 1682 | uint64_t res_bytes = 0; |
| 1683 | uint64_t res_packets = 0; |
| 1684 | |
| 1685 | this->mutex->lock (this->mutex); |
| 1686 | sa = this->sas->get (this->sas, id); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1687 | if (!sa) |
| 1688 | { |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1689 | this->mutex->unlock (this->mutex); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1690 | DBG1 (DBG_KNL, "SA not found"); |
| 1691 | return NOT_FOUND; |
| 1692 | } |
| 1693 | |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1694 | if (this->sm == NULL) |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1695 | { |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1696 | stat_client_main_t *sm = NULL; |
| 1697 | sm = stat_client_get (); |
| 1698 | |
| 1699 | if (!sm) |
| 1700 | { |
| 1701 | DBG1 (DBG_KNL, "Not connecting with stats segmentation"); |
| 1702 | this->mutex->unlock (this->mutex); |
| 1703 | return NOT_FOUND; |
| 1704 | } |
| 1705 | this->sm = sm; |
| 1706 | int rv_stat = stat_segment_connect_r ("/run/vpp/stats.sock", this->sm); |
| 1707 | if (rv_stat != 0) |
| 1708 | { |
| 1709 | stat_client_free (this->sm); |
| 1710 | this->sm = NULL; |
| 1711 | DBG1 (DBG_KNL, "Not connecting with stats segmentation"); |
| 1712 | this->mutex->unlock (this->mutex); |
| 1713 | return NOT_FOUND; |
| 1714 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1715 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1716 | |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1717 | vec_add1 (pattern, (u8 *) "/net/ipsec/sa"); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1718 | dir = stat_segment_ls_r ((u8 **) pattern, this->sm); |
| 1719 | res = stat_segment_dump_r (dir, this->sm); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1720 | /* i-loop for each results find by pattern - here two: |
| 1721 | * 1. /net/ipsec/sa |
| 1722 | * 2. /net/ipsec/sa/lost |
| 1723 | */ |
| 1724 | for (i = 0; i < vec_len (res); i++) |
| 1725 | { |
| 1726 | switch (res[i].type) |
| 1727 | { |
| 1728 | /* type for how many packets are lost */ |
| 1729 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
| 1730 | if (res[i].simple_counter_vec == 0) |
| 1731 | continue; |
| 1732 | break; |
| 1733 | /* type for counter for each SA */ |
| 1734 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
| 1735 | if (res[i].combined_counter_vec == 0) |
| 1736 | continue; |
| 1737 | /* k-loop for each threads - that you run VPP */ |
| 1738 | for (k = 0; k < vec_len (res[i].combined_counter_vec); k++) |
| 1739 | { |
| 1740 | if (sa->stat_index <= vec_len (res[i].combined_counter_vec[k])) |
| 1741 | { |
| 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); |
| 1745 | res_bytes += |
| 1746 | res[i].combined_counter_vec[k][sa->stat_index].bytes; |
| 1747 | res_packets += |
| 1748 | res[i].combined_counter_vec[k][sa->stat_index].packets; |
| 1749 | } |
| 1750 | } |
| 1751 | break; |
| 1752 | case STAT_DIR_TYPE_NAME_VECTOR: |
| 1753 | if (res[i].name_vector == 0) |
| 1754 | continue; |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1758 | |
| 1759 | vec_free (pattern); |
| 1760 | vec_free (dir); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1761 | stat_segment_data_free (res); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1762 | |
| 1763 | if (bytes) |
| 1764 | { |
| 1765 | *bytes = res_bytes; |
| 1766 | } |
| 1767 | if (packets) |
| 1768 | { |
| 1769 | *packets = res_packets; |
| 1770 | } |
| 1771 | if (time) |
| 1772 | { |
| 1773 | *time = 0; |
| 1774 | } |
| 1775 | |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1776 | this->mutex->unlock (this->mutex); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1777 | rv = SUCCESS; |
| 1778 | return rv; |
| 1779 | } |
| 1780 | |
| 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) |
| 1783 | { |
| 1784 | char *out = NULL; |
| 1785 | int out_len; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1786 | vl_api_ipsec_sad_entry_add_del_t *mp = NULL; |
| 1787 | vl_api_ipsec_sad_entry_add_del_reply_t *rmp = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1788 | status_t rv = FAILED; |
| 1789 | sa_t *sa; |
| 1790 | |
| 1791 | this->mutex->lock (this->mutex); |
| 1792 | sa = this->sas->get (this->sas, id); |
| 1793 | if (!sa) |
| 1794 | { |
| 1795 | DBG1 (DBG_KNL, "SA not found"); |
| 1796 | rv = NOT_FOUND; |
| 1797 | goto error; |
| 1798 | } |
| 1799 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 1800 | memset (mp, 0, sizeof (*mp)); |
| 1801 | mp->is_add = 0; |
| 1802 | u16 msg_id = |
| 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); |
| 1806 | |
| 1807 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 1808 | { |
| 1809 | DBG1 (DBG_KNL, "vac removing SA failed"); |
| 1810 | goto error; |
| 1811 | } |
| 1812 | rmp = (void *) out; |
| 1813 | if (rmp->retval) |
| 1814 | { |
| 1815 | DBG1 (DBG_KNL, "del SA failed rv:%d", ntohl (rmp->retval)); |
| 1816 | goto error; |
| 1817 | } |
| 1818 | |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1819 | void *temp = this->sas->remove (this->sas, id); |
| 1820 | if (sa->sa_id_p) |
| 1821 | { |
| 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); |
| 1826 | free (sa->sa_id_p); |
| 1827 | } |
| 1828 | free (sa); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1829 | rv = SUCCESS; |
| 1830 | error: |
| 1831 | free (out); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1832 | vl_msg_api_free (mp); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1833 | this->mutex->unlock (this->mutex); |
| 1834 | return rv; |
| 1835 | } |
| 1836 | |
| 1837 | METHOD (kernel_ipsec_t, flush_sas, status_t, private_kernel_vpp_ipsec_t *this) |
| 1838 | { |
| 1839 | enumerator_t *enumerator; |
| 1840 | int out_len; |
| 1841 | char *out; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1842 | vl_api_ipsec_sad_entry_add_del_t *mp = NULL; |
| 1843 | vl_api_ipsec_sad_entry_add_del_reply_t *rmp = NULL; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1844 | sa_t *sa = NULL; |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1845 | status_t rv = FAILED; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1846 | |
| 1847 | this->mutex->lock (this->mutex); |
| 1848 | enumerator = this->sas->create_enumerator (this->sas); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1849 | while (enumerator->enumerate (enumerator, &sa)) |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1850 | { |
| 1851 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 1852 | memset (mp, 0, sizeof (*mp)); |
| 1853 | u16 msg_id = |
| 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); |
| 1857 | mp->is_add = 0; |
| 1858 | if (vac->send (vac, (char *) mp, sizeof (*mp), &out, &out_len)) |
| 1859 | { |
| 1860 | DBG1 (DBG_KNL, "flush_sas failed!!!!"); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1861 | goto error; |
| 1862 | } |
| 1863 | rmp = (void *) out; |
| 1864 | if (rmp->retval) |
| 1865 | { |
| 1866 | DBG1 (DBG_KNL, "flush_sas failed!!!! rv: %d", ntohl (rmp->retval)); |
| 1867 | goto error; |
| 1868 | } |
| 1869 | if (sa->sa_id_p) |
| 1870 | { |
| 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); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1875 | } |
| 1876 | free (out); |
| 1877 | vl_msg_api_free (mp); |
| 1878 | this->sas->remove_at (this->sas, enumerator); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1879 | free (sa->sa_id_p); |
| 1880 | free (sa); |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1881 | } |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1882 | rv = SUCCESS; |
| 1883 | error: |
| 1884 | if (out != NULL) |
| 1885 | free (out); |
| 1886 | if (mp != NULL) |
| 1887 | vl_msg_api_free (mp); |
| 1888 | |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1889 | enumerator->destroy (enumerator); |
| 1890 | this->mutex->unlock (this->mutex); |
| 1891 | |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1892 | return rv; |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 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) |
| 1897 | { |
| 1898 | return manage_policy (this, TRUE, id, data); |
| 1899 | } |
| 1900 | |
| 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) |
| 1904 | { |
| 1905 | return NOT_SUPPORTED; |
| 1906 | } |
| 1907 | |
| 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) |
| 1910 | { |
| 1911 | return manage_policy (this, FALSE, id, data); |
| 1912 | } |
| 1913 | |
| 1914 | METHOD (kernel_ipsec_t, flush_policies, status_t, |
| 1915 | private_kernel_vpp_ipsec_t *this) |
| 1916 | { |
| 1917 | return NOT_SUPPORTED; |
| 1918 | } |
| 1919 | |
| 1920 | METHOD (kernel_ipsec_t, bypass_socket, bool, private_kernel_vpp_ipsec_t *this, |
| 1921 | int fd, int family) |
| 1922 | { |
| 1923 | return FALSE; |
| 1924 | } |
| 1925 | |
| 1926 | METHOD (kernel_ipsec_t, enable_udp_decap, bool, |
| 1927 | private_kernel_vpp_ipsec_t *this, int fd, int family, u_int16_t port) |
| 1928 | { |
| 1929 | DBG1 (DBG_KNL, "enable_udp_decap not supported!!!!!!!!!!!!!!!!!!!!!!!!!"); |
| 1930 | return FALSE; |
| 1931 | } |
| 1932 | |
| 1933 | METHOD (kernel_ipsec_t, destroy, void, private_kernel_vpp_ipsec_t *this) |
| 1934 | { |
| 1935 | this->mutex->destroy (this->mutex); |
| 1936 | this->sas->destroy (this->sas); |
| 1937 | this->spds->destroy (this->spds); |
| 1938 | this->routes->destroy (this->routes); |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1939 | if (this->sm) |
| 1940 | { |
| 1941 | stat_segment_disconnect_r (this->sm); |
| 1942 | stat_client_free (this->sm); |
| 1943 | this->sm = NULL; |
| 1944 | } |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1945 | free (this); |
| 1946 | } |
| 1947 | |
| 1948 | kernel_vpp_ipsec_t * |
| 1949 | kernel_vpp_ipsec_create () |
| 1950 | { |
| 1951 | private_kernel_vpp_ipsec_t *this; |
| 1952 | |
| 1953 | INIT(this, |
| 1954 | .public = { |
| 1955 | .interface = { |
| 1956 | .get_features = _get_features, |
| 1957 | .get_spi = _get_spi, |
| 1958 | .get_cpi = _get_cpi, |
| 1959 | .add_sa = _add_sa, |
| 1960 | .update_sa = _update_sa, |
| 1961 | .query_sa = _query_sa, |
| 1962 | .del_sa = _del_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, |
| 1971 | }, |
| 1972 | }, |
| 1973 | .next_sad_id = 0, |
| 1974 | .next_spd_id = 0, |
| 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), |
Atzm Watanabe | fc2d95d | 2023-01-25 14:11:10 +0900 | [diff] [blame] | 1983 | .use_tunnel_mode_sa = lib->settings->get_bool(lib->settings, |
| 1984 | "%s.plugins.kernel-vpp.use_tunnel_mode_sa", |
| 1985 | TRUE, lib->ns), |
Gabriel Oginski | 2da99e5 | 2023-02-14 08:46:36 +0000 | [diff] [blame] | 1986 | .sm = NULL, |
Gabriel Oginski | 4e88e04 | 2022-06-29 12:54:30 +0000 | [diff] [blame] | 1987 | ); |
| 1988 | |
| 1989 | if (!init_spi (this)) |
| 1990 | { |
| 1991 | destroy (this); |
| 1992 | return NULL; |
| 1993 | } |
| 1994 | |
| 1995 | return &this->public; |
| 1996 | } |