make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / net / ipv4 / arp.c
1 /* linux/net/inet/arp.c
2  *
3  * Version:     $Id: arp.c,v 1.1.1.1 2005/04/11 02:51:13 jack 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  */
70
71 #include <linux/types.h>
72 #include <linux/string.h>
73 #include <linux/kernel.h>
74 #include <linux/sched.h>
75 #include <linux/config.h>
76 #include <linux/socket.h>
77 #include <linux/sockios.h>
78 #include <linux/errno.h>
79 #include <linux/in.h>
80 #include <linux/mm.h>
81 #include <linux/inet.h>
82 #include <linux/netdevice.h>
83 #include <linux/etherdevice.h>
84 #include <linux/fddidevice.h>
85 #include <linux/if_arp.h>
86 #include <linux/trdevice.h>
87 #include <linux/skbuff.h>
88 #include <linux/proc_fs.h>
89 #include <linux/stat.h>
90 #include <linux/init.h>
91 #ifdef CONFIG_SYSCTL
92 #include <linux/sysctl.h>
93 #endif
94
95 #include <net/ip.h>
96 #include <net/icmp.h>
97 #include <net/route.h>
98 #include <net/protocol.h>
99 #include <net/tcp.h>
100 #include <net/sock.h>
101 #include <net/arp.h>
102 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
103 #include <net/ax25.h>
104 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
105 #include <net/netrom.h>
106 #endif
107 #endif
108 #ifdef CONFIG_ATM_CLIP
109 #include <net/atmclip.h>
110 #endif
111
112 #include <asm/system.h>
113 #include <asm/uaccess.h>
114
115 #include <linux/netfilter_arp.h>
116
117 /*
118  *      Interface to generic neighbour cache.
119  */
120 static u32 arp_hash(const void *pkey, const struct net_device *dev);
121 static int arp_constructor(struct neighbour *neigh);
122 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
123 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
124 static void parp_redo(struct sk_buff *skb);
125
126 static struct neigh_ops arp_generic_ops = {
127         family:                 AF_INET,
128         solicit:                arp_solicit,
129         error_report:           arp_error_report,
130         output:                 neigh_resolve_output,
131         connected_output:       neigh_connected_output,
132         hh_output:              dev_queue_xmit,
133         queue_xmit:             dev_queue_xmit,
134 };
135
136 static struct neigh_ops arp_hh_ops = {
137         family:                 AF_INET,
138         solicit:                arp_solicit,
139         error_report:           arp_error_report,
140         output:                 neigh_resolve_output,
141         connected_output:       neigh_resolve_output,
142         hh_output:              dev_queue_xmit,
143         queue_xmit:             dev_queue_xmit,
144 };
145
146 static struct neigh_ops arp_direct_ops = {
147         family:                 AF_INET,
148         output:                 dev_queue_xmit,
149         connected_output:       dev_queue_xmit,
150         hh_output:              dev_queue_xmit,
151         queue_xmit:             dev_queue_xmit,
152 };
153
154 struct neigh_ops arp_broken_ops = {
155         family:                 AF_INET,
156         solicit:                arp_solicit,
157         error_report:           arp_error_report,
158         output:                 neigh_compat_output,
159         connected_output:       neigh_compat_output,
160         hh_output:              dev_queue_xmit,
161         queue_xmit:             dev_queue_xmit,
162 };
163
164 struct neigh_table arp_tbl = {
165         family:         AF_INET,
166         entry_size:     sizeof(struct neighbour) + 4,
167         key_len:        4,
168         hash:           arp_hash,
169         constructor:    arp_constructor,
170         proxy_redo:     parp_redo,
171         id:             "arp_cache",
172         parms: {
173                 tbl:                    &arp_tbl,
174                 base_reachable_time:    30 * HZ,
175                 retrans_time:           1 * HZ,
176                 gc_staletime:           60 * HZ,
177                 reachable_time:         30 * HZ,
178                 delay_probe_time:       5 * HZ,
179                 queue_len:              3,
180                 ucast_probes:           3,
181                 mcast_probes:           3,
182                 anycast_delay:          1 * HZ,
183                 proxy_delay:            (8 * HZ) / 10,
184                 proxy_qlen:             64,
185                 locktime:               1 * HZ,
186         },
187         gc_interval:    30 * HZ,
188         gc_thresh1:     128,
189         gc_thresh2:     512,
190         gc_thresh3:     1024,
191 };
192
193 int arp_mc_map(u32 addr, u8 *haddr, struct net_device *dev, int dir)
194 {
195         switch (dev->type) {
196         case ARPHRD_ETHER:
197         case ARPHRD_FDDI:
198         case ARPHRD_IEEE802:
199                 ip_eth_mc_map(addr, haddr);
200                 return 0; 
201         case ARPHRD_IEEE802_TR:
202                 ip_tr_mc_map(addr, haddr);
203                 return 0;
204         default:
205                 if (dir) {
206                         memcpy(haddr, dev->broadcast, dev->addr_len);
207                         return 0;
208                 }
209         }
210         return -EINVAL;
211 }
212
213
214 static u32 arp_hash(const void *pkey, const struct net_device *dev)
215 {
216         u32 hash_val;
217
218         hash_val = *(u32*)pkey;
219         hash_val ^= (hash_val>>16);
220         hash_val ^= hash_val>>8;
221         hash_val ^= hash_val>>3;
222         hash_val = (hash_val^dev->ifindex)&NEIGH_HASHMASK;
223
224         return hash_val;
225 }
226
227 static int arp_constructor(struct neighbour *neigh)
228 {
229         u32 addr = *(u32*)neigh->primary_key;
230         struct net_device *dev = neigh->dev;
231         struct in_device *in_dev = in_dev_get(dev);
232
233         if (in_dev == NULL)
234                 return -EINVAL;
235
236         neigh->type = inet_addr_type(addr);
237         if (in_dev->arp_parms)
238                 neigh->parms = in_dev->arp_parms;
239
240         in_dev_put(in_dev);
241
242         if (dev->hard_header == NULL) {
243                 neigh->nud_state = NUD_NOARP;
244                 neigh->ops = &arp_direct_ops;
245                 neigh->output = neigh->ops->queue_xmit;
246         } else {
247                 /* Good devices (checked by reading texts, but only Ethernet is
248                    tested)
249
250                    ARPHRD_ETHER: (ethernet, apfddi)
251                    ARPHRD_FDDI: (fddi)
252                    ARPHRD_IEEE802: (tr)
253                    ARPHRD_METRICOM: (strip)
254                    ARPHRD_ARCNET:
255                    etc. etc. etc.
256
257                    ARPHRD_IPDDP will also work, if author repairs it.
258                    I did not it, because this driver does not work even
259                    in old paradigm.
260                  */
261
262 #if 1
263                 /* So... these "amateur" devices are hopeless.
264                    The only thing, that I can say now:
265                    It is very sad that we need to keep ugly obsolete
266                    code to make them happy.
267
268                    They should be moved to more reasonable state, now
269                    they use rebuild_header INSTEAD OF hard_start_xmit!!!
270                    Besides that, they are sort of out of date
271                    (a lot of redundant clones/copies, useless in 2.1),
272                    I wonder why people believe that they work.
273                  */
274                 switch (dev->type) {
275                 default:
276                         break;
277                 case ARPHRD_ROSE:       
278 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
279                 case ARPHRD_AX25:
280 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
281                 case ARPHRD_NETROM:
282 #endif
283                         neigh->ops = &arp_broken_ops;
284                         neigh->output = neigh->ops->output;
285                         return 0;
286 #endif
287                 ;}
288 #endif
289                 if (neigh->type == RTN_MULTICAST) {
290                         neigh->nud_state = NUD_NOARP;
291                         arp_mc_map(addr, neigh->ha, dev, 1);
292                 } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
293                         neigh->nud_state = NUD_NOARP;
294                         memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
295                 } else if (neigh->type == RTN_BROADCAST || dev->flags&IFF_POINTOPOINT) {
296                         neigh->nud_state = NUD_NOARP;
297                         memcpy(neigh->ha, dev->broadcast, dev->addr_len);
298                 }
299                 if (dev->hard_header_cache)
300                         neigh->ops = &arp_hh_ops;
301                 else
302                         neigh->ops = &arp_generic_ops;
303                 if (neigh->nud_state&NUD_VALID)
304                         neigh->output = neigh->ops->connected_output;
305                 else
306                         neigh->output = neigh->ops->output;
307         }
308         return 0;
309 }
310
311 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
312 {
313         dst_link_failure(skb);
314         kfree_skb(skb);
315 }
316
317 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
318 {
319         u32 saddr;
320         u8  *dst_ha = NULL;
321         struct net_device *dev = neigh->dev;
322         u32 target = *(u32*)neigh->primary_key;
323         int probes = atomic_read(&neigh->probes);
324
325         if (skb && inet_addr_type(skb->nh.iph->saddr) == RTN_LOCAL)
326                 saddr = skb->nh.iph->saddr;
327         else
328                 saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
329
330         if ((probes -= neigh->parms->ucast_probes) < 0) {
331                 if (!(neigh->nud_state&NUD_VALID))
332                         printk(KERN_DEBUG "trying to ucast probe in NUD_INVALID\n");
333                 dst_ha = neigh->ha;
334                 read_lock_bh(&neigh->lock);
335         } else if ((probes -= neigh->parms->app_probes) < 0) {
336 #ifdef CONFIG_ARPD
337                 neigh_app_ns(neigh);
338 #endif
339                 return;
340         }
341
342         arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
343                  dst_ha, dev->dev_addr, NULL);
344         if (dst_ha)
345                 read_unlock_bh(&neigh->lock);
346 }
347
348 static int arp_filter(__u32 sip, __u32 tip, struct net_device *dev)
349 {
350         struct rtable *rt;
351         int flag = 0; 
352         /*unsigned long now; */
353
354         if (ip_route_output(&rt, sip, tip, 0, 0) < 0) 
355                 return 1;
356         if (rt->u.dst.dev != dev) { 
357                 NET_INC_STATS_BH(ArpFilter);
358                 flag = 1;
359         } 
360         ip_rt_put(rt); 
361         return flag; 
362
363
364 /* OBSOLETE FUNCTIONS */
365
366 /*
367  *      Find an arp mapping in the cache. If not found, post a request.
368  *
369  *      It is very UGLY routine: it DOES NOT use skb->dst->neighbour,
370  *      even if it exists. It is supposed that skb->dev was mangled
371  *      by a virtual device (eql, shaper). Nobody but broken devices
372  *      is allowed to use this function, it is scheduled to be removed. --ANK
373  */
374
375 static int arp_set_predefined(int addr_hint, unsigned char * haddr, u32 paddr, struct net_device * dev)
376 {
377         switch (addr_hint) {
378         case RTN_LOCAL:
379                 printk(KERN_DEBUG "ARP: arp called for own IP address\n");
380                 memcpy(haddr, dev->dev_addr, dev->addr_len);
381                 return 1;
382         case RTN_MULTICAST:
383                 arp_mc_map(paddr, haddr, dev, 1);
384                 return 1;
385         case RTN_BROADCAST:
386                 memcpy(haddr, dev->broadcast, dev->addr_len);
387                 return 1;
388         }
389         return 0;
390 }
391
392
393 int arp_find(unsigned char *haddr, struct sk_buff *skb)
394 {
395         struct net_device *dev = skb->dev;
396         u32 paddr;
397         struct neighbour *n;
398
399         if (!skb->dst) {
400                 printk(KERN_DEBUG "arp_find is called with dst==NULL\n");
401                 kfree_skb(skb);
402                 return 1;
403         }
404
405         paddr = ((struct rtable*)skb->dst)->rt_gateway;
406
407         if (arp_set_predefined(inet_addr_type(paddr), haddr, paddr, dev))
408                 return 0;
409
410         n = __neigh_lookup(&arp_tbl, &paddr, dev, 1);
411
412         if (n) {
413                 n->used = jiffies;
414                 if (n->nud_state&NUD_VALID || neigh_event_send(n, skb) == 0) {
415                         read_lock_bh(&n->lock);
416                         memcpy(haddr, n->ha, dev->addr_len);
417                         read_unlock_bh(&n->lock);
418                         neigh_release(n);
419                         return 0;
420                 }
421                 neigh_release(n);
422         } else
423                 kfree_skb(skb);
424         return 1;
425 }
426
427 /* END OF OBSOLETE FUNCTIONS */
428
429 int arp_bind_neighbour(struct dst_entry *dst)
430 {
431         struct net_device *dev = dst->dev;
432         struct neighbour *n = dst->neighbour;
433
434         if (dev == NULL)
435                 return -EINVAL;
436         if (n == NULL) {
437                 u32 nexthop = ((struct rtable*)dst)->rt_gateway;
438                 if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT))
439                         nexthop = 0;
440                 n = __neigh_lookup_errno(
441 #ifdef CONFIG_ATM_CLIP
442                     dev->type == ARPHRD_ATM ? &clip_tbl :
443 #endif
444                     &arp_tbl, &nexthop, dev);
445                 if (IS_ERR(n))
446                         return PTR_ERR(n);
447                 dst->neighbour = n;
448         }
449         return 0;
450 }
451
452 /*
453  * Check if we can use proxy ARP for this path
454  */
455
456 static inline int arp_fwd_proxy(struct in_device *in_dev, struct rtable *rt)
457 {
458         struct in_device *out_dev;
459         int imi, omi = -1;
460
461         if (!IN_DEV_PROXY_ARP(in_dev))
462                 return 0;
463
464         if ((imi = IN_DEV_MEDIUM_ID(in_dev)) == 0)
465                 return 1;
466         if (imi == -1)
467                 return 0;
468
469         /* place to check for proxy_arp for routes */
470
471         if ((out_dev = in_dev_get(rt->u.dst.dev)) != NULL) {
472                 omi = IN_DEV_MEDIUM_ID(out_dev);
473                 in_dev_put(out_dev);
474         }
475         return (omi != imi && omi != -1);
476 }
477
478 /*
479  *      Interface to link layer: send routine and receive handler.
480  */
481
482 /*
483  *      Create and send an arp packet. If (dest_hw == NULL), we create a broadcast
484  *      message.
485  */
486
487 void arp_send(int type, int ptype, u32 dest_ip, 
488               struct net_device *dev, u32 src_ip, 
489               unsigned char *dest_hw, unsigned char *src_hw,
490               unsigned char *target_hw)
491 {
492         struct sk_buff *skb;
493         struct arphdr *arp;
494         unsigned char *arp_ptr;
495
496         /*
497          *      No arp on this interface.
498          */
499         
500         if (dev->flags&IFF_NOARP)
501                 return;
502
503         /*
504          *      Allocate a buffer
505          */
506         
507         skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
508                                 + dev->hard_header_len + 15, GFP_ATOMIC);
509         if (skb == NULL)
510                 return;
511
512         skb_reserve(skb, (dev->hard_header_len+15)&~15);
513         skb->nh.raw = skb->data;
514         arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4));
515         skb->dev = dev;
516         skb->protocol = htons (ETH_P_ARP);
517         if (src_hw == NULL)
518                 src_hw = dev->dev_addr;
519         if (dest_hw == NULL)
520                 dest_hw = dev->broadcast;
521
522         /*
523          *      Fill the device header for the ARP frame
524          */
525         if (dev->hard_header &&
526             dev->hard_header(skb,dev,ptype,dest_hw,src_hw,skb->len) < 0)
527                 goto out;
528
529         /*
530          * Fill out the arp protocol part.
531          *
532          * The arp hardware type should match the device type, except for FDDI,
533          * which (according to RFC 1390) should always equal 1 (Ethernet).
534          */
535         /*
536          *      Exceptions everywhere. AX.25 uses the AX.25 PID value not the
537          *      DIX code for the protocol. Make these device structure fields.
538          */
539         switch (dev->type) {
540         default:
541                 arp->ar_hrd = htons(dev->type);
542                 arp->ar_pro = htons(ETH_P_IP);
543                 break;
544
545 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
546         case ARPHRD_AX25:
547                 arp->ar_hrd = htons(ARPHRD_AX25);
548                 arp->ar_pro = htons(AX25_P_IP);
549                 break;
550
551 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
552         case ARPHRD_NETROM:
553                 arp->ar_hrd = htons(ARPHRD_NETROM);
554                 arp->ar_pro = htons(AX25_P_IP);
555                 break;
556 #endif
557 #endif
558
559 #ifdef CONFIG_FDDI
560         case ARPHRD_FDDI:
561                 arp->ar_hrd = htons(ARPHRD_ETHER);
562                 arp->ar_pro = htons(ETH_P_IP);
563                 break;
564 #endif
565 #ifdef CONFIG_TR
566         case ARPHRD_IEEE802_TR:
567                 arp->ar_hrd = htons(ARPHRD_IEEE802);
568                 arp->ar_pro = htons(ETH_P_IP);
569                 break;
570 #endif
571         }
572
573         arp->ar_hln = dev->addr_len;
574         arp->ar_pln = 4;
575         arp->ar_op = htons(type);
576
577         arp_ptr=(unsigned char *)(arp+1);
578
579         memcpy(arp_ptr, src_hw, dev->addr_len);
580         arp_ptr+=dev->addr_len;
581         memcpy(arp_ptr, &src_ip,4);
582         arp_ptr+=4;
583         if (target_hw != NULL)
584                 memcpy(arp_ptr, target_hw, dev->addr_len);
585         else
586                 memset(arp_ptr, 0, dev->addr_len);
587         arp_ptr+=dev->addr_len;
588         memcpy(arp_ptr, &dest_ip, 4);
589
590         /* Send it off, maybe filter it using firewalling first.  */
591         NF_HOOK(NF_ARP, NF_ARP_OUT, skb, NULL, dev, dev_queue_xmit);
592         return;
593
594 out:
595         kfree_skb(skb);
596 }
597
598 static void parp_redo(struct sk_buff *skb)
599 {
600         arp_rcv(skb, skb->dev, NULL);
601 }
602
603 /*
604  *      Process an arp request.
605  */
606
607 int arp_process(struct sk_buff *skb)
608 {
609         struct net_device *dev = skb->dev;
610         struct in_device *in_dev = in_dev_get(dev);
611         struct arphdr *arp;
612         unsigned char *arp_ptr;
613         struct rtable *rt;
614         unsigned char *sha, *tha;
615         u32 sip, tip;
616         u16 dev_type = dev->type;
617         int addr_type;
618         struct neighbour *n;
619
620         /* arp_rcv below verifies the ARP header, verifies the device
621          * is ARP'able, and linearizes the SKB (if needed).
622          */
623
624         if (in_dev == NULL)
625                 goto out;
626
627         arp = skb->nh.arph;
628         arp_ptr= (unsigned char *)(arp+1);
629
630         switch (dev_type) {
631         default:        
632                 if (arp->ar_pro != htons(ETH_P_IP))
633                         goto out;
634                 if (htons(dev_type) != arp->ar_hrd)
635                         goto out;
636                 break;
637 #ifdef CONFIG_NET_ETHERNET
638         case ARPHRD_ETHER:
639                 /*
640                  * ETHERNET devices will accept ARP hardware types of either
641                  * 1 (Ethernet) or 6 (IEEE 802.2).
642                  */
643                 if (arp->ar_hrd != htons(ARPHRD_ETHER) &&
644                     arp->ar_hrd != htons(ARPHRD_IEEE802))
645                         goto out;
646                 if (arp->ar_pro != htons(ETH_P_IP))
647                         goto out;
648                 break;
649 #endif
650 #ifdef CONFIG_TR
651         case ARPHRD_IEEE802_TR:
652                 /*
653                  * Token ring devices will accept ARP hardware types of either
654                  * 1 (Ethernet) or 6 (IEEE 802.2).
655                  */
656                 if (arp->ar_hrd != htons(ARPHRD_ETHER) &&
657                     arp->ar_hrd != htons(ARPHRD_IEEE802))
658                         goto out;
659                 if (arp->ar_pro != htons(ETH_P_IP))
660                         goto out;
661                 break;
662 #endif
663 #ifdef CONFIG_FDDI
664         case ARPHRD_FDDI:
665                 /*
666                  * According to RFC 1390, FDDI devices should accept ARP hardware types
667                  * of 1 (Ethernet).  However, to be more robust, we'll accept hardware
668                  * types of either 1 (Ethernet) or 6 (IEEE 802.2).
669                  */
670                 if (arp->ar_hrd != htons(ARPHRD_ETHER) &&
671                     arp->ar_hrd != htons(ARPHRD_IEEE802))
672                         goto out;
673                 if (arp->ar_pro != htons(ETH_P_IP))
674                         goto out;
675                 break;
676 #endif
677 #ifdef CONFIG_NET_FC
678         case ARPHRD_IEEE802:
679                 /*
680                  * According to RFC 2625, Fibre Channel devices (which are IEEE
681                  * 802 devices) should accept ARP hardware types of 6 (IEEE 802)
682                  * and 1 (Ethernet).
683                  */
684                 if (arp->ar_hrd != htons(ARPHRD_ETHER) &&
685                     arp->ar_hrd != htons(ARPHRD_IEEE802))
686                         goto out;
687                 if (arp->ar_pro != htons(ETH_P_IP))
688                         goto out;
689                 break;
690 #endif
691 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
692         case ARPHRD_AX25:
693                 if (arp->ar_pro != htons(AX25_P_IP))
694                         goto out;
695                 if (arp->ar_hrd != htons(ARPHRD_AX25))
696                         goto out;
697                 break;
698 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
699         case ARPHRD_NETROM:
700                 if (arp->ar_pro != htons(AX25_P_IP))
701                         goto out;
702                 if (arp->ar_hrd != htons(ARPHRD_NETROM))
703                         goto out;
704                 break;
705 #endif
706 #endif
707         }
708
709         /* Understand only these message types */
710
711         if (arp->ar_op != htons(ARPOP_REPLY) &&
712             arp->ar_op != htons(ARPOP_REQUEST))
713                 goto out;
714
715 /*
716  *      Extract fields
717  */
718         sha=arp_ptr;
719         arp_ptr += dev->addr_len;
720         memcpy(&sip, arp_ptr, 4);
721         arp_ptr += 4;
722         tha=arp_ptr;
723         arp_ptr += dev->addr_len;
724         memcpy(&tip, arp_ptr, 4);
725 /* 
726  *      Check for bad requests for 127.x.x.x and requests for multicast
727  *      addresses.  If this is one such, delete it.
728  */
729         if (LOOPBACK(tip) || MULTICAST(tip))
730                 goto out;
731
732 /*
733  *     Special case: We must set Frame Relay source Q.922 address
734  */
735         if (dev_type == ARPHRD_DLCI)
736                 sha = dev->broadcast;
737
738 /*
739  *  Process entry.  The idea here is we want to send a reply if it is a
740  *  request for us or if it is a request for someone else that we hold
741  *  a proxy for.  We want to add an entry to our cache if it is a reply
742  *  to us or if it is a request for our address.  
743  *  (The assumption for this last is that if someone is requesting our 
744  *  address, they are probably intending to talk to us, so it saves time 
745  *  if we cache their address.  Their address is also probably not in 
746  *  our cache, since ours is not in their cache.)
747  * 
748  *  Putting this another way, we only care about replies if they are to
749  *  us, in which case we add them to the cache.  For requests, we care
750  *  about those for us and those for our proxies.  We reply to both,
751  *  and in the case of requests for us we add the requester to the arp 
752  *  cache.
753  */
754
755         /* Special case: IPv4 duplicate address detection packet (RFC2131) */
756         if (sip == 0) {
757                 if (arp->ar_op == htons(ARPOP_REQUEST) &&
758                     inet_addr_type(tip) == RTN_LOCAL)
759                         arp_send(ARPOP_REPLY,ETH_P_ARP,tip,dev,tip,sha,dev->dev_addr,dev->dev_addr);
760                 goto out;
761         }
762
763         if (arp->ar_op == htons(ARPOP_REQUEST) &&
764             ip_route_input(skb, tip, sip, 0, dev) == 0) {
765
766                 rt = (struct rtable*)skb->dst;
767                 addr_type = rt->rt_type;
768
769                 if (addr_type == RTN_LOCAL) {
770                         n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
771                         if (n) {
772                                 int dont_send = 0;
773                                 if (IN_DEV_ARPFILTER(in_dev))
774                                         dont_send |= arp_filter(sip,tip,dev); 
775                                 if (!dont_send)
776                                         arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
777
778                                 neigh_release(n);
779                         }
780                         goto out;
781                 } else if (IN_DEV_FORWARD(in_dev)) {
782                         if ((rt->rt_flags&RTCF_DNAT) ||
783                             (addr_type == RTN_UNICAST  && rt->u.dst.dev != dev &&
784                              (arp_fwd_proxy(in_dev, rt) || pneigh_lookup(&arp_tbl, &tip, dev, 0)))) {
785                                 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
786                                 if (n)
787                                         neigh_release(n);
788
789                                 if (skb->stamp.tv_sec == 0 ||
790                                     skb->pkt_type == PACKET_HOST ||
791                                     in_dev->arp_parms->proxy_delay == 0) {
792                                         arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
793                                 } else {
794                                         pneigh_enqueue(&arp_tbl, in_dev->arp_parms, skb);
795                                         in_dev_put(in_dev);
796                                         return 0;
797                                 }
798                                 goto out;
799                         }
800                 }
801         }
802
803         /* Update our ARP tables */
804
805         n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
806
807 #ifdef CONFIG_IP_ACCEPT_UNSOLICITED_ARP
808         /* Unsolicited ARP is not accepted by default.
809            It is possible, that this option should be enabled for some
810            devices (strip is candidate)
811          */
812         if (n == NULL &&
813             arp->ar_op == htons(ARPOP_REPLY) &&
814             inet_addr_type(sip) == RTN_UNICAST)
815                 n = __neigh_lookup(&arp_tbl, &sip, dev, -1);
816 #endif
817
818         if (n) {
819                 int state = NUD_REACHABLE;
820                 int override = 0;
821
822                 /* If several different ARP replies follows back-to-back,
823                    use the FIRST one. It is possible, if several proxy
824                    agents are active. Taking the first reply prevents
825                    arp trashing and chooses the fastest router.
826                  */
827                 if (jiffies - n->updated >= n->parms->locktime)
828                         override = 1;
829
830                 /* Broadcast replies and request packets
831                    do not assert neighbour reachability.
832                  */
833                 if (arp->ar_op != htons(ARPOP_REPLY) ||
834                     skb->pkt_type != PACKET_HOST)
835                         state = NUD_STALE;
836                 neigh_update(n, sha, state, override, 1);
837                 neigh_release(n);
838         }
839
840 out:
841         if (in_dev)
842                 in_dev_put(in_dev);
843         kfree_skb(skb);
844         return 0;
845 }
846
847
848 /*
849  *      Receive an arp request from the device layer.
850  */
851
852 int arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
853 {
854         struct arphdr *arp = skb->nh.arph;
855
856         if (arp->ar_hln != dev->addr_len ||
857             dev->flags & IFF_NOARP ||
858             skb->pkt_type == PACKET_OTHERHOST ||
859             skb->pkt_type == PACKET_LOOPBACK ||
860             arp->ar_pln != 4)
861                 goto freeskb;
862
863         if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
864                 goto out_of_mem;
865
866         if (skb_is_nonlinear(skb)) {
867                 if (skb_linearize(skb, GFP_ATOMIC) != 0)
868                         goto freeskb;
869         }
870
871         return NF_HOOK(NF_ARP, NF_ARP_IN, skb, dev, NULL, arp_process);
872
873 freeskb:
874         kfree_skb(skb);
875 out_of_mem:
876         return 0;
877 }
878
879 /*
880  *      User level interface (ioctl, /proc)
881  */
882
883 /*
884  *      Set (create) an ARP cache entry.
885  */
886
887 int arp_req_set(struct arpreq *r, struct net_device * dev)
888 {
889         u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
890         struct neighbour *neigh;
891         int err;
892
893         if (r->arp_flags&ATF_PUBL) {
894                 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
895                 if (mask && mask != 0xFFFFFFFF)
896                         return -EINVAL;
897                 if (!dev && (r->arp_flags & ATF_COM)) {
898                         dev = dev_getbyhwaddr(r->arp_ha.sa_family, r->arp_ha.sa_data);
899                         if (!dev)
900                                 return -ENODEV;
901                 }
902                 if (mask) {
903                         if (pneigh_lookup(&arp_tbl, &ip, dev, 1) == NULL)
904                                 return -ENOBUFS;
905                         return 0;
906                 }
907                 if (dev == NULL) {
908                         ipv4_devconf.proxy_arp = 1;
909                         return 0;
910                 }
911                 if (__in_dev_get(dev)) {
912                         __in_dev_get(dev)->cnf.proxy_arp = 1;
913                         return 0;
914                 }
915                 return -ENXIO;
916         }
917
918         if (r->arp_flags & ATF_PERM)
919                 r->arp_flags |= ATF_COM;
920         if (dev == NULL) {
921                 struct rtable * rt;
922                 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
923                         return err;
924                 dev = rt->u.dst.dev;
925                 ip_rt_put(rt);
926                 if (!dev)
927                         return -EINVAL;
928         }
929         if (r->arp_ha.sa_family != dev->type)   
930                 return -EINVAL;
931
932         neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
933         err = PTR_ERR(neigh);
934         if (!IS_ERR(neigh)) {
935                 unsigned state = NUD_STALE;
936                 if (r->arp_flags & ATF_PERM)
937                         state = NUD_PERMANENT;
938                 err = neigh_update(neigh, (r->arp_flags&ATF_COM) ?
939                                    r->arp_ha.sa_data : NULL, state, 1, 0);
940                 neigh_release(neigh);
941         }
942         return err;
943 }
944
945 static unsigned arp_state_to_flags(struct neighbour *neigh)
946 {
947         unsigned flags = 0;
948         if (neigh->nud_state&NUD_PERMANENT)
949                 flags = ATF_PERM|ATF_COM;
950         else if (neigh->nud_state&NUD_VALID)
951                 flags = ATF_COM;
952         return flags;
953 }
954
955 /*
956  *      Get an ARP cache entry.
957  */
958
959 static int arp_req_get(struct arpreq *r, struct net_device *dev)
960 {
961         u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
962         struct neighbour *neigh;
963         int err = -ENXIO;
964
965         neigh = neigh_lookup(&arp_tbl, &ip, dev);
966         if (neigh) {
967                 read_lock_bh(&neigh->lock);
968                 memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
969                 r->arp_flags = arp_state_to_flags(neigh);
970                 read_unlock_bh(&neigh->lock);
971                 r->arp_ha.sa_family = dev->type;
972                 strncpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
973                 neigh_release(neigh);
974                 err = 0;
975         }
976         return err;
977 }
978
979 int arp_req_delete(struct arpreq *r, struct net_device * dev)
980 {
981         int err;
982         u32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
983         struct neighbour *neigh;
984
985         if (r->arp_flags & ATF_PUBL) {
986                 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
987                 if (mask == 0xFFFFFFFF)
988                         return pneigh_delete(&arp_tbl, &ip, dev);
989                 if (mask == 0) {
990                         if (dev == NULL) {
991                                 ipv4_devconf.proxy_arp = 0;
992                                 return 0;
993                         }
994                         if (__in_dev_get(dev)) {
995                                 __in_dev_get(dev)->cnf.proxy_arp = 0;
996                                 return 0;
997                         }
998                         return -ENXIO;
999                 }
1000                 return -EINVAL;
1001         }
1002
1003         if (dev == NULL) {
1004                 struct rtable * rt;
1005                 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
1006                         return err;
1007                 dev = rt->u.dst.dev;
1008                 ip_rt_put(rt);
1009                 if (!dev)
1010                         return -EINVAL;
1011         }
1012         err = -ENXIO;
1013         neigh = neigh_lookup(&arp_tbl, &ip, dev);
1014         if (neigh) {
1015                 if (neigh->nud_state&~NUD_NOARP)
1016                         err = neigh_update(neigh, NULL, NUD_FAILED, 1, 0);
1017                 neigh_release(neigh);
1018         }
1019         return err;
1020 }
1021
1022 /*
1023  *      Handle an ARP layer I/O control request.
1024  */
1025
1026 int arp_ioctl(unsigned int cmd, void *arg)
1027 {
1028         int err;
1029         struct arpreq r;
1030         struct net_device * dev = NULL;
1031
1032         switch(cmd) {
1033                 case SIOCDARP:
1034                 case SIOCSARP:
1035                         if (!capable(CAP_NET_ADMIN))
1036                                 return -EPERM;
1037                 case SIOCGARP:
1038                         err = copy_from_user(&r, arg, sizeof(struct arpreq));
1039                         if (err)
1040                                 return -EFAULT;
1041                         break;
1042                 default:
1043                         return -EINVAL;
1044         }
1045
1046         if (r.arp_pa.sa_family != AF_INET)
1047                 return -EPFNOSUPPORT;
1048
1049         if (!(r.arp_flags & ATF_PUBL) &&
1050             (r.arp_flags & (ATF_NETMASK|ATF_DONTPUB)))
1051                 return -EINVAL;
1052         if (!(r.arp_flags & ATF_NETMASK))
1053                 ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr=htonl(0xFFFFFFFFUL);
1054
1055         rtnl_lock();
1056         if (r.arp_dev[0]) {
1057                 err = -ENODEV;
1058                 if ((dev = __dev_get_by_name(r.arp_dev)) == NULL)
1059                         goto out;
1060
1061                 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
1062                 if (!r.arp_ha.sa_family)
1063                         r.arp_ha.sa_family = dev->type;
1064                 err = -EINVAL;
1065                 if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
1066                         goto out;
1067         } else if (cmd == SIOCGARP) {
1068                 err = -ENODEV;
1069                 goto out;
1070         }
1071
1072         switch(cmd) {
1073         case SIOCDARP:
1074                 err = arp_req_delete(&r, dev);
1075                 break;
1076         case SIOCSARP:
1077                 err = arp_req_set(&r, dev);
1078                 break;
1079         case SIOCGARP:
1080                 err = arp_req_get(&r, dev);
1081                 if (!err && copy_to_user(arg, &r, sizeof(r)))
1082                         err = -EFAULT;
1083                 break;
1084         }
1085 out:
1086         rtnl_unlock();
1087         return err;
1088 }
1089
1090 /*
1091  *      Write the contents of the ARP cache to a PROCfs file.
1092  */
1093 #ifndef CONFIG_PROC_FS
1094 static int arp_get_info(char *buffer, char **start, off_t offset, int length) { return 0; }
1095 #else
1096 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1097 static char *ax2asc2(ax25_address *a, char *buf);
1098 #endif
1099 #define HBUFFERLEN 30
1100
1101 static int arp_get_info(char *buffer, char **start, off_t offset, int length)
1102 {
1103         int len=0;
1104         off_t pos=0;
1105         int size;
1106         char hbuffer[HBUFFERLEN];
1107         int i,j,k;
1108         const char hexbuf[] =  "0123456789ABCDEF";
1109
1110         size = sprintf(buffer,"IP address       HW type     Flags       HW address            Mask     Device\n");
1111
1112         pos+=size;
1113         len+=size;
1114
1115         for(i=0; i<=NEIGH_HASHMASK; i++) {
1116                 struct neighbour *n;
1117                 read_lock_bh(&arp_tbl.lock);
1118                 for (n=arp_tbl.hash_buckets[i]; n; n=n->next) {
1119                         struct net_device *dev = n->dev;
1120                         int hatype = dev->type;
1121
1122                         /* Do not confuse users "arp -a" with magic entries */
1123                         if (!(n->nud_state&~NUD_NOARP))
1124                                 continue;
1125
1126                         read_lock(&n->lock);
1127
1128 /*
1129  *      Convert hardware address to XX:XX:XX:XX ... form.
1130  */
1131 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1132                         if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
1133                                 ax2asc2((ax25_address *)n->ha, hbuffer);
1134                         else {
1135 #endif
1136                         for (k=0,j=0;k<HBUFFERLEN-3 && j<dev->addr_len;j++) {
1137                                 hbuffer[k++]=hexbuf[(n->ha[j]>>4)&15 ];
1138                                 hbuffer[k++]=hexbuf[n->ha[j]&15     ];
1139                                 hbuffer[k++]=':';
1140                         }
1141                         hbuffer[--k]=0;
1142
1143 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1144                 }
1145 #endif
1146
1147                         {
1148                                 char tbuf[16];
1149                                 sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(*(u32*)n->primary_key));
1150                                 size = sprintf(buffer+len, "%-16s 0x%-10x0x%-10x%s"
1151                                                         "     *        %s\n",
1152                                         tbuf,
1153                                         hatype,
1154                                         arp_state_to_flags(n), 
1155                                         hbuffer,
1156                                         dev->name);
1157                         }
1158
1159                         read_unlock(&n->lock);
1160
1161                         len += size;
1162                         pos += size;
1163                   
1164                         if (pos <= offset)
1165                                 len=0;
1166                         if (pos >= offset+length) {
1167                                 read_unlock_bh(&arp_tbl.lock);
1168                                 goto done;
1169                         }
1170                 }
1171                 read_unlock_bh(&arp_tbl.lock);
1172         }
1173
1174         for (i=0; i<=PNEIGH_HASHMASK; i++) {
1175                 struct pneigh_entry *n;
1176                 for (n=arp_tbl.phash_buckets[i]; n; n=n->next) {
1177                         struct net_device *dev = n->dev;
1178                         int hatype = dev ? dev->type : 0;
1179
1180                         {
1181                                 char tbuf[16];
1182                                 sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(*(u32*)n->key));
1183                                 size = sprintf(buffer+len, "%-16s 0x%-10x0x%-10x%s"
1184                                                         "     *        %s\n",
1185                                         tbuf,
1186                                         hatype,
1187                                         ATF_PUBL|ATF_PERM,
1188                                         "00:00:00:00:00:00",
1189                                         dev ? dev->name : "*");
1190                         }
1191
1192                         len += size;
1193                         pos += size;
1194                   
1195                         if (pos <= offset)
1196                                 len=0;
1197                         if (pos >= offset+length)
1198                                 goto done;
1199                 }
1200         }
1201
1202 done:
1203   
1204         *start = buffer+len-(pos-offset);       /* Start of wanted data */
1205         len = pos-offset;                       /* Start slop */
1206         if (len>length)
1207                 len = length;                   /* Ending slop */
1208         if (len<0)
1209                 len = 0;
1210         return len;
1211 }
1212 #endif
1213
1214 /* Note, that it is not on notifier chain.
1215    It is necessary, that this routine was called after route cache will be
1216    flushed.
1217  */
1218 void arp_ifdown(struct net_device *dev)
1219 {
1220         neigh_ifdown(&arp_tbl, dev);
1221 }
1222
1223
1224 /*
1225  *      Called once on startup.
1226  */
1227
1228 static struct packet_type arp_packet_type = {
1229         type:   __constant_htons(ETH_P_ARP),
1230         func:   arp_rcv,
1231         data:   (void*) 1, /* understand shared skbs */
1232 };
1233
1234 void __init arp_init (void)
1235 {
1236         neigh_table_init(&arp_tbl);
1237
1238         dev_add_pack(&arp_packet_type);
1239
1240         proc_net_create ("arp", 0, arp_get_info);
1241
1242 #ifdef CONFIG_SYSCTL
1243         neigh_sysctl_register(NULL, &arp_tbl.parms, NET_IPV4, NET_IPV4_NEIGH, "ipv4");
1244 #endif
1245 }
1246
1247
1248 #ifdef CONFIG_PROC_FS
1249 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1250
1251 /*
1252  *      ax25 -> ASCII conversion
1253  */
1254 char *ax2asc2(ax25_address *a, char *buf)
1255 {
1256         char c, *s;
1257         int n;
1258
1259         for (n = 0, s = buf; n < 6; n++) {
1260                 c = (a->ax25_call[n] >> 1) & 0x7F;
1261
1262                 if (c != ' ') *s++ = c;
1263         }
1264         
1265         *s++ = '-';
1266
1267         if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
1268                 *s++ = '1';
1269                 n -= 10;
1270         }
1271         
1272         *s++ = n + '0';
1273         *s++ = '\0';
1274
1275         if (*buf == '\0' || *buf == '-')
1276            return "*";
1277
1278         return buf;
1279
1280 }
1281
1282 #endif
1283 #endif