Main Page | Class List | File List | Class Members | File Members

arp.c

Go to the documentation of this file.
00001 /* linux/net/inet/arp.c 00002 * 00003 * Version: $Id: arp.c,v 1.99 2001/08/30 22:55:42 davem Exp $ 00004 * 00005 * Copyright (C) 1994 by Florian La Roche 00006 * 00007 * This module implements the Address Resolution Protocol ARP (RFC 826), 00008 * which is used to convert IP addresses (or in the future maybe other 00009 * high-level addresses) into a low-level hardware address (like an Ethernet 00010 * address). 00011 * 00012 * This program is free software; you can redistribute it and/or 00013 * modify it under the terms of the GNU General Public License 00014 * as published by the Free Software Foundation; either version 00015 * 2 of the License, or (at your option) any later version. 00016 * 00017 * Fixes: 00018 * Alan Cox : Removed the Ethernet assumptions in 00019 * Florian's code 00020 * Alan Cox : Fixed some small errors in the ARP 00021 * logic 00022 * Alan Cox : Allow >4K in /proc 00023 * Alan Cox : Make ARP add its own protocol entry 00024 * Ross Martin : Rewrote arp_rcv() and arp_get_info() 00025 * Stephen Henson : Add AX25 support to arp_get_info() 00026 * Alan Cox : Drop data when a device is downed. 00027 * Alan Cox : Use init_timer(). 00028 * Alan Cox : Double lock fixes. 00029 * Martin Seine : Move the arphdr structure 00030 * to if_arp.h for compatibility. 00031 * with BSD based programs. 00032 * Andrew Tridgell : Added ARP netmask code and 00033 * re-arranged proxy handling. 00034 * Alan Cox : Changed to use notifiers. 00035 * Niibe Yutaka : Reply for this device or proxies only. 00036 * Alan Cox : Don't proxy across hardware types! 00037 * Jonathan Naylor : Added support for NET/ROM. 00038 * Mike Shaver : RFC1122 checks. 00039 * Jonathan Naylor : Only lookup the hardware address for 00040 * the correct hardware type. 00041 * Germano Caronni : Assorted subtle races. 00042 * Craig Schlenter : Don't modify permanent entry 00043 * during arp_rcv. 00044 * Russ Nelson : Tidied up a few bits. 00045 * Alexey Kuznetsov: Major changes to caching and behaviour, 00046 * eg intelligent arp probing and 00047 * generation 00048 * of host down events. 00049 * Alan Cox : Missing unlock in device events. 00050 * Eckes : ARP ioctl control errors. 00051 * Alexey Kuznetsov: Arp free fix. 00052 * Manuel Rodriguez: Gratuitous ARP. 00053 * Jonathan Layes : Added arpd support through kerneld 00054 * message queue (960314) 00055 * Mike Shaver : /proc/sys/net/ipv4/arp_* support 00056 * Mike McLagan : Routing by source 00057 * Stuart Cheshire : Metricom and grat arp fixes 00058 * *** FOR 2.1 clean this up *** 00059 * Lawrence V. Stefani: (08/12/96) Added FDDI support. 00060 * Alan Cox : Took the AP1000 nasty FDDI hack and 00061 * folded into the mainstream FDDI code. 00062 * Ack spit, Linus how did you allow that 00063 * one in... 00064 * Jes Sorensen : Make FDDI work again in 2.1.x and 00065 * clean up the APFDDI & gen. FDDI bits. 00066 * Alexey Kuznetsov: new arp state machine; 00067 * now it is in net/core/neighbour.c. 00068 * Krzysztof Halasa: Added Frame Relay ARP support. 00069 */ 00070 00071 #include <linux/types.h> 00072 #include <linux/string.h> 00073 #include <linux/kernel.h> 00074 #include <linux/sched.h> 00075 #include <linux/config.h> 00076 #include <linux/socket.h> 00077 #include <linux/sockios.h> 00078 #include <linux/errno.h> 00079 #include <linux/in.h> 00080 #include <linux/mm.h> 00081 #include <linux/inet.h> 00082 #include <linux/netdevice.h> 00083 #include <linux/etherdevice.h> 00084 #include <linux/fddidevice.h> 00085 #include <linux/if_arp.h> 00086 #include <linux/trdevice.h> 00087 #include <linux/skbuff.h> 00088 #include <linux/proc_fs.h> 00089 #include <linux/stat.h> 00090 #include <linux/init.h> 00091 #ifdef CONFIG_SYSCTL 00092 #include <linux/sysctl.h> 00093 #endif 00094 00095 #include <net/ip.h> 00096 #include <net/icmp.h> 00097 #include <net/route.h> 00098 #include <net/protocol.h> 00099 #include <net/tcp.h> 00100 #include <net/sock.h> 00101 #include <net/arp.h> 00102 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 00103 #include <net/ax25.h> 00104 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE) 00105 #include <net/netrom.h> 00106 #endif 00107 #endif 00108 #if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE) 00109 #include <net/atmclip.h> 00110 struct neigh_table *clip_tbl_hook; 00111 #endif 00112 00113 #include <asm/system.h> 00114 #include <asm/uaccess.h> 00115 00116 #include <linux/netfilter_arp.h> 00117 00118 /* 00119 * Interface to generic neighbour cache. 00120 */ 00121 static u32 arp_hash(const void *pkey, const struct net_device *dev); 00122 static int arp_constructor(struct neighbour *neigh); 00123 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb); 00124 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb); 00125 static void parp_redo(struct sk_buff *skb); 00126 00127 static struct neigh_ops arp_generic_ops = { 00128 family: AF_INET, 00129 solicit: arp_solicit, 00130 error_report: arp_error_report, 00131 output: neigh_resolve_output, 00132 connected_output: neigh_connected_output, 00133 hh_output: dev_queue_xmit, 00134 queue_xmit: dev_queue_xmit, 00135 }; 00136 00137 static struct neigh_ops arp_hh_ops = { 00138 family: AF_INET, 00139 solicit: arp_solicit, 00140 error_report: arp_error_report, 00141 output: neigh_resolve_output, 00142 connected_output: neigh_resolve_output, 00143 hh_output: dev_queue_xmit, 00144 queue_xmit: dev_queue_xmit, 00145 }; 00146 00147 static struct neigh_ops arp_direct_ops = { 00148 family: AF_INET, 00149 output: dev_queue_xmit, 00150 connected_output: dev_queue_xmit, 00151 hh_output: dev_queue_xmit, 00152 queue_xmit: dev_queue_xmit, 00153 }; 00154 00155 struct neigh_ops arp_broken_ops = { 00156 family: AF_INET, 00157 solicit: arp_solicit, 00158 error_report: arp_error_report, 00159 output: neigh_compat_output, 00160 connected_output: neigh_compat_output, 00161 hh_output: dev_queue_xmit, 00162 queue_xmit: dev_queue_xmit, 00163 }; 00164 00165 struct neigh_table arp_tbl = { 00166 family: AF_INET, 00167 entry_size: sizeof(struct neighbour) + 4, 00168 key_len: 4, 00169 hash: arp_hash, 00170 constructor: arp_constructor, 00171 proxy_redo: parp_redo, 00172 id: "arp_cache", 00173 parms: { 00174 tbl: &arp_tbl, 00175 base_reachable_time: 30 * HZ, 00176 retrans_time: 1 * HZ, 00177 gc_staletime: 60 * HZ, 00178 reachable_time: 30 * HZ, 00179 delay_probe_time: 5 * HZ, 00180 queue_len: 3, 00181 ucast_probes: 3, 00182 mcast_probes: 3, 00183 anycast_delay: 1 * HZ, 00184 proxy_delay: (8 * HZ) / 10, 00185 proxy_qlen: 64, 00186 locktime: 1 * HZ, 00187 }, 00188 gc_interval: 30 * HZ, 00189 gc_thresh1: 128, 00190 gc_thresh2: 512, 00191 gc_thresh3: 1024, 00192 }; 00193 00194 int arp_mc_map(u32 addr, u8 *haddr, struct net_device *dev, int dir) 00195 { 00196 switch (dev->type) { 00197 case ARPHRD_ETHER: 00198 case ARPHRD_FDDI: 00199 case ARPHRD_IEEE802: 00200 ip_eth_mc_map(addr, haddr); 00201 return 0; 00202 case ARPHRD_IEEE802_TR: 00203 ip_tr_mc_map(addr, haddr); 00204 return 0; 00205 default: 00206 if (dir) { 00207 memcpy(haddr, dev->broadcast, dev->addr_len); 00208 return 0; 00209 } 00210 } 00211 return -EINVAL; 00212 } 00213 00214 00215 static u32 arp_hash(const void *pkey, const struct net_device *dev) 00216 { 00217 u32 hash_val; 00218 00219 hash_val = *(u32*)pkey; 00220 hash_val ^= (hash_val>>16); 00221 hash_val ^= hash_val>>8; 00222 hash_val ^= hash_val>>3; 00223 hash_val = (hash_val^dev->ifindex)&NEIGH_HASHMASK; 00224 00225 return hash_val; 00226 } 00227 00228 static int arp_constructor(struct neighbour *neigh) 00229 { 00230 u32 addr = *(u32*)neigh->primary_key; 00231 struct net_device *dev = neigh->dev; 00232 struct in_device *in_dev = in_dev_get(dev); 00233 00234 if (in_dev == NULL) 00235 return -EINVAL; 00236 00237 neigh->type = inet_addr_type(addr); 00238 if (in_dev->arp_parms) 00239 neigh->parms = in_dev->arp_parms; 00240 00241 in_dev_put(in_dev); 00242 00243 if (dev->hard_header == NULL) { 00244 neigh->nud_state = NUD_NOARP; 00245 neigh->ops = &arp_direct_ops; 00246 neigh->output = neigh->ops->queue_xmit; 00247 } else { 00248 /* Good devices (checked by reading texts, but only Ethernet is 00249 tested) 00250 00251 ARPHRD_ETHER: (ethernet, apfddi) 00252 ARPHRD_FDDI: (fddi) 00253 ARPHRD_IEEE802: (tr) 00254 ARPHRD_METRICOM: (strip) 00255 ARPHRD_ARCNET: 00256 etc. etc. etc. 00257 00258 ARPHRD_IPDDP will also work, if author repairs it. 00259 I did not it, because this driver does not work even 00260 in old paradigm. 00261 */ 00262 00263 #if 1 00264 /* So... these "amateur" devices are hopeless. 00265 The only thing, that I can say now: 00266 It is very sad that we need to keep ugly obsolete 00267 code to make them happy. 00268 00269 They should be moved to more reasonable state, now 00270 they use rebuild_header INSTEAD OF hard_start_xmit!!! 00271 Besides that, they are sort of out of date 00272 (a lot of redundant clones/copies, useless in 2.1), 00273 I wonder why people believe that they work. 00274 */ 00275 switch (dev->type) { 00276 default: 00277 break; 00278 case ARPHRD_ROSE: 00279 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 00280 case ARPHRD_AX25: 00281 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE) 00282 case ARPHRD_NETROM: 00283 #endif 00284 neigh->ops = &arp_broken_ops; 00285 neigh->output = neigh->ops->output; 00286 return 0; 00287 #endif 00288 ;} 00289 #endif 00290 if (neigh->type == RTN_MULTICAST) { 00291 neigh->nud_state = NUD_NOARP; 00292 arp_mc_map(addr, neigh->ha, dev, 1); 00293 } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) { 00294 neigh->nud_state = NUD_NOARP; 00295 memcpy(neigh->ha, dev->dev_addr, dev->addr_len); 00296 } else if (neigh->type == RTN_BROADCAST || dev->flags&IFF_POINTOPOINT) { 00297 neigh->nud_state = NUD_NOARP; 00298 memcpy(neigh->ha, dev->broadcast, dev->addr_len); 00299 } 00300 if (dev->hard_header_cache) 00301 neigh->ops = &arp_hh_ops; 00302 else 00303 neigh->ops = &arp_generic_ops; 00304 if (neigh->nud_state&NUD_VALID) 00305 neigh->output = neigh->ops->connected_output; 00306 else 00307 neigh->output = neigh->ops->output; 00308 } 00309 return 0; 00310 } 00311 00312 static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb) 00313 { 00314 dst_link_failure(skb); 00315 kfree_skb(skb); 00316 } 00317 00318 static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb) 00319 { 00320 u32 saddr; 00321 u8 *dst_ha = NULL; 00322 struct net_device *dev = neigh->dev; 00323 u32 target = *(u32*)neigh->primary_key; 00324 int probes = atomic_read(&neigh->probes); 00325 00326 if (skb && inet_addr_type(skb->nh.iph->saddr) == RTN_LOCAL) 00327 saddr = skb->nh.iph->saddr; 00328 else 00329 saddr = inet_select_addr(dev, target, RT_SCOPE_LINK); 00330 00331 if ((probes -= neigh->parms->ucast_probes) < 0) { 00332 if (!(neigh->nud_state&NUD_VALID)) 00333 printk(KERN_DEBUG "trying to ucast probe in NUD_INVALID\n"); 00334 dst_ha = neigh->ha; 00335 read_lock_bh(&neigh->lock); 00336 } else if ((probes -= neigh->parms->app_probes) < 0) { 00337 #ifdef CONFIG_ARPD 00338 neigh_app_ns(neigh); 00339 #endif 00340 return; 00341 } 00342 00343 arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr, 00344 dst_ha, dev->dev_addr, NULL); 00345 if (dst_ha) 00346 read_unlock_bh(&neigh->lock); 00347 } 00348 00349 static int arp_filter(__u32 sip, __u32 tip, struct net_device *dev) 00350 { 00351 struct rtable *rt; 00352 int flag = 0; 00353 /*unsigned long now; */ 00354 00355 if (ip_route_output(&rt, sip, tip, 0, 0) < 0) 00356 return 1; 00357 if (rt->u.dst.dev != dev) { 00358 NET_INC_STATS_BH(ArpFilter); 00359 flag = 1; 00360 } 00361 ip_rt_put(rt); 00362 return flag; 00363 } 00364 00365 /* OBSOLETE FUNCTIONS */ 00366 00367 /* 00368 * Find an arp mapping in the cache. If not found, post a request. 00369 * 00370 * It is very UGLY routine: it DOES NOT use skb->dst->neighbour, 00371 * even if it exists. It is supposed that skb->dev was mangled 00372 * by a virtual device (eql, shaper). Nobody but broken devices 00373 * is allowed to use this function, it is scheduled to be removed. --ANK 00374 */ 00375 00376 static int arp_set_predefined(int addr_hint, unsigned char * haddr, u32 paddr, struct net_device * dev) 00377 { 00378 switch (addr_hint) { 00379 case RTN_LOCAL: 00380 printk(KERN_DEBUG "ARP: arp called for own IP address\n"); 00381 memcpy(haddr, dev->dev_addr, dev->addr_len); 00382 return 1; 00383 case RTN_MULTICAST: 00384 arp_mc_map(paddr, haddr, dev, 1); 00385 return 1; 00386 case RTN_BROADCAST: 00387 memcpy(haddr, dev->broadcast, dev->addr_len); 00388 return 1; 00389 } 00390 return 0; 00391 } 00392 00393 00394 int arp_find(unsigned char *haddr, struct sk_buff *skb) 00395 { 00396 struct net_device *dev = skb->dev; 00397 u32 paddr; 00398 struct neighbour *n; 00399 00400 if (!skb->dst) { 00401 printk(KERN_DEBUG "arp_find is called with dst==NULL\n"); 00402 kfree_skb(skb); 00403 return 1; 00404 } 00405 00406 paddr = ((struct rtable*)skb->dst)->rt_gateway; 00407 00408 if (arp_set_predefined(inet_addr_type(paddr), haddr, paddr, dev)) 00409 return 0; 00410 00411 n = __neigh_lookup(&arp_tbl, &paddr, dev, 1); 00412 00413 if (n) { 00414 n->used = jiffies; 00415 if (n->nud_state&NUD_VALID || neigh_event_send(n, skb) == 0) { 00416 read_lock_bh(&n->lock); 00417 memcpy(haddr, n->ha, dev->addr_len); 00418 read_unlock_bh(&n->lock); 00419 neigh_release(n); 00420 return 0; 00421 } 00422 neigh_release(n); 00423 } else 00424 kfree_skb(skb); 00425 return 1; 00426 } 00427 00428 /* END OF OBSOLETE FUNCTIONS */ 00429 00430 int arp_bind_neighbour(struct dst_entry *dst) 00431 { 00432 struct net_device *dev = dst->dev; 00433 struct neighbour *n = dst->neighbour; 00434 00435 if (dev == NULL) 00436 return -EINVAL; 00437 if (n == NULL) { 00438 u32 nexthop = ((struct rtable*)dst)->rt_gateway; 00439 if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT)) 00440 nexthop = 0; 00441 n = __neigh_lookup_errno( 00442 #if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE) 00443 dev->type == ARPHRD_ATM ? clip_tbl_hook : 00444 #endif 00445 &arp_tbl, &nexthop, dev); 00446 if (IS_ERR(n)) 00447 return PTR_ERR(n); 00448 dst->neighbour = n; 00449 } 00450 return 0; 00451 } 00452 00453 /* 00454 * Check if we can use proxy ARP for this path 00455 */ 00456 00457 static inline int arp_fwd_proxy(struct in_device *in_dev, struct rtable *rt) 00458 { 00459 struct in_device *out_dev; 00460 int imi, omi = -1; 00461 00462 if (!IN_DEV_PROXY_ARP(in_dev)) 00463 return 0; 00464 00465 if ((imi = IN_DEV_MEDIUM_ID(in_dev)) == 0) 00466 return 1; 00467 if (imi == -1) 00468 return 0; 00469 00470 /* place to check for proxy_arp for routes */ 00471 00472 if ((out_dev = in_dev_get(rt->u.dst.dev)) != NULL) { 00473 omi = IN_DEV_MEDIUM_ID(out_dev); 00474 in_dev_put(out_dev); 00475 } 00476 return (omi != imi && omi != -1); 00477 } 00478 00479 /* 00480 * Interface to link layer: send routine and receive handler. 00481 */ 00482 00483 /* 00484 * Create and send an arp packet. If (dest_hw == NULL), we create a broadcast 00485 * message. 00486 */ 00487 00488 void arp_send(int type, int ptype, u32 dest_ip, 00489 struct net_device *dev, u32 src_ip, 00490 unsigned char *dest_hw, unsigned char *src_hw, 00491 unsigned char *target_hw) 00492 { 00493 struct sk_buff *skb; 00494 struct arphdr *arp; 00495 unsigned char *arp_ptr; 00496 00497 /* 00498 * No arp on this interface. 00499 */ 00500 00501 if (dev->flags&IFF_NOARP) 00502 return; 00503 00504 /* 00505 * Allocate a buffer 00506 */ 00507 00508 skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4) 00509 + dev->hard_header_len + 15, GFP_ATOMIC); 00510 if (skb == NULL) 00511 return; 00512 00513 skb_reserve(skb, (dev->hard_header_len+15)&~15); 00514 skb->nh.raw = skb->data; 00515 arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4)); 00516 skb->dev = dev; 00517 skb->protocol = htons (ETH_P_ARP); 00518 if (src_hw == NULL) 00519 src_hw = dev->dev_addr; 00520 if (dest_hw == NULL) 00521 dest_hw = dev->broadcast; 00522 00523 /* 00524 * Fill the device header for the ARP frame 00525 */ 00526 if (dev->hard_header && 00527 dev->hard_header(skb,dev,ptype,dest_hw,src_hw,skb->len) < 0) 00528 goto out; 00529 00530 /* 00531 * Fill out the arp protocol part. 00532 * 00533 * The arp hardware type should match the device type, except for FDDI, 00534 * which (according to RFC 1390) should always equal 1 (Ethernet). 00535 */ 00536 /* 00537 * Exceptions everywhere. AX.25 uses the AX.25 PID value not the 00538 * DIX code for the protocol. Make these device structure fields. 00539 */ 00540 switch (dev->type) { 00541 default: 00542 arp->ar_hrd = htons(dev->type); 00543 arp->ar_pro = htons(ETH_P_IP); 00544 break; 00545 00546 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 00547 case ARPHRD_AX25: 00548 arp->ar_hrd = htons(ARPHRD_AX25); 00549 arp->ar_pro = htons(AX25_P_IP); 00550 break; 00551 00552 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE) 00553 case ARPHRD_NETROM: 00554 arp->ar_hrd = htons(ARPHRD_NETROM); 00555 arp->ar_pro = htons(AX25_P_IP); 00556 break; 00557 #endif 00558 #endif 00559 00560 #ifdef CONFIG_FDDI 00561 case ARPHRD_FDDI: 00562 arp->ar_hrd = htons(ARPHRD_ETHER); 00563 arp->ar_pro = htons(ETH_P_IP); 00564 break; 00565 #endif 00566 #ifdef CONFIG_TR 00567 case ARPHRD_IEEE802_TR: 00568 arp->ar_hrd = htons(ARPHRD_IEEE802); 00569 arp->ar_pro = htons(ETH_P_IP); 00570 break; 00571 #endif 00572 } 00573 00574 arp->ar_hln = dev->addr_len; 00575 arp->ar_pln = 4; 00576 arp->ar_op = htons(type); 00577 00578 arp_ptr=(unsigned char *)(arp+1); 00579 00580 memcpy(arp_ptr, src_hw, dev->addr_len); 00581 arp_ptr+=dev->addr_len; 00582 memcpy(arp_ptr, &src_ip,4); 00583 arp_ptr+=4; 00584 if (target_hw != NULL) 00585 memcpy(arp_ptr, target_hw, dev->addr_len); 00586 else 00587 memset(arp_ptr, 0, dev->addr_len); 00588 arp_ptr+=dev->addr_len; 00589 memcpy(arp_ptr, &dest_ip, 4); 00590 00591 /* Send it off, maybe filter it using firewalling first. */ 00592 NF_HOOK(NF_ARP, NF_ARP_OUT, skb, NULL, dev, dev_queue_xmit); 00593 return; 00594 00595 out: 00596 kfree_skb(skb); 00597 } 00598 00599 static void parp_redo(struct sk_buff *skb) 00600 { 00601 arp_rcv(skb, skb->dev, NULL); 00602 } 00603 00604 /* 00605 * Process an arp request. 00606 */ 00607 00608 int arp_process(struct sk_buff *skb) 00609 { 00610 struct net_device *dev = skb->dev; 00611 struct in_device *in_dev = in_dev_get(dev); 00612 struct arphdr *arp; 00613 unsigned char *arp_ptr; 00614 struct rtable *rt; 00615 unsigned char *sha, *tha; 00616 u32 sip, tip; 00617 u16 dev_type = dev->type; 00618 int addr_type; 00619 struct neighbour *n; 00620 00621 /* arp_rcv below verifies the ARP header, verifies the device 00622 * is ARP'able, and linearizes the SKB (if needed). 00623 */ 00624 00625 if (in_dev == NULL) 00626 goto out; 00627 00628 arp = skb->nh.arph; 00629 arp_ptr= (unsigned char *)(arp+1); 00630 00631 switch (dev_type) { 00632 default: 00633 if (arp->ar_pro != htons(ETH_P_IP)) 00634 goto out; 00635 if (htons(dev_type) != arp->ar_hrd) 00636 goto out; 00637 break; 00638 #ifdef CONFIG_NET_ETHERNET 00639 case ARPHRD_ETHER: 00640 /* 00641 * ETHERNET devices will accept ARP hardware types of either 00642 * 1 (Ethernet) or 6 (IEEE 802.2). 00643 */ 00644 if (arp->ar_hrd != htons(ARPHRD_ETHER) && 00645 arp->ar_hrd != htons(ARPHRD_IEEE802)) 00646 goto out; 00647 if (arp->ar_pro != htons(ETH_P_IP)) 00648 goto out; 00649 break; 00650 #endif 00651 #ifdef CONFIG_TR 00652 case ARPHRD_IEEE802_TR: 00653 /* 00654 * Token ring devices will accept ARP hardware types of either 00655 * 1 (Ethernet) or 6 (IEEE 802.2). 00656 */ 00657 if (arp->ar_hrd != htons(ARPHRD_ETHER) && 00658 arp->ar_hrd != htons(ARPHRD_IEEE802)) 00659 goto out; 00660 if (arp->ar_pro != htons(ETH_P_IP)) 00661 goto out; 00662 break; 00663 #endif 00664 #ifdef CONFIG_FDDI 00665 case ARPHRD_FDDI: 00666 /* 00667 * According to RFC 1390, FDDI devices should accept ARP hardware types 00668 * of 1 (Ethernet). However, to be more robust, we'll accept hardware 00669 * types of either 1 (Ethernet) or 6 (IEEE 802.2). 00670 */ 00671 if (arp->ar_hrd != htons(ARPHRD_ETHER) && 00672 arp->ar_hrd != htons(ARPHRD_IEEE802)) 00673 goto out; 00674 if (arp->ar_pro != htons(ETH_P_IP)) 00675 goto out; 00676 break; 00677 #endif 00678 #ifdef CONFIG_NET_FC 00679 case ARPHRD_IEEE802: 00680 /* 00681 * According to RFC 2625, Fibre Channel devices (which are IEEE 00682 * 802 devices) should accept ARP hardware types of 6 (IEEE 802) 00683 * and 1 (Ethernet). 00684 */ 00685 if (arp->ar_hrd != htons(ARPHRD_ETHER) && 00686 arp->ar_hrd != htons(ARPHRD_IEEE802)) 00687 goto out; 00688 if (arp->ar_pro != htons(ETH_P_IP)) 00689 goto out; 00690 break; 00691 #endif 00692 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 00693 case ARPHRD_AX25: 00694 if (arp->ar_pro != htons(AX25_P_IP)) 00695 goto out; 00696 if (arp->ar_hrd != htons(ARPHRD_AX25)) 00697 goto out; 00698 break; 00699 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE) 00700 case ARPHRD_NETROM: 00701 if (arp->ar_pro != htons(AX25_P_IP)) 00702 goto out; 00703 if (arp->ar_hrd != htons(ARPHRD_NETROM)) 00704 goto out; 00705 break; 00706 #endif 00707 #endif 00708 } 00709 00710 /* Understand only these message types */ 00711 00712 if (arp->ar_op != htons(ARPOP_REPLY) && 00713 arp->ar_op != htons(ARPOP_REQUEST)) 00714 goto out; 00715 00716 /* 00717 * Extract fields 00718 */ 00719 sha=arp_ptr; 00720 arp_ptr += dev->addr_len; 00721 memcpy(&sip, arp_ptr, 4); 00722 arp_ptr += 4; 00723 tha=arp_ptr; 00724 arp_ptr += dev->addr_len; 00725 memcpy(&tip, arp_ptr, 4); 00726 /* 00727 * Check for bad requests for 127.x.x.x and requests for multicast 00728 * addresses. If this is one such, delete it. 00729 */ 00730 if (LOOPBACK(tip) || MULTICAST(tip)) 00731 goto out; 00732 00733 /* 00734 * Special case: We must set Frame Relay source Q.922 address 00735 */ 00736 if (dev_type == ARPHRD_DLCI) 00737 sha = dev->broadcast; 00738 00739 /* 00740 * Process entry. The idea here is we want to send a reply if it is a 00741 * request for us or if it is a request for someone else that we hold 00742 * a proxy for. We want to add an entry to our cache if it is a reply 00743 * to us or if it is a request for our address. 00744 * (The assumption for this last is that if someone is requesting our 00745 * address, they are probably intending to talk to us, so it saves time 00746 * if we cache their address. Their address is also probably not in 00747 * our cache, since ours is not in their cache.) 00748 * 00749 * Putting this another way, we only care about replies if they are to 00750 * us, in which case we add them to the cache. For requests, we care 00751 * about those for us and those for our proxies. We reply to both, 00752 * and in the case of requests for us we add the requester to the arp 00753 * cache. 00754 */ 00755 00756 /* Special case: IPv4 duplicate address detection packet (RFC2131) */ 00757 if (sip == 0) { 00758 if (arp->ar_op == htons(ARPOP_REQUEST) && 00759 inet_addr_type(tip) == RTN_LOCAL) 00760 arp_send(ARPOP_REPLY,ETH_P_ARP,tip,dev,tip,sha,dev->dev_addr,dev->dev_addr); 00761 goto out; 00762 } 00763 00764 if (arp->ar_op == htons(ARPOP_REQUEST) && 00765 ip_route_input(skb, tip, sip, 0, dev) == 0) { 00766 00767 rt = (struct rtable*)skb->dst; 00768 addr_type = rt->rt_type; 00769 00770 if (addr_type == RTN_LOCAL) { 00771 n = neigh_event_ns(&arp_tbl, sha, &sip, dev); 00772 if (n) { 00773 int dont_send = 0; 00774 if (IN_DEV_ARPFILTER(in_dev)) 00775 dont_send |= arp_filter(sip,tip,dev); 00776 if (!dont_send) 00777 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha); 00778 00779 neigh_release(n); 00780 } 00781 goto out; 00782 } else if (IN_DEV_FORWARD(in_dev)) { 00783 if ((rt->rt_flags&RTCF_DNAT) || 00784 (addr_type == RTN_UNICAST && rt->u.dst.dev != dev && 00785 (arp_fwd_proxy(in_dev, rt) || pneigh_lookup(&arp_tbl, &tip, dev, 0)))) { 00786 n = neigh_event_ns(&arp_tbl, sha, &sip, dev); 00787 if (n) 00788 neigh_release(n); 00789 00790 if (skb->stamp.tv_sec == 0 || 00791 skb->pkt_type == PACKET_HOST || 00792 in_dev->arp_parms->proxy_delay == 0) { 00793 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha); 00794 } else { 00795 pneigh_enqueue(&arp_tbl, in_dev->arp_parms, skb); 00796 in_dev_put(in_dev); 00797 return 0; 00798 } 00799 goto out; 00800 } 00801 } 00802 } 00803 00804 /* Update our ARP tables */ 00805 00806 n = __neigh_lookup(&arp_tbl, &sip, dev, 0); 00807 00808 #ifdef CONFIG_IP_ACCEPT_UNSOLICITED_ARP 00809 /* Unsolicited ARP is not accepted by default. 00810 It is possible, that this option should be enabled for some 00811 devices (strip is candidate) 00812 */ 00813 if (n == NULL && 00814 arp->ar_op == htons(ARPOP_REPLY) && 00815 inet_addr_type(sip) == RTN_UNICAST) 00816 n = __neigh_lookup(&arp_tbl, &sip, dev, -1); 00817 #endif 00818 00819 if (n) { 00820 int state = NUD_REACHABLE; 00821 int override = 0; 00822 00823 /* If several different ARP replies follows back-to-back, 00824 use the FIRST one. It is possible, if several proxy 00825 agents are active. Taking the first reply prevents 00826 arp trashing and chooses the fastest router. 00827 */ 00828 if (jiffies - n->updated >= n->parms->locktime) 00829 override = 1; 00830 00831 /* Broadcast replies and request packets 00832 do not assert neighbour reachability. 00833 */ 00834 if (arp->ar_op != htons(ARPOP_REPLY) || 00835 skb->pkt_type != PACKET_HOST) 00836 state = NUD_STALE; 00837 neigh_update(n, sha, state, override, 1); 00838 neigh_release(n); 00839 } 00840 00841 out: 00842 if (in_dev) 00843 in_dev_put(in_dev); 00844 kfree_skb(skb); 00845 return 0; 00846 } 00847 00848 00849 /* 00850 * Receive an arp request from the device layer. 00851 */ 00852 00853 int arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt) 00854 { 00855 struct arphdr *arp; 00856 00857 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */ 00858 if (!pskb_may_pull(skb, (sizeof(struct arphdr) + 00859 (2 * dev->addr_len) + 00860 (2 * sizeof(u32))))) 00861 goto freeskb; 00862 00863 arp = skb->nh.arph; 00864 if (arp->ar_hln != dev->addr_len || 00865 dev->flags & IFF_NOARP || 00866 skb->pkt_type == PACKET_OTHERHOST || 00867 skb->pkt_type == PACKET_LOOPBACK || 00868 arp->ar_pln != 4) 00869 goto freeskb; 00870 00871 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 00872 goto out_of_mem; 00873 00874 return NF_HOOK(NF_ARP, NF_ARP_IN, skb, dev, NULL, arp_process); 00875 00876 freeskb: 00877 kfree_skb(skb); 00878 out_of_mem: 00879 return 0; 00880 } 00881 00882 /* 00883 * User level interface (ioctl, /proc) 00884 */ 00885 00886 /* 00887 * Set (create) an ARP cache entry. 00888 */ 00889 00890 int arp_req_set(struct arpreq *r, struct net_device * dev) 00891 { 00892 u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr; 00893 struct neighbour *neigh; 00894 int err; 00895 00896 if (r->arp_flags&ATF_PUBL) { 00897 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr; 00898 if (mask && mask != 0xFFFFFFFF) 00899 return -EINVAL; 00900 if (!dev && (r->arp_flags & ATF_COM)) { 00901 dev = dev_getbyhwaddr(r->arp_ha.sa_family, r->arp_ha.sa_data); 00902 if (!dev) 00903 return -ENODEV; 00904 } 00905 if (mask) { 00906 if (pneigh_lookup(&arp_tbl, &ip, dev, 1) == NULL) 00907 return -ENOBUFS; 00908 return 0; 00909 } 00910 if (dev == NULL) { 00911 ipv4_devconf.proxy_arp = 1; 00912 return 0; 00913 } 00914 if (__in_dev_get(dev)) { 00915 __in_dev_get(dev)->cnf.proxy_arp = 1; 00916 return 0; 00917 } 00918 return -ENXIO; 00919 } 00920 00921 if (r->arp_flags & ATF_PERM) 00922 r->arp_flags |= ATF_COM; 00923 if (dev == NULL) { 00924 struct rtable * rt; 00925 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0) 00926 return err; 00927 dev = rt->u.dst.dev; 00928 ip_rt_put(rt); 00929 if (!dev) 00930 return -EINVAL; 00931 } 00932 if (r->arp_ha.sa_family != dev->type) 00933 return -EINVAL; 00934 00935 neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev); 00936 err = PTR_ERR(neigh); 00937 if (!IS_ERR(neigh)) { 00938 unsigned state = NUD_STALE; 00939 if (r->arp_flags & ATF_PERM) 00940 state = NUD_PERMANENT; 00941 err = neigh_update(neigh, (r->arp_flags&ATF_COM) ? 00942 r->arp_ha.sa_data : NULL, state, 1, 0); 00943 neigh_release(neigh); 00944 } 00945 return err; 00946 } 00947 00948 static unsigned arp_state_to_flags(struct neighbour *neigh) 00949 { 00950 unsigned flags = 0; 00951 if (neigh->nud_state&NUD_PERMANENT) 00952 flags = ATF_PERM|ATF_COM; 00953 else if (neigh->nud_state&NUD_VALID) 00954 flags = ATF_COM; 00955 return flags; 00956 } 00957 00958 /* 00959 * Get an ARP cache entry. 00960 */ 00961 00962 static int arp_req_get(struct arpreq *r, struct net_device *dev) 00963 { 00964 u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr; 00965 struct neighbour *neigh; 00966 int err = -ENXIO; 00967 00968 neigh = neigh_lookup(&arp_tbl, &ip, dev); 00969 if (neigh) { 00970 read_lock_bh(&neigh->lock); 00971 memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len); 00972 r->arp_flags = arp_state_to_flags(neigh); 00973 read_unlock_bh(&neigh->lock); 00974 r->arp_ha.sa_family = dev->type; 00975 strncpy(r->arp_dev, dev->name, sizeof(r->arp_dev)); 00976 neigh_release(neigh); 00977 err = 0; 00978 } 00979 return err; 00980 } 00981 00982 int arp_req_delete(struct arpreq *r, struct net_device * dev) 00983 { 00984 int err; 00985 u32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; 00986 struct neighbour *neigh; 00987 00988 if (r->arp_flags & ATF_PUBL) { 00989 u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr; 00990 if (mask == 0xFFFFFFFF) 00991 return pneigh_delete(&arp_tbl, &ip, dev); 00992 if (mask == 0) { 00993 if (dev == NULL) { 00994 ipv4_devconf.proxy_arp = 0; 00995 return 0; 00996 } 00997 if (__in_dev_get(dev)) { 00998 __in_dev_get(dev)->cnf.proxy_arp = 0; 00999 return 0; 01000 } 01001 return -ENXIO; 01002 } 01003 return -EINVAL; 01004 } 01005 01006 if (dev == NULL) { 01007 struct rtable * rt; 01008 if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0) 01009 return err; 01010 dev = rt->u.dst.dev; 01011 ip_rt_put(rt); 01012 if (!dev) 01013 return -EINVAL; 01014 } 01015 err = -ENXIO; 01016 neigh = neigh_lookup(&arp_tbl, &ip, dev); 01017 if (neigh) { 01018 if (neigh->nud_state&~NUD_NOARP) 01019 err = neigh_update(neigh, NULL, NUD_FAILED, 1, 0); 01020 neigh_release(neigh); 01021 } 01022 return err; 01023 } 01024 01025 /* 01026 * Handle an ARP layer I/O control request. 01027 */ 01028 01029 int arp_ioctl(unsigned int cmd, void *arg) 01030 { 01031 int err; 01032 struct arpreq r; 01033 struct net_device * dev = NULL; 01034 01035 switch(cmd) { 01036 case SIOCDARP: 01037 case SIOCSARP: 01038 if (!capable(CAP_NET_ADMIN)) 01039 return -EPERM; 01040 case SIOCGARP: 01041 err = copy_from_user(&r, arg, sizeof(struct arpreq)); 01042 if (err) 01043 return -EFAULT; 01044 break; 01045 default: 01046 return -EINVAL; 01047 } 01048 01049 if (r.arp_pa.sa_family != AF_INET) 01050 return -EPFNOSUPPORT; 01051 01052 if (!(r.arp_flags & ATF_PUBL) && 01053 (r.arp_flags & (ATF_NETMASK|ATF_DONTPUB))) 01054 return -EINVAL; 01055 if (!(r.arp_flags & ATF_NETMASK)) 01056 ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr=htonl(0xFFFFFFFFUL); 01057 01058 rtnl_lock(); 01059 if (r.arp_dev[0]) { 01060 err = -ENODEV; 01061 if ((dev = __dev_get_by_name(r.arp_dev)) == NULL) 01062 goto out; 01063 01064 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */ 01065 if (!r.arp_ha.sa_family) 01066 r.arp_ha.sa_family = dev->type; 01067 err = -EINVAL; 01068 if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type) 01069 goto out; 01070 } else if (cmd == SIOCGARP) { 01071 err = -ENODEV; 01072 goto out; 01073 } 01074 01075 switch(cmd) { 01076 case SIOCDARP: 01077 err = arp_req_delete(&r, dev); 01078 break; 01079 case SIOCSARP: 01080 err = arp_req_set(&r, dev); 01081 break; 01082 case SIOCGARP: 01083 err = arp_req_get(&r, dev); 01084 if (!err && copy_to_user(arg, &r, sizeof(r))) 01085 err = -EFAULT; 01086 break; 01087 } 01088 out: 01089 rtnl_unlock(); 01090 return err; 01091 } 01092 01093 /* 01094 * Write the contents of the ARP cache to a PROCfs file. 01095 */ 01096 #ifndef CONFIG_PROC_FS 01097 static int arp_get_info(char *buffer, char **start, off_t offset, int length) { return 0; } 01098 #else 01099 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 01100 static char *ax2asc2(ax25_address *a, char *buf); 01101 #endif 01102 #define HBUFFERLEN 30 01103 01104 static int arp_get_info(char *buffer, char **start, off_t offset, int length) 01105 { 01106 int len=0; 01107 off_t pos=0; 01108 int size; 01109 char hbuffer[HBUFFERLEN]; 01110 int i,j,k; 01111 const char hexbuf[] = "0123456789ABCDEF"; 01112 01113 size = sprintf(buffer,"IP address HW type Flags HW address Mask Device\n"); 01114 01115 pos+=size; 01116 len+=size; 01117 01118 for(i=0; i<=NEIGH_HASHMASK; i++) { 01119 struct neighbour *n; 01120 read_lock_bh(&arp_tbl.lock); 01121 for (n=arp_tbl.hash_buckets[i]; n; n=n->next) { 01122 struct net_device *dev = n->dev; 01123 int hatype = dev->type; 01124 01125 /* Do not confuse users "arp -a" with magic entries */ 01126 if (!(n->nud_state&~NUD_NOARP)) 01127 continue; 01128 01129 read_lock(&n->lock); 01130 01131 /* 01132 * Convert hardware address to XX:XX:XX:XX ... form. 01133 */ 01134 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 01135 if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM) 01136 ax2asc2((ax25_address *)n->ha, hbuffer); 01137 else { 01138 #endif 01139 for (k=0,j=0;k<HBUFFERLEN-3 && j<dev->addr_len;j++) { 01140 hbuffer[k++]=hexbuf[(n->ha[j]>>4)&15 ]; 01141 hbuffer[k++]=hexbuf[n->ha[j]&15 ]; 01142 hbuffer[k++]=':'; 01143 } 01144 hbuffer[--k]=0; 01145 01146 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 01147 } 01148 #endif 01149 01150 { 01151 char tbuf[16]; 01152 sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(*(u32*)n->primary_key)); 01153 size = sprintf(buffer+len, "%-16s 0x%-10x0x%-10x%s" 01154 " * %s\n", 01155 tbuf, 01156 hatype, 01157 arp_state_to_flags(n), 01158 hbuffer, 01159 dev->name); 01160 } 01161 01162 read_unlock(&n->lock); 01163 01164 len += size; 01165 pos += size; 01166 01167 if (pos <= offset) 01168 len=0; 01169 if (pos >= offset+length) { 01170 read_unlock_bh(&arp_tbl.lock); 01171 goto done; 01172 } 01173 } 01174 read_unlock_bh(&arp_tbl.lock); 01175 } 01176 01177 for (i=0; i<=PNEIGH_HASHMASK; i++) { 01178 struct pneigh_entry *n; 01179 for (n=arp_tbl.phash_buckets[i]; n; n=n->next) { 01180 struct net_device *dev = n->dev; 01181 int hatype = dev ? dev->type : 0; 01182 01183 { 01184 char tbuf[16]; 01185 sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(*(u32*)n->key)); 01186 size = sprintf(buffer+len, "%-16s 0x%-10x0x%-10x%s" 01187 " * %s\n", 01188 tbuf, 01189 hatype, 01190 ATF_PUBL|ATF_PERM, 01191 "00:00:00:00:00:00", 01192 dev ? dev->name : "*"); 01193 } 01194 01195 len += size; 01196 pos += size; 01197 01198 if (pos <= offset) 01199 len=0; 01200 if (pos >= offset+length) 01201 goto done; 01202 } 01203 } 01204 01205 done: 01206 01207 *start = buffer+len-(pos-offset); /* Start of wanted data */ 01208 len = pos-offset; /* Start slop */ 01209 if (len>length) 01210 len = length; /* Ending slop */ 01211 if (len<0) 01212 len = 0; 01213 return len; 01214 } 01215 #endif 01216 01217 static int arp_netdev_event(struct notifier_block *this, unsigned long event, void *ptr) 01218 { 01219 struct net_device *dev = ptr; 01220 01221 switch (event) { 01222 case NETDEV_CHANGEADDR: 01223 neigh_changeaddr(&arp_tbl, dev); 01224 rt_cache_flush(0); 01225 break; 01226 default: 01227 break; 01228 } 01229 01230 return NOTIFY_DONE; 01231 } 01232 01233 struct notifier_block arp_netdev_notifier = { 01234 .notifier_call = arp_netdev_event, 01235 }; 01236 01237 /* Note, that it is not on notifier chain. 01238 It is necessary, that this routine was called after route cache will be 01239 flushed. 01240 */ 01241 void arp_ifdown(struct net_device *dev) 01242 { 01243 neigh_ifdown(&arp_tbl, dev); 01244 } 01245 01246 01247 /* 01248 * Called once on startup. 01249 */ 01250 01251 static struct packet_type arp_packet_type = { 01252 type: __constant_htons(ETH_P_ARP), 01253 func: arp_rcv, 01254 data: (void*) 1, /* understand shared skbs */ 01255 }; 01256 01257 void __init arp_init (void) 01258 { 01259 neigh_table_init(&arp_tbl); 01260 01261 dev_add_pack(&arp_packet_type); 01262 01263 proc_net_create ("arp", 0, arp_get_info); 01264 01265 #ifdef CONFIG_SYSCTL 01266 neigh_sysctl_register(NULL, &arp_tbl.parms, NET_IPV4, NET_IPV4_NEIGH, "ipv4"); 01267 #endif 01268 register_netdevice_notifier(&arp_netdev_notifier); 01269 } 01270 01271 01272 #ifdef CONFIG_PROC_FS 01273 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 01274 01275 /* 01276 * ax25 -> ASCII conversion 01277 */ 01278 char *ax2asc2(ax25_address *a, char *buf) 01279 { 01280 char c, *s; 01281 int n; 01282 01283 for (n = 0, s = buf; n < 6; n++) { 01284 c = (a->ax25_call[n] >> 1) & 0x7F; 01285 01286 if (c != ' ') *s++ = c; 01287 } 01288 01289 *s++ = '-'; 01290 01291 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) { 01292 *s++ = '1'; 01293 n -= 10; 01294 } 01295 01296 *s++ = n + '0'; 01297 *s++ = '\0'; 01298 01299 if (*buf == '\0' || *buf == '-') 01300 return "*"; 01301 01302 return buf; 01303 01304 } 01305 01306 #endif 01307 #endif

Generated on Wed Dec 1 21:25:29 2004 for Linux 2.4.23 Networking by doxygen 1.3.8