blob: 03c62e0b6846cce7feb5c98d16759691925610d4 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * ip/udp.h: udp protocol
3 *
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef included_udp_h
19#define included_udp_h
20
21#include <vnet/vnet.h>
22#include <vnet/ip/udp_packet.h>
23#include <vnet/ip/ip.h>
24#include <vnet/ip/ip4.h>
25#include <vnet/ip/ip4_packet.h>
26#include <vnet/pg/pg.h>
27#include <vnet/ip/format.h>
28
Dave Barachd7cb1b52016-12-09 09:52:16 -050029typedef enum
30{
Ed Warnickecb9cada2015-12-08 15:45:58 -070031#define udp_error(n,s) UDP_ERROR_##n,
32#include <vnet/ip/udp_error.def>
33#undef udp_error
34 UDP_N_ERROR,
35} udp_error_t;
36
37#define foreach_udp4_dst_port \
38_ (67, dhcp_to_server) \
39_ (68, dhcp_to_client) \
40_ (500, ikev2) \
Klement Sekera0e3c0de2016-09-29 14:43:44 +020041_ (3784, bfd4) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070042_ (4341, lisp_gpe) \
Florin Corase127a7e2016-02-18 22:20:01 +010043_ (4342, lisp_cp) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070044_ (4739, ipfix) \
45_ (4789, vxlan) \
Chris Luke99cb3352016-04-26 10:49:53 -040046_ (4789, vxlan6) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070047_ (4790, vxlan_gpe) \
48_ (6633, vpath_3)
49
50
51#define foreach_udp6_dst_port \
52_ (547, dhcpv6_to_server) \
53_ (546, dhcpv6_to_client) \
Klement Sekera0e3c0de2016-09-29 14:43:44 +020054_ (3784, bfd6) \
Florin Coras02655bd2016-04-26 00:17:24 +020055_ (4341, lisp_gpe6) \
Florin Corase127a7e2016-02-18 22:20:01 +010056_ (4342, lisp_cp6) \
Hongjun Nidf921cc2016-05-25 01:16:19 +080057_ (4790, vxlan6_gpe) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070058_ (6633, vpath6_3)
59
Dave Barachd7cb1b52016-12-09 09:52:16 -050060typedef enum
61{
Ed Warnickecb9cada2015-12-08 15:45:58 -070062#define _(n,f) UDP_DST_PORT_##f = n,
Dave Barachd7cb1b52016-12-09 09:52:16 -050063 foreach_udp4_dst_port foreach_udp6_dst_port
Ed Warnickecb9cada2015-12-08 15:45:58 -070064#undef _
65} udp_dst_port_t;
66
Dave Barachd7cb1b52016-12-09 09:52:16 -050067typedef enum
68{
Ed Warnickecb9cada2015-12-08 15:45:58 -070069#define _(n,f) UDP6_DST_PORT_##f = n,
70 foreach_udp6_dst_port
71#undef _
72} udp6_dst_port_t;
73
Dave Barachd7cb1b52016-12-09 09:52:16 -050074typedef struct
75{
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 /* Name (a c string). */
Dave Barachd7cb1b52016-12-09 09:52:16 -050077 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79 /* GRE protocol type in host byte order. */
80 udp_dst_port_t dst_port;
81
82 /* Node which handles this type. */
83 u32 node_index;
84
85 /* Next index for this type. */
86 u32 next_index;
87} udp_dst_port_info_t;
88
Dave Barachd7cb1b52016-12-09 09:52:16 -050089typedef enum
90{
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 UDP_IP6 = 0,
Dave Barachd7cb1b52016-12-09 09:52:16 -050092 UDP_IP4, /* the code is full of is_ip4... */
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 N_UDP_AF,
94} udp_af_t;
95
Dave Barachd7cb1b52016-12-09 09:52:16 -050096typedef struct
97{
98 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100 /* Hash tables mapping name/protocol to protocol info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500101 uword *dst_port_info_by_name[N_UDP_AF];
102 uword *dst_port_info_by_dst_port[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
104 /* convenience */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500105 vlib_main_t *vlib_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106} udp_main_t;
107
108always_inline udp_dst_port_info_t *
109udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
110{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500111 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
113}
114
115format_function_t format_udp_header;
116format_function_t format_udp_rx_trace;
117
118unformat_function_t unformat_udp_header;
119
120void udp_register_dst_port (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121 udp_dst_port_t dst_port,
122 u32 node_index, u8 is_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
Dave Barachd7cb1b52016-12-09 09:52:16 -0500124void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800125
Florin Corase127a7e2016-02-18 22:20:01 +0100126always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500127ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
Florin Corase127a7e2016-02-18 22:20:01 +0100128{
Florin Corase127a7e2016-02-18 22:20:01 +0100129 u16 new_l0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500130 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Florin Coras02655bd2016-04-26 00:17:24 +0200132 if (is_ip4)
133 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500134 ip4_header_t *ip0;
Florin Coras02655bd2016-04-26 00:17:24 +0200135 ip_csum_t sum0;
136 u16 old_l0 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100137
Dave Barachd7cb1b52016-12-09 09:52:16 -0500138 ip0 = vlib_buffer_get_current (b0);
Florin Corase127a7e2016-02-18 22:20:01 +0100139
Florin Coras02655bd2016-04-26 00:17:24 +0200140 /* fix the <bleep>ing outer-IP checksum */
141 sum0 = ip0->checksum;
142 /* old_l0 always 0, see the rewrite setup */
143 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
Florin Corase127a7e2016-02-18 22:20:01 +0100144
Dave Barachd7cb1b52016-12-09 09:52:16 -0500145 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
146 length /* changed member */ );
Florin Coras02655bd2016-04-26 00:17:24 +0200147 ip0->checksum = ip_csum_fold (sum0);
148 ip0->length = new_l0;
149
150 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500151 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200152 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500153 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200154 udp0->length = new_l0;
155 }
156 else
157 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500158 ip6_header_t *ip0;
Florin Coras210bfc82016-04-29 16:04:33 +0200159 int bogus0;
160
Dave Barachd7cb1b52016-12-09 09:52:16 -0500161 ip0 = vlib_buffer_get_current (b0);
Florin Coras02655bd2016-04-26 00:17:24 +0200162
Florin Coras02655bd2016-04-26 00:17:24 +0200163 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500164 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200165 ip0->payload_length = new_l0;
166
167 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500168 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200169 udp0->length = new_l0;
Florin Coras210bfc82016-04-29 16:04:33 +0200170
Dave Barachd7cb1b52016-12-09 09:52:16 -0500171 udp0->checksum =
172 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
173 ASSERT (bogus0 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200174
175 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500176 udp0->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200177 }
Florin Corase127a7e2016-02-18 22:20:01 +0100178}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500179
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100180always_inline void
181ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182 u8 is_ip4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100183{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500184 vlib_buffer_advance (b0, -ec_len);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100185
186 if (is_ip4)
187 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500188 ip4_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100189
Dave Barachd7cb1b52016-12-09 09:52:16 -0500190 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191
192 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500193 clib_memcpy (ip0, ec0, ec_len);
194 ip_udp_fixup_one (vm, b0, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100195 }
196 else
197 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500198 ip6_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100199
Dave Barachd7cb1b52016-12-09 09:52:16 -0500200 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100201
202 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500203 clib_memcpy (ip0, ec0, ec_len);
204 ip_udp_fixup_one (vm, b0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100205 }
206}
Florin Corase127a7e2016-02-18 22:20:01 +0100207
208always_inline void
Florin Coras02655bd2016-04-26 00:17:24 +0200209ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500210 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
Florin Corase127a7e2016-02-18 22:20:01 +0100211{
Florin Corase127a7e2016-02-18 22:20:01 +0100212 u16 new_l0, new_l1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500213 udp_header_t *udp0, *udp1;
Florin Corase127a7e2016-02-18 22:20:01 +0100214
Dave Barachd7cb1b52016-12-09 09:52:16 -0500215 ASSERT (_vec_len (ec0) == _vec_len (ec1));
Florin Corase127a7e2016-02-18 22:20:01 +0100216
217 vlib_buffer_advance (b0, -ec_len);
218 vlib_buffer_advance (b1, -ec_len);
219
Florin Coras02655bd2016-04-26 00:17:24 +0200220 if (is_v4)
221 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222 ip4_header_t *ip0, *ip1;
Florin Coras02655bd2016-04-26 00:17:24 +0200223 ip_csum_t sum0, sum1;
224 u16 old_l0 = 0, old_l1 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100225
Florin Coras02655bd2016-04-26 00:17:24 +0200226 ip0 = vlib_buffer_get_current (b0);
227 ip1 = vlib_buffer_get_current (b1);
Florin Corase127a7e2016-02-18 22:20:01 +0100228
Florin Coras02655bd2016-04-26 00:17:24 +0200229 /* Apply the encap string */
230 clib_memcpy (ip0, ec0, ec_len);
231 clib_memcpy (ip1, ec1, ec_len);
Florin Corase127a7e2016-02-18 22:20:01 +0100232
Florin Coras02655bd2016-04-26 00:17:24 +0200233 /* fix the <bleep>ing outer-IP checksum */
234 sum0 = ip0->checksum;
235 sum1 = ip1->checksum;
Florin Corase127a7e2016-02-18 22:20:01 +0100236
Florin Coras02655bd2016-04-26 00:17:24 +0200237 /* old_l0 always 0, see the rewrite setup */
238 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
239 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
Florin Corase127a7e2016-02-18 22:20:01 +0100240
Dave Barachd7cb1b52016-12-09 09:52:16 -0500241 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
242 length /* changed member */ );
243 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
244 length /* changed member */ );
Florin Corase127a7e2016-02-18 22:20:01 +0100245
Florin Coras02655bd2016-04-26 00:17:24 +0200246 ip0->checksum = ip_csum_fold (sum0);
247 ip1->checksum = ip_csum_fold (sum1);
Florin Corase127a7e2016-02-18 22:20:01 +0100248
Florin Coras02655bd2016-04-26 00:17:24 +0200249 ip0->length = new_l0;
250 ip1->length = new_l1;
Florin Corase127a7e2016-02-18 22:20:01 +0100251
Florin Coras02655bd2016-04-26 00:17:24 +0200252 /* Fix UDP length */
253 udp0 = (udp_header_t *) (ip0 + 1);
254 udp1 = (udp_header_t *) (ip1 + 1);
255
Dave Barachd7cb1b52016-12-09 09:52:16 -0500256 new_l0 =
257 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
258 sizeof (*ip0));
259 new_l1 =
260 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
261 sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200262 udp0->length = new_l0;
263 udp1->length = new_l1;
264 }
265 else
266 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500267 ip6_header_t *ip0, *ip1;
Florin Coras210bfc82016-04-29 16:04:33 +0200268 int bogus0, bogus1;
269
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 ip0 = vlib_buffer_get_current (b0);
271 ip1 = vlib_buffer_get_current (b1);
Florin Coras02655bd2016-04-26 00:17:24 +0200272
273 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500274 clib_memcpy (ip0, ec0, ec_len);
275 clib_memcpy (ip1, ec1, ec_len);
Florin Coras02655bd2016-04-26 00:17:24 +0200276
277 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500278 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200279 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500280 - sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200281 ip0->payload_length = new_l0;
282 ip1->payload_length = new_l1;
283
284 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500285 udp0 = (udp_header_t *) (ip0 + 1);
286 udp1 = (udp_header_t *) (ip1 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200287
288 udp0->length = new_l0;
289 udp1->length = new_l1;
Florin Coras210bfc82016-04-29 16:04:33 +0200290
Dave Barachd7cb1b52016-12-09 09:52:16 -0500291 udp0->checksum =
292 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
293 udp1->checksum =
294 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
295 ASSERT (bogus0 == 0);
296 ASSERT (bogus1 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200297
298 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500299 udp0->checksum = 0xffff;
Florin Coras210bfc82016-04-29 16:04:33 +0200300 if (udp1->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500301 udp1->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200302 }
Florin Corase127a7e2016-02-18 22:20:01 +0100303}
304
305#endif /* included_udp_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500306
307/*
308 * fd.io coding-style-patch-verification: ON
309 *
310 * Local Variables:
311 * eval: (c-set-style "gnu")
312 * End:
313 */