Import 2.3.16
[davej-history.git] / net / ipv4 / arp.c
blob0b1ee63876c0cdd5c205b7ed5f7db108d840695c
1 /* linux/net/inet/arp.c
3 * Version: $Id: arp.c,v 1.81 1999/08/30 10:17:05 davem Exp $
5 * Copyright (C) 1994 by Florian La Roche
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).
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.
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.
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
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
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
122 #include <asm/system.h>
123 #include <asm/uaccess.h>
125 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
126 static char *ax2asc2(ax25_address *a, char *buf);
127 #endif
131 * Interface to generic neighbour cache.
133 static u32 arp_hash(const void *pkey, const struct net_device *dev);
134 static int arp_constructor(struct neighbour *neigh);
135 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
136 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
137 static void parp_redo(struct sk_buff *skb);
139 static struct neigh_ops arp_generic_ops =
141 AF_INET,
142 NULL,
143 arp_solicit,
144 arp_error_report,
145 neigh_resolve_output,
146 neigh_connected_output,
147 dev_queue_xmit,
148 dev_queue_xmit
151 static struct neigh_ops arp_hh_ops =
153 AF_INET,
154 NULL,
155 arp_solicit,
156 arp_error_report,
157 neigh_resolve_output,
158 neigh_resolve_output,
159 dev_queue_xmit,
160 dev_queue_xmit
163 static struct neigh_ops arp_direct_ops =
165 AF_INET,
166 NULL,
167 NULL,
168 NULL,
169 dev_queue_xmit,
170 dev_queue_xmit,
171 dev_queue_xmit,
172 dev_queue_xmit
175 struct neigh_ops arp_broken_ops =
177 AF_INET,
178 NULL,
179 arp_solicit,
180 arp_error_report,
181 neigh_compat_output,
182 neigh_compat_output,
183 dev_queue_xmit,
184 dev_queue_xmit,
187 struct neigh_table arp_tbl =
189 NULL,
190 AF_INET,
191 sizeof(struct neighbour) + 4,
193 arp_hash,
194 arp_constructor,
195 NULL,
196 NULL,
197 parp_redo,
198 "arp_cache",
199 { NULL, NULL, &arp_tbl, 0, NULL, NULL,
200 30*HZ, 1*HZ, 60*HZ, 30*HZ, 5*HZ, 3, 3, 0, 3, 1*HZ, (8*HZ)/10, 64, 1*HZ },
201 30*HZ, 128, 512, 1024,
204 int arp_mc_map(u32 addr, u8 *haddr, struct net_device *dev, int dir)
206 switch (dev->type) {
207 case ARPHRD_ETHER:
208 case ARPHRD_IEEE802:
209 case ARPHRD_FDDI:
210 ip_eth_mc_map(addr, haddr);
211 return 0;
212 default:
213 if (dir) {
214 memcpy(haddr, dev->broadcast, dev->addr_len);
215 return 0;
218 return -EINVAL;
222 static u32 arp_hash(const void *pkey, const struct net_device *dev)
224 u32 hash_val;
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;
232 return hash_val;
235 static int arp_constructor(struct neighbour *neigh)
237 u32 addr = *(u32*)neigh->primary_key;
238 struct net_device *dev = neigh->dev;
239 struct in_device *in_dev = in_dev_get(dev);
241 if (in_dev == NULL)
242 return -EINVAL;
244 neigh->type = inet_addr_type(addr);
245 if (in_dev->arp_parms)
246 neigh->parms = in_dev->arp_parms;
248 in_dev_put(in_dev);
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)
258 ARPHRD_ETHER: (ethernet, apfddi)
259 ARPHRD_FDDI: (fddi)
260 ARPHRD_IEEE802: (tr)
261 ARPHRD_METRICOM: (strip)
262 ARPHRD_ARCNET:
263 etc. etc. etc.
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.
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.
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.
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
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);
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;
316 return 0;
319 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
321 dst_link_failure(skb);
322 kfree_skb(skb);
325 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
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);
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);
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;
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);
356 /* OBSOLETE FUNCTIONS */
359 * Find an arp mapping in the cache. If not found, post a request.
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
367 static int arp_set_predefined(int addr_hint, unsigned char * haddr, u32 paddr, struct net_device * dev)
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;
381 return 0;
385 int arp_find(unsigned char *haddr, struct sk_buff *skb)
387 struct net_device *dev = skb->dev;
388 u32 paddr;
389 struct neighbour *n;
391 if (!skb->dst) {
392 printk(KERN_DEBUG "arp_find is called with dst==NULL\n");
393 kfree_skb(skb);
394 return 1;
397 paddr = ((struct rtable*)skb->dst)->rt_gateway;
399 if (arp_set_predefined(inet_addr_type(paddr), haddr, paddr, dev))
400 return 0;
402 n = __neigh_lookup(&arp_tbl, &paddr, dev, 1);
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;
413 neigh_release(n);
414 } else
415 kfree_skb(skb);
416 return 1;
419 /* END OF OBSOLETE FUNCTIONS */
421 int arp_bind_neighbour(struct dst_entry *dst)
423 struct net_device *dev = dst->dev;
425 if (dev == NULL)
426 return 0;
427 if (dst->neighbour == NULL) {
428 u32 nexthop = ((struct rtable*)dst)->rt_gateway;
429 if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT))
430 nexthop = 0;
431 dst->neighbour = __neigh_lookup(
432 #ifdef CONFIG_ATM_CLIP
433 dev->type == ARPHRD_ATM ? &clip_tbl :
434 #endif
435 &arp_tbl, &nexthop, dev, 1);
437 return (dst->neighbour != NULL);
441 * Interface to link layer: send routine and receive handler.
445 * Create and send an arp packet. If (dest_hw == NULL), we create a broadcast
446 * message.
449 void arp_send(int type, int ptype, u32 dest_ip,
450 struct net_device *dev, u32 src_ip,
451 unsigned char *dest_hw, unsigned char *src_hw,
452 unsigned char *target_hw)
454 struct sk_buff *skb;
455 struct arphdr *arp;
456 unsigned char *arp_ptr;
459 * No arp on this interface.
462 if (dev->flags&IFF_NOARP)
463 return;
466 * Allocate a buffer
469 skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
470 + dev->hard_header_len + 15, GFP_ATOMIC);
471 if (skb == NULL)
472 return;
474 skb_reserve(skb, (dev->hard_header_len+15)&~15);
475 skb->nh.raw = skb->data;
476 arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4));
477 skb->dev = dev;
478 skb->protocol = __constant_htons (ETH_P_ARP);
479 if (src_hw == NULL)
480 src_hw = dev->dev_addr;
481 if (dest_hw == NULL)
482 dest_hw = dev->broadcast;
485 * Fill the device header for the ARP frame
487 dev->hard_header(skb,dev,ptype,dest_hw,src_hw,skb->len);
490 * Fill out the arp protocol part.
492 * The arp hardware type should match the device type, except for FDDI,
493 * which (according to RFC 1390) should always equal 1 (Ethernet).
496 * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
497 * DIX code for the protocol. Make these device structure fields.
499 switch (dev->type) {
500 default:
501 arp->ar_hrd = htons(dev->type);
502 arp->ar_pro = __constant_htons(ETH_P_IP);
503 break;
505 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
506 case ARPHRD_AX25:
507 arp->ar_hrd = __constant_htons(ARPHRD_AX25);
508 arp->ar_pro = __constant_htons(AX25_P_IP);
509 break;
511 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
512 case ARPHRD_NETROM:
513 arp->ar_hrd = __constant_htons(ARPHRD_NETROM);
514 arp->ar_pro = __constant_htons(AX25_P_IP);
515 break;
516 #endif
517 #endif
519 #ifdef CONFIG_FDDI
520 case ARPHRD_FDDI:
521 arp->ar_hrd = __constant_htons(ARPHRD_ETHER);
522 arp->ar_pro = __constant_htons(ETH_P_IP);
523 break;
524 #endif
527 arp->ar_hln = dev->addr_len;
528 arp->ar_pln = 4;
529 arp->ar_op = htons(type);
531 arp_ptr=(unsigned char *)(arp+1);
533 memcpy(arp_ptr, src_hw, dev->addr_len);
534 arp_ptr+=dev->addr_len;
535 memcpy(arp_ptr, &src_ip,4);
536 arp_ptr+=4;
537 if (target_hw != NULL)
538 memcpy(arp_ptr, target_hw, dev->addr_len);
539 else
540 memset(arp_ptr, 0, dev->addr_len);
541 arp_ptr+=dev->addr_len;
542 memcpy(arp_ptr, &dest_ip, 4);
543 skb->dev = dev;
545 dev_queue_xmit(skb);
548 static void parp_redo(struct sk_buff *skb)
550 arp_rcv(skb, skb->dev, NULL);
554 * Receive an arp request by the device layer.
557 int arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
559 struct arphdr *arp = skb->nh.arph;
560 unsigned char *arp_ptr= (unsigned char *)(arp+1);
561 struct rtable *rt;
562 unsigned char *sha, *tha;
563 u32 sip, tip;
564 u16 dev_type = dev->type;
565 int addr_type;
566 struct in_device *in_dev = in_dev_get(dev);
567 struct neighbour *n;
570 * The hardware length of the packet should match the hardware length
571 * of the device. Similarly, the hardware types should match. The
572 * device should be ARP-able. Also, if pln is not 4, then the lookup
573 * is not from an IP number. We can't currently handle this, so toss
574 * it.
576 if (in_dev == NULL ||
577 arp->ar_hln != dev->addr_len ||
578 dev->flags & IFF_NOARP ||
579 skb->pkt_type == PACKET_OTHERHOST ||
580 skb->pkt_type == PACKET_LOOPBACK ||
581 arp->ar_pln != 4)
582 goto out;
584 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
585 goto out_of_mem;
587 switch (dev_type) {
588 default:
589 if (arp->ar_pro != __constant_htons(ETH_P_IP))
590 goto out;
591 if (htons(dev_type) != arp->ar_hrd)
592 goto out;
593 break;
594 #ifdef CONFIG_NET_ETHERNET
595 case ARPHRD_ETHER:
597 * ETHERNET devices will accept ARP hardware types of either
598 * 1 (Ethernet) or 6 (IEEE 802.2).
600 if (arp->ar_hrd != __constant_htons(ARPHRD_ETHER) &&
601 arp->ar_hrd != __constant_htons(ARPHRD_IEEE802))
602 goto out;
603 if (arp->ar_pro != __constant_htons(ETH_P_IP))
604 goto out;
605 break;
606 #endif
607 #ifdef CONFIG_FDDI
608 case ARPHRD_FDDI:
610 * According to RFC 1390, FDDI devices should accept ARP hardware types
611 * of 1 (Ethernet). However, to be more robust, we'll accept hardware
612 * types of either 1 (Ethernet) or 6 (IEEE 802.2).
614 if (arp->ar_hrd != __constant_htons(ARPHRD_ETHER) &&
615 arp->ar_hrd != __constant_htons(ARPHRD_IEEE802))
616 goto out;
617 if (arp->ar_pro != __constant_htons(ETH_P_IP))
618 goto out;
619 break;
620 #endif
621 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
622 case ARPHRD_AX25:
623 if (arp->ar_pro != __constant_htons(AX25_P_IP))
624 goto out;
625 if (arp->ar_hrd != __constant_htons(ARPHRD_AX25))
626 goto out;
627 break;
628 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
629 case ARPHRD_NETROM:
630 if (arp->ar_pro != __constant_htons(AX25_P_IP))
631 goto out;
632 if (arp->ar_hrd != __constant_htons(ARPHRD_NETROM))
633 goto out;
634 break;
635 #endif
636 #endif
639 /* Understand only these message types */
641 if (arp->ar_op != __constant_htons(ARPOP_REPLY) &&
642 arp->ar_op != __constant_htons(ARPOP_REQUEST))
643 goto out;
646 * Extract fields
648 sha=arp_ptr;
649 arp_ptr += dev->addr_len;
650 memcpy(&sip, arp_ptr, 4);
651 arp_ptr += 4;
652 tha=arp_ptr;
653 arp_ptr += dev->addr_len;
654 memcpy(&tip, arp_ptr, 4);
656 * Check for bad requests for 127.x.x.x and requests for multicast
657 * addresses. If this is one such, delete it.
659 if (LOOPBACK(tip) || MULTICAST(tip))
660 goto out;
663 * Process entry. The idea here is we want to send a reply if it is a
664 * request for us or if it is a request for someone else that we hold
665 * a proxy for. We want to add an entry to our cache if it is a reply
666 * to us or if it is a request for our address.
667 * (The assumption for this last is that if someone is requesting our
668 * address, they are probably intending to talk to us, so it saves time
669 * if we cache their address. Their address is also probably not in
670 * our cache, since ours is not in their cache.)
672 * Putting this another way, we only care about replies if they are to
673 * us, in which case we add them to the cache. For requests, we care
674 * about those for us and those for our proxies. We reply to both,
675 * and in the case of requests for us we add the requester to the arp
676 * cache.
679 /* Special case: IPv4 duplicate address detection packet (RFC2131) */
680 if (sip == 0) {
681 if (arp->ar_op == __constant_htons(ARPOP_REQUEST) &&
682 inet_addr_type(tip) == RTN_LOCAL)
683 arp_send(ARPOP_REPLY,ETH_P_ARP,tip,dev,tip,sha,dev->dev_addr,dev->dev_addr);
684 goto out;
687 if (arp->ar_op == __constant_htons(ARPOP_REQUEST) &&
688 ip_route_input(skb, tip, sip, 0, dev) == 0) {
690 rt = (struct rtable*)skb->dst;
691 addr_type = rt->rt_type;
693 if (addr_type == RTN_LOCAL) {
694 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
695 if (n) {
696 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
697 neigh_release(n);
699 goto out;
700 } else if (IN_DEV_FORWARD(in_dev)) {
701 if ((rt->rt_flags&RTCF_DNAT) ||
702 (addr_type == RTN_UNICAST && rt->u.dst.dev != dev &&
703 (IN_DEV_PROXY_ARP(in_dev) || pneigh_lookup(&arp_tbl, &tip, dev, 0)))) {
704 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
705 if (n)
706 neigh_release(n);
708 if (skb->stamp.tv_sec == 0 ||
709 skb->pkt_type == PACKET_HOST ||
710 in_dev->arp_parms->proxy_delay == 0) {
711 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
712 } else {
713 pneigh_enqueue(&arp_tbl, in_dev->arp_parms, skb);
714 in_dev_put(in_dev);
715 return 0;
717 goto out;
722 /* Update our ARP tables */
724 n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
726 #ifdef CONFIG_IP_ACCEPT_UNSOLICITED_ARP
727 /* Unsolicited ARP is not accepted by default.
728 It is possible, that this option should be enabled for some
729 devices (strip is candidate)
731 if (n == NULL &&
732 arp->ar_op == __constant_htons(ARPOP_REPLY) &&
733 inet_addr_type(sip) == RTN_UNICAST)
734 n = __neigh_lookup(&arp_tbl, &sip, dev, -1);
735 #endif
737 if (n) {
738 int state = NUD_REACHABLE;
739 int override = 0;
741 /* If several different ARP replies follows back-to-back,
742 use the FIRST one. It is possible, if several proxy
743 agents are active. Taking the first reply prevents
744 arp trashing and chooses the fastest router.
746 if (jiffies - n->updated >= n->parms->locktime)
747 override = 1;
749 /* Broadcast replies and request packets
750 do not assert neighbour reachability.
752 if (arp->ar_op != __constant_htons(ARPOP_REPLY) ||
753 skb->pkt_type != PACKET_HOST)
754 state = NUD_STALE;
755 neigh_update(n, sha, state, override, 1);
756 neigh_release(n);
759 out:
760 kfree_skb(skb);
761 if (in_dev)
762 in_dev_put(in_dev);
763 out_of_mem:
764 return 0;
770 * User level interface (ioctl, /proc)
774 * Set (create) an ARP cache entry.
777 int arp_req_set(struct arpreq *r, struct net_device * dev)
779 u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
780 struct neighbour *neigh;
781 int err;
783 if (r->arp_flags&ATF_PUBL) {
784 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
785 if (mask && mask != 0xFFFFFFFF)
786 return -EINVAL;
787 if (!dev && (r->arp_flags & ATF_COM)) {
788 dev = dev_getbyhwaddr(r->arp_ha.sa_family, r->arp_ha.sa_data);
789 if (!dev)
790 return -ENODEV;
792 if (mask) {
793 if (pneigh_lookup(&arp_tbl, &ip, dev, 1) == NULL)
794 return -ENOBUFS;
795 return 0;
797 if (dev == NULL) {
798 ipv4_devconf.proxy_arp = 1;
799 return 0;
801 if (__in_dev_get(dev)) {
802 __in_dev_get(dev)->cnf.proxy_arp = 1;
803 return 0;
805 return -ENXIO;
808 if (r->arp_flags & ATF_PERM)
809 r->arp_flags |= ATF_COM;
810 if (dev == NULL) {
811 struct rtable * rt;
812 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
813 return err;
814 dev = rt->u.dst.dev;
815 ip_rt_put(rt);
816 if (!dev)
817 return -EINVAL;
819 if (r->arp_ha.sa_family != dev->type)
820 return -EINVAL;
822 err = -ENOBUFS;
823 neigh = __neigh_lookup(&arp_tbl, &ip, dev, 1);
824 if (neigh) {
825 unsigned state = NUD_STALE;
826 if (r->arp_flags & ATF_PERM)
827 state = NUD_PERMANENT;
828 err = neigh_update(neigh, (r->arp_flags&ATF_COM) ?
829 r->arp_ha.sa_data : NULL, state, 1, 0);
830 neigh_release(neigh);
832 return err;
835 static unsigned arp_state_to_flags(struct neighbour *neigh)
837 unsigned flags = 0;
838 if (neigh->nud_state&NUD_PERMANENT)
839 flags = ATF_PERM|ATF_COM;
840 else if (neigh->nud_state&NUD_VALID)
841 flags = ATF_COM;
842 return flags;
846 * Get an ARP cache entry.
849 static int arp_req_get(struct arpreq *r, struct net_device *dev)
851 u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
852 struct neighbour *neigh;
853 int err = -ENXIO;
855 neigh = neigh_lookup(&arp_tbl, &ip, dev);
856 if (neigh) {
857 read_lock_bh(&neigh->lock);
858 memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
859 r->arp_flags = arp_state_to_flags(neigh);
860 read_unlock_bh(&neigh->lock);
861 r->arp_ha.sa_family = dev->type;
862 strncpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
863 neigh_release(neigh);
864 err = 0;
866 return err;
869 int arp_req_delete(struct arpreq *r, struct net_device * dev)
871 int err;
872 u32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
873 struct neighbour *neigh;
875 if (r->arp_flags & ATF_PUBL) {
876 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
877 if (mask == 0xFFFFFFFF)
878 return pneigh_delete(&arp_tbl, &ip, dev);
879 if (mask == 0) {
880 if (dev == NULL) {
881 ipv4_devconf.proxy_arp = 0;
882 return 0;
884 if (__in_dev_get(dev)) {
885 __in_dev_get(dev)->cnf.proxy_arp = 0;
886 return 0;
888 return -ENXIO;
890 return -EINVAL;
893 if (dev == NULL) {
894 struct rtable * rt;
895 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
896 return err;
897 dev = rt->u.dst.dev;
898 ip_rt_put(rt);
899 if (!dev)
900 return -EINVAL;
902 err = -ENXIO;
903 neigh = neigh_lookup(&arp_tbl, &ip, dev);
904 if (neigh) {
905 if (neigh->nud_state&~NUD_NOARP)
906 err = neigh_update(neigh, NULL, NUD_FAILED, 1, 0);
907 neigh_release(neigh);
909 return err;
913 * Handle an ARP layer I/O control request.
916 int arp_ioctl(unsigned int cmd, void *arg)
918 int err;
919 struct arpreq r;
920 struct net_device * dev = NULL;
922 switch(cmd) {
923 case SIOCDARP:
924 case SIOCSARP:
925 if (!capable(CAP_NET_ADMIN))
926 return -EPERM;
927 case SIOCGARP:
928 err = copy_from_user(&r, arg, sizeof(struct arpreq));
929 if (err)
930 return -EFAULT;
931 break;
932 default:
933 return -EINVAL;
936 if (r.arp_pa.sa_family != AF_INET)
937 return -EPFNOSUPPORT;
939 if (!(r.arp_flags & ATF_PUBL) &&
940 (r.arp_flags & (ATF_NETMASK|ATF_DONTPUB)))
941 return -EINVAL;
942 if (!(r.arp_flags & ATF_NETMASK))
943 ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr=__constant_htonl(0xFFFFFFFFUL);
945 rtnl_lock();
946 if (r.arp_dev[0]) {
947 err = -ENODEV;
948 if ((dev = __dev_get_by_name(r.arp_dev)) == NULL)
949 goto out;
951 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
952 if (!r.arp_ha.sa_family)
953 r.arp_ha.sa_family = dev->type;
954 err = -EINVAL;
955 if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
956 goto out;
957 } else if (cmd == SIOCGARP) {
958 err = -ENODEV;
959 goto out;
962 switch(cmd) {
963 case SIOCDARP:
964 err = arp_req_delete(&r, dev);
965 break;
966 case SIOCSARP:
967 err = arp_req_set(&r, dev);
968 break;
969 case SIOCGARP:
970 err = arp_req_get(&r, dev);
971 if (!err && copy_to_user(arg, &r, sizeof(r)))
972 err = -EFAULT;
973 break;
975 out:
976 rtnl_unlock();
977 return err;
981 * Write the contents of the ARP cache to a PROCfs file.
983 #ifdef CONFIG_PROC_FS
985 #define HBUFFERLEN 30
987 int arp_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
989 int len=0;
990 off_t pos=0;
991 int size;
992 char hbuffer[HBUFFERLEN];
993 int i,j,k;
994 const char hexbuf[] = "0123456789ABCDEF";
995 char abuf[16];
997 size = sprintf(buffer,"IP address HW type Flags HW address Mask Device\n");
999 pos+=size;
1000 len+=size;
1002 for(i=0; i<=NEIGH_HASHMASK; i++) {
1003 struct neighbour *n;
1004 read_lock_bh(&arp_tbl.lock);
1005 for (n=arp_tbl.hash_buckets[i]; n; n=n->next) {
1006 struct net_device *dev = n->dev;
1007 int hatype = dev->type;
1009 /* Do not confuse users "arp -a" with magic entries */
1010 if (!(n->nud_state&~NUD_NOARP))
1011 continue;
1013 read_lock(&n->lock);
1016 * Convert hardware address to XX:XX:XX:XX ... form.
1018 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1019 if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
1020 ax2asc2((ax25_address *)n->ha, hbuffer);
1021 else {
1022 #endif
1023 for (k=0,j=0;k<HBUFFERLEN-3 && j<dev->addr_len;j++) {
1024 hbuffer[k++]=hexbuf[(n->ha[j]>>4)&15 ];
1025 hbuffer[k++]=hexbuf[n->ha[j]&15 ];
1026 hbuffer[k++]=':';
1028 hbuffer[--k]=0;
1030 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1032 #endif
1034 size = sprintf(buffer+len,
1035 "%-17s0x%-10x0x%-10x%s",
1036 in_ntoa2(*(u32*)n->primary_key, abuf),
1037 hatype,
1038 arp_state_to_flags(n),
1039 hbuffer);
1040 size += sprintf(buffer+len+size,
1041 " %-17s %s\n",
1042 "*", dev->name);
1043 read_unlock(&n->lock);
1045 len += size;
1046 pos += size;
1048 if (pos <= offset)
1049 len=0;
1050 if (pos >= offset+length) {
1051 read_unlock_bh(&arp_tbl.lock);
1052 goto done;
1055 read_unlock_bh(&arp_tbl.lock);
1058 for (i=0; i<=PNEIGH_HASHMASK; i++) {
1059 struct pneigh_entry *n;
1060 for (n=arp_tbl.phash_buckets[i]; n; n=n->next) {
1061 struct net_device *dev = n->dev;
1062 int hatype = dev ? dev->type : 0;
1064 size = sprintf(buffer+len,
1065 "%-17s0x%-10x0x%-10x%s",
1066 in_ntoa2(*(u32*)n->key, abuf),
1067 hatype,
1068 ATF_PUBL|ATF_PERM,
1069 "00:00:00:00:00:00");
1070 size += sprintf(buffer+len+size,
1071 " %-17s %s\n",
1072 "*", dev ? dev->name : "*");
1074 len += size;
1075 pos += size;
1077 if (pos <= offset)
1078 len=0;
1079 if (pos >= offset+length)
1080 goto done;
1084 done:
1086 *start = buffer+len-(pos-offset); /* Start of wanted data */
1087 len = pos-offset; /* Start slop */
1088 if (len>length)
1089 len = length; /* Ending slop */
1090 if (len<0)
1091 len = 0;
1092 return len;
1094 #endif
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.
1100 void arp_ifdown(struct net_device *dev)
1102 neigh_ifdown(&arp_tbl, dev);
1107 * Called once on startup.
1110 static struct packet_type arp_packet_type =
1112 __constant_htons(ETH_P_ARP),
1113 NULL, /* All devices */
1114 arp_rcv,
1115 (void*)1,
1116 NULL
1119 #ifdef CONFIG_PROC_FS
1120 static struct proc_dir_entry proc_net_arp = {
1121 PROC_NET_ARP, 3, "arp",
1122 S_IFREG | S_IRUGO, 1, 0, 0,
1123 0, &proc_net_inode_operations,
1124 arp_get_info
1126 #endif
1128 void __init arp_init (void)
1130 neigh_table_init(&arp_tbl);
1132 dev_add_pack(&arp_packet_type);
1134 #ifdef CONFIG_PROC_FS
1135 proc_net_register(&proc_net_arp);
1136 #endif
1137 #ifdef CONFIG_SYSCTL
1138 neigh_sysctl_register(NULL, &arp_tbl.parms, NET_IPV4, NET_IPV4_NEIGH, "ipv4");
1139 #endif
1143 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1146 * ax25 -> ASCII conversion
1148 char *ax2asc2(ax25_address *a, char *buf)
1150 char c, *s;
1151 int n;
1153 for (n = 0, s = buf; n < 6; n++) {
1154 c = (a->ax25_call[n] >> 1) & 0x7F;
1156 if (c != ' ') *s++ = c;
1159 *s++ = '-';
1161 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
1162 *s++ = '1';
1163 n -= 10;
1166 *s++ = n + '0';
1167 *s++ = '\0';
1169 if (*buf == '\0' || *buf == '-')
1170 return "*";
1172 return buf;
1176 #endif