blob: a9a62c27bb377ac846b0d939ec31a09a43205a01 [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) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070093_ (6633, vpath_3)
94
95
96#define foreach_udp6_dst_port \
97_ (547, dhcpv6_to_server) \
98_ (546, dhcpv6_to_client) \
Hongjun Nief486b12017-04-12 19:21:16 +080099_ (2152, GTPU6) \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100100_ (3784, bfd6) \
101_ (3785, bfd_echo6) \
Florin Coras02655bd2016-04-26 00:17:24 +0200102_ (4341, lisp_gpe6) \
Florin Corase127a7e2016-02-18 22:20:01 +0100103_ (4342, lisp_cp6) \
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +0800104_ (4790, VXLAN6_GPE) \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105_ (6633, vpath6_3)
106
Dave Barachd7cb1b52016-12-09 09:52:16 -0500107typedef enum
108{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109#define _(n,f) UDP_DST_PORT_##f = n,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500110 foreach_udp4_dst_port foreach_udp6_dst_port
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111#undef _
112} udp_dst_port_t;
113
Dave Barachd7cb1b52016-12-09 09:52:16 -0500114typedef enum
115{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116#define _(n,f) UDP6_DST_PORT_##f = n,
117 foreach_udp6_dst_port
118#undef _
119} udp6_dst_port_t;
120
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121typedef struct
122{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123 /* Name (a c string). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500124 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
126 /* GRE protocol type in host byte order. */
127 udp_dst_port_t dst_port;
128
129 /* Node which handles this type. */
130 u32 node_index;
131
132 /* Next index for this type. */
133 u32 next_index;
134} udp_dst_port_info_t;
135
Dave Barachd7cb1b52016-12-09 09:52:16 -0500136typedef enum
137{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 UDP_IP6 = 0,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500139 UDP_IP4, /* the code is full of is_ip4... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 N_UDP_AF,
141} udp_af_t;
142
Dave Barachd7cb1b52016-12-09 09:52:16 -0500143typedef struct
144{
145 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147 /* Hash tables mapping name/protocol to protocol info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500148 uword *dst_port_info_by_name[N_UDP_AF];
149 uword *dst_port_info_by_dst_port[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Damjan Marion615fc612017-04-03 14:56:08 +0200151 /* Sparse vector mapping udp dst_port in network byte order
152 to next index. */
153 u16 *next_by_dst_port4;
154 u16 *next_by_dst_port6;
155 u8 punt_unknown4;
156 u8 punt_unknown6;
157
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 /* convenience */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159 vlib_main_t *vlib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160} udp_main_t;
161
162always_inline udp_dst_port_info_t *
163udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
164{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500165 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
167}
168
169format_function_t format_udp_header;
170format_function_t format_udp_rx_trace;
171
172unformat_function_t unformat_udp_header;
173
174void udp_register_dst_port (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500175 udp_dst_port_t dst_port,
176 u32 node_index, u8 is_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Dave Barach68b0fb02017-02-28 15:15:56 -0500178void
179udp_unregister_dst_port (vlib_main_t * vm,
180 udp_dst_port_t dst_port, u8 is_ip4);
181
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800183
Florin Corase127a7e2016-02-18 22:20:01 +0100184always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500185ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
Florin Corase127a7e2016-02-18 22:20:01 +0100186{
Florin Corase127a7e2016-02-18 22:20:01 +0100187 u16 new_l0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500188 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
Florin Coras02655bd2016-04-26 00:17:24 +0200190 if (is_ip4)
191 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500192 ip4_header_t *ip0;
Florin Coras02655bd2016-04-26 00:17:24 +0200193 ip_csum_t sum0;
194 u16 old_l0 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100195
Dave Barachd7cb1b52016-12-09 09:52:16 -0500196 ip0 = vlib_buffer_get_current (b0);
Florin Corase127a7e2016-02-18 22:20:01 +0100197
Florin Coras02655bd2016-04-26 00:17:24 +0200198 /* fix the <bleep>ing outer-IP checksum */
199 sum0 = ip0->checksum;
200 /* old_l0 always 0, see the rewrite setup */
201 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
Florin Corase127a7e2016-02-18 22:20:01 +0100202
Dave Barachd7cb1b52016-12-09 09:52:16 -0500203 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
204 length /* changed member */ );
Florin Coras02655bd2016-04-26 00:17:24 +0200205 ip0->checksum = ip_csum_fold (sum0);
206 ip0->length = new_l0;
207
208 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500209 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200210 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500211 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200212 udp0->length = new_l0;
213 }
214 else
215 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500216 ip6_header_t *ip0;
Florin Coras210bfc82016-04-29 16:04:33 +0200217 int bogus0;
218
Dave Barachd7cb1b52016-12-09 09:52:16 -0500219 ip0 = vlib_buffer_get_current (b0);
Florin Coras02655bd2016-04-26 00:17:24 +0200220
Florin Coras02655bd2016-04-26 00:17:24 +0200221 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200223 ip0->payload_length = new_l0;
224
225 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500226 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200227 udp0->length = new_l0;
Florin Coras210bfc82016-04-29 16:04:33 +0200228
Dave Barachd7cb1b52016-12-09 09:52:16 -0500229 udp0->checksum =
230 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
231 ASSERT (bogus0 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200232
233 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500234 udp0->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200235 }
Florin Corase127a7e2016-02-18 22:20:01 +0100236}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500237
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100238always_inline void
239ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500240 u8 is_ip4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100241{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500242 vlib_buffer_advance (b0, -ec_len);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100243
244 if (is_ip4)
245 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500246 ip4_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100247
Dave Barachd7cb1b52016-12-09 09:52:16 -0500248 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100249
250 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500251 clib_memcpy (ip0, ec0, ec_len);
252 ip_udp_fixup_one (vm, b0, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100253 }
254 else
255 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500256 ip6_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100257
Dave Barachd7cb1b52016-12-09 09:52:16 -0500258 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100259
260 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500261 clib_memcpy (ip0, ec0, ec_len);
262 ip_udp_fixup_one (vm, b0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100263 }
264}
Florin Corase127a7e2016-02-18 22:20:01 +0100265
266always_inline void
Florin Coras02655bd2016-04-26 00:17:24 +0200267ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500268 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
Florin Corase127a7e2016-02-18 22:20:01 +0100269{
Florin Corase127a7e2016-02-18 22:20:01 +0100270 u16 new_l0, new_l1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500271 udp_header_t *udp0, *udp1;
Florin Corase127a7e2016-02-18 22:20:01 +0100272
Dave Barachd7cb1b52016-12-09 09:52:16 -0500273 ASSERT (_vec_len (ec0) == _vec_len (ec1));
Florin Corase127a7e2016-02-18 22:20:01 +0100274
275 vlib_buffer_advance (b0, -ec_len);
276 vlib_buffer_advance (b1, -ec_len);
277
Florin Coras02655bd2016-04-26 00:17:24 +0200278 if (is_v4)
279 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500280 ip4_header_t *ip0, *ip1;
Florin Coras02655bd2016-04-26 00:17:24 +0200281 ip_csum_t sum0, sum1;
282 u16 old_l0 = 0, old_l1 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100283
Florin Coras02655bd2016-04-26 00:17:24 +0200284 ip0 = vlib_buffer_get_current (b0);
285 ip1 = vlib_buffer_get_current (b1);
Florin Corase127a7e2016-02-18 22:20:01 +0100286
Florin Coras02655bd2016-04-26 00:17:24 +0200287 /* Apply the encap string */
288 clib_memcpy (ip0, ec0, ec_len);
289 clib_memcpy (ip1, ec1, ec_len);
Florin Corase127a7e2016-02-18 22:20:01 +0100290
Florin Coras02655bd2016-04-26 00:17:24 +0200291 /* fix the <bleep>ing outer-IP checksum */
292 sum0 = ip0->checksum;
293 sum1 = ip1->checksum;
Florin Corase127a7e2016-02-18 22:20:01 +0100294
Florin Coras02655bd2016-04-26 00:17:24 +0200295 /* old_l0 always 0, see the rewrite setup */
296 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
297 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
Florin Corase127a7e2016-02-18 22:20:01 +0100298
Dave Barachd7cb1b52016-12-09 09:52:16 -0500299 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
300 length /* changed member */ );
301 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
302 length /* changed member */ );
Florin Corase127a7e2016-02-18 22:20:01 +0100303
Florin Coras02655bd2016-04-26 00:17:24 +0200304 ip0->checksum = ip_csum_fold (sum0);
305 ip1->checksum = ip_csum_fold (sum1);
Florin Corase127a7e2016-02-18 22:20:01 +0100306
Florin Coras02655bd2016-04-26 00:17:24 +0200307 ip0->length = new_l0;
308 ip1->length = new_l1;
Florin Corase127a7e2016-02-18 22:20:01 +0100309
Florin Coras02655bd2016-04-26 00:17:24 +0200310 /* Fix UDP length */
311 udp0 = (udp_header_t *) (ip0 + 1);
312 udp1 = (udp_header_t *) (ip1 + 1);
313
Dave Barachd7cb1b52016-12-09 09:52:16 -0500314 new_l0 =
315 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
316 sizeof (*ip0));
317 new_l1 =
318 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
319 sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200320 udp0->length = new_l0;
321 udp1->length = new_l1;
322 }
323 else
324 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500325 ip6_header_t *ip0, *ip1;
Florin Coras210bfc82016-04-29 16:04:33 +0200326 int bogus0, bogus1;
327
Dave Barachd7cb1b52016-12-09 09:52:16 -0500328 ip0 = vlib_buffer_get_current (b0);
329 ip1 = vlib_buffer_get_current (b1);
Florin Coras02655bd2016-04-26 00:17:24 +0200330
331 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332 clib_memcpy (ip0, ec0, ec_len);
333 clib_memcpy (ip1, ec1, ec_len);
Florin Coras02655bd2016-04-26 00:17:24 +0200334
335 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500336 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200337 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500338 - sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200339 ip0->payload_length = new_l0;
340 ip1->payload_length = new_l1;
341
342 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500343 udp0 = (udp_header_t *) (ip0 + 1);
344 udp1 = (udp_header_t *) (ip1 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200345
346 udp0->length = new_l0;
347 udp1->length = new_l1;
Florin Coras210bfc82016-04-29 16:04:33 +0200348
Dave Barachd7cb1b52016-12-09 09:52:16 -0500349 udp0->checksum =
350 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
351 udp1->checksum =
352 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
353 ASSERT (bogus0 == 0);
354 ASSERT (bogus1 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200355
356 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500357 udp0->checksum = 0xffff;
Florin Coras210bfc82016-04-29 16:04:33 +0200358 if (udp1->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359 udp1->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200360 }
Florin Corase127a7e2016-02-18 22:20:01 +0100361}
362
Dave Barachd7cb1b52016-12-09 09:52:16 -0500363/*
364 * fd.io coding-style-patch-verification: ON
365 *
366 * Local Variables:
367 * eval: (c-set-style "gnu")
368 * End:
369 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500370
371#endif /* __included_udp_h__ */