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

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

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.