Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | * @file |
| 17 | * @brief IPv6 to IPv4 translation |
| 18 | */ |
| 19 | #ifndef __included_ip6_to_ip4_h__ |
| 20 | #define __included_ip6_to_ip4_h__ |
| 21 | |
| 22 | #include <vnet/ip/ip.h> |
| 23 | |
| 24 | /** |
| 25 | * IPv6 to IPv4 set call back function type |
| 26 | */ |
| 27 | typedef int (*ip6_to_ip4_set_fn_t) (ip6_header_t * ip6, ip4_header_t * ip4, |
| 28 | void *ctx); |
| 29 | |
| 30 | /* *INDENT-OFF* */ |
| 31 | static u8 icmp6_to_icmp_updater_pointer_table[] = |
| 32 | { 0, 1, ~0, ~0, |
| 33 | 2, 2, 9, 8, |
| 34 | 12, 12, 12, 12, |
| 35 | 12, 12, 12, 12, |
| 36 | 12, 12, 12, 12, |
| 37 | 12, 12, 12, 12, |
| 38 | 24, 24, 24, 24, |
| 39 | 24, 24, 24, 24, |
| 40 | 24, 24, 24, 24, |
| 41 | 24, 24, 24, 24 |
| 42 | }; |
| 43 | /* *INDENT-ON* */ |
| 44 | |
| 45 | #define frag_id_6to4(id) ((id) ^ ((id) >> 16)) |
| 46 | |
| 47 | /** |
| 48 | * @brief Parse some useful information from IPv6 header. |
| 49 | * |
| 50 | * @param ip6 IPv6 header. |
| 51 | * @param buff_len Buffer length. |
| 52 | * @param l4_protocol L4 protocol number. |
| 53 | * @param l4_offset L4 header offset. |
| 54 | * @param frag_hdr_offset Fragment header offset if present, 0 otherwise. |
| 55 | * |
| 56 | * @returns 0 on success, non-zero value otherwise. |
| 57 | */ |
| 58 | static_always_inline int |
| 59 | ip6_parse (const ip6_header_t * ip6, u32 buff_len, |
| 60 | u8 * l4_protocol, u16 * l4_offset, u16 * frag_hdr_offset) |
| 61 | { |
| 62 | if (ip6->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION) |
| 63 | { |
| 64 | *l4_protocol = ((ip6_frag_hdr_t *) (ip6 + 1))->next_hdr; |
| 65 | *frag_hdr_offset = sizeof (*ip6); |
| 66 | *l4_offset = sizeof (*ip6) + sizeof (ip6_frag_hdr_t); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | *l4_protocol = ip6->protocol; |
| 71 | *frag_hdr_offset = 0; |
| 72 | *l4_offset = sizeof (*ip6); |
| 73 | } |
| 74 | |
| 75 | return (buff_len < (*l4_offset + 4)) || |
| 76 | (clib_net_to_host_u16 (ip6->payload_length) < |
| 77 | (*l4_offset + 4 - sizeof (*ip6))); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @brief Get TCP/UDP port number or ICMP id from IPv6 packet. |
| 82 | * |
| 83 | * @param ip6 IPv6 header. |
| 84 | * @param sender 1 get sender port, 0 get receiver port. |
| 85 | * @param buffer_len Buffer length. |
| 86 | * |
| 87 | * @returns Port number on success, 0 otherwise. |
| 88 | */ |
| 89 | always_inline u16 |
| 90 | ip6_get_port (ip6_header_t * ip6, u8 sender, u16 buffer_len) |
| 91 | { |
| 92 | u8 l4_protocol; |
| 93 | u16 l4_offset; |
| 94 | u16 frag_offset; |
| 95 | u8 *l4; |
| 96 | |
| 97 | if (ip6_parse (ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset)) |
| 98 | return 0; |
| 99 | |
| 100 | if (frag_offset && |
| 101 | ip6_frag_hdr_offset (((ip6_frag_hdr_t *) |
| 102 | u8_ptr_add (ip6, frag_offset)))) |
| 103 | return 0; //Can't deal with non-first fragment for now |
| 104 | |
| 105 | l4 = u8_ptr_add (ip6, l4_offset); |
| 106 | if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP) |
| 107 | { |
| 108 | return (sender) ? ((udp_header_t *) (l4))->src_port : ((udp_header_t |
| 109 | *) |
| 110 | (l4))->dst_port; |
| 111 | } |
| 112 | else if (l4_protocol == IP_PROTOCOL_ICMP6) |
| 113 | { |
| 114 | icmp46_header_t *icmp = (icmp46_header_t *) (l4); |
| 115 | if (icmp->type == ICMP6_echo_request) |
| 116 | { |
| 117 | return (sender) ? ((u16 *) (icmp))[2] : -1; |
| 118 | } |
| 119 | else if (icmp->type == ICMP6_echo_reply) |
| 120 | { |
| 121 | return (sender) ? -1 : ((u16 *) (icmp))[2]; |
| 122 | } |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @brief Convert type and code value from ICMP6 to ICMP4. |
| 129 | * |
| 130 | * @param icmp ICMP header. |
| 131 | * @param inner_ip6 Inner IPv6 header if present, 0 otherwise. |
| 132 | * |
| 133 | * @returns 0 on success, non-zero value otherwise. |
| 134 | */ |
| 135 | static_always_inline int |
| 136 | icmp6_to_icmp_header (icmp46_header_t * icmp, ip6_header_t ** inner_ip6) |
| 137 | { |
| 138 | *inner_ip6 = NULL; |
| 139 | switch (icmp->type) |
| 140 | { |
| 141 | case ICMP6_echo_request: |
| 142 | icmp->type = ICMP4_echo_request; |
| 143 | break; |
| 144 | case ICMP6_echo_reply: |
| 145 | icmp->type = ICMP4_echo_reply; |
| 146 | break; |
| 147 | case ICMP6_destination_unreachable: |
| 148 | *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8); |
| 149 | |
| 150 | switch (icmp->code) |
| 151 | { |
| 152 | case ICMP6_destination_unreachable_no_route_to_destination: //0 |
| 153 | case ICMP6_destination_unreachable_beyond_scope_of_source_address: //2 |
| 154 | case ICMP6_destination_unreachable_address_unreachable: //3 |
| 155 | icmp->type = ICMP4_destination_unreachable; |
| 156 | icmp->code = |
| 157 | ICMP4_destination_unreachable_destination_unreachable_host; |
| 158 | break; |
| 159 | case ICMP6_destination_unreachable_destination_administratively_prohibited: //1 |
| 160 | icmp->type = |
| 161 | ICMP4_destination_unreachable; |
| 162 | icmp->code = |
| 163 | ICMP4_destination_unreachable_communication_administratively_prohibited; |
| 164 | break; |
| 165 | case ICMP6_destination_unreachable_port_unreachable: |
| 166 | icmp->type = ICMP4_destination_unreachable; |
| 167 | icmp->code = ICMP4_destination_unreachable_port_unreachable; |
| 168 | break; |
| 169 | default: |
| 170 | return -1; |
| 171 | } |
| 172 | break; |
| 173 | case ICMP6_packet_too_big: |
| 174 | *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8); |
| 175 | |
| 176 | icmp->type = ICMP4_destination_unreachable; |
| 177 | icmp->code = 4; |
| 178 | { |
| 179 | u32 advertised_mtu = clib_net_to_host_u32 (*((u32 *) (icmp + 1))); |
| 180 | advertised_mtu -= 20; |
| 181 | //FIXME: = minimum(advertised MTU-20, MTU_of_IPv4_nexthop, (MTU_of_IPv6_nexthop)-20) |
| 182 | ((u16 *) (icmp))[3] = clib_host_to_net_u16 (advertised_mtu); |
| 183 | } |
| 184 | break; |
| 185 | |
| 186 | case ICMP6_time_exceeded: |
| 187 | *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8); |
| 188 | |
| 189 | icmp->type = ICMP4_time_exceeded; |
| 190 | break; |
| 191 | |
| 192 | case ICMP6_parameter_problem: |
| 193 | *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8); |
| 194 | |
| 195 | switch (icmp->code) |
| 196 | { |
| 197 | case ICMP6_parameter_problem_erroneous_header_field: |
| 198 | icmp->type = ICMP4_parameter_problem; |
| 199 | icmp->code = ICMP4_parameter_problem_pointer_indicates_error; |
| 200 | u32 pointer = clib_net_to_host_u32 (*((u32 *) (icmp + 1))); |
| 201 | if (pointer >= 40) |
| 202 | return -1; |
| 203 | |
| 204 | ((u8 *) (icmp + 1))[0] = |
| 205 | icmp6_to_icmp_updater_pointer_table[pointer]; |
| 206 | break; |
| 207 | case ICMP6_parameter_problem_unrecognized_next_header: |
| 208 | icmp->type = ICMP4_destination_unreachable; |
| 209 | icmp->code = ICMP4_destination_unreachable_port_unreachable; |
| 210 | break; |
| 211 | case ICMP6_parameter_problem_unrecognized_option: |
| 212 | default: |
| 213 | return -1; |
| 214 | } |
| 215 | break; |
| 216 | default: |
| 217 | return -1; |
| 218 | break; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @brief Translate TOS value from IPv6 to IPv4. |
| 225 | * |
| 226 | * @param ip6 IPv6 header. |
| 227 | * |
| 228 | * @returns IPv4 TOS value. |
| 229 | */ |
| 230 | static_always_inline u8 |
| 231 | ip6_translate_tos (const ip6_header_t * ip6) |
| 232 | { |
| 233 | return (clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label) |
| 234 | & 0x0ff00000) >> 20; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @brief Translate ICMP6 packet to ICMP4. |
| 239 | * |
| 240 | * @param p Buffer to translate. |
| 241 | * @param fn The function to translate outer header. |
| 242 | * @param ctx A context passed in the outer header translate function. |
| 243 | * @param inner_fn The function to translate inner header. |
| 244 | * @param inner_ctx A context passed in the inner header translate function. |
| 245 | * |
| 246 | * @returns 0 on success, non-zero value otherwise. |
| 247 | */ |
| 248 | always_inline int |
| 249 | icmp6_to_icmp (vlib_buffer_t * p, ip6_to_ip4_set_fn_t fn, void *ctx, |
| 250 | ip6_to_ip4_set_fn_t inner_fn, void *inner_ctx) |
| 251 | { |
| 252 | ip6_header_t *ip6, *inner_ip6; |
| 253 | ip4_header_t *ip4, *inner_ip4; |
| 254 | u32 ip6_pay_len; |
| 255 | icmp46_header_t *icmp; |
| 256 | ip_csum_t csum; |
| 257 | int rv; |
Matus Fabian | c8e294b | 2017-11-08 00:18:15 -0800 | [diff] [blame] | 258 | ip6_address_t old_src, old_dst; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 259 | |
| 260 | ip6 = vlib_buffer_get_current (p); |
| 261 | ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length); |
| 262 | icmp = (icmp46_header_t *) (ip6 + 1); |
| 263 | ASSERT (ip6_pay_len + sizeof (*ip6) <= p->current_length); |
| 264 | |
| 265 | //No extensions headers allowed here |
| 266 | if (ip6->protocol != IP_PROTOCOL_ICMP6) |
| 267 | return -1; |
| 268 | |
| 269 | //There are no fragmented ICMP messages, so no extension header for now |
| 270 | if (icmp6_to_icmp_header (icmp, &inner_ip6)) |
| 271 | return -1; |
| 272 | |
| 273 | if (inner_ip6) |
| 274 | { |
| 275 | u16 *inner_L4_checksum, inner_l4_offset, inner_frag_offset, |
| 276 | inner_frag_id; |
| 277 | u8 *inner_l4, inner_protocol; |
| 278 | |
| 279 | //We have two headers to translate |
| 280 | // FROM |
| 281 | // [ IPv6 ]<- ext ->[IC][ IPv6 ]<- ext ->[L4 header ... |
| 282 | // Handled cases: |
| 283 | // [ IPv6 ][IC][ IPv6 ][L4 header ... |
| 284 | // [ IPv6 ][IC][ IPv6 ][Fr][L4 header ... |
| 285 | // TO |
| 286 | // [ IPv4][IC][ IPv4][L4 header ... |
| 287 | |
| 288 | if (ip6_parse (inner_ip6, ip6_pay_len - 8, |
| 289 | &inner_protocol, &inner_l4_offset, &inner_frag_offset)) |
| 290 | return -1; |
| 291 | |
| 292 | inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset); |
| 293 | inner_ip4 = |
| 294 | (ip4_header_t *) u8_ptr_add (inner_l4, -sizeof (*inner_ip4)); |
| 295 | if (inner_frag_offset) |
| 296 | { |
| 297 | ip6_frag_hdr_t *inner_frag = |
| 298 | (ip6_frag_hdr_t *) u8_ptr_add (inner_ip6, inner_frag_offset); |
| 299 | inner_frag_id = frag_id_6to4 (inner_frag->identification); |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | inner_frag_id = 0; |
| 304 | } |
| 305 | |
| 306 | //Do the translation of the inner packet |
| 307 | if (inner_protocol == IP_PROTOCOL_TCP) |
| 308 | { |
| 309 | inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 16); |
| 310 | } |
| 311 | else if (inner_protocol == IP_PROTOCOL_UDP) |
| 312 | { |
| 313 | inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 6); |
| 314 | } |
| 315 | else if (inner_protocol == IP_PROTOCOL_ICMP6) |
| 316 | { |
| 317 | icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 318 | //It cannot be of a different type as ip6_icmp_to_icmp6_in_place succeeded |
| 319 | inner_icmp->type = (inner_icmp->type == ICMP6_echo_request) ? |
| 320 | ICMP4_echo_request : ICMP4_echo_reply; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 321 | inner_protocol = IP_PROTOCOL_ICMP; //Will be copied to ip6 later |
| 322 | inner_L4_checksum = &inner_icmp->checksum; |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | return -1; |
| 327 | } |
| 328 | |
Matus Fabian | c8e294b | 2017-11-08 00:18:15 -0800 | [diff] [blame] | 329 | old_src.as_u64[0] = inner_ip6->src_address.as_u64[0]; |
| 330 | old_src.as_u64[1] = inner_ip6->src_address.as_u64[1]; |
| 331 | old_dst.as_u64[0] = inner_ip6->dst_address.as_u64[0]; |
| 332 | old_dst.as_u64[1] = inner_ip6->dst_address.as_u64[1]; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 333 | |
| 334 | if ((rv = inner_fn (inner_ip6, inner_ip4, inner_ctx)) != 0) |
| 335 | return rv; |
| 336 | |
| 337 | inner_ip4->ip_version_and_header_length = |
| 338 | IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS; |
| 339 | inner_ip4->tos = ip6_translate_tos (inner_ip6); |
| 340 | inner_ip4->length = |
| 341 | u16_net_add (inner_ip6->payload_length, |
| 342 | sizeof (*ip4) + sizeof (*ip6) - inner_l4_offset); |
| 343 | inner_ip4->fragment_id = inner_frag_id; |
| 344 | inner_ip4->flags_and_fragment_offset = |
| 345 | clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS); |
| 346 | inner_ip4->ttl = inner_ip6->hop_limit; |
| 347 | inner_ip4->protocol = inner_protocol; |
| 348 | inner_ip4->checksum = ip4_header_checksum (inner_ip4); |
| 349 | |
| 350 | if (inner_ip4->protocol == IP_PROTOCOL_ICMP) |
| 351 | { |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 352 | //Recompute ICMP checksum |
| 353 | icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4; |
| 354 | inner_icmp->checksum = 0; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 355 | csum = |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 356 | ip_incremental_checksum (0, inner_icmp, |
| 357 | clib_net_to_host_u16 (inner_ip4->length) |
| 358 | - sizeof (*inner_ip4)); |
| 359 | inner_icmp->checksum = ~ip_csum_fold (csum); |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 360 | } |
| 361 | else |
| 362 | { |
| 363 | //Update to new pseudo-header |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 364 | csum = *inner_L4_checksum; |
Matus Fabian | c8e294b | 2017-11-08 00:18:15 -0800 | [diff] [blame] | 365 | csum = ip_csum_sub_even (csum, old_src.as_u64[0]); |
| 366 | csum = ip_csum_sub_even (csum, old_src.as_u64[1]); |
| 367 | csum = ip_csum_sub_even (csum, old_dst.as_u64[0]); |
| 368 | csum = ip_csum_sub_even (csum, old_dst.as_u64[1]); |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 369 | csum = ip_csum_add_even (csum, inner_ip4->src_address.as_u32); |
| 370 | csum = ip_csum_add_even (csum, inner_ip4->dst_address.as_u32); |
Matus Fabian | 029f3d2 | 2017-06-15 02:28:50 -0700 | [diff] [blame] | 371 | *inner_L4_checksum = ip_csum_fold (csum); |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 372 | } |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 373 | |
| 374 | //Move up icmp header |
| 375 | ip4 = (ip4_header_t *) u8_ptr_add (inner_l4, -2 * sizeof (*ip4) - 8); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame^] | 376 | clib_memcpy_fast (u8_ptr_add (inner_l4, -sizeof (*ip4) - 8), icmp, 8); |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 377 | icmp = (icmp46_header_t *) u8_ptr_add (inner_l4, -sizeof (*ip4) - 8); |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | //Only one header to translate |
| 382 | ip4 = (ip4_header_t *) u8_ptr_add (ip6, sizeof (*ip6) - sizeof (*ip4)); |
| 383 | } |
| 384 | |
| 385 | vlib_buffer_advance (p, (u32) (((u8 *) ip4) - ((u8 *) ip6))); |
| 386 | |
| 387 | if ((rv = fn (ip6, ip4, ctx)) != 0) |
| 388 | return rv; |
| 389 | |
| 390 | ip4->ip_version_and_header_length = |
| 391 | IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS; |
| 392 | ip4->tos = ip6_translate_tos (ip6); |
| 393 | ip4->fragment_id = 0; |
| 394 | ip4->flags_and_fragment_offset = 0; |
| 395 | ip4->ttl = ip6->hop_limit; |
| 396 | ip4->protocol = IP_PROTOCOL_ICMP; |
| 397 | //TODO fix the length depending on offset length |
| 398 | ip4->length = u16_net_add (ip6->payload_length, |
| 399 | (inner_ip6 == |
| 400 | NULL) ? sizeof (*ip4) : (2 * sizeof (*ip4) - |
| 401 | sizeof (*ip6))); |
| 402 | ip4->checksum = ip4_header_checksum (ip4); |
| 403 | |
| 404 | //Recompute ICMP checksum |
| 405 | icmp->checksum = 0; |
| 406 | csum = |
| 407 | ip_incremental_checksum (0, icmp, |
| 408 | clib_net_to_host_u16 (ip4->length) - |
| 409 | sizeof (*ip4)); |
| 410 | icmp->checksum = ~ip_csum_fold (csum); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * @brief Translate IPv6 fragmented packet to IPv4. |
| 417 | * |
| 418 | * @param p Buffer to translate. |
| 419 | * @param fn The function to translate header. |
| 420 | * @param ctx A context passed in the header translate function. |
| 421 | * |
| 422 | * @returns 0 on success, non-zero value otherwise. |
| 423 | */ |
| 424 | always_inline int |
| 425 | ip6_to_ip4_fragmented (vlib_buffer_t * p, ip6_to_ip4_set_fn_t fn, void *ctx) |
| 426 | { |
| 427 | ip6_header_t *ip6; |
| 428 | ip6_frag_hdr_t *frag; |
| 429 | ip4_header_t *ip4; |
| 430 | u16 frag_id; |
| 431 | u8 frag_more; |
| 432 | u16 frag_offset; |
| 433 | u8 l4_protocol; |
| 434 | u16 l4_offset; |
| 435 | int rv; |
| 436 | |
| 437 | ip6 = vlib_buffer_get_current (p); |
| 438 | |
| 439 | if (ip6_parse |
| 440 | (ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset)) |
| 441 | return -1; |
| 442 | |
| 443 | frag = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset); |
| 444 | ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4)); |
| 445 | vlib_buffer_advance (p, l4_offset - sizeof (*ip4)); |
| 446 | |
| 447 | frag_id = frag_id_6to4 (frag->identification); |
| 448 | frag_more = ip6_frag_hdr_more (frag); |
| 449 | frag_offset = ip6_frag_hdr_offset (frag); |
| 450 | |
| 451 | if ((rv = fn (ip6, ip4, ctx)) != 0) |
| 452 | return rv; |
| 453 | |
| 454 | ip4->ip_version_and_header_length = |
| 455 | IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS; |
| 456 | ip4->tos = ip6_translate_tos (ip6); |
| 457 | ip4->length = u16_net_add (ip6->payload_length, |
| 458 | sizeof (*ip4) - l4_offset + sizeof (*ip6)); |
| 459 | ip4->fragment_id = frag_id; |
| 460 | ip4->flags_and_fragment_offset = |
| 461 | clib_host_to_net_u16 (frag_offset | |
| 462 | (frag_more ? IP4_HEADER_FLAG_MORE_FRAGMENTS : 0)); |
| 463 | ip4->ttl = ip6->hop_limit; |
| 464 | ip4->protocol = |
| 465 | (l4_protocol == IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : l4_protocol; |
| 466 | ip4->checksum = ip4_header_checksum (ip4); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * @brief Translate IPv6 UDP/TCP packet to IPv4. |
| 473 | * |
| 474 | * @param p Buffer to translate. |
| 475 | * @param fn The function to translate header. |
| 476 | * @param ctx A context passed in the header translate function. |
| 477 | * |
| 478 | * @returns 0 on success, non-zero value otherwise. |
| 479 | */ |
| 480 | always_inline int |
| 481 | ip6_to_ip4_tcp_udp (vlib_buffer_t * p, ip6_to_ip4_set_fn_t fn, void *ctx, |
| 482 | u8 udp_checksum) |
| 483 | { |
| 484 | ip6_header_t *ip6; |
| 485 | u16 *checksum; |
Matus Fabian | b1291e2 | 2017-05-05 04:57:16 -0700 | [diff] [blame] | 486 | ip_csum_t csum = 0; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 487 | ip4_header_t *ip4; |
| 488 | u16 fragment_id; |
| 489 | u16 flags; |
| 490 | u16 frag_offset; |
| 491 | u8 l4_protocol; |
| 492 | u16 l4_offset; |
| 493 | int rv; |
Matus Fabian | c8e294b | 2017-11-08 00:18:15 -0800 | [diff] [blame] | 494 | ip6_address_t old_src, old_dst; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 495 | |
| 496 | ip6 = vlib_buffer_get_current (p); |
| 497 | |
| 498 | if (ip6_parse |
| 499 | (ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset)) |
| 500 | return -1; |
| 501 | |
| 502 | if (l4_protocol == IP_PROTOCOL_TCP) |
| 503 | { |
| 504 | tcp_header_t *tcp = ip6_next_header (ip6); |
| 505 | checksum = &tcp->checksum; |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | udp_header_t *udp = ip6_next_header (ip6); |
| 510 | checksum = &udp->checksum; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Matus Fabian | c8e294b | 2017-11-08 00:18:15 -0800 | [diff] [blame] | 513 | old_src.as_u64[0] = ip6->src_address.as_u64[0]; |
| 514 | old_src.as_u64[1] = ip6->src_address.as_u64[1]; |
| 515 | old_dst.as_u64[0] = ip6->dst_address.as_u64[0]; |
| 516 | old_dst.as_u64[1] = ip6->dst_address.as_u64[1]; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 517 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 518 | ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4)); |
| 519 | |
| 520 | vlib_buffer_advance (p, l4_offset - sizeof (*ip4)); |
| 521 | |
| 522 | if (PREDICT_FALSE (frag_offset)) |
| 523 | { |
| 524 | //Only the first fragment |
| 525 | ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset); |
| 526 | fragment_id = frag_id_6to4 (hdr->identification); |
| 527 | flags = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS); |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | fragment_id = 0; |
| 532 | flags = 0; |
| 533 | } |
| 534 | |
| 535 | if ((rv = fn (ip6, ip4, ctx)) != 0) |
| 536 | return rv; |
| 537 | |
| 538 | ip4->ip_version_and_header_length = |
| 539 | IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS; |
| 540 | ip4->tos = ip6_translate_tos (ip6); |
| 541 | ip4->length = u16_net_add (ip6->payload_length, |
| 542 | sizeof (*ip4) + sizeof (*ip6) - l4_offset); |
| 543 | ip4->fragment_id = fragment_id; |
| 544 | ip4->flags_and_fragment_offset = flags; |
| 545 | ip4->ttl = ip6->hop_limit; |
| 546 | ip4->protocol = l4_protocol; |
| 547 | ip4->checksum = ip4_header_checksum (ip4); |
| 548 | |
| 549 | //UDP checksum is optional over IPv4 |
| 550 | if (!udp_checksum && l4_protocol == IP_PROTOCOL_UDP) |
| 551 | { |
| 552 | *checksum = 0; |
| 553 | } |
| 554 | else |
| 555 | { |
Matus Fabian | c8e294b | 2017-11-08 00:18:15 -0800 | [diff] [blame] | 556 | csum = ip_csum_sub_even (*checksum, old_src.as_u64[0]); |
| 557 | csum = ip_csum_sub_even (csum, old_src.as_u64[1]); |
| 558 | csum = ip_csum_sub_even (csum, old_dst.as_u64[0]); |
| 559 | csum = ip_csum_sub_even (csum, old_dst.as_u64[1]); |
| 560 | csum = ip_csum_add_even (csum, ip4->dst_address.as_u32); |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 561 | csum = ip_csum_add_even (csum, ip4->src_address.as_u32); |
| 562 | *checksum = ip_csum_fold (csum); |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
Matus Fabian | f8cd581 | 2017-07-11 03:55:02 -0700 | [diff] [blame] | 568 | /** |
| 569 | * @brief Translate IPv6 packet to IPv4 (IP header only). |
| 570 | * |
| 571 | * @param p Buffer to translate. |
| 572 | * @param fn The function to translate header. |
| 573 | * @param ctx A context passed in the header translate function. |
| 574 | * |
| 575 | * @returns 0 on success, non-zero value otherwise. |
| 576 | */ |
| 577 | always_inline int |
| 578 | ip6_to_ip4 (vlib_buffer_t * p, ip6_to_ip4_set_fn_t fn, void *ctx) |
| 579 | { |
| 580 | ip6_header_t *ip6; |
| 581 | ip4_header_t *ip4; |
| 582 | u16 fragment_id; |
| 583 | u16 flags; |
| 584 | u16 frag_offset; |
| 585 | u8 l4_protocol; |
| 586 | u16 l4_offset; |
| 587 | int rv; |
| 588 | |
| 589 | ip6 = vlib_buffer_get_current (p); |
| 590 | |
| 591 | if (ip6_parse |
| 592 | (ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset)) |
| 593 | return -1; |
| 594 | |
| 595 | ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4)); |
| 596 | |
| 597 | vlib_buffer_advance (p, l4_offset - sizeof (*ip4)); |
| 598 | |
| 599 | if (PREDICT_FALSE (frag_offset)) |
| 600 | { |
| 601 | //Only the first fragment |
| 602 | ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset); |
| 603 | fragment_id = frag_id_6to4 (hdr->identification); |
| 604 | flags = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS); |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | fragment_id = 0; |
| 609 | flags = 0; |
| 610 | } |
| 611 | |
| 612 | if ((rv = fn (ip6, ip4, ctx)) != 0) |
| 613 | return rv; |
| 614 | |
| 615 | ip4->ip_version_and_header_length = |
| 616 | IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS; |
| 617 | ip4->tos = ip6_translate_tos (ip6); |
| 618 | ip4->length = u16_net_add (ip6->payload_length, |
| 619 | sizeof (*ip4) + sizeof (*ip6) - l4_offset); |
| 620 | ip4->fragment_id = fragment_id; |
| 621 | ip4->flags_and_fragment_offset = flags; |
| 622 | ip4->ttl = ip6->hop_limit; |
| 623 | ip4->protocol = l4_protocol; |
| 624 | ip4->checksum = ip4_header_checksum (ip4); |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame] | 629 | #endif /* __included_ip6_to_ip4_h__ */ |
| 630 | |
| 631 | /* |
| 632 | * fd.io coding-style-patch-verification: ON |
| 633 | * |
| 634 | * Local Variables: |
| 635 | * eval: (c-set-style "gnu") |
| 636 | * End: |
| 637 | */ |