blob: dae611cc418419aa603f3401691850345865be08 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Dave Barach68b0fb02017-02-28 15:15:56 -05002 * Copyright (c) 2017 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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 */
Dave Barach68b0fb02017-02-28 15:15:56 -050015#ifndef __included_udp_h__
16#define __included_udp_h__
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
18#include <vnet/vnet.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vnet/udp/udp_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <vnet/ip/ip.h>
21#include <vnet/ip/ip4.h>
22#include <vnet/ip/ip4_packet.h>
23#include <vnet/pg/pg.h>
24#include <vnet/ip/format.h>
25
Dave Barach68b0fb02017-02-28 15:15:56 -050026#include <vnet/ip/ip.h>
27#include <vnet/session/transport.h>
28
29typedef struct
30{
31 transport_connection_t connection; /** must be first */
32
33 /** ersatz MTU to limit fifo pushes to test data size */
34 u32 mtu;
35} udp_connection_t;
36
37typedef struct _udp_uri_main
38{
39 /* Per-worker thread udp connection pools */
40 udp_connection_t **udp_sessions;
41 udp_connection_t *udp_listeners;
42
43 /* convenience */
44 vlib_main_t *vlib_main;
45 vnet_main_t *vnet_main;
46 ip4_main_t *ip4_main;
47 ip6_main_t *ip6_main;
48} udp_uri_main_t;
49
50extern udp_uri_main_t udp_uri_main;
51extern vlib_node_registration_t udp4_uri_input_node;
52
53always_inline udp_uri_main_t *
54vnet_get_udp_main ()
55{
56 return &udp_uri_main;
57}
58
59always_inline udp_connection_t *
60udp_connection_get (u32 conn_index, u32 thread_index)
61{
62 return pool_elt_at_index (udp_uri_main.udp_sessions[thread_index],
63 conn_index);
64}
65
66always_inline udp_connection_t *
67udp_listener_get (u32 conn_index)
68{
69 return pool_elt_at_index (udp_uri_main.udp_listeners, conn_index);
70}
71
Dave Barachd7cb1b52016-12-09 09:52:16 -050072typedef enum
73{
Ed Warnickecb9cada2015-12-08 15:45:58 -070074#define udp_error(n,s) UDP_ERROR_##n,
Dave Barach68b0fb02017-02-28 15:15:56 -050075#include <vnet/udp/udp_error.def>
Ed Warnickecb9cada2015-12-08 15:45:58 -070076#undef udp_error
77 UDP_N_ERROR,
78} udp_error_t;
79
80#define foreach_udp4_dst_port \
81_ (67, dhcp_to_server) \
82_ (68, dhcp_to_client) \
83_ (500, ikev2) \
Hongjun Nief486b12017-04-12 19:21:16 +080084_ (2152, GTPU) \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010085_ (3784, bfd4) \
86_ (3785, bfd_echo4) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070087_ (4341, lisp_gpe) \
Florin Corase127a7e2016-02-18 22:20:01 +010088_ (4342, lisp_cp) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070089_ (4739, ipfix) \
90_ (4789, vxlan) \
Chris Luke99cb3352016-04-26 10:49:53 -040091_ (4789, vxlan6) \
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080092_ (4790, VXLAN_GPE) \
Marco Varleseb598f1d2017-09-19 14:25:28 +020093_ (6633, vpath_3) \
94_ (6081, geneve)
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96
97#define foreach_udp6_dst_port \
98_ (547, dhcpv6_to_server) \
99_ (546, dhcpv6_to_client) \
Hongjun Nief486b12017-04-12 19:21:16 +0800100_ (2152, GTPU6) \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100101_ (3784, bfd6) \
102_ (3785, bfd_echo6) \
Florin Coras02655bd2016-04-26 00:17:24 +0200103_ (4341, lisp_gpe6) \
Florin Corase127a7e2016-02-18 22:20:01 +0100104_ (4342, lisp_cp6) \
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800105_ (4790, VXLAN6_GPE) \
Marco Varleseb598f1d2017-09-19 14:25:28 +0200106_ (6633, vpath6_3) \
107_ (6081, geneve6)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
Dave Barachd7cb1b52016-12-09 09:52:16 -0500109typedef enum
110{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111#define _(n,f) UDP_DST_PORT_##f = n,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500112 foreach_udp4_dst_port foreach_udp6_dst_port
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113#undef _
114} udp_dst_port_t;
115
Dave Barachd7cb1b52016-12-09 09:52:16 -0500116typedef enum
117{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118#define _(n,f) UDP6_DST_PORT_##f = n,
119 foreach_udp6_dst_port
120#undef _
121} udp6_dst_port_t;
122
Dave Barachd7cb1b52016-12-09 09:52:16 -0500123typedef struct
124{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 /* Name (a c string). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500126 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
128 /* GRE protocol type in host byte order. */
129 udp_dst_port_t dst_port;
130
131 /* Node which handles this type. */
132 u32 node_index;
133
134 /* Next index for this type. */
135 u32 next_index;
136} udp_dst_port_info_t;
137
Dave Barachd7cb1b52016-12-09 09:52:16 -0500138typedef enum
139{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 UDP_IP6 = 0,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500141 UDP_IP4, /* the code is full of is_ip4... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 N_UDP_AF,
143} udp_af_t;
144
Dave Barachd7cb1b52016-12-09 09:52:16 -0500145typedef struct
146{
147 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
149 /* Hash tables mapping name/protocol to protocol info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500150 uword *dst_port_info_by_name[N_UDP_AF];
151 uword *dst_port_info_by_dst_port[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Damjan Marion615fc612017-04-03 14:56:08 +0200153 /* Sparse vector mapping udp dst_port in network byte order
154 to next index. */
155 u16 *next_by_dst_port4;
156 u16 *next_by_dst_port6;
157 u8 punt_unknown4;
158 u8 punt_unknown6;
159
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 /* convenience */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500161 vlib_main_t *vlib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162} udp_main_t;
163
164always_inline udp_dst_port_info_t *
165udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
166{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500167 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
169}
170
171format_function_t format_udp_header;
172format_function_t format_udp_rx_trace;
173
174unformat_function_t unformat_udp_header;
175
176void udp_register_dst_port (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500177 udp_dst_port_t dst_port,
178 u32 node_index, u8 is_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
Dave Barach68b0fb02017-02-28 15:15:56 -0500180void
181udp_unregister_dst_port (vlib_main_t * vm,
182 udp_dst_port_t dst_port, u8 is_ip4);
183
Dave Barachd7cb1b52016-12-09 09:52:16 -0500184void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800185
Florin Corase127a7e2016-02-18 22:20:01 +0100186always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500187ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
Florin Corase127a7e2016-02-18 22:20:01 +0100188{
Florin Corase127a7e2016-02-18 22:20:01 +0100189 u16 new_l0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500190 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Florin Coras02655bd2016-04-26 00:17:24 +0200192 if (is_ip4)
193 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500194 ip4_header_t *ip0;
Florin Coras02655bd2016-04-26 00:17:24 +0200195 ip_csum_t sum0;
196 u16 old_l0 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100197
Dave Barachd7cb1b52016-12-09 09:52:16 -0500198 ip0 = vlib_buffer_get_current (b0);
Florin Corase127a7e2016-02-18 22:20:01 +0100199
Florin Coras02655bd2016-04-26 00:17:24 +0200200 /* fix the <bleep>ing outer-IP checksum */
201 sum0 = ip0->checksum;
202 /* old_l0 always 0, see the rewrite setup */
203 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
Florin Corase127a7e2016-02-18 22:20:01 +0100204
Dave Barachd7cb1b52016-12-09 09:52:16 -0500205 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
206 length /* changed member */ );
Florin Coras02655bd2016-04-26 00:17:24 +0200207 ip0->checksum = ip_csum_fold (sum0);
208 ip0->length = new_l0;
209
210 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500211 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200212 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500213 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200214 udp0->length = new_l0;
215 }
216 else
217 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500218 ip6_header_t *ip0;
Florin Coras210bfc82016-04-29 16:04:33 +0200219 int bogus0;
220
Dave Barachd7cb1b52016-12-09 09:52:16 -0500221 ip0 = vlib_buffer_get_current (b0);
Florin Coras02655bd2016-04-26 00:17:24 +0200222
Florin Coras02655bd2016-04-26 00:17:24 +0200223 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500224 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200225 ip0->payload_length = new_l0;
226
227 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200229 udp0->length = new_l0;
Florin Coras210bfc82016-04-29 16:04:33 +0200230
Dave Barachd7cb1b52016-12-09 09:52:16 -0500231 udp0->checksum =
232 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
233 ASSERT (bogus0 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200234
235 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236 udp0->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200237 }
Florin Corase127a7e2016-02-18 22:20:01 +0100238}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500239
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240always_inline void
241ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500242 u8 is_ip4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100243{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500244 vlib_buffer_advance (b0, -ec_len);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100245
246 if (is_ip4)
247 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500248 ip4_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100249
Dave Barachd7cb1b52016-12-09 09:52:16 -0500250 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100251
252 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500253 clib_memcpy (ip0, ec0, ec_len);
254 ip_udp_fixup_one (vm, b0, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100255 }
256 else
257 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500258 ip6_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100259
Dave Barachd7cb1b52016-12-09 09:52:16 -0500260 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100261
262 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500263 clib_memcpy (ip0, ec0, ec_len);
264 ip_udp_fixup_one (vm, b0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100265 }
266}
Florin Corase127a7e2016-02-18 22:20:01 +0100267
268always_inline void
Florin Coras02655bd2016-04-26 00:17:24 +0200269ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
Florin Corase127a7e2016-02-18 22:20:01 +0100271{
Florin Corase127a7e2016-02-18 22:20:01 +0100272 u16 new_l0, new_l1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500273 udp_header_t *udp0, *udp1;
Florin Corase127a7e2016-02-18 22:20:01 +0100274
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 ASSERT (_vec_len (ec0) == _vec_len (ec1));
Florin Corase127a7e2016-02-18 22:20:01 +0100276
277 vlib_buffer_advance (b0, -ec_len);
278 vlib_buffer_advance (b1, -ec_len);
279
Florin Coras02655bd2016-04-26 00:17:24 +0200280 if (is_v4)
281 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500282 ip4_header_t *ip0, *ip1;
Florin Coras02655bd2016-04-26 00:17:24 +0200283 ip_csum_t sum0, sum1;
284 u16 old_l0 = 0, old_l1 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100285
Florin Coras02655bd2016-04-26 00:17:24 +0200286 ip0 = vlib_buffer_get_current (b0);
287 ip1 = vlib_buffer_get_current (b1);
Florin Corase127a7e2016-02-18 22:20:01 +0100288
Florin Coras02655bd2016-04-26 00:17:24 +0200289 /* Apply the encap string */
290 clib_memcpy (ip0, ec0, ec_len);
291 clib_memcpy (ip1, ec1, ec_len);
Florin Corase127a7e2016-02-18 22:20:01 +0100292
Florin Coras02655bd2016-04-26 00:17:24 +0200293 /* fix the <bleep>ing outer-IP checksum */
294 sum0 = ip0->checksum;
295 sum1 = ip1->checksum;
Florin Corase127a7e2016-02-18 22:20:01 +0100296
Florin Coras02655bd2016-04-26 00:17:24 +0200297 /* old_l0 always 0, see the rewrite setup */
298 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
299 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
Florin Corase127a7e2016-02-18 22:20:01 +0100300
Dave Barachd7cb1b52016-12-09 09:52:16 -0500301 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
302 length /* changed member */ );
303 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
304 length /* changed member */ );
Florin Corase127a7e2016-02-18 22:20:01 +0100305
Florin Coras02655bd2016-04-26 00:17:24 +0200306 ip0->checksum = ip_csum_fold (sum0);
307 ip1->checksum = ip_csum_fold (sum1);
Florin Corase127a7e2016-02-18 22:20:01 +0100308
Florin Coras02655bd2016-04-26 00:17:24 +0200309 ip0->length = new_l0;
310 ip1->length = new_l1;
Florin Corase127a7e2016-02-18 22:20:01 +0100311
Florin Coras02655bd2016-04-26 00:17:24 +0200312 /* Fix UDP length */
313 udp0 = (udp_header_t *) (ip0 + 1);
314 udp1 = (udp_header_t *) (ip1 + 1);
315
Dave Barachd7cb1b52016-12-09 09:52:16 -0500316 new_l0 =
317 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
318 sizeof (*ip0));
319 new_l1 =
320 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
321 sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200322 udp0->length = new_l0;
323 udp1->length = new_l1;
324 }
325 else
326 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500327 ip6_header_t *ip0, *ip1;
Florin Coras210bfc82016-04-29 16:04:33 +0200328 int bogus0, bogus1;
329
Dave Barachd7cb1b52016-12-09 09:52:16 -0500330 ip0 = vlib_buffer_get_current (b0);
331 ip1 = vlib_buffer_get_current (b1);
Florin Coras02655bd2016-04-26 00:17:24 +0200332
333 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500334 clib_memcpy (ip0, ec0, ec_len);
335 clib_memcpy (ip1, ec1, ec_len);
Florin Coras02655bd2016-04-26 00:17:24 +0200336
337 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500338 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200339 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500340 - sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200341 ip0->payload_length = new_l0;
342 ip1->payload_length = new_l1;
343
344 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500345 udp0 = (udp_header_t *) (ip0 + 1);
346 udp1 = (udp_header_t *) (ip1 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200347
348 udp0->length = new_l0;
349 udp1->length = new_l1;
Florin Coras210bfc82016-04-29 16:04:33 +0200350
Dave Barachd7cb1b52016-12-09 09:52:16 -0500351 udp0->checksum =
352 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
353 udp1->checksum =
354 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
355 ASSERT (bogus0 == 0);
356 ASSERT (bogus1 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200357
358 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359 udp0->checksum = 0xffff;
Florin Coras210bfc82016-04-29 16:04:33 +0200360 if (udp1->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500361 udp1->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200362 }
Florin Corase127a7e2016-02-18 22:20:01 +0100363}
364
Dave Barachd7cb1b52016-12-09 09:52:16 -0500365/*
366 * fd.io coding-style-patch-verification: ON
367 *
368 * Local Variables:
369 * eval: (c-set-style "gnu")
370 * End:
371 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500372
373#endif /* __included_udp_h__ */