Import 2.3.10pre5
[davej-history.git] / net / ipv4 / arp.c
bloba3ca8870108afc0a365ab8979f7df24edb394781
1 /* linux/net/inet/arp.c
3 * Version: $Id: arp.c,v 1.78 1999/06/09 10:10:36 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>
122 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
123 static char *ax2asc2(ax25_address *a, char *buf);
124 #endif
128 * Interface to generic neighbour cache.
130 static int arp_constructor(struct neighbour *neigh);
131 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
132 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
133 static void parp_redo(struct sk_buff *skb);
135 static struct neigh_ops arp_generic_ops =
137 AF_INET,
138 NULL,
139 arp_solicit,
140 arp_error_report,
141 neigh_resolve_output,
142 neigh_connected_output,
143 dev_queue_xmit,
144 dev_queue_xmit
147 static struct neigh_ops arp_hh_ops =
149 AF_INET,
150 NULL,
151 arp_solicit,
152 arp_error_report,
153 neigh_resolve_output,
154 neigh_resolve_output,
155 dev_queue_xmit,
156 dev_queue_xmit
159 static struct neigh_ops arp_direct_ops =
161 AF_INET,
162 NULL,
163 NULL,
164 NULL,
165 dev_queue_xmit,
166 dev_queue_xmit,
167 dev_queue_xmit,
168 dev_queue_xmit
171 struct neigh_ops arp_broken_ops =
173 AF_INET,
174 NULL,
175 arp_solicit,
176 arp_error_report,
177 neigh_compat_output,
178 neigh_compat_output,
179 dev_queue_xmit,
180 dev_queue_xmit,
183 struct neigh_table arp_tbl =
185 NULL,
186 AF_INET,
187 sizeof(struct neighbour) + 4,
189 arp_constructor,
190 NULL,
191 NULL,
192 parp_redo,
193 { NULL, NULL, &arp_tbl, 0, NULL, NULL,
194 30*HZ, 1*HZ, 60*HZ, 30*HZ, 5*HZ, 3, 3, 0, 3, 1*HZ, (8*HZ)/10, 64, 1*HZ },
195 30*HZ, 128, 512, 1024,
198 int arp_mc_map(u32 addr, u8 *haddr, struct device *dev, int dir)
200 switch (dev->type) {
201 case ARPHRD_ETHER:
202 case ARPHRD_IEEE802:
203 case ARPHRD_FDDI:
204 ip_eth_mc_map(addr, haddr);
205 return 0;
206 default:
207 if (dir) {
208 memcpy(haddr, dev->broadcast, dev->addr_len);
209 return 0;
212 return -EINVAL;
217 static int arp_constructor(struct neighbour *neigh)
219 u32 addr = *(u32*)neigh->primary_key;
220 struct device *dev = neigh->dev;
221 struct in_device *in_dev = dev->ip_ptr;
223 if (in_dev == NULL)
224 return -EINVAL;
226 neigh->type = inet_addr_type(addr);
227 if (in_dev->arp_parms)
228 neigh->parms = in_dev->arp_parms;
230 if (dev->hard_header == NULL) {
231 neigh->nud_state = NUD_NOARP;
232 neigh->ops = &arp_direct_ops;
233 neigh->output = neigh->ops->queue_xmit;
234 } else {
235 /* Good devices (checked by reading texts, but only Ethernet is
236 tested)
238 ARPHRD_ETHER: (ethernet, apfddi)
239 ARPHRD_FDDI: (fddi)
240 ARPHRD_IEEE802: (tr)
241 ARPHRD_METRICOM: (strip)
242 ARPHRD_ARCNET:
243 etc. etc. etc.
245 ARPHRD_IPDDP will also work, if author repairs it.
246 I did not it, because this driver does not work even
247 in old paradigm.
250 #if 1
251 /* So... these "amateur" devices are hopeless.
252 The only thing, that I can say now:
253 It is very sad that we need to keep ugly obsolete
254 code to make them happy.
256 They should be moved to more reasonable state, now
257 they use rebuild_header INSTEAD OF hard_start_xmit!!!
258 Besides that, they are sort of out of date
259 (a lot of redundant clones/copies, useless in 2.1),
260 I wonder why people believe that they work.
262 switch (dev->type) {
263 default:
264 break;
265 case ARPHRD_ROSE:
266 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
267 case ARPHRD_AX25:
268 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
269 case ARPHRD_NETROM:
270 #endif
271 neigh->ops = &arp_broken_ops;
272 neigh->output = neigh->ops->output;
273 return 0;
274 #endif
276 #endif
277 if (neigh->type == RTN_MULTICAST) {
278 neigh->nud_state = NUD_NOARP;
279 arp_mc_map(addr, neigh->ha, dev, 1);
280 } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
281 neigh->nud_state = NUD_NOARP;
282 memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
283 } else if (neigh->type == RTN_BROADCAST || dev->flags&IFF_POINTOPOINT) {
284 neigh->nud_state = NUD_NOARP;
285 memcpy(neigh->ha, dev->broadcast, dev->addr_len);
287 if (dev->hard_header_cache)
288 neigh->ops = &arp_hh_ops;
289 else
290 neigh->ops = &arp_generic_ops;
291 if (neigh->nud_state&NUD_VALID)
292 neigh->output = neigh->ops->connected_output;
293 else
294 neigh->output = neigh->ops->output;
297 return 0;
300 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
302 dst_link_failure(skb);
303 kfree_skb(skb);
306 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
308 u32 saddr;
309 u8 *dst_ha = NULL;
310 struct device *dev = neigh->dev;
311 u32 target = *(u32*)neigh->primary_key;
312 int probes = atomic_read(&neigh->probes);
314 if (skb && inet_addr_type(skb->nh.iph->saddr) == RTN_LOCAL)
315 saddr = skb->nh.iph->saddr;
316 else
317 saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
319 if ((probes -= neigh->parms->ucast_probes) < 0) {
320 if (!(neigh->nud_state&NUD_VALID))
321 printk(KERN_DEBUG "trying to ucast probe in NUD_INVALID\n");
322 dst_ha = neigh->ha;
323 read_lock_bh(&neigh->lock);
324 } else if ((probes -= neigh->parms->app_probes) < 0) {
325 #ifdef CONFIG_ARPD
326 neigh_app_ns(neigh);
327 #endif
328 return;
331 arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
332 dst_ha, dev->dev_addr, NULL);
333 if (dst_ha)
334 read_unlock_bh(&neigh->lock);
337 /* OBSOLETE FUNCTIONS */
340 * Find an arp mapping in the cache. If not found, post a request.
342 * It is very UGLY routine: it DOES NOT use skb->dst->neighbour,
343 * even if it exists. It is supposed that skb->dev was mangled
344 * by a virtual device (eql, shaper). Nobody but broken devices
345 * is allowed to use this function, it is scheduled to be removed. --ANK
348 static int arp_set_predefined(int addr_hint, unsigned char * haddr, u32 paddr, struct device * dev)
350 switch (addr_hint) {
351 case RTN_LOCAL:
352 printk(KERN_DEBUG "ARP: arp called for own IP address\n");
353 memcpy(haddr, dev->dev_addr, dev->addr_len);
354 return 1;
355 case RTN_MULTICAST:
356 arp_mc_map(paddr, haddr, dev, 1);
357 return 1;
358 case RTN_BROADCAST:
359 memcpy(haddr, dev->broadcast, dev->addr_len);
360 return 1;
362 return 0;
366 int arp_find(unsigned char *haddr, struct sk_buff *skb)
368 struct device *dev = skb->dev;
369 u32 paddr;
370 struct neighbour *n;
372 if (!skb->dst) {
373 printk(KERN_DEBUG "arp_find is called with dst==NULL\n");
374 kfree_skb(skb);
375 return 1;
378 paddr = ((struct rtable*)skb->dst)->rt_gateway;
380 if (arp_set_predefined(inet_addr_type(paddr), haddr, paddr, dev))
381 return 0;
383 n = __neigh_lookup(&arp_tbl, &paddr, dev, 1);
385 if (n) {
386 n->used = jiffies;
387 if (n->nud_state&NUD_VALID || neigh_event_send(n, skb) == 0) {
388 read_lock_bh(&n->lock);
389 memcpy(haddr, n->ha, dev->addr_len);
390 read_unlock_bh(&n->lock);
391 neigh_release(n);
392 return 0;
394 neigh_release(n);
395 } else
396 kfree_skb(skb);
397 return 1;
400 /* END OF OBSOLETE FUNCTIONS */
402 int arp_bind_neighbour(struct dst_entry *dst)
404 struct device *dev = dst->dev;
406 if (dev == NULL)
407 return 0;
408 if (dst->neighbour == NULL) {
409 u32 nexthop = ((struct rtable*)dst)->rt_gateway;
410 if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT))
411 nexthop = 0;
412 dst->neighbour = __neigh_lookup(&arp_tbl, &nexthop, dev, 1);
414 return (dst->neighbour != NULL);
418 * Interface to link layer: send routine and receive handler.
422 * Create and send an arp packet. If (dest_hw == NULL), we create a broadcast
423 * message.
426 void arp_send(int type, int ptype, u32 dest_ip,
427 struct device *dev, u32 src_ip,
428 unsigned char *dest_hw, unsigned char *src_hw,
429 unsigned char *target_hw)
431 struct sk_buff *skb;
432 struct arphdr *arp;
433 unsigned char *arp_ptr;
436 * No arp on this interface.
439 if (dev->flags&IFF_NOARP)
440 return;
443 * Allocate a buffer
446 skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
447 + dev->hard_header_len + 15, GFP_ATOMIC);
448 if (skb == NULL)
449 return;
451 skb_reserve(skb, (dev->hard_header_len+15)&~15);
452 skb->nh.raw = skb->data;
453 arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4));
454 skb->dev = dev;
455 skb->protocol = __constant_htons (ETH_P_ARP);
456 if (src_hw == NULL)
457 src_hw = dev->dev_addr;
458 if (dest_hw == NULL)
459 dest_hw = dev->broadcast;
462 * Fill the device header for the ARP frame
464 dev->hard_header(skb,dev,ptype,dest_hw,src_hw,skb->len);
467 * Fill out the arp protocol part.
469 * The arp hardware type should match the device type, except for FDDI,
470 * which (according to RFC 1390) should always equal 1 (Ethernet).
473 * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
474 * DIX code for the protocol. Make these device structure fields.
476 switch (dev->type) {
477 default:
478 arp->ar_hrd = htons(dev->type);
479 arp->ar_pro = __constant_htons(ETH_P_IP);
480 break;
482 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
483 case ARPHRD_AX25:
484 arp->ar_hrd = __constant_htons(ARPHRD_AX25);
485 arp->ar_pro = __constant_htons(AX25_P_IP);
486 break;
488 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
489 case ARPHRD_NETROM:
490 arp->ar_hrd = __constant_htons(ARPHRD_NETROM);
491 arp->ar_pro = __constant_htons(AX25_P_IP);
492 break;
493 #endif
494 #endif
496 #ifdef CONFIG_FDDI
497 case ARPHRD_FDDI:
498 arp->ar_hrd = __constant_htons(ARPHRD_ETHER);
499 arp->ar_pro = __constant_htons(ETH_P_IP);
500 break;
501 #endif
504 arp->ar_hln = dev->addr_len;
505 arp->ar_pln = 4;
506 arp->ar_op = htons(type);
508 arp_ptr=(unsigned char *)(arp+1);
510 memcpy(arp_ptr, src_hw, dev->addr_len);
511 arp_ptr+=dev->addr_len;
512 memcpy(arp_ptr, &src_ip,4);
513 arp_ptr+=4;
514 if (target_hw != NULL)
515 memcpy(arp_ptr, target_hw, dev->addr_len);
516 else
517 memset(arp_ptr, 0, dev->addr_len);
518 arp_ptr+=dev->addr_len;
519 memcpy(arp_ptr, &dest_ip, 4);
520 skb->dev = dev;
522 dev_queue_xmit(skb);
525 static void parp_redo(struct sk_buff *skb)
527 arp_rcv(skb, skb->dev, NULL);
531 * Receive an arp request by the device layer.
534 int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
536 struct arphdr *arp = skb->nh.arph;
537 unsigned char *arp_ptr= (unsigned char *)(arp+1);
538 struct rtable *rt;
539 unsigned char *sha, *tha;
540 u32 sip, tip;
541 u16 dev_type = dev->type;
542 int addr_type;
543 struct in_device *in_dev = dev->ip_ptr;
544 struct neighbour *n;
547 * The hardware length of the packet should match the hardware length
548 * of the device. Similarly, the hardware types should match. The
549 * device should be ARP-able. Also, if pln is not 4, then the lookup
550 * is not from an IP number. We can't currently handle this, so toss
551 * it.
553 if (in_dev == NULL ||
554 arp->ar_hln != dev->addr_len ||
555 dev->flags & IFF_NOARP ||
556 skb->pkt_type == PACKET_OTHERHOST ||
557 skb->pkt_type == PACKET_LOOPBACK ||
558 arp->ar_pln != 4)
559 goto out;
561 switch (dev_type) {
562 default:
563 if (arp->ar_pro != __constant_htons(ETH_P_IP))
564 goto out;
565 if (htons(dev_type) != arp->ar_hrd)
566 goto out;
567 break;
568 #ifdef CONFIG_NET_ETHERNET
569 case ARPHRD_ETHER:
571 * ETHERNET devices will accept ARP hardware types of either
572 * 1 (Ethernet) or 6 (IEEE 802.2).
574 if (arp->ar_hrd != __constant_htons(ARPHRD_ETHER) &&
575 arp->ar_hrd != __constant_htons(ARPHRD_IEEE802))
576 goto out;
577 if (arp->ar_pro != __constant_htons(ETH_P_IP))
578 goto out;
579 break;
580 #endif
581 #ifdef CONFIG_FDDI
582 case ARPHRD_FDDI:
584 * According to RFC 1390, FDDI devices should accept ARP hardware types
585 * of 1 (Ethernet). However, to be more robust, we'll accept hardware
586 * types of either 1 (Ethernet) or 6 (IEEE 802.2).
588 if (arp->ar_hrd != __constant_htons(ARPHRD_ETHER) &&
589 arp->ar_hrd != __constant_htons(ARPHRD_IEEE802))
590 goto out;
591 if (arp->ar_pro != __constant_htons(ETH_P_IP))
592 goto out;
593 break;
594 #endif
595 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
596 case ARPHRD_AX25:
597 if (arp->ar_pro != __constant_htons(AX25_P_IP))
598 goto out;
599 if (arp->ar_hrd != __constant_htons(ARPHRD_AX25))
600 goto out;
601 break;
602 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
603 case ARPHRD_NETROM:
604 if (arp->ar_pro != __constant_htons(AX25_P_IP))
605 goto out;
606 if (arp->ar_hrd != __constant_htons(ARPHRD_NETROM))
607 goto out;
608 break;
609 #endif
610 #endif
613 /* Undertsand only these message types */
615 if (arp->ar_op != __constant_htons(ARPOP_REPLY) &&
616 arp->ar_op != __constant_htons(ARPOP_REQUEST))
617 goto out;
620 * Extract fields
622 sha=arp_ptr;
623 arp_ptr += dev->addr_len;
624 memcpy(&sip, arp_ptr, 4);
625 arp_ptr += 4;
626 tha=arp_ptr;
627 arp_ptr += dev->addr_len;
628 memcpy(&tip, arp_ptr, 4);
630 * Check for bad requests for 127.x.x.x and requests for multicast
631 * addresses. If this is one such, delete it.
633 if (LOOPBACK(tip) || MULTICAST(tip))
634 goto out;
637 * Process entry. The idea here is we want to send a reply if it is a
638 * request for us or if it is a request for someone else that we hold
639 * a proxy for. We want to add an entry to our cache if it is a reply
640 * to us or if it is a request for our address.
641 * (The assumption for this last is that if someone is requesting our
642 * address, they are probably intending to talk to us, so it saves time
643 * if we cache their address. Their address is also probably not in
644 * our cache, since ours is not in their cache.)
646 * Putting this another way, we only care about replies if they are to
647 * us, in which case we add them to the cache. For requests, we care
648 * about those for us and those for our proxies. We reply to both,
649 * and in the case of requests for us we add the requester to the arp
650 * cache.
653 /* Special case: IPv4 duplicate address detection packet (RFC2131) */
654 if (sip == 0) {
655 if (arp->ar_op == __constant_htons(ARPOP_REQUEST) &&
656 inet_addr_type(tip) == RTN_LOCAL)
657 arp_send(ARPOP_REPLY,ETH_P_ARP,tip,dev,tip,sha,dev->dev_addr,dev->dev_addr);
658 goto out;
661 if (arp->ar_op == __constant_htons(ARPOP_REQUEST) &&
662 ip_route_input(skb, tip, sip, 0, dev) == 0) {
664 rt = (struct rtable*)skb->dst;
665 addr_type = rt->rt_type;
667 if (addr_type == RTN_LOCAL) {
668 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
669 if (n) {
670 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
671 neigh_release(n);
673 goto out;
674 } else if (IN_DEV_FORWARD(in_dev)) {
675 if ((rt->rt_flags&RTCF_DNAT) ||
676 (addr_type == RTN_UNICAST && rt->u.dst.dev != dev &&
677 (IN_DEV_PROXY_ARP(in_dev) || pneigh_lookup(&arp_tbl, &tip, dev, 0)))) {
678 n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
679 if (n)
680 neigh_release(n);
682 if (skb->stamp.tv_sec == 0 ||
683 skb->pkt_type == PACKET_HOST ||
684 in_dev->arp_parms->proxy_delay == 0) {
685 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
686 } else {
687 pneigh_enqueue(&arp_tbl, in_dev->arp_parms, skb);
688 return 0;
690 goto out;
695 /* Update our ARP tables */
697 n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
699 #ifdef CONFIG_IP_ACCEPT_UNSOLICITED_ARP
700 /* Unsolicited ARP is not accepted by default.
701 It is possible, that this option should be enabled for some
702 devices (strip is candidate)
704 if (n == NULL &&
705 arp->ar_op == __constant_htons(ARPOP_REPLY) &&
706 inet_addr_type(sip) == RTN_UNICAST)
707 n = __neigh_lookup(&arp_tbl, &sip, dev, -1);
708 #endif
710 if (n) {
711 int state = NUD_REACHABLE;
712 int override = 0;
714 /* If several different ARP replies follows back-to-back,
715 use the FIRST one. It is possible, if several proxy
716 agents are active. Taking the first reply prevents
717 arp trashing and chooses the fastest router.
719 if (jiffies - n->updated >= n->parms->locktime)
720 override = 1;
722 /* Broadcast replies and request packets
723 do not assert neighbour reachability.
725 if (arp->ar_op != __constant_htons(ARPOP_REPLY) ||
726 skb->pkt_type != PACKET_HOST)
727 state = NUD_STALE;
728 neigh_update(n, sha, state, override, 1);
729 neigh_release(n);
732 out:
733 kfree_skb(skb);
734 return 0;
740 * User level interface (ioctl, /proc)
744 * Set (create) an ARP cache entry.
747 int arp_req_set(struct arpreq *r, struct device * dev)
749 u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
750 struct neighbour *neigh;
751 int err;
753 if (r->arp_flags&ATF_PUBL) {
754 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
755 if (mask && mask != 0xFFFFFFFF)
756 return -EINVAL;
757 if (!dev && (r->arp_flags & ATF_COM)) {
758 dev = dev_getbyhwaddr(r->arp_ha.sa_family, r->arp_ha.sa_data);
759 if (!dev)
760 return -ENODEV;
762 if (mask) {
763 if (pneigh_lookup(&arp_tbl, &ip, dev, 1) == NULL)
764 return -ENOBUFS;
765 return 0;
767 if (dev == NULL) {
768 ipv4_devconf.proxy_arp = 1;
769 return 0;
771 if (dev->ip_ptr) {
772 ((struct in_device*)dev->ip_ptr)->cnf.proxy_arp = 1;
773 return 0;
775 return -ENXIO;
778 if (r->arp_flags & ATF_PERM)
779 r->arp_flags |= ATF_COM;
780 if (dev == NULL) {
781 struct rtable * rt;
782 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
783 return err;
784 dev = rt->u.dst.dev;
785 ip_rt_put(rt);
786 if (!dev)
787 return -EINVAL;
789 if (r->arp_ha.sa_family != dev->type)
790 return -EINVAL;
792 err = -ENOBUFS;
793 neigh = __neigh_lookup(&arp_tbl, &ip, dev, 1);
794 if (neigh) {
795 unsigned state = NUD_STALE;
796 if (r->arp_flags & ATF_PERM)
797 state = NUD_PERMANENT;
798 err = neigh_update(neigh, (r->arp_flags&ATF_COM) ?
799 r->arp_ha.sa_data : NULL, state, 1, 0);
800 neigh_release(neigh);
802 return err;
805 static unsigned arp_state_to_flags(struct neighbour *neigh)
807 unsigned flags = 0;
808 if (neigh->nud_state&NUD_PERMANENT)
809 flags = ATF_PERM|ATF_COM;
810 else if (neigh->nud_state&NUD_VALID)
811 flags = ATF_COM;
812 return flags;
816 * Get an ARP cache entry.
819 static int arp_req_get(struct arpreq *r, struct device *dev)
821 u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
822 struct neighbour *neigh;
823 int err = -ENXIO;
825 neigh = neigh_lookup(&arp_tbl, &ip, dev);
826 if (neigh) {
827 read_lock_bh(&neigh->lock);
828 memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
829 r->arp_flags = arp_state_to_flags(neigh);
830 read_unlock_bh(&neigh->lock);
831 r->arp_ha.sa_family = dev->type;
832 strncpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
833 neigh_release(neigh);
834 err = 0;
836 return err;
839 int arp_req_delete(struct arpreq *r, struct device * dev)
841 int err;
842 u32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
843 struct neighbour *neigh;
845 if (r->arp_flags & ATF_PUBL) {
846 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
847 if (mask == 0xFFFFFFFF)
848 return pneigh_delete(&arp_tbl, &ip, dev);
849 if (mask == 0) {
850 if (dev == NULL) {
851 ipv4_devconf.proxy_arp = 0;
852 return 0;
854 if (dev->ip_ptr) {
855 ((struct in_device*)dev->ip_ptr)->cnf.proxy_arp = 0;
856 return 0;
858 return -ENXIO;
860 return -EINVAL;
863 if (dev == NULL) {
864 struct rtable * rt;
865 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
866 return err;
867 dev = rt->u.dst.dev;
868 ip_rt_put(rt);
869 if (!dev)
870 return -EINVAL;
872 err = -ENXIO;
873 neigh = neigh_lookup(&arp_tbl, &ip, dev);
874 if (neigh) {
875 if (neigh->nud_state&~NUD_NOARP)
876 err = neigh_update(neigh, NULL, NUD_FAILED, 1, 0);
877 neigh_release(neigh);
879 return err;
883 * Handle an ARP layer I/O control request.
886 int arp_ioctl(unsigned int cmd, void *arg)
888 int err;
889 struct arpreq r;
890 struct device * dev = NULL;
892 switch(cmd) {
893 case SIOCDARP:
894 case SIOCSARP:
895 if (!capable(CAP_NET_ADMIN))
896 return -EPERM;
897 case SIOCGARP:
898 err = copy_from_user(&r, arg, sizeof(struct arpreq));
899 if (err)
900 return -EFAULT;
901 break;
902 default:
903 return -EINVAL;
906 if (r.arp_pa.sa_family != AF_INET)
907 return -EPFNOSUPPORT;
909 if (!(r.arp_flags & ATF_PUBL) &&
910 (r.arp_flags & (ATF_NETMASK|ATF_DONTPUB)))
911 return -EINVAL;
912 if (!(r.arp_flags & ATF_NETMASK))
913 ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr=__constant_htonl(0xFFFFFFFFUL);
915 rtnl_lock();
916 if (r.arp_dev[0]) {
917 err = -ENODEV;
918 if ((dev = dev_get(r.arp_dev)) == NULL)
919 goto out;
921 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
922 if (!r.arp_ha.sa_family)
923 r.arp_ha.sa_family = dev->type;
924 err = -EINVAL;
925 if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
926 goto out;
927 } else if (cmd == SIOCGARP) {
928 err = -ENODEV;
929 goto out;
932 switch(cmd) {
933 case SIOCDARP:
934 err = arp_req_delete(&r, dev);
935 break;
936 case SIOCSARP:
937 err = arp_req_set(&r, dev);
938 break;
939 case SIOCGARP:
940 err = arp_req_get(&r, dev);
941 if (!err && copy_to_user(arg, &r, sizeof(r)))
942 err = -EFAULT;
943 break;
945 out:
946 rtnl_unlock();
947 return err;
951 * Write the contents of the ARP cache to a PROCfs file.
953 #ifdef CONFIG_PROC_FS
955 #define HBUFFERLEN 30
957 int arp_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
959 int len=0;
960 off_t pos=0;
961 int size;
962 char hbuffer[HBUFFERLEN];
963 int i,j,k;
964 const char hexbuf[] = "0123456789ABCDEF";
965 char abuf[16];
967 size = sprintf(buffer,"IP address HW type Flags HW address Mask Device\n");
969 pos+=size;
970 len+=size;
972 for(i=0; i<=NEIGH_HASHMASK; i++) {
973 struct neighbour *n;
974 read_lock_bh(&arp_tbl.lock);
975 for (n=arp_tbl.hash_buckets[i]; n; n=n->next) {
976 struct device *dev = n->dev;
977 int hatype = dev->type;
979 /* Do not confuse users "arp -a" with magic entries */
980 if (!(n->nud_state&~NUD_NOARP))
981 continue;
983 read_lock(&n->lock);
986 * Convert hardware address to XX:XX:XX:XX ... form.
988 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
989 if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
990 ax2asc2((ax25_address *)n->ha, hbuffer);
991 else {
992 #endif
993 for (k=0,j=0;k<HBUFFERLEN-3 && j<dev->addr_len;j++) {
994 hbuffer[k++]=hexbuf[(n->ha[j]>>4)&15 ];
995 hbuffer[k++]=hexbuf[n->ha[j]&15 ];
996 hbuffer[k++]=':';
998 hbuffer[--k]=0;
1000 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1002 #endif
1004 size = sprintf(buffer+len,
1005 "%-17s0x%-10x0x%-10x%s",
1006 in_ntoa2(*(u32*)n->primary_key, abuf),
1007 hatype,
1008 arp_state_to_flags(n),
1009 hbuffer);
1010 size += sprintf(buffer+len+size,
1011 " %-17s %s\n",
1012 "*", dev->name);
1013 read_unlock(&n->lock);
1015 len += size;
1016 pos += size;
1018 if (pos <= offset)
1019 len=0;
1020 if (pos >= offset+length) {
1021 read_unlock_bh(&arp_tbl.lock);
1022 goto done;
1025 read_unlock_bh(&arp_tbl.lock);
1028 for (i=0; i<=PNEIGH_HASHMASK; i++) {
1029 struct pneigh_entry *n;
1030 for (n=arp_tbl.phash_buckets[i]; n; n=n->next) {
1031 struct device *dev = n->dev;
1032 int hatype = dev ? dev->type : 0;
1034 size = sprintf(buffer+len,
1035 "%-17s0x%-10x0x%-10x%s",
1036 in_ntoa2(*(u32*)n->key, abuf),
1037 hatype,
1038 ATF_PUBL|ATF_PERM,
1039 "00:00:00:00:00:00");
1040 size += sprintf(buffer+len+size,
1041 " %-17s %s\n",
1042 "*", dev ? dev->name : "*");
1044 len += size;
1045 pos += size;
1047 if (pos <= offset)
1048 len=0;
1049 if (pos >= offset+length)
1050 goto done;
1054 done:
1056 *start = buffer+len-(pos-offset); /* Start of wanted data */
1057 len = pos-offset; /* Start slop */
1058 if (len>length)
1059 len = length; /* Ending slop */
1060 if (len<0)
1061 len = 0;
1062 return len;
1064 #endif
1066 /* Note, that it is not on notifier chain.
1067 It is necessary, that this routine was called after route cache will be
1068 flushed.
1070 void arp_ifdown(struct device *dev)
1072 neigh_ifdown(&arp_tbl, dev);
1077 * Called once on startup.
1080 static struct packet_type arp_packet_type =
1082 __constant_htons(ETH_P_ARP),
1083 NULL, /* All devices */
1084 arp_rcv,
1085 NULL,
1086 NULL
1089 #ifdef CONFIG_PROC_FS
1090 static struct proc_dir_entry proc_net_arp = {
1091 PROC_NET_ARP, 3, "arp",
1092 S_IFREG | S_IRUGO, 1, 0, 0,
1093 0, &proc_net_inode_operations,
1094 arp_get_info
1096 #endif
1098 __initfunc(void arp_init (void))
1100 neigh_table_init(&arp_tbl);
1102 dev_add_pack(&arp_packet_type);
1104 #ifdef CONFIG_PROC_FS
1105 proc_net_register(&proc_net_arp);
1106 #endif
1107 #ifdef CONFIG_SYSCTL
1108 neigh_sysctl_register(NULL, &arp_tbl.parms, NET_IPV4, NET_IPV4_NEIGH, "ipv4");
1109 #endif
1113 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1116 * ax25 -> ASCII conversion
1118 char *ax2asc2(ax25_address *a, char *buf)
1120 char c, *s;
1121 int n;
1123 for (n = 0, s = buf; n < 6; n++) {
1124 c = (a->ax25_call[n] >> 1) & 0x7F;
1126 if (c != ' ') *s++ = c;
1129 *s++ = '-';
1131 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
1132 *s++ = '1';
1133 n -= 10;
1136 *s++ = n + '0';
1137 *s++ = '\0';
1139 if (*buf == '\0' || *buf == '-')
1140 return "*";
1142 return buf;
1146 #endif