tap: detach queues from the NETDEV_UNREGISTER notifier

tap_del_queues() is currently called from the macvtap and ipvtap
dellink handlers. Two things in this area only work because
tap_open() runs under RTNL, which a following patch removes:

1) Not all unregistrations go through dellink. If register_netdevice()
   fails after the NETDEV_REGISTER notifier created the char device,
   or if macvlan_common_newlink() / ipvlan_link_new() fail after
   register_netdevice(), the device is unregistered without
   tap_del_queues() being called. Nothing can open the char device in
   this window today, because RTNL is held from the minor allocation
   to its release.

2) tap_del_queues() sets tap->numvtaps to MAX_TAP_QUEUES to "guarantee
   that any future tap_set_queue will fail", but since commit
   815f236d6227 ("macvtap: add TUNSETQUEUE ioctl") tap_set_queue()
   tests tap->numqueues, which is zero at this point. A later
   tap_set_queue() would succeed and write to tap->taps[MAX_TAP_QUEUES],
   one element past the array. Again, this can not happen today since
   the minor is released in the same RTNL section.

Call tap_del_queues() from the NETDEV_UNREGISTER notifier instead, so
that every unregistration path is covered. The notifier is also invoked
when the device moves to another netns, in which case the queues must
stay attached: only act when the device is really being unregistered.

Replace the numvtaps trick with an explicit tap->dead flag, tested in
tap_set_queue().

No functional change intended: none of the above is reachable as long
as tap_open() holds RTNL. These issues were found by code inspection,
with the help of an AI coding assistant, while looking at RTNL removal
in tap. They were not triggered.

Signed-off-by: Eric Dumazet <edumazet@kernel.org>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Link: https://patch.msgid.link/20261119072258.3157902-2-edumazet@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
4 files changed