xref: /linux/net/ipv4/arp.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
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 
arp_mc_map(__be32 addr,u8 * haddr,struct net_device * dev,int dir)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 
arp_hash(const void * pkey,const struct net_device * dev,__u32 * hash_rnd)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 
arp_key_eq(const struct neighbour * neigh,const void * pkey)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 
arp_constructor(struct neighbour * neigh)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 
arp_error_report(struct neighbour * neigh,struct sk_buff * skb)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. */
arp_send_dst(int type,int ptype,__be32 dest_ip,struct net_device * dev,__be32 src_ip,const unsigned char * dest_hw,const unsigned char * src_hw,const unsigned char * target_hw,struct dst_entry * dst)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 
arp_send(int type,int ptype,__be32 dest_ip,struct net_device * dev,__be32 src_ip,const unsigned char * dest_hw,const unsigned char * src_hw,const unsigned char * target_hw)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 
arp_solicit(struct neighbour * neigh,struct sk_buff * skb)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 
arp_ignore(struct in_device * in_dev,__be32 sip,__be32 tip)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 
arp_accept(struct in_device * in_dev,__be32 sip)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 
arp_filter(__be32 sip,__be32 tip,struct net_device * dev)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  */
arp_fwd_proxy(struct in_device * in_dev,struct net_device * dev,struct rtable * rt)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  */
arp_fwd_pvlan(struct in_device * in_dev,struct net_device * dev,struct rtable * rt,__be32 sip,__be32 tip)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  */
arp_create(int type,int ptype,__be32 dest_ip,struct net_device * dev,__be32 src_ip,const unsigned char * dest_hw,const unsigned char * src_hw,const unsigned char * target_hw)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 
arp_xmit_finish(struct net * net,struct sock * sk,struct sk_buff * skb)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  */
arp_xmit(struct sk_buff * skb)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 
arp_is_garp(struct net * net,struct net_device * dev,int * addr_type,__be16 ar_op,__be32 sip,__be32 tip,unsigned char * sha,unsigned char * tha)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 
arp_process(struct net * net,struct sock * sk,struct sk_buff * skb)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 
parp_redo(struct sk_buff * skb)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 
arp_is_multicast(const void * pkey)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 
arp_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)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 
arp_req_dev_by_name(struct net * net,struct arpreq * r,bool getarp)1006*bf4ea588SKuniyuki Iwashima static struct net_device *arp_req_dev_by_name(struct net *net, struct arpreq *r,
1007*bf4ea588SKuniyuki Iwashima 					      bool getarp)
1008a428bfc7SKuniyuki Iwashima {
1009a428bfc7SKuniyuki Iwashima 	struct net_device *dev;
1010a428bfc7SKuniyuki Iwashima 
1011*bf4ea588SKuniyuki Iwashima 	if (getarp)
1012*bf4ea588SKuniyuki Iwashima 		dev = dev_get_by_name_rcu(net, r->arp_dev);
1013*bf4ea588SKuniyuki Iwashima 	else
1014a428bfc7SKuniyuki Iwashima 		dev = __dev_get_by_name(net, r->arp_dev);
1015a428bfc7SKuniyuki Iwashima 	if (!dev)
1016a428bfc7SKuniyuki Iwashima 		return ERR_PTR(-ENODEV);
1017a428bfc7SKuniyuki Iwashima 
1018a428bfc7SKuniyuki Iwashima 	/* Mmmm... It is wrong... ARPHRD_NETROM == 0 */
1019a428bfc7SKuniyuki Iwashima 	if (!r->arp_ha.sa_family)
1020a428bfc7SKuniyuki Iwashima 		r->arp_ha.sa_family = dev->type;
1021a428bfc7SKuniyuki Iwashima 
1022a428bfc7SKuniyuki Iwashima 	if ((r->arp_flags & ATF_COM) && r->arp_ha.sa_family != dev->type)
1023a428bfc7SKuniyuki Iwashima 		return ERR_PTR(-EINVAL);
1024a428bfc7SKuniyuki Iwashima 
1025a428bfc7SKuniyuki Iwashima 	return dev;
1026a428bfc7SKuniyuki Iwashima }
1027a428bfc7SKuniyuki Iwashima 
arp_req_dev(struct net * net,struct arpreq * r)1028f8696133SKuniyuki Iwashima static struct net_device *arp_req_dev(struct net *net, struct arpreq *r)
1029f8696133SKuniyuki Iwashima {
1030f8696133SKuniyuki Iwashima 	struct net_device *dev;
1031f8696133SKuniyuki Iwashima 	struct rtable *rt;
1032f8696133SKuniyuki Iwashima 	__be32 ip;
1033f8696133SKuniyuki Iwashima 
1034a428bfc7SKuniyuki Iwashima 	if (r->arp_dev[0])
1035*bf4ea588SKuniyuki Iwashima 		return arp_req_dev_by_name(net, r, false);
1036a428bfc7SKuniyuki Iwashima 
1037a428bfc7SKuniyuki Iwashima 	if (r->arp_flags & ATF_PUBL)
1038a428bfc7SKuniyuki Iwashima 		return NULL;
1039a428bfc7SKuniyuki Iwashima 
1040f8696133SKuniyuki Iwashima 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1041f8696133SKuniyuki Iwashima 
1042f8696133SKuniyuki Iwashima 	rt = ip_route_output(net, ip, 0, 0, 0, RT_SCOPE_LINK);
1043f8696133SKuniyuki Iwashima 	if (IS_ERR(rt))
1044f8696133SKuniyuki Iwashima 		return ERR_CAST(rt);
1045f8696133SKuniyuki Iwashima 
1046f8696133SKuniyuki Iwashima 	dev = rt->dst.dev;
1047f8696133SKuniyuki Iwashima 	ip_rt_put(rt);
1048f8696133SKuniyuki Iwashima 
1049f8696133SKuniyuki Iwashima 	if (!dev)
1050f8696133SKuniyuki Iwashima 		return ERR_PTR(-EINVAL);
1051f8696133SKuniyuki Iwashima 
1052f8696133SKuniyuki Iwashima 	return dev;
1053f8696133SKuniyuki Iwashima }
1054f8696133SKuniyuki Iwashima 
10551da177e4SLinus Torvalds /*
10561da177e4SLinus Torvalds  *	Set (create) an ARP cache entry.
10571da177e4SLinus Torvalds  */
10581da177e4SLinus Torvalds 
arp_req_set_proxy(struct net * net,struct net_device * dev,int on)105932e569b7SPavel Emelyanov static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
1060f8b33fdfSPavel Emelyanov {
106151456b29SIan Morris 	if (!dev) {
1062586f1211SPavel Emelyanov 		IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
1063f8b33fdfSPavel Emelyanov 		return 0;
1064f8b33fdfSPavel Emelyanov 	}
1065c506653dSEric Dumazet 	if (__in_dev_get_rtnl(dev)) {
1066c506653dSEric Dumazet 		IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
1067f8b33fdfSPavel Emelyanov 		return 0;
1068f8b33fdfSPavel Emelyanov 	}
1069f8b33fdfSPavel Emelyanov 	return -ENXIO;
1070f8b33fdfSPavel Emelyanov }
1071f8b33fdfSPavel Emelyanov 
arp_req_set_public(struct net * net,struct arpreq * r,struct net_device * dev)107232e569b7SPavel Emelyanov static int arp_req_set_public(struct net *net, struct arpreq *r,
107332e569b7SPavel Emelyanov 		struct net_device *dev)
10741da177e4SLinus Torvalds {
1075ed9bad06SAl Viro 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
107643dc1701SPavel Emelyanov 
10771da177e4SLinus Torvalds 	if (!dev && (r->arp_flags & ATF_COM)) {
1078941666c2SEric Dumazet 		dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
107943dc1701SPavel Emelyanov 				      r->arp_ha.sa_data);
10801da177e4SLinus Torvalds 		if (!dev)
10811da177e4SLinus Torvalds 			return -ENODEV;
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 	if (mask) {
108405923674SKuniyuki Iwashima 		__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
108505923674SKuniyuki Iwashima 
108651456b29SIan Morris 		if (!pneigh_lookup(&arp_tbl, net, &ip, dev, 1))
10871da177e4SLinus Torvalds 			return -ENOBUFS;
10881da177e4SLinus Torvalds 		return 0;
10891da177e4SLinus Torvalds 	}
1090f8b33fdfSPavel Emelyanov 
109132e569b7SPavel Emelyanov 	return arp_req_set_proxy(net, dev, 1);
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
arp_req_set(struct net * net,struct arpreq * r)1094a428bfc7SKuniyuki Iwashima static int arp_req_set(struct net *net, struct arpreq *r)
109543dc1701SPavel Emelyanov {
109643dc1701SPavel Emelyanov 	struct neighbour *neigh;
1097a428bfc7SKuniyuki Iwashima 	struct net_device *dev;
1098f8696133SKuniyuki Iwashima 	__be32 ip;
109943dc1701SPavel Emelyanov 	int err;
110043dc1701SPavel Emelyanov 
1101a428bfc7SKuniyuki Iwashima 	dev = arp_req_dev(net, r);
1102a428bfc7SKuniyuki Iwashima 	if (IS_ERR(dev))
1103a428bfc7SKuniyuki Iwashima 		return PTR_ERR(dev);
1104a428bfc7SKuniyuki Iwashima 
110543dc1701SPavel Emelyanov 	if (r->arp_flags & ATF_PUBL)
110632e569b7SPavel Emelyanov 		return arp_req_set_public(net, r, dev);
110743dc1701SPavel Emelyanov 
11081da177e4SLinus Torvalds 	switch (dev->type) {
110940e4783eSIgor Maravic #if IS_ENABLED(CONFIG_FDDI)
11101da177e4SLinus Torvalds 	case ARPHRD_FDDI:
11111da177e4SLinus Torvalds 		/*
11121da177e4SLinus Torvalds 		 * According to RFC 1390, FDDI devices should accept ARP
11131da177e4SLinus Torvalds 		 * hardware types of 1 (Ethernet).  However, to be more
11141da177e4SLinus Torvalds 		 * robust, we'll accept hardware types of either 1 (Ethernet)
11151da177e4SLinus Torvalds 		 * or 6 (IEEE 802.2).
11161da177e4SLinus Torvalds 		 */
11171da177e4SLinus Torvalds 		if (r->arp_ha.sa_family != ARPHRD_FDDI &&
11181da177e4SLinus Torvalds 		    r->arp_ha.sa_family != ARPHRD_ETHER &&
11191da177e4SLinus Torvalds 		    r->arp_ha.sa_family != ARPHRD_IEEE802)
11201da177e4SLinus Torvalds 			return -EINVAL;
11211da177e4SLinus Torvalds 		break;
11221da177e4SLinus Torvalds #endif
11231da177e4SLinus Torvalds 	default:
11241da177e4SLinus Torvalds 		if (r->arp_ha.sa_family != dev->type)
11251da177e4SLinus Torvalds 			return -EINVAL;
11261da177e4SLinus Torvalds 		break;
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 
1129f8696133SKuniyuki Iwashima 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1130f8696133SKuniyuki Iwashima 
11311da177e4SLinus Torvalds 	neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
11321da177e4SLinus Torvalds 	err = PTR_ERR(neigh);
11331da177e4SLinus Torvalds 	if (!IS_ERR(neigh)) {
113495c96174SEric Dumazet 		unsigned int state = NUD_STALE;
113542033d0cSKuniyuki Iwashima 
113642033d0cSKuniyuki Iwashima 		if (r->arp_flags & ATF_PERM) {
113742033d0cSKuniyuki Iwashima 			r->arp_flags |= ATF_COM;
11381da177e4SLinus Torvalds 			state = NUD_PERMANENT;
113942033d0cSKuniyuki Iwashima 		}
114042033d0cSKuniyuki Iwashima 
11411da177e4SLinus Torvalds 		err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
11421da177e4SLinus Torvalds 				   r->arp_ha.sa_data : NULL, state,
11431da177e4SLinus Torvalds 				   NEIGH_UPDATE_F_OVERRIDE |
11447b8f7a40SRoopa Prabhu 				   NEIGH_UPDATE_F_ADMIN, 0);
11451da177e4SLinus Torvalds 		neigh_release(neigh);
11461da177e4SLinus Torvalds 	}
11471da177e4SLinus Torvalds 	return err;
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
arp_state_to_flags(struct neighbour * neigh)115095c96174SEric Dumazet static unsigned int arp_state_to_flags(struct neighbour *neigh)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds 	if (neigh->nud_state&NUD_PERMANENT)
1153deffd777SChangli Gao 		return ATF_PERM | ATF_COM;
11541da177e4SLinus Torvalds 	else if (neigh->nud_state&NUD_VALID)
1155deffd777SChangli Gao 		return ATF_COM;
1156deffd777SChangli Gao 	else
1157deffd777SChangli Gao 		return 0;
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds /*
11611da177e4SLinus Torvalds  *	Get an ARP cache entry.
11621da177e4SLinus Torvalds  */
11631da177e4SLinus Torvalds 
arp_req_get(struct net * net,struct arpreq * r)1164a428bfc7SKuniyuki Iwashima static int arp_req_get(struct net *net, struct arpreq *r)
11651da177e4SLinus Torvalds {
1166ed9bad06SAl Viro 	__be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
11671da177e4SLinus Torvalds 	struct neighbour *neigh;
1168a428bfc7SKuniyuki Iwashima 	struct net_device *dev;
1169a428bfc7SKuniyuki Iwashima 
1170a428bfc7SKuniyuki Iwashima 	if (!r->arp_dev[0])
1171a428bfc7SKuniyuki Iwashima 		return -ENODEV;
1172a428bfc7SKuniyuki Iwashima 
1173*bf4ea588SKuniyuki Iwashima 	dev = arp_req_dev_by_name(net, r, true);
1174a428bfc7SKuniyuki Iwashima 	if (IS_ERR(dev))
1175a428bfc7SKuniyuki Iwashima 		return PTR_ERR(dev);
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	neigh = neigh_lookup(&arp_tbl, &ip, dev);
117851e9ba48SKuniyuki Iwashima 	if (!neigh)
117951e9ba48SKuniyuki Iwashima 		return -ENXIO;
118051e9ba48SKuniyuki Iwashima 
118151e9ba48SKuniyuki Iwashima 	if (READ_ONCE(neigh->nud_state) & NUD_NOARP) {
118251e9ba48SKuniyuki Iwashima 		neigh_release(neigh);
118351e9ba48SKuniyuki Iwashima 		return -ENXIO;
118451e9ba48SKuniyuki Iwashima 	}
118551e9ba48SKuniyuki Iwashima 
11861da177e4SLinus Torvalds 	read_lock_bh(&neigh->lock);
1187a7d60277SKuniyuki Iwashima 	memcpy(r->arp_ha.sa_data, neigh->ha,
1188a7d60277SKuniyuki Iwashima 	       min(dev->addr_len, sizeof(r->arp_ha.sa_data_min)));
11891da177e4SLinus Torvalds 	r->arp_flags = arp_state_to_flags(neigh);
11901da177e4SLinus Torvalds 	read_unlock_bh(&neigh->lock);
119151e9ba48SKuniyuki Iwashima 
119251e9ba48SKuniyuki Iwashima 	neigh_release(neigh);
119351e9ba48SKuniyuki Iwashima 
11941da177e4SLinus Torvalds 	r->arp_ha.sa_family = dev->type;
1195*bf4ea588SKuniyuki Iwashima 	netdev_copy_name(dev, r->arp_dev);
119651e9ba48SKuniyuki Iwashima 
119751e9ba48SKuniyuki Iwashima 	return 0;
11981da177e4SLinus Torvalds }
11991da177e4SLinus Torvalds 
arp_invalidate(struct net_device * dev,__be32 ip,bool force)12000c51e12eSIdo Schimmel int arp_invalidate(struct net_device *dev, __be32 ip, bool force)
1201545ecdc3SMaxim Levitsky {
1202545ecdc3SMaxim Levitsky 	struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
1203545ecdc3SMaxim Levitsky 	int err = -ENXIO;
12045071034eSSowmini Varadhan 	struct neigh_table *tbl = &arp_tbl;
1205545ecdc3SMaxim Levitsky 
1206545ecdc3SMaxim Levitsky 	if (neigh) {
1207b071af52SEric Dumazet 		if ((READ_ONCE(neigh->nud_state) & NUD_VALID) && !force) {
12080c51e12eSIdo Schimmel 			neigh_release(neigh);
12090c51e12eSIdo Schimmel 			return 0;
12100c51e12eSIdo Schimmel 		}
12110c51e12eSIdo Schimmel 
1212b071af52SEric Dumazet 		if (READ_ONCE(neigh->nud_state) & ~NUD_NOARP)
1213545ecdc3SMaxim Levitsky 			err = neigh_update(neigh, NULL, NUD_FAILED,
1214545ecdc3SMaxim Levitsky 					   NEIGH_UPDATE_F_OVERRIDE|
12157b8f7a40SRoopa Prabhu 					   NEIGH_UPDATE_F_ADMIN, 0);
12165071034eSSowmini Varadhan 		write_lock_bh(&tbl->lock);
1217545ecdc3SMaxim Levitsky 		neigh_release(neigh);
12185071034eSSowmini Varadhan 		neigh_remove_one(neigh, tbl);
12195071034eSSowmini Varadhan 		write_unlock_bh(&tbl->lock);
1220545ecdc3SMaxim Levitsky 	}
1221545ecdc3SMaxim Levitsky 
1222545ecdc3SMaxim Levitsky 	return err;
1223545ecdc3SMaxim Levitsky }
1224545ecdc3SMaxim Levitsky 
arp_req_delete_public(struct net * net,struct arpreq * r,struct net_device * dev)122532e569b7SPavel Emelyanov static int arp_req_delete_public(struct net *net, struct arpreq *r,
122632e569b7SPavel Emelyanov 		struct net_device *dev)
12271da177e4SLinus Torvalds {
122846479b43SPavel Emelyanov 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
12291da177e4SLinus Torvalds 
123005923674SKuniyuki Iwashima 	if (mask) {
123105923674SKuniyuki Iwashima 		__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
123246479b43SPavel Emelyanov 
123305923674SKuniyuki Iwashima 		return pneigh_delete(&arp_tbl, net, &ip, dev);
123405923674SKuniyuki Iwashima 	}
1235f8b33fdfSPavel Emelyanov 
123632e569b7SPavel Emelyanov 	return arp_req_set_proxy(net, dev, 0);
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
arp_req_delete(struct net * net,struct arpreq * r)1239a428bfc7SKuniyuki Iwashima static int arp_req_delete(struct net *net, struct arpreq *r)
124046479b43SPavel Emelyanov {
1241a428bfc7SKuniyuki Iwashima 	struct net_device *dev;
124246479b43SPavel Emelyanov 	__be32 ip;
124346479b43SPavel Emelyanov 
1244a428bfc7SKuniyuki Iwashima 	dev = arp_req_dev(net, r);
1245a428bfc7SKuniyuki Iwashima 	if (IS_ERR(dev))
1246a428bfc7SKuniyuki Iwashima 		return PTR_ERR(dev);
1247a428bfc7SKuniyuki Iwashima 
124846479b43SPavel Emelyanov 	if (r->arp_flags & ATF_PUBL)
124932e569b7SPavel Emelyanov 		return arp_req_delete_public(net, r, dev);
125046479b43SPavel Emelyanov 
125146479b43SPavel Emelyanov 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1252a428bfc7SKuniyuki Iwashima 
12530c51e12eSIdo Schimmel 	return arp_invalidate(dev, ip, true);
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds /*
12571da177e4SLinus Torvalds  *	Handle an ARP layer I/O control request.
12581da177e4SLinus Torvalds  */
12591da177e4SLinus Torvalds 
arp_ioctl(struct net * net,unsigned int cmd,void __user * arg)126032e569b7SPavel Emelyanov int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
12611da177e4SLinus Torvalds {
126205923674SKuniyuki Iwashima 	struct arpreq r;
126305923674SKuniyuki Iwashima 	__be32 *netmask;
126405923674SKuniyuki Iwashima 	int err;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	switch (cmd) {
12671da177e4SLinus Torvalds 	case SIOCDARP:
12681da177e4SLinus Torvalds 	case SIOCSARP:
126952e804c6SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
12701da177e4SLinus Torvalds 			return -EPERM;
1271a8eceea8SJoe Perches 		fallthrough;
12721da177e4SLinus Torvalds 	case SIOCGARP:
12731da177e4SLinus Torvalds 		err = copy_from_user(&r, arg, sizeof(struct arpreq));
12741da177e4SLinus Torvalds 		if (err)
12751da177e4SLinus Torvalds 			return -EFAULT;
12761da177e4SLinus Torvalds 		break;
12771da177e4SLinus Torvalds 	default:
12781da177e4SLinus Torvalds 		return -EINVAL;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	if (r.arp_pa.sa_family != AF_INET)
12821da177e4SLinus Torvalds 		return -EPFNOSUPPORT;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	if (!(r.arp_flags & ATF_PUBL) &&
12851da177e4SLinus Torvalds 	    (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
12861da177e4SLinus Torvalds 		return -EINVAL;
128705923674SKuniyuki Iwashima 
128805923674SKuniyuki Iwashima 	netmask = &((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr;
12891da177e4SLinus Torvalds 	if (!(r.arp_flags & ATF_NETMASK))
129005923674SKuniyuki Iwashima 		*netmask = htonl(0xFFFFFFFFUL);
129105923674SKuniyuki Iwashima 	else if (*netmask && *netmask != htonl(0xFFFFFFFFUL))
129205923674SKuniyuki Iwashima 		return -EINVAL;
129305923674SKuniyuki Iwashima 
12941da177e4SLinus Torvalds 	switch (cmd) {
12951da177e4SLinus Torvalds 	case SIOCDARP:
1296*bf4ea588SKuniyuki Iwashima 		rtnl_lock();
1297a428bfc7SKuniyuki Iwashima 		err = arp_req_delete(net, &r);
1298*bf4ea588SKuniyuki Iwashima 		rtnl_unlock();
12991da177e4SLinus Torvalds 		break;
13001da177e4SLinus Torvalds 	case SIOCSARP:
1301*bf4ea588SKuniyuki Iwashima 		rtnl_lock();
1302a428bfc7SKuniyuki Iwashima 		err = arp_req_set(net, &r);
1303*bf4ea588SKuniyuki Iwashima 		rtnl_unlock();
13041da177e4SLinus Torvalds 		break;
13051da177e4SLinus Torvalds 	case SIOCGARP:
1306*bf4ea588SKuniyuki Iwashima 		rcu_read_lock();
1307a428bfc7SKuniyuki Iwashima 		err = arp_req_get(net, &r);
1308*bf4ea588SKuniyuki Iwashima 		rcu_read_unlock();
1309*bf4ea588SKuniyuki Iwashima 
1310*bf4ea588SKuniyuki Iwashima 		if (!err && copy_to_user(arg, &r, sizeof(r)))
1311*bf4ea588SKuniyuki Iwashima 			err = -EFAULT;
13121da177e4SLinus Torvalds 		break;
13131da177e4SLinus Torvalds 	}
1314a428bfc7SKuniyuki Iwashima 
13151da177e4SLinus Torvalds 	return err;
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
arp_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)1318deffd777SChangli Gao static int arp_netdev_event(struct notifier_block *this, unsigned long event,
1319deffd777SChangli Gao 			    void *ptr)
13201da177e4SLinus Torvalds {
1321351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
13226c8b4e3fSTimo Teräs 	struct netdev_notifier_change_info *change_info;
1323fcdb44d0SJames Prestwood 	struct in_device *in_dev;
1324fcdb44d0SJames Prestwood 	bool evict_nocarrier;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	switch (event) {
13271da177e4SLinus Torvalds 	case NETDEV_CHANGEADDR:
13281da177e4SLinus Torvalds 		neigh_changeaddr(&arp_tbl, dev);
1329bafa6d9dSNicolas Dichtel 		rt_cache_flush(dev_net(dev));
13301da177e4SLinus Torvalds 		break;
13316c8b4e3fSTimo Teräs 	case NETDEV_CHANGE:
13326c8b4e3fSTimo Teräs 		change_info = ptr;
13336c8b4e3fSTimo Teräs 		if (change_info->flags_changed & IFF_NOARP)
13346c8b4e3fSTimo Teräs 			neigh_changeaddr(&arp_tbl, dev);
1335fcdb44d0SJames Prestwood 
1336fcdb44d0SJames Prestwood 		in_dev = __in_dev_get_rtnl(dev);
1337fcdb44d0SJames Prestwood 		if (!in_dev)
1338fcdb44d0SJames Prestwood 			evict_nocarrier = true;
1339fcdb44d0SJames Prestwood 		else
1340fcdb44d0SJames Prestwood 			evict_nocarrier = IN_DEV_ARP_EVICT_NOCARRIER(in_dev);
1341fcdb44d0SJames Prestwood 
1342fcdb44d0SJames Prestwood 		if (evict_nocarrier && !netif_carrier_ok(dev))
1343859bd2efSDavid Ahern 			neigh_carrier_down(&arp_tbl, dev);
13446c8b4e3fSTimo Teräs 		break;
13451da177e4SLinus Torvalds 	default:
13461da177e4SLinus Torvalds 		break;
13471da177e4SLinus Torvalds 	}
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	return NOTIFY_DONE;
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds static struct notifier_block arp_netdev_notifier = {
13531da177e4SLinus Torvalds 	.notifier_call = arp_netdev_event,
13541da177e4SLinus Torvalds };
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds /* Note, that it is not on notifier chain.
13571da177e4SLinus Torvalds    It is necessary, that this routine was called after route cache will be
13581da177e4SLinus Torvalds    flushed.
13591da177e4SLinus Torvalds  */
arp_ifdown(struct net_device * dev)13601da177e4SLinus Torvalds void arp_ifdown(struct net_device *dev)
13611da177e4SLinus Torvalds {
13621da177e4SLinus Torvalds 	neigh_ifdown(&arp_tbl, dev);
13631da177e4SLinus Torvalds }
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds /*
13671da177e4SLinus Torvalds  *	Called once on startup.
13681da177e4SLinus Torvalds  */
13691da177e4SLinus Torvalds 
13707546dd97SStephen Hemminger static struct packet_type arp_packet_type __read_mostly = {
137109640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_ARP),
13721da177e4SLinus Torvalds 	.func =	arp_rcv,
13731da177e4SLinus Torvalds };
13741da177e4SLinus Torvalds 
1375b0e653b2SYajun Deng #ifdef CONFIG_PROC_FS
137640e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25)
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds /*
13791da177e4SLinus Torvalds  *	ax25 -> ASCII conversion
13801da177e4SLinus Torvalds  */
ax2asc2(ax25_address * a,char * buf)13814872e57cSRalf Baechle static void ax2asc2(ax25_address *a, char *buf)
13821da177e4SLinus Torvalds {
13831da177e4SLinus Torvalds 	char c, *s;
13841da177e4SLinus Torvalds 	int n;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	for (n = 0, s = buf; n < 6; n++) {
13871da177e4SLinus Torvalds 		c = (a->ax25_call[n] >> 1) & 0x7F;
13881da177e4SLinus Torvalds 
1389deffd777SChangli Gao 		if (c != ' ')
1390deffd777SChangli Gao 			*s++ = c;
13911da177e4SLinus Torvalds 	}
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	*s++ = '-';
1394deffd777SChangli Gao 	n = (a->ax25_call[6] >> 1) & 0x0F;
1395deffd777SChangli Gao 	if (n > 9) {
13961da177e4SLinus Torvalds 		*s++ = '1';
13971da177e4SLinus Torvalds 		n -= 10;
13981da177e4SLinus Torvalds 	}
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	*s++ = n + '0';
14011da177e4SLinus Torvalds 	*s++ = '\0';
14021da177e4SLinus Torvalds 
14034872e57cSRalf Baechle 	if (*buf == '\0' || *buf == '-') {
14044872e57cSRalf Baechle 		buf[0] = '*';
14054872e57cSRalf Baechle 		buf[1] = '\0';
14064872e57cSRalf Baechle 	}
14071da177e4SLinus Torvalds }
14081da177e4SLinus Torvalds #endif /* CONFIG_AX25 */
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds #define HBUFFERLEN 30
14111da177e4SLinus Torvalds 
arp_format_neigh_entry(struct seq_file * seq,struct neighbour * n)14121da177e4SLinus Torvalds static void arp_format_neigh_entry(struct seq_file *seq,
14131da177e4SLinus Torvalds 				   struct neighbour *n)
14141da177e4SLinus Torvalds {
14151da177e4SLinus Torvalds 	char hbuffer[HBUFFERLEN];
14161da177e4SLinus Torvalds 	int k, j;
14171da177e4SLinus Torvalds 	char tbuf[16];
14181da177e4SLinus Torvalds 	struct net_device *dev = n->dev;
14191da177e4SLinus Torvalds 	int hatype = dev->type;
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	read_lock(&n->lock);
14221da177e4SLinus Torvalds 	/* Convert hardware address to XX:XX:XX:XX ... form. */
142340e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25)
14241da177e4SLinus Torvalds 	if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
14251da177e4SLinus Torvalds 		ax2asc2((ax25_address *)n->ha, hbuffer);
14261da177e4SLinus Torvalds 	else {
14271da177e4SLinus Torvalds #endif
14281da177e4SLinus Torvalds 	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
142951f82a2bSDenis Cheng 		hbuffer[k++] = hex_asc_hi(n->ha[j]);
143051f82a2bSDenis Cheng 		hbuffer[k++] = hex_asc_lo(n->ha[j]);
14311da177e4SLinus Torvalds 		hbuffer[k++] = ':';
14321da177e4SLinus Torvalds 	}
1433a3e8ee68Sroel kluin 	if (k != 0)
1434a3e8ee68Sroel kluin 		--k;
1435a3e8ee68Sroel kluin 	hbuffer[k] = 0;
143640e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25)
14371da177e4SLinus Torvalds 	}
14381da177e4SLinus Torvalds #endif
1439673d57e7SHarvey Harrison 	sprintf(tbuf, "%pI4", n->primary_key);
14404872e57cSRalf Baechle 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%-17s     *        %s\n",
14411da177e4SLinus Torvalds 		   tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
14421da177e4SLinus Torvalds 	read_unlock(&n->lock);
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
arp_format_pneigh_entry(struct seq_file * seq,struct pneigh_entry * n)14451da177e4SLinus Torvalds static void arp_format_pneigh_entry(struct seq_file *seq,
14461da177e4SLinus Torvalds 				    struct pneigh_entry *n)
14471da177e4SLinus Torvalds {
14481da177e4SLinus Torvalds 	struct net_device *dev = n->dev;
14491da177e4SLinus Torvalds 	int hatype = dev ? dev->type : 0;
14501da177e4SLinus Torvalds 	char tbuf[16];
14511da177e4SLinus Torvalds 
1452673d57e7SHarvey Harrison 	sprintf(tbuf, "%pI4", n->key);
14531da177e4SLinus Torvalds 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%s     *        %s\n",
14541da177e4SLinus Torvalds 		   tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
14551da177e4SLinus Torvalds 		   dev ? dev->name : "*");
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
arp_seq_show(struct seq_file * seq,void * v)14581da177e4SLinus Torvalds static int arp_seq_show(struct seq_file *seq, void *v)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
14611da177e4SLinus Torvalds 		seq_puts(seq, "IP address       HW type     Flags       "
14621da177e4SLinus Torvalds 			      "HW address            Mask     Device\n");
14631da177e4SLinus Torvalds 	} else {
14641da177e4SLinus Torvalds 		struct neigh_seq_state *state = seq->private;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 		if (state->flags & NEIGH_SEQ_IS_PNEIGH)
14671da177e4SLinus Torvalds 			arp_format_pneigh_entry(seq, v);
14681da177e4SLinus Torvalds 		else
14691da177e4SLinus Torvalds 			arp_format_neigh_entry(seq, v);
14701da177e4SLinus Torvalds 	}
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	return 0;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
arp_seq_start(struct seq_file * seq,loff_t * pos)14751da177e4SLinus Torvalds static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
14761da177e4SLinus Torvalds {
14771da177e4SLinus Torvalds 	/* Don't want to confuse "arp -a" w/ magic entries,
14781da177e4SLinus Torvalds 	 * so we tell the generic iterator to skip NUD_NOARP.
14791da177e4SLinus Torvalds 	 */
14801da177e4SLinus Torvalds 	return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
1483f690808eSStephen Hemminger static const struct seq_operations arp_seq_ops = {
14841da177e4SLinus Torvalds 	.start	= arp_seq_start,
14851da177e4SLinus Torvalds 	.next	= neigh_seq_next,
14861da177e4SLinus Torvalds 	.stop	= neigh_seq_stop,
14871da177e4SLinus Torvalds 	.show	= arp_seq_show,
14881da177e4SLinus Torvalds };
1489b0e653b2SYajun Deng #endif /* CONFIG_PROC_FS */
1490ffc31d3dSDenis V. Lunev 
arp_net_init(struct net * net)1491ffc31d3dSDenis V. Lunev static int __net_init arp_net_init(struct net *net)
14921da177e4SLinus Torvalds {
1493c3506372SChristoph Hellwig 	if (!proc_create_net("arp", 0444, net->proc_net, &arp_seq_ops,
1494c3506372SChristoph Hellwig 			sizeof(struct neigh_seq_state)))
14951da177e4SLinus Torvalds 		return -ENOMEM;
14961da177e4SLinus Torvalds 	return 0;
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
arp_net_exit(struct net * net)1499ffc31d3dSDenis V. Lunev static void __net_exit arp_net_exit(struct net *net)
1500ffc31d3dSDenis V. Lunev {
1501ece31ffdSGao feng 	remove_proc_entry("arp", net->proc_net);
1502ffc31d3dSDenis V. Lunev }
1503ffc31d3dSDenis V. Lunev 
1504ffc31d3dSDenis V. Lunev static struct pernet_operations arp_net_ops = {
1505ffc31d3dSDenis V. Lunev 	.init = arp_net_init,
1506ffc31d3dSDenis V. Lunev 	.exit = arp_net_exit,
1507ffc31d3dSDenis V. Lunev };
1508ffc31d3dSDenis V. Lunev 
arp_init(void)1509e968b1b3SYajun Deng void __init arp_init(void)
1510ffc31d3dSDenis V. Lunev {
1511e968b1b3SYajun Deng 	neigh_table_init(NEIGH_ARP_TABLE, &arp_tbl);
1512e968b1b3SYajun Deng 
1513e968b1b3SYajun Deng 	dev_add_pack(&arp_packet_type);
1514e968b1b3SYajun Deng 	register_pernet_subsys(&arp_net_ops);
1515e968b1b3SYajun Deng #ifdef CONFIG_SYSCTL
1516e968b1b3SYajun Deng 	neigh_sysctl_register(NULL, &arp_tbl.parms, NULL);
1517e968b1b3SYajun Deng #endif
1518e968b1b3SYajun Deng 	register_netdevice_notifier(&arp_netdev_notifier);
1519ffc31d3dSDenis V. Lunev }
1520