ipv4: Use ipv4_neigh_lookup() and friends.

It is error-prone to require callers to fetch netns and
neigh_table and pass it with dev to neigh_lookup(), etc.

Let's replace neigh_lookup() and friends with IPv4 helpers.

Note that __neigh_lookup(create=false) is replaced with
ipv4_neigh_lookup() and __neigh_lookup(create=true) and
__neigh_lookup_errno() are split into ipv4_neigh_lookup()
and ipv4_neigh_create().

Fixes: 2430df635a ("ipv4: Replace &arp_tbl with arp_table(net).")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Fernando Fernandez Mancera <fmancera@suse.de>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
Link: https://patch.msgid.link/20261003212336.1304988-8-kuniyu@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Kuniyuki Iwashima 2026-10-03 21:23:20 +00:00 committed by Jakub Kicinski
parent 0e59523b44
commit f2f25d59f5
14 changed files with 42 additions and 59 deletions

View File

@ -683,8 +683,7 @@ __prestera_k_arb_n_offload_set(struct prestera_switch *sw,
{
struct neighbour *n;
n = neigh_lookup(arp_table(&init_net), &nc->key.addr.u.ipv4,
nc->key.dev);
n = ipv4_neigh_lookup(nc->key.dev, &nc->key.addr.u.ipv4);
if (!n)
return;
@ -790,7 +789,7 @@ __prestera_k_arb_nc_kern_n_fetch(struct prestera_switch *sw,
int err;
memset(&nc->nh_neigh_info, 0, sizeof(nc->nh_neigh_info));
n = neigh_lookup(arp_table(&init_net), &nc->key.addr.u.ipv4, nc->key.dev);
n = ipv4_neigh_lookup(nc->key.dev, &nc->key.addr.u.ipv4);
if (!n)
goto out;
@ -1052,13 +1051,10 @@ static void __prestera_k_arb_hw_state_upd(struct prestera_switch *sw,
#endif /* PRESTERA_IMPLICITY_RESOLVE_DEAD_NEIGH */
if (nc->key.addr.v == PRESTERA_IPV4) {
struct neigh_table *tbl = arp_table(&init_net);
n = neigh_lookup(tbl, &nc->key.addr.u.ipv4,
nc->key.dev);
n = ipv4_neigh_lookup(nc->key.dev, &nc->key.addr.u.ipv4);
if (!n)
n = neigh_create(tbl, &nc->key.addr.u.ipv4,
nc->key.dev);
n = ipv4_neigh_create(nc->key.dev,
&nc->key.addr.u.ipv4);
} else {
n = NULL;
}

View File

@ -438,7 +438,6 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
if (neigh_used) {
struct net_device *dev = READ_ONCE(nhe->neigh_dev);
struct net *net = dev_net(dev);
nhe->reported_lastuse = jiffies;
@ -450,7 +449,7 @@ void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
n = ipv6_neigh_lookup(dev, &m_neigh->dst_ip);
else
#endif
n = neigh_lookup(arp_table(net), &m_neigh->dst_ip, dev);
n = ipv4_neigh_lookup(dev, &m_neigh->dst_ip);
if (!n)
return;

View File

@ -262,7 +262,6 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,
struct net_device *netdev = sa_entry->dev;
struct xfrm_state *x = sa_entry->x;
struct dst_entry *rt_dst_entry;
struct neigh_table *tbl;
struct flowi4 fl4 = {};
struct flowi6 fl6 = {};
struct neighbour *n;
@ -365,10 +364,9 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry,
return;
neigh:
tbl = arp_table(dev_net(netdev));
n = neigh_lookup(tbl, pkey, netdev);
n = ipv4_neigh_lookup(netdev, pkey);
if (!n) {
n = neigh_create(tbl, pkey, netdev);
n = ipv4_neigh_create(netdev, pkey);
if (IS_ERR(n))
return;
neigh_event_send(n, NULL);

View File

@ -2416,8 +2416,6 @@ static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
int ent_index)
{
u64 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
struct net *net = mlxsw_sp_net(mlxsw_sp);
struct neigh_table *tbl;
struct net_device *dev;
struct neighbour *n;
__be32 dipn;
@ -2433,10 +2431,9 @@ static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
return;
}
tbl = arp_table(net);
dipn = htonl(dip);
dev = mlxsw_sp_rif_dev(mlxsw_sp->router->rifs[rif]);
n = neigh_lookup(tbl, &dipn, dev);
n = ipv4_neigh_lookup(dev, &dipn);
if (!n)
return;
@ -4301,13 +4298,10 @@ static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
static struct neighbour *
mlxsw_sp_nexthop_neigh_lookup(struct mlxsw_sp_nexthop *nh)
{
struct neigh_table *tbl;
struct net_device *dev;
struct neighbour *n;
struct net *net;
dev = mlxsw_sp_nexthop_dev(nh);
net = dev_net(dev);
#if IS_ENABLED(CONFIG_IPV6)
if (nh->family == AF_INET6) {
@ -4320,10 +4314,9 @@ mlxsw_sp_nexthop_neigh_lookup(struct mlxsw_sp_nexthop *nh)
} else
#endif
{
tbl = arp_table(net);
n = neigh_lookup(tbl, &nh->gw_addr, dev);
n = ipv4_neigh_lookup(dev, &nh->gw_addr);
if (!n) {
n = neigh_create(tbl, &nh->gw_addr, dev);
n = ipv4_neigh_create(dev, &nh->gw_addr);
if (!IS_ERR(n))
neigh_event_send(n, NULL);
}

View File

@ -229,8 +229,6 @@ static int mlxsw_sp_span_dmac(int family,
struct net_device *dev,
unsigned char dmac[ETH_ALEN])
{
struct net *net = dev_net(dev);
struct neigh_table *tbl;
struct neighbour *neigh;
int err = 0;
@ -245,10 +243,9 @@ static int mlxsw_sp_span_dmac(int family,
} else
#endif
{
tbl = arp_table(net);
neigh = neigh_lookup(tbl, pkey, dev);
neigh = ipv4_neigh_lookup(dev, pkey);
if (!neigh) {
neigh = neigh_create(tbl, pkey, dev);
neigh = ipv4_neigh_create(dev, pkey);
if (IS_ERR(neigh))
return PTR_ERR(neigh);
}

View File

@ -209,7 +209,6 @@ void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb)
{
struct nfp_tun_active_tuns *payload;
struct net_device *netdev;
struct neigh_table *tbl;
int count, i, pay_len;
struct neighbour *n;
__be32 ipv4_addr;
@ -236,8 +235,7 @@ void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb)
if (!netdev)
continue;
tbl = arp_table(dev_net(netdev));
n = neigh_lookup(tbl, &ipv4_addr, netdev);
n = ipv4_neigh_lookup(netdev, &ipv4_addr);
if (!n)
continue;

View File

@ -1336,7 +1336,7 @@ static int ofdpa_port_ipv4_resolve(struct ofdpa_port *ofdpa_port,
int err = 0;
if (!n) {
n = neigh_create(arp_table(&init_net), &ip_addr, dev);
n = ipv4_neigh_create(dev, &ip_addr);
if (IS_ERR(n))
return PTR_ERR(n);
}

View File

@ -91,7 +91,6 @@ static void efx_tc_counter_work(struct work_struct *work)
struct efx_tc_action_set *act;
unsigned long touched;
struct neighbour *n;
struct net *net;
spin_lock_bh(&cnt->lock);
touched = READ_ONCE(cnt->touched);
@ -106,14 +105,13 @@ static void efx_tc_counter_work(struct work_struct *work)
continue;
encap->neigh->used = touched;
net = encap->neigh->net;
/* We have passed traffic using this ARP entry, so
* indicate to the ARP cache that it's still active
*/
if (encap->neigh->dst_ip)
n = neigh_lookup(arp_table(net), &encap->neigh->dst_ip,
encap->neigh->egdev);
n = ipv4_neigh_lookup(encap->neigh->egdev,
&encap->neigh->dst_ip);
else
#if IS_ENABLED(CONFIG_IPV6)
n = ipv6_neigh_lookup(encap->neigh->egdev,

View File

@ -1877,7 +1877,6 @@ static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
static int arp_reduce(struct net_device *dev, struct sk_buff *skb,
const struct vxlan_config *cfg, __be32 vni)
{
struct neigh_table *tbl = arp_table(dev_net(dev));
struct vxlan_dev *vxlan = netdev_priv(dev);
struct neighbour *n;
struct arphdr *parp;
@ -1914,8 +1913,7 @@ static int arp_reduce(struct net_device *dev, struct sk_buff *skb,
ipv4_is_multicast(tip))
goto out;
n = neigh_lookup(tbl, &tip, dev);
n = ipv4_neigh_lookup(dev, &tip);
if (n) {
struct vxlan_rdst *rdst = NULL;
u8 ha[ETH_ALEN] __aligned(2);
@ -2145,7 +2143,6 @@ static int neigh_reduce(struct net_device *dev, struct sk_buff *skb,
static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb,
const struct vxlan_config *cfg)
{
struct neigh_table *tbl;
struct neighbour *n;
if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
@ -2160,9 +2157,8 @@ static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb,
if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
return false;
tbl = arp_table(dev_net(dev));
pip = ip_hdr(skb);
n = neigh_lookup(tbl, &pip->daddr, dev);
n = ipv4_neigh_lookup(dev, &pip->daddr);
if (!n && (cfg->flags & VXLAN_F_L3MISS)) {
union vxlan_addr ipa = {
.sin.sin_addr.s_addr = pip->daddr,

View File

@ -210,7 +210,7 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
return;
}
n = neigh_lookup(arp_table(dev_net(vlandev)), &tip, vlandev);
n = ipv4_neigh_lookup(vlandev, &tip);
if (n) {
struct net_bridge_fdb_entry *f;
u8 ha[ETH_ALEN] __aligned(2);

View File

@ -892,7 +892,7 @@ static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
/* Update our ARP tables */
n = __neigh_lookup(tbl, &sip, dev, 0);
n = ipv4_neigh_lookup(dev, &sip);
addr_type = -1;
if (n || arp_accept(in_dev, sip)) {
@ -911,9 +911,14 @@ static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
(addr_type == RTN_UNICAST ||
(addr_type < 0 &&
/* postpone calculation to as late as possible */
inet_addr_type_dev_table(net, dev, sip) ==
RTN_UNICAST)))))
n = __neigh_lookup(tbl, &sip, dev, 1);
inet_addr_type_dev_table(net, dev, sip) == RTN_UNICAST))))) {
n = ipv4_neigh_lookup(dev, &sip);
if (!n) {
n = ipv4_neigh_create(dev, &sip);
if (IS_ERR(n))
n = NULL;
}
}
}
if (n) {
@ -1102,7 +1107,6 @@ static int arp_req_set_public(struct net *net, struct arpreq *r,
static int arp_req_set(struct net *net, struct arpreq *r)
{
struct neigh_table *tbl = arp_table(net);
struct neighbour *neigh;
struct net_device *dev;
__be32 ip;
@ -1138,7 +1142,9 @@ static int arp_req_set(struct net *net, struct arpreq *r)
ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
neigh = __neigh_lookup_errno(tbl, &ip, dev);
neigh = ipv4_neigh_lookup(dev, &ip);
if (!neigh)
neigh = ipv4_neigh_create(dev, &ip);
err = PTR_ERR(neigh);
if (!IS_ERR(neigh)) {
unsigned int state = NUD_STALE;
@ -1174,7 +1180,6 @@ static unsigned int arp_state_to_flags(struct neighbour *neigh)
static int arp_req_get(struct net *net, struct arpreq *r)
{
__be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
struct neigh_table *tbl = arp_table(net);
struct neighbour *neigh;
struct net_device *dev;
@ -1185,7 +1190,7 @@ static int arp_req_get(struct net *net, struct arpreq *r)
if (IS_ERR(dev))
return PTR_ERR(dev);
neigh = neigh_lookup(tbl, &ip, dev);
neigh = ipv4_neigh_lookup(dev, &ip);
if (!neigh)
return -ENXIO;
@ -1210,12 +1215,13 @@ static int arp_req_get(struct net *net, struct arpreq *r)
int arp_invalidate(struct net_device *dev, __be32 ip, bool force)
{
struct neigh_table *tbl = arp_table(dev_net(dev));
struct neighbour *neigh;
int err = -ENXIO;
neigh = neigh_lookup(tbl, &ip, dev);
neigh = ipv4_neigh_lookup(dev, &ip);
if (neigh) {
struct neigh_table *tbl = neigh->tbl;
if ((READ_ONCE(neigh->nud_state) & NUD_VALID) && !force) {
neigh_release(neigh);
return 0;

View File

@ -610,12 +610,11 @@ static int fib_detect_death(struct fib_info *fi, int order,
int dflt)
{
const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
struct net *net = fi->fib_net;
int state = NUD_NONE;
struct neighbour *n;
if (likely(nhc->nhc_gw_family == AF_INET))
n = neigh_lookup(arp_table(net), &nhc->nhc_gw.ipv4, nhc->nhc_dev);
n = ipv4_neigh_lookup(nhc->nhc_dev, &nhc->nhc_gw.ipv4);
else if (IS_ENABLED(CONFIG_IPV6) && nhc->nhc_gw_family == AF_INET6)
n = ipv6_neigh_lookup(nhc->nhc_dev, &nhc->nhc_gw.ipv6);
else

View File

@ -817,7 +817,7 @@ static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flow
n = __ipv4_neigh_lookup(rt->dst.dev, (__force u32)new_gw);
if (!n)
n = neigh_create(arp_table(net), &new_gw, rt->dst.dev);
n = ipv4_neigh_create(rt->dst.dev, &new_gw);
if (!IS_ERR(n)) {
if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
neigh_event_send(n, NULL);

View File

@ -15,6 +15,7 @@
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/moduleparam.h>
#include <net/arp.h>
#include <net/dst.h>
#include <net/ndisc.h>
#include <net/neighbour.h>
@ -266,7 +267,9 @@ __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res,
} else
#endif
{
mn = __neigh_lookup_errno(n->tbl, n->primary_key, dev);
mn = ipv4_neigh_lookup(dev, n->primary_key);
if (!mn)
mn = ipv4_neigh_create(dev, n->primary_key);
}
neigh_release(n);
if (IS_ERR(mn))