Skip to contentSkip to conversation

Adopting nlmsg_payload() in IPv4/IPv6

PATCH Applied Breno Leitao 3 participants 19 messages Apr 15, 2025 – Apr 17, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Adopting nlmsg_payload() in IPv4/IPv6

The commit 95d06e92a4019 ("netlink: Introduce nlmsg_payload helper") introduced the nlmsg_payload() helper function.

This patchset aims to replace manual implementations with the nlmsg_payload() helper in IPv4 and IPv6 files, one file per patch.

---
Breno Leitao (8):
ipv6: Use nlmsg_payload in addrlabel file
ipv6: Use nlmsg_payload in addrconf file
ipv6: Use nlmsg_payload in route file
ipv4: Use nlmsg_payload in devinet file
ipv4: Use nlmsg_payload in fib_frontend file
ipv4: Use nlmsg_payload in route file
ipv4: Use nlmsg_payload in ipmr file
vxlan: Use nlmsg_payload in vxlan_vnifilter_dump

 drivers/net/vxlan/vxlan_vnifilter.c | 5 ++---
 net/ipv4/devinet.c                  | 4 ++--
 net/ipv4/fib_frontend.c             | 4 ++--
 net/ipv4/ipmr.c                     | 8 ++++----
 net/ipv4/route.c                    | 4 ++--
 net/ipv6/addrconf.c                 | 4 ++--
 net/ipv6/addrlabel.c                | 8 ++++----
 net/ipv6/route.c                    | 4 ++--
 8 files changed, 20 insertions(+), 21 deletions(-)

---
base-commit: 96f8bf85d11acdd3ebc9a91f41cb18eac6f8e26d
change-id: 20250415-nlmsg_v2-204ca5de7791

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
#1 lore raw

PATCH 1/8 ipv6: Use nlmsg_payload in addrlabel file

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

This changes function ip6addrlbl_valid_get_req() and ip6addrlbl_valid_dump_req().

---
 net/ipv6/addrlabel.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/ipv6/addrlabel.c +4 -4
index ab054f329e12d..fb63ffbcfc647 100644
@@ -473,12 +473,12 @@ static int ip6addrlbl_valid_dump_req(const struct nlmsghdr *nlh,
 {
 	struct ifaddrlblmsg *ifal;
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) {
+	ifal = nlmsg_payload(nlh, sizeof(*ifal));
+	if (!ifal) {
 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for address label dump request");
 		return -EINVAL;
 	}
 
-	ifal = nlmsg_data(nlh);
 	if (ifal->__ifal_reserved || ifal->ifal_prefixlen ||
 	    ifal->ifal_flags || ifal->ifal_index || ifal->ifal_seq) {
 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address label dump request");
@@ -543,7 +543,8 @@ static int ip6addrlbl_valid_get_req(struct sk_buff *skb,
 	struct ifaddrlblmsg *ifal;
 	int i, err;
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifal))) {
+	ifal = nlmsg_payload(nlh, sizeof(*ifal));
+	if (!ifal) {
 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for addrlabel get request");
 		return -EINVAL;
 	}
@@ -552,7 +553,6 @@ static int ip6addrlbl_valid_get_req(struct sk_buff *skb,
 		return nlmsg_parse_deprecated(nlh, sizeof(*ifal), tb,
 					      IFAL_MAX, ifal_policy, extack);
 
-	ifal = nlmsg_data(nlh);
 	if (ifal->__ifal_reserved || ifal->ifal_flags || ifal->ifal_seq) {
 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for addrlabel get request");
 		return -EINVAL;
Signature · 1 line
2.47.1
Reviewed-by on v1 1/8
#2 lore raw

Re: [PATCH net-next 1/8] ipv6: Use nlmsg_payload in addrlabel file

From: Breno Leitao <[email protected]>
Date: Tue, 15 Apr 2025 12:28:52 -0700

Quoted 7 lines

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

This changes function ip6addrlbl_valid_get_req() and ip6addrlbl_valid_dump_req().

#3 lore raw

PATCH 2/8 ipv6: Use nlmsg_payload in addrconf file

In reply to Breno Leitao

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

---
 net/ipv6/addrconf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv6/addrconf.c +2 -2
index 4af2761795428..ec6c4ca6d6d99 100644
@@ -6112,7 +6112,8 @@ static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
 {
 	struct ifinfomsg *ifm;
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
+	ifm = nlmsg_payload(nlh, sizeof(*ifm));
+	if (!ifm) {
 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
 		return -EINVAL;
 	}
@@ -6122,7 +6123,6 @@ static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
 		return -EINVAL;
 	}
 
-	ifm = nlmsg_data(nlh);
 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
 	    ifm->ifi_change || ifm->ifi_index) {
 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
Signature · 1 line
2.47.1
#5 lore raw

PATCH 3/8 ipv6: Use nlmsg_payload in route file

In reply to Breno Leitao

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

---
 net/ipv6/route.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv6/route.c +2 -2
index 210b84cecc242..e2c6c0b0684b2 100644
@@ -6029,7 +6029,8 @@ static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
 	struct rtmsg *rtm;
 	int i, err;
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
+	rtm = nlmsg_payload(nlh, sizeof(*rtm));
+	if (!rtm) {
 		NL_SET_ERR_MSG_MOD(extack,
 				   "Invalid header for get route request");
 		return -EINVAL;
@@ -6039,7 +6040,6 @@ static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
 					      rtm_ipv6_policy, extack);
 
-	rtm = nlmsg_data(nlh);
 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
Signature · 1 line
2.47.1
#7 lore raw

PATCH 4/8 ipv4: Use nlmsg_payload in devinet file

In reply to Breno Leitao

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

---
 net/ipv4/devinet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/devinet.c +2 -2
index 77e5705ac799e..c47d3828d4f65 100644
@@ -1792,12 +1792,12 @@ static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
 	struct ifaddrmsg *ifm;
 	int err, i;
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
+	ifm = nlmsg_payload(nlh, sizeof(*ifm));
+	if (!ifm) {
 		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
 		return -EINVAL;
 	}
 
-	ifm = nlmsg_data(nlh);
 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
 		NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
 		return -EINVAL;
Signature · 1 line
2.47.1
#9 lore raw

PATCH 5/8 ipv4: Use nlmsg_payload in fib_frontend file

In reply to Breno Leitao

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

---
 net/ipv4/fib_frontend.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/fib_frontend.c +2 -2
index 3f4e629998fab..57f088e5540e2 100644
@@ -948,12 +948,12 @@ int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
 	if (filter->rtnl_held)
 		ASSERT_RTNL();
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
+	rtm = nlmsg_payload(nlh, sizeof(*rtm));
+	if (!rtm) {
 		NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request");
 		return -EINVAL;
 	}
 
-	rtm = nlmsg_data(nlh);
 	if (rtm->rtm_dst_len || rtm->rtm_src_len  || rtm->rtm_tos   ||
 	    rtm->rtm_scope) {
 		NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request");
Signature · 1 line
2.47.1
#11 lore raw

PATCH 6/8 ipv4: Use nlmsg_payload in route file

In reply to Breno Leitao

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

---
 net/ipv4/route.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/route.c +2 -2
index 22dfc971aab4b..49cffbe838029 100644
@@ -3205,7 +3205,8 @@ static int inet_rtm_valid_getroute_req(struct sk_buff *skb,
 	struct rtmsg *rtm;
 	int i, err;
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
+	rtm = nlmsg_payload(nlh, sizeof(*rtm));
+	if (!rtm) {
 		NL_SET_ERR_MSG(extack,
 			       "ipv4: Invalid header for route get request");
 		return -EINVAL;
@@ -3215,7 +3216,6 @@ static int inet_rtm_valid_getroute_req(struct sk_buff *skb,
 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
 					      rtm_ipv4_policy, extack);
 
-	rtm = nlmsg_data(nlh);
 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
 	    rtm->rtm_table || rtm->rtm_protocol ||
Signature · 1 line
2.47.1
#13 lore raw

PATCH 7/8 ipv4: Use nlmsg_payload in ipmr file

In reply to Breno Leitao

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

---
 net/ipv4/ipmr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/ipv4/ipmr.c +4 -4
index a8b04d4abcaae..3b5677d8467ef 100644
@@ -2511,7 +2511,8 @@ static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
 	struct rtmsg *rtm;
 	int i, err;
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
+	rtm = nlmsg_payload(nlh, sizeof(*rtm));
+	if (!rtm) {
 		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
 		return -EINVAL;
 	}
@@ -2520,7 +2521,6 @@ static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
 					      rtm_ipv4_policy, extack);
 
-	rtm = nlmsg_data(nlh);
 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
 	    rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
@@ -2836,7 +2836,8 @@ static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
 {
 	struct ifinfomsg *ifm;
 
-	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
+	ifm = nlmsg_payload(nlh, sizeof(*ifm));
+	if (!ifm) {
 		NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
 		return -EINVAL;
 	}
@@ -2846,7 +2847,6 @@ static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
 		return -EINVAL;
 	}
 
-	ifm = nlmsg_data(nlh);
 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
 	    ifm->ifi_change || ifm->ifi_index) {
 		NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
Signature · 1 line
2.47.1
#15 lore raw

PATCH 8/8 vxlan: Use nlmsg_payload in vxlan_vnifilter_dump

In reply to Breno Leitao

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

---
 drivers/net/vxlan/vxlan_vnifilter.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/net/vxlan/vxlan_vnifilter.c +2 -3
index 6e6e9f05509ab..d0753776d3394 100644
@@ -411,13 +411,12 @@ static int vxlan_vnifilter_dump(struct sk_buff *skb, struct netlink_callback *cb
 	struct tunnel_msg *tmsg;
 	struct net_device *dev;
 
-	if (cb->nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct tunnel_msg))) {
+	tmsg = nlmsg_payload(cb->nlh, sizeof(*tmsg));
+	if (!tmsg) {
 		NL_SET_ERR_MSG(cb->extack, "Invalid msg length");
 		return -EINVAL;
 	}
 
-	tmsg = nlmsg_data(cb->nlh);
-
 	if (tmsg->flags & ~TUNNEL_MSG_VALID_USER_FLAGS) {
 		NL_SET_ERR_MSG(cb->extack, "Invalid tunnelmsg flags in ancillary header");
 		return -EINVAL;
Signature · 1 line
2.47.1
Reviewed-by on v1 8/8
#16 lore raw

Re: [PATCH net-next 8/8] vxlan: Use nlmsg_payload in vxlan_vnifilter_dump

From: Breno Leitao <[email protected]>
Date: Tue, 15 Apr 2025 12:28:59 -0700

Quoted 4 lines

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

Just grepped nlmsg_msg_size() and looks like the next series is last :)

neigh_valid_dump_req
rtnl_valid_dump_ifinfo_req
rtnl_valid_getlink_req
valid_fdb_get_strict
valid_bridge_getlink_req
rtnl_valid_stats_req
rtnl_mdb_valid_dump_req

#17 lore raw

Re: [PATCH net-next 8/8] vxlan: Use nlmsg_payload in vxlan_vnifilter_dump

Hello Kuniyuki,

Quoted 11 lines · Kuniyuki Iwashima

From: Breno Leitao <[email protected]>
Date: Tue, 15 Apr 2025 12:28:59 -0700

Quoted 4 lines

Leverage the new nlmsg_payload() helper to avoid checking for message size and then reading the nlmsg data.

Just grepped nlmsg_msg_size() and looks like the next series is last :)

Quoted 7 lines

neigh_valid_dump_req
rtnl_valid_dump_ifinfo_req
rtnl_valid_getlink_req
valid_fdb_get_strict
valid_bridge_getlink_req
rtnl_valid_stats_req
rtnl_mdb_valid_dump_req

Right, that matches my list as well.

Thanks for all this review,
--breno

#18 lore raw
In reply to Breno Leitao
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Tue, 15 Apr 2025 12:28:51 -0700 you wrote:

The commit 95d06e92a4019 ("netlink: Introduce nlmsg_payload helper") introduced the nlmsg_payload() helper function.

This patchset aims to replace manual implementations with the nlmsg_payload() helper in IPv4 and IPv6 files, one file per patch.

[...]

Here is the summary with links:
- [net-next,1/8] ipv6: Use nlmsg_payload in addrlabel file

    https://git.kernel.org/netdev/net-next/c/5ef4097ed155
  - [net-next,2/8] ipv6: Use nlmsg_payload in addrconf file
    https://git.kernel.org/netdev/net-next/c/6c454270a851
  - [net-next,3/8] ipv6: Use nlmsg_payload in route file
    https://git.kernel.org/netdev/net-next/c/bc05add844fc
  - [net-next,4/8] ipv4: Use nlmsg_payload in devinet file
    https://git.kernel.org/netdev/net-next/c/7d82cc229c09
  - [net-next,5/8] ipv4: Use nlmsg_payload in fib_frontend file
    https://git.kernel.org/netdev/net-next/c/b411638fb925
  - [net-next,6/8] ipv4: Use nlmsg_payload in route file
    https://git.kernel.org/netdev/net-next/c/d5ce0ed528c4
  - [net-next,7/8] ipv4: Use nlmsg_payload in ipmr file
    https://git.kernel.org/netdev/net-next/c/04e00a849e7c
  - [net-next,8/8] vxlan: Use nlmsg_payload in vxlan_vnifilter_dump
    https://git.kernel.org/netdev/net-next/c/9b1097a4108f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html