map: fix translation of icmp6 error messages
[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 /* *INDENT-OFF* */
35 static u8 icmp6_to_icmp_updater_pointer_table[] =
36   { 0, 1, ~0, ~0,
37     2, 2, 9, 8,
38     12, 12, 12, 12,
39     12, 12, 12, 12,
40     12, 12, 12, 12,
41     12, 12, 12, 12,
42     24, 24, 24, 24,
43     24, 24, 24, 24,
44     24, 24, 24, 24,
45     24, 24, 24, 24
46   };
47 /* *INDENT-ON* */
48
49 #define frag_id_6to4(id) ((id) ^ ((id) >> 16))
50
51 /**
52  * @brief Parse some useful information from IPv6 header.
53  *
54  * @param vm              vlib main
55  * @param b               vlib buffer
56  * @param ip6             IPv6 header.
57  * @param buff_len        Buffer length.
58  * @param l4_protocol     L4 protocol number.
59  * @param l4_offset       L4 header offset.
60  * @param frag_hdr_offset Fragment header offset if present, 0 otherwise.
61  *
62  * @returns 0 on success, non-zero value otherwise.
63  */
64 static_always_inline int
65 ip6_parse (vlib_main_t * vm, vlib_buffer_t * b, const ip6_header_t * ip6,
66            u32 buff_len, u8 * l4_protocol, u16 * l4_offset,
67            u16 * frag_hdr_offset)
68 {
69   ip6_ext_header_t *last_hdr, *frag_hdr;
70   u32 length;
71   if (ip6_walk_ext_hdr
72       (vm, b, ip6, IP_PROTOCOL_IPV6_FRAGMENTATION, &length, &frag_hdr,
73        &last_hdr))
74     {
75       return -1;
76     }
77
78   if (length > 0)
79     {
80       if (frag_hdr)
81         {
82           *frag_hdr_offset = (u8 *) frag_hdr - (u8 *) ip6;
83         }
84       else
85         {
86           *frag_hdr_offset = 0;
87         }
88       *l4_protocol = last_hdr->next_hdr;
89     }
90   else
91     {
92       *frag_hdr_offset = 0;
93       *l4_protocol = ip6->protocol;
94     }
95   *l4_offset = sizeof (*ip6) + length;
96
97   return (buff_len < (*l4_offset + 4)) ||
98     (clib_net_to_host_u16 (ip6->payload_length) <
99      (*l4_offset + 4 - sizeof (*ip6)));
100 }
101
102 /**
103  * @brief Get L4 information like port number or ICMP id from IPv6 packet.
104  *
105  * @param ip6        IPv6 header.
106  * @param buffer_len Buffer length.
107  * @param ip_protocol L4 protocol
108  * @param src_port L4 src port or icmp id
109  * @param dst_post L4 dst port or icmp id
110  * @param icmp_type_or_tcp_flags ICMP type or TCP flags, if applicable
111  * @param tcp_ack_number TCP ack number, if applicable
112  * @param tcp_seq_number TCP seq number, if applicable
113  *
114  * @returns 1 on success, 0 otherwise.
115  */
116 always_inline u16
117 ip6_get_port (vlib_main_t * vm, vlib_buffer_t * b, ip6_header_t * ip6,
118               u16 buffer_len, u8 * ip_protocol, u16 * src_port,
119               u16 * dst_port, u8 * icmp_type_or_tcp_flags,
120               u32 * tcp_ack_number, u32 * tcp_seq_number)
121 {
122   u8 l4_protocol;
123   u16 l4_offset;
124   u16 frag_offset;
125   u8 *l4;
126
127   if (ip6_parse
128       (vm, b, ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
129     return 0;
130
131   if (frag_offset &&
132       ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
133                             u8_ptr_add (ip6, frag_offset))))
134     return 0;                   //Can't deal with non-first fragment for now
135
136   if (ip_protocol)
137     {
138       *ip_protocol = l4_protocol;
139     }
140   l4 = u8_ptr_add (ip6, l4_offset);
141   if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
142     {
143       if (src_port)
144         *src_port = ((udp_header_t *) (l4))->src_port;
145       if (dst_port)
146         *dst_port = ((udp_header_t *) (l4))->dst_port;
147       if (icmp_type_or_tcp_flags && l4_protocol == IP_PROTOCOL_TCP)
148         *icmp_type_or_tcp_flags = ((tcp_header_t *) (l4))->flags;
149       if (tcp_ack_number && l4_protocol == IP_PROTOCOL_TCP)
150         *tcp_ack_number = ((tcp_header_t *) (l4))->ack_number;
151       if (tcp_seq_number && l4_protocol == IP_PROTOCOL_TCP)
152         *tcp_seq_number = ((tcp_header_t *) (l4))->seq_number;
153     }
154   else if (l4_protocol == IP_PROTOCOL_ICMP6)
155     {
156       icmp46_header_t *icmp = (icmp46_header_t *) (l4);
157       if (icmp_type_or_tcp_flags)
158         *icmp_type_or_tcp_flags = ((icmp46_header_t *) (l4))->type;
159       if (icmp->type == ICMP6_echo_request)
160         {
161           if (src_port)
162             *src_port = ((u16 *) (icmp))[2];
163           if (dst_port)
164             *dst_port = ((u16 *) (icmp))[2];
165         }
166       else if (icmp->type == ICMP6_echo_reply)
167         {
168           if (src_port)
169             *src_port = ((u16 *) (icmp))[2];
170           if (dst_port)
171             *dst_port = ((u16 *) (icmp))[2];
172         }
173       else if (clib_net_to_host_u16 (ip6->payload_length) >= 64)
174         {
175           u16 ip6_pay_len;
176           ip6_header_t *inner_ip6;
177           u8 inner_l4_protocol;
178           u16 inner_l4_offset;
179           u16 inner_frag_offset;
180           u8 *inner_l4;
181
182           ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length);
183           inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
184
185           if (ip6_parse (vm, b, inner_ip6, ip6_pay_len - 8,
186                          &inner_l4_protocol, &inner_l4_offset,
187                          &inner_frag_offset))
188             return 0;
189
190           if (inner_frag_offset &&
191               ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
192                                     u8_ptr_add (inner_ip6,
193                                                 inner_frag_offset))))
194             return 0;
195
196           inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset);
197           if (inner_l4_protocol == IP_PROTOCOL_TCP ||
198               inner_l4_protocol == IP_PROTOCOL_UDP)
199             {
200               if (src_port)
201                 *src_port = ((udp_header_t *) (inner_l4))->dst_port;
202               if (dst_port)
203                 *dst_port = ((udp_header_t *) (inner_l4))->src_port;
204             }
205           else if (inner_l4_protocol == IP_PROTOCOL_ICMP6)
206             {
207               icmp46_header_t *inner_icmp = (icmp46_header_t *) (inner_l4);
208               if (inner_icmp->type == ICMP6_echo_request)
209                 {
210                   if (src_port)
211                     *src_port = ((u16 *) (inner_icmp))[2];
212                   if (dst_port)
213                     *dst_port = ((u16 *) (inner_icmp))[2];
214                 }
215               else if (inner_icmp->type == ICMP6_echo_reply)
216                 {
217                   if (src_port)
218                     *src_port = ((u16 *) (inner_icmp))[2];
219                   if (dst_port)
220                     *dst_port = ((u16 *) (inner_icmp))[2];
221                 }
222             }
223         }
224     }
225   return 1;
226 }
227
228 /**
229  * @brief Convert type and code value from ICMP6 to ICMP4.
230  *
231  * @param icmp      ICMP header.
232  * @param inner_ip6 Inner IPv6 header if present, 0 otherwise.
233  *
234  * @returns 0 on success, non-zero value otherwise.
235  */
236 static_always_inline int
237 icmp6_to_icmp_header (icmp46_header_t * icmp, ip6_header_t ** inner_ip6)
238 {
239   *inner_ip6 = NULL;
240   switch (icmp->type)
241     {
242     case ICMP6_echo_request:
243       icmp->type = ICMP4_echo_request;
244       break;
245     case ICMP6_echo_reply:
246       icmp->type = ICMP4_echo_reply;
247       break;
248     case ICMP6_destination_unreachable:
249       *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
250
251       switch (icmp->code)
252         {
253         case ICMP6_destination_unreachable_no_route_to_destination:     //0
254         case ICMP6_destination_unreachable_beyond_scope_of_source_address:      //2
255         case ICMP6_destination_unreachable_address_unreachable: //3
256           icmp->type = ICMP4_destination_unreachable;
257           icmp->code =
258             ICMP4_destination_unreachable_destination_unreachable_host;
259           break;
260         case ICMP6_destination_unreachable_destination_administratively_prohibited:     //1
261           icmp->type =
262             ICMP4_destination_unreachable;
263           icmp->code =
264             ICMP4_destination_unreachable_communication_administratively_prohibited;
265           break;
266         case ICMP6_destination_unreachable_port_unreachable:
267           icmp->type = ICMP4_destination_unreachable;
268           icmp->code = ICMP4_destination_unreachable_port_unreachable;
269           break;
270         default:
271           return -1;
272         }
273       break;
274     case ICMP6_packet_too_big:
275       *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
276
277       icmp->type = ICMP4_destination_unreachable;
278       icmp->code = 4;
279       {
280         u32 advertised_mtu = clib_net_to_host_u32 (*((u32 *) (icmp + 1)));
281         advertised_mtu -= 20;
282         //FIXME: = minimum(advertised MTU-20, MTU_of_IPv4_nexthop, (MTU_of_IPv6_nexthop)-20)
283         ((u16 *) (icmp))[3] = clib_host_to_net_u16 (advertised_mtu);
284       }
285       break;
286
287     case ICMP6_time_exceeded:
288       *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
289
290       icmp->type = ICMP4_time_exceeded;
291       break;
292
293     case ICMP6_parameter_problem:
294       *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
295
296       switch (icmp->code)
297         {
298         case ICMP6_parameter_problem_erroneous_header_field:
299           icmp->type = ICMP4_parameter_problem;
300           icmp->code = ICMP4_parameter_problem_pointer_indicates_error;
301           u32 pointer = clib_net_to_host_u32 (*((u32 *) (icmp + 1)));
302           if (pointer >= 40)
303             return -1;
304
305           ((u8 *) (icmp + 1))[0] =
306             icmp6_to_icmp_updater_pointer_table[pointer];
307           break;
308         case ICMP6_parameter_problem_unrecognized_next_header:
309           icmp->type = ICMP4_destination_unreachable;
310           icmp->code = ICMP4_destination_unreachable_port_unreachable;
311           break;
312         case ICMP6_parameter_problem_unrecognized_option:
313         default:
314           return -1;
315         }
316       break;
317     default:
318       return -1;
319       break;
320     }
321   return 0;
322 }
323
324 /**
325  * @brief Translate TOS value from IPv6 to IPv4.
326  *
327  * @param ip_version_traffic_class_and_flow_label in network byte order
328  *
329  * @returns IPv4 TOS value.
330  */
331 static_always_inline u8
332 ip6_translate_tos (u32 ip_version_traffic_class_and_flow_label)
333 {
334   return (clib_net_to_host_u32 (ip_version_traffic_class_and_flow_label)
335           & 0x0ff00000) >> 20;
336 }
337
338 /**
339  * @brief Translate ICMP6 packet to ICMP4.
340  *
341  * @param p         Buffer to translate.
342  * @param fn        The function to translate outer header.
343  * @param ctx       A context passed in the outer header translate function.
344  * @param inner_fn  The function to translate inner header.
345  * @param inner_ctx A context passed in the inner header translate function.
346  *
347  * @returns 0 on success, non-zero value otherwise.
348  */
349 always_inline int
350 icmp6_to_icmp (vlib_main_t * vm, vlib_buffer_t * p,
351                ip6_to_ip4_icmp_set_fn_t fn, void *ctx,
352                ip6_to_ip4_icmp_set_fn_t inner_fn, void *inner_ctx)
353 {
354   ip6_header_t *ip6, *inner_ip6;
355   ip4_header_t *ip4, *inner_ip4;
356   u32 ip6_pay_len;
357   icmp46_header_t *icmp;
358   ip_csum_t csum;
359   int rv;
360   ip6_address_t old_src, old_dst;
361
362   ip6 = vlib_buffer_get_current (p);
363   ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length);
364   icmp = (icmp46_header_t *) (ip6 + 1);
365   ASSERT (ip6_pay_len + sizeof (*ip6) <= p->current_length);
366
367   //No extensions headers allowed here
368   if (ip6->protocol != IP_PROTOCOL_ICMP6)
369     return -1;
370
371   //There are no fragmented ICMP messages, so no extension header for now
372   if (icmp6_to_icmp_header (icmp, &inner_ip6))
373     return -1;
374
375   if (inner_ip6)
376     {
377       u16 *inner_L4_checksum, inner_l4_offset, inner_frag_offset,
378         inner_frag_id;
379       u8 *inner_l4, inner_protocol;
380
381       //We have two headers to translate
382       //   FROM
383       //   [   IPv6   ]<- ext ->[IC][   IPv6   ]<- ext ->[L4 header ...
384       // Handled cases:
385       //                     [   IPv6   ][IC][   IPv6   ][L4 header ...
386       //                 [   IPv6   ][IC][   IPv6   ][Fr][L4 header ...
387       //    TO
388       //                               [ IPv4][IC][ IPv4][L4 header ...
389
390       if (ip6_parse (vm, p, inner_ip6, ip6_pay_len - 8,
391                      &inner_protocol, &inner_l4_offset, &inner_frag_offset))
392         return -1;
393
394       inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset);
395       inner_ip4 =
396         (ip4_header_t *) u8_ptr_add (inner_l4, -sizeof (*inner_ip4));
397       if (inner_frag_offset)
398         {
399           ip6_frag_hdr_t *inner_frag =
400             (ip6_frag_hdr_t *) u8_ptr_add (inner_ip6, inner_frag_offset);
401           inner_frag_id = frag_id_6to4 (inner_frag->identification);
402         }
403       else
404         {
405           inner_frag_id = 0;
406         }
407
408       //Do the translation of the inner packet
409       if (inner_protocol == IP_PROTOCOL_TCP)
410         {
411           inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 16);
412         }
413       else if (inner_protocol == IP_PROTOCOL_UDP)
414         {
415           inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 6);
416         }
417       else if (inner_protocol == IP_PROTOCOL_ICMP6)
418         {
419           icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
420           //It cannot be of a different type as ip6_icmp_to_icmp6_in_place succeeded
421           inner_icmp->type = (inner_icmp->type == ICMP6_echo_request) ?
422             ICMP4_echo_request : ICMP4_echo_reply;
423           inner_protocol = IP_PROTOCOL_ICMP;    //Will be copied to ip6 later
424           inner_L4_checksum = &inner_icmp->checksum;
425         }
426       else
427         {
428           return -1;
429         }
430
431       old_src.as_u64[0] = inner_ip6->src_address.as_u64[0];
432       old_src.as_u64[1] = inner_ip6->src_address.as_u64[1];
433       old_dst.as_u64[0] = inner_ip6->dst_address.as_u64[0];
434       old_dst.as_u64[1] = inner_ip6->dst_address.as_u64[1];
435
436       if ((rv = inner_fn (inner_ip6, inner_ip4, inner_ctx)) != 0)
437         return rv;
438
439       inner_ip4->ip_version_and_header_length =
440         IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
441       inner_ip4->tos =
442         ip6_translate_tos
443         (inner_ip6->ip_version_traffic_class_and_flow_label);
444       inner_ip4->length =
445         u16_net_add (inner_ip6->payload_length,
446                      sizeof (*ip4) + sizeof (*ip6) - inner_l4_offset);
447       inner_ip4->fragment_id = inner_frag_id;
448       inner_ip4->flags_and_fragment_offset =
449         clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
450       inner_ip4->ttl = inner_ip6->hop_limit;
451       inner_ip4->protocol = inner_protocol;
452       inner_ip4->checksum = ip4_header_checksum (inner_ip4);
453
454       if (inner_ip4->protocol == IP_PROTOCOL_ICMP)
455         {
456           //Recompute ICMP checksum
457           icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
458           inner_icmp->checksum = 0;
459           csum =
460             ip_incremental_checksum (0, inner_icmp,
461                                      clib_net_to_host_u16 (inner_ip4->length)
462                                      - sizeof (*inner_ip4));
463           inner_icmp->checksum = ~ip_csum_fold (csum);
464         }
465       else
466         {
467           //Update to new pseudo-header
468           csum = *inner_L4_checksum;
469           csum = ip_csum_sub_even (csum, old_src.as_u64[0]);
470           csum = ip_csum_sub_even (csum, old_src.as_u64[1]);
471           csum = ip_csum_sub_even (csum, old_dst.as_u64[0]);
472           csum = ip_csum_sub_even (csum, old_dst.as_u64[1]);
473           csum = ip_csum_add_even (csum, inner_ip4->src_address.as_u32);
474           csum = ip_csum_add_even (csum, inner_ip4->dst_address.as_u32);
475           *inner_L4_checksum = ip_csum_fold (csum);
476         }
477
478       //Move up icmp header
479       ip4 = (ip4_header_t *) u8_ptr_add (inner_l4, -2 * sizeof (*ip4) - 8);
480       clib_memcpy_fast (u8_ptr_add (inner_l4, -sizeof (*ip4) - 8), icmp, 8);
481       icmp = (icmp46_header_t *) u8_ptr_add (inner_l4, -sizeof (*ip4) - 8);
482     }
483   else
484     {
485       //Only one header to translate
486       ip4 = (ip4_header_t *) u8_ptr_add (ip6, sizeof (*ip6) - sizeof (*ip4));
487     }
488
489   vlib_buffer_advance (p, (u32) (((u8 *) ip4) - ((u8 *) ip6)));
490
491   if ((rv = fn (ip6, ip4, ctx)) != 0)
492     return rv;
493
494   ip4->ip_version_and_header_length =
495     IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
496   ip4->tos = ip6_translate_tos (ip6->ip_version_traffic_class_and_flow_label);
497   ip4->fragment_id = 0;
498   ip4->flags_and_fragment_offset = 0;
499   ip4->ttl = ip6->hop_limit;
500   ip4->protocol = IP_PROTOCOL_ICMP;
501   //TODO fix the length depending on offset length
502   ip4->length = u16_net_add (ip6->payload_length,
503                              (inner_ip6 ==
504                               NULL) ? sizeof (*ip4) : (2 * sizeof (*ip4) -
505                                                        sizeof (*ip6)));
506   ip4->checksum = ip4_header_checksum (ip4);
507
508   //Recompute ICMP checksum
509   icmp->checksum = 0;
510   csum =
511     ip_incremental_checksum (0, icmp,
512                              clib_net_to_host_u16 (ip4->length) -
513                              sizeof (*ip4));
514   icmp->checksum = ~ip_csum_fold (csum);
515
516   return 0;
517 }
518
519 #endif /* __included_ip6_to_ip4_h__ */
520
521 /*
522  * fd.io coding-style-patch-verification: ON
523  *
524  * Local Variables:
525  * eval: (c-set-style "gnu")
526  * End:
527  */