GBP: iVXLAN reflection check
[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_ERROR_##sym,
29   foreach_gbp_policy
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
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;
119
120   next_index = 0;
121   n_left_from = frame->n_vectors;
122   from = vlib_frame_vector_args (frame);
123
124   while (n_left_from > 0)
125     {
126       u32 n_left_to_next;
127
128       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
129
130       while (n_left_from > 0 && n_left_to_next > 0)
131         {
132           const ethernet_header_t *h0;
133           const gbp_endpoint_t *ge0;
134           const gbp_contract_t *gc0;
135           gbp_policy_next_t next0;
136           gbp_contract_key_t key0;
137           u32 bi0, sw_if_index0;
138           vlib_buffer_t *b0;
139           index_t gci0;
140
141           gc0 = NULL;
142           next0 = GBP_POLICY_NEXT_DROP;
143           bi0 = from[0];
144           to_next[0] = bi0;
145           from += 1;
146           to_next += 1;
147           n_left_from -= 1;
148           n_left_to_next -= 1;
149
150           b0 = vlib_get_buffer (vm, bi0);
151           h0 = vlib_buffer_get_current (b0);
152           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
153
154           /*
155            * Reflection check; in and out on an ivxlan tunnel
156            */
157           if ((~0 != vxlan_gbp_tunnel_by_sw_if_index (sw_if_index0)) &&
158               (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_R))
159             {
160               goto trace;
161             }
162
163           /*
164            * If the A-bit is set then policy has already been applied
165            * and we skip enforcement here.
166            */
167           if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_A)
168             {
169               next0 = vnet_l2_feature_next (b0,
170                                             gpm->l2_output_feat_next
171                                             [is_port_based],
172                                             (is_port_based ?
173                                              L2OUTPUT_FEAT_GBP_POLICY_PORT :
174                                              L2OUTPUT_FEAT_GBP_POLICY_MAC));
175               key0.as_u32 = ~0;
176               goto trace;
177             }
178
179           /*
180            * determine the src and dst EPG
181            */
182           if (is_port_based)
183             ge0 = gbp_endpoint_find_itf (sw_if_index0);
184           else
185             ge0 = gbp_endpoint_find_mac (h0->dst_address,
186                                          vnet_buffer (b0)->l2.bd_index);
187
188           if (NULL != ge0)
189             key0.gck_dst = ge0->ge_fwd.gef_sclass;
190           else
191             /* If you cannot determine the destination EP then drop */
192             goto trace;
193
194           key0.gck_src = vnet_buffer2 (b0)->gbp.sclass;
195
196           if (SCLASS_INVALID != key0.gck_src)
197             {
198               if (PREDICT_FALSE (key0.gck_src == key0.gck_dst))
199                 {
200                   /*
201                    * intra-epg allowed
202                    */
203                   next0 =
204                     vnet_l2_feature_next (b0,
205                                           gpm->l2_output_feat_next
206                                           [is_port_based],
207                                           (is_port_based ?
208                                            L2OUTPUT_FEAT_GBP_POLICY_PORT :
209                                            L2OUTPUT_FEAT_GBP_POLICY_MAC));
210                   vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
211                 }
212               else
213                 {
214                   gci0 = gbp_contract_find (&key0);
215
216                   if (INDEX_INVALID != gci0)
217                     {
218                       u32 rule_match_p0, trace_bitmap0;
219                       fa_5tuple_opaque_t pkt_5tuple0;
220                       u32 acl_pos_p0, acl_match_p0;
221                       u8 is_ip60, l2_len0, action0;
222                       const gbp_rule_t *gu;
223                       u16 ether_type0;
224                       const u8 *h0;
225
226                       action0 = 0;
227                       gc0 = gbp_contract_get (gci0);
228                       l2_len0 = vnet_buffer (b0)->l2.l2_len;
229                       h0 = vlib_buffer_get_current (b0);
230
231                       ether_type0 = *(u16 *) (h0 + l2_len0 - 2);
232
233                       if (!gbp_policy_is_ethertype_allowed (gc0, ether_type0))
234                         {
235                           /*
236                            * black list model so drop
237                            */
238                           goto trace;
239                         }
240
241                       if ((ether_type0 ==
242                            clib_net_to_host_u16 (ETHERNET_TYPE_IP6))
243                           || (ether_type0 ==
244                               clib_net_to_host_u16 (ETHERNET_TYPE_IP4)))
245                         {
246                           is_ip60 =
247                             (ether_type0 ==
248                              clib_net_to_host_u16 (ETHERNET_TYPE_IP6)) ? 1 :
249                             0;
250                           /*
251                            * tests against the ACL
252                            */
253                           acl_plugin_fill_5tuple_inline (gm->
254                                                          acl_plugin.p_acl_main,
255                                                          gc0->gc_lc_index, b0,
256                                                          is_ip60,
257                                                          /* is_input */ 0,
258                                                          /* is_l2_path */ 1,
259                                                          &pkt_5tuple0);
260                           acl_plugin_match_5tuple_inline (gm->
261                                                           acl_plugin.p_acl_main,
262                                                           gc0->gc_lc_index,
263                                                           &pkt_5tuple0,
264                                                           is_ip60, &action0,
265                                                           &acl_pos_p0,
266                                                           &acl_match_p0,
267                                                           &rule_match_p0,
268                                                           &trace_bitmap0);
269
270                           if (action0 > 0)
271                             {
272                               vnet_buffer2 (b0)->gbp.flags |=
273                                 VXLAN_GBP_GPFLAGS_A;
274                               gu =
275                                 gbp_rule_get (gc0->gc_rules[rule_match_p0]);
276
277                               switch (gu->gu_action)
278                                 {
279                                 case GBP_RULE_PERMIT:
280                                   next0 = vnet_l2_feature_next
281                                     (b0,
282                                      gpm->l2_output_feat_next
283                                      [is_port_based],
284                                      (is_port_based ?
285                                       L2OUTPUT_FEAT_GBP_POLICY_PORT :
286                                       L2OUTPUT_FEAT_GBP_POLICY_MAC));
287                                   break;
288                                 case GBP_RULE_DENY:
289                                   next0 = 0;
290                                   break;
291                                 case GBP_RULE_REDIRECT:
292                                   next0 = gbp_rule_l2_redirect (gu, b0);
293                                   break;
294                                 }
295                             }
296                         }
297                     }
298                 }
299             }
300           else
301             {
302               /*
303                * the src EPG is not set when the packet arrives on an EPG
304                * uplink interface and we do not need to apply policy
305                */
306               next0 =
307                 vnet_l2_feature_next (b0,
308                                       gpm->l2_output_feat_next[is_port_based],
309                                       (is_port_based ?
310                                        L2OUTPUT_FEAT_GBP_POLICY_PORT :
311                                        L2OUTPUT_FEAT_GBP_POLICY_MAC));
312             }
313
314         trace:
315           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
316             {
317               gbp_policy_trace_t *t =
318                 vlib_add_trace (vm, node, b0, sizeof (*t));
319               t->sclass = key0.gck_src;
320               t->dst_epg = key0.gck_dst;
321               t->acl_index = (gc0 ? gc0->gc_acl_index : ~0);
322               t->allowed = (next0 != GBP_POLICY_NEXT_DROP);
323               t->flags = vnet_buffer2 (b0)->gbp.flags;
324             }
325
326           /* verify speculative enqueue, maybe switch current next frame */
327           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
328                                            to_next, n_left_to_next,
329                                            bi0, next0);
330         }
331
332       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
333     }
334
335   return frame->n_vectors;
336 }
337
338 VLIB_NODE_FN (gbp_policy_port_node) (vlib_main_t * vm,
339                                      vlib_node_runtime_t * node,
340                                      vlib_frame_t * frame)
341 {
342   return (gbp_policy_inline (vm, node, frame, 1));
343 }
344
345 VLIB_NODE_FN (gbp_policy_mac_node) (vlib_main_t * vm,
346                                     vlib_node_runtime_t * node,
347                                     vlib_frame_t * frame)
348 {
349   return (gbp_policy_inline (vm, node, frame, 0));
350 }
351
352 /* packet trace format function */
353 static u8 *
354 format_gbp_policy_trace (u8 * s, va_list * args)
355 {
356   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
357   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
358   gbp_policy_trace_t *t = va_arg (*args, gbp_policy_trace_t *);
359
360   s =
361     format (s, "sclass:%d, dst:%d, acl:%d allowed:%d flags:%U",
362             t->sclass, t->dst_epg, t->acl_index, t->allowed,
363             format_vxlan_gbp_header_gpflags, t->flags);
364
365   return s;
366 }
367
368 /* *INDENT-OFF* */
369 VLIB_REGISTER_NODE (gbp_policy_port_node) = {
370   .name = "gbp-policy-port",
371   .vector_size = sizeof (u32),
372   .format_trace = format_gbp_policy_trace,
373   .type = VLIB_NODE_TYPE_INTERNAL,
374
375   .n_errors = ARRAY_LEN(gbp_policy_error_strings),
376   .error_strings = gbp_policy_error_strings,
377
378   .n_next_nodes = GBP_POLICY_N_NEXT,
379
380   .next_nodes = {
381     [GBP_POLICY_NEXT_DROP] = "error-drop",
382   },
383 };
384
385 VLIB_REGISTER_NODE (gbp_policy_mac_node) = {
386   .name = "gbp-policy-mac",
387   .vector_size = sizeof (u32),
388   .format_trace = format_gbp_policy_trace,
389   .type = VLIB_NODE_TYPE_INTERNAL,
390   .sibling_of = "gbp-policy-port",
391 };
392
393 /* *INDENT-ON* */
394
395 /*
396  * fd.io coding-style-patch-verification: ON
397  *
398  * Local Variables:
399  * eval: (c-set-style "gnu")
400  * End:
401  */