Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
Network Packet Filter API

Network Packet Filter API. More...

Topics

 Basic Filter Conditions
 Ethernet Filter Conditions
 IP Filter Conditions
 TCP Filter Conditions
 UDP Filter Conditions

Data Structures

struct  npf_test
 common filter test structure to be embedded into larger structures More...
struct  npf_rule
 filter rule structure More...
struct  npf_rule_list
 rule set for a given test location More...

Macros

#define NPF_RULE(_name, _result, ...)
 Statically define one packet filter rule.
#define NPF_PRIORITY(_name, _priority, ...)
 Statically define one packet filter rule that sets the packet priority.

Typedefs

typedef 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.

Enumerations

enum  npf_rule_type {
  NPF_RULE_TYPE_UNKNOWN = 0 , NPF_RULE_TYPE_SEND , NPF_RULE_TYPE_RECV , NPF_RULE_TYPE_LOCAL_IN_RECV ,
  NPF_RULE_TYPE_IPV4_RECV , NPF_RULE_TYPE_IPV6_RECV
}
 Type of the packet filter rule. More...

Functions

void npf_insert_rule (struct npf_rule_list *rules, struct npf_rule *rule)
 Insert a rule at the front of given rule list.
void npf_append_rule (struct npf_rule_list *rules, struct npf_rule *rule)
 Append a rule at the end 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.
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.

Variables

struct npf_rule npf_default_ok
 Default rule list termination for accepting a packet.
struct npf_rule npf_default_drop
 Default rule list termination for rejecting a packet.
struct npf_rule_list npf_send_rules
 rule list applied to outgoing packets
struct npf_rule_list npf_recv_rules
 rule list applied to incoming packets
struct npf_rule_list npf_local_in_recv_rules
 rule list applied for local incoming packets
struct npf_rule_list npf_ipv4_recv_rules
 rule list applied for IPv4 incoming packets
struct npf_rule_list npf_ipv6_recv_rules
 rule list applied for IPv6 incoming packets

Detailed Description

Network Packet Filter API.

Since
3.0
Version
0.8.0

Macro Definition Documentation

◆ NPF_PRIORITY

#define NPF_PRIORITY ( _name,
_priority,
... )

#include <zephyr/net/net_pkt_filter.h>

Value:
struct npf_rule _name = { \
.result = NET_CONTINUE, \
.priority = (_priority), \
.nb_tests = NUM_VA_ARGS_LESS_1(__VA_ARGS__) + 1, \
.tests = {FOR_EACH(Z_NPF_TEST_ADDR, (,), __VA_ARGS__)}, \
}
@ NET_CONTINUE
Packet has not been touched, other part should decide about its fate.
Definition net_core.h:90
#define FOR_EACH(F, sep,...)
Call a macro F on each provided argument with a given separator between each call.
Definition util_macro.h:554
#define NUM_VA_ARGS_LESS_1(...)
Number of arguments in the variable arguments list minus one.
Definition util_macro.h:739
filter rule structure
Definition net_pkt_filter.h:93

Statically define one packet filter rule that sets the packet priority.

This creates a rule from a variable amount of filter conditions. Unlike a rule created with NPF_RULE(), it does not determine the fate of the packet: if all conditions are true, the priority of the packet is set to _priority and the next rule in the list is evaluated.

A priority rule does not end the evaluation. If no later rule in the list matches, the packet is dropped, so the list needs a terminating rule such as npf_default_ok, as in the example below.

Example:

static NPF_ETH_TYPE_MATCH(is_ptp, NET_ETH_PTYPE_PTP);
static NPF_PRIORITY(priority_nc, NET_PRIORITY_NC, is_ptp);
void install_my_filter(void)
{
npf_append_recv_rule(&priority_nc);
npf_append_recv_rule(&npf_default_ok);
}
@ NET_PRIORITY_NC
Network control (highest).
Definition net_ip.h:575
#define NPF_PRIORITY(_name, _priority,...)
Statically define one packet filter rule that sets the packet priority.
Definition net_pkt_filter.h:324
struct npf_rule npf_default_ok
Default rule list termination for accepting a packet.
#define NPF_ETH_TYPE_MATCH(_name, _type)
Statically define an "Ethernet type match" packet filter condition.
Definition net_pkt_filter.h:692

The above would accept all packets and give the PTP packets the network control priority.

Parameters
_nameName for this rule.
_priorityPriority of the packet if all conditions are true, one of net_priority.
...List of conditions for this rule.

◆ NPF_RULE

#define NPF_RULE ( _name,
_result,
... )

#include <zephyr/net/net_pkt_filter.h>

Value:
struct npf_rule _name = { \
.result = (_result), \
.nb_tests = NUM_VA_ARGS_LESS_1(__VA_ARGS__) + 1, \
.tests = { FOR_EACH(Z_NPF_TEST_ADDR, (,), __VA_ARGS__) }, \
}

Statically define one packet filter rule.

This creates a rule from a variable amount of filter conditions. This rule can then be inserted or appended to the rule list for a given network packet path.

Example:

static NPF_SIZE_MAX(maxsize_200, 200);
static NPF_ETH_TYPE_MATCH(ip_packet, NET_ETH_PTYPE_IP);
static NPF_RULE(small_ip_pkt, NET_OK, ip_packet, maxsize_200);
void install_my_filter(void)
{
npf_insert_recv_rule(&npf_default_drop);
npf_insert_recv_rule(&small_ip_pkt);
}
@ NET_OK
Packet has been taken care of.
Definition net_core.h:86
#define NPF_RULE(_name, _result,...)
Statically define one packet filter rule.
Definition net_pkt_filter.h:281
struct npf_rule npf_default_drop
Default rule list termination for rejecting a packet.
#define NPF_SIZE_MAX(_name, _size)
Statically define a "data maximum size" packet filter condition.
Definition net_pkt_filter.h:452

The above would accept IP packets that are 200 bytes or smaller, and drop all other packets.

Another (less efficient) way to create the same result could be:

static NPF_SIZE_MIN(minsize_201, 201);
static NPF_ETH_TYPE_UNMATCH(not_ip_packet, NET_ETH_PTYPE_IP);
static NPF_RULE(reject_big_pkts, NET_DROP, minsize_201);
static NPF_RULE(reject_non_ip, NET_DROP, not_ip_packet);
void install_my_filter(void) {
npf_append_recv_rule(&reject_big_pkts);
npf_append_recv_rule(&reject_non_ip);
npf_append_recv_rule(&npf_default_ok);
}
@ NET_DROP
Packet must be dropped.
Definition net_core.h:92
#define NPF_SIZE_MIN(_name, _size)
Statically define a "data minimum size" packet filter condition.
Definition net_pkt_filter.h:436
#define NPF_ETH_TYPE_UNMATCH(_name, _type)
Statically define an "Ethernet type unmatch" packet filter condition.
Definition net_pkt_filter.h:707

The first rule in the list for which all conditions are true determines the fate of the packet. If one condition is false then the next rule in the list is evaluated.

Parameters
_nameName for this rule.
_resultFate of the packet if all conditions are true, either NET_OK or NET_DROP.
...List of conditions for this rule.

Typedef Documentation

◆ npf_rule_cb_t

typedef void(* npf_rule_cb_t) (struct npf_rule *rule, enum npf_rule_type type, void *user_data)

#include <zephyr/net/net_pkt_filter.h>

Callback used while iterating over network packet filter rules.

Parameters
rulePointer to current network packet filter rule
typeType of the rule (rx, tx, local_in, IPv4 or IPv6)
user_dataA valid pointer to user data or NULL

Enumeration Type Documentation

◆ npf_rule_type

#include <zephyr/net/net_pkt_filter.h>

Type of the packet filter rule.

Enumerator
NPF_RULE_TYPE_UNKNOWN 

Unknown rule type.

NPF_RULE_TYPE_SEND 

Rule for outgoing packets.

NPF_RULE_TYPE_RECV 

Rule for incoming packets.

NPF_RULE_TYPE_LOCAL_IN_RECV 

Rule for local incoming packets.

NPF_RULE_TYPE_IPV4_RECV 

Rule for IPv4 incoming packets.

NPF_RULE_TYPE_IPV6_RECV 

Rule for IPv6 incoming packets.

Function Documentation

◆ npf_append_rule()

void npf_append_rule ( struct npf_rule_list * rules,
struct npf_rule * rule )

#include <zephyr/net/net_pkt_filter.h>

Append a rule at the end of given rule list.

Parameters
rulesthe affected rule list
rulethe rule to be appended

◆ npf_insert_rule()

void npf_insert_rule ( struct npf_rule_list * rules,
struct npf_rule * rule )

#include <zephyr/net/net_pkt_filter.h>

Insert a rule at the front of given rule list.

Parameters
rulesthe affected rule list
rulethe rule to be inserted

◆ npf_remove_all_rules()

bool npf_remove_all_rules ( struct npf_rule_list * rules)

#include <zephyr/net/net_pkt_filter.h>

Remove all rules from the given rule list.

Parameters
rulesthe affected rule list
Return values
trueif at least one rule was removed from the rule list

◆ npf_remove_rule()

bool npf_remove_rule ( struct npf_rule_list * rules,
struct npf_rule * rule )

#include <zephyr/net/net_pkt_filter.h>

Remove a rule from the given rule list.

Parameters
rulesthe affected rule list
rulethe rule to be removed
Return values
trueif given rule was found in the rule list and removed

◆ npf_rules_foreach()

void npf_rules_foreach ( npf_rule_cb_t cb,
void * user_data )

#include <zephyr/net/net_pkt_filter.h>

Go through all the network packet filter rules and call callback for each of them.

Parameters
cbUser-supplied callback function to call
user_dataUser specified data

Variable Documentation

◆ npf_default_drop

struct npf_rule npf_default_drop
extern

#include <zephyr/net/net_pkt_filter.h>

Default rule list termination for rejecting a packet.

◆ npf_default_ok

struct npf_rule npf_default_ok
extern

#include <zephyr/net/net_pkt_filter.h>

Default rule list termination for accepting a packet.

◆ npf_ipv4_recv_rules

struct npf_rule_list npf_ipv4_recv_rules
extern

#include <zephyr/net/net_pkt_filter.h>

rule list applied for IPv4 incoming packets

◆ npf_ipv6_recv_rules

struct npf_rule_list npf_ipv6_recv_rules
extern

#include <zephyr/net/net_pkt_filter.h>

rule list applied for IPv6 incoming packets

◆ npf_local_in_recv_rules

struct npf_rule_list npf_local_in_recv_rules
extern

#include <zephyr/net/net_pkt_filter.h>

rule list applied for local incoming packets

◆ npf_recv_rules

struct npf_rule_list npf_recv_rules
extern

#include <zephyr/net/net_pkt_filter.h>

rule list applied to incoming packets

◆ npf_send_rules

struct npf_rule_list npf_send_rules
extern

#include <zephyr/net/net_pkt_filter.h>

rule list applied to outgoing packets