pre-2.3.4..
[davej-history.git] / net / ipv4 / arp.c
blob2c311f2334d7ccd92ece8ae4831dbfcb5676e9ae
1 /* linux/net/inet/arp.c
3 * Version: $Id: arp.c,v 1.77 1999/03/21 05:22:30 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
119 #include <asm/system.h>
120 #include <asm/uaccess.h>
123 * Interface to generic neighbour cache.
125 static int arp_constructor(struct neighbour *neigh);
126 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
127 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
128 static void parp_redo(struct sk_buff *skb);
130 static struct neigh_ops arp_generic_ops =
132 AF_INET,
133 NULL,
134 arp_solicit,
135 arp_error_report,
136 neigh_resolve_output,
137 neigh_connected_output,
138 dev_queue_xmit,
139 dev_queue_xmit
142 static struct neigh_ops arp_hh_ops =
144 AF_INET,
145 NULL,
146 arp_solicit,
147 arp_error_report,
148 neigh_resolve_output,
149 neigh_resolve_output,
150 dev_queue_xmit,
151 dev_queue_xmit
154 static struct neigh_ops arp_direct_ops =
156 AF_INET,
157 NULL,
158 NULL,
159 NULL,
160 dev_queue_xmit,
161 dev_queue_xmit,
162 dev_queue_xmit,
163 dev_queue_xmit
166 struct neigh_ops arp_broken_ops =
168 AF_INET,
169 NULL,
170 arp_solicit,
171 arp_error_report,
172 neigh_compat_output,
173 neigh_compat_output,
174 dev_queue_xmit,
175 dev_queue_xmit,
178 struct neigh_table arp_tbl =
180 NULL,
181 AF_INET,
182 sizeof(struct neighbour) + 4,
184 arp_constructor,
185 NULL,
186 NULL,
187 parp_redo,
188 { NULL, NULL, &arp_tbl, 0, NULL, NULL,
189 30*HZ, 1*HZ, 60*HZ, 30*HZ, 5*HZ, 3, 3, 0, 3, 1*HZ, (8*HZ)/10, 64, 1*HZ },
190 30*HZ, 128, 512, 1024,
193 int arp_mc_map(u32 addr, u8 *haddr, struct device *dev, int dir)
195 switch (dev->type) {
196 case ARPHRD_ETHER:
197 case ARPHRD_IEEE802:
198 case ARPHRD_FDDI:
199 ip_eth_mc_map(addr, haddr);
200 return 0;
201 default:
202 if (dir) {
203 memcpy(haddr, dev->broadcast, dev->addr_len);
204 return 0;
207 return -EINVAL;
212 static int arp_constructor(struct neighbour *neigh)
214 u32 addr = *(u32*)neigh->primary_key;
215 struct device *dev = neigh->dev;
216 struct in_device *in_dev = dev->ip_ptr;
218 if (in_dev == NULL)
219 return -EINVAL;
221 neigh->type = inet_addr_type(addr);
222 if (in_dev->arp_parms)
223 neigh->parms = in_dev->arp_parms;
225 if (dev->hard_header == NULL) {
226 neigh->nud_state = NUD_NOARP;
227 neigh->ops = &arp_direct_ops;
228 neigh->output = neigh->ops->queue_xmit;
229 } else {
230 /* Good devices (checked by reading texts, but only Ethernet is
231 tested)
233 ARPHRD_ETHER: (ethernet, apfddi)
234 ARPHRD_FDDI: (fddi)
235 ARPHRD_IEEE802: (tr)
236 ARPHRD_METRICOM: (strip)
237 ARPHRD_ARCNET:
238 etc. etc. etc.
240 ARPHRD_IPDDP will also work, if author repairs it.
241 I did not it, because this driver does not work even
242 in old paradigm.
245 #if 1
246 /* So... these "amateur" devices are hopeless.
247 The only thing, that I can say now:
248 It is very sad that we need to keep ugly obsolete
249 code to make them happy.
251 They should be moved to more reasonable state, now
252 they use rebuild_header INSTEAD OF hard_start_xmit!!!
253 Besides that, they are sort of out of date
254 (a lot of redundant clones/copies, useless in 2.1),
255 I wonder why people believe that they work.
257 switch (dev->type) {
258 default:
259 break;
260 case ARPHRD_ROSE:
261 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
262 case ARPHRD_AX25:
263 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
264 case ARPHRD_NETROM:
265 #endif
266 neigh->ops = &arp_broken_ops;
267 neigh->output = neigh->ops->output;
268 return 0;
269 #endif
271 #endif
272 if (neigh->type == RTN_MULTICAST) {
273 neigh->nud_state = NUD_NOARP;
274 arp_mc_map(addr, neigh->ha, dev, 1);
275 } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
276 neigh->nud_state = NUD_NOARP;
277 memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
278 } else if (neigh->type == RTN_BROADCAST || dev->flags&IFF_POINTOPOINT) {
279 neigh->nud_state = NUD_NOARP;
280 memcpy(neigh->ha, dev->broadcast, dev->addr_len);
282 if (dev->hard_header_cache)
283 neigh->ops = &arp_hh_ops;
284 else
285 neigh->ops = &arp_generic_ops;
286 if (neigh->nud_state&NUD_VALID)
287 neigh->output = neigh->ops->connected_output;
288 else
289 neigh->output = neigh->ops->output;
292 return 0;
295 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
297 dst_link_failure(skb);
298 kfree_skb(skb);
301 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
303 u32 saddr;
304 u8 *dst_ha = NULL;
305 struct device *dev = neigh->dev;
306 u32 target = *(u32*)neigh->primary_key;
307 int probes = neigh->probes;
309 if (skb && inet_addr_type(skb->nh.iph->saddr) == RTN_LOCAL)
310 saddr = skb->nh.iph->saddr;
311 else
312 saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
314 if ((probes -= neigh->parms->ucast_probes) < 0) {
315 if (!(neigh->nud_state&NUD_VALID))
316 printk(KERN_DEBUG "trying to ucast probe in NUD_INVALID\n");
317 dst_ha = neigh->ha;
318 } else if ((probes -= neigh->parms->app_probes) < 0) {
319 #ifdef CONFIG_ARPD
320 neigh_app_ns(neigh);
321 #endif
322 return;
325 arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
326 dst_ha, dev->dev_addr, NULL);
329 /* OBSOLETE FUNCTIONS */
332 * Find an arp mapping in the cache. If not found, post a request.
334 * It is very UGLY routine: it DOES NOT use skb->dst->neighbour,
335 * even if it exists. It is supposed that skb->dev was mangled
336 * by a virtual device (eql, shaper). Nobody but broken devices
337 * is allowed to use this function, it is scheduled to be removed. --ANK
340 static int arp_set_predefined(int addr_hint, unsigned char * haddr, u32 paddr, struct device * dev)
342 switch (addr_hint) {
343 case RTN_LOCAL:
344 printk(KERN_DEBUG "ARP: arp called for own IP address\n");
345 memcpy(haddr, dev->dev_addr, dev->addr_len);
346 return 1;
347 case RTN_MULTICAST:
348 arp_mc_map(paddr, haddr, dev, 1);
349 return 1;
350 case RTN_BROADCAST:
351 memcpy(haddr, dev->broadcast, dev->addr_len);
352 return 1;
354 return 0;
358 int arp_find(unsigned char *haddr, struct sk_buff *skb)
360 struct device *dev = skb->dev;
361 u32 paddr;
362 struct neighbour *n;
364 if (!skb->dst) {
365 printk(KERN_DEBUG "arp_find is called with dst==NULL\n");
366 kfree_skb(skb);
367 return 1;
370 paddr = ((struct rtable*)skb->dst)->rt_gateway;
372 if (arp_set_predefined(inet_addr_type(paddr), haddr, paddr, dev))
373 return 0;
375 start_bh_atomic();
376 n = __neigh_lookup(&arp_tbl, &paddr, dev, 1);
378 if (n) {
379 n->used = jiffies;
380 if (n->nud_state&NUD_VALID || neigh_event_send(n, skb) == 0) {
381 memcpy(haddr, n->ha, dev->addr_len);
382 neigh_release(n);
383 end_bh_atomic();
384 return 0;
386 } else
387 kfree_skb(skb);
388 neigh_release(n);
389 end_bh_atomic();
390 return 1;
393 /* END OF OBSOLETE FUNCTIONS */
396 * Note: requires bh_atomic locking.
398 int arp_bind_neighbour(struct dst_entry *dst)
400 struct device *dev = dst->dev;
402 if (dev == NULL)
403 return 0;
404 if (dst->neighbour == NULL) {
405 u32 nexthop = ((struct rtable*)dst)->rt_gateway;
406 if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT))
407 nexthop = 0;
408 dst->neighbour = __neigh_lookup(&arp_tbl, &nexthop, dev, 1);
410 return (dst->neighbour != NULL);
414 * Interface to link layer: send routine and receive handler.
418 * Create and send an arp packet. If (dest_hw == NULL), we create a broadcast
419 * message.
422 void arp_send(int type, int ptype, u32 dest_ip,
423 struct device *dev, u32 src_ip,
424 unsigned char *dest_hw, unsigned char *src_hw,
425 unsigned char *target_hw)
427 struct sk_buff *skb;
428 struct arphdr *arp;
429 unsigned char *arp_ptr;
432 * No arp on this interface.
435 if (dev->flags&IFF_NOARP)
436 return;
439 * Allocate a buffer
442 skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
443 + dev->hard_header_len + 15, GFP_ATOMIC);
444 if (skb == NULL)
445 return;
447 skb_reserve(skb, (dev->hard_header_len+15)&~15);
448 skb->nh.raw = skb->data;
449 arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4));
450 skb->dev = dev;
451 skb->protocol = __constant_htons (ETH_P_ARP);
452 if (src_hw == NULL)
453 src_hw = dev->dev_addr;
454 if (dest_hw == NULL)
455 dest_hw = dev->broadcast;
458 * Fill the device header for the ARP frame
460 dev->hard_header(skb,dev,ptype,dest_hw,src_hw,skb->len);
463 * Fill out the arp protocol part.
465 * The arp hardware type should match the device type, except for FDDI,
466 * which (according to RFC 1390) should always equal 1 (Ethernet).
469 * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
470 * DIX code for the protocol. Make these device structure fields.
472 switch (dev->type) {
473 default:
474 arp->ar_hrd = htons(dev->type);
475 arp->ar_pro = __constant_htons(ETH_P_IP);
476 break;
478 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
479 case ARPHRD_AX25:
480 arp->ar_hrd = __constant_htons(ARPHRD_AX25);
481 arp->ar_pro = __constant_htons(AX25_P_IP);
482 break;
484 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
485 case ARPHRD_NETROM:
486 arp->ar_hrd = __constant_htons(ARPHRD_NETROM);
487 arp->ar_pro = __constant_htons(AX25_P_IP);
488 break;
489 #endif
490 #endif
492 #ifdef CONFIG_FDDI
493 case ARPHRD_FDDI:
494 arp->ar_hrd = __constant_htons(ARPHRD_ETHER);
495 arp->ar_pro = __constant_htons(ETH_P_IP);
496 break;
497 #endif
500 arp->ar_hln = dev->addr_len;
501 arp->ar_pln = 4;
502 arp->ar_op = htons(type);
504 arp_ptr=(unsigned char *)(arp+1);
506 memcpy(arp_ptr, src_hw, dev->addr_len);
507 arp_ptr+=dev->addr_len;
508 memcpy(arp_ptr, &src_ip,4);
509 arp_ptr+=4;
510 if (target_hw != NULL)
511 memcpy(arp_ptr, target_hw, dev->addr_len);
512 else
513 memset(arp_ptr, 0, dev->addr_len);
514 arp_ptr+=dev->addr_len;
515 memcpy(arp_ptr, &dest_ip, 4);
516 skb->dev = dev;
518 dev_queue_xmit(skb);
521 static void parp_redo(struct sk_buff *skb)
523 arp_rcv(skb, skb->dev, NULL);
527 * Receive an arp request by the device layer.
530 int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
532 struct arphdr *arp = skb->nh.arph;
533 unsigned char *arp_ptr= (unsigned char *)(arp+1);
534 struct rtable *rt;
535 unsigned char *sha, *tha;
536 u32 sip, tip;
537 u16 dev_type = dev->type;
538 int addr_type;
539 struct in_device *in_dev = dev->ip_ptr;
540 struct neighbour *n;
543 * The hardware length of the packet should match the hardware length
544 * of the device. Similarly, the hardware types should match. The
545 * device should be ARP-able. Also, if pln is not 4, then the lookup
546 * is not from an IP number. We can't currently handle this, so toss
547 * it.
549 if (in_dev == NULL ||
550 arp->ar_hln != dev->addr_len ||
551 dev->flags & IFF_NOARP ||
552 skb->pkt_type == PACKET_OTHERHOST ||
553 skb->pkt_type == PACKET_LOOPBACK ||
554 arp->ar_pln != 4)
555 goto out;
557 switch (dev_type) {
558 default:
559 if (arp->ar_pro != __constant_htons(ETH_P_IP))
560 goto out;
561 if (htons(dev_type) != arp->ar_hrd)
562 goto out;
563 break;
564 #ifdef CONFIG_NET_ETHERNET
565 case ARPHRD_ETHER:
567 * ETHERNET devices will accept ARP hardware types of either
568 * 1 (Ethernet) or 6 (IEEE 802.2).
570 if (arp->ar_hrd != __constant_htons(ARPHRD_ETHER) &&
571 arp->ar_hrd != __constant_htons(ARPHRD_IEEE802))
572 goto out;
573 if (arp->ar_pro != __constant_htons(ETH_P_IP))
574 goto out;
575 break;
576 #endif
577 #ifdef CONFIG_FDDI
578 case ARPHRD_FDDI:
580 * According to RFC 1390, FDDI devices should accept ARP hardware types
581 * of 1 (Ethernet). However, to be more robust, we'll accept hardware
582 * types of either 1 (Ethernet) or 6 (IEEE 802.2).
584 if (arp->ar_hrd != __constant_htons(ARPHRD_ETHER) &&
585 arp->ar_hrd != __constant_htons(ARPHRD_IEEE802))
586 goto out;
587 if (arp->ar_pro != __constant_htons(ETH_P_IP))
588 goto out;
589 break;
590 #endif
591 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
592 case ARPHRD_AX25:
593 if (arp->ar_pro != __constant_htons(AX25_P_IP))
594 goto out;
595 if (arp->ar_hrd != __constant_htons(ARPHRD_AX25))
596 goto out;
597 break;
598 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
599 case ARPHRD_NETROM:
600 if (arp->ar_pro != __constant_htons(AX25_P_IP))
601 goto out;
602 if (arp->ar_hrd != __constant_htons(ARPHRD_NETROM))
603 goto out;
604 break;
605 #endif
606 #endif
609 /* Undertsand only these message types */
611 if (arp->ar_op != __constant_htons(ARPOP_REPLY) &&
612 arp->ar_op != __constant_htons(ARPOP_REQUEST))
613 goto out;
616 * Extract fields
618 sha=arp_ptr;
619 arp_ptr += dev->addr_len;
620 memcpy(&sip, arp_ptr, 4);
621 arp_ptr += 4;
622 tha=arp_ptr;
623 arp_ptr += dev->addr_len;
624 memcpy(&tip, arp_ptr, 4);
626 * Check for bad requests for 127.x.x.x and requests for multicast
627 * addresses. If this is one such, delete it.
629 if (LOOPBACK(tip) || MULTICAST(tip))
630 goto out;
633 * Process entry. The idea here is we want to send a reply if it is a
634 * request for us or if it is a request for someone else that we hold
635 * a proxy for. We want to add an entry to our cache if it is a reply
636 * to us or if it is a request for our address.
637 * (The assumption for this last is that if someone is requesting our
638 * address, they are probably intending to talk to us, so it saves time
639 * if we cache their address. Their address is also probably not in
640 * our cache, since ours is not in their cache.)
642 * Putting this another way, we only care about replies if they are to
643 * us, in which case we add them to the cache. For requests, we care
644 * about those for us and those for our proxies. We reply to both,
645 * and in the case of requests for us we add the requester to the arp
646 * cache.
649 /* Special case: IPv4 duplicate address detection packet (RFC2131) */
650 if (sip == 0) {
651 if (arp->ar_op == __constant_htons(ARPOP_REQUEST) &&
652 inet_addr_type(tip) == RTN_LOCAL)
653 arp_send(ARPOP_REPLY,ETH_P_ARP,tip,dev,tip,sha,dev->dev_addr,dev->dev_addr);
654 goto out;
657 if (arp->ar_op == __constant_htons(ARPOP_REQUEST) &&
658 ip_route_input(skb, tip, sip, 0, dev) == 0) {
660 rt = (struct rtable*)skb->dst;
661 addr_type = rt->rt_type;
663 if (addr_type == RTN_LOCAL) {
664 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
665 if (n) {
666 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
667 neigh_release(n);
669 goto out;
670 } else if (IN_DEV_FORWARD(in_dev)) {
671 if ((rt->rt_flags&RTCF_DNAT) ||
672 (addr_type == RTN_UNICAST && rt->u.dst.dev != dev &&
673 (IN_DEV_PROXY_ARP(in_dev) || pneigh_lookup(&arp_tbl, &tip, dev, 0)))) {
674 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
675 neigh_release(n);
677 if (skb->stamp.tv_sec == 0 ||
678 skb->pkt_type == PACKET_HOST ||
679 in_dev->arp_parms->proxy_delay == 0) {
680 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
681 } else {
682 pneigh_enqueue(&arp_tbl, in_dev->arp_parms, skb);
683 return 0;
685 goto out;
690 /* Update our ARP tables */
692 n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
694 #ifdef CONFIG_IP_ACCEPT_UNSOLICITED_ARP
695 /* Unsolicited ARP is not accepted by default.
696 It is possible, that this option should be enabled for some
697 devices (strip is candidate)
699 if (n == NULL &&
700 arp->ar_op == __constant_htons(ARPOP_REPLY) &&
701 inet_addr_type(sip) == RTN_UNICAST)
702 n = __neigh_lookup(&arp_tbl, &sip, dev, -1);
703 #endif
705 if (n) {
706 int state = NUD_REACHABLE;
707 int override = 0;
709 /* If several different ARP replies follows back-to-back,
710 use the FIRST one. It is possible, if several proxy
711 agents are active. Taking the first reply prevents
712 arp trashing and chooses the fastest router.
714 if (jiffies - n->updated >= n->parms->locktime)
715 override = 1;
717 /* Broadcast replies and request packets
718 do not assert neighbour reachability.
720 if (arp->ar_op != __constant_htons(ARPOP_REPLY) ||
721 skb->pkt_type != PACKET_HOST)
722 state = NUD_STALE;
723 neigh_update(n, sha, state, override, 1);
724 neigh_release(n);
727 out:
728 kfree_skb(skb);
729 return 0;
735 * User level interface (ioctl, /proc)
739 * Set (create) an ARP cache entry.
742 int arp_req_set(struct arpreq *r, struct device * dev)
744 u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
745 struct neighbour *neigh;
746 int err;
748 if (r->arp_flags&ATF_PUBL) {
749 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
750 if (mask && mask != 0xFFFFFFFF)
751 return -EINVAL;
752 if (!dev && (r->arp_flags & ATF_COM)) {
753 dev = dev_getbyhwaddr(r->arp_ha.sa_family, r->arp_ha.sa_data);
754 if (!dev)
755 return -ENODEV;
757 if (mask) {
758 if (pneigh_lookup(&arp_tbl, &ip, dev, 1) == NULL)
759 return -ENOBUFS;
760 return 0;
762 if (dev == NULL) {
763 ipv4_devconf.proxy_arp = 1;
764 return 0;
766 if (dev->ip_ptr) {
767 ((struct in_device*)dev->ip_ptr)->cnf.proxy_arp = 1;
768 return 0;
770 return -ENXIO;
773 if (r->arp_flags & ATF_PERM)
774 r->arp_flags |= ATF_COM;
775 if (dev == NULL) {
776 struct rtable * rt;
777 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
778 return err;
779 dev = rt->u.dst.dev;
780 ip_rt_put(rt);
781 if (!dev)
782 return -EINVAL;
784 if (r->arp_ha.sa_family != dev->type)
785 return -EINVAL;
787 err = -ENOBUFS;
788 start_bh_atomic();
789 neigh = __neigh_lookup(&arp_tbl, &ip, dev, 1);
790 if (neigh) {
791 unsigned state = NUD_STALE;
792 if (r->arp_flags & ATF_PERM)
793 state = NUD_PERMANENT;
794 err = neigh_update(neigh, (r->arp_flags&ATF_COM) ?
795 r->arp_ha.sa_data : NULL, state, 1, 0);
796 neigh_release(neigh);
798 end_bh_atomic();
799 return err;
802 static unsigned arp_state_to_flags(struct neighbour *neigh)
804 unsigned flags = 0;
805 if (neigh->nud_state&NUD_PERMANENT)
806 flags = ATF_PERM|ATF_COM;
807 else if (neigh->nud_state&NUD_VALID)
808 flags = ATF_COM;
809 return flags;
813 * Get an ARP cache entry.
816 static int arp_req_get(struct arpreq *r, struct device *dev)
818 u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
819 struct neighbour *neigh;
820 int err = -ENXIO;
822 start_bh_atomic();
823 neigh = __neigh_lookup(&arp_tbl, &ip, dev, 0);
824 if (neigh) {
825 memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
826 r->arp_ha.sa_family = dev->type;
827 strncpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
828 r->arp_flags = arp_state_to_flags(neigh);
829 neigh_release(neigh);
830 err = 0;
832 end_bh_atomic();
833 return err;
836 int arp_req_delete(struct arpreq *r, struct device * dev)
838 int err;
839 u32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
840 struct neighbour *neigh;
842 if (r->arp_flags & ATF_PUBL) {
843 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
844 if (mask == 0xFFFFFFFF)
845 return pneigh_delete(&arp_tbl, &ip, dev);
846 if (mask == 0) {
847 if (dev == NULL) {
848 ipv4_devconf.proxy_arp = 0;
849 return 0;
851 if (dev->ip_ptr) {
852 ((struct in_device*)dev->ip_ptr)->cnf.proxy_arp = 0;
853 return 0;
855 return -ENXIO;
857 return -EINVAL;
860 if (dev == NULL) {
861 struct rtable * rt;
862 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
863 return err;
864 dev = rt->u.dst.dev;
865 ip_rt_put(rt);
866 if (!dev)
867 return -EINVAL;
869 err = -ENXIO;
870 start_bh_atomic();
871 neigh = __neigh_lookup(&arp_tbl, &ip, dev, 0);
872 if (neigh) {
873 if (neigh->nud_state&~NUD_NOARP)
874 err = neigh_update(neigh, NULL, NUD_FAILED, 1, 0);
875 neigh_release(neigh);
877 end_bh_atomic();
878 return err;
882 * Handle an ARP layer I/O control request.
885 int arp_ioctl(unsigned int cmd, void *arg)
887 int err;
888 struct arpreq r;
889 struct device * dev = NULL;
891 switch(cmd) {
892 case SIOCDARP:
893 case SIOCSARP:
894 if (!capable(CAP_NET_ADMIN))
895 return -EPERM;
896 case SIOCGARP:
897 err = copy_from_user(&r, arg, sizeof(struct arpreq));
898 if (err)
899 return -EFAULT;
900 break;
901 default:
902 return -EINVAL;
905 if (r.arp_pa.sa_family != AF_INET)
906 return -EPFNOSUPPORT;
908 if (!(r.arp_flags & ATF_PUBL) &&
909 (r.arp_flags & (ATF_NETMASK|ATF_DONTPUB)))
910 return -EINVAL;
911 if (!(r.arp_flags & ATF_NETMASK))
912 ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr=__constant_htonl(0xFFFFFFFFUL);
914 rtnl_lock();
915 if (r.arp_dev[0]) {
916 err = -ENODEV;
917 if ((dev = dev_get(r.arp_dev)) == NULL)
918 goto out;
920 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
921 if (!r.arp_ha.sa_family)
922 r.arp_ha.sa_family = dev->type;
923 err = -EINVAL;
924 if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
925 goto out;
926 } else if (cmd == SIOCGARP) {
927 err = -ENODEV;
928 goto out;
931 switch(cmd) {
932 case SIOCDARP:
933 err = arp_req_delete(&r, dev);
934 break;
935 case SIOCSARP:
936 err = arp_req_set(&r, dev);
937 break;
938 case SIOCGARP:
939 err = arp_req_get(&r, dev);
940 if (!err && copy_to_user(arg, &r, sizeof(r)))
941 err = -EFAULT;
942 break;
944 out:
945 rtnl_unlock();
946 return err;
950 * Write the contents of the ARP cache to a PROCfs file.
952 #ifdef CONFIG_PROC_FS
954 #define HBUFFERLEN 30
956 int arp_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
958 int len=0;
959 off_t pos=0;
960 int size;
961 char hbuffer[HBUFFERLEN];
962 int i,j,k;
963 const char hexbuf[] = "0123456789ABCDEF";
965 size = sprintf(buffer,"IP address HW type Flags HW address Mask Device\n");
967 pos+=size;
968 len+=size;
970 neigh_table_lock(&arp_tbl);
972 for(i=0; i<=NEIGH_HASHMASK; i++) {
973 struct neighbour *n;
974 for (n=arp_tbl.hash_buckets[i]; n; n=n->next) {
975 struct device *dev = n->dev;
976 int hatype = dev->type;
978 /* Do not confuse users "arp -a" with magic entries */
979 if (!(n->nud_state&~NUD_NOARP))
980 continue;
982 /* I'd get great pleasure deleting
983 this ugly code. Let's output it in hexadecimal format.
984 "arp" utility will eventually repaired --ANK
986 #if 1 /* UGLY CODE */
988 * Convert hardware address to XX:XX:XX:XX ... form.
990 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
991 if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
992 strcpy(hbuffer,ax2asc((ax25_address *)n->ha));
993 else {
994 #endif
995 for (k=0,j=0;k<HBUFFERLEN-3 && j<dev->addr_len;j++) {
996 hbuffer[k++]=hexbuf[(n->ha[j]>>4)&15 ];
997 hbuffer[k++]=hexbuf[n->ha[j]&15 ];
998 hbuffer[k++]=':';
1000 hbuffer[--k]=0;
1002 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1004 #endif
1005 #else
1006 if ((neigh->nud_state&NUD_VALID) && dev->addr_len) {
1007 int j;
1008 for (j=0; j < dev->addr_len; j++)
1009 sprintf(hbuffer+2*j, "%02x", neigh->ha[j]);
1010 } else
1011 sprintf(hbuffer, "0");
1012 #endif
1014 size = sprintf(buffer+len,
1015 "%-17s0x%-10x0x%-10x%s",
1016 in_ntoa(*(u32*)n->primary_key),
1017 hatype,
1018 arp_state_to_flags(n),
1019 hbuffer);
1020 size += sprintf(buffer+len+size,
1021 " %-17s %s\n",
1022 "*", dev->name);
1024 len += size;
1025 pos += size;
1027 if (pos <= offset)
1028 len=0;
1029 if (pos >= offset+length)
1030 goto done;
1034 for (i=0; i<=PNEIGH_HASHMASK; i++) {
1035 struct pneigh_entry *n;
1036 for (n=arp_tbl.phash_buckets[i]; n; n=n->next) {
1037 struct device *dev = n->dev;
1038 int hatype = dev ? dev->type : 0;
1040 size = sprintf(buffer+len,
1041 "%-17s0x%-10x0x%-10x%s",
1042 in_ntoa(*(u32*)n->key),
1043 hatype,
1044 ATF_PUBL|ATF_PERM,
1045 "00:00:00:00:00:00");
1046 size += sprintf(buffer+len+size,
1047 " %-17s %s\n",
1048 "*", dev ? dev->name : "*");
1050 len += size;
1051 pos += size;
1053 if (pos <= offset)
1054 len=0;
1055 if (pos >= offset+length)
1056 goto done;
1060 done:
1061 neigh_table_unlock(&arp_tbl);
1063 *start = buffer+len-(pos-offset); /* Start of wanted data */
1064 len = pos-offset; /* Start slop */
1065 if (len>length)
1066 len = length; /* Ending slop */
1067 if (len<0)
1068 len = 0;
1069 return len;
1071 #endif
1073 /* Note, that it is not on notifier chain.
1074 It is necessary, that this routine was called after route cache will be
1075 flushed.
1077 void arp_ifdown(struct device *dev)
1079 neigh_ifdown(&arp_tbl, dev);
1084 * Called once on startup.
1087 static struct packet_type arp_packet_type =
1089 __constant_htons(ETH_P_ARP),
1090 NULL, /* All devices */
1091 arp_rcv,
1092 NULL,
1093 NULL
1096 #ifdef CONFIG_PROC_FS
1097 static struct proc_dir_entry proc_net_arp = {
1098 PROC_NET_ARP, 3, "arp",
1099 S_IFREG | S_IRUGO, 1, 0, 0,
1100 0, &proc_net_inode_operations,
1101 arp_get_info
1103 #endif
1105 __initfunc(void arp_init (void))
1107 neigh_table_init(&arp_tbl);
1109 dev_add_pack(&arp_packet_type);
1111 #ifdef CONFIG_PROC_FS
1112 proc_net_register(&proc_net_arp);
1113 #endif
1114 #ifdef CONFIG_SYSCTL
1115 neigh_sysctl_register(NULL, &arp_tbl.parms, NET_IPV4, NET_IPV4_NEIGH, "ipv4");
1116 #endif
1120 #ifdef CONFIG_AX25_MODULE
1123 * ax25 -> ASCII conversion
1125 char *ax2asc(ax25_address *a)
1127 static char buf[11];
1128 char c, *s;
1129 int n;
1131 for (n = 0, s = buf; n < 6; n++) {
1132 c = (a->ax25_call[n] >> 1) & 0x7F;
1134 if (c != ' ') *s++ = c;
1137 *s++ = '-';
1139 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
1140 *s++ = '1';
1141 n -= 10;
1144 *s++ = n + '0';
1145 *s++ = '\0';
1147 if (*buf == '\0' || *buf == '-')
1148 return "*";
1150 return buf;
1154 #endif