From nobody Mon Jun 8 07:22:55 2026 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D007378D9F for ; Sat, 6 Jun 2026 12:52:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780750380; cv=none; b=D6HM8LnY+LVRkwZJGgfay7hinRqRcZDZbv7gw9y4Ca1jAbVhVXQrwnCeYac5Ecuug1KrrkOEUucAkb6pufTwsZHPeWDxCxz80+vJVF3pT8Mu4OSmKoDy/fSuiPhaMXCjUw57Dx6xJw9Timx8zn6AoBon/1TTGHFoeM64Gcoe3L0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780750380; c=relaxed/simple; bh=XaTqpFxxwKS8yojvoQ6qdW4M4wgPwmYAuFFS3RqvR2A=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=SKcF796MvLUeB00b8XquCTHg4E5SkyAWiDzfCOzT3KFphG8SNlL/PWrF87ILeCrxO8mBOTe4HZrQhOMuhn4TpWgPUW1rEOJPiqKp6cZ0MhBPD9IOqXtkTq7m7Ii69POMa2pFSE1NItE+5WuHC2Gwq9JjwIkZXMkn6FE6XabKf3o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ohlEaqEk; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ohlEaqEk" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-c8532ba6c95so1003710a12.0 for ; Sat, 06 Jun 2026 05:52:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780750377; x=1781355177; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=6yTRs05ISCu/833adbwR/BZNotnYl1U3ybYttxpUcAg=; b=ohlEaqEkA/G1stN5cZWsvZ6ox8kzLkAa77PrWtMzofYb6LbzIDUe4NMvLXTvG50fOk FguJZCGflpKHudTwjqqEBiWa7VkNHZ/hi03urIpfxnfyRoiHNsMX3zeOrTKoUVIEt2WA bcIhHlck7dtrHlR9BY5w4Pi1d3wOJrBsWD4YH1Cp7SGsD5Ufe2zViB+ph2PTwGKLBoPg ej35k8bR5d4/d+DM72wgYaAQGARC2XKoTA/BDCBz0suzBinFTY3f0vStUmhbCxo68QhZ R9usW0jLckpKdYbJ6WI+PlGsz1iGspWGRlTWy4LRZlTr1QhuO4ucBSRTGAtoVksRPc52 DffA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780750377; x=1781355177; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=6yTRs05ISCu/833adbwR/BZNotnYl1U3ybYttxpUcAg=; b=LmFVSaSz3+WVwMCtvmk+RvJoDfAwv2afExDn1RXil1zV/bkAikXXX+eQW8/zJeDdTW XMPbyDJaBbA86GapIpQuoy3PEaDkCPpTmP7Dr1zfxJqpNNmNxamyZv5sNLWd5TGaIYRv l2JAsnrdSazxTNPVBmYd6qJg94PNqfU6Ynb9AjDFTe6jKYh7VS0v2r5Sjfd/Y2gtMh9J 10gxYYt7pXWofzIjCWMH52TdLcbjKPCP3e9od8Aa07C2xr+VtDECrNgixuPmnuXufqQK rAoIpQCh+n+9UScLg6CdMgp4ehe+sG6CXK1GpomclsTAA3jl8A6jhZ9HL/JYHjDp3Fio t0VA== X-Forwarded-Encrypted: i=1; AFNElJ/yDQBbl2A3OSsE1Z0BAJN7qYMzCUMEwtZECYAWzPl9b4TO2eeZHpxXp2vSsMQBeEAlHuoqN7i/REgmZC4=@vger.kernel.org X-Gm-Message-State: AOJu0YxaWL2QtST0U34oacOHs7AZXAWLqAxr7v6sEAZV+UP+wbgbCDHr gZXTk58QjTkQTLGhze08v+naA+cellvsahELiZfxKsAYr6cvWX86c7Wq X-Gm-Gg: Acq92OGokGyTqTkfZfAfbeNMSNSiXzuL/c3D3LmKWkjnB0RWLlX/hE1SvMM6jcm34Rn jfjcc0vA1R9HJPsA3yY1V2l6y4cK+4MOfG7TmtmPvOBrOyP2uYbgzRa9KTFrHXqRJckT/IHnzoY CsB6oyaJYXRobZB9hVN1jEsEtyz2zQqDGmXrnGoI480kYLd30sfymWdD2EyQo6EvIseL+0MOw3n xNWZP70So/LXxZB1syADBPTSWQyGfDGymQ7CzMjWbBiD5eyp4T2/VcbHjkTclNuBZcTOaMtQ1Sd nHAvgskuq+GHQUHZa+FuRxMj28oqS20D8UzktDsStfmcZ5VdCVbo6Pkb+tze0CDDlbrPPUdFtWp CIW7DU+CHAVkrtHsy+t4WuyS36hjO6P94SNALmPCxcMBlluInTyYrWGT7X6U9UOwN6YqZiZ4ifT PD5jYO1G+/LRh25ybdTDrBhIPBqI8PjIYtaj9J2e1W6dJe43OpAQcFsVYt85Tb X-Received: by 2002:a05:6a00:ad0b:b0:82f:1b1b:e166 with SMTP id d2e1a72fcca58-842b1041871mr8509020b3a.33.1780750377375; Sat, 06 Jun 2026 05:52:57 -0700 (PDT) Received: from d.home.yangfl.dn42 ([2a09:bac5:661b:3046::4cf:38]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84282904889sm12288621b3a.55.2026.06.06.05.52.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Jun 2026 05:52:56 -0700 (PDT) From: David Yang To: netdev@vger.kernel.org Cc: David Yang , Vladimir Oltean , UNGLinuxDriver@microchip.com, Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org Subject: [PATCH net-next v2] net: mscc: ocelot: validate netdev belongs to switch in .netdev_to_port() Date: Sat, 6 Jun 2026 20:52:44 +0800 Message-ID: <20260606125247.305167-1-mmyangfl@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The .netdev_to_port() currently takes only a net_device and returns the port index, without verifying the netdev actually belongs to the switch being operated on. This can cause flower rule parsing to silently resolve to a wrong port on the local hardware. Update both implementations felix_netdev_to_port() and ocelot_netdev_to_port() to validate ownership. Also update the callers in ocelot_flower.c to pass through the ocelot context. Signed-off-by: David Yang --- v1: https://lore.kernel.org/r/20260603024234.66603-1-mmyangfl@gmail.com - also fix ocelot_netdev_to_port() drivers/net/dsa/ocelot/felix.c | 6 ++++-- drivers/net/dsa/ocelot/felix.h | 2 +- drivers/net/ethernet/mscc/ocelot.h | 2 +- drivers/net/ethernet/mscc/ocelot_flower.c | 4 ++-- drivers/net/ethernet/mscc/ocelot_net.c | 4 +++- include/soc/mscc/ocelot.h | 2 +- 6 files changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/net/dsa/ocelot/felix.c b/drivers/net/dsa/ocelot/felix.c index 4272ea6e9ca8..84a95007825f 100644 --- a/drivers/net/dsa/ocelot/felix.c +++ b/drivers/net/dsa/ocelot/felix.c @@ -2446,12 +2446,14 @@ struct net_device *felix_port_to_netdev(struct ocel= ot *ocelot, int port) } EXPORT_SYMBOL_GPL(felix_port_to_netdev); =20 -int felix_netdev_to_port(struct net_device *dev) +int felix_netdev_to_port(struct ocelot *ocelot, struct net_device *dev) { + struct felix *felix =3D ocelot_to_felix(ocelot); + struct dsa_switch *ds =3D felix->ds; struct dsa_port *dp; =20 dp =3D dsa_port_from_netdev(dev); - if (IS_ERR(dp)) + if (IS_ERR(dp) || dp->ds !=3D ds) return -EINVAL; =20 return dp->index; diff --git a/drivers/net/dsa/ocelot/felix.h b/drivers/net/dsa/ocelot/felix.h index a657b190c5d7..19addcfd62be 100644 --- a/drivers/net/dsa/ocelot/felix.h +++ b/drivers/net/dsa/ocelot/felix.h @@ -104,6 +104,6 @@ int felix_register_switch(struct device *dev, resource_= size_t switch_base, enum dsa_tag_protocol init_tag_proto, const struct felix_info *info); struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port); -int felix_netdev_to_port(struct net_device *dev); +int felix_netdev_to_port(struct ocelot *ocelot, struct net_device *dev); =20 #endif diff --git a/drivers/net/ethernet/mscc/ocelot.h b/drivers/net/ethernet/mscc= /ocelot.h index e50be508c166..42d2c456f712 100644 --- a/drivers/net/ethernet/mscc/ocelot.h +++ b/drivers/net/ethernet/mscc/ocelot.h @@ -92,7 +92,7 @@ int ocelot_mact_learn(struct ocelot *ocelot, int port, int ocelot_mact_forget(struct ocelot *ocelot, const unsigned char mac[ETH_ALEN], unsigned int vid); struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port); -int ocelot_netdev_to_port(struct net_device *dev); +int ocelot_netdev_to_port(struct ocelot *ocelot, struct net_device *dev); =20 int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *targ= et, struct device_node *portnp); diff --git a/drivers/net/ethernet/mscc/ocelot_flower.c b/drivers/net/ethern= et/mscc/ocelot_flower.c index 844292eb7422..c5983c56a544 100644 --- a/drivers/net/ethernet/mscc/ocelot_flower.c +++ b/drivers/net/ethernet/mscc/ocelot_flower.c @@ -233,8 +233,8 @@ ocelot_flower_parse_egress_port(struct ocelot *ocelot, = struct flow_cls_offload * const struct flow_action_entry *a, bool mirror, struct netlink_ext_ack *extack) { + int egress_port =3D ocelot->ops->netdev_to_port(ocelot, a->dev); const char *act_string =3D mirror ? "mirror" : "redirect"; - int egress_port =3D ocelot->ops->netdev_to_port(a->dev); enum flow_action_id offloadable_act_id; =20 offloadable_act_id =3D mirror ? FLOW_ACTION_MIRRED : FLOW_ACTION_REDIRECT; @@ -580,7 +580,7 @@ static int ocelot_flower_parse_indev(struct ocelot *oce= lot, int port, return -ENOENT; } =20 - ingress_port =3D ocelot->ops->netdev_to_port(indev); + ingress_port =3D ocelot->ops->netdev_to_port(ocelot, indev); if (ingress_port < 0) { NL_SET_ERR_MSG_MOD(extack, "Can only offload an ocelot ingress port"); diff --git a/drivers/net/ethernet/mscc/ocelot_net.c b/drivers/net/ethernet/= mscc/ocelot_net.c index 1b8269320464..4d45e136f08f 100644 --- a/drivers/net/ethernet/mscc/ocelot_net.c +++ b/drivers/net/ethernet/mscc/ocelot_net.c @@ -984,7 +984,7 @@ static bool ocelot_netdevice_dev_check(const struct net= _device *dev) return dev->netdev_ops =3D=3D &ocelot_port_netdev_ops; } =20 -int ocelot_netdev_to_port(struct net_device *dev) +int ocelot_netdev_to_port(struct ocelot *ocelot, struct net_device *dev) { struct ocelot_port_private *priv; =20 @@ -992,6 +992,8 @@ int ocelot_netdev_to_port(struct net_device *dev) return -EINVAL; =20 priv =3D netdev_priv(dev); + if (priv->port.ocelot !=3D ocelot) + return -EINVAL; =20 return priv->port.index; } diff --git a/include/soc/mscc/ocelot.h b/include/soc/mscc/ocelot.h index 48d6deb3efd7..05a8191b148e 100644 --- a/include/soc/mscc/ocelot.h +++ b/include/soc/mscc/ocelot.h @@ -650,7 +650,7 @@ struct device_node; =20 struct ocelot_ops { struct net_device *(*port_to_netdev)(struct ocelot *ocelot, int port); - int (*netdev_to_port)(struct net_device *dev); + int (*netdev_to_port)(struct ocelot *ocelot, struct net_device *dev); int (*reset)(struct ocelot *ocelot); u16 (*wm_enc)(u16 value); u16 (*wm_dec)(u16 value); --=20 2.53.0