VPP-1506: dump local punts and registered punt sockets
[vpp.git] / src / vnet / ip / ip4_source_check.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/ip4_source_check.c: IP v4 check source address (unicast RPF check)
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 <vnet/ip/ip.h>
41 #include <vnet/fib/ip4_fib.h>
42 #include <vnet/fib/fib_urpf_list.h>
43 #include <vnet/dpo/load_balance.h>
44
45 /**
46  * @file
47  * @brief IPv4 Unicast Source Check.
48  *
49  * This file contains the IPv4 interface unicast source check.
50  */
51
52
53 typedef struct
54 {
55   u8 packet_data[64];
56   index_t urpf;
57 } ip4_source_check_trace_t;
58
59 static u8 *
60 format_ip4_source_check_trace (u8 * s, va_list * va)
61 {
62   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
63   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
64   ip4_source_check_trace_t *t = va_arg (*va, ip4_source_check_trace_t *);
65
66   s = format (s, "%U",
67               format_ip4_header, t->packet_data, sizeof (t->packet_data));
68
69   return s;
70 }
71
72 typedef enum
73 {
74   IP4_SOURCE_CHECK_NEXT_DROP,
75   IP4_SOURCE_CHECK_N_NEXT,
76 } ip4_source_check_next_t;
77
78 typedef enum
79 {
80   IP4_SOURCE_CHECK_REACHABLE_VIA_RX,
81   IP4_SOURCE_CHECK_REACHABLE_VIA_ANY,
82 } ip4_source_check_type_t;
83
84 typedef union
85 {
86   u32 fib_index;
87 } ip4_source_check_config_t;
88
89 always_inline uword
90 ip4_source_check_inline (vlib_main_t * vm,
91                          vlib_node_runtime_t * node,
92                          vlib_frame_t * frame,
93                          ip4_source_check_type_t source_check_type)
94 {
95   u32 n_left_from, *from, *to_next;
96   u32 next_index;
97   vlib_node_runtime_t *error_node =
98     vlib_node_get_runtime (vm, ip4_input_node.index);
99
100   from = vlib_frame_vector_args (frame);
101   n_left_from = frame->n_vectors;
102   next_index = node->cached_next_index;
103
104   if (node->flags & VLIB_NODE_FLAG_TRACE)
105     vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
106                                    /* stride */ 1,
107                                    sizeof (ip4_source_check_trace_t));
108
109   while (n_left_from > 0)
110     {
111       u32 n_left_to_next;
112
113       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
114
115       while (n_left_from >= 4 && n_left_to_next >= 2)
116         {
117           vlib_buffer_t *p0, *p1;
118           ip4_header_t *ip0, *ip1;
119           ip4_fib_mtrie_t *mtrie0, *mtrie1;
120           ip4_fib_mtrie_leaf_t leaf0, leaf1;
121           ip4_source_check_config_t *c0, *c1;
122           const load_balance_t *lb0, *lb1;
123           u32 pi0, next0, pass0, lb_index0;
124           u32 pi1, next1, pass1, lb_index1;
125
126           /* Prefetch next iteration. */
127           {
128             vlib_buffer_t *p2, *p3;
129
130             p2 = vlib_get_buffer (vm, from[2]);
131             p3 = vlib_get_buffer (vm, from[3]);
132
133             vlib_prefetch_buffer_header (p2, LOAD);
134             vlib_prefetch_buffer_header (p3, LOAD);
135
136             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
137             CLIB_PREFETCH (p3->data, sizeof (ip1[0]), LOAD);
138           }
139
140           pi0 = to_next[0] = from[0];
141           pi1 = to_next[1] = from[1];
142           from += 2;
143           to_next += 2;
144           n_left_from -= 2;
145           n_left_to_next -= 2;
146
147           p0 = vlib_get_buffer (vm, pi0);
148           p1 = vlib_get_buffer (vm, pi1);
149
150           ip0 = vlib_buffer_get_current (p0);
151           ip1 = vlib_buffer_get_current (p1);
152
153           c0 = vnet_feature_next_with_data (&next0, p0, sizeof (c0[0]));
154           c1 = vnet_feature_next_with_data (&next1, p1, sizeof (c1[0]));
155
156           mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
157           mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie;
158
159           leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
160           leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address);
161
162           leaf0 =
163             ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
164           leaf1 =
165             ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 2);
166
167           leaf0 =
168             ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
169           leaf1 =
170             ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 3);
171
172           lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
173           lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
174
175           lb0 = load_balance_get (lb_index0);
176           lb1 = load_balance_get (lb_index1);
177
178           /* Pass multicast. */
179           pass0 = ip4_address_is_multicast (&ip0->src_address)
180             || ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
181           pass1 = ip4_address_is_multicast (&ip1->src_address)
182             || ip1->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
183
184           if (IP4_SOURCE_CHECK_REACHABLE_VIA_RX == source_check_type)
185             {
186               pass0 |= fib_urpf_check (lb0->lb_urpf,
187                                        vnet_buffer (p0)->sw_if_index
188                                        [VLIB_RX]);
189               pass1 |=
190                 fib_urpf_check (lb1->lb_urpf,
191                                 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
192             }
193           else
194             {
195               pass0 |= fib_urpf_check_size (lb0->lb_urpf);
196               pass1 |= fib_urpf_check_size (lb1->lb_urpf);
197             }
198           next0 = (pass0 ? next0 : IP4_SOURCE_CHECK_NEXT_DROP);
199           next1 = (pass1 ? next1 : IP4_SOURCE_CHECK_NEXT_DROP);
200
201           p0->error =
202             error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
203           p1->error =
204             error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
205
206           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
207                                            to_next, n_left_to_next,
208                                            pi0, pi1, next0, next1);
209         }
210
211       while (n_left_from > 0 && n_left_to_next > 0)
212         {
213           vlib_buffer_t *p0;
214           ip4_header_t *ip0;
215           ip4_fib_mtrie_t *mtrie0;
216           ip4_fib_mtrie_leaf_t leaf0;
217           ip4_source_check_config_t *c0;
218           u32 pi0, next0, pass0, lb_index0;
219           const load_balance_t *lb0;
220
221           pi0 = from[0];
222           to_next[0] = pi0;
223           from += 1;
224           to_next += 1;
225           n_left_from -= 1;
226           n_left_to_next -= 1;
227
228           p0 = vlib_get_buffer (vm, pi0);
229           ip0 = vlib_buffer_get_current (p0);
230
231           c0 = vnet_feature_next_with_data (&next0, p0, sizeof (c0[0]));
232
233           mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
234
235           leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
236
237           leaf0 =
238             ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
239
240           leaf0 =
241             ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
242
243           lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
244
245           lb0 = load_balance_get (lb_index0);
246
247           /* Pass multicast. */
248           pass0 = ip4_address_is_multicast (&ip0->src_address)
249             || ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
250
251           if (IP4_SOURCE_CHECK_REACHABLE_VIA_RX == source_check_type)
252             {
253               pass0 |= fib_urpf_check (lb0->lb_urpf,
254                                        vnet_buffer (p0)->sw_if_index
255                                        [VLIB_RX]);
256             }
257           else
258             {
259               pass0 |= fib_urpf_check_size (lb0->lb_urpf);
260             }
261
262           next0 = (pass0 ? next0 : IP4_SOURCE_CHECK_NEXT_DROP);
263           p0->error =
264             error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
265
266           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
267                                            to_next, n_left_to_next,
268                                            pi0, next0);
269         }
270
271       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
272     }
273
274   return frame->n_vectors;
275 }
276
277 static uword
278 ip4_source_check_reachable_via_any (vlib_main_t * vm,
279                                     vlib_node_runtime_t * node,
280                                     vlib_frame_t * frame)
281 {
282   return ip4_source_check_inline (vm, node, frame,
283                                   IP4_SOURCE_CHECK_REACHABLE_VIA_ANY);
284 }
285
286 static uword
287 ip4_source_check_reachable_via_rx (vlib_main_t * vm,
288                                    vlib_node_runtime_t * node,
289                                    vlib_frame_t * frame)
290 {
291   return ip4_source_check_inline (vm, node, frame,
292                                   IP4_SOURCE_CHECK_REACHABLE_VIA_RX);
293 }
294
295 /* *INDENT-OFF* */
296 VLIB_REGISTER_NODE (ip4_check_source_reachable_via_any) = {
297   .function = ip4_source_check_reachable_via_any,
298   .name = "ip4-source-check-via-any",
299   .vector_size = sizeof (u32),
300
301   .n_next_nodes = IP4_SOURCE_CHECK_N_NEXT,
302   .next_nodes = {
303     [IP4_SOURCE_CHECK_NEXT_DROP] = "ip4-drop",
304   },
305
306   .format_buffer = format_ip4_header,
307   .format_trace = format_ip4_source_check_trace,
308 };
309 /* *INDENT-ON* */
310
311 VLIB_NODE_FUNCTION_MULTIARCH (ip4_check_source_reachable_via_any,
312                               ip4_source_check_reachable_via_any);
313
314 /* *INDENT-OFF* */
315 VLIB_REGISTER_NODE (ip4_check_source_reachable_via_rx) = {
316   .function = ip4_source_check_reachable_via_rx,
317   .name = "ip4-source-check-via-rx",
318   .vector_size = sizeof (u32),
319
320   .n_next_nodes = IP4_SOURCE_CHECK_N_NEXT,
321   .next_nodes = {
322     [IP4_SOURCE_CHECK_NEXT_DROP] = "ip4-drop",
323   },
324
325   .format_buffer = format_ip4_header,
326   .format_trace = format_ip4_source_check_trace,
327 };
328 /* *INDENT-ON* */
329
330 VLIB_NODE_FUNCTION_MULTIARCH (ip4_check_source_reachable_via_rx,
331                               ip4_source_check_reachable_via_rx);
332
333 static clib_error_t *
334 set_ip_source_check (vlib_main_t * vm,
335                      unformat_input_t * input, vlib_cli_command_t * cmd)
336 {
337   unformat_input_t _line_input, *line_input = &_line_input;
338   vnet_main_t *vnm = vnet_get_main ();
339   ip4_main_t *im = &ip4_main;
340   clib_error_t *error = 0;
341   u32 sw_if_index, is_del;
342   ip4_source_check_config_t config;
343   char *feature_name = "ip4-source-check-via-rx";
344
345   sw_if_index = ~0;
346   is_del = 0;
347
348   if (!unformat_user (input, unformat_line_input, line_input))
349     return 0;
350
351   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
352     {
353       if (unformat_user
354           (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
355         ;
356       else if (unformat (line_input, "del"))
357         is_del = 1;
358       else if (unformat (line_input, "loose"))
359         feature_name = "ip4-source-check-via-any";
360       else
361         {
362           error = unformat_parse_error (line_input);
363           goto done;
364         }
365     }
366
367   if (~0 == sw_if_index)
368     {
369       error = clib_error_return (0, "unknown interface `%U'",
370                                  format_unformat_error, line_input);
371       goto done;
372     }
373
374   config.fib_index = im->fib_index_by_sw_if_index[sw_if_index];
375   vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
376                                is_del == 0, &config, sizeof (config));
377 done:
378   unformat_free (line_input);
379
380   return error;
381 }
382
383 /*?
384  * This command adds the 'ip4-source-check-via-rx' graph node for
385  * a given interface. By adding the IPv4 source check graph node to
386  * an interface, the code verifies that the source address of incoming
387  * unicast packets are reachable over the incoming interface. Two flavours
388  * are supported (the default is strict):
389  * - loose: accept ingress packet if there is a route to reach the source
390  * - strict: accept ingress packet if it arrived on an interface which
391  *          the route to the source uses. i.e. an interface that the source
392  *          is reachable via.
393  *
394  * @cliexpar
395  * @parblock
396  * Example of graph node before range checking is enabled:
397  * @cliexstart{show vlib graph ip4-source-check-via-rx}
398  *            Name                      Next                    Previous
399  * ip4-source-check-via-rx         ip4-drop [0]
400  * @cliexend
401  *
402  * Example of how to enable unicast source checking on an interface:
403  * @cliexcmd{set interface ip source-check GigabitEthernet2/0/0 loose}
404  *
405  * Example of graph node after range checking is enabled:
406  * @cliexstart{show vlib graph ip4-source-check-via-rx}
407  *            Name                      Next                    Previous
408  * ip4-source-check-via-rx         ip4-drop [0]           ip4-input-no-checksum
409  *                           ip4-source-and-port-range-         ip4-input
410  * @cliexend
411  *
412  * Example of how to display the feature enabed on an interface:
413  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
414  * IP feature paths configured on GigabitEthernet2/0/0...
415  *
416  * ipv4 unicast:
417  *   ip4-source-check-via-rx
418  *   ip4-lookup
419  *
420  * ipv4 multicast:
421  *   ip4-lookup-multicast
422  *
423  * ipv4 multicast:
424  *   interface-output
425  *
426  * ipv6 unicast:
427  *   ip6-lookup
428  *
429  * ipv6 multicast:
430  *   ip6-lookup
431  *
432  * ipv6 multicast:
433  *   interface-output
434  * @cliexend
435  *
436  * Example of how to disable unicast source checking on an interface:
437  * @cliexcmd{set interface ip source-check GigabitEthernet2/0/0 del}
438  * @endparblock
439 ?*/
440 /* *INDENT-OFF* */
441 VLIB_CLI_COMMAND (set_interface_ip_source_check_command, static) = {
442   .path = "set interface ip source-check",
443   .function = set_ip_source_check,
444   .short_help = "set interface ip source-check <interface> [strict|loose] [del]",
445 };
446 /* *INDENT-ON* */
447
448 static clib_error_t *
449 ip_source_check_accept (vlib_main_t * vm,
450                         unformat_input_t * input, vlib_cli_command_t * cmd)
451 {
452   unformat_input_t _line_input, *line_input = &_line_input;
453   fib_prefix_t pfx = {
454     .fp_proto = FIB_PROTOCOL_IP4,
455   };
456   clib_error_t *error = NULL;
457   u32 table_id, is_add, fib_index;
458
459   is_add = 1;
460   table_id = ~0;
461
462   /* Get a line of input. */
463   if (!unformat_user (input, unformat_line_input, line_input))
464     return 0;
465
466   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
467     {
468       if (unformat (line_input, "table %d", &table_id))
469         ;
470       else if (unformat (line_input, "del"))
471         is_add = 0;
472       else if (unformat (line_input, "add"))
473         is_add = 1;
474       else if (unformat (line_input, "%U/%d",
475                          unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len))
476         pfx.fp_proto = FIB_PROTOCOL_IP4;
477       else
478         {
479           error = unformat_parse_error (line_input);
480           goto done;
481         }
482     }
483
484   if (~0 != table_id)
485     {
486       fib_index = fib_table_find (pfx.fp_proto, table_id);
487       if (~0 == fib_index)
488         {
489           error = clib_error_return (0, "Nonexistent table id %d", table_id);
490           goto done;
491         }
492     }
493   else
494     {
495       fib_index = 0;
496     }
497
498   if (is_add)
499     {
500       fib_table_entry_special_add (fib_index,
501                                    &pfx,
502                                    FIB_SOURCE_URPF_EXEMPT,
503                                    FIB_ENTRY_FLAG_DROP);
504     }
505   else
506     {
507       fib_table_entry_special_remove (fib_index,
508                                       &pfx, FIB_SOURCE_URPF_EXEMPT);
509     }
510
511 done:
512   unformat_free (line_input);
513
514   return error;
515 }
516
517 /*?
518  * Add an exemption for a prefix to pass the Unicast Reverse Path
519  * Forwarding (uRPF) loose check. This is for testing purposes only.
520  * If the '<em>table</em>' is not enter it is defaulted to 0. Default
521  * is to '<em>add</em>'. VPP always performs a loose uRPF check for
522  * for-us traffic.
523  *
524  * @cliexpar
525  * Example of how to add a uRPF exception to a FIB table to pass the
526  * loose RPF tests:
527  * @cliexcmd{ip urpf-accept table 7 add}
528 ?*/
529 /* *INDENT-OFF* */
530 VLIB_CLI_COMMAND (ip_source_check_accept_command, static) = {
531   .path = "ip urpf-accept",
532   .function = ip_source_check_accept,
533   .short_help = "ip urpf-accept [table <table-id>] [add|del]",
534 };
535 /* *INDENT-ON* */
536
537
538 /* Dummy init function to get us linked in. */
539 clib_error_t *
540 ip4_source_check_init (vlib_main_t * vm)
541 {
542   return 0;
543 }
544
545 VLIB_INIT_FUNCTION (ip4_source_check_init);
546
547 /*
548  * fd.io coding-style-patch-verification: ON
549  *
550  * Local Variables:
551  * eval: (c-set-style "gnu")
552  * End:
553  */