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:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 * @brief Local TCP/IP stack punt infrastructure.
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
24 #include <vlib/vlib.h>
25 #include <vnet/pg/pg.h>
26 #include <vnet/udp/udp.h>
27 #include <vnet/ip/punt.h>
29 #define foreach_punt_next \
30 _ (PUNT, "error-punt")
34 #define _(s,n) PUNT_NEXT_##s,
40 vlib_node_registration_t udp4_punt_node;
41 vlib_node_registration_t udp6_punt_node;
43 /** @brief IPv4/IPv6 UDP punt node main loop.
45 This is the main loop inline function for IPv4/IPv6 UDP punt
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
54 udp46_punt_inline (vlib_main_t * vm,
55 vlib_node_runtime_t * node,
56 vlib_frame_t * from_frame, int is_ip4)
58 u32 n_left_from, *from, *to_next;
61 from = vlib_frame_vector_args (from_frame);
62 n_left_from = from_frame->n_vectors;
64 /* udp[46]_lookup hands us the data payload, not the IP header */
66 advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
68 advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
70 while (n_left_from > 0)
74 vlib_get_next_frame (vm, node, PUNT_NEXT_PUNT, to_next, n_left_to_next);
76 while (n_left_from > 0 && n_left_to_next > 0)
88 b0 = vlib_get_buffer (vm, bi0);
89 vlib_buffer_advance (b0, advance);
90 b0->error = node->errors[PUNT_ERROR_UDP_PORT];
93 vlib_put_next_frame (vm, node, PUNT_NEXT_PUNT, n_left_to_next);
96 return from_frame->n_vectors;
99 static char *punt_error_strings[] = {
100 #define punt_error(n,s) s,
101 #include "punt_error.def"
105 /** @brief IPv4 UDP punt node.
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".
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
117 @par Graph mechanics: next index usage
120 - <code>vnet_buffer(b)->current_data</code>
121 - <code>vnet_buffer(b)->current_len</code>
124 - Dispatches the packet to the "error-punt" node
127 udp4_punt (vlib_main_t * vm,
128 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
130 return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
133 /** @brief IPv6 UDP punt node.
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".
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
145 @par Graph mechanics: next index usage
148 - <code>vnet_buffer(b)->current_data</code>
149 - <code>vnet_buffer(b)->current_len</code>
152 - Dispatches the packet to the "error-punt" node
155 udp6_punt (vlib_main_t * vm,
156 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
158 return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
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),
168 .n_errors = PUNT_N_ERROR,
169 .error_strings = punt_error_strings,
171 .n_next_nodes = PUNT_N_NEXT,
173 #define _(s,n) [PUNT_NEXT_##s] = n,
179 VLIB_NODE_FUNCTION_MULTIARCH (udp4_punt_node, udp4_punt);
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),
187 .n_errors = PUNT_N_ERROR,
188 .error_strings = punt_error_strings,
190 .n_next_nodes = PUNT_N_NEXT,
192 #define _(s,n) [PUNT_NEXT_##s] = n,
199 VLIB_NODE_FUNCTION_MULTIARCH (udp6_punt_node, udp6_punt);;
202 * @brief Request IP traffic punt to the local TCP/IP stack.
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
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
216 * @returns 0 on success, non-zero value otherwise
219 vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port,
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);
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);
231 if (port == (u16) ~ 0)
233 if (ipv == 4 || ipv == (u8) ~ 0)
234 udp_punt_unknown (vm, 1, is_add);
236 if (ipv == 6 || ipv == (u8) ~ 0)
237 udp_punt_unknown (vm, 0, is_add);
244 if (ipv == 4 || ipv == (u8) ~ 0)
245 udp_register_dst_port (vm, port, udp4_punt_node.index, 1);
247 if (ipv == 6 || ipv == (u8) ~ 0)
248 udp_register_dst_port (vm, port, udp6_punt_node.index, 0);
254 return clib_error_return (0, "punt delete is not supported yet");
257 static clib_error_t *
258 udp_punt_cli (vlib_main_t * vm,
259 unformat_input_t * input, vlib_cli_command_t * cmd)
265 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
267 if (unformat (input, "del"))
269 if (unformat (input, "all"))
271 /* punt both IPv6 and IPv4 when used in CLI */
272 error = vnet_punt_add_del (vm, ~0, IP_PROTOCOL_UDP, ~0, is_add);
274 clib_error_report (error);
276 else if (unformat (input, "%d", &udp_port))
278 /* punt both IPv6 and IPv4 when used in CLI */
279 error = vnet_punt_add_del (vm, ~0, IP_PROTOCOL_UDP,
282 clib_error_report (error);
290 * The set of '<em>set punt</em>' commands allows specific IP traffic to
291 * be punted to the host TCP/IP stack
294 * - UDP is the only protocol supported in the current implementation
295 * - All TCP traffic is currently punted to the host by default
299 * Example of how to request NTP traffic to be punted
300 * @cliexcmd{set punt udp 125}
302 * Example of how to request all 'unknown' UDP traffic to be punted
303 * @cliexcmd{set punt udp all}
305 * Example of how to stop all 'unknown' UDP traffic to be punted
306 * @cliexcmd{set punt udp del all}
310 VLIB_CLI_COMMAND (punt_udp_command, static) = {
311 .path = "set punt udp",
312 .short_help = "set punt udp [del] <all | port-num1 [port-num2 ...]>",
313 .function = udp_punt_cli,
318 * fd.io coding-style-patch-verification: ON
321 * eval: (c-set-style "gnu")