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