Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 15 | #ifndef __included_udp_h__ |
| 16 | #define __included_udp_h__ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | |
| 18 | #include <vnet/vnet.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 19 | #include <vnet/udp/udp_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | #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 Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | #include <vnet/ip/ip.h> |
| 27 | #include <vnet/session/transport.h> |
| 28 | |
| 29 | typedef 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 | |
| 37 | typedef 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 | |
| 50 | extern udp_uri_main_t udp_uri_main; |
| 51 | extern vlib_node_registration_t udp4_uri_input_node; |
| 52 | |
| 53 | always_inline udp_uri_main_t * |
| 54 | vnet_get_udp_main () |
| 55 | { |
| 56 | return &udp_uri_main; |
| 57 | } |
| 58 | |
| 59 | always_inline udp_connection_t * |
| 60 | udp_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 | |
| 66 | always_inline udp_connection_t * |
| 67 | udp_listener_get (u32 conn_index) |
| 68 | { |
| 69 | return pool_elt_at_index (udp_uri_main.udp_listeners, conn_index); |
| 70 | } |
| 71 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 72 | typedef enum |
| 73 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | #define udp_error(n,s) UDP_ERROR_##n, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 75 | #include <vnet/udp/udp_error.def> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | #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) \ |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 84 | _ (3784, bfd4) \ |
| 85 | _ (3785, bfd_echo4) \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | _ (4341, lisp_gpe) \ |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 87 | _ (4342, lisp_cp) \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | _ (4739, ipfix) \ |
| 89 | _ (4789, vxlan) \ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 90 | _ (4789, vxlan6) \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | _ (4790, vxlan_gpe) \ |
| 92 | _ (6633, vpath_3) |
| 93 | |
| 94 | |
| 95 | #define foreach_udp6_dst_port \ |
| 96 | _ (547, dhcpv6_to_server) \ |
| 97 | _ (546, dhcpv6_to_client) \ |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 98 | _ (3784, bfd6) \ |
| 99 | _ (3785, bfd_echo6) \ |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 100 | _ (4341, lisp_gpe6) \ |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 101 | _ (4342, lisp_cp6) \ |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 102 | _ (4790, vxlan6_gpe) \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | _ (6633, vpath6_3) |
| 104 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 105 | typedef enum |
| 106 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | #define _(n,f) UDP_DST_PORT_##f = n, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 108 | foreach_udp4_dst_port foreach_udp6_dst_port |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | #undef _ |
| 110 | } udp_dst_port_t; |
| 111 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 112 | typedef enum |
| 113 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | #define _(n,f) UDP6_DST_PORT_##f = n, |
| 115 | foreach_udp6_dst_port |
| 116 | #undef _ |
| 117 | } udp6_dst_port_t; |
| 118 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 119 | typedef struct |
| 120 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | /* Name (a c string). */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 122 | char *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
| 124 | /* GRE protocol type in host byte order. */ |
| 125 | udp_dst_port_t dst_port; |
| 126 | |
| 127 | /* Node which handles this type. */ |
| 128 | u32 node_index; |
| 129 | |
| 130 | /* Next index for this type. */ |
| 131 | u32 next_index; |
| 132 | } udp_dst_port_info_t; |
| 133 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 134 | typedef enum |
| 135 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | UDP_IP6 = 0, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 137 | UDP_IP4, /* the code is full of is_ip4... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | N_UDP_AF, |
| 139 | } udp_af_t; |
| 140 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 141 | typedef struct |
| 142 | { |
| 143 | udp_dst_port_info_t *dst_port_infos[N_UDP_AF]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
| 145 | /* Hash tables mapping name/protocol to protocol info index. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 146 | uword *dst_port_info_by_name[N_UDP_AF]; |
| 147 | uword *dst_port_info_by_dst_port[N_UDP_AF]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | |
Damjan Marion | 615fc61 | 2017-04-03 14:56:08 +0200 | [diff] [blame^] | 149 | /* Sparse vector mapping udp dst_port in network byte order |
| 150 | to next index. */ |
| 151 | u16 *next_by_dst_port4; |
| 152 | u16 *next_by_dst_port6; |
| 153 | u8 punt_unknown4; |
| 154 | u8 punt_unknown6; |
| 155 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | /* convenience */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 157 | vlib_main_t *vlib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | } udp_main_t; |
| 159 | |
| 160 | always_inline udp_dst_port_info_t * |
| 161 | udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4) |
| 162 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 163 | uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0; |
| 165 | } |
| 166 | |
| 167 | format_function_t format_udp_header; |
| 168 | format_function_t format_udp_rx_trace; |
| 169 | |
| 170 | unformat_function_t unformat_udp_header; |
| 171 | |
| 172 | void udp_register_dst_port (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 173 | udp_dst_port_t dst_port, |
| 174 | u32 node_index, u8 is_ip4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 176 | void |
| 177 | udp_unregister_dst_port (vlib_main_t * vm, |
| 178 | udp_dst_port_t dst_port, u8 is_ip4); |
| 179 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 180 | void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add); |
Alexander Popovsky (apopovsk) | 740bcdb | 2016-11-15 15:36:23 -0800 | [diff] [blame] | 181 | |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 182 | always_inline void |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 183 | ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 184 | { |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 185 | u16 new_l0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 186 | udp_header_t *udp0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 188 | if (is_ip4) |
| 189 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 190 | ip4_header_t *ip0; |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 191 | ip_csum_t sum0; |
| 192 | u16 old_l0 = 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 193 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 194 | ip0 = vlib_buffer_get_current (b0); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 195 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 196 | /* fix the <bleep>ing outer-IP checksum */ |
| 197 | sum0 = ip0->checksum; |
| 198 | /* old_l0 always 0, see the rewrite setup */ |
| 199 | new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 200 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 201 | sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, |
| 202 | length /* changed member */ ); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 203 | ip0->checksum = ip_csum_fold (sum0); |
| 204 | ip0->length = new_l0; |
| 205 | |
| 206 | /* Fix UDP length */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 207 | udp0 = (udp_header_t *) (ip0 + 1); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 208 | new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 209 | - sizeof (*ip0)); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 210 | udp0->length = new_l0; |
| 211 | } |
| 212 | else |
| 213 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 214 | ip6_header_t *ip0; |
Florin Coras | 210bfc8 | 2016-04-29 16:04:33 +0200 | [diff] [blame] | 215 | int bogus0; |
| 216 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 217 | ip0 = vlib_buffer_get_current (b0); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 218 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 219 | new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 220 | - sizeof (*ip0)); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 221 | ip0->payload_length = new_l0; |
| 222 | |
| 223 | /* Fix UDP length */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 224 | udp0 = (udp_header_t *) (ip0 + 1); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 225 | udp0->length = new_l0; |
Florin Coras | 210bfc8 | 2016-04-29 16:04:33 +0200 | [diff] [blame] | 226 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 227 | udp0->checksum = |
| 228 | ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0); |
| 229 | ASSERT (bogus0 == 0); |
Florin Coras | 210bfc8 | 2016-04-29 16:04:33 +0200 | [diff] [blame] | 230 | |
| 231 | if (udp0->checksum == 0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 232 | udp0->checksum = 0xffff; |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 233 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 234 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 235 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 236 | always_inline void |
| 237 | ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 238 | u8 is_ip4) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 239 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 240 | vlib_buffer_advance (b0, -ec_len); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 241 | |
| 242 | if (is_ip4) |
| 243 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 244 | ip4_header_t *ip0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 245 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 246 | ip0 = vlib_buffer_get_current (b0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 247 | |
| 248 | /* Apply the encap string. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 249 | clib_memcpy (ip0, ec0, ec_len); |
| 250 | ip_udp_fixup_one (vm, b0, 1); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 254 | ip6_header_t *ip0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 255 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 256 | ip0 = vlib_buffer_get_current (b0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 257 | |
| 258 | /* Apply the encap string. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 259 | clib_memcpy (ip0, ec0, ec_len); |
| 260 | ip_udp_fixup_one (vm, b0, 0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 261 | } |
| 262 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 263 | |
| 264 | always_inline void |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 265 | ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 266 | u8 * ec0, u8 * ec1, word ec_len, u8 is_v4) |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 267 | { |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 268 | u16 new_l0, new_l1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 269 | udp_header_t *udp0, *udp1; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 270 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 271 | ASSERT (_vec_len (ec0) == _vec_len (ec1)); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 272 | |
| 273 | vlib_buffer_advance (b0, -ec_len); |
| 274 | vlib_buffer_advance (b1, -ec_len); |
| 275 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 276 | if (is_v4) |
| 277 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 278 | ip4_header_t *ip0, *ip1; |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 279 | ip_csum_t sum0, sum1; |
| 280 | u16 old_l0 = 0, old_l1 = 0; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 281 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 282 | ip0 = vlib_buffer_get_current (b0); |
| 283 | ip1 = vlib_buffer_get_current (b1); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 284 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 285 | /* Apply the encap string */ |
| 286 | clib_memcpy (ip0, ec0, ec_len); |
| 287 | clib_memcpy (ip1, ec1, ec_len); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 288 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 289 | /* fix the <bleep>ing outer-IP checksum */ |
| 290 | sum0 = ip0->checksum; |
| 291 | sum1 = ip1->checksum; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 292 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 293 | /* old_l0 always 0, see the rewrite setup */ |
| 294 | new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); |
| 295 | new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 296 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 297 | sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, |
| 298 | length /* changed member */ ); |
| 299 | sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t, |
| 300 | length /* changed member */ ); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 301 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 302 | ip0->checksum = ip_csum_fold (sum0); |
| 303 | ip1->checksum = ip_csum_fold (sum1); |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 304 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 305 | ip0->length = new_l0; |
| 306 | ip1->length = new_l1; |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 307 | |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 308 | /* Fix UDP length */ |
| 309 | udp0 = (udp_header_t *) (ip0 + 1); |
| 310 | udp1 = (udp_header_t *) (ip1 + 1); |
| 311 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 312 | new_l0 = |
| 313 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) - |
| 314 | sizeof (*ip0)); |
| 315 | new_l1 = |
| 316 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) - |
| 317 | sizeof (*ip1)); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 318 | udp0->length = new_l0; |
| 319 | udp1->length = new_l1; |
| 320 | } |
| 321 | else |
| 322 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 323 | ip6_header_t *ip0, *ip1; |
Florin Coras | 210bfc8 | 2016-04-29 16:04:33 +0200 | [diff] [blame] | 324 | int bogus0, bogus1; |
| 325 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 326 | ip0 = vlib_buffer_get_current (b0); |
| 327 | ip1 = vlib_buffer_get_current (b1); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 328 | |
| 329 | /* Apply the encap string. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 330 | clib_memcpy (ip0, ec0, ec_len); |
| 331 | clib_memcpy (ip1, ec1, ec_len); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 332 | |
| 333 | new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 334 | - sizeof (*ip0)); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 335 | new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 336 | - sizeof (*ip1)); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 337 | ip0->payload_length = new_l0; |
| 338 | ip1->payload_length = new_l1; |
| 339 | |
| 340 | /* Fix UDP length */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 341 | udp0 = (udp_header_t *) (ip0 + 1); |
| 342 | udp1 = (udp_header_t *) (ip1 + 1); |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 343 | |
| 344 | udp0->length = new_l0; |
| 345 | udp1->length = new_l1; |
Florin Coras | 210bfc8 | 2016-04-29 16:04:33 +0200 | [diff] [blame] | 346 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 347 | udp0->checksum = |
| 348 | ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0); |
| 349 | udp1->checksum = |
| 350 | ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1); |
| 351 | ASSERT (bogus0 == 0); |
| 352 | ASSERT (bogus1 == 0); |
Florin Coras | 210bfc8 | 2016-04-29 16:04:33 +0200 | [diff] [blame] | 353 | |
| 354 | if (udp0->checksum == 0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 355 | udp0->checksum = 0xffff; |
Florin Coras | 210bfc8 | 2016-04-29 16:04:33 +0200 | [diff] [blame] | 356 | if (udp1->checksum == 0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 357 | udp1->checksum = 0xffff; |
Florin Coras | 02655bd | 2016-04-26 00:17:24 +0200 | [diff] [blame] | 358 | } |
Florin Coras | e127a7e | 2016-02-18 22:20:01 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 361 | /* |
| 362 | * fd.io coding-style-patch-verification: ON |
| 363 | * |
| 364 | * Local Variables: |
| 365 | * eval: (c-set-style "gnu") |
| 366 | * End: |
| 367 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 368 | |
| 369 | #endif /* __included_udp_h__ */ |