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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
22 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
23 #include <vnet/vxlan-gbp/vxlan_gbp.h>
25 #define foreach_gbp_policy \
27 _(REFLECTION, "reflection")
31 #define _(sym,str) GBP_POLICY_ERROR_##sym,
32 foreach_gbp_policy_error
37 static char *gbp_policy_error_strings[] = {
38 #define _(sym,string) string,
39 foreach_gbp_policy_error
50 * per-packet trace data
52 typedef struct gbp_policy_trace_t_
54 /* per-pkt trace data */
63 always_inline dpo_proto_t
64 ethertype_to_dpo_proto (u16 etype)
66 etype = clib_net_to_host_u16 (etype);
70 case ETHERNET_TYPE_IP4:
71 return (DPO_PROTO_IP4);
72 case ETHERNET_TYPE_IP6:
73 return (DPO_PROTO_IP6);
76 return (DPO_PROTO_NONE);
80 gbp_rule_l2_redirect (const gbp_rule_t * gu, vlib_buffer_t * b0)
82 const ethernet_header_t *eth0;
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);
90 dproto = ethertype_to_dpo_proto (eth0->type);
91 dpo = &gu->gu_dpo[GBP_POLICY_NODE_L2][dproto];
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;
98 return (dpo->dpoi_next_node);
102 gbp_policy_is_ethertype_allowed (const gbp_contract_t * gc0, u16 ethertype)
106 vec_foreach (et, gc0->gc_allowed_ethertypes)
108 if (*et == ethertype)
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)
122 case GBP_POLICY_PORT:
123 feat_bit = L2OUTPUT_FEAT_GBP_POLICY_PORT;
126 feat_bit = L2OUTPUT_FEAT_GBP_POLICY_MAC;
129 feat_bit = L2OUTPUT_FEAT_GBP_POLICY_LPM;
132 return GBP_POLICY_NEXT_DROP;
135 return vnet_l2_feature_next (b, gpm->l2_output_feat_next[type], feat_bit);
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)
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;
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;
155 while (n_left_from > 0)
159 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
161 while (n_left_from > 0 && n_left_to_next > 0)
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;
173 next0 = GBP_POLICY_NEXT_DROP;
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];
185 /* zero out the key to ensure the pad space is clear */
189 * Reflection check; in and out on an ivxlan tunnel
191 if ((~0 != vxlan_gbp_tunnel_by_sw_if_index (sw_if_index0)) &&
192 (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_R))
198 * If the A-bit is set then policy has already been applied
199 * and we skip enforcement here.
201 if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_A)
203 next0 = gbp_policy_l2_feature_next (gpm, b0, type);
210 * determine the src and dst EPG
213 key0.gck_dst = SCLASS_INVALID;
215 if (GBP_POLICY_LPM == type)
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))
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]);
230 key0.gck_dst = gpd->gpd_sclass;
235 if (GBP_POLICY_PORT == type)
236 ge0 = gbp_endpoint_find_itf (sw_if_index0);
238 ge0 = gbp_endpoint_find_mac (h0->dst_address,
239 vnet_buffer (b0)->l2.bd_index);
241 key0.gck_dst = ge0->ge_fwd.gef_sclass;
244 if (SCLASS_INVALID == key0.gck_dst)
246 /* If you cannot determine the destination EP then drop */
247 b0->error = node->errors[GBP_POLICY_ERROR_DROP_NO_DCLASS];
250 key0.gck_src = vnet_buffer2 (b0)->gbp.sclass;
252 if (SCLASS_INVALID != key0.gck_src)
254 if (PREDICT_FALSE (key0.gck_src == key0.gck_dst))
259 next0 = gbp_policy_l2_feature_next (gpm, b0, type);
260 vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
263 else if (PREDICT_FALSE (key0.gck_src == 1 || key0.gck_dst == 1))
266 * sclass or dclass 1 allowed
268 next0 = gbp_policy_l2_feature_next (gpm, b0, type);
269 vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
275 gbp_bridge_domain_get_scope (vnet_buffer (b0)->
277 gci0 = gbp_contract_find (&key0);
279 if (INDEX_INVALID != gci0)
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;
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);
295 gc0 = gbp_contract_get (gci0);
296 l2_len0 = vnet_buffer (b0)->l2.l2_len;
297 h0 = vlib_buffer_get_current (b0);
299 ether_type0 = *(u16 *) (h0 + l2_len0 - 2);
301 if (!gbp_policy_is_ethertype_allowed (gc0, ether_type0))
304 * black list model so drop
307 node->errors[GBP_POLICY_ERROR_DROP_ETHER_TYPE];
309 vlib_increment_combined_counter
310 (&gbp_contract_drop_counters,
312 gci0, 1, vlib_buffer_length_in_chain (vm, b0));
318 clib_net_to_host_u16 (ETHERNET_TYPE_IP6))
320 clib_net_to_host_u16 (ETHERNET_TYPE_IP4)))
324 clib_net_to_host_u16 (ETHERNET_TYPE_IP6)) ? 1 :
327 * tests against the ACL
329 acl_plugin_fill_5tuple_inline (gm->
330 acl_plugin.p_acl_main,
331 gc0->gc_lc_index, b0,
336 acl_plugin_match_5tuple_inline (gm->
337 acl_plugin.p_acl_main,
348 vnet_buffer2 (b0)->gbp.flags |=
351 gbp_rule_get (gc0->gc_rules[rule_match_p0]);
353 switch (gu->gu_action)
355 case GBP_RULE_PERMIT:
357 gbp_policy_l2_feature_next (gpm, b0,
361 next0 = GBP_POLICY_NEXT_DROP;
363 case GBP_RULE_REDIRECT:
364 next0 = gbp_rule_l2_redirect (gu, b0);
369 if (next0 == GBP_POLICY_NEXT_DROP)
371 vlib_increment_combined_counter
372 (&gbp_contract_drop_counters,
374 gci0, 1, vlib_buffer_length_in_chain (vm, b0));
376 node->errors[GBP_POLICY_ERROR_DROP_CONTRACT];
380 vlib_increment_combined_counter
381 (&gbp_contract_permit_counters,
383 gci0, 1, vlib_buffer_length_in_chain (vm, b0));
389 node->errors[GBP_POLICY_ERROR_DROP_NO_CONTRACT];
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
399 next0 = gbp_policy_l2_feature_next (gpm, b0, type);
403 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
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;
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,
421 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
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,
432 return frame->n_vectors;
435 VLIB_NODE_FN (gbp_policy_port_node) (vlib_main_t * vm,
436 vlib_node_runtime_t * node,
437 vlib_frame_t * frame)
439 return (gbp_policy_inline (vm, node, frame, GBP_POLICY_PORT));
442 VLIB_NODE_FN (gbp_policy_mac_node) (vlib_main_t * vm,
443 vlib_node_runtime_t * node,
444 vlib_frame_t * frame)
446 return (gbp_policy_inline (vm, node, frame, GBP_POLICY_MAC));
449 VLIB_NODE_FN (gbp_policy_lpm_node) (vlib_main_t * vm,
450 vlib_node_runtime_t * node,
451 vlib_frame_t * frame)
453 return (gbp_policy_inline (vm, node, frame, GBP_POLICY_LPM));
456 /* packet trace format function */
458 format_gbp_policy_trace (u8 * s, va_list * args)
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 *);
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);
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,
479 .n_errors = ARRAY_LEN(gbp_policy_error_strings),
480 .error_strings = gbp_policy_error_strings,
482 .n_next_nodes = GBP_POLICY_N_NEXT,
484 [GBP_POLICY_NEXT_DROP] = "error-drop",
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,
494 .n_errors = ARRAY_LEN(gbp_policy_error_strings),
495 .error_strings = gbp_policy_error_strings,
497 .n_next_nodes = GBP_POLICY_N_NEXT,
499 [GBP_POLICY_NEXT_DROP] = "error-drop",
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,
509 .n_errors = ARRAY_LEN(gbp_policy_error_strings),
510 .error_strings = gbp_policy_error_strings,
512 .n_next_nodes = GBP_POLICY_N_NEXT,
514 [GBP_POLICY_NEXT_DROP] = "error-drop",
521 * fd.io coding-style-patch-verification: ON
524 * eval: (c-set-style "gnu")