ipsec: IPSec protection for multi-point tunnel interfaces
[vpp.git] / src / vnet / mfib / mfib_forward.c
1 /*
2  * Copyright (c) 2016 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 #include <vnet/mfib/mfib_itf.h>
17 #include <vnet/mfib/mfib_entry.h>
18 #include <vnet/dpo/replicate_dpo.h>
19 #include <vnet/mfib/ip4_mfib.h>
20 #include <vnet/mfib/ip6_mfib.h>
21 #include <vnet/mfib/mfib_signal.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/fib/ip6_fib.h>
24
25 #include <vnet/ip/ip4.h>
26 #include <vnet/vnet.h>
27
28 typedef struct mfib_forward_lookup_trace_t_ {
29     u32 entry_index;
30     u32 fib_index;
31 } mfib_forward_lookup_trace_t;
32
33 static u8 *
34 format_mfib_forward_lookup_trace (u8 * s, va_list * args)
35 {
36     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38     mfib_forward_lookup_trace_t * t = va_arg (*args, mfib_forward_lookup_trace_t *);
39
40     s = format (s, "fib %d entry %d", t->fib_index, t->entry_index);
41     return s;
42 }
43
44 /* Common trace function for all ip4-forward next nodes. */
45 static void
46 mfib_forward_lookup_trace (vlib_main_t * vm,
47                            vlib_node_runtime_t * node,
48                            vlib_frame_t * frame)
49 {
50     u32 * from, n_left;
51     ip4_main_t * im = &ip4_main;
52
53     n_left = frame->n_vectors;
54     from = vlib_frame_vector_args (frame);
55
56     while (n_left >= 4)
57     {
58         mfib_forward_lookup_trace_t * t0, * t1;
59         vlib_buffer_t * b0, * b1;
60         u32 bi0, bi1;
61
62         /* Prefetch next iteration. */
63         vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
64         vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
65
66         bi0 = from[0];
67         bi1 = from[1];
68
69         b0 = vlib_get_buffer (vm, bi0);
70         b1 = vlib_get_buffer (vm, bi1);
71
72         if (b0->flags & VLIB_BUFFER_IS_TRACED)
73         {
74             t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
75             t0->entry_index = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
76             t0->fib_index = vec_elt (im->mfib_index_by_sw_if_index,
77                                      vnet_buffer(b1)->sw_if_index[VLIB_RX]);
78         }
79         if (b1->flags & VLIB_BUFFER_IS_TRACED)
80         {
81             t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
82             t1->entry_index = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
83             t1->fib_index = vec_elt (im->mfib_index_by_sw_if_index,
84                                      vnet_buffer(b1)->sw_if_index[VLIB_RX]);
85         }
86         from += 2;
87         n_left -= 2;
88     }
89
90     while (n_left >= 1)
91     {
92         mfib_forward_lookup_trace_t * t0;
93         vlib_buffer_t * b0;
94         u32 bi0;
95
96         bi0 = from[0];
97
98         b0 = vlib_get_buffer (vm, bi0);
99
100         if (b0->flags & VLIB_BUFFER_IS_TRACED)
101         {
102             t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
103             t0->entry_index = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
104             t0->fib_index = vec_elt (im->mfib_index_by_sw_if_index,
105                                      vnet_buffer(b0)->sw_if_index[VLIB_RX]);
106         }
107         from += 1;
108         n_left -= 1;
109     }
110 }
111
112 typedef enum mfib_forward_lookup_next_t_ {
113     MFIB_FORWARD_LOOKUP_NEXT_RPF,
114     MFIB_FORWARD_LOOKUP_N_NEXT,
115 } mfib_forward_lookup_next_t;
116
117 static uword
118 mfib_forward_lookup (vlib_main_t * vm,
119                      vlib_node_runtime_t * node,
120                      vlib_frame_t * frame,
121                      int is_v4)
122 {
123     u32 n_left_from, n_left_to_next, * from, * to_next;
124
125     from = vlib_frame_vector_args (frame);
126     n_left_from = frame->n_vectors;
127
128     while (n_left_from > 0)
129     {
130         vlib_get_next_frame (vm, node, MFIB_FORWARD_LOOKUP_NEXT_RPF,
131                              to_next, n_left_to_next);
132
133         while (n_left_from > 0 && n_left_to_next > 0)
134         {
135             fib_node_index_t mfei0;
136             vlib_buffer_t * p0;
137             u32 fib_index0;
138             u32 pi0;
139
140             pi0 = from[0];
141             to_next[0] = pi0;
142             from += 1;
143             to_next += 1;
144             n_left_to_next -= 1;
145             n_left_from -= 1;
146
147             p0 = vlib_get_buffer (vm, pi0);
148
149             if (is_v4)
150             {
151                 ip4_header_t * ip0;
152
153                 fib_index0 = vec_elt (ip4_main.mfib_index_by_sw_if_index,
154                                       vnet_buffer(p0)->sw_if_index[VLIB_RX]);
155                 ip0 = vlib_buffer_get_current (p0);
156                 mfei0 = ip4_mfib_table_lookup(ip4_mfib_get(fib_index0),
157                                               &ip0->src_address,
158                                               &ip0->dst_address,
159                                               64);
160             }
161             else
162             {
163                 ip6_header_t * ip0;
164
165                 fib_index0 = vec_elt (ip6_main.mfib_index_by_sw_if_index,
166                                       vnet_buffer(p0)->sw_if_index[VLIB_RX]);
167                 ip0 = vlib_buffer_get_current (p0);
168                 mfei0 = ip6_mfib_table_fwd_lookup(ip6_mfib_get(fib_index0),
169                                                   &ip0->src_address,
170                                                   &ip0->dst_address);
171             }
172
173             vnet_buffer (p0)->ip.adj_index[VLIB_TX] = mfei0;
174         }
175
176         vlib_put_next_frame(vm, node,
177                             MFIB_FORWARD_LOOKUP_NEXT_RPF,
178                             n_left_to_next);
179     }
180
181     if (node->flags & VLIB_NODE_FLAG_TRACE)
182         mfib_forward_lookup_trace(vm, node, frame);
183
184     return frame->n_vectors;
185 }
186
187 VLIB_NODE_FN (ip4_mfib_forward_lookup_node) (vlib_main_t * vm,
188                          vlib_node_runtime_t * node,
189                          vlib_frame_t * frame)
190 {
191     return (mfib_forward_lookup (vm, node, frame, 1));
192 }
193
194 VLIB_REGISTER_NODE (ip4_mfib_forward_lookup_node) = {
195     .name = "ip4-mfib-forward-lookup",
196     .vector_size = sizeof (u32),
197
198     .format_trace = format_mfib_forward_lookup_trace,
199
200     .n_next_nodes = MFIB_FORWARD_LOOKUP_N_NEXT,
201     .next_nodes = {
202         [MFIB_FORWARD_LOOKUP_NEXT_RPF] = "ip4-mfib-forward-rpf",
203     },
204 };
205
206 VLIB_NODE_FN (ip6_mfib_forward_lookup_node) (vlib_main_t * vm,
207                          vlib_node_runtime_t * node,
208                          vlib_frame_t * frame)
209 {
210     return (mfib_forward_lookup (vm, node, frame, 0));
211 }
212
213 VLIB_REGISTER_NODE (ip6_mfib_forward_lookup_node) = {
214     .name = "ip6-mfib-forward-lookup",
215     .vector_size = sizeof (u32),
216
217     .format_trace = format_mfib_forward_lookup_trace,
218
219     .n_next_nodes = MFIB_FORWARD_LOOKUP_N_NEXT,
220     .next_nodes = {
221         [MFIB_FORWARD_LOOKUP_NEXT_RPF] = "ip6-mfib-forward-rpf",
222     },
223 };
224
225
226 typedef struct mfib_forward_rpf_trace_t_ {
227     u32 entry_index;
228     u32 sw_if_index;
229     mfib_itf_flags_t itf_flags;
230 } mfib_forward_rpf_trace_t;
231
232 typedef enum mfib_forward_rpf_next_t_ {
233     MFIB_FORWARD_RPF_NEXT_DROP,
234     MFIB_FORWARD_RPF_N_NEXT,
235 } mfib_forward_rpf_next_t;
236
237 static u8 *
238 format_mfib_forward_rpf_trace (u8 * s, va_list * args)
239 {
240     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
241     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
242     mfib_forward_rpf_trace_t * t = va_arg (*args, mfib_forward_rpf_trace_t *);
243
244     s = format (s, "entry %d", t->entry_index);
245     s = format (s, " itf %d", t->sw_if_index);
246     s = format (s, " flags %U", format_mfib_itf_flags, t->itf_flags);
247
248     return s;
249 }
250
251 static int
252 mfib_forward_connected_check (vlib_buffer_t * b0,
253                               u32 sw_if_index,
254                               int is_v4)
255 {
256     /*
257      * Lookup the source of the IP packet in the
258      * FIB. return true if the entry is attached.
259      */
260     index_t lbi0;
261
262     if (is_v4)
263     {
264         load_balance_t *lb0;
265         ip4_header_t *ip0;
266
267         ip0 = vlib_buffer_get_current(b0);
268
269         lbi0 = ip4_fib_forwarding_lookup(
270                    ip4_fib_table_get_index_for_sw_if_index(
271                        sw_if_index),
272                    &ip0->src_address);
273         lb0 = load_balance_get(lbi0);
274
275         return (FIB_ENTRY_FLAG_ATTACHED &
276                 lb0->lb_fib_entry_flags);
277     }
278     else
279     {
280         ASSERT(0);
281     }
282     return (0);
283 }
284
285 static void
286 mfib_forward_itf_signal (vlib_main_t *vm,
287                          const mfib_entry_t *mfe,
288                          mfib_itf_t *mfi,
289                          vlib_buffer_t *b0)
290 {
291     mfib_itf_flags_t old_flags;
292
293     old_flags = clib_atomic_fetch_or(&mfi->mfi_flags,
294                                      MFIB_ITF_FLAG_SIGNAL_PRESENT);
295
296     if (!(old_flags & MFIB_ITF_FLAG_SIGNAL_PRESENT))
297     {
298         /*
299          * we were the lucky ones to set the signal present flag
300          */
301         if (!(old_flags & MFIB_ITF_FLAG_DONT_PRESERVE))
302         {
303             /*
304              * preserve a copy of the packet for the control
305              * plane to examine.
306              * Only allow one preserved packet at at time, since
307              * when the signal present flag is cleared so is the
308              * preserved packet.
309              */
310             mfib_signal_push(mfe, mfi, b0);
311         }
312         else
313         {
314             /*
315              *  The control plane just wants the signal, not the packet as well
316              */
317             mfib_signal_push(mfe, mfi, NULL);
318         }
319     }
320     /*
321      * else
322      *   there is already a signal present on this interface that the
323      *   control plane has not yet acknowledged
324      */
325 }
326
327 always_inline uword
328 mfib_forward_rpf (vlib_main_t * vm,
329                   vlib_node_runtime_t * node,
330                   vlib_frame_t * frame,
331                   int is_v4)
332 {
333     u32 n_left_from, n_left_to_next, * from, * to_next;
334     mfib_forward_rpf_next_t next;
335     vlib_node_runtime_t *error_node;
336
337     if (is_v4)
338         error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
339     else
340         error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
341     from = vlib_frame_vector_args (frame);
342     n_left_from = frame->n_vectors;
343     next = MFIB_FORWARD_RPF_NEXT_DROP;
344
345     while (n_left_from > 0)
346     {
347         vlib_get_next_frame (vm, node, next,
348                              to_next, n_left_to_next);
349
350         while (n_left_from > 0 && n_left_to_next > 0)
351         {
352             fib_node_index_t mfei0;
353             const mfib_entry_t *mfe0;
354             mfib_itf_t *mfi0;
355             vlib_buffer_t * b0;
356             u32 pi0, next0;
357             mfib_itf_flags_t iflags0;
358             mfib_entry_flags_t eflags0;
359             u8 error0;
360
361             pi0 = from[0];
362             to_next[0] = pi0;
363             from += 1;
364             to_next += 1;
365             n_left_to_next -= 1;
366             n_left_from -= 1;
367
368             error0 = IP4_ERROR_NONE;
369             b0 = vlib_get_buffer (vm, pi0);
370             mfei0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
371             mfe0 = mfib_entry_get(mfei0);
372             mfi0 = mfib_entry_get_itf(mfe0,
373                                       vnet_buffer(b0)->sw_if_index[VLIB_RX]);
374
375             /*
376              * throughout this function we are 'PREDICT' optimising
377              * for the case of throughput traffic that is not replicated
378              * to the host stack nor sets local flags
379              */
380
381             /*
382              * If the mfib entry has a configured RPF-ID check that
383              * in preference to an interface based RPF
384              */
385             if (MFIB_RPF_ID_NONE != mfe0->mfe_rpf_id)
386             {
387                 iflags0 = (mfe0->mfe_rpf_id == vnet_buffer(b0)->ip.rpf_id ?
388                            MFIB_ITF_FLAG_ACCEPT :
389                            MFIB_ITF_FLAG_NONE);
390             }
391             else
392             {
393                 if (PREDICT_TRUE(NULL != mfi0))
394                 {
395                     iflags0 = mfi0->mfi_flags;
396                 }
397                 else
398                 {
399                     iflags0 = MFIB_ITF_FLAG_NONE;
400                 }
401             }
402             eflags0 = mfe0->mfe_flags;
403
404             if (PREDICT_FALSE(eflags0 & MFIB_ENTRY_FLAG_CONNECTED))
405             {
406                 /*
407                  * lookup the source in the unicast FIB - check it
408                  * matches a connected.
409                  */
410                 if (mfib_forward_connected_check(
411                         b0,
412                         vnet_buffer(b0)->sw_if_index[VLIB_RX],
413                         is_v4))
414                 {
415                     mfib_forward_itf_signal(vm, mfe0, mfi0, b0);
416                 }
417             }
418             if (PREDICT_FALSE((eflags0 & MFIB_ENTRY_FLAG_SIGNAL) ^
419                               (iflags0 & MFIB_ITF_FLAG_NEGATE_SIGNAL)))
420             {
421                 /*
422                  * Entry signal XOR interface negate-signal
423                  */
424                 if (NULL != mfi0)
425                 {
426                     mfib_forward_itf_signal(vm, mfe0, mfi0, b0);
427                 }
428             }
429
430             if (PREDICT_TRUE((iflags0 & MFIB_ITF_FLAG_ACCEPT) ||
431                              (eflags0 & MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF)))
432             {
433                 /*
434                  * This interface is accepting packets for the matching entry
435                  */
436                 next0 = mfe0->mfe_rep.dpoi_next_node;
437
438                 vnet_buffer(b0)->ip.adj_index[VLIB_TX] =
439                     mfe0->mfe_rep.dpoi_index;
440             }
441             else
442             {
443                 next0 = MFIB_FORWARD_RPF_NEXT_DROP;
444                 error0 = IP4_ERROR_RPF_FAILURE;
445             }
446
447             b0->error = error0 ? error_node->errors[error0] : 0;
448
449             if (b0->flags & VLIB_BUFFER_IS_TRACED)
450             {
451                 mfib_forward_rpf_trace_t *t0;
452
453                 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
454                 t0->entry_index = mfei0;
455                 t0->itf_flags = iflags0;
456                 if (NULL == mfi0)
457                 {
458                     t0->sw_if_index = ~0;
459                 }
460                 else
461                 {
462                     t0->sw_if_index = mfi0->mfi_sw_if_index;
463                 }
464             }
465             vlib_validate_buffer_enqueue_x1 (vm, node, next,
466                                              to_next, n_left_to_next,
467                                              pi0, next0);
468         }
469
470         vlib_put_next_frame(vm, node, next, n_left_to_next);
471     }
472
473     return frame->n_vectors;
474 }
475
476 VLIB_NODE_FN (ip4_mfib_forward_rpf_node) (vlib_main_t * vm,
477                       vlib_node_runtime_t * node,
478                       vlib_frame_t * frame)
479 {
480     return (mfib_forward_rpf(vm, node, frame, 1));
481 }
482
483
484 VLIB_REGISTER_NODE (ip4_mfib_forward_rpf_node) = {
485     .name = "ip4-mfib-forward-rpf",
486     .vector_size = sizeof (u32),
487
488     .format_trace = format_mfib_forward_rpf_trace,
489
490     .n_next_nodes = MFIB_FORWARD_RPF_N_NEXT,
491     .next_nodes = {
492         [MFIB_FORWARD_RPF_NEXT_DROP] = "ip4-drop",
493     },
494 };
495
496 VLIB_NODE_FN (ip6_mfib_forward_rpf_node) (vlib_main_t * vm,
497                       vlib_node_runtime_t * node,
498                       vlib_frame_t * frame)
499 {
500     return (mfib_forward_rpf(vm, node, frame, 0));
501 }
502
503
504 VLIB_REGISTER_NODE (ip6_mfib_forward_rpf_node) = {
505     .name = "ip6-mfib-forward-rpf",
506     .vector_size = sizeof (u32),
507
508     .format_trace = format_mfib_forward_rpf_trace,
509
510     .n_next_nodes = MFIB_FORWARD_RPF_N_NEXT,
511     .next_nodes = {
512         [MFIB_FORWARD_RPF_NEXT_DROP] = "ip6-drop",
513     },
514 };
515