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_fill_5tuple (&acl_main, sw_if_index0, b[0], is_ip6,
148 is_input, is_l2_path, &fa_5tuple);
150 #ifdef FA_NODE_VERBOSE_DEBUG
152 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx %016llx",
153 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
154 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
157 /* Try to match an existing session first */
159 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
161 u64 value_sess = ~0ULL;
162 if (acl_fa_find_session
163 (am, is_ip6, sw_if_index0, &fa_5tuple, &value_sess)
164 && (value_sess != ~0ULL))
166 trace_bitmap |= 0x80000000;
167 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
168 fa_full_session_id_t f_sess_id;
170 f_sess_id.as_u64 = value_sess;
171 ASSERT (f_sess_id.thread_index < vec_len (vlib_mains));
174 get_session_ptr (am, f_sess_id.thread_index,
175 f_sess_id.session_index);
176 int old_timeout_type = fa_session_get_timeout_type (am, sess);
178 acl_fa_track_session (am, is_input, sw_if_index0, now,
180 /* expose the session id to the tracer */
181 match_rule_index = f_sess_id.session_index;
182 int new_timeout_type = fa_session_get_timeout_type (am, sess);
183 acl_check_needed = 0;
184 pkts_exist_session += 1;
185 /* Tracking might have changed the session timeout type, e.g. from transient to established */
186 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
188 acl_fa_restart_timer_for_session (am, now, f_sess_id);
189 pkts_restart_session_timer++;
191 0x00010000 + ((0xff & old_timeout_type) << 8) +
192 (0xff & new_timeout_type);
195 * I estimate the likelihood to be very low - the VPP needs
196 * to have >64K interfaces to start with and then on
197 * exactly 64K indices apart needs to be exactly the same
198 * 5-tuple... Anyway, since this probability is nonzero -
199 * print an error and drop the unlucky packet.
200 * If this shows up in real world, we would need to bump
201 * the hash key length.
203 if (PREDICT_FALSE (sess->sw_if_index != sw_if_index0))
206 ("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
207 acl_check_needed = 0;
210 if (PREDICT_FALSE (am->reclassify_sessions))
212 /* if the MSB of policy epoch matches but not the LSB means it is a stale session */
214 ((current_policy_epoch ^
215 f_sess_id.intf_policy_epoch) &
216 FA_POLICY_EPOCH_IS_INPUT))
217 && (current_policy_epoch !=
218 f_sess_id.intf_policy_epoch))
220 /* delete session and increment the counter */
222 (pw->fa_session_epoch_change_by_sw_if_index,
224 vec_elt (pw->fa_session_epoch_change_by_sw_if_index,
226 if (acl_fa_conn_list_delete_session
227 (am, f_sess_id, now))
229 /* delete the session only if we were able to unlink it */
230 acl_fa_two_stage_delete_session (am, sw_if_index0,
233 acl_check_needed = 1;
234 trace_bitmap |= 0x40000000;
240 if (acl_check_needed)
242 action = 0; /* deny by default */
243 acl_plugin_match_5tuple_inline (&acl_main, lc_index0,
244 (fa_5tuple_opaque_t *) &
245 fa_5tuple, is_ip6, &action,
248 &match_rule_index, &trace_bitmap);
251 pkts_acl_permit += 1;
254 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
255 acl_fa_try_recycle_session (am, is_input, thread_index,
258 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
261 acl_fa_add_session (am, is_input, is_ip6,
264 current_policy_epoch);
265 acl_fa_track_session (am, is_input, sw_if_index0,
266 now, sess, &fa_5tuple);
267 pkts_new_session += 1;
272 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
282 next0 = vnet_l2_feature_next (b[0], l2_feat_next_node_index, 0);
284 vnet_feature_next (&next0, b[0]);
286 #ifdef FA_NODE_VERBOSE_DEBUG
288 ("ACL_FA_NODE_DBG: sw_if_index %d lc_index %d action %d acl_index %d rule_index %d",
289 sw_if_index0, lc_index0, action, match_acl_in_index,
293 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
294 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
296 acl_fa_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
297 t->sw_if_index = sw_if_index0;
298 t->lc_index = lc_index0;
299 t->next_index = next0;
300 t->match_acl_in_index = match_acl_in_index;
301 t->match_rule_index = match_rule_index;
302 t->packet_info[0] = fa_5tuple.kv_40_8.key[0];
303 t->packet_info[1] = fa_5tuple.kv_40_8.key[1];
304 t->packet_info[2] = fa_5tuple.kv_40_8.key[2];
305 t->packet_info[3] = fa_5tuple.kv_40_8.key[3];
306 t->packet_info[4] = fa_5tuple.kv_40_8.key[4];
307 t->packet_info[5] = fa_5tuple.kv_40_8.value;
309 t->trace_bitmap = trace_bitmap;
312 next0 = next0 < node->n_next_nodes ? next0 : 0;
314 b[0]->error = error_node->errors[error0];
319 pkts_acl_checked += 1;
322 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
324 vlib_node_increment_counter (vm, acl_fa_node->index,
325 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
326 vlib_node_increment_counter (vm, acl_fa_node->index,
327 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
328 vlib_node_increment_counter (vm, acl_fa_node->index,
329 ACL_FA_ERROR_ACL_NEW_SESSION,
331 vlib_node_increment_counter (vm, acl_fa_node->index,
332 ACL_FA_ERROR_ACL_EXIST_SESSION,
334 vlib_node_increment_counter (vm, acl_fa_node->index,
335 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
336 pkts_restart_session_timer);
337 return frame->n_vectors;
340 vlib_node_registration_t acl_in_l2_ip6_node;
341 VLIB_NODE_FN (acl_in_l2_ip6_node) (vlib_main_t * vm,
342 vlib_node_runtime_t * node,
343 vlib_frame_t * frame)
345 acl_main_t *am = &acl_main;
346 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
347 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
348 &acl_in_l2_ip6_node);
351 vlib_node_registration_t acl_in_l2_ip4_node;
352 VLIB_NODE_FN (acl_in_l2_ip4_node) (vlib_main_t * vm,
353 vlib_node_runtime_t * node,
354 vlib_frame_t * frame)
356 acl_main_t *am = &acl_main;
357 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
358 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
359 &acl_in_l2_ip4_node);
362 vlib_node_registration_t acl_out_l2_ip6_node;
363 VLIB_NODE_FN (acl_out_l2_ip6_node) (vlib_main_t * vm,
364 vlib_node_runtime_t * node,
365 vlib_frame_t * frame)
367 acl_main_t *am = &acl_main;
368 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
369 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
370 &acl_out_l2_ip6_node);
373 vlib_node_registration_t acl_out_l2_ip4_node;
374 VLIB_NODE_FN (acl_out_l2_ip4_node) (vlib_main_t * vm,
375 vlib_node_runtime_t * node,
376 vlib_frame_t * frame)
378 acl_main_t *am = &acl_main;
379 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
380 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
381 &acl_out_l2_ip4_node);
384 /**** L3 processing path nodes ****/
386 vlib_node_registration_t acl_in_fa_ip6_node;
387 VLIB_NODE_FN (acl_in_fa_ip6_node) (vlib_main_t * vm,
388 vlib_node_runtime_t * node,
389 vlib_frame_t * frame)
391 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
394 vlib_node_registration_t acl_in_fa_ip4_node;
395 VLIB_NODE_FN (acl_in_fa_ip4_node) (vlib_main_t * vm,
396 vlib_node_runtime_t * node,
397 vlib_frame_t * frame)
399 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
402 vlib_node_registration_t acl_out_fa_ip6_node;
403 VLIB_NODE_FN (acl_out_fa_ip6_node) (vlib_main_t * vm,
404 vlib_node_runtime_t * node,
405 vlib_frame_t * frame)
407 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
410 vlib_node_registration_t acl_out_fa_ip4_node;
411 VLIB_NODE_FN (acl_out_fa_ip4_node) (vlib_main_t * vm,
412 vlib_node_runtime_t * node,
413 vlib_frame_t * frame)
415 return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
419 format_fa_5tuple (u8 * s, va_list * args)
421 fa_5tuple_t *p5t = va_arg (*args, fa_5tuple_t *);
424 return format (s, "lc_index %d (lsb16 of sw_if_index %d) l3 %s%s %U -> %U"
425 " l4 proto %d l4_valid %d port %d -> %d tcp flags (%s) %02x rsvd %x",
426 p5t->pkt.lc_index, p5t->l4.lsb_of_sw_if_index,
429 pkt.is_nonfirst_fragment ? " non-initial fragment" : "",
430 format_ip6_address, &p5t->ip6_addr[0], format_ip6_address,
431 &p5t->ip6_addr[1], p5t->l4.proto, p5t->pkt.l4_valid,
432 p5t->l4.port[0], p5t->l4.port[1],
433 p5t->pkt.tcp_flags_valid ? "valid" : "invalid",
434 p5t->pkt.tcp_flags, p5t->pkt.flags_reserved);
436 return format (s, "lc_index %d (lsb16 of sw_if_index %d) l3 %s%s %U -> %U"
437 " l4 proto %d l4_valid %d port %d -> %d tcp flags (%s) %02x rsvd %x",
438 p5t->pkt.lc_index, p5t->l4.lsb_of_sw_if_index,
441 pkt.is_nonfirst_fragment ? " non-initial fragment" : "",
442 format_ip4_address, &p5t->ip4_addr[0], format_ip4_address,
443 &p5t->ip4_addr[1], p5t->l4.proto, p5t->pkt.l4_valid,
444 p5t->l4.port[0], p5t->l4.port[1],
445 p5t->pkt.tcp_flags_valid ? "valid" : "invalid",
446 p5t->pkt.tcp_flags, p5t->pkt.flags_reserved);
449 #ifndef CLIB_MARCH_VARIANT
451 format_acl_plugin_5tuple (u8 * s, va_list * args)
453 return format_fa_5tuple (s, args);
457 /* packet trace format function */
459 format_acl_plugin_trace (u8 * s, va_list * args)
461 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
462 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
463 acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
467 "acl-plugin: lc_index: %d, sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
468 " pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
469 t->lc_index, t->sw_if_index, t->next_index, t->action,
470 t->match_acl_in_index, t->match_rule_index, t->trace_bitmap,
471 t->packet_info[0], t->packet_info[1], t->packet_info[2],
472 t->packet_info[3], t->packet_info[4], t->packet_info[5]);
474 /* Now also print out the packet_info in a form usable by humans */
475 s = format (s, "\n %U", format_fa_5tuple, t->packet_info);
481 static char *acl_fa_error_strings[] = {
482 #define _(sym,string) string,
487 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
489 .name = "acl-plugin-in-ip6-l2",
490 .vector_size = sizeof (u32),
491 .format_trace = format_acl_plugin_trace,
492 .type = VLIB_NODE_TYPE_INTERNAL,
493 .n_errors = ARRAY_LEN (acl_fa_error_strings),
494 .error_strings = acl_fa_error_strings,
495 .n_next_nodes = ACL_FA_N_NEXT,
498 [ACL_FA_ERROR_DROP] = "error-drop",
502 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
504 .name = "acl-plugin-in-ip4-l2",
505 .vector_size = sizeof (u32),
506 .format_trace = format_acl_plugin_trace,
507 .type = VLIB_NODE_TYPE_INTERNAL,
508 .n_errors = ARRAY_LEN (acl_fa_error_strings),
509 .error_strings = acl_fa_error_strings,
510 .n_next_nodes = ACL_FA_N_NEXT,
513 [ACL_FA_ERROR_DROP] = "error-drop",
517 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
519 .name = "acl-plugin-out-ip6-l2",
520 .vector_size = sizeof (u32),
521 .format_trace = format_acl_plugin_trace,
522 .type = VLIB_NODE_TYPE_INTERNAL,
523 .n_errors = ARRAY_LEN (acl_fa_error_strings),
524 .error_strings = acl_fa_error_strings,
525 .n_next_nodes = ACL_FA_N_NEXT,
528 [ACL_FA_ERROR_DROP] = "error-drop",
532 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
534 .name = "acl-plugin-out-ip4-l2",
535 .vector_size = sizeof (u32),
536 .format_trace = format_acl_plugin_trace,
537 .type = VLIB_NODE_TYPE_INTERNAL,
538 .n_errors = ARRAY_LEN (acl_fa_error_strings),
539 .error_strings = acl_fa_error_strings,
540 .n_next_nodes = ACL_FA_N_NEXT,
543 [ACL_FA_ERROR_DROP] = "error-drop",
548 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
550 .name = "acl-plugin-in-ip6-fa",
551 .vector_size = sizeof (u32),
552 .format_trace = format_acl_plugin_trace,
553 .type = VLIB_NODE_TYPE_INTERNAL,
554 .n_errors = ARRAY_LEN (acl_fa_error_strings),
555 .error_strings = acl_fa_error_strings,
556 .n_next_nodes = ACL_FA_N_NEXT,
559 [ACL_FA_ERROR_DROP] = "error-drop",
563 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
565 .arc_name = "ip6-unicast",
566 .node_name = "acl-plugin-in-ip6-fa",
567 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
570 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
572 .name = "acl-plugin-in-ip4-fa",
573 .vector_size = sizeof (u32),
574 .format_trace = format_acl_plugin_trace,
575 .type = VLIB_NODE_TYPE_INTERNAL,
576 .n_errors = ARRAY_LEN (acl_fa_error_strings),
577 .error_strings = acl_fa_error_strings,
578 .n_next_nodes = ACL_FA_N_NEXT,
581 [ACL_FA_ERROR_DROP] = "error-drop",
585 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
587 .arc_name = "ip4-unicast",
588 .node_name = "acl-plugin-in-ip4-fa",
589 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
593 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
595 .name = "acl-plugin-out-ip6-fa",
596 .vector_size = sizeof (u32),
597 .format_trace = format_acl_plugin_trace,
598 .type = VLIB_NODE_TYPE_INTERNAL,
599 .n_errors = ARRAY_LEN (acl_fa_error_strings),
600 .error_strings = acl_fa_error_strings,
601 .n_next_nodes = ACL_FA_N_NEXT,
604 [ACL_FA_ERROR_DROP] = "error-drop",
608 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
610 .arc_name = "ip6-output",
611 .node_name = "acl-plugin-out-ip6-fa",
612 .runs_before = VNET_FEATURES ("interface-output"),
615 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
617 .name = "acl-plugin-out-ip4-fa",
618 .vector_size = sizeof (u32),
619 .format_trace = format_acl_plugin_trace,
620 .type = VLIB_NODE_TYPE_INTERNAL,
621 .n_errors = ARRAY_LEN (acl_fa_error_strings),
622 .error_strings = acl_fa_error_strings,
623 .n_next_nodes = ACL_FA_N_NEXT,
624 /* edit / add dispositions here */
627 [ACL_FA_ERROR_DROP] = "error-drop",
631 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
633 .arc_name = "ip4-output",
634 .node_name = "acl-plugin-out-ip4-fa",
635 .runs_before = VNET_FEATURES ("interface-output"),
641 * fd.io coding-style-patch-verification: ON
644 * eval: (c-set-style "gnu")