diff mbox series

[net-next] IPv4: add extack info for IPv4 address add/delete

Message ID 20230817032815.1675525-1-liuhangbin@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next] IPv4: add extack info for IPv4 address add/delete | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1332 this patch: 1332
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 1353 this patch: 1353
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1355 this patch: 1355
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns WARNING: line length of 96 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Hangbin Liu Aug. 17, 2023, 3:28 a.m. UTC
Add extack info for IPv4 address add/delete, which would be useful for
users to understand the problem without having to read kernel code.

No extack message for the ifa_local checking in __inet_insert_ifa() as
it has been checked in find_matching_ifa().

Suggested-by: Ido Schimmel <idosch@idosch.org>
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
 net/ipv4/devinet.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

Comments

Ido Schimmel Aug. 17, 2023, 3:06 p.m. UTC | #1
On Thu, Aug 17, 2023 at 11:28:15AM +0800, Hangbin Liu wrote:
> Add extack info for IPv4 address add/delete, which would be useful for
> users to understand the problem without having to read kernel code.
> 
> No extack message for the ifa_local checking in __inet_insert_ifa() as
> it has been checked in find_matching_ifa().
> 
> Suggested-by: Ido Schimmel <idosch@idosch.org>
> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>

Thanks for the follow-up. A couple of minor comments below.

> ---
>  net/ipv4/devinet.c | 31 +++++++++++++++++++++++++++----
>  1 file changed, 27 insertions(+), 4 deletions(-)
> 
> diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
> index 5deac0517ef7..40f90d01ce0e 100644
> --- a/net/ipv4/devinet.c
> +++ b/net/ipv4/devinet.c
> @@ -509,6 +509,7 @@ static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
>  				return -EEXIST;
>  			}
>  			if (ifa1->ifa_scope != ifa->ifa_scope) {
> +				NL_SET_ERR_MSG(extack, "ipv4: Invalid scope value");
>  				inet_free_ifa(ifa);
>  				return -EINVAL;
>  			}
> @@ -664,6 +665,7 @@ static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
>  	ifm = nlmsg_data(nlh);
>  	in_dev = inetdev_by_index(net, ifm->ifa_index);
>  	if (!in_dev) {
> +		NL_SET_ERR_MSG(extack, "ipv4: Device not found");
>  		err = -ENODEV;
>  		goto errout;
>  	}
> @@ -688,6 +690,7 @@ static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
>  		return 0;
>  	}
>  
> +	NL_SET_ERR_MSG(extack, "ipv4: Address not found");
>  	err = -EADDRNOTAVAIL;
>  errout:
>  	return err;
> @@ -839,13 +842,23 @@ static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
>  
>  	ifm = nlmsg_data(nlh);
>  	err = -EINVAL;
> -	if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
> +
> +	if (ifm->ifa_prefixlen > 32) {
> +		NL_SET_ERR_MSG(extack, "IPv4: Invalid prefix length");

s/IPv4/ipv4/

To be consistent with the rest and IPv6.

> +		goto errout;
> +	}
> +
> +	if (!tb[IFA_LOCAL]) {
> +		NL_SET_ERR_MSG(extack, "ipv4: Local address is not supplied");
>  		goto errout;
> +	}
>  
>  	dev = __dev_get_by_index(net, ifm->ifa_index);
>  	err = -ENODEV;
> -	if (!dev)
> +	if (!dev) {
> +		NL_SET_ERR_MSG(extack, "ipv4: Device not found");
>  		goto errout;
> +	}
>  
>  	in_dev = __in_dev_get_rtnl(dev);
>  	err = -ENOBUFS;
> @@ -896,7 +909,14 @@ static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
>  		struct ifa_cacheinfo *ci;
>  
>  		ci = nla_data(tb[IFA_CACHEINFO]);
> -		if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {

IPv6 just says for both "address lifetime invalid"

> +		if (!ci->ifa_valid) {
> +			NL_SET_ERR_MSG(extack, "ipv4: valid_lft is zero");
> +			err = -EINVAL;
> +			goto errout_free;
> +		}
> +
> +		if (ci->ifa_prefered > ci->ifa_valid) {
> +			NL_SET_ERR_MSG(extack, "ipv4: preferred_lft is greater than valid_lft");
>  			err = -EINVAL;
>  			goto errout_free;
>  		}
> @@ -954,6 +974,7 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
>  			int ret = ip_mc_autojoin_config(net, true, ifa);
>  
>  			if (ret < 0) {
> +				NL_SET_ERR_MSG(extack, "ipv4: Multicast auto join failed");
>  				inet_free_ifa(ifa);
>  				return ret;
>  			}
> @@ -967,8 +988,10 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
>  		inet_free_ifa(ifa);
>  
>  		if (nlh->nlmsg_flags & NLM_F_EXCL ||
> -		    !(nlh->nlmsg_flags & NLM_F_REPLACE))
> +		    !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
> +			NL_SET_ERR_MSG(extack, "ipv4: Address already assigned");
>  			return -EEXIST;
> +		}
>  		ifa = ifa_existing;
>  
>  		if (ifa->ifa_rt_priority != new_metric) {
> -- 
> 2.38.1
>
diff mbox series

Patch

diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index 5deac0517ef7..40f90d01ce0e 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -509,6 +509,7 @@  static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
 				return -EEXIST;
 			}
 			if (ifa1->ifa_scope != ifa->ifa_scope) {
+				NL_SET_ERR_MSG(extack, "ipv4: Invalid scope value");
 				inet_free_ifa(ifa);
 				return -EINVAL;
 			}
@@ -664,6 +665,7 @@  static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
 	ifm = nlmsg_data(nlh);
 	in_dev = inetdev_by_index(net, ifm->ifa_index);
 	if (!in_dev) {
+		NL_SET_ERR_MSG(extack, "ipv4: Device not found");
 		err = -ENODEV;
 		goto errout;
 	}
@@ -688,6 +690,7 @@  static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return 0;
 	}
 
+	NL_SET_ERR_MSG(extack, "ipv4: Address not found");
 	err = -EADDRNOTAVAIL;
 errout:
 	return err;
@@ -839,13 +842,23 @@  static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
 
 	ifm = nlmsg_data(nlh);
 	err = -EINVAL;
-	if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
+
+	if (ifm->ifa_prefixlen > 32) {
+		NL_SET_ERR_MSG(extack, "IPv4: Invalid prefix length");
+		goto errout;
+	}
+
+	if (!tb[IFA_LOCAL]) {
+		NL_SET_ERR_MSG(extack, "ipv4: Local address is not supplied");
 		goto errout;
+	}
 
 	dev = __dev_get_by_index(net, ifm->ifa_index);
 	err = -ENODEV;
-	if (!dev)
+	if (!dev) {
+		NL_SET_ERR_MSG(extack, "ipv4: Device not found");
 		goto errout;
+	}
 
 	in_dev = __in_dev_get_rtnl(dev);
 	err = -ENOBUFS;
@@ -896,7 +909,14 @@  static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
 		struct ifa_cacheinfo *ci;
 
 		ci = nla_data(tb[IFA_CACHEINFO]);
-		if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
+		if (!ci->ifa_valid) {
+			NL_SET_ERR_MSG(extack, "ipv4: valid_lft is zero");
+			err = -EINVAL;
+			goto errout_free;
+		}
+
+		if (ci->ifa_prefered > ci->ifa_valid) {
+			NL_SET_ERR_MSG(extack, "ipv4: preferred_lft is greater than valid_lft");
 			err = -EINVAL;
 			goto errout_free;
 		}
@@ -954,6 +974,7 @@  static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 			int ret = ip_mc_autojoin_config(net, true, ifa);
 
 			if (ret < 0) {
+				NL_SET_ERR_MSG(extack, "ipv4: Multicast auto join failed");
 				inet_free_ifa(ifa);
 				return ret;
 			}
@@ -967,8 +988,10 @@  static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 		inet_free_ifa(ifa);
 
 		if (nlh->nlmsg_flags & NLM_F_EXCL ||
-		    !(nlh->nlmsg_flags & NLM_F_REPLACE))
+		    !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
+			NL_SET_ERR_MSG(extack, "ipv4: Address already assigned");
 			return -EEXIST;
+		}
 		ifa = ifa_existing;
 
 		if (ifa->ifa_rt_priority != new_metric) {