1 /* linux/net/inet/arp.c
3 * Version: $Id: arp.c,v 1.84 2000/01/18 08:24:14 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>
118 #ifdef CONFIG_ATM_CLIP
119 #include <net/atmclip.h>
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
);
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
=
145 neigh_resolve_output
,
146 neigh_connected_output
,
151 static struct neigh_ops arp_hh_ops
=
157 neigh_resolve_output
,
158 neigh_resolve_output
,
163 static struct neigh_ops arp_direct_ops
=
175 struct neigh_ops arp_broken_ops
=
187 struct neigh_table arp_tbl
=
191 sizeof(struct neighbour
) + 4,
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
)
210 ip_eth_mc_map(addr
, haddr
) ;
212 case ARPHRD_IEEE802_TR
:
213 ip_tr_mc_map(addr
, haddr
) ;
217 memcpy(haddr
, dev
->broadcast
, dev
->addr_len
);
225 static u32
arp_hash(const void *pkey
, const struct net_device
*dev
)
229 hash_val
= *(u32
*)pkey
;
230 hash_val
^= (hash_val
>>16);
231 hash_val
^= hash_val
>>8;
232 hash_val
^= hash_val
>>3;
233 hash_val
= (hash_val
^dev
->ifindex
)&NEIGH_HASHMASK
;
238 static int arp_constructor(struct neighbour
*neigh
)
240 u32 addr
= *(u32
*)neigh
->primary_key
;
241 struct net_device
*dev
= neigh
->dev
;
242 struct in_device
*in_dev
= in_dev_get(dev
);
247 neigh
->type
= inet_addr_type(addr
);
248 if (in_dev
->arp_parms
)
249 neigh
->parms
= in_dev
->arp_parms
;
253 if (dev
->hard_header
== NULL
) {
254 neigh
->nud_state
= NUD_NOARP
;
255 neigh
->ops
= &arp_direct_ops
;
256 neigh
->output
= neigh
->ops
->queue_xmit
;
258 /* Good devices (checked by reading texts, but only Ethernet is
261 ARPHRD_ETHER: (ethernet, apfddi)
264 ARPHRD_METRICOM: (strip)
268 ARPHRD_IPDDP will also work, if author repairs it.
269 I did not it, because this driver does not work even
274 /* So... these "amateur" devices are hopeless.
275 The only thing, that I can say now:
276 It is very sad that we need to keep ugly obsolete
277 code to make them happy.
279 They should be moved to more reasonable state, now
280 they use rebuild_header INSTEAD OF hard_start_xmit!!!
281 Besides that, they are sort of out of date
282 (a lot of redundant clones/copies, useless in 2.1),
283 I wonder why people believe that they work.
289 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
291 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
294 neigh
->ops
= &arp_broken_ops
;
295 neigh
->output
= neigh
->ops
->output
;
300 if (neigh
->type
== RTN_MULTICAST
) {
301 neigh
->nud_state
= NUD_NOARP
;
302 arp_mc_map(addr
, neigh
->ha
, dev
, 1);
303 } else if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
)) {
304 neigh
->nud_state
= NUD_NOARP
;
305 memcpy(neigh
->ha
, dev
->dev_addr
, dev
->addr_len
);
306 } else if (neigh
->type
== RTN_BROADCAST
|| dev
->flags
&IFF_POINTOPOINT
) {
307 neigh
->nud_state
= NUD_NOARP
;
308 memcpy(neigh
->ha
, dev
->broadcast
, dev
->addr_len
);
310 if (dev
->hard_header_cache
)
311 neigh
->ops
= &arp_hh_ops
;
313 neigh
->ops
= &arp_generic_ops
;
314 if (neigh
->nud_state
&NUD_VALID
)
315 neigh
->output
= neigh
->ops
->connected_output
;
317 neigh
->output
= neigh
->ops
->output
;
322 static void arp_error_report(struct neighbour
*neigh
, struct sk_buff
*skb
)
324 dst_link_failure(skb
);
328 static void arp_solicit(struct neighbour
*neigh
, struct sk_buff
*skb
)
332 struct net_device
*dev
= neigh
->dev
;
333 u32 target
= *(u32
*)neigh
->primary_key
;
334 int probes
= atomic_read(&neigh
->probes
);
336 if (skb
&& inet_addr_type(skb
->nh
.iph
->saddr
) == RTN_LOCAL
)
337 saddr
= skb
->nh
.iph
->saddr
;
339 saddr
= inet_select_addr(dev
, target
, RT_SCOPE_LINK
);
341 if ((probes
-= neigh
->parms
->ucast_probes
) < 0) {
342 if (!(neigh
->nud_state
&NUD_VALID
))
343 printk(KERN_DEBUG
"trying to ucast probe in NUD_INVALID\n");
345 read_lock_bh(&neigh
->lock
);
346 } else if ((probes
-= neigh
->parms
->app_probes
) < 0) {
353 arp_send(ARPOP_REQUEST
, ETH_P_ARP
, target
, dev
, saddr
,
354 dst_ha
, dev
->dev_addr
, NULL
);
356 read_unlock_bh(&neigh
->lock
);
359 /* OBSOLETE FUNCTIONS */
362 * Find an arp mapping in the cache. If not found, post a request.
364 * It is very UGLY routine: it DOES NOT use skb->dst->neighbour,
365 * even if it exists. It is supposed that skb->dev was mangled
366 * by a virtual device (eql, shaper). Nobody but broken devices
367 * is allowed to use this function, it is scheduled to be removed. --ANK
370 static int arp_set_predefined(int addr_hint
, unsigned char * haddr
, u32 paddr
, struct net_device
* dev
)
374 printk(KERN_DEBUG
"ARP: arp called for own IP address\n");
375 memcpy(haddr
, dev
->dev_addr
, dev
->addr_len
);
378 arp_mc_map(paddr
, haddr
, dev
, 1);
381 memcpy(haddr
, dev
->broadcast
, dev
->addr_len
);
388 int arp_find(unsigned char *haddr
, struct sk_buff
*skb
)
390 struct net_device
*dev
= skb
->dev
;
395 printk(KERN_DEBUG
"arp_find is called with dst==NULL\n");
400 paddr
= ((struct rtable
*)skb
->dst
)->rt_gateway
;
402 if (arp_set_predefined(inet_addr_type(paddr
), haddr
, paddr
, dev
))
405 n
= __neigh_lookup(&arp_tbl
, &paddr
, dev
, 1);
409 if (n
->nud_state
&NUD_VALID
|| neigh_event_send(n
, skb
) == 0) {
410 read_lock_bh(&n
->lock
);
411 memcpy(haddr
, n
->ha
, dev
->addr_len
);
412 read_unlock_bh(&n
->lock
);
422 /* END OF OBSOLETE FUNCTIONS */
424 int arp_bind_neighbour(struct dst_entry
*dst
)
426 struct net_device
*dev
= dst
->dev
;
430 if (dst
->neighbour
== NULL
) {
431 u32 nexthop
= ((struct rtable
*)dst
)->rt_gateway
;
432 if (dev
->flags
&(IFF_LOOPBACK
|IFF_POINTOPOINT
))
434 dst
->neighbour
= __neigh_lookup(
435 #ifdef CONFIG_ATM_CLIP
436 dev
->type
== ARPHRD_ATM
? &clip_tbl
:
438 &arp_tbl
, &nexthop
, dev
, 1);
440 return (dst
->neighbour
!= NULL
);
444 * Interface to link layer: send routine and receive handler.
448 * Create and send an arp packet. If (dest_hw == NULL), we create a broadcast
452 void arp_send(int type
, int ptype
, u32 dest_ip
,
453 struct net_device
*dev
, u32 src_ip
,
454 unsigned char *dest_hw
, unsigned char *src_hw
,
455 unsigned char *target_hw
)
459 unsigned char *arp_ptr
;
462 * No arp on this interface.
465 if (dev
->flags
&IFF_NOARP
)
472 skb
= alloc_skb(sizeof(struct arphdr
)+ 2*(dev
->addr_len
+4)
473 + dev
->hard_header_len
+ 15, GFP_ATOMIC
);
477 skb_reserve(skb
, (dev
->hard_header_len
+15)&~15);
478 skb
->nh
.raw
= skb
->data
;
479 arp
= (struct arphdr
*) skb_put(skb
,sizeof(struct arphdr
) + 2*(dev
->addr_len
+4));
481 skb
->protocol
= __constant_htons (ETH_P_ARP
);
483 src_hw
= dev
->dev_addr
;
485 dest_hw
= dev
->broadcast
;
488 * Fill the device header for the ARP frame
490 if (dev
->hard_header
&&
491 dev
->hard_header(skb
,dev
,ptype
,dest_hw
,src_hw
,skb
->len
) < 0)
495 * Fill out the arp protocol part.
497 * The arp hardware type should match the device type, except for FDDI,
498 * which (according to RFC 1390) should always equal 1 (Ethernet).
501 * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
502 * DIX code for the protocol. Make these device structure fields.
506 arp
->ar_hrd
= htons(dev
->type
);
507 arp
->ar_pro
= __constant_htons(ETH_P_IP
);
510 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
512 arp
->ar_hrd
= __constant_htons(ARPHRD_AX25
);
513 arp
->ar_pro
= __constant_htons(AX25_P_IP
);
516 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
518 arp
->ar_hrd
= __constant_htons(ARPHRD_NETROM
);
519 arp
->ar_pro
= __constant_htons(AX25_P_IP
);
526 arp
->ar_hrd
= __constant_htons(ARPHRD_ETHER
);
527 arp
->ar_pro
= __constant_htons(ETH_P_IP
);
531 case ARPHRD_IEEE802_TR
:
532 arp
->ar_hrd
= __constant_htons(ARPHRD_IEEE802
);
533 arp
->ar_pro
= __constant_htons(ETH_P_IP
);
538 arp
->ar_hln
= dev
->addr_len
;
540 arp
->ar_op
= htons(type
);
542 arp_ptr
=(unsigned char *)(arp
+1);
544 memcpy(arp_ptr
, src_hw
, dev
->addr_len
);
545 arp_ptr
+=dev
->addr_len
;
546 memcpy(arp_ptr
, &src_ip
,4);
548 if (target_hw
!= NULL
)
549 memcpy(arp_ptr
, target_hw
, dev
->addr_len
);
551 memset(arp_ptr
, 0, dev
->addr_len
);
552 arp_ptr
+=dev
->addr_len
;
553 memcpy(arp_ptr
, &dest_ip
, 4);
563 static void parp_redo(struct sk_buff
*skb
)
565 arp_rcv(skb
, skb
->dev
, NULL
);
569 * Receive an arp request by the device layer.
572 int arp_rcv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
*pt
)
574 struct arphdr
*arp
= skb
->nh
.arph
;
575 unsigned char *arp_ptr
= (unsigned char *)(arp
+1);
577 unsigned char *sha
, *tha
;
579 u16 dev_type
= dev
->type
;
581 struct in_device
*in_dev
= in_dev_get(dev
);
585 * The hardware length of the packet should match the hardware length
586 * of the device. Similarly, the hardware types should match. The
587 * device should be ARP-able. Also, if pln is not 4, then the lookup
588 * is not from an IP number. We can't currently handle this, so toss
591 if (in_dev
== NULL
||
592 arp
->ar_hln
!= dev
->addr_len
||
593 dev
->flags
& IFF_NOARP
||
594 skb
->pkt_type
== PACKET_OTHERHOST
||
595 skb
->pkt_type
== PACKET_LOOPBACK
||
599 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)
604 if (arp
->ar_pro
!= __constant_htons(ETH_P_IP
))
606 if (htons(dev_type
) != arp
->ar_hrd
)
609 #ifdef CONFIG_NET_ETHERNET
612 * ETHERNET devices will accept ARP hardware types of either
613 * 1 (Ethernet) or 6 (IEEE 802.2).
615 if (arp
->ar_hrd
!= __constant_htons(ARPHRD_ETHER
) &&
616 arp
->ar_hrd
!= __constant_htons(ARPHRD_IEEE802
))
618 if (arp
->ar_pro
!= __constant_htons(ETH_P_IP
))
623 case ARPHRD_IEEE802_TR
:
625 * Token ring devices will accept ARP hardware types of either
626 * 1 (Ethernet) or 6 (IEEE 802.2).
628 if (arp
->ar_hrd
!= __constant_htons(ARPHRD_ETHER
) &&
629 arp
->ar_hrd
!= __constant_htons(ARPHRD_IEEE802
))
631 if (arp
->ar_pro
!= __constant_htons(ETH_P_IP
))
638 * According to RFC 1390, FDDI devices should accept ARP hardware types
639 * of 1 (Ethernet). However, to be more robust, we'll accept hardware
640 * types of either 1 (Ethernet) or 6 (IEEE 802.2).
642 if (arp
->ar_hrd
!= __constant_htons(ARPHRD_ETHER
) &&
643 arp
->ar_hrd
!= __constant_htons(ARPHRD_IEEE802
))
645 if (arp
->ar_pro
!= __constant_htons(ETH_P_IP
))
649 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
651 if (arp
->ar_pro
!= __constant_htons(AX25_P_IP
))
653 if (arp
->ar_hrd
!= __constant_htons(ARPHRD_AX25
))
656 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
658 if (arp
->ar_pro
!= __constant_htons(AX25_P_IP
))
660 if (arp
->ar_hrd
!= __constant_htons(ARPHRD_NETROM
))
667 /* Understand only these message types */
669 if (arp
->ar_op
!= __constant_htons(ARPOP_REPLY
) &&
670 arp
->ar_op
!= __constant_htons(ARPOP_REQUEST
))
677 arp_ptr
+= dev
->addr_len
;
678 memcpy(&sip
, arp_ptr
, 4);
681 arp_ptr
+= dev
->addr_len
;
682 memcpy(&tip
, arp_ptr
, 4);
684 * Check for bad requests for 127.x.x.x and requests for multicast
685 * addresses. If this is one such, delete it.
687 if (LOOPBACK(tip
) || MULTICAST(tip
))
691 * Process entry. The idea here is we want to send a reply if it is a
692 * request for us or if it is a request for someone else that we hold
693 * a proxy for. We want to add an entry to our cache if it is a reply
694 * to us or if it is a request for our address.
695 * (The assumption for this last is that if someone is requesting our
696 * address, they are probably intending to talk to us, so it saves time
697 * if we cache their address. Their address is also probably not in
698 * our cache, since ours is not in their cache.)
700 * Putting this another way, we only care about replies if they are to
701 * us, in which case we add them to the cache. For requests, we care
702 * about those for us and those for our proxies. We reply to both,
703 * and in the case of requests for us we add the requester to the arp
707 /* Special case: IPv4 duplicate address detection packet (RFC2131) */
709 if (arp
->ar_op
== __constant_htons(ARPOP_REQUEST
) &&
710 inet_addr_type(tip
) == RTN_LOCAL
)
711 arp_send(ARPOP_REPLY
,ETH_P_ARP
,tip
,dev
,tip
,sha
,dev
->dev_addr
,dev
->dev_addr
);
715 if (arp
->ar_op
== __constant_htons(ARPOP_REQUEST
) &&
716 ip_route_input(skb
, tip
, sip
, 0, dev
) == 0) {
718 rt
= (struct rtable
*)skb
->dst
;
719 addr_type
= rt
->rt_type
;
721 if (addr_type
== RTN_LOCAL
) {
722 n
= neigh_event_ns(&arp_tbl
, sha
, &sip
, dev
);
724 arp_send(ARPOP_REPLY
,ETH_P_ARP
,sip
,dev
,tip
,sha
,dev
->dev_addr
,sha
);
728 } else if (IN_DEV_FORWARD(in_dev
)) {
729 if ((rt
->rt_flags
&RTCF_DNAT
) ||
730 (addr_type
== RTN_UNICAST
&& rt
->u
.dst
.dev
!= dev
&&
731 (IN_DEV_PROXY_ARP(in_dev
) || pneigh_lookup(&arp_tbl
, &tip
, dev
, 0)))) {
732 n
= neigh_event_ns(&arp_tbl
, sha
, &sip
, dev
);
736 if (skb
->stamp
.tv_sec
== 0 ||
737 skb
->pkt_type
== PACKET_HOST
||
738 in_dev
->arp_parms
->proxy_delay
== 0) {
739 arp_send(ARPOP_REPLY
,ETH_P_ARP
,sip
,dev
,tip
,sha
,dev
->dev_addr
,sha
);
741 pneigh_enqueue(&arp_tbl
, in_dev
->arp_parms
, skb
);
750 /* Update our ARP tables */
752 n
= __neigh_lookup(&arp_tbl
, &sip
, dev
, 0);
754 #ifdef CONFIG_IP_ACCEPT_UNSOLICITED_ARP
755 /* Unsolicited ARP is not accepted by default.
756 It is possible, that this option should be enabled for some
757 devices (strip is candidate)
760 arp
->ar_op
== __constant_htons(ARPOP_REPLY
) &&
761 inet_addr_type(sip
) == RTN_UNICAST
)
762 n
= __neigh_lookup(&arp_tbl
, &sip
, dev
, -1);
766 int state
= NUD_REACHABLE
;
769 /* If several different ARP replies follows back-to-back,
770 use the FIRST one. It is possible, if several proxy
771 agents are active. Taking the first reply prevents
772 arp trashing and chooses the fastest router.
774 if (jiffies
- n
->updated
>= n
->parms
->locktime
)
777 /* Broadcast replies and request packets
778 do not assert neighbour reachability.
780 if (arp
->ar_op
!= __constant_htons(ARPOP_REPLY
) ||
781 skb
->pkt_type
!= PACKET_HOST
)
783 neigh_update(n
, sha
, state
, override
, 1);
798 * User level interface (ioctl, /proc)
802 * Set (create) an ARP cache entry.
805 int arp_req_set(struct arpreq
*r
, struct net_device
* dev
)
807 u32 ip
= ((struct sockaddr_in
*) &r
->arp_pa
)->sin_addr
.s_addr
;
808 struct neighbour
*neigh
;
811 if (r
->arp_flags
&ATF_PUBL
) {
812 u32 mask
= ((struct sockaddr_in
*) &r
->arp_netmask
)->sin_addr
.s_addr
;
813 if (mask
&& mask
!= 0xFFFFFFFF)
815 if (!dev
&& (r
->arp_flags
& ATF_COM
)) {
816 dev
= dev_getbyhwaddr(r
->arp_ha
.sa_family
, r
->arp_ha
.sa_data
);
821 if (pneigh_lookup(&arp_tbl
, &ip
, dev
, 1) == NULL
)
826 ipv4_devconf
.proxy_arp
= 1;
829 if (__in_dev_get(dev
)) {
830 __in_dev_get(dev
)->cnf
.proxy_arp
= 1;
836 if (r
->arp_flags
& ATF_PERM
)
837 r
->arp_flags
|= ATF_COM
;
840 if ((err
= ip_route_output(&rt
, ip
, 0, RTO_ONLINK
, 0)) != 0)
847 if (r
->arp_ha
.sa_family
!= dev
->type
)
851 neigh
= __neigh_lookup(&arp_tbl
, &ip
, dev
, 1);
853 unsigned state
= NUD_STALE
;
854 if (r
->arp_flags
& ATF_PERM
)
855 state
= NUD_PERMANENT
;
856 err
= neigh_update(neigh
, (r
->arp_flags
&ATF_COM
) ?
857 r
->arp_ha
.sa_data
: NULL
, state
, 1, 0);
858 neigh_release(neigh
);
863 static unsigned arp_state_to_flags(struct neighbour
*neigh
)
866 if (neigh
->nud_state
&NUD_PERMANENT
)
867 flags
= ATF_PERM
|ATF_COM
;
868 else if (neigh
->nud_state
&NUD_VALID
)
874 * Get an ARP cache entry.
877 static int arp_req_get(struct arpreq
*r
, struct net_device
*dev
)
879 u32 ip
= ((struct sockaddr_in
*) &r
->arp_pa
)->sin_addr
.s_addr
;
880 struct neighbour
*neigh
;
883 neigh
= neigh_lookup(&arp_tbl
, &ip
, dev
);
885 read_lock_bh(&neigh
->lock
);
886 memcpy(r
->arp_ha
.sa_data
, neigh
->ha
, dev
->addr_len
);
887 r
->arp_flags
= arp_state_to_flags(neigh
);
888 read_unlock_bh(&neigh
->lock
);
889 r
->arp_ha
.sa_family
= dev
->type
;
890 strncpy(r
->arp_dev
, dev
->name
, sizeof(r
->arp_dev
));
891 neigh_release(neigh
);
897 int arp_req_delete(struct arpreq
*r
, struct net_device
* dev
)
900 u32 ip
= ((struct sockaddr_in
*)&r
->arp_pa
)->sin_addr
.s_addr
;
901 struct neighbour
*neigh
;
903 if (r
->arp_flags
& ATF_PUBL
) {
904 u32 mask
= ((struct sockaddr_in
*) &r
->arp_netmask
)->sin_addr
.s_addr
;
905 if (mask
== 0xFFFFFFFF)
906 return pneigh_delete(&arp_tbl
, &ip
, dev
);
909 ipv4_devconf
.proxy_arp
= 0;
912 if (__in_dev_get(dev
)) {
913 __in_dev_get(dev
)->cnf
.proxy_arp
= 0;
923 if ((err
= ip_route_output(&rt
, ip
, 0, RTO_ONLINK
, 0)) != 0)
931 neigh
= neigh_lookup(&arp_tbl
, &ip
, dev
);
933 if (neigh
->nud_state
&~NUD_NOARP
)
934 err
= neigh_update(neigh
, NULL
, NUD_FAILED
, 1, 0);
935 neigh_release(neigh
);
941 * Handle an ARP layer I/O control request.
944 int arp_ioctl(unsigned int cmd
, void *arg
)
948 struct net_device
* dev
= NULL
;
953 if (!capable(CAP_NET_ADMIN
))
956 err
= copy_from_user(&r
, arg
, sizeof(struct arpreq
));
964 if (r
.arp_pa
.sa_family
!= AF_INET
)
965 return -EPFNOSUPPORT
;
967 if (!(r
.arp_flags
& ATF_PUBL
) &&
968 (r
.arp_flags
& (ATF_NETMASK
|ATF_DONTPUB
)))
970 if (!(r
.arp_flags
& ATF_NETMASK
))
971 ((struct sockaddr_in
*)&r
.arp_netmask
)->sin_addr
.s_addr
=__constant_htonl(0xFFFFFFFFUL
);
976 if ((dev
= __dev_get_by_name(r
.arp_dev
)) == NULL
)
979 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
980 if (!r
.arp_ha
.sa_family
)
981 r
.arp_ha
.sa_family
= dev
->type
;
983 if ((r
.arp_flags
& ATF_COM
) && r
.arp_ha
.sa_family
!= dev
->type
)
985 } else if (cmd
== SIOCGARP
) {
992 err
= arp_req_delete(&r
, dev
);
995 err
= arp_req_set(&r
, dev
);
998 err
= arp_req_get(&r
, dev
);
999 if (!err
&& copy_to_user(arg
, &r
, sizeof(r
)))
1009 * Write the contents of the ARP cache to a PROCfs file.
1011 #ifndef CONFIG_PROC_FS
1012 static int arp_get_info(char *buffer
, char **start
, off_t offset
, int length
) { return 0; }
1014 #define HBUFFERLEN 30
1016 static int arp_get_info(char *buffer
, char **start
, off_t offset
, int length
)
1021 char hbuffer
[HBUFFERLEN
];
1023 const char hexbuf
[] = "0123456789ABCDEF";
1026 size
= sprintf(buffer
,"IP address HW type Flags HW address Mask Device\n");
1031 for(i
=0; i
<=NEIGH_HASHMASK
; i
++) {
1032 struct neighbour
*n
;
1033 read_lock_bh(&arp_tbl
.lock
);
1034 for (n
=arp_tbl
.hash_buckets
[i
]; n
; n
=n
->next
) {
1035 struct net_device
*dev
= n
->dev
;
1036 int hatype
= dev
->type
;
1038 /* Do not confuse users "arp -a" with magic entries */
1039 if (!(n
->nud_state
&~NUD_NOARP
))
1042 read_lock(&n
->lock
);
1045 * Convert hardware address to XX:XX:XX:XX ... form.
1047 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1048 if (hatype
== ARPHRD_AX25
|| hatype
== ARPHRD_NETROM
)
1049 ax2asc2((ax25_address
*)n
->ha
, hbuffer
);
1052 for (k
=0,j
=0;k
<HBUFFERLEN
-3 && j
<dev
->addr_len
;j
++) {
1053 hbuffer
[k
++]=hexbuf
[(n
->ha
[j
]>>4)&15 ];
1054 hbuffer
[k
++]=hexbuf
[n
->ha
[j
]&15 ];
1059 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1063 size
= sprintf(buffer
+len
,
1064 "%-17s0x%-10x0x%-10x%s",
1065 in_ntoa2(*(u32
*)n
->primary_key
, abuf
),
1067 arp_state_to_flags(n
),
1069 size
+= sprintf(buffer
+len
+size
,
1072 read_unlock(&n
->lock
);
1079 if (pos
>= offset
+length
) {
1080 read_unlock_bh(&arp_tbl
.lock
);
1084 read_unlock_bh(&arp_tbl
.lock
);
1087 for (i
=0; i
<=PNEIGH_HASHMASK
; i
++) {
1088 struct pneigh_entry
*n
;
1089 for (n
=arp_tbl
.phash_buckets
[i
]; n
; n
=n
->next
) {
1090 struct net_device
*dev
= n
->dev
;
1091 int hatype
= dev
? dev
->type
: 0;
1093 size
= sprintf(buffer
+len
,
1094 "%-17s0x%-10x0x%-10x%s",
1095 in_ntoa2(*(u32
*)n
->key
, abuf
),
1098 "00:00:00:00:00:00");
1099 size
+= sprintf(buffer
+len
+size
,
1101 "*", dev
? dev
->name
: "*");
1108 if (pos
>= offset
+length
)
1115 *start
= buffer
+len
-(pos
-offset
); /* Start of wanted data */
1116 len
= pos
-offset
; /* Start slop */
1118 len
= length
; /* Ending slop */
1125 /* Note, that it is not on notifier chain.
1126 It is necessary, that this routine was called after route cache will be
1129 void arp_ifdown(struct net_device
*dev
)
1131 neigh_ifdown(&arp_tbl
, dev
);
1136 * Called once on startup.
1139 static struct packet_type arp_packet_type
=
1141 __constant_htons(ETH_P_ARP
),
1142 NULL
, /* All devices */
1148 void __init
arp_init (void)
1150 neigh_table_init(&arp_tbl
);
1152 dev_add_pack(&arp_packet_type
);
1154 proc_net_create ("arp", 0, arp_get_info
);
1156 #ifdef CONFIG_SYSCTL
1157 neigh_sysctl_register(NULL
, &arp_tbl
.parms
, NET_IPV4
, NET_IPV4_NEIGH
, "ipv4");
1162 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1165 * ax25 -> ASCII conversion
1167 char *ax2asc2(ax25_address
*a
, char *buf
)
1172 for (n
= 0, s
= buf
; n
< 6; n
++) {
1173 c
= (a
->ax25_call
[n
] >> 1) & 0x7F;
1175 if (c
!= ' ') *s
++ = c
;
1180 if ((n
= ((a
->ax25_call
[6] >> 1) & 0x0F)) > 9) {
1188 if (*buf
== '\0' || *buf
== '-')