Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2] ipvlan: update port flags only after ipvlan_set_port_mode() succeeds
@ 2026-11-12  6:48 Wenbo Sun
  2026-11-13  7:00 ` Hangbin Liu
  2026-11-16 13:50 ` netdev-bot+sashiko
  0 siblings, 2 replies; 3+ messages in thread
From: Wenbo Sun @ 2026-11-12  6:48 UTC (permalink / raw)
  To: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni
  Cc: Hangbin Liu, Kuniyuki Iwashima, Mahesh Bandewar, netdev,
	linux-kernel, sunwenbo, 1342116488

In ipvlan_link_new(), port->flags is updated before calling
ipvlan_set_port_mode(). If ipvlan_set_port_mode() fails (for example,
when switching to L3S mode without CONFIG_IPVLAN_L3S), error unwinding
calls unregister_netdevice(dev) via unlink_netdev.

While unregister_netdevice(dev) destroys the port when creating the first
ipvlan device on a lower interface, struct ipvl_port is shared across all
ipvlan interfaces on the same lower device (tracked via port->count). If
another ipvlan interface already exists, unregister_netdevice(dev) only
drops the extra reference and leaves the shared port alive with the
updated port->flags, unintentionally altering the flags of existing
ipvlan interfaces.

Defer updating port->flags until after ipvlan_set_port_mode() succeeds,
after which ipvlan_link_new() has no remaining failure paths. This avoids
modifying port->flags on failure without needing to save and restore it,
matching ipvlan_nl_changelink().

This issue was identified with the assistance of an LLM-based code
review tool, confirmed in a User-Mode Linux (UML) environment by creating
a second ipvlan interface that fails in ipvlan_set_port_mode(), and
verified in UML that the existing interface's port flags remain
unchanged upon failure.

Fixes: a190d04db937 ("ipvlan: introduce 'private' attribute for all existing modes.")
Signed-off-by: Wenbo Sun <sunwenbo@cmss.chinamobile.com>
---
v2:
  - Clarify in the commit message that when creating a second or
    subsequent ipvlan interface on an already-active port,
    unregister_netdevice() only drops the extra reference rather than
    destroying the shared port, and explain why deferring the update
    avoids needing to save and restore port->flags (Hangbin Liu).
  - Soften and clarify the wording in the commit message.
v1: https://lore.kernel.org/netdev/20261111064655.233265-1-sunwenbo@cmss.chinamobile.com/

 drivers/net/ipvlan/ipvlan_main.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/net/ipvlan/ipvlan_main.c b/drivers/net/ipvlan/ipvlan_main.c
index f29864db662a..5732b167c5b3 100644
--- a/drivers/net/ipvlan/ipvlan_main.c
+++ b/drivers/net/ipvlan/ipvlan_main.c
@@ -678,12 +678,6 @@ int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params,
 	if (err)
 		goto remove_ida;
 
-	/* Flags are per port and latest update overrides. User has
-	 * to be consistent in setting it just like the mode attribute.
-	 */
-	if (data && data[IFLA_IPVLAN_FLAGS])
-		port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
-
 	if (data && data[IFLA_IPVLAN_MODE])
 		mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
 
@@ -691,6 +685,12 @@ int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params,
 	if (err)
 		goto unlink_netdev;
 
+	/* Flags are per port and latest update overrides. User has
+	 * to be consistent in setting it just like the mode attribute.
+	 */
+	if (data && data[IFLA_IPVLAN_FLAGS])
+		port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
+
 	mutex_lock(&port->pnodes_lock);
 	list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
 	mutex_unlock(&port->pnodes_lock);
-- 
2.33.0




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2] ipvlan: update port flags only after ipvlan_set_port_mode() succeeds
  2026-11-12  6:48 [PATCH net v2] ipvlan: update port flags only after ipvlan_set_port_mode() succeeds Wenbo Sun
@ 2026-11-13  7:00 ` Hangbin Liu
  2026-11-16 13:50 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: Hangbin Liu @ 2026-11-13  7:00 UTC (permalink / raw)
  To: Wenbo Sun
  Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Kuniyuki Iwashima, Mahesh Bandewar, netdev,
	linux-kernel, 1342116488

On Thu, Nov 12, 2026 at 02:48:21PM +0800, Wenbo Sun wrote:
> In ipvlan_link_new(), port->flags is updated before calling
> ipvlan_set_port_mode(). If ipvlan_set_port_mode() fails (for example,
> when switching to L3S mode without CONFIG_IPVLAN_L3S), error unwinding
> calls unregister_netdevice(dev) via unlink_netdev.
> 
> While unregister_netdevice(dev) destroys the port when creating the first
> ipvlan device on a lower interface, struct ipvl_port is shared across all
> ipvlan interfaces on the same lower device (tracked via port->count). If
> another ipvlan interface already exists, unregister_netdevice(dev) only
> drops the extra reference and leaves the shared port alive with the
> updated port->flags, unintentionally altering the flags of existing
> ipvlan interfaces.
> 
> Defer updating port->flags until after ipvlan_set_port_mode() succeeds,
> after which ipvlan_link_new() has no remaining failure paths. This avoids
> modifying port->flags on failure without needing to save and restore it,
> matching ipvlan_nl_changelink().
> 
> This issue was identified with the assistance of an LLM-based code
> review tool, confirmed in a User-Mode Linux (UML) environment by creating
> a second ipvlan interface that fails in ipvlan_set_port_mode(), and
> verified in UML that the existing interface's port flags remain
> unchanged upon failure.
> 
> Fixes: a190d04db937 ("ipvlan: introduce 'private' attribute for all existing modes.")
> Signed-off-by: Wenbo Sun <sunwenbo@cmss.chinamobile.com>
> ---
> v2:
>   - Clarify in the commit message that when creating a second or
>     subsequent ipvlan interface on an already-active port,
>     unregister_netdevice() only drops the extra reference rather than
>     destroying the shared port, and explain why deferring the update
>     avoids needing to save and restore port->flags (Hangbin Liu).
>   - Soften and clarify the wording in the commit message.
> v1: https://lore.kernel.org/netdev/20261111064655.233265-1-sunwenbo@cmss.chinamobile.com/
> 
>  drivers/net/ipvlan/ipvlan_main.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/net/ipvlan/ipvlan_main.c b/drivers/net/ipvlan/ipvlan_main.c
> index f29864db662a..5732b167c5b3 100644
> --- a/drivers/net/ipvlan/ipvlan_main.c
> +++ b/drivers/net/ipvlan/ipvlan_main.c
> @@ -678,12 +678,6 @@ int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params,
>  	if (err)
>  		goto remove_ida;
>  
> -	/* Flags are per port and latest update overrides. User has
> -	 * to be consistent in setting it just like the mode attribute.
> -	 */
> -	if (data && data[IFLA_IPVLAN_FLAGS])
> -		port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
> -
>  	if (data && data[IFLA_IPVLAN_MODE])
>  		mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
>  
> @@ -691,6 +685,12 @@ int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params,
>  	if (err)
>  		goto unlink_netdev;
>  
> +	/* Flags are per port and latest update overrides. User has
> +	 * to be consistent in setting it just like the mode attribute.
> +	 */
> +	if (data && data[IFLA_IPVLAN_FLAGS])
> +		port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
> +
>  	mutex_lock(&port->pnodes_lock);
>  	list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
>  	mutex_unlock(&port->pnodes_lock);
> -- 
> 2.33.0
> 

Reviewed-by: Hangbin Liu <liuhangbin@kylinos.cn>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2] ipvlan: update port flags only after ipvlan_set_port_mode() succeeds
  2026-11-12  6:48 [PATCH net v2] ipvlan: update port flags only after ipvlan_set_port_mode() succeeds Wenbo Sun
  2026-11-13  7:00 ` Hangbin Liu
@ 2026-11-16 13:50 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 13:50 UTC (permalink / raw)
  To: sunwenbo
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, hangbin.liu, kuniyu,
	maheshb, netdev, linux-kernel, 1342116488

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The fix is incomplete.

--

ipvlan: update port flags only after ipvlan_set_port_mode() succeeds

This patch moves the write of the shared port->flags in ipvlan_link_new()
to after ipvlan_set_port_mode() has succeeded. The goal is that a failed
creation of a second ipvlan on an active port does not change the flags
of the ipvlan interfaces that already exist.

> diff --git a/drivers/net/ipvlan/ipvlan_main.c b/drivers/net/ipvlan/ipvlan_main.c
> index f29864db662aa..5732b167c5b3c 100644
> --- a/drivers/net/ipvlan/ipvlan_main.c
> +++ b/drivers/net/ipvlan/ipvlan_main.c

[ ... ]

> @@ -691,6 +685,12 @@ int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params,
>  	if (err)
>  		goto unlink_netdev;
>  
> +	/* Flags are per port and latest update overrides. User has
> +	 * to be consistent in setting it just like the mode attribute.
> +	 */
> +	if (data && data[IFLA_IPVLAN_FLAGS])
> +		port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
> +

[Severity: High]
Can a failed RTM_NEWLINK still overwrite the shared port->flags here?

The commit message says:

  "Defer updating port->flags until after ipvlan_set_port_mode() succeeds,
  after which ipvlan_link_new() has no remaining failure paths."

When ipvlan_link_new() returns 0, the creation is not yet committed. The
caller, rtnl_newlink_create(), can still fail after ops->newlink():

net/core/rtnetlink.c:rtnl_newlink_create() {
    ...
	err = rtnl_configure_link(dev, ifm, portid, nlh);
	if (err < 0)
		goto out_unregister;
	if (tb[IFLA_MASTER]) {
		netdev_lock_ops(dev);
		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
		netdev_unlock_ops(dev);
		if (err)
			goto out_unregister;
	}
    ...
out_unregister:
	if (ops->newlink) {
		LIST_HEAD(list_kill);

		ops->dellink(dev, &list_kill);
		unregister_netdevice_many(&list_kill);
    ...
}

For ipvlan, the dellink callback is ipvlan_link_delete(), which calls
__ipvlan_link_delete(). Neither function restores port->flags. When
port->count > 1, the shared port survives with the flags from the failed
request.

The IFLA_MASTER failure is easy to trigger. A new ipvlan has no master
upper device, so do_set_master() goes straight to the ifindex lookup. That
returns -EINVAL for a nonexistent ifindex, or -EOPNOTSUPP for a device
without ndo_add_slave, such as lo.

For example, with ipv1 on eth0 in bridge mode, this command fails but
leaves ipv1 in private mode:

  ip link add link eth0 name ipv2 master lo type ipvlan mode l2 private

The port->mode change made by ipvlan_set_port_mode() also persists on this
path. Moving the assignment within ipvlan_link_new() cannot cover this
case.

Would this need port state to be saved and restored across the whole
rtnl transaction, for example by restoring it from dellink when creation
is rolled back?

>  	mutex_lock(&port->pnodes_lock);
>  	list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
>  	mutex_unlock(&port->pnodes_lock);

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112064821.85624-1-sunwenbo%40cmss.chinamobile.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-16 13:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12  6:48 [PATCH net v2] ipvlan: update port flags only after ipvlan_set_port_mode() succeeds Wenbo Sun
2026-11-13  7:00 ` Hangbin Liu
2026-11-16 13:50 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox