14#ifndef ZEPHYR_INCLUDE_NET_NET_PKT_FILTER_H_
15#define ZEPHYR_INCLUDE_NET_NET_PKT_FILTER_H_
43 NPF_TEST_TYPE_UNKNOWN = 0,
44 NPF_TEST_TYPE_IFACE_MATCH,
45 NPF_TEST_TYPE_IFACE_UNMATCH,
46 NPF_TEST_TYPE_ORIG_IFACE_MATCH,
47 NPF_TEST_TYPE_ORIG_IFACE_UNMATCH,
48 NPF_TEST_TYPE_SIZE_MIN,
49 NPF_TEST_TYPE_SIZE_MAX,
50 NPF_TEST_TYPE_SIZE_BOUNDS,
51 NPF_TEST_TYPE_IP_SRC_ADDR_ALLOWLIST,
52 NPF_TEST_TYPE_IP_SRC_ADDR_BLOCKLIST,
53 NPF_TEST_TYPE_ETH_SRC_ADDR_MATCH,
54 NPF_TEST_TYPE_ETH_SRC_ADDR_UNMATCH,
55 NPF_TEST_TYPE_ETH_DST_ADDR_MATCH,
56 NPF_TEST_TYPE_ETH_DST_ADDR_UNMATCH,
57 NPF_TEST_TYPE_ETH_SRC_ADDR_MASK_MATCH,
58 NPF_TEST_TYPE_ETH_DST_ADDR_MASK_MATCH,
59 NPF_TEST_TYPE_ETH_TYPE_MATCH,
60 NPF_TEST_TYPE_ETH_TYPE_UNMATCH,
61 NPF_TEST_TYPE_ETH_VLAN_TYPE_MATCH,
62 NPF_TEST_TYPE_ETH_VLAN_TYPE_UNMATCH,
63 NPF_TEST_TYPE_LOCAL_IN_MATCH,
64 NPF_TEST_TYPE_UDP_SRC_PORT_MATCH,
65 NPF_TEST_TYPE_UDP_DST_PORT_MATCH,
66 NPF_TEST_TYPE_TCP_SRC_PORT_MATCH,
67 NPF_TEST_TYPE_TCP_DST_PORT_MATCH,
68 NPF_TEST_TYPE_TCP_FLAGS_MATCH,
71#if defined(CONFIG_NET_PKT_FILTER_LOG_LEVEL_DBG) || \
72 defined(CONFIG_NET_SHELL_PKT_FILTER_SUPPORTED)
73#define NPF_TEST_ENABLE_NAME 1
74#elif defined(NPF_TEST_ENABLE_NAME)
75#undef NPF_TEST_ENABLE_NAME
88 (
enum npf_test_type type;))
159#define npf_insert_send_rule(rule) npf_insert_rule(&npf_send_rules, rule)
160#define npf_insert_recv_rule(rule) npf_insert_rule(&npf_recv_rules, rule)
161#define npf_append_send_rule(rule) npf_append_rule(&npf_send_rules, rule)
162#define npf_append_recv_rule(rule) npf_append_rule(&npf_recv_rules, rule)
163#define npf_remove_send_rule(rule) npf_remove_rule(&npf_send_rules, rule)
164#define npf_remove_recv_rule(rule) npf_remove_rule(&npf_recv_rules, rule)
165#define npf_remove_all_send_rules() npf_remove_all_rules(&npf_send_rules)
166#define npf_remove_all_recv_rules() npf_remove_all_rules(&npf_recv_rules)
168#ifdef CONFIG_NET_PKT_FILTER_LOCAL_IN_HOOK
169#define npf_insert_local_in_recv_rule(rule) npf_insert_rule(&npf_local_in_recv_rules, rule)
170#define npf_append_local_in_recv_rule(rule) npf_append_rule(&npf_local_in_recv_rules, rule)
171#define npf_remove_local_in_recv_rule(rule) npf_remove_rule(&npf_local_in_recv_rules, rule)
172#define npf_remove_all_local_in_recv_rules() npf_remove_all_rules(&npf_local_in_recv_rules)
175#ifdef CONFIG_NET_PKT_FILTER_IPV4_HOOK
176#define npf_insert_ipv4_recv_rule(rule) npf_insert_rule(&npf_ipv4_recv_rules, rule)
177#define npf_append_ipv4_recv_rule(rule) npf_append_rule(&npf_ipv4_recv_rules, rule)
178#define npf_remove_ipv4_recv_rule(rule) npf_remove_rule(&npf_ipv4_recv_rules, rule)
179#define npf_remove_all_ipv4_recv_rules() npf_remove_all_rules(&npf_ipv4_recv_rules)
182#ifdef CONFIG_NET_PKT_FILTER_IPV6_HOOK
183#define npf_insert_ipv6_recv_rule(rule) npf_insert_rule(&npf_ipv6_recv_rules, rule)
184#define npf_append_ipv6_recv_rule(rule) npf_append_rule(&npf_ipv6_recv_rules, rule)
185#define npf_remove_ipv6_recv_rule(rule) npf_remove_rule(&npf_ipv6_recv_rules, rule)
186#define npf_remove_all_ipv6_recv_rules() npf_remove_all_rules(&npf_ipv6_recv_rules)
245#define NPF_RULE(_name, _result, ...) \
246 struct npf_rule _name = { \
247 .result = (_result), \
248 .nb_tests = NUM_VA_ARGS_LESS_1(__VA_ARGS__) + 1, \
249 .tests = { FOR_EACH(Z_NPF_TEST_ADDR, (,), __VA_ARGS__) }, \
252#define NPF_PRIORITY(_name, _priority, ...) \
253 struct npf_rule _name = { \
254 .result = NET_CONTINUE, \
255 .priority = (_priority), \
256 .nb_tests = NUM_VA_ARGS_LESS_1(__VA_ARGS__) + 1, \
257 .tests = {FOR_EACH(Z_NPF_TEST_ADDR, (,), __VA_ARGS__)}, \
260#define Z_NPF_TEST_ADDR(arg) &arg.test
274struct npf_test_iface {
279extern npf_test_fn_t npf_iface_match;
280extern npf_test_fn_t npf_iface_unmatch;
281extern npf_test_fn_t npf_orig_iface_match;
282extern npf_test_fn_t npf_orig_iface_unmatch;
292#define NPF_IFACE_MATCH(_name, _iface) \
293 struct npf_test_iface _name = { \
295 .test.fn = npf_iface_match, \
296 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
297 (.test.name = "iface", \
298 .test.type = NPF_TEST_TYPE_IFACE_MATCH,)) \
307#define NPF_IFACE_UNMATCH(_name, _iface) \
308 struct npf_test_iface _name = { \
310 .test.fn = npf_iface_unmatch, \
311 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
312 (.test.name = "!iface", \
313 .test.type = NPF_TEST_TYPE_IFACE_UNMATCH,)) \
322#define NPF_ORIG_IFACE_MATCH(_name, _iface) \
323 struct npf_test_iface _name = { \
325 .test.fn = npf_orig_iface_match, \
326 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
327 (.test.name = "orig iface", \
328 .test.type = NPF_TEST_TYPE_ORIG_IFACE_MATCH,)) \
337#define NPF_ORIG_IFACE_UNMATCH(_name, _iface) \
338 struct npf_test_iface _name = { \
340 .test.fn = npf_orig_iface_unmatch, \
341 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
342 (.test.name = "!orig iface", \
343 .test.type = NPF_TEST_TYPE_ORIG_IFACE_UNMATCH,)) \
348struct npf_test_size_bounds {
354extern npf_test_fn_t npf_size_inbounds;
364#define NPF_SIZE_MIN(_name, _size) \
365 struct npf_test_size_bounds _name = { \
368 .test.fn = npf_size_inbounds, \
369 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
370 (.test.name = "size min", \
371 .test.type = NPF_TEST_TYPE_SIZE_MIN,)) \
380#define NPF_SIZE_MAX(_name, _size) \
381 struct npf_test_size_bounds _name = { \
384 .test.fn = npf_size_inbounds, \
385 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
386 (.test.name = "size max", \
387 .test.type = NPF_TEST_TYPE_SIZE_MAX,)) \
397#define NPF_SIZE_BOUNDS(_name, _min_size, _max_size) \
398 struct npf_test_size_bounds _name = { \
399 .min = (_min_size), \
400 .max = (_max_size), \
401 .test.fn = npf_size_inbounds, \
402 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
403 (.test.name = "size bounds", \
404 .test.type = NPF_TEST_TYPE_SIZE_BOUNDS,)) \
416extern npf_test_fn_t npf_ip_src_addr_match;
417extern npf_test_fn_t npf_ip_src_addr_unmatch;
436#define NPF_IP_SRC_ADDR_ALLOWLIST(_name, _ip_addr_array, _ip_addr_num, _af) \
437 struct npf_test_ip _name = { \
438 .addr_family = _af, \
439 .ipaddr = (_ip_addr_array), \
440 .ipaddr_num = _ip_addr_num, \
441 .test.fn = npf_ip_src_addr_match, \
442 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
443 (.test.name = "ip src allow", \
444 .test.type = NPF_TEST_TYPE_IP_SRC_ADDR_ALLOWLIST,)) \
462#define NPF_IP_SRC_ADDR_BLOCKLIST(_name, _ip_addr_array, _ip_addr_num, _af) \
463 struct npf_test_ip _name = { \
464 .addr_family = _af, \
465 .ipaddr = (_ip_addr_array), \
466 .ipaddr_num = _ip_addr_num, \
467 .test.fn = npf_ip_src_addr_unmatch, \
468 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
469 (.test.name = "ip src block", \
470 .test.type = NPF_TEST_TYPE_IP_SRC_ADDR_BLOCKLIST,)) \
485struct npf_test_eth_addr {
487 unsigned int nb_addresses;
492extern npf_test_fn_t npf_eth_src_addr_match;
493extern npf_test_fn_t npf_eth_src_addr_unmatch;
494extern npf_test_fn_t npf_eth_dst_addr_match;
495extern npf_test_fn_t npf_eth_dst_addr_unmatch;
508#define NPF_ETH_SRC_ADDR_MATCH(_name, _addr_array) \
509 struct npf_test_eth_addr _name = { \
510 .addresses = (_addr_array), \
511 .nb_addresses = ARRAY_SIZE(_addr_array), \
512 .test.fn = npf_eth_src_addr_match, \
513 .mask.addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, \
514 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
515 (.test.name = "eth src", \
516 .test.type = NPF_TEST_TYPE_ETH_SRC_ADDR_MATCH,)) \
528#define NPF_ETH_SRC_ADDR_UNMATCH(_name, _addr_array) \
529 struct npf_test_eth_addr _name = { \
530 .addresses = (_addr_array), \
531 .nb_addresses = ARRAY_SIZE(_addr_array), \
532 .test.fn = npf_eth_src_addr_unmatch, \
533 .mask.addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, \
534 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
535 (.test.name = "!eth src", \
536 .test.type = NPF_TEST_TYPE_ETH_SRC_ADDR_UNMATCH,)) \
548#define NPF_ETH_DST_ADDR_MATCH(_name, _addr_array) \
549 struct npf_test_eth_addr _name = { \
550 .addresses = (_addr_array), \
551 .nb_addresses = ARRAY_SIZE(_addr_array), \
552 .test.fn = npf_eth_dst_addr_match, \
553 .mask.addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, \
554 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
555 (.test.name = "eth dst", \
556 .test.type = NPF_TEST_TYPE_ETH_DST_ADDR_MATCH,)) \
568#define NPF_ETH_DST_ADDR_UNMATCH(_name, _addr_array) \
569 struct npf_test_eth_addr _name = { \
570 .addresses = (_addr_array), \
571 .nb_addresses = ARRAY_SIZE(_addr_array), \
572 .test.fn = npf_eth_dst_addr_unmatch, \
573 .mask.addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, \
574 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
575 (.test.name = "!eth dst", \
576 .test.type = NPF_TEST_TYPE_ETH_DST_ADDR_UNMATCH,)) \
589#define NPF_ETH_SRC_ADDR_MASK_MATCH(_name, _addr_array, ...) \
590 struct npf_test_eth_addr _name = { \
591 .addresses = (_addr_array), \
592 .nb_addresses = ARRAY_SIZE(_addr_array), \
593 .mask.addr = { __VA_ARGS__ }, \
594 .test.fn = npf_eth_src_addr_match, \
595 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
596 (.test.name = "eth src mask", \
597 .test.type = NPF_TEST_TYPE_ETH_SRC_ADDR_MASK_MATCH,)) \
610#define NPF_ETH_DST_ADDR_MASK_MATCH(_name, _addr_array, ...) \
611 struct npf_test_eth_addr _name = { \
612 .addresses = (_addr_array), \
613 .nb_addresses = ARRAY_SIZE(_addr_array), \
614 .mask.addr = { __VA_ARGS__ }, \
615 .test.fn = npf_eth_dst_addr_match, \
616 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
617 (.test.name = "eth dst mask", \
618 .test.type = NPF_TEST_TYPE_ETH_DST_ADDR_MASK_MATCH,)) \
623struct npf_test_eth_type {
628extern npf_test_fn_t npf_eth_type_match;
629extern npf_test_fn_t npf_eth_type_unmatch;
630extern npf_test_fn_t npf_eth_vlan_type_match;
631extern npf_test_fn_t npf_eth_vlan_type_unmatch;
641#define NPF_ETH_TYPE_MATCH(_name, _type) \
642 struct npf_test_eth_type _name = { \
643 .type = net_htons(_type), \
644 .test.fn = npf_eth_type_match, \
645 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
646 (.test.name = "eth type", \
647 .test.type = NPF_TEST_TYPE_ETH_TYPE_MATCH,)) \
656#define NPF_ETH_TYPE_UNMATCH(_name, _type) \
657 struct npf_test_eth_type _name = { \
658 .type = net_htons(_type), \
659 .test.fn = npf_eth_type_unmatch, \
660 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
661 (.test.name = "!eth type", \
662 .test.type = NPF_TEST_TYPE_ETH_TYPE_UNMATCH,)) \
672#define NPF_ETH_VLAN_TYPE_MATCH(_name, _type) \
673 struct npf_test_eth_type _name = { \
674 .type = net_htons(_type), \
675 .test.fn = npf_eth_vlan_type_match, \
676 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
677 (.test.name = "eth vlan type", \
678 .test.type = NPF_TEST_TYPE_ETH_VLAN_TYPE_MATCH,)) \
688#define NPF_ETH_VLAN_TYPE_UNMATCH(_name, _type) \
689 struct npf_test_eth_type _name = { \
690 .type = net_htons(_type), \
691 .test.fn = npf_eth_vlan_type_unmatch, \
692 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
693 (.test.name = "!eth vlan type", \
694 .test.type = NPF_TEST_TYPE_ETH_VLAN_TYPE_UNMATCH,)) \
712extern npf_test_fn_t npf_local_in_match;
714struct npf_test_local_in {
729#define NPF_LOCAL_IN_MATCH(_name, _handler, _user_data) \
730 struct npf_test_local_in _name = { \
731 .test.fn = npf_local_in_match, \
733 .user_data = (_user_data), \
734 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
735 (.test.name = "local_in", \
736 .test.type = NPF_TEST_TYPE_LOCAL_IN_MATCH,)) \
771const char *npf_test_get_str(
struct npf_test *test,
char *buf,
792struct npf_test_port {
798extern npf_test_fn_t npf_src_port_match;
799extern npf_test_fn_t npf_dst_port_match;
811#define NPF_UDP_SRC_PORT_MATCH(_name, _port) \
812 struct npf_test_port _name = { \
813 .proto = NET_IPPROTO_UDP, \
814 .port = net_htons(_port), \
815 .test.fn = npf_src_port_match, \
816 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
817 (.test.name = "udp src port", \
818 .test.type = NPF_TEST_TYPE_UDP_SRC_PORT_MATCH,)) \
829#define NPF_UDP_DST_PORT_MATCH(_name, _port) \
830 struct npf_test_port _name = { \
831 .proto = NET_IPPROTO_UDP, \
832 .port = net_htons(_port), \
833 .test.fn = npf_dst_port_match, \
834 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
835 (.test.name = "udp dst port", \
836 .test.type = NPF_TEST_TYPE_UDP_DST_PORT_MATCH,)) \
856struct npf_test_tcp_flags {
862extern npf_test_fn_t npf_tcp_flags_match;
874#define NPF_TCP_SRC_PORT_MATCH(_name, _port) \
875 struct npf_test_port _name = { \
876 .proto = NET_IPPROTO_TCP, \
877 .port = net_htons(_port), \
878 .test.fn = npf_src_port_match, \
879 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
880 (.test.name = "tcp src port", \
881 .test.type = NPF_TEST_TYPE_TCP_SRC_PORT_MATCH,)) \
892#define NPF_TCP_DST_PORT_MATCH(_name, _port) \
893 struct npf_test_port _name = { \
894 .proto = NET_IPPROTO_TCP, \
895 .port = net_htons(_port), \
896 .test.fn = npf_dst_port_match, \
897 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
898 (.test.name = "tcp dst port", \
899 .test.type = NPF_TEST_TYPE_TCP_DST_PORT_MATCH,)) \
909#define NPF_TCP_FLAG_FIN BIT(0)
910#define NPF_TCP_FLAG_SYN BIT(1)
911#define NPF_TCP_FLAG_RST BIT(2)
912#define NPF_TCP_FLAG_PSH BIT(3)
913#define NPF_TCP_FLAG_ACK BIT(4)
914#define NPF_TCP_FLAG_URG BIT(5)
915#define NPF_TCP_FLAG_ECE BIT(6)
916#define NPF_TCP_FLAG_CWR BIT(7)
935#define NPF_TCP_FLAGS_MATCH(_name, _mask, _flags) \
936 struct npf_test_tcp_flags _name = { \
938 .flags = (_flags) + ZERO_OR_COMPILE_ERROR(((_flags) & ~(_mask)) == 0), \
939 .test.fn = npf_tcp_flags_match, \
940 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
941 (.test.name = "tcp flags", \
942 .test.type = NPF_TEST_TYPE_TCP_FLAGS_MATCH,)) \
net_priority
Network packet priority settings described in IEEE 802.1Q Annex I.1.
Definition net_ip.h:567
net_verdict
Net Verdict.
Definition net_core.h:84
void npf_insert_rule(struct npf_rule_list *rules, struct npf_rule *rule)
Insert a rule at the front of given rule list.
bool npf_remove_rule(struct npf_rule_list *rules, struct npf_rule *rule)
Remove a rule from the given rule list.
bool npf_remove_all_rules(struct npf_rule_list *rules)
Remove all rules from the given rule list.
struct npf_rule_list npf_local_in_recv_rules
rule list applied for local incoming packets
struct npf_rule_list npf_send_rules
rule list applied to outgoing packets
struct npf_rule npf_default_drop
Default rule list termination for rejecting a packet.
struct npf_rule_list npf_recv_rules
rule list applied to incoming packets
struct npf_rule npf_default_ok
Default rule list termination for accepting a packet.
struct npf_rule_list npf_ipv6_recv_rules
rule list applied for IPv6 incoming packets
struct npf_rule_list npf_ipv4_recv_rules
rule list applied for IPv4 incoming packets
void npf_append_rule(struct npf_rule_list *rules, struct npf_rule *rule)
Append a rule at the end of given rule list.
void npf_rules_foreach(npf_rule_cb_t cb, void *user_data)
Go through all the network packet filter rules and call callback for each of them.
bool npf_local_in_fn_t(struct net_pkt *pkt, void *user_data)
Function that is called to get the verdict what should happen to the network packet.
Definition net_pkt_filter.h:708
void(* npf_rule_cb_t)(struct npf_rule *rule, enum npf_rule_type type, void *user_data)
Callback used while iterating over network packet filter rules.
Definition net_pkt_filter.h:757
npf_rule_type
Type of the packet filter rule.
Definition net_pkt_filter.h:740
@ NPF_RULE_TYPE_UNKNOWN
Unknown rule type.
Definition net_pkt_filter.h:741
@ NPF_RULE_TYPE_RECV
Rule for incoming packets.
Definition net_pkt_filter.h:743
@ NPF_RULE_TYPE_LOCAL_IN_RECV
Rule for local incoming packets.
Definition net_pkt_filter.h:744
@ NPF_RULE_TYPE_SEND
Rule for outgoing packets.
Definition net_pkt_filter.h:742
@ NPF_RULE_TYPE_IPV4_RECV
Rule for IPv4 incoming packets.
Definition net_pkt_filter.h:745
@ NPF_RULE_TYPE_IPV6_RECV
Rule for IPv6 incoming packets.
Definition net_pkt_filter.h:746
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:54
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:44
#define IF_ENABLED(_flag, _code)
Insert code if _flag is defined and equals 1.
Definition util_macro.h:290
#define min(a, b)
Return smaller value of two provided expressions.
Definition minmax.h:81
#define max(a, b)
Return larger value of two provided expressions.
Definition minmax.h:65
Network core definitions.
flags
Definition parser.h:86
Header file for the single-linked list API.
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Kernel Spin Lock.
Definition spinlock.h:45
Ethernet address.
Definition ethernet.h:51
Network Interface structure.
Definition net_if.h:875
Network packet.
Definition net_pkt.h:119
rule set for a given test location
Definition net_pkt_filter.h:107
sys_slist_t rule_head
List head.
Definition net_pkt_filter.h:108
struct k_spinlock lock
Lock protecting the list access.
Definition net_pkt_filter.h:109
filter rule structure
Definition net_pkt_filter.h:93
uint32_t nb_tests
number of tests for this rule
Definition net_pkt_filter.h:97
enum net_priority priority
priority in case of NET_CONTINUE
Definition net_pkt_filter.h:96
struct npf_test * tests[]
pointers to npf_test instances
Definition net_pkt_filter.h:98
enum net_verdict result
result if all tests pass
Definition net_pkt_filter.h:95
sys_snode_t node
Slist rule list node.
Definition net_pkt_filter.h:94
common filter test structure to be embedded into larger structures
Definition net_pkt_filter.h:81
npf_test_fn_t * fn
packet condition test function
Definition net_pkt_filter.h:82