@@ -59,6 +59,33 @@ static NPF_RULE(arp_pkt_vlan2, NET_OK, match_iface_vlan2, match_arp_vlan);
5959static NPF_PRIORITY (arp_priority_vlan1 , NET_PRIORITY_BK , match_iface_vlan1 , match_arp_vlan ) ;
6060static 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+
6289static 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
147180int main (void )
0 commit comments