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