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