blob: 920ef963ace3e6db3c28dc96eb9adbde7c60bc39 [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
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,
Dave Barach68b0fb02017-02-28 15:15:56 -050032#include <vnet/udp/udp_error.def>
Ed Warnickecb9cada2015-12-08 15:45:58 -070033#undef udp_error
34 UDP_N_ERROR,
35} udp_error_t;
36
Florin Coras3cbc04b2017-10-02 00:18:51 -070037typedef struct
38{
39 transport_connection_t connection; /** must be first */
40 /** ersatz MTU to limit fifo pushes to test data size */
41 u32 mtu;
42} udp_connection_t;
43
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#define foreach_udp4_dst_port \
Dave Barach65457162017-10-10 17:53:14 -040045_ (53, dns) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070046_ (67, dhcp_to_server) \
47_ (68, dhcp_to_client) \
48_ (500, ikev2) \
Hongjun Nief486b12017-04-12 19:21:16 +080049_ (2152, GTPU) \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010050_ (3784, bfd4) \
51_ (3785, bfd_echo4) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070052_ (4341, lisp_gpe) \
Florin Corase127a7e2016-02-18 22:20:01 +010053_ (4342, lisp_cp) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070054_ (4739, ipfix) \
55_ (4789, vxlan) \
Chris Luke99cb3352016-04-26 10:49:53 -040056_ (4789, vxlan6) \
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080057_ (4790, VXLAN_GPE) \
Marco Varleseb598f1d2017-09-19 14:25:28 +020058_ (6633, vpath_3) \
Dave Barach65457162017-10-10 17:53:14 -040059_ (6081, geneve) \
60_ (53053, dns_reply)
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
62
63#define foreach_udp6_dst_port \
Dave Barach65457162017-10-10 17:53:14 -040064_ (53, dns6) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070065_ (547, dhcpv6_to_server) \
66_ (546, dhcpv6_to_client) \
Hongjun Nief486b12017-04-12 19:21:16 +080067_ (2152, GTPU6) \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010068_ (3784, bfd6) \
69_ (3785, bfd_echo6) \
Florin Coras02655bd2016-04-26 00:17:24 +020070_ (4341, lisp_gpe6) \
Florin Corase127a7e2016-02-18 22:20:01 +010071_ (4342, lisp_cp6) \
Hongjun Ni8a0a0ae2017-05-27 20:23:09 +080072_ (4790, VXLAN6_GPE) \
Marco Varleseb598f1d2017-09-19 14:25:28 +020073_ (6633, vpath6_3) \
Dave Barach65457162017-10-10 17:53:14 -040074_ (6081, geneve6) \
75_ (53053, dns_reply6)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Dave Barachd7cb1b52016-12-09 09:52:16 -050077typedef enum
78{
Ed Warnickecb9cada2015-12-08 15:45:58 -070079#define _(n,f) UDP_DST_PORT_##f = n,
Dave Barachd7cb1b52016-12-09 09:52:16 -050080 foreach_udp4_dst_port foreach_udp6_dst_port
Ed Warnickecb9cada2015-12-08 15:45:58 -070081#undef _
82} udp_dst_port_t;
83
Dave Barachd7cb1b52016-12-09 09:52:16 -050084typedef enum
85{
Ed Warnickecb9cada2015-12-08 15:45:58 -070086#define _(n,f) UDP6_DST_PORT_##f = n,
87 foreach_udp6_dst_port
88#undef _
89} udp6_dst_port_t;
90
Dave Barachd7cb1b52016-12-09 09:52:16 -050091typedef struct
92{
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 /* Name (a c string). */
Dave Barachd7cb1b52016-12-09 09:52:16 -050094 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 /* GRE protocol type in host byte order. */
97 udp_dst_port_t dst_port;
98
99 /* Node which handles this type. */
100 u32 node_index;
101
102 /* Next index for this type. */
103 u32 next_index;
104} udp_dst_port_info_t;
105
Dave Barachd7cb1b52016-12-09 09:52:16 -0500106typedef enum
107{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 UDP_IP6 = 0,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500109 UDP_IP4, /* the code is full of is_ip4... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 N_UDP_AF,
111} udp_af_t;
112
Dave Barachd7cb1b52016-12-09 09:52:16 -0500113typedef struct
114{
115 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
117 /* Hash tables mapping name/protocol to protocol info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500118 uword *dst_port_info_by_name[N_UDP_AF];
119 uword *dst_port_info_by_dst_port[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
Damjan Marion615fc612017-04-03 14:56:08 +0200121 /* Sparse vector mapping udp dst_port in network byte order
122 to next index. */
123 u16 *next_by_dst_port4;
124 u16 *next_by_dst_port6;
125 u8 punt_unknown4;
126 u8 punt_unknown6;
127
Florin Coras3cbc04b2017-10-02 00:18:51 -0700128 /*
129 * Per-worker thread udp connection pools used with session layer
130 */
131 udp_connection_t **connections;
132 u32 *connection_peekers;
133 clib_spinlock_t *peekers_readers_locks;
134 clib_spinlock_t *peekers_write_locks;
135 udp_connection_t *listener_pool;
136
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137} udp_main_t;
138
Florin Coras3cbc04b2017-10-02 00:18:51 -0700139extern udp_main_t udp_main;
140extern vlib_node_registration_t udp4_input_node;
141extern vlib_node_registration_t udp6_input_node;
142
143always_inline udp_connection_t *
144udp_connection_get (u32 conn_index, u32 thread_index)
145{
146 if (pool_is_free_index (udp_main.connections[thread_index], conn_index))
147 return 0;
148 return pool_elt_at_index (udp_main.connections[thread_index], conn_index);
149}
150
151always_inline udp_connection_t *
152udp_listener_get (u32 conn_index)
153{
154 return pool_elt_at_index (udp_main.listener_pool, conn_index);
155}
156
157always_inline udp_main_t *
158vnet_get_udp_main ()
159{
160 return &udp_main;
161}
162
163always_inline udp_connection_t *
164udp_get_connection_from_transport (transport_connection_t * tc)
165{
166 return ((udp_connection_t *) tc);
167}
168
169always_inline u32
170udp_connection_index (udp_connection_t * uc)
171{
172 return (uc - udp_main.connections[uc->c_thread_index]);
173}
174
175udp_connection_t *udp_connection_alloc (u32 thread_index);
176
177/**
178 * Acquires a lock that blocks a connection pool from expanding.
179 */
180always_inline void
181udp_pool_add_peeker (u32 thread_index)
182{
183 if (thread_index != vlib_get_thread_index ())
184 return;
185 clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
186 udp_main.connection_peekers[thread_index] += 1;
187 if (udp_main.connection_peekers[thread_index] == 1)
188 clib_spinlock_lock_if_init (&udp_main.peekers_write_locks[thread_index]);
189 clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
190 [thread_index]);
191}
192
193always_inline void
194udp_pool_remove_peeker (u32 thread_index)
195{
196 if (thread_index != vlib_get_thread_index ())
197 return;
198 ASSERT (udp_main.connection_peekers[thread_index] > 0);
199 clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
200 udp_main.connection_peekers[thread_index] -= 1;
201 if (udp_main.connection_peekers[thread_index] == 0)
202 clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
203 [thread_index]);
204 clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
205 [thread_index]);
206}
207
208always_inline udp_connection_t *
209udp_conenction_clone_safe (u32 connection_index, u32 thread_index)
210{
211 udp_connection_t *old_c, *new_c;
212 u32 current_thread_index = vlib_get_thread_index ();
213 new_c = udp_connection_alloc (current_thread_index);
214
215 /* If during the memcpy pool is reallocated AND the memory allocator
216 * decides to give the old chunk of memory to somebody in a hurry to
217 * scribble something on it, we have a problem. So add this thread as
218 * a session pool peeker.
219 */
220 udp_pool_add_peeker (thread_index);
221 old_c = udp_main.connections[thread_index] + connection_index;
222 clib_memcpy (new_c, old_c, sizeof (*new_c));
223 udp_pool_remove_peeker (thread_index);
224 new_c->c_thread_index = current_thread_index;
225 new_c->c_c_index = udp_connection_index (new_c);
226 return new_c;
227}
228
229
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230always_inline udp_dst_port_info_t *
231udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
232{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
235}
236
237format_function_t format_udp_header;
238format_function_t format_udp_rx_trace;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239unformat_function_t unformat_udp_header;
240
241void udp_register_dst_port (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500242 udp_dst_port_t dst_port,
243 u32 node_index, u8 is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700244void udp_unregister_dst_port (vlib_main_t * vm,
245 udp_dst_port_t dst_port, u8 is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -0500246
Dave Barachd7cb1b52016-12-09 09:52:16 -0500247void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800248
Florin Coras3cbc04b2017-10-02 00:18:51 -0700249always_inline void *
250vlib_buffer_push_udp (vlib_buffer_t * b, u16 sp, u16 dp, u8 offload_csum)
251{
252 udp_header_t *uh;
253
254 uh = vlib_buffer_push_uninit (b, sizeof (udp_header_t));
255 uh->src_port = sp;
256 uh->dst_port = dp;
257 uh->checksum = 0;
258 uh->length = clib_host_to_net_u16 (b->current_length);
259 if (offload_csum)
260 {
261 b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
262 vnet_buffer (b)->l4_hdr_offset = (u8 *) uh - b->data;
263 }
264 return uh;
265}
266
Florin Corase127a7e2016-02-18 22:20:01 +0100267always_inline void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500268ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
Florin Corase127a7e2016-02-18 22:20:01 +0100269{
Florin Corase127a7e2016-02-18 22:20:01 +0100270 u16 new_l0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500271 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
Florin Coras02655bd2016-04-26 00:17:24 +0200273 if (is_ip4)
274 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 ip4_header_t *ip0;
Florin Coras02655bd2016-04-26 00:17:24 +0200276 ip_csum_t sum0;
277 u16 old_l0 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100278
Dave Barachd7cb1b52016-12-09 09:52:16 -0500279 ip0 = vlib_buffer_get_current (b0);
Florin Corase127a7e2016-02-18 22:20:01 +0100280
Florin Coras02655bd2016-04-26 00:17:24 +0200281 /* fix the <bleep>ing outer-IP checksum */
282 sum0 = ip0->checksum;
283 /* old_l0 always 0, see the rewrite setup */
284 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
Florin Corase127a7e2016-02-18 22:20:01 +0100285
Dave Barachd7cb1b52016-12-09 09:52:16 -0500286 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
287 length /* changed member */ );
Florin Coras02655bd2016-04-26 00:17:24 +0200288 ip0->checksum = ip_csum_fold (sum0);
289 ip0->length = new_l0;
290
291 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500292 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200293 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500294 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200295 udp0->length = new_l0;
296 }
297 else
298 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500299 ip6_header_t *ip0;
Florin Coras210bfc82016-04-29 16:04:33 +0200300 int bogus0;
301
Dave Barachd7cb1b52016-12-09 09:52:16 -0500302 ip0 = vlib_buffer_get_current (b0);
Florin Coras02655bd2016-04-26 00:17:24 +0200303
Florin Coras02655bd2016-04-26 00:17:24 +0200304 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500305 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200306 ip0->payload_length = new_l0;
307
308 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500309 udp0 = (udp_header_t *) (ip0 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200310 udp0->length = new_l0;
Florin Coras210bfc82016-04-29 16:04:33 +0200311
Dave Barachd7cb1b52016-12-09 09:52:16 -0500312 udp0->checksum =
313 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
314 ASSERT (bogus0 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200315
316 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500317 udp0->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200318 }
Florin Corase127a7e2016-02-18 22:20:01 +0100319}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500320
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100321always_inline void
322ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 u8 is_ip4)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100324{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500325 vlib_buffer_advance (b0, -ec_len);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100326
327 if (is_ip4)
328 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500329 ip4_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100330
Dave Barachd7cb1b52016-12-09 09:52:16 -0500331 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100332
333 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500334 clib_memcpy (ip0, ec0, ec_len);
335 ip_udp_fixup_one (vm, b0, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100336 }
337 else
338 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500339 ip6_header_t *ip0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100340
Dave Barachd7cb1b52016-12-09 09:52:16 -0500341 ip0 = vlib_buffer_get_current (b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100342
343 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500344 clib_memcpy (ip0, ec0, ec_len);
345 ip_udp_fixup_one (vm, b0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100346 }
347}
Florin Corase127a7e2016-02-18 22:20:01 +0100348
349always_inline void
Florin Coras02655bd2016-04-26 00:17:24 +0200350ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500351 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
Florin Corase127a7e2016-02-18 22:20:01 +0100352{
Florin Corase127a7e2016-02-18 22:20:01 +0100353 u16 new_l0, new_l1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500354 udp_header_t *udp0, *udp1;
Florin Corase127a7e2016-02-18 22:20:01 +0100355
Dave Barachd7cb1b52016-12-09 09:52:16 -0500356 ASSERT (_vec_len (ec0) == _vec_len (ec1));
Florin Corase127a7e2016-02-18 22:20:01 +0100357
358 vlib_buffer_advance (b0, -ec_len);
359 vlib_buffer_advance (b1, -ec_len);
360
Florin Coras02655bd2016-04-26 00:17:24 +0200361 if (is_v4)
362 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500363 ip4_header_t *ip0, *ip1;
Florin Coras02655bd2016-04-26 00:17:24 +0200364 ip_csum_t sum0, sum1;
365 u16 old_l0 = 0, old_l1 = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100366
Florin Coras02655bd2016-04-26 00:17:24 +0200367 ip0 = vlib_buffer_get_current (b0);
368 ip1 = vlib_buffer_get_current (b1);
Florin Corase127a7e2016-02-18 22:20:01 +0100369
Florin Coras02655bd2016-04-26 00:17:24 +0200370 /* Apply the encap string */
371 clib_memcpy (ip0, ec0, ec_len);
372 clib_memcpy (ip1, ec1, ec_len);
Florin Corase127a7e2016-02-18 22:20:01 +0100373
Florin Coras02655bd2016-04-26 00:17:24 +0200374 /* fix the <bleep>ing outer-IP checksum */
375 sum0 = ip0->checksum;
376 sum1 = ip1->checksum;
Florin Corase127a7e2016-02-18 22:20:01 +0100377
Florin Coras02655bd2016-04-26 00:17:24 +0200378 /* old_l0 always 0, see the rewrite setup */
379 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
380 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
Florin Corase127a7e2016-02-18 22:20:01 +0100381
Dave Barachd7cb1b52016-12-09 09:52:16 -0500382 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
383 length /* changed member */ );
384 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
385 length /* changed member */ );
Florin Corase127a7e2016-02-18 22:20:01 +0100386
Florin Coras02655bd2016-04-26 00:17:24 +0200387 ip0->checksum = ip_csum_fold (sum0);
388 ip1->checksum = ip_csum_fold (sum1);
Florin Corase127a7e2016-02-18 22:20:01 +0100389
Florin Coras02655bd2016-04-26 00:17:24 +0200390 ip0->length = new_l0;
391 ip1->length = new_l1;
Florin Corase127a7e2016-02-18 22:20:01 +0100392
Florin Coras02655bd2016-04-26 00:17:24 +0200393 /* Fix UDP length */
394 udp0 = (udp_header_t *) (ip0 + 1);
395 udp1 = (udp_header_t *) (ip1 + 1);
396
Dave Barachd7cb1b52016-12-09 09:52:16 -0500397 new_l0 =
398 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
399 sizeof (*ip0));
400 new_l1 =
401 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
402 sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200403 udp0->length = new_l0;
404 udp1->length = new_l1;
405 }
406 else
407 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500408 ip6_header_t *ip0, *ip1;
Florin Coras210bfc82016-04-29 16:04:33 +0200409 int bogus0, bogus1;
410
Dave Barachd7cb1b52016-12-09 09:52:16 -0500411 ip0 = vlib_buffer_get_current (b0);
412 ip1 = vlib_buffer_get_current (b1);
Florin Coras02655bd2016-04-26 00:17:24 +0200413
414 /* Apply the encap string. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500415 clib_memcpy (ip0, ec0, ec_len);
416 clib_memcpy (ip1, ec1, ec_len);
Florin Coras02655bd2016-04-26 00:17:24 +0200417
418 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500419 - sizeof (*ip0));
Florin Coras02655bd2016-04-26 00:17:24 +0200420 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500421 - sizeof (*ip1));
Florin Coras02655bd2016-04-26 00:17:24 +0200422 ip0->payload_length = new_l0;
423 ip1->payload_length = new_l1;
424
425 /* Fix UDP length */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500426 udp0 = (udp_header_t *) (ip0 + 1);
427 udp1 = (udp_header_t *) (ip1 + 1);
Florin Coras02655bd2016-04-26 00:17:24 +0200428
429 udp0->length = new_l0;
430 udp1->length = new_l1;
Florin Coras210bfc82016-04-29 16:04:33 +0200431
Dave Barachd7cb1b52016-12-09 09:52:16 -0500432 udp0->checksum =
433 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
434 udp1->checksum =
435 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
436 ASSERT (bogus0 == 0);
437 ASSERT (bogus1 == 0);
Florin Coras210bfc82016-04-29 16:04:33 +0200438
439 if (udp0->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500440 udp0->checksum = 0xffff;
Florin Coras210bfc82016-04-29 16:04:33 +0200441 if (udp1->checksum == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500442 udp1->checksum = 0xffff;
Florin Coras02655bd2016-04-26 00:17:24 +0200443 }
Florin Corase127a7e2016-02-18 22:20:01 +0100444}
445
Dave Barachd7cb1b52016-12-09 09:52:16 -0500446/*
447 * fd.io coding-style-patch-verification: ON
448 *
449 * Local Variables:
450 * eval: (c-set-style "gnu")
451 * End:
452 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500453
454#endif /* __included_udp_h__ */