udp: remove buggy assert in udp encap
[vpp.git] / src / vnet / udp / udp_inlines.h
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * 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  */
15
16 #ifndef SRC_VNET_UDP_UDP_INLINES_H_
17 #define SRC_VNET_UDP_UDP_INLINES_H_
18
19 #include <vnet/vnet.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/ip/ip6.h>
22 #include <vnet/udp/udp_packet.h>
23 #include <vnet/interface_output.h>
24
25 always_inline void *
26 vlib_buffer_push_udp (vlib_buffer_t * b, u16 sp, u16 dp, u8 offload_csum)
27 {
28   udp_header_t *uh;
29   u16 udp_len = sizeof (udp_header_t) + b->current_length;
30   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
31     udp_len += b->total_length_not_including_first_buffer;
32
33   uh = vlib_buffer_push_uninit (b, sizeof (udp_header_t));
34   uh->src_port = sp;
35   uh->dst_port = dp;
36   uh->checksum = 0;
37   uh->length = clib_host_to_net_u16 (udp_len);
38   if (offload_csum)
39     vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
40   vnet_buffer (b)->l4_hdr_offset = (u8 *) uh - b->data;
41   b->flags |= VNET_BUFFER_F_L4_HDR_OFFSET_VALID;
42   return uh;
43 }
44
45 always_inline void
46 ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
47 {
48   u16 new_l0;
49   udp_header_t *udp0;
50
51   if (is_ip4)
52     {
53       ip4_header_t *ip0;
54       ip_csum_t sum0;
55       u16 old_l0 = 0;
56
57       ip0 = vlib_buffer_get_current (b0);
58
59       /* fix the <bleep>ing outer-IP checksum */
60       sum0 = ip0->checksum;
61       /* old_l0 always 0, see the rewrite setup */
62       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
63
64       sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
65                              length /* changed member */ );
66       ip0->checksum = ip_csum_fold (sum0);
67       ip0->length = new_l0;
68
69       /* Fix UDP length */
70       udp0 = (udp_header_t *) (ip0 + 1);
71       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
72                                      - sizeof (*ip0));
73       udp0->length = new_l0;
74     }
75   else
76     {
77       ip6_header_t *ip0;
78       int bogus0;
79
80       ip0 = vlib_buffer_get_current (b0);
81
82       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
83                                      - sizeof (*ip0));
84       ip0->payload_length = new_l0;
85
86       /* Fix UDP length */
87       udp0 = (udp_header_t *) (ip0 + 1);
88       udp0->length = new_l0;
89
90       udp0->checksum =
91         ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
92       ASSERT (bogus0 == 0);
93
94       if (udp0->checksum == 0)
95         udp0->checksum = 0xffff;
96     }
97 }
98
99 always_inline void
100 ip_udp_encap_one (vlib_main_t *vm, vlib_buffer_t *b0, u8 *ec0, word ec_len,
101                   ip_address_family_t encap_family,
102                   ip_address_family_t payload_family)
103 {
104
105   if (payload_family < N_AF)
106     {
107       vnet_calc_checksums_inline (vm, b0, payload_family == AF_IP4,
108                                   payload_family == AF_IP6);
109     }
110
111   vlib_buffer_advance (b0, -ec_len);
112
113   if (encap_family == AF_IP4)
114     {
115       ip4_header_t *ip0;
116
117       ip0 = vlib_buffer_get_current (b0);
118
119       /* Apply the encap string. */
120       clib_memcpy_fast (ip0, ec0, ec_len);
121       ip_udp_fixup_one (vm, b0, 1);
122     }
123   else
124     {
125       ip6_header_t *ip0;
126
127       ip0 = vlib_buffer_get_current (b0);
128
129       /* Apply the encap string. */
130       clib_memcpy_fast (ip0, ec0, ec_len);
131       ip_udp_fixup_one (vm, b0, 0);
132     }
133 }
134
135 always_inline void
136 ip_udp_encap_two (vlib_main_t *vm, vlib_buffer_t *b0, vlib_buffer_t *b1,
137                   u8 *ec0, u8 *ec1, word ec_len,
138                   ip_address_family_t encap_family,
139                   ip_address_family_t payload_family)
140 {
141   u16 new_l0, new_l1;
142   udp_header_t *udp0, *udp1;
143   int payload_ip4 = (payload_family == AF_IP4);
144
145   if (payload_family < N_AF)
146     {
147       vnet_calc_checksums_inline (vm, b0, payload_ip4, !payload_ip4);
148       vnet_calc_checksums_inline (vm, b1, payload_ip4, !payload_ip4);
149     }
150
151   vlib_buffer_advance (b0, -ec_len);
152   vlib_buffer_advance (b1, -ec_len);
153
154   if (encap_family == AF_IP4)
155     {
156       ip4_header_t *ip0, *ip1;
157       ip_csum_t sum0, sum1;
158       u16 old_l0 = 0, old_l1 = 0;
159
160       ip0 = vlib_buffer_get_current (b0);
161       ip1 = vlib_buffer_get_current (b1);
162
163       /* Apply the encap string */
164       clib_memcpy_fast (ip0, ec0, ec_len);
165       clib_memcpy_fast (ip1, ec1, ec_len);
166
167       /* fix the <bleep>ing outer-IP checksum */
168       sum0 = ip0->checksum;
169       sum1 = ip1->checksum;
170
171       /* old_l0 always 0, see the rewrite setup */
172       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
173       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
174
175       sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
176                              length /* changed member */ );
177       sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
178                              length /* changed member */ );
179
180       ip0->checksum = ip_csum_fold (sum0);
181       ip1->checksum = ip_csum_fold (sum1);
182
183       ip0->length = new_l0;
184       ip1->length = new_l1;
185
186       /* Fix UDP length */
187       udp0 = (udp_header_t *) (ip0 + 1);
188       udp1 = (udp_header_t *) (ip1 + 1);
189
190       new_l0 =
191         clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
192                               sizeof (*ip0));
193       new_l1 =
194         clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
195                               sizeof (*ip1));
196       udp0->length = new_l0;
197       udp1->length = new_l1;
198     }
199   else
200     {
201       ip6_header_t *ip0, *ip1;
202       int bogus0, bogus1;
203
204       ip0 = vlib_buffer_get_current (b0);
205       ip1 = vlib_buffer_get_current (b1);
206
207       /* Apply the encap string. */
208       clib_memcpy_fast (ip0, ec0, ec_len);
209       clib_memcpy_fast (ip1, ec1, ec_len);
210
211       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
212                                      - sizeof (*ip0));
213       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
214                                      - sizeof (*ip1));
215       ip0->payload_length = new_l0;
216       ip1->payload_length = new_l1;
217
218       /* Fix UDP length */
219       udp0 = (udp_header_t *) (ip0 + 1);
220       udp1 = (udp_header_t *) (ip1 + 1);
221
222       udp0->length = new_l0;
223       udp1->length = new_l1;
224
225       udp0->checksum =
226         ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
227       udp1->checksum =
228         ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
229       ASSERT (bogus0 == 0);
230       ASSERT (bogus1 == 0);
231
232       if (udp0->checksum == 0)
233         udp0->checksum = 0xffff;
234       if (udp1->checksum == 0)
235         udp1->checksum = 0xffff;
236     }
237 }
238
239 #endif /* SRC_VNET_UDP_UDP_INLINES_H_ */
240
241 /*
242  * fd.io coding-style-patch-verification: ON
243  *
244  * Local Variables:
245  * eval: (c-set-style "gnu")
246  * End:
247  */