xref: /linux/net/ipv4/arp.c (revision f2f25d59f56a0a225447af203d73e51b4f7f92f5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* linux/net/ipv4/arp.c
3  *
4  * Copyright (C) 1994 by Florian  La Roche
5  *
6  * This module implements the Address Resolution Protocol ARP (RFC 826),
7  * which is used to convert IP addresses (or in the future maybe other
8  * high-level addresses) into a low-level hardware address (like an Ethernet
9  * address).
10  *
11  * Fixes:
12  *		Alan Cox	:	Removed the Ethernet assumptions in
13  *					Florian's code
14  *		Alan Cox	:	Fixed some small errors in the ARP
15  *					logic
16  *		Alan Cox	:	Allow >4K in /proc
17  *		Alan Cox	:	Make ARP add its own protocol entry
18  *		Ross Martin     :       Rewrote arp_rcv() and arp_get_info()
19  *		Stephen Henson	:	Add AX25 support to arp_get_info()
20  *		Alan Cox	:	Drop data when a device is downed.
21  *		Alan Cox	:	Use init_timer().
22  *		Alan Cox	:	Double lock fixes.
23  *		Martin Seine	:	Move the arphdr structure
24  *					to if_arp.h for compatibility.
25  *					with BSD based programs.
26  *		Andrew Tridgell :       Added ARP netmask code and
27  *					re-arranged proxy handling.
28  *		Alan Cox	:	Changed to use notifiers.
29  *		Niibe Yutaka	:	Reply for this device or proxies only.
30  *		Alan Cox	:	Don't proxy across hardware types!
31  *		Jonathan Naylor :	Added support for NET/ROM.
32  *		Mike Shaver     :       RFC1122 checks.
33  *		Jonathan Naylor :	Only lookup the hardware address for
34  *					the correct hardware type.
35  *		Germano Caronni	:	Assorted subtle races.
36  *		Craig Schlenter :	Don't modify permanent entry
37  *					during arp_rcv.
38  *		Russ Nelson	:	Tidied up a few bits.
39  *		Alexey Kuznetsov:	Major changes to caching and behaviour,
40  *					eg intelligent arp probing and
41  *					generation
42  *					of host down events.
43  *		Alan Cox	:	Missing unlock in device events.
44  *		Eckes		:	ARP ioctl control errors.
45  *		Alexey Kuznetsov:	Arp free fix.
46  *		Manuel Rodriguez:	Gratuitous ARP.
47  *              Jonathan Layes  :       Added arpd support through kerneld
48  *                                      message queue (960314)
49  *		Mike Shaver	:	/proc/sys/net/ipv4/arp_* support
50  *		Mike McLagan    :	Routing by source
51  *		Stuart Cheshire	:	Metricom and grat arp fixes
52  *					*** FOR 2.1 clean this up ***
53  *		Lawrence V. Stefani: (08/12/96) Added FDDI support.
54  *		Alan Cox	:	Took the AP1000 nasty FDDI hack and
55  *					folded into the mainstream FDDI code.
56  *					Ack spit, Linus how did you allow that
57  *					one in...
58  *		Jes Sorensen	:	Make FDDI work again in 2.1.x and
59  *					clean up the APFDDI & gen. FDDI bits.
60  *		Alexey Kuznetsov:	new arp state machine;
61  *					now it is in net/core/neighbour.c.
62  *		Krzysztof Halasa:	Added Frame Relay ARP support.
63  *		Arnaldo C. Melo :	convert /proc/net/arp to seq_file
64  *		Shmulik Hen:		Split arp_send to arp_create and
65  *					arp_xmit so intermediate drivers like
66  *					bonding can change the skb before
67  *					sending (e.g. insert 8021q tag).
68  *		Harald Welte	:	convert to make use of jenkins hash
69  *		Jesper D. Brouer:       Proxy ARP PVLAN RFC 3069 support.
70  */
71 
72 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
73 
74 #include <linux/module.h>
75 #include <linux/types.h>
76 #include <linux/string.h>
77 #include <linux/kernel.h>
78 #include <linux/capability.h>
79 #include <linux/socket.h>
80 #include <linux/sockios.h>
81 #include <linux/errno.h>
82 #include <linux/hex.h>
83 #include <linux/in.h>
84 #include <linux/mm.h>
85 #include <linux/inet.h>
86 #include <linux/inetdevice.h>
87 #include <linux/netdevice.h>
88 #include <linux/etherdevice.h>
89 #include <linux/fddidevice.h>
90 #include <linux/if_arp.h>
91 #include <linux/skbuff.h>
92 #include <linux/proc_fs.h>
93 #include <linux/seq_file.h>
94 #include <linux/stat.h>
95 #include <linux/init.h>
96 #include <linux/net.h>
97 #include <linux/rcupdate.h>
98 #include <linux/slab.h>
99 #ifdef CONFIG_SYSCTL
100 #include <linux/sysctl.h>
101 #endif
102 
103 #include <net/net_namespace.h>
104 #include <net/ip.h>
105 #include <net/icmp.h>
106 #include <net/route.h>
107 #include <net/protocol.h>
108 #include <net/tcp.h>
109 #include <net/sock.h>
110 #include <net/arp.h>
111 #include <net/ax25.h>
112 #include <net/dst_metadata.h>
113 #include <net/ip_tunnels.h>
114 
115 #include <linux/uaccess.h>
116 
117 #include <linux/netfilter_arp.h>
118 
119 /*
120  *	Interface to generic neighbour cache.
121  */
122 static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd);
123 static bool arp_key_eq(const struct neighbour *n, const void *pkey);
124 static int arp_constructor(struct neighbour *neigh);
125 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
126 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
127 static void parp_redo(struct sk_buff *skb);
128 static int arp_is_multicast(const void *pkey);
129 
130 static const struct neigh_ops arp_generic_ops = {
131 	.family =		AF_INET,
132 	.solicit =		arp_solicit,
133 	.error_report =		arp_error_report,
134 	.output =		neigh_resolve_output,
135 	.connected_output =	neigh_connected_output,
136 };
137 
138 static const struct neigh_ops arp_hh_ops = {
139 	.family =		AF_INET,
140 	.solicit =		arp_solicit,
141 	.error_report =		arp_error_report,
142 	.output =		neigh_resolve_output,
143 	.connected_output =	neigh_resolve_output,
144 };
145 
146 static const struct neigh_ops arp_direct_ops = {
147 	.family =		AF_INET,
148 	.output =		neigh_direct_output,
149 	.connected_output =	neigh_direct_output,
150 };
151 
152 static struct neigh_table arp_tbl = {
153 	.family		= AF_INET,
154 	.key_len	= 4,
155 	.protocol	= cpu_to_be16(ETH_P_IP),
156 	.hash		= arp_hash,
157 	.key_eq		= arp_key_eq,
158 	.constructor	= arp_constructor,
159 	.proxy_redo	= parp_redo,
160 	.is_multicast	= arp_is_multicast,
161 	.id		= "arp_cache",
162 	.parms		= {
163 		.tbl			= &arp_tbl,
164 		.reachable_time		= 30 * HZ,
165 		.data	= {
166 			[NEIGH_VAR_MCAST_PROBES] = 3,
167 			[NEIGH_VAR_UCAST_PROBES] = 3,
168 			[NEIGH_VAR_RETRANS_TIME] = 1 * HZ,
169 			[NEIGH_VAR_BASE_REACHABLE_TIME] = 30 * HZ,
170 			[NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
171 			[NEIGH_VAR_INTERVAL_PROBE_TIME_MS] = 5 * HZ,
172 			[NEIGH_VAR_GC_STALETIME] = 60 * HZ,
173 			[NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_DEFAULT,
174 			[NEIGH_VAR_PROXY_QLEN] = 64,
175 			[NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
176 			[NEIGH_VAR_PROXY_DELAY]	= (8 * HZ) / 10,
177 			[NEIGH_VAR_LOCKTIME] = 1 * HZ,
178 		},
179 	},
180 	.gc_interval	= 30 * HZ,
181 	.gc_thresh1	= 128,
182 	.gc_thresh2	= 512,
183 	.gc_thresh3	= 1024,
184 };
185 
186 int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir)
187 {
188 	switch (dev->type) {
189 	case ARPHRD_ETHER:
190 	case ARPHRD_FDDI:
191 	case ARPHRD_IEEE802:
192 		ip_eth_mc_map(addr, haddr);
193 		return 0;
194 	case ARPHRD_INFINIBAND:
195 		ip_ib_mc_map(addr, dev->broadcast, haddr);
196 		return 0;
197 	case ARPHRD_IPGRE:
198 		ip_ipgre_mc_map(addr, dev->broadcast, haddr);
199 		return 0;
200 	default:
201 		if (dir) {
202 			memcpy(haddr, dev->broadcast, dev->addr_len);
203 			return 0;
204 		}
205 	}
206 	return -EINVAL;
207 }
208 
209 
210 static u32 arp_hash(const void *pkey,
211 		    const struct net_device *dev,
212 		    __u32 *hash_rnd)
213 {
214 	return arp_hashfn(pkey, dev, hash_rnd);
215 }
216 
217 static bool arp_key_eq(const struct neighbour *neigh, const void *pkey)
218 {
219 	return neigh_key_eq32(neigh, pkey);
220 }
221 
222 static int arp_constructor(struct neighbour *neigh)
223 {
224 	struct net_device *dev = neigh->dev;
225 	struct net *net = dev_net(dev);
226 	u32 inaddr_any = INADDR_ANY;
227 	struct neigh_parms *parms;
228 	struct in_device *in_dev;
229 	struct neigh_table *tbl;
230 	__be32 addr;
231 
232 	tbl = arp_table(net);
233 
234 	if (dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
235 		memcpy(neigh->primary_key, &inaddr_any, tbl->key_len);
236 
237 	addr = *(__be32 *)neigh->primary_key;
238 	rcu_read_lock();
239 	in_dev = __in_dev_get_rcu(dev);
240 	if (!in_dev) {
241 		rcu_read_unlock();
242 		return -EINVAL;
243 	}
244 
245 	neigh->type = inet_addr_type_dev_table(net, dev, addr);
246 
247 	parms = in_dev->arp_parms;
248 	__neigh_parms_put(neigh->parms);
249 	neigh->parms = neigh_parms_clone(parms);
250 	rcu_read_unlock();
251 
252 	if (!dev->header_ops) {
253 		neigh->nud_state = NUD_NOARP;
254 		neigh->ops = &arp_direct_ops;
255 		neigh->output = neigh_direct_output;
256 	} else {
257 		/* Good devices (checked by reading texts, but only Ethernet is
258 		   tested)
259 
260 		   ARPHRD_ETHER: (ethernet, apfddi)
261 		   ARPHRD_FDDI: (fddi)
262 		   ARPHRD_IEEE802: (tr)
263 		   ARPHRD_METRICOM: (strip)
264 		   ARPHRD_ARCNET:
265 		   etc. etc. etc.
266 
267 		   ARPHRD_IPDDP will also work, if author repairs it.
268 		   I did not it, because this driver does not work even
269 		   in old paradigm.
270 		 */
271 
272 		if (neigh->type == RTN_MULTICAST) {
273 			neigh->nud_state = NUD_NOARP;
274 			arp_mc_map(addr, neigh->ha, dev, 1);
275 		} else if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) {
276 			neigh->nud_state = NUD_NOARP;
277 			memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
278 		} else if (neigh->type == RTN_BROADCAST ||
279 			   (dev->flags & IFF_POINTOPOINT)) {
280 			neigh->nud_state = NUD_NOARP;
281 			memcpy(neigh->ha, dev->broadcast, dev->addr_len);
282 		}
283 
284 		if (dev->header_ops->cache)
285 			neigh->ops = &arp_hh_ops;
286 		else
287 			neigh->ops = &arp_generic_ops;
288 
289 		if (neigh->nud_state & NUD_VALID)
290 			neigh->output = neigh->ops->connected_output;
291 		else
292 			neigh->output = neigh->ops->output;
293 	}
294 	return 0;
295 }
296 
297 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
298 {
299 	dst_link_failure(skb);
300 	kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_FAILED);
301 }
302 
303 /* Create and send an arp packet. */
304 static void arp_send_dst(int type, int ptype, __be32 dest_ip,
305 			 struct net_device *dev, __be32 src_ip,
306 			 const unsigned char *dest_hw,
307 			 const unsigned char *src_hw,
308 			 const unsigned char *target_hw,
309 			 struct dst_entry *dst)
310 {
311 	struct sk_buff *skb;
312 
313 	/* arp on this interface. */
314 	if (dev->flags & IFF_NOARP)
315 		return;
316 
317 	skb = arp_create(type, ptype, dest_ip, dev, src_ip,
318 			 dest_hw, src_hw, target_hw);
319 	if (!skb)
320 		return;
321 
322 	skb_dst_set(skb, dst_clone(dst));
323 	arp_xmit(skb);
324 }
325 
326 void arp_send(int type, int ptype, __be32 dest_ip,
327 	      struct net_device *dev, __be32 src_ip,
328 	      const unsigned char *dest_hw, const unsigned char *src_hw,
329 	      const unsigned char *target_hw)
330 {
331 	arp_send_dst(type, ptype, dest_ip, dev, src_ip, dest_hw, src_hw,
332 		     target_hw, NULL);
333 }
334 EXPORT_SYMBOL(arp_send);
335 
336 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
337 {
338 	__be32 saddr = 0;
339 	u8 dst_ha[MAX_ADDR_LEN], *dst_hw = NULL;
340 	struct net_device *dev = neigh->dev;
341 	__be32 target = *(__be32 *)neigh->primary_key;
342 	int probes = atomic_read(&neigh->probes);
343 	struct in_device *in_dev;
344 	struct dst_entry *dst = NULL;
345 
346 	rcu_read_lock();
347 	in_dev = __in_dev_get_rcu(dev);
348 	if (!in_dev) {
349 		rcu_read_unlock();
350 		return;
351 	}
352 	switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
353 	default:
354 	case 0:		/* By default announce any local IP */
355 		if (skb && inet_addr_type_dev_table(dev_net(dev), dev,
356 					  ip_hdr(skb)->saddr) == RTN_LOCAL)
357 			saddr = ip_hdr(skb)->saddr;
358 		break;
359 	case 1:		/* Restrict announcements of saddr in same subnet */
360 		if (!skb)
361 			break;
362 		saddr = ip_hdr(skb)->saddr;
363 		if (inet_addr_type_dev_table(dev_net(dev), dev,
364 					     saddr) == RTN_LOCAL) {
365 			/* saddr should be known to target */
366 			if (inet_addr_onlink(in_dev, target, saddr))
367 				break;
368 		}
369 		saddr = 0;
370 		break;
371 	case 2:		/* Avoid secondary IPs, get a primary/preferred one */
372 		break;
373 	}
374 	rcu_read_unlock();
375 
376 	if (!saddr)
377 		saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
378 
379 	probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
380 	if (probes < 0) {
381 		if (!(READ_ONCE(neigh->nud_state) & NUD_VALID))
382 			pr_debug("trying to ucast probe in NUD_INVALID\n");
383 		neigh_ha_snapshot(dst_ha, neigh, dev);
384 		dst_hw = dst_ha;
385 	} else {
386 		probes -= NEIGH_VAR(neigh->parms, APP_PROBES);
387 		if (probes < 0) {
388 			neigh_app_ns(neigh);
389 			return;
390 		}
391 	}
392 
393 	if (skb && !(dev->priv_flags & IFF_XMIT_DST_RELEASE))
394 		dst = skb_dst(skb);
395 	arp_send_dst(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
396 		     dst_hw, dev->dev_addr, NULL, dst);
397 }
398 
399 static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip)
400 {
401 	struct net *net = dev_net(in_dev->dev);
402 	int scope;
403 
404 	switch (IN_DEV_ARP_IGNORE(in_dev)) {
405 	case 0:	/* Reply, the tip is already validated */
406 		return 0;
407 	case 1:	/* Reply only if tip is configured on the incoming interface */
408 		sip = 0;
409 		scope = RT_SCOPE_HOST;
410 		break;
411 	case 2:	/*
412 		 * Reply only if tip is configured on the incoming interface
413 		 * and is in same subnet as sip
414 		 */
415 		scope = RT_SCOPE_HOST;
416 		break;
417 	case 3:	/* Do not reply for scope host addresses */
418 		sip = 0;
419 		scope = RT_SCOPE_LINK;
420 		in_dev = NULL;
421 		break;
422 	case 4:	/* Reserved */
423 	case 5:
424 	case 6:
425 	case 7:
426 		return 0;
427 	case 8:	/* Do not reply */
428 		return 1;
429 	default:
430 		return 0;
431 	}
432 	return !inet_confirm_addr(net, in_dev, sip, tip, scope);
433 }
434 
435 static int arp_accept(struct in_device *in_dev, __be32 sip)
436 {
437 	struct net *net = dev_net(in_dev->dev);
438 	int scope = RT_SCOPE_LINK;
439 
440 	switch (IN_DEV_ARP_ACCEPT(in_dev)) {
441 	case 0: /* Don't create new entries from garp */
442 		return 0;
443 	case 1: /* Create new entries from garp */
444 		return 1;
445 	case 2: /* Create a neighbor in the arp table only if sip
446 		 * is in the same subnet as an address configured
447 		 * on the interface that received the garp message
448 		 */
449 		return !!inet_confirm_addr(net, in_dev, sip, 0, scope);
450 	default:
451 		return 0;
452 	}
453 }
454 
455 static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev)
456 {
457 	struct rtable *rt;
458 	int flag = 0;
459 	/*unsigned long now; */
460 	struct net *net = dev_net(dev);
461 
462 	rt = ip_route_output(net, sip, tip, 0, l3mdev_master_ifindex_rcu(dev),
463 			     RT_SCOPE_UNIVERSE);
464 	if (IS_ERR(rt))
465 		return 1;
466 	if (rt->dst.dev != dev) {
467 		__NET_INC_STATS(net, LINUX_MIB_ARPFILTER);
468 		flag = 1;
469 	}
470 	ip_rt_put(rt);
471 	return flag;
472 }
473 
474 /*
475  * Check if we can use proxy ARP for this path
476  */
477 static inline int arp_fwd_proxy(struct in_device *in_dev,
478 				struct net_device *dev,	struct rtable *rt)
479 {
480 	struct in_device *out_dev;
481 	int imi, omi = -1;
482 
483 	if (rt->dst.dev == dev)
484 		return 0;
485 
486 	if (!IN_DEV_PROXY_ARP(in_dev))
487 		return 0;
488 	imi = IN_DEV_MEDIUM_ID(in_dev);
489 	if (imi == 0)
490 		return 1;
491 	if (imi == -1)
492 		return 0;
493 
494 	/* place to check for proxy_arp for routes */
495 
496 	out_dev = __in_dev_get_rcu(rt->dst.dev);
497 	if (out_dev)
498 		omi = IN_DEV_MEDIUM_ID(out_dev);
499 
500 	return omi != imi && omi != -1;
501 }
502 
503 /*
504  * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev)
505  *
506  * RFC3069 supports proxy arp replies back to the same interface.  This
507  * is done to support (ethernet) switch features, like RFC 3069, where
508  * the individual ports are not allowed to communicate with each
509  * other, BUT they are allowed to talk to the upstream router.  As
510  * described in RFC 3069, it is possible to allow these hosts to
511  * communicate through the upstream router, by proxy_arp'ing.
512  *
513  * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation"
514  *
515  *  This technology is known by different names:
516  *    In RFC 3069 it is called VLAN Aggregation.
517  *    Cisco and Allied Telesyn call it Private VLAN.
518  *    Hewlett-Packard call it Source-Port filtering or port-isolation.
519  *    Ericsson call it MAC-Forced Forwarding (RFC Draft).
520  *
521  */
522 static inline int arp_fwd_pvlan(struct in_device *in_dev,
523 				struct net_device *dev,	struct rtable *rt,
524 				__be32 sip, __be32 tip)
525 {
526 	/* Private VLAN is only concerned about the same ethernet segment */
527 	if (rt->dst.dev != dev)
528 		return 0;
529 
530 	/* Don't reply on self probes (often done by windowz boxes)*/
531 	if (sip == tip)
532 		return 0;
533 
534 	if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
535 		return 1;
536 	else
537 		return 0;
538 }
539 
540 /*
541  *	Interface to link layer: send routine and receive handler.
542  */
543 
544 /*
545  *	Create an arp packet. If dest_hw is not set, we create a broadcast
546  *	message.
547  */
548 struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
549 			   struct net_device *dev, __be32 src_ip,
550 			   const unsigned char *dest_hw,
551 			   const unsigned char *src_hw,
552 			   const unsigned char *target_hw)
553 {
554 	struct sk_buff *skb;
555 	struct arphdr *arp;
556 	unsigned char *arp_ptr;
557 	int hlen = LL_RESERVED_SPACE(dev);
558 	int tlen = dev->needed_tailroom;
559 
560 	/*
561 	 *	Allocate a buffer
562 	 */
563 
564 	skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC);
565 	if (!skb)
566 		return NULL;
567 
568 	skb_reserve(skb, hlen);
569 	skb_reset_network_header(skb);
570 	skb_put(skb, arp_hdr_len(dev));
571 	skb->dev = dev;
572 	skb->protocol = htons(ETH_P_ARP);
573 	if (!src_hw)
574 		src_hw = dev->dev_addr;
575 	if (!dest_hw)
576 		dest_hw = dev->broadcast;
577 
578 	/* Fill the device header for the ARP frame.
579 	 * Note: skb->head can be changed.
580 	 */
581 	if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
582 		goto out;
583 
584 	arp = arp_hdr(skb);
585 	/*
586 	 * Fill out the arp protocol part.
587 	 *
588 	 * The arp hardware type should match the device type, except for FDDI,
589 	 * which (according to RFC 1390) should always equal 1 (Ethernet).
590 	 */
591 	/*
592 	 *	Exceptions everywhere. AX.25 uses the AX.25 PID value not the
593 	 *	DIX code for the protocol. Make these device structure fields.
594 	 */
595 	switch (dev->type) {
596 	default:
597 		arp->ar_hrd = htons(dev->type);
598 		arp->ar_pro = htons(ETH_P_IP);
599 		break;
600 
601 #if IS_ENABLED(CONFIG_AX25)
602 	case ARPHRD_AX25:
603 		arp->ar_hrd = htons(ARPHRD_AX25);
604 		arp->ar_pro = htons(AX25_P_IP);
605 		break;
606 
607 #if IS_ENABLED(CONFIG_NETROM)
608 	case ARPHRD_NETROM:
609 		arp->ar_hrd = htons(ARPHRD_NETROM);
610 		arp->ar_pro = htons(AX25_P_IP);
611 		break;
612 #endif
613 #endif
614 
615 #if IS_ENABLED(CONFIG_FDDI)
616 	case ARPHRD_FDDI:
617 		arp->ar_hrd = htons(ARPHRD_ETHER);
618 		arp->ar_pro = htons(ETH_P_IP);
619 		break;
620 #endif
621 	}
622 
623 	arp->ar_hln = dev->addr_len;
624 	arp->ar_pln = 4;
625 	arp->ar_op = htons(type);
626 
627 	arp_ptr = (unsigned char *)(arp + 1);
628 
629 	memcpy(arp_ptr, src_hw, dev->addr_len);
630 	arp_ptr += dev->addr_len;
631 	memcpy(arp_ptr, &src_ip, 4);
632 	arp_ptr += 4;
633 
634 	switch (dev->type) {
635 #if IS_ENABLED(CONFIG_FIREWIRE_NET)
636 	case ARPHRD_IEEE1394:
637 		break;
638 #endif
639 	default:
640 		if (target_hw)
641 			memcpy(arp_ptr, target_hw, dev->addr_len);
642 		else
643 			memset(arp_ptr, 0, dev->addr_len);
644 		arp_ptr += dev->addr_len;
645 	}
646 	memcpy(arp_ptr, &dest_ip, 4);
647 
648 	return skb;
649 
650 out:
651 	kfree_skb(skb);
652 	return NULL;
653 }
654 EXPORT_SYMBOL(arp_create);
655 
656 static int arp_xmit_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
657 {
658 	return dev_queue_xmit(skb);
659 }
660 
661 /*
662  *	Send an arp packet.
663  */
664 void arp_xmit(struct sk_buff *skb)
665 {
666 	rcu_read_lock();
667 	/* Send it off, maybe filter it using firewalling first.  */
668 	NF_HOOK(NFPROTO_ARP, NF_ARP_OUT,
669 		dev_net_rcu(skb->dev), NULL, skb, NULL, skb->dev,
670 		arp_xmit_finish);
671 	rcu_read_unlock();
672 }
673 EXPORT_SYMBOL(arp_xmit);
674 
675 static bool arp_is_garp(struct net *net, struct net_device *dev,
676 			int *addr_type, __be16 ar_op,
677 			__be32 sip, __be32 tip,
678 			unsigned char *sha, unsigned char *tha)
679 {
680 	bool is_garp = tip == sip;
681 
682 	/* Gratuitous ARP _replies_ also require target hwaddr to be
683 	 * the same as source.
684 	 */
685 	if (is_garp && ar_op == htons(ARPOP_REPLY))
686 		is_garp =
687 			/* IPv4 over IEEE 1394 doesn't provide target
688 			 * hardware address field in its ARP payload.
689 			 */
690 			tha &&
691 			!memcmp(tha, sha, dev->addr_len);
692 
693 	if (is_garp) {
694 		*addr_type = inet_addr_type_dev_table(net, dev, sip);
695 		if (*addr_type != RTN_UNICAST)
696 			is_garp = false;
697 	}
698 	return is_garp;
699 }
700 
701 /*
702  *	Process an arp request.
703  */
704 
705 static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
706 {
707 	struct neigh_table *tbl = arp_table(net);
708 	struct dst_entry *reply_dst = NULL;
709 	struct net_device *dev = skb->dev;
710 	unsigned char *sha, *tha = NULL;
711 	struct in_device *in_dev;
712 	u16 dev_type = dev->type;
713 	unsigned char *arp_ptr;
714 	bool is_garp = false;
715 	struct neighbour *n;
716 	struct arphdr *arp;
717 	struct rtable *rt;
718 	__be32 sip, tip;
719 	int addr_type;
720 
721 	/* arp_rcv below verifies the ARP header and verifies the device
722 	 * is ARP'able.
723 	 */
724 	in_dev = __in_dev_get_rcu(dev);
725 	if (!in_dev)
726 		goto out_free_skb;
727 
728 	arp = arp_hdr(skb);
729 
730 	switch (dev_type) {
731 	default:
732 		if (arp->ar_pro != htons(ETH_P_IP) ||
733 		    htons(dev_type) != arp->ar_hrd)
734 			goto out_free_skb;
735 		break;
736 	case ARPHRD_ETHER:
737 	case ARPHRD_FDDI:
738 	case ARPHRD_IEEE802:
739 		/*
740 		 * ETHERNET, and Fibre Channel (which are IEEE 802
741 		 * devices, according to RFC 2625) devices will accept ARP
742 		 * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
743 		 * This is the case also of FDDI, where the RFC 1390 says that
744 		 * FDDI devices should accept ARP hardware of (1) Ethernet,
745 		 * however, to be more robust, we'll accept both 1 (Ethernet)
746 		 * or 6 (IEEE 802.2)
747 		 */
748 		if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
749 		     arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
750 		    arp->ar_pro != htons(ETH_P_IP))
751 			goto out_free_skb;
752 		break;
753 	case ARPHRD_AX25:
754 		if (arp->ar_pro != htons(AX25_P_IP) ||
755 		    arp->ar_hrd != htons(ARPHRD_AX25))
756 			goto out_free_skb;
757 		break;
758 	case ARPHRD_NETROM:
759 		if (arp->ar_pro != htons(AX25_P_IP) ||
760 		    arp->ar_hrd != htons(ARPHRD_NETROM))
761 			goto out_free_skb;
762 		break;
763 	}
764 
765 	/* Understand only these message types */
766 
767 	if (arp->ar_op != htons(ARPOP_REPLY) &&
768 	    arp->ar_op != htons(ARPOP_REQUEST))
769 		goto out_free_skb;
770 
771 /*
772  *	Extract fields
773  */
774 	arp_ptr = (unsigned char *)(arp + 1);
775 	sha	= arp_ptr;
776 	arp_ptr += dev->addr_len;
777 	memcpy(&sip, arp_ptr, 4);
778 	arp_ptr += 4;
779 	switch (dev_type) {
780 #if IS_ENABLED(CONFIG_FIREWIRE_NET)
781 	case ARPHRD_IEEE1394:
782 		break;
783 #endif
784 	default:
785 		tha = arp_ptr;
786 		arp_ptr += dev->addr_len;
787 	}
788 	memcpy(&tip, arp_ptr, 4);
789 /*
790  *	Check for bad requests for 127.x.x.x and requests for multicast
791  *	addresses.  If this is one such, delete it.
792  */
793 	if (ipv4_is_multicast(tip) ||
794 	    (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip)))
795 		goto out_free_skb;
796 
797  /*
798   *	For some 802.11 wireless deployments (and possibly other networks),
799   *	there will be an ARP proxy and gratuitous ARP frames are attacks
800   *	and thus should not be accepted.
801   */
802 	if (sip == tip && IN_DEV_ORCONF(in_dev, DROP_GRATUITOUS_ARP))
803 		goto out_free_skb;
804 
805 /*
806  *     Special case: We must set Frame Relay source Q.922 address
807  */
808 	if (dev_type == ARPHRD_DLCI)
809 		sha = dev->broadcast;
810 
811 /*
812  *  Process entry.  The idea here is we want to send a reply if it is a
813  *  request for us or if it is a request for someone else that we hold
814  *  a proxy for.  We want to add an entry to our cache if it is a reply
815  *  to us or if it is a request for our address.
816  *  (The assumption for this last is that if someone is requesting our
817  *  address, they are probably intending to talk to us, so it saves time
818  *  if we cache their address.  Their address is also probably not in
819  *  our cache, since ours is not in their cache.)
820  *
821  *  Putting this another way, we only care about replies if they are to
822  *  us, in which case we add them to the cache.  For requests, we care
823  *  about those for us and those for our proxies.  We reply to both,
824  *  and in the case of requests for us we add the requester to the arp
825  *  cache.
826  */
827 
828 	if (arp->ar_op == htons(ARPOP_REQUEST) && skb_metadata_dst(skb))
829 		reply_dst = (struct dst_entry *)
830 			    iptunnel_metadata_reply(skb_metadata_dst(skb),
831 						    GFP_ATOMIC);
832 
833 	/* Special case: IPv4 duplicate address detection packet (RFC2131) */
834 	if (sip == 0) {
835 		if (arp->ar_op == htons(ARPOP_REQUEST) &&
836 		    inet_addr_type_dev_table(net, dev, tip) == RTN_LOCAL &&
837 		    !arp_ignore(in_dev, sip, tip))
838 			arp_send_dst(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip,
839 				     sha, dev->dev_addr, sha, reply_dst);
840 		goto out_consume_skb;
841 	}
842 
843 	if (arp->ar_op == htons(ARPOP_REQUEST) &&
844 	    ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
845 
846 		rt = skb_rtable(skb);
847 		addr_type = rt->rt_type;
848 
849 		if (addr_type == RTN_LOCAL) {
850 			int dont_send;
851 
852 			dont_send = arp_ignore(in_dev, sip, tip);
853 			if (!dont_send && IN_DEV_ARPFILTER(in_dev))
854 				dont_send = arp_filter(sip, tip, dev);
855 			if (!dont_send) {
856 				n = neigh_event_ns(tbl, sha, &sip, dev);
857 				if (n) {
858 					arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
859 						     sip, dev, tip, sha,
860 						     dev->dev_addr, sha,
861 						     reply_dst);
862 					neigh_release(n);
863 				}
864 			}
865 			goto out_consume_skb;
866 		} else if (IN_DEV_FORWARD(in_dev)) {
867 			if (addr_type == RTN_UNICAST  &&
868 			    (arp_fwd_proxy(in_dev, dev, rt) ||
869 			     arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
870 			     (rt->dst.dev != dev &&
871 			      pneigh_lookup(tbl, &tip, dev)))) {
872 				n = neigh_event_ns(tbl, sha, &sip, dev);
873 				if (n)
874 					neigh_release(n);
875 
876 				if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
877 				    skb->pkt_type == PACKET_HOST ||
878 				    NEIGH_VAR(in_dev->arp_parms, PROXY_DELAY) == 0) {
879 					arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
880 						     sip, dev, tip, sha,
881 						     dev->dev_addr, sha,
882 						     reply_dst);
883 				} else {
884 					pneigh_enqueue(tbl,
885 						       in_dev->arp_parms, skb);
886 					goto out_free_dst;
887 				}
888 				goto out_consume_skb;
889 			}
890 		}
891 	}
892 
893 	/* Update our ARP tables */
894 
895 	n = ipv4_neigh_lookup(dev, &sip);
896 
897 	addr_type = -1;
898 	if (n || arp_accept(in_dev, sip)) {
899 		is_garp = arp_is_garp(net, dev, &addr_type, arp->ar_op,
900 				      sip, tip, sha, tha);
901 	}
902 
903 	if (arp_accept(in_dev, sip)) {
904 		/* Unsolicited ARP is not accepted by default.
905 		   It is possible, that this option should be enabled for some
906 		   devices (strip is candidate)
907 		 */
908 		if (!n &&
909 		    (is_garp ||
910 		     (arp->ar_op == htons(ARPOP_REPLY) &&
911 		      (addr_type == RTN_UNICAST ||
912 		       (addr_type < 0 &&
913 			/* postpone calculation to as late as possible */
914 			inet_addr_type_dev_table(net, dev, sip) == RTN_UNICAST))))) {
915 			n = ipv4_neigh_lookup(dev, &sip);
916 			if (!n) {
917 				n = ipv4_neigh_create(dev, &sip);
918 				if (IS_ERR(n))
919 					n = NULL;
920 			}
921 		}
922 	}
923 
924 	if (n) {
925 		int state = NUD_REACHABLE;
926 		int override;
927 
928 		/* If several different ARP replies follows back-to-back,
929 		   use the FIRST one. It is possible, if several proxy
930 		   agents are active. Taking the first reply prevents
931 		   arp trashing and chooses the fastest router.
932 		 */
933 		override = time_after(jiffies,
934 				      n->updated +
935 				      NEIGH_VAR(n->parms, LOCKTIME)) ||
936 			   is_garp;
937 
938 		/* Broadcast replies and request packets
939 		   do not assert neighbour reachability.
940 		 */
941 		if (arp->ar_op != htons(ARPOP_REPLY) ||
942 		    skb->pkt_type != PACKET_HOST)
943 			state = NUD_STALE;
944 		neigh_update(n, sha, state,
945 			     override ? NEIGH_UPDATE_F_OVERRIDE : 0, 0);
946 		neigh_release(n);
947 	}
948 
949 out_consume_skb:
950 	consume_skb(skb);
951 
952 out_free_dst:
953 	dst_release(reply_dst);
954 	return NET_RX_SUCCESS;
955 
956 out_free_skb:
957 	kfree_skb(skb);
958 	return NET_RX_DROP;
959 }
960 
961 static void parp_redo(struct sk_buff *skb)
962 {
963 	arp_process(dev_net(skb->dev), NULL, skb);
964 }
965 
966 static int arp_is_multicast(const void *pkey)
967 {
968 	return ipv4_is_multicast(*((__be32 *)pkey));
969 }
970 
971 /*
972  *	Receive an arp request from the device layer.
973  */
974 
975 static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
976 		   struct packet_type *pt, struct net_device *orig_dev)
977 {
978 	enum skb_drop_reason drop_reason;
979 	const struct arphdr *arp;
980 
981 	/* do not tweak dropwatch on an ARP we will ignore */
982 	if (dev->flags & IFF_NOARP ||
983 	    skb->pkt_type == PACKET_OTHERHOST ||
984 	    skb->pkt_type == PACKET_LOOPBACK)
985 		goto consumeskb;
986 
987 	skb = skb_share_check(skb, GFP_ATOMIC);
988 	if (!skb)
989 		goto out_of_mem;
990 
991 	/* ARP header, plus 2 device addresses, plus 2 IP addresses.  */
992 	drop_reason = pskb_may_pull_reason(skb, arp_hdr_len(dev));
993 	if (drop_reason != SKB_NOT_DROPPED_YET)
994 		goto freeskb;
995 
996 	arp = arp_hdr(skb);
997 	if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4) {
998 		drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
999 		goto freeskb;
1000 	}
1001 
1002 	memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
1003 
1004 	return NF_HOOK(NFPROTO_ARP, NF_ARP_IN,
1005 		       dev_net(dev), NULL, skb, dev, NULL,
1006 		       arp_process);
1007 
1008 consumeskb:
1009 	consume_skb(skb);
1010 	return NET_RX_SUCCESS;
1011 freeskb:
1012 	kfree_skb_reason(skb, drop_reason);
1013 out_of_mem:
1014 	return NET_RX_DROP;
1015 }
1016 
1017 /*
1018  *	User level interface (ioctl)
1019  */
1020 
1021 static struct net_device *arp_req_dev_by_name(struct net *net, struct arpreq *r,
1022 					      bool getarp)
1023 {
1024 	struct net_device *dev;
1025 
1026 	if (getarp)
1027 		dev = dev_get_by_name_rcu(net, r->arp_dev);
1028 	else
1029 		dev = __dev_get_by_name(net, r->arp_dev);
1030 	if (!dev)
1031 		return ERR_PTR(-ENODEV);
1032 
1033 	/* Mmmm... It is wrong... ARPHRD_NETROM == 0 */
1034 	if (!r->arp_ha.sa_family)
1035 		r->arp_ha.sa_family = dev->type;
1036 
1037 	if ((r->arp_flags & ATF_COM) && r->arp_ha.sa_family != dev->type)
1038 		return ERR_PTR(-EINVAL);
1039 
1040 	return dev;
1041 }
1042 
1043 static struct net_device *arp_req_dev(struct net *net, struct arpreq *r)
1044 {
1045 	struct net_device *dev;
1046 	struct rtable *rt;
1047 	__be32 ip;
1048 
1049 	if (r->arp_dev[0])
1050 		return arp_req_dev_by_name(net, r, false);
1051 
1052 	if (r->arp_flags & ATF_PUBL)
1053 		return NULL;
1054 
1055 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1056 
1057 	rt = ip_route_output(net, ip, 0, 0, 0, RT_SCOPE_LINK);
1058 	if (IS_ERR(rt))
1059 		return ERR_CAST(rt);
1060 
1061 	dev = rt->dst.dev;
1062 	ip_rt_put(rt);
1063 
1064 	if (!dev)
1065 		return ERR_PTR(-EINVAL);
1066 
1067 	return dev;
1068 }
1069 
1070 /*
1071  *	Set (create) an ARP cache entry.
1072  */
1073 
1074 static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
1075 {
1076 	if (!dev) {
1077 		IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
1078 		return 0;
1079 	}
1080 	if (__in_dev_get_rtnl_net(dev)) {
1081 		IN_DEV_CONF_SET(__in_dev_get_rtnl_net(dev), PROXY_ARP, on);
1082 		return 0;
1083 	}
1084 	return -ENXIO;
1085 }
1086 
1087 static int arp_req_set_public(struct net *net, struct arpreq *r,
1088 			      struct net_device *dev)
1089 {
1090 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
1091 	struct neigh_table *tbl = arp_table(net);
1092 
1093 	if (!dev && (r->arp_flags & ATF_COM)) {
1094 		dev = dev_getbyhwaddr(net, r->arp_ha.sa_family,
1095 				      r->arp_ha.sa_data);
1096 		if (!dev)
1097 			return -ENODEV;
1098 	}
1099 	if (mask) {
1100 		__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1101 
1102 		return pneigh_create(tbl, &ip, dev, 0, 0, false);
1103 	}
1104 
1105 	return arp_req_set_proxy(net, dev, 1);
1106 }
1107 
1108 static int arp_req_set(struct net *net, struct arpreq *r)
1109 {
1110 	struct neighbour *neigh;
1111 	struct net_device *dev;
1112 	__be32 ip;
1113 	int err;
1114 
1115 	dev = arp_req_dev(net, r);
1116 	if (IS_ERR(dev))
1117 		return PTR_ERR(dev);
1118 
1119 	if (r->arp_flags & ATF_PUBL)
1120 		return arp_req_set_public(net, r, dev);
1121 
1122 	switch (dev->type) {
1123 #if IS_ENABLED(CONFIG_FDDI)
1124 	case ARPHRD_FDDI:
1125 		/*
1126 		 * According to RFC 1390, FDDI devices should accept ARP
1127 		 * hardware types of 1 (Ethernet).  However, to be more
1128 		 * robust, we'll accept hardware types of either 1 (Ethernet)
1129 		 * or 6 (IEEE 802.2).
1130 		 */
1131 		if (r->arp_ha.sa_family != ARPHRD_FDDI &&
1132 		    r->arp_ha.sa_family != ARPHRD_ETHER &&
1133 		    r->arp_ha.sa_family != ARPHRD_IEEE802)
1134 			return -EINVAL;
1135 		break;
1136 #endif
1137 	default:
1138 		if (r->arp_ha.sa_family != dev->type)
1139 			return -EINVAL;
1140 		break;
1141 	}
1142 
1143 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1144 
1145 	neigh = ipv4_neigh_lookup(dev, &ip);
1146 	if (!neigh)
1147 		neigh = ipv4_neigh_create(dev, &ip);
1148 	err = PTR_ERR(neigh);
1149 	if (!IS_ERR(neigh)) {
1150 		unsigned int state = NUD_STALE;
1151 
1152 		if (r->arp_flags & ATF_PERM) {
1153 			r->arp_flags |= ATF_COM;
1154 			state = NUD_PERMANENT;
1155 		}
1156 
1157 		err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
1158 				   r->arp_ha.sa_data : NULL, state,
1159 				   NEIGH_UPDATE_F_OVERRIDE |
1160 				   NEIGH_UPDATE_F_ADMIN, 0);
1161 		neigh_release(neigh);
1162 	}
1163 	return err;
1164 }
1165 
1166 static unsigned int arp_state_to_flags(struct neighbour *neigh)
1167 {
1168 	if (neigh->nud_state&NUD_PERMANENT)
1169 		return ATF_PERM | ATF_COM;
1170 	else if (neigh->nud_state&NUD_VALID)
1171 		return ATF_COM;
1172 	else
1173 		return 0;
1174 }
1175 
1176 /*
1177  *	Get an ARP cache entry.
1178  */
1179 
1180 static int arp_req_get(struct net *net, struct arpreq *r)
1181 {
1182 	__be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
1183 	struct neighbour *neigh;
1184 	struct net_device *dev;
1185 
1186 	if (!r->arp_dev[0])
1187 		return -ENODEV;
1188 
1189 	dev = arp_req_dev_by_name(net, r, true);
1190 	if (IS_ERR(dev))
1191 		return PTR_ERR(dev);
1192 
1193 	neigh = ipv4_neigh_lookup(dev, &ip);
1194 	if (!neigh)
1195 		return -ENXIO;
1196 
1197 	if (READ_ONCE(neigh->nud_state) & NUD_NOARP) {
1198 		neigh_release(neigh);
1199 		return -ENXIO;
1200 	}
1201 
1202 	read_lock_bh(&neigh->lock);
1203 	memcpy(r->arp_ha.sa_data, neigh->ha,
1204 	       min(dev->addr_len, sizeof(r->arp_ha.sa_data)));
1205 	r->arp_flags = arp_state_to_flags(neigh);
1206 	read_unlock_bh(&neigh->lock);
1207 
1208 	neigh_release(neigh);
1209 
1210 	r->arp_ha.sa_family = dev->type;
1211 	netdev_copy_name(dev, r->arp_dev);
1212 
1213 	return 0;
1214 }
1215 
1216 int arp_invalidate(struct net_device *dev, __be32 ip, bool force)
1217 {
1218 	struct neighbour *neigh;
1219 	int err = -ENXIO;
1220 
1221 	neigh = ipv4_neigh_lookup(dev, &ip);
1222 	if (neigh) {
1223 		struct neigh_table *tbl = neigh->tbl;
1224 
1225 		if ((READ_ONCE(neigh->nud_state) & NUD_VALID) && !force) {
1226 			neigh_release(neigh);
1227 			return 0;
1228 		}
1229 
1230 		if (READ_ONCE(neigh->nud_state) & ~NUD_NOARP)
1231 			err = neigh_update(neigh, NULL, NUD_FAILED,
1232 					   NEIGH_UPDATE_F_OVERRIDE|
1233 					   NEIGH_UPDATE_F_ADMIN, 0);
1234 		spin_lock_bh(&tbl->lock);
1235 		neigh_release(neigh);
1236 		neigh_remove_one(neigh);
1237 		spin_unlock_bh(&tbl->lock);
1238 	}
1239 
1240 	return err;
1241 }
1242 
1243 static int arp_req_delete_public(struct net *net, struct arpreq *r,
1244 				 struct net_device *dev)
1245 {
1246 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
1247 	struct neigh_table *tbl = arp_table(net);
1248 
1249 	if (mask) {
1250 		__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1251 
1252 		return pneigh_delete(tbl, &ip, dev);
1253 	}
1254 
1255 	return arp_req_set_proxy(net, dev, 0);
1256 }
1257 
1258 static int arp_req_delete(struct net *net, struct arpreq *r)
1259 {
1260 	struct net_device *dev;
1261 	__be32 ip;
1262 
1263 	dev = arp_req_dev(net, r);
1264 	if (IS_ERR(dev))
1265 		return PTR_ERR(dev);
1266 
1267 	if (r->arp_flags & ATF_PUBL)
1268 		return arp_req_delete_public(net, r, dev);
1269 
1270 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1271 
1272 	return arp_invalidate(dev, ip, true);
1273 }
1274 
1275 /*
1276  *	Handle an ARP layer I/O control request.
1277  */
1278 
1279 int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1280 {
1281 	struct arpreq r;
1282 	__be32 *netmask;
1283 	int err;
1284 
1285 	switch (cmd) {
1286 	case SIOCDARP:
1287 	case SIOCSARP:
1288 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1289 			return -EPERM;
1290 		fallthrough;
1291 	case SIOCGARP:
1292 		err = copy_from_user(&r, arg, sizeof(struct arpreq));
1293 		if (err)
1294 			return -EFAULT;
1295 		r.arp_dev[IFNAMSIZ - 1] = '\0';
1296 		break;
1297 	default:
1298 		return -EINVAL;
1299 	}
1300 
1301 	if (r.arp_pa.sa_family != AF_INET)
1302 		return -EPFNOSUPPORT;
1303 
1304 	if (!(r.arp_flags & ATF_PUBL) &&
1305 	    (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
1306 		return -EINVAL;
1307 
1308 	netmask = &((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr;
1309 	if (!(r.arp_flags & ATF_NETMASK))
1310 		*netmask = htonl(0xFFFFFFFFUL);
1311 	else if (*netmask && *netmask != htonl(0xFFFFFFFFUL))
1312 		return -EINVAL;
1313 
1314 	switch (cmd) {
1315 	case SIOCDARP:
1316 		rtnl_net_lock(net);
1317 		err = arp_req_delete(net, &r);
1318 		rtnl_net_unlock(net);
1319 		break;
1320 	case SIOCSARP:
1321 		rtnl_net_lock(net);
1322 		err = arp_req_set(net, &r);
1323 		rtnl_net_unlock(net);
1324 		break;
1325 	case SIOCGARP:
1326 		rcu_read_lock();
1327 		err = arp_req_get(net, &r);
1328 		rcu_read_unlock();
1329 
1330 		if (!err && copy_to_user(arg, &r, sizeof(r)))
1331 			err = -EFAULT;
1332 		break;
1333 	}
1334 
1335 	return err;
1336 }
1337 
1338 static int arp_netdev_event(struct notifier_block *this, unsigned long event,
1339 			    void *ptr)
1340 {
1341 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1342 	struct netdev_notifier_change_info *change_info;
1343 	struct net *net = dev_net(dev);
1344 	struct in_device *in_dev;
1345 	struct neigh_table *tbl;
1346 	bool evict_nocarrier;
1347 
1348 	tbl = arp_table(net);
1349 
1350 	switch (event) {
1351 	case NETDEV_CHANGEADDR:
1352 		neigh_changeaddr(tbl, dev);
1353 		rt_cache_flush(net);
1354 		break;
1355 	case NETDEV_CHANGE:
1356 		change_info = ptr;
1357 		if (change_info->flags_changed & IFF_NOARP)
1358 			neigh_changeaddr(tbl, dev);
1359 
1360 		in_dev = __in_dev_get_rtnl(dev);
1361 		if (!in_dev)
1362 			evict_nocarrier = true;
1363 		else
1364 			evict_nocarrier = IN_DEV_ARP_EVICT_NOCARRIER(in_dev);
1365 
1366 		if (evict_nocarrier && !netif_carrier_ok(dev))
1367 			neigh_carrier_down(tbl, dev);
1368 		break;
1369 	default:
1370 		break;
1371 	}
1372 
1373 	return NOTIFY_DONE;
1374 }
1375 
1376 static struct notifier_block arp_netdev_notifier = {
1377 	.notifier_call = arp_netdev_event,
1378 };
1379 
1380 /* Note, that it is not on notifier chain.
1381    It is necessary, that this routine was called after route cache will be
1382    flushed.
1383  */
1384 void arp_ifdown(struct net_device *dev)
1385 {
1386 	struct neigh_table *tbl = arp_table(dev_net(dev));
1387 
1388 	neigh_ifdown(tbl, dev);
1389 }
1390 
1391 
1392 /*
1393  *	Called once on startup.
1394  */
1395 
1396 static struct packet_type arp_packet_type __read_mostly = {
1397 	.type =	cpu_to_be16(ETH_P_ARP),
1398 	.func =	arp_rcv,
1399 };
1400 
1401 #ifdef CONFIG_PROC_FS
1402 #if IS_ENABLED(CONFIG_AX25)
1403 
1404 /*
1405  *	ax25 -> ASCII conversion
1406  */
1407 static void ax2asc2(ax25_address *a, char *buf)
1408 {
1409 	char c, *s;
1410 	int n;
1411 
1412 	for (n = 0, s = buf; n < 6; n++) {
1413 		c = (a->ax25_call[n] >> 1) & 0x7F;
1414 
1415 		if (c != ' ')
1416 			*s++ = c;
1417 	}
1418 
1419 	*s++ = '-';
1420 	n = (a->ax25_call[6] >> 1) & 0x0F;
1421 	if (n > 9) {
1422 		*s++ = '1';
1423 		n -= 10;
1424 	}
1425 
1426 	*s++ = n + '0';
1427 	*s++ = '\0';
1428 
1429 	if (*buf == '\0' || *buf == '-') {
1430 		buf[0] = '*';
1431 		buf[1] = '\0';
1432 	}
1433 }
1434 #endif /* CONFIG_AX25 */
1435 
1436 #define HBUFFERLEN 30
1437 
1438 static void arp_format_neigh_entry(struct seq_file *seq,
1439 				   struct neighbour *n)
1440 {
1441 	char hbuffer[HBUFFERLEN];
1442 	int k, j;
1443 	char tbuf[16];
1444 	struct net_device *dev = n->dev;
1445 	int hatype = dev->type;
1446 
1447 	read_lock(&n->lock);
1448 	/* Convert hardware address to XX:XX:XX:XX ... form. */
1449 #if IS_ENABLED(CONFIG_AX25)
1450 	if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
1451 		ax2asc2((ax25_address *)n->ha, hbuffer);
1452 	else {
1453 #endif
1454 	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
1455 		hbuffer[k++] = hex_asc_hi(n->ha[j]);
1456 		hbuffer[k++] = hex_asc_lo(n->ha[j]);
1457 		hbuffer[k++] = ':';
1458 	}
1459 	if (k != 0)
1460 		--k;
1461 	hbuffer[k] = 0;
1462 #if IS_ENABLED(CONFIG_AX25)
1463 	}
1464 #endif
1465 	sprintf(tbuf, "%pI4", n->primary_key);
1466 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%-17s     *        %s\n",
1467 		   tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
1468 	read_unlock(&n->lock);
1469 }
1470 
1471 static void arp_format_pneigh_entry(struct seq_file *seq,
1472 				    struct pneigh_entry *n)
1473 {
1474 	struct net_device *dev = n->dev;
1475 	int hatype = dev ? dev->type : 0;
1476 	char tbuf[16];
1477 
1478 	sprintf(tbuf, "%pI4", n->key);
1479 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%s     *        %s\n",
1480 		   tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
1481 		   dev ? dev->name : "*");
1482 }
1483 
1484 static int arp_seq_show(struct seq_file *seq, void *v)
1485 {
1486 	if (v == SEQ_START_TOKEN) {
1487 		seq_puts(seq, "IP address       HW type     Flags       "
1488 			      "HW address            Mask     Device\n");
1489 	} else {
1490 		struct neigh_seq_state *state = seq->private;
1491 
1492 		if (state->flags & NEIGH_SEQ_IS_PNEIGH)
1493 			arp_format_pneigh_entry(seq, v);
1494 		else
1495 			arp_format_neigh_entry(seq, v);
1496 	}
1497 
1498 	return 0;
1499 }
1500 
1501 static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
1502 {
1503 	struct neigh_table *tbl = arp_table(seq_file_net(seq));
1504 
1505 	/* Don't want to confuse "arp -a" w/ magic entries,
1506 	 * so we tell the generic iterator to skip NUD_NOARP.
1507 	 */
1508 	return neigh_seq_start(seq, pos, tbl, NEIGH_SEQ_SKIP_NOARP);
1509 }
1510 
1511 static const struct seq_operations arp_seq_ops = {
1512 	.start	= arp_seq_start,
1513 	.next	= neigh_seq_next,
1514 	.stop	= neigh_seq_stop,
1515 	.show	= arp_seq_show,
1516 };
1517 #endif /* CONFIG_PROC_FS */
1518 
1519 static int __net_init arp_net_init(struct net *net)
1520 {
1521 	int err;
1522 
1523 	err = neigh_table_register(net, &arp_tbl, NEIGH_ARP_TABLE);
1524 	if (err)
1525 		goto err;
1526 
1527 #ifdef CONFIG_PROC_FS
1528 #ifdef CONFIG_SYSCTL
1529 	err = neigh_sysctl_register(NULL, &arp_table(net)->parms, NULL);
1530 	if (err)
1531 		goto err_sysctl;
1532 #endif
1533 
1534 	if (!proc_create_net("arp", 0444, net->proc_net, &arp_seq_ops,
1535 			     sizeof(struct neigh_seq_state))) {
1536 		err = -ENOMEM;
1537 		goto err_proc_create;
1538 	}
1539 #endif
1540 
1541 	return 0;
1542 
1543 #ifdef CONFIG_PROC_FS
1544 err_proc_create:
1545 #ifdef CONFIG_SYSCTL
1546 	neigh_sysctl_unregister(&arp_table(net)->parms);
1547 err_sysctl:
1548 #endif
1549 	neigh_table_unregister(net, NEIGH_ARP_TABLE);
1550 #endif
1551 err:
1552 	return err;
1553 }
1554 
1555 static void __net_exit arp_net_exit(struct net *net)
1556 {
1557 	remove_proc_entry("arp", net->proc_net);
1558 #ifdef CONFIG_SYSCTL
1559 	neigh_sysctl_unregister(&arp_table(net)->parms);
1560 #endif
1561 	neigh_table_unregister(net, NEIGH_ARP_TABLE);
1562 }
1563 
1564 static struct pernet_operations arp_net_ops = {
1565 	.init = arp_net_init,
1566 	.exit = arp_net_exit,
1567 };
1568 
1569 void __init arp_init(void)
1570 {
1571 	if (register_pernet_subsys(&arp_net_ops))
1572 		panic("Cannot allocate arp table\n");
1573 
1574 	dev_add_pack(&arp_packet_type);
1575 	register_netdevice_notifier(&arp_netdev_notifier);
1576 }
1577