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);
109 static clib_error_t *
110 mactime_enable_disable_command_fn (vlib_main_t * vm,
111 unformat_input_t * input,
112 vlib_cli_command_t * cmd)
114 mactime_main_t *mm = &mactime_main;
115 u32 sw_if_index = ~0;
116 int enable_disable = 1;
120 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
122 if (unformat (input, "disable"))
124 else if (unformat (input, "%U", unformat_vnet_sw_interface,
125 mm->vnet_main, &sw_if_index))
131 if (sw_if_index == ~0)
132 return clib_error_return (0, "Please specify an interface...");
134 rv = mactime_enable_disable (mm, sw_if_index, enable_disable);
141 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
142 return clib_error_return
143 (0, "Invalid interface, only works on physical ports");
146 case VNET_API_ERROR_UNIMPLEMENTED:
147 return clib_error_return (0,
148 "Device driver doesn't support redirection");
152 return clib_error_return (0, "mactime_enable_disable returned %d", rv);
158 VLIB_CLI_COMMAND (mactime_enable_disable_command, static) =
160 .path = "mactime enable-disable",
162 "mactime enable-disable <interface-name> [disable]",
163 .function = mactime_enable_disable_command_fn,
168 /** Enable / disable time-base src mac filtration on an interface
171 static void vl_api_mactime_enable_disable_t_handler
172 (vl_api_mactime_enable_disable_t * mp)
174 vl_api_mactime_enable_disable_reply_t *rmp;
175 mactime_main_t *mm = &mactime_main;
178 rv = mactime_enable_disable (mm, ntohl (mp->sw_if_index),
179 (int) (mp->enable_disable));
181 REPLY_MACRO (VL_API_MACTIME_ENABLE_DISABLE_REPLY);
184 /** Add or delete static / dynamic accept/drop configuration for a src mac
187 static void vl_api_mactime_add_del_range_t_handler
188 (vl_api_mactime_add_del_range_t * mp)
190 mactime_main_t *mm = &mactime_main;
191 vl_api_mactime_add_del_range_reply_t *rmp;
192 mactime_device_t *dp;
193 clib_bihash_kv_8_8_t kv;
195 clib_bihash_8_8_t *lut = &mm->lookup_table;
200 memset (&kv, 0, sizeof (kv));
201 memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address));
203 /* See if we have a lookup table entry for this src mac address */
204 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
210 /* Create the device entry? */
213 pool_get (mm->devices, dp);
214 memset (dp, 0, sizeof (*dp));
215 vlib_validate_simple_counter (&mm->allow_counters,
217 vlib_zero_simple_counter (&mm->allow_counters, dp - mm->devices);
218 vlib_validate_simple_counter (&mm->drop_counters, dp - mm->devices);
219 vlib_zero_simple_counter (&mm->drop_counters, dp - mm->devices);
220 mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0;
221 dp->device_name = format (0, "%s%c", mp->device_name, 0);
222 memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address));
223 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
225 clib_timebase_range_t _r, *r = &_r;
226 r->start = mp->ranges[i].start;
227 r->end = mp->ranges[i].end;
228 vec_add1 (dp->ranges, r[0]);
230 /* If we found some time ranges */
233 /* Set allow/drop based on msg flags */
235 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
237 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
241 /* no ranges, it's a static allow/drop */
243 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
245 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
248 /* Add the hash table entry */
249 kv.value = dp - mm->devices;
250 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
252 else /* add more ranges */
254 dp = pool_elt_at_index (mm->devices, kv.value);
255 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
257 clib_timebase_range_t _r, *r = &_r;
258 r->start = mp->ranges[i].start;
259 r->end = mp->ranges[i].end;
260 vec_add1 (dp->ranges, r[0]);
264 else /* delete case */
268 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
272 /* find the device entry */
273 dp = pool_elt_at_index (mm->devices, kv.value);
275 /* Remove it from the lookup table */
276 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
277 vec_free (dp->ranges);
278 pool_put (mm->devices, dp);
282 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
285 /* Set up the API message handling tables */
286 static clib_error_t *
287 mactime_plugin_api_hookup (vlib_main_t * vm)
289 mactime_main_t *mm = &mactime_main;
291 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
293 vl_api_##n##_t_handler, \
295 vl_api_##n##_t_endian, \
296 vl_api_##n##_t_print, \
297 sizeof(vl_api_##n##_t), 1);
298 foreach_mactime_plugin_api_msg;
304 #define vl_msg_name_crc_list
305 #include <mactime/mactime_all_api_h.h>
306 #undef vl_msg_name_crc_list
309 setup_message_id_table (mactime_main_t * mm, api_main_t * am)
311 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n #crc, id + mm->msg_id_base);
312 foreach_vl_msg_name_crc_mactime;
316 static clib_error_t *
317 mactime_init (vlib_main_t * vm)
319 mactime_main_t *mm = &mactime_main;
320 clib_error_t *error = 0;
324 mm->vnet_main = vnet_get_main ();
326 name = format (0, "mactime_%08x%c", api_version, 0);
328 /* Ask for a correctly-sized block of API message decode slots */
329 mm->msg_id_base = vl_msg_api_get_msg_ids
330 ((char *) name, VL_MSG_FIRST_AVAILABLE);
332 error = mactime_plugin_api_hookup (vm);
334 /* Add our API messages to the global name_crc hash table */
335 setup_message_id_table (mm, &api_main);
339 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
340 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
341 mm->timezone_offset = -5; /* US EST / EDT */
345 VLIB_INIT_FUNCTION (mactime_init);
347 static clib_error_t *
348 mactime_config (vlib_main_t * vm, unformat_input_t * input)
350 mactime_main_t *mm = &mactime_main;
352 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
354 if (unformat (input, "lookup-table-buckets %u",
355 &mm->lookup_table_num_buckets))
357 else if (unformat (input, "lookup-table-memory %U",
358 unformat_memory_size, &mm->lookup_table_memory_size))
360 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
364 return clib_error_return (0, "unknown input '%U'",
365 format_unformat_error, input);
371 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
374 VNET_FEATURE_INIT (mactime, static) =
376 .arc_name = "device-input",
377 .node_name = "mactime",
378 .runs_before = VNET_FEATURES ("ethernet-input"),
383 VLIB_PLUGIN_REGISTER () =
385 .version = VPP_BUILD_VER,
386 .description = "Time-based MAC source-address filter",
391 format_mac_address (u8 * s, va_list * args)
393 u8 *a = va_arg (*args, u8 *);
394 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
395 a[0], a[1], a[2], a[3], a[4], a[5]);
398 static clib_error_t *
399 show_mactime_command_fn (vlib_main_t * vm,
400 unformat_input_t * input, vlib_cli_command_t * cmd)
402 mactime_main_t *mm = &mactime_main;
403 mactime_device_t *dp;
406 u32 *pool_indices = 0;
408 int current_status = 99;
413 now = clib_timebase_now (&mm->timebase);
415 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
416 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
418 if (unformat (input, "verbose %d", &verbose))
421 if (unformat (input, "verbose"))
425 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
428 pool_foreach (dp, mm->devices,
430 vec_add1 (pool_indices, dp - mm->devices);
434 vlib_cli_output (vm, "%-15s %20s %16s %10s %10s",
435 "Device Name", "MAC address", "Current Status", "Allow",
438 for (i = 0; i < vec_len (pool_indices); i++)
440 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
442 /* Check dynamic ranges */
443 for (j = 0; j < vec_len (dp->ranges); j++)
445 clib_timebase_range_t *r = dp->ranges + j;
448 start0 = r->start + mm->sunday_midnight;
449 end0 = r->end + mm->sunday_midnight;
451 vlib_cli_output (vm, " Range %d: %U - %U", j,
452 format_clib_timebase_time, start0,
453 format_clib_timebase_time, end0);
455 if (now >= start0 && now <= end0)
457 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
463 vlib_cli_output (vm, " Time in range %d:", j);
464 vlib_cli_output (vm, " %U - %U",
465 format_clib_timebase_time, start0,
466 format_clib_timebase_time, end0);
472 vlib_cli_output (vm, " No range match.");
473 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
475 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
477 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
479 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
483 vec_reset_length (macstring);
484 macstring = format (0, "%U", format_mac_address, dp->mac_address);
485 switch (current_status)
488 status_string = "static drop";
491 status_string = "static allow";
494 status_string = "dynamic drop";
497 status_string = "dynamic allow";
500 status_string = "code bug!";
503 allow = vlib_get_simple_counter (&mm->allow_counters, dp - mm->devices);
504 drop = vlib_get_simple_counter (&mm->drop_counters, dp - mm->devices);
505 vlib_cli_output (vm, "%-15s %20s %16s %10lld %10lld",
506 dp->device_name, macstring, status_string,
510 vec_free (macstring);
511 vec_free (pool_indices);
517 VLIB_CLI_COMMAND (show_mactime_command, static) =
519 .path = "show mactime",
520 .short_help = "show mactime [verbose]",
521 .function = show_mactime_command_fn,
528 * fd.io coding-style-patch-verification: ON
531 * eval: (c-set-style "gnu")