~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/arp.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp