blob: 7ab26ce96f0b0404fd32195f9b4221c4c5059341 [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) \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010084_ (3784, bfd4) \
85_ (3785, bfd_echo4) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070086_ (4341, lisp_gpe) \
Florin Corase127a7e2016-02-18 22:20:01 +010087_ (4342, lisp_cp) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070088_ (4739, ipfix) \
89_ (4789, vxlan) \
Chris Luke99cb3352016-04-26 10:49:53 -040090_ (4789, vxlan6) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070091_ (4790, vxlan_gpe) \
92_ (6633, vpath_3)
93
94
95#define foreach_udp6_dst_port \
96_ (547, dhcpv6_to_server) \
97_ (546, dhcpv6_to_client) \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010098_ (3784, bfd6) \
99_ (3785, bfd_echo6) \
Florin Coras02655bd2016-04-26 00:17:24 +0200100_ (4341, lisp_gpe6) \
Florin Corase127a7e2016-02-18 22:20:01 +0100101_ (4342, lisp_cp6) \
Hongjun Nidf921cc2016-05-25 01:16:19 +0800102_ (4790, vxlan6_gpe) \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103_ (6633, vpath6_3)
104
Dave Barachd7cb1b52016-12-09 09:52:16 -0500105typedef enum
106{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107#define _(n,f) UDP_DST_PORT_##f = n,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500108 foreach_udp4_dst_port foreach_udp6_dst_port
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109#undef _
110} udp_dst_port_t;
111
Dave Barachd7cb1b52016-12-09 09:52:16 -0500112typedef enum
113{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114#define _(n,f) UDP6_DST_PORT_##f = n,
115 foreach_udp6_dst_port
116#undef _
117} udp6_dst_port_t;
118
Dave Barachd7cb1b52016-12-09 09:52:16 -0500119typedef struct
120{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 /* Name (a c string). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
124 /* GRE protocol type in host byte order. */
125 udp_dst_port_t dst_port;
126
127 /* Node which handles this type. */
128 u32 node_index;
129
130 /* Next index for this type. */
131 u32 next_index;
132} udp_dst_port_info_t;
133
Dave Barachd7cb1b52016-12-09 09:52:16 -0500134typedef enum
135{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 UDP_IP6 = 0,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500137 UDP_IP4, /* the code is full of is_ip4... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 N_UDP_AF,
139} udp_af_t;
140
Dave Barachd7cb1b52016-12-09 09:52:16 -0500141typedef struct
142{
143 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
145 /* Hash tables mapping name/protocol to protocol info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500146 uword *dst_port_info_by_name[N_UDP_AF];
147 uword *dst_port_info_by_dst_port[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
149 /* convenience */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500150 vlib_main_t *vlib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151} udp_main_t;
152
153always_inline udp_dst_port_info_t *
154udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
155{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500156 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
158}
159
160format_function_t format_udp_header;
161format_function_t format_udp_rx_trace;
162
163unformat_function_t unformat_udp_header;
164
165void udp_register_dst_port (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500166 udp_dst_port_t dst_port,
167 u32 node_index, u8 is_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Dave Barach68b0fb02017-02-28 15:15:56 -0500169void
170udp_unregister_dst_port (vlib_main_t * vm,
171 udp_dst_port_t dst_port, u8 is_ip4);
172
Dave Barachd7cb1b52016-12-09 09:52:16 -0500173void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800174
Florin Corase127a7e2016-02-18 22:20:01 +0100175always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500176ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
Florin Corase127a7e2016-02-18 22:20:01 +0100177{
Florin Corase127a7e2016-02-18 22:20:01 +0100178 u16 new_l0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500179 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
Florin Coras02655bd2016-04-26 00:17:24 +0200181 if (is_ip4)
182 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500183 ip4_header_t *ip0;
Florin Coras02655bd2016-04-26 00:17:24 +0200184 ip_csum_t sum0;
185 u16 old_l0 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100186
Dave Barachd7cb1b52016-12-09 09:52:16 -0500187 ip0 = vlib_buffer_get_current (b0);
Florin Corase127a7e2016-02-18 22:20:01 +0100188
Florin Coras02655bd2016-04-26 00:17:24 +0200189 /* fix the <bleep>ing outer-IP checksum */
190 sum0 = ip0->checksum;
191 /* old_l0 always 0, see the rewrite setup */
192 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
Florin Corase127a7e2016-02-18 22:20:01 +0100193
Dave Barachd7cb1b52016-12-09 09:52:16 -0500194 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
195 length /* changed member */ );
Florin Coras02655bd2016-04-26 00:17:24 +0200196 ip0->checksum = ip_csum_fold (sum0);
197 ip0->length = new_l0;
198
199 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500200 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200201 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500202 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200203 udp0->length = new_l0;
204 }
205 else
206 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500207 ip6_header_t *ip0;
Florin Coras210bfc82016-04-29 16:04:33 +0200208 int bogus0;
209
Dave Barachd7cb1b52016-12-09 09:52:16 -0500210 ip0 = vlib_buffer_get_current (b0);
Florin Coras02655bd2016-04-26 00:17:24 +0200211
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 ip0->payload_length = new_l0;
215
216 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500217 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200218 udp0->length = new_l0;
Florin Coras210bfc82016-04-29 16:04:33 +0200219
Dave Barachd7cb1b52016-12-09 09:52:16 -0500220 udp0->checksum =
221 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
222 ASSERT (bogus0 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200223
224 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500225 udp0->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200226 }
Florin Corase127a7e2016-02-18 22:20:01 +0100227}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100229always_inline void
230ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500231 u8 is_ip4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100232{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233 vlib_buffer_advance (b0, -ec_len);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100234
235 if (is_ip4)
236 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500237 ip4_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100238
Dave Barachd7cb1b52016-12-09 09:52:16 -0500239 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240
241 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500242 clib_memcpy (ip0, ec0, ec_len);
243 ip_udp_fixup_one (vm, b0, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244 }
245 else
246 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500247 ip6_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100248
Dave Barachd7cb1b52016-12-09 09:52:16 -0500249 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100250
251 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500252 clib_memcpy (ip0, ec0, ec_len);
253 ip_udp_fixup_one (vm, b0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100254 }
255}
Florin Corase127a7e2016-02-18 22:20:01 +0100256
257always_inline void
Florin Coras02655bd2016-04-26 00:17:24 +0200258ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500259 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
Florin Corase127a7e2016-02-18 22:20:01 +0100260{
Florin Corase127a7e2016-02-18 22:20:01 +0100261 u16 new_l0, new_l1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500262 udp_header_t *udp0, *udp1;
Florin Corase127a7e2016-02-18 22:20:01 +0100263
Dave Barachd7cb1b52016-12-09 09:52:16 -0500264 ASSERT (_vec_len (ec0) == _vec_len (ec1));
Florin Corase127a7e2016-02-18 22:20:01 +0100265
266 vlib_buffer_advance (b0, -ec_len);
267 vlib_buffer_advance (b1, -ec_len);
268
Florin Coras02655bd2016-04-26 00:17:24 +0200269 if (is_v4)
270 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500271 ip4_header_t *ip0, *ip1;
Florin Coras02655bd2016-04-26 00:17:24 +0200272 ip_csum_t sum0, sum1;
273 u16 old_l0 = 0, old_l1 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100274
Florin Coras02655bd2016-04-26 00:17:24 +0200275 ip0 = vlib_buffer_get_current (b0);
276 ip1 = vlib_buffer_get_current (b1);
Florin Corase127a7e2016-02-18 22:20:01 +0100277
Florin Coras02655bd2016-04-26 00:17:24 +0200278 /* Apply the encap string */
279 clib_memcpy (ip0, ec0, ec_len);
280 clib_memcpy (ip1, ec1, ec_len);
Florin Corase127a7e2016-02-18 22:20:01 +0100281
Florin Coras02655bd2016-04-26 00:17:24 +0200282 /* fix the <bleep>ing outer-IP checksum */
283 sum0 = ip0->checksum;
284 sum1 = ip1->checksum;
Florin Corase127a7e2016-02-18 22:20:01 +0100285
Florin Coras02655bd2016-04-26 00:17:24 +0200286 /* old_l0 always 0, see the rewrite setup */
287 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
288 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
Florin Corase127a7e2016-02-18 22:20:01 +0100289
Dave Barachd7cb1b52016-12-09 09:52:16 -0500290 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
291 length /* changed member */ );
292 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
293 length /* changed member */ );
Florin Corase127a7e2016-02-18 22:20:01 +0100294
Florin Coras02655bd2016-04-26 00:17:24 +0200295 ip0->checksum = ip_csum_fold (sum0);
296 ip1->checksum = ip_csum_fold (sum1);
Florin Corase127a7e2016-02-18 22:20:01 +0100297
Florin Coras02655bd2016-04-26 00:17:24 +0200298 ip0->length = new_l0;
299 ip1->length = new_l1;
Florin Corase127a7e2016-02-18 22:20:01 +0100300
Florin Coras02655bd2016-04-26 00:17:24 +0200301 /* Fix UDP length */
302 udp0 = (udp_header_t *) (ip0 + 1);
303 udp1 = (udp_header_t *) (ip1 + 1);
304
Dave Barachd7cb1b52016-12-09 09:52:16 -0500305 new_l0 =
306 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
307 sizeof (*ip0));
308 new_l1 =
309 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
310 sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200311 udp0->length = new_l0;
312 udp1->length = new_l1;
313 }
314 else
315 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500316 ip6_header_t *ip0, *ip1;
Florin Coras210bfc82016-04-29 16:04:33 +0200317 int bogus0, bogus1;
318
Dave Barachd7cb1b52016-12-09 09:52:16 -0500319 ip0 = vlib_buffer_get_current (b0);
320 ip1 = vlib_buffer_get_current (b1);
Florin Coras02655bd2016-04-26 00:17:24 +0200321
322 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 clib_memcpy (ip0, ec0, ec_len);
324 clib_memcpy (ip1, ec1, ec_len);
Florin Coras02655bd2016-04-26 00:17:24 +0200325
326 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500327 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200328 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500329 - sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200330 ip0->payload_length = new_l0;
331 ip1->payload_length = new_l1;
332
333 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500334 udp0 = (udp_header_t *) (ip0 + 1);
335 udp1 = (udp_header_t *) (ip1 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200336
337 udp0->length = new_l0;
338 udp1->length = new_l1;
Florin Coras210bfc82016-04-29 16:04:33 +0200339
Dave Barachd7cb1b52016-12-09 09:52:16 -0500340 udp0->checksum =
341 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
342 udp1->checksum =
343 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
344 ASSERT (bogus0 == 0);
345 ASSERT (bogus1 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200346
347 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500348 udp0->checksum = 0xffff;
Florin Coras210bfc82016-04-29 16:04:33 +0200349 if (udp1->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350 udp1->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200351 }
Florin Corase127a7e2016-02-18 22:20:01 +0100352}
353
Dave Barachd7cb1b52016-12-09 09:52:16 -0500354/*
355 * fd.io coding-style-patch-verification: ON
356 *
357 * Local Variables:
358 * eval: (c-set-style "gnu")
359 * End:
360 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500361
362#endif /* __included_udp_h__ */