gbp: add anonymous l3-out external interfaces
[vpp.git] / src / plugins / gbp / gbp_policy_node.c
1 /*
2  * Copyright (c) 2018 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 <plugins/gbp/gbp.h>
17 #include <plugins/gbp/gbp_classify.h>
18 #include <plugins/gbp/gbp_policy_dpo.h>
19 #include <plugins/gbp/gbp_bridge_domain.h>
20 #include <plugins/gbp/gbp_ext_itf.h>
21
22 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
23 #include <vnet/vxlan-gbp/vxlan_gbp.h>
24
25 #define foreach_gbp_policy                      \
26   _(DENY,    "deny")                            \
27   _(REFLECTION, "reflection")
28
29 typedef enum
30 {
31 #define _(sym,str) GBP_POLICY_ERROR_##sym,
32   foreach_gbp_policy_error
33 #undef _
34     GBP_POLICY_N_ERROR,
35 } gbp_policy_error_t;
36
37 static char *gbp_policy_error_strings[] = {
38 #define _(sym,string) string,
39   foreach_gbp_policy_error
40 #undef _
41 };
42
43 typedef enum
44 {
45   GBP_POLICY_NEXT_DROP,
46   GBP_POLICY_N_NEXT,
47 } gbp_policy_next_t;
48
49 /**
50  * per-packet trace data
51  */
52 typedef struct gbp_policy_trace_t_
53 {
54   /* per-pkt trace data */
55   gbp_scope_t scope;
56   sclass_t sclass;
57   sclass_t dclass;
58   u32 acl_index;
59   u32 allowed;
60   u32 flags;
61 } gbp_policy_trace_t;
62
63 always_inline dpo_proto_t
64 ethertype_to_dpo_proto (u16 etype)
65 {
66   etype = clib_net_to_host_u16 (etype);
67
68   switch (etype)
69     {
70     case ETHERNET_TYPE_IP4:
71       return (DPO_PROTO_IP4);
72     case ETHERNET_TYPE_IP6:
73       return (DPO_PROTO_IP6);
74     }
75
76   return (DPO_PROTO_NONE);
77 }
78
79 always_inline u32
80 gbp_rule_l2_redirect (const gbp_rule_t * gu, vlib_buffer_t * b0)
81 {
82   const ethernet_header_t *eth0;
83   const dpo_id_t *dpo;
84   dpo_proto_t dproto;
85
86   eth0 = vlib_buffer_get_current (b0);
87   /* pop the ethernet header to prepare for L3 rewrite */
88   vlib_buffer_advance (b0, vnet_buffer (b0)->l2.l2_len);
89
90   dproto = ethertype_to_dpo_proto (eth0->type);
91   dpo = &gu->gu_dpo[GBP_POLICY_NODE_L2][dproto];
92
93   /* save the LB index for the next node and reset the IP flow hash
94    * so it's recalculated */
95   vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
96   vnet_buffer (b0)->ip.flow_hash = 0;
97
98   return (dpo->dpoi_next_node);
99 }
100
101 always_inline u8
102 gbp_policy_is_ethertype_allowed (const gbp_contract_t * gc0, u16 ethertype)
103 {
104   u16 *et;
105
106   vec_foreach (et, gc0->gc_allowed_ethertypes)
107   {
108     if (*et == ethertype)
109       return (1);
110   }
111   return (0);
112 }
113
114 static_always_inline gbp_policy_next_t
115 gbp_policy_l2_feature_next (gbp_policy_main_t * gpm, vlib_buffer_t * b,
116                             const gbp_policy_type_t type)
117 {
118   u32 feat_bit;
119
120   switch (type)
121     {
122     case GBP_POLICY_PORT:
123       feat_bit = L2OUTPUT_FEAT_GBP_POLICY_PORT;
124       break;
125     case GBP_POLICY_MAC:
126       feat_bit = L2OUTPUT_FEAT_GBP_POLICY_MAC;
127       break;
128     case GBP_POLICY_LPM:
129       feat_bit = L2OUTPUT_FEAT_GBP_POLICY_LPM;
130       break;
131     default:
132       return GBP_POLICY_NEXT_DROP;
133     }
134
135   return vnet_l2_feature_next (b, gpm->l2_output_feat_next[type], feat_bit);
136 }
137
138 static uword
139 gbp_policy_inline (vlib_main_t * vm,
140                    vlib_node_runtime_t * node,
141                    vlib_frame_t * frame, const gbp_policy_type_t type)
142 {
143   gbp_main_t *gm = &gbp_main;
144   gbp_policy_main_t *gpm = &gbp_policy_main;
145   u32 n_left_from, *from, *to_next;
146   u32 next_index, thread_index;
147   u32 n_allow_intra, n_allow_a_bit, n_allow_sclass_1;
148
149   next_index = 0;
150   n_left_from = frame->n_vectors;
151   from = vlib_frame_vector_args (frame);
152   thread_index = vm->thread_index;
153   n_allow_intra = n_allow_a_bit = n_allow_sclass_1 = 0;
154
155   while (n_left_from > 0)
156     {
157       u32 n_left_to_next;
158
159       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
160
161       while (n_left_from > 0 && n_left_to_next > 0)
162         {
163           const ethernet_header_t *h0;
164           const gbp_endpoint_t *ge0;
165           const gbp_contract_t *gc0;
166           gbp_policy_next_t next0;
167           gbp_contract_key_t key0;
168           u32 bi0, sw_if_index0;
169           vlib_buffer_t *b0;
170           index_t gci0;
171
172           gc0 = NULL;
173           next0 = GBP_POLICY_NEXT_DROP;
174           bi0 = from[0];
175           to_next[0] = bi0;
176           from += 1;
177           to_next += 1;
178           n_left_from -= 1;
179           n_left_to_next -= 1;
180
181           b0 = vlib_get_buffer (vm, bi0);
182           h0 = vlib_buffer_get_current (b0);
183           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
184
185           /* zero out the key to ensure the pad space is clear */
186           key0.as_u64 = 0;
187
188           /*
189            * Reflection check; in and out on an ivxlan tunnel
190            */
191           if ((~0 != vxlan_gbp_tunnel_by_sw_if_index (sw_if_index0)) &&
192               (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_R))
193             {
194               goto trace;
195             }
196
197           /*
198            * If the A-bit is set then policy has already been applied
199            * and we skip enforcement here.
200            */
201           if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_A)
202             {
203               next0 = gbp_policy_l2_feature_next (gpm, b0, type);
204               n_allow_a_bit++;
205               key0.as_u64 = ~0;
206               goto trace;
207             }
208
209           /*
210            * determine the src and dst EPG
211            */
212
213           key0.gck_dst = SCLASS_INVALID;
214
215           if (GBP_POLICY_LPM == type)
216             {
217               const ip4_address_t *ip4 = 0;
218               const ip6_address_t *ip6 = 0;
219               const dpo_proto_t proto =
220                 gbp_classify_get_ip_address (h0, &ip4, &ip6,
221                                              GBP_CLASSIFY_GET_IP_DST);
222               if (PREDICT_TRUE (DPO_PROTO_NONE != proto))
223                 {
224                   const gbp_ext_itf_t *ext_itf =
225                     gbp_ext_itf_get (sw_if_index0);
226                   const gbp_policy_dpo_t *gpd =
227                     gbp_classify_get_gpd (ip4, ip6,
228                                           ext_itf->gx_fib_index[proto]);
229                   if (gpd)
230                     key0.gck_dst = gpd->gpd_sclass;
231                 }
232             }
233           else
234             {
235               if (GBP_POLICY_PORT == type)
236                 ge0 = gbp_endpoint_find_itf (sw_if_index0);
237               else
238                 ge0 = gbp_endpoint_find_mac (h0->dst_address,
239                                              vnet_buffer (b0)->l2.bd_index);
240               if (NULL != ge0)
241                 key0.gck_dst = ge0->ge_fwd.gef_sclass;
242             }
243
244           if (SCLASS_INVALID == key0.gck_dst)
245             {
246               /* If you cannot determine the destination EP then drop */
247               b0->error = node->errors[GBP_POLICY_ERROR_DROP_NO_DCLASS];
248               goto trace;
249             }
250           key0.gck_src = vnet_buffer2 (b0)->gbp.sclass;
251
252           if (SCLASS_INVALID != key0.gck_src)
253             {
254               if (PREDICT_FALSE (key0.gck_src == key0.gck_dst))
255                 {
256                   /*
257                    * intra-epg allowed
258                    */
259                   next0 = gbp_policy_l2_feature_next (gpm, b0, type);
260                   vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
261                   n_allow_intra++;
262                 }
263               else if (PREDICT_FALSE (key0.gck_src == 1 || key0.gck_dst == 1))
264                 {
265                   /*
266                    * sclass or dclass 1 allowed
267                    */
268                   next0 = gbp_policy_l2_feature_next (gpm, b0, type);
269                   vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
270                   n_allow_sclass_1++;
271                 }
272               else
273                 {
274                   key0.gck_scope =
275                     gbp_bridge_domain_get_scope (vnet_buffer (b0)->
276                                                  l2.bd_index);
277                   gci0 = gbp_contract_find (&key0);
278
279                   if (INDEX_INVALID != gci0)
280                     {
281                       u32 rule_match_p0, trace_bitmap0;
282                       fa_5tuple_opaque_t pkt_5tuple0;
283                       u32 acl_pos_p0, acl_match_p0;
284                       u8 is_ip60, l2_len0, action0;
285                       const gbp_rule_t *gu;
286                       u16 ether_type0;
287                       const u8 *h0;
288
289                       vlib_prefetch_combined_counter
290                         (&gbp_contract_drop_counters, thread_index, gci0);
291                       vlib_prefetch_combined_counter
292                         (&gbp_contract_permit_counters, thread_index, gci0);
293
294                       action0 = 0;
295                       gc0 = gbp_contract_get (gci0);
296                       l2_len0 = vnet_buffer (b0)->l2.l2_len;
297                       h0 = vlib_buffer_get_current (b0);
298
299                       ether_type0 = *(u16 *) (h0 + l2_len0 - 2);
300
301                       if (!gbp_policy_is_ethertype_allowed (gc0, ether_type0))
302                         {
303                           /*
304                            * black list model so drop
305                            */
306                           b0->error =
307                             node->errors[GBP_POLICY_ERROR_DROP_ETHER_TYPE];
308
309                           vlib_increment_combined_counter
310                             (&gbp_contract_drop_counters,
311                              thread_index,
312                              gci0, 1, vlib_buffer_length_in_chain (vm, b0));
313
314                           goto trace;
315                         }
316
317                       if ((ether_type0 ==
318                            clib_net_to_host_u16 (ETHERNET_TYPE_IP6))
319                           || (ether_type0 ==
320                               clib_net_to_host_u16 (ETHERNET_TYPE_IP4)))
321                         {
322                           is_ip60 =
323                             (ether_type0 ==
324                              clib_net_to_host_u16 (ETHERNET_TYPE_IP6)) ? 1 :
325                             0;
326                           /*
327                            * tests against the ACL
328                            */
329                           acl_plugin_fill_5tuple_inline (gm->
330                                                          acl_plugin.p_acl_main,
331                                                          gc0->gc_lc_index, b0,
332                                                          is_ip60,
333                                                          /* is_input */ 0,
334                                                          /* is_l2_path */ 1,
335                                                          &pkt_5tuple0);
336                           acl_plugin_match_5tuple_inline (gm->
337                                                           acl_plugin.p_acl_main,
338                                                           gc0->gc_lc_index,
339                                                           &pkt_5tuple0,
340                                                           is_ip60, &action0,
341                                                           &acl_pos_p0,
342                                                           &acl_match_p0,
343                                                           &rule_match_p0,
344                                                           &trace_bitmap0);
345
346                           if (action0 > 0)
347                             {
348                               vnet_buffer2 (b0)->gbp.flags |=
349                                 VXLAN_GBP_GPFLAGS_A;
350                               gu =
351                                 gbp_rule_get (gc0->gc_rules[rule_match_p0]);
352
353                               switch (gu->gu_action)
354                                 {
355                                 case GBP_RULE_PERMIT:
356                                   next0 =
357                                     gbp_policy_l2_feature_next (gpm, b0,
358                                                                 type);
359                                   break;
360                                 case GBP_RULE_DENY:
361                                   next0 = GBP_POLICY_NEXT_DROP;
362                                   break;
363                                 case GBP_RULE_REDIRECT:
364                                   next0 = gbp_rule_l2_redirect (gu, b0);
365                                   break;
366                                 }
367                             }
368                         }
369                       if (next0 == GBP_POLICY_NEXT_DROP)
370                         {
371                           vlib_increment_combined_counter
372                             (&gbp_contract_drop_counters,
373                              thread_index,
374                              gci0, 1, vlib_buffer_length_in_chain (vm, b0));
375                           b0->error =
376                             node->errors[GBP_POLICY_ERROR_DROP_CONTRACT];
377                         }
378                       else
379                         {
380                           vlib_increment_combined_counter
381                             (&gbp_contract_permit_counters,
382                              thread_index,
383                              gci0, 1, vlib_buffer_length_in_chain (vm, b0));
384                         }
385                     }
386                   else
387                     {
388                       b0->error =
389                         node->errors[GBP_POLICY_ERROR_DROP_NO_CONTRACT];
390                     }
391                 }
392             }
393           else
394             {
395               /*
396                * the src EPG is not set when the packet arrives on an EPG
397                * uplink interface and we do not need to apply policy
398                */
399               next0 = gbp_policy_l2_feature_next (gpm, b0, type);
400             }
401
402         trace:
403           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
404             {
405               gbp_policy_trace_t *t =
406                 vlib_add_trace (vm, node, b0, sizeof (*t));
407               t->sclass = key0.gck_src;
408               t->dclass = key0.gck_dst;
409               t->scope = key0.gck_scope;
410               t->acl_index = (gc0 ? gc0->gc_acl_index : ~0);
411               t->allowed = (next0 != GBP_POLICY_NEXT_DROP);
412               t->flags = vnet_buffer2 (b0)->gbp.flags;
413             }
414
415           /* verify speculative enqueue, maybe switch current next frame */
416           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
417                                            to_next, n_left_to_next,
418                                            bi0, next0);
419         }
420
421       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
422     }
423
424   vlib_node_increment_counter (vm, node->node_index,
425                                GBP_POLICY_ERROR_ALLOW_INTRA, n_allow_intra);
426   vlib_node_increment_counter (vm, node->node_index,
427                                GBP_POLICY_ERROR_ALLOW_A_BIT, n_allow_a_bit);
428   vlib_node_increment_counter (vm, node->node_index,
429                                GBP_POLICY_ERROR_ALLOW_SCLASS_1,
430                                n_allow_sclass_1);
431
432   return frame->n_vectors;
433 }
434
435 VLIB_NODE_FN (gbp_policy_port_node) (vlib_main_t * vm,
436                                      vlib_node_runtime_t * node,
437                                      vlib_frame_t * frame)
438 {
439   return (gbp_policy_inline (vm, node, frame, GBP_POLICY_PORT));
440 }
441
442 VLIB_NODE_FN (gbp_policy_mac_node) (vlib_main_t * vm,
443                                     vlib_node_runtime_t * node,
444                                     vlib_frame_t * frame)
445 {
446   return (gbp_policy_inline (vm, node, frame, GBP_POLICY_MAC));
447 }
448
449 VLIB_NODE_FN (gbp_policy_lpm_node) (vlib_main_t * vm,
450                                     vlib_node_runtime_t * node,
451                                     vlib_frame_t * frame)
452 {
453   return (gbp_policy_inline (vm, node, frame, GBP_POLICY_LPM));
454 }
455
456 /* packet trace format function */
457 static u8 *
458 format_gbp_policy_trace (u8 * s, va_list * args)
459 {
460   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
461   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
462   gbp_policy_trace_t *t = va_arg (*args, gbp_policy_trace_t *);
463
464   s =
465     format (s, "scope:%d sclass:%d, dclass:%d, acl:%d allowed:%d flags:%U",
466             t->scope, t->sclass, t->dclass, t->acl_index, t->allowed,
467             format_vxlan_gbp_header_gpflags, t->flags);
468
469   return s;
470 }
471
472 /* *INDENT-OFF* */
473 VLIB_REGISTER_NODE (gbp_policy_port_node) = {
474   .name = "gbp-policy-port",
475   .vector_size = sizeof (u32),
476   .format_trace = format_gbp_policy_trace,
477   .type = VLIB_NODE_TYPE_INTERNAL,
478
479   .n_errors = ARRAY_LEN(gbp_policy_error_strings),
480   .error_strings = gbp_policy_error_strings,
481
482   .n_next_nodes = GBP_POLICY_N_NEXT,
483   .next_nodes = {
484     [GBP_POLICY_NEXT_DROP] = "error-drop",
485   },
486 };
487
488 VLIB_REGISTER_NODE (gbp_policy_mac_node) = {
489   .name = "gbp-policy-mac",
490   .vector_size = sizeof (u32),
491   .format_trace = format_gbp_policy_trace,
492   .type = VLIB_NODE_TYPE_INTERNAL,
493
494   .n_errors = ARRAY_LEN(gbp_policy_error_strings),
495   .error_strings = gbp_policy_error_strings,
496
497   .n_next_nodes = GBP_POLICY_N_NEXT,
498   .next_nodes = {
499     [GBP_POLICY_NEXT_DROP] = "error-drop",
500   },
501 };
502
503 VLIB_REGISTER_NODE (gbp_policy_lpm_node) = {
504   .name = "gbp-policy-lpm",
505   .vector_size = sizeof (u32),
506   .format_trace = format_gbp_policy_trace,
507   .type = VLIB_NODE_TYPE_INTERNAL,
508
509   .n_errors = ARRAY_LEN(gbp_policy_error_strings),
510   .error_strings = gbp_policy_error_strings,
511
512   .n_next_nodes = GBP_POLICY_N_NEXT,
513   .next_nodes = {
514     [GBP_POLICY_NEXT_DROP] = "error-drop",
515   },
516 };
517
518 /* *INDENT-ON* */
519
520 /*
521  * fd.io coding-style-patch-verification: ON
522  *
523  * Local Variables:
524  * eval: (c-set-style "gnu")
525  * End:
526  */