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