xref: /linux/net/ipv4/arp.c (revision f8696133f6aa4e6a83c9fb2d9dddc6d194a2ba1f)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f30c2269SUwe Zeisberger /* linux/net/ipv4/arp.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 1994 by Florian  La Roche
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This module implements the Address Resolution Protocol ARP (RFC 826),
71da177e4SLinus Torvalds  * which is used to convert IP addresses (or in the future maybe other
81da177e4SLinus Torvalds  * high-level addresses) into a low-level hardware address (like an Ethernet
91da177e4SLinus Torvalds  * address).
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Fixes:
121da177e4SLinus Torvalds  *		Alan Cox	:	Removed the Ethernet assumptions in
131da177e4SLinus Torvalds  *					Florian's code
141da177e4SLinus Torvalds  *		Alan Cox	:	Fixed some small errors in the ARP
151da177e4SLinus Torvalds  *					logic
161da177e4SLinus Torvalds  *		Alan Cox	:	Allow >4K in /proc
171da177e4SLinus Torvalds  *		Alan Cox	:	Make ARP add its own protocol entry
181da177e4SLinus Torvalds  *		Ross Martin     :       Rewrote arp_rcv() and arp_get_info()
191da177e4SLinus Torvalds  *		Stephen Henson	:	Add AX25 support to arp_get_info()
201da177e4SLinus Torvalds  *		Alan Cox	:	Drop data when a device is downed.
211da177e4SLinus Torvalds  *		Alan Cox	:	Use init_timer().
221da177e4SLinus Torvalds  *		Alan Cox	:	Double lock fixes.
231da177e4SLinus Torvalds  *		Martin Seine	:	Move the arphdr structure
241da177e4SLinus Torvalds  *					to if_arp.h for compatibility.
251da177e4SLinus Torvalds  *					with BSD based programs.
261da177e4SLinus Torvalds  *		Andrew Tridgell :       Added ARP netmask code and
271da177e4SLinus Torvalds  *					re-arranged proxy handling.
281da177e4SLinus Torvalds  *		Alan Cox	:	Changed to use notifiers.
291da177e4SLinus Torvalds  *		Niibe Yutaka	:	Reply for this device or proxies only.
301da177e4SLinus Torvalds  *		Alan Cox	:	Don't proxy across hardware types!
311da177e4SLinus Torvalds  *		Jonathan Naylor :	Added support for NET/ROM.
321da177e4SLinus Torvalds  *		Mike Shaver     :       RFC1122 checks.
331da177e4SLinus Torvalds  *		Jonathan Naylor :	Only lookup the hardware address for
341da177e4SLinus Torvalds  *					the correct hardware type.
351da177e4SLinus Torvalds  *		Germano Caronni	:	Assorted subtle races.
361da177e4SLinus Torvalds  *		Craig Schlenter :	Don't modify permanent entry
371da177e4SLinus Torvalds  *					during arp_rcv.
381da177e4SLinus Torvalds  *		Russ Nelson	:	Tidied up a few bits.
391da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Major changes to caching and behaviour,
401da177e4SLinus Torvalds  *					eg intelligent arp probing and
411da177e4SLinus Torvalds  *					generation
421da177e4SLinus Torvalds  *					of host down events.
431da177e4SLinus Torvalds  *		Alan Cox	:	Missing unlock in device events.
441da177e4SLinus Torvalds  *		Eckes		:	ARP ioctl control errors.
451da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Arp free fix.
461da177e4SLinus Torvalds  *		Manuel Rodriguez:	Gratuitous ARP.
471da177e4SLinus Torvalds  *              Jonathan Layes  :       Added arpd support through kerneld
481da177e4SLinus Torvalds  *                                      message queue (960314)
491da177e4SLinus Torvalds  *		Mike Shaver	:	/proc/sys/net/ipv4/arp_* support
501da177e4SLinus Torvalds  *		Mike McLagan    :	Routing by source
511da177e4SLinus Torvalds  *		Stuart Cheshire	:	Metricom and grat arp fixes
521da177e4SLinus Torvalds  *					*** FOR 2.1 clean this up ***
531da177e4SLinus Torvalds  *		Lawrence V. Stefani: (08/12/96) Added FDDI support.
541da177e4SLinus Torvalds  *		Alan Cox	:	Took the AP1000 nasty FDDI hack and
551da177e4SLinus Torvalds  *					folded into the mainstream FDDI code.
561da177e4SLinus Torvalds  *					Ack spit, Linus how did you allow that
571da177e4SLinus Torvalds  *					one in...
581da177e4SLinus Torvalds  *		Jes Sorensen	:	Make FDDI work again in 2.1.x and
591da177e4SLinus Torvalds  *					clean up the APFDDI & gen. FDDI bits.
601da177e4SLinus Torvalds  *		Alexey Kuznetsov:	new arp state machine;
611da177e4SLinus Torvalds  *					now it is in net/core/neighbour.c.
621da177e4SLinus Torvalds  *		Krzysztof Halasa:	Added Frame Relay ARP support.
631da177e4SLinus Torvalds  *		Arnaldo C. Melo :	convert /proc/net/arp to seq_file
641da177e4SLinus Torvalds  *		Shmulik Hen:		Split arp_send to arp_create and
651da177e4SLinus Torvalds  *					arp_xmit so intermediate drivers like
661da177e4SLinus Torvalds  *					bonding can change the skb before
671da177e4SLinus Torvalds  *					sending (e.g. insert 8021q tag).
681da177e4SLinus Torvalds  *		Harald Welte	:	convert to make use of jenkins hash
6965324144SJesper Dangaard Brouer  *		Jesper D. Brouer:       Proxy ARP PVLAN RFC 3069 support.
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds 
7291df42beSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7391df42beSJoe Perches 
741da177e4SLinus Torvalds #include <linux/module.h>
751da177e4SLinus Torvalds #include <linux/types.h>
761da177e4SLinus Torvalds #include <linux/string.h>
771da177e4SLinus Torvalds #include <linux/kernel.h>
784fc268d2SRandy Dunlap #include <linux/capability.h>
791da177e4SLinus Torvalds #include <linux/socket.h>
801da177e4SLinus Torvalds #include <linux/sockios.h>
811da177e4SLinus Torvalds #include <linux/errno.h>
821da177e4SLinus Torvalds #include <linux/in.h>
831da177e4SLinus Torvalds #include <linux/mm.h>
841da177e4SLinus Torvalds #include <linux/inet.h>
8514c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
861da177e4SLinus Torvalds #include <linux/netdevice.h>
871da177e4SLinus Torvalds #include <linux/etherdevice.h>
881da177e4SLinus Torvalds #include <linux/fddidevice.h>
891da177e4SLinus Torvalds #include <linux/if_arp.h>
901da177e4SLinus Torvalds #include <linux/skbuff.h>
911da177e4SLinus Torvalds #include <linux/proc_fs.h>
921da177e4SLinus Torvalds #include <linux/seq_file.h>
931da177e4SLinus Torvalds #include <linux/stat.h>
941da177e4SLinus Torvalds #include <linux/init.h>
951da177e4SLinus Torvalds #include <linux/net.h>
961da177e4SLinus Torvalds #include <linux/rcupdate.h>
975a0e3ad6STejun Heo #include <linux/slab.h>
981da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
991da177e4SLinus Torvalds #include <linux/sysctl.h>
1001da177e4SLinus Torvalds #endif
1011da177e4SLinus Torvalds 
102457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1031da177e4SLinus Torvalds #include <net/ip.h>
1041da177e4SLinus Torvalds #include <net/icmp.h>
1051da177e4SLinus Torvalds #include <net/route.h>
1061da177e4SLinus Torvalds #include <net/protocol.h>
1071da177e4SLinus Torvalds #include <net/tcp.h>
1081da177e4SLinus Torvalds #include <net/sock.h>
1091da177e4SLinus Torvalds #include <net/arp.h>
1101da177e4SLinus Torvalds #include <net/ax25.h>
1111da177e4SLinus Torvalds #include <net/netrom.h>
11263d008a4SJiri Benc #include <net/dst_metadata.h>
11363d008a4SJiri Benc #include <net/ip_tunnels.h>
1141da177e4SLinus Torvalds 
115deffd777SChangli Gao #include <linux/uaccess.h>
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #include <linux/netfilter_arp.h>
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /*
1201da177e4SLinus Torvalds  *	Interface to generic neighbour cache.
1211da177e4SLinus Torvalds  */
1222c2aba6cSDavid S. Miller static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd);
12360395a20SEric W. Biederman static bool arp_key_eq(const struct neighbour *n, const void *pkey);
1241da177e4SLinus Torvalds static int arp_constructor(struct neighbour *neigh);
1251da177e4SLinus Torvalds static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
1261da177e4SLinus Torvalds static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
1271da177e4SLinus Torvalds static void parp_redo(struct sk_buff *skb);
1288cf8821eSJeff Dike static int arp_is_multicast(const void *pkey);
1291da177e4SLinus Torvalds 
13089d69d2bSStephen Hemminger static const struct neigh_ops arp_generic_ops = {
1311da177e4SLinus Torvalds 	.family =		AF_INET,
1321da177e4SLinus Torvalds 	.solicit =		arp_solicit,
1331da177e4SLinus Torvalds 	.error_report =		arp_error_report,
1341da177e4SLinus Torvalds 	.output =		neigh_resolve_output,
1351da177e4SLinus Torvalds 	.connected_output =	neigh_connected_output,
1361da177e4SLinus Torvalds };
1371da177e4SLinus Torvalds 
13889d69d2bSStephen Hemminger static const struct neigh_ops arp_hh_ops = {
1391da177e4SLinus Torvalds 	.family =		AF_INET,
1401da177e4SLinus Torvalds 	.solicit =		arp_solicit,
1411da177e4SLinus Torvalds 	.error_report =		arp_error_report,
1421da177e4SLinus Torvalds 	.output =		neigh_resolve_output,
1431da177e4SLinus Torvalds 	.connected_output =	neigh_resolve_output,
1441da177e4SLinus Torvalds };
1451da177e4SLinus Torvalds 
14689d69d2bSStephen Hemminger static const struct neigh_ops arp_direct_ops = {
1471da177e4SLinus Torvalds 	.family =		AF_INET,
1488f40b161SDavid S. Miller 	.output =		neigh_direct_output,
1498f40b161SDavid S. Miller 	.connected_output =	neigh_direct_output,
1501da177e4SLinus Torvalds };
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds struct neigh_table arp_tbl = {
1531da177e4SLinus Torvalds 	.family		= AF_INET,
1541da177e4SLinus Torvalds 	.key_len	= 4,
155bdf53c58SEric W. Biederman 	.protocol	= cpu_to_be16(ETH_P_IP),
1561da177e4SLinus Torvalds 	.hash		= arp_hash,
15760395a20SEric W. Biederman 	.key_eq		= arp_key_eq,
1581da177e4SLinus Torvalds 	.constructor	= arp_constructor,
1591da177e4SLinus Torvalds 	.proxy_redo	= parp_redo,
1608cf8821eSJeff Dike 	.is_multicast	= arp_is_multicast,
1611da177e4SLinus Torvalds 	.id		= "arp_cache",
1621da177e4SLinus Torvalds 	.parms		= {
1631da177e4SLinus Torvalds 		.tbl			= &arp_tbl,
1641da177e4SLinus Torvalds 		.reachable_time		= 30 * HZ,
1651f9248e5SJiri Pirko 		.data	= {
1661f9248e5SJiri Pirko 			[NEIGH_VAR_MCAST_PROBES] = 3,
1671f9248e5SJiri Pirko 			[NEIGH_VAR_UCAST_PROBES] = 3,
1681f9248e5SJiri Pirko 			[NEIGH_VAR_RETRANS_TIME] = 1 * HZ,
1691f9248e5SJiri Pirko 			[NEIGH_VAR_BASE_REACHABLE_TIME] = 30 * HZ,
1701f9248e5SJiri Pirko 			[NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
171211da42eSYuwei Wang 			[NEIGH_VAR_INTERVAL_PROBE_TIME_MS] = 5 * HZ,
1721f9248e5SJiri Pirko 			[NEIGH_VAR_GC_STALETIME] = 60 * HZ,
173eaa72dc4SEric Dumazet 			[NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_MAX,
1741f9248e5SJiri Pirko 			[NEIGH_VAR_PROXY_QLEN] = 64,
1751f9248e5SJiri Pirko 			[NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
1761f9248e5SJiri Pirko 			[NEIGH_VAR_PROXY_DELAY]	= (8 * HZ) / 10,
1771f9248e5SJiri Pirko 			[NEIGH_VAR_LOCKTIME] = 1 * HZ,
1781f9248e5SJiri Pirko 		},
1791da177e4SLinus Torvalds 	},
1801da177e4SLinus Torvalds 	.gc_interval	= 30 * HZ,
1811da177e4SLinus Torvalds 	.gc_thresh1	= 128,
1821da177e4SLinus Torvalds 	.gc_thresh2	= 512,
1831da177e4SLinus Torvalds 	.gc_thresh3	= 1024,
1841da177e4SLinus Torvalds };
1854bc2f18bSEric Dumazet EXPORT_SYMBOL(arp_tbl);
1861da177e4SLinus Torvalds 
187714e85beSAl Viro int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	switch (dev->type) {
1901da177e4SLinus Torvalds 	case ARPHRD_ETHER:
1911da177e4SLinus Torvalds 	case ARPHRD_FDDI:
1921da177e4SLinus Torvalds 	case ARPHRD_IEEE802:
1931da177e4SLinus Torvalds 		ip_eth_mc_map(addr, haddr);
1941da177e4SLinus Torvalds 		return 0;
1951da177e4SLinus Torvalds 	case ARPHRD_INFINIBAND:
196a9e527e3SRolf Manderscheid 		ip_ib_mc_map(addr, dev->broadcast, haddr);
1971da177e4SLinus Torvalds 		return 0;
19893ca3bb5STimo Teräs 	case ARPHRD_IPGRE:
19993ca3bb5STimo Teräs 		ip_ipgre_mc_map(addr, dev->broadcast, haddr);
20093ca3bb5STimo Teräs 		return 0;
2011da177e4SLinus Torvalds 	default:
2021da177e4SLinus Torvalds 		if (dir) {
2031da177e4SLinus Torvalds 			memcpy(haddr, dev->broadcast, dev->addr_len);
2041da177e4SLinus Torvalds 			return 0;
2051da177e4SLinus Torvalds 		}
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 	return -EINVAL;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 
211d6bf7817SEric Dumazet static u32 arp_hash(const void *pkey,
212d6bf7817SEric Dumazet 		    const struct net_device *dev,
2132c2aba6cSDavid S. Miller 		    __u32 *hash_rnd)
2141da177e4SLinus Torvalds {
21560395a20SEric W. Biederman 	return arp_hashfn(pkey, dev, hash_rnd);
21660395a20SEric W. Biederman }
21760395a20SEric W. Biederman 
21860395a20SEric W. Biederman static bool arp_key_eq(const struct neighbour *neigh, const void *pkey)
21960395a20SEric W. Biederman {
22060395a20SEric W. Biederman 	return neigh_key_eq32(neigh, pkey);
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds static int arp_constructor(struct neighbour *neigh)
2241da177e4SLinus Torvalds {
225cd9ff4deSJim Westfall 	__be32 addr;
2261da177e4SLinus Torvalds 	struct net_device *dev = neigh->dev;
2271da177e4SLinus Torvalds 	struct in_device *in_dev;
2281da177e4SLinus Torvalds 	struct neigh_parms *parms;
229cd9ff4deSJim Westfall 	u32 inaddr_any = INADDR_ANY;
2301da177e4SLinus Torvalds 
231cd9ff4deSJim Westfall 	if (dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
232cd9ff4deSJim Westfall 		memcpy(neigh->primary_key, &inaddr_any, arp_tbl.key_len);
233cd9ff4deSJim Westfall 
234cd9ff4deSJim Westfall 	addr = *(__be32 *)neigh->primary_key;
2351da177e4SLinus Torvalds 	rcu_read_lock();
236e5ed6399SHerbert Xu 	in_dev = __in_dev_get_rcu(dev);
23751456b29SIan Morris 	if (!in_dev) {
2381da177e4SLinus Torvalds 		rcu_read_unlock();
2391da177e4SLinus Torvalds 		return -EINVAL;
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds 
24230bbaa19SDavid Ahern 	neigh->type = inet_addr_type_dev_table(dev_net(dev), dev, addr);
243a79878f0SDenis V. Lunev 
2441da177e4SLinus Torvalds 	parms = in_dev->arp_parms;
2451da177e4SLinus Torvalds 	__neigh_parms_put(neigh->parms);
2461da177e4SLinus Torvalds 	neigh->parms = neigh_parms_clone(parms);
2471da177e4SLinus Torvalds 	rcu_read_unlock();
2481da177e4SLinus Torvalds 
2493b04dddeSStephen Hemminger 	if (!dev->header_ops) {
2501da177e4SLinus Torvalds 		neigh->nud_state = NUD_NOARP;
2511da177e4SLinus Torvalds 		neigh->ops = &arp_direct_ops;
2528f40b161SDavid S. Miller 		neigh->output = neigh_direct_output;
2531da177e4SLinus Torvalds 	} else {
2541da177e4SLinus Torvalds 		/* Good devices (checked by reading texts, but only Ethernet is
2551da177e4SLinus Torvalds 		   tested)
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 		   ARPHRD_ETHER: (ethernet, apfddi)
2581da177e4SLinus Torvalds 		   ARPHRD_FDDI: (fddi)
2591da177e4SLinus Torvalds 		   ARPHRD_IEEE802: (tr)
2601da177e4SLinus Torvalds 		   ARPHRD_METRICOM: (strip)
2611da177e4SLinus Torvalds 		   ARPHRD_ARCNET:
2621da177e4SLinus Torvalds 		   etc. etc. etc.
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		   ARPHRD_IPDDP will also work, if author repairs it.
2651da177e4SLinus Torvalds 		   I did not it, because this driver does not work even
2661da177e4SLinus Torvalds 		   in old paradigm.
2671da177e4SLinus Torvalds 		 */
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		if (neigh->type == RTN_MULTICAST) {
2701da177e4SLinus Torvalds 			neigh->nud_state = NUD_NOARP;
2711da177e4SLinus Torvalds 			arp_mc_map(addr, neigh->ha, dev, 1);
2721da177e4SLinus Torvalds 		} else if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) {
2731da177e4SLinus Torvalds 			neigh->nud_state = NUD_NOARP;
2741da177e4SLinus Torvalds 			memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
275deffd777SChangli Gao 		} else if (neigh->type == RTN_BROADCAST ||
276deffd777SChangli Gao 			   (dev->flags & IFF_POINTOPOINT)) {
2771da177e4SLinus Torvalds 			neigh->nud_state = NUD_NOARP;
2781da177e4SLinus Torvalds 			memcpy(neigh->ha, dev->broadcast, dev->addr_len);
2791da177e4SLinus Torvalds 		}
2803b04dddeSStephen Hemminger 
2813b04dddeSStephen Hemminger 		if (dev->header_ops->cache)
2821da177e4SLinus Torvalds 			neigh->ops = &arp_hh_ops;
2831da177e4SLinus Torvalds 		else
2841da177e4SLinus Torvalds 			neigh->ops = &arp_generic_ops;
2853b04dddeSStephen Hemminger 
2861da177e4SLinus Torvalds 		if (neigh->nud_state & NUD_VALID)
2871da177e4SLinus Torvalds 			neigh->output = neigh->ops->connected_output;
2881da177e4SLinus Torvalds 		else
2891da177e4SLinus Torvalds 			neigh->output = neigh->ops->output;
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	return 0;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	dst_link_failure(skb);
29756d4b4e4SMenglong Dong 	kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_FAILED);
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3000accfc26SThomas Graf /* Create and send an arp packet. */
3010accfc26SThomas Graf static void arp_send_dst(int type, int ptype, __be32 dest_ip,
3020accfc26SThomas Graf 			 struct net_device *dev, __be32 src_ip,
3030accfc26SThomas Graf 			 const unsigned char *dest_hw,
3040accfc26SThomas Graf 			 const unsigned char *src_hw,
30563d008a4SJiri Benc 			 const unsigned char *target_hw,
30663d008a4SJiri Benc 			 struct dst_entry *dst)
3070accfc26SThomas Graf {
3080accfc26SThomas Graf 	struct sk_buff *skb;
3090accfc26SThomas Graf 
3100accfc26SThomas Graf 	/* arp on this interface. */
3110accfc26SThomas Graf 	if (dev->flags & IFF_NOARP)
3120accfc26SThomas Graf 		return;
3130accfc26SThomas Graf 
3140accfc26SThomas Graf 	skb = arp_create(type, ptype, dest_ip, dev, src_ip,
3150accfc26SThomas Graf 			 dest_hw, src_hw, target_hw);
3160accfc26SThomas Graf 	if (!skb)
3170accfc26SThomas Graf 		return;
3180accfc26SThomas Graf 
319181a4224SJiri Benc 	skb_dst_set(skb, dst_clone(dst));
3200accfc26SThomas Graf 	arp_xmit(skb);
3210accfc26SThomas Graf }
3220accfc26SThomas Graf 
3230accfc26SThomas Graf void arp_send(int type, int ptype, __be32 dest_ip,
3240accfc26SThomas Graf 	      struct net_device *dev, __be32 src_ip,
3250accfc26SThomas Graf 	      const unsigned char *dest_hw, const unsigned char *src_hw,
3260accfc26SThomas Graf 	      const unsigned char *target_hw)
3270accfc26SThomas Graf {
3280accfc26SThomas Graf 	arp_send_dst(type, ptype, dest_ip, dev, src_ip, dest_hw, src_hw,
3290accfc26SThomas Graf 		     target_hw, NULL);
3300accfc26SThomas Graf }
3310accfc26SThomas Graf EXPORT_SYMBOL(arp_send);
3320accfc26SThomas Graf 
3331da177e4SLinus Torvalds static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
3341da177e4SLinus Torvalds {
335a61ced5dSAl Viro 	__be32 saddr = 0;
336cf0be880SCong Wang 	u8 dst_ha[MAX_ADDR_LEN], *dst_hw = NULL;
3371da177e4SLinus Torvalds 	struct net_device *dev = neigh->dev;
338a61ced5dSAl Viro 	__be32 target = *(__be32 *)neigh->primary_key;
3391da177e4SLinus Torvalds 	int probes = atomic_read(&neigh->probes);
3404b4194c4SEric Dumazet 	struct in_device *in_dev;
34163d008a4SJiri Benc 	struct dst_entry *dst = NULL;
3421da177e4SLinus Torvalds 
3434b4194c4SEric Dumazet 	rcu_read_lock();
3444b4194c4SEric Dumazet 	in_dev = __in_dev_get_rcu(dev);
3454b4194c4SEric Dumazet 	if (!in_dev) {
3464b4194c4SEric Dumazet 		rcu_read_unlock();
3471da177e4SLinus Torvalds 		return;
3484b4194c4SEric Dumazet 	}
3491da177e4SLinus Torvalds 	switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
3501da177e4SLinus Torvalds 	default:
3511da177e4SLinus Torvalds 	case 0:		/* By default announce any local IP */
35230bbaa19SDavid Ahern 		if (skb && inet_addr_type_dev_table(dev_net(dev), dev,
353deffd777SChangli Gao 					  ip_hdr(skb)->saddr) == RTN_LOCAL)
354eddc9ec5SArnaldo Carvalho de Melo 			saddr = ip_hdr(skb)->saddr;
3551da177e4SLinus Torvalds 		break;
3561da177e4SLinus Torvalds 	case 1:		/* Restrict announcements of saddr in same subnet */
3571da177e4SLinus Torvalds 		if (!skb)
3581da177e4SLinus Torvalds 			break;
359eddc9ec5SArnaldo Carvalho de Melo 		saddr = ip_hdr(skb)->saddr;
36030bbaa19SDavid Ahern 		if (inet_addr_type_dev_table(dev_net(dev), dev,
36130bbaa19SDavid Ahern 					     saddr) == RTN_LOCAL) {
3621da177e4SLinus Torvalds 			/* saddr should be known to target */
3631da177e4SLinus Torvalds 			if (inet_addr_onlink(in_dev, target, saddr))
3641da177e4SLinus Torvalds 				break;
3651da177e4SLinus Torvalds 		}
3661da177e4SLinus Torvalds 		saddr = 0;
3671da177e4SLinus Torvalds 		break;
3681da177e4SLinus Torvalds 	case 2:		/* Avoid secondary IPs, get a primary/preferred one */
3691da177e4SLinus Torvalds 		break;
3701da177e4SLinus Torvalds 	}
3714b4194c4SEric Dumazet 	rcu_read_unlock();
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	if (!saddr)
3741da177e4SLinus Torvalds 		saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
3751da177e4SLinus Torvalds 
3761f9248e5SJiri Pirko 	probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
377deffd777SChangli Gao 	if (probes < 0) {
378b071af52SEric Dumazet 		if (!(READ_ONCE(neigh->nud_state) & NUD_VALID))
37991df42beSJoe Perches 			pr_debug("trying to ucast probe in NUD_INVALID\n");
3809650388bSEric Dumazet 		neigh_ha_snapshot(dst_ha, neigh, dev);
381cf0be880SCong Wang 		dst_hw = dst_ha;
382deffd777SChangli Gao 	} else {
3831f9248e5SJiri Pirko 		probes -= NEIGH_VAR(neigh->parms, APP_PROBES);
384deffd777SChangli Gao 		if (probes < 0) {
3851da177e4SLinus Torvalds 			neigh_app_ns(neigh);
3861da177e4SLinus Torvalds 			return;
3871da177e4SLinus Torvalds 		}
388deffd777SChangli Gao 	}
3891da177e4SLinus Torvalds 
39063d008a4SJiri Benc 	if (skb && !(dev->priv_flags & IFF_XMIT_DST_RELEASE))
391181a4224SJiri Benc 		dst = skb_dst(skb);
3920accfc26SThomas Graf 	arp_send_dst(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
39363d008a4SJiri Benc 		     dst_hw, dev->dev_addr, NULL, dst);
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3969bd85e32SDenis V. Lunev static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip)
3971da177e4SLinus Torvalds {
398b601fa19SNicolas Dichtel 	struct net *net = dev_net(in_dev->dev);
3991da177e4SLinus Torvalds 	int scope;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	switch (IN_DEV_ARP_IGNORE(in_dev)) {
4021da177e4SLinus Torvalds 	case 0:	/* Reply, the tip is already validated */
4031da177e4SLinus Torvalds 		return 0;
4041da177e4SLinus Torvalds 	case 1:	/* Reply only if tip is configured on the incoming interface */
4051da177e4SLinus Torvalds 		sip = 0;
4061da177e4SLinus Torvalds 		scope = RT_SCOPE_HOST;
4071da177e4SLinus Torvalds 		break;
4081da177e4SLinus Torvalds 	case 2:	/*
4091da177e4SLinus Torvalds 		 * Reply only if tip is configured on the incoming interface
4101da177e4SLinus Torvalds 		 * and is in same subnet as sip
4111da177e4SLinus Torvalds 		 */
4121da177e4SLinus Torvalds 		scope = RT_SCOPE_HOST;
4131da177e4SLinus Torvalds 		break;
4141da177e4SLinus Torvalds 	case 3:	/* Do not reply for scope host addresses */
4151da177e4SLinus Torvalds 		sip = 0;
4161da177e4SLinus Torvalds 		scope = RT_SCOPE_LINK;
417b601fa19SNicolas Dichtel 		in_dev = NULL;
4181da177e4SLinus Torvalds 		break;
4191da177e4SLinus Torvalds 	case 4:	/* Reserved */
4201da177e4SLinus Torvalds 	case 5:
4211da177e4SLinus Torvalds 	case 6:
4221da177e4SLinus Torvalds 	case 7:
4231da177e4SLinus Torvalds 		return 0;
4241da177e4SLinus Torvalds 	case 8:	/* Do not reply */
4251da177e4SLinus Torvalds 		return 1;
4261da177e4SLinus Torvalds 	default:
4271da177e4SLinus Torvalds 		return 0;
4281da177e4SLinus Torvalds 	}
429b601fa19SNicolas Dichtel 	return !inet_confirm_addr(net, in_dev, sip, tip, scope);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
432e68c5dcfSJaehee Park static int arp_accept(struct in_device *in_dev, __be32 sip)
433e68c5dcfSJaehee Park {
434e68c5dcfSJaehee Park 	struct net *net = dev_net(in_dev->dev);
435e68c5dcfSJaehee Park 	int scope = RT_SCOPE_LINK;
436e68c5dcfSJaehee Park 
437e68c5dcfSJaehee Park 	switch (IN_DEV_ARP_ACCEPT(in_dev)) {
438e68c5dcfSJaehee Park 	case 0: /* Don't create new entries from garp */
439e68c5dcfSJaehee Park 		return 0;
440e68c5dcfSJaehee Park 	case 1: /* Create new entries from garp */
441e68c5dcfSJaehee Park 		return 1;
442e68c5dcfSJaehee Park 	case 2: /* Create a neighbor in the arp table only if sip
443e68c5dcfSJaehee Park 		 * is in the same subnet as an address configured
444e68c5dcfSJaehee Park 		 * on the interface that received the garp message
445e68c5dcfSJaehee Park 		 */
446e68c5dcfSJaehee Park 		return !!inet_confirm_addr(net, in_dev, sip, 0, scope);
447e68c5dcfSJaehee Park 	default:
448e68c5dcfSJaehee Park 		return 0;
449e68c5dcfSJaehee Park 	}
450e68c5dcfSJaehee Park }
451e68c5dcfSJaehee Park 
452ed9bad06SAl Viro static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	struct rtable *rt;
4551da177e4SLinus Torvalds 	int flag = 0;
4561da177e4SLinus Torvalds 	/*unsigned long now; */
457ca12a1a4SPavel Emelyanov 	struct net *net = dev_net(dev);
4581da177e4SLinus Torvalds 
459ec20b283SGuillaume Nault 	rt = ip_route_output(net, sip, tip, 0, l3mdev_master_ifindex_rcu(dev),
460ec20b283SGuillaume Nault 			     RT_SCOPE_UNIVERSE);
461b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
4621da177e4SLinus Torvalds 		return 1;
463d8d1f30bSChangli Gao 	if (rt->dst.dev != dev) {
46402a1d6e7SEric Dumazet 		__NET_INC_STATS(net, LINUX_MIB_ARPFILTER);
4651da177e4SLinus Torvalds 		flag = 1;
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 	ip_rt_put(rt);
4681da177e4SLinus Torvalds 	return flag;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds /*
4721da177e4SLinus Torvalds  * Check if we can use proxy ARP for this path
4731da177e4SLinus Torvalds  */
47465324144SJesper Dangaard Brouer static inline int arp_fwd_proxy(struct in_device *in_dev,
47565324144SJesper Dangaard Brouer 				struct net_device *dev,	struct rtable *rt)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	struct in_device *out_dev;
4781da177e4SLinus Torvalds 	int imi, omi = -1;
4791da177e4SLinus Torvalds 
480d8d1f30bSChangli Gao 	if (rt->dst.dev == dev)
48165324144SJesper Dangaard Brouer 		return 0;
48265324144SJesper Dangaard Brouer 
4831da177e4SLinus Torvalds 	if (!IN_DEV_PROXY_ARP(in_dev))
4841da177e4SLinus Torvalds 		return 0;
485deffd777SChangli Gao 	imi = IN_DEV_MEDIUM_ID(in_dev);
486deffd777SChangli Gao 	if (imi == 0)
4871da177e4SLinus Torvalds 		return 1;
4881da177e4SLinus Torvalds 	if (imi == -1)
4891da177e4SLinus Torvalds 		return 0;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	/* place to check for proxy_arp for routes */
4921da177e4SLinus Torvalds 
493d8d1f30bSChangli Gao 	out_dev = __in_dev_get_rcu(rt->dst.dev);
494faa9dcf7SEric Dumazet 	if (out_dev)
4951da177e4SLinus Torvalds 		omi = IN_DEV_MEDIUM_ID(out_dev);
496faa9dcf7SEric Dumazet 
497a02cec21SEric Dumazet 	return omi != imi && omi != -1;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /*
50165324144SJesper Dangaard Brouer  * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev)
50265324144SJesper Dangaard Brouer  *
50365324144SJesper Dangaard Brouer  * RFC3069 supports proxy arp replies back to the same interface.  This
50465324144SJesper Dangaard Brouer  * is done to support (ethernet) switch features, like RFC 3069, where
50565324144SJesper Dangaard Brouer  * the individual ports are not allowed to communicate with each
50665324144SJesper Dangaard Brouer  * other, BUT they are allowed to talk to the upstream router.  As
50765324144SJesper Dangaard Brouer  * described in RFC 3069, it is possible to allow these hosts to
50865324144SJesper Dangaard Brouer  * communicate through the upstream router, by proxy_arp'ing.
50965324144SJesper Dangaard Brouer  *
51065324144SJesper Dangaard Brouer  * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation"
51165324144SJesper Dangaard Brouer  *
51265324144SJesper Dangaard Brouer  *  This technology is known by different names:
51365324144SJesper Dangaard Brouer  *    In RFC 3069 it is called VLAN Aggregation.
51465324144SJesper Dangaard Brouer  *    Cisco and Allied Telesyn call it Private VLAN.
51565324144SJesper Dangaard Brouer  *    Hewlett-Packard call it Source-Port filtering or port-isolation.
51665324144SJesper Dangaard Brouer  *    Ericsson call it MAC-Forced Forwarding (RFC Draft).
51765324144SJesper Dangaard Brouer  *
51865324144SJesper Dangaard Brouer  */
51965324144SJesper Dangaard Brouer static inline int arp_fwd_pvlan(struct in_device *in_dev,
52065324144SJesper Dangaard Brouer 				struct net_device *dev,	struct rtable *rt,
52165324144SJesper Dangaard Brouer 				__be32 sip, __be32 tip)
52265324144SJesper Dangaard Brouer {
52365324144SJesper Dangaard Brouer 	/* Private VLAN is only concerned about the same ethernet segment */
524d8d1f30bSChangli Gao 	if (rt->dst.dev != dev)
52565324144SJesper Dangaard Brouer 		return 0;
52665324144SJesper Dangaard Brouer 
52765324144SJesper Dangaard Brouer 	/* Don't reply on self probes (often done by windowz boxes)*/
52865324144SJesper Dangaard Brouer 	if (sip == tip)
52965324144SJesper Dangaard Brouer 		return 0;
53065324144SJesper Dangaard Brouer 
53165324144SJesper Dangaard Brouer 	if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
53265324144SJesper Dangaard Brouer 		return 1;
53365324144SJesper Dangaard Brouer 	else
53465324144SJesper Dangaard Brouer 		return 0;
53565324144SJesper Dangaard Brouer }
53665324144SJesper Dangaard Brouer 
53765324144SJesper Dangaard Brouer /*
5381da177e4SLinus Torvalds  *	Interface to link layer: send routine and receive handler.
5391da177e4SLinus Torvalds  */
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds /*
54251456b29SIan Morris  *	Create an arp packet. If dest_hw is not set, we create a broadcast
5431da177e4SLinus Torvalds  *	message.
5441da177e4SLinus Torvalds  */
545ed9bad06SAl Viro struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
546ed9bad06SAl Viro 			   struct net_device *dev, __be32 src_ip,
547abfdf1c4SJan Engelhardt 			   const unsigned char *dest_hw,
548abfdf1c4SJan Engelhardt 			   const unsigned char *src_hw,
549abfdf1c4SJan Engelhardt 			   const unsigned char *target_hw)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	struct sk_buff *skb;
5521da177e4SLinus Torvalds 	struct arphdr *arp;
5531da177e4SLinus Torvalds 	unsigned char *arp_ptr;
55466088243SHerbert Xu 	int hlen = LL_RESERVED_SPACE(dev);
55566088243SHerbert Xu 	int tlen = dev->needed_tailroom;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	/*
5581da177e4SLinus Torvalds 	 *	Allocate a buffer
5591da177e4SLinus Torvalds 	 */
5601da177e4SLinus Torvalds 
56166088243SHerbert Xu 	skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC);
56251456b29SIan Morris 	if (!skb)
5631da177e4SLinus Torvalds 		return NULL;
5641da177e4SLinus Torvalds 
56566088243SHerbert Xu 	skb_reserve(skb, hlen);
566c1d2bbe1SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
5674df864c1SJohannes Berg 	arp = skb_put(skb, arp_hdr_len(dev));
5681da177e4SLinus Torvalds 	skb->dev = dev;
5691da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_ARP);
57051456b29SIan Morris 	if (!src_hw)
5711da177e4SLinus Torvalds 		src_hw = dev->dev_addr;
57251456b29SIan Morris 	if (!dest_hw)
5731da177e4SLinus Torvalds 		dest_hw = dev->broadcast;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	/*
5761da177e4SLinus Torvalds 	 *	Fill the device header for the ARP frame
5771da177e4SLinus Torvalds 	 */
5780c4e8581SStephen Hemminger 	if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
5791da177e4SLinus Torvalds 		goto out;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	/*
5821da177e4SLinus Torvalds 	 * Fill out the arp protocol part.
5831da177e4SLinus Torvalds 	 *
5841da177e4SLinus Torvalds 	 * The arp hardware type should match the device type, except for FDDI,
5851da177e4SLinus Torvalds 	 * which (according to RFC 1390) should always equal 1 (Ethernet).
5861da177e4SLinus Torvalds 	 */
5871da177e4SLinus Torvalds 	/*
5881da177e4SLinus Torvalds 	 *	Exceptions everywhere. AX.25 uses the AX.25 PID value not the
5891da177e4SLinus Torvalds 	 *	DIX code for the protocol. Make these device structure fields.
5901da177e4SLinus Torvalds 	 */
5911da177e4SLinus Torvalds 	switch (dev->type) {
5921da177e4SLinus Torvalds 	default:
5931da177e4SLinus Torvalds 		arp->ar_hrd = htons(dev->type);
5941da177e4SLinus Torvalds 		arp->ar_pro = htons(ETH_P_IP);
5951da177e4SLinus Torvalds 		break;
5961da177e4SLinus Torvalds 
59740e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25)
5981da177e4SLinus Torvalds 	case ARPHRD_AX25:
5991da177e4SLinus Torvalds 		arp->ar_hrd = htons(ARPHRD_AX25);
6001da177e4SLinus Torvalds 		arp->ar_pro = htons(AX25_P_IP);
6011da177e4SLinus Torvalds 		break;
6021da177e4SLinus Torvalds 
60340e4783eSIgor Maravic #if IS_ENABLED(CONFIG_NETROM)
6041da177e4SLinus Torvalds 	case ARPHRD_NETROM:
6051da177e4SLinus Torvalds 		arp->ar_hrd = htons(ARPHRD_NETROM);
6061da177e4SLinus Torvalds 		arp->ar_pro = htons(AX25_P_IP);
6071da177e4SLinus Torvalds 		break;
6081da177e4SLinus Torvalds #endif
6091da177e4SLinus Torvalds #endif
6101da177e4SLinus Torvalds 
61140e4783eSIgor Maravic #if IS_ENABLED(CONFIG_FDDI)
6121da177e4SLinus Torvalds 	case ARPHRD_FDDI:
6131da177e4SLinus Torvalds 		arp->ar_hrd = htons(ARPHRD_ETHER);
6141da177e4SLinus Torvalds 		arp->ar_pro = htons(ETH_P_IP);
6151da177e4SLinus Torvalds 		break;
6161da177e4SLinus Torvalds #endif
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	arp->ar_hln = dev->addr_len;
6201da177e4SLinus Torvalds 	arp->ar_pln = 4;
6211da177e4SLinus Torvalds 	arp->ar_op = htons(type);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	arp_ptr = (unsigned char *)(arp + 1);
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	memcpy(arp_ptr, src_hw, dev->addr_len);
6261da177e4SLinus Torvalds 	arp_ptr += dev->addr_len;
6271da177e4SLinus Torvalds 	memcpy(arp_ptr, &src_ip, 4);
6281da177e4SLinus Torvalds 	arp_ptr += 4;
6296752c8dbSYOSHIFUJI Hideaki / 吉藤英明 
6306752c8dbSYOSHIFUJI Hideaki / 吉藤英明 	switch (dev->type) {
6316752c8dbSYOSHIFUJI Hideaki / 吉藤英明 #if IS_ENABLED(CONFIG_FIREWIRE_NET)
6326752c8dbSYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
6336752c8dbSYOSHIFUJI Hideaki / 吉藤英明 		break;
6346752c8dbSYOSHIFUJI Hideaki / 吉藤英明 #endif
6356752c8dbSYOSHIFUJI Hideaki / 吉藤英明 	default:
63600db4124SIan Morris 		if (target_hw)
6371da177e4SLinus Torvalds 			memcpy(arp_ptr, target_hw, dev->addr_len);
6381da177e4SLinus Torvalds 		else
6391da177e4SLinus Torvalds 			memset(arp_ptr, 0, dev->addr_len);
6401da177e4SLinus Torvalds 		arp_ptr += dev->addr_len;
6416752c8dbSYOSHIFUJI Hideaki / 吉藤英明 	}
6421da177e4SLinus Torvalds 	memcpy(arp_ptr, &dest_ip, 4);
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	return skb;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds out:
6471da177e4SLinus Torvalds 	kfree_skb(skb);
6481da177e4SLinus Torvalds 	return NULL;
6491da177e4SLinus Torvalds }
6504bc2f18bSEric Dumazet EXPORT_SYMBOL(arp_create);
6511da177e4SLinus Torvalds 
6520c4b51f0SEric W. Biederman static int arp_xmit_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
653f9e4306fSEric W. Biederman {
654f9e4306fSEric W. Biederman 	return dev_queue_xmit(skb);
655f9e4306fSEric W. Biederman }
656f9e4306fSEric W. Biederman 
6571da177e4SLinus Torvalds /*
6581da177e4SLinus Torvalds  *	Send an arp packet.
6591da177e4SLinus Torvalds  */
6601da177e4SLinus Torvalds void arp_xmit(struct sk_buff *skb)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	/* Send it off, maybe filter it using firewalling first.  */
66329a26a56SEric W. Biederman 	NF_HOOK(NFPROTO_ARP, NF_ARP_OUT,
66429a26a56SEric W. Biederman 		dev_net(skb->dev), NULL, skb, NULL, skb->dev,
66529a26a56SEric W. Biederman 		arp_xmit_finish);
6661da177e4SLinus Torvalds }
6674bc2f18bSEric Dumazet EXPORT_SYMBOL(arp_xmit);
6681da177e4SLinus Torvalds 
669d9ef2e7bSIhar Hrachyshka static bool arp_is_garp(struct net *net, struct net_device *dev,
670d9ef2e7bSIhar Hrachyshka 			int *addr_type, __be16 ar_op,
6716fd05633SIhar Hrachyshka 			__be32 sip, __be32 tip,
6726fd05633SIhar Hrachyshka 			unsigned char *sha, unsigned char *tha)
6736fd05633SIhar Hrachyshka {
674d9ef2e7bSIhar Hrachyshka 	bool is_garp = tip == sip;
6756fd05633SIhar Hrachyshka 
6766fd05633SIhar Hrachyshka 	/* Gratuitous ARP _replies_ also require target hwaddr to be
6776fd05633SIhar Hrachyshka 	 * the same as source.
6786fd05633SIhar Hrachyshka 	 */
6796fd05633SIhar Hrachyshka 	if (is_garp && ar_op == htons(ARPOP_REPLY))
6806fd05633SIhar Hrachyshka 		is_garp =
6816fd05633SIhar Hrachyshka 			/* IPv4 over IEEE 1394 doesn't provide target
6826fd05633SIhar Hrachyshka 			 * hardware address field in its ARP payload.
6836fd05633SIhar Hrachyshka 			 */
6846fd05633SIhar Hrachyshka 			tha &&
6856fd05633SIhar Hrachyshka 			!memcmp(tha, sha, dev->addr_len);
6866fd05633SIhar Hrachyshka 
687d9ef2e7bSIhar Hrachyshka 	if (is_garp) {
688d9ef2e7bSIhar Hrachyshka 		*addr_type = inet_addr_type_dev_table(net, dev, sip);
689d9ef2e7bSIhar Hrachyshka 		if (*addr_type != RTN_UNICAST)
690d9ef2e7bSIhar Hrachyshka 			is_garp = false;
691d9ef2e7bSIhar Hrachyshka 	}
6926fd05633SIhar Hrachyshka 	return is_garp;
6936fd05633SIhar Hrachyshka }
6946fd05633SIhar Hrachyshka 
6951da177e4SLinus Torvalds /*
6961da177e4SLinus Torvalds  *	Process an arp request.
6971da177e4SLinus Torvalds  */
6981da177e4SLinus Torvalds 
6990c4b51f0SEric W. Biederman static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
702faa9dcf7SEric Dumazet 	struct in_device *in_dev = __in_dev_get_rcu(dev);
7031da177e4SLinus Torvalds 	struct arphdr *arp;
7041da177e4SLinus Torvalds 	unsigned char *arp_ptr;
7051da177e4SLinus Torvalds 	struct rtable *rt;
706e0260fedSMark Ryden 	unsigned char *sha;
70723d268ebSIhar Hrachyshka 	unsigned char *tha = NULL;
7089e12bb22SAl Viro 	__be32 sip, tip;
7091da177e4SLinus Torvalds 	u16 dev_type = dev->type;
7101da177e4SLinus Torvalds 	int addr_type;
7111da177e4SLinus Torvalds 	struct neighbour *n;
71263d008a4SJiri Benc 	struct dst_entry *reply_dst = NULL;
71356022a8fSSalam Noureddine 	bool is_garp = false;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	/* arp_rcv below verifies the ARP header and verifies the device
7161da177e4SLinus Torvalds 	 * is ARP'able.
7171da177e4SLinus Torvalds 	 */
7181da177e4SLinus Torvalds 
71951456b29SIan Morris 	if (!in_dev)
7208dfd329fSZhang Shengju 		goto out_free_skb;
7211da177e4SLinus Torvalds 
722d0a92be0SArnaldo Carvalho de Melo 	arp = arp_hdr(skb);
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	switch (dev_type) {
7251da177e4SLinus Torvalds 	default:
7261da177e4SLinus Torvalds 		if (arp->ar_pro != htons(ETH_P_IP) ||
7271da177e4SLinus Torvalds 		    htons(dev_type) != arp->ar_hrd)
7288dfd329fSZhang Shengju 			goto out_free_skb;
7291da177e4SLinus Torvalds 		break;
7301da177e4SLinus Torvalds 	case ARPHRD_ETHER:
7311da177e4SLinus Torvalds 	case ARPHRD_FDDI:
7321da177e4SLinus Torvalds 	case ARPHRD_IEEE802:
7331da177e4SLinus Torvalds 		/*
734211ed865SPaul Gortmaker 		 * ETHERNET, and Fibre Channel (which are IEEE 802
7351da177e4SLinus Torvalds 		 * devices, according to RFC 2625) devices will accept ARP
7361da177e4SLinus Torvalds 		 * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
7371da177e4SLinus Torvalds 		 * This is the case also of FDDI, where the RFC 1390 says that
7381da177e4SLinus Torvalds 		 * FDDI devices should accept ARP hardware of (1) Ethernet,
7391da177e4SLinus Torvalds 		 * however, to be more robust, we'll accept both 1 (Ethernet)
7401da177e4SLinus Torvalds 		 * or 6 (IEEE 802.2)
7411da177e4SLinus Torvalds 		 */
7421da177e4SLinus Torvalds 		if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
7431da177e4SLinus Torvalds 		     arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
7441da177e4SLinus Torvalds 		    arp->ar_pro != htons(ETH_P_IP))
7458dfd329fSZhang Shengju 			goto out_free_skb;
7461da177e4SLinus Torvalds 		break;
7471da177e4SLinus Torvalds 	case ARPHRD_AX25:
7481da177e4SLinus Torvalds 		if (arp->ar_pro != htons(AX25_P_IP) ||
7491da177e4SLinus Torvalds 		    arp->ar_hrd != htons(ARPHRD_AX25))
7508dfd329fSZhang Shengju 			goto out_free_skb;
7511da177e4SLinus Torvalds 		break;
7521da177e4SLinus Torvalds 	case ARPHRD_NETROM:
7531da177e4SLinus Torvalds 		if (arp->ar_pro != htons(AX25_P_IP) ||
7541da177e4SLinus Torvalds 		    arp->ar_hrd != htons(ARPHRD_NETROM))
7558dfd329fSZhang Shengju 			goto out_free_skb;
7561da177e4SLinus Torvalds 		break;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	/* Understand only these message types */
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	if (arp->ar_op != htons(ARPOP_REPLY) &&
7621da177e4SLinus Torvalds 	    arp->ar_op != htons(ARPOP_REQUEST))
7638dfd329fSZhang Shengju 		goto out_free_skb;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds /*
7661da177e4SLinus Torvalds  *	Extract fields
7671da177e4SLinus Torvalds  */
7681da177e4SLinus Torvalds 	arp_ptr = (unsigned char *)(arp + 1);
7691da177e4SLinus Torvalds 	sha	= arp_ptr;
7701da177e4SLinus Torvalds 	arp_ptr += dev->addr_len;
7711da177e4SLinus Torvalds 	memcpy(&sip, arp_ptr, 4);
7721da177e4SLinus Torvalds 	arp_ptr += 4;
7736752c8dbSYOSHIFUJI Hideaki / 吉藤英明 	switch (dev_type) {
7746752c8dbSYOSHIFUJI Hideaki / 吉藤英明 #if IS_ENABLED(CONFIG_FIREWIRE_NET)
7756752c8dbSYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
7766752c8dbSYOSHIFUJI Hideaki / 吉藤英明 		break;
7776752c8dbSYOSHIFUJI Hideaki / 吉藤英明 #endif
7786752c8dbSYOSHIFUJI Hideaki / 吉藤英明 	default:
77923d268ebSIhar Hrachyshka 		tha = arp_ptr;
7801da177e4SLinus Torvalds 		arp_ptr += dev->addr_len;
7816752c8dbSYOSHIFUJI Hideaki / 吉藤英明 	}
7821da177e4SLinus Torvalds 	memcpy(&tip, arp_ptr, 4);
7831da177e4SLinus Torvalds /*
7841da177e4SLinus Torvalds  *	Check for bad requests for 127.x.x.x and requests for multicast
7851da177e4SLinus Torvalds  *	addresses.  If this is one such, delete it.
7861da177e4SLinus Torvalds  */
787d0daebc3SThomas Graf 	if (ipv4_is_multicast(tip) ||
788d0daebc3SThomas Graf 	    (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip)))
7898dfd329fSZhang Shengju 		goto out_free_skb;
7901da177e4SLinus Torvalds 
79197daf331SJohannes Berg  /*
79297daf331SJohannes Berg   *	For some 802.11 wireless deployments (and possibly other networks),
79397daf331SJohannes Berg   *	there will be an ARP proxy and gratuitous ARP frames are attacks
79497daf331SJohannes Berg   *	and thus should not be accepted.
79597daf331SJohannes Berg   */
79697daf331SJohannes Berg 	if (sip == tip && IN_DEV_ORCONF(in_dev, DROP_GRATUITOUS_ARP))
7978dfd329fSZhang Shengju 		goto out_free_skb;
79897daf331SJohannes Berg 
7991da177e4SLinus Torvalds /*
8001da177e4SLinus Torvalds  *     Special case: We must set Frame Relay source Q.922 address
8011da177e4SLinus Torvalds  */
8021da177e4SLinus Torvalds 	if (dev_type == ARPHRD_DLCI)
8031da177e4SLinus Torvalds 		sha = dev->broadcast;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds /*
8061da177e4SLinus Torvalds  *  Process entry.  The idea here is we want to send a reply if it is a
8071da177e4SLinus Torvalds  *  request for us or if it is a request for someone else that we hold
8081da177e4SLinus Torvalds  *  a proxy for.  We want to add an entry to our cache if it is a reply
8091da177e4SLinus Torvalds  *  to us or if it is a request for our address.
8101da177e4SLinus Torvalds  *  (The assumption for this last is that if someone is requesting our
8111da177e4SLinus Torvalds  *  address, they are probably intending to talk to us, so it saves time
8121da177e4SLinus Torvalds  *  if we cache their address.  Their address is also probably not in
8131da177e4SLinus Torvalds  *  our cache, since ours is not in their cache.)
8141da177e4SLinus Torvalds  *
8151da177e4SLinus Torvalds  *  Putting this another way, we only care about replies if they are to
8161da177e4SLinus Torvalds  *  us, in which case we add them to the cache.  For requests, we care
8171da177e4SLinus Torvalds  *  about those for us and those for our proxies.  We reply to both,
8181da177e4SLinus Torvalds  *  and in the case of requests for us we add the requester to the arp
8191da177e4SLinus Torvalds  *  cache.
8201da177e4SLinus Torvalds  */
8211da177e4SLinus Torvalds 
82263d008a4SJiri Benc 	if (arp->ar_op == htons(ARPOP_REQUEST) && skb_metadata_dst(skb))
82363d008a4SJiri Benc 		reply_dst = (struct dst_entry *)
82463d008a4SJiri Benc 			    iptunnel_metadata_reply(skb_metadata_dst(skb),
82563d008a4SJiri Benc 						    GFP_ATOMIC);
82663d008a4SJiri Benc 
827f8a68e75SEric W. Biederman 	/* Special case: IPv4 duplicate address detection packet (RFC2131) */
828f8a68e75SEric W. Biederman 	if (sip == 0) {
8291da177e4SLinus Torvalds 		if (arp->ar_op == htons(ARPOP_REQUEST) &&
83030bbaa19SDavid Ahern 		    inet_addr_type_dev_table(net, dev, tip) == RTN_LOCAL &&
8319bd85e32SDenis V. Lunev 		    !arp_ignore(in_dev, sip, tip))
83263d008a4SJiri Benc 			arp_send_dst(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip,
83363d008a4SJiri Benc 				     sha, dev->dev_addr, sha, reply_dst);
8348dfd329fSZhang Shengju 		goto out_consume_skb;
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	if (arp->ar_op == htons(ARPOP_REQUEST) &&
838c6cffba4SDavid S. Miller 	    ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
8391da177e4SLinus Torvalds 
840511c3f92SEric Dumazet 		rt = skb_rtable(skb);
8411da177e4SLinus Torvalds 		addr_type = rt->rt_type;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 		if (addr_type == RTN_LOCAL) {
844deffd777SChangli Gao 			int dont_send;
8451da177e4SLinus Torvalds 
846deffd777SChangli Gao 			dont_send = arp_ignore(in_dev, sip, tip);
8471da177e4SLinus Torvalds 			if (!dont_send && IN_DEV_ARPFILTER(in_dev))
848ae9c416dSChangli Gao 				dont_send = arp_filter(sip, tip, dev);
8498164f1b7SBen Greear 			if (!dont_send) {
8508164f1b7SBen Greear 				n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
8518164f1b7SBen Greear 				if (n) {
85263d008a4SJiri Benc 					arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
85363d008a4SJiri Benc 						     sip, dev, tip, sha,
85463d008a4SJiri Benc 						     dev->dev_addr, sha,
85563d008a4SJiri Benc 						     reply_dst);
8561da177e4SLinus Torvalds 					neigh_release(n);
8571da177e4SLinus Torvalds 				}
8588164f1b7SBen Greear 			}
8598dfd329fSZhang Shengju 			goto out_consume_skb;
8601da177e4SLinus Torvalds 		} else if (IN_DEV_FORWARD(in_dev)) {
86165324144SJesper Dangaard Brouer 			if (addr_type == RTN_UNICAST  &&
86265324144SJesper Dangaard Brouer 			    (arp_fwd_proxy(in_dev, dev, rt) ||
86365324144SJesper Dangaard Brouer 			     arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
86470620c46SThomas Graf 			     (rt->dst.dev != dev &&
86570620c46SThomas Graf 			      pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))) {
8661da177e4SLinus Torvalds 				n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
8671da177e4SLinus Torvalds 				if (n)
8681da177e4SLinus Torvalds 					neigh_release(n);
8691da177e4SLinus Torvalds 
870a61bbcf2SPatrick McHardy 				if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
8711da177e4SLinus Torvalds 				    skb->pkt_type == PACKET_HOST ||
8721f9248e5SJiri Pirko 				    NEIGH_VAR(in_dev->arp_parms, PROXY_DELAY) == 0) {
87363d008a4SJiri Benc 					arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
87463d008a4SJiri Benc 						     sip, dev, tip, sha,
87563d008a4SJiri Benc 						     dev->dev_addr, sha,
87663d008a4SJiri Benc 						     reply_dst);
8771da177e4SLinus Torvalds 				} else {
878deffd777SChangli Gao 					pneigh_enqueue(&arp_tbl,
879deffd777SChangli Gao 						       in_dev->arp_parms, skb);
880181a4224SJiri Benc 					goto out_free_dst;
8811da177e4SLinus Torvalds 				}
8828dfd329fSZhang Shengju 				goto out_consume_skb;
8831da177e4SLinus Torvalds 			}
8841da177e4SLinus Torvalds 		}
8851da177e4SLinus Torvalds 	}
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	/* Update our ARP tables */
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
8901da177e4SLinus Torvalds 
891d9ef2e7bSIhar Hrachyshka 	addr_type = -1;
892e68c5dcfSJaehee Park 	if (n || arp_accept(in_dev, sip)) {
8937d472a59SIhar Hrachyshka 		is_garp = arp_is_garp(net, dev, &addr_type, arp->ar_op,
8947d472a59SIhar Hrachyshka 				      sip, tip, sha, tha);
8957d472a59SIhar Hrachyshka 	}
89630bbaa19SDavid Ahern 
897e68c5dcfSJaehee Park 	if (arp_accept(in_dev, sip)) {
8981da177e4SLinus Torvalds 		/* Unsolicited ARP is not accepted by default.
8991da177e4SLinus Torvalds 		   It is possible, that this option should be enabled for some
9001da177e4SLinus Torvalds 		   devices (strip is candidate)
9011da177e4SLinus Torvalds 		 */
90251456b29SIan Morris 		if (!n &&
903d9ef2e7bSIhar Hrachyshka 		    (is_garp ||
904d9ef2e7bSIhar Hrachyshka 		     (arp->ar_op == htons(ARPOP_REPLY) &&
905d9ef2e7bSIhar Hrachyshka 		      (addr_type == RTN_UNICAST ||
906d9ef2e7bSIhar Hrachyshka 		       (addr_type < 0 &&
907d9ef2e7bSIhar Hrachyshka 			/* postpone calculation to as late as possible */
908d9ef2e7bSIhar Hrachyshka 			inet_addr_type_dev_table(net, dev, sip) ==
909d9ef2e7bSIhar Hrachyshka 				RTN_UNICAST)))))
9101b1ac759SJean Delvare 			n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
911abd596a4SNeil Horman 	}
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	if (n) {
9141da177e4SLinus Torvalds 		int state = NUD_REACHABLE;
9151da177e4SLinus Torvalds 		int override;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		/* If several different ARP replies follows back-to-back,
9181da177e4SLinus Torvalds 		   use the FIRST one. It is possible, if several proxy
9191da177e4SLinus Torvalds 		   agents are active. Taking the first reply prevents
9201da177e4SLinus Torvalds 		   arp trashing and chooses the fastest router.
9211da177e4SLinus Torvalds 		 */
92256022a8fSSalam Noureddine 		override = time_after(jiffies,
92356022a8fSSalam Noureddine 				      n->updated +
92456022a8fSSalam Noureddine 				      NEIGH_VAR(n->parms, LOCKTIME)) ||
92556022a8fSSalam Noureddine 			   is_garp;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 		/* Broadcast replies and request packets
9281da177e4SLinus Torvalds 		   do not assert neighbour reachability.
9291da177e4SLinus Torvalds 		 */
9301da177e4SLinus Torvalds 		if (arp->ar_op != htons(ARPOP_REPLY) ||
9311da177e4SLinus Torvalds 		    skb->pkt_type != PACKET_HOST)
9321da177e4SLinus Torvalds 			state = NUD_STALE;
933deffd777SChangli Gao 		neigh_update(n, sha, state,
9347b8f7a40SRoopa Prabhu 			     override ? NEIGH_UPDATE_F_OVERRIDE : 0, 0);
9351da177e4SLinus Torvalds 		neigh_release(n);
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 
9388dfd329fSZhang Shengju out_consume_skb:
939ead2ceb0SNeil Horman 	consume_skb(skb);
9408dfd329fSZhang Shengju 
941181a4224SJiri Benc out_free_dst:
942181a4224SJiri Benc 	dst_release(reply_dst);
9438dfd329fSZhang Shengju 	return NET_RX_SUCCESS;
9448dfd329fSZhang Shengju 
9458dfd329fSZhang Shengju out_free_skb:
9468dfd329fSZhang Shengju 	kfree_skb(skb);
9478dfd329fSZhang Shengju 	return NET_RX_DROP;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
950444fc8fcSHerbert Xu static void parp_redo(struct sk_buff *skb)
951444fc8fcSHerbert Xu {
9520c4b51f0SEric W. Biederman 	arp_process(dev_net(skb->dev), NULL, skb);
953444fc8fcSHerbert Xu }
954444fc8fcSHerbert Xu 
9558cf8821eSJeff Dike static int arp_is_multicast(const void *pkey)
9568cf8821eSJeff Dike {
9578cf8821eSJeff Dike 	return ipv4_is_multicast(*((__be32 *)pkey));
9588cf8821eSJeff Dike }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds /*
9611da177e4SLinus Torvalds  *	Receive an arp request from the device layer.
9621da177e4SLinus Torvalds  */
9631da177e4SLinus Torvalds 
9646c97e72aSAdrian Bunk static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
9656c97e72aSAdrian Bunk 		   struct packet_type *pt, struct net_device *orig_dev)
9661da177e4SLinus Torvalds {
967044453b3SEric Dumazet 	const struct arphdr *arp;
968044453b3SEric Dumazet 
969825bae5dSRick Jones 	/* do not tweak dropwatch on an ARP we will ignore */
970044453b3SEric Dumazet 	if (dev->flags & IFF_NOARP ||
971044453b3SEric Dumazet 	    skb->pkt_type == PACKET_OTHERHOST ||
972044453b3SEric Dumazet 	    skb->pkt_type == PACKET_LOOPBACK)
973825bae5dSRick Jones 		goto consumeskb;
974044453b3SEric Dumazet 
975044453b3SEric Dumazet 	skb = skb_share_check(skb, GFP_ATOMIC);
976044453b3SEric Dumazet 	if (!skb)
977044453b3SEric Dumazet 		goto out_of_mem;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	/* ARP header, plus 2 device addresses, plus 2 IP addresses.  */
980988b7050SPavel Emelyanov 	if (!pskb_may_pull(skb, arp_hdr_len(dev)))
9811da177e4SLinus Torvalds 		goto freeskb;
9821da177e4SLinus Torvalds 
983d0a92be0SArnaldo Carvalho de Melo 	arp = arp_hdr(skb);
984044453b3SEric Dumazet 	if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4)
9851da177e4SLinus Torvalds 		goto freeskb;
9861da177e4SLinus Torvalds 
987a61bbcf2SPatrick McHardy 	memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
988a61bbcf2SPatrick McHardy 
98929a26a56SEric W. Biederman 	return NF_HOOK(NFPROTO_ARP, NF_ARP_IN,
99029a26a56SEric W. Biederman 		       dev_net(dev), NULL, skb, dev, NULL,
99129a26a56SEric W. Biederman 		       arp_process);
9921da177e4SLinus Torvalds 
993825bae5dSRick Jones consumeskb:
994825bae5dSRick Jones 	consume_skb(skb);
9958dfd329fSZhang Shengju 	return NET_RX_SUCCESS;
9961da177e4SLinus Torvalds freeskb:
9971da177e4SLinus Torvalds 	kfree_skb(skb);
9981da177e4SLinus Torvalds out_of_mem:
9998dfd329fSZhang Shengju 	return NET_RX_DROP;
10001da177e4SLinus Torvalds }
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds /*
10031da177e4SLinus Torvalds  *	User level interface (ioctl)
10041da177e4SLinus Torvalds  */
10051da177e4SLinus Torvalds 
1006*f8696133SKuniyuki Iwashima static struct net_device *arp_req_dev(struct net *net, struct arpreq *r)
1007*f8696133SKuniyuki Iwashima {
1008*f8696133SKuniyuki Iwashima 	struct net_device *dev;
1009*f8696133SKuniyuki Iwashima 	struct rtable *rt;
1010*f8696133SKuniyuki Iwashima 	__be32 ip;
1011*f8696133SKuniyuki Iwashima 
1012*f8696133SKuniyuki Iwashima 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1013*f8696133SKuniyuki Iwashima 
1014*f8696133SKuniyuki Iwashima 	rt = ip_route_output(net, ip, 0, 0, 0, RT_SCOPE_LINK);
1015*f8696133SKuniyuki Iwashima 	if (IS_ERR(rt))
1016*f8696133SKuniyuki Iwashima 		return ERR_CAST(rt);
1017*f8696133SKuniyuki Iwashima 
1018*f8696133SKuniyuki Iwashima 	dev = rt->dst.dev;
1019*f8696133SKuniyuki Iwashima 	ip_rt_put(rt);
1020*f8696133SKuniyuki Iwashima 
1021*f8696133SKuniyuki Iwashima 	if (!dev)
1022*f8696133SKuniyuki Iwashima 		return ERR_PTR(-EINVAL);
1023*f8696133SKuniyuki Iwashima 
1024*f8696133SKuniyuki Iwashima 	return dev;
1025*f8696133SKuniyuki Iwashima }
1026*f8696133SKuniyuki Iwashima 
10271da177e4SLinus Torvalds /*
10281da177e4SLinus Torvalds  *	Set (create) an ARP cache entry.
10291da177e4SLinus Torvalds  */
10301da177e4SLinus Torvalds 
103132e569b7SPavel Emelyanov static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
1032f8b33fdfSPavel Emelyanov {
103351456b29SIan Morris 	if (!dev) {
1034586f1211SPavel Emelyanov 		IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
1035f8b33fdfSPavel Emelyanov 		return 0;
1036f8b33fdfSPavel Emelyanov 	}
1037c506653dSEric Dumazet 	if (__in_dev_get_rtnl(dev)) {
1038c506653dSEric Dumazet 		IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
1039f8b33fdfSPavel Emelyanov 		return 0;
1040f8b33fdfSPavel Emelyanov 	}
1041f8b33fdfSPavel Emelyanov 	return -ENXIO;
1042f8b33fdfSPavel Emelyanov }
1043f8b33fdfSPavel Emelyanov 
104432e569b7SPavel Emelyanov static int arp_req_set_public(struct net *net, struct arpreq *r,
104532e569b7SPavel Emelyanov 		struct net_device *dev)
10461da177e4SLinus Torvalds {
1047ed9bad06SAl Viro 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
104843dc1701SPavel Emelyanov 
10491da177e4SLinus Torvalds 	if (!dev && (r->arp_flags & ATF_COM)) {
1050941666c2SEric Dumazet 		dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
105143dc1701SPavel Emelyanov 				      r->arp_ha.sa_data);
10521da177e4SLinus Torvalds 		if (!dev)
10531da177e4SLinus Torvalds 			return -ENODEV;
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds 	if (mask) {
105605923674SKuniyuki Iwashima 		__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
105705923674SKuniyuki Iwashima 
105851456b29SIan Morris 		if (!pneigh_lookup(&arp_tbl, net, &ip, dev, 1))
10591da177e4SLinus Torvalds 			return -ENOBUFS;
10601da177e4SLinus Torvalds 		return 0;
10611da177e4SLinus Torvalds 	}
1062f8b33fdfSPavel Emelyanov 
106332e569b7SPavel Emelyanov 	return arp_req_set_proxy(net, dev, 1);
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
106632e569b7SPavel Emelyanov static int arp_req_set(struct net *net, struct arpreq *r,
106732e569b7SPavel Emelyanov 		       struct net_device *dev)
106843dc1701SPavel Emelyanov {
106943dc1701SPavel Emelyanov 	struct neighbour *neigh;
1070*f8696133SKuniyuki Iwashima 	__be32 ip;
107143dc1701SPavel Emelyanov 	int err;
107243dc1701SPavel Emelyanov 
107343dc1701SPavel Emelyanov 	if (r->arp_flags & ATF_PUBL)
107432e569b7SPavel Emelyanov 		return arp_req_set_public(net, r, dev);
107543dc1701SPavel Emelyanov 
107651456b29SIan Morris 	if (!dev) {
1077*f8696133SKuniyuki Iwashima 		dev = arp_req_dev(net, r);
1078*f8696133SKuniyuki Iwashima 		if (IS_ERR(dev))
1079*f8696133SKuniyuki Iwashima 			return PTR_ERR(dev);
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 	switch (dev->type) {
108240e4783eSIgor Maravic #if IS_ENABLED(CONFIG_FDDI)
10831da177e4SLinus Torvalds 	case ARPHRD_FDDI:
10841da177e4SLinus Torvalds 		/*
10851da177e4SLinus Torvalds 		 * According to RFC 1390, FDDI devices should accept ARP
10861da177e4SLinus Torvalds 		 * hardware types of 1 (Ethernet).  However, to be more
10871da177e4SLinus Torvalds 		 * robust, we'll accept hardware types of either 1 (Ethernet)
10881da177e4SLinus Torvalds 		 * or 6 (IEEE 802.2).
10891da177e4SLinus Torvalds 		 */
10901da177e4SLinus Torvalds 		if (r->arp_ha.sa_family != ARPHRD_FDDI &&
10911da177e4SLinus Torvalds 		    r->arp_ha.sa_family != ARPHRD_ETHER &&
10921da177e4SLinus Torvalds 		    r->arp_ha.sa_family != ARPHRD_IEEE802)
10931da177e4SLinus Torvalds 			return -EINVAL;
10941da177e4SLinus Torvalds 		break;
10951da177e4SLinus Torvalds #endif
10961da177e4SLinus Torvalds 	default:
10971da177e4SLinus Torvalds 		if (r->arp_ha.sa_family != dev->type)
10981da177e4SLinus Torvalds 			return -EINVAL;
10991da177e4SLinus Torvalds 		break;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
1102*f8696133SKuniyuki Iwashima 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1103*f8696133SKuniyuki Iwashima 
11041da177e4SLinus Torvalds 	neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
11051da177e4SLinus Torvalds 	err = PTR_ERR(neigh);
11061da177e4SLinus Torvalds 	if (!IS_ERR(neigh)) {
110795c96174SEric Dumazet 		unsigned int state = NUD_STALE;
110842033d0cSKuniyuki Iwashima 
110942033d0cSKuniyuki Iwashima 		if (r->arp_flags & ATF_PERM) {
111042033d0cSKuniyuki Iwashima 			r->arp_flags |= ATF_COM;
11111da177e4SLinus Torvalds 			state = NUD_PERMANENT;
111242033d0cSKuniyuki Iwashima 		}
111342033d0cSKuniyuki Iwashima 
11141da177e4SLinus Torvalds 		err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
11151da177e4SLinus Torvalds 				   r->arp_ha.sa_data : NULL, state,
11161da177e4SLinus Torvalds 				   NEIGH_UPDATE_F_OVERRIDE |
11177b8f7a40SRoopa Prabhu 				   NEIGH_UPDATE_F_ADMIN, 0);
11181da177e4SLinus Torvalds 		neigh_release(neigh);
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 	return err;
11211da177e4SLinus Torvalds }
11221da177e4SLinus Torvalds 
112395c96174SEric Dumazet static unsigned int arp_state_to_flags(struct neighbour *neigh)
11241da177e4SLinus Torvalds {
11251da177e4SLinus Torvalds 	if (neigh->nud_state&NUD_PERMANENT)
1126deffd777SChangli Gao 		return ATF_PERM | ATF_COM;
11271da177e4SLinus Torvalds 	else if (neigh->nud_state&NUD_VALID)
1128deffd777SChangli Gao 		return ATF_COM;
1129deffd777SChangli Gao 	else
1130deffd777SChangli Gao 		return 0;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds /*
11341da177e4SLinus Torvalds  *	Get an ARP cache entry.
11351da177e4SLinus Torvalds  */
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds static int arp_req_get(struct arpreq *r, struct net_device *dev)
11381da177e4SLinus Torvalds {
1139ed9bad06SAl Viro 	__be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
11401da177e4SLinus Torvalds 	struct neighbour *neigh;
11411da177e4SLinus Torvalds 	int err = -ENXIO;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	neigh = neigh_lookup(&arp_tbl, &ip, dev);
11441da177e4SLinus Torvalds 	if (neigh) {
1145b071af52SEric Dumazet 		if (!(READ_ONCE(neigh->nud_state) & NUD_NOARP)) {
11461da177e4SLinus Torvalds 			read_lock_bh(&neigh->lock);
1147a7d60277SKuniyuki Iwashima 			memcpy(r->arp_ha.sa_data, neigh->ha,
1148a7d60277SKuniyuki Iwashima 			       min(dev->addr_len, sizeof(r->arp_ha.sa_data_min)));
11491da177e4SLinus Torvalds 			r->arp_flags = arp_state_to_flags(neigh);
11501da177e4SLinus Torvalds 			read_unlock_bh(&neigh->lock);
11511da177e4SLinus Torvalds 			r->arp_ha.sa_family = dev->type;
115201e454f2SWolfram Sang 			strscpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
11531da177e4SLinus Torvalds 			err = 0;
11541da177e4SLinus Torvalds 		}
115511c91ef9SEric Dumazet 		neigh_release(neigh);
115611c91ef9SEric Dumazet 	}
11571da177e4SLinus Torvalds 	return err;
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11600c51e12eSIdo Schimmel int arp_invalidate(struct net_device *dev, __be32 ip, bool force)
1161545ecdc3SMaxim Levitsky {
1162545ecdc3SMaxim Levitsky 	struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
1163545ecdc3SMaxim Levitsky 	int err = -ENXIO;
11645071034eSSowmini Varadhan 	struct neigh_table *tbl = &arp_tbl;
1165545ecdc3SMaxim Levitsky 
1166545ecdc3SMaxim Levitsky 	if (neigh) {
1167b071af52SEric Dumazet 		if ((READ_ONCE(neigh->nud_state) & NUD_VALID) && !force) {
11680c51e12eSIdo Schimmel 			neigh_release(neigh);
11690c51e12eSIdo Schimmel 			return 0;
11700c51e12eSIdo Schimmel 		}
11710c51e12eSIdo Schimmel 
1172b071af52SEric Dumazet 		if (READ_ONCE(neigh->nud_state) & ~NUD_NOARP)
1173545ecdc3SMaxim Levitsky 			err = neigh_update(neigh, NULL, NUD_FAILED,
1174545ecdc3SMaxim Levitsky 					   NEIGH_UPDATE_F_OVERRIDE|
11757b8f7a40SRoopa Prabhu 					   NEIGH_UPDATE_F_ADMIN, 0);
11765071034eSSowmini Varadhan 		write_lock_bh(&tbl->lock);
1177545ecdc3SMaxim Levitsky 		neigh_release(neigh);
11785071034eSSowmini Varadhan 		neigh_remove_one(neigh, tbl);
11795071034eSSowmini Varadhan 		write_unlock_bh(&tbl->lock);
1180545ecdc3SMaxim Levitsky 	}
1181545ecdc3SMaxim Levitsky 
1182545ecdc3SMaxim Levitsky 	return err;
1183545ecdc3SMaxim Levitsky }
1184545ecdc3SMaxim Levitsky 
118532e569b7SPavel Emelyanov static int arp_req_delete_public(struct net *net, struct arpreq *r,
118632e569b7SPavel Emelyanov 		struct net_device *dev)
11871da177e4SLinus Torvalds {
118846479b43SPavel Emelyanov 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
11891da177e4SLinus Torvalds 
119005923674SKuniyuki Iwashima 	if (mask) {
119105923674SKuniyuki Iwashima 		__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
119246479b43SPavel Emelyanov 
119305923674SKuniyuki Iwashima 		return pneigh_delete(&arp_tbl, net, &ip, dev);
119405923674SKuniyuki Iwashima 	}
1195f8b33fdfSPavel Emelyanov 
119632e569b7SPavel Emelyanov 	return arp_req_set_proxy(net, dev, 0);
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds 
119932e569b7SPavel Emelyanov static int arp_req_delete(struct net *net, struct arpreq *r,
120032e569b7SPavel Emelyanov 			  struct net_device *dev)
120146479b43SPavel Emelyanov {
120246479b43SPavel Emelyanov 	__be32 ip;
120346479b43SPavel Emelyanov 
120446479b43SPavel Emelyanov 	if (r->arp_flags & ATF_PUBL)
120532e569b7SPavel Emelyanov 		return arp_req_delete_public(net, r, dev);
120646479b43SPavel Emelyanov 
120746479b43SPavel Emelyanov 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
120851456b29SIan Morris 	if (!dev) {
1209*f8696133SKuniyuki Iwashima 		dev = arp_req_dev(net, r);
1210*f8696133SKuniyuki Iwashima 		if (IS_ERR(dev))
1211*f8696133SKuniyuki Iwashima 			return PTR_ERR(dev);
12121da177e4SLinus Torvalds 	}
12130c51e12eSIdo Schimmel 	return arp_invalidate(dev, ip, true);
12141da177e4SLinus Torvalds }
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds /*
12171da177e4SLinus Torvalds  *	Handle an ARP layer I/O control request.
12181da177e4SLinus Torvalds  */
12191da177e4SLinus Torvalds 
122032e569b7SPavel Emelyanov int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
12211da177e4SLinus Torvalds {
12221da177e4SLinus Torvalds 	struct net_device *dev = NULL;
122305923674SKuniyuki Iwashima 	struct arpreq r;
122405923674SKuniyuki Iwashima 	__be32 *netmask;
122505923674SKuniyuki Iwashima 	int err;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	switch (cmd) {
12281da177e4SLinus Torvalds 	case SIOCDARP:
12291da177e4SLinus Torvalds 	case SIOCSARP:
123052e804c6SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
12311da177e4SLinus Torvalds 			return -EPERM;
1232a8eceea8SJoe Perches 		fallthrough;
12331da177e4SLinus Torvalds 	case SIOCGARP:
12341da177e4SLinus Torvalds 		err = copy_from_user(&r, arg, sizeof(struct arpreq));
12351da177e4SLinus Torvalds 		if (err)
12361da177e4SLinus Torvalds 			return -EFAULT;
12371da177e4SLinus Torvalds 		break;
12381da177e4SLinus Torvalds 	default:
12391da177e4SLinus Torvalds 		return -EINVAL;
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 	if (r.arp_pa.sa_family != AF_INET)
12431da177e4SLinus Torvalds 		return -EPFNOSUPPORT;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	if (!(r.arp_flags & ATF_PUBL) &&
12461da177e4SLinus Torvalds 	    (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
12471da177e4SLinus Torvalds 		return -EINVAL;
124805923674SKuniyuki Iwashima 
124905923674SKuniyuki Iwashima 	netmask = &((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr;
12501da177e4SLinus Torvalds 	if (!(r.arp_flags & ATF_NETMASK))
125105923674SKuniyuki Iwashima 		*netmask = htonl(0xFFFFFFFFUL);
125205923674SKuniyuki Iwashima 	else if (*netmask && *netmask != htonl(0xFFFFFFFFUL))
125305923674SKuniyuki Iwashima 		return -EINVAL;
125405923674SKuniyuki Iwashima 
1255c506653dSEric Dumazet 	rtnl_lock();
12561da177e4SLinus Torvalds 	if (r.arp_dev[0]) {
12571da177e4SLinus Torvalds 		err = -ENODEV;
1258c506653dSEric Dumazet 		dev = __dev_get_by_name(net, r.arp_dev);
125951456b29SIan Morris 		if (!dev)
12601da177e4SLinus Torvalds 			goto out;
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 		/* Mmmm... It is wrong... ARPHRD_NETROM==0 */
12631da177e4SLinus Torvalds 		if (!r.arp_ha.sa_family)
12641da177e4SLinus Torvalds 			r.arp_ha.sa_family = dev->type;
12651da177e4SLinus Torvalds 		err = -EINVAL;
12661da177e4SLinus Torvalds 		if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
12671da177e4SLinus Torvalds 			goto out;
12681da177e4SLinus Torvalds 	} else if (cmd == SIOCGARP) {
12691da177e4SLinus Torvalds 		err = -ENODEV;
12701da177e4SLinus Torvalds 		goto out;
12711da177e4SLinus Torvalds 	}
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	switch (cmd) {
12741da177e4SLinus Torvalds 	case SIOCDARP:
127532e569b7SPavel Emelyanov 		err = arp_req_delete(net, &r, dev);
12761da177e4SLinus Torvalds 		break;
12771da177e4SLinus Torvalds 	case SIOCSARP:
127832e569b7SPavel Emelyanov 		err = arp_req_set(net, &r, dev);
12791da177e4SLinus Torvalds 		break;
12801da177e4SLinus Torvalds 	case SIOCGARP:
12811da177e4SLinus Torvalds 		err = arp_req_get(&r, dev);
12821da177e4SLinus Torvalds 		break;
12831da177e4SLinus Torvalds 	}
12841da177e4SLinus Torvalds out:
1285c506653dSEric Dumazet 	rtnl_unlock();
1286941666c2SEric Dumazet 	if (cmd == SIOCGARP && !err && copy_to_user(arg, &r, sizeof(r)))
1287941666c2SEric Dumazet 		err = -EFAULT;
12881da177e4SLinus Torvalds 	return err;
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
1291deffd777SChangli Gao static int arp_netdev_event(struct notifier_block *this, unsigned long event,
1292deffd777SChangli Gao 			    void *ptr)
12931da177e4SLinus Torvalds {
1294351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
12956c8b4e3fSTimo Teräs 	struct netdev_notifier_change_info *change_info;
1296fcdb44d0SJames Prestwood 	struct in_device *in_dev;
1297fcdb44d0SJames Prestwood 	bool evict_nocarrier;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	switch (event) {
13001da177e4SLinus Torvalds 	case NETDEV_CHANGEADDR:
13011da177e4SLinus Torvalds 		neigh_changeaddr(&arp_tbl, dev);
1302bafa6d9dSNicolas Dichtel 		rt_cache_flush(dev_net(dev));
13031da177e4SLinus Torvalds 		break;
13046c8b4e3fSTimo Teräs 	case NETDEV_CHANGE:
13056c8b4e3fSTimo Teräs 		change_info = ptr;
13066c8b4e3fSTimo Teräs 		if (change_info->flags_changed & IFF_NOARP)
13076c8b4e3fSTimo Teräs 			neigh_changeaddr(&arp_tbl, dev);
1308fcdb44d0SJames Prestwood 
1309fcdb44d0SJames Prestwood 		in_dev = __in_dev_get_rtnl(dev);
1310fcdb44d0SJames Prestwood 		if (!in_dev)
1311fcdb44d0SJames Prestwood 			evict_nocarrier = true;
1312fcdb44d0SJames Prestwood 		else
1313fcdb44d0SJames Prestwood 			evict_nocarrier = IN_DEV_ARP_EVICT_NOCARRIER(in_dev);
1314fcdb44d0SJames Prestwood 
1315fcdb44d0SJames Prestwood 		if (evict_nocarrier && !netif_carrier_ok(dev))
1316859bd2efSDavid Ahern 			neigh_carrier_down(&arp_tbl, dev);
13176c8b4e3fSTimo Teräs 		break;
13181da177e4SLinus Torvalds 	default:
13191da177e4SLinus Torvalds 		break;
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	return NOTIFY_DONE;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds static struct notifier_block arp_netdev_notifier = {
13261da177e4SLinus Torvalds 	.notifier_call = arp_netdev_event,
13271da177e4SLinus Torvalds };
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds /* Note, that it is not on notifier chain.
13301da177e4SLinus Torvalds    It is necessary, that this routine was called after route cache will be
13311da177e4SLinus Torvalds    flushed.
13321da177e4SLinus Torvalds  */
13331da177e4SLinus Torvalds void arp_ifdown(struct net_device *dev)
13341da177e4SLinus Torvalds {
13351da177e4SLinus Torvalds 	neigh_ifdown(&arp_tbl, dev);
13361da177e4SLinus Torvalds }
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds /*
13401da177e4SLinus Torvalds  *	Called once on startup.
13411da177e4SLinus Torvalds  */
13421da177e4SLinus Torvalds 
13437546dd97SStephen Hemminger static struct packet_type arp_packet_type __read_mostly = {
134409640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_ARP),
13451da177e4SLinus Torvalds 	.func =	arp_rcv,
13461da177e4SLinus Torvalds };
13471da177e4SLinus Torvalds 
1348b0e653b2SYajun Deng #ifdef CONFIG_PROC_FS
134940e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25)
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds /*
13521da177e4SLinus Torvalds  *	ax25 -> ASCII conversion
13531da177e4SLinus Torvalds  */
13544872e57cSRalf Baechle static void ax2asc2(ax25_address *a, char *buf)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	char c, *s;
13571da177e4SLinus Torvalds 	int n;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	for (n = 0, s = buf; n < 6; n++) {
13601da177e4SLinus Torvalds 		c = (a->ax25_call[n] >> 1) & 0x7F;
13611da177e4SLinus Torvalds 
1362deffd777SChangli Gao 		if (c != ' ')
1363deffd777SChangli Gao 			*s++ = c;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	*s++ = '-';
1367deffd777SChangli Gao 	n = (a->ax25_call[6] >> 1) & 0x0F;
1368deffd777SChangli Gao 	if (n > 9) {
13691da177e4SLinus Torvalds 		*s++ = '1';
13701da177e4SLinus Torvalds 		n -= 10;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	*s++ = n + '0';
13741da177e4SLinus Torvalds 	*s++ = '\0';
13751da177e4SLinus Torvalds 
13764872e57cSRalf Baechle 	if (*buf == '\0' || *buf == '-') {
13774872e57cSRalf Baechle 		buf[0] = '*';
13784872e57cSRalf Baechle 		buf[1] = '\0';
13794872e57cSRalf Baechle 	}
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds #endif /* CONFIG_AX25 */
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds #define HBUFFERLEN 30
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds static void arp_format_neigh_entry(struct seq_file *seq,
13861da177e4SLinus Torvalds 				   struct neighbour *n)
13871da177e4SLinus Torvalds {
13881da177e4SLinus Torvalds 	char hbuffer[HBUFFERLEN];
13891da177e4SLinus Torvalds 	int k, j;
13901da177e4SLinus Torvalds 	char tbuf[16];
13911da177e4SLinus Torvalds 	struct net_device *dev = n->dev;
13921da177e4SLinus Torvalds 	int hatype = dev->type;
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	read_lock(&n->lock);
13951da177e4SLinus Torvalds 	/* Convert hardware address to XX:XX:XX:XX ... form. */
139640e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25)
13971da177e4SLinus Torvalds 	if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
13981da177e4SLinus Torvalds 		ax2asc2((ax25_address *)n->ha, hbuffer);
13991da177e4SLinus Torvalds 	else {
14001da177e4SLinus Torvalds #endif
14011da177e4SLinus Torvalds 	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
140251f82a2bSDenis Cheng 		hbuffer[k++] = hex_asc_hi(n->ha[j]);
140351f82a2bSDenis Cheng 		hbuffer[k++] = hex_asc_lo(n->ha[j]);
14041da177e4SLinus Torvalds 		hbuffer[k++] = ':';
14051da177e4SLinus Torvalds 	}
1406a3e8ee68Sroel kluin 	if (k != 0)
1407a3e8ee68Sroel kluin 		--k;
1408a3e8ee68Sroel kluin 	hbuffer[k] = 0;
140940e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25)
14101da177e4SLinus Torvalds 	}
14111da177e4SLinus Torvalds #endif
1412673d57e7SHarvey Harrison 	sprintf(tbuf, "%pI4", n->primary_key);
14134872e57cSRalf Baechle 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%-17s     *        %s\n",
14141da177e4SLinus Torvalds 		   tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
14151da177e4SLinus Torvalds 	read_unlock(&n->lock);
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds static void arp_format_pneigh_entry(struct seq_file *seq,
14191da177e4SLinus Torvalds 				    struct pneigh_entry *n)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	struct net_device *dev = n->dev;
14221da177e4SLinus Torvalds 	int hatype = dev ? dev->type : 0;
14231da177e4SLinus Torvalds 	char tbuf[16];
14241da177e4SLinus Torvalds 
1425673d57e7SHarvey Harrison 	sprintf(tbuf, "%pI4", n->key);
14261da177e4SLinus Torvalds 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%s     *        %s\n",
14271da177e4SLinus Torvalds 		   tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
14281da177e4SLinus Torvalds 		   dev ? dev->name : "*");
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds static int arp_seq_show(struct seq_file *seq, void *v)
14321da177e4SLinus Torvalds {
14331da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
14341da177e4SLinus Torvalds 		seq_puts(seq, "IP address       HW type     Flags       "
14351da177e4SLinus Torvalds 			      "HW address            Mask     Device\n");
14361da177e4SLinus Torvalds 	} else {
14371da177e4SLinus Torvalds 		struct neigh_seq_state *state = seq->private;
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 		if (state->flags & NEIGH_SEQ_IS_PNEIGH)
14401da177e4SLinus Torvalds 			arp_format_pneigh_entry(seq, v);
14411da177e4SLinus Torvalds 		else
14421da177e4SLinus Torvalds 			arp_format_neigh_entry(seq, v);
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	return 0;
14461da177e4SLinus Torvalds }
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
14491da177e4SLinus Torvalds {
14501da177e4SLinus Torvalds 	/* Don't want to confuse "arp -a" w/ magic entries,
14511da177e4SLinus Torvalds 	 * so we tell the generic iterator to skip NUD_NOARP.
14521da177e4SLinus Torvalds 	 */
14531da177e4SLinus Torvalds 	return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds 
1456f690808eSStephen Hemminger static const struct seq_operations arp_seq_ops = {
14571da177e4SLinus Torvalds 	.start	= arp_seq_start,
14581da177e4SLinus Torvalds 	.next	= neigh_seq_next,
14591da177e4SLinus Torvalds 	.stop	= neigh_seq_stop,
14601da177e4SLinus Torvalds 	.show	= arp_seq_show,
14611da177e4SLinus Torvalds };
1462b0e653b2SYajun Deng #endif /* CONFIG_PROC_FS */
1463ffc31d3dSDenis V. Lunev 
1464ffc31d3dSDenis V. Lunev static int __net_init arp_net_init(struct net *net)
14651da177e4SLinus Torvalds {
1466c3506372SChristoph Hellwig 	if (!proc_create_net("arp", 0444, net->proc_net, &arp_seq_ops,
1467c3506372SChristoph Hellwig 			sizeof(struct neigh_seq_state)))
14681da177e4SLinus Torvalds 		return -ENOMEM;
14691da177e4SLinus Torvalds 	return 0;
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds 
1472ffc31d3dSDenis V. Lunev static void __net_exit arp_net_exit(struct net *net)
1473ffc31d3dSDenis V. Lunev {
1474ece31ffdSGao feng 	remove_proc_entry("arp", net->proc_net);
1475ffc31d3dSDenis V. Lunev }
1476ffc31d3dSDenis V. Lunev 
1477ffc31d3dSDenis V. Lunev static struct pernet_operations arp_net_ops = {
1478ffc31d3dSDenis V. Lunev 	.init = arp_net_init,
1479ffc31d3dSDenis V. Lunev 	.exit = arp_net_exit,
1480ffc31d3dSDenis V. Lunev };
1481ffc31d3dSDenis V. Lunev 
1482e968b1b3SYajun Deng void __init arp_init(void)
1483ffc31d3dSDenis V. Lunev {
1484e968b1b3SYajun Deng 	neigh_table_init(NEIGH_ARP_TABLE, &arp_tbl);
1485e968b1b3SYajun Deng 
1486e968b1b3SYajun Deng 	dev_add_pack(&arp_packet_type);
1487e968b1b3SYajun Deng 	register_pernet_subsys(&arp_net_ops);
1488e968b1b3SYajun Deng #ifdef CONFIG_SYSCTL
1489e968b1b3SYajun Deng 	neigh_sysctl_register(NULL, &arp_tbl.parms, NULL);
1490e968b1b3SYajun Deng #endif
1491e968b1b3SYajun Deng 	register_netdevice_notifier(&arp_netdev_notifier);
1492ffc31d3dSDenis V. Lunev }
1493