3c8300ff01422aa7b7049176524d0bbe98ee3b94
[vpp.git] / src / vnet / ip / icmp6.c
1 /*
2  * Copyright (c) 2015 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  * ip/icmp6.c: ip6 icmp
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vlib/vlib.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/pg/pg.h>
43 #include <vnet/ip/ip_sas.h>
44
45 static u8 *
46 format_ip6_icmp_type_and_code (u8 * s, va_list * args)
47 {
48   icmp6_type_t type = va_arg (*args, int);
49   u8 code = va_arg (*args, int);
50   char *t = 0;
51
52 #define _(n,f) case n: t = #f; break;
53
54   switch (type)
55     {
56       foreach_icmp6_type;
57
58     default:
59       break;
60     }
61
62 #undef _
63
64   if (!t)
65     return format (s, "unknown 0x%x", type);
66
67   s = format (s, "%s", t);
68
69   t = 0;
70   switch ((type << 8) | code)
71     {
72 #define _(a,n,f) case (ICMP6_##a << 8) | (n): t = #f; break;
73
74       foreach_icmp6_code;
75
76 #undef _
77     }
78
79   if (t)
80     s = format (s, " %s", t);
81
82   return s;
83 }
84
85 static u8 *
86 format_icmp6_header (u8 * s, va_list * args)
87 {
88   icmp46_header_t *icmp = va_arg (*args, icmp46_header_t *);
89   u32 max_header_bytes = va_arg (*args, u32);
90
91   /* Nothing to do. */
92   if (max_header_bytes < sizeof (icmp[0]))
93     return format (s, "ICMP header truncated");
94
95   s = format (s, "ICMP %U checksum 0x%x",
96               format_ip6_icmp_type_and_code, icmp->type, icmp->code,
97               clib_net_to_host_u16 (icmp->checksum));
98
99   if (max_header_bytes >=
100       sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t) &&
101       (icmp->type == ICMP6_neighbor_solicitation ||
102        icmp->type == ICMP6_neighbor_advertisement))
103     {
104       icmp6_neighbor_solicitation_or_advertisement_header_t *icmp6_nd =
105         (icmp6_neighbor_solicitation_or_advertisement_header_t *) icmp;
106       s = format (s, "\n    target address %U",
107                   format_ip6_address, &icmp6_nd->target_address);
108     }
109
110   return s;
111 }
112
113 u8 *
114 format_icmp6_input_trace (u8 * s, va_list * va)
115 {
116   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
117   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
118   icmp6_input_trace_t *t = va_arg (*va, icmp6_input_trace_t *);
119
120   s = format (s, "%U",
121               format_ip6_header, t->packet_data, sizeof (t->packet_data));
122
123   return s;
124 }
125
126 static char *icmp_error_strings[] = {
127 #define _(f,s) s,
128   foreach_icmp6_error
129 #undef _
130 };
131
132 typedef enum
133 {
134   ICMP_INPUT_NEXT_PUNT,
135   ICMP_INPUT_N_NEXT,
136 } icmp_input_next_t;
137
138 typedef struct
139 {
140   uword *type_and_code_by_name;
141
142   uword *type_by_name;
143
144   /* Vector dispatch table indexed by [icmp type]. */
145   u8 input_next_index_by_type[256];
146
147   /* Max valid code indexed by icmp type. */
148   u8 max_valid_code_by_type[256];
149
150   /* hop_limit must be >= this value for this icmp type. */
151   u8 min_valid_hop_limit_by_type[256];
152
153   u8 min_valid_length_by_type[256];
154 } icmp6_main_t;
155
156 icmp6_main_t icmp6_main;
157
158 static uword
159 ip6_icmp_input (vlib_main_t * vm,
160                 vlib_node_runtime_t * node, vlib_frame_t * frame)
161 {
162   icmp6_main_t *im = &icmp6_main;
163   u32 *from, *to_next;
164   u32 n_left_from, n_left_to_next, next_index;
165
166   from = vlib_frame_vector_args (frame);
167   n_left_from = frame->n_vectors;
168   next_index = node->cached_next_index;
169
170   if (node->flags & VLIB_NODE_FLAG_TRACE)
171     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
172                                    /* stride */ 1,
173                                    sizeof (icmp6_input_trace_t));
174
175   while (n_left_from > 0)
176     {
177       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
178
179       while (n_left_from > 0 && n_left_to_next > 0)
180         {
181           vlib_buffer_t *b0;
182           ip6_header_t *ip0;
183           icmp46_header_t *icmp0;
184           icmp6_type_t type0;
185           u32 bi0, next0, error0, len0;
186
187           bi0 = to_next[0] = from[0];
188
189           from += 1;
190           n_left_from -= 1;
191           to_next += 1;
192           n_left_to_next -= 1;
193
194           b0 = vlib_get_buffer (vm, bi0);
195           ip0 = vlib_buffer_get_current (b0);
196           icmp0 = ip6_next_header (ip0);
197           type0 = icmp0->type;
198
199           error0 = ICMP6_ERROR_NONE;
200
201           next0 = im->input_next_index_by_type[type0];
202           error0 =
203             next0 == ICMP_INPUT_NEXT_PUNT ? ICMP6_ERROR_UNKNOWN_TYPE : error0;
204
205           /* Check code is valid for type. */
206           error0 =
207             icmp0->code >
208             im->max_valid_code_by_type[type0] ?
209             ICMP6_ERROR_INVALID_CODE_FOR_TYPE : error0;
210
211           /* Checksum is already validated by ip6_local node so we don't need to check that. */
212
213           /* Check that hop limit == 255 for certain types. */
214           error0 =
215             ip0->hop_limit <
216             im->min_valid_hop_limit_by_type[type0] ?
217             ICMP6_ERROR_INVALID_HOP_LIMIT_FOR_TYPE : error0;
218
219           len0 = clib_net_to_host_u16 (ip0->payload_length);
220           error0 =
221             len0 <
222             im->min_valid_length_by_type[type0] ?
223             ICMP6_ERROR_LENGTH_TOO_SMALL_FOR_TYPE : error0;
224
225           b0->error = node->errors[error0];
226
227           next0 = error0 != ICMP6_ERROR_NONE ? ICMP_INPUT_NEXT_PUNT : next0;
228
229           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
230                                            to_next, n_left_to_next,
231                                            bi0, next0);
232         }
233
234       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
235     }
236
237   return frame->n_vectors;
238 }
239
240 /* *INDENT-OFF* */
241 VLIB_REGISTER_NODE (ip6_icmp_input_node) = {
242   .function = ip6_icmp_input,
243   .name = "ip6-icmp-input",
244
245   .vector_size = sizeof (u32),
246
247   .format_trace = format_icmp6_input_trace,
248
249   .n_errors = ARRAY_LEN (icmp_error_strings),
250   .error_strings = icmp_error_strings,
251
252   .n_next_nodes = 1,
253   .next_nodes = {
254     [ICMP_INPUT_NEXT_PUNT] = "ip6-punt",
255   },
256 };
257 /* *INDENT-ON* */
258
259 typedef enum
260 {
261   IP6_ICMP_ERROR_NEXT_DROP,
262   IP6_ICMP_ERROR_NEXT_LOOKUP,
263   IP6_ICMP_ERROR_N_NEXT,
264 } ip6_icmp_error_next_t;
265
266 void
267 icmp6_error_set_vnet_buffer (vlib_buffer_t * b, u8 type, u8 code, u32 data)
268 {
269   vnet_buffer (b)->ip.icmp.type = type;
270   vnet_buffer (b)->ip.icmp.code = code;
271   vnet_buffer (b)->ip.icmp.data = data;
272 }
273
274 static u8
275 icmp6_icmp_type_to_error (u8 type)
276 {
277   switch (type)
278     {
279     case ICMP6_destination_unreachable:
280       return ICMP6_ERROR_DEST_UNREACH_SENT;
281     case ICMP6_packet_too_big:
282       return ICMP6_ERROR_PACKET_TOO_BIG_SENT;
283     case ICMP6_time_exceeded:
284       return ICMP6_ERROR_TTL_EXPIRE_SENT;
285     case ICMP6_parameter_problem:
286       return ICMP6_ERROR_PARAM_PROBLEM_SENT;
287     default:
288       return ICMP6_ERROR_DROP;
289     }
290 }
291
292 static uword
293 ip6_icmp_error (vlib_main_t * vm,
294                 vlib_node_runtime_t * node, vlib_frame_t * frame)
295 {
296   u32 *from, *to_next;
297   uword n_left_from, n_left_to_next;
298   ip6_icmp_error_next_t next_index;
299
300   from = vlib_frame_vector_args (frame);
301   n_left_from = frame->n_vectors;
302   next_index = node->cached_next_index;
303
304   if (node->flags & VLIB_NODE_FLAG_TRACE)
305     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
306                                    /* stride */ 1,
307                                    sizeof (icmp6_input_trace_t));
308
309   while (n_left_from > 0)
310     {
311       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
312
313       while (n_left_from > 0 && n_left_to_next > 0)
314         {
315           /*
316            * Duplicate first buffer and free the original chain.  Keep
317            * as much of the original packet as possible, within the
318            * minimum MTU. We chat "a little" here by keeping whatever
319            * is available in the first buffer.
320            */
321
322           u32 pi0 = ~0;
323           u32 org_pi0 = from[0];
324           u32 next0 = IP6_ICMP_ERROR_NEXT_LOOKUP;
325           u8 error0 = ICMP6_ERROR_NONE;
326           vlib_buffer_t *p0, *org_p0;
327           ip6_header_t *ip0, *out_ip0;
328           icmp46_header_t *icmp0;
329           u32 sw_if_index0;
330           int bogus_length;
331
332           org_p0 = vlib_get_buffer (vm, org_pi0);
333           p0 = vlib_buffer_copy_no_chain (vm, org_p0, &pi0);
334           if (!p0 || pi0 == ~0) /* Out of buffers */
335             continue;
336
337           /* Speculatively enqueue p0 to the current next frame */
338           to_next[0] = pi0;
339           from += 1;
340           to_next += 1;
341           n_left_from -= 1;
342           n_left_to_next -= 1;
343
344           ip0 = vlib_buffer_get_current (p0);
345           sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
346
347           /* Add IP header and ICMPv6 header including a 4 byte data field */
348           vlib_buffer_advance (p0,
349                                -(sizeof (ip6_header_t) +
350                                  sizeof (icmp46_header_t) + 4));
351
352           vnet_buffer (p0)->sw_if_index[VLIB_TX] = ~0;
353           p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
354           p0->current_length =
355             p0->current_length > 1280 ? 1280 : p0->current_length;
356
357           out_ip0 = vlib_buffer_get_current (p0);
358           icmp0 = (icmp46_header_t *) & out_ip0[1];
359
360           /* Fill ip header fields */
361           out_ip0->ip_version_traffic_class_and_flow_label =
362             clib_host_to_net_u32 (0x6 << 28);
363
364           out_ip0->payload_length =
365             clib_host_to_net_u16 (p0->current_length - sizeof (ip6_header_t));
366           out_ip0->protocol = IP_PROTOCOL_ICMP6;
367           out_ip0->hop_limit = 0xff;
368           out_ip0->dst_address = ip0->src_address;
369           /* Prefer a source address from "offending interface" */
370           if (!ip6_sas_by_sw_if_index (sw_if_index0, &out_ip0->dst_address,
371                                        &out_ip0->src_address))
372             { /* interface has no IP6 address - should not happen */
373               next0 = IP6_ICMP_ERROR_NEXT_DROP;
374               error0 = ICMP6_ERROR_DROP;
375             }
376
377           /* Fill icmp header fields */
378           icmp0->type = vnet_buffer (p0)->ip.icmp.type;
379           icmp0->code = vnet_buffer (p0)->ip.icmp.code;
380           *((u32 *) (icmp0 + 1)) =
381             clib_host_to_net_u32 (vnet_buffer (p0)->ip.icmp.data);
382           icmp0->checksum = 0;
383           icmp0->checksum =
384             ip6_tcp_udp_icmp_compute_checksum (vm, p0, out_ip0,
385                                                &bogus_length);
386
387           /* Update error status */
388           if (error0 == ICMP6_ERROR_NONE)
389             error0 = icmp6_icmp_type_to_error (icmp0->type);
390
391           vlib_error_count (vm, node->node_index, error0, 1);
392
393           /* Verify speculative enqueue, maybe switch current next frame */
394           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
395                                            to_next, n_left_to_next,
396                                            pi0, next0);
397         }
398       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
399     }
400
401   /*
402    * push the original buffers to error-drop, so that
403    * they can get the error counters handled, then freed
404    */
405   vlib_buffer_enqueue_to_single_next (vm, node,
406                                       vlib_frame_vector_args (frame),
407                                       IP6_ICMP_ERROR_NEXT_DROP,
408                                       frame->n_vectors);
409
410   return frame->n_vectors;
411 }
412
413 /* *INDENT-OFF* */
414 VLIB_REGISTER_NODE (ip6_icmp_error_node) = {
415   .function = ip6_icmp_error,
416   .name = "ip6-icmp-error",
417   .vector_size = sizeof (u32),
418
419   .n_errors = ARRAY_LEN (icmp_error_strings),
420   .error_strings = icmp_error_strings,
421
422   .n_next_nodes = IP6_ICMP_ERROR_N_NEXT,
423   .next_nodes = {
424     [IP6_ICMP_ERROR_NEXT_DROP] = "error-drop",
425     [IP6_ICMP_ERROR_NEXT_LOOKUP] = "ip6-lookup",
426   },
427
428   .format_trace = format_icmp6_input_trace,
429 };
430 /* *INDENT-ON* */
431
432
433 static uword
434 unformat_icmp_type_and_code (unformat_input_t * input, va_list * args)
435 {
436   icmp46_header_t *h = va_arg (*args, icmp46_header_t *);
437   icmp6_main_t *cm = &icmp6_main;
438   u32 i;
439
440   if (unformat_user (input, unformat_vlib_number_by_name,
441                      cm->type_and_code_by_name, &i))
442     {
443       h->type = (i >> 8) & 0xff;
444       h->code = (i >> 0) & 0xff;
445     }
446   else if (unformat_user (input, unformat_vlib_number_by_name,
447                           cm->type_by_name, &i))
448     {
449       h->type = i;
450       h->code = 0;
451     }
452   else
453     return 0;
454
455   return 1;
456 }
457
458 static void
459 icmp6_pg_edit_function (pg_main_t * pg,
460                         pg_stream_t * s,
461                         pg_edit_group_t * g, u32 * packets, u32 n_packets)
462 {
463   vlib_main_t *vm = vlib_get_main ();
464   u32 ip_offset, icmp_offset;
465   int bogus_length;
466
467   icmp_offset = g->start_byte_offset;
468   ip_offset = (g - 1)->start_byte_offset;
469
470   while (n_packets >= 1)
471     {
472       vlib_buffer_t *p0;
473       ip6_header_t *ip0;
474       icmp46_header_t *icmp0;
475
476       p0 = vlib_get_buffer (vm, packets[0]);
477       n_packets -= 1;
478       packets += 1;
479
480       ASSERT (p0->current_data == 0);
481       ip0 = (void *) (p0->data + ip_offset);
482       icmp0 = (void *) (p0->data + icmp_offset);
483
484       icmp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
485                                                            &bogus_length);
486       ASSERT (bogus_length == 0);
487     }
488 }
489
490 typedef struct
491 {
492   pg_edit_t type, code;
493   pg_edit_t checksum;
494 } pg_icmp46_header_t;
495
496 always_inline void
497 pg_icmp_header_init (pg_icmp46_header_t * p)
498 {
499   /* Initialize fields that are not bit fields in the IP header. */
500 #define _(f) pg_edit_init (&p->f, icmp46_header_t, f);
501   _(type);
502   _(code);
503   _(checksum);
504 #undef _
505 }
506
507 static uword
508 unformat_pg_icmp_header (unformat_input_t * input, va_list * args)
509 {
510   pg_stream_t *s = va_arg (*args, pg_stream_t *);
511   pg_icmp46_header_t *p;
512   u32 group_index;
513
514   p = pg_create_edit_group (s, sizeof (p[0]), sizeof (icmp46_header_t),
515                             &group_index);
516   pg_icmp_header_init (p);
517
518   p->checksum.type = PG_EDIT_UNSPECIFIED;
519
520   {
521     icmp46_header_t tmp;
522
523     if (!unformat (input, "ICMP %U", unformat_icmp_type_and_code, &tmp))
524       goto error;
525
526     pg_edit_set_fixed (&p->type, tmp.type);
527     pg_edit_set_fixed (&p->code, tmp.code);
528   }
529
530   /* Parse options. */
531   while (1)
532     {
533       if (unformat (input, "checksum %U",
534                     unformat_pg_edit, unformat_pg_number, &p->checksum))
535         ;
536
537       /* Can't parse input: try next protocol level. */
538       else
539         break;
540     }
541
542   if (!unformat_user (input, unformat_pg_payload, s))
543     goto error;
544
545   if (p->checksum.type == PG_EDIT_UNSPECIFIED)
546     {
547       pg_edit_group_t *g = pg_stream_get_group (s, group_index);
548       g->edit_function = icmp6_pg_edit_function;
549       g->edit_function_opaque = 0;
550     }
551
552   return 1;
553
554 error:
555   /* Free up any edits we may have added. */
556   pg_free_edit_group (s);
557   return 0;
558 }
559
560 void
561 icmp6_register_type (vlib_main_t * vm, icmp6_type_t type, u32 node_index)
562 {
563   icmp6_main_t *im = &icmp6_main;
564
565   ASSERT ((int) type < ARRAY_LEN (im->input_next_index_by_type));
566   im->input_next_index_by_type[type]
567     = vlib_node_add_next (vm, ip6_icmp_input_node.index, node_index);
568 }
569
570 static clib_error_t *
571 icmp6_init (vlib_main_t * vm)
572 {
573   ip_main_t *im = &ip_main;
574   ip_protocol_info_t *pi;
575   icmp6_main_t *cm = &icmp6_main;
576   clib_error_t *error;
577
578   error = vlib_call_init_function (vm, ip_main_init);
579
580   if (error)
581     return error;
582
583   pi = ip_get_protocol_info (im, IP_PROTOCOL_ICMP6);
584   pi->format_header = format_icmp6_header;
585   pi->unformat_pg_edit = unformat_pg_icmp_header;
586
587   cm->type_by_name = hash_create_string (0, sizeof (uword));
588 #define _(n,t) hash_set_mem (cm->type_by_name, #t, (n));
589   foreach_icmp6_type;
590 #undef _
591
592   cm->type_and_code_by_name = hash_create_string (0, sizeof (uword));
593 #define _(a,n,t) hash_set_mem (cm->type_by_name, #t, (n) | (ICMP6_##a << 8));
594   foreach_icmp6_code;
595 #undef _
596
597   clib_memset (cm->input_next_index_by_type,
598                ICMP_INPUT_NEXT_PUNT, sizeof (cm->input_next_index_by_type));
599   clib_memset (cm->max_valid_code_by_type, 0,
600                sizeof (cm->max_valid_code_by_type));
601
602 #define _(a,n,t) cm->max_valid_code_by_type[ICMP6_##a] = clib_max (cm->max_valid_code_by_type[ICMP6_##a], n);
603   foreach_icmp6_code;
604 #undef _
605
606   clib_memset (cm->min_valid_hop_limit_by_type, 0,
607                sizeof (cm->min_valid_hop_limit_by_type));
608   cm->min_valid_hop_limit_by_type[ICMP6_router_solicitation] = 255;
609   cm->min_valid_hop_limit_by_type[ICMP6_router_advertisement] = 255;
610   cm->min_valid_hop_limit_by_type[ICMP6_neighbor_solicitation] = 255;
611   cm->min_valid_hop_limit_by_type[ICMP6_neighbor_advertisement] = 255;
612   cm->min_valid_hop_limit_by_type[ICMP6_redirect] = 255;
613
614   clib_memset (cm->min_valid_length_by_type, sizeof (icmp46_header_t),
615                sizeof (cm->min_valid_length_by_type));
616   cm->min_valid_length_by_type[ICMP6_router_solicitation] =
617     sizeof (icmp6_neighbor_discovery_header_t);
618   cm->min_valid_length_by_type[ICMP6_router_advertisement] =
619     sizeof (icmp6_router_advertisement_header_t);
620   cm->min_valid_length_by_type[ICMP6_neighbor_solicitation] =
621     sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t);
622   cm->min_valid_length_by_type[ICMP6_neighbor_advertisement] =
623     sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t);
624   cm->min_valid_length_by_type[ICMP6_redirect] =
625     sizeof (icmp6_redirect_header_t);
626
627   return (NULL);
628 }
629
630 VLIB_INIT_FUNCTION (icmp6_init);
631
632 /*
633  * fd.io coding-style-patch-verification: ON
634  *
635  * Local Variables:
636  * eval: (c-set-style "gnu")
637  * End:
638  */