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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief IPv6 to IPv4 translation
19 #ifndef __included_ip6_to_ip4_h__
20 #define __included_ip6_to_ip4_h__
22 #include <vnet/ip/ip.h>
25 * IPv6 to IPv4 set call back function type
27 typedef int (*ip6_to_ip4_set_fn_t) (ip6_header_t * ip6, ip4_header_t * ip4,
31 static u8 icmp6_to_icmp_updater_pointer_table[] =
45 #define frag_id_6to4(id) ((id) ^ ((id) >> 16))
48 * @brief Parse some useful information from IPv6 header.
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.
56 * @returns 0 on success, non-zero value otherwise.
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)
62 if (ip6->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
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);
70 *l4_protocol = ip6->protocol;
72 *l4_offset = sizeof (*ip6);
75 return (buff_len < (*l4_offset + 4)) ||
76 (clib_net_to_host_u16 (ip6->payload_length) <
77 (*l4_offset + 4 - sizeof (*ip6)));
81 * @brief Get TCP/UDP port number or ICMP id from IPv6 packet.
83 * @param ip6 IPv6 header.
84 * @param sender 1 get sender port, 0 get receiver port.
85 * @param buffer_len Buffer length.
87 * @returns Port number on success, 0 otherwise.
90 ip6_get_port (ip6_header_t * ip6, u8 sender, u16 buffer_len)
97 if (ip6_parse (ip6, buffer_len, &l4_protocol, &l4_offset, &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
105 l4 = u8_ptr_add (ip6, l4_offset);
106 if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
108 return (sender) ? ((udp_header_t *) (l4))->src_port : ((udp_header_t
112 else if (l4_protocol == IP_PROTOCOL_ICMP6)
114 icmp46_header_t *icmp = (icmp46_header_t *) (l4);
115 if (icmp->type == ICMP6_echo_request)
117 return (sender) ? ((u16 *) (icmp))[2] : -1;
119 else if (icmp->type == ICMP6_echo_reply)
121 return (sender) ? -1 : ((u16 *) (icmp))[2];
128 * @brief Convert type and code value from ICMP6 to ICMP4.
130 * @param icmp ICMP header.
131 * @param inner_ip6 Inner IPv6 header if present, 0 otherwise.
133 * @returns 0 on success, non-zero value otherwise.
135 static_always_inline int
136 icmp6_to_icmp_header (icmp46_header_t * icmp, ip6_header_t ** inner_ip6)
141 case ICMP6_echo_request:
142 icmp->type = ICMP4_echo_request;
144 case ICMP6_echo_reply:
145 icmp->type = ICMP4_echo_reply;
147 case ICMP6_destination_unreachable:
148 *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
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;
157 ICMP4_destination_unreachable_destination_unreachable_host;
159 case ICMP6_destination_unreachable_destination_administratively_prohibited: //1
161 ICMP4_destination_unreachable;
163 ICMP4_destination_unreachable_communication_administratively_prohibited;
165 case ICMP6_destination_unreachable_port_unreachable:
166 icmp->type = ICMP4_destination_unreachable;
167 icmp->code = ICMP4_destination_unreachable_port_unreachable;
173 case ICMP6_packet_too_big:
174 *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
176 icmp->type = ICMP4_destination_unreachable;
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);
186 case ICMP6_time_exceeded:
187 *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
189 icmp->type = ICMP4_time_exceeded;
192 case ICMP6_parameter_problem:
193 *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
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)));
204 ((u8 *) (icmp + 1))[0] =
205 icmp6_to_icmp_updater_pointer_table[pointer];
207 case ICMP6_parameter_problem_unrecognized_next_header:
208 icmp->type = ICMP4_destination_unreachable;
209 icmp->code = ICMP4_destination_unreachable_port_unreachable;
211 case ICMP6_parameter_problem_unrecognized_option:
224 * @brief Translate TOS value from IPv6 to IPv4.
226 * @param ip6 IPv6 header.
228 * @returns IPv4 TOS value.
230 static_always_inline u8
231 ip6_translate_tos (const ip6_header_t * ip6)
233 return (clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label)
238 * @brief Translate ICMP6 packet to ICMP4.
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.
246 * @returns 0 on success, non-zero value otherwise.
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)
252 ip6_header_t *ip6, *inner_ip6;
253 ip4_header_t *ip4, *inner_ip4;
255 icmp46_header_t *icmp;
259 ip6 = vlib_buffer_get_current (p);
260 ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length);
261 icmp = (icmp46_header_t *) (ip6 + 1);
262 ASSERT (ip6_pay_len + sizeof (*ip6) <= p->current_length);
264 //No extensions headers allowed here
265 if (ip6->protocol != IP_PROTOCOL_ICMP6)
268 //There are no fragmented ICMP messages, so no extension header for now
269 if (icmp6_to_icmp_header (icmp, &inner_ip6))
274 u16 *inner_L4_checksum, inner_l4_offset, inner_frag_offset,
276 u8 *inner_l4, inner_protocol;
278 //We have two headers to translate
280 // [ IPv6 ]<- ext ->[IC][ IPv6 ]<- ext ->[L4 header ...
282 // [ IPv6 ][IC][ IPv6 ][L4 header ...
283 // [ IPv6 ][IC][ IPv6 ][Fr][L4 header ...
285 // [ IPv4][IC][ IPv4][L4 header ...
287 if (ip6_parse (inner_ip6, ip6_pay_len - 8,
288 &inner_protocol, &inner_l4_offset, &inner_frag_offset))
291 inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset);
293 (ip4_header_t *) u8_ptr_add (inner_l4, -sizeof (*inner_ip4));
294 if (inner_frag_offset)
296 ip6_frag_hdr_t *inner_frag =
297 (ip6_frag_hdr_t *) u8_ptr_add (inner_ip6, inner_frag_offset);
298 inner_frag_id = frag_id_6to4 (inner_frag->identification);
305 //Do the translation of the inner packet
306 if (inner_protocol == IP_PROTOCOL_TCP)
308 inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 16);
310 else if (inner_protocol == IP_PROTOCOL_UDP)
312 inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 6);
314 else if (inner_protocol == IP_PROTOCOL_ICMP6)
316 icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
317 csum = inner_icmp->checksum;
318 csum = ip_csum_sub_even (csum, *((u16 *) inner_icmp));
319 //It cannot be of a different type as ip6_icmp_to_icmp6_in_place succeeded
320 inner_icmp->type = (inner_icmp->type == ICMP6_echo_request) ?
321 ICMP4_echo_request : ICMP4_echo_reply;
322 csum = ip_csum_add_even (csum, *((u16 *) inner_icmp));
323 inner_icmp->checksum = ip_csum_fold (csum);
324 inner_protocol = IP_PROTOCOL_ICMP; //Will be copied to ip6 later
325 inner_L4_checksum = &inner_icmp->checksum;
332 csum = *inner_L4_checksum;
333 csum = ip_csum_sub_even (csum, inner_ip6->src_address.as_u64[0]);
334 csum = ip_csum_sub_even (csum, inner_ip6->src_address.as_u64[1]);
335 csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[0]);
336 csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[1]);
338 if ((rv = inner_fn (inner_ip6, inner_ip4, inner_ctx)) != 0)
341 inner_ip4->ip_version_and_header_length =
342 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
343 inner_ip4->tos = ip6_translate_tos (inner_ip6);
345 u16_net_add (inner_ip6->payload_length,
346 sizeof (*ip4) + sizeof (*ip6) - inner_l4_offset);
347 inner_ip4->fragment_id = inner_frag_id;
348 inner_ip4->flags_and_fragment_offset =
349 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
350 inner_ip4->ttl = inner_ip6->hop_limit;
351 inner_ip4->protocol = inner_protocol;
352 inner_ip4->checksum = ip4_header_checksum (inner_ip4);
354 if (inner_ip4->protocol == IP_PROTOCOL_ICMP)
356 //Remove remainings of the pseudo-header in the csum
358 ip_csum_sub_even (csum, clib_host_to_net_u16 (IP_PROTOCOL_ICMP6));
360 ip_csum_sub_even (csum, inner_ip4->length - sizeof (*inner_ip4));
364 //Update to new pseudo-header
365 csum = ip_csum_add_even (csum, inner_ip4->src_address.as_u32);
366 csum = ip_csum_add_even (csum, inner_ip4->dst_address.as_u32);
368 *inner_L4_checksum = ip_csum_fold (csum);
370 //Move up icmp header
371 ip4 = (ip4_header_t *) u8_ptr_add (inner_l4, -2 * sizeof (*ip4) - 8);
372 clib_memcpy (u8_ptr_add (inner_l4, -sizeof (*ip4) - 8), icmp, 8);
373 icmp = (icmp46_header_t *) u8_ptr_add (inner_l4, -sizeof (*ip4) - 8);
377 //Only one header to translate
378 ip4 = (ip4_header_t *) u8_ptr_add (ip6, sizeof (*ip6) - sizeof (*ip4));
381 vlib_buffer_advance (p, (u32) (((u8 *) ip4) - ((u8 *) ip6)));
383 if ((rv = fn (ip6, ip4, ctx)) != 0)
386 ip4->ip_version_and_header_length =
387 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
388 ip4->tos = ip6_translate_tos (ip6);
389 ip4->fragment_id = 0;
390 ip4->flags_and_fragment_offset = 0;
391 ip4->ttl = ip6->hop_limit;
392 ip4->protocol = IP_PROTOCOL_ICMP;
393 //TODO fix the length depending on offset length
394 ip4->length = u16_net_add (ip6->payload_length,
396 NULL) ? sizeof (*ip4) : (2 * sizeof (*ip4) -
398 ip4->checksum = ip4_header_checksum (ip4);
400 //Recompute ICMP checksum
403 ip_incremental_checksum (0, icmp,
404 clib_net_to_host_u16 (ip4->length) -
406 icmp->checksum = ~ip_csum_fold (csum);
412 * @brief Translate IPv6 fragmented packet to IPv4.
414 * @param p Buffer to translate.
415 * @param fn The function to translate header.
416 * @param ctx A context passed in the header translate function.
418 * @returns 0 on success, non-zero value otherwise.
421 ip6_to_ip4_fragmented (vlib_buffer_t * p, ip6_to_ip4_set_fn_t fn, void *ctx)
424 ip6_frag_hdr_t *frag;
433 ip6 = vlib_buffer_get_current (p);
436 (ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset))
439 frag = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset);
440 ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4));
441 vlib_buffer_advance (p, l4_offset - sizeof (*ip4));
443 frag_id = frag_id_6to4 (frag->identification);
444 frag_more = ip6_frag_hdr_more (frag);
445 frag_offset = ip6_frag_hdr_offset (frag);
447 if ((rv = fn (ip6, ip4, ctx)) != 0)
450 ip4->ip_version_and_header_length =
451 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
452 ip4->tos = ip6_translate_tos (ip6);
453 ip4->length = u16_net_add (ip6->payload_length,
454 sizeof (*ip4) - l4_offset + sizeof (*ip6));
455 ip4->fragment_id = frag_id;
456 ip4->flags_and_fragment_offset =
457 clib_host_to_net_u16 (frag_offset |
458 (frag_more ? IP4_HEADER_FLAG_MORE_FRAGMENTS : 0));
459 ip4->ttl = ip6->hop_limit;
461 (l4_protocol == IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : l4_protocol;
462 ip4->checksum = ip4_header_checksum (ip4);
468 * @brief Translate IPv6 UDP/TCP packet to IPv4.
470 * @param p Buffer to translate.
471 * @param fn The function to translate header.
472 * @param ctx A context passed in the header translate function.
474 * @returns 0 on success, non-zero value otherwise.
477 ip6_to_ip4_tcp_udp (vlib_buffer_t * p, ip6_to_ip4_set_fn_t fn, void *ctx,
491 ip6 = vlib_buffer_get_current (p);
494 (ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset))
497 if (l4_protocol == IP_PROTOCOL_TCP)
499 tcp_header_t *tcp = ip6_next_header (ip6);
500 checksum = &tcp->checksum;
504 udp_header_t *udp = ip6_next_header (ip6);
505 checksum = &udp->checksum;
506 //UDP checksum is optional over IPv4
511 csum = ip_csum_sub_even (*checksum, ip6->src_address.as_u64[0]);
512 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
513 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
514 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
517 ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4));
519 vlib_buffer_advance (p, l4_offset - sizeof (*ip4));
521 if (PREDICT_FALSE (frag_offset))
523 //Only the first fragment
524 ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset);
525 fragment_id = frag_id_6to4 (hdr->identification);
526 flags = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
534 if ((rv = fn (ip6, ip4, ctx)) != 0)
537 ip4->ip_version_and_header_length =
538 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
539 ip4->tos = ip6_translate_tos (ip6);
540 ip4->length = u16_net_add (ip6->payload_length,
541 sizeof (*ip4) + sizeof (*ip6) - l4_offset);
542 ip4->fragment_id = fragment_id;
543 ip4->flags_and_fragment_offset = flags;
544 ip4->ttl = ip6->hop_limit;
545 ip4->protocol = l4_protocol;
546 ip4->checksum = ip4_header_checksum (ip4);
548 //UDP checksum is optional over IPv4
549 if (!udp_checksum && l4_protocol == IP_PROTOCOL_UDP)
555 csum = ip_csum_add_even (csum, ip4->dst_address.as_u32);
556 csum = ip_csum_add_even (csum, ip4->src_address.as_u32);
557 *checksum = ip_csum_fold (csum);
563 #endif /* __included_ip6_to_ip4_h__ */
566 * fd.io coding-style-patch-verification: ON
569 * eval: (c-set-style "gnu")