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))
133 if (sw_if_index == ~0)
134 return clib_error_return (0, "Please specify an interface...");
136 rv = mactime_enable_disable (mm, sw_if_index, enable_disable);
143 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
144 return clib_error_return
145 (0, "Invalid interface, only works on physical ports");
148 case VNET_API_ERROR_UNIMPLEMENTED:
149 return clib_error_return (0,
150 "Device driver doesn't support redirection");
154 return clib_error_return (0, "mactime_enable_disable returned %d", rv);
160 VLIB_CLI_COMMAND (mactime_enable_disable_command, static) =
162 .path = "mactime enable-disable",
164 "mactime enable-disable <interface-name> [disable]",
165 .function = mactime_enable_disable_command_fn,
170 /** Enable / disable time-base src mac filtration on an interface
173 static void vl_api_mactime_enable_disable_t_handler
174 (vl_api_mactime_enable_disable_t * mp)
176 vl_api_mactime_enable_disable_reply_t *rmp;
177 mactime_main_t *mm = &mactime_main;
180 VALIDATE_SW_IF_INDEX (mp);
182 rv = mactime_enable_disable (mm, ntohl (mp->sw_if_index),
183 (int) (mp->enable_disable));
184 BAD_SW_IF_INDEX_LABEL;
185 REPLY_MACRO (VL_API_MACTIME_ENABLE_DISABLE_REPLY);
188 /** Create a lookup table entry for the indicated mac address
191 mactime_send_create_entry_message (u8 * mac_address)
193 mactime_main_t *mm = &mactime_main;
195 vl_shmem_hdr_t *shmem_hdr;
197 vl_api_mactime_add_del_range_t *mp;
200 shmem_hdr = am->shmem_hdr;
201 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
202 memset (mp, 0, sizeof (*mp));
203 mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base);
204 name = format (0, "mac-%U", format_mac_address, mac_address);
206 memcpy (mp->device_name, name, vec_len (name));
207 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
208 /* $$$ config: create allow / drop / range */
211 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
214 /** Add or delete static / dynamic accept/drop configuration for a src mac
217 static void vl_api_mactime_add_del_range_t_handler
218 (vl_api_mactime_add_del_range_t * mp)
220 mactime_main_t *mm = &mactime_main;
221 vl_api_mactime_add_del_range_reply_t *rmp;
222 mactime_device_t *dp;
223 clib_bihash_kv_8_8_t kv;
225 clib_bihash_8_8_t *lut = &mm->lookup_table;
230 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 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;
272 /* no ranges, it's a static allow/drop */
274 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
276 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
279 /* Add the hash table entry */
280 kv.value = dp - mm->devices;
281 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
283 else /* add more ranges */
285 dp = pool_elt_at_index (mm->devices, kv.value);
286 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
288 clib_timebase_range_t _r, *r = &_r;
289 r->start = mp->ranges[i].start;
290 r->end = mp->ranges[i].end;
291 vec_add1 (dp->ranges, r[0]);
295 else /* delete case */
299 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
303 /* find the device entry */
304 dp = pool_elt_at_index (mm->devices, kv.value);
306 /* Remove it from the lookup table */
307 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
308 vec_free (dp->ranges);
309 pool_put (mm->devices, dp);
313 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
316 /* Set up the API message handling tables */
317 static clib_error_t *
318 mactime_plugin_api_hookup (vlib_main_t * vm)
320 mactime_main_t *mm = &mactime_main;
322 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
324 vl_api_##n##_t_handler, \
326 vl_api_##n##_t_endian, \
327 vl_api_##n##_t_print, \
328 sizeof(vl_api_##n##_t), 1);
329 foreach_mactime_plugin_api_msg;
335 #define vl_msg_name_crc_list
336 #include <mactime/mactime_all_api_h.h>
337 #undef vl_msg_name_crc_list
340 setup_message_id_table (mactime_main_t * mm, api_main_t * am)
342 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n #crc, id + mm->msg_id_base);
343 foreach_vl_msg_name_crc_mactime;
347 static clib_error_t *
348 mactime_init (vlib_main_t * vm)
350 mactime_main_t *mm = &mactime_main;
351 clib_error_t *error = 0;
355 mm->vnet_main = vnet_get_main ();
357 name = format (0, "mactime_%08x%c", api_version, 0);
359 /* Ask for a correctly-sized block of API message decode slots */
360 mm->msg_id_base = vl_msg_api_get_msg_ids
361 ((char *) name, VL_MSG_FIRST_AVAILABLE);
363 error = mactime_plugin_api_hookup (vm);
365 /* Add our API messages to the global name_crc hash table */
366 setup_message_id_table (mm, &api_main);
370 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
371 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
372 mm->timezone_offset = -5; /* US EST / EDT */
376 VLIB_INIT_FUNCTION (mactime_init);
378 static clib_error_t *
379 mactime_config (vlib_main_t * vm, unformat_input_t * input)
381 mactime_main_t *mm = &mactime_main;
383 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
385 if (unformat (input, "lookup-table-buckets %u",
386 &mm->lookup_table_num_buckets))
388 else if (unformat (input, "lookup-table-memory %U",
389 unformat_memory_size, &mm->lookup_table_memory_size))
391 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
395 return clib_error_return (0, "unknown input '%U'",
396 format_unformat_error, input);
402 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
405 VNET_FEATURE_INIT (mactime, static) =
407 .arc_name = "device-input",
408 .node_name = "mactime",
409 .runs_before = VNET_FEATURES ("ethernet-input"),
414 VNET_FEATURE_INIT (mactime_tx, static) =
416 .arc_name = "interface-output",
417 .node_name = "mactime-tx",
418 .runs_before = VNET_FEATURES ("interface-tx"),
423 VLIB_PLUGIN_REGISTER () =
425 .version = VPP_BUILD_VER,
426 .description = "Time-based MAC source-address filter",
430 static clib_error_t *
431 show_mactime_command_fn (vlib_main_t * vm,
432 unformat_input_t * input, vlib_cli_command_t * cmd)
434 mactime_main_t *mm = &mactime_main;
435 mactime_device_t *dp;
438 u32 *pool_indices = 0;
440 int current_status = 99;
443 vlib_counter_t allow, drop;
444 ethernet_arp_ip4_entry_t *n, *pool;
446 vec_reset_length (mm->arp_cache_copy);
447 pool = ip4_neighbors_pool ();
450 pool_foreach (n, pool,
452 vec_add1 (mm->arp_cache_copy, n[0]);
456 now = clib_timebase_now (&mm->timebase);
458 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
459 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
461 if (unformat (input, "verbose %d", &verbose))
464 if (unformat (input, "verbose"))
468 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
471 pool_foreach (dp, mm->devices,
473 vec_add1 (pool_indices, dp - mm->devices);
477 vlib_cli_output (vm, "%-15s %18s %14s %10s %10s %10s",
478 "Device Name", "Addresses", "Status",
479 "AllowPkt", "AllowByte", "DropPkt");
481 for (i = 0; i < vec_len (pool_indices); i++)
483 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
485 /* Check dynamic ranges */
486 for (j = 0; j < vec_len (dp->ranges); j++)
488 clib_timebase_range_t *r = dp->ranges + j;
491 start0 = r->start + mm->sunday_midnight;
492 end0 = r->end + mm->sunday_midnight;
494 vlib_cli_output (vm, " Range %d: %U - %U", j,
495 format_clib_timebase_time, start0,
496 format_clib_timebase_time, end0);
498 if (now >= start0 && now <= end0)
500 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
506 vlib_cli_output (vm, " Time in range %d:", j);
507 vlib_cli_output (vm, " %U - %U",
508 format_clib_timebase_time, start0,
509 format_clib_timebase_time, end0);
515 vlib_cli_output (vm, " No range match.");
516 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
518 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
520 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
522 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
526 vec_reset_length (macstring);
527 macstring = format (0, "%U", format_mac_address, dp->mac_address);
528 switch (current_status)
531 status_string = "static drop";
534 status_string = "static allow";
537 status_string = "dynamic drop";
540 status_string = "dynamic allow";
543 status_string = "code bug!";
546 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
548 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
549 vlib_cli_output (vm, "%-15s %18s %14s %10lld %10lld %10lld",
550 dp->device_name, macstring, status_string,
551 allow.packets, allow.bytes, drop.packets);
552 /* This is really only good for small N... */
553 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
555 n = mm->arp_cache_copy + j;
556 if (!memcmp (dp->mac_address, n->ethernet_address,
557 sizeof (n->ethernet_address)))
559 vlib_cli_output (vm, "%17s%U", " ", format_ip4_address,
564 vec_free (macstring);
565 vec_free (pool_indices);
571 VLIB_CLI_COMMAND (show_mactime_command, static) =
573 .path = "show mactime",
574 .short_help = "show mactime [verbose]",
575 .function = show_mactime_command_fn,
579 static clib_error_t *
580 clear_mactime_command_fn (vlib_main_t * vm,
581 unformat_input_t * input, vlib_cli_command_t * cmd)
583 mactime_main_t *mm = &mactime_main;
585 if (mm->feature_initialized == 0)
586 return clib_error_return (0, "feature not enabled");
588 vlib_clear_combined_counters (&mm->allow_counters);
589 vlib_clear_combined_counters (&mm->drop_counters);
590 vlib_cli_output (vm, "Mactime counters cleared...");
595 VLIB_CLI_COMMAND (clear_mactime_command, static) =
597 .path = "clear mactime",
598 .short_help = "clear mactime counters",
599 .function = clear_mactime_command_fn,
606 * fd.io coding-style-patch-verification: ON
609 * eval: (c-set-style "gnu")