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);
143 VLIB_CLI_COMMAND (mactime_enable_disable_command, static) =
145 .path = "mactime enable-disable",
147 "mactime enable-disable <interface-name> [disable]",
148 .function = mactime_enable_disable_command_fn,
152 /** Enable / disable time-base src mac filtration on an interface
155 static void vl_api_mactime_enable_disable_t_handler
156 (vl_api_mactime_enable_disable_t * mp)
158 vl_api_mactime_enable_disable_reply_t *rmp;
159 mactime_main_t *mm = &mactime_main;
162 VALIDATE_SW_IF_INDEX (mp);
164 rv = mactime_enable_disable (mm, ntohl (mp->sw_if_index),
165 (int) (mp->enable_disable));
166 BAD_SW_IF_INDEX_LABEL;
167 REPLY_MACRO (VL_API_MACTIME_ENABLE_DISABLE_REPLY);
171 vl_api_mactime_dump_t_handler (vl_api_mactime_dump_t * mp)
173 vl_api_mactime_details_t *ep;
174 vl_api_mactime_dump_reply_t *rmp;
175 mactime_device_t *dev;
176 mactime_main_t *mm = &mactime_main;
177 vl_api_registration_t *rp;
179 u32 his_table_epoch = clib_net_to_host_u32 (mp->my_table_epoch);
184 rp = vl_api_client_index_to_registration (mp->client_index);
188 if (his_table_epoch == mm->device_table_epoch)
190 rv = VNET_API_ERROR_NO_CHANGE;
194 pool_foreach (dev, mm->devices)
196 message_size = sizeof(*ep) + vec_len(dev->device_name) +
197 vec_len(dev->ranges) * sizeof(ep->ranges[0]);
199 ep = vl_msg_api_alloc (message_size);
200 memset (ep, 0, message_size);
201 ep->_vl_msg_id = clib_host_to_net_u16 (VL_API_MACTIME_DETAILS
203 ep->context = mp->context;
204 /* Index is the key for the stats segment combined counters */
205 ep->pool_index = clib_host_to_net_u32 (dev - mm->devices);
207 clib_memcpy_fast (ep->mac_address, dev->mac_address,
208 sizeof (ep->mac_address));
209 ep->data_quota = clib_host_to_net_u64 (dev->data_quota);
210 ep->data_used_in_range = clib_host_to_net_u64 (dev->data_used_in_range);
211 ep->flags = clib_host_to_net_u32 (dev->flags);
212 nranges = vec_len (dev->ranges);
213 ep->nranges = clib_host_to_net_u32 (nranges);
215 for (i = 0; i < vec_len (dev->ranges); i++)
217 ep->ranges[i].start = dev->ranges[i].start;
218 ep->ranges[i].end = dev->ranges[i].end;
221 name_len = vec_len (dev->device_name);
222 name_len = (name_len < ARRAY_LEN(ep->device_name)) ?
223 name_len : ARRAY_LEN(ep->device_name) - 1;
225 clib_memcpy_fast (ep->device_name, dev->device_name,
227 ep->device_name [ARRAY_LEN(ep->device_name) -1] = 0;
228 vl_api_send_msg (rp, (u8 *)ep);
232 REPLY_MACRO2 (VL_API_MACTIME_DUMP_REPLY,
234 rmp->table_epoch = clib_host_to_net_u32 (mm->device_table_epoch);
238 /** Create a lookup table entry for the indicated mac address
241 mactime_send_create_entry_message (u8 * mac_address)
243 mactime_main_t *mm = &mactime_main;
245 vl_shmem_hdr_t *shmem_hdr;
247 vl_api_mactime_add_del_range_t *mp;
249 am = vlibapi_get_main ();
250 shmem_hdr = am->shmem_hdr;
251 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
252 clib_memset (mp, 0, sizeof (*mp));
253 mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base);
254 name = format (0, "mac-%U", format_mac_address, mac_address);
256 memcpy (mp->device_name, name, vec_len (name));
257 memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
258 /* $$$ config: create allow / drop / range */
261 vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp);
264 /** Add or delete static / dynamic accept/drop configuration for a src mac
267 static void vl_api_mactime_add_del_range_t_handler
268 (vl_api_mactime_add_del_range_t * mp)
270 mactime_main_t *mm = &mactime_main;
271 vl_api_mactime_add_del_range_reply_t *rmp;
272 mactime_device_t *dp;
273 clib_bihash_kv_8_8_t kv;
275 clib_bihash_8_8_t *lut = &mm->lookup_table;
282 * Change the table epoch. Skip 0 so clients can code my_table_epoch = 0
283 * to receive a full dump.
285 mm->device_table_epoch++;
286 if (PREDICT_FALSE (mm->device_table_epoch == 0))
287 mm->device_table_epoch++;
289 data_quota = clib_net_to_host_u64 (mp->data_quota);
291 clib_memset (&kv, 0, sizeof (kv));
292 memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address));
294 /* See if we have a lookup table entry for this src mac address */
295 if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0)
301 /* Create the device entry? */
304 pool_get (mm->devices, dp);
305 clib_memset (dp, 0, sizeof (*dp));
306 vlib_validate_combined_counter (&mm->allow_counters,
308 vlib_zero_combined_counter (&mm->allow_counters, dp - mm->devices);
309 vlib_validate_combined_counter (&mm->drop_counters,
311 vlib_zero_combined_counter (&mm->drop_counters, dp - mm->devices);
312 mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0;
313 dp->device_name = format (0, "%s%c", mp->device_name, 0);
314 memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address));
315 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
317 clib_timebase_range_t _r, *r = &_r;
318 r->start = mp->ranges[i].start;
319 r->end = mp->ranges[i].end;
320 vec_add1 (dp->ranges, r[0]);
322 /* If we found some time ranges */
325 /* Set allow/drop based on msg flags */
327 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
329 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
331 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
335 /* no ranges, it's a static allow/drop */
337 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
339 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
341 if (mp->no_udp_10001)
342 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
344 dp->data_quota = data_quota;
346 /* Add the hash table entry */
347 kv.value = dp - mm->devices;
348 clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ );
350 else /* add more ranges, flags, etc. */
352 dp = pool_elt_at_index (mm->devices, kv.value);
354 for (i = 0; i < clib_net_to_host_u32 (mp->count); i++)
356 clib_timebase_range_t _r, *r = &_r;
357 r->start = mp->ranges[i].start;
358 r->end = mp->ranges[i].end;
359 vec_add1 (dp->ranges, r[0]);
362 if (vec_len (dp->ranges))
364 /* Set allow/drop based on msg flags */
366 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP;
368 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW;
370 dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA;
374 /* no ranges, it's a static allow/drop */
376 dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP;
378 dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW;
380 if (mp->no_udp_10001)
381 dp->flags |= MACTIME_DEVICE_FLAG_DROP_UDP_10001;
383 dp->data_quota = data_quota;
386 else /* delete case */
390 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
394 /* find the device entry */
395 dp = pool_elt_at_index (mm->devices, kv.value);
397 /* Remove it from the lookup table */
398 clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ );
399 vec_free (dp->ranges);
400 pool_put (mm->devices, dp);
404 REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY);
407 #include <mactime/mactime.api.c>
408 static clib_error_t *
409 mactime_init (vlib_main_t * vm)
411 mactime_main_t *mm = &mactime_main;
414 mm->vnet_main = vnet_get_main ();
416 /* Ask for a correctly-sized block of API message decode slots */
417 mm->msg_id_base = setup_message_id_table ();
419 mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS;
420 mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE;
421 mm->timezone_offset = -5; /* US EST / EDT */
425 VLIB_INIT_FUNCTION (mactime_init) =
427 .runs_after = VLIB_INITS("ip_neighbor_init"),
430 static clib_error_t *
431 mactime_config (vlib_main_t * vm, unformat_input_t * input)
433 mactime_main_t *mm = &mactime_main;
435 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
437 if (unformat (input, "lookup-table-buckets %u",
438 &mm->lookup_table_num_buckets))
440 else if (unformat (input, "lookup-table-memory %U",
441 unformat_memory_size, &mm->lookup_table_memory_size))
443 else if (unformat (input, "timezone_offset %d", &mm->timezone_offset))
447 return clib_error_return (0, "unknown input '%U'",
448 format_unformat_error, input);
454 VLIB_CONFIG_FUNCTION (mactime_config, "mactime");
456 VNET_FEATURE_INIT (mactime, static) =
458 .arc_name = "device-input",
459 .node_name = "mactime",
460 .runs_before = VNET_FEATURES ("ethernet-input"),
463 VNET_FEATURE_INIT (mactime_tx, static) = {
464 .arc_name = "interface-output",
465 .node_name = "mactime-tx",
466 .runs_before = VNET_FEATURES ("interface-output-arc-end"),
469 VLIB_PLUGIN_REGISTER () =
471 .version = VPP_BUILD_VER,
472 .description = "Time-based MAC Source Address Filter",
476 format_bytes_with_width (u8 * s, va_list * va)
478 uword nbytes = va_arg (*va, u64);
479 int width = va_arg (*va, int);
485 fmt = format (0, "%%%d.3f%%s%c", width, 0);
487 fmt = format (0, "%%.3f%%s%c", 0);
489 if (nbytes > (1024ULL * 1024ULL * 1024ULL))
491 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0 * 1024.0);
494 else if (nbytes > (1024ULL * 1024ULL))
496 nbytes_f64 = ((f64) nbytes) / (1024.0 * 1024.0);
499 else if (nbytes > 1024ULL)
501 nbytes_f64 = ((f64) nbytes) / (1024.0);
506 nbytes_f64 = (f64) nbytes;
510 s = format (s, (char *) fmt, nbytes_f64, suffix);
516 mactime_ip_neighbor_copy (index_t ipni, void *ctx)
518 mactime_main_t *mm = ctx;
520 vec_add1 (mm->arp_cache_copy, ipni);
522 return (WALK_CONTINUE);
525 static clib_error_t *
526 show_mactime_command_fn (vlib_main_t * vm,
527 unformat_input_t * input, vlib_cli_command_t * cmd)
529 mactime_main_t *mm = &mactime_main;
530 mactime_device_t *dp;
533 u32 *pool_indices = 0;
535 int current_status = 99;
538 vlib_counter_t allow, drop;
541 if (mm->feature_initialized == 0)
542 return clib_error_return
544 "Feature not initialized, suggest 'help mactime enable-disable'...");
546 vec_reset_length (mm->arp_cache_copy);
547 /* Walk all ip4 neighbours on all interfaces */
548 ip_neighbor_walk (AF_IP4, ~0, mactime_ip_neighbor_copy, mm);
550 now = clib_timebase_now (&mm->timebase);
552 if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0))
553 mm->sunday_midnight = clib_timebase_find_sunday_midnight (now);
555 if (unformat (input, "verbose %d", &verbose))
558 if (unformat (input, "verbose"))
562 vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now);
564 pool_foreach (dp, mm->devices)
566 vec_add1 (pool_indices, dp - mm->devices);
569 vlib_cli_output (vm, "%-15s %18s %14s %10s %11s %13s",
570 "Device Name", "Addresses", "Status",
571 "AllowPkt", "AllowByte", "DropPkt");
573 for (i = 0; i < vec_len (pool_indices); i++)
575 dp = pool_elt_at_index (mm->devices, pool_indices[i]);
577 /* Check dynamic ranges */
578 for (j = 0; j < vec_len (dp->ranges); j++)
580 clib_timebase_range_t *r = dp->ranges + j;
583 start0 = r->start + mm->sunday_midnight;
584 end0 = r->end + mm->sunday_midnight;
586 vlib_cli_output (vm, " Range %d: %U - %U", j,
587 format_clib_timebase_time, start0,
588 format_clib_timebase_time, end0);
590 if (now >= start0 && now <= end0)
592 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
594 else if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
600 vlib_cli_output (vm, " Time in range %d:", j);
601 vlib_cli_output (vm, " %U - %U",
602 format_clib_timebase_time, start0,
603 format_clib_timebase_time, end0);
609 vlib_cli_output (vm, " No range match.");
610 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP)
612 if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW)
614 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW)
616 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP)
618 if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW_QUOTA)
622 vec_reset_length (macstring);
624 format (macstring, "%U", format_mac_address, dp->mac_address);
625 switch (current_status)
628 status_string = "static drop";
631 status_string = "static allow";
634 status_string = "dynamic drop";
637 status_string = "dynamic allow";
640 status_string = "d-quota inact";
643 status_string = "d-quota activ";
646 status_string = "code bug!";
649 vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices,
651 vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop);
652 vlib_cli_output (vm, "%-15s %18s %14s %10lld %U %13lld",
653 dp->device_name, macstring, status_string,
654 allow.packets, format_bytes_with_width, allow.bytes,
656 if (dp->data_quota > 0)
657 vlib_cli_output (vm, "%-54s %s%U %s%U", " ", "Quota ",
658 format_bytes_with_width, dp->data_quota, 10,
659 "Use ", format_bytes_with_width,
660 dp->data_used_in_range, 8);
661 /* This is really only good for small N... */
662 for (j = 0; j < vec_len (mm->arp_cache_copy); j++)
664 ipn = ip_neighbor_get (mm->arp_cache_copy[j]);
666 (dp->mac_address, ipn->ipn_mac.bytes, sizeof (ipn->ipn_mac)))
668 vlib_cli_output (vm, "%17s%U", " ", format_ip46_address,
669 ip_neighbor_get_ip (ipn), IP46_TYPE_IP4);
673 vec_free (macstring);
674 vec_free (pool_indices);
679 VLIB_CLI_COMMAND (show_mactime_command, static) =
681 .path = "show mactime",
682 .short_help = "show mactime [verbose]",
683 .function = show_mactime_command_fn,
686 static clib_error_t *
687 clear_mactime_command_fn (vlib_main_t * vm,
688 unformat_input_t * input, vlib_cli_command_t * cmd)
690 mactime_main_t *mm = &mactime_main;
692 if (mm->feature_initialized == 0)
693 return clib_error_return
695 "Feature not initialized, suggest 'help mactime enable-disable'...");
697 vlib_clear_combined_counters (&mm->allow_counters);
698 vlib_clear_combined_counters (&mm->drop_counters);
699 vlib_cli_output (vm, "Mactime counters cleared...");
703 VLIB_CLI_COMMAND (clear_mactime_command, static) =
705 .path = "clear mactime",
706 .short_help = "clear mactime counters",
707 .function = clear_mactime_command_fn,
713 * fd.io coding-style-patch-verification: ON
716 * eval: (c-set-style "gnu")