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;))
188const char *npf_test_get_str(
struct npf_test *test,
char *buf,
195#define npf_insert_send_rule(rule) npf_insert_rule(&npf_send_rules, rule)
196#define npf_insert_recv_rule(rule) npf_insert_rule(&npf_recv_rules, rule)
197#define npf_append_send_rule(rule) npf_append_rule(&npf_send_rules, rule)
198#define npf_append_recv_rule(rule) npf_append_rule(&npf_recv_rules, rule)
199#define npf_remove_send_rule(rule) npf_remove_rule(&npf_send_rules, rule)
200#define npf_remove_recv_rule(rule) npf_remove_rule(&npf_recv_rules, rule)
201#define npf_remove_all_send_rules() npf_remove_all_rules(&npf_send_rules)
202#define npf_remove_all_recv_rules() npf_remove_all_rules(&npf_recv_rules)
204#ifdef CONFIG_NET_PKT_FILTER_LOCAL_IN_HOOK
205#define npf_insert_local_in_recv_rule(rule) npf_insert_rule(&npf_local_in_recv_rules, rule)
206#define npf_append_local_in_recv_rule(rule) npf_append_rule(&npf_local_in_recv_rules, rule)
207#define npf_remove_local_in_recv_rule(rule) npf_remove_rule(&npf_local_in_recv_rules, rule)
208#define npf_remove_all_local_in_recv_rules() npf_remove_all_rules(&npf_local_in_recv_rules)
211#ifdef CONFIG_NET_PKT_FILTER_IPV4_HOOK
212#define npf_insert_ipv4_recv_rule(rule) npf_insert_rule(&npf_ipv4_recv_rules, rule)
213#define npf_append_ipv4_recv_rule(rule) npf_append_rule(&npf_ipv4_recv_rules, rule)
214#define npf_remove_ipv4_recv_rule(rule) npf_remove_rule(&npf_ipv4_recv_rules, rule)
215#define npf_remove_all_ipv4_recv_rules() npf_remove_all_rules(&npf_ipv4_recv_rules)
218#ifdef CONFIG_NET_PKT_FILTER_IPV6_HOOK
219#define npf_insert_ipv6_recv_rule(rule) npf_insert_rule(&npf_ipv6_recv_rules, rule)
220#define npf_append_ipv6_recv_rule(rule) npf_append_rule(&npf_ipv6_recv_rules, rule)
221#define npf_remove_ipv6_recv_rule(rule) npf_remove_rule(&npf_ipv6_recv_rules, rule)
222#define npf_remove_all_ipv6_recv_rules() npf_remove_all_rules(&npf_ipv6_recv_rules)
281#define NPF_RULE(_name, _result, ...) \
282 struct npf_rule _name = { \
283 .result = (_result), \
284 .nb_tests = NUM_VA_ARGS_LESS_1(__VA_ARGS__) + 1, \
285 .tests = { FOR_EACH(Z_NPF_TEST_ADDR, (,), __VA_ARGS__) }, \
324#define NPF_PRIORITY(_name, _priority, ...) \
325 struct npf_rule _name = { \
326 .result = NET_CONTINUE, \
327 .priority = (_priority), \
328 .nb_tests = NUM_VA_ARGS_LESS_1(__VA_ARGS__) + 1, \
329 .tests = {FOR_EACH(Z_NPF_TEST_ADDR, (,), __VA_ARGS__)}, \
332#define Z_NPF_TEST_ADDR(arg) &arg.test
346struct npf_test_iface {
351extern npf_test_fn_t npf_iface_match;
352extern npf_test_fn_t npf_iface_unmatch;
353extern npf_test_fn_t npf_orig_iface_match;
354extern npf_test_fn_t npf_orig_iface_unmatch;
364#define NPF_IFACE_MATCH(_name, _iface) \
365 struct npf_test_iface _name = { \
367 .test.fn = npf_iface_match, \
368 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
369 (.test.name = "iface", \
370 .test.type = NPF_TEST_TYPE_IFACE_MATCH,)) \
379#define NPF_IFACE_UNMATCH(_name, _iface) \
380 struct npf_test_iface _name = { \
382 .test.fn = npf_iface_unmatch, \
383 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
384 (.test.name = "!iface", \
385 .test.type = NPF_TEST_TYPE_IFACE_UNMATCH,)) \
394#define NPF_ORIG_IFACE_MATCH(_name, _iface) \
395 struct npf_test_iface _name = { \
397 .test.fn = npf_orig_iface_match, \
398 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
399 (.test.name = "orig iface", \
400 .test.type = NPF_TEST_TYPE_ORIG_IFACE_MATCH,)) \
409#define NPF_ORIG_IFACE_UNMATCH(_name, _iface) \
410 struct npf_test_iface _name = { \
412 .test.fn = npf_orig_iface_unmatch, \
413 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
414 (.test.name = "!orig iface", \
415 .test.type = NPF_TEST_TYPE_ORIG_IFACE_UNMATCH,)) \
420struct npf_test_size_bounds {
426extern npf_test_fn_t npf_size_inbounds;
436#define NPF_SIZE_MIN(_name, _size) \
437 struct npf_test_size_bounds _name = { \
440 .test.fn = npf_size_inbounds, \
441 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
442 (.test.name = "size min", \
443 .test.type = NPF_TEST_TYPE_SIZE_MIN,)) \
452#define NPF_SIZE_MAX(_name, _size) \
453 struct npf_test_size_bounds _name = { \
456 .test.fn = npf_size_inbounds, \
457 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
458 (.test.name = "size max", \
459 .test.type = NPF_TEST_TYPE_SIZE_MAX,)) \
469#define NPF_SIZE_BOUNDS(_name, _min_size, _max_size) \
470 struct npf_test_size_bounds _name = { \
471 .min = (_min_size), \
472 .max = (_max_size), \
473 .test.fn = npf_size_inbounds, \
474 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
475 (.test.name = "size bounds", \
476 .test.type = NPF_TEST_TYPE_SIZE_BOUNDS,)) \
494extern npf_test_fn_t npf_local_in_match;
496struct npf_test_local_in {
514#define NPF_LOCAL_IN_MATCH(_name, _handler, _user_data) \
515 struct npf_test_local_in _name = { \
516 .test.fn = npf_local_in_match, \
518 .user_data = (_user_data), \
519 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
520 (.test.name = "local_in", \
521 .test.type = NPF_TEST_TYPE_LOCAL_IN_MATCH,)) \
536struct npf_test_eth_addr {
538 unsigned int nb_addresses;
543extern npf_test_fn_t npf_eth_src_addr_match;
544extern npf_test_fn_t npf_eth_src_addr_unmatch;
545extern npf_test_fn_t npf_eth_dst_addr_match;
546extern npf_test_fn_t npf_eth_dst_addr_unmatch;
559#define NPF_ETH_SRC_ADDR_MATCH(_name, _addr_array) \
560 struct npf_test_eth_addr _name = { \
561 .addresses = (_addr_array), \
562 .nb_addresses = ARRAY_SIZE(_addr_array), \
563 .test.fn = npf_eth_src_addr_match, \
564 .mask.addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, \
565 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
566 (.test.name = "eth src", \
567 .test.type = NPF_TEST_TYPE_ETH_SRC_ADDR_MATCH,)) \
579#define NPF_ETH_SRC_ADDR_UNMATCH(_name, _addr_array) \
580 struct npf_test_eth_addr _name = { \
581 .addresses = (_addr_array), \
582 .nb_addresses = ARRAY_SIZE(_addr_array), \
583 .test.fn = npf_eth_src_addr_unmatch, \
584 .mask.addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, \
585 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
586 (.test.name = "!eth src", \
587 .test.type = NPF_TEST_TYPE_ETH_SRC_ADDR_UNMATCH,)) \
599#define NPF_ETH_DST_ADDR_MATCH(_name, _addr_array) \
600 struct npf_test_eth_addr _name = { \
601 .addresses = (_addr_array), \
602 .nb_addresses = ARRAY_SIZE(_addr_array), \
603 .test.fn = npf_eth_dst_addr_match, \
604 .mask.addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, \
605 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
606 (.test.name = "eth dst", \
607 .test.type = NPF_TEST_TYPE_ETH_DST_ADDR_MATCH,)) \
619#define NPF_ETH_DST_ADDR_UNMATCH(_name, _addr_array) \
620 struct npf_test_eth_addr _name = { \
621 .addresses = (_addr_array), \
622 .nb_addresses = ARRAY_SIZE(_addr_array), \
623 .test.fn = npf_eth_dst_addr_unmatch, \
624 .mask.addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, \
625 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
626 (.test.name = "!eth dst", \
627 .test.type = NPF_TEST_TYPE_ETH_DST_ADDR_UNMATCH,)) \
640#define NPF_ETH_SRC_ADDR_MASK_MATCH(_name, _addr_array, ...) \
641 struct npf_test_eth_addr _name = { \
642 .addresses = (_addr_array), \
643 .nb_addresses = ARRAY_SIZE(_addr_array), \
644 .mask.addr = { __VA_ARGS__ }, \
645 .test.fn = npf_eth_src_addr_match, \
646 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
647 (.test.name = "eth src mask", \
648 .test.type = NPF_TEST_TYPE_ETH_SRC_ADDR_MASK_MATCH,)) \
661#define NPF_ETH_DST_ADDR_MASK_MATCH(_name, _addr_array, ...) \
662 struct npf_test_eth_addr _name = { \
663 .addresses = (_addr_array), \
664 .nb_addresses = ARRAY_SIZE(_addr_array), \
665 .mask.addr = { __VA_ARGS__ }, \
666 .test.fn = npf_eth_dst_addr_match, \
667 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
668 (.test.name = "eth dst mask", \
669 .test.type = NPF_TEST_TYPE_ETH_DST_ADDR_MASK_MATCH,)) \
674struct npf_test_eth_type {
679extern npf_test_fn_t npf_eth_type_match;
680extern npf_test_fn_t npf_eth_type_unmatch;
681extern npf_test_fn_t npf_eth_vlan_type_match;
682extern npf_test_fn_t npf_eth_vlan_type_unmatch;
692#define NPF_ETH_TYPE_MATCH(_name, _type) \
693 struct npf_test_eth_type _name = { \
694 .type = net_htons(_type), \
695 .test.fn = npf_eth_type_match, \
696 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
697 (.test.name = "eth type", \
698 .test.type = NPF_TEST_TYPE_ETH_TYPE_MATCH,)) \
707#define NPF_ETH_TYPE_UNMATCH(_name, _type) \
708 struct npf_test_eth_type _name = { \
709 .type = net_htons(_type), \
710 .test.fn = npf_eth_type_unmatch, \
711 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
712 (.test.name = "!eth type", \
713 .test.type = NPF_TEST_TYPE_ETH_TYPE_UNMATCH,)) \
723#define NPF_ETH_VLAN_TYPE_MATCH(_name, _type) \
724 struct npf_test_eth_type _name = { \
725 .type = net_htons(_type), \
726 .test.fn = npf_eth_vlan_type_match, \
727 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
728 (.test.name = "eth vlan type", \
729 .test.type = NPF_TEST_TYPE_ETH_VLAN_TYPE_MATCH,)) \
739#define NPF_ETH_VLAN_TYPE_UNMATCH(_name, _type) \
740 struct npf_test_eth_type _name = { \
741 .type = net_htons(_type), \
742 .test.fn = npf_eth_vlan_type_unmatch, \
743 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
744 (.test.name = "!eth vlan type", \
745 .test.type = NPF_TEST_TYPE_ETH_VLAN_TYPE_UNMATCH,)) \
767extern npf_test_fn_t npf_ip_src_addr_match;
768extern npf_test_fn_t npf_ip_src_addr_unmatch;
787#define NPF_IP_SRC_ADDR_ALLOWLIST(_name, _ip_addr_array, _ip_addr_num, _af) \
788 struct npf_test_ip _name = { \
789 .addr_family = _af, \
790 .ipaddr = (_ip_addr_array), \
791 .ipaddr_num = _ip_addr_num, \
792 .test.fn = npf_ip_src_addr_match, \
793 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
794 (.test.name = "ip src allow", \
795 .test.type = NPF_TEST_TYPE_IP_SRC_ADDR_ALLOWLIST,)) \
813#define NPF_IP_SRC_ADDR_BLOCKLIST(_name, _ip_addr_array, _ip_addr_num, _af) \
814 struct npf_test_ip _name = { \
815 .addr_family = _af, \
816 .ipaddr = (_ip_addr_array), \
817 .ipaddr_num = _ip_addr_num, \
818 .test.fn = npf_ip_src_addr_unmatch, \
819 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
820 (.test.name = "ip src block", \
821 .test.type = NPF_TEST_TYPE_IP_SRC_ADDR_BLOCKLIST,)) \
841struct npf_test_port {
847extern npf_test_fn_t npf_src_port_match;
848extern npf_test_fn_t npf_dst_port_match;
860#define NPF_UDP_SRC_PORT_MATCH(_name, _port) \
861 struct npf_test_port _name = { \
862 .proto = NET_IPPROTO_UDP, \
863 .port = net_htons(_port), \
864 .test.fn = npf_src_port_match, \
865 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
866 (.test.name = "udp src port", \
867 .test.type = NPF_TEST_TYPE_UDP_SRC_PORT_MATCH,)) \
878#define NPF_UDP_DST_PORT_MATCH(_name, _port) \
879 struct npf_test_port _name = { \
880 .proto = NET_IPPROTO_UDP, \
881 .port = net_htons(_port), \
882 .test.fn = npf_dst_port_match, \
883 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
884 (.test.name = "udp dst port", \
885 .test.type = NPF_TEST_TYPE_UDP_DST_PORT_MATCH,)) \
905struct npf_test_tcp_flags {
911extern npf_test_fn_t npf_tcp_flags_match;
923#define NPF_TCP_SRC_PORT_MATCH(_name, _port) \
924 struct npf_test_port _name = { \
925 .proto = NET_IPPROTO_TCP, \
926 .port = net_htons(_port), \
927 .test.fn = npf_src_port_match, \
928 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
929 (.test.name = "tcp src port", \
930 .test.type = NPF_TEST_TYPE_TCP_SRC_PORT_MATCH,)) \
941#define NPF_TCP_DST_PORT_MATCH(_name, _port) \
942 struct npf_test_port _name = { \
943 .proto = NET_IPPROTO_TCP, \
944 .port = net_htons(_port), \
945 .test.fn = npf_dst_port_match, \
946 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
947 (.test.name = "tcp dst port", \
948 .test.type = NPF_TEST_TYPE_TCP_DST_PORT_MATCH,)) \
958#define NPF_TCP_FLAG_FIN BIT(0)
959#define NPF_TCP_FLAG_SYN BIT(1)
960#define NPF_TCP_FLAG_RST BIT(2)
961#define NPF_TCP_FLAG_PSH BIT(3)
962#define NPF_TCP_FLAG_ACK BIT(4)
963#define NPF_TCP_FLAG_URG BIT(5)
964#define NPF_TCP_FLAG_ECE BIT(6)
965#define NPF_TCP_FLAG_CWR BIT(7)
984#define NPF_TCP_FLAGS_MATCH(_name, _mask, _flags) \
985 struct npf_test_tcp_flags _name = { \
987 .flags = (_flags) + ZERO_OR_COMPILE_ERROR(((_flags) & ~(_mask)) == 0), \
988 .test.fn = npf_tcp_flags_match, \
989 IF_ENABLED(NPF_TEST_ENABLE_NAME, \
990 (.test.name = "tcp flags", \
991 .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_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.
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
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:174
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.
npf_rule_type
Type of the packet filter rule.
Definition net_pkt_filter.h:157
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.
@ NPF_RULE_TYPE_UNKNOWN
Unknown rule type.
Definition net_pkt_filter.h:158
@ NPF_RULE_TYPE_RECV
Rule for incoming packets.
Definition net_pkt_filter.h:160
@ NPF_RULE_TYPE_LOCAL_IN_RECV
Rule for local incoming packets.
Definition net_pkt_filter.h:161
@ NPF_RULE_TYPE_SEND
Rule for outgoing packets.
Definition net_pkt_filter.h:159
@ NPF_RULE_TYPE_IPV4_RECV
Rule for IPv4 incoming packets.
Definition net_pkt_filter.h:162
@ NPF_RULE_TYPE_IPV6_RECV
Rule for IPv6 incoming packets.
Definition net_pkt_filter.h:163
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:490
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