Skip to content

Commit 3b762c0

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: bcm: fix stale rx/tx ops after device removal
RX: an RX_SETUP update(!) for an existing op skipped can_rx_register() unconditionally, even when a concurrent NETDEV_UNREGISTER had already torn down its registration (op->rx_reg_dev == NULL). This silently did not re-enable frame delivery for that updated filter. bcm_rx_setup() now re-registers in that case, while leaving rx_ops with ifindex = 0 (all CAN devices) which never carry a tracked rx_reg_dev registered as-is. TX: bcm_notify() only handled bo->rx_ops on NETDEV_UNREGISTER, leaving tx_ops with an active cyclic transmission re-arming its hrtimer indefinitely to execute bcm_tx_timeout_handler(). Cancelling the hrtimer prevents the runaway timer and any injection into a later reused ifindex, since nothing else calls bcm_can_tx() for the op until an explicit TX_SETUP update re-arms it. Unlike bcm_rx_unreg(), which clears the tracked rx_reg_dev for rx_ops, the ifindex is intentionally left unchanged for tx_ops. bcm_tx_setup() always rejects ifindex 0, so clearing it would strand the op: neither a later TX_SETUP (bcm_find_op()) nor TX_DELETE (bcm_delete_tx_op()) could ever find it again, since both require an exact ifindex match. Reported-by: sashiko-bot@kernel.org Closes: https://lore.kernel.org/linux-can/20260708094536.DDF821F00A3A@smtp.kernel.org/ Closes: https://lore.kernel.org/linux-can/20260708154039.347ED1F000E9@smtp.kernel.org/ Fixes: ffd980f ("[CAN]: Add broadcast manager (bcm) protocol") Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Link: https://patch.msgid.link/20260714-bcm_fixes-v15-9-562f7e3e42da@hartkopp.net Cc: stable@kernel.org Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent d599482 commit 3b762c0

1 file changed

Lines changed: 44 additions & 10 deletions

File tree

net/can/bcm.c

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
12871287
struct bcm_sock *bo = bcm_sk(sk);
12881288
struct bcm_op *op;
12891289
int do_rx_register;
1290+
int new_op = 0;
12901291
int err = 0;
12911292

12921293
if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
@@ -1371,8 +1372,15 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
13711372
/* free temporary frames / kfree(NULL) is safe */
13721373
kfree(new_frames);
13731374

1374-
/* Only an update -> do not call can_rx_register() */
1375-
do_rx_register = 0;
1375+
/* Don't register a new CAN filter for the rx_op update unless
1376+
* a concurrent NETDEV_UNREGISTER notifier already tore down
1377+
* the previous registration. In this case the receiver needs
1378+
* to be re-registered here so that this update doesn't
1379+
* silently stop delivering frames for the given ifindex.
1380+
* Ops with ifindex = 0 (all CAN interfaces) never carry a
1381+
* tracked rx_reg_dev and stay registered as-is.
1382+
*/
1383+
do_rx_register = (ifindex && !op->rx_reg_dev) ? 1 : 0;
13761384

13771385
} else {
13781386
/* insert new BCM operation for the given can_id */
@@ -1439,6 +1447,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
14391447

14401448
/* call can_rx_register() */
14411449
do_rx_register = 1;
1450+
new_op = 1;
14421451

14431452
} /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
14441453

@@ -1452,7 +1461,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
14521461
if (op->flags & SETTIMER) {
14531462

14541463
/* set timers (locked) for newly created op */
1455-
if (do_rx_register) {
1464+
if (new_op) {
14561465
spin_lock_bh(&op->bcm_rx_update_lock);
14571466
op->ival1 = msg_head->ival1;
14581467
op->ival2 = msg_head->ival2;
@@ -1482,7 +1491,10 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
14821491
HRTIMER_MODE_REL_SOFT);
14831492
}
14841493

1485-
/* now we can register for can_ids, if we added a new bcm_op */
1494+
/* now we can register for can_ids, if we added a new bcm_op
1495+
* or need to re-register after a NETDEV_UNREGISTER tore down
1496+
* the previous registration of an existing op
1497+
*/
14861498
if (do_rx_register) {
14871499
if (ifindex) {
14881500
struct net_device *dev;
@@ -1514,18 +1526,32 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
15141526
err = -ENODEV;
15151527
}
15161528

1517-
} else
1529+
} else {
15181530
err = can_rx_register(sock_net(sk), NULL, op->can_id,
15191531
REGMASK(op->can_id),
15201532
bcm_rx_handler, op, "bcm", sk);
1533+
}
1534+
15211535
if (err) {
1522-
/* this bcm rx op is broken -> remove it */
1523-
bcm_remove_op(op);
1536+
/* newly created bcm rx op is broken -> remove it */
1537+
if (new_op) {
1538+
bcm_remove_op(op);
1539+
return err;
1540+
}
1541+
1542+
/* an existing op just stays unregistered.
1543+
* Cancel op->timer and (defensively) op->thrtimer.
1544+
* Other settings can't be reached until the next
1545+
* successful RX_SETUP.
1546+
*/
1547+
hrtimer_cancel(&op->timer);
1548+
hrtimer_cancel(&op->thrtimer);
15241549
return err;
15251550
}
15261551

1527-
/* add this bcm_op to the list of the rx_ops */
1528-
list_add_rcu(&op->list, &bo->rx_ops);
1552+
/* add a new bcm_op to the list of the rx_ops */
1553+
if (new_op)
1554+
list_add_rcu(&op->list, &bo->rx_ops);
15291555
}
15301556

15311557
return msg_head->nframes * op->cfsiz + MHSIZ;
@@ -1745,11 +1771,19 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
17451771
case NETDEV_UNREGISTER:
17461772
lock_sock(sk);
17471773

1748-
/* remove device specific receive entries */
1774+
/* rx_ops: remove device specific receive entries */
17491775
list_for_each_entry(op, &bo->rx_ops, list)
17501776
if (op->rx_reg_dev == dev)
17511777
bcm_rx_unreg(dev, op);
17521778

1779+
/* tx_ops: stop device specific cyclic transmissions on the
1780+
* vanishing ifindex. Cancelling the timer is enough to stop
1781+
* cyclic bcm_can_tx() calls as there is no re-arming.
1782+
*/
1783+
list_for_each_entry(op, &bo->tx_ops, list)
1784+
if (op->ifindex == dev->ifindex)
1785+
hrtimer_cancel(&op->timer);
1786+
17531787
/* remove device reference, if this is our bound device */
17541788
if (bo->bound && bo->ifindex == dev->ifindex) {
17551789
#if IS_ENABLED(CONFIG_PROC_FS)

0 commit comments

Comments
 (0)