net: added module for filtering packets based on l2 addresses#7066
net: added module for filtering packets based on l2 addresses#7066miri64 merged 6 commits intoRIOT-OS:masterfrom
Conversation
sys/include/net/netopt.h
Outdated
| NETOPT_NUMOF, | ||
|
|
||
| /** | ||
| * @brief add/remove an address to the link layer filter list |
There was a problem hiding this comment.
What are the required types? How are removal and addition distinguished?
There was a problem hiding this comment.
- "remove" should be removed here as it needs an other netopt parameter. Addition and removal are not distinguished here
- expects a pointer to
l2filter_t structongetor to a hardware address (usuallyuint8_t) onset
| } | ||
| } | ||
|
|
||
| int l2filter_add(l2filter_t *list, const void *addr, size_t addr_len) |
There was a problem hiding this comment.
This function does not check for duplicate entries. Comparing the IPv6 blacklist module they used a bitfield to detect existing entries, for performance reasons. I can add check later, if you don't have time. @haukepetersen, @miri64 what's you opinion. Should we rather
- not check for duplicates
- iterate through the list (via '
match()function) - add a bitfield as done in IPv6 blacklist
IMO we should give 2. a try.
There was a problem hiding this comment.
TBH I don't have a strong opinion on either option. Since this is a user operation 1. is the most efficient if documented well but it would be different to the IPv6 counterpart indeed. Also I'm not sure which implications this would have for removal.
There was a problem hiding this comment.
I decided for (1) intentionally. IMHO this feature is basically made for testing and experiments, where in both cases one has pretty good control and knowledge what one is doing. So there is IMHO no need to catch duplicate entries...
|
I can confirm both blacklist and whitelist working on native and some boards. IMO this is a very useful tool which I will need in very near future. However, I'm not 100% happy with introducing two new |
|
Please have a look at #38 |
sys/include/net/netopt.h
Outdated
| NETOPT_NUMOF, | ||
|
|
||
| /** | ||
| * @brief add/remove an address to/from the link layer filter list |
There was a problem hiding this comment.
Isn't NETOPT_L2FILTER_RM for remove now?
sys/include/net/netopt.h
Outdated
| * blacklisting and whitelisting. | ||
| */ | ||
| NETOPT_L2FILTER, | ||
| NETOPT_L2FILTER_RM, /**< get will always return -ENOTSUP */ |
There was a problem hiding this comment.
Please add some type doc for this netopt, too.
I'm not happy either, but for list operations this seems to be the only way at the momement. |
smlng
left a comment
There was a problem hiding this comment.
besides other comments, some suggestions to improve documentation
| * @param[in] list pointer to the filter list | ||
| * @param[in] addr address to be added to list | ||
| * @param[in] addr_len size of @p addr [in byte] | ||
| * |
There was a problem hiding this comment.
missing @pres stating the assert conditions (list and addr != NULL, and addr_len < MAX_ADDRLEN.
| * @param[in] list pointer to the filter list | ||
| * @param[in] addr address to remove from the list | ||
| * @param[in] addr_len length of @p addr [in byte] | ||
| * |
| * @param[in] list list with black-/whitelisted addresses | ||
| * @param[in] addr address to check against the entries in @p list | ||
| * @param[in] addr_len length of @p addr [in byte] | ||
| * |
|
|
||
| /** | ||
| * @brief Number of slots in each filter list (filter entries per device) | ||
| */ |
There was a problem hiding this comment.
inconsistent documentation here outside of #ifndef, above its within - don't [know] whats (more) correct?
There was a problem hiding this comment.
I prefer above, because then it is consistent with constructs like this
/**
* @brief Doc-brief
* @note 1 if @ref sys_example is present, 2 if not.
*/
#ifdef MODULE_EXAMPLE
#define EXAMPLE_VALUE (1)
#else
#define EXAMPLE_VALUE (2)
#endifThere was a problem hiding this comment.
(and yes there is a use-case for this construct. E.g. gnrc_netif uses it to calculate the number of required addresses).
sys/include/net/l2filter.h
Outdated
| /** | ||
| * @brief Add an entry to a devices filter list | ||
| * | ||
| * @param[in] list pointer to the filter list |
There was a problem hiding this comment.
this is actually [in|out], isn't it?
There was a problem hiding this comment.
It depends on the point of view but IMO its rather an in because you give in a pointer to the list where you want to add the entry.
There was a problem hiding this comment.
if it is an in than this could beconst, but I doubt you can add an element then ...
There was a problem hiding this comment.
I am with @smlng here. The element that is pointed to by list (aka list[0]) will be modified (probably at the first addition)
There was a problem hiding this comment.
yapp, but its [in,out] :-)
sys/include/net/l2filter.h
Outdated
| /** | ||
| * @brief Remove an entry from the given filter list | ||
| * | ||
| * @param[in] list pointer to the filter list |
There was a problem hiding this comment.
same as above: its [in|out] param
sys/include/net/netopt.h
Outdated
| @@ -248,6 +248,16 @@ typedef enum { | |||
| * @note Interfaces are not meant to respond to that. | |||
| */ | |||
| NETOPT_NUMOF, | |||
There was a problem hiding this comment.
Uhm.... better put this below the newly defined netopts ;-)
sys/include/net/netopt.h
Outdated
| * blacklisting and whitelisting. | ||
| */ | ||
| NETOPT_L2FILTER, | ||
| NETOPT_L2FILTER_RM, /**< get will always return -ENOTSUP */ |
There was a problem hiding this comment.
Btw, I think you need to extend _netopt_strmap, too.
|
@miri64, @smlng I addressed your comments and PRed against @haukepetersen's branch #39. If it's just about the doc it would be so great if you approve so we can merge soon |
| if (list[i].addr_len == 0) { | ||
| list[i].addr_len = addr_len; | ||
| memcpy(list[i].addr, addr, addr_len); | ||
| res = 0; |
There was a problem hiding this comment.
I would rather break here instead of testing res in the for loop header (all following for loops, too)
There was a problem hiding this comment.
makes probably sense. Some old MISRA rules did not allow to use break (changed in newer MISRA rules, though), so I tend to be stuck not to use break when possible... But times change and I will try to adapt :-)
|
Tried to address all comments (@PeterKietzmann thanks for the help!)
|
| #include <sys/uio.h> | ||
|
|
||
| #include "net/netstats.h" | ||
| #include "net/l2filter.h" |
There was a problem hiding this comment.
IMHO no need for this -> netstats does also not do this...
There was a problem hiding this comment.
I'm also against #ifdef for header includes (unless 100% needed). Just bloats the top part of the code with more lines and the compiler ignores non-implemented header definitions anyway.
There was a problem hiding this comment.
yeah should be fine as long there is no global or external variable definition that could result in undefined reference or unused variable error when the module is not used.
There was a problem hiding this comment.
hm, thinking about @smlng's comment: including header files, where the implementation is known to be not present, may still lead to name clashes if those functions / variables are too generic in their names and in the worst case to undefined behavior (I assume there is no big #ifdef in the header file itself). IMO it's also better to take off some work for the preprocessor / compiler by hiding those includes, iff its known that they are not needed. But as @miri64 states, it could get ugly and bloated very quickly.
There was a problem hiding this comment.
If you are not happy with that, please provide a follow-up for these cases and let's discuss it there.
This is not how we should review our PRs. Rather than trying to get them in quickly, but sloppy, and rely on follow-up PRs, we should aim for our best in the first iteration.
Does it hurt so much to not copy a previously applied style that might be flawed? It's just two lines more for less problems .. Since when are unused include files not considered as bad coding? (;
There was a problem hiding this comment.
If a clash occurs with a header from our codebase the header is flawed IMHO, not the consumer of that header (in the same codebase). I would rather take a look into the header and assure the clash does not happen there (e.g. with proper prefixing according to our naming conventions), and not in the consumers of that header. This is how you build a proper API: not putting the need to remember exceptions on the shoulders of the "user". Mind you, that is not true for external headers, but there you still have can name clashes in the linker (see emb6's clist e.g.) but those need to be fixed by other means anyway.
There was a problem hiding this comment.
This is not sloppiness, this is moving tangential discussions (that stall the PR's review) to a separate thread BTW ;-).
There was a problem hiding this comment.
If a clash occurs with a header from our codebase the header is flawed IMHO, not the consumer of that header (in the same codebase). I would rather take a look into the header and assure the clash does not happen there (e.g. with proper prefixing according to our naming conventions), and not in the consumers of that header.
Prefixing according to our coding conventions surely helps, but does not resolve the problem of clashing with external packages. You yourself gave a good example for what I am talking about.
Nevertheless, the question is not how to design a good API, but whether a header file should be included that is definitly not used. There are no arguments against ifdefing unused header files, besides the bloating. But, IMO, bloating is a good price to pay to reduce
- global name space pollution
- compilation time
- strange behaviours due to compiler hints, like e.g. pragmas
- potential clash of pre-processor statements
- .. and depending on the order or a misconfigured build system, external software, where we do not have full control (besides of patching), might be affected by above bullet points.
I see that there is not really a point reasoning here as the discussion will spin around forever. I am just surprised that the harms of unused includes are not considered as a bad style and that flaws (IMO) are blindly copied.
@miri64 go ahead and merge.
makefiles/pseudomodules.inc.mk
Outdated
| PSEUDOMODULES += gnrc_sixlowpan_router_default | ||
| PSEUDOMODULES += gnrc_sock_check_reuse | ||
| PSEUDOMODULES += gnrc_txtsnd | ||
| PSEUDOMODULES += l2filter_% |
There was a problem hiding this comment.
So... l2filter_foobar gives me blacklist... Can you give explicit names here. Just having this translate to anything doesn't seem like a good idea. Pseudo-modules are already in-transparent as they are.
sys/shell/commands/sc_netif.c
Outdated
| " * \"cca_threshold\" - set ED threshold during CCA in dBm\n" | ||
| #ifdef MODULE_L2FILTER | ||
| " * \"l2filter\" - add link layer address to filter\n" | ||
| " * \"l2filter_rm\" - remove link layer address from filter\n" |
There was a problem hiding this comment.
Can the command be ifconfig l2filter add or ifconfig l2filter del please? IP addresses are also lists of addresses and are configured that way.
|
Tested for native. Apart from command structure and the pseudomodule names I'm okay with this PR |
|
all fixed. |
|
and one more minor thing that @miri64 pointed out offline. |
4fd691c to
b45339c
Compare
|
squashed. |
I know it's nice and less time-consuming to quickly merge open PRs, but how is #7066 (comment) considered as |
b45339c to
359a89f
Compare
|
@cgundogan: are you satisfied with the answers above? If yes, feel free to press the big green button :-) |
@cgundogan As stated above that's a broader discussion that should be discussed in its own issue, so if you agree, please hit the merge button :-). |
|
@cgundogan: though I brought up the point and started this discussion in the first place, I'm with @miri64 here to solve this in a separate PR. Cause, as mentioned by @haukepetersen its the same for netstats (and maybe other modules, too). So fixing this here, does not resolve it for the rest. Hence, it make sense to create an issue on that matter or provide a PR for further discussion. |
This module allows to filter network packets base on linker layer addresses. It can be used in two configurations,
blacklistingorwhitelistingpackets. To chose a mode, simply include the right module:or
The actual filter list are then controlled via the
ifconfigshell command:So far this module is hooked for all Ethernet and IEEE802.15.4 devices.
Missing/open issues (will get attention in follow-up PRs):
ifconfig-> @PeterKietzmann will add thisnetstatsmodule counts packets that are dropped by the filter -> PR will follow soon