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 | * |
| 20 | * Provides a set of VPP nodes togather with the relevant APIs and CLI |
| 21 | * commands in order to adjust and dispatch packets from the VPP data plane |
| 22 | * to the local TCP/IP stack |
| 23 | */ |
| 24 | #include <vlib/vlib.h> |
| 25 | #include <vnet/pg/pg.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | #include <vnet/udp/udp.h> |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 27 | #include <vnet/ip/punt.h> |
| 28 | |
| 29 | #define foreach_punt_next \ |
| 30 | _ (PUNT, "error-punt") |
| 31 | |
| 32 | typedef enum |
| 33 | { |
| 34 | #define _(s,n) PUNT_NEXT_##s, |
| 35 | foreach_punt_next |
| 36 | #undef _ |
| 37 | PUNT_N_NEXT, |
| 38 | } punt_next_t; |
| 39 | |
| 40 | vlib_node_registration_t udp4_punt_node; |
| 41 | vlib_node_registration_t udp6_punt_node; |
| 42 | |
| 43 | /** @brief IPv4/IPv6 UDP punt node main loop. |
| 44 | |
| 45 | This is the main loop inline function for IPv4/IPv6 UDP punt |
| 46 | transition node. |
| 47 | |
| 48 | @param vm vlib_main_t corresponding to the current thread |
| 49 | @param node vlib_node_runtime_t |
| 50 | @param frame vlib_frame_t whose contents should be dispatched |
| 51 | @param is_ipv4 indicates if called for IPv4 or IPv6 node |
| 52 | */ |
| 53 | always_inline uword |
| 54 | udp46_punt_inline (vlib_main_t * vm, |
| 55 | vlib_node_runtime_t * node, |
| 56 | vlib_frame_t * from_frame, int is_ip4) |
| 57 | { |
| 58 | u32 n_left_from, *from, *to_next; |
| 59 | word advance; |
| 60 | |
| 61 | from = vlib_frame_vector_args (from_frame); |
| 62 | n_left_from = from_frame->n_vectors; |
| 63 | |
| 64 | /* udp[46]_lookup hands us the data payload, not the IP header */ |
| 65 | if (is_ip4) |
| 66 | advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t)); |
| 67 | else |
| 68 | advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t)); |
| 69 | |
| 70 | while (n_left_from > 0) |
| 71 | { |
| 72 | u32 n_left_to_next; |
| 73 | |
| 74 | vlib_get_next_frame (vm, node, PUNT_NEXT_PUNT, to_next, n_left_to_next); |
| 75 | |
| 76 | while (n_left_from > 0 && n_left_to_next > 0) |
| 77 | { |
| 78 | u32 bi0; |
| 79 | vlib_buffer_t *b0; |
| 80 | |
| 81 | bi0 = from[0]; |
| 82 | to_next[0] = bi0; |
| 83 | from += 1; |
| 84 | to_next += 1; |
| 85 | n_left_from -= 1; |
| 86 | n_left_to_next -= 1; |
| 87 | |
| 88 | b0 = vlib_get_buffer (vm, bi0); |
| 89 | vlib_buffer_advance (b0, advance); |
| 90 | b0->error = node->errors[PUNT_ERROR_UDP_PORT]; |
| 91 | } |
| 92 | |
| 93 | vlib_put_next_frame (vm, node, PUNT_NEXT_PUNT, n_left_to_next); |
| 94 | } |
| 95 | |
| 96 | return from_frame->n_vectors; |
| 97 | } |
| 98 | |
| 99 | static char *punt_error_strings[] = { |
| 100 | #define punt_error(n,s) s, |
| 101 | #include "punt_error.def" |
| 102 | #undef punt_error |
| 103 | }; |
| 104 | |
| 105 | /** @brief IPv4 UDP punt node. |
| 106 | @node ip4-udp-punt |
| 107 | |
| 108 | This is the IPv4 UDP punt transition node. It is registered as a next |
| 109 | node for the "ip4-udp-lookup" handling UDP port(s) requested for punt. |
| 110 | The buffer's current data pointer is adjusted to the original packet |
| 111 | IPv4 header. All buffers are dispatched to "error-punt". |
| 112 | |
| 113 | @param vm vlib_main_t corresponding to the current thread |
| 114 | @param node vlib_node_runtime_t |
| 115 | @param frame vlib_frame_t whose contents should be dispatched |
| 116 | |
| 117 | @par Graph mechanics: next index usage |
| 118 | |
| 119 | @em Sets: |
| 120 | - <code>vnet_buffer(b)->current_data</code> |
| 121 | - <code>vnet_buffer(b)->current_len</code> |
| 122 | |
| 123 | <em>Next Index:</em> |
| 124 | - Dispatches the packet to the "error-punt" node |
| 125 | */ |
| 126 | static uword |
| 127 | udp4_punt (vlib_main_t * vm, |
| 128 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 129 | { |
| 130 | return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ ); |
| 131 | } |
| 132 | |
| 133 | /** @brief IPv6 UDP punt node. |
| 134 | @node ip6-udp-punt |
| 135 | |
| 136 | This is the IPv6 UDP punt transition node. It is registered as a next |
| 137 | node for the "ip6-udp-lookup" handling UDP port(s) requested for punt. |
| 138 | The buffer's current data pointer is adjusted to the original packet |
| 139 | IPv6 header. All buffers are dispatched to "error-punt". |
| 140 | |
| 141 | @param vm vlib_main_t corresponding to the current thread |
| 142 | @param node vlib_node_runtime_t |
| 143 | @param frame vlib_frame_t whose contents should be dispatched |
| 144 | |
| 145 | @par Graph mechanics: next index usage |
| 146 | |
| 147 | @em Sets: |
| 148 | - <code>vnet_buffer(b)->current_data</code> |
| 149 | - <code>vnet_buffer(b)->current_len</code> |
| 150 | |
| 151 | <em>Next Index:</em> |
| 152 | - Dispatches the packet to the "error-punt" node |
| 153 | */ |
| 154 | static uword |
| 155 | udp6_punt (vlib_main_t * vm, |
| 156 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 157 | { |
| 158 | return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ ); |
| 159 | } |
| 160 | |
| 161 | /* *INDENT-OFF* */ |
| 162 | VLIB_REGISTER_NODE (udp4_punt_node) = { |
| 163 | .function = udp4_punt, |
| 164 | .name = "ip4-udp-punt", |
| 165 | /* Takes a vector of packets. */ |
| 166 | .vector_size = sizeof (u32), |
| 167 | |
| 168 | .n_errors = PUNT_N_ERROR, |
| 169 | .error_strings = punt_error_strings, |
| 170 | |
| 171 | .n_next_nodes = PUNT_N_NEXT, |
| 172 | .next_nodes = { |
| 173 | #define _(s,n) [PUNT_NEXT_##s] = n, |
| 174 | foreach_punt_next |
| 175 | #undef _ |
| 176 | }, |
| 177 | }; |
| 178 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 179 | VLIB_NODE_FUNCTION_MULTIARCH (udp4_punt_node, udp4_punt); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 180 | |
| 181 | VLIB_REGISTER_NODE (udp6_punt_node) = { |
| 182 | .function = udp6_punt, |
| 183 | .name = "ip6-udp-punt", |
| 184 | /* Takes a vector of packets. */ |
| 185 | .vector_size = sizeof (u32), |
| 186 | |
| 187 | .n_errors = PUNT_N_ERROR, |
| 188 | .error_strings = punt_error_strings, |
| 189 | |
| 190 | .n_next_nodes = PUNT_N_NEXT, |
| 191 | .next_nodes = { |
| 192 | #define _(s,n) [PUNT_NEXT_##s] = n, |
| 193 | foreach_punt_next |
| 194 | #undef _ |
| 195 | }, |
| 196 | }; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 197 | /* *INDENT-ON* */ |
| 198 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 199 | VLIB_NODE_FUNCTION_MULTIARCH (udp6_punt_node, udp6_punt);; |
| 200 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 201 | /** |
| 202 | * @brief Request IP traffic punt to the local TCP/IP stack. |
| 203 | * |
| 204 | * @em Note |
| 205 | * - UDP is the only protocol supported in the current implementation |
| 206 | * - When requesting UDP punt port number(s) must be specified |
| 207 | * - All TCP traffic is currently punted to the host by default |
| 208 | * |
| 209 | * @param vm vlib_main_t corresponding to the current thread |
| 210 | * @param ipv IP protcol version. |
| 211 | * 4 - IPv4, 6 - IPv6, ~0 for both IPv6 and IPv4 |
| 212 | * @param protocol 8-bits L4 protocol value |
| 213 | * Only value of 17 (UDP) is currently supported |
| 214 | * @param port 16-bits L4 (TCP/IP) port number when applicable |
| 215 | * |
| 216 | * @returns 0 on success, non-zero value otherwise |
| 217 | */ |
| 218 | clib_error_t * |
| 219 | vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port, |
| 220 | int is_add) |
| 221 | { |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 222 | /* For now we only support UDP punt */ |
| 223 | if (protocol != IP_PROTOCOL_UDP) |
| 224 | return clib_error_return (0, |
| 225 | "only UDP protocol (%d) is supported, got %d", |
| 226 | IP_PROTOCOL_UDP, protocol); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 227 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 228 | if (ipv != (u8) ~ 0 && ipv != 4 && ipv != 6) |
| 229 | 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] | 230 | |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 231 | if (port == (u16) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 232 | { |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 233 | if (ipv == 4 || ipv == (u8) ~ 0) |
| 234 | udp_punt_unknown (vm, 1, is_add); |
| 235 | |
| 236 | if (ipv == 6 || ipv == (u8) ~ 0) |
| 237 | udp_punt_unknown (vm, 0, is_add); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | else if (is_add) |
| 243 | { |
| 244 | if (ipv == 4 || ipv == (u8) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 245 | udp_register_dst_port (vm, port, udp4_punt_node.index, 1); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 246 | |
| 247 | if (ipv == 6 || ipv == (u8) ~ 0) |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 248 | udp_register_dst_port (vm, port, udp6_punt_node.index, 0); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 249 | |
| 250 | return 0; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 251 | } |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 252 | |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 253 | else |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 254 | return clib_error_return (0, "punt delete is not supported yet"); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static clib_error_t * |
| 258 | udp_punt_cli (vlib_main_t * vm, |
| 259 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 260 | { |
| 261 | u32 udp_port; |
| 262 | int is_add = 1; |
| 263 | clib_error_t *error; |
| 264 | |
| 265 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 266 | { |
| 267 | if (unformat (input, "del")) |
| 268 | is_add = 0; |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 269 | if (unformat (input, "all")) |
| 270 | { |
| 271 | /* punt both IPv6 and IPv4 when used in CLI */ |
| 272 | error = vnet_punt_add_del (vm, ~0, IP_PROTOCOL_UDP, ~0, is_add); |
| 273 | if (error) |
| 274 | clib_error_report (error); |
| 275 | } |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 276 | else if (unformat (input, "%d", &udp_port)) |
| 277 | { |
| 278 | /* punt both IPv6 and IPv4 when used in CLI */ |
| 279 | error = vnet_punt_add_del (vm, ~0, IP_PROTOCOL_UDP, |
| 280 | udp_port, is_add); |
| 281 | if (error) |
| 282 | clib_error_report (error); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /*? |
| 290 | * The set of '<em>set punt</em>' commands allows specific IP traffic to |
| 291 | * be punted to the host TCP/IP stack |
| 292 | * |
| 293 | * @em Note |
| 294 | * - UDP is the only protocol supported in the current implementation |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 295 | * - All TCP traffic is currently punted to the host by default |
| 296 | * |
| 297 | * @cliexpar |
| 298 | * @parblock |
| 299 | * Example of how to request NTP traffic to be punted |
| 300 | * @cliexcmd{set punt udp 125} |
| 301 | * |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 302 | * Example of how to request all 'unknown' UDP traffic to be punted |
| 303 | * @cliexcmd{set punt udp all} |
| 304 | * |
| 305 | * Example of how to stop all 'unknown' UDP traffic to be punted |
| 306 | * @cliexcmd{set punt udp del all} |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 307 | * @endparblock |
| 308 | ?*/ |
| 309 | /* *INDENT-OFF* */ |
| 310 | VLIB_CLI_COMMAND (punt_udp_command, static) = { |
| 311 | .path = "set punt udp", |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 312 | .short_help = "set punt udp [del] <all | port-num1 [port-num2 ...]>", |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 313 | .function = udp_punt_cli, |
| 314 | }; |
| 315 | /* *INDENT-ON* */ |
| 316 | |
| 317 | /* |
| 318 | * fd.io coding-style-patch-verification: ON |
| 319 | * |
| 320 | * Local Variables: |
| 321 | * eval: (c-set-style "gnu") |
| 322 | * End: |
| 323 | */ |