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,
70 get_current_policy_epoch (acl_main_t * am, int is_input, u32 sw_if_index0)
73 is_input ? &am->input_policy_epoch_by_sw_if_index :
74 &am->output_policy_epoch_by_sw_if_index;
75 u16 current_policy_epoch =
76 sw_if_index0 < vec_len (*p_epoch_vec) ? vec_elt (*p_epoch_vec,
78 : (is_input * FA_POLICY_EPOCH_IS_INPUT);
79 return current_policy_epoch;
83 acl_fa_node_fn (vlib_main_t * vm,
84 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
85 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
86 vlib_node_registration_t * acl_fa_node)
89 u32 pkts_acl_checked = 0;
90 u32 pkts_new_session = 0;
91 u32 pkts_exist_session = 0;
92 u32 pkts_acl_permit = 0;
93 u32 pkts_restart_session_timer = 0;
95 acl_main_t *am = &acl_main;
96 fa_5tuple_t fa_5tuple;
97 vlib_node_runtime_t *error_node;
98 u64 now = clib_cpu_time_now ();
99 uword thread_index = os_get_thread_index ();
100 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
102 u16 nexts[VLIB_FRAME_SIZE], *next;
103 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
105 from = vlib_frame_vector_args (frame);
107 error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
109 vlib_get_buffers (vm, from, bufs, frame->n_vectors);
110 /* set the initial values for the current buffer the next pointers */
114 n_left = frame->n_vectors;
121 int acl_check_needed = 1;
122 u32 match_acl_in_index = ~0;
123 u32 match_acl_pos = ~0;
124 u32 match_rule_index = ~0;
130 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
132 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
135 lc_index0 = am->input_lc_index_by_sw_if_index[sw_if_index0];
137 lc_index0 = am->output_lc_index_by_sw_if_index[sw_if_index0];
139 u16 current_policy_epoch =
140 get_current_policy_epoch (am, is_input, sw_if_index0);
144 * Extract the L3/L4 matching info into a 5-tuple structure.
147 acl_plugin_fill_5tuple_inline (&acl_main, lc_index0, b[0], is_ip6,
148 is_input, is_l2_path,
149 (fa_5tuple_opaque_t *) & fa_5tuple);
150 fa_5tuple.l4.lsb_of_sw_if_index = sw_if_index0 & 0xffff;
151 fa_5tuple.pkt.mask_type_index_lsb = ~0;
152 #ifdef FA_NODE_VERBOSE_DEBUG
154 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx %016llx",
155 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
156 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
159 /* Try to match an existing session first */
161 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
163 u64 value_sess = ~0ULL;
164 if (acl_fa_find_session
165 (am, is_ip6, sw_if_index0, &fa_5tuple, &value_sess)
166 && (value_sess != ~0ULL))
168 trace_bitmap |= 0x80000000;
169 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
170 fa_full_session_id_t f_sess_id;
172 f_sess_id.as_u64 = value_sess;
173 ASSERT (f_sess_id.thread_index < vec_len (vlib_mains));
176 get_session_ptr (am, f_sess_id.thread_index,
177 f_sess_id.session_index);
178 int old_timeout_type = fa_session_get_timeout_type (am, sess);
180 acl_fa_track_session (am, is_input, sw_if_index0, now,
182 /* expose the session id to the tracer */
183 match_rule_index = f_sess_id.session_index;
184 int new_timeout_type = fa_session_get_timeout_type (am, sess);
185 acl_check_needed = 0;
186 pkts_exist_session += 1;
187 /* Tracking might have changed the session timeout type, e.g. from transient to established */
188 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
190 acl_fa_restart_timer_for_session (am, now, f_sess_id);
191 pkts_restart_session_timer++;
193 0x00010000 + ((0xff & old_timeout_type) << 8) +
194 (0xff & new_timeout_type);
197 * I estimate the likelihood to be very low - the VPP needs
198 * to have >64K interfaces to start with and then on
199 * exactly 64K indices apart needs to be exactly the same
200 * 5-tuple... Anyway, since this probability is nonzero -
201 * print an error and drop the unlucky packet.
202 * If this shows up in real world, we would need to bump
203 * the hash key length.
205 if (PREDICT_FALSE (sess->sw_if_index != sw_if_index0))
208 ("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
209 acl_check_needed = 0;
212 if (PREDICT_FALSE (am->reclassify_sessions))
214 /* if the MSB of policy epoch matches but not the LSB means it is a stale session */
216 ((current_policy_epoch ^
217 f_sess_id.intf_policy_epoch) &
218 FA_POLICY_EPOCH_IS_INPUT))
219 && (current_policy_epoch !=
220 f_sess_id.intf_policy_epoch))
222 /* delete session and increment the counter */
224 (pw->fa_session_epoch_change_by_sw_if_index,
226 vec_elt (pw->fa_session_epoch_change_by_sw_if_index,
228 if (acl_fa_conn_list_delete_session
229 (am, f_sess_id, now))
231 /* delete the session only if we were able to unlink it */
232 acl_fa_two_stage_delete_session (am, sw_if_index0,
235 acl_check_needed = 1;
236 trace_bitmap |= 0x40000000;
242 if (acl_check_needed)
244 action = 0; /* deny by default */
245 acl_plugin_match_5tuple_inline (&acl_main, lc_index0,
246 (fa_5tuple_opaque_t *) &
247 fa_5tuple, is_ip6, &action,
250 &match_rule_index, &trace_bitmap);
253 pkts_acl_permit += 1;
256 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
257 acl_fa_try_recycle_session (am, is_input, thread_index,
260 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
263 acl_fa_add_session (am, is_input, is_ip6,
266 current_policy_epoch);
267 acl_fa_track_session (am, is_input, sw_if_index0,
268 now, sess, &fa_5tuple);
269 pkts_new_session += 1;
274 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
284 next0 = vnet_l2_feature_next (b[0], l2_feat_next_node_index, 0);
286 vnet_feature_next (&next0, b[0]);
288 #ifdef FA_NODE_VERBOSE_DEBUG
290 ("ACL_FA_NODE_DBG: sw_if_index %d lc_index %d action %d acl_index %d rule_index %d",
291 sw_if_index0, lc_index0, action, match_acl_in_index,
295 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
296 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
298 acl_fa_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
299 t->sw_if_index = sw_if_index0;
300 t->lc_index = lc_index0;
301 t->next_index = next0;
302 t->match_acl_in_index = match_acl_in_index;
303 t->match_rule_index = match_rule_index;
304 t->packet_info[0] = fa_5tuple.kv_40_8.key[0];
305 t->packet_info[1] = fa_5tuple.kv_40_8.key[1];
306 t->packet_info[2] = fa_5tuple.kv_40_8.key[2];
307 t->packet_info[3] = fa_5tuple.kv_40_8.key[3];
308 t->packet_info[4] = fa_5tuple.kv_40_8.key[4];
309 t->packet_info[5] = fa_5tuple.kv_40_8.value;
311 t->trace_bitmap = trace_bitmap;
314 next0 = next0 < node->n_next_nodes ? next0 : 0;
316 b[0]->error = error_node->errors[error0];
321 pkts_acl_checked += 1;
324 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
326 vlib_node_increment_counter (vm, acl_fa_node->index,
327 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
328 vlib_node_increment_counter (vm, acl_fa_node->index,
329 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
330 vlib_node_increment_counter (vm, acl_fa_node->index,
331 ACL_FA_ERROR_ACL_NEW_SESSION,
333 vlib_node_increment_counter (vm, acl_fa_node->index,
334 ACL_FA_ERROR_ACL_EXIST_SESSION,
336 vlib_node_increment_counter (vm, acl_fa_node->index,
337 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
338 pkts_restart_session_timer);
339 return frame->n_vectors;
342 vlib_node_registration_t acl_in_l2_ip6_node;
343 VLIB_NODE_FN (acl_in_l2_ip6_node) (vlib_main_t * vm,
344 vlib_node_runtime_t * node,
345 vlib_frame_t * frame)
347 acl_main_t *am = &acl_main;
348 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
349 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
350 &acl_in_l2_ip6_node);
353 vlib_node_registration_t acl_in_l2_ip4_node;
354 VLIB_NODE_FN (acl_in_l2_ip4_node) (vlib_main_t * vm,
355 vlib_node_runtime_t * node,
356 vlib_frame_t * frame)
358 acl_main_t *am = &acl_main;
359 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
360 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
361 &acl_in_l2_ip4_node);
364 vlib_node_registration_t acl_out_l2_ip6_node;
365 VLIB_NODE_FN (acl_out_l2_ip6_node) (vlib_main_t * vm,
366 vlib_node_runtime_t * node,
367 vlib_frame_t * frame)
369 acl_main_t *am = &acl_main;
370 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
371 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
372 &acl_out_l2_ip6_node);
375 vlib_node_registration_t acl_out_l2_ip4_node;
376 VLIB_NODE_FN (acl_out_l2_ip4_node) (vlib_main_t * vm,
377 vlib_node_runtime_t * node,
378 vlib_frame_t * frame)
380 acl_main_t *am = &acl_main;
381 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
382 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
383 &acl_out_l2_ip4_node);
386 /**** L3 processing path nodes ****/
388 vlib_node_registration_t acl_in_fa_ip6_node;
389 VLIB_NODE_FN (acl_in_fa_ip6_node) (vlib_main_t * vm,
390 vlib_node_runtime_t * node,
391 vlib_frame_t * frame)
393 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
396 vlib_node_registration_t acl_in_fa_ip4_node;
397 VLIB_NODE_FN (acl_in_fa_ip4_node) (vlib_main_t * vm,
398 vlib_node_runtime_t * node,
399 vlib_frame_t * frame)
401 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
404 vlib_node_registration_t acl_out_fa_ip6_node;
405 VLIB_NODE_FN (acl_out_fa_ip6_node) (vlib_main_t * vm,
406 vlib_node_runtime_t * node,
407 vlib_frame_t * frame)
409 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
412 vlib_node_registration_t acl_out_fa_ip4_node;
413 VLIB_NODE_FN (acl_out_fa_ip4_node) (vlib_main_t * vm,
414 vlib_node_runtime_t * node,
415 vlib_frame_t * frame)
417 return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
420 #ifndef CLIB_MARCH_VARIANT
422 format_fa_5tuple (u8 * s, va_list * args)
424 fa_5tuple_t *p5t = va_arg (*args, fa_5tuple_t *);
427 return format (s, "lc_index %d (lsb16 of sw_if_index %d) l3 %s%s %U -> %U"
428 " l4 proto %d l4_valid %d port %d -> %d tcp flags (%s) %02x rsvd %x",
429 p5t->pkt.lc_index, p5t->l4.lsb_of_sw_if_index,
432 pkt.is_nonfirst_fragment ? " non-initial fragment" : "",
433 format_ip6_address, &p5t->ip6_addr[0], format_ip6_address,
434 &p5t->ip6_addr[1], p5t->l4.proto, p5t->pkt.l4_valid,
435 p5t->l4.port[0], p5t->l4.port[1],
436 p5t->pkt.tcp_flags_valid ? "valid" : "invalid",
437 p5t->pkt.tcp_flags, p5t->pkt.flags_reserved);
439 return format (s, "lc_index %d (lsb16 of sw_if_index %d) l3 %s%s %U -> %U"
440 " l4 proto %d l4_valid %d port %d -> %d tcp flags (%s) %02x rsvd %x",
441 p5t->pkt.lc_index, p5t->l4.lsb_of_sw_if_index,
444 pkt.is_nonfirst_fragment ? " non-initial fragment" : "",
445 format_ip4_address, &p5t->ip4_addr[0], format_ip4_address,
446 &p5t->ip4_addr[1], p5t->l4.proto, p5t->pkt.l4_valid,
447 p5t->l4.port[0], p5t->l4.port[1],
448 p5t->pkt.tcp_flags_valid ? "valid" : "invalid",
449 p5t->pkt.tcp_flags, p5t->pkt.flags_reserved);
453 format_acl_plugin_5tuple (u8 * s, va_list * args)
455 return format_fa_5tuple (s, args);
458 /* packet trace format function */
460 format_acl_plugin_trace (u8 * s, va_list * args)
462 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
463 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
464 acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
468 "acl-plugin: lc_index: %d, sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
469 " pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
470 t->lc_index, t->sw_if_index, t->next_index, t->action,
471 t->match_acl_in_index, t->match_rule_index, t->trace_bitmap,
472 t->packet_info[0], t->packet_info[1], t->packet_info[2],
473 t->packet_info[3], t->packet_info[4], t->packet_info[5]);
475 /* Now also print out the packet_info in a form usable by humans */
476 s = format (s, "\n %U", format_fa_5tuple, t->packet_info);
483 static char *acl_fa_error_strings[] = {
484 #define _(sym,string) string,
489 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
491 .name = "acl-plugin-in-ip6-l2",
492 .vector_size = sizeof (u32),
493 .format_trace = format_acl_plugin_trace,
494 .type = VLIB_NODE_TYPE_INTERNAL,
495 .n_errors = ARRAY_LEN (acl_fa_error_strings),
496 .error_strings = acl_fa_error_strings,
497 .n_next_nodes = ACL_FA_N_NEXT,
500 [ACL_FA_ERROR_DROP] = "error-drop",
504 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
506 .name = "acl-plugin-in-ip4-l2",
507 .vector_size = sizeof (u32),
508 .format_trace = format_acl_plugin_trace,
509 .type = VLIB_NODE_TYPE_INTERNAL,
510 .n_errors = ARRAY_LEN (acl_fa_error_strings),
511 .error_strings = acl_fa_error_strings,
512 .n_next_nodes = ACL_FA_N_NEXT,
515 [ACL_FA_ERROR_DROP] = "error-drop",
519 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
521 .name = "acl-plugin-out-ip6-l2",
522 .vector_size = sizeof (u32),
523 .format_trace = format_acl_plugin_trace,
524 .type = VLIB_NODE_TYPE_INTERNAL,
525 .n_errors = ARRAY_LEN (acl_fa_error_strings),
526 .error_strings = acl_fa_error_strings,
527 .n_next_nodes = ACL_FA_N_NEXT,
530 [ACL_FA_ERROR_DROP] = "error-drop",
534 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
536 .name = "acl-plugin-out-ip4-l2",
537 .vector_size = sizeof (u32),
538 .format_trace = format_acl_plugin_trace,
539 .type = VLIB_NODE_TYPE_INTERNAL,
540 .n_errors = ARRAY_LEN (acl_fa_error_strings),
541 .error_strings = acl_fa_error_strings,
542 .n_next_nodes = ACL_FA_N_NEXT,
545 [ACL_FA_ERROR_DROP] = "error-drop",
550 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
552 .name = "acl-plugin-in-ip6-fa",
553 .vector_size = sizeof (u32),
554 .format_trace = format_acl_plugin_trace,
555 .type = VLIB_NODE_TYPE_INTERNAL,
556 .n_errors = ARRAY_LEN (acl_fa_error_strings),
557 .error_strings = acl_fa_error_strings,
558 .n_next_nodes = ACL_FA_N_NEXT,
561 [ACL_FA_ERROR_DROP] = "error-drop",
565 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
567 .arc_name = "ip6-unicast",
568 .node_name = "acl-plugin-in-ip6-fa",
569 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
572 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
574 .name = "acl-plugin-in-ip4-fa",
575 .vector_size = sizeof (u32),
576 .format_trace = format_acl_plugin_trace,
577 .type = VLIB_NODE_TYPE_INTERNAL,
578 .n_errors = ARRAY_LEN (acl_fa_error_strings),
579 .error_strings = acl_fa_error_strings,
580 .n_next_nodes = ACL_FA_N_NEXT,
583 [ACL_FA_ERROR_DROP] = "error-drop",
587 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
589 .arc_name = "ip4-unicast",
590 .node_name = "acl-plugin-in-ip4-fa",
591 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
595 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
597 .name = "acl-plugin-out-ip6-fa",
598 .vector_size = sizeof (u32),
599 .format_trace = format_acl_plugin_trace,
600 .type = VLIB_NODE_TYPE_INTERNAL,
601 .n_errors = ARRAY_LEN (acl_fa_error_strings),
602 .error_strings = acl_fa_error_strings,
603 .n_next_nodes = ACL_FA_N_NEXT,
606 [ACL_FA_ERROR_DROP] = "error-drop",
610 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
612 .arc_name = "ip6-output",
613 .node_name = "acl-plugin-out-ip6-fa",
614 .runs_before = VNET_FEATURES ("interface-output"),
617 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
619 .name = "acl-plugin-out-ip4-fa",
620 .vector_size = sizeof (u32),
621 .format_trace = format_acl_plugin_trace,
622 .type = VLIB_NODE_TYPE_INTERNAL,
623 .n_errors = ARRAY_LEN (acl_fa_error_strings),
624 .error_strings = acl_fa_error_strings,
625 .n_next_nodes = ACL_FA_N_NEXT,
626 /* edit / add dispositions here */
629 [ACL_FA_ERROR_DROP] = "error-drop",
633 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
635 .arc_name = "ip4-output",
636 .node_name = "acl-plugin-out-ip4-fa",
637 .runs_before = VNET_FEATURES ("interface-output"),
644 * fd.io coding-style-patch-verification: ON
647 * eval: (c-set-style "gnu")