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>
32 #define REPLY_MSG_ID_BASE mm->msg_id_base
33 #include <vlibapi/api_helper_macros.h>
35 #include <vnet/ip-neighbor/ip_neighbor.h>
37 mactime_main_t mactime_main;
40 * time-base src-mac filter device-input feature arc implementation
44 feature_init (mactime_main_t * mm)
46 if (mm->feature_initialized == 0)
48 /* Create the lookup table */
49 clib_bihash_init_8_8 (&mm->lookup_table, "mactime lookup table",
50 mm->lookup_table_num_buckets,
51 mm->lookup_table_memory_size);
52 clib_timebase_init (&mm->timebase, mm->timezone_offset,
53 CLIB_TIMEBASE_DAYLIGHT_USA,
54 &(mm->vlib_main->clib_time));
55 mm->allow_counters.name = "allow";
56 mm->allow_counters.stat_segment_name = "/mactime/allow";
57 mm->drop_counters.name = "drop";
58 mm->drop_counters.stat_segment_name = "/mactime/drop";
59 mm->feature_initialized = 1;
63 /** Action function shared between message handler and debug CLI
66 mactime_enable_disable (mactime_main_t * mm, u32 sw_if_index,
69 vnet_sw_interface_t *sw;
71 static u8 url_init_done;
76 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
78 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
80 /* Not a physical port? */
81 sw = vnet_get_sw_interface (mm->vnet_main, sw_if_index);
82 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
83 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
85 vnet_feature_enable_disable ("device-input", "mactime",
86 sw_if_index, enable_disable, 0, 0);
87 vnet_feature_enable_disable ("interface-output", "mactime-tx",
88 sw_if_index, enable_disable, 0, 0);
89 if (url_init_done == 0)
91 mactime_url_init (mm->vlib_main);
99 mactime_enable_disable_command_fn (vlib_main_t * vm,
100 unformat_input_t * input,
101 vlib_cli_command_t * cmd)
103 mactime_main_t *mm = &mactime_main;
104 u32 sw_if_index = ~0;
105 int enable_disable = 1;
109 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
111 if (unformat (input, "disable"))
113 else if (unformat (input, "%U", unformat_vnet_sw_interface,
114 mm->vnet_main, &sw_if_index))
116 else if (unformat (input, "sw_if_index %d", &sw_if_index))
122 if (sw_if_index == ~0)
123 return clib_error_return (0, "Please specify an interface...");
125 rv = mactime_enable_disable (mm, sw_if_index, enable_disable);
132 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
133 return clib_error_return
134 (0, "Invalid interface, only works on physical ports");
138 return clib_error_return (0, "mactime_enable_disable returned %d", rv);
144 VLIB_CLI_COMMAND (mactime_enable_disable_command, static) =
146 .path = "mactime enable-disable",
148 "mactime enable-disable <interface-name> [disable]",
149 .function = mactime_enable_disable_command_fn,
154 /** Enable / disable time-base src mac filtration on an interface
157 static void vl_api_mactime_enable_disable_t_handler
158 (vl_api_mactime_enable_disable_t * mp)
160 vl_api_mactime_enable_disable_reply_t *rmp;
161 mactime_main_t *mm = &mactime_main;
164 VALIDATE_SW_IF_INDEX (mp);
166 rv = mactime_enable_disable (mm, ntohl (mp->sw_if_index),
167 (int) (mp->enable_disable));
168 BAD_SW_IF_INDEX_LABEL;
169 REPLY_MACRO (VL_API_MACTIME_ENABLE_DISABLE_REPLY);
173 vl_api_mactime_dump_t_handler (vl_api_mactime_dump_t * mp)
175 vl_api_mactime_details_t *ep;
176 vl_api_mactime_dump_reply_t *rmp;
177 mactime_device_t *dev;
178 mactime_main_t *mm = &mactime_main;
179 vl_api_registration_t *rp;
181 u32 his_table_epoch = clib_net_to_host_u32 (mp->my_table_epoch);
186 rp = vl_api_client_index_to_registration (mp->client_index);
190 if (his_table_epoch == mm->device_table_epoch)
192 rv = VNET_API_ERROR_NO_CHANGE;
197 pool_foreach (dev, mm->devices)
199 message_size = sizeof(*ep) + vec_len(dev->device_name) +
200 vec_len(dev->ranges) * sizeof(ep->ranges[0]);
202 ep = vl_msg_api_alloc (message_size);
203 memset (ep, 0, message_size);
204 ep->_vl_msg_id = clib_host_to_net_u16 (VL_API_MACTIME_DETAILS
206 ep->context = mp->context;
207 /* Index is the key for the stats segment combined counters */
208 ep->pool_index = clib_host_to_net_u32 (dev - mm->devices);
210 clib_memcpy_fast (ep->mac_address, dev->mac_address,
211 sizeof (ep->mac_address));
212 ep->data_quota = clib_host_to_net_u64 (dev->data_quota);
213 ep->data_used_in_range = clib_host_to_net_u64 (dev->data_used_in_range);
214 ep->flags = clib_host_to_net_u32 (dev->flags);
215 nranges = vec_len (dev->ranges);
216 ep->nranges = clib_host_to_net_u32 (nranges);
218 for (i = 0; i < vec_len (dev->ranges); i++)
220 ep->ranges[i].start = dev->ranges[i].start;
221 ep->ranges[i].end = dev->ranges[i].end;
224 name_len = vec_len (dev->device_name);
225 name_len = (name_len < ARRAY_LEN(ep->device_name)) ?
226 name_len : ARRAY_LEN(ep->device_name) - 1;
228 clib_memcpy_fast (ep->device_name, dev->device_name,
230 ep->device_name [ARRAY_LEN(ep->device_name) -1] = 0;
231 vl_api_send_msg (rp, (u8 *)ep);
237 REPLY_MACRO2 (VL_API_MACTIME_DUMP_REPLY,
239 rmp->table_epoch = clib_host_to_net_u32 (mm->device_table_epoch);
244 /** Create a lookup table entry for the indicated mac address
247 mactime_send_create_entry_message (u8 * mac_address)
249 mactime_main_t *mm = &mactime_main;
251 vl_shmem_hdr_t *shmem_hdr;
253 vl_api_mactime_add_del_range_t *mp;
255 am = vlibapi_get_main ();
256 shmem_hdr = am->shmem_hdr;
257 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
258 clib_memset (mp, 0, sizeof (*mp));
259 mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base);
260 name = format (0, "mac-%U", format_mac_address, mac_address);
262 memcpy (mp->device_name, name, vec_len (name));
263 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
264 /* $$$ config: create allow / drop / range */
267 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
270 /** Add or delete static / dynamic accept/drop configuration for a src mac
273 static void vl_api_mactime_add_del_range_t_handler
274 (vl_api_mactime_add_del_range_t * mp)
276 mactime_main_t *mm = &mactime_main;
277 vl_api_mactime_add_del_range_reply_t *rmp;
278 mactime_device_t *dp;
279 clib_bihash_kv_8_8_t kv;
281 clib_bihash_8_8_t *lut = &mm->lookup_table;
288 * Change the table epoch. Skip 0 so clients can code my_table_epoch = 0
289 * to receive a full dump.
291 mm->device_table_epoch++;
292 if (PREDICT_FALSE (mm->device_table_epoch == 0))
293 mm->device_table_epoch++;
295 data_quota = clib_net_to_host_u64 (mp->data_quota);
297 clib_memset (&kv, 0, sizeof (kv));
298 memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address));
300 /* See if we have a lookup table entry for this src mac address */
301 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
307 /* Create the device entry? */
310 pool_get (mm->devices, dp);
311 clib_memset (dp, 0, sizeof (*dp));
312 vlib_validate_combined_counter (&mm->allow_counters,
314 vlib_zero_combined_counter (&mm->allow_counters, dp - mm->devices);
315 vlib_validate_combined_counter (&mm->drop_counters,
317 vlib_zero_combined_counter (&mm->drop_counters, dp - mm->devices);
318 mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0;
319 dp->device_name = format (0, "%s%c", mp->device_name, 0);
320 memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address));
321 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
323 clib_timebase_range_t _r, *r = &_r;
324 r->start = mp->ranges[i].start;
325 r->end = mp->ranges[i].end;
326 vec_add1 (dp->ranges, r[0]);
328 /* If we found some time ranges */
331 /* Set allow/drop based on msg flags */
333 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
335 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
337 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
341 /* no ranges, it's a static allow/drop */
343 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
345 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
347 if (mp->no_udp_10001)
348 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
350 dp->data_quota = data_quota;
352 /* Add the hash table entry */
353 kv.value = dp - mm->devices;
354 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
356 else /* add more ranges, flags, etc. */
358 dp = pool_elt_at_index (mm->devices, kv.value);
360 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
362 clib_timebase_range_t _r, *r = &_r;
363 r->start = mp->ranges[i].start;
364 r->end = mp->ranges[i].end;
365 vec_add1 (dp->ranges, r[0]);
368 if (vec_len (dp->ranges))
370 /* Set allow/drop based on msg flags */
372 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
374 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
376 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
380 /* no ranges, it's a static allow/drop */
382 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
384 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
386 if (mp->no_udp_10001)
387 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
389 dp->data_quota = data_quota;
392 else /* delete case */
396 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
400 /* find the device entry */
401 dp = pool_elt_at_index (mm->devices, kv.value);
403 /* Remove it from the lookup table */
404 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
405 vec_free (dp->ranges);
406 pool_put (mm->devices, dp);
410 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
413 #include <mactime/mactime.api.c>
414 static clib_error_t *
415 mactime_init (vlib_main_t * vm)
417 mactime_main_t *mm = &mactime_main;
420 mm->vnet_main = vnet_get_main ();
422 /* Ask for a correctly-sized block of API message decode slots */
423 mm->msg_id_base = setup_message_id_table ();
425 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
426 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
427 mm->timezone_offset = -5; /* US EST / EDT */
432 VLIB_INIT_FUNCTION (mactime_init) =
434 .runs_after = VLIB_INITS("ip_neighbor_init"),
438 static clib_error_t *
439 mactime_config (vlib_main_t * vm, unformat_input_t * input)
441 mactime_main_t *mm = &mactime_main;
443 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
445 if (unformat (input, "lookup-table-buckets %u",
446 &mm->lookup_table_num_buckets))
448 else if (unformat (input, "lookup-table-memory %U",
449 unformat_memory_size, &mm->lookup_table_memory_size))
451 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
455 return clib_error_return (0, "unknown input '%U'",
456 format_unformat_error, input);
462 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
465 VNET_FEATURE_INIT (mactime, static) =
467 .arc_name = "device-input",
468 .node_name = "mactime",
469 .runs_before = VNET_FEATURES ("ethernet-input"),
474 VNET_FEATURE_INIT (mactime_tx, static) = {
475 .arc_name = "interface-output",
476 .node_name = "mactime-tx",
477 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
482 VLIB_PLUGIN_REGISTER () =
484 .version = VPP_BUILD_VER,
485 .description = "Time-based MAC Source Address Filter",
490 format_bytes_with_width (u8 * s, va_list * va)
492 uword nbytes = va_arg (*va, u64);
493 int width = va_arg (*va, int);
499 fmt = format (0, "%%%d.3f%%s%c", width, 0);
501 fmt = format (0, "%%.3f%%s%c", 0);
503 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
505 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
508 else if (nbytes > (1024ULL * 1024ULL))
510 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
513 else if (nbytes > 1024ULL)
515 nbytes_f64 = ((f64) nbytes) / (1024.0);
520 nbytes_f64 = (f64) nbytes;
524 s = format (s, (char *) fmt, nbytes_f64, suffix);
530 mactime_ip_neighbor_copy (index_t ipni, void *ctx)
532 mactime_main_t *mm = ctx;
534 vec_add1 (mm->arp_cache_copy, ipni);
536 return (WALK_CONTINUE);
539 static clib_error_t *
540 show_mactime_command_fn (vlib_main_t * vm,
541 unformat_input_t * input, vlib_cli_command_t * cmd)
543 mactime_main_t *mm = &mactime_main;
544 mactime_device_t *dp;
547 u32 *pool_indices = 0;
549 int current_status = 99;
552 vlib_counter_t allow, drop;
555 if (mm->feature_initialized == 0)
556 return clib_error_return
558 "Feature not initialized, suggest 'help mactime enable-disable'...");
560 vec_reset_length (mm->arp_cache_copy);
561 /* Walk all ip4 neighbours on all interfaces */
562 ip_neighbor_walk (AF_IP4, ~0, mactime_ip_neighbor_copy, mm);
564 now = clib_timebase_now (&mm->timebase);
566 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
567 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
569 if (unformat (input, "verbose %d", &verbose))
572 if (unformat (input, "verbose"))
576 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
579 pool_foreach (dp, mm->devices)
581 vec_add1 (pool_indices, dp - mm->devices);
585 vlib_cli_output (vm, "%-15s %18s %14s %10s %11s %13s",
586 "Device Name", "Addresses", "Status",
587 "AllowPkt", "AllowByte", "DropPkt");
589 for (i = 0; i < vec_len (pool_indices); i++)
591 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
593 /* Check dynamic ranges */
594 for (j = 0; j < vec_len (dp->ranges); j++)
596 clib_timebase_range_t *r = dp->ranges + j;
599 start0 = r->start + mm->sunday_midnight;
600 end0 = r->end + mm->sunday_midnight;
602 vlib_cli_output (vm, " Range %d: %U - %U", j,
603 format_clib_timebase_time, start0,
604 format_clib_timebase_time, end0);
606 if (now >= start0 && now <= end0)
608 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
610 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
616 vlib_cli_output (vm, " Time in range %d:", j);
617 vlib_cli_output (vm, " %U - %U",
618 format_clib_timebase_time, start0,
619 format_clib_timebase_time, end0);
625 vlib_cli_output (vm, " No range match.");
626 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
628 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
630 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
632 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
634 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
638 vec_reset_length (macstring);
640 format (macstring, "%U", format_mac_address, dp->mac_address);
641 switch (current_status)
644 status_string = "static drop";
647 status_string = "static allow";
650 status_string = "dynamic drop";
653 status_string = "dynamic allow";
656 status_string = "d-quota inact";
659 status_string = "d-quota activ";
662 status_string = "code bug!";
665 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
667 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
668 vlib_cli_output (vm, "%-15s %18s %14s %10lld %U %13lld",
669 dp->device_name, macstring, status_string,
670 allow.packets, format_bytes_with_width, allow.bytes,
672 if (dp->data_quota > 0)
673 vlib_cli_output (vm, "%-54s %s%U %s%U", " ", "Quota ",
674 format_bytes_with_width, dp->data_quota, 10,
675 "Use ", format_bytes_with_width,
676 dp->data_used_in_range, 8);
677 /* This is really only good for small N... */
678 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
680 ipn = ip_neighbor_get (mm->arp_cache_copy[j]);
682 (dp->mac_address, ipn->ipn_mac.bytes, sizeof (ipn->ipn_mac)))
684 vlib_cli_output (vm, "%17s%U", " ", format_ip46_address,
685 ip_neighbor_get_ip (ipn), IP46_TYPE_IP4);
689 vec_free (macstring);
690 vec_free (pool_indices);
696 VLIB_CLI_COMMAND (show_mactime_command, static) =
698 .path = "show mactime",
699 .short_help = "show mactime [verbose]",
700 .function = show_mactime_command_fn,
704 static clib_error_t *
705 clear_mactime_command_fn (vlib_main_t * vm,
706 unformat_input_t * input, vlib_cli_command_t * cmd)
708 mactime_main_t *mm = &mactime_main;
710 if (mm->feature_initialized == 0)
711 return clib_error_return
713 "Feature not initialized, suggest 'help mactime enable-disable'...");
715 vlib_clear_combined_counters (&mm->allow_counters);
716 vlib_clear_combined_counters (&mm->drop_counters);
717 vlib_cli_output (vm, "Mactime counters cleared...");
722 VLIB_CLI_COMMAND (clear_mactime_command, static) =
724 .path = "clear mactime",
725 .short_help = "clear mactime counters",
726 .function = clear_mactime_command_fn,
733 * fd.io coding-style-patch-verification: ON
736 * eval: (c-set-style "gnu")