[PATCH net-next 1/6] bonding: check XDP support before enslaving changes state

From: Hangbin Liu

Date: Fri Nov 13 2026 - 09:28:51 EST


From: Hangbin Liu <liuhangbin@xxxxxxxxxx>

The XDP capability checks (ndo_bpf/ndo_xdp_xmit) and the slave XDP
program count check in bond_enslave() run after the slave has already
been opened, attached and made visible to the datapath. In particular
the slave can become bond->curr_active_slave, or be published via
bond->slave_cnt, before we verify that slave_dev->netdev_ops->ndo_xdp_xmit
exists, so bond_xdp_xmit() may call a NULL ndo_xdp_xmit.

Move both checks to the validation section at the top of bond_enslave(),
before the first state change. On failure nothing has been mutated yet,
so the function can return directly. The dev_xdp_propagate() setup is
left in place for now and still uses the same error path.

Error messages and errno values are unchanged (-EOPNOTSUPP with the
same netlink extack strings), and the success path is unaffected
beyond running the read-only checks earlier.

Signed-off-by: Hangbin Liu <liuhangbin@xxxxxxxxxx>
---
drivers/net/bonding/bond_main.c | 35 +++++++++++++++++++----------------
1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index 9249a79a168e..340af4c3fa20 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -1948,6 +1948,23 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
return -EPERM;
}

+ /* Check XDP support before any state change, so a slave without
+ * XDP can never become visible to bond_xdp_xmit().
+ */
+ if (bond->xdp_prog) {
+ if (!slave_ops->ndo_bpf || !slave_ops->ndo_xdp_xmit) {
+ SLAVE_NL_ERR(bond_dev, slave_dev, extack,
+ "Slave does not support XDP");
+ return -EOPNOTSUPP;
+ }
+
+ if (dev_xdp_prog_count(slave_dev) > 0) {
+ SLAVE_NL_ERR(bond_dev, slave_dev, extack,
+ "Slave has XDP program loaded, please unload before enslaving");
+ return -EOPNOTSUPP;
+ }
+ }
+
/* set bonding device ether type by slave - bonding netdevices are
* created with ether_setup, so when the slave type is not ARPHRD_ETHER
* there is a need to override some of the type dependent attribs/funcs.
@@ -2311,15 +2328,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
unblock_netpoll_tx();
}

- if (!slave_dev->netdev_ops->ndo_bpf ||
- !slave_dev->netdev_ops->ndo_xdp_xmit) {
- if (bond->xdp_prog) {
- SLAVE_NL_ERR(bond_dev, slave_dev, extack,
- "Slave does not support XDP");
- res = -EOPNOTSUPP;
- goto err_dec_slave_cnt;
- }
- } else if (bond->xdp_prog) {
+ /* XDP capability and prog-count were already validated above */
+ if (bond->xdp_prog) {
struct netdev_bpf xdp = {
.command = XDP_SETUP_PROG,
.flags = 0,
@@ -2327,13 +2337,6 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
.extack = extack,
};

- if (dev_xdp_prog_count(slave_dev) > 0) {
- SLAVE_NL_ERR(bond_dev, slave_dev, extack,
- "Slave has XDP program loaded, please unload before enslaving");
- res = -EOPNOTSUPP;
- goto err_dec_slave_cnt;
- }
-
res = dev_xdp_propagate(slave_dev, &xdp);
if (res < 0) {
/* ndo_bpf() sets extack error message */

--
2.56.0