Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 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> |
Arthur de Kerhor | 7aeb647 | 2021-06-01 11:27:48 +0200 | [diff] [blame] | 23 | #include <vnet/interface_output.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 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) |
Mohsin Kazmi | 6809538 | 2021-02-10 11:26:24 +0100 | [diff] [blame] | 39 | vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM); |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 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 |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 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) |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 103 | { |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 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 | } |
Arthur de Kerhor | 7aeb647 | 2021-06-01 11:27:48 +0200 | [diff] [blame] | 110 | |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 111 | vlib_buffer_advance (b0, -ec_len); |
| 112 | |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 113 | if (encap_family == AF_IP4) |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 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 |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 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) |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 140 | { |
| 141 | u16 new_l0, new_l1; |
| 142 | udp_header_t *udp0, *udp1; |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 143 | int payload_ip4 = (payload_family == AF_IP4); |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 144 | |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame^] | 145 | ASSERT (vec_len (ec0) == vec_len (ec1)); |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 146 | |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 147 | if (payload_family < N_AF) |
| 148 | { |
| 149 | vnet_calc_checksums_inline (vm, b0, payload_ip4, !payload_ip4); |
| 150 | vnet_calc_checksums_inline (vm, b1, payload_ip4, !payload_ip4); |
| 151 | } |
Arthur de Kerhor | 7aeb647 | 2021-06-01 11:27:48 +0200 | [diff] [blame] | 152 | |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 153 | vlib_buffer_advance (b0, -ec_len); |
| 154 | vlib_buffer_advance (b1, -ec_len); |
| 155 | |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 156 | if (encap_family == AF_IP4) |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 157 | { |
| 158 | ip4_header_t *ip0, *ip1; |
| 159 | ip_csum_t sum0, sum1; |
| 160 | u16 old_l0 = 0, old_l1 = 0; |
| 161 | |
| 162 | ip0 = vlib_buffer_get_current (b0); |
| 163 | ip1 = vlib_buffer_get_current (b1); |
| 164 | |
| 165 | /* Apply the encap string */ |
| 166 | clib_memcpy_fast (ip0, ec0, ec_len); |
| 167 | clib_memcpy_fast (ip1, ec1, ec_len); |
| 168 | |
| 169 | /* fix the <bleep>ing outer-IP checksum */ |
| 170 | sum0 = ip0->checksum; |
| 171 | sum1 = ip1->checksum; |
| 172 | |
| 173 | /* old_l0 always 0, see the rewrite setup */ |
| 174 | new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); |
| 175 | new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)); |
| 176 | |
| 177 | sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, |
| 178 | length /* changed member */ ); |
| 179 | sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t, |
| 180 | length /* changed member */ ); |
| 181 | |
| 182 | ip0->checksum = ip_csum_fold (sum0); |
| 183 | ip1->checksum = ip_csum_fold (sum1); |
| 184 | |
| 185 | ip0->length = new_l0; |
| 186 | ip1->length = new_l1; |
| 187 | |
| 188 | /* Fix UDP length */ |
| 189 | udp0 = (udp_header_t *) (ip0 + 1); |
| 190 | udp1 = (udp_header_t *) (ip1 + 1); |
| 191 | |
| 192 | new_l0 = |
| 193 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) - |
| 194 | sizeof (*ip0)); |
| 195 | new_l1 = |
| 196 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) - |
| 197 | sizeof (*ip1)); |
| 198 | udp0->length = new_l0; |
| 199 | udp1->length = new_l1; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | ip6_header_t *ip0, *ip1; |
| 204 | int bogus0, bogus1; |
| 205 | |
| 206 | ip0 = vlib_buffer_get_current (b0); |
| 207 | ip1 = vlib_buffer_get_current (b1); |
| 208 | |
| 209 | /* Apply the encap string. */ |
| 210 | clib_memcpy_fast (ip0, ec0, ec_len); |
| 211 | clib_memcpy_fast (ip1, ec1, ec_len); |
| 212 | |
| 213 | new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) |
| 214 | - sizeof (*ip0)); |
| 215 | new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) |
| 216 | - sizeof (*ip1)); |
| 217 | ip0->payload_length = new_l0; |
| 218 | ip1->payload_length = new_l1; |
| 219 | |
| 220 | /* Fix UDP length */ |
| 221 | udp0 = (udp_header_t *) (ip0 + 1); |
| 222 | udp1 = (udp_header_t *) (ip1 + 1); |
| 223 | |
| 224 | udp0->length = new_l0; |
| 225 | udp1->length = new_l1; |
| 226 | |
| 227 | udp0->checksum = |
| 228 | ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0); |
| 229 | udp1->checksum = |
| 230 | ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1); |
| 231 | ASSERT (bogus0 == 0); |
| 232 | ASSERT (bogus1 == 0); |
| 233 | |
| 234 | if (udp0->checksum == 0) |
| 235 | udp0->checksum = 0xffff; |
| 236 | if (udp1->checksum == 0) |
| 237 | udp1->checksum = 0xffff; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | #endif /* SRC_VNET_UDP_UDP_INLINES_H_ */ |
| 242 | |
| 243 | /* |
| 244 | * fd.io coding-style-patch-verification: ON |
| 245 | * |
| 246 | * Local Variables: |
| 247 | * eval: (c-set-style "gnu") |
| 248 | * End: |
| 249 | */ |