Skip to content

Commit 47941a6

Browse files
jukkarnashif
authored andcommitted
samples: net: pkt_filter: Add IPv4/6 address blocklist
Add information how to block IPv4 or IPv6 addresses. Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
1 parent 9dd57b3 commit 47941a6

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

samples/net/pkt_filter/README.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ In network shell, you can monitor the network packet filters:
6161
[ 4] recv OK N/A N/A N/A 2 iface[2],eth vlan type[0x0806]
6262
[ 5] recv OK N/A N/A N/A 2 iface[3],eth vlan type[0x0806]
6363
[ 6] recv DROP N/A N/A N/A 0
64+
[ 7] IPv4 recv OK N/A N/A N/A 1 ip src block[192.0.2.2,198.51.100.2]
65+
[ 8] IPv6 recv OK N/A N/A N/A 1 ip src block[2001:db8::2,2001:db8::100:2]
6466
6567
The above sample application network packet filter rules can be interpreted
6668
like this:
@@ -80,6 +82,10 @@ like this:
8082
* Rule 6: Drop all other packets. This also means that IPv6 packets are
8183
dropped.
8284

85+
* Rule 7: Drop IPv4 packets where the source address is either ``192.0.2.2`` or ``198.51.100.2``.
86+
87+
* Rule 8: Drop IPv6 packets where the source address is either ``2001:db8::2`` or ``2001:db8::100:2``.
88+
8389
If you enable network packet priority option :kconfig:option:`CONFIG_NET_SAMPLE_USE_PACKET_PRIORITIES`
8490
then the sample will install extra rules for setting up the priorities.
8591

@@ -96,13 +102,15 @@ then the sample will install extra rules for setting up the priorities.
96102
[ 9] recv OK N/A N/A N/A 2 iface[2],eth vlan type[0x0806]
97103
[10] recv OK N/A N/A N/A 2 iface[3],eth vlan type[0x0806]
98104
[11] recv DROP N/A N/A N/A 0
105+
[12] IPv4 recv OK N/A N/A N/A 1 ip src block[192.0.2.2,198.51.100.2]
106+
[13] IPv6 recv OK N/A N/A N/A 1 ip src block[2001:db8::2,2001:db8::100:2]
99107

100108
The above sample application network packet filter rules can be interpreted
101109
like this:
102110

103111
* Rules 1 - 5: Add rules to set network packet priority to certain type packets.
104112

105-
* Rule 6 - 11: These are the same as in previous rule list.
113+
* Rule 6 - 13: These are the same as in previous rule list.
106114

107115
The network statistics can be used to see that the packets are dropped.
108116
Use ``net stats`` command to monitor statistics.

samples/net/pkt_filter/src/main.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,33 @@ static NPF_RULE(arp_pkt_vlan2, NET_OK, match_iface_vlan2, match_arp_vlan);
5959
static NPF_PRIORITY(arp_priority_vlan1, NET_PRIORITY_BK, match_iface_vlan1, match_arp_vlan);
6060
static NPF_PRIORITY(arp_priority_vlan2, NET_PRIORITY_BK, match_iface_vlan2, match_arp_vlan);
6161

62+
/* Block IPv4 or IPv6 packets from only these addresses */
63+
#define PEER1_IPV4_ADDR_INIT {{{ 192, 0, 2, 2 }}}
64+
#define PEER2_IPV4_ADDR_INIT {{{ 198, 51, 100, 2 }}}
65+
#define PEER1_IPV6_ADDR_INIT \
66+
{{{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02 }}}
67+
#define PEER2_IPV6_ADDR_INIT \
68+
{{{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0x02 }}}
69+
70+
static struct net_in_addr peer_ipv4_addr[] = {
71+
[0] = PEER1_IPV4_ADDR_INIT,
72+
[1] = PEER2_IPV4_ADDR_INIT,
73+
};
74+
75+
static struct net_in6_addr peer_ipv6_addr[] = {
76+
[0] = PEER1_IPV6_ADDR_INIT,
77+
[1] = PEER2_IPV6_ADDR_INIT,
78+
};
79+
80+
static NPF_IP_SRC_ADDR_BLOCKLIST(ipv4_src_block,
81+
peer_ipv4_addr, ARRAY_SIZE(peer_ipv4_addr),
82+
NET_AF_INET);
83+
static NPF_IP_SRC_ADDR_BLOCKLIST(ipv6_src_block,
84+
peer_ipv6_addr, ARRAY_SIZE(peer_ipv6_addr),
85+
NET_AF_INET6);
86+
static NPF_RULE(ipv4_addr_block, NET_OK, ipv4_src_block);
87+
static NPF_RULE(ipv6_addr_block, NET_OK, ipv6_src_block);
88+
6289
static void iface_cb(struct net_if *iface, void *user_data)
6390
{
6491
int count = 0;
@@ -116,7 +143,7 @@ static void init_app(void)
116143
/* The sample will setup the Ethernet interface and two VLAN
117144
* optional interfaces (if VLAN is enabled).
118145
* We allow all traffic to the Ethernet interface, but have
119-
* filters for the VLAN interfaces.
146+
* filters for the VLAN interfaces and check IPv4 and IPv6 source addresses.
120147
*
121148
* First append the priority rules, so that they get evaluated before
122149
* deciding on the final verdict for the packet.
@@ -142,6 +169,12 @@ static void init_app(void)
142169

143170
/* The remaining packets that do not match are dropped */
144171
npf_append_recv_rule(&npf_default_drop);
172+
173+
/* We block packets from specific IPv4 addresses */
174+
npf_append_ipv4_recv_rule(&ipv4_addr_block);
175+
176+
/* We block packets from specific IPv6 addresses */
177+
npf_append_ipv6_recv_rule(&ipv6_addr_block);
145178
}
146179

147180
int main(void)

0 commit comments

Comments
 (0)