19 #ifndef __included_ip6_to_ip4_h__ 20 #define __included_ip6_to_ip4_h__ 45 #define frag_id_6to4(id) ((id) ^ ((id) >> 16)) 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);
72 *l4_offset =
sizeof (*ip6);
75 return (buff_len < (*l4_offset + 4)) ||
77 (*l4_offset + 4 -
sizeof (*ip6)));
97 if (
ip6_parse (ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
106 if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
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];
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:
152 case ICMP6_destination_unreachable_no_route_to_destination:
153 case ICMP6_destination_unreachable_beyond_scope_of_source_address:
154 case ICMP6_destination_unreachable_address_unreachable:
155 icmp->type = ICMP4_destination_unreachable;
157 ICMP4_destination_unreachable_destination_unreachable_host;
159 case ICMP6_destination_unreachable_destination_administratively_prohibited:
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:
176 icmp->type = ICMP4_destination_unreachable;
179 u32 advertised_mtu = clib_net_to_host_u32 (*((
u32 *) (icmp + 1)));
180 advertised_mtu -= 20;
182 ((
u16 *) (icmp))[3] = clib_host_to_net_u16 (advertised_mtu);
186 case ICMP6_time_exceeded:
189 icmp->type = ICMP4_time_exceeded;
192 case ICMP6_parameter_problem:
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] =
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:
255 icmp46_header_t *icmp;
261 icmp = (icmp46_header_t *) (ip6 + 1);
265 if (ip6->
protocol != IP_PROTOCOL_ICMP6)
274 u16 *inner_L4_checksum, inner_l4_offset, inner_frag_offset,
276 u8 *inner_l4, inner_protocol;
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);
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);
306 if (inner_protocol == IP_PROTOCOL_TCP)
310 else if (inner_protocol == IP_PROTOCOL_UDP)
314 else if (inner_protocol == IP_PROTOCOL_ICMP6)
316 icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
318 inner_icmp->type = (inner_icmp->type == ICMP6_echo_request) ?
319 ICMP4_echo_request : ICMP4_echo_reply;
320 inner_protocol = IP_PROTOCOL_ICMP;
321 inner_L4_checksum = &inner_icmp->checksum;
328 csum = *inner_L4_checksum;
335 if ((rv = inner_fn (inner_ip6, inner_ip4, inner_ctx)) != 0)
343 sizeof (*ip4) +
sizeof (*ip6) - inner_l4_offset);
348 inner_ip4->
protocol = inner_protocol;
351 if (inner_ip4->
protocol == IP_PROTOCOL_ICMP)
354 icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
355 inner_icmp->checksum = 0;
358 clib_net_to_host_u16 (inner_ip4->
length)
359 - sizeof (*inner_ip4));
365 csum = *inner_L4_checksum;
374 icmp = (icmp46_header_t *)
u8_ptr_add (inner_l4, -
sizeof (*ip4) - 8);
384 if ((rv = fn (ip6, ip4, ctx)) != 0)
397 NULL) ? sizeof (*ip4) : (2 *
sizeof (*ip4) -
405 clib_net_to_host_u16 (ip4->
length) -
425 ip6_frag_hdr_t *frag;
440 frag = (ip6_frag_hdr_t *)
u8_ptr_add (ip6, frag_offset);
448 if ((rv = fn (ip6, ip4, ctx)) != 0)
455 sizeof (*ip4) - l4_offset +
sizeof (*ip6));
458 clib_host_to_net_u16 (frag_offset |
462 (l4_protocol == IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : l4_protocol;
498 if (l4_protocol == IP_PROTOCOL_TCP)
501 checksum = &tcp->checksum;
526 ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *)
u8_ptr_add (ip6, frag_offset);
536 if ((rv = fn (ip6, ip4, ctx)) != 0)
543 sizeof (*ip4) +
sizeof (*ip6) - l4_offset);
551 if (!udp_checksum && l4_protocol == IP_PROTOCOL_UDP)
static_always_inline u8 ip6_translate_tos(const ip6_header_t *ip6)
Translate TOS value from IPv6 to IPv4.
static u16 ip6_get_port(ip6_header_t *ip6, u8 sender, u16 buffer_len)
Get TCP/UDP port number or ICMP id from IPv6 packet.
u16 udp_checksum(udp_header_t *uh, u32 udp_len, void *ih, u8 version)
static int icmp6_to_icmp(vlib_buffer_t *p, ip6_to_ip4_set_fn_t fn, void *ctx, ip6_to_ip4_set_fn_t inner_fn, void *inner_ctx)
Translate ICMP6 packet to ICMP4.
struct _tcp_header tcp_header_t
#define u16_net_add(u, val)
static_always_inline int icmp6_to_icmp_header(icmp46_header_t *icmp, ip6_header_t **inner_ip6)
Convert type and code value from ICMP6 to ICMP4.
#define static_always_inline
ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
static_always_inline int ip6_parse(const ip6_header_t *ip6, u32 buff_len, u8 *l4_protocol, u16 *l4_offset, u16 *frag_hdr_offset)
Parse some useful information from IPv6 header.
static int ip6_to_ip4_tcp_udp(vlib_buffer_t *p, ip6_to_ip4_set_fn_t fn, void *ctx, u8 udp_checksum)
Translate IPv6 UDP/TCP packet to IPv4.
#define ip6_frag_hdr_more(hdr)
u16 current_length
Nbytes between current data and the end of this buffer.
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
#define IP4_HEADER_FLAG_MORE_FRAGMENTS
#define clib_memcpy(a, b, c)
static void * ip6_next_header(ip6_header_t *i)
#define ip6_frag_hdr_offset(hdr)
#define u8_ptr_add(ptr, index)
static ip_csum_t ip_csum_sub_even(ip_csum_t c, ip_csum_t x)
static u8 icmp6_to_icmp_updater_pointer_table[]
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
#define IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS
int(* ip6_to_ip4_set_fn_t)(ip6_header_t *ip6, ip4_header_t *ip4, void *ctx)
IPv6 to IPv4 set call back function type.
static u16 ip4_header_checksum(ip4_header_t *i)
static u16 ip_csum_fold(ip_csum_t c)
static ip_csum_t ip_csum_add_even(ip_csum_t c, ip_csum_t x)
static int ip6_to_ip4_fragmented(vlib_buffer_t *p, ip6_to_ip4_set_fn_t fn, void *ctx)
Translate IPv6 fragmented packet to IPv4.