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
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.
18 * Alan Cox : Removed the Ethernet assumptions in
20 * Alan Cox : Fixed some small errors in the ARP
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
44 * Russ Nelson : Tidied up a few bits.
45 * Alexey Kuznetsov: Major changes to caching and behaviour,
46 * eg intelligent arp probing and
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
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.
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)
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>
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>
102 #include <linux/sysctl.h>
106 #include <net/icmp.h>
107 #include <net/route.h>
108 #include <net/protocol.h>
110 #include <net/sock.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>
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
=
136 neigh_resolve_output
,
137 neigh_connected_output
,
142 static struct neigh_ops arp_hh_ops
=
148 neigh_resolve_output
,
149 neigh_resolve_output
,
154 static struct neigh_ops arp_direct_ops
=
166 struct neigh_ops arp_broken_ops
=
178 struct neigh_table arp_tbl
=
182 sizeof(struct neighbour
) + 4,
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
)
199 ip_eth_mc_map(addr
, haddr
);
203 memcpy(haddr
, dev
->broadcast
, dev
->addr_len
);
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
;
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
;
230 /* Good devices (checked by reading texts, but only Ethernet is
233 ARPHRD_ETHER: (ethernet, apfddi)
236 ARPHRD_METRICOM: (strip)
240 ARPHRD_IPDDP will also work, if author repairs it.
241 I did not it, because this driver does not work even
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.
261 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
263 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
266 neigh
->ops
= &arp_broken_ops
;
267 neigh
->output
= neigh
->ops
->output
;
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
;
285 neigh
->ops
= &arp_generic_ops
;
286 if (neigh
->nud_state
&NUD_VALID
)
287 neigh
->output
= neigh
->ops
->connected_output
;
289 neigh
->output
= neigh
->ops
->output
;
295 static void arp_error_report(struct neighbour
*neigh
, struct sk_buff
*skb
)
297 dst_link_failure(skb
);
301 static void arp_solicit(struct neighbour
*neigh
, struct sk_buff
*skb
)
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
;
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");
318 } else if ((probes
-= neigh
->parms
->app_probes
) < 0) {
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
)
344 printk(KERN_DEBUG
"ARP: arp called for own IP address\n");
345 memcpy(haddr
, dev
->dev_addr
, dev
->addr_len
);
348 arp_mc_map(paddr
, haddr
, dev
, 1);
351 memcpy(haddr
, dev
->broadcast
, dev
->addr_len
);
358 int arp_find(unsigned char *haddr
, struct sk_buff
*skb
)
360 struct device
*dev
= skb
->dev
;
365 printk(KERN_DEBUG
"arp_find is called with dst==NULL\n");
370 paddr
= ((struct rtable
*)skb
->dst
)->rt_gateway
;
372 if (arp_set_predefined(inet_addr_type(paddr
), haddr
, paddr
, dev
))
376 n
= __neigh_lookup(&arp_tbl
, &paddr
, dev
, 1);
380 if (n
->nud_state
&NUD_VALID
|| neigh_event_send(n
, skb
) == 0) {
381 memcpy(haddr
, n
->ha
, dev
->addr_len
);
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
;
404 if (dst
->neighbour
== NULL
) {
405 u32 nexthop
= ((struct rtable
*)dst
)->rt_gateway
;
406 if (dev
->flags
&(IFF_LOOPBACK
|IFF_POINTOPOINT
))
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
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
)
429 unsigned char *arp_ptr
;
432 * No arp on this interface.
435 if (dev
->flags
&IFF_NOARP
)
442 skb
= alloc_skb(sizeof(struct arphdr
)+ 2*(dev
->addr_len
+4)
443 + dev
->hard_header_len
+ 15, GFP_ATOMIC
);
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));
451 skb
->protocol
= __constant_htons (ETH_P_ARP
);
453 src_hw
= dev
->dev_addr
;
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.
474 arp
->ar_hrd
= htons(dev
->type
);
475 arp
->ar_pro
= __constant_htons(ETH_P_IP
);
478 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
480 arp
->ar_hrd
= __constant_htons(ARPHRD_AX25
);
481 arp
->ar_pro
= __constant_htons(AX25_P_IP
);
484 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
486 arp
->ar_hrd
= __constant_htons(ARPHRD_NETROM
);
487 arp
->ar_pro
= __constant_htons(AX25_P_IP
);
494 arp
->ar_hrd
= __constant_htons(ARPHRD_ETHER
);
495 arp
->ar_pro
= __constant_htons(ETH_P_IP
);
500 arp
->ar_hln
= dev
->addr_len
;
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);
510 if (target_hw
!= NULL
)
511 memcpy(arp_ptr
, target_hw
, dev
->addr_len
);
513 memset(arp_ptr
, 0, dev
->addr_len
);
514 arp_ptr
+=dev
->addr_len
;
515 memcpy(arp_ptr
, &dest_ip
, 4);
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);
535 unsigned char *sha
, *tha
;
537 u16 dev_type
= dev
->type
;
539 struct in_device
*in_dev
= dev
->ip_ptr
;
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
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
||
559 if (arp
->ar_pro
!= __constant_htons(ETH_P_IP
))
561 if (htons(dev_type
) != arp
->ar_hrd
)
564 #ifdef CONFIG_NET_ETHERNET
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
))
573 if (arp
->ar_pro
!= __constant_htons(ETH_P_IP
))
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
))
587 if (arp
->ar_pro
!= __constant_htons(ETH_P_IP
))
591 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
593 if (arp
->ar_pro
!= __constant_htons(AX25_P_IP
))
595 if (arp
->ar_hrd
!= __constant_htons(ARPHRD_AX25
))
598 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
600 if (arp
->ar_pro
!= __constant_htons(AX25_P_IP
))
602 if (arp
->ar_hrd
!= __constant_htons(ARPHRD_NETROM
))
609 /* Undertsand only these message types */
611 if (arp
->ar_op
!= __constant_htons(ARPOP_REPLY
) &&
612 arp
->ar_op
!= __constant_htons(ARPOP_REQUEST
))
619 arp_ptr
+= dev
->addr_len
;
620 memcpy(&sip
, arp_ptr
, 4);
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
))
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
649 /* Special case: IPv4 duplicate address detection packet (RFC2131) */
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
);
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
);
666 arp_send(ARPOP_REPLY
,ETH_P_ARP
,sip
,dev
,tip
,sha
,dev
->dev_addr
,sha
);
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
);
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
);
682 pneigh_enqueue(&arp_tbl
, in_dev
->arp_parms
, skb
);
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)
700 arp
->ar_op
== __constant_htons(ARPOP_REPLY
) &&
701 inet_addr_type(sip
) == RTN_UNICAST
)
702 n
= __neigh_lookup(&arp_tbl
, &sip
, dev
, -1);
706 int state
= NUD_REACHABLE
;
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
)
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
)
723 neigh_update(n
, sha
, state
, override
, 1);
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
;
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)
752 if (!dev
&& (r
->arp_flags
& ATF_COM
)) {
753 dev
= dev_getbyhwaddr(r
->arp_ha
.sa_family
, r
->arp_ha
.sa_data
);
758 if (pneigh_lookup(&arp_tbl
, &ip
, dev
, 1) == NULL
)
763 ipv4_devconf
.proxy_arp
= 1;
767 ((struct in_device
*)dev
->ip_ptr
)->cnf
.proxy_arp
= 1;
773 if (r
->arp_flags
& ATF_PERM
)
774 r
->arp_flags
|= ATF_COM
;
777 if ((err
= ip_route_output(&rt
, ip
, 0, RTO_ONLINK
, 0)) != 0)
784 if (r
->arp_ha
.sa_family
!= dev
->type
)
789 neigh
= __neigh_lookup(&arp_tbl
, &ip
, dev
, 1);
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
);
802 static unsigned arp_state_to_flags(struct neighbour
*neigh
)
805 if (neigh
->nud_state
&NUD_PERMANENT
)
806 flags
= ATF_PERM
|ATF_COM
;
807 else if (neigh
->nud_state
&NUD_VALID
)
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
;
823 neigh
= __neigh_lookup(&arp_tbl
, &ip
, dev
, 0);
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
);
836 int arp_req_delete(struct arpreq
*r
, struct device
* dev
)
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
);
848 ipv4_devconf
.proxy_arp
= 0;
852 ((struct in_device
*)dev
->ip_ptr
)->cnf
.proxy_arp
= 0;
862 if ((err
= ip_route_output(&rt
, ip
, 0, RTO_ONLINK
, 0)) != 0)
871 neigh
= __neigh_lookup(&arp_tbl
, &ip
, dev
, 0);
873 if (neigh
->nud_state
&~NUD_NOARP
)
874 err
= neigh_update(neigh
, NULL
, NUD_FAILED
, 1, 0);
875 neigh_release(neigh
);
882 * Handle an ARP layer I/O control request.
885 int arp_ioctl(unsigned int cmd
, void *arg
)
889 struct device
* dev
= NULL
;
894 if (!capable(CAP_NET_ADMIN
))
897 err
= copy_from_user(&r
, arg
, sizeof(struct arpreq
));
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
)))
911 if (!(r
.arp_flags
& ATF_NETMASK
))
912 ((struct sockaddr_in
*)&r
.arp_netmask
)->sin_addr
.s_addr
=__constant_htonl(0xFFFFFFFFUL
);
917 if ((dev
= dev_get(r
.arp_dev
)) == NULL
)
920 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
921 if (!r
.arp_ha
.sa_family
)
922 r
.arp_ha
.sa_family
= dev
->type
;
924 if ((r
.arp_flags
& ATF_COM
) && r
.arp_ha
.sa_family
!= dev
->type
)
926 } else if (cmd
== SIOCGARP
) {
933 err
= arp_req_delete(&r
, dev
);
936 err
= arp_req_set(&r
, dev
);
939 err
= arp_req_get(&r
, dev
);
940 if (!err
&& copy_to_user(arg
, &r
, sizeof(r
)))
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
)
961 char hbuffer
[HBUFFERLEN
];
963 const char hexbuf
[] = "0123456789ABCDEF";
965 size
= sprintf(buffer
,"IP address HW type Flags HW address Mask Device\n");
970 neigh_table_lock(&arp_tbl
);
972 for(i
=0; i
<=NEIGH_HASHMASK
; i
++) {
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
))
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
));
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 ];
1002 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1006 if ((neigh
->nud_state
&NUD_VALID
) && dev
->addr_len
) {
1008 for (j
=0; j
< dev
->addr_len
; j
++)
1009 sprintf(hbuffer
+2*j
, "%02x", neigh
->ha
[j
]);
1011 sprintf(hbuffer
, "0");
1014 size
= sprintf(buffer
+len
,
1015 "%-17s0x%-10x0x%-10x%s",
1016 in_ntoa(*(u32
*)n
->primary_key
),
1018 arp_state_to_flags(n
),
1020 size
+= sprintf(buffer
+len
+size
,
1029 if (pos
>= offset
+length
)
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
),
1045 "00:00:00:00:00:00");
1046 size
+= sprintf(buffer
+len
+size
,
1048 "*", dev
? dev
->name
: "*");
1055 if (pos
>= offset
+length
)
1061 neigh_table_unlock(&arp_tbl
);
1063 *start
= buffer
+len
-(pos
-offset
); /* Start of wanted data */
1064 len
= pos
-offset
; /* Start slop */
1066 len
= length
; /* Ending slop */
1073 /* Note, that it is not on notifier chain.
1074 It is necessary, that this routine was called after route cache will be
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 */
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
,
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
);
1114 #ifdef CONFIG_SYSCTL
1115 neigh_sysctl_register(NULL
, &arp_tbl
.parms
, NET_IPV4
, NET_IPV4_NEIGH
, "ipv4");
1120 #ifdef CONFIG_AX25_MODULE
1123 * ax25 -> ASCII conversion
1125 char *ax2asc(ax25_address
*a
)
1127 static char buf
[11];
1131 for (n
= 0, s
= buf
; n
< 6; n
++) {
1132 c
= (a
->ax25_call
[n
] >> 1) & 0x7F;
1134 if (c
!= ' ') *s
++ = c
;
1139 if ((n
= ((a
->ax25_call
[6] >> 1) & 0x0F)) > 9) {
1147 if (*buf
== '\0' || *buf
== '-')