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,
71 acl_fa_node_fn (vlib_main_t * vm,
72 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
73 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
74 vlib_node_registration_t * acl_fa_node)
77 u32 pkts_acl_checked = 0;
78 u32 pkts_new_session = 0;
79 u32 pkts_exist_session = 0;
80 u32 pkts_acl_permit = 0;
81 u32 pkts_restart_session_timer = 0;
83 acl_main_t *am = &acl_main;
84 fa_5tuple_t fa_5tuple;
85 clib_bihash_kv_40_8_t value_sess;
86 vlib_node_runtime_t *error_node;
87 u64 now = clib_cpu_time_now ();
88 uword thread_index = os_get_thread_index ();
89 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
91 u16 nexts[VLIB_FRAME_SIZE], *next;
92 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
94 from = vlib_frame_vector_args (frame);
96 error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
98 vlib_get_buffers (vm, from, bufs, frame->n_vectors);
99 /* set the initial values for the current buffer the next pointers */
103 n_left = frame->n_vectors;
110 int acl_check_needed = 1;
111 u32 match_acl_in_index = ~0;
112 u32 match_acl_pos = ~0;
113 u32 match_rule_index = ~0;
119 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
121 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
124 lc_index0 = am->input_lc_index_by_sw_if_index[sw_if_index0];
126 lc_index0 = am->output_lc_index_by_sw_if_index[sw_if_index0];
130 is_input ? &am->input_policy_epoch_by_sw_if_index :
131 &am->output_policy_epoch_by_sw_if_index;
132 u16 current_policy_epoch =
133 sw_if_index0 < vec_len (*p_epoch_vec) ? vec_elt (*p_epoch_vec,
135 : (is_input * FA_POLICY_EPOCH_IS_INPUT);
137 * Extract the L3/L4 matching info into a 5-tuple structure.
140 acl_plugin_fill_5tuple_inline (lc_index0, b[0], is_ip6, is_input,
142 (fa_5tuple_opaque_t *) & fa_5tuple);
143 fa_5tuple.l4.lsb_of_sw_if_index = sw_if_index0 & 0xffff;
144 fa_5tuple.pkt.mask_type_index_lsb = ~0;
145 #ifdef FA_NODE_VERBOSE_DEBUG
147 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx %016llx",
148 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
149 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
152 /* Try to match an existing session first */
154 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
156 if (acl_fa_find_session (am, sw_if_index0, &fa_5tuple, &value_sess)
157 && (value_sess.value != ~0ULL))
159 trace_bitmap |= 0x80000000;
160 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
161 fa_full_session_id_t f_sess_id;
163 f_sess_id.as_u64 = value_sess.value;
164 ASSERT (f_sess_id.thread_index < vec_len (vlib_mains));
167 get_session_ptr (am, f_sess_id.thread_index,
168 f_sess_id.session_index);
169 int old_timeout_type = fa_session_get_timeout_type (am, sess);
171 acl_fa_track_session (am, is_input, sw_if_index0, now,
173 /* expose the session id to the tracer */
174 match_rule_index = f_sess_id.session_index;
175 int new_timeout_type = fa_session_get_timeout_type (am, sess);
176 acl_check_needed = 0;
177 pkts_exist_session += 1;
178 /* Tracking might have changed the session timeout type, e.g. from transient to established */
179 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
181 acl_fa_restart_timer_for_session (am, now, f_sess_id);
182 pkts_restart_session_timer++;
184 0x00010000 + ((0xff & old_timeout_type) << 8) +
185 (0xff & new_timeout_type);
188 * I estimate the likelihood to be very low - the VPP needs
189 * to have >64K interfaces to start with and then on
190 * exactly 64K indices apart needs to be exactly the same
191 * 5-tuple... Anyway, since this probability is nonzero -
192 * print an error and drop the unlucky packet.
193 * If this shows up in real world, we would need to bump
194 * the hash key length.
196 if (PREDICT_FALSE (sess->sw_if_index != sw_if_index0))
199 ("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
200 acl_check_needed = 0;
203 if (PREDICT_FALSE (am->reclassify_sessions))
205 /* if the MSB of policy epoch matches but not the LSB means it is a stale session */
207 ((current_policy_epoch ^
208 f_sess_id.intf_policy_epoch) &
209 FA_POLICY_EPOCH_IS_INPUT))
210 && (current_policy_epoch !=
211 f_sess_id.intf_policy_epoch))
213 /* delete session and increment the counter */
215 (pw->fa_session_epoch_change_by_sw_if_index,
217 vec_elt (pw->fa_session_epoch_change_by_sw_if_index,
219 if (acl_fa_conn_list_delete_session
220 (am, f_sess_id, now))
222 /* delete the session only if we were able to unlink it */
223 acl_fa_two_stage_delete_session (am, sw_if_index0,
226 acl_check_needed = 1;
227 trace_bitmap |= 0x40000000;
233 if (acl_check_needed)
235 action = 0; /* deny by default */
236 acl_plugin_match_5tuple_inline (lc_index0,
237 (fa_5tuple_opaque_t *) &
238 fa_5tuple, is_ip6, &action,
241 &match_rule_index, &trace_bitmap);
244 pkts_acl_permit += 1;
247 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
248 acl_fa_try_recycle_session (am, is_input, thread_index,
251 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
254 acl_fa_add_session (am, is_input, is_ip6,
257 current_policy_epoch);
258 acl_fa_track_session (am, is_input, sw_if_index0,
259 now, sess, &fa_5tuple);
260 pkts_new_session += 1;
265 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
275 next0 = vnet_l2_feature_next (b[0], l2_feat_next_node_index, 0);
277 vnet_feature_next (sw_if_index0, &next0, b[0]);
279 #ifdef FA_NODE_VERBOSE_DEBUG
281 ("ACL_FA_NODE_DBG: sw_if_index %d lc_index %d action %d acl_index %d rule_index %d",
282 sw_if_index0, lc_index0, action, match_acl_in_index,
286 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
287 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
289 acl_fa_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
290 t->sw_if_index = sw_if_index0;
291 t->lc_index = lc_index0;
292 t->next_index = next0;
293 t->match_acl_in_index = match_acl_in_index;
294 t->match_rule_index = match_rule_index;
295 t->packet_info[0] = fa_5tuple.kv.key[0];
296 t->packet_info[1] = fa_5tuple.kv.key[1];
297 t->packet_info[2] = fa_5tuple.kv.key[2];
298 t->packet_info[3] = fa_5tuple.kv.key[3];
299 t->packet_info[4] = fa_5tuple.kv.key[4];
300 t->packet_info[5] = fa_5tuple.kv.value;
302 t->trace_bitmap = trace_bitmap;
305 next0 = next0 < node->n_next_nodes ? next0 : 0;
307 b[0]->error = error_node->errors[error0];
312 pkts_acl_checked += 1;
315 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
317 vlib_node_increment_counter (vm, acl_fa_node->index,
318 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
319 vlib_node_increment_counter (vm, acl_fa_node->index,
320 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
321 vlib_node_increment_counter (vm, acl_fa_node->index,
322 ACL_FA_ERROR_ACL_NEW_SESSION,
324 vlib_node_increment_counter (vm, acl_fa_node->index,
325 ACL_FA_ERROR_ACL_EXIST_SESSION,
327 vlib_node_increment_counter (vm, acl_fa_node->index,
328 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
329 pkts_restart_session_timer);
330 return frame->n_vectors;
333 vlib_node_registration_t acl_in_l2_ip6_node;
334 VLIB_NODE_FN (acl_in_l2_ip6_node) (vlib_main_t * vm,
335 vlib_node_runtime_t * node,
336 vlib_frame_t * frame)
338 acl_main_t *am = &acl_main;
339 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
340 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
341 &acl_in_l2_ip6_node);
344 vlib_node_registration_t acl_in_l2_ip4_node;
345 VLIB_NODE_FN (acl_in_l2_ip4_node) (vlib_main_t * vm,
346 vlib_node_runtime_t * node,
347 vlib_frame_t * frame)
349 acl_main_t *am = &acl_main;
350 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
351 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
352 &acl_in_l2_ip4_node);
355 vlib_node_registration_t acl_out_l2_ip6_node;
356 VLIB_NODE_FN (acl_out_l2_ip6_node) (vlib_main_t * vm,
357 vlib_node_runtime_t * node,
358 vlib_frame_t * frame)
360 acl_main_t *am = &acl_main;
361 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
362 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
363 &acl_out_l2_ip6_node);
366 vlib_node_registration_t acl_out_l2_ip4_node;
367 VLIB_NODE_FN (acl_out_l2_ip4_node) (vlib_main_t * vm,
368 vlib_node_runtime_t * node,
369 vlib_frame_t * frame)
371 acl_main_t *am = &acl_main;
372 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
373 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
374 &acl_out_l2_ip4_node);
377 /**** L3 processing path nodes ****/
379 vlib_node_registration_t acl_in_fa_ip6_node;
380 VLIB_NODE_FN (acl_in_fa_ip6_node) (vlib_main_t * vm,
381 vlib_node_runtime_t * node,
382 vlib_frame_t * frame)
384 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
387 vlib_node_registration_t acl_in_fa_ip4_node;
388 VLIB_NODE_FN (acl_in_fa_ip4_node) (vlib_main_t * vm,
389 vlib_node_runtime_t * node,
390 vlib_frame_t * frame)
392 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
395 vlib_node_registration_t acl_out_fa_ip6_node;
396 VLIB_NODE_FN (acl_out_fa_ip6_node) (vlib_main_t * vm,
397 vlib_node_runtime_t * node,
398 vlib_frame_t * frame)
400 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
403 vlib_node_registration_t acl_out_fa_ip4_node;
404 VLIB_NODE_FN (acl_out_fa_ip4_node) (vlib_main_t * vm,
405 vlib_node_runtime_t * node,
406 vlib_frame_t * frame)
408 return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
411 #ifndef CLIB_MARCH_VARIANT
413 format_fa_5tuple (u8 * s, va_list * args)
415 fa_5tuple_t *p5t = va_arg (*args, fa_5tuple_t *);
417 return format (s, "lc_index %d (lsb16 of sw_if_index %d) l3 %s%s %U -> %U"
418 " l4 proto %d l4_valid %d port %d -> %d tcp flags (%s) %02x rsvd %x",
419 p5t->pkt.lc_index, p5t->l4.lsb_of_sw_if_index,
420 p5t->pkt.is_ip6 ? "ip6" : "ip4",
421 p5t->pkt.is_nonfirst_fragment ? " non-initial fragment" : "",
422 format_ip46_address, &p5t->addr[0],
423 p5t->pkt.is_ip6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
424 format_ip46_address, &p5t->addr[1],
425 p5t->pkt.is_ip6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
426 p5t->l4.proto, p5t->pkt.l4_valid, p5t->l4.port[0],
428 p5t->pkt.tcp_flags_valid ? "valid" : "invalid",
429 p5t->pkt.tcp_flags, p5t->pkt.flags_reserved);
433 format_acl_plugin_5tuple (u8 * s, va_list * args)
435 return format_fa_5tuple (s, args);
438 /* packet trace format function */
440 format_acl_plugin_trace (u8 * s, va_list * args)
442 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
443 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
444 acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
448 "acl-plugin: lc_index: %d, sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
449 " pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
450 t->lc_index, t->sw_if_index, t->next_index, t->action,
451 t->match_acl_in_index, t->match_rule_index, t->trace_bitmap,
452 t->packet_info[0], t->packet_info[1], t->packet_info[2],
453 t->packet_info[3], t->packet_info[4], t->packet_info[5]);
455 /* Now also print out the packet_info in a form usable by humans */
456 s = format (s, "\n %U", format_fa_5tuple, t->packet_info);
463 static char *acl_fa_error_strings[] = {
464 #define _(sym,string) string,
469 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
471 .name = "acl-plugin-in-ip6-l2",
472 .vector_size = sizeof (u32),
473 .format_trace = format_acl_plugin_trace,
474 .type = VLIB_NODE_TYPE_INTERNAL,
475 .n_errors = ARRAY_LEN (acl_fa_error_strings),
476 .error_strings = acl_fa_error_strings,
477 .n_next_nodes = ACL_FA_N_NEXT,
480 [ACL_FA_ERROR_DROP] = "error-drop",
484 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
486 .name = "acl-plugin-in-ip4-l2",
487 .vector_size = sizeof (u32),
488 .format_trace = format_acl_plugin_trace,
489 .type = VLIB_NODE_TYPE_INTERNAL,
490 .n_errors = ARRAY_LEN (acl_fa_error_strings),
491 .error_strings = acl_fa_error_strings,
492 .n_next_nodes = ACL_FA_N_NEXT,
495 [ACL_FA_ERROR_DROP] = "error-drop",
499 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
501 .name = "acl-plugin-out-ip6-l2",
502 .vector_size = sizeof (u32),
503 .format_trace = format_acl_plugin_trace,
504 .type = VLIB_NODE_TYPE_INTERNAL,
505 .n_errors = ARRAY_LEN (acl_fa_error_strings),
506 .error_strings = acl_fa_error_strings,
507 .n_next_nodes = ACL_FA_N_NEXT,
510 [ACL_FA_ERROR_DROP] = "error-drop",
514 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
516 .name = "acl-plugin-out-ip4-l2",
517 .vector_size = sizeof (u32),
518 .format_trace = format_acl_plugin_trace,
519 .type = VLIB_NODE_TYPE_INTERNAL,
520 .n_errors = ARRAY_LEN (acl_fa_error_strings),
521 .error_strings = acl_fa_error_strings,
522 .n_next_nodes = ACL_FA_N_NEXT,
525 [ACL_FA_ERROR_DROP] = "error-drop",
530 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
532 .name = "acl-plugin-in-ip6-fa",
533 .vector_size = sizeof (u32),
534 .format_trace = format_acl_plugin_trace,
535 .type = VLIB_NODE_TYPE_INTERNAL,
536 .n_errors = ARRAY_LEN (acl_fa_error_strings),
537 .error_strings = acl_fa_error_strings,
538 .n_next_nodes = ACL_FA_N_NEXT,
541 [ACL_FA_ERROR_DROP] = "error-drop",
545 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
547 .arc_name = "ip6-unicast",
548 .node_name = "acl-plugin-in-ip6-fa",
549 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
552 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
554 .name = "acl-plugin-in-ip4-fa",
555 .vector_size = sizeof (u32),
556 .format_trace = format_acl_plugin_trace,
557 .type = VLIB_NODE_TYPE_INTERNAL,
558 .n_errors = ARRAY_LEN (acl_fa_error_strings),
559 .error_strings = acl_fa_error_strings,
560 .n_next_nodes = ACL_FA_N_NEXT,
563 [ACL_FA_ERROR_DROP] = "error-drop",
567 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
569 .arc_name = "ip4-unicast",
570 .node_name = "acl-plugin-in-ip4-fa",
571 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
575 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
577 .name = "acl-plugin-out-ip6-fa",
578 .vector_size = sizeof (u32),
579 .format_trace = format_acl_plugin_trace,
580 .type = VLIB_NODE_TYPE_INTERNAL,
581 .n_errors = ARRAY_LEN (acl_fa_error_strings),
582 .error_strings = acl_fa_error_strings,
583 .n_next_nodes = ACL_FA_N_NEXT,
586 [ACL_FA_ERROR_DROP] = "error-drop",
590 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
592 .arc_name = "ip6-output",
593 .node_name = "acl-plugin-out-ip6-fa",
594 .runs_before = VNET_FEATURES ("interface-output"),
597 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
599 .name = "acl-plugin-out-ip4-fa",
600 .vector_size = sizeof (u32),
601 .format_trace = format_acl_plugin_trace,
602 .type = VLIB_NODE_TYPE_INTERNAL,
603 .n_errors = ARRAY_LEN (acl_fa_error_strings),
604 .error_strings = acl_fa_error_strings,
605 .n_next_nodes = ACL_FA_N_NEXT,
606 /* edit / add dispositions here */
609 [ACL_FA_ERROR_DROP] = "error-drop",
613 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
615 .arc_name = "ip4-output",
616 .node_name = "acl-plugin-out-ip4-fa",
617 .runs_before = VNET_FEATURES ("interface-output"),
624 * fd.io coding-style-patch-verification: ON
627 * eval: (c-set-style "gnu")