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