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> |
| 27 | #include <vnet/pg/pg.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 28 | #include <vnet/udp/udp.h> |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 29 | #include <vnet/tcp/tcp.h> |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 30 | #include <vnet/sctp/sctp.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 31 | #include <vnet/ip/punt.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 32 | #include <vppinfra/sparse_vec.h> |
| 33 | #include <vlib/unix/unix.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 34 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <unistd.h> |
| 37 | #include <sys/socket.h> |
Marco Varlese | 2234983 | 2017-09-08 10:40:34 +0200 | [diff] [blame] | 38 | #include <sys/uio.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 39 | #include <stdlib.h> |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 40 | |
| 41 | #define foreach_punt_next \ |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 42 | _ (PUNT4, "ip4-punt") \ |
| 43 | _ (PUNT6, "ip6-punt") |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 44 | |
| 45 | typedef enum |
| 46 | { |
| 47 | #define _(s,n) PUNT_NEXT_##s, |
| 48 | foreach_punt_next |
| 49 | #undef _ |
| 50 | PUNT_N_NEXT, |
| 51 | } punt_next_t; |
| 52 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 53 | enum punt_socket_rx_next_e |
| 54 | { |
| 55 | PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT, |
| 56 | PUNT_SOCKET_RX_NEXT_IP4_LOOKUP, |
| 57 | PUNT_SOCKET_RX_NEXT_IP6_LOOKUP, |
| 58 | PUNT_SOCKET_RX_N_NEXT |
| 59 | }; |
| 60 | |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 61 | #define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6) |
| 62 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 63 | vlib_node_registration_t udp4_punt_node; |
| 64 | vlib_node_registration_t udp6_punt_node; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 65 | vlib_node_registration_t udp4_punt_socket_node; |
| 66 | vlib_node_registration_t udp6_punt_socket_node; |
| 67 | static vlib_node_registration_t punt_socket_rx_node; |
| 68 | |
| 69 | punt_main_t punt_main; |
| 70 | |
| 71 | char * |
| 72 | vnet_punt_get_server_pathname (void) |
| 73 | { |
| 74 | punt_main_t *pm = &punt_main; |
| 75 | return pm->sun_path; |
| 76 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 77 | |
| 78 | /** @brief IPv4/IPv6 UDP punt node main loop. |
| 79 | |
| 80 | This is the main loop inline function for IPv4/IPv6 UDP punt |
| 81 | transition node. |
| 82 | |
| 83 | @param vm vlib_main_t corresponding to the current thread |
| 84 | @param node vlib_node_runtime_t |
| 85 | @param frame vlib_frame_t whose contents should be dispatched |
| 86 | @param is_ipv4 indicates if called for IPv4 or IPv6 node |
| 87 | */ |
| 88 | always_inline uword |
| 89 | udp46_punt_inline (vlib_main_t * vm, |
| 90 | vlib_node_runtime_t * node, |
| 91 | vlib_frame_t * from_frame, int is_ip4) |
| 92 | { |
| 93 | u32 n_left_from, *from, *to_next; |
| 94 | word advance; |
| 95 | |
| 96 | from = vlib_frame_vector_args (from_frame); |
| 97 | n_left_from = from_frame->n_vectors; |
| 98 | |
| 99 | /* udp[46]_lookup hands us the data payload, not the IP header */ |
| 100 | if (is_ip4) |
| 101 | advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t)); |
| 102 | else |
| 103 | advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t)); |
| 104 | |
| 105 | while (n_left_from > 0) |
| 106 | { |
| 107 | u32 n_left_to_next; |
| 108 | |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 109 | vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next, |
| 110 | n_left_to_next); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 111 | |
| 112 | while (n_left_from > 0 && n_left_to_next > 0) |
| 113 | { |
| 114 | u32 bi0; |
| 115 | vlib_buffer_t *b0; |
| 116 | |
| 117 | bi0 = from[0]; |
| 118 | to_next[0] = bi0; |
| 119 | from += 1; |
| 120 | to_next += 1; |
| 121 | n_left_from -= 1; |
| 122 | n_left_to_next -= 1; |
| 123 | |
| 124 | b0 = vlib_get_buffer (vm, bi0); |
| 125 | vlib_buffer_advance (b0, advance); |
| 126 | b0->error = node->errors[PUNT_ERROR_UDP_PORT]; |
| 127 | } |
| 128 | |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 129 | vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | return from_frame->n_vectors; |
| 133 | } |
| 134 | |
| 135 | static char *punt_error_strings[] = { |
| 136 | #define punt_error(n,s) s, |
| 137 | #include "punt_error.def" |
| 138 | #undef punt_error |
| 139 | }; |
| 140 | |
| 141 | /** @brief IPv4 UDP punt node. |
| 142 | @node ip4-udp-punt |
| 143 | |
| 144 | This is the IPv4 UDP punt transition node. It is registered as a next |
| 145 | node for the "ip4-udp-lookup" handling UDP port(s) requested for punt. |
| 146 | The buffer's current data pointer is adjusted to the original packet |
| 147 | IPv4 header. All buffers are dispatched to "error-punt". |
| 148 | |
| 149 | @param vm vlib_main_t corresponding to the current thread |
| 150 | @param node vlib_node_runtime_t |
| 151 | @param frame vlib_frame_t whose contents should be dispatched |
| 152 | |
| 153 | @par Graph mechanics: next index usage |
| 154 | |
| 155 | @em Sets: |
| 156 | - <code>vnet_buffer(b)->current_data</code> |
| 157 | - <code>vnet_buffer(b)->current_len</code> |
| 158 | |
| 159 | <em>Next Index:</em> |
| 160 | - Dispatches the packet to the "error-punt" node |
| 161 | */ |
| 162 | static uword |
| 163 | udp4_punt (vlib_main_t * vm, |
| 164 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 165 | { |
| 166 | return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 167 | } |
| 168 | |
| 169 | /** @brief IPv6 UDP punt node. |
| 170 | @node ip6-udp-punt |
| 171 | |
| 172 | This is the IPv6 UDP punt transition node. It is registered as a next |
| 173 | node for the "ip6-udp-lookup" handling UDP port(s) requested for punt. |
| 174 | The buffer's current data pointer is adjusted to the original packet |
| 175 | IPv6 header. All buffers are dispatched to "error-punt". |
| 176 | |
| 177 | @param vm vlib_main_t corresponding to the current thread |
| 178 | @param node vlib_node_runtime_t |
| 179 | @param frame vlib_frame_t whose contents should be dispatched |
| 180 | |
| 181 | @par Graph mechanics: next index usage |
| 182 | |
| 183 | @em Sets: |
| 184 | - <code>vnet_buffer(b)->current_data</code> |
| 185 | - <code>vnet_buffer(b)->current_len</code> |
| 186 | |
| 187 | <em>Next Index:</em> |
| 188 | - Dispatches the packet to the "error-punt" node |
| 189 | */ |
| 190 | static uword |
| 191 | udp6_punt (vlib_main_t * vm, |
| 192 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 193 | { |
| 194 | return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 195 | } |
| 196 | |
| 197 | /* *INDENT-OFF* */ |
| 198 | VLIB_REGISTER_NODE (udp4_punt_node) = { |
| 199 | .function = udp4_punt, |
| 200 | .name = "ip4-udp-punt", |
| 201 | /* Takes a vector of packets. */ |
| 202 | .vector_size = sizeof (u32), |
| 203 | |
| 204 | .n_errors = PUNT_N_ERROR, |
| 205 | .error_strings = punt_error_strings, |
| 206 | |
| 207 | .n_next_nodes = PUNT_N_NEXT, |
| 208 | .next_nodes = { |
| 209 | #define _(s,n) [PUNT_NEXT_##s] = n, |
| 210 | foreach_punt_next |
| 211 | #undef _ |
| 212 | }, |
| 213 | }; |
| 214 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 215 | VLIB_NODE_FUNCTION_MULTIARCH (udp4_punt_node, udp4_punt); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 216 | |
| 217 | VLIB_REGISTER_NODE (udp6_punt_node) = { |
| 218 | .function = udp6_punt, |
| 219 | .name = "ip6-udp-punt", |
| 220 | /* Takes a vector of packets. */ |
| 221 | .vector_size = sizeof (u32), |
| 222 | |
| 223 | .n_errors = PUNT_N_ERROR, |
| 224 | .error_strings = punt_error_strings, |
| 225 | |
| 226 | .n_next_nodes = PUNT_N_NEXT, |
| 227 | .next_nodes = { |
| 228 | #define _(s,n) [PUNT_NEXT_##s] = n, |
| 229 | foreach_punt_next |
| 230 | #undef _ |
| 231 | }, |
| 232 | }; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 233 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 234 | VLIB_NODE_FUNCTION_MULTIARCH (udp6_punt_node, udp6_punt);; |
| 235 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 236 | /* *INDENT-ON* */ |
| 237 | |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 238 | static punt_client_t * |
| 239 | punt_client_get (bool is_ip4, u16 port) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 240 | { |
| 241 | punt_main_t *pm = &punt_main; |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 242 | punt_client_t *v = |
| 243 | is_ip4 ? pm->clients_by_dst_port4 : pm->clients_by_dst_port6; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 244 | |
| 245 | u16 i = sparse_vec_index (v, port); |
| 246 | if (i == SPARSE_VEC_INVALID_INDEX) |
| 247 | return 0; |
| 248 | |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 249 | return &vec_elt (v, i); |
| 250 | } |
| 251 | |
| 252 | static struct sockaddr_un * |
| 253 | punt_socket_get (bool is_ip4, u16 port) |
| 254 | { |
| 255 | punt_client_t *v = punt_client_get (is_ip4, port); |
| 256 | if (v) |
| 257 | return &v->caddr; |
| 258 | |
| 259 | return NULL; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 262 | static int |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 263 | punt_socket_register (bool is_ip4, u8 protocol, u16 port, |
| 264 | char *client_pathname) |
| 265 | { |
| 266 | punt_main_t *pm = &punt_main; |
| 267 | punt_client_t c, *n; |
| 268 | punt_client_t *v = is_ip4 ? pm->clients_by_dst_port4 : |
| 269 | pm->clients_by_dst_port6; |
| 270 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 271 | if (strncmp (client_pathname, vnet_punt_get_server_pathname (), |
| 272 | UNIX_PATH_MAX) == 0) |
| 273 | return -1; |
| 274 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 275 | clib_memset (&c, 0, sizeof (c)); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 276 | memcpy (c.caddr.sun_path, client_pathname, sizeof (c.caddr.sun_path)); |
| 277 | c.caddr.sun_family = AF_UNIX; |
| 278 | c.port = port; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 279 | c.protocol = protocol; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 280 | n = sparse_vec_validate (v, port); |
| 281 | n[0] = c; |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 282 | return 0; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* $$$$ Just leaves the mapping in place for now */ |
| 286 | static void |
| 287 | punt_socket_unregister (bool is_ip4, u8 protocol, u16 port) |
| 288 | { |
| 289 | return; |
| 290 | } |
| 291 | |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 292 | typedef struct |
| 293 | { |
| 294 | punt_client_t client; |
| 295 | u8 is_midchain; |
| 296 | } udp_punt_trace_t; |
| 297 | |
| 298 | u8 * |
| 299 | format_udp_punt_trace (u8 * s, va_list * args) |
| 300 | { |
| 301 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 302 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 303 | udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *); |
| 304 | u32 indent = format_get_indent (s); |
| 305 | s = format (s, "to: %s", t->client.caddr.sun_path); |
| 306 | if (t->is_midchain) |
| 307 | { |
| 308 | s = format (s, "\n%U(buffer is part of chain)", format_white_space, |
| 309 | indent); |
| 310 | } |
| 311 | return s; |
| 312 | } |
| 313 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 314 | always_inline uword |
| 315 | udp46_punt_socket_inline (vlib_main_t * vm, |
| 316 | vlib_node_runtime_t * node, |
| 317 | vlib_frame_t * frame, bool is_ip4) |
| 318 | { |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 319 | u32 *buffers = vlib_frame_vector_args (frame); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 320 | uword n_packets = frame->n_vectors; |
| 321 | struct iovec *iovecs = 0; |
| 322 | punt_main_t *pm = &punt_main; |
| 323 | int i; |
| 324 | |
| 325 | u32 node_index = is_ip4 ? udp4_punt_socket_node.index : |
| 326 | udp6_punt_socket_node.index; |
| 327 | |
| 328 | for (i = 0; i < n_packets; i++) |
| 329 | { |
| 330 | struct iovec *iov; |
| 331 | vlib_buffer_t *b; |
| 332 | uword l; |
| 333 | punt_packetdesc_t packetdesc; |
| 334 | |
| 335 | b = vlib_get_buffer (vm, buffers[i]); |
| 336 | |
| 337 | /* Reverse UDP Punt advance */ |
| 338 | udp_header_t *udp; |
| 339 | if (is_ip4) |
| 340 | { |
| 341 | vlib_buffer_advance (b, -(sizeof (ip4_header_t) + |
| 342 | sizeof (udp_header_t))); |
| 343 | ip4_header_t *ip = vlib_buffer_get_current (b); |
| 344 | udp = (udp_header_t *) (ip + 1); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | vlib_buffer_advance (b, -(sizeof (ip6_header_t) + |
| 349 | sizeof (udp_header_t))); |
| 350 | ip6_header_t *ip = vlib_buffer_get_current (b); |
| 351 | udp = (udp_header_t *) (ip + 1); |
| 352 | } |
| 353 | |
| 354 | u16 port = clib_net_to_host_u16 (udp->dst_port); |
| 355 | |
| 356 | /* |
| 357 | * Find registerered client |
| 358 | * If no registered client, drop packet and count |
| 359 | */ |
| 360 | struct sockaddr_un *caddr; |
| 361 | caddr = punt_socket_get (is_ip4, port); |
| 362 | if (!caddr) |
| 363 | { |
| 364 | vlib_node_increment_counter (vm, node_index, |
| 365 | PUNT_ERROR_SOCKET_TX_ERROR, 1); |
| 366 | goto error; |
| 367 | } |
| 368 | |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 369 | punt_client_t *c = NULL; |
| 370 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 371 | { |
Klement Sekera | b716308 | 2017-11-08 04:13:49 +0100 | [diff] [blame] | 372 | c = punt_client_get (is_ip4, port); |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 373 | udp_punt_trace_t *t; |
| 374 | t = vlib_add_trace (vm, node, b, sizeof (t[0])); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 375 | clib_memcpy_fast (&t->client, c, sizeof (t->client)); |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 378 | /* Re-set iovecs if present. */ |
| 379 | if (iovecs) |
| 380 | _vec_len (iovecs) = 0; |
| 381 | |
| 382 | /* Add packet descriptor */ |
| 383 | packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
| 384 | packetdesc.action = 0; |
| 385 | vec_add2 (iovecs, iov, 1); |
| 386 | iov->iov_base = &packetdesc; |
| 387 | iov->iov_len = sizeof (packetdesc); |
| 388 | |
| 389 | /** VLIB buffer chain -> Unix iovec(s). */ |
| 390 | vlib_buffer_advance (b, -(sizeof (ethernet_header_t))); |
| 391 | vec_add2 (iovecs, iov, 1); |
| 392 | iov->iov_base = b->data + b->current_data; |
| 393 | iov->iov_len = l = b->current_length; |
| 394 | |
| 395 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 396 | { |
| 397 | do |
| 398 | { |
| 399 | b = vlib_get_buffer (vm, b->next_buffer); |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 400 | if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) |
| 401 | { |
Klement Sekera | b716308 | 2017-11-08 04:13:49 +0100 | [diff] [blame] | 402 | if (PREDICT_FALSE (!c)) |
| 403 | { |
| 404 | c = punt_client_get (is_ip4, port); |
| 405 | } |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 406 | udp_punt_trace_t *t; |
| 407 | t = vlib_add_trace (vm, node, b, sizeof (t[0])); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 408 | clib_memcpy_fast (&t->client, c, sizeof (t->client)); |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 409 | t->is_midchain = 1; |
| 410 | } |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 411 | |
| 412 | vec_add2 (iovecs, iov, 1); |
| 413 | |
| 414 | iov->iov_base = b->data + b->current_data; |
| 415 | iov->iov_len = b->current_length; |
| 416 | l += b->current_length; |
| 417 | } |
| 418 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 419 | } |
| 420 | |
| 421 | struct msghdr msg = { |
| 422 | .msg_name = caddr, |
| 423 | .msg_namelen = sizeof (*caddr), |
| 424 | .msg_iov = iovecs, |
| 425 | .msg_iovlen = vec_len (iovecs), |
| 426 | }; |
| 427 | |
Klement Sekera | 6bd0bc3 | 2017-11-08 12:18:21 +0100 | [diff] [blame] | 428 | if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 429 | vlib_node_increment_counter (vm, node_index, |
| 430 | PUNT_ERROR_SOCKET_TX_ERROR, 1); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 431 | else |
| 432 | vlib_node_increment_counter (vm, node_index, PUNT_ERROR_SOCKET_TX, 1); |
| 433 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | error: |
Klement Sekera | 0dc11a1 | 2017-11-03 06:39:28 +0100 | [diff] [blame] | 437 | vlib_buffer_free (vm, buffers, n_packets); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 438 | |
| 439 | return n_packets; |
| 440 | } |
| 441 | |
| 442 | static uword |
| 443 | udp4_punt_socket (vlib_main_t * vm, |
| 444 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 445 | { |
| 446 | return udp46_punt_socket_inline (vm, node, from_frame, true /* is_ip4 */ ); |
| 447 | } |
| 448 | |
| 449 | static uword |
| 450 | udp6_punt_socket (vlib_main_t * vm, |
| 451 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 452 | { |
| 453 | return udp46_punt_socket_inline (vm, node, from_frame, false /* is_ip4 */ ); |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /* *INDENT-OFF* */ |
| 458 | VLIB_REGISTER_NODE (udp4_punt_socket_node) = { |
| 459 | .function = udp4_punt_socket, |
| 460 | .name = "ip4-udp-punt-socket", |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 461 | .format_trace = format_udp_punt_trace, |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 462 | .flags = VLIB_NODE_FLAG_IS_DROP, |
| 463 | /* Takes a vector of packets. */ |
| 464 | .vector_size = sizeof (u32), |
| 465 | .n_errors = PUNT_N_ERROR, |
| 466 | .error_strings = punt_error_strings, |
| 467 | }; |
| 468 | VLIB_REGISTER_NODE (udp6_punt_socket_node) = { |
| 469 | .function = udp6_punt_socket, |
| 470 | .name = "ip6-udp-punt-socket", |
Klement Sekera | 3c37ad5 | 2017-11-03 10:25:23 +0100 | [diff] [blame] | 471 | .format_trace = format_udp_punt_trace, |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 472 | .flags = VLIB_NODE_FLAG_IS_DROP, |
| 473 | .vector_size = sizeof (u32), |
| 474 | .n_errors = PUNT_N_ERROR, |
| 475 | .error_strings = punt_error_strings, |
| 476 | }; |
| 477 | /* *INDENT-ON* */ |
| 478 | |
| 479 | typedef struct |
| 480 | { |
| 481 | enum punt_action_e action; |
| 482 | u32 sw_if_index; |
| 483 | } punt_trace_t; |
| 484 | |
| 485 | static u8 * |
| 486 | format_punt_trace (u8 * s, va_list * va) |
| 487 | { |
| 488 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 489 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
| 490 | vnet_main_t *vnm = vnet_get_main (); |
| 491 | punt_trace_t *t = va_arg (*va, punt_trace_t *); |
| 492 | s = format (s, "%U Action: %d", format_vnet_sw_if_index_name, |
| 493 | vnm, t->sw_if_index, t->action); |
| 494 | return s; |
| 495 | } |
| 496 | |
| 497 | static uword |
| 498 | punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd) |
| 499 | { |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 500 | const uword buffer_size = vlib_buffer_get_default_data_size (vm); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 501 | u32 n_trace = vlib_get_trace_count (vm, node); |
| 502 | u32 next = node->cached_next_index; |
| 503 | u32 n_left_to_next, next_index; |
| 504 | u32 *to_next; |
| 505 | u32 error = PUNT_ERROR_NONE; |
| 506 | vlib_get_next_frame (vm, node, next, to_next, n_left_to_next); |
| 507 | |
| 508 | /* $$$$ Only dealing with one buffer at the time for now */ |
| 509 | |
| 510 | u32 bi; |
| 511 | vlib_buffer_t *b; |
| 512 | punt_packetdesc_t packetdesc; |
| 513 | ssize_t size; |
| 514 | struct iovec io[2]; |
| 515 | |
| 516 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 517 | { |
| 518 | error = PUNT_ERROR_NOBUFFER; |
| 519 | goto error; |
| 520 | } |
| 521 | |
| 522 | b = vlib_get_buffer (vm, bi); |
| 523 | io[0].iov_base = &packetdesc; |
| 524 | io[0].iov_len = sizeof (packetdesc); |
| 525 | io[1].iov_base = b->data; |
| 526 | io[1].iov_len = buffer_size; |
| 527 | |
| 528 | size = readv (fd, io, 2); |
| 529 | /* We need at least the packet descriptor plus a header */ |
| 530 | if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t))) |
| 531 | { |
| 532 | vlib_buffer_free (vm, &bi, 1); |
| 533 | error = PUNT_ERROR_READV; |
| 534 | goto error; |
| 535 | } |
| 536 | |
| 537 | b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 538 | b->current_length = size - sizeof (packetdesc); |
| 539 | |
| 540 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b); |
| 541 | |
| 542 | switch (packetdesc.action) |
| 543 | { |
| 544 | case PUNT_L2: |
| 545 | vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index; |
| 546 | next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT; |
| 547 | break; |
| 548 | |
| 549 | case PUNT_IP4_ROUTED: |
| 550 | vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index; |
| 551 | vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; |
| 552 | next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP; |
| 553 | break; |
| 554 | |
| 555 | case PUNT_IP6_ROUTED: |
| 556 | vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index; |
| 557 | vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; |
| 558 | next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP; |
| 559 | break; |
| 560 | |
| 561 | default: |
| 562 | error = PUNT_ERROR_ACTION; |
| 563 | vlib_buffer_free (vm, &bi, 1); |
| 564 | goto error; |
| 565 | } |
| 566 | |
| 567 | if (PREDICT_FALSE (n_trace > 0)) |
| 568 | { |
| 569 | punt_trace_t *t; |
| 570 | vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ ); |
| 571 | vlib_set_trace_count (vm, node, --n_trace); |
| 572 | t = vlib_add_trace (vm, node, b, sizeof (*t)); |
| 573 | t->sw_if_index = packetdesc.sw_if_index; |
| 574 | t->action = packetdesc.action; |
| 575 | } |
| 576 | |
| 577 | to_next[0] = bi; |
| 578 | to_next++; |
| 579 | n_left_to_next--; |
| 580 | |
| 581 | vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next, |
| 582 | bi, next_index); |
| 583 | vlib_put_next_frame (vm, node, next, n_left_to_next); |
| 584 | return 1; |
| 585 | |
| 586 | error: |
| 587 | vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static uword |
| 592 | punt_socket_rx (vlib_main_t * vm, |
| 593 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 594 | { |
| 595 | punt_main_t *pm = &punt_main; |
| 596 | u32 total_count = 0; |
| 597 | int i; |
| 598 | |
| 599 | for (i = 0; i < vec_len (pm->ready_fds); i++) |
| 600 | { |
| 601 | total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]); |
| 602 | vec_del1 (pm->ready_fds, i); |
| 603 | } |
| 604 | return total_count; |
| 605 | } |
| 606 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 607 | /* *INDENT-OFF* */ |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 608 | VLIB_REGISTER_NODE (punt_socket_rx_node, static) = |
| 609 | { |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 610 | .function = punt_socket_rx, |
| 611 | .name = "punt-socket-rx", |
| 612 | .type = VLIB_NODE_TYPE_INPUT, |
| 613 | .state = VLIB_NODE_STATE_INTERRUPT, |
| 614 | .vector_size = 1, |
| 615 | .n_errors = PUNT_N_ERROR, |
| 616 | .error_strings = punt_error_strings, |
| 617 | .n_next_nodes = PUNT_SOCKET_RX_N_NEXT, |
| 618 | .next_nodes = { |
| 619 | [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output", |
| 620 | [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 621 | [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 622 | }, |
| 623 | .format_trace = format_punt_trace, |
| 624 | }; |
| 625 | /* *INDENT-ON* */ |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 626 | |
| 627 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 628 | punt_socket_read_ready (clib_file_t * uf) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 629 | { |
| 630 | vlib_main_t *vm = vlib_get_main (); |
| 631 | punt_main_t *pm = &punt_main; |
| 632 | |
| 633 | /** Schedule the rx node */ |
| 634 | vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index); |
| 635 | vec_add1 (pm->ready_fds, uf->file_descriptor); |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | clib_error_t * |
| 641 | vnet_punt_socket_add (vlib_main_t * vm, u32 header_version, |
| 642 | bool is_ip4, u8 protocol, u16 port, |
| 643 | char *client_pathname) |
| 644 | { |
| 645 | punt_main_t *pm = &punt_main; |
| 646 | |
| 647 | if (!pm->is_configured) |
| 648 | return clib_error_return (0, "socket is not configured"); |
| 649 | |
| 650 | if (header_version != PUNT_PACKETDESC_VERSION) |
| 651 | return clib_error_return (0, "Invalid packet descriptor version"); |
| 652 | |
| 653 | /* For now we only support UDP punt */ |
| 654 | if (protocol != IP_PROTOCOL_UDP) |
| 655 | return clib_error_return (0, |
| 656 | "only UDP protocol (%d) is supported, got %d", |
| 657 | IP_PROTOCOL_UDP, protocol); |
| 658 | |
| 659 | if (port == (u16) ~ 0) |
| 660 | return clib_error_return (0, "UDP port number required"); |
| 661 | |
| 662 | /* Register client */ |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 663 | if (punt_socket_register (is_ip4, protocol, port, client_pathname) < 0) |
| 664 | return clib_error_return (0, |
| 665 | "Punt socket: Invalid client path: %s", |
| 666 | client_pathname); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 667 | |
| 668 | u32 node_index = is_ip4 ? udp4_punt_socket_node.index : |
| 669 | udp6_punt_socket_node.index; |
| 670 | |
| 671 | udp_register_dst_port (vm, port, node_index, is_ip4); |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | clib_error_t * |
| 677 | vnet_punt_socket_del (vlib_main_t * vm, bool is_ip4, u8 l4_protocol, u16 port) |
| 678 | { |
| 679 | punt_main_t *pm = &punt_main; |
| 680 | |
| 681 | if (!pm->is_configured) |
| 682 | return clib_error_return (0, "socket is not configured"); |
| 683 | |
| 684 | punt_socket_unregister (is_ip4, l4_protocol, port); |
| 685 | udp_unregister_dst_port (vm, port, is_ip4); |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 690 | /** |
| 691 | * @brief Request IP traffic punt to the local TCP/IP stack. |
| 692 | * |
| 693 | * @em Note |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 694 | * - UDP and TCP are the only protocols supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 695 | * |
| 696 | * @param vm vlib_main_t corresponding to the current thread |
| 697 | * @param ipv IP protcol version. |
| 698 | * 4 - IPv4, 6 - IPv6, ~0 for both IPv6 and IPv4 |
| 699 | * @param protocol 8-bits L4 protocol value |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 700 | * UDP is 17 |
| 701 | * TCP is 1 |
| 702 | * @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] | 703 | * |
| 704 | * @returns 0 on success, non-zero value otherwise |
| 705 | */ |
| 706 | clib_error_t * |
| 707 | vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port, |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 708 | bool is_add) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 709 | { |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 710 | |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 711 | /* For now we only support TCP, UDP and SCTP punt */ |
| 712 | if (protocol != IP_PROTOCOL_UDP && |
| 713 | protocol != IP_PROTOCOL_TCP && protocol != IP_PROTOCOL_SCTP) |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 714 | return clib_error_return (0, |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 715 | "only UDP (%d), TCP (%d) and SCTP (%d) protocols are supported, got %d", |
| 716 | IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, |
| 717 | IP_PROTOCOL_SCTP, protocol); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 718 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 719 | if (ipv != (u8) ~ 0 && ipv != 4 && ipv != 6) |
| 720 | return clib_error_return (0, "IP version must be 4 or 6, got %d", ipv); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 721 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 722 | if (port == (u16) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 723 | { |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 724 | if ((ipv == 4) || (ipv == (u8) ~ 0)) |
| 725 | { |
| 726 | if (protocol == IP_PROTOCOL_UDP) |
| 727 | udp_punt_unknown (vm, 1, is_add); |
| 728 | else if (protocol == IP_PROTOCOL_TCP) |
| 729 | tcp_punt_unknown (vm, 1, is_add); |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 730 | else if (protocol == IP_PROTOCOL_SCTP) |
| 731 | sctp_punt_unknown (vm, 1, is_add); |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 732 | } |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 733 | |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 734 | if ((ipv == 6) || (ipv == (u8) ~ 0)) |
| 735 | { |
| 736 | if (protocol == IP_PROTOCOL_UDP) |
| 737 | udp_punt_unknown (vm, 0, is_add); |
| 738 | else if (protocol == IP_PROTOCOL_TCP) |
| 739 | tcp_punt_unknown (vm, 0, is_add); |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 740 | else if (protocol == IP_PROTOCOL_SCTP) |
| 741 | sctp_punt_unknown (vm, 0, is_add); |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 742 | } |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | else if (is_add) |
| 748 | { |
Marco Varlese | 191a594 | 2017-10-30 18:17:21 +0100 | [diff] [blame] | 749 | if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP) |
| 750 | return clib_error_return (0, |
| 751 | "punt TCP/SCTP ports is not supported yet"); |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 752 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 753 | if (ipv == 4 || ipv == (u8) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 754 | udp_register_dst_port (vm, port, udp4_punt_node.index, 1); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 755 | |
| 756 | if (ipv == 6 || ipv == (u8) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 757 | udp_register_dst_port (vm, port, udp6_punt_node.index, 0); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 758 | |
| 759 | return 0; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 760 | } |
| 761 | else |
Pavel Kotucek | 41b923a | 2018-12-05 17:16:23 +0100 | [diff] [blame] | 762 | { |
| 763 | if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP) |
| 764 | return clib_error_return (0, |
| 765 | "punt TCP/SCTP ports is not supported yet"); |
| 766 | if (ipv == 4 || ipv == (u8) ~ 0) |
| 767 | udp_unregister_dst_port (vm, port, 1); |
| 768 | |
| 769 | if (ipv == 6 || ipv == (u8) ~ 0) |
| 770 | udp_unregister_dst_port (vm, port, 0); |
| 771 | |
| 772 | return 0; |
| 773 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | static clib_error_t * |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 777 | punt_cli (vlib_main_t * vm, |
| 778 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 779 | { |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 780 | u32 port = ~0; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 781 | bool is_add = true; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 782 | u32 protocol = ~0; |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 783 | clib_error_t *error = NULL; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 784 | |
| 785 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 786 | { |
| 787 | if (unformat (input, "del")) |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 788 | is_add = false; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 789 | else if (unformat (input, "all")) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 790 | ; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 791 | else if (unformat (input, "%d", &port)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 792 | ; |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 793 | else if (unformat (input, "udp")) |
| 794 | protocol = IP_PROTOCOL_UDP; |
| 795 | else if (unformat (input, "tcp")) |
| 796 | protocol = IP_PROTOCOL_TCP; |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 797 | else |
| 798 | { |
| 799 | error = clib_error_return (0, "parse error: '%U'", |
| 800 | format_unformat_error, input); |
| 801 | goto done; |
| 802 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 803 | } |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 804 | |
| 805 | /* punt both IPv6 and IPv4 when used in CLI */ |
| 806 | error = vnet_punt_add_del (vm, ~0, protocol, port, is_add); |
| 807 | if (error) |
| 808 | { |
| 809 | clib_error_report (error); |
| 810 | goto done; |
| 811 | } |
| 812 | |
Swarup Nayak | 1b70884 | 2017-12-13 13:27:23 +0530 | [diff] [blame] | 813 | done: |
| 814 | return error; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /*? |
| 818 | * The set of '<em>set punt</em>' commands allows specific IP traffic to |
| 819 | * be punted to the host TCP/IP stack |
| 820 | * |
| 821 | * @em Note |
| 822 | * - UDP is the only protocol supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 823 | * - All TCP traffic is currently punted to the host by default |
| 824 | * |
| 825 | * @cliexpar |
| 826 | * @parblock |
| 827 | * Example of how to request NTP traffic to be punted |
| 828 | * @cliexcmd{set punt udp 125} |
| 829 | * |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 830 | * Example of how to request all 'unknown' UDP traffic to be punted |
| 831 | * @cliexcmd{set punt udp all} |
| 832 | * |
| 833 | * Example of how to stop all 'unknown' UDP traffic to be punted |
| 834 | * @cliexcmd{set punt udp del all} |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 835 | * @endparblock |
| 836 | ?*/ |
| 837 | /* *INDENT-OFF* */ |
Pierre Pfister | 7fe51f3 | 2017-09-20 08:48:36 +0200 | [diff] [blame] | 838 | VLIB_CLI_COMMAND (punt_command, static) = { |
| 839 | .path = "set punt", |
| 840 | .short_help = "set punt [udp|tcp] [del] <all | port-num1 [port-num2 ...]>", |
| 841 | .function = punt_cli, |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 842 | }; |
| 843 | /* *INDENT-ON* */ |
| 844 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 845 | static clib_error_t * |
| 846 | punt_socket_register_cmd (vlib_main_t * vm, |
| 847 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 848 | { |
| 849 | bool is_ipv4 = true; |
| 850 | u32 protocol = ~0; |
| 851 | u32 port = ~0; |
| 852 | u8 *socket_name = 0; |
| 853 | clib_error_t *error = NULL; |
| 854 | |
| 855 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 856 | { |
| 857 | if (unformat (input, "ipv4")) |
| 858 | ; |
| 859 | else if (unformat (input, "ipv6")) |
| 860 | is_ipv4 = false; |
| 861 | else if (unformat (input, "udp")) |
| 862 | protocol = IP_PROTOCOL_UDP; |
| 863 | else if (unformat (input, "tcp")) |
| 864 | protocol = IP_PROTOCOL_TCP; |
| 865 | else if (unformat (input, "%d", &port)) |
| 866 | ; |
| 867 | else if (unformat (input, "socket %s", &socket_name)) |
| 868 | ; |
| 869 | else |
| 870 | { |
| 871 | error = clib_error_return (0, "parse error: '%U'", |
| 872 | format_unformat_error, input); |
| 873 | goto done; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | error = |
| 878 | vnet_punt_socket_add (vm, 1, is_ipv4, protocol, port, |
| 879 | (char *) socket_name); |
| 880 | if (error) |
| 881 | { |
| 882 | goto done; |
| 883 | } |
| 884 | done: |
| 885 | return error; |
| 886 | } |
| 887 | |
| 888 | /*? |
| 889 | * |
| 890 | * @cliexpar |
| 891 | * @cliexcmd{punt socket register} |
| 892 | ?*/ |
| 893 | /* *INDENT-OFF* */ |
| 894 | VLIB_CLI_COMMAND (punt_socket_register_command, static) = |
| 895 | { |
| 896 | .path = "punt socket register", |
| 897 | .function = punt_socket_register_cmd, |
| 898 | .short_help = "punt socket register [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]> <socket>", |
| 899 | .is_mp_safe = 1, |
| 900 | }; |
| 901 | /* *INDENT-ON* */ |
| 902 | |
| 903 | static clib_error_t * |
| 904 | punt_socket_deregister_cmd (vlib_main_t * vm, |
| 905 | unformat_input_t * input, |
| 906 | vlib_cli_command_t * cmd) |
| 907 | { |
| 908 | bool is_ipv4 = true; |
| 909 | u32 protocol = ~0; |
| 910 | u32 port = ~0; |
| 911 | clib_error_t *error = NULL; |
| 912 | |
| 913 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 914 | { |
| 915 | if (unformat (input, "ipv4")) |
| 916 | ; |
| 917 | else if (unformat (input, "ipv6")) |
| 918 | is_ipv4 = false; |
| 919 | else if (unformat (input, "udp")) |
| 920 | protocol = IP_PROTOCOL_UDP; |
| 921 | else if (unformat (input, "tcp")) |
| 922 | protocol = IP_PROTOCOL_TCP; |
| 923 | else if (unformat (input, "%d", &port)) |
| 924 | ; |
| 925 | else |
| 926 | { |
| 927 | error = clib_error_return (0, "parse error: '%U'", |
| 928 | format_unformat_error, input); |
| 929 | goto done; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | error = vnet_punt_socket_del (vm, is_ipv4, protocol, port); |
| 934 | if (error) |
| 935 | { |
| 936 | goto done; |
| 937 | } |
| 938 | done: |
| 939 | return error; |
| 940 | } |
| 941 | |
| 942 | /*? |
| 943 | * |
| 944 | * @cliexpar |
| 945 | * @cliexcmd{punt socket register} |
| 946 | ?*/ |
| 947 | /* *INDENT-OFF* */ |
| 948 | VLIB_CLI_COMMAND (punt_socket_deregister_command, static) = |
| 949 | { |
| 950 | .path = "punt socket deregister", |
| 951 | .function = punt_socket_deregister_cmd, |
| 952 | .short_help = "punt socket deregister [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]>", |
| 953 | .is_mp_safe = 1, |
| 954 | }; |
| 955 | /* *INDENT-ON* */ |
| 956 | |
| 957 | punt_socket_detail_t * |
| 958 | punt_socket_entries (u8 ipv) |
| 959 | { |
| 960 | punt_main_t *pm = &punt_main; |
| 961 | punt_client_t *pc; |
| 962 | punt_socket_detail_t *ps = 0; |
| 963 | bool is_valid; |
| 964 | |
| 965 | punt_client_t *v = !ipv ? pm->clients_by_dst_port4 : |
| 966 | pm->clients_by_dst_port6; |
| 967 | |
| 968 | vec_foreach (pc, v) |
| 969 | { |
| 970 | if (pc && pc->port != 0) |
| 971 | { |
| 972 | is_valid = false; |
| 973 | if (pc->protocol == IP_PROTOCOL_UDP) |
| 974 | { |
| 975 | is_valid = udp_is_valid_dst_port (pc->port, !ipv); |
| 976 | } |
| 977 | if (is_valid) |
| 978 | { |
| 979 | punt_socket_detail_t detail = { |
| 980 | .ipv = ipv, |
| 981 | .l4_protocol = pc->protocol, |
| 982 | .l4_port = pc->port |
| 983 | }; |
| 984 | memcpy (detail.pathname, pc->caddr.sun_path, |
| 985 | sizeof (pc->caddr.sun_path)); |
| 986 | vec_add1 (ps, detail); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | return ps; |
| 991 | } |
| 992 | |
| 993 | u8 * |
| 994 | format_punt_socket (u8 * s, va_list * args) |
| 995 | { |
| 996 | punt_client_t *clients = va_arg (*args, punt_client_t *); |
| 997 | u8 *is_ipv6 = va_arg (*args, u8 *); |
| 998 | punt_client_t *pc; |
| 999 | bool is_valid; |
| 1000 | |
| 1001 | vec_foreach (pc, clients) |
| 1002 | { |
| 1003 | if (pc && pc->port != 0) |
| 1004 | { |
| 1005 | is_valid = false; |
| 1006 | if (pc->protocol == IP_PROTOCOL_UDP) |
| 1007 | { |
| 1008 | is_valid = udp_is_valid_dst_port (pc->port, !(*is_ipv6)); |
| 1009 | } |
| 1010 | if (is_valid) |
| 1011 | { |
| 1012 | s = format (s, " punt %s port %d to socket %s \n", |
| 1013 | (pc->protocol == IP_PROTOCOL_UDP) ? "UDP" : "TCP", |
| 1014 | pc->port, pc->caddr.sun_path); |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | return (s); |
| 1020 | } |
| 1021 | |
| 1022 | static clib_error_t * |
| 1023 | punt_socket_show_cmd (vlib_main_t * vm, |
| 1024 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 1025 | { |
| 1026 | u8 is_ipv6; |
| 1027 | punt_main_t *pm = &punt_main; |
| 1028 | clib_error_t *error = NULL; |
| 1029 | |
| 1030 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1031 | { |
| 1032 | if (unformat (input, "ipv4")) |
| 1033 | is_ipv6 = 0; |
| 1034 | else if (unformat (input, "ipv6")) |
| 1035 | is_ipv6 = 1; |
| 1036 | else |
| 1037 | { |
| 1038 | error = clib_error_return (0, "parse error: '%U'", |
| 1039 | format_unformat_error, input); |
| 1040 | goto done; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | punt_client_t *v = |
| 1045 | is_ipv6 ? pm->clients_by_dst_port6 : pm->clients_by_dst_port4; |
| 1046 | vlib_cli_output (vm, "%U", format_punt_socket, v, &is_ipv6); |
| 1047 | |
| 1048 | done: |
| 1049 | return (error); |
| 1050 | } |
| 1051 | |
| 1052 | /*? |
| 1053 | * |
| 1054 | * @cliexpar |
| 1055 | * @cliexcmd{show punt socket ipv4} |
| 1056 | ?*/ |
| 1057 | /* *INDENT-OFF* */ |
| 1058 | VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) = |
| 1059 | { |
| 1060 | .path = "show punt socket registrations", |
| 1061 | .function = punt_socket_show_cmd, |
| 1062 | .short_help = "show punt socket registrations [ipv4|ipv6]", |
| 1063 | .is_mp_safe = 1, |
| 1064 | }; |
| 1065 | /* *INDENT-ON* */ |
| 1066 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 1067 | clib_error_t * |
| 1068 | punt_init (vlib_main_t * vm) |
| 1069 | { |
| 1070 | punt_main_t *pm = &punt_main; |
| 1071 | |
| 1072 | pm->clients_by_dst_port6 = sparse_vec_new |
| 1073 | (sizeof (pm->clients_by_dst_port6[0]), |
| 1074 | BITS (((udp_header_t *) 0)->dst_port)); |
| 1075 | pm->clients_by_dst_port4 = sparse_vec_new |
| 1076 | (sizeof (pm->clients_by_dst_port4[0]), |
| 1077 | BITS (((udp_header_t *) 0)->dst_port)); |
| 1078 | |
| 1079 | pm->is_configured = false; |
| 1080 | pm->interface_output_node = vlib_get_node_by_name (vm, |
| 1081 | (u8 *) |
| 1082 | "interface-output"); |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | VLIB_INIT_FUNCTION (punt_init); |
| 1087 | |
| 1088 | static clib_error_t * |
| 1089 | punt_config (vlib_main_t * vm, unformat_input_t * input) |
| 1090 | { |
| 1091 | punt_main_t *pm = &punt_main; |
| 1092 | char *socket_path = 0; |
| 1093 | |
| 1094 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1095 | { |
| 1096 | if (unformat (input, "socket %s", &socket_path)) |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 1097 | strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 1098 | else |
| 1099 | return clib_error_return (0, "unknown input `%U'", |
| 1100 | format_unformat_error, input); |
| 1101 | } |
| 1102 | |
| 1103 | if (socket_path == 0) |
| 1104 | return 0; |
| 1105 | |
| 1106 | /* UNIX domain socket */ |
| 1107 | struct sockaddr_un addr; |
| 1108 | if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1) |
| 1109 | { |
| 1110 | return clib_error_return (0, "socket error"); |
| 1111 | } |
| 1112 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1113 | clib_memset (&addr, 0, sizeof (addr)); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 1114 | addr.sun_family = AF_UNIX; |
| 1115 | if (*socket_path == '\0') |
| 1116 | { |
| 1117 | *addr.sun_path = '\0'; |
| 1118 | strncpy (addr.sun_path + 1, socket_path + 1, |
| 1119 | sizeof (addr.sun_path) - 2); |
| 1120 | } |
| 1121 | else |
| 1122 | { |
| 1123 | strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1); |
| 1124 | unlink (socket_path); |
| 1125 | } |
| 1126 | |
| 1127 | if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1) |
| 1128 | { |
| 1129 | return clib_error_return (0, "bind error"); |
| 1130 | } |
| 1131 | |
| 1132 | /* Register socket */ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1133 | clib_file_main_t *fm = &file_main; |
| 1134 | clib_file_t template = { 0 }; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 1135 | template.read_function = punt_socket_read_ready; |
| 1136 | template.file_descriptor = pm->socket_fd; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 1137 | template.description = format (0, "%s", socket_path); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1138 | pm->clib_file_index = clib_file_add (fm, &template); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 1139 | |
| 1140 | pm->is_configured = true; |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | VLIB_CONFIG_FUNCTION (punt_config, "punt"); |
| 1146 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 1147 | /* |
| 1148 | * fd.io coding-style-patch-verification: ON |
| 1149 | * |
| 1150 | * Local Variables: |
| 1151 | * eval: (c-set-style "gnu") |
| 1152 | * End: |
| 1153 | */ |