net/ipv4: return the real errno instead of -EINVAL
Subsystems (3)
PATCH 1/1 net/ipv4: return the real errno instead of -EINVAL
From: xu xin <[email protected]>
For now, no matter what error pointer ip_neigh_for_gw() returns, ip_finish_output2() always return -EINVAL, which may mislead the upper users.
For exemple, an application uses sendto to send an UDP packet, but when the neighbor table overflows, sendto() will get a value of -EINVAL, and it will cause users to waste a lot of time checking parameters for errors.
Return the real errno instead of -EINVAL.
--- net/ipv4/ip_output.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
net/ipv4/ip_output.c +4 -5
@@ -201,6 +201,7 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *sunsigned int hh_len =LL_RESERVED_SPACE (dev);struct neighbour *neigh;bool is_v6gw = false; +int res;if (rt->rt_type ==RTN_MULTICAST ) {IP_UPD_PO_STATS (net,IPSTATS_MIB_OUTMCAST , skb->len);
@@ -214,8 +215,7 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *s }if (lwtunnel_xmit_redirect (dst->lwtstate)) { -int res =lwtunnel_xmit (skb); - + res =lwtunnel_xmit (skb);if (res <0 || res ==LWTUNNEL_XMIT_DONE )return res; }
@@ -223,8 +223,6 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *srcu_read_lock (); neigh =ip_neigh_for_gw (rt, skb, &is_v6gw);if (!IS_ERR (neigh)) { -int res; -sock_confirm_neigh (skb, neigh);/* if crossing protocols, can not use the cached header */ res =neigh_output (neigh, skb, is_v6gw);
@@ -236,7 +234,8 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *snet_dbg_ratelimited ("%s: No header cache and no neighbour!\n" , __func__);kfree_skb_reason (skb,SKB_DROP_REASON_NEIGH_CREATEFAIL ); -return -EINVAL ; + res =PTR_ERR (neigh); +return res; } static int ip_finish_output_gso(struct net *net, struct sock *sk,
Signature · 1 line
2.15.2
Quoted 53 lines · On 04/08/2023 09:48, [email protected] wrote:
From: xu xin <[email protected]>
For now, no matter what error pointer
ip_neigh_for_gw()returns,ip_finish_output2()always return-EINVAL, which may mislead the upper users.For exemple, an application uses sendto to send an UDP packet, but when the neighbor table overflows,
sendto()will get a value of-EINVAL, and it will cause users to waste a lot of time checking parameters for errors.Return the real errno instead of
-EINVAL.Signed-off-by xu xin <[email protected]>Reviewed-by Yang Yang <[email protected]>Cc Si Hao <[email protected]>Cc Dai Shixin <[email protected]>Cc Jiang Xuexin <[email protected]>--- net/ipv4/ip_output.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)net/ipv4/ip_output.c +2
-4@@ -201,6 +201,7 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *sunsigned int hh_len =LL_RESERVED_SPACE (dev);struct neighbour *neigh;bool is_v6gw = false; +int res;if (rt->rt_type ==RTN_MULTICAST ) {IP_UPD_PO_STATS (net,IPSTATS_MIB_OUTMCAST , skb->len);@@ -214,8 +215,7 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *s }if (lwtunnel_xmit_redirect (dst->lwtstate)) { -int res =lwtunnel_xmit (skb); - + res =lwtunnel_xmit (skb);if (res <0 || res ==LWTUNNEL_XMIT_DONE )return res; }@@ -223,8 +223,6 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *srcu_read_lock (); neigh =ip_neigh_for_gw (rt, skb, &is_v6gw);if (!IS_ERR (neigh)) { -int res; -sock_confirm_neigh (skb, neigh);/* if crossing protocols, can not use the cached header */ res =neigh_output (neigh, skb, is_v6gw);
net/ipv4/ip_output.c static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *s · line 238
Quoted 6 lines
net/ipv4/ip_output.c +2
-1@@ -236,7 +234,8 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *snet_dbg_ratelimited ("%s: No header cache and no neighbour!\n" , __func__);kfree_skb_reason (skb,SKB_DROP_REASON_NEIGH_CREATEFAIL ); -return -EINVAL ; + res =PTR_ERR (neigh); +return res;
I believe this part could be written as
- return -EINVAL;
+ return PTR_ERR(neigh);
and there is no need to change the code is other places. This will be easier to track/backport.
Quoted 3 lines
net/ipv4/ip_output.c +0
-0} static int ip_finish_output_gso(struct net *net, struct sock *sk,
Quoted 14 lines
net/ipv4/ip_output.c static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *s · line 238
Quoted 7 lines
net/ipv4/ip_output.c +2
-1@@ -236,7 +234,8 @@ static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *snet_dbg_ratelimited ("%s: No header cache and no neighbour!\n" , __func__);kfree_skb_reason (skb,SKB_DROP_REASON_NEIGH_CREATEFAIL ); -return -EINVAL ; + res =PTR_ERR (neigh); +return res;I believe this part could be written as
(quoted diff) +1
-1- return -EINVAL; + return PTR_ERR(neigh);
Quoted 2 lines
and there is no need to change the code is other places. This will be easier to track/backport.
Sure, I initially wrote the same thing with you, but considering the convenience of maintainer review, I deliberately modified some other parts of the code to present a clear context of the core modifications.
From your reply, I can see you agree with my key idea of this patch, so I have sent a v2 patch[1] according to your view.