xref: /linux/net/ipv4/arp.c (revision 42a9fb3382fc2573e92f41d203b095d9a372cfc9)
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 = __neigh_lookup(tbl, &sip, dev, 0);
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) ==
915 				RTN_UNICAST)))))
916 			n = __neigh_lookup(tbl, &sip, dev, 1);
917 	}
918 
919 	if (n) {
920 		int state = NUD_REACHABLE;
921 		int override;
922 
923 		/* If several different ARP replies follows back-to-back,
924 		   use the FIRST one. It is possible, if several proxy
925 		   agents are active. Taking the first reply prevents
926 		   arp trashing and chooses the fastest router.
927 		 */
928 		override = time_after(jiffies,
929 				      n->updated +
930 				      NEIGH_VAR(n->parms, LOCKTIME)) ||
931 			   is_garp;
932 
933 		/* Broadcast replies and request packets
934 		   do not assert neighbour reachability.
935 		 */
936 		if (arp->ar_op != htons(ARPOP_REPLY) ||
937 		    skb->pkt_type != PACKET_HOST)
938 			state = NUD_STALE;
939 		neigh_update(n, sha, state,
940 			     override ? NEIGH_UPDATE_F_OVERRIDE : 0, 0);
941 		neigh_release(n);
942 	}
943 
944 out_consume_skb:
945 	consume_skb(skb);
946 
947 out_free_dst:
948 	dst_release(reply_dst);
949 	return NET_RX_SUCCESS;
950 
951 out_free_skb:
952 	kfree_skb(skb);
953 	return NET_RX_DROP;
954 }
955 
956 static void parp_redo(struct sk_buff *skb)
957 {
958 	arp_process(dev_net(skb->dev), NULL, skb);
959 }
960 
961 static int arp_is_multicast(const void *pkey)
962 {
963 	return ipv4_is_multicast(*((__be32 *)pkey));
964 }
965 
966 /*
967  *	Receive an arp request from the device layer.
968  */
969 
970 static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
971 		   struct packet_type *pt, struct net_device *orig_dev)
972 {
973 	enum skb_drop_reason drop_reason;
974 	const struct arphdr *arp;
975 
976 	/* do not tweak dropwatch on an ARP we will ignore */
977 	if (dev->flags & IFF_NOARP ||
978 	    skb->pkt_type == PACKET_OTHERHOST ||
979 	    skb->pkt_type == PACKET_LOOPBACK)
980 		goto consumeskb;
981 
982 	skb = skb_share_check(skb, GFP_ATOMIC);
983 	if (!skb)
984 		goto out_of_mem;
985 
986 	/* ARP header, plus 2 device addresses, plus 2 IP addresses.  */
987 	drop_reason = pskb_may_pull_reason(skb, arp_hdr_len(dev));
988 	if (drop_reason != SKB_NOT_DROPPED_YET)
989 		goto freeskb;
990 
991 	arp = arp_hdr(skb);
992 	if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4) {
993 		drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
994 		goto freeskb;
995 	}
996 
997 	memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
998 
999 	return NF_HOOK(NFPROTO_ARP, NF_ARP_IN,
1000 		       dev_net(dev), NULL, skb, dev, NULL,
1001 		       arp_process);
1002 
1003 consumeskb:
1004 	consume_skb(skb);
1005 	return NET_RX_SUCCESS;
1006 freeskb:
1007 	kfree_skb_reason(skb, drop_reason);
1008 out_of_mem:
1009 	return NET_RX_DROP;
1010 }
1011 
1012 /*
1013  *	User level interface (ioctl)
1014  */
1015 
1016 static struct net_device *arp_req_dev_by_name(struct net *net, struct arpreq *r,
1017 					      bool getarp)
1018 {
1019 	struct net_device *dev;
1020 
1021 	if (getarp)
1022 		dev = dev_get_by_name_rcu(net, r->arp_dev);
1023 	else
1024 		dev = __dev_get_by_name(net, r->arp_dev);
1025 	if (!dev)
1026 		return ERR_PTR(-ENODEV);
1027 
1028 	/* Mmmm... It is wrong... ARPHRD_NETROM == 0 */
1029 	if (!r->arp_ha.sa_family)
1030 		r->arp_ha.sa_family = dev->type;
1031 
1032 	if ((r->arp_flags & ATF_COM) && r->arp_ha.sa_family != dev->type)
1033 		return ERR_PTR(-EINVAL);
1034 
1035 	return dev;
1036 }
1037 
1038 static struct net_device *arp_req_dev(struct net *net, struct arpreq *r)
1039 {
1040 	struct net_device *dev;
1041 	struct rtable *rt;
1042 	__be32 ip;
1043 
1044 	if (r->arp_dev[0])
1045 		return arp_req_dev_by_name(net, r, false);
1046 
1047 	if (r->arp_flags & ATF_PUBL)
1048 		return NULL;
1049 
1050 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1051 
1052 	rt = ip_route_output(net, ip, 0, 0, 0, RT_SCOPE_LINK);
1053 	if (IS_ERR(rt))
1054 		return ERR_CAST(rt);
1055 
1056 	dev = rt->dst.dev;
1057 	ip_rt_put(rt);
1058 
1059 	if (!dev)
1060 		return ERR_PTR(-EINVAL);
1061 
1062 	return dev;
1063 }
1064 
1065 /*
1066  *	Set (create) an ARP cache entry.
1067  */
1068 
1069 static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
1070 {
1071 	if (!dev) {
1072 		IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
1073 		return 0;
1074 	}
1075 	if (__in_dev_get_rtnl_net(dev)) {
1076 		IN_DEV_CONF_SET(__in_dev_get_rtnl_net(dev), PROXY_ARP, on);
1077 		return 0;
1078 	}
1079 	return -ENXIO;
1080 }
1081 
1082 static int arp_req_set_public(struct net *net, struct arpreq *r,
1083 			      struct net_device *dev)
1084 {
1085 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
1086 	struct neigh_table *tbl = arp_table(net);
1087 
1088 	if (!dev && (r->arp_flags & ATF_COM)) {
1089 		dev = dev_getbyhwaddr(net, r->arp_ha.sa_family,
1090 				      r->arp_ha.sa_data);
1091 		if (!dev)
1092 			return -ENODEV;
1093 	}
1094 	if (mask) {
1095 		__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1096 
1097 		return pneigh_create(tbl, &ip, dev, 0, 0, false);
1098 	}
1099 
1100 	return arp_req_set_proxy(net, dev, 1);
1101 }
1102 
1103 static int arp_req_set(struct net *net, struct arpreq *r)
1104 {
1105 	struct neigh_table *tbl = arp_table(net);
1106 	struct neighbour *neigh;
1107 	struct net_device *dev;
1108 	__be32 ip;
1109 	int err;
1110 
1111 	dev = arp_req_dev(net, r);
1112 	if (IS_ERR(dev))
1113 		return PTR_ERR(dev);
1114 
1115 	if (r->arp_flags & ATF_PUBL)
1116 		return arp_req_set_public(net, r, dev);
1117 
1118 	switch (dev->type) {
1119 #if IS_ENABLED(CONFIG_FDDI)
1120 	case ARPHRD_FDDI:
1121 		/*
1122 		 * According to RFC 1390, FDDI devices should accept ARP
1123 		 * hardware types of 1 (Ethernet).  However, to be more
1124 		 * robust, we'll accept hardware types of either 1 (Ethernet)
1125 		 * or 6 (IEEE 802.2).
1126 		 */
1127 		if (r->arp_ha.sa_family != ARPHRD_FDDI &&
1128 		    r->arp_ha.sa_family != ARPHRD_ETHER &&
1129 		    r->arp_ha.sa_family != ARPHRD_IEEE802)
1130 			return -EINVAL;
1131 		break;
1132 #endif
1133 	default:
1134 		if (r->arp_ha.sa_family != dev->type)
1135 			return -EINVAL;
1136 		break;
1137 	}
1138 
1139 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1140 
1141 	neigh = __neigh_lookup_errno(tbl, &ip, dev);
1142 	err = PTR_ERR(neigh);
1143 	if (!IS_ERR(neigh)) {
1144 		unsigned int state = NUD_STALE;
1145 
1146 		if (r->arp_flags & ATF_PERM) {
1147 			r->arp_flags |= ATF_COM;
1148 			state = NUD_PERMANENT;
1149 		}
1150 
1151 		err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
1152 				   r->arp_ha.sa_data : NULL, state,
1153 				   NEIGH_UPDATE_F_OVERRIDE |
1154 				   NEIGH_UPDATE_F_ADMIN, 0);
1155 		neigh_release(neigh);
1156 	}
1157 	return err;
1158 }
1159 
1160 static unsigned int arp_state_to_flags(struct neighbour *neigh)
1161 {
1162 	if (neigh->nud_state&NUD_PERMANENT)
1163 		return ATF_PERM | ATF_COM;
1164 	else if (neigh->nud_state&NUD_VALID)
1165 		return ATF_COM;
1166 	else
1167 		return 0;
1168 }
1169 
1170 /*
1171  *	Get an ARP cache entry.
1172  */
1173 
1174 static int arp_req_get(struct net *net, struct arpreq *r)
1175 {
1176 	__be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
1177 	struct neigh_table *tbl = arp_table(net);
1178 	struct neighbour *neigh;
1179 	struct net_device *dev;
1180 
1181 	if (!r->arp_dev[0])
1182 		return -ENODEV;
1183 
1184 	dev = arp_req_dev_by_name(net, r, true);
1185 	if (IS_ERR(dev))
1186 		return PTR_ERR(dev);
1187 
1188 	neigh = neigh_lookup(tbl, &ip, dev);
1189 	if (!neigh)
1190 		return -ENXIO;
1191 
1192 	if (READ_ONCE(neigh->nud_state) & NUD_NOARP) {
1193 		neigh_release(neigh);
1194 		return -ENXIO;
1195 	}
1196 
1197 	read_lock_bh(&neigh->lock);
1198 	memcpy(r->arp_ha.sa_data, neigh->ha,
1199 	       min(dev->addr_len, sizeof(r->arp_ha.sa_data)));
1200 	r->arp_flags = arp_state_to_flags(neigh);
1201 	read_unlock_bh(&neigh->lock);
1202 
1203 	neigh_release(neigh);
1204 
1205 	r->arp_ha.sa_family = dev->type;
1206 	netdev_copy_name(dev, r->arp_dev);
1207 
1208 	return 0;
1209 }
1210 
1211 int arp_invalidate(struct net_device *dev, __be32 ip, bool force)
1212 {
1213 	struct neigh_table *tbl = arp_table(dev_net(dev));
1214 	struct neighbour *neigh;
1215 	int err = -ENXIO;
1216 
1217 	neigh = neigh_lookup(tbl, &ip, dev);
1218 	if (neigh) {
1219 		if ((READ_ONCE(neigh->nud_state) & NUD_VALID) && !force) {
1220 			neigh_release(neigh);
1221 			return 0;
1222 		}
1223 
1224 		if (READ_ONCE(neigh->nud_state) & ~NUD_NOARP)
1225 			err = neigh_update(neigh, NULL, NUD_FAILED,
1226 					   NEIGH_UPDATE_F_OVERRIDE|
1227 					   NEIGH_UPDATE_F_ADMIN, 0);
1228 		spin_lock_bh(&tbl->lock);
1229 		neigh_release(neigh);
1230 		neigh_remove_one(neigh);
1231 		spin_unlock_bh(&tbl->lock);
1232 	}
1233 
1234 	return err;
1235 }
1236 
1237 static int arp_req_delete_public(struct net *net, struct arpreq *r,
1238 				 struct net_device *dev)
1239 {
1240 	__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
1241 	struct neigh_table *tbl = arp_table(net);
1242 
1243 	if (mask) {
1244 		__be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1245 
1246 		return pneigh_delete(tbl, &ip, dev);
1247 	}
1248 
1249 	return arp_req_set_proxy(net, dev, 0);
1250 }
1251 
1252 static int arp_req_delete(struct net *net, struct arpreq *r)
1253 {
1254 	struct net_device *dev;
1255 	__be32 ip;
1256 
1257 	dev = arp_req_dev(net, r);
1258 	if (IS_ERR(dev))
1259 		return PTR_ERR(dev);
1260 
1261 	if (r->arp_flags & ATF_PUBL)
1262 		return arp_req_delete_public(net, r, dev);
1263 
1264 	ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1265 
1266 	return arp_invalidate(dev, ip, true);
1267 }
1268 
1269 /*
1270  *	Handle an ARP layer I/O control request.
1271  */
1272 
1273 int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1274 {
1275 	struct arpreq r;
1276 	__be32 *netmask;
1277 	int err;
1278 
1279 	switch (cmd) {
1280 	case SIOCDARP:
1281 	case SIOCSARP:
1282 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1283 			return -EPERM;
1284 		fallthrough;
1285 	case SIOCGARP:
1286 		err = copy_from_user(&r, arg, sizeof(struct arpreq));
1287 		if (err)
1288 			return -EFAULT;
1289 		r.arp_dev[IFNAMSIZ - 1] = '\0';
1290 		break;
1291 	default:
1292 		return -EINVAL;
1293 	}
1294 
1295 	if (r.arp_pa.sa_family != AF_INET)
1296 		return -EPFNOSUPPORT;
1297 
1298 	if (!(r.arp_flags & ATF_PUBL) &&
1299 	    (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
1300 		return -EINVAL;
1301 
1302 	netmask = &((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr;
1303 	if (!(r.arp_flags & ATF_NETMASK))
1304 		*netmask = htonl(0xFFFFFFFFUL);
1305 	else if (*netmask && *netmask != htonl(0xFFFFFFFFUL))
1306 		return -EINVAL;
1307 
1308 	switch (cmd) {
1309 	case SIOCDARP:
1310 		rtnl_net_lock(net);
1311 		err = arp_req_delete(net, &r);
1312 		rtnl_net_unlock(net);
1313 		break;
1314 	case SIOCSARP:
1315 		rtnl_net_lock(net);
1316 		err = arp_req_set(net, &r);
1317 		rtnl_net_unlock(net);
1318 		break;
1319 	case SIOCGARP:
1320 		rcu_read_lock();
1321 		err = arp_req_get(net, &r);
1322 		rcu_read_unlock();
1323 
1324 		if (!err && copy_to_user(arg, &r, sizeof(r)))
1325 			err = -EFAULT;
1326 		break;
1327 	}
1328 
1329 	return err;
1330 }
1331 
1332 static int arp_netdev_event(struct notifier_block *this, unsigned long event,
1333 			    void *ptr)
1334 {
1335 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1336 	struct netdev_notifier_change_info *change_info;
1337 	struct net *net = dev_net(dev);
1338 	struct in_device *in_dev;
1339 	struct neigh_table *tbl;
1340 	bool evict_nocarrier;
1341 
1342 	tbl = arp_table(net);
1343 
1344 	switch (event) {
1345 	case NETDEV_CHANGEADDR:
1346 		neigh_changeaddr(tbl, dev);
1347 		rt_cache_flush(net);
1348 		break;
1349 	case NETDEV_CHANGE:
1350 		change_info = ptr;
1351 		if (change_info->flags_changed & IFF_NOARP)
1352 			neigh_changeaddr(tbl, dev);
1353 
1354 		in_dev = __in_dev_get_rtnl(dev);
1355 		if (!in_dev)
1356 			evict_nocarrier = true;
1357 		else
1358 			evict_nocarrier = IN_DEV_ARP_EVICT_NOCARRIER(in_dev);
1359 
1360 		if (evict_nocarrier && !netif_carrier_ok(dev))
1361 			neigh_carrier_down(tbl, dev);
1362 		break;
1363 	default:
1364 		break;
1365 	}
1366 
1367 	return NOTIFY_DONE;
1368 }
1369 
1370 static struct notifier_block arp_netdev_notifier = {
1371 	.notifier_call = arp_netdev_event,
1372 };
1373 
1374 /* Note, that it is not on notifier chain.
1375    It is necessary, that this routine was called after route cache will be
1376    flushed.
1377  */
1378 void arp_ifdown(struct net_device *dev)
1379 {
1380 	struct neigh_table *tbl = arp_table(dev_net(dev));
1381 
1382 	neigh_ifdown(tbl, dev);
1383 }
1384 
1385 
1386 /*
1387  *	Called once on startup.
1388  */
1389 
1390 static struct packet_type arp_packet_type __read_mostly = {
1391 	.type =	cpu_to_be16(ETH_P_ARP),
1392 	.func =	arp_rcv,
1393 };
1394 
1395 #ifdef CONFIG_PROC_FS
1396 #if IS_ENABLED(CONFIG_AX25)
1397 
1398 /*
1399  *	ax25 -> ASCII conversion
1400  */
1401 static void ax2asc2(ax25_address *a, char *buf)
1402 {
1403 	char c, *s;
1404 	int n;
1405 
1406 	for (n = 0, s = buf; n < 6; n++) {
1407 		c = (a->ax25_call[n] >> 1) & 0x7F;
1408 
1409 		if (c != ' ')
1410 			*s++ = c;
1411 	}
1412 
1413 	*s++ = '-';
1414 	n = (a->ax25_call[6] >> 1) & 0x0F;
1415 	if (n > 9) {
1416 		*s++ = '1';
1417 		n -= 10;
1418 	}
1419 
1420 	*s++ = n + '0';
1421 	*s++ = '\0';
1422 
1423 	if (*buf == '\0' || *buf == '-') {
1424 		buf[0] = '*';
1425 		buf[1] = '\0';
1426 	}
1427 }
1428 #endif /* CONFIG_AX25 */
1429 
1430 #define HBUFFERLEN 30
1431 
1432 static void arp_format_neigh_entry(struct seq_file *seq,
1433 				   struct neighbour *n)
1434 {
1435 	char hbuffer[HBUFFERLEN];
1436 	int k, j;
1437 	char tbuf[16];
1438 	struct net_device *dev = n->dev;
1439 	int hatype = dev->type;
1440 
1441 	read_lock(&n->lock);
1442 	/* Convert hardware address to XX:XX:XX:XX ... form. */
1443 #if IS_ENABLED(CONFIG_AX25)
1444 	if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
1445 		ax2asc2((ax25_address *)n->ha, hbuffer);
1446 	else {
1447 #endif
1448 	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
1449 		hbuffer[k++] = hex_asc_hi(n->ha[j]);
1450 		hbuffer[k++] = hex_asc_lo(n->ha[j]);
1451 		hbuffer[k++] = ':';
1452 	}
1453 	if (k != 0)
1454 		--k;
1455 	hbuffer[k] = 0;
1456 #if IS_ENABLED(CONFIG_AX25)
1457 	}
1458 #endif
1459 	sprintf(tbuf, "%pI4", n->primary_key);
1460 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%-17s     *        %s\n",
1461 		   tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
1462 	read_unlock(&n->lock);
1463 }
1464 
1465 static void arp_format_pneigh_entry(struct seq_file *seq,
1466 				    struct pneigh_entry *n)
1467 {
1468 	struct net_device *dev = n->dev;
1469 	int hatype = dev ? dev->type : 0;
1470 	char tbuf[16];
1471 
1472 	sprintf(tbuf, "%pI4", n->key);
1473 	seq_printf(seq, "%-16s 0x%-10x0x%-10x%s     *        %s\n",
1474 		   tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
1475 		   dev ? dev->name : "*");
1476 }
1477 
1478 static int arp_seq_show(struct seq_file *seq, void *v)
1479 {
1480 	if (v == SEQ_START_TOKEN) {
1481 		seq_puts(seq, "IP address       HW type     Flags       "
1482 			      "HW address            Mask     Device\n");
1483 	} else {
1484 		struct neigh_seq_state *state = seq->private;
1485 
1486 		if (state->flags & NEIGH_SEQ_IS_PNEIGH)
1487 			arp_format_pneigh_entry(seq, v);
1488 		else
1489 			arp_format_neigh_entry(seq, v);
1490 	}
1491 
1492 	return 0;
1493 }
1494 
1495 static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
1496 {
1497 	struct neigh_table *tbl = arp_table(seq_file_net(seq));
1498 
1499 	/* Don't want to confuse "arp -a" w/ magic entries,
1500 	 * so we tell the generic iterator to skip NUD_NOARP.
1501 	 */
1502 	return neigh_seq_start(seq, pos, tbl, NEIGH_SEQ_SKIP_NOARP);
1503 }
1504 
1505 static const struct seq_operations arp_seq_ops = {
1506 	.start	= arp_seq_start,
1507 	.next	= neigh_seq_next,
1508 	.stop	= neigh_seq_stop,
1509 	.show	= arp_seq_show,
1510 };
1511 #endif /* CONFIG_PROC_FS */
1512 
1513 static int __net_init arp_net_init(struct net *net)
1514 {
1515 	int err;
1516 
1517 	err = neigh_table_register(net, &arp_tbl, NEIGH_ARP_TABLE);
1518 	if (err)
1519 		goto err;
1520 
1521 #ifdef CONFIG_PROC_FS
1522 #ifdef CONFIG_SYSCTL
1523 	err = neigh_sysctl_register(NULL, &arp_table(net)->parms, NULL);
1524 	if (err)
1525 		goto err_sysctl;
1526 #endif
1527 
1528 	if (!proc_create_net("arp", 0444, net->proc_net, &arp_seq_ops,
1529 			     sizeof(struct neigh_seq_state))) {
1530 		err = -ENOMEM;
1531 		goto err_proc_create;
1532 	}
1533 #endif
1534 
1535 	return 0;
1536 
1537 #ifdef CONFIG_PROC_FS
1538 err_proc_create:
1539 #ifdef CONFIG_SYSCTL
1540 	neigh_sysctl_unregister(&arp_table(net)->parms);
1541 err_sysctl:
1542 #endif
1543 	neigh_table_unregister(net, NEIGH_ARP_TABLE);
1544 #endif
1545 err:
1546 	return err;
1547 }
1548 
1549 static void __net_exit arp_net_exit(struct net *net)
1550 {
1551 	remove_proc_entry("arp", net->proc_net);
1552 #ifdef CONFIG_SYSCTL
1553 	neigh_sysctl_unregister(&arp_table(net)->parms);
1554 #endif
1555 	neigh_table_unregister(net, NEIGH_ARP_TABLE);
1556 }
1557 
1558 static struct pernet_operations arp_net_ops = {
1559 	.init = arp_net_init,
1560 	.exit = arp_net_exit,
1561 };
1562 
1563 void __init arp_init(void)
1564 {
1565 	if (register_pernet_subsys(&arp_net_ops))
1566 		panic("Cannot allocate arp table\n");
1567 
1568 	dev_add_pack(&arp_packet_type);
1569 	register_netdevice_notifier(&arp_netdev_notifier);
1570 }
1571