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 <vnet/format_fns.h>
28 #include <mactime/mactime.api_enum.h>
29 #include <mactime/mactime.api_types.h>
31 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
33 #define REPLY_MSG_ID_BASE mm->msg_id_base
34 #include <vlibapi/api_helper_macros.h>
36 #include <vnet/ip-neighbor/ip_neighbor.h>
38 mactime_main_t mactime_main;
41 * time-base src-mac filter device-input feature arc implementation
45 feature_init (mactime_main_t * mm)
47 if (mm->feature_initialized == 0)
49 /* Create the lookup table */
50 clib_bihash_init_8_8 (&mm->lookup_table, "mactime lookup table",
51 mm->lookup_table_num_buckets,
52 mm->lookup_table_memory_size);
53 clib_timebase_init (&mm->timebase, mm->timezone_offset,
54 CLIB_TIMEBASE_DAYLIGHT_USA,
55 &(mm->vlib_main->clib_time));
56 mm->allow_counters.name = "allow";
57 mm->allow_counters.stat_segment_name = "/mactime/allow";
58 mm->drop_counters.name = "drop";
59 mm->drop_counters.stat_segment_name = "/mactime/drop";
60 mm->feature_initialized = 1;
64 /** Action function shared between message handler and debug CLI
67 mactime_enable_disable (mactime_main_t * mm, u32 sw_if_index,
70 vnet_sw_interface_t *sw;
72 static u8 url_init_done;
77 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
79 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
81 /* Not a physical port? */
82 sw = vnet_get_sw_interface (mm->vnet_main, sw_if_index);
83 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
84 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
86 vnet_feature_enable_disable ("device-input", "mactime",
87 sw_if_index, enable_disable, 0, 0);
88 vnet_feature_enable_disable ("interface-output", "mactime-tx",
89 sw_if_index, enable_disable, 0, 0);
90 if (url_init_done == 0)
92 mactime_url_init (mm->vlib_main);
100 mactime_enable_disable_command_fn (vlib_main_t * vm,
101 unformat_input_t * input,
102 vlib_cli_command_t * cmd)
104 mactime_main_t *mm = &mactime_main;
105 u32 sw_if_index = ~0;
106 int enable_disable = 1;
110 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
112 if (unformat (input, "disable"))
114 else if (unformat (input, "%U", unformat_vnet_sw_interface,
115 mm->vnet_main, &sw_if_index))
117 else if (unformat (input, "sw_if_index %d", &sw_if_index))
123 if (sw_if_index == ~0)
124 return clib_error_return (0, "Please specify an interface...");
126 rv = mactime_enable_disable (mm, sw_if_index, enable_disable);
133 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
134 return clib_error_return
135 (0, "Invalid interface, only works on physical ports");
139 return clib_error_return (0, "mactime_enable_disable returned %d", rv);
145 VLIB_CLI_COMMAND (mactime_enable_disable_command, static) =
147 .path = "mactime enable-disable",
149 "mactime enable-disable <interface-name> [disable]",
150 .function = mactime_enable_disable_command_fn,
155 /** Enable / disable time-base src mac filtration on an interface
158 static void vl_api_mactime_enable_disable_t_handler
159 (vl_api_mactime_enable_disable_t * mp)
161 vl_api_mactime_enable_disable_reply_t *rmp;
162 mactime_main_t *mm = &mactime_main;
165 VALIDATE_SW_IF_INDEX (mp);
167 rv = mactime_enable_disable (mm, ntohl (mp->sw_if_index),
168 (int) (mp->enable_disable));
169 BAD_SW_IF_INDEX_LABEL;
170 REPLY_MACRO (VL_API_MACTIME_ENABLE_DISABLE_REPLY);
174 vl_api_mactime_dump_t_handler (vl_api_mactime_dump_t * mp)
176 vl_api_mactime_details_t *ep;
177 vl_api_mactime_dump_reply_t *rmp;
178 mactime_device_t *dev;
179 mactime_main_t *mm = &mactime_main;
180 vl_api_registration_t *rp;
182 u32 his_table_epoch = clib_net_to_host_u32 (mp->my_table_epoch);
187 rp = vl_api_client_index_to_registration (mp->client_index);
191 if (his_table_epoch == mm->device_table_epoch)
193 rv = VNET_API_ERROR_NO_CHANGE;
198 pool_foreach (dev, mm->devices)
200 message_size = sizeof(*ep) + vec_len(dev->device_name) +
201 vec_len(dev->ranges) * sizeof(ep->ranges[0]);
203 ep = vl_msg_api_alloc (message_size);
204 memset (ep, 0, message_size);
205 ep->_vl_msg_id = clib_host_to_net_u16 (VL_API_MACTIME_DETAILS
207 ep->context = mp->context;
208 /* Index is the key for the stats segment combined counters */
209 ep->pool_index = clib_host_to_net_u32 (dev - mm->devices);
211 clib_memcpy_fast (ep->mac_address, dev->mac_address,
212 sizeof (ep->mac_address));
213 ep->data_quota = clib_host_to_net_u64 (dev->data_quota);
214 ep->data_used_in_range = clib_host_to_net_u64 (dev->data_used_in_range);
215 ep->flags = clib_host_to_net_u32 (dev->flags);
216 nranges = vec_len (dev->ranges);
217 ep->nranges = clib_host_to_net_u32 (nranges);
219 for (i = 0; i < vec_len (dev->ranges); i++)
221 ep->ranges[i].start = dev->ranges[i].start;
222 ep->ranges[i].end = dev->ranges[i].end;
225 name_len = vec_len (dev->device_name);
226 name_len = (name_len < ARRAY_LEN(ep->device_name)) ?
227 name_len : ARRAY_LEN(ep->device_name) - 1;
229 clib_memcpy_fast (ep->device_name, dev->device_name,
231 ep->device_name [ARRAY_LEN(ep->device_name) -1] = 0;
232 vl_api_send_msg (rp, (u8 *)ep);
238 REPLY_MACRO2 (VL_API_MACTIME_DUMP_REPLY,
240 rmp->table_epoch = clib_host_to_net_u32 (mm->device_table_epoch);
245 /** Create a lookup table entry for the indicated mac address
248 mactime_send_create_entry_message (u8 * mac_address)
250 mactime_main_t *mm = &mactime_main;
252 vl_shmem_hdr_t *shmem_hdr;
254 vl_api_mactime_add_del_range_t *mp;
256 am = vlibapi_get_main ();
257 shmem_hdr = am->shmem_hdr;
258 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
259 clib_memset (mp, 0, sizeof (*mp));
260 mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base);
261 name = format (0, "mac-%U", format_mac_address, mac_address);
263 memcpy (mp->device_name, name, vec_len (name));
264 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
265 /* $$$ config: create allow / drop / range */
268 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
271 /** Add or delete static / dynamic accept/drop configuration for a src mac
274 static void vl_api_mactime_add_del_range_t_handler
275 (vl_api_mactime_add_del_range_t * mp)
277 mactime_main_t *mm = &mactime_main;
278 vl_api_mactime_add_del_range_reply_t *rmp;
279 mactime_device_t *dp;
280 clib_bihash_kv_8_8_t kv;
282 clib_bihash_8_8_t *lut = &mm->lookup_table;
289 * Change the table epoch. Skip 0 so clients can code my_table_epoch = 0
290 * to receive a full dump.
292 mm->device_table_epoch++;
293 if (PREDICT_FALSE (mm->device_table_epoch == 0))
294 mm->device_table_epoch++;
296 data_quota = clib_net_to_host_u64 (mp->data_quota);
298 clib_memset (&kv, 0, sizeof (kv));
299 memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address));
301 /* See if we have a lookup table entry for this src mac address */
302 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
308 /* Create the device entry? */
311 pool_get (mm->devices, dp);
312 clib_memset (dp, 0, sizeof (*dp));
313 vlib_validate_combined_counter (&mm->allow_counters,
315 vlib_zero_combined_counter (&mm->allow_counters, dp - mm->devices);
316 vlib_validate_combined_counter (&mm->drop_counters,
318 vlib_zero_combined_counter (&mm->drop_counters, dp - mm->devices);
319 mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0;
320 dp->device_name = format (0, "%s%c", mp->device_name, 0);
321 memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address));
322 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
324 clib_timebase_range_t _r, *r = &_r;
325 r->start = mp->ranges[i].start;
326 r->end = mp->ranges[i].end;
327 vec_add1 (dp->ranges, r[0]);
329 /* If we found some time ranges */
332 /* Set allow/drop based on msg flags */
334 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
336 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
338 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
342 /* no ranges, it's a static allow/drop */
344 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
346 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
348 if (mp->no_udp_10001)
349 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
351 dp->data_quota = data_quota;
353 /* Add the hash table entry */
354 kv.value = dp - mm->devices;
355 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
357 else /* add more ranges, flags, etc. */
359 dp = pool_elt_at_index (mm->devices, kv.value);
361 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
363 clib_timebase_range_t _r, *r = &_r;
364 r->start = mp->ranges[i].start;
365 r->end = mp->ranges[i].end;
366 vec_add1 (dp->ranges, r[0]);
369 if (vec_len (dp->ranges))
371 /* Set allow/drop based on msg flags */
373 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
375 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
377 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
381 /* no ranges, it's a static allow/drop */
383 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
385 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
387 if (mp->no_udp_10001)
388 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
390 dp->data_quota = data_quota;
393 else /* delete case */
397 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
401 /* find the device entry */
402 dp = pool_elt_at_index (mm->devices, kv.value);
404 /* Remove it from the lookup table */
405 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
406 vec_free (dp->ranges);
407 pool_put (mm->devices, dp);
411 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
414 #include <mactime/mactime.api.c>
415 static clib_error_t *
416 mactime_init (vlib_main_t * vm)
418 mactime_main_t *mm = &mactime_main;
421 mm->vnet_main = vnet_get_main ();
423 /* Ask for a correctly-sized block of API message decode slots */
424 mm->msg_id_base = setup_message_id_table ();
426 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
427 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
428 mm->timezone_offset = -5; /* US EST / EDT */
433 VLIB_INIT_FUNCTION (mactime_init) =
435 .runs_after = VLIB_INITS("ip_neighbor_init"),
439 static clib_error_t *
440 mactime_config (vlib_main_t * vm, unformat_input_t * input)
442 mactime_main_t *mm = &mactime_main;
444 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
446 if (unformat (input, "lookup-table-buckets %u",
447 &mm->lookup_table_num_buckets))
449 else if (unformat (input, "lookup-table-memory %U",
450 unformat_memory_size, &mm->lookup_table_memory_size))
452 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
456 return clib_error_return (0, "unknown input '%U'",
457 format_unformat_error, input);
463 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
466 VNET_FEATURE_INIT (mactime, static) =
468 .arc_name = "device-input",
469 .node_name = "mactime",
470 .runs_before = VNET_FEATURES ("ethernet-input"),
475 VNET_FEATURE_INIT (mactime_tx, static) =
477 .arc_name = "interface-output",
478 .node_name = "mactime-tx",
479 .runs_before = VNET_FEATURES ("interface-tx"),
484 VLIB_PLUGIN_REGISTER () =
486 .version = VPP_BUILD_VER,
487 .description = "Time-based MAC Source Address Filter",
492 format_bytes_with_width (u8 * s, va_list * va)
494 uword nbytes = va_arg (*va, u64);
495 int width = va_arg (*va, int);
501 fmt = format (0, "%%%d.3f%%s%c", width, 0);
503 fmt = format (0, "%%.3f%%s%c", 0);
505 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
507 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
510 else if (nbytes > (1024ULL * 1024ULL))
512 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
515 else if (nbytes > 1024ULL)
517 nbytes_f64 = ((f64) nbytes) / (1024.0);
522 nbytes_f64 = (f64) nbytes;
526 s = format (s, (char *) fmt, nbytes_f64, suffix);
532 mactime_ip_neighbor_copy (index_t ipni, void *ctx)
534 mactime_main_t *mm = ctx;
536 vec_add1 (mm->arp_cache_copy, ipni);
538 return (WALK_CONTINUE);
541 static clib_error_t *
542 show_mactime_command_fn (vlib_main_t * vm,
543 unformat_input_t * input, vlib_cli_command_t * cmd)
545 mactime_main_t *mm = &mactime_main;
546 mactime_device_t *dp;
549 u32 *pool_indices = 0;
551 int current_status = 99;
554 vlib_counter_t allow, drop;
557 if (mm->feature_initialized == 0)
558 return clib_error_return
560 "Feature not initialized, suggest 'help mactime enable-disable'...");
562 vec_reset_length (mm->arp_cache_copy);
563 /* Walk all ip4 neighbours on all interfaces */
564 ip_neighbor_walk (AF_IP4, ~0, mactime_ip_neighbor_copy, mm);
566 now = clib_timebase_now (&mm->timebase);
568 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
569 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
571 if (unformat (input, "verbose %d", &verbose))
574 if (unformat (input, "verbose"))
578 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
581 pool_foreach (dp, mm->devices)
583 vec_add1 (pool_indices, dp - mm->devices);
587 vlib_cli_output (vm, "%-15s %18s %14s %10s %11s %13s",
588 "Device Name", "Addresses", "Status",
589 "AllowPkt", "AllowByte", "DropPkt");
591 for (i = 0; i < vec_len (pool_indices); i++)
593 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
595 /* Check dynamic ranges */
596 for (j = 0; j < vec_len (dp->ranges); j++)
598 clib_timebase_range_t *r = dp->ranges + j;
601 start0 = r->start + mm->sunday_midnight;
602 end0 = r->end + mm->sunday_midnight;
604 vlib_cli_output (vm, " Range %d: %U - %U", j,
605 format_clib_timebase_time, start0,
606 format_clib_timebase_time, end0);
608 if (now >= start0 && now <= end0)
610 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
612 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
618 vlib_cli_output (vm, " Time in range %d:", j);
619 vlib_cli_output (vm, " %U - %U",
620 format_clib_timebase_time, start0,
621 format_clib_timebase_time, end0);
627 vlib_cli_output (vm, " No range match.");
628 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
630 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
632 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
634 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
636 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
640 vec_reset_length (macstring);
641 macstring = format (0, "%U", format_mac_address, dp->mac_address);
642 switch (current_status)
645 status_string = "static drop";
648 status_string = "static allow";
651 status_string = "dynamic drop";
654 status_string = "dynamic allow";
657 status_string = "d-quota inact";
660 status_string = "d-quota activ";
663 status_string = "code bug!";
666 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
668 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
669 vlib_cli_output (vm, "%-15s %18s %14s %10lld %U %13lld",
670 dp->device_name, macstring, status_string,
671 allow.packets, format_bytes_with_width, allow.bytes,
673 if (dp->data_quota > 0)
674 vlib_cli_output (vm, "%-54s %s%U %s%U", " ", "Quota ",
675 format_bytes_with_width, dp->data_quota, 10,
676 "Use ", format_bytes_with_width,
677 dp->data_used_in_range, 8);
678 /* This is really only good for small N... */
679 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
681 ipn = ip_neighbor_get (mm->arp_cache_copy[j]);
683 (dp->mac_address, ipn->ipn_mac.bytes, sizeof (ipn->ipn_mac)))
685 vlib_cli_output (vm, "%17s%U", " ", format_ip46_address,
686 ip_neighbor_get_ip (ipn), IP46_TYPE_IP4);
690 vec_free (macstring);
691 vec_free (pool_indices);
697 VLIB_CLI_COMMAND (show_mactime_command, static) =
699 .path = "show mactime",
700 .short_help = "show mactime [verbose]",
701 .function = show_mactime_command_fn,
705 static clib_error_t *
706 clear_mactime_command_fn (vlib_main_t * vm,
707 unformat_input_t * input, vlib_cli_command_t * cmd)
709 mactime_main_t *mm = &mactime_main;
711 if (mm->feature_initialized == 0)
712 return clib_error_return
714 "Feature not initialized, suggest 'help mactime enable-disable'...");
716 vlib_clear_combined_counters (&mm->allow_counters);
717 vlib_clear_combined_counters (&mm->drop_counters);
718 vlib_cli_output (vm, "Mactime counters cleared...");
723 VLIB_CLI_COMMAND (clear_mactime_command, static) =
725 .path = "clear mactime",
726 .short_help = "clear mactime counters",
727 .function = clear_mactime_command_fn,
734 * fd.io coding-style-patch-verification: ON
737 * eval: (c-set-style "gnu")