From nobody Thu Nov 21 20:21:12 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7A7282C60 for ; Thu, 7 Nov 2024 06:45:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730961956; cv=none; b=kXexRHoumsbVzMUu8BaKkv4Fbt2iIgDbksI9K21gCaHkMDM4buozTi94D/sB7RFXyol/OGt6C64EoVyph416JofMMpzyvm8uazJdlm/Bm7lDWLxFyUdMoKdHGh0bKmK2junWhZOyJn2xFsD3OHfgMwv76Zb8p2pVmakhFA31xFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730961956; c=relaxed/simple; bh=iF+yVEngQsM8pod0s4X8LO+fbay/FWDDzOeidkhlsCM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CHFvdn4Tcjd/JT7/xuhq6zQ69MyNtzpZE9Coln3LkyVhaek3G3ouPgunTomssoRN33P5ujpgh9LQKSQqHcxqqukZqf8y1F+1ESaVhcuoZECfCkXotGCn/+1uUE5NVm89Ikoznw1kP7B9jV+ijZdNIV7QNnDs9ZWwEUmSSvmnp6g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LPmCrIz1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LPmCrIz1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C2E4C4CECC; Thu, 7 Nov 2024 06:45:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730961956; bh=iF+yVEngQsM8pod0s4X8LO+fbay/FWDDzOeidkhlsCM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LPmCrIz1hqK+64d3gY9QhWckg/Awq/d6QzOmN0diEo+uvvvqVAcEnth9IzZ6EfRaP m6XBkwboGn1+9sd3uDD7YvYaHbaStxI2BP185BXuw1l9o6HqyXhfVLXBGdvxu9l+HB HLticl4ZjuISmIHdMwWlGNNHQ7rmws5W6BLzKUsRGtzNuH81dGek1bG5ROYjOsMil/ BJVxJvNNYCVkj1Rtrz8PfP/Erl09Fd5GOGS5oL6fm17eRcL4LwgkcZ3dkHsF7Qfz9H RqBZ04eieEjPKr3f7FDca+5dYxmkP1eQdPAaAekbYDAAP2wiGhFOmS4zm/LmKXPTUz cxHrY7pKorydg== From: Geliang Tang To: mptcp@lists.linux.dev Cc: Geliang Tang Subject: [PATCH mptcp-next v3 3/9] mptcp: add mptcp_userspace_pm_get_sock helper Date: Thu, 7 Nov 2024 14:45:37 +0800 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Geliang Tang Each userspace pm netlink function uses nla_get_u32() to get the msk token value, then pass it to mptcp_token_get_sock() to get the msk. Finally check whether userspace PM is selected on this msk. It makes sense to wrap them into a helper, named mptcp_userspace_pm_get_sock(), to do this. This patch doesn't change the behaviour of the code, just refactoring. Signed-off-by: Geliang Tang --- net/mptcp/pm_userspace.c | 144 +++++++++++++-------------------------- 1 file changed, 47 insertions(+), 97 deletions(-) diff --git a/net/mptcp/pm_userspace.c b/net/mptcp/pm_userspace.c index c99ec28c1bd5..a6de837d8958 100644 --- a/net/mptcp/pm_userspace.c +++ b/net/mptcp/pm_userspace.c @@ -172,36 +172,50 @@ bool mptcp_userspace_pm_is_backup(struct mptcp_sock *= msk, return backup; } =20 -int mptcp_pm_nl_announce_doit(struct sk_buff *skb, struct genl_info *info) +static struct mptcp_sock *mptcp_userspace_pm_get_sock(const struct genl_in= fo *info) { struct nlattr *token =3D info->attrs[MPTCP_PM_ATTR_TOKEN]; + struct mptcp_sock *msk; + + if (!token) { + GENL_SET_ERR_MSG(info, "missing required token"); + return NULL; + } + + msk =3D mptcp_token_get_sock(genl_info_net(info), nla_get_u32(token)); + if (!msk) { + NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); + return NULL; + } + + if (!mptcp_pm_is_userspace(msk)) { + GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); + sock_put((struct sock *)msk); + return NULL; + } + + return msk; +} + +int mptcp_pm_nl_announce_doit(struct sk_buff *skb, struct genl_info *info) +{ struct nlattr *addr =3D info->attrs[MPTCP_PM_ATTR_ADDR]; struct mptcp_pm_addr_entry addr_val; struct mptcp_sock *msk; int err =3D -EINVAL; struct sock *sk; - u32 token_val; =20 - if (!addr || !token) { - GENL_SET_ERR_MSG(info, "missing required inputs"); + if (!addr) { + GENL_SET_ERR_MSG(info, "missing required address"); return err; } =20 - token_val =3D nla_get_u32(token); - - msk =3D mptcp_token_get_sock(sock_net(skb->sk), token_val); - if (!msk) { - NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); + msk =3D mptcp_userspace_pm_get_sock(info); + if (!msk) return err; - } =20 sk =3D (struct sock *)msk; =20 - if (!mptcp_pm_is_userspace(msk)) { - GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); - goto announce_err; - } - err =3D mptcp_pm_parse_entry(addr, info, true, &addr_val); if (err < 0) { GENL_SET_ERR_MSG(info, "error parsing local address"); @@ -274,7 +288,6 @@ static int mptcp_userspace_pm_remove_id_zero_address(st= ruct mptcp_sock *msk, =20 int mptcp_pm_nl_remove_doit(struct sk_buff *skb, struct genl_info *info) { - struct nlattr *token =3D info->attrs[MPTCP_PM_ATTR_TOKEN]; struct nlattr *id =3D info->attrs[MPTCP_PM_ATTR_LOC_ID]; struct mptcp_pm_addr_entry *match; struct mptcp_pm_addr_entry *entry; @@ -282,30 +295,21 @@ int mptcp_pm_nl_remove_doit(struct sk_buff *skb, stru= ct genl_info *info) LIST_HEAD(free_list); int err =3D -EINVAL; struct sock *sk; - u32 token_val; u8 id_val; =20 - if (!id || !token) { - GENL_SET_ERR_MSG(info, "missing required inputs"); + if (!id) { + GENL_SET_ERR_MSG(info, "missing required ID"); return err; } =20 id_val =3D nla_get_u8(id); - token_val =3D nla_get_u32(token); =20 - msk =3D mptcp_token_get_sock(sock_net(skb->sk), token_val); - if (!msk) { - NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); + msk =3D mptcp_userspace_pm_get_sock(info); + if (!msk) return err; - } =20 sk =3D (struct sock *)msk; =20 - if (!mptcp_pm_is_userspace(msk)) { - GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); - goto out; - } - if (id_val =3D=3D 0) { err =3D mptcp_userspace_pm_remove_id_zero_address(msk, info); goto out; @@ -342,7 +346,6 @@ int mptcp_pm_nl_remove_doit(struct sk_buff *skb, struct= genl_info *info) int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info = *info) { struct nlattr *raddr =3D info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE]; - struct nlattr *token =3D info->attrs[MPTCP_PM_ATTR_TOKEN]; struct nlattr *laddr =3D info->attrs[MPTCP_PM_ATTR_ADDR]; struct mptcp_pm_addr_entry entry =3D { 0 }; struct mptcp_addr_info addr_r; @@ -350,28 +353,18 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *s= kb, struct genl_info *info) struct mptcp_sock *msk; int err =3D -EINVAL; struct sock *sk; - u32 token_val; =20 - if (!laddr || !raddr || !token) { - GENL_SET_ERR_MSG(info, "missing required inputs"); + if (!laddr || !raddr) { + GENL_SET_ERR_MSG(info, "missing required address(es)"); return err; } =20 - token_val =3D nla_get_u32(token); - - msk =3D mptcp_token_get_sock(genl_info_net(info), token_val); - if (!msk) { - NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); + msk =3D mptcp_userspace_pm_get_sock(info); + if (!msk) return err; - } =20 sk =3D (struct sock *)msk; =20 - if (!mptcp_pm_is_userspace(msk)) { - GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); - goto create_err; - } - err =3D mptcp_pm_parse_entry(laddr, info, true, &entry); if (err < 0) { NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr"); @@ -474,35 +467,24 @@ static struct sock *mptcp_nl_find_ssk(struct mptcp_so= ck *msk, int mptcp_pm_nl_subflow_destroy_doit(struct sk_buff *skb, struct genl_info= *info) { struct nlattr *raddr =3D info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE]; - struct nlattr *token =3D info->attrs[MPTCP_PM_ATTR_TOKEN]; struct nlattr *laddr =3D info->attrs[MPTCP_PM_ATTR_ADDR]; struct mptcp_addr_info addr_l; struct mptcp_addr_info addr_r; struct mptcp_sock *msk; struct sock *sk, *ssk; int err =3D -EINVAL; - u32 token_val; =20 - if (!laddr || !raddr || !token) { - GENL_SET_ERR_MSG(info, "missing required inputs"); + if (!laddr || !raddr) { + GENL_SET_ERR_MSG(info, "missing required address(es)"); return err; } =20 - token_val =3D nla_get_u32(token); - - msk =3D mptcp_token_get_sock(genl_info_net(info), token_val); - if (!msk) { - NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); + msk =3D mptcp_userspace_pm_get_sock(info); + if (!msk) return err; - } =20 sk =3D (struct sock *)msk; =20 - if (!mptcp_pm_is_userspace(msk)) { - GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); - goto destroy_err; - } - err =3D mptcp_pm_parse_addr(laddr, info, &addr_l); if (err < 0) { NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr"); @@ -565,31 +547,19 @@ int mptcp_userspace_pm_set_flags(struct sk_buff *skb,= struct genl_info *info) struct mptcp_pm_addr_entry loc =3D { .addr =3D { .family =3D AF_UNSPEC },= }; struct mptcp_pm_addr_entry rem =3D { .addr =3D { .family =3D AF_UNSPEC },= }; struct nlattr *attr_rem =3D info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE]; - struct nlattr *token =3D info->attrs[MPTCP_PM_ATTR_TOKEN]; struct nlattr *attr =3D info->attrs[MPTCP_PM_ATTR_ADDR]; - struct net *net =3D sock_net(skb->sk); struct mptcp_pm_addr_entry *entry; struct mptcp_sock *msk; int ret =3D -EINVAL; struct sock *sk; - u32 token_val; u8 bkup =3D 0; =20 - token_val =3D nla_get_u32(token); - - msk =3D mptcp_token_get_sock(net, token_val); - if (!msk) { - NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); + msk =3D mptcp_userspace_pm_get_sock(info); + if (!msk) return ret; - } =20 sk =3D (struct sock *)msk; =20 - if (!mptcp_pm_is_userspace(msk)) { - GENL_SET_ERR_MSG(info, "userspace PM not selected"); - goto set_flags_err; - } - ret =3D mptcp_pm_parse_entry(attr, info, false, &loc); if (ret < 0) goto set_flags_err; @@ -636,30 +606,20 @@ int mptcp_userspace_pm_dump_addr(struct sk_buff *msg, DECLARE_BITMAP(map, MPTCP_PM_MAX_ADDR_ID + 1); } *bitmap; const struct genl_info *info =3D genl_info_dump(cb); - struct net *net =3D sock_net(msg->sk); struct mptcp_pm_addr_entry *entry; struct mptcp_sock *msk; - struct nlattr *token; int ret =3D -EINVAL; struct sock *sk; void *hdr; =20 bitmap =3D (struct id_bitmap *)cb->ctx; - token =3D info->attrs[MPTCP_PM_ATTR_TOKEN]; =20 - msk =3D mptcp_token_get_sock(net, nla_get_u32(token)); - if (!msk) { - NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); + msk =3D mptcp_userspace_pm_get_sock(info); + if (!msk) return ret; - } =20 sk =3D (struct sock *)msk; =20 - if (!mptcp_pm_is_userspace(msk)) { - GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); - goto out; - } - lock_sock(sk); spin_lock_bh(&msk->pm.lock); mptcp_for_each_userspace_pm_addr(msk, entry) { @@ -684,7 +644,6 @@ int mptcp_userspace_pm_dump_addr(struct sk_buff *msg, release_sock(sk); ret =3D msg->len; =20 -out: sock_put(sk); return ret; } @@ -693,28 +652,19 @@ int mptcp_userspace_pm_get_addr(struct sk_buff *skb, struct genl_info *info) { struct nlattr *attr =3D info->attrs[MPTCP_PM_ENDPOINT_ADDR]; - struct nlattr *token =3D info->attrs[MPTCP_PM_ATTR_TOKEN]; struct mptcp_pm_addr_entry addr, *entry; - struct net *net =3D sock_net(skb->sk); struct mptcp_sock *msk; struct sk_buff *msg; int ret =3D -EINVAL; struct sock *sk; void *reply; =20 - msk =3D mptcp_token_get_sock(net, nla_get_u32(token)); - if (!msk) { - NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); + msk =3D mptcp_userspace_pm_get_sock(info); + if (!msk) return ret; - } =20 sk =3D (struct sock *)msk; =20 - if (!mptcp_pm_is_userspace(msk)) { - GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); - goto out; - } - ret =3D mptcp_pm_parse_entry(attr, info, false, &addr); if (ret < 0) goto out; --=20 2.45.2