Skip to contentSkip to conversation

netfilter: flowtable: initial bridge support

PATCH In review Pablo Neira Ayuso 2 participants 4 messages Oct 7 – Oct 8

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v5 1/1 netfilter: flowtable: initial bridge support

This patch adds bridge flowtable support, this allows to define a shortcut between two bridge ports. This is complementary to the existing inet family flowtable support.

Set up does not require userspace updates, an example ruleset to enable the flowtable in the bridge family is provided here below:

 table bridge x {
        flowtable y {
                hook ingress priority 0
                devices = { veth0, veth1 }
        }
        chain forward {
                type filter hook forward priority 0
                ip protocol tcp flow add @y counter
                counter
        }

}

I decided to add an explicit nft_flow_offload_bridge_eval() instead of recycling the existing inet function by adding branches to skip the routing part which is obviously not needed in the bridge path. I consider this mostly boiler plate for feature extensibility and better maintability is better to keep it separated. Similarly, the bridge hook that represents the flowtable bridge datapath is implemented in a separated function.

Although connection tracking in the bridge does not support the tracking of IP flows encapsulated in PPPoE and VLAN tracking yet, there are scenarios that involved PPPoE and VLAN that can be supported already, such as those where packets flows through the bridge with no tagging, eg. a VLAN device is used as a bridge port which decapsulates the packets at the ingress path.

Tested with:
- Plain forwarding between bridge ports with no VLAN tagging.
- VLAN device used in bridged ports, as long as packets that are untagged when circulating within the bridge.

This initial bridge flowtable support does support VLAN tagged packets circulating within the bridge yet, because nf_conntrack_bridge still does not support PPPoE/VLAN natively.

Source and destination mac addresses are statically cached in the flow tuple Roaming between devices is not supported either.

No hardware offload at this stage, but patches has been proposed to enable it and will follow up.

---
v5: fix kbuild test robot compilation problem when CONFIG_BRIDGE_NETFILTER
is disabled, use skb_ext_exist().

 include/net/netfilter/nf_flow_table.h |   7 ++
 net/netfilter/nf_flow_table_inet.c    |  12 ++
 net/netfilter/nf_flow_table_ip.c      | 155 ++++++++++++++++++++++++++
 net/netfilter/nf_flow_table_offload.c |   8 +-
 net/netfilter/nf_flow_table_path.c    |  63 +++++++++++
 net/netfilter/nft_flow_offload.c      | 104 ++++++++++++++++-
 6 files changed, 345 insertions(+), 4 deletions(-)
include/net/netfilter/nf_flow_table.h +7 -0
index 5b611efaa3cd..279eb56cc39c 100644
@@ -248,6 +248,8 @@ struct nft_pktinfo;
 int nft_flow_route(const struct nft_pktinfo *pkt, const struct nf_conn *ct,
 		   struct nf_flow_route *route, enum ip_conntrack_dir dir,
 		   struct nft_flowtable *ft);
+int nft_flow_bridge(struct flow_offload *flow, const struct nft_pktinfo *pkt,
+		    enum ip_conntrack_dir dir, struct nft_flowtable *ft);
 
 static inline int
 nf_flow_table_offload_add_cb(struct nf_flowtable *flow_table,
@@ -350,6 +352,8 @@ unsigned int nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
 				     const struct nf_hook_state *state);
 unsigned int nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
 				       const struct nf_hook_state *state);
+unsigned int nf_flow_offload_bridge_hook(void *priv, struct sk_buff *skb,
+					 const struct nf_hook_state *state);
 
 #if (IS_BUILTIN(CONFIG_NF_FLOW_TABLE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
     (IS_MODULE(CONFIG_NF_FLOW_TABLE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
@@ -385,6 +389,9 @@ int nf_flow_rule_route_ipv4(struct net *net, struct flow_offload *flow,
 int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow,
 			    enum flow_offload_tuple_dir dir,
 			    struct nf_flow_rule *flow_rule);
+int nf_flow_rule_bridge(struct net *net, struct flow_offload *flow,
+			enum flow_offload_tuple_dir dir,
+			struct nf_flow_rule *flow_rule);
 
 int nf_flow_table_offload_init(void);
 void nf_flow_table_offload_exit(void);
net/netfilter/nf_flow_table_inet.c +12 -0
index b0f199171932..44790a0d3012 100644
@@ -65,6 +65,15 @@ static int nf_flow_rule_route_inet(struct net *net,
 	return err;
 }
 
+static struct nf_flowtable_type flowtable_bridge = {
+	.family		= NFPROTO_BRIDGE,
+	.init		= nf_flow_table_init,
+	.setup		= nf_flow_table_offload_setup,
+	.free		= nf_flow_table_free,
+	.hook		= nf_flow_offload_bridge_hook,
+	.owner		= THIS_MODULE,
+};
+
 static struct nf_flowtable_type flowtable_inet = {
 	.family		= NFPROTO_INET,
 	.init		= nf_flow_table_init,
@@ -97,6 +106,7 @@ static struct nf_flowtable_type flowtable_ipv6 = {
 
 static int __init nf_flow_inet_module_init(void)
 {
+	nft_register_flowtable_type(&flowtable_bridge);
 	nft_register_flowtable_type(&flowtable_ipv4);
 	nft_register_flowtable_type(&flowtable_ipv6);
 	nft_register_flowtable_type(&flowtable_inet);
@@ -109,6 +119,7 @@ static void __exit nf_flow_inet_module_exit(void)
 	nft_unregister_flowtable_type(&flowtable_inet);
 	nft_unregister_flowtable_type(&flowtable_ipv6);
 	nft_unregister_flowtable_type(&flowtable_ipv4);
+	nft_unregister_flowtable_type(&flowtable_bridge);
 }
 
 module_init(nf_flow_inet_module_init);
@@ -118,5 +129,6 @@ MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Pablo Neira Ayuso <[email protected]>");
 MODULE_ALIAS_NF_FLOWTABLE(AF_INET);
 MODULE_ALIAS_NF_FLOWTABLE(AF_INET6);
+MODULE_ALIAS_NF_FLOWTABLE(AF_BRIDGE);
 MODULE_ALIAS_NF_FLOWTABLE(1); /* NFPROTO_INET */
 MODULE_DESCRIPTION("Netfilter flow table mixed IPv4/IPv6 module");
net/netfilter/nf_flow_table_ip.c +155 -0

158 lines not on this page · raw message

net/netfilter/nf_flow_table_offload.c +5 -3
index 4365859220e6..5cba6074b1d4 100644
@@ -1102,9 +1102,11 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,
 	return offload;
 }
 
-static bool nf_flow_offload_unsupported(struct flow_offload *flow)
+static bool nf_flow_offload_unsupported(struct nf_flowtable *flowtable,
+					struct flow_offload *flow)
 {
-	if (flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.tun_num ||
+	if (flowtable->type->family == NFPROTO_BRIDGE ||
+	    flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.tun_num ||
 	    flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.tun_num)
 		return true;
 
@@ -1126,7 +1128,7 @@ void nf_flow_offload_refresh(struct nf_flowtable *flowtable,
 void nf_flow_offload_add(struct nf_flowtable *flowtable,
 			 struct flow_offload *flow)
 {
-	if (nf_flow_offload_unsupported(flow))
+	if (nf_flow_offload_unsupported(flowtable, flow))
 		return;
 
 	set_bit(NF_FLOW_HW, &flow->flags);
net/netfilter/nf_flow_table_path.c +63 -0

72 lines not on this page · raw message

net/netfilter/nft_flow_offload.c +103 -1

142 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#1 lore raw
Quoted 3 lines · Pablo Neira Ayuso

This patch adds bridge flowtable support, this allows to define a shortcut between two bridge ports. This is complementary to the existing inet family flowtable support.

This v5 rebase needs more careful look, because of several updates that went into the tree. I will post a v6 asap.

#2 lore raw
In reply to Pablo Neira Ayuso
kernel test robot noticed the following build errors:

Hi Pablo,

kernel test robot noticed the following build errors:

[auto build test ERROR on netfilter-nf/main]
[also build test ERROR on nf-next/main linus/master v7.3-rc6 next-20261006] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Pablo-Neira-Ayuso/netfilter-flowtable-initial-bridge-support/20261008-002056
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main
patch link:    https://lore.kernel.org/r/20261007222057.1141093-1-pablo%40netfilter.org
patch subject: [PATCH nf-next,v5] netfilter: flowtable: initial bridge support
config: parisc-randconfig-1001-20261008 (https://download.01.org/0day-ci/archive/20261008/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261008/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

net/netfilter/nft_flow_offload.c: In function 'nft_flow_offload_bridge_eval':

>> net/netfilter/nft_flow_offload.c:153:35: error: 'skb' undeclared (first use in this function)
     153 |  if (!pkt->flags || skb_ext_exist(skb, SKB_EXT_BRIDGE_NF))
         |                                   ^~~
   net/netfilter/nft_flow_offload.c:153:35: note: each undeclared identifier is reported only once for each function it appears in
>> net/netfilter/nft_flow_offload.c:153:40: error: 'SKB_EXT_BRIDGE_NF' undeclared (first use in this function)
     153 |  if (!pkt->flags || skb_ext_exist(skb, SKB_EXT_BRIDGE_NF))
         |                                        ^~~~~~~~~~~~~~~~~
--
   net/netfilter/nf_flow_table_path.c: In function 'nft_dev_fill_bridge_path':
>> net/netfilter/nf_flow_table_path.c:342:28: error: passing argument 1 of 'dev_fill_forward_path' from incompatible pointer type [-Werror=incompatible-pointer-types]
     342 |  if (dev_fill_forward_path(dev, dst_ha, &stack) < 0 ||
         |                            ^~~
         |                            |
         |                            const struct net_device *
   In file included from include/linux/etherdevice.h:21,
                    from net/netfilter/nf_flow_table_path.c:5:
   include/linux/netdevice.h:3440:55: note: expected 'struct net_device_path_ctx *' but argument is of type 'const struct net_device *'
    3440 | int dev_fill_forward_path(struct net_device_path_ctx *ctx,
         |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
   net/netfilter/nf_flow_table_path.c:342:33: error: passing argument 2 of 'dev_fill_forward_path' from incompatible pointer type [-Werror=incompatible-pointer-types]
     342 |  if (dev_fill_forward_path(dev, dst_ha, &stack) < 0 ||
         |                                 ^~~~~~
         |                                 |
         |                                 unsigned char *
   In file included from include/linux/etherdevice.h:21,
                    from net/netfilter/nf_flow_table_path.c:5:
   include/linux/netdevice.h:3441:36: note: expected 'struct net_device_path_stack *' but argument is of type 'unsigned char *'
    3441 |      struct net_device_path_stack *stack);
         |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
>> net/netfilter/nf_flow_table_path.c:342:6: error: too many arguments to function 'dev_fill_forward_path'
     342 |  if (dev_fill_forward_path(dev, dst_ha, &stack) < 0 ||
         |      ^~~~~~~~~~~~~~~~~~~~~
   In file included from include/linux/etherdevice.h:21,
                    from net/netfilter/nf_flow_table_path.c:5:
   include/linux/netdevice.h:3440:5: note: declared here
    3440 | int dev_fill_forward_path(struct net_device_path_ctx *ctx,
         |     ^~~~~~~~~~~~~~~~~~~~~
>> net/netfilter/nf_flow_table_path.c:343:47: error: passing argument 4 of 'nft_dev_path_info' from incompatible pointer type [-Werror=incompatible-pointer-types]
     343 |      nft_dev_path_info(&stack, &info, dst_ha, &ft->data) < 0)
         |                                               ^~~~~~~~~
         |                                               |
         |                                               struct nf_flowtable *
   net/netfilter/nf_flow_table_path.c:108:50: note: expected 'struct nft_flowtable *' but argument is of type 'struct nf_flowtable *'
     108 |         unsigned char *ha, struct nft_flowtable *ft)
         |                            ~~~~~~~~~~~~~~~~~~~~~~^~
>> net/netfilter/nf_flow_table_path.c:346:35: error: 'struct nft_forward_info' has no member named 'indev'; did you mean 'dev'?
     346 |  if (!nft_flowtable_find_dev(info.indev, ft))
         |                                   ^~~~~
         |                                   dev
   net/netfilter/nf_flow_table_path.c:349:28: error: 'struct nft_forward_info' has no member named 'indev'; did you mean 'dev'?
     349 |  this_tuple->iifidx = info.indev->ifindex;
         |                            ^~~~~
         |                            dev

cc1: some warnings being treated as errors

vim +/skb +153 net/netfilter/nft_flow_offload.c

138
139 static void nft_flow_offload_bridge_eval(const struct nft_expr *expr,
140 struct nft_regs *regs,
141 const struct nft_pktinfo *pkt)
142 {
143 struct nft_flow_offload *priv = nft_expr_priv(expr);
144 struct nf_flowtable *flowtable = &priv->flowtable->data;
145 struct tcphdr _tcph, *tcph = NULL;
146 enum ip_conntrack_info ctinfo;
147 struct flow_offload *flow;
148 enum ip_conntrack_dir dir;
149 struct nf_conn *ct;
150 int ret;
151
152 /* Is this a non-IP packet or br_netfilter? If so, skip. */

 > 153		if (!pkt->flags || skb_ext_exist(skb, SKB_EXT_BRIDGE_NF))

154 goto out;
155
156 ct = nf_ct_get(pkt->skb, &ctinfo);
157 if (!ct || !nf_ct_is_confirmed(ct))
158 goto out;
159
160 /* Unlikely, conntrack bridge should not see neither helpers nor NAT in
161 * the bridge forward path.
162 */
163 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
164 ct->status & (IPS_SEQ_ADJUST | IPS_NAT_CLASH))
165 goto out;
166
167 switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
168 case IPPROTO_TCP:
169 tcph = skb_header_pointer(pkt->skb, nft_thoff(pkt),
170 sizeof(_tcph), &_tcph);
171 if (unlikely(!tcph || tcph->fin || tcph->rst ||
172 !nf_conntrack_tcp_established(ct)))
173 goto out;
174 break;
175 case IPPROTO_UDP:
176 break;
177 default:
178 goto out;
179 }
180
181 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
182 goto out;
183
184 flow = flow_offload_alloc(ct);
185 if (!flow)
186 goto err_flow_forward;
187
188 dir = CTINFO2DIR(ctinfo);
189 if (nft_flow_bridge(flow, pkt, dir, priv->flowtable) < 0)
190 goto err_flow_add;
191
192 if (tcph)
193 flow_offload_ct_tcp(ct);
194
195 __set_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags);
196 ret = flow_offload_add(flowtable, flow);
197 if (ret < 0)
198 goto err_flow_add;
199
200 return;
201
202 err_flow_add:
203 flow_offload_free(flow);
204 err_flow_forward:
205 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
206 out:
207 regs->verdict.code = NFT_BREAK;
208 }
209

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#3 lore raw
In reply to Pablo Neira Ayuso
kernel test robot noticed the following build errors:

Hi Pablo,

kernel test robot noticed the following build errors:

[auto build test ERROR on netfilter-nf/main]
[also build test ERROR on nf-next/main linus/master v7.3-rc6 next-20261006] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Pablo-Neira-Ayuso/netfilter-flowtable-initial-bridge-support/20261008-002056
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main
patch link:    https://lore.kernel.org/r/20261007222057.1141093-1-pablo%40netfilter.org
patch subject: [PATCH nf-next,v5] netfilter: flowtable: initial bridge support
config: s390-defconfig (https://download.01.org/0day-ci/archive/20261008/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261008/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> net/netfilter/nft_flow_offload.c:153:35: error: use of undeclared identifier 'skb'
     153 |         if (!pkt->flags || skb_ext_exist(skb, SKB_EXT_BRIDGE_NF))
         |                                          ^
   1 error generated.
--
>> net/netfilter/nf_flow_table_path.c:342:41: error: too many arguments to function call, expected 2, have 3
     342 |         if (dev_fill_forward_path(dev, dst_ha, &stack) < 0 ||
         |             ~~~~~~~~~~~~~~~~~~~~~              ^~~~~~
   include/linux/netdevice.h:3440:5: note: 'dev_fill_forward_path' declared here
    3440 | int dev_fill_forward_path(struct net_device_path_ctx *ctx,
         |     ^                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    3441 |                           struct net_device_path_stack *stack);
         |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> net/netfilter/nf_flow_table_path.c:343:47: error: incompatible pointer types passing 'struct nf_flowtable *' to parameter of type 'struct nft_flowtable *' [-Werror,-Wincompatible-pointer-types]
     343 |             nft_dev_path_info(&stack, &info, dst_ha, &ft->data) < 0)
         |                                                      ^~~~~~~~~
   net/netfilter/nf_flow_table_path.c:108:50: note: passing argument to parameter 'ft' here
     108 |                              unsigned char *ha, struct nft_flowtable *ft)
         |                                                                       ^
>> net/netfilter/nf_flow_table_path.c:346:35: error: no member named 'indev' in 'struct nft_forward_info'

346 | if (!nft_flowtable_find_dev(info.indev, ft))
| ~~~~ ^
net/netfilter/nf_flow_table_path.c:349:28: error: no member named 'indev' in 'struct nft_forward_info'
349 | this_tuple->iifidx = info.indev->ifindex;
| ~~~~ ^
4 errors generated.

vim +/skb +153 net/netfilter/nft_flow_offload.c

138
139 static void nft_flow_offload_bridge_eval(const struct nft_expr *expr,
140 struct nft_regs *regs,
141 const struct nft_pktinfo *pkt)
142 {
143 struct nft_flow_offload *priv = nft_expr_priv(expr);
144 struct nf_flowtable *flowtable = &priv->flowtable->data;
145 struct tcphdr _tcph, *tcph = NULL;
146 enum ip_conntrack_info ctinfo;
147 struct flow_offload *flow;
148 enum ip_conntrack_dir dir;
149 struct nf_conn *ct;
150 int ret;
151
152 /* Is this a non-IP packet or br_netfilter? If so, skip. */

 > 153		if (!pkt->flags || skb_ext_exist(skb, SKB_EXT_BRIDGE_NF))

154 goto out;
155
156 ct = nf_ct_get(pkt->skb, &ctinfo);
157 if (!ct || !nf_ct_is_confirmed(ct))
158 goto out;
159
160 /* Unlikely, conntrack bridge should not see neither helpers nor NAT in
161 * the bridge forward path.
162 */
163 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
164 ct->status & (IPS_SEQ_ADJUST | IPS_NAT_CLASH))
165 goto out;
166
167 switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
168 case IPPROTO_TCP:
169 tcph = skb_header_pointer(pkt->skb, nft_thoff(pkt),
170 sizeof(_tcph), &_tcph);
171 if (unlikely(!tcph || tcph->fin || tcph->rst ||
172 !nf_conntrack_tcp_established(ct)))
173 goto out;
174 break;
175 case IPPROTO_UDP:
176 break;
177 default:
178 goto out;
179 }
180
181 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
182 goto out;
183
184 flow = flow_offload_alloc(ct);
185 if (!flow)
186 goto err_flow_forward;
187
188 dir = CTINFO2DIR(ctinfo);
189 if (nft_flow_bridge(flow, pkt, dir, priv->flowtable) < 0)
190 goto err_flow_add;
191
192 if (tcph)
193 flow_offload_ct_tcp(ct);
194
195 __set_bit(NF_FLOW_HW_BIDIRECTIONAL, &flow->flags);
196 ret = flow_offload_add(flowtable, flow);
197 if (ret < 0)
198 goto err_flow_add;
199
200 return;
201
202 err_flow_add:
203 flow_offload_free(flow);
204 err_flow_forward:
205 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
206 out:
207 regs->verdict.code = NFT_BREAK;
208 }
209

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki