2 * Copyright (c) 2016-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 <netinet/in.h>
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vppinfra/error.h>
25 #include <vnet/ip/icmp46_packet.h>
27 #include <plugins/acl/fa_node.h>
28 #include <plugins/acl/acl.h>
29 #include <plugins/acl/lookup_context.h>
30 #include <plugins/acl/public_inlines.h>
31 #include <plugins/acl/session_inlines.h>
33 #include <vppinfra/bihash_40_8.h>
34 #include <vppinfra/bihash_template.h>
41 u32 match_acl_in_index;
48 #define foreach_acl_fa_error \
49 _(ACL_DROP, "ACL deny packets") \
50 _(ACL_PERMIT, "ACL permit packets") \
51 _(ACL_NEW_SESSION, "new sessions added") \
52 _(ACL_EXIST_SESSION, "existing session packets") \
53 _(ACL_CHECK, "checked packets") \
54 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
55 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
60 #define _(sym,str) ACL_FA_ERROR_##sym,
72 } nonip_in_out_trace_t;
74 /* packet trace format function */
76 format_nonip_in_out_trace (u8 * s, u32 is_output, va_list * args)
78 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
79 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
80 nonip_in_out_trace_t *t = va_arg (*args, nonip_in_out_trace_t *);
82 s = format (s, "%s: sw_if_index %d next_index %x ethertype %x",
83 is_output ? "OUT-ETHER-WHITELIST" : "IN-ETHER-WHITELIST",
84 t->sw_if_index, t->next_index, t->ethertype);
89 format_l2_nonip_in_trace (u8 * s, va_list * args)
91 return format_nonip_in_out_trace (s, 0, args);
95 format_l2_nonip_out_trace (u8 * s, va_list * args)
97 return format_nonip_in_out_trace (s, 1, args);
100 #define foreach_nonip_in_error \
101 _(DROP, "dropped inbound non-whitelisted non-ip packets") \
102 _(PERMIT, "permitted inbound whitelisted non-ip packets") \
105 #define foreach_nonip_out_error \
106 _(DROP, "dropped outbound non-whitelisted non-ip packets") \
107 _(PERMIT, "permitted outbound whitelisted non-ip packets") \
113 #define _(sym,str) FA_IN_NONIP_ERROR_##sym,
114 foreach_nonip_in_error
117 } l2_in_feat_arc_error_t;
119 static char *fa_in_nonip_error_strings[] = {
120 #define _(sym,string) string,
121 foreach_nonip_in_error
127 #define _(sym,str) FA_OUT_NONIP_ERROR_##sym,
128 foreach_nonip_out_error
130 FA_OUT_NONIP_N_ERROR,
131 } l2_out_feat_arc_error_t;
133 static char *fa_out_nonip_error_strings[] = {
134 #define _(sym,string) string,
135 foreach_nonip_out_error
141 is_permitted_ethertype (acl_main_t * am, int sw_if_index0, int is_output,
145 ? am->output_etype_whitelist_by_sw_if_index
146 : am->input_etype_whitelist_by_sw_if_index;
147 u16 *whitelist = vec_elt (v, sw_if_index0);
150 if (vec_len (whitelist) == 0)
153 for (i = 0; i < vec_len (whitelist); i++)
154 if (whitelist[i] == ethertype)
159 #define get_u16(addr) ( *((u16 *)(addr)) )
162 nonip_in_out_node_fn (vlib_main_t * vm,
163 vlib_node_runtime_t * node, vlib_frame_t * frame,
166 acl_main_t *am = &acl_main;
168 u16 nexts[VLIB_FRAME_SIZE], *next;
169 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
170 vlib_node_runtime_t *error_node;
172 from = vlib_frame_vector_args (frame);
173 error_node = vlib_node_get_runtime (vm, node->node_index);
174 vlib_get_buffers (vm, from, bufs, frame->n_vectors);
175 /* set the initial values for the current buffer the next pointers */
179 n_left = frame->n_vectors;
184 vnet_buffer (b[0])->sw_if_index[is_output ? VLIB_TX : VLIB_RX];
189 ethernet_header_t *h0 = vlib_buffer_get_current (b[0]);
190 u8 *l3h0 = (u8 *) h0 + vnet_buffer (b[0])->l2.l2_len;
191 ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2));
193 if (is_permitted_ethertype (am, sw_if_index0, is_output, ethertype))
194 vnet_feature_next (&next_index, b[0]);
196 next[0] = next_index;
199 b[0]->error = error_node->errors[error0];
201 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
202 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
204 nonip_in_out_trace_t *t =
205 vlib_add_trace (vm, node, b[0], sizeof (*t));
206 t->sw_if_index = sw_if_index0;
207 t->ethertype = ethertype;
208 t->next_index = next[0];
210 next[0] = next[0] < node->n_next_nodes ? next[0] : 0;
216 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
218 return frame->n_vectors;
221 VLIB_NODE_FN (acl_in_nonip_node) (vlib_main_t * vm,
222 vlib_node_runtime_t * node,
223 vlib_frame_t * frame)
225 return nonip_in_out_node_fn (vm, node, frame, 0);
228 VLIB_NODE_FN (acl_out_nonip_node) (vlib_main_t * vm,
229 vlib_node_runtime_t * node,
230 vlib_frame_t * frame)
232 return nonip_in_out_node_fn (vm, node, frame, 1);
237 VLIB_REGISTER_NODE (acl_in_nonip_node) =
239 .name = "acl-plugin-in-nonip-l2",
240 .vector_size = sizeof (u32),
241 .format_trace = format_l2_nonip_in_trace,
242 .type = VLIB_NODE_TYPE_INTERNAL,
243 .n_errors = ARRAY_LEN (fa_in_nonip_error_strings),
244 .error_strings = fa_in_nonip_error_strings,
245 .n_next_nodes = ACL_FA_N_NEXT,
248 [ACL_FA_ERROR_DROP] = "error-drop",
252 VNET_FEATURE_INIT (acl_in_l2_nonip_fa_feature, static) =
254 .arc_name = "l2-input-nonip",
255 .node_name = "acl-plugin-in-nonip-l2",
256 .runs_before = VNET_FEATURES ("l2-input-feat-arc-end"),
259 VLIB_REGISTER_NODE (acl_out_nonip_node) =
261 .name = "acl-plugin-out-nonip-l2",
262 .vector_size = sizeof (u32),
263 .format_trace = format_l2_nonip_out_trace,
264 .type = VLIB_NODE_TYPE_INTERNAL,
265 .n_errors = ARRAY_LEN (fa_out_nonip_error_strings),
266 .error_strings = fa_out_nonip_error_strings,
267 .n_next_nodes = ACL_FA_N_NEXT,
270 [ACL_FA_ERROR_DROP] = "error-drop",
274 VNET_FEATURE_INIT (acl_out_l2_nonip_fa_feature, static) =
276 .arc_name = "l2-output-nonip",
277 .node_name = "acl-plugin-out-nonip-l2",
278 .runs_before = VNET_FEATURES ("l2-output-feat-arc-end"),
283 * fd.io coding-style-patch-verification: ON
286 * eval: (c-set-style "gnu")