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;
49 #define foreach_acl_fa_error \
50 _(ACL_DROP, "ACL deny packets") \
51 _(ACL_PERMIT, "ACL permit packets") \
52 _(ACL_NEW_SESSION, "new sessions added") \
53 _(ACL_EXIST_SESSION, "existing session packets") \
54 _(ACL_CHECK, "checked packets") \
55 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
56 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
61 #define _(sym,str) ACL_FA_ERROR_##sym,
74 } nonip_in_out_trace_t;
76 /* packet trace format function */
78 format_nonip_in_out_trace (u8 * s, u32 is_output, va_list * args)
80 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
81 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
82 nonip_in_out_trace_t *t = va_arg (*args, nonip_in_out_trace_t *);
84 s = format (s, "%s: sw_if_index %d next_index %x ethertype %x",
85 is_output ? "OUT-ETHER-WHITELIST" : "IN-ETHER-WHITELIST",
86 t->sw_if_index, t->next_index, t->ethertype);
91 format_l2_nonip_in_trace (u8 * s, va_list * args)
93 return format_nonip_in_out_trace (s, 0, args);
97 format_l2_nonip_out_trace (u8 * s, va_list * args)
99 return format_nonip_in_out_trace (s, 1, args);
102 #define foreach_nonip_in_error \
103 _(DROP, "dropped inbound non-whitelisted non-ip packets") \
104 _(PERMIT, "permitted inbound whitelisted non-ip packets") \
107 #define foreach_nonip_out_error \
108 _(DROP, "dropped outbound non-whitelisted non-ip packets") \
109 _(PERMIT, "permitted outbound whitelisted non-ip packets") \
116 #define _(sym,str) FA_IN_NONIP_ERROR_##sym,
117 foreach_nonip_in_error
120 } l2_in_feat_arc_error_t;
122 static char *fa_in_nonip_error_strings[] = {
123 #define _(sym,string) string,
124 foreach_nonip_in_error
130 #define _(sym,str) FA_OUT_NONIP_ERROR_##sym,
131 foreach_nonip_out_error
133 FA_OUT_NONIP_N_ERROR,
134 } l2_out_feat_arc_error_t;
136 static char *fa_out_nonip_error_strings[] = {
137 #define _(sym,string) string,
138 foreach_nonip_out_error
145 is_permitted_ethertype (acl_main_t * am, int sw_if_index0, int is_output,
149 ? am->output_etype_whitelist_by_sw_if_index
150 : am->input_etype_whitelist_by_sw_if_index;
151 u16 *whitelist = vec_elt (v, sw_if_index0);
154 if (vec_len (whitelist) == 0)
157 for (i = 0; i < vec_len (whitelist); i++)
158 if (whitelist[i] == ethertype)
163 #define get_u16(addr) ( *((u16 *)(addr)) )
166 nonip_in_out_node_fn (vlib_main_t * vm,
167 vlib_node_runtime_t * node, vlib_frame_t * frame,
170 acl_main_t *am = &acl_main;
172 u16 nexts[VLIB_FRAME_SIZE], *next;
173 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
174 vlib_node_runtime_t *error_node;
176 from = vlib_frame_vector_args (frame);
177 error_node = vlib_node_get_runtime (vm, node->node_index);
178 vlib_get_buffers (vm, from, bufs, frame->n_vectors);
179 /* set the initial values for the current buffer the next pointers */
183 n_left = frame->n_vectors;
188 vnet_buffer (b[0])->sw_if_index[is_output ? VLIB_TX : VLIB_RX];
193 ethernet_header_t *h0 = vlib_buffer_get_current (b[0]);
194 u8 *l3h0 = (u8 *) h0 + vnet_buffer (b[0])->l2.l2_len;
195 ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2));
197 if (is_permitted_ethertype (am, sw_if_index0, is_output, ethertype))
198 vnet_feature_next (&next_index, b[0]);
200 next[0] = next_index;
203 b[0]->error = error_node->errors[error0];
205 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
206 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
208 nonip_in_out_trace_t *t =
209 vlib_add_trace (vm, node, b[0], sizeof (*t));
210 t->sw_if_index = sw_if_index0;
211 t->ethertype = ethertype;
212 t->next_index = next[0];
214 next[0] = next[0] < node->n_next_nodes ? next[0] : 0;
220 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
222 return frame->n_vectors;
225 VLIB_NODE_FN (acl_in_nonip_node) (vlib_main_t * vm,
226 vlib_node_runtime_t * node,
227 vlib_frame_t * frame)
229 return nonip_in_out_node_fn (vm, node, frame, 0);
232 VLIB_NODE_FN (acl_out_nonip_node) (vlib_main_t * vm,
233 vlib_node_runtime_t * node,
234 vlib_frame_t * frame)
236 return nonip_in_out_node_fn (vm, node, frame, 1);
242 VLIB_REGISTER_NODE (acl_in_nonip_node) =
244 .name = "acl-plugin-in-nonip-l2",
245 .vector_size = sizeof (u32),
246 .format_trace = format_l2_nonip_in_trace,
247 .type = VLIB_NODE_TYPE_INTERNAL,
248 .n_errors = ARRAY_LEN (fa_in_nonip_error_strings),
249 .error_strings = fa_in_nonip_error_strings,
250 .n_next_nodes = ACL_FA_N_NEXT,
253 [ACL_FA_ERROR_DROP] = "error-drop",
257 VNET_FEATURE_INIT (acl_in_l2_nonip_fa_feature, static) =
259 .arc_name = "l2-input-nonip",
260 .node_name = "acl-plugin-in-nonip-l2",
261 .runs_before = VNET_FEATURES ("l2-input-feat-arc-end"),
264 VLIB_REGISTER_NODE (acl_out_nonip_node) =
266 .name = "acl-plugin-out-nonip-l2",
267 .vector_size = sizeof (u32),
268 .format_trace = format_l2_nonip_out_trace,
269 .type = VLIB_NODE_TYPE_INTERNAL,
270 .n_errors = ARRAY_LEN (fa_out_nonip_error_strings),
271 .error_strings = fa_out_nonip_error_strings,
272 .n_next_nodes = ACL_FA_N_NEXT,
275 [ACL_FA_ERROR_DROP] = "error-drop",
279 VNET_FEATURE_INIT (acl_out_l2_nonip_fa_feature, static) =
281 .arc_name = "l2-output-nonip",
282 .node_name = "acl-plugin-out-nonip-l2",
283 .runs_before = VNET_FEATURES ("l2-output-feat-arc-end"),
289 * fd.io coding-style-patch-verification: ON
292 * eval: (c-set-style "gnu")