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_icmp_set_fn_t) (ip6_header_t * ip6,
28 ip4_header_t * ip4, void *ctx);
30 typedef int (*ip6_to_ip4_tcp_udp_set_fn_t) (vlib_buffer_t * b,
32 ip4_header_t * ip4, void *ctx);
34 static u8 icmp6_to_icmp_updater_pointer_table[] =
47 #define frag_id_6to4(id) ((id) ^ ((id) >> 16))
50 * @brief Parse some useful information from IPv6 header.
53 * @param b vlib buffer
54 * @param ip6 IPv6 header.
55 * @param buff_len Buffer length.
56 * @param l4_protocol L4 protocol number.
57 * @param l4_offset L4 header offset.
58 * @param frag_hdr_offset Fragment header offset if present, 0 otherwise.
60 * @returns 0 on success, non-zero value otherwise.
62 static_always_inline int
63 ip6_parse (vlib_main_t *vm, vlib_buffer_t *b, ip6_header_t *ip6, u32 buff_len,
64 u8 *l4_protocol, u16 *l4_offset, u16 *frag_hdr_offset)
66 ip6_ext_hdr_chain_t hdr_chain;
68 ip6_ext_header_walk (b, ip6, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
73 if (hdr_chain.eh[res].protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
74 *frag_hdr_offset = hdr_chain.eh[res].offset;
78 *l4_protocol = hdr_chain.eh[hdr_chain.length - 1].protocol;
79 *l4_offset = hdr_chain.eh[hdr_chain.length - 1].offset;
85 * @brief Get L4 information like port number or ICMP id from IPv6 packet.
87 * @param ip6 IPv6 header.
88 * @param buffer_len Buffer length.
89 * @param ip_protocol L4 protocol
90 * @param src_port L4 src port or icmp id
91 * @param dst_post L4 dst port or icmp id
92 * @param icmp_type_or_tcp_flags ICMP type or TCP flags, if applicable
93 * @param tcp_ack_number TCP ack number, if applicable
94 * @param tcp_seq_number TCP seq number, if applicable
96 * @returns 1 on success, 0 otherwise.
99 ip6_get_port (vlib_main_t * vm, vlib_buffer_t * b, ip6_header_t * ip6,
100 u16 buffer_len, u8 * ip_protocol, u16 * src_port,
101 u16 * dst_port, u8 * icmp_type_or_tcp_flags,
102 u32 * tcp_ack_number, u32 * tcp_seq_number)
109 if (ip6_parse (vm, b, ip6, buffer_len, &l4_protocol, &l4_offset,
115 ip6_frag_hdr_offset (((ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset))))
116 return 0; //Can't deal with non-first fragment for now
120 *ip_protocol = l4_protocol;
122 l4 = u8_ptr_add (ip6, l4_offset);
123 if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
126 *src_port = ((udp_header_t *) (l4))->src_port;
128 *dst_port = ((udp_header_t *) (l4))->dst_port;
129 if (icmp_type_or_tcp_flags && l4_protocol == IP_PROTOCOL_TCP)
130 *icmp_type_or_tcp_flags = ((tcp_header_t *) (l4))->flags;
131 if (tcp_ack_number && l4_protocol == IP_PROTOCOL_TCP)
132 *tcp_ack_number = ((tcp_header_t *) (l4))->ack_number;
133 if (tcp_seq_number && l4_protocol == IP_PROTOCOL_TCP)
134 *tcp_seq_number = ((tcp_header_t *) (l4))->seq_number;
136 else if (l4_protocol == IP_PROTOCOL_ICMP6)
138 icmp46_header_t *icmp = (icmp46_header_t *) (l4);
139 if (icmp_type_or_tcp_flags)
140 *icmp_type_or_tcp_flags = ((icmp46_header_t *) (l4))->type;
141 if (icmp->type == ICMP6_echo_request)
144 *src_port = ((u16 *) (icmp))[2];
146 *dst_port = ((u16 *) (icmp))[2];
148 else if (icmp->type == ICMP6_echo_reply)
151 *src_port = ((u16 *) (icmp))[2];
153 *dst_port = ((u16 *) (icmp))[2];
155 else if (clib_net_to_host_u16 (ip6->payload_length) >= 64)
158 ip6_header_t *inner_ip6;
159 u8 inner_l4_protocol;
161 u16 inner_frag_offset;
164 ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length);
165 inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
167 if (ip6_parse (vm, b, inner_ip6, ip6_pay_len - 8,
168 &inner_l4_protocol, &inner_l4_offset,
172 if (inner_frag_offset &&
173 ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
174 u8_ptr_add (inner_ip6,
175 inner_frag_offset))))
178 inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset);
179 if (inner_l4_protocol == IP_PROTOCOL_TCP ||
180 inner_l4_protocol == IP_PROTOCOL_UDP)
183 *src_port = ((udp_header_t *) (inner_l4))->dst_port;
185 *dst_port = ((udp_header_t *) (inner_l4))->src_port;
187 else if (inner_l4_protocol == IP_PROTOCOL_ICMP6)
189 icmp46_header_t *inner_icmp = (icmp46_header_t *) (inner_l4);
190 if (inner_icmp->type == ICMP6_echo_request)
193 *src_port = ((u16 *) (inner_icmp))[2];
195 *dst_port = ((u16 *) (inner_icmp))[2];
197 else if (inner_icmp->type == ICMP6_echo_reply)
200 *src_port = ((u16 *) (inner_icmp))[2];
202 *dst_port = ((u16 *) (inner_icmp))[2];
211 * @brief Convert type and code value from ICMP6 to ICMP4.
213 * @param icmp ICMP header.
214 * @param inner_ip6 Inner IPv6 header if present, 0 otherwise.
216 * @returns 0 on success, non-zero value otherwise.
218 static_always_inline int
219 icmp6_to_icmp_header (icmp46_header_t * icmp, ip6_header_t ** inner_ip6)
224 case ICMP6_echo_request:
225 icmp->type = ICMP4_echo_request;
227 case ICMP6_echo_reply:
228 icmp->type = ICMP4_echo_reply;
230 case ICMP6_destination_unreachable:
231 *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
235 case ICMP6_destination_unreachable_no_route_to_destination: //0
236 case ICMP6_destination_unreachable_beyond_scope_of_source_address: //2
237 case ICMP6_destination_unreachable_address_unreachable: //3
238 icmp->type = ICMP4_destination_unreachable;
240 ICMP4_destination_unreachable_destination_unreachable_host;
242 case ICMP6_destination_unreachable_destination_administratively_prohibited: //1
244 ICMP4_destination_unreachable;
246 ICMP4_destination_unreachable_communication_administratively_prohibited;
248 case ICMP6_destination_unreachable_port_unreachable:
249 icmp->type = ICMP4_destination_unreachable;
250 icmp->code = ICMP4_destination_unreachable_port_unreachable;
256 case ICMP6_packet_too_big:
257 *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
259 icmp->type = ICMP4_destination_unreachable;
262 u32 advertised_mtu = clib_net_to_host_u32 (*((u32 *) (icmp + 1)));
263 advertised_mtu -= 20;
264 //FIXME: = minimum(advertised MTU-20, MTU_of_IPv4_nexthop, (MTU_of_IPv6_nexthop)-20)
265 ((u16 *) (icmp))[3] = clib_host_to_net_u16 (advertised_mtu);
269 case ICMP6_time_exceeded:
270 *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
272 icmp->type = ICMP4_time_exceeded;
275 case ICMP6_parameter_problem:
276 *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
280 case ICMP6_parameter_problem_erroneous_header_field:
281 icmp->type = ICMP4_parameter_problem;
282 icmp->code = ICMP4_parameter_problem_pointer_indicates_error;
283 u32 pointer = clib_net_to_host_u32 (*((u32 *) (icmp + 1)));
287 ((u8 *) (icmp + 1))[0] =
288 icmp6_to_icmp_updater_pointer_table[pointer];
290 case ICMP6_parameter_problem_unrecognized_next_header:
291 icmp->type = ICMP4_destination_unreachable;
292 icmp->code = ICMP4_destination_unreachable_port_unreachable;
294 case ICMP6_parameter_problem_unrecognized_option:
307 * @brief Translate TOS value from IPv6 to IPv4.
309 * @param ip_version_traffic_class_and_flow_label in network byte order
311 * @returns IPv4 TOS value.
313 static_always_inline u8
314 ip6_translate_tos (u32 ip_version_traffic_class_and_flow_label)
316 return (clib_net_to_host_u32 (ip_version_traffic_class_and_flow_label)
321 * @brief Translate ICMP6 packet to ICMP4.
323 * @param p Buffer to translate.
324 * @param fn The function to translate outer header.
325 * @param ctx A context passed in the outer header translate function.
326 * @param inner_fn The function to translate inner header.
327 * @param inner_ctx A context passed in the inner header translate function.
329 * @returns 0 on success, non-zero value otherwise.
332 icmp6_to_icmp (vlib_main_t * vm, vlib_buffer_t * p,
333 ip6_to_ip4_icmp_set_fn_t fn, void *ctx,
334 ip6_to_ip4_icmp_set_fn_t inner_fn, void *inner_ctx)
336 ip6_header_t *ip6, *inner_ip6;
337 ip4_header_t *ip4, *inner_ip4;
339 icmp46_header_t *icmp;
342 ip6_address_t old_src, old_dst;
344 ip6 = vlib_buffer_get_current (p);
345 ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length);
346 icmp = (icmp46_header_t *) (ip6 + 1);
347 ASSERT (ip6_pay_len + sizeof (*ip6) <= p->current_length);
349 //No extensions headers allowed here
350 if (ip6->protocol != IP_PROTOCOL_ICMP6)
353 //There are no fragmented ICMP messages, so no extension header for now
354 if (icmp6_to_icmp_header (icmp, &inner_ip6))
359 u16 *inner_L4_checksum, inner_l4_offset, inner_frag_offset,
361 u8 *inner_l4, inner_protocol;
363 //We have two headers to translate
365 // [ IPv6 ]<- ext ->[IC][ IPv6 ]<- ext ->[L4 header ...
367 // [ IPv6 ][IC][ IPv6 ][L4 header ...
368 // [ IPv6 ][IC][ IPv6 ][Fr][L4 header ...
370 // [ IPv4][IC][ IPv4][L4 header ...
372 if (ip6_parse (vm, p, inner_ip6, ip6_pay_len - 8,
373 &inner_protocol, &inner_l4_offset, &inner_frag_offset))
376 inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset);
378 (ip4_header_t *) u8_ptr_add (inner_l4, -sizeof (*inner_ip4));
379 if (inner_frag_offset)
381 ip6_frag_hdr_t *inner_frag =
382 (ip6_frag_hdr_t *) u8_ptr_add (inner_ip6, inner_frag_offset);
383 inner_frag_id = frag_id_6to4 (inner_frag->identification);
390 //Do the translation of the inner packet
391 if (inner_protocol == IP_PROTOCOL_TCP)
393 inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 16);
395 else if (inner_protocol == IP_PROTOCOL_UDP)
397 inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 6);
399 else if (inner_protocol == IP_PROTOCOL_ICMP6)
401 icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
402 //It cannot be of a different type as ip6_icmp_to_icmp6_in_place succeeded
403 inner_icmp->type = (inner_icmp->type == ICMP6_echo_request) ?
404 ICMP4_echo_request : ICMP4_echo_reply;
405 inner_protocol = IP_PROTOCOL_ICMP; //Will be copied to ip6 later
406 inner_L4_checksum = &inner_icmp->checksum;
413 old_src.as_u64[0] = inner_ip6->src_address.as_u64[0];
414 old_src.as_u64[1] = inner_ip6->src_address.as_u64[1];
415 old_dst.as_u64[0] = inner_ip6->dst_address.as_u64[0];
416 old_dst.as_u64[1] = inner_ip6->dst_address.as_u64[1];
418 if ((rv = inner_fn (inner_ip6, inner_ip4, inner_ctx)) != 0)
421 inner_ip4->ip_version_and_header_length =
422 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
425 (inner_ip6->ip_version_traffic_class_and_flow_label);
427 u16_net_add (inner_ip6->payload_length,
428 sizeof (*ip4) + sizeof (*ip6) - inner_l4_offset);
429 inner_ip4->fragment_id = inner_frag_id;
430 inner_ip4->flags_and_fragment_offset =
431 clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
432 inner_ip4->ttl = inner_ip6->hop_limit;
433 inner_ip4->protocol = inner_protocol;
434 inner_ip4->checksum = ip4_header_checksum (inner_ip4);
436 if (inner_ip4->protocol == IP_PROTOCOL_ICMP)
438 //Recompute ICMP checksum
439 icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
440 inner_icmp->checksum = 0;
442 ip_incremental_checksum (0, inner_icmp,
443 clib_net_to_host_u16 (inner_ip4->length)
444 - sizeof (*inner_ip4));
445 inner_icmp->checksum = ~ip_csum_fold (csum);
449 //Update to new pseudo-header
450 csum = *inner_L4_checksum;
451 csum = ip_csum_sub_even (csum, old_src.as_u64[0]);
452 csum = ip_csum_sub_even (csum, old_src.as_u64[1]);
453 csum = ip_csum_sub_even (csum, old_dst.as_u64[0]);
454 csum = ip_csum_sub_even (csum, old_dst.as_u64[1]);
455 csum = ip_csum_add_even (csum, inner_ip4->src_address.as_u32);
456 csum = ip_csum_add_even (csum, inner_ip4->dst_address.as_u32);
457 *inner_L4_checksum = ip_csum_fold (csum);
460 //Move up icmp header
461 ip4 = (ip4_header_t *) u8_ptr_add (inner_l4, -2 * sizeof (*ip4) - 8);
462 clib_memcpy_fast (u8_ptr_add (inner_l4, -sizeof (*ip4) - 8), icmp, 8);
463 icmp = (icmp46_header_t *) u8_ptr_add (inner_l4, -sizeof (*ip4) - 8);
467 //Only one header to translate
468 ip4 = (ip4_header_t *) u8_ptr_add (ip6, sizeof (*ip6) - sizeof (*ip4));
471 vlib_buffer_advance (p, (u32) (((u8 *) ip4) - ((u8 *) ip6)));
473 if ((rv = fn (ip6, ip4, ctx)) != 0)
476 ip4->ip_version_and_header_length =
477 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
478 ip4->tos = ip6_translate_tos (ip6->ip_version_traffic_class_and_flow_label);
479 ip4->fragment_id = 0;
480 ip4->flags_and_fragment_offset = 0;
481 ip4->ttl = ip6->hop_limit;
482 ip4->protocol = IP_PROTOCOL_ICMP;
483 //TODO fix the length depending on offset length
484 ip4->length = u16_net_add (ip6->payload_length,
486 NULL) ? sizeof (*ip4) : (2 * sizeof (*ip4) -
488 ip4->checksum = ip4_header_checksum (ip4);
490 //Recompute ICMP checksum
493 ip_incremental_checksum (0, icmp,
494 clib_net_to_host_u16 (ip4->length) -
496 icmp->checksum = ~ip_csum_fold (csum);
501 #endif /* __included_ip6_to_ip4_h__ */
504 * fd.io coding-style-patch-verification: ON
507 * eval: (c-set-style "gnu")