2 * mactime.c - time-based src mac address filtration
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <mactime/mactime.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
26 /* define message IDs */
27 #include <mactime/mactime_msg_enum.h>
29 /* define message structures */
31 #include <mactime/mactime_all_api_h.h>
34 /* define generated endian-swappers */
36 #include <mactime/mactime_all_api_h.h>
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #include <mactime/mactime_all_api_h.h>
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <mactime/mactime_all_api_h.h>
50 #define REPLY_MSG_ID_BASE mm->msg_id_base
51 #include <vlibapi/api_helper_macros.h>
53 mactime_main_t mactime_main;
55 /** \file time-base src-mac filter device-input feature arc implementation
58 /* List of message types that this plugin understands */
60 #define foreach_mactime_plugin_api_msg \
61 _(MACTIME_ENABLE_DISABLE, mactime_enable_disable) \
62 _(MACTIME_ADD_DEL_RANGE, mactime_add_del_range)
65 feature_init (mactime_main_t * mm)
67 if (mm->feature_initialized == 0)
69 /* Create the lookup table */
70 clib_bihash_init_8_8 (&mm->lookup_table, "mactime lookup table",
71 mm->lookup_table_num_buckets,
72 mm->lookup_table_memory_size);
73 clib_timebase_init (&mm->timebase, mm->timezone_offset,
74 CLIB_TIMEBASE_DAYLIGHT_USA);
75 mm->allow_counters.name = "allow";
76 mm->allow_counters.stat_segment_name = "/mactime/allow";
77 mm->drop_counters.name = "drop";
78 mm->drop_counters.stat_segment_name = "/mactime/drop";
79 mm->feature_initialized = 1;
83 /** Action function shared between message handler and debug CLI
86 mactime_enable_disable (mactime_main_t * mm, u32 sw_if_index,
89 vnet_sw_interface_t *sw;
95 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
97 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
99 /* Not a physical port? */
100 sw = vnet_get_sw_interface (mm->vnet_main, sw_if_index);
101 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
102 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
104 vnet_feature_enable_disable ("device-input", "mactime",
105 sw_if_index, enable_disable, 0, 0);
106 vnet_feature_enable_disable ("interface-output", "mactime-tx",
107 sw_if_index, enable_disable, 0, 0);
111 static clib_error_t *
112 mactime_enable_disable_command_fn (vlib_main_t * vm,
113 unformat_input_t * input,
114 vlib_cli_command_t * cmd)
116 mactime_main_t *mm = &mactime_main;
117 u32 sw_if_index = ~0;
118 int enable_disable = 1;
122 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
124 if (unformat (input, "disable"))
126 else if (unformat (input, "%U", unformat_vnet_sw_interface,
127 mm->vnet_main, &sw_if_index))
129 else if (unformat (input, "sw_if_index %d", &sw_if_index))
135 if (sw_if_index == ~0)
136 return clib_error_return (0, "Please specify an interface...");
138 rv = mactime_enable_disable (mm, sw_if_index, enable_disable);
145 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
146 return clib_error_return
147 (0, "Invalid interface, only works on physical ports");
151 return clib_error_return (0, "mactime_enable_disable returned %d", rv);
157 VLIB_CLI_COMMAND (mactime_enable_disable_command, static) =
159 .path = "mactime enable-disable",
161 "mactime enable-disable <interface-name> [disable]",
162 .function = mactime_enable_disable_command_fn,
167 /** Enable / disable time-base src mac filtration on an interface
170 static void vl_api_mactime_enable_disable_t_handler
171 (vl_api_mactime_enable_disable_t * mp)
173 vl_api_mactime_enable_disable_reply_t *rmp;
174 mactime_main_t *mm = &mactime_main;
177 VALIDATE_SW_IF_INDEX (mp);
179 rv = mactime_enable_disable (mm, ntohl (mp->sw_if_index),
180 (int) (mp->enable_disable));
181 BAD_SW_IF_INDEX_LABEL;
182 REPLY_MACRO (VL_API_MACTIME_ENABLE_DISABLE_REPLY);
185 /** Create a lookup table entry for the indicated mac address
188 mactime_send_create_entry_message (u8 * mac_address)
190 mactime_main_t *mm = &mactime_main;
192 vl_shmem_hdr_t *shmem_hdr;
194 vl_api_mactime_add_del_range_t *mp;
197 shmem_hdr = am->shmem_hdr;
198 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
199 clib_memset (mp, 0, sizeof (*mp));
200 mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base);
201 name = format (0, "mac-%U", format_mac_address, mac_address);
203 memcpy (mp->device_name, name, vec_len (name));
204 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
205 /* $$$ config: create allow / drop / range */
208 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
211 /** Add or delete static / dynamic accept/drop configuration for a src mac
214 static void vl_api_mactime_add_del_range_t_handler
215 (vl_api_mactime_add_del_range_t * mp)
217 mactime_main_t *mm = &mactime_main;
218 vl_api_mactime_add_del_range_reply_t *rmp;
219 mactime_device_t *dp;
220 clib_bihash_kv_8_8_t kv;
222 clib_bihash_8_8_t *lut = &mm->lookup_table;
228 data_quota = clib_net_to_host_u64 (mp->data_quota);
230 clib_memset (&kv, 0, sizeof (kv));
231 memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address));
233 /* See if we have a lookup table entry for this src mac address */
234 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
240 /* Create the device entry? */
243 pool_get (mm->devices, dp);
244 clib_memset (dp, 0, sizeof (*dp));
245 vlib_validate_combined_counter (&mm->allow_counters,
247 vlib_zero_combined_counter (&mm->allow_counters, dp - mm->devices);
248 vlib_validate_combined_counter (&mm->drop_counters,
250 vlib_zero_combined_counter (&mm->drop_counters, dp - mm->devices);
251 mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0;
252 dp->device_name = format (0, "%s%c", mp->device_name, 0);
253 memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address));
254 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
256 clib_timebase_range_t _r, *r = &_r;
257 r->start = mp->ranges[i].start;
258 r->end = mp->ranges[i].end;
259 vec_add1 (dp->ranges, r[0]);
261 /* If we found some time ranges */
264 /* Set allow/drop based on msg flags */
266 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
268 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
270 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
274 /* no ranges, it's a static allow/drop */
276 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
278 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
280 if (mp->no_udp_10001)
281 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
283 dp->data_quota = data_quota;
285 /* Add the hash table entry */
286 kv.value = dp - mm->devices;
287 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
289 else /* add more ranges, flags, etc. */
291 dp = pool_elt_at_index (mm->devices, kv.value);
293 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
295 clib_timebase_range_t _r, *r = &_r;
296 r->start = mp->ranges[i].start;
297 r->end = mp->ranges[i].end;
298 vec_add1 (dp->ranges, r[0]);
301 if (vec_len (dp->ranges))
303 /* Set allow/drop based on msg flags */
305 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
307 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
309 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
313 /* no ranges, it's a static allow/drop */
315 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
317 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
319 if (mp->no_udp_10001)
320 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
322 dp->data_quota = data_quota;
325 else /* delete case */
329 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
333 /* find the device entry */
334 dp = pool_elt_at_index (mm->devices, kv.value);
336 /* Remove it from the lookup table */
337 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
338 vec_free (dp->ranges);
339 pool_put (mm->devices, dp);
343 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
346 /* Set up the API message handling tables */
347 static clib_error_t *
348 mactime_plugin_api_hookup (vlib_main_t * vm)
350 mactime_main_t *mm = &mactime_main;
352 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
354 vl_api_##n##_t_handler, \
356 vl_api_##n##_t_endian, \
357 vl_api_##n##_t_print, \
358 sizeof(vl_api_##n##_t), 1);
359 foreach_mactime_plugin_api_msg;
365 #define vl_msg_name_crc_list
366 #include <mactime/mactime_all_api_h.h>
367 #undef vl_msg_name_crc_list
370 setup_message_id_table (mactime_main_t * mm, api_main_t * am)
372 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
373 foreach_vl_msg_name_crc_mactime;
377 static clib_error_t *
378 mactime_init (vlib_main_t * vm)
380 mactime_main_t *mm = &mactime_main;
381 clib_error_t *error = 0;
385 mm->vnet_main = vnet_get_main ();
387 name = format (0, "mactime_%08x%c", api_version, 0);
389 /* Ask for a correctly-sized block of API message decode slots */
390 mm->msg_id_base = vl_msg_api_get_msg_ids
391 ((char *) name, VL_MSG_FIRST_AVAILABLE);
393 error = mactime_plugin_api_hookup (vm);
395 /* Add our API messages to the global name_crc hash table */
396 setup_message_id_table (mm, &api_main);
400 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
401 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
402 mm->timezone_offset = -5; /* US EST / EDT */
406 VLIB_INIT_FUNCTION (mactime_init);
408 static clib_error_t *
409 mactime_config (vlib_main_t * vm, unformat_input_t * input)
411 mactime_main_t *mm = &mactime_main;
413 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
415 if (unformat (input, "lookup-table-buckets %u",
416 &mm->lookup_table_num_buckets))
418 else if (unformat (input, "lookup-table-memory %U",
419 unformat_memory_size, &mm->lookup_table_memory_size))
421 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
425 return clib_error_return (0, "unknown input '%U'",
426 format_unformat_error, input);
432 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
435 VNET_FEATURE_INIT (mactime, static) =
437 .arc_name = "device-input",
438 .node_name = "mactime",
439 .runs_before = VNET_FEATURES ("ethernet-input"),
444 VNET_FEATURE_INIT (mactime_tx, static) =
446 .arc_name = "interface-output",
447 .node_name = "mactime-tx",
448 .runs_before = VNET_FEATURES ("interface-tx"),
453 VLIB_PLUGIN_REGISTER () =
455 .version = VPP_BUILD_VER,
456 .description = "Time-based MAC Source Address Filter",
461 format_bytes_with_width (u8 * s, va_list * va)
463 uword nbytes = va_arg (*va, u64);
464 int width = va_arg (*va, int);
470 fmt = format (0, "%%%d.3f%%s%c", width, 0);
472 fmt = format (0, "%%.3f%%s%c", 0);
474 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
476 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
479 else if (nbytes > (1024ULL * 1024ULL))
481 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
484 else if (nbytes > 1024ULL)
486 nbytes_f64 = ((f64) nbytes) / (1024.0);
491 nbytes_f64 = (f64) nbytes;
495 s = format (s, (char *) fmt, nbytes_f64, suffix);
500 static clib_error_t *
501 show_mactime_command_fn (vlib_main_t * vm,
502 unformat_input_t * input, vlib_cli_command_t * cmd)
504 mactime_main_t *mm = &mactime_main;
505 mactime_device_t *dp;
508 u32 *pool_indices = 0;
510 int current_status = 99;
513 vlib_counter_t allow, drop;
514 ethernet_arp_ip4_entry_t *n, *pool;
516 vec_reset_length (mm->arp_cache_copy);
517 pool = ip4_neighbors_pool ();
520 pool_foreach (n, pool,
522 vec_add1 (mm->arp_cache_copy, n[0]);
526 now = clib_timebase_now (&mm->timebase);
528 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
529 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
531 if (unformat (input, "verbose %d", &verbose))
534 if (unformat (input, "verbose"))
538 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
541 pool_foreach (dp, mm->devices,
543 vec_add1 (pool_indices, dp - mm->devices);
547 vlib_cli_output (vm, "%-15s %18s %14s %10s %11s %13s",
548 "Device Name", "Addresses", "Status",
549 "AllowPkt", "AllowByte", "DropPkt");
551 for (i = 0; i < vec_len (pool_indices); i++)
553 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
555 /* Check dynamic ranges */
556 for (j = 0; j < vec_len (dp->ranges); j++)
558 clib_timebase_range_t *r = dp->ranges + j;
561 start0 = r->start + mm->sunday_midnight;
562 end0 = r->end + mm->sunday_midnight;
564 vlib_cli_output (vm, " Range %d: %U - %U", j,
565 format_clib_timebase_time, start0,
566 format_clib_timebase_time, end0);
568 if (now >= start0 && now <= end0)
570 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
572 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
578 vlib_cli_output (vm, " Time in range %d:", j);
579 vlib_cli_output (vm, " %U - %U",
580 format_clib_timebase_time, start0,
581 format_clib_timebase_time, end0);
587 vlib_cli_output (vm, " No range match.");
588 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
590 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
592 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
594 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
596 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
600 vec_reset_length (macstring);
601 macstring = format (0, "%U", format_mac_address, dp->mac_address);
602 switch (current_status)
605 status_string = "static drop";
608 status_string = "static allow";
611 status_string = "dynamic drop";
614 status_string = "dynamic allow";
617 status_string = "d-quota inact";
620 status_string = "d-quota activ";
623 status_string = "code bug!";
626 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
628 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
629 vlib_cli_output (vm, "%-15s %18s %14s %10lld %U %13lld",
630 dp->device_name, macstring, status_string,
631 allow.packets, format_bytes_with_width, allow.bytes,
633 if (dp->data_quota > 0)
634 vlib_cli_output (vm, "%-54s %s%U %s%U", " ", "Quota ",
635 format_bytes_with_width, dp->data_quota, 10,
636 "Use ", format_bytes_with_width,
637 dp->data_used_in_range, 8);
638 /* This is really only good for small N... */
639 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
641 n = mm->arp_cache_copy + j;
642 if (!memcmp (dp->mac_address, n->mac.bytes, sizeof (n->mac)))
644 vlib_cli_output (vm, "%17s%U", " ", format_ip4_address,
649 vec_free (macstring);
650 vec_free (pool_indices);
656 VLIB_CLI_COMMAND (show_mactime_command, static) =
658 .path = "show mactime",
659 .short_help = "show mactime [verbose]",
660 .function = show_mactime_command_fn,
664 static clib_error_t *
665 clear_mactime_command_fn (vlib_main_t * vm,
666 unformat_input_t * input, vlib_cli_command_t * cmd)
668 mactime_main_t *mm = &mactime_main;
670 if (mm->feature_initialized == 0)
671 return clib_error_return (0, "feature not enabled");
673 vlib_clear_combined_counters (&mm->allow_counters);
674 vlib_clear_combined_counters (&mm->drop_counters);
675 vlib_cli_output (vm, "Mactime counters cleared...");
680 VLIB_CLI_COMMAND (clear_mactime_command, static) =
682 .path = "clear mactime",
683 .short_help = "clear mactime counters",
684 .function = clear_mactime_command_fn,
691 * fd.io coding-style-patch-verification: ON
694 * eval: (c-set-style "gnu")