Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco 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 | /** |
| 17 | * @file |
| 18 | * @brief Local TCP/IP stack punt infrastructure. |
| 19 | * |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 20 | * Provides a set of VPP nodes together with the relevant APIs and CLI |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 21 | * commands in order to adjust and dispatch packets from the VPP data plane |
| 22 | * to the local TCP/IP stack |
| 23 | */ |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 24 | |
| 25 | #include <vnet/ip/ip.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 26 | #include <vlib/vlib.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | #include <vnet/udp/udp.h> |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 28 | #include <vnet/tcp/tcp.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 29 | #include <vnet/ip/punt.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 30 | #include <vlib/unix/unix.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 31 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/socket.h> |
Marco Varlese | 2234983 | 2017-09-08 10:40:34 +0200 | [diff] [blame] | 35 | #include <sys/uio.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 36 | #include <stdlib.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 37 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 38 | punt_main_t punt_main; |
| 39 | |
| 40 | char * |
| 41 | vnet_punt_get_server_pathname (void) |
| 42 | { |
| 43 | punt_main_t *pm = &punt_main; |
| 44 | return pm->sun_path; |
| 45 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 46 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 47 | static void |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 48 | punt_client_l4_db_add (ip_address_family_t af, u16 port, u32 index) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 49 | { |
| 50 | punt_main_t *pm = &punt_main; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 51 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 52 | pm->db.clients_by_l4_port = hash_set (pm->db.clients_by_l4_port, |
| 53 | punt_client_l4_mk_key (af, port), |
| 54 | index); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 57 | static u32 |
| 58 | punt_client_l4_db_remove (ip_address_family_t af, u16 port) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 59 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 60 | punt_main_t *pm = &punt_main; |
| 61 | u32 key, index = ~0; |
| 62 | uword *p; |
| 63 | |
| 64 | key = punt_client_l4_mk_key (af, port); |
| 65 | p = hash_get (pm->db.clients_by_l4_port, key); |
| 66 | |
| 67 | if (p) |
| 68 | index = p[0]; |
| 69 | |
| 70 | hash_unset (pm->db.clients_by_l4_port, key); |
| 71 | |
| 72 | return (index); |
| 73 | } |
| 74 | |
| 75 | static void |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 76 | punt_client_ip_proto_db_add (ip_address_family_t af, |
| 77 | ip_protocol_t proto, u32 index) |
| 78 | { |
| 79 | punt_main_t *pm = &punt_main; |
| 80 | |
| 81 | pm->db.clients_by_ip_proto = hash_set (pm->db.clients_by_ip_proto, |
| 82 | punt_client_ip_proto_mk_key (af, |
| 83 | proto), |
| 84 | index); |
| 85 | } |
| 86 | |
| 87 | static u32 |
| 88 | punt_client_ip_proto_db_remove (ip_address_family_t af, ip_protocol_t proto) |
| 89 | { |
| 90 | punt_main_t *pm = &punt_main; |
| 91 | u32 key, index = ~0; |
| 92 | uword *p; |
| 93 | |
| 94 | key = punt_client_ip_proto_mk_key (af, proto); |
| 95 | p = hash_get (pm->db.clients_by_ip_proto, key); |
| 96 | |
| 97 | if (p) |
| 98 | index = p[0]; |
| 99 | |
| 100 | hash_unset (pm->db.clients_by_ip_proto, key); |
| 101 | |
| 102 | return (index); |
| 103 | } |
| 104 | |
| 105 | static void |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 106 | punt_client_exception_db_add (vlib_punt_reason_t reason, u32 pci) |
| 107 | { |
| 108 | punt_main_t *pm = &punt_main; |
| 109 | |
| 110 | vec_validate_init_empty (pm->db.clients_by_exception, reason, ~0); |
| 111 | |
| 112 | pm->db.clients_by_exception[reason] = pci; |
| 113 | } |
| 114 | |
| 115 | static u32 |
| 116 | punt_client_exception_db_remove (vlib_punt_reason_t reason) |
| 117 | { |
| 118 | punt_main_t *pm = &punt_main; |
| 119 | u32 pci = ~0; |
| 120 | |
| 121 | if (punt_client_exception_get (reason)) |
| 122 | { |
| 123 | pci = pm->db.clients_by_exception[reason]; |
| 124 | pm->db.clients_by_exception[reason] = ~0; |
| 125 | } |
| 126 | |
| 127 | return pci; |
| 128 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 129 | |
| 130 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 131 | punt_socket_read_ready (clib_file_t * uf) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 132 | { |
| 133 | vlib_main_t *vm = vlib_get_main (); |
| 134 | punt_main_t *pm = &punt_main; |
| 135 | |
| 136 | /** Schedule the rx node */ |
| 137 | vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index); |
| 138 | vec_add1 (pm->ready_fds, uf->file_descriptor); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 143 | static clib_error_t * |
| 144 | punt_socket_register_l4 (vlib_main_t * vm, |
| 145 | ip_address_family_t af, |
| 146 | u8 protocol, u16 port, char *client_pathname) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 147 | { |
| 148 | punt_main_t *pm = &punt_main; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 149 | punt_client_t *c; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 150 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 151 | if (port == (u16) ~ 0) |
Ole Troan | 56b8abc | 2023-09-05 08:27:53 +0200 | [diff] [blame] | 152 | return clib_error_return (0, "Port number required"); |
| 153 | |
| 154 | u32 node_index; |
| 155 | switch (protocol) |
| 156 | { |
| 157 | case IP_PROTOCOL_UDP: |
| 158 | node_index = (af == AF_IP4 ? udp4_punt_socket_node.index : |
| 159 | udp6_punt_socket_node.index); |
| 160 | udp_register_dst_port (vm, port, node_index, af == AF_IP4); |
| 161 | break; |
| 162 | case IP_PROTOCOL_ICMP6: |
| 163 | if (af != AF_IP6) |
| 164 | return clib_error_return ( |
| 165 | 0, "only UDP or ICMP6 protocol (%d, %d) is supported, got %d", |
| 166 | IP_PROTOCOL_UDP, IP_PROTOCOL_ICMP6, protocol); |
| 167 | |
| 168 | node_index = icmp6_punt_socket_node.index; |
| 169 | icmp6_register_type (vm, port, node_index); |
| 170 | break; |
| 171 | default: |
| 172 | return clib_error_return ( |
| 173 | 0, "only UDP or ICMP6 protocol (%d) is supported, got %d", |
| 174 | IP_PROTOCOL_UDP, protocol); |
| 175 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 176 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 177 | c = punt_client_l4_get (af, port); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 178 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 179 | if (NULL == c) |
| 180 | { |
| 181 | pool_get_zero (pm->punt_client_pool, c); |
| 182 | punt_client_l4_db_add (af, port, c - pm->punt_client_pool); |
| 183 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 184 | |
BenoƮt Ganne | edb0d45 | 2021-10-12 10:14:30 +0200 | [diff] [blame] | 185 | snprintf (c->caddr.sun_path, sizeof (c->caddr.sun_path), "%s", |
| 186 | client_pathname); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 187 | c->caddr.sun_family = AF_UNIX; |
| 188 | c->reg.type = PUNT_TYPE_L4; |
| 189 | c->reg.punt.l4.port = port; |
| 190 | c->reg.punt.l4.protocol = protocol; |
| 191 | c->reg.punt.l4.af = af; |
| 192 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 193 | return (NULL); |
| 194 | } |
| 195 | |
| 196 | static clib_error_t * |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 197 | punt_socket_register_ip_proto (vlib_main_t * vm, |
| 198 | ip_address_family_t af, |
| 199 | ip_protocol_t proto, char *client_pathname) |
| 200 | { |
| 201 | punt_main_t *pm = &punt_main; |
| 202 | punt_client_t *c; |
| 203 | |
| 204 | c = punt_client_ip_proto_get (af, proto); |
| 205 | |
| 206 | if (NULL == c) |
| 207 | { |
| 208 | pool_get_zero (pm->punt_client_pool, c); |
| 209 | punt_client_ip_proto_db_add (af, proto, c - pm->punt_client_pool); |
| 210 | } |
| 211 | |
BenoƮt Ganne | edb0d45 | 2021-10-12 10:14:30 +0200 | [diff] [blame] | 212 | snprintf (c->caddr.sun_path, sizeof (c->caddr.sun_path), "%s", |
| 213 | client_pathname); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 214 | c->caddr.sun_family = AF_UNIX; |
| 215 | c->reg.type = PUNT_TYPE_IP_PROTO; |
| 216 | c->reg.punt.ip_proto.protocol = proto; |
| 217 | c->reg.punt.ip_proto.af = af; |
| 218 | |
| 219 | if (af == AF_IP4) |
| 220 | ip4_register_protocol (proto, ip4_proto_punt_socket_node.index); |
| 221 | else |
| 222 | ip6_register_protocol (proto, ip6_proto_punt_socket_node.index); |
| 223 | |
| 224 | return (NULL); |
| 225 | } |
| 226 | |
| 227 | static clib_error_t * |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 228 | punt_socket_register_exception (vlib_main_t * vm, |
| 229 | vlib_punt_reason_t reason, |
| 230 | char *client_pathname) |
| 231 | { |
| 232 | punt_main_t *pm = &punt_main; |
| 233 | punt_client_t *pc; |
| 234 | |
| 235 | pc = punt_client_exception_get (reason); |
| 236 | |
| 237 | if (NULL == pc) |
| 238 | { |
| 239 | pool_get_zero (pm->punt_client_pool, pc); |
| 240 | punt_client_exception_db_add (reason, pc - pm->punt_client_pool); |
| 241 | } |
| 242 | |
BenoƮt Ganne | edb0d45 | 2021-10-12 10:14:30 +0200 | [diff] [blame] | 243 | snprintf (pc->caddr.sun_path, sizeof (pc->caddr.sun_path), "%s", |
| 244 | client_pathname); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 245 | pc->caddr.sun_family = AF_UNIX; |
| 246 | pc->reg.type = PUNT_TYPE_EXCEPTION; |
| 247 | pc->reg.punt.exception.reason = reason; |
| 248 | |
| 249 | vlib_punt_register (pm->hdl, |
| 250 | pc->reg.punt.exception.reason, "exception-punt-socket"); |
| 251 | |
| 252 | return (NULL); |
| 253 | } |
| 254 | |
| 255 | static clib_error_t * |
| 256 | punt_socket_unregister_l4 (ip_address_family_t af, |
| 257 | ip_protocol_t protocol, u16 port) |
| 258 | { |
| 259 | u32 pci; |
| 260 | |
| 261 | udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4); |
| 262 | |
| 263 | pci = punt_client_l4_db_remove (af, port); |
| 264 | |
| 265 | if (~0 != pci) |
| 266 | pool_put_index (punt_main.punt_client_pool, pci); |
| 267 | |
| 268 | return (NULL); |
| 269 | } |
| 270 | |
| 271 | static clib_error_t * |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 272 | punt_socket_unregister_ip_proto (ip_address_family_t af, ip_protocol_t proto) |
| 273 | { |
| 274 | u32 pci; |
| 275 | |
| 276 | if (af == AF_IP4) |
| 277 | ip4_unregister_protocol (proto); |
| 278 | else |
| 279 | ip6_unregister_protocol (proto); |
| 280 | |
| 281 | pci = punt_client_ip_proto_db_remove (af, proto); |
| 282 | |
| 283 | if (~0 != pci) |
| 284 | pool_put_index (punt_main.punt_client_pool, pci); |
| 285 | |
| 286 | return (NULL); |
| 287 | } |
| 288 | |
| 289 | static clib_error_t * |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 290 | punt_socket_unregister_exception (vlib_punt_reason_t reason) |
| 291 | { |
| 292 | u32 pci; |
| 293 | |
| 294 | pci = punt_client_exception_db_remove (reason); |
| 295 | |
| 296 | if (~0 != pci) |
| 297 | pool_put_index (punt_main.punt_client_pool, pci); |
| 298 | |
| 299 | return (NULL); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | clib_error_t * |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 303 | vnet_punt_socket_add (vlib_main_t * vm, u32 header_version, |
| 304 | const punt_reg_t * pr, char *client_pathname) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 305 | { |
| 306 | punt_main_t *pm = &punt_main; |
| 307 | |
| 308 | if (!pm->is_configured) |
| 309 | return clib_error_return (0, "socket is not configured"); |
| 310 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 311 | if (header_version != PUNT_PACKETDESC_VERSION) |
| 312 | return clib_error_return (0, "Invalid packet descriptor version"); |
| 313 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 314 | if (strncmp (client_pathname, vnet_punt_get_server_pathname (), |
| 315 | UNIX_PATH_MAX) == 0) |
| 316 | return clib_error_return (0, |
| 317 | "Punt socket: Invalid client path: %s", |
| 318 | client_pathname); |
| 319 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 320 | /* Register client */ |
| 321 | switch (pr->type) |
| 322 | { |
| 323 | case PUNT_TYPE_L4: |
| 324 | return (punt_socket_register_l4 (vm, |
| 325 | pr->punt.l4.af, |
| 326 | pr->punt.l4.protocol, |
| 327 | pr->punt.l4.port, client_pathname)); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 328 | case PUNT_TYPE_IP_PROTO: |
| 329 | return (punt_socket_register_ip_proto (vm, |
| 330 | pr->punt.ip_proto.af, |
| 331 | pr->punt.ip_proto.protocol, |
| 332 | client_pathname)); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 333 | case PUNT_TYPE_EXCEPTION: |
| 334 | return (punt_socket_register_exception (vm, |
| 335 | pr->punt.exception.reason, |
| 336 | client_pathname)); |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | clib_error_t * |
| 343 | vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr) |
| 344 | { |
| 345 | punt_main_t *pm = &punt_main; |
| 346 | |
| 347 | if (!pm->is_configured) |
| 348 | return clib_error_return (0, "socket is not configured"); |
| 349 | |
| 350 | switch (pr->type) |
| 351 | { |
| 352 | case PUNT_TYPE_L4: |
| 353 | return (punt_socket_unregister_l4 (pr->punt.l4.af, |
| 354 | pr->punt.l4.protocol, |
| 355 | pr->punt.l4.port)); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 356 | case PUNT_TYPE_IP_PROTO: |
| 357 | return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af, |
| 358 | pr->punt.ip_proto.protocol)); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 359 | case PUNT_TYPE_EXCEPTION: |
| 360 | return (punt_socket_unregister_exception (pr->punt.exception.reason)); |
| 361 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 366 | /** |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 367 | * @brief Request IP L4 traffic punt to the local TCP/IP stack. |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 368 | * |
| 369 | * @em Note |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 370 | * - UDP is the only protocol supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 371 | * |
| 372 | * @param vm vlib_main_t corresponding to the current thread |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 373 | * @param af IP address family. |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 374 | * @param protocol 8-bits L4 protocol value |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 375 | * UDP is 17 |
| 376 | * TCP is 1 |
| 377 | * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 378 | * |
| 379 | * @returns 0 on success, non-zero value otherwise |
| 380 | */ |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 381 | static clib_error_t * |
| 382 | punt_l4_add_del (vlib_main_t * vm, |
| 383 | ip_address_family_t af, |
| 384 | ip_protocol_t protocol, u16 port, bool is_add) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 385 | { |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 386 | int is_ip4 = af == AF_IP4; |
| 387 | |
Florin Coras | 3ffe6ca | 2019-06-26 16:27:13 -0700 | [diff] [blame] | 388 | /* For now we only support TCP and UDP punt */ |
| 389 | if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP) |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 390 | return clib_error_return (0, |
Florin Coras | 3ffe6ca | 2019-06-26 16:27:13 -0700 | [diff] [blame] | 391 | "only UDP (%d) and TCP (%d) protocols are supported, got %d", |
| 392 | IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 393 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 394 | if (port == (u16) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 395 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 396 | if (protocol == IP_PROTOCOL_UDP) |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 397 | udp_punt_unknown (vm, is_ip4, is_add); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 398 | else if (protocol == IP_PROTOCOL_TCP) |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 399 | tcp_punt_unknown (vm, is_ip4, is_add); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | else if (is_add) |
| 405 | { |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 406 | const vlib_node_registration_t *punt_node = |
| 407 | is_ip4 ? &udp4_punt_node : &udp6_punt_node; |
| 408 | |
Florin Coras | 3ffe6ca | 2019-06-26 16:27:13 -0700 | [diff] [blame] | 409 | if (protocol == IP_PROTOCOL_TCP) |
| 410 | return clib_error_return (0, "punt TCP ports is not supported yet"); |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 411 | |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 412 | udp_register_dst_port (vm, port, punt_node->index, is_ip4); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 413 | |
| 414 | return 0; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 415 | } |
| 416 | else |
Pavel Kotucek | 41b923a | 2018-12-05 17:16:23 +0100 | [diff] [blame] | 417 | { |
Florin Coras | 3ffe6ca | 2019-06-26 16:27:13 -0700 | [diff] [blame] | 418 | if (protocol == IP_PROTOCOL_TCP) |
| 419 | return clib_error_return (0, "punt TCP ports is not supported yet"); |
Pavel Kotucek | 41b923a | 2018-12-05 17:16:23 +0100 | [diff] [blame] | 420 | |
BenoƮt Ganne | 7c7b505 | 2021-10-04 12:03:20 +0200 | [diff] [blame] | 421 | udp_unregister_dst_port (vm, port, is_ip4); |
Pavel Kotucek | 41b923a | 2018-12-05 17:16:23 +0100 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 425 | } |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 426 | |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 427 | /** |
| 428 | * @brief Request exception traffic punt. |
| 429 | * |
| 430 | * @param reason Punting reason |
| 431 | * |
| 432 | * @returns 0 on success, non-zero value otherwise |
| 433 | */ |
| 434 | static clib_error_t * |
| 435 | punt_exception_add_del (vlib_punt_reason_t reason, bool is_add) |
| 436 | { |
| 437 | punt_main_t *pm = &punt_main; |
| 438 | int rv = 0; |
| 439 | vnet_punt_reason_flag_t flag = vlib_punt_reason_get_flags (reason); |
| 440 | const char *node_name = |
| 441 | vnet_punt_reason_flag_is_IP6_PACKET (flag) ? "ip6-punt" : "ip4-punt"; |
| 442 | if (is_add) |
| 443 | rv = vlib_punt_register (pm->hdl, reason, node_name); |
| 444 | else |
| 445 | rv = vlib_punt_unregister (pm->hdl, reason, node_name); |
| 446 | if (!rv) |
| 447 | return 0; |
| 448 | else |
| 449 | return clib_error_return (0, is_add ? "Existing punting registration..." : |
| 450 | "Punting registration not found..."); |
| 451 | } |
| 452 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 453 | clib_error_t * |
| 454 | vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add) |
| 455 | { |
| 456 | switch (pr->type) |
| 457 | { |
| 458 | case PUNT_TYPE_L4: |
| 459 | return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol, |
| 460 | pr->punt.l4.port, is_add)); |
| 461 | case PUNT_TYPE_EXCEPTION: |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 462 | return punt_exception_add_del (pr->punt.exception.reason, is_add); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 463 | case PUNT_TYPE_IP_PROTO: |
| 464 | break; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | return (clib_error_return (0, "Unsupported punt type: %d", pr->type)); |
| 468 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 469 | |
| 470 | static clib_error_t * |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 471 | punt_cli (vlib_main_t * vm, |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 472 | unformat_input_t * input__, vlib_cli_command_t * cmd) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 473 | { |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 474 | unformat_input_t line_input, *input = &line_input; |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 475 | clib_error_t *error = NULL; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 476 | bool is_add = true; |
| 477 | punt_reg_t pr = { |
| 478 | .punt = { |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 479 | .l4 = { |
| 480 | .af = AF_IP4, |
| 481 | .port = ~0, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 482 | .protocol = IP_PROTOCOL_UDP, |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 483 | }, |
| 484 | }, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 485 | .type = PUNT_TYPE_L4, |
| 486 | }; |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 487 | u32 port; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 488 | |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 489 | if (!unformat_user (input__, unformat_line_input, input)) |
| 490 | return 0; |
| 491 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 492 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 493 | { |
| 494 | if (unformat (input, "del")) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 495 | is_add = false; |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 496 | else if (unformat (input, "reason %U", unformat_punt_reason, |
| 497 | &pr.punt.exception.reason)) |
| 498 | pr.type = PUNT_TYPE_EXCEPTION; |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 499 | else if (unformat (input, "ipv4")) |
| 500 | pr.punt.l4.af = AF_IP4; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 501 | else if (unformat (input, "ipv6")) |
| 502 | pr.punt.l4.af = AF_IP6; |
| 503 | else if (unformat (input, "ip6")) |
| 504 | pr.punt.l4.af = AF_IP6; |
Neale Ranns | 68577d2 | 2019-06-04 13:31:23 +0000 | [diff] [blame] | 505 | else if (unformat (input, "%d", &port)) |
| 506 | pr.punt.l4.port = port; |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 507 | else if (unformat (input, "all")) |
| 508 | pr.punt.l4.port = ~0; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 509 | else if (unformat (input, "udp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 510 | pr.punt.l4.protocol = IP_PROTOCOL_UDP; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 511 | else if (unformat (input, "tcp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 512 | pr.punt.l4.protocol = IP_PROTOCOL_TCP; |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 513 | else |
| 514 | { |
| 515 | error = clib_error_return (0, "parse error: '%U'", |
| 516 | format_unformat_error, input); |
| 517 | goto done; |
| 518 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 519 | } |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 520 | |
| 521 | /* punt both IPv6 and IPv4 when used in CLI */ |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 522 | error = vnet_punt_add_del (vm, &pr, is_add); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 523 | if (error) |
| 524 | { |
| 525 | clib_error_report (error); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 528 | done: |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 529 | unformat_free (input); |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 530 | return error; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /*? |
| 534 | * The set of '<em>set punt</em>' commands allows specific IP traffic to |
| 535 | * be punted to the host TCP/IP stack |
| 536 | * |
| 537 | * @em Note |
| 538 | * - UDP is the only protocol supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 539 | * - All TCP traffic is currently punted to the host by default |
| 540 | * |
| 541 | * @cliexpar |
| 542 | * @parblock |
| 543 | * Example of how to request NTP traffic to be punted |
| 544 | * @cliexcmd{set punt udp 125} |
| 545 | * |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 546 | * Example of how to request all 'unknown' UDP traffic to be punted |
| 547 | * @cliexcmd{set punt udp all} |
| 548 | * |
| 549 | * Example of how to stop all 'unknown' UDP traffic to be punted |
| 550 | * @cliexcmd{set punt udp del all} |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 551 | * @endparblock |
| 552 | ?*/ |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 553 | VLIB_CLI_COMMAND (punt_command, static) = { |
| 554 | .path = "set punt", |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 555 | .short_help = "set punt [IPV4|ip6|ipv6] [UDP|tcp] [del] [ALL|<port-num>]", |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 556 | .function = punt_cli, |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 557 | }; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 558 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 559 | static clib_error_t * |
| 560 | punt_socket_register_cmd (vlib_main_t * vm, |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 561 | unformat_input_t * input__, |
| 562 | vlib_cli_command_t * cmd) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 563 | { |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 564 | unformat_input_t line_input, *input = &line_input; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 565 | u8 *socket_name = 0; |
| 566 | clib_error_t *error = NULL; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 567 | punt_reg_t pr = { |
| 568 | .punt = { |
| 569 | .l4 = { |
| 570 | .af = AF_IP4, |
| 571 | .port = ~0, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 572 | .protocol = IP_PROTOCOL_UDP, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 573 | }, |
| 574 | }, |
| 575 | .type = PUNT_TYPE_L4, |
| 576 | }; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 577 | |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 578 | if (!unformat_user (input__, unformat_line_input, input)) |
| 579 | return 0; |
| 580 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 581 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 582 | { |
| 583 | if (unformat (input, "ipv4")) |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 584 | pr.punt.l4.af = AF_IP4; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 585 | else if (unformat (input, "ipv6")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 586 | pr.punt.l4.af = AF_IP6; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 587 | else if (unformat (input, "udp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 588 | pr.punt.l4.protocol = IP_PROTOCOL_UDP; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 589 | else if (unformat (input, "tcp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 590 | pr.punt.l4.protocol = IP_PROTOCOL_TCP; |
| 591 | else if (unformat (input, "%d", &pr.punt.l4.port)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 592 | ; |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 593 | else if (unformat (input, "all")) |
| 594 | pr.punt.l4.port = ~0; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 595 | else if (unformat (input, "socket %s", &socket_name)) |
| 596 | ; |
Arthur de Kerhor | a80ff13 | 2021-04-12 08:16:56 -0700 | [diff] [blame] | 597 | else if (unformat (input, "reason %U", unformat_punt_reason, |
| 598 | &pr.punt.exception.reason)) |
| 599 | pr.type = PUNT_TYPE_EXCEPTION; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 600 | else |
| 601 | { |
| 602 | error = clib_error_return (0, "parse error: '%U'", |
| 603 | format_unformat_error, input); |
| 604 | goto done; |
| 605 | } |
| 606 | } |
| 607 | |
Neale Ranns | 4f3c104 | 2019-06-11 01:39:08 -0700 | [diff] [blame] | 608 | if (!socket_name) |
| 609 | error = clib_error_return (0, "socket name not specified"); |
| 610 | else |
| 611 | error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 612 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 613 | done: |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 614 | unformat_free (input); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 615 | return error; |
| 616 | } |
| 617 | |
| 618 | /*? |
| 619 | * |
| 620 | * @cliexpar |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 621 | * @cliexcmd{punt socket register socket punt_l4_foo.sock} |
| 622 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 623 | ?*/ |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 624 | VLIB_CLI_COMMAND (punt_socket_register_command, static) = |
| 625 | { |
| 626 | .path = "punt socket register", |
| 627 | .function = punt_socket_register_cmd, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 628 | .short_help = "punt socket register [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>] socket <socket>", |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 629 | .is_mp_safe = 1, |
| 630 | }; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 631 | |
| 632 | static clib_error_t * |
| 633 | punt_socket_deregister_cmd (vlib_main_t * vm, |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 634 | unformat_input_t * input__, |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 635 | vlib_cli_command_t * cmd) |
| 636 | { |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 637 | unformat_input_t line_input, *input = &line_input; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 638 | clib_error_t *error = NULL; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 639 | punt_reg_t pr = { |
| 640 | .punt = { |
| 641 | .l4 = { |
| 642 | .af = AF_IP4, |
| 643 | .port = ~0, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 644 | .protocol = IP_PROTOCOL_UDP, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 645 | }, |
| 646 | }, |
| 647 | .type = PUNT_TYPE_L4, |
| 648 | }; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 649 | |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 650 | if (!unformat_user (input__, unformat_line_input, input)) |
| 651 | return 0; |
| 652 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 653 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 654 | { |
| 655 | if (unformat (input, "ipv4")) |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 656 | pr.punt.l4.af = AF_IP4; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 657 | else if (unformat (input, "ipv6")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 658 | pr.punt.l4.af = AF_IP6; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 659 | else if (unformat (input, "udp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 660 | pr.punt.l4.protocol = IP_PROTOCOL_UDP; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 661 | else if (unformat (input, "tcp")) |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 662 | pr.punt.l4.protocol = IP_PROTOCOL_TCP; |
| 663 | else if (unformat (input, "%d", &pr.punt.l4.port)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 664 | ; |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 665 | else if (unformat (input, "all")) |
| 666 | pr.punt.l4.port = ~0; |
Arthur de Kerhor | a80ff13 | 2021-04-12 08:16:56 -0700 | [diff] [blame] | 667 | else if (unformat (input, "reason %U", unformat_punt_reason, |
| 668 | &pr.punt.exception.reason)) |
| 669 | pr.type = PUNT_TYPE_EXCEPTION; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 670 | else |
| 671 | { |
| 672 | error = clib_error_return (0, "parse error: '%U'", |
| 673 | format_unformat_error, input); |
| 674 | goto done; |
| 675 | } |
| 676 | } |
| 677 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 678 | error = vnet_punt_socket_del (vm, &pr); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 679 | done: |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 680 | unformat_free (input); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 681 | return error; |
| 682 | } |
| 683 | |
| 684 | /*? |
| 685 | * |
| 686 | * @cliexpar |
| 687 | * @cliexcmd{punt socket register} |
| 688 | ?*/ |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 689 | VLIB_CLI_COMMAND (punt_socket_deregister_command, static) = |
| 690 | { |
| 691 | .path = "punt socket deregister", |
| 692 | .function = punt_socket_deregister_cmd, |
Paul Vinciguerra | 32c4d38 | 2019-10-23 16:07:32 -0400 | [diff] [blame] | 693 | .short_help = "punt socket deregister [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>]", |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 694 | .is_mp_safe = 1, |
| 695 | }; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 696 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 697 | void |
| 698 | punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 699 | { |
| 700 | punt_main_t *pm = &punt_main; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 701 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 702 | switch (pt) |
| 703 | { |
| 704 | case PUNT_TYPE_L4: |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 705 | { |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 706 | u32 pci, key; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 707 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 708 | hash_foreach(key, pci, pm->db.clients_by_l4_port, |
| 709 | ({ |
| 710 | cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx); |
| 711 | })); |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 712 | break; |
| 713 | } |
| 714 | case PUNT_TYPE_IP_PROTO: |
| 715 | { |
| 716 | u32 pci, key; |
| 717 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 718 | hash_foreach(key, pci, pm->db.clients_by_ip_proto, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 719 | ({ |
| 720 | cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx); |
| 721 | })); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 722 | break; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 723 | } |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 724 | case PUNT_TYPE_EXCEPTION: |
| 725 | { |
| 726 | u32 *pci; |
| 727 | |
| 728 | vec_foreach (pci, pm->db.clients_by_exception) |
| 729 | { |
| 730 | if (~0 != *pci) |
| 731 | cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx); |
| 732 | } |
| 733 | |
| 734 | break; |
| 735 | } |
| 736 | } |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 739 | static u8 * |
| 740 | format_punt_client (u8 * s, va_list * args) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 741 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 742 | punt_client_t *pc = va_arg (*args, punt_client_t *); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 743 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 744 | s = format (s, " punt "); |
| 745 | |
| 746 | switch (pc->reg.type) |
| 747 | { |
| 748 | case PUNT_TYPE_L4: |
| 749 | s = format (s, "%U %U port %d", |
| 750 | format_ip_address_family, pc->reg.punt.l4.af, |
| 751 | format_ip_protocol, pc->reg.punt.l4.protocol, |
| 752 | pc->reg.punt.l4.port); |
| 753 | break; |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 754 | case PUNT_TYPE_IP_PROTO: |
| 755 | s = format (s, "%U %U", |
| 756 | format_ip_address_family, pc->reg.punt.ip_proto.af, |
| 757 | format_ip_protocol, pc->reg.punt.ip_proto.protocol); |
| 758 | break; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 759 | case PUNT_TYPE_EXCEPTION: |
| 760 | s = format (s, " %U", format_vlib_punt_reason, |
| 761 | pc->reg.punt.exception.reason); |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | s = format (s, " to socket %s \n", pc->caddr.sun_path); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 766 | |
| 767 | return (s); |
| 768 | } |
| 769 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 770 | static walk_rc_t |
| 771 | punt_client_show_one (const punt_client_t * pc, void *ctx) |
| 772 | { |
| 773 | vlib_cli_output (ctx, "%U", format_punt_client, pc); |
| 774 | |
| 775 | return (WALK_CONTINUE); |
| 776 | } |
| 777 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 778 | static clib_error_t * |
| 779 | punt_socket_show_cmd (vlib_main_t * vm, |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 780 | unformat_input_t * input__, vlib_cli_command_t * cmd) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 781 | { |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 782 | unformat_input_t line_input, *input = &line_input; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 783 | clib_error_t *error = NULL; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 784 | punt_type_t pt; |
| 785 | |
| 786 | pt = PUNT_TYPE_L4; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 787 | |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 788 | if (!unformat_user (input__, unformat_line_input, input)) |
| 789 | return 0; |
| 790 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 791 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 792 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 793 | if (unformat (input, "exception")) |
| 794 | pt = PUNT_TYPE_EXCEPTION; |
| 795 | else if (unformat (input, "l4")) |
| 796 | pt = PUNT_TYPE_L4; |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 797 | else if (unformat (input, "ip")) |
| 798 | pt = PUNT_TYPE_IP_PROTO; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 799 | else |
| 800 | { |
| 801 | error = clib_error_return (0, "parse error: '%U'", |
| 802 | format_unformat_error, input); |
| 803 | goto done; |
| 804 | } |
| 805 | } |
| 806 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 807 | punt_client_walk (pt, punt_client_show_one, vm); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 808 | |
| 809 | done: |
BenoƮt Ganne | 9ae3c6a | 2020-07-27 18:27:57 +0200 | [diff] [blame] | 810 | unformat_free (input); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 811 | return (error); |
| 812 | } |
| 813 | |
| 814 | /*? |
| 815 | * |
| 816 | * @cliexpar |
| 817 | * @cliexcmd{show punt socket ipv4} |
| 818 | ?*/ |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 819 | VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) = |
| 820 | { |
| 821 | .path = "show punt socket registrations", |
| 822 | .function = punt_socket_show_cmd, |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 823 | .short_help = "show punt socket registrations [l4|exception]", |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 824 | .is_mp_safe = 1, |
| 825 | }; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 826 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 827 | clib_error_t * |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 828 | ip_punt_init (vlib_main_t * vm) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 829 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 830 | clib_error_t *error = NULL; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 831 | punt_main_t *pm = &punt_main; |
Neale Ranns | 39040a6 | 2019-07-10 01:47:15 -0700 | [diff] [blame] | 832 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 833 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 834 | pm->is_configured = false; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 835 | pm->interface_output_node = |
| 836 | vlib_get_node_by_name (vm, (u8 *) "interface-output"); |
| 837 | |
| 838 | if ((error = vlib_call_init_function (vm, punt_init))) |
| 839 | return error; |
| 840 | |
| 841 | pm->hdl = vlib_punt_client_register ("ip-punt"); |
| 842 | |
Neale Ranns | 39040a6 | 2019-07-10 01:47:15 -0700 | [diff] [blame] | 843 | vec_validate_aligned (pm->thread_data, tm->n_vlib_mains, |
| 844 | CLIB_CACHE_LINE_BYTES); |
| 845 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 846 | return (error); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 849 | u8 * |
| 850 | format_vnet_punt_reason_flags (u8 *s, va_list *args) |
| 851 | { |
| 852 | vnet_punt_reason_flag_t flag = va_arg (*args, int); |
| 853 | #define _(pos, len, value, name, str) \ |
| 854 | if (vnet_punt_reason_flag_is_##name (flag)) \ |
| 855 | s = format (s, "%s ", str); |
| 856 | |
| 857 | foreach_vnet_punt_reason_flag |
| 858 | #undef _ |
| 859 | return (s); |
| 860 | } |
| 861 | |
Neale Ranns | 76b5649 | 2018-09-28 15:16:14 +0000 | [diff] [blame] | 862 | VLIB_INIT_FUNCTION (ip_punt_init); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 863 | |
| 864 | static clib_error_t * |
| 865 | punt_config (vlib_main_t * vm, unformat_input_t * input) |
| 866 | { |
| 867 | punt_main_t *pm = &punt_main; |
| 868 | char *socket_path = 0; |
| 869 | |
| 870 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 871 | { |
| 872 | if (unformat (input, "socket %s", &socket_path)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 873 | strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 874 | else |
| 875 | return clib_error_return (0, "unknown input `%U'", |
| 876 | format_unformat_error, input); |
| 877 | } |
| 878 | |
| 879 | if (socket_path == 0) |
| 880 | return 0; |
| 881 | |
| 882 | /* UNIX domain socket */ |
| 883 | struct sockaddr_un addr; |
| 884 | if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1) |
| 885 | { |
| 886 | return clib_error_return (0, "socket error"); |
| 887 | } |
| 888 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 889 | clib_memset (&addr, 0, sizeof (addr)); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 890 | addr.sun_family = AF_UNIX; |
| 891 | if (*socket_path == '\0') |
| 892 | { |
| 893 | *addr.sun_path = '\0'; |
| 894 | strncpy (addr.sun_path + 1, socket_path + 1, |
| 895 | sizeof (addr.sun_path) - 2); |
| 896 | } |
| 897 | else |
| 898 | { |
| 899 | strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1); |
| 900 | unlink (socket_path); |
| 901 | } |
| 902 | |
| 903 | if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1) |
| 904 | { |
| 905 | return clib_error_return (0, "bind error"); |
| 906 | } |
| 907 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 908 | int n_bytes = 0x10000; |
| 909 | |
| 910 | if (setsockopt |
| 911 | (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes, |
| 912 | sizeof (n_bytes)) == -1) |
| 913 | { |
| 914 | return clib_error_return (0, "setsockopt error"); |
| 915 | } |
| 916 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 917 | /* Register socket */ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 918 | clib_file_main_t *fm = &file_main; |
| 919 | clib_file_t template = { 0 }; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 920 | template.read_function = punt_socket_read_ready; |
| 921 | template.file_descriptor = pm->socket_fd; |
Paul Vinciguerra | 5481ad4 | 2020-01-28 14:47:17 -0500 | [diff] [blame] | 922 | template.description = format (0, "punt socket %s", socket_path); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 923 | pm->clib_file_index = clib_file_add (fm, &template); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 924 | |
| 925 | pm->is_configured = true; |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | VLIB_CONFIG_FUNCTION (punt_config, "punt"); |
| 931 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 932 | /* |
| 933 | * fd.io coding-style-patch-verification: ON |
| 934 | * |
| 935 | * Local Variables: |
| 936 | * eval: (c-set-style "gnu") |
| 937 | * End: |
| 938 | */ |