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