hs-test: fixed timed out tests passing in the CI
[vpp.git] / src / vnet / ip / ip6_to_ip4.h
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_icmp_set_fn_t) (ip6_header_t * ip6,
28                                          ip4_header_t * ip4, void *ctx);
29
30 typedef int (*ip6_to_ip4_tcp_udp_set_fn_t) (vlib_buffer_t * b,
31                                             ip6_header_t * ip6,
32                                             ip4_header_t * ip4, void *ctx);
33
34 static u8 icmp6_to_icmp_updater_pointer_table[] =
35   { 0, 1, ~0, ~0,
36     2, 2, 9, 8,
37     12, 12, 12, 12,
38     12, 12, 12, 12,
39     12, 12, 12, 12,
40     12, 12, 12, 12,
41     24, 24, 24, 24,
42     24, 24, 24, 24,
43     24, 24, 24, 24,
44     24, 24, 24, 24
45   };
46
47 #define frag_id_6to4(id) ((id) ^ ((id) >> 16))
48
49 /**
50  * @brief Parse some useful information from IPv6 header.
51  *
52  * @param vm              vlib main
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.
59  *
60  * @returns 0 on success, non-zero value otherwise.
61  */
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)
65 {
66   ip6_ext_hdr_chain_t hdr_chain;
67   int res =
68     ip6_ext_header_walk (b, ip6, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
69   if (res < 0)
70     {
71       return -1;
72     }
73   if (hdr_chain.eh[res].protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
74     *frag_hdr_offset = hdr_chain.eh[res].offset;
75   else
76     *frag_hdr_offset = 0;
77
78   *l4_protocol = hdr_chain.eh[hdr_chain.length - 1].protocol;
79   *l4_offset = hdr_chain.eh[hdr_chain.length - 1].offset;
80
81   return 0;
82 }
83
84 /**
85  * @brief Get L4 information like port number or ICMP id from IPv6 packet.
86  *
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
95  *
96  * @returns 1 on success, 0 otherwise.
97  */
98 always_inline u16
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)
103 {
104   u8 l4_protocol;
105   u16 l4_offset;
106   u16 frag_offset;
107   u8 *l4;
108
109   if (ip6_parse (vm, b, ip6, buffer_len, &l4_protocol, &l4_offset,
110                  &frag_offset))
111     {
112       return 0;
113     }
114   if (frag_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
117
118   if (ip_protocol)
119     {
120       *ip_protocol = l4_protocol;
121     }
122   l4 = u8_ptr_add (ip6, l4_offset);
123   if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
124     {
125       if (src_port)
126         *src_port = ((udp_header_t *) (l4))->src_port;
127       if (dst_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;
135     }
136   else if (l4_protocol == IP_PROTOCOL_ICMP6)
137     {
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)
142         {
143           if (src_port)
144             *src_port = ((u16 *) (icmp))[2];
145           if (dst_port)
146             *dst_port = ((u16 *) (icmp))[2];
147         }
148       else if (icmp->type == ICMP6_echo_reply)
149         {
150           if (src_port)
151             *src_port = ((u16 *) (icmp))[2];
152           if (dst_port)
153             *dst_port = ((u16 *) (icmp))[2];
154         }
155       else if (clib_net_to_host_u16 (ip6->payload_length) >= 64)
156         {
157           u16 ip6_pay_len;
158           ip6_header_t *inner_ip6;
159           u8 inner_l4_protocol;
160           u16 inner_l4_offset;
161           u16 inner_frag_offset;
162           u8 *inner_l4;
163
164           ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length);
165           inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
166
167           if (ip6_parse (vm, b, inner_ip6, ip6_pay_len - 8,
168                          &inner_l4_protocol, &inner_l4_offset,
169                          &inner_frag_offset))
170             return 0;
171
172           if (inner_frag_offset &&
173               ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
174                                     u8_ptr_add (inner_ip6,
175                                                 inner_frag_offset))))
176             return 0;
177
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)
181             {
182               if (src_port)
183                 *src_port = ((udp_header_t *) (inner_l4))->dst_port;
184               if (dst_port)
185                 *dst_port = ((udp_header_t *) (inner_l4))->src_port;
186             }
187           else if (inner_l4_protocol == IP_PROTOCOL_ICMP6)
188             {
189               icmp46_header_t *inner_icmp = (icmp46_header_t *) (inner_l4);
190               if (inner_icmp->type == ICMP6_echo_request)
191                 {
192                   if (src_port)
193                     *src_port = ((u16 *) (inner_icmp))[2];
194                   if (dst_port)
195                     *dst_port = ((u16 *) (inner_icmp))[2];
196                 }
197               else if (inner_icmp->type == ICMP6_echo_reply)
198                 {
199                   if (src_port)
200                     *src_port = ((u16 *) (inner_icmp))[2];
201                   if (dst_port)
202                     *dst_port = ((u16 *) (inner_icmp))[2];
203                 }
204             }
205         }
206     }
207   return 1;
208 }
209
210 /**
211  * @brief Convert type and code value from ICMP6 to ICMP4.
212  *
213  * @param icmp      ICMP header.
214  * @param inner_ip6 Inner IPv6 header if present, 0 otherwise.
215  *
216  * @returns 0 on success, non-zero value otherwise.
217  */
218 static_always_inline int
219 icmp6_to_icmp_header (icmp46_header_t * icmp, ip6_header_t ** inner_ip6)
220 {
221   *inner_ip6 = NULL;
222   switch (icmp->type)
223     {
224     case ICMP6_echo_request:
225       icmp->type = ICMP4_echo_request;
226       break;
227     case ICMP6_echo_reply:
228       icmp->type = ICMP4_echo_reply;
229       break;
230     case ICMP6_destination_unreachable:
231       *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
232
233       switch (icmp->code)
234         {
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;
239           icmp->code =
240             ICMP4_destination_unreachable_destination_unreachable_host;
241           break;
242         case ICMP6_destination_unreachable_destination_administratively_prohibited:     //1
243           icmp->type =
244             ICMP4_destination_unreachable;
245           icmp->code =
246             ICMP4_destination_unreachable_communication_administratively_prohibited;
247           break;
248         case ICMP6_destination_unreachable_port_unreachable:
249           icmp->type = ICMP4_destination_unreachable;
250           icmp->code = ICMP4_destination_unreachable_port_unreachable;
251           break;
252         default:
253           return -1;
254         }
255       break;
256     case ICMP6_packet_too_big:
257       *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
258
259       icmp->type = ICMP4_destination_unreachable;
260       icmp->code = 4;
261       {
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);
266       }
267       break;
268
269     case ICMP6_time_exceeded:
270       *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
271
272       icmp->type = ICMP4_time_exceeded;
273       break;
274
275     case ICMP6_parameter_problem:
276       *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
277
278       switch (icmp->code)
279         {
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)));
284           if (pointer >= 40)
285             return -1;
286
287           ((u8 *) (icmp + 1))[0] =
288             icmp6_to_icmp_updater_pointer_table[pointer];
289           break;
290         case ICMP6_parameter_problem_unrecognized_next_header:
291           icmp->type = ICMP4_destination_unreachable;
292           icmp->code = ICMP4_destination_unreachable_port_unreachable;
293           break;
294         case ICMP6_parameter_problem_unrecognized_option:
295         default:
296           return -1;
297         }
298       break;
299     default:
300       return -1;
301       break;
302     }
303   return 0;
304 }
305
306 /**
307  * @brief Translate TOS value from IPv6 to IPv4.
308  *
309  * @param ip_version_traffic_class_and_flow_label in network byte order
310  *
311  * @returns IPv4 TOS value.
312  */
313 static_always_inline u8
314 ip6_translate_tos (u32 ip_version_traffic_class_and_flow_label)
315 {
316   return (clib_net_to_host_u32 (ip_version_traffic_class_and_flow_label)
317           & 0x0ff00000) >> 20;
318 }
319
320 /**
321  * @brief Translate ICMP6 packet to ICMP4.
322  *
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.
328  *
329  * @returns 0 on success, non-zero value otherwise.
330  */
331 always_inline int
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)
335 {
336   ip6_header_t *ip6, *inner_ip6;
337   ip4_header_t *ip4, *inner_ip4;
338   u32 ip6_pay_len;
339   icmp46_header_t *icmp;
340   ip_csum_t csum;
341   int rv;
342   ip6_address_t old_src, old_dst;
343
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);
348
349   //No extensions headers allowed here
350   if (ip6->protocol != IP_PROTOCOL_ICMP6)
351     return -1;
352
353   //There are no fragmented ICMP messages, so no extension header for now
354   if (icmp6_to_icmp_header (icmp, &inner_ip6))
355     return -1;
356
357   if (inner_ip6)
358     {
359       u16 *inner_L4_checksum, inner_l4_offset, inner_frag_offset,
360         inner_frag_id;
361       u8 *inner_l4, inner_protocol;
362
363       //We have two headers to translate
364       //   FROM
365       //   [   IPv6   ]<- ext ->[IC][   IPv6   ]<- ext ->[L4 header ...
366       // Handled cases:
367       //                     [   IPv6   ][IC][   IPv6   ][L4 header ...
368       //                 [   IPv6   ][IC][   IPv6   ][Fr][L4 header ...
369       //    TO
370       //                               [ IPv4][IC][ IPv4][L4 header ...
371
372       if (ip6_parse (vm, p, inner_ip6, ip6_pay_len - 8,
373                      &inner_protocol, &inner_l4_offset, &inner_frag_offset))
374         return -1;
375
376       inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset);
377       inner_ip4 =
378         (ip4_header_t *) u8_ptr_add (inner_l4, -sizeof (*inner_ip4));
379       if (inner_frag_offset)
380         {
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);
384         }
385       else
386         {
387           inner_frag_id = 0;
388         }
389
390       //Do the translation of the inner packet
391       if (inner_protocol == IP_PROTOCOL_TCP)
392         {
393           inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 16);
394         }
395       else if (inner_protocol == IP_PROTOCOL_UDP)
396         {
397           inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 6);
398         }
399       else if (inner_protocol == IP_PROTOCOL_ICMP6)
400         {
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;
407         }
408       else
409         {
410           return -1;
411         }
412
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];
417
418       if ((rv = inner_fn (inner_ip6, inner_ip4, inner_ctx)) != 0)
419         return rv;
420
421       inner_ip4->ip_version_and_header_length =
422         IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
423       inner_ip4->tos =
424         ip6_translate_tos
425         (inner_ip6->ip_version_traffic_class_and_flow_label);
426       inner_ip4->length =
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);
435
436       if (inner_ip4->protocol == IP_PROTOCOL_ICMP)
437         {
438           //Recompute ICMP checksum
439           icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
440           inner_icmp->checksum = 0;
441           csum =
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);
446         }
447       else
448         {
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);
458         }
459
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);
464     }
465   else
466     {
467       //Only one header to translate
468       ip4 = (ip4_header_t *) u8_ptr_add (ip6, sizeof (*ip6) - sizeof (*ip4));
469     }
470
471   vlib_buffer_advance (p, (u32) (((u8 *) ip4) - ((u8 *) ip6)));
472
473   if ((rv = fn (ip6, ip4, ctx)) != 0)
474     return rv;
475
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,
485                              (inner_ip6 ==
486                               NULL) ? sizeof (*ip4) : (2 * sizeof (*ip4) -
487                                                        sizeof (*ip6)));
488   ip4->checksum = ip4_header_checksum (ip4);
489
490   //Recompute ICMP checksum
491   icmp->checksum = 0;
492   csum =
493     ip_incremental_checksum (0, icmp,
494                              clib_net_to_host_u16 (ip4->length) -
495                              sizeof (*ip4));
496   icmp->checksum = ~ip_csum_fold (csum);
497
498   return 0;
499 }
500
501 #endif /* __included_ip6_to_ip4_h__ */
502
503 /*
504  * fd.io coding-style-patch-verification: ON
505  *
506  * Local Variables:
507  * eval: (c-set-style "gnu")
508  * End:
509  */