blob: a3ccc43375e1e106f0a919d5887ada2e301e78ae [file] [log] [blame]
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -07001/*
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 Troanf7a55ad2017-05-16 14:59:29 +020020 * Provides a set of VPP nodes together with the relevant APIs and CLI
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070021 * commands in order to adjust and dispatch packets from the VPP data plane
22 * to the local TCP/IP stack
23 */
Ole Troanf7a55ad2017-05-16 14:59:29 +020024
25#include <vnet/ip/ip.h>
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070026#include <vlib/vlib.h>
27#include <vnet/pg/pg.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050028#include <vnet/udp/udp.h>
Pierre Pfister7fe51f32017-09-20 08:48:36 +020029#include <vnet/tcp/tcp.h>
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070030#include <vnet/ip/punt.h>
Ole Troanf7a55ad2017-05-16 14:59:29 +020031#include <vlib/unix/unix.h>
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070032
Ole Troanf7a55ad2017-05-16 14:59:29 +020033#include <stdio.h>
34#include <unistd.h>
35#include <sys/socket.h>
Marco Varlese22349832017-09-08 10:40:34 +020036#include <sys/uio.h>
Ole Troanf7a55ad2017-05-16 14:59:29 +020037#include <stdlib.h>
Ole Troanf7a55ad2017-05-16 14:59:29 +020038
Ole Troanf7a55ad2017-05-16 14:59:29 +020039punt_main_t punt_main;
40
41char *
42vnet_punt_get_server_pathname (void)
43{
44 punt_main_t *pm = &punt_main;
45 return pm->sun_path;
46}
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -070047
Ole Troanf7a55ad2017-05-16 14:59:29 +020048static void
Neale Ranns50f0ac02019-05-15 02:13:37 -070049punt_client_l4_db_add (ip_address_family_t af, u16 port, u32 index)
Ole Troanf7a55ad2017-05-16 14:59:29 +020050{
51 punt_main_t *pm = &punt_main;
Ole Troanf7a55ad2017-05-16 14:59:29 +020052
Neale Ranns50f0ac02019-05-15 02:13:37 -070053 pm->db.clients_by_l4_port = hash_set (pm->db.clients_by_l4_port,
54 punt_client_l4_mk_key (af, port),
55 index);
Ole Troanf7a55ad2017-05-16 14:59:29 +020056}
57
Neale Ranns50f0ac02019-05-15 02:13:37 -070058static u32
59punt_client_l4_db_remove (ip_address_family_t af, u16 port)
Ole Troanf7a55ad2017-05-16 14:59:29 +020060{
Neale Ranns50f0ac02019-05-15 02:13:37 -070061 punt_main_t *pm = &punt_main;
62 u32 key, index = ~0;
63 uword *p;
64
65 key = punt_client_l4_mk_key (af, port);
66 p = hash_get (pm->db.clients_by_l4_port, key);
67
68 if (p)
69 index = p[0];
70
71 hash_unset (pm->db.clients_by_l4_port, key);
72
73 return (index);
74}
75
76static void
Neale Rannsb538dd82019-05-21 06:54:54 -070077punt_client_ip_proto_db_add (ip_address_family_t af,
78 ip_protocol_t proto, u32 index)
79{
80 punt_main_t *pm = &punt_main;
81
82 pm->db.clients_by_ip_proto = hash_set (pm->db.clients_by_ip_proto,
83 punt_client_ip_proto_mk_key (af,
84 proto),
85 index);
86}
87
88static u32
89punt_client_ip_proto_db_remove (ip_address_family_t af, ip_protocol_t proto)
90{
91 punt_main_t *pm = &punt_main;
92 u32 key, index = ~0;
93 uword *p;
94
95 key = punt_client_ip_proto_mk_key (af, proto);
96 p = hash_get (pm->db.clients_by_ip_proto, key);
97
98 if (p)
99 index = p[0];
100
101 hash_unset (pm->db.clients_by_ip_proto, key);
102
103 return (index);
104}
105
106static void
Neale Ranns50f0ac02019-05-15 02:13:37 -0700107punt_client_exception_db_add (vlib_punt_reason_t reason, u32 pci)
108{
109 punt_main_t *pm = &punt_main;
110
111 vec_validate_init_empty (pm->db.clients_by_exception, reason, ~0);
112
113 pm->db.clients_by_exception[reason] = pci;
114}
115
116static u32
117punt_client_exception_db_remove (vlib_punt_reason_t reason)
118{
119 punt_main_t *pm = &punt_main;
120 u32 pci = ~0;
121
122 if (punt_client_exception_get (reason))
123 {
124 pci = pm->db.clients_by_exception[reason];
125 pm->db.clients_by_exception[reason] = ~0;
126 }
127
128 return pci;
129}
Ole Troanf7a55ad2017-05-16 14:59:29 +0200130
131static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200132punt_socket_read_ready (clib_file_t * uf)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200133{
134 vlib_main_t *vm = vlib_get_main ();
135 punt_main_t *pm = &punt_main;
136
137 /** Schedule the rx node */
138 vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index);
139 vec_add1 (pm->ready_fds, uf->file_descriptor);
140
141 return 0;
142}
143
Neale Ranns50f0ac02019-05-15 02:13:37 -0700144static clib_error_t *
145punt_socket_register_l4 (vlib_main_t * vm,
146 ip_address_family_t af,
147 u8 protocol, u16 port, char *client_pathname)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200148{
149 punt_main_t *pm = &punt_main;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700150 punt_client_t *c;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200151
152 /* For now we only support UDP punt */
153 if (protocol != IP_PROTOCOL_UDP)
154 return clib_error_return (0,
155 "only UDP protocol (%d) is supported, got %d",
156 IP_PROTOCOL_UDP, protocol);
157
158 if (port == (u16) ~ 0)
159 return clib_error_return (0, "UDP port number required");
160
Neale Ranns50f0ac02019-05-15 02:13:37 -0700161 c = punt_client_l4_get (af, port);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200162
Neale Ranns50f0ac02019-05-15 02:13:37 -0700163 if (NULL == c)
164 {
165 pool_get_zero (pm->punt_client_pool, c);
166 punt_client_l4_db_add (af, port, c - pm->punt_client_pool);
167 }
Ole Troanf7a55ad2017-05-16 14:59:29 +0200168
Neale Ranns50f0ac02019-05-15 02:13:37 -0700169 memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
170 c->caddr.sun_family = AF_UNIX;
171 c->reg.type = PUNT_TYPE_L4;
172 c->reg.punt.l4.port = port;
173 c->reg.punt.l4.protocol = protocol;
174 c->reg.punt.l4.af = af;
175
176 u32 node_index = (af == AF_IP4 ?
177 udp4_punt_socket_node.index :
178 udp6_punt_socket_node.index);
179
180 udp_register_dst_port (vm, port, node_index, af == AF_IP4);
181
182 return (NULL);
183}
184
185static clib_error_t *
Neale Rannsb538dd82019-05-21 06:54:54 -0700186punt_socket_register_ip_proto (vlib_main_t * vm,
187 ip_address_family_t af,
188 ip_protocol_t proto, char *client_pathname)
189{
190 punt_main_t *pm = &punt_main;
191 punt_client_t *c;
192
193 c = punt_client_ip_proto_get (af, proto);
194
195 if (NULL == c)
196 {
197 pool_get_zero (pm->punt_client_pool, c);
198 punt_client_ip_proto_db_add (af, proto, c - pm->punt_client_pool);
199 }
200
201 memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
202 c->caddr.sun_family = AF_UNIX;
203 c->reg.type = PUNT_TYPE_IP_PROTO;
204 c->reg.punt.ip_proto.protocol = proto;
205 c->reg.punt.ip_proto.af = af;
206
207 if (af == AF_IP4)
208 ip4_register_protocol (proto, ip4_proto_punt_socket_node.index);
209 else
210 ip6_register_protocol (proto, ip6_proto_punt_socket_node.index);
211
212 return (NULL);
213}
214
215static clib_error_t *
Neale Ranns50f0ac02019-05-15 02:13:37 -0700216punt_socket_register_exception (vlib_main_t * vm,
217 vlib_punt_reason_t reason,
218 char *client_pathname)
219{
220 punt_main_t *pm = &punt_main;
221 punt_client_t *pc;
222
223 pc = punt_client_exception_get (reason);
224
225 if (NULL == pc)
226 {
227 pool_get_zero (pm->punt_client_pool, pc);
228 punt_client_exception_db_add (reason, pc - pm->punt_client_pool);
229 }
230
231 memcpy (pc->caddr.sun_path, client_pathname, sizeof (pc->caddr.sun_path));
232 pc->caddr.sun_family = AF_UNIX;
233 pc->reg.type = PUNT_TYPE_EXCEPTION;
234 pc->reg.punt.exception.reason = reason;
235
236 vlib_punt_register (pm->hdl,
237 pc->reg.punt.exception.reason, "exception-punt-socket");
238
239 return (NULL);
240}
241
242static clib_error_t *
243punt_socket_unregister_l4 (ip_address_family_t af,
244 ip_protocol_t protocol, u16 port)
245{
246 u32 pci;
247
248 udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4);
249
250 pci = punt_client_l4_db_remove (af, port);
251
252 if (~0 != pci)
253 pool_put_index (punt_main.punt_client_pool, pci);
254
255 return (NULL);
256}
257
258static clib_error_t *
Neale Rannsb538dd82019-05-21 06:54:54 -0700259punt_socket_unregister_ip_proto (ip_address_family_t af, ip_protocol_t proto)
260{
261 u32 pci;
262
263 if (af == AF_IP4)
264 ip4_unregister_protocol (proto);
265 else
266 ip6_unregister_protocol (proto);
267
268 pci = punt_client_ip_proto_db_remove (af, proto);
269
270 if (~0 != pci)
271 pool_put_index (punt_main.punt_client_pool, pci);
272
273 return (NULL);
274}
275
276static clib_error_t *
Neale Ranns50f0ac02019-05-15 02:13:37 -0700277punt_socket_unregister_exception (vlib_punt_reason_t reason)
278{
279 u32 pci;
280
281 pci = punt_client_exception_db_remove (reason);
282
283 if (~0 != pci)
284 pool_put_index (punt_main.punt_client_pool, pci);
285
286 return (NULL);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200287}
288
289clib_error_t *
Neale Ranns50f0ac02019-05-15 02:13:37 -0700290vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
291 const punt_reg_t * pr, char *client_pathname)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200292{
293 punt_main_t *pm = &punt_main;
294
295 if (!pm->is_configured)
296 return clib_error_return (0, "socket is not configured");
297
Neale Ranns50f0ac02019-05-15 02:13:37 -0700298 if (header_version != PUNT_PACKETDESC_VERSION)
299 return clib_error_return (0, "Invalid packet descriptor version");
300
Neale Rannsb538dd82019-05-21 06:54:54 -0700301 if (strncmp (client_pathname, vnet_punt_get_server_pathname (),
302 UNIX_PATH_MAX) == 0)
303 return clib_error_return (0,
304 "Punt socket: Invalid client path: %s",
305 client_pathname);
306
Neale Ranns50f0ac02019-05-15 02:13:37 -0700307 /* Register client */
308 switch (pr->type)
309 {
310 case PUNT_TYPE_L4:
311 return (punt_socket_register_l4 (vm,
312 pr->punt.l4.af,
313 pr->punt.l4.protocol,
314 pr->punt.l4.port, client_pathname));
Neale Rannsb538dd82019-05-21 06:54:54 -0700315 case PUNT_TYPE_IP_PROTO:
316 return (punt_socket_register_ip_proto (vm,
317 pr->punt.ip_proto.af,
318 pr->punt.ip_proto.protocol,
319 client_pathname));
Neale Ranns50f0ac02019-05-15 02:13:37 -0700320 case PUNT_TYPE_EXCEPTION:
321 return (punt_socket_register_exception (vm,
322 pr->punt.exception.reason,
323 client_pathname));
324 }
325
326 return 0;
327}
328
329clib_error_t *
330vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr)
331{
332 punt_main_t *pm = &punt_main;
333
334 if (!pm->is_configured)
335 return clib_error_return (0, "socket is not configured");
336
337 switch (pr->type)
338 {
339 case PUNT_TYPE_L4:
340 return (punt_socket_unregister_l4 (pr->punt.l4.af,
341 pr->punt.l4.protocol,
342 pr->punt.l4.port));
Neale Rannsb538dd82019-05-21 06:54:54 -0700343 case PUNT_TYPE_IP_PROTO:
344 return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af,
345 pr->punt.ip_proto.protocol));
Neale Ranns50f0ac02019-05-15 02:13:37 -0700346 case PUNT_TYPE_EXCEPTION:
347 return (punt_socket_unregister_exception (pr->punt.exception.reason));
348 }
Ole Troanf7a55ad2017-05-16 14:59:29 +0200349
350 return 0;
351}
352
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700353/**
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400354 * @brief Request IP L4 traffic punt to the local TCP/IP stack.
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700355 *
356 * @em Note
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400357 * - UDP is the only protocol supported in the current implementation
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700358 *
359 * @param vm vlib_main_t corresponding to the current thread
Neale Ranns50f0ac02019-05-15 02:13:37 -0700360 * @param af IP address family.
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700361 * @param protocol 8-bits L4 protocol value
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200362 * UDP is 17
363 * TCP is 1
364 * @param port 16-bits L4 (TCP/IP) port number when applicable (UDP only)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700365 *
366 * @returns 0 on success, non-zero value otherwise
367 */
Neale Ranns50f0ac02019-05-15 02:13:37 -0700368static clib_error_t *
369punt_l4_add_del (vlib_main_t * vm,
370 ip_address_family_t af,
371 ip_protocol_t protocol, u16 port, bool is_add)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700372{
Florin Coras3ffe6ca2019-06-26 16:27:13 -0700373 /* For now we only support TCP and UDP punt */
374 if (protocol != IP_PROTOCOL_UDP && protocol != IP_PROTOCOL_TCP)
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800375 return clib_error_return (0,
Florin Coras3ffe6ca2019-06-26 16:27:13 -0700376 "only UDP (%d) and TCP (%d) protocols are supported, got %d",
377 IP_PROTOCOL_UDP, IP_PROTOCOL_TCP, protocol);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700378
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800379 if (port == (u16) ~ 0)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700380 {
Neale Ranns50f0ac02019-05-15 02:13:37 -0700381 if (protocol == IP_PROTOCOL_UDP)
382 udp_punt_unknown (vm, af == AF_IP4, is_add);
383 else if (protocol == IP_PROTOCOL_TCP)
384 tcp_punt_unknown (vm, af == AF_IP4, is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800385
386 return 0;
387 }
388
389 else if (is_add)
390 {
Florin Coras3ffe6ca2019-06-26 16:27:13 -0700391 if (protocol == IP_PROTOCOL_TCP)
392 return clib_error_return (0, "punt TCP ports is not supported yet");
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200393
Neale Ranns50f0ac02019-05-15 02:13:37 -0700394 udp_register_dst_port (vm, port, udp4_punt_node.index, af == AF_IP4);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800395
396 return 0;
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700397 }
398 else
Pavel Kotucek41b923a2018-12-05 17:16:23 +0100399 {
Florin Coras3ffe6ca2019-06-26 16:27:13 -0700400 if (protocol == IP_PROTOCOL_TCP)
401 return clib_error_return (0, "punt TCP ports is not supported yet");
Pavel Kotucek41b923a2018-12-05 17:16:23 +0100402
Neale Ranns50f0ac02019-05-15 02:13:37 -0700403 udp_unregister_dst_port (vm, port, af == AF_IP4);
Pavel Kotucek41b923a2018-12-05 17:16:23 +0100404
405 return 0;
406 }
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700407}
Neale Ranns50f0ac02019-05-15 02:13:37 -0700408
Neale Ranns50f0ac02019-05-15 02:13:37 -0700409clib_error_t *
410vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
411{
412 switch (pr->type)
413 {
414 case PUNT_TYPE_L4:
415 return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
416 pr->punt.l4.port, is_add));
417 case PUNT_TYPE_EXCEPTION:
Neale Rannsb538dd82019-05-21 06:54:54 -0700418 case PUNT_TYPE_IP_PROTO:
419 break;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700420 }
421
422 return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
423}
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700424
425static clib_error_t *
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200426punt_cli (vlib_main_t * vm,
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200427 unformat_input_t * input__, vlib_cli_command_t * cmd)
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700428{
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200429 unformat_input_t line_input, *input = &line_input;
Swarup Nayak1b708842017-12-13 13:27:23 +0530430 clib_error_t *error = NULL;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700431 bool is_add = true;
Neale Ranns68577d22019-06-04 13:31:23 +0000432 /* *INDENT-OFF* */
Neale Ranns50f0ac02019-05-15 02:13:37 -0700433 punt_reg_t pr = {
434 .punt = {
Neale Ranns68577d22019-06-04 13:31:23 +0000435 .l4 = {
436 .af = AF_IP4,
437 .port = ~0,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400438 .protocol = IP_PROTOCOL_UDP,
Neale Ranns68577d22019-06-04 13:31:23 +0000439 },
440 },
Neale Ranns50f0ac02019-05-15 02:13:37 -0700441 .type = PUNT_TYPE_L4,
442 };
Neale Ranns68577d22019-06-04 13:31:23 +0000443 u32 port;
444 /* *INDENT-ON* */
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700445
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200446 if (!unformat_user (input__, unformat_line_input, input))
447 return 0;
448
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700449 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
450 {
451 if (unformat (input, "del"))
Ole Troanf7a55ad2017-05-16 14:59:29 +0200452 is_add = false;
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400453 else if (unformat (input, "ipv4"))
454 pr.punt.l4.af = AF_IP4;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700455 else if (unformat (input, "ipv6"))
456 pr.punt.l4.af = AF_IP6;
457 else if (unformat (input, "ip6"))
458 pr.punt.l4.af = AF_IP6;
Neale Ranns68577d22019-06-04 13:31:23 +0000459 else if (unformat (input, "%d", &port))
460 pr.punt.l4.port = port;
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400461 else if (unformat (input, "all"))
462 pr.punt.l4.port = ~0;
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200463 else if (unformat (input, "udp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700464 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200465 else if (unformat (input, "tcp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700466 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
Swarup Nayak1b708842017-12-13 13:27:23 +0530467 else
468 {
469 error = clib_error_return (0, "parse error: '%U'",
470 format_unformat_error, input);
471 goto done;
472 }
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700473 }
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100474
475 /* punt both IPv6 and IPv4 when used in CLI */
Neale Ranns50f0ac02019-05-15 02:13:37 -0700476 error = vnet_punt_add_del (vm, &pr, is_add);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100477 if (error)
478 {
479 clib_error_report (error);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100480 }
481
Swarup Nayak1b708842017-12-13 13:27:23 +0530482done:
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200483 unformat_free (input);
Swarup Nayak1b708842017-12-13 13:27:23 +0530484 return error;
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700485}
486
487/*?
488 * The set of '<em>set punt</em>' commands allows specific IP traffic to
489 * be punted to the host TCP/IP stack
490 *
491 * @em Note
492 * - UDP is the only protocol supported in the current implementation
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700493 * - All TCP traffic is currently punted to the host by default
494 *
495 * @cliexpar
496 * @parblock
497 * Example of how to request NTP traffic to be punted
498 * @cliexcmd{set punt udp 125}
499 *
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800500 * Example of how to request all 'unknown' UDP traffic to be punted
501 * @cliexcmd{set punt udp all}
502 *
503 * Example of how to stop all 'unknown' UDP traffic to be punted
504 * @cliexcmd{set punt udp del all}
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700505 * @endparblock
506?*/
507/* *INDENT-OFF* */
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200508VLIB_CLI_COMMAND (punt_command, static) = {
509 .path = "set punt",
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400510 .short_help = "set punt [IPV4|ip6|ipv6] [UDP|tcp] [del] [ALL|<port-num>]",
Pierre Pfister7fe51f32017-09-20 08:48:36 +0200511 .function = punt_cli,
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700512};
513/* *INDENT-ON* */
514
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100515static clib_error_t *
516punt_socket_register_cmd (vlib_main_t * vm,
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200517 unformat_input_t * input__,
518 vlib_cli_command_t * cmd)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100519{
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200520 unformat_input_t line_input, *input = &line_input;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100521 u8 *socket_name = 0;
522 clib_error_t *error = NULL;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700523 /* *INDENT-OFF* */
524 punt_reg_t pr = {
525 .punt = {
526 .l4 = {
527 .af = AF_IP4,
528 .port = ~0,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400529 .protocol = IP_PROTOCOL_UDP,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700530 },
531 },
532 .type = PUNT_TYPE_L4,
533 };
534 /* *INDENT-ON* */
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100535
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200536 if (!unformat_user (input__, unformat_line_input, input))
537 return 0;
538
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100539 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
540 {
541 if (unformat (input, "ipv4"))
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400542 pr.punt.l4.af = AF_IP4;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100543 else if (unformat (input, "ipv6"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700544 pr.punt.l4.af = AF_IP6;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100545 else if (unformat (input, "udp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700546 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100547 else if (unformat (input, "tcp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700548 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
549 else if (unformat (input, "%d", &pr.punt.l4.port))
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100550 ;
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400551 else if (unformat (input, "all"))
552 pr.punt.l4.port = ~0;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100553 else if (unformat (input, "socket %s", &socket_name))
554 ;
555 else
556 {
557 error = clib_error_return (0, "parse error: '%U'",
558 format_unformat_error, input);
559 goto done;
560 }
561 }
562
Neale Ranns4f3c1042019-06-11 01:39:08 -0700563 if (!socket_name)
564 error = clib_error_return (0, "socket name not specified");
565 else
566 error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700567
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100568done:
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200569 unformat_free (input);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100570 return error;
571}
572
573/*?
574 *
575 * @cliexpar
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400576 * @cliexcmd{punt socket register socket punt_l4_foo.sock}
577
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100578 ?*/
579/* *INDENT-OFF* */
580VLIB_CLI_COMMAND (punt_socket_register_command, static) =
581{
582 .path = "punt socket register",
583 .function = punt_socket_register_cmd,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400584 .short_help = "punt socket register [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>] socket <socket>",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100585 .is_mp_safe = 1,
586};
587/* *INDENT-ON* */
588
589static clib_error_t *
590punt_socket_deregister_cmd (vlib_main_t * vm,
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200591 unformat_input_t * input__,
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100592 vlib_cli_command_t * cmd)
593{
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200594 unformat_input_t line_input, *input = &line_input;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100595 clib_error_t *error = NULL;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700596 /* *INDENT-OFF* */
597 punt_reg_t pr = {
598 .punt = {
599 .l4 = {
600 .af = AF_IP4,
601 .port = ~0,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400602 .protocol = IP_PROTOCOL_UDP,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700603 },
604 },
605 .type = PUNT_TYPE_L4,
606 };
607 /* *INDENT-ON* */
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100608
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200609 if (!unformat_user (input__, unformat_line_input, input))
610 return 0;
611
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100612 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
613 {
614 if (unformat (input, "ipv4"))
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400615 pr.punt.l4.af = AF_IP4;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100616 else if (unformat (input, "ipv6"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700617 pr.punt.l4.af = AF_IP6;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100618 else if (unformat (input, "udp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700619 pr.punt.l4.protocol = IP_PROTOCOL_UDP;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100620 else if (unformat (input, "tcp"))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700621 pr.punt.l4.protocol = IP_PROTOCOL_TCP;
622 else if (unformat (input, "%d", &pr.punt.l4.port))
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100623 ;
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400624 else if (unformat (input, "all"))
625 pr.punt.l4.port = ~0;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100626 else
627 {
628 error = clib_error_return (0, "parse error: '%U'",
629 format_unformat_error, input);
630 goto done;
631 }
632 }
633
Neale Ranns50f0ac02019-05-15 02:13:37 -0700634 error = vnet_punt_socket_del (vm, &pr);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100635done:
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200636 unformat_free (input);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100637 return error;
638}
639
640/*?
641 *
642 * @cliexpar
643 * @cliexcmd{punt socket register}
644 ?*/
645/* *INDENT-OFF* */
646VLIB_CLI_COMMAND (punt_socket_deregister_command, static) =
647{
648 .path = "punt socket deregister",
649 .function = punt_socket_deregister_cmd,
Paul Vinciguerra32c4d382019-10-23 16:07:32 -0400650 .short_help = "punt socket deregister [IPV4|ipv6] [UDP|tcp] [ALL|<port-num>]",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100651 .is_mp_safe = 1,
652};
653/* *INDENT-ON* */
654
Neale Ranns50f0ac02019-05-15 02:13:37 -0700655void
656punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100657{
658 punt_main_t *pm = &punt_main;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100659
Neale Ranns50f0ac02019-05-15 02:13:37 -0700660 switch (pt)
661 {
662 case PUNT_TYPE_L4:
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100663 {
Neale Rannsb538dd82019-05-21 06:54:54 -0700664 u32 pci, key;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700665
666 /* *INDENT-OFF* */
Neale Rannsb538dd82019-05-21 06:54:54 -0700667 hash_foreach(key, pci, pm->db.clients_by_l4_port,
668 ({
669 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
670 }));
671 /* *INDENT-ON* */
672 break;
673 }
674 case PUNT_TYPE_IP_PROTO:
675 {
676 u32 pci, key;
677
678 /* *INDENT-OFF* */
679 hash_foreach(key, pci, pm->db.clients_by_ip_proto,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700680 ({
681 cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
682 }));
683 /* *INDENT-ON* */
684 break;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100685 }
Neale Ranns50f0ac02019-05-15 02:13:37 -0700686 case PUNT_TYPE_EXCEPTION:
687 {
688 u32 *pci;
689
690 vec_foreach (pci, pm->db.clients_by_exception)
691 {
692 if (~0 != *pci)
693 cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
694 }
695
696 break;
697 }
698 }
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100699}
700
Neale Ranns50f0ac02019-05-15 02:13:37 -0700701static u8 *
702format_punt_client (u8 * s, va_list * args)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100703{
Neale Ranns50f0ac02019-05-15 02:13:37 -0700704 punt_client_t *pc = va_arg (*args, punt_client_t *);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100705
Neale Ranns50f0ac02019-05-15 02:13:37 -0700706 s = format (s, " punt ");
707
708 switch (pc->reg.type)
709 {
710 case PUNT_TYPE_L4:
711 s = format (s, "%U %U port %d",
712 format_ip_address_family, pc->reg.punt.l4.af,
713 format_ip_protocol, pc->reg.punt.l4.protocol,
714 pc->reg.punt.l4.port);
715 break;
Neale Rannsb538dd82019-05-21 06:54:54 -0700716 case PUNT_TYPE_IP_PROTO:
717 s = format (s, "%U %U",
718 format_ip_address_family, pc->reg.punt.ip_proto.af,
719 format_ip_protocol, pc->reg.punt.ip_proto.protocol);
720 break;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700721 case PUNT_TYPE_EXCEPTION:
722 s = format (s, " %U", format_vlib_punt_reason,
723 pc->reg.punt.exception.reason);
724 break;
725 }
726
727 s = format (s, " to socket %s \n", pc->caddr.sun_path);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100728
729 return (s);
730}
731
Neale Ranns50f0ac02019-05-15 02:13:37 -0700732static walk_rc_t
733punt_client_show_one (const punt_client_t * pc, void *ctx)
734{
735 vlib_cli_output (ctx, "%U", format_punt_client, pc);
736
737 return (WALK_CONTINUE);
738}
739
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100740static clib_error_t *
741punt_socket_show_cmd (vlib_main_t * vm,
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200742 unformat_input_t * input__, vlib_cli_command_t * cmd)
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100743{
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200744 unformat_input_t line_input, *input = &line_input;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100745 clib_error_t *error = NULL;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700746 punt_type_t pt;
747
748 pt = PUNT_TYPE_L4;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100749
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200750 if (!unformat_user (input__, unformat_line_input, input))
751 return 0;
752
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100753 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
754 {
Neale Ranns50f0ac02019-05-15 02:13:37 -0700755 if (unformat (input, "exception"))
756 pt = PUNT_TYPE_EXCEPTION;
757 else if (unformat (input, "l4"))
758 pt = PUNT_TYPE_L4;
Neale Rannsb538dd82019-05-21 06:54:54 -0700759 else if (unformat (input, "ip"))
760 pt = PUNT_TYPE_IP_PROTO;
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100761 else
762 {
763 error = clib_error_return (0, "parse error: '%U'",
764 format_unformat_error, input);
765 goto done;
766 }
767 }
768
Neale Ranns50f0ac02019-05-15 02:13:37 -0700769 punt_client_walk (pt, punt_client_show_one, vm);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100770
771done:
BenoƮt Ganne9ae3c6a2020-07-27 18:27:57 +0200772 unformat_free (input);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100773 return (error);
774}
775
776/*?
777 *
778 * @cliexpar
779 * @cliexcmd{show punt socket ipv4}
780 ?*/
781/* *INDENT-OFF* */
782VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
783{
784 .path = "show punt socket registrations",
785 .function = punt_socket_show_cmd,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700786 .short_help = "show punt socket registrations [l4|exception]",
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100787 .is_mp_safe = 1,
788};
789/* *INDENT-ON* */
790
Ole Troanf7a55ad2017-05-16 14:59:29 +0200791clib_error_t *
Neale Ranns76b56492018-09-28 15:16:14 +0000792ip_punt_init (vlib_main_t * vm)
Ole Troanf7a55ad2017-05-16 14:59:29 +0200793{
Neale Ranns50f0ac02019-05-15 02:13:37 -0700794 clib_error_t *error = NULL;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200795 punt_main_t *pm = &punt_main;
Neale Ranns39040a62019-07-10 01:47:15 -0700796 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ole Troanf7a55ad2017-05-16 14:59:29 +0200797
Ole Troanf7a55ad2017-05-16 14:59:29 +0200798 pm->is_configured = false;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700799 pm->interface_output_node =
800 vlib_get_node_by_name (vm, (u8 *) "interface-output");
801
802 if ((error = vlib_call_init_function (vm, punt_init)))
803 return error;
804
805 pm->hdl = vlib_punt_client_register ("ip-punt");
806
Neale Ranns39040a62019-07-10 01:47:15 -0700807 vec_validate_aligned (pm->thread_data, tm->n_vlib_mains,
808 CLIB_CACHE_LINE_BYTES);
809
Neale Ranns50f0ac02019-05-15 02:13:37 -0700810 return (error);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200811}
812
Neale Ranns76b56492018-09-28 15:16:14 +0000813VLIB_INIT_FUNCTION (ip_punt_init);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200814
815static clib_error_t *
816punt_config (vlib_main_t * vm, unformat_input_t * input)
817{
818 punt_main_t *pm = &punt_main;
819 char *socket_path = 0;
820
821 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
822 {
823 if (unformat (input, "socket %s", &socket_path))
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100824 strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200825 else
826 return clib_error_return (0, "unknown input `%U'",
827 format_unformat_error, input);
828 }
829
830 if (socket_path == 0)
831 return 0;
832
833 /* UNIX domain socket */
834 struct sockaddr_un addr;
835 if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
836 {
837 return clib_error_return (0, "socket error");
838 }
839
Dave Barachb7b92992018-10-17 10:38:51 -0400840 clib_memset (&addr, 0, sizeof (addr));
Ole Troanf7a55ad2017-05-16 14:59:29 +0200841 addr.sun_family = AF_UNIX;
842 if (*socket_path == '\0')
843 {
844 *addr.sun_path = '\0';
845 strncpy (addr.sun_path + 1, socket_path + 1,
846 sizeof (addr.sun_path) - 2);
847 }
848 else
849 {
850 strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
851 unlink (socket_path);
852 }
853
854 if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
855 {
856 return clib_error_return (0, "bind error");
857 }
858
Neale Ranns50f0ac02019-05-15 02:13:37 -0700859 int n_bytes = 0x10000;
860
861 if (setsockopt
862 (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
863 sizeof (n_bytes)) == -1)
864 {
865 return clib_error_return (0, "setsockopt error");
866 }
867
Ole Troanf7a55ad2017-05-16 14:59:29 +0200868 /* Register socket */
Damjan Marion56dd5432017-09-08 19:52:02 +0200869 clib_file_main_t *fm = &file_main;
870 clib_file_t template = { 0 };
Ole Troanf7a55ad2017-05-16 14:59:29 +0200871 template.read_function = punt_socket_read_ready;
872 template.file_descriptor = pm->socket_fd;
Damjan Marionceab7882018-01-19 20:56:12 +0100873 template.description = format (0, "%s", socket_path);
Damjan Marion56dd5432017-09-08 19:52:02 +0200874 pm->clib_file_index = clib_file_add (fm, &template);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200875
876 pm->is_configured = true;
877
878 return 0;
879}
880
881VLIB_CONFIG_FUNCTION (punt_config, "punt");
882
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700883/*
884 * fd.io coding-style-patch-verification: ON
885 *
886 * Local Variables:
887 * eval: (c-set-style "gnu")
888 * End:
889 */