11da177e4SLinus Torvalds /* linux/net/inet/arp.c 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * Version: $Id: arp.c,v 1.99 2001/08/30 22:55:42 davem Exp $ 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1994 by Florian La Roche 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This module implements the Address Resolution Protocol ARP (RFC 826), 81da177e4SLinus Torvalds * which is used to convert IP addresses (or in the future maybe other 91da177e4SLinus Torvalds * high-level addresses) into a low-level hardware address (like an Ethernet 101da177e4SLinus Torvalds * address). 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 131da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 141da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 151da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Fixes: 181da177e4SLinus Torvalds * Alan Cox : Removed the Ethernet assumptions in 191da177e4SLinus Torvalds * Florian's code 201da177e4SLinus Torvalds * Alan Cox : Fixed some small errors in the ARP 211da177e4SLinus Torvalds * logic 221da177e4SLinus Torvalds * Alan Cox : Allow >4K in /proc 231da177e4SLinus Torvalds * Alan Cox : Make ARP add its own protocol entry 241da177e4SLinus Torvalds * Ross Martin : Rewrote arp_rcv() and arp_get_info() 251da177e4SLinus Torvalds * Stephen Henson : Add AX25 support to arp_get_info() 261da177e4SLinus Torvalds * Alan Cox : Drop data when a device is downed. 271da177e4SLinus Torvalds * Alan Cox : Use init_timer(). 281da177e4SLinus Torvalds * Alan Cox : Double lock fixes. 291da177e4SLinus Torvalds * Martin Seine : Move the arphdr structure 301da177e4SLinus Torvalds * to if_arp.h for compatibility. 311da177e4SLinus Torvalds * with BSD based programs. 321da177e4SLinus Torvalds * Andrew Tridgell : Added ARP netmask code and 331da177e4SLinus Torvalds * re-arranged proxy handling. 341da177e4SLinus Torvalds * Alan Cox : Changed to use notifiers. 351da177e4SLinus Torvalds * Niibe Yutaka : Reply for this device or proxies only. 361da177e4SLinus Torvalds * Alan Cox : Don't proxy across hardware types! 371da177e4SLinus Torvalds * Jonathan Naylor : Added support for NET/ROM. 381da177e4SLinus Torvalds * Mike Shaver : RFC1122 checks. 391da177e4SLinus Torvalds * Jonathan Naylor : Only lookup the hardware address for 401da177e4SLinus Torvalds * the correct hardware type. 411da177e4SLinus Torvalds * Germano Caronni : Assorted subtle races. 421da177e4SLinus Torvalds * Craig Schlenter : Don't modify permanent entry 431da177e4SLinus Torvalds * during arp_rcv. 441da177e4SLinus Torvalds * Russ Nelson : Tidied up a few bits. 451da177e4SLinus Torvalds * Alexey Kuznetsov: Major changes to caching and behaviour, 461da177e4SLinus Torvalds * eg intelligent arp probing and 471da177e4SLinus Torvalds * generation 481da177e4SLinus Torvalds * of host down events. 491da177e4SLinus Torvalds * Alan Cox : Missing unlock in device events. 501da177e4SLinus Torvalds * Eckes : ARP ioctl control errors. 511da177e4SLinus Torvalds * Alexey Kuznetsov: Arp free fix. 521da177e4SLinus Torvalds * Manuel Rodriguez: Gratuitous ARP. 531da177e4SLinus Torvalds * Jonathan Layes : Added arpd support through kerneld 541da177e4SLinus Torvalds * message queue (960314) 551da177e4SLinus Torvalds * Mike Shaver : /proc/sys/net/ipv4/arp_* support 561da177e4SLinus Torvalds * Mike McLagan : Routing by source 571da177e4SLinus Torvalds * Stuart Cheshire : Metricom and grat arp fixes 581da177e4SLinus Torvalds * *** FOR 2.1 clean this up *** 591da177e4SLinus Torvalds * Lawrence V. Stefani: (08/12/96) Added FDDI support. 601da177e4SLinus Torvalds * Alan Cox : Took the AP1000 nasty FDDI hack and 611da177e4SLinus Torvalds * folded into the mainstream FDDI code. 621da177e4SLinus Torvalds * Ack spit, Linus how did you allow that 631da177e4SLinus Torvalds * one in... 641da177e4SLinus Torvalds * Jes Sorensen : Make FDDI work again in 2.1.x and 651da177e4SLinus Torvalds * clean up the APFDDI & gen. FDDI bits. 661da177e4SLinus Torvalds * Alexey Kuznetsov: new arp state machine; 671da177e4SLinus Torvalds * now it is in net/core/neighbour.c. 681da177e4SLinus Torvalds * Krzysztof Halasa: Added Frame Relay ARP support. 691da177e4SLinus Torvalds * Arnaldo C. Melo : convert /proc/net/arp to seq_file 701da177e4SLinus Torvalds * Shmulik Hen: Split arp_send to arp_create and 711da177e4SLinus Torvalds * arp_xmit so intermediate drivers like 721da177e4SLinus Torvalds * bonding can change the skb before 731da177e4SLinus Torvalds * sending (e.g. insert 8021q tag). 741da177e4SLinus Torvalds * Harald Welte : convert to make use of jenkins hash 751da177e4SLinus Torvalds */ 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds #include <linux/module.h> 781da177e4SLinus Torvalds #include <linux/types.h> 791da177e4SLinus Torvalds #include <linux/string.h> 801da177e4SLinus Torvalds #include <linux/kernel.h> 811da177e4SLinus Torvalds #include <linux/sched.h> 824fc268d2SRandy Dunlap #include <linux/capability.h> 831da177e4SLinus Torvalds #include <linux/config.h> 841da177e4SLinus Torvalds #include <linux/socket.h> 851da177e4SLinus Torvalds #include <linux/sockios.h> 861da177e4SLinus Torvalds #include <linux/errno.h> 871da177e4SLinus Torvalds #include <linux/in.h> 881da177e4SLinus Torvalds #include <linux/mm.h> 891da177e4SLinus Torvalds #include <linux/inet.h> 9014c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h> 911da177e4SLinus Torvalds #include <linux/netdevice.h> 921da177e4SLinus Torvalds #include <linux/etherdevice.h> 931da177e4SLinus Torvalds #include <linux/fddidevice.h> 941da177e4SLinus Torvalds #include <linux/if_arp.h> 951da177e4SLinus Torvalds #include <linux/trdevice.h> 961da177e4SLinus Torvalds #include <linux/skbuff.h> 971da177e4SLinus Torvalds #include <linux/proc_fs.h> 981da177e4SLinus Torvalds #include <linux/seq_file.h> 991da177e4SLinus Torvalds #include <linux/stat.h> 1001da177e4SLinus Torvalds #include <linux/init.h> 1011da177e4SLinus Torvalds #include <linux/net.h> 1021da177e4SLinus Torvalds #include <linux/rcupdate.h> 1031da177e4SLinus Torvalds #include <linux/jhash.h> 1041da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1051da177e4SLinus Torvalds #include <linux/sysctl.h> 1061da177e4SLinus Torvalds #endif 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds #include <net/ip.h> 1091da177e4SLinus Torvalds #include <net/icmp.h> 1101da177e4SLinus Torvalds #include <net/route.h> 1111da177e4SLinus Torvalds #include <net/protocol.h> 1121da177e4SLinus Torvalds #include <net/tcp.h> 1131da177e4SLinus Torvalds #include <net/sock.h> 1141da177e4SLinus Torvalds #include <net/arp.h> 1151da177e4SLinus Torvalds #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 1161da177e4SLinus Torvalds #include <net/ax25.h> 1171da177e4SLinus Torvalds #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE) 1181da177e4SLinus Torvalds #include <net/netrom.h> 1191da177e4SLinus Torvalds #endif 1201da177e4SLinus Torvalds #endif 1211da177e4SLinus Torvalds #if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE) 1221da177e4SLinus Torvalds #include <net/atmclip.h> 1231da177e4SLinus Torvalds struct neigh_table *clip_tbl_hook; 1241da177e4SLinus Torvalds #endif 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds #include <asm/system.h> 1271da177e4SLinus Torvalds #include <asm/uaccess.h> 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds #include <linux/netfilter_arp.h> 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * Interface to generic neighbour cache. 1331da177e4SLinus Torvalds */ 1341da177e4SLinus Torvalds static u32 arp_hash(const void *pkey, const struct net_device *dev); 1351da177e4SLinus Torvalds static int arp_constructor(struct neighbour *neigh); 1361da177e4SLinus Torvalds static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb); 1371da177e4SLinus Torvalds static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb); 1381da177e4SLinus Torvalds static void parp_redo(struct sk_buff *skb); 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static struct neigh_ops arp_generic_ops = { 1411da177e4SLinus Torvalds .family = AF_INET, 1421da177e4SLinus Torvalds .solicit = arp_solicit, 1431da177e4SLinus Torvalds .error_report = arp_error_report, 1441da177e4SLinus Torvalds .output = neigh_resolve_output, 1451da177e4SLinus Torvalds .connected_output = neigh_connected_output, 1461da177e4SLinus Torvalds .hh_output = dev_queue_xmit, 1471da177e4SLinus Torvalds .queue_xmit = dev_queue_xmit, 1481da177e4SLinus Torvalds }; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds static struct neigh_ops arp_hh_ops = { 1511da177e4SLinus Torvalds .family = AF_INET, 1521da177e4SLinus Torvalds .solicit = arp_solicit, 1531da177e4SLinus Torvalds .error_report = arp_error_report, 1541da177e4SLinus Torvalds .output = neigh_resolve_output, 1551da177e4SLinus Torvalds .connected_output = neigh_resolve_output, 1561da177e4SLinus Torvalds .hh_output = dev_queue_xmit, 1571da177e4SLinus Torvalds .queue_xmit = dev_queue_xmit, 1581da177e4SLinus Torvalds }; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static struct neigh_ops arp_direct_ops = { 1611da177e4SLinus Torvalds .family = AF_INET, 1621da177e4SLinus Torvalds .output = dev_queue_xmit, 1631da177e4SLinus Torvalds .connected_output = dev_queue_xmit, 1641da177e4SLinus Torvalds .hh_output = dev_queue_xmit, 1651da177e4SLinus Torvalds .queue_xmit = dev_queue_xmit, 1661da177e4SLinus Torvalds }; 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds struct neigh_ops arp_broken_ops = { 1691da177e4SLinus Torvalds .family = AF_INET, 1701da177e4SLinus Torvalds .solicit = arp_solicit, 1711da177e4SLinus Torvalds .error_report = arp_error_report, 1721da177e4SLinus Torvalds .output = neigh_compat_output, 1731da177e4SLinus Torvalds .connected_output = neigh_compat_output, 1741da177e4SLinus Torvalds .hh_output = dev_queue_xmit, 1751da177e4SLinus Torvalds .queue_xmit = dev_queue_xmit, 1761da177e4SLinus Torvalds }; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds struct neigh_table arp_tbl = { 1791da177e4SLinus Torvalds .family = AF_INET, 1801da177e4SLinus Torvalds .entry_size = sizeof(struct neighbour) + 4, 1811da177e4SLinus Torvalds .key_len = 4, 1821da177e4SLinus Torvalds .hash = arp_hash, 1831da177e4SLinus Torvalds .constructor = arp_constructor, 1841da177e4SLinus Torvalds .proxy_redo = parp_redo, 1851da177e4SLinus Torvalds .id = "arp_cache", 1861da177e4SLinus Torvalds .parms = { 1871da177e4SLinus Torvalds .tbl = &arp_tbl, 1881da177e4SLinus Torvalds .base_reachable_time = 30 * HZ, 1891da177e4SLinus Torvalds .retrans_time = 1 * HZ, 1901da177e4SLinus Torvalds .gc_staletime = 60 * HZ, 1911da177e4SLinus Torvalds .reachable_time = 30 * HZ, 1921da177e4SLinus Torvalds .delay_probe_time = 5 * HZ, 1931da177e4SLinus Torvalds .queue_len = 3, 1941da177e4SLinus Torvalds .ucast_probes = 3, 1951da177e4SLinus Torvalds .mcast_probes = 3, 1961da177e4SLinus Torvalds .anycast_delay = 1 * HZ, 1971da177e4SLinus Torvalds .proxy_delay = (8 * HZ) / 10, 1981da177e4SLinus Torvalds .proxy_qlen = 64, 1991da177e4SLinus Torvalds .locktime = 1 * HZ, 2001da177e4SLinus Torvalds }, 2011da177e4SLinus Torvalds .gc_interval = 30 * HZ, 2021da177e4SLinus Torvalds .gc_thresh1 = 128, 2031da177e4SLinus Torvalds .gc_thresh2 = 512, 2041da177e4SLinus Torvalds .gc_thresh3 = 1024, 2051da177e4SLinus Torvalds }; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds int arp_mc_map(u32 addr, u8 *haddr, struct net_device *dev, int dir) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds switch (dev->type) { 2101da177e4SLinus Torvalds case ARPHRD_ETHER: 2111da177e4SLinus Torvalds case ARPHRD_FDDI: 2121da177e4SLinus Torvalds case ARPHRD_IEEE802: 2131da177e4SLinus Torvalds ip_eth_mc_map(addr, haddr); 2141da177e4SLinus Torvalds return 0; 2151da177e4SLinus Torvalds case ARPHRD_IEEE802_TR: 2161da177e4SLinus Torvalds ip_tr_mc_map(addr, haddr); 2171da177e4SLinus Torvalds return 0; 2181da177e4SLinus Torvalds case ARPHRD_INFINIBAND: 2191da177e4SLinus Torvalds ip_ib_mc_map(addr, haddr); 2201da177e4SLinus Torvalds return 0; 2211da177e4SLinus Torvalds default: 2221da177e4SLinus Torvalds if (dir) { 2231da177e4SLinus Torvalds memcpy(haddr, dev->broadcast, dev->addr_len); 2241da177e4SLinus Torvalds return 0; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds return -EINVAL; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static u32 arp_hash(const void *pkey, const struct net_device *dev) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds return jhash_2words(*(u32 *)pkey, dev->ifindex, arp_tbl.hash_rnd); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static int arp_constructor(struct neighbour *neigh) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds u32 addr = *(u32*)neigh->primary_key; 2391da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 2401da177e4SLinus Torvalds struct in_device *in_dev; 2411da177e4SLinus Torvalds struct neigh_parms *parms; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds neigh->type = inet_addr_type(addr); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds rcu_read_lock(); 246e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(dev); 2471da177e4SLinus Torvalds if (in_dev == NULL) { 2481da177e4SLinus Torvalds rcu_read_unlock(); 2491da177e4SLinus Torvalds return -EINVAL; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds parms = in_dev->arp_parms; 2531da177e4SLinus Torvalds __neigh_parms_put(neigh->parms); 2541da177e4SLinus Torvalds neigh->parms = neigh_parms_clone(parms); 2551da177e4SLinus Torvalds rcu_read_unlock(); 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (dev->hard_header == NULL) { 2581da177e4SLinus Torvalds neigh->nud_state = NUD_NOARP; 2591da177e4SLinus Torvalds neigh->ops = &arp_direct_ops; 2601da177e4SLinus Torvalds neigh->output = neigh->ops->queue_xmit; 2611da177e4SLinus Torvalds } else { 2621da177e4SLinus Torvalds /* Good devices (checked by reading texts, but only Ethernet is 2631da177e4SLinus Torvalds tested) 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds ARPHRD_ETHER: (ethernet, apfddi) 2661da177e4SLinus Torvalds ARPHRD_FDDI: (fddi) 2671da177e4SLinus Torvalds ARPHRD_IEEE802: (tr) 2681da177e4SLinus Torvalds ARPHRD_METRICOM: (strip) 2691da177e4SLinus Torvalds ARPHRD_ARCNET: 2701da177e4SLinus Torvalds etc. etc. etc. 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds ARPHRD_IPDDP will also work, if author repairs it. 2731da177e4SLinus Torvalds I did not it, because this driver does not work even 2741da177e4SLinus Torvalds in old paradigm. 2751da177e4SLinus Torvalds */ 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds #if 1 2781da177e4SLinus Torvalds /* So... these "amateur" devices are hopeless. 2791da177e4SLinus Torvalds The only thing, that I can say now: 2801da177e4SLinus Torvalds It is very sad that we need to keep ugly obsolete 2811da177e4SLinus Torvalds code to make them happy. 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds They should be moved to more reasonable state, now 2841da177e4SLinus Torvalds they use rebuild_header INSTEAD OF hard_start_xmit!!! 2851da177e4SLinus Torvalds Besides that, they are sort of out of date 2861da177e4SLinus Torvalds (a lot of redundant clones/copies, useless in 2.1), 2871da177e4SLinus Torvalds I wonder why people believe that they work. 2881da177e4SLinus Torvalds */ 2891da177e4SLinus Torvalds switch (dev->type) { 2901da177e4SLinus Torvalds default: 2911da177e4SLinus Torvalds break; 2921da177e4SLinus Torvalds case ARPHRD_ROSE: 2931da177e4SLinus Torvalds #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 2941da177e4SLinus Torvalds case ARPHRD_AX25: 2951da177e4SLinus Torvalds #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE) 2961da177e4SLinus Torvalds case ARPHRD_NETROM: 2971da177e4SLinus Torvalds #endif 2981da177e4SLinus Torvalds neigh->ops = &arp_broken_ops; 2991da177e4SLinus Torvalds neigh->output = neigh->ops->output; 3001da177e4SLinus Torvalds return 0; 3011da177e4SLinus Torvalds #endif 3021da177e4SLinus Torvalds ;} 3031da177e4SLinus Torvalds #endif 3041da177e4SLinus Torvalds if (neigh->type == RTN_MULTICAST) { 3051da177e4SLinus Torvalds neigh->nud_state = NUD_NOARP; 3061da177e4SLinus Torvalds arp_mc_map(addr, neigh->ha, dev, 1); 3071da177e4SLinus Torvalds } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) { 3081da177e4SLinus Torvalds neigh->nud_state = NUD_NOARP; 3091da177e4SLinus Torvalds memcpy(neigh->ha, dev->dev_addr, dev->addr_len); 3101da177e4SLinus Torvalds } else if (neigh->type == RTN_BROADCAST || dev->flags&IFF_POINTOPOINT) { 3111da177e4SLinus Torvalds neigh->nud_state = NUD_NOARP; 3121da177e4SLinus Torvalds memcpy(neigh->ha, dev->broadcast, dev->addr_len); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds if (dev->hard_header_cache) 3151da177e4SLinus Torvalds neigh->ops = &arp_hh_ops; 3161da177e4SLinus Torvalds else 3171da177e4SLinus Torvalds neigh->ops = &arp_generic_ops; 3181da177e4SLinus Torvalds if (neigh->nud_state&NUD_VALID) 3191da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 3201da177e4SLinus Torvalds else 3211da177e4SLinus Torvalds neigh->output = neigh->ops->output; 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds return 0; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds dst_link_failure(skb); 3291da177e4SLinus Torvalds kfree_skb(skb); 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb) 3331da177e4SLinus Torvalds { 3341da177e4SLinus Torvalds u32 saddr = 0; 3351da177e4SLinus Torvalds u8 *dst_ha = NULL; 3361da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 3371da177e4SLinus Torvalds u32 target = *(u32*)neigh->primary_key; 3381da177e4SLinus Torvalds int probes = atomic_read(&neigh->probes); 3391da177e4SLinus Torvalds struct in_device *in_dev = in_dev_get(dev); 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds if (!in_dev) 3421da177e4SLinus Torvalds return; 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds switch (IN_DEV_ARP_ANNOUNCE(in_dev)) { 3451da177e4SLinus Torvalds default: 3461da177e4SLinus Torvalds case 0: /* By default announce any local IP */ 3471da177e4SLinus Torvalds if (skb && inet_addr_type(skb->nh.iph->saddr) == RTN_LOCAL) 3481da177e4SLinus Torvalds saddr = skb->nh.iph->saddr; 3491da177e4SLinus Torvalds break; 3501da177e4SLinus Torvalds case 1: /* Restrict announcements of saddr in same subnet */ 3511da177e4SLinus Torvalds if (!skb) 3521da177e4SLinus Torvalds break; 3531da177e4SLinus Torvalds saddr = skb->nh.iph->saddr; 3541da177e4SLinus Torvalds if (inet_addr_type(saddr) == RTN_LOCAL) { 3551da177e4SLinus Torvalds /* saddr should be known to target */ 3561da177e4SLinus Torvalds if (inet_addr_onlink(in_dev, target, saddr)) 3571da177e4SLinus Torvalds break; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds saddr = 0; 3601da177e4SLinus Torvalds break; 3611da177e4SLinus Torvalds case 2: /* Avoid secondary IPs, get a primary/preferred one */ 3621da177e4SLinus Torvalds break; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds if (in_dev) 3661da177e4SLinus Torvalds in_dev_put(in_dev); 3671da177e4SLinus Torvalds if (!saddr) 3681da177e4SLinus Torvalds saddr = inet_select_addr(dev, target, RT_SCOPE_LINK); 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds if ((probes -= neigh->parms->ucast_probes) < 0) { 3711da177e4SLinus Torvalds if (!(neigh->nud_state&NUD_VALID)) 3721da177e4SLinus Torvalds printk(KERN_DEBUG "trying to ucast probe in NUD_INVALID\n"); 3731da177e4SLinus Torvalds dst_ha = neigh->ha; 3741da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 3751da177e4SLinus Torvalds } else if ((probes -= neigh->parms->app_probes) < 0) { 3761da177e4SLinus Torvalds #ifdef CONFIG_ARPD 3771da177e4SLinus Torvalds neigh_app_ns(neigh); 3781da177e4SLinus Torvalds #endif 3791da177e4SLinus Torvalds return; 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr, 3831da177e4SLinus Torvalds dst_ha, dev->dev_addr, NULL); 3841da177e4SLinus Torvalds if (dst_ha) 3851da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds static int arp_ignore(struct in_device *in_dev, struct net_device *dev, 3891da177e4SLinus Torvalds u32 sip, u32 tip) 3901da177e4SLinus Torvalds { 3911da177e4SLinus Torvalds int scope; 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds switch (IN_DEV_ARP_IGNORE(in_dev)) { 3941da177e4SLinus Torvalds case 0: /* Reply, the tip is already validated */ 3951da177e4SLinus Torvalds return 0; 3961da177e4SLinus Torvalds case 1: /* Reply only if tip is configured on the incoming interface */ 3971da177e4SLinus Torvalds sip = 0; 3981da177e4SLinus Torvalds scope = RT_SCOPE_HOST; 3991da177e4SLinus Torvalds break; 4001da177e4SLinus Torvalds case 2: /* 4011da177e4SLinus Torvalds * Reply only if tip is configured on the incoming interface 4021da177e4SLinus Torvalds * and is in same subnet as sip 4031da177e4SLinus Torvalds */ 4041da177e4SLinus Torvalds scope = RT_SCOPE_HOST; 4051da177e4SLinus Torvalds break; 4061da177e4SLinus Torvalds case 3: /* Do not reply for scope host addresses */ 4071da177e4SLinus Torvalds sip = 0; 4081da177e4SLinus Torvalds scope = RT_SCOPE_LINK; 4091da177e4SLinus Torvalds dev = NULL; 4101da177e4SLinus Torvalds break; 4111da177e4SLinus Torvalds case 4: /* Reserved */ 4121da177e4SLinus Torvalds case 5: 4131da177e4SLinus Torvalds case 6: 4141da177e4SLinus Torvalds case 7: 4151da177e4SLinus Torvalds return 0; 4161da177e4SLinus Torvalds case 8: /* Do not reply */ 4171da177e4SLinus Torvalds return 1; 4181da177e4SLinus Torvalds default: 4191da177e4SLinus Torvalds return 0; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds return !inet_confirm_addr(dev, sip, tip, scope); 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds static int arp_filter(__u32 sip, __u32 tip, struct net_device *dev) 4251da177e4SLinus Torvalds { 4261da177e4SLinus Torvalds struct flowi fl = { .nl_u = { .ip4_u = { .daddr = sip, 4271da177e4SLinus Torvalds .saddr = tip } } }; 4281da177e4SLinus Torvalds struct rtable *rt; 4291da177e4SLinus Torvalds int flag = 0; 4301da177e4SLinus Torvalds /*unsigned long now; */ 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds if (ip_route_output_key(&rt, &fl) < 0) 4331da177e4SLinus Torvalds return 1; 4341da177e4SLinus Torvalds if (rt->u.dst.dev != dev) { 4351da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_ARPFILTER); 4361da177e4SLinus Torvalds flag = 1; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds ip_rt_put(rt); 4391da177e4SLinus Torvalds return flag; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds /* OBSOLETE FUNCTIONS */ 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* 4451da177e4SLinus Torvalds * Find an arp mapping in the cache. If not found, post a request. 4461da177e4SLinus Torvalds * 4471da177e4SLinus Torvalds * It is very UGLY routine: it DOES NOT use skb->dst->neighbour, 4481da177e4SLinus Torvalds * even if it exists. It is supposed that skb->dev was mangled 4491da177e4SLinus Torvalds * by a virtual device (eql, shaper). Nobody but broken devices 4501da177e4SLinus Torvalds * is allowed to use this function, it is scheduled to be removed. --ANK 4511da177e4SLinus Torvalds */ 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds static int arp_set_predefined(int addr_hint, unsigned char * haddr, u32 paddr, struct net_device * dev) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds switch (addr_hint) { 4561da177e4SLinus Torvalds case RTN_LOCAL: 4571da177e4SLinus Torvalds printk(KERN_DEBUG "ARP: arp called for own IP address\n"); 4581da177e4SLinus Torvalds memcpy(haddr, dev->dev_addr, dev->addr_len); 4591da177e4SLinus Torvalds return 1; 4601da177e4SLinus Torvalds case RTN_MULTICAST: 4611da177e4SLinus Torvalds arp_mc_map(paddr, haddr, dev, 1); 4621da177e4SLinus Torvalds return 1; 4631da177e4SLinus Torvalds case RTN_BROADCAST: 4641da177e4SLinus Torvalds memcpy(haddr, dev->broadcast, dev->addr_len); 4651da177e4SLinus Torvalds return 1; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds return 0; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds int arp_find(unsigned char *haddr, struct sk_buff *skb) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds struct net_device *dev = skb->dev; 4741da177e4SLinus Torvalds u32 paddr; 4751da177e4SLinus Torvalds struct neighbour *n; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds if (!skb->dst) { 4781da177e4SLinus Torvalds printk(KERN_DEBUG "arp_find is called with dst==NULL\n"); 4791da177e4SLinus Torvalds kfree_skb(skb); 4801da177e4SLinus Torvalds return 1; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds paddr = ((struct rtable*)skb->dst)->rt_gateway; 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds if (arp_set_predefined(inet_addr_type(paddr), haddr, paddr, dev)) 4861da177e4SLinus Torvalds return 0; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds n = __neigh_lookup(&arp_tbl, &paddr, dev, 1); 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds if (n) { 4911da177e4SLinus Torvalds n->used = jiffies; 4921da177e4SLinus Torvalds if (n->nud_state&NUD_VALID || neigh_event_send(n, skb) == 0) { 4931da177e4SLinus Torvalds read_lock_bh(&n->lock); 4941da177e4SLinus Torvalds memcpy(haddr, n->ha, dev->addr_len); 4951da177e4SLinus Torvalds read_unlock_bh(&n->lock); 4961da177e4SLinus Torvalds neigh_release(n); 4971da177e4SLinus Torvalds return 0; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds neigh_release(n); 5001da177e4SLinus Torvalds } else 5011da177e4SLinus Torvalds kfree_skb(skb); 5021da177e4SLinus Torvalds return 1; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* END OF OBSOLETE FUNCTIONS */ 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds int arp_bind_neighbour(struct dst_entry *dst) 5081da177e4SLinus Torvalds { 5091da177e4SLinus Torvalds struct net_device *dev = dst->dev; 5101da177e4SLinus Torvalds struct neighbour *n = dst->neighbour; 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds if (dev == NULL) 5131da177e4SLinus Torvalds return -EINVAL; 5141da177e4SLinus Torvalds if (n == NULL) { 5151da177e4SLinus Torvalds u32 nexthop = ((struct rtable*)dst)->rt_gateway; 5161da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT)) 5171da177e4SLinus Torvalds nexthop = 0; 5181da177e4SLinus Torvalds n = __neigh_lookup_errno( 5191da177e4SLinus Torvalds #if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE) 5201da177e4SLinus Torvalds dev->type == ARPHRD_ATM ? clip_tbl_hook : 5211da177e4SLinus Torvalds #endif 5221da177e4SLinus Torvalds &arp_tbl, &nexthop, dev); 5231da177e4SLinus Torvalds if (IS_ERR(n)) 5241da177e4SLinus Torvalds return PTR_ERR(n); 5251da177e4SLinus Torvalds dst->neighbour = n; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds return 0; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * Check if we can use proxy ARP for this path 5321da177e4SLinus Torvalds */ 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds static inline int arp_fwd_proxy(struct in_device *in_dev, struct rtable *rt) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds struct in_device *out_dev; 5371da177e4SLinus Torvalds int imi, omi = -1; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds if (!IN_DEV_PROXY_ARP(in_dev)) 5401da177e4SLinus Torvalds return 0; 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds if ((imi = IN_DEV_MEDIUM_ID(in_dev)) == 0) 5431da177e4SLinus Torvalds return 1; 5441da177e4SLinus Torvalds if (imi == -1) 5451da177e4SLinus Torvalds return 0; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /* place to check for proxy_arp for routes */ 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds if ((out_dev = in_dev_get(rt->u.dst.dev)) != NULL) { 5501da177e4SLinus Torvalds omi = IN_DEV_MEDIUM_ID(out_dev); 5511da177e4SLinus Torvalds in_dev_put(out_dev); 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds return (omi != imi && omi != -1); 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds /* 5571da177e4SLinus Torvalds * Interface to link layer: send routine and receive handler. 5581da177e4SLinus Torvalds */ 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds /* 5611da177e4SLinus Torvalds * Create an arp packet. If (dest_hw == NULL), we create a broadcast 5621da177e4SLinus Torvalds * message. 5631da177e4SLinus Torvalds */ 5641da177e4SLinus Torvalds struct sk_buff *arp_create(int type, int ptype, u32 dest_ip, 5651da177e4SLinus Torvalds struct net_device *dev, u32 src_ip, 5661da177e4SLinus Torvalds unsigned char *dest_hw, unsigned char *src_hw, 5671da177e4SLinus Torvalds unsigned char *target_hw) 5681da177e4SLinus Torvalds { 5691da177e4SLinus Torvalds struct sk_buff *skb; 5701da177e4SLinus Torvalds struct arphdr *arp; 5711da177e4SLinus Torvalds unsigned char *arp_ptr; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /* 5741da177e4SLinus Torvalds * Allocate a buffer 5751da177e4SLinus Torvalds */ 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4) 5781da177e4SLinus Torvalds + LL_RESERVED_SPACE(dev), GFP_ATOMIC); 5791da177e4SLinus Torvalds if (skb == NULL) 5801da177e4SLinus Torvalds return NULL; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds skb_reserve(skb, LL_RESERVED_SPACE(dev)); 5831da177e4SLinus Torvalds skb->nh.raw = skb->data; 5841da177e4SLinus Torvalds arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4)); 5851da177e4SLinus Torvalds skb->dev = dev; 5861da177e4SLinus Torvalds skb->protocol = htons(ETH_P_ARP); 5871da177e4SLinus Torvalds if (src_hw == NULL) 5881da177e4SLinus Torvalds src_hw = dev->dev_addr; 5891da177e4SLinus Torvalds if (dest_hw == NULL) 5901da177e4SLinus Torvalds dest_hw = dev->broadcast; 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds /* 5931da177e4SLinus Torvalds * Fill the device header for the ARP frame 5941da177e4SLinus Torvalds */ 5951da177e4SLinus Torvalds if (dev->hard_header && 5961da177e4SLinus Torvalds dev->hard_header(skb,dev,ptype,dest_hw,src_hw,skb->len) < 0) 5971da177e4SLinus Torvalds goto out; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* 6001da177e4SLinus Torvalds * Fill out the arp protocol part. 6011da177e4SLinus Torvalds * 6021da177e4SLinus Torvalds * The arp hardware type should match the device type, except for FDDI, 6031da177e4SLinus Torvalds * which (according to RFC 1390) should always equal 1 (Ethernet). 6041da177e4SLinus Torvalds */ 6051da177e4SLinus Torvalds /* 6061da177e4SLinus Torvalds * Exceptions everywhere. AX.25 uses the AX.25 PID value not the 6071da177e4SLinus Torvalds * DIX code for the protocol. Make these device structure fields. 6081da177e4SLinus Torvalds */ 6091da177e4SLinus Torvalds switch (dev->type) { 6101da177e4SLinus Torvalds default: 6111da177e4SLinus Torvalds arp->ar_hrd = htons(dev->type); 6121da177e4SLinus Torvalds arp->ar_pro = htons(ETH_P_IP); 6131da177e4SLinus Torvalds break; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 6161da177e4SLinus Torvalds case ARPHRD_AX25: 6171da177e4SLinus Torvalds arp->ar_hrd = htons(ARPHRD_AX25); 6181da177e4SLinus Torvalds arp->ar_pro = htons(AX25_P_IP); 6191da177e4SLinus Torvalds break; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE) 6221da177e4SLinus Torvalds case ARPHRD_NETROM: 6231da177e4SLinus Torvalds arp->ar_hrd = htons(ARPHRD_NETROM); 6241da177e4SLinus Torvalds arp->ar_pro = htons(AX25_P_IP); 6251da177e4SLinus Torvalds break; 6261da177e4SLinus Torvalds #endif 6271da177e4SLinus Torvalds #endif 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds #ifdef CONFIG_FDDI 6301da177e4SLinus Torvalds case ARPHRD_FDDI: 6311da177e4SLinus Torvalds arp->ar_hrd = htons(ARPHRD_ETHER); 6321da177e4SLinus Torvalds arp->ar_pro = htons(ETH_P_IP); 6331da177e4SLinus Torvalds break; 6341da177e4SLinus Torvalds #endif 6351da177e4SLinus Torvalds #ifdef CONFIG_TR 6361da177e4SLinus Torvalds case ARPHRD_IEEE802_TR: 6371da177e4SLinus Torvalds arp->ar_hrd = htons(ARPHRD_IEEE802); 6381da177e4SLinus Torvalds arp->ar_pro = htons(ETH_P_IP); 6391da177e4SLinus Torvalds break; 6401da177e4SLinus Torvalds #endif 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds arp->ar_hln = dev->addr_len; 6441da177e4SLinus Torvalds arp->ar_pln = 4; 6451da177e4SLinus Torvalds arp->ar_op = htons(type); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds arp_ptr=(unsigned char *)(arp+1); 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds memcpy(arp_ptr, src_hw, dev->addr_len); 6501da177e4SLinus Torvalds arp_ptr+=dev->addr_len; 6511da177e4SLinus Torvalds memcpy(arp_ptr, &src_ip,4); 6521da177e4SLinus Torvalds arp_ptr+=4; 6531da177e4SLinus Torvalds if (target_hw != NULL) 6541da177e4SLinus Torvalds memcpy(arp_ptr, target_hw, dev->addr_len); 6551da177e4SLinus Torvalds else 6561da177e4SLinus Torvalds memset(arp_ptr, 0, dev->addr_len); 6571da177e4SLinus Torvalds arp_ptr+=dev->addr_len; 6581da177e4SLinus Torvalds memcpy(arp_ptr, &dest_ip, 4); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds return skb; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds out: 6631da177e4SLinus Torvalds kfree_skb(skb); 6641da177e4SLinus Torvalds return NULL; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /* 6681da177e4SLinus Torvalds * Send an arp packet. 6691da177e4SLinus Torvalds */ 6701da177e4SLinus Torvalds void arp_xmit(struct sk_buff *skb) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds /* Send it off, maybe filter it using firewalling first. */ 6731da177e4SLinus Torvalds NF_HOOK(NF_ARP, NF_ARP_OUT, skb, NULL, skb->dev, dev_queue_xmit); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 6771da177e4SLinus Torvalds * Create and send an arp packet. 6781da177e4SLinus Torvalds */ 6791da177e4SLinus Torvalds void arp_send(int type, int ptype, u32 dest_ip, 6801da177e4SLinus Torvalds struct net_device *dev, u32 src_ip, 6811da177e4SLinus Torvalds unsigned char *dest_hw, unsigned char *src_hw, 6821da177e4SLinus Torvalds unsigned char *target_hw) 6831da177e4SLinus Torvalds { 6841da177e4SLinus Torvalds struct sk_buff *skb; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /* 6871da177e4SLinus Torvalds * No arp on this interface. 6881da177e4SLinus Torvalds */ 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds if (dev->flags&IFF_NOARP) 6911da177e4SLinus Torvalds return; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds skb = arp_create(type, ptype, dest_ip, dev, src_ip, 6941da177e4SLinus Torvalds dest_hw, src_hw, target_hw); 6951da177e4SLinus Torvalds if (skb == NULL) { 6961da177e4SLinus Torvalds return; 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds arp_xmit(skb); 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds /* 7031da177e4SLinus Torvalds * Process an arp request. 7041da177e4SLinus Torvalds */ 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds static int arp_process(struct sk_buff *skb) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds struct net_device *dev = skb->dev; 7091da177e4SLinus Torvalds struct in_device *in_dev = in_dev_get(dev); 7101da177e4SLinus Torvalds struct arphdr *arp; 7111da177e4SLinus Torvalds unsigned char *arp_ptr; 7121da177e4SLinus Torvalds struct rtable *rt; 7131da177e4SLinus Torvalds unsigned char *sha, *tha; 7141da177e4SLinus Torvalds u32 sip, tip; 7151da177e4SLinus Torvalds u16 dev_type = dev->type; 7161da177e4SLinus Torvalds int addr_type; 7171da177e4SLinus Torvalds struct neighbour *n; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds /* arp_rcv below verifies the ARP header and verifies the device 7201da177e4SLinus Torvalds * is ARP'able. 7211da177e4SLinus Torvalds */ 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds if (in_dev == NULL) 7241da177e4SLinus Torvalds goto out; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds arp = skb->nh.arph; 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds switch (dev_type) { 7291da177e4SLinus Torvalds default: 7301da177e4SLinus Torvalds if (arp->ar_pro != htons(ETH_P_IP) || 7311da177e4SLinus Torvalds htons(dev_type) != arp->ar_hrd) 7321da177e4SLinus Torvalds goto out; 7331da177e4SLinus Torvalds break; 7341da177e4SLinus Torvalds #ifdef CONFIG_NET_ETHERNET 7351da177e4SLinus Torvalds case ARPHRD_ETHER: 7361da177e4SLinus Torvalds #endif 7371da177e4SLinus Torvalds #ifdef CONFIG_TR 7381da177e4SLinus Torvalds case ARPHRD_IEEE802_TR: 7391da177e4SLinus Torvalds #endif 7401da177e4SLinus Torvalds #ifdef CONFIG_FDDI 7411da177e4SLinus Torvalds case ARPHRD_FDDI: 7421da177e4SLinus Torvalds #endif 7431da177e4SLinus Torvalds #ifdef CONFIG_NET_FC 7441da177e4SLinus Torvalds case ARPHRD_IEEE802: 7451da177e4SLinus Torvalds #endif 7461da177e4SLinus Torvalds #if defined(CONFIG_NET_ETHERNET) || defined(CONFIG_TR) || \ 7471da177e4SLinus Torvalds defined(CONFIG_FDDI) || defined(CONFIG_NET_FC) 7481da177e4SLinus Torvalds /* 7491da177e4SLinus Torvalds * ETHERNET, Token Ring and Fibre Channel (which are IEEE 802 7501da177e4SLinus Torvalds * devices, according to RFC 2625) devices will accept ARP 7511da177e4SLinus Torvalds * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2). 7521da177e4SLinus Torvalds * This is the case also of FDDI, where the RFC 1390 says that 7531da177e4SLinus Torvalds * FDDI devices should accept ARP hardware of (1) Ethernet, 7541da177e4SLinus Torvalds * however, to be more robust, we'll accept both 1 (Ethernet) 7551da177e4SLinus Torvalds * or 6 (IEEE 802.2) 7561da177e4SLinus Torvalds */ 7571da177e4SLinus Torvalds if ((arp->ar_hrd != htons(ARPHRD_ETHER) && 7581da177e4SLinus Torvalds arp->ar_hrd != htons(ARPHRD_IEEE802)) || 7591da177e4SLinus Torvalds arp->ar_pro != htons(ETH_P_IP)) 7601da177e4SLinus Torvalds goto out; 7611da177e4SLinus Torvalds break; 7621da177e4SLinus Torvalds #endif 7631da177e4SLinus Torvalds #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 7641da177e4SLinus Torvalds case ARPHRD_AX25: 7651da177e4SLinus Torvalds if (arp->ar_pro != htons(AX25_P_IP) || 7661da177e4SLinus Torvalds arp->ar_hrd != htons(ARPHRD_AX25)) 7671da177e4SLinus Torvalds goto out; 7681da177e4SLinus Torvalds break; 7691da177e4SLinus Torvalds #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE) 7701da177e4SLinus Torvalds case ARPHRD_NETROM: 7711da177e4SLinus Torvalds if (arp->ar_pro != htons(AX25_P_IP) || 7721da177e4SLinus Torvalds arp->ar_hrd != htons(ARPHRD_NETROM)) 7731da177e4SLinus Torvalds goto out; 7741da177e4SLinus Torvalds break; 7751da177e4SLinus Torvalds #endif 7761da177e4SLinus Torvalds #endif 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* Understand only these message types */ 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds if (arp->ar_op != htons(ARPOP_REPLY) && 7821da177e4SLinus Torvalds arp->ar_op != htons(ARPOP_REQUEST)) 7831da177e4SLinus Torvalds goto out; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * Extract fields 7871da177e4SLinus Torvalds */ 7881da177e4SLinus Torvalds arp_ptr= (unsigned char *)(arp+1); 7891da177e4SLinus Torvalds sha = arp_ptr; 7901da177e4SLinus Torvalds arp_ptr += dev->addr_len; 7911da177e4SLinus Torvalds memcpy(&sip, arp_ptr, 4); 7921da177e4SLinus Torvalds arp_ptr += 4; 7931da177e4SLinus Torvalds tha = arp_ptr; 7941da177e4SLinus Torvalds arp_ptr += dev->addr_len; 7951da177e4SLinus Torvalds memcpy(&tip, arp_ptr, 4); 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * Check for bad requests for 127.x.x.x and requests for multicast 7981da177e4SLinus Torvalds * addresses. If this is one such, delete it. 7991da177e4SLinus Torvalds */ 8001da177e4SLinus Torvalds if (LOOPBACK(tip) || MULTICAST(tip)) 8011da177e4SLinus Torvalds goto out; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* 8041da177e4SLinus Torvalds * Special case: We must set Frame Relay source Q.922 address 8051da177e4SLinus Torvalds */ 8061da177e4SLinus Torvalds if (dev_type == ARPHRD_DLCI) 8071da177e4SLinus Torvalds sha = dev->broadcast; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds /* 8101da177e4SLinus Torvalds * Process entry. The idea here is we want to send a reply if it is a 8111da177e4SLinus Torvalds * request for us or if it is a request for someone else that we hold 8121da177e4SLinus Torvalds * a proxy for. We want to add an entry to our cache if it is a reply 8131da177e4SLinus Torvalds * to us or if it is a request for our address. 8141da177e4SLinus Torvalds * (The assumption for this last is that if someone is requesting our 8151da177e4SLinus Torvalds * address, they are probably intending to talk to us, so it saves time 8161da177e4SLinus Torvalds * if we cache their address. Their address is also probably not in 8171da177e4SLinus Torvalds * our cache, since ours is not in their cache.) 8181da177e4SLinus Torvalds * 8191da177e4SLinus Torvalds * Putting this another way, we only care about replies if they are to 8201da177e4SLinus Torvalds * us, in which case we add them to the cache. For requests, we care 8211da177e4SLinus Torvalds * about those for us and those for our proxies. We reply to both, 8221da177e4SLinus Torvalds * and in the case of requests for us we add the requester to the arp 8231da177e4SLinus Torvalds * cache. 8241da177e4SLinus Torvalds */ 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* Special case: IPv4 duplicate address detection packet (RFC2131) */ 8271da177e4SLinus Torvalds if (sip == 0) { 8281da177e4SLinus Torvalds if (arp->ar_op == htons(ARPOP_REQUEST) && 8291da177e4SLinus Torvalds inet_addr_type(tip) == RTN_LOCAL && 8301da177e4SLinus Torvalds !arp_ignore(in_dev,dev,sip,tip)) 8311da177e4SLinus Torvalds arp_send(ARPOP_REPLY,ETH_P_ARP,tip,dev,tip,sha,dev->dev_addr,dev->dev_addr); 8321da177e4SLinus Torvalds goto out; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds if (arp->ar_op == htons(ARPOP_REQUEST) && 8361da177e4SLinus Torvalds ip_route_input(skb, tip, sip, 0, dev) == 0) { 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds rt = (struct rtable*)skb->dst; 8391da177e4SLinus Torvalds addr_type = rt->rt_type; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds if (addr_type == RTN_LOCAL) { 8421da177e4SLinus Torvalds n = neigh_event_ns(&arp_tbl, sha, &sip, dev); 8431da177e4SLinus Torvalds if (n) { 8441da177e4SLinus Torvalds int dont_send = 0; 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds if (!dont_send) 8471da177e4SLinus Torvalds dont_send |= arp_ignore(in_dev,dev,sip,tip); 8481da177e4SLinus Torvalds if (!dont_send && IN_DEV_ARPFILTER(in_dev)) 8491da177e4SLinus Torvalds dont_send |= arp_filter(sip,tip,dev); 8501da177e4SLinus Torvalds if (!dont_send) 8511da177e4SLinus Torvalds arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha); 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds neigh_release(n); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds goto out; 8561da177e4SLinus Torvalds } else if (IN_DEV_FORWARD(in_dev)) { 8571da177e4SLinus Torvalds if ((rt->rt_flags&RTCF_DNAT) || 8581da177e4SLinus Torvalds (addr_type == RTN_UNICAST && rt->u.dst.dev != dev && 8591da177e4SLinus Torvalds (arp_fwd_proxy(in_dev, rt) || pneigh_lookup(&arp_tbl, &tip, dev, 0)))) { 8601da177e4SLinus Torvalds n = neigh_event_ns(&arp_tbl, sha, &sip, dev); 8611da177e4SLinus Torvalds if (n) 8621da177e4SLinus Torvalds neigh_release(n); 8631da177e4SLinus Torvalds 864a61bbcf2SPatrick McHardy if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED || 8651da177e4SLinus Torvalds skb->pkt_type == PACKET_HOST || 8661da177e4SLinus Torvalds in_dev->arp_parms->proxy_delay == 0) { 8671da177e4SLinus Torvalds arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha); 8681da177e4SLinus Torvalds } else { 8691da177e4SLinus Torvalds pneigh_enqueue(&arp_tbl, in_dev->arp_parms, skb); 8701da177e4SLinus Torvalds in_dev_put(in_dev); 8711da177e4SLinus Torvalds return 0; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds goto out; 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds /* Update our ARP tables */ 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds n = __neigh_lookup(&arp_tbl, &sip, dev, 0); 8811da177e4SLinus Torvalds 882*abd596a4SNeil Horman if (ipv4_devconf.arp_accept) { 8831da177e4SLinus Torvalds /* Unsolicited ARP is not accepted by default. 8841da177e4SLinus Torvalds It is possible, that this option should be enabled for some 8851da177e4SLinus Torvalds devices (strip is candidate) 8861da177e4SLinus Torvalds */ 8871da177e4SLinus Torvalds if (n == NULL && 8881da177e4SLinus Torvalds arp->ar_op == htons(ARPOP_REPLY) && 8891da177e4SLinus Torvalds inet_addr_type(sip) == RTN_UNICAST) 8901da177e4SLinus Torvalds n = __neigh_lookup(&arp_tbl, &sip, dev, -1); 891*abd596a4SNeil Horman } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds if (n) { 8941da177e4SLinus Torvalds int state = NUD_REACHABLE; 8951da177e4SLinus Torvalds int override; 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds /* If several different ARP replies follows back-to-back, 8981da177e4SLinus Torvalds use the FIRST one. It is possible, if several proxy 8991da177e4SLinus Torvalds agents are active. Taking the first reply prevents 9001da177e4SLinus Torvalds arp trashing and chooses the fastest router. 9011da177e4SLinus Torvalds */ 9021da177e4SLinus Torvalds override = time_after(jiffies, n->updated + n->parms->locktime); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds /* Broadcast replies and request packets 9051da177e4SLinus Torvalds do not assert neighbour reachability. 9061da177e4SLinus Torvalds */ 9071da177e4SLinus Torvalds if (arp->ar_op != htons(ARPOP_REPLY) || 9081da177e4SLinus Torvalds skb->pkt_type != PACKET_HOST) 9091da177e4SLinus Torvalds state = NUD_STALE; 9101da177e4SLinus Torvalds neigh_update(n, sha, state, override ? NEIGH_UPDATE_F_OVERRIDE : 0); 9111da177e4SLinus Torvalds neigh_release(n); 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds out: 9151da177e4SLinus Torvalds if (in_dev) 9161da177e4SLinus Torvalds in_dev_put(in_dev); 9171da177e4SLinus Torvalds kfree_skb(skb); 9181da177e4SLinus Torvalds return 0; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 921444fc8fcSHerbert Xu static void parp_redo(struct sk_buff *skb) 922444fc8fcSHerbert Xu { 923444fc8fcSHerbert Xu arp_process(skb); 924444fc8fcSHerbert Xu } 925444fc8fcSHerbert Xu 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds /* 9281da177e4SLinus Torvalds * Receive an arp request from the device layer. 9291da177e4SLinus Torvalds */ 9301da177e4SLinus Torvalds 931f2ccd8faSDavid S. Miller int arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) 9321da177e4SLinus Torvalds { 9331da177e4SLinus Torvalds struct arphdr *arp; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds /* ARP header, plus 2 device addresses, plus 2 IP addresses. */ 9361da177e4SLinus Torvalds if (!pskb_may_pull(skb, (sizeof(struct arphdr) + 9371da177e4SLinus Torvalds (2 * dev->addr_len) + 9381da177e4SLinus Torvalds (2 * sizeof(u32))))) 9391da177e4SLinus Torvalds goto freeskb; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds arp = skb->nh.arph; 9421da177e4SLinus Torvalds if (arp->ar_hln != dev->addr_len || 9431da177e4SLinus Torvalds dev->flags & IFF_NOARP || 9441da177e4SLinus Torvalds skb->pkt_type == PACKET_OTHERHOST || 9451da177e4SLinus Torvalds skb->pkt_type == PACKET_LOOPBACK || 9461da177e4SLinus Torvalds arp->ar_pln != 4) 9471da177e4SLinus Torvalds goto freeskb; 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 9501da177e4SLinus Torvalds goto out_of_mem; 9511da177e4SLinus Torvalds 952a61bbcf2SPatrick McHardy memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb)); 953a61bbcf2SPatrick McHardy 9541da177e4SLinus Torvalds return NF_HOOK(NF_ARP, NF_ARP_IN, skb, dev, NULL, arp_process); 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds freeskb: 9571da177e4SLinus Torvalds kfree_skb(skb); 9581da177e4SLinus Torvalds out_of_mem: 9591da177e4SLinus Torvalds return 0; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds /* 9631da177e4SLinus Torvalds * User level interface (ioctl) 9641da177e4SLinus Torvalds */ 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds /* 9671da177e4SLinus Torvalds * Set (create) an ARP cache entry. 9681da177e4SLinus Torvalds */ 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds static int arp_req_set(struct arpreq *r, struct net_device * dev) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr; 9731da177e4SLinus Torvalds struct neighbour *neigh; 9741da177e4SLinus Torvalds int err; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds if (r->arp_flags&ATF_PUBL) { 9771da177e4SLinus Torvalds u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr; 9781da177e4SLinus Torvalds if (mask && mask != 0xFFFFFFFF) 9791da177e4SLinus Torvalds return -EINVAL; 9801da177e4SLinus Torvalds if (!dev && (r->arp_flags & ATF_COM)) { 9811da177e4SLinus Torvalds dev = dev_getbyhwaddr(r->arp_ha.sa_family, r->arp_ha.sa_data); 9821da177e4SLinus Torvalds if (!dev) 9831da177e4SLinus Torvalds return -ENODEV; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds if (mask) { 9861da177e4SLinus Torvalds if (pneigh_lookup(&arp_tbl, &ip, dev, 1) == NULL) 9871da177e4SLinus Torvalds return -ENOBUFS; 9881da177e4SLinus Torvalds return 0; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds if (dev == NULL) { 9911da177e4SLinus Torvalds ipv4_devconf.proxy_arp = 1; 9921da177e4SLinus Torvalds return 0; 9931da177e4SLinus Torvalds } 994e5ed6399SHerbert Xu if (__in_dev_get_rtnl(dev)) { 995e5ed6399SHerbert Xu __in_dev_get_rtnl(dev)->cnf.proxy_arp = 1; 9961da177e4SLinus Torvalds return 0; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds return -ENXIO; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds if (r->arp_flags & ATF_PERM) 10021da177e4SLinus Torvalds r->arp_flags |= ATF_COM; 10031da177e4SLinus Torvalds if (dev == NULL) { 10041da177e4SLinus Torvalds struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, 10051da177e4SLinus Torvalds .tos = RTO_ONLINK } } }; 10061da177e4SLinus Torvalds struct rtable * rt; 10071da177e4SLinus Torvalds if ((err = ip_route_output_key(&rt, &fl)) != 0) 10081da177e4SLinus Torvalds return err; 10091da177e4SLinus Torvalds dev = rt->u.dst.dev; 10101da177e4SLinus Torvalds ip_rt_put(rt); 10111da177e4SLinus Torvalds if (!dev) 10121da177e4SLinus Torvalds return -EINVAL; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds switch (dev->type) { 10151da177e4SLinus Torvalds #ifdef CONFIG_FDDI 10161da177e4SLinus Torvalds case ARPHRD_FDDI: 10171da177e4SLinus Torvalds /* 10181da177e4SLinus Torvalds * According to RFC 1390, FDDI devices should accept ARP 10191da177e4SLinus Torvalds * hardware types of 1 (Ethernet). However, to be more 10201da177e4SLinus Torvalds * robust, we'll accept hardware types of either 1 (Ethernet) 10211da177e4SLinus Torvalds * or 6 (IEEE 802.2). 10221da177e4SLinus Torvalds */ 10231da177e4SLinus Torvalds if (r->arp_ha.sa_family != ARPHRD_FDDI && 10241da177e4SLinus Torvalds r->arp_ha.sa_family != ARPHRD_ETHER && 10251da177e4SLinus Torvalds r->arp_ha.sa_family != ARPHRD_IEEE802) 10261da177e4SLinus Torvalds return -EINVAL; 10271da177e4SLinus Torvalds break; 10281da177e4SLinus Torvalds #endif 10291da177e4SLinus Torvalds default: 10301da177e4SLinus Torvalds if (r->arp_ha.sa_family != dev->type) 10311da177e4SLinus Torvalds return -EINVAL; 10321da177e4SLinus Torvalds break; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev); 10361da177e4SLinus Torvalds err = PTR_ERR(neigh); 10371da177e4SLinus Torvalds if (!IS_ERR(neigh)) { 10381da177e4SLinus Torvalds unsigned state = NUD_STALE; 10391da177e4SLinus Torvalds if (r->arp_flags & ATF_PERM) 10401da177e4SLinus Torvalds state = NUD_PERMANENT; 10411da177e4SLinus Torvalds err = neigh_update(neigh, (r->arp_flags&ATF_COM) ? 10421da177e4SLinus Torvalds r->arp_ha.sa_data : NULL, state, 10431da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 10441da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 10451da177e4SLinus Torvalds neigh_release(neigh); 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds return err; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds static unsigned arp_state_to_flags(struct neighbour *neigh) 10511da177e4SLinus Torvalds { 10521da177e4SLinus Torvalds unsigned flags = 0; 10531da177e4SLinus Torvalds if (neigh->nud_state&NUD_PERMANENT) 10541da177e4SLinus Torvalds flags = ATF_PERM|ATF_COM; 10551da177e4SLinus Torvalds else if (neigh->nud_state&NUD_VALID) 10561da177e4SLinus Torvalds flags = ATF_COM; 10571da177e4SLinus Torvalds return flags; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /* 10611da177e4SLinus Torvalds * Get an ARP cache entry. 10621da177e4SLinus Torvalds */ 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds static int arp_req_get(struct arpreq *r, struct net_device *dev) 10651da177e4SLinus Torvalds { 10661da177e4SLinus Torvalds u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr; 10671da177e4SLinus Torvalds struct neighbour *neigh; 10681da177e4SLinus Torvalds int err = -ENXIO; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds neigh = neigh_lookup(&arp_tbl, &ip, dev); 10711da177e4SLinus Torvalds if (neigh) { 10721da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 10731da177e4SLinus Torvalds memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len); 10741da177e4SLinus Torvalds r->arp_flags = arp_state_to_flags(neigh); 10751da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 10761da177e4SLinus Torvalds r->arp_ha.sa_family = dev->type; 10771da177e4SLinus Torvalds strlcpy(r->arp_dev, dev->name, sizeof(r->arp_dev)); 10781da177e4SLinus Torvalds neigh_release(neigh); 10791da177e4SLinus Torvalds err = 0; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds return err; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds static int arp_req_delete(struct arpreq *r, struct net_device * dev) 10851da177e4SLinus Torvalds { 10861da177e4SLinus Torvalds int err; 10871da177e4SLinus Torvalds u32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; 10881da177e4SLinus Torvalds struct neighbour *neigh; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds if (r->arp_flags & ATF_PUBL) { 10911da177e4SLinus Torvalds u32 mask = 10921da177e4SLinus Torvalds ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr; 10931da177e4SLinus Torvalds if (mask == 0xFFFFFFFF) 10941da177e4SLinus Torvalds return pneigh_delete(&arp_tbl, &ip, dev); 10951da177e4SLinus Torvalds if (mask == 0) { 10961da177e4SLinus Torvalds if (dev == NULL) { 10971da177e4SLinus Torvalds ipv4_devconf.proxy_arp = 0; 10981da177e4SLinus Torvalds return 0; 10991da177e4SLinus Torvalds } 1100e5ed6399SHerbert Xu if (__in_dev_get_rtnl(dev)) { 1101e5ed6399SHerbert Xu __in_dev_get_rtnl(dev)->cnf.proxy_arp = 0; 11021da177e4SLinus Torvalds return 0; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds return -ENXIO; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds return -EINVAL; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds if (dev == NULL) { 11101da177e4SLinus Torvalds struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, 11111da177e4SLinus Torvalds .tos = RTO_ONLINK } } }; 11121da177e4SLinus Torvalds struct rtable * rt; 11131da177e4SLinus Torvalds if ((err = ip_route_output_key(&rt, &fl)) != 0) 11141da177e4SLinus Torvalds return err; 11151da177e4SLinus Torvalds dev = rt->u.dst.dev; 11161da177e4SLinus Torvalds ip_rt_put(rt); 11171da177e4SLinus Torvalds if (!dev) 11181da177e4SLinus Torvalds return -EINVAL; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds err = -ENXIO; 11211da177e4SLinus Torvalds neigh = neigh_lookup(&arp_tbl, &ip, dev); 11221da177e4SLinus Torvalds if (neigh) { 11231da177e4SLinus Torvalds if (neigh->nud_state&~NUD_NOARP) 11241da177e4SLinus Torvalds err = neigh_update(neigh, NULL, NUD_FAILED, 11251da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 11261da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 11271da177e4SLinus Torvalds neigh_release(neigh); 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds return err; 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds /* 11331da177e4SLinus Torvalds * Handle an ARP layer I/O control request. 11341da177e4SLinus Torvalds */ 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds int arp_ioctl(unsigned int cmd, void __user *arg) 11371da177e4SLinus Torvalds { 11381da177e4SLinus Torvalds int err; 11391da177e4SLinus Torvalds struct arpreq r; 11401da177e4SLinus Torvalds struct net_device *dev = NULL; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds switch (cmd) { 11431da177e4SLinus Torvalds case SIOCDARP: 11441da177e4SLinus Torvalds case SIOCSARP: 11451da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 11461da177e4SLinus Torvalds return -EPERM; 11471da177e4SLinus Torvalds case SIOCGARP: 11481da177e4SLinus Torvalds err = copy_from_user(&r, arg, sizeof(struct arpreq)); 11491da177e4SLinus Torvalds if (err) 11501da177e4SLinus Torvalds return -EFAULT; 11511da177e4SLinus Torvalds break; 11521da177e4SLinus Torvalds default: 11531da177e4SLinus Torvalds return -EINVAL; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds if (r.arp_pa.sa_family != AF_INET) 11571da177e4SLinus Torvalds return -EPFNOSUPPORT; 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds if (!(r.arp_flags & ATF_PUBL) && 11601da177e4SLinus Torvalds (r.arp_flags & (ATF_NETMASK|ATF_DONTPUB))) 11611da177e4SLinus Torvalds return -EINVAL; 11621da177e4SLinus Torvalds if (!(r.arp_flags & ATF_NETMASK)) 11631da177e4SLinus Torvalds ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr = 11641da177e4SLinus Torvalds htonl(0xFFFFFFFFUL); 11651da177e4SLinus Torvalds rtnl_lock(); 11661da177e4SLinus Torvalds if (r.arp_dev[0]) { 11671da177e4SLinus Torvalds err = -ENODEV; 11681da177e4SLinus Torvalds if ((dev = __dev_get_by_name(r.arp_dev)) == NULL) 11691da177e4SLinus Torvalds goto out; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* Mmmm... It is wrong... ARPHRD_NETROM==0 */ 11721da177e4SLinus Torvalds if (!r.arp_ha.sa_family) 11731da177e4SLinus Torvalds r.arp_ha.sa_family = dev->type; 11741da177e4SLinus Torvalds err = -EINVAL; 11751da177e4SLinus Torvalds if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type) 11761da177e4SLinus Torvalds goto out; 11771da177e4SLinus Torvalds } else if (cmd == SIOCGARP) { 11781da177e4SLinus Torvalds err = -ENODEV; 11791da177e4SLinus Torvalds goto out; 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds switch(cmd) { 11831da177e4SLinus Torvalds case SIOCDARP: 11841da177e4SLinus Torvalds err = arp_req_delete(&r, dev); 11851da177e4SLinus Torvalds break; 11861da177e4SLinus Torvalds case SIOCSARP: 11871da177e4SLinus Torvalds err = arp_req_set(&r, dev); 11881da177e4SLinus Torvalds break; 11891da177e4SLinus Torvalds case SIOCGARP: 11901da177e4SLinus Torvalds err = arp_req_get(&r, dev); 11911da177e4SLinus Torvalds if (!err && copy_to_user(arg, &r, sizeof(r))) 11921da177e4SLinus Torvalds err = -EFAULT; 11931da177e4SLinus Torvalds break; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds out: 11961da177e4SLinus Torvalds rtnl_unlock(); 11971da177e4SLinus Torvalds return err; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds static int arp_netdev_event(struct notifier_block *this, unsigned long event, void *ptr) 12011da177e4SLinus Torvalds { 12021da177e4SLinus Torvalds struct net_device *dev = ptr; 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds switch (event) { 12051da177e4SLinus Torvalds case NETDEV_CHANGEADDR: 12061da177e4SLinus Torvalds neigh_changeaddr(&arp_tbl, dev); 12071da177e4SLinus Torvalds rt_cache_flush(0); 12081da177e4SLinus Torvalds break; 12091da177e4SLinus Torvalds default: 12101da177e4SLinus Torvalds break; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds return NOTIFY_DONE; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds static struct notifier_block arp_netdev_notifier = { 12171da177e4SLinus Torvalds .notifier_call = arp_netdev_event, 12181da177e4SLinus Torvalds }; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /* Note, that it is not on notifier chain. 12211da177e4SLinus Torvalds It is necessary, that this routine was called after route cache will be 12221da177e4SLinus Torvalds flushed. 12231da177e4SLinus Torvalds */ 12241da177e4SLinus Torvalds void arp_ifdown(struct net_device *dev) 12251da177e4SLinus Torvalds { 12261da177e4SLinus Torvalds neigh_ifdown(&arp_tbl, dev); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds /* 12311da177e4SLinus Torvalds * Called once on startup. 12321da177e4SLinus Torvalds */ 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds static struct packet_type arp_packet_type = { 12351da177e4SLinus Torvalds .type = __constant_htons(ETH_P_ARP), 12361da177e4SLinus Torvalds .func = arp_rcv, 12371da177e4SLinus Torvalds }; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds static int arp_proc_init(void); 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds void __init arp_init(void) 12421da177e4SLinus Torvalds { 12431da177e4SLinus Torvalds neigh_table_init(&arp_tbl); 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds dev_add_pack(&arp_packet_type); 12461da177e4SLinus Torvalds arp_proc_init(); 12471da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 12481da177e4SLinus Torvalds neigh_sysctl_register(NULL, &arp_tbl.parms, NET_IPV4, 12491da177e4SLinus Torvalds NET_IPV4_NEIGH, "ipv4", NULL, NULL); 12501da177e4SLinus Torvalds #endif 12511da177e4SLinus Torvalds register_netdevice_notifier(&arp_netdev_notifier); 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 12551da177e4SLinus Torvalds #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 12581da177e4SLinus Torvalds /* 12591da177e4SLinus Torvalds * ax25 -> ASCII conversion 12601da177e4SLinus Torvalds */ 12611da177e4SLinus Torvalds static char *ax2asc2(ax25_address *a, char *buf) 12621da177e4SLinus Torvalds { 12631da177e4SLinus Torvalds char c, *s; 12641da177e4SLinus Torvalds int n; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds for (n = 0, s = buf; n < 6; n++) { 12671da177e4SLinus Torvalds c = (a->ax25_call[n] >> 1) & 0x7F; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds if (c != ' ') *s++ = c; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds *s++ = '-'; 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) { 12751da177e4SLinus Torvalds *s++ = '1'; 12761da177e4SLinus Torvalds n -= 10; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds *s++ = n + '0'; 12801da177e4SLinus Torvalds *s++ = '\0'; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds if (*buf == '\0' || *buf == '-') 12831da177e4SLinus Torvalds return "*"; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds return buf; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds #endif /* CONFIG_AX25 */ 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds #define HBUFFERLEN 30 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds static void arp_format_neigh_entry(struct seq_file *seq, 12931da177e4SLinus Torvalds struct neighbour *n) 12941da177e4SLinus Torvalds { 12951da177e4SLinus Torvalds char hbuffer[HBUFFERLEN]; 12961da177e4SLinus Torvalds const char hexbuf[] = "0123456789ABCDEF"; 12971da177e4SLinus Torvalds int k, j; 12981da177e4SLinus Torvalds char tbuf[16]; 12991da177e4SLinus Torvalds struct net_device *dev = n->dev; 13001da177e4SLinus Torvalds int hatype = dev->type; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds read_lock(&n->lock); 13031da177e4SLinus Torvalds /* Convert hardware address to XX:XX:XX:XX ... form. */ 13041da177e4SLinus Torvalds #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 13051da177e4SLinus Torvalds if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM) 13061da177e4SLinus Torvalds ax2asc2((ax25_address *)n->ha, hbuffer); 13071da177e4SLinus Torvalds else { 13081da177e4SLinus Torvalds #endif 13091da177e4SLinus Torvalds for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) { 13101da177e4SLinus Torvalds hbuffer[k++] = hexbuf[(n->ha[j] >> 4) & 15]; 13111da177e4SLinus Torvalds hbuffer[k++] = hexbuf[n->ha[j] & 15]; 13121da177e4SLinus Torvalds hbuffer[k++] = ':'; 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds hbuffer[--k] = 0; 13151da177e4SLinus Torvalds #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE) 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds #endif 13181da177e4SLinus Torvalds sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(*(u32*)n->primary_key)); 13191da177e4SLinus Torvalds seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n", 13201da177e4SLinus Torvalds tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name); 13211da177e4SLinus Torvalds read_unlock(&n->lock); 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds static void arp_format_pneigh_entry(struct seq_file *seq, 13251da177e4SLinus Torvalds struct pneigh_entry *n) 13261da177e4SLinus Torvalds { 13271da177e4SLinus Torvalds struct net_device *dev = n->dev; 13281da177e4SLinus Torvalds int hatype = dev ? dev->type : 0; 13291da177e4SLinus Torvalds char tbuf[16]; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(*(u32*)n->key)); 13321da177e4SLinus Torvalds seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n", 13331da177e4SLinus Torvalds tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00", 13341da177e4SLinus Torvalds dev ? dev->name : "*"); 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds static int arp_seq_show(struct seq_file *seq, void *v) 13381da177e4SLinus Torvalds { 13391da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 13401da177e4SLinus Torvalds seq_puts(seq, "IP address HW type Flags " 13411da177e4SLinus Torvalds "HW address Mask Device\n"); 13421da177e4SLinus Torvalds } else { 13431da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds if (state->flags & NEIGH_SEQ_IS_PNEIGH) 13461da177e4SLinus Torvalds arp_format_pneigh_entry(seq, v); 13471da177e4SLinus Torvalds else 13481da177e4SLinus Torvalds arp_format_neigh_entry(seq, v); 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds return 0; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds static void *arp_seq_start(struct seq_file *seq, loff_t *pos) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds /* Don't want to confuse "arp -a" w/ magic entries, 13571da177e4SLinus Torvalds * so we tell the generic iterator to skip NUD_NOARP. 13581da177e4SLinus Torvalds */ 13591da177e4SLinus Torvalds return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP); 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds static struct seq_operations arp_seq_ops = { 13651da177e4SLinus Torvalds .start = arp_seq_start, 13661da177e4SLinus Torvalds .next = neigh_seq_next, 13671da177e4SLinus Torvalds .stop = neigh_seq_stop, 13681da177e4SLinus Torvalds .show = arp_seq_show, 13691da177e4SLinus Torvalds }; 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds static int arp_seq_open(struct inode *inode, struct file *file) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds struct seq_file *seq; 13741da177e4SLinus Torvalds int rc = -ENOMEM; 13751da177e4SLinus Torvalds struct neigh_seq_state *s = kmalloc(sizeof(*s), GFP_KERNEL); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds if (!s) 13781da177e4SLinus Torvalds goto out; 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds memset(s, 0, sizeof(*s)); 13811da177e4SLinus Torvalds rc = seq_open(file, &arp_seq_ops); 13821da177e4SLinus Torvalds if (rc) 13831da177e4SLinus Torvalds goto out_kfree; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds seq = file->private_data; 13861da177e4SLinus Torvalds seq->private = s; 13871da177e4SLinus Torvalds out: 13881da177e4SLinus Torvalds return rc; 13891da177e4SLinus Torvalds out_kfree: 13901da177e4SLinus Torvalds kfree(s); 13911da177e4SLinus Torvalds goto out; 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds static struct file_operations arp_seq_fops = { 13951da177e4SLinus Torvalds .owner = THIS_MODULE, 13961da177e4SLinus Torvalds .open = arp_seq_open, 13971da177e4SLinus Torvalds .read = seq_read, 13981da177e4SLinus Torvalds .llseek = seq_lseek, 13991da177e4SLinus Torvalds .release = seq_release_private, 14001da177e4SLinus Torvalds }; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds static int __init arp_proc_init(void) 14031da177e4SLinus Torvalds { 14041da177e4SLinus Torvalds if (!proc_net_fops_create("arp", S_IRUGO, &arp_seq_fops)) 14051da177e4SLinus Torvalds return -ENOMEM; 14061da177e4SLinus Torvalds return 0; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds #else /* CONFIG_PROC_FS */ 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds static int __init arp_proc_init(void) 14121da177e4SLinus Torvalds { 14131da177e4SLinus Torvalds return 0; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds EXPORT_SYMBOL(arp_broken_ops); 14191da177e4SLinus Torvalds EXPORT_SYMBOL(arp_find); 14201da177e4SLinus Torvalds EXPORT_SYMBOL(arp_rcv); 14211da177e4SLinus Torvalds EXPORT_SYMBOL(arp_create); 14221da177e4SLinus Torvalds EXPORT_SYMBOL(arp_xmit); 14231da177e4SLinus Torvalds EXPORT_SYMBOL(arp_send); 14241da177e4SLinus Torvalds EXPORT_SYMBOL(arp_tbl); 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds #if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE) 14271da177e4SLinus Torvalds EXPORT_SYMBOL(clip_tbl_hook); 14281da177e4SLinus Torvalds #endif 1429