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