1f30c2269SUwe Zeisberger /* linux/net/ipv4/arp.c 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * Copyright (C) 1994 by Florian La Roche 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This module implements the Address Resolution Protocol ARP (RFC 826), 61da177e4SLinus Torvalds * which is used to convert IP addresses (or in the future maybe other 71da177e4SLinus Torvalds * high-level addresses) into a low-level hardware address (like an Ethernet 81da177e4SLinus Torvalds * address). 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 111da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 121da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 131da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixes: 161da177e4SLinus Torvalds * Alan Cox : Removed the Ethernet assumptions in 171da177e4SLinus Torvalds * Florian's code 181da177e4SLinus Torvalds * Alan Cox : Fixed some small errors in the ARP 191da177e4SLinus Torvalds * logic 201da177e4SLinus Torvalds * Alan Cox : Allow >4K in /proc 211da177e4SLinus Torvalds * Alan Cox : Make ARP add its own protocol entry 221da177e4SLinus Torvalds * Ross Martin : Rewrote arp_rcv() and arp_get_info() 231da177e4SLinus Torvalds * Stephen Henson : Add AX25 support to arp_get_info() 241da177e4SLinus Torvalds * Alan Cox : Drop data when a device is downed. 251da177e4SLinus Torvalds * Alan Cox : Use init_timer(). 261da177e4SLinus Torvalds * Alan Cox : Double lock fixes. 271da177e4SLinus Torvalds * Martin Seine : Move the arphdr structure 281da177e4SLinus Torvalds * to if_arp.h for compatibility. 291da177e4SLinus Torvalds * with BSD based programs. 301da177e4SLinus Torvalds * Andrew Tridgell : Added ARP netmask code and 311da177e4SLinus Torvalds * re-arranged proxy handling. 321da177e4SLinus Torvalds * Alan Cox : Changed to use notifiers. 331da177e4SLinus Torvalds * Niibe Yutaka : Reply for this device or proxies only. 341da177e4SLinus Torvalds * Alan Cox : Don't proxy across hardware types! 351da177e4SLinus Torvalds * Jonathan Naylor : Added support for NET/ROM. 361da177e4SLinus Torvalds * Mike Shaver : RFC1122 checks. 371da177e4SLinus Torvalds * Jonathan Naylor : Only lookup the hardware address for 381da177e4SLinus Torvalds * the correct hardware type. 391da177e4SLinus Torvalds * Germano Caronni : Assorted subtle races. 401da177e4SLinus Torvalds * Craig Schlenter : Don't modify permanent entry 411da177e4SLinus Torvalds * during arp_rcv. 421da177e4SLinus Torvalds * Russ Nelson : Tidied up a few bits. 431da177e4SLinus Torvalds * Alexey Kuznetsov: Major changes to caching and behaviour, 441da177e4SLinus Torvalds * eg intelligent arp probing and 451da177e4SLinus Torvalds * generation 461da177e4SLinus Torvalds * of host down events. 471da177e4SLinus Torvalds * Alan Cox : Missing unlock in device events. 481da177e4SLinus Torvalds * Eckes : ARP ioctl control errors. 491da177e4SLinus Torvalds * Alexey Kuznetsov: Arp free fix. 501da177e4SLinus Torvalds * Manuel Rodriguez: Gratuitous ARP. 511da177e4SLinus Torvalds * Jonathan Layes : Added arpd support through kerneld 521da177e4SLinus Torvalds * message queue (960314) 531da177e4SLinus Torvalds * Mike Shaver : /proc/sys/net/ipv4/arp_* support 541da177e4SLinus Torvalds * Mike McLagan : Routing by source 551da177e4SLinus Torvalds * Stuart Cheshire : Metricom and grat arp fixes 561da177e4SLinus Torvalds * *** FOR 2.1 clean this up *** 571da177e4SLinus Torvalds * Lawrence V. Stefani: (08/12/96) Added FDDI support. 581da177e4SLinus Torvalds * Alan Cox : Took the AP1000 nasty FDDI hack and 591da177e4SLinus Torvalds * folded into the mainstream FDDI code. 601da177e4SLinus Torvalds * Ack spit, Linus how did you allow that 611da177e4SLinus Torvalds * one in... 621da177e4SLinus Torvalds * Jes Sorensen : Make FDDI work again in 2.1.x and 631da177e4SLinus Torvalds * clean up the APFDDI & gen. FDDI bits. 641da177e4SLinus Torvalds * Alexey Kuznetsov: new arp state machine; 651da177e4SLinus Torvalds * now it is in net/core/neighbour.c. 661da177e4SLinus Torvalds * Krzysztof Halasa: Added Frame Relay ARP support. 671da177e4SLinus Torvalds * Arnaldo C. Melo : convert /proc/net/arp to seq_file 681da177e4SLinus Torvalds * Shmulik Hen: Split arp_send to arp_create and 691da177e4SLinus Torvalds * arp_xmit so intermediate drivers like 701da177e4SLinus Torvalds * bonding can change the skb before 711da177e4SLinus Torvalds * sending (e.g. insert 8021q tag). 721da177e4SLinus Torvalds * Harald Welte : convert to make use of jenkins hash 7365324144SJesper Dangaard Brouer * Jesper D. Brouer: Proxy ARP PVLAN RFC 3069 support. 741da177e4SLinus Torvalds */ 751da177e4SLinus Torvalds 7691df42beSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7791df42beSJoe Perches 781da177e4SLinus Torvalds #include <linux/module.h> 791da177e4SLinus Torvalds #include <linux/types.h> 801da177e4SLinus Torvalds #include <linux/string.h> 811da177e4SLinus Torvalds #include <linux/kernel.h> 824fc268d2SRandy Dunlap #include <linux/capability.h> 831da177e4SLinus Torvalds #include <linux/socket.h> 841da177e4SLinus Torvalds #include <linux/sockios.h> 851da177e4SLinus Torvalds #include <linux/errno.h> 861da177e4SLinus Torvalds #include <linux/in.h> 871da177e4SLinus Torvalds #include <linux/mm.h> 881da177e4SLinus Torvalds #include <linux/inet.h> 8914c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h> 901da177e4SLinus Torvalds #include <linux/netdevice.h> 911da177e4SLinus Torvalds #include <linux/etherdevice.h> 921da177e4SLinus Torvalds #include <linux/fddidevice.h> 931da177e4SLinus Torvalds #include <linux/if_arp.h> 941da177e4SLinus Torvalds #include <linux/skbuff.h> 951da177e4SLinus Torvalds #include <linux/proc_fs.h> 961da177e4SLinus Torvalds #include <linux/seq_file.h> 971da177e4SLinus Torvalds #include <linux/stat.h> 981da177e4SLinus Torvalds #include <linux/init.h> 991da177e4SLinus Torvalds #include <linux/net.h> 1001da177e4SLinus Torvalds #include <linux/rcupdate.h> 1015a0e3ad6STejun Heo #include <linux/slab.h> 1021da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 1031da177e4SLinus Torvalds #include <linux/sysctl.h> 1041da177e4SLinus Torvalds #endif 1051da177e4SLinus Torvalds 106457c4cbcSEric W. Biederman #include <net/net_namespace.h> 1071da177e4SLinus Torvalds #include <net/ip.h> 1081da177e4SLinus Torvalds #include <net/icmp.h> 1091da177e4SLinus Torvalds #include <net/route.h> 1101da177e4SLinus Torvalds #include <net/protocol.h> 1111da177e4SLinus Torvalds #include <net/tcp.h> 1121da177e4SLinus Torvalds #include <net/sock.h> 1131da177e4SLinus Torvalds #include <net/arp.h> 1141da177e4SLinus Torvalds #include <net/ax25.h> 1151da177e4SLinus Torvalds #include <net/netrom.h> 1161da177e4SLinus Torvalds 117deffd777SChangli Gao #include <linux/uaccess.h> 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds #include <linux/netfilter_arp.h> 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * Interface to generic neighbour cache. 1231da177e4SLinus Torvalds */ 1242c2aba6cSDavid S. Miller static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd); 1251da177e4SLinus Torvalds static int arp_constructor(struct neighbour *neigh); 1261da177e4SLinus Torvalds static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb); 1271da177e4SLinus Torvalds static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb); 1281da177e4SLinus Torvalds static void parp_redo(struct sk_buff *skb); 1291da177e4SLinus Torvalds 13089d69d2bSStephen Hemminger static const struct neigh_ops arp_generic_ops = { 1311da177e4SLinus Torvalds .family = AF_INET, 1321da177e4SLinus Torvalds .solicit = arp_solicit, 1331da177e4SLinus Torvalds .error_report = arp_error_report, 1341da177e4SLinus Torvalds .output = neigh_resolve_output, 1351da177e4SLinus Torvalds .connected_output = neigh_connected_output, 1361da177e4SLinus Torvalds }; 1371da177e4SLinus Torvalds 13889d69d2bSStephen Hemminger static const struct neigh_ops arp_hh_ops = { 1391da177e4SLinus Torvalds .family = AF_INET, 1401da177e4SLinus Torvalds .solicit = arp_solicit, 1411da177e4SLinus Torvalds .error_report = arp_error_report, 1421da177e4SLinus Torvalds .output = neigh_resolve_output, 1431da177e4SLinus Torvalds .connected_output = neigh_resolve_output, 1441da177e4SLinus Torvalds }; 1451da177e4SLinus Torvalds 14689d69d2bSStephen Hemminger static const struct neigh_ops arp_direct_ops = { 1471da177e4SLinus Torvalds .family = AF_INET, 1488f40b161SDavid S. Miller .output = neigh_direct_output, 1498f40b161SDavid S. Miller .connected_output = neigh_direct_output, 1501da177e4SLinus Torvalds }; 1511da177e4SLinus Torvalds 152a64de47cSstephen hemminger static const struct neigh_ops arp_broken_ops = { 1531da177e4SLinus Torvalds .family = AF_INET, 1541da177e4SLinus Torvalds .solicit = arp_solicit, 1551da177e4SLinus Torvalds .error_report = arp_error_report, 1561da177e4SLinus Torvalds .output = neigh_compat_output, 1571da177e4SLinus Torvalds .connected_output = neigh_compat_output, 1581da177e4SLinus Torvalds }; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds struct neigh_table arp_tbl = { 1611da177e4SLinus Torvalds .family = AF_INET, 1621da177e4SLinus Torvalds .key_len = 4, 1631da177e4SLinus Torvalds .hash = arp_hash, 1641da177e4SLinus Torvalds .constructor = arp_constructor, 1651da177e4SLinus Torvalds .proxy_redo = parp_redo, 1661da177e4SLinus Torvalds .id = "arp_cache", 1671da177e4SLinus Torvalds .parms = { 1681da177e4SLinus Torvalds .tbl = &arp_tbl, 1691da177e4SLinus Torvalds .base_reachable_time = 30 * HZ, 1701da177e4SLinus Torvalds .retrans_time = 1 * HZ, 1711da177e4SLinus Torvalds .gc_staletime = 60 * HZ, 1721da177e4SLinus Torvalds .reachable_time = 30 * HZ, 1731da177e4SLinus Torvalds .delay_probe_time = 5 * HZ, 1748b5c171bSEric Dumazet .queue_len_bytes = 64*1024, 1751da177e4SLinus Torvalds .ucast_probes = 3, 1761da177e4SLinus Torvalds .mcast_probes = 3, 1771da177e4SLinus Torvalds .anycast_delay = 1 * HZ, 1781da177e4SLinus Torvalds .proxy_delay = (8 * HZ) / 10, 1791da177e4SLinus Torvalds .proxy_qlen = 64, 1801da177e4SLinus Torvalds .locktime = 1 * HZ, 1811da177e4SLinus Torvalds }, 1821da177e4SLinus Torvalds .gc_interval = 30 * HZ, 1831da177e4SLinus Torvalds .gc_thresh1 = 128, 1841da177e4SLinus Torvalds .gc_thresh2 = 512, 1851da177e4SLinus Torvalds .gc_thresh3 = 1024, 1861da177e4SLinus Torvalds }; 1874bc2f18bSEric Dumazet EXPORT_SYMBOL(arp_tbl); 1881da177e4SLinus Torvalds 189714e85beSAl Viro int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds switch (dev->type) { 1921da177e4SLinus Torvalds case ARPHRD_ETHER: 1931da177e4SLinus Torvalds case ARPHRD_FDDI: 1941da177e4SLinus Torvalds case ARPHRD_IEEE802: 1951da177e4SLinus Torvalds ip_eth_mc_map(addr, haddr); 1961da177e4SLinus Torvalds return 0; 1971da177e4SLinus Torvalds case ARPHRD_INFINIBAND: 198a9e527e3SRolf Manderscheid ip_ib_mc_map(addr, dev->broadcast, haddr); 1991da177e4SLinus Torvalds return 0; 20093ca3bb5STimo Teräs case ARPHRD_IPGRE: 20193ca3bb5STimo Teräs ip_ipgre_mc_map(addr, dev->broadcast, haddr); 20293ca3bb5STimo Teräs return 0; 2031da177e4SLinus Torvalds default: 2041da177e4SLinus Torvalds if (dir) { 2051da177e4SLinus Torvalds memcpy(haddr, dev->broadcast, dev->addr_len); 2061da177e4SLinus Torvalds return 0; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds return -EINVAL; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds 213d6bf7817SEric Dumazet static u32 arp_hash(const void *pkey, 214d6bf7817SEric Dumazet const struct net_device *dev, 2152c2aba6cSDavid S. Miller __u32 *hash_rnd) 2161da177e4SLinus Torvalds { 2172c2aba6cSDavid S. Miller return arp_hashfn(*(u32 *)pkey, dev, *hash_rnd); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static int arp_constructor(struct neighbour *neigh) 2211da177e4SLinus Torvalds { 222fd683222SAl Viro __be32 addr = *(__be32 *)neigh->primary_key; 2231da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 2241da177e4SLinus Torvalds struct in_device *in_dev; 2251da177e4SLinus Torvalds struct neigh_parms *parms; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds rcu_read_lock(); 228e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(dev); 2291da177e4SLinus Torvalds if (in_dev == NULL) { 2301da177e4SLinus Torvalds rcu_read_unlock(); 2311da177e4SLinus Torvalds return -EINVAL; 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds 234c346dca1SYOSHIFUJI Hideaki neigh->type = inet_addr_type(dev_net(dev), addr); 235a79878f0SDenis V. Lunev 2361da177e4SLinus Torvalds parms = in_dev->arp_parms; 2371da177e4SLinus Torvalds __neigh_parms_put(neigh->parms); 2381da177e4SLinus Torvalds neigh->parms = neigh_parms_clone(parms); 2391da177e4SLinus Torvalds rcu_read_unlock(); 2401da177e4SLinus Torvalds 2413b04dddeSStephen Hemminger if (!dev->header_ops) { 2421da177e4SLinus Torvalds neigh->nud_state = NUD_NOARP; 2431da177e4SLinus Torvalds neigh->ops = &arp_direct_ops; 2448f40b161SDavid S. Miller neigh->output = neigh_direct_output; 2451da177e4SLinus Torvalds } else { 2461da177e4SLinus Torvalds /* Good devices (checked by reading texts, but only Ethernet is 2471da177e4SLinus Torvalds tested) 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds ARPHRD_ETHER: (ethernet, apfddi) 2501da177e4SLinus Torvalds ARPHRD_FDDI: (fddi) 2511da177e4SLinus Torvalds ARPHRD_IEEE802: (tr) 2521da177e4SLinus Torvalds ARPHRD_METRICOM: (strip) 2531da177e4SLinus Torvalds ARPHRD_ARCNET: 2541da177e4SLinus Torvalds etc. etc. etc. 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds ARPHRD_IPDDP will also work, if author repairs it. 2571da177e4SLinus Torvalds I did not it, because this driver does not work even 2581da177e4SLinus Torvalds in old paradigm. 2591da177e4SLinus Torvalds */ 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds #if 1 2621da177e4SLinus Torvalds /* So... these "amateur" devices are hopeless. 2631da177e4SLinus Torvalds The only thing, that I can say now: 2641da177e4SLinus Torvalds It is very sad that we need to keep ugly obsolete 2651da177e4SLinus Torvalds code to make them happy. 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds They should be moved to more reasonable state, now 2681da177e4SLinus Torvalds they use rebuild_header INSTEAD OF hard_start_xmit!!! 2691da177e4SLinus Torvalds Besides that, they are sort of out of date 2701da177e4SLinus Torvalds (a lot of redundant clones/copies, useless in 2.1), 2711da177e4SLinus Torvalds I wonder why people believe that they work. 2721da177e4SLinus Torvalds */ 2731da177e4SLinus Torvalds switch (dev->type) { 2741da177e4SLinus Torvalds default: 2751da177e4SLinus Torvalds break; 2761da177e4SLinus Torvalds case ARPHRD_ROSE: 27740e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25) 2781da177e4SLinus Torvalds case ARPHRD_AX25: 27940e4783eSIgor Maravic #if IS_ENABLED(CONFIG_NETROM) 2801da177e4SLinus Torvalds case ARPHRD_NETROM: 2811da177e4SLinus Torvalds #endif 2821da177e4SLinus Torvalds neigh->ops = &arp_broken_ops; 2831da177e4SLinus Torvalds neigh->output = neigh->ops->output; 2841da177e4SLinus Torvalds return 0; 285deffd777SChangli Gao #else 286deffd777SChangli Gao break; 2871da177e4SLinus Torvalds #endif 288deffd777SChangli Gao } 2891da177e4SLinus Torvalds #endif 2901da177e4SLinus Torvalds if (neigh->type == RTN_MULTICAST) { 2911da177e4SLinus Torvalds neigh->nud_state = NUD_NOARP; 2921da177e4SLinus Torvalds arp_mc_map(addr, neigh->ha, dev, 1); 2931da177e4SLinus Torvalds } else if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) { 2941da177e4SLinus Torvalds neigh->nud_state = NUD_NOARP; 2951da177e4SLinus Torvalds memcpy(neigh->ha, dev->dev_addr, dev->addr_len); 296deffd777SChangli Gao } else if (neigh->type == RTN_BROADCAST || 297deffd777SChangli Gao (dev->flags & IFF_POINTOPOINT)) { 2981da177e4SLinus Torvalds neigh->nud_state = NUD_NOARP; 2991da177e4SLinus Torvalds memcpy(neigh->ha, dev->broadcast, dev->addr_len); 3001da177e4SLinus Torvalds } 3013b04dddeSStephen Hemminger 3023b04dddeSStephen Hemminger if (dev->header_ops->cache) 3031da177e4SLinus Torvalds neigh->ops = &arp_hh_ops; 3041da177e4SLinus Torvalds else 3051da177e4SLinus Torvalds neigh->ops = &arp_generic_ops; 3063b04dddeSStephen Hemminger 3071da177e4SLinus Torvalds if (neigh->nud_state & NUD_VALID) 3081da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 3091da177e4SLinus Torvalds else 3101da177e4SLinus Torvalds neigh->output = neigh->ops->output; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds return 0; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb) 3161da177e4SLinus Torvalds { 3171da177e4SLinus Torvalds dst_link_failure(skb); 3181da177e4SLinus Torvalds kfree_skb(skb); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb) 3221da177e4SLinus Torvalds { 323a61ced5dSAl Viro __be32 saddr = 0; 324cf0be880SCong Wang u8 dst_ha[MAX_ADDR_LEN], *dst_hw = NULL; 3251da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 326a61ced5dSAl Viro __be32 target = *(__be32 *)neigh->primary_key; 3271da177e4SLinus Torvalds int probes = atomic_read(&neigh->probes); 3284b4194c4SEric Dumazet struct in_device *in_dev; 3291da177e4SLinus Torvalds 3304b4194c4SEric Dumazet rcu_read_lock(); 3314b4194c4SEric Dumazet in_dev = __in_dev_get_rcu(dev); 3324b4194c4SEric Dumazet if (!in_dev) { 3334b4194c4SEric Dumazet rcu_read_unlock(); 3341da177e4SLinus Torvalds return; 3354b4194c4SEric Dumazet } 3361da177e4SLinus Torvalds switch (IN_DEV_ARP_ANNOUNCE(in_dev)) { 3371da177e4SLinus Torvalds default: 3381da177e4SLinus Torvalds case 0: /* By default announce any local IP */ 339deffd777SChangli Gao if (skb && inet_addr_type(dev_net(dev), 340deffd777SChangli Gao ip_hdr(skb)->saddr) == RTN_LOCAL) 341eddc9ec5SArnaldo Carvalho de Melo saddr = ip_hdr(skb)->saddr; 3421da177e4SLinus Torvalds break; 3431da177e4SLinus Torvalds case 1: /* Restrict announcements of saddr in same subnet */ 3441da177e4SLinus Torvalds if (!skb) 3451da177e4SLinus Torvalds break; 346eddc9ec5SArnaldo Carvalho de Melo saddr = ip_hdr(skb)->saddr; 347c346dca1SYOSHIFUJI Hideaki if (inet_addr_type(dev_net(dev), saddr) == RTN_LOCAL) { 3481da177e4SLinus Torvalds /* saddr should be known to target */ 3491da177e4SLinus Torvalds if (inet_addr_onlink(in_dev, target, saddr)) 3501da177e4SLinus Torvalds break; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds saddr = 0; 3531da177e4SLinus Torvalds break; 3541da177e4SLinus Torvalds case 2: /* Avoid secondary IPs, get a primary/preferred one */ 3551da177e4SLinus Torvalds break; 3561da177e4SLinus Torvalds } 3574b4194c4SEric Dumazet rcu_read_unlock(); 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds if (!saddr) 3601da177e4SLinus Torvalds saddr = inet_select_addr(dev, target, RT_SCOPE_LINK); 3611da177e4SLinus Torvalds 362deffd777SChangli Gao probes -= neigh->parms->ucast_probes; 363deffd777SChangli Gao if (probes < 0) { 3641da177e4SLinus Torvalds if (!(neigh->nud_state & NUD_VALID)) 36591df42beSJoe Perches pr_debug("trying to ucast probe in NUD_INVALID\n"); 3669650388bSEric Dumazet neigh_ha_snapshot(dst_ha, neigh, dev); 367cf0be880SCong Wang dst_hw = dst_ha; 368deffd777SChangli Gao } else { 369deffd777SChangli Gao probes -= neigh->parms->app_probes; 370deffd777SChangli Gao if (probes < 0) { 3711da177e4SLinus Torvalds #ifdef CONFIG_ARPD 3721da177e4SLinus Torvalds neigh_app_ns(neigh); 3731da177e4SLinus Torvalds #endif 3741da177e4SLinus Torvalds return; 3751da177e4SLinus Torvalds } 376deffd777SChangli Gao } 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr, 379cf0be880SCong Wang dst_hw, dev->dev_addr, NULL); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3829bd85e32SDenis V. Lunev static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip) 3831da177e4SLinus Torvalds { 3841da177e4SLinus Torvalds int scope; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds switch (IN_DEV_ARP_IGNORE(in_dev)) { 3871da177e4SLinus Torvalds case 0: /* Reply, the tip is already validated */ 3881da177e4SLinus Torvalds return 0; 3891da177e4SLinus Torvalds case 1: /* Reply only if tip is configured on the incoming interface */ 3901da177e4SLinus Torvalds sip = 0; 3911da177e4SLinus Torvalds scope = RT_SCOPE_HOST; 3921da177e4SLinus Torvalds break; 3931da177e4SLinus Torvalds case 2: /* 3941da177e4SLinus Torvalds * Reply only if tip is configured on the incoming interface 3951da177e4SLinus Torvalds * and is in same subnet as sip 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds scope = RT_SCOPE_HOST; 3981da177e4SLinus Torvalds break; 3991da177e4SLinus Torvalds case 3: /* Do not reply for scope host addresses */ 4001da177e4SLinus Torvalds sip = 0; 4011da177e4SLinus Torvalds scope = RT_SCOPE_LINK; 4021da177e4SLinus Torvalds break; 4031da177e4SLinus Torvalds case 4: /* Reserved */ 4041da177e4SLinus Torvalds case 5: 4051da177e4SLinus Torvalds case 6: 4061da177e4SLinus Torvalds case 7: 4071da177e4SLinus Torvalds return 0; 4081da177e4SLinus Torvalds case 8: /* Do not reply */ 4091da177e4SLinus Torvalds return 1; 4101da177e4SLinus Torvalds default: 4111da177e4SLinus Torvalds return 0; 4121da177e4SLinus Torvalds } 4139bd85e32SDenis V. Lunev return !inet_confirm_addr(in_dev, sip, tip, scope); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 416ed9bad06SAl Viro static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev) 4171da177e4SLinus Torvalds { 4181da177e4SLinus Torvalds struct rtable *rt; 4191da177e4SLinus Torvalds int flag = 0; 4201da177e4SLinus Torvalds /*unsigned long now; */ 421ca12a1a4SPavel Emelyanov struct net *net = dev_net(dev); 4221da177e4SLinus Torvalds 42378fbfd8aSDavid S. Miller rt = ip_route_output(net, sip, tip, 0, 0); 424b23dd4feSDavid S. Miller if (IS_ERR(rt)) 4251da177e4SLinus Torvalds return 1; 426d8d1f30bSChangli Gao if (rt->dst.dev != dev) { 427de0744afSPavel Emelyanov NET_INC_STATS_BH(net, LINUX_MIB_ARPFILTER); 4281da177e4SLinus Torvalds flag = 1; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds ip_rt_put(rt); 4311da177e4SLinus Torvalds return flag; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* OBSOLETE FUNCTIONS */ 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* 4371da177e4SLinus Torvalds * Find an arp mapping in the cache. If not found, post a request. 4381da177e4SLinus Torvalds * 4391da177e4SLinus Torvalds * It is very UGLY routine: it DOES NOT use skb->dst->neighbour, 4401da177e4SLinus Torvalds * even if it exists. It is supposed that skb->dev was mangled 4411da177e4SLinus Torvalds * by a virtual device (eql, shaper). Nobody but broken devices 4421da177e4SLinus Torvalds * is allowed to use this function, it is scheduled to be removed. --ANK 4431da177e4SLinus Torvalds */ 4441da177e4SLinus Torvalds 445deffd777SChangli Gao static int arp_set_predefined(int addr_hint, unsigned char *haddr, 446deffd777SChangli Gao __be32 paddr, struct net_device *dev) 4471da177e4SLinus Torvalds { 4481da177e4SLinus Torvalds switch (addr_hint) { 4491da177e4SLinus Torvalds case RTN_LOCAL: 45091df42beSJoe Perches pr_debug("arp called for own IP address\n"); 4511da177e4SLinus Torvalds memcpy(haddr, dev->dev_addr, dev->addr_len); 4521da177e4SLinus Torvalds return 1; 4531da177e4SLinus Torvalds case RTN_MULTICAST: 4541da177e4SLinus Torvalds arp_mc_map(paddr, haddr, dev, 1); 4551da177e4SLinus Torvalds return 1; 4561da177e4SLinus Torvalds case RTN_BROADCAST: 4571da177e4SLinus Torvalds memcpy(haddr, dev->broadcast, dev->addr_len); 4581da177e4SLinus Torvalds return 1; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds return 0; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds int arp_find(unsigned char *haddr, struct sk_buff *skb) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds struct net_device *dev = skb->dev; 467fd683222SAl Viro __be32 paddr; 4681da177e4SLinus Torvalds struct neighbour *n; 4691da177e4SLinus Torvalds 470adf30907SEric Dumazet if (!skb_dst(skb)) { 47191df42beSJoe Perches pr_debug("arp_find is called with dst==NULL\n"); 4721da177e4SLinus Torvalds kfree_skb(skb); 4731da177e4SLinus Torvalds return 1; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 476f8126f1dSDavid S. Miller paddr = rt_nexthop(skb_rtable(skb), ip_hdr(skb)->daddr); 477deffd777SChangli Gao if (arp_set_predefined(inet_addr_type(dev_net(dev), paddr), haddr, 478deffd777SChangli Gao paddr, dev)) 4791da177e4SLinus Torvalds return 0; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds n = __neigh_lookup(&arp_tbl, &paddr, dev, 1); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds if (n) { 4841da177e4SLinus Torvalds n->used = jiffies; 4851da177e4SLinus Torvalds if (n->nud_state & NUD_VALID || neigh_event_send(n, skb) == 0) { 4860ed8ddf4SEric Dumazet neigh_ha_snapshot(haddr, n, dev); 4871da177e4SLinus Torvalds neigh_release(n); 4881da177e4SLinus Torvalds return 0; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds neigh_release(n); 4911da177e4SLinus Torvalds } else 4921da177e4SLinus Torvalds kfree_skb(skb); 4931da177e4SLinus Torvalds return 1; 4941da177e4SLinus Torvalds } 4954bc2f18bSEric Dumazet EXPORT_SYMBOL(arp_find); 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* END OF OBSOLETE FUNCTIONS */ 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds /* 5001da177e4SLinus Torvalds * Check if we can use proxy ARP for this path 5011da177e4SLinus Torvalds */ 50265324144SJesper Dangaard Brouer static inline int arp_fwd_proxy(struct in_device *in_dev, 50365324144SJesper Dangaard Brouer struct net_device *dev, struct rtable *rt) 5041da177e4SLinus Torvalds { 5051da177e4SLinus Torvalds struct in_device *out_dev; 5061da177e4SLinus Torvalds int imi, omi = -1; 5071da177e4SLinus Torvalds 508d8d1f30bSChangli Gao if (rt->dst.dev == dev) 50965324144SJesper Dangaard Brouer return 0; 51065324144SJesper Dangaard Brouer 5111da177e4SLinus Torvalds if (!IN_DEV_PROXY_ARP(in_dev)) 5121da177e4SLinus Torvalds return 0; 513deffd777SChangli Gao imi = IN_DEV_MEDIUM_ID(in_dev); 514deffd777SChangli Gao if (imi == 0) 5151da177e4SLinus Torvalds return 1; 5161da177e4SLinus Torvalds if (imi == -1) 5171da177e4SLinus Torvalds return 0; 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /* place to check for proxy_arp for routes */ 5201da177e4SLinus Torvalds 521d8d1f30bSChangli Gao out_dev = __in_dev_get_rcu(rt->dst.dev); 522faa9dcf7SEric Dumazet if (out_dev) 5231da177e4SLinus Torvalds omi = IN_DEV_MEDIUM_ID(out_dev); 524faa9dcf7SEric Dumazet 525a02cec21SEric Dumazet return omi != imi && omi != -1; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds /* 52965324144SJesper Dangaard Brouer * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev) 53065324144SJesper Dangaard Brouer * 53165324144SJesper Dangaard Brouer * RFC3069 supports proxy arp replies back to the same interface. This 53265324144SJesper Dangaard Brouer * is done to support (ethernet) switch features, like RFC 3069, where 53365324144SJesper Dangaard Brouer * the individual ports are not allowed to communicate with each 53465324144SJesper Dangaard Brouer * other, BUT they are allowed to talk to the upstream router. As 53565324144SJesper Dangaard Brouer * described in RFC 3069, it is possible to allow these hosts to 53665324144SJesper Dangaard Brouer * communicate through the upstream router, by proxy_arp'ing. 53765324144SJesper Dangaard Brouer * 53865324144SJesper Dangaard Brouer * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation" 53965324144SJesper Dangaard Brouer * 54065324144SJesper Dangaard Brouer * This technology is known by different names: 54165324144SJesper Dangaard Brouer * In RFC 3069 it is called VLAN Aggregation. 54265324144SJesper Dangaard Brouer * Cisco and Allied Telesyn call it Private VLAN. 54365324144SJesper Dangaard Brouer * Hewlett-Packard call it Source-Port filtering or port-isolation. 54465324144SJesper Dangaard Brouer * Ericsson call it MAC-Forced Forwarding (RFC Draft). 54565324144SJesper Dangaard Brouer * 54665324144SJesper Dangaard Brouer */ 54765324144SJesper Dangaard Brouer static inline int arp_fwd_pvlan(struct in_device *in_dev, 54865324144SJesper Dangaard Brouer struct net_device *dev, struct rtable *rt, 54965324144SJesper Dangaard Brouer __be32 sip, __be32 tip) 55065324144SJesper Dangaard Brouer { 55165324144SJesper Dangaard Brouer /* Private VLAN is only concerned about the same ethernet segment */ 552d8d1f30bSChangli Gao if (rt->dst.dev != dev) 55365324144SJesper Dangaard Brouer return 0; 55465324144SJesper Dangaard Brouer 55565324144SJesper Dangaard Brouer /* Don't reply on self probes (often done by windowz boxes)*/ 55665324144SJesper Dangaard Brouer if (sip == tip) 55765324144SJesper Dangaard Brouer return 0; 55865324144SJesper Dangaard Brouer 55965324144SJesper Dangaard Brouer if (IN_DEV_PROXY_ARP_PVLAN(in_dev)) 56065324144SJesper Dangaard Brouer return 1; 56165324144SJesper Dangaard Brouer else 56265324144SJesper Dangaard Brouer return 0; 56365324144SJesper Dangaard Brouer } 56465324144SJesper Dangaard Brouer 56565324144SJesper Dangaard Brouer /* 5661da177e4SLinus Torvalds * Interface to link layer: send routine and receive handler. 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds /* 5701da177e4SLinus Torvalds * Create an arp packet. If (dest_hw == NULL), we create a broadcast 5711da177e4SLinus Torvalds * message. 5721da177e4SLinus Torvalds */ 573ed9bad06SAl Viro struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip, 574ed9bad06SAl Viro struct net_device *dev, __be32 src_ip, 575abfdf1c4SJan Engelhardt const unsigned char *dest_hw, 576abfdf1c4SJan Engelhardt const unsigned char *src_hw, 577abfdf1c4SJan Engelhardt const unsigned char *target_hw) 5781da177e4SLinus Torvalds { 5791da177e4SLinus Torvalds struct sk_buff *skb; 5801da177e4SLinus Torvalds struct arphdr *arp; 5811da177e4SLinus Torvalds unsigned char *arp_ptr; 58266088243SHerbert Xu int hlen = LL_RESERVED_SPACE(dev); 58366088243SHerbert Xu int tlen = dev->needed_tailroom; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds /* 5861da177e4SLinus Torvalds * Allocate a buffer 5871da177e4SLinus Torvalds */ 5881da177e4SLinus Torvalds 58966088243SHerbert Xu skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC); 5901da177e4SLinus Torvalds if (skb == NULL) 5911da177e4SLinus Torvalds return NULL; 5921da177e4SLinus Torvalds 59366088243SHerbert Xu skb_reserve(skb, hlen); 594c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 595988b7050SPavel Emelyanov arp = (struct arphdr *) skb_put(skb, arp_hdr_len(dev)); 5961da177e4SLinus Torvalds skb->dev = dev; 5971da177e4SLinus Torvalds skb->protocol = htons(ETH_P_ARP); 5981da177e4SLinus Torvalds if (src_hw == NULL) 5991da177e4SLinus Torvalds src_hw = dev->dev_addr; 6001da177e4SLinus Torvalds if (dest_hw == NULL) 6011da177e4SLinus Torvalds dest_hw = dev->broadcast; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* 6041da177e4SLinus Torvalds * Fill the device header for the ARP frame 6051da177e4SLinus Torvalds */ 6060c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0) 6071da177e4SLinus Torvalds goto out; 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds /* 6101da177e4SLinus Torvalds * Fill out the arp protocol part. 6111da177e4SLinus Torvalds * 6121da177e4SLinus Torvalds * The arp hardware type should match the device type, except for FDDI, 6131da177e4SLinus Torvalds * which (according to RFC 1390) should always equal 1 (Ethernet). 6141da177e4SLinus Torvalds */ 6151da177e4SLinus Torvalds /* 6161da177e4SLinus Torvalds * Exceptions everywhere. AX.25 uses the AX.25 PID value not the 6171da177e4SLinus Torvalds * DIX code for the protocol. Make these device structure fields. 6181da177e4SLinus Torvalds */ 6191da177e4SLinus Torvalds switch (dev->type) { 6201da177e4SLinus Torvalds default: 6211da177e4SLinus Torvalds arp->ar_hrd = htons(dev->type); 6221da177e4SLinus Torvalds arp->ar_pro = htons(ETH_P_IP); 6231da177e4SLinus Torvalds break; 6241da177e4SLinus Torvalds 62540e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25) 6261da177e4SLinus Torvalds case ARPHRD_AX25: 6271da177e4SLinus Torvalds arp->ar_hrd = htons(ARPHRD_AX25); 6281da177e4SLinus Torvalds arp->ar_pro = htons(AX25_P_IP); 6291da177e4SLinus Torvalds break; 6301da177e4SLinus Torvalds 63140e4783eSIgor Maravic #if IS_ENABLED(CONFIG_NETROM) 6321da177e4SLinus Torvalds case ARPHRD_NETROM: 6331da177e4SLinus Torvalds arp->ar_hrd = htons(ARPHRD_NETROM); 6341da177e4SLinus Torvalds arp->ar_pro = htons(AX25_P_IP); 6351da177e4SLinus Torvalds break; 6361da177e4SLinus Torvalds #endif 6371da177e4SLinus Torvalds #endif 6381da177e4SLinus Torvalds 63940e4783eSIgor Maravic #if IS_ENABLED(CONFIG_FDDI) 6401da177e4SLinus Torvalds case ARPHRD_FDDI: 6411da177e4SLinus Torvalds arp->ar_hrd = htons(ARPHRD_ETHER); 6421da177e4SLinus Torvalds arp->ar_pro = htons(ETH_P_IP); 6431da177e4SLinus Torvalds break; 6441da177e4SLinus Torvalds #endif 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds arp->ar_hln = dev->addr_len; 6481da177e4SLinus Torvalds arp->ar_pln = 4; 6491da177e4SLinus Torvalds arp->ar_op = htons(type); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds arp_ptr = (unsigned char *)(arp + 1); 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds memcpy(arp_ptr, src_hw, dev->addr_len); 6541da177e4SLinus Torvalds arp_ptr += dev->addr_len; 6551da177e4SLinus Torvalds memcpy(arp_ptr, &src_ip, 4); 6561da177e4SLinus Torvalds arp_ptr += 4; 6571da177e4SLinus Torvalds if (target_hw != NULL) 6581da177e4SLinus Torvalds memcpy(arp_ptr, target_hw, dev->addr_len); 6591da177e4SLinus Torvalds else 6601da177e4SLinus Torvalds memset(arp_ptr, 0, dev->addr_len); 6611da177e4SLinus Torvalds arp_ptr += dev->addr_len; 6621da177e4SLinus Torvalds memcpy(arp_ptr, &dest_ip, 4); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds return skb; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds out: 6671da177e4SLinus Torvalds kfree_skb(skb); 6681da177e4SLinus Torvalds return NULL; 6691da177e4SLinus Torvalds } 6704bc2f18bSEric Dumazet EXPORT_SYMBOL(arp_create); 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds /* 6731da177e4SLinus Torvalds * Send an arp packet. 6741da177e4SLinus Torvalds */ 6751da177e4SLinus Torvalds void arp_xmit(struct sk_buff *skb) 6761da177e4SLinus Torvalds { 6771da177e4SLinus Torvalds /* Send it off, maybe filter it using firewalling first. */ 678fdc9314cSJan Engelhardt NF_HOOK(NFPROTO_ARP, NF_ARP_OUT, skb, NULL, skb->dev, dev_queue_xmit); 6791da177e4SLinus Torvalds } 6804bc2f18bSEric Dumazet EXPORT_SYMBOL(arp_xmit); 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds /* 6831da177e4SLinus Torvalds * Create and send an arp packet. 6841da177e4SLinus Torvalds */ 685ed9bad06SAl Viro void arp_send(int type, int ptype, __be32 dest_ip, 686ed9bad06SAl Viro struct net_device *dev, __be32 src_ip, 687abfdf1c4SJan Engelhardt const unsigned char *dest_hw, const unsigned char *src_hw, 688abfdf1c4SJan Engelhardt const unsigned char *target_hw) 6891da177e4SLinus Torvalds { 6901da177e4SLinus Torvalds struct sk_buff *skb; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* 6931da177e4SLinus Torvalds * No arp on this interface. 6941da177e4SLinus Torvalds */ 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds if (dev->flags&IFF_NOARP) 6971da177e4SLinus Torvalds return; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds skb = arp_create(type, ptype, dest_ip, dev, src_ip, 7001da177e4SLinus Torvalds dest_hw, src_hw, target_hw); 701deffd777SChangli Gao if (skb == NULL) 7021da177e4SLinus Torvalds return; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds arp_xmit(skb); 7051da177e4SLinus Torvalds } 7064bc2f18bSEric Dumazet EXPORT_SYMBOL(arp_send); 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds /* 7091da177e4SLinus Torvalds * Process an arp request. 7101da177e4SLinus Torvalds */ 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds static int arp_process(struct sk_buff *skb) 7131da177e4SLinus Torvalds { 7141da177e4SLinus Torvalds struct net_device *dev = skb->dev; 715faa9dcf7SEric Dumazet struct in_device *in_dev = __in_dev_get_rcu(dev); 7161da177e4SLinus Torvalds struct arphdr *arp; 7171da177e4SLinus Torvalds unsigned char *arp_ptr; 7181da177e4SLinus Torvalds struct rtable *rt; 719e0260fedSMark Ryden unsigned char *sha; 7209e12bb22SAl Viro __be32 sip, tip; 7211da177e4SLinus Torvalds u16 dev_type = dev->type; 7221da177e4SLinus Torvalds int addr_type; 7231da177e4SLinus Torvalds struct neighbour *n; 724c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds /* arp_rcv below verifies the ARP header and verifies the device 7271da177e4SLinus Torvalds * is ARP'able. 7281da177e4SLinus Torvalds */ 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds if (in_dev == NULL) 7311da177e4SLinus Torvalds goto out; 7321da177e4SLinus Torvalds 733d0a92be0SArnaldo Carvalho de Melo arp = arp_hdr(skb); 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds switch (dev_type) { 7361da177e4SLinus Torvalds default: 7371da177e4SLinus Torvalds if (arp->ar_pro != htons(ETH_P_IP) || 7381da177e4SLinus Torvalds htons(dev_type) != arp->ar_hrd) 7391da177e4SLinus Torvalds goto out; 7401da177e4SLinus Torvalds break; 7411da177e4SLinus Torvalds case ARPHRD_ETHER: 7421da177e4SLinus Torvalds case ARPHRD_FDDI: 7431da177e4SLinus Torvalds case ARPHRD_IEEE802: 7441da177e4SLinus Torvalds /* 745211ed865SPaul Gortmaker * ETHERNET, and Fibre Channel (which are IEEE 802 7461da177e4SLinus Torvalds * devices, according to RFC 2625) devices will accept ARP 7471da177e4SLinus Torvalds * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2). 7481da177e4SLinus Torvalds * This is the case also of FDDI, where the RFC 1390 says that 7491da177e4SLinus Torvalds * FDDI devices should accept ARP hardware of (1) Ethernet, 7501da177e4SLinus Torvalds * however, to be more robust, we'll accept both 1 (Ethernet) 7511da177e4SLinus Torvalds * or 6 (IEEE 802.2) 7521da177e4SLinus Torvalds */ 7531da177e4SLinus Torvalds if ((arp->ar_hrd != htons(ARPHRD_ETHER) && 7541da177e4SLinus Torvalds arp->ar_hrd != htons(ARPHRD_IEEE802)) || 7551da177e4SLinus Torvalds arp->ar_pro != htons(ETH_P_IP)) 7561da177e4SLinus Torvalds goto out; 7571da177e4SLinus Torvalds break; 7581da177e4SLinus Torvalds case ARPHRD_AX25: 7591da177e4SLinus Torvalds if (arp->ar_pro != htons(AX25_P_IP) || 7601da177e4SLinus Torvalds arp->ar_hrd != htons(ARPHRD_AX25)) 7611da177e4SLinus Torvalds goto out; 7621da177e4SLinus Torvalds break; 7631da177e4SLinus Torvalds case ARPHRD_NETROM: 7641da177e4SLinus Torvalds if (arp->ar_pro != htons(AX25_P_IP) || 7651da177e4SLinus Torvalds arp->ar_hrd != htons(ARPHRD_NETROM)) 7661da177e4SLinus Torvalds goto out; 7671da177e4SLinus Torvalds break; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds /* Understand only these message types */ 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds if (arp->ar_op != htons(ARPOP_REPLY) && 7731da177e4SLinus Torvalds arp->ar_op != htons(ARPOP_REQUEST)) 7741da177e4SLinus Torvalds goto out; 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds /* 7771da177e4SLinus Torvalds * Extract fields 7781da177e4SLinus Torvalds */ 7791da177e4SLinus Torvalds arp_ptr = (unsigned char *)(arp + 1); 7801da177e4SLinus Torvalds sha = arp_ptr; 7811da177e4SLinus Torvalds arp_ptr += dev->addr_len; 7821da177e4SLinus Torvalds memcpy(&sip, arp_ptr, 4); 7831da177e4SLinus Torvalds arp_ptr += 4; 7841da177e4SLinus Torvalds arp_ptr += dev->addr_len; 7851da177e4SLinus Torvalds memcpy(&tip, arp_ptr, 4); 7861da177e4SLinus Torvalds /* 7871da177e4SLinus Torvalds * Check for bad requests for 127.x.x.x and requests for multicast 7881da177e4SLinus Torvalds * addresses. If this is one such, delete it. 7891da177e4SLinus Torvalds */ 790d0daebc3SThomas Graf if (ipv4_is_multicast(tip) || 791d0daebc3SThomas Graf (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip))) 7921da177e4SLinus Torvalds goto out; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * Special case: We must set Frame Relay source Q.922 address 7961da177e4SLinus Torvalds */ 7971da177e4SLinus Torvalds if (dev_type == ARPHRD_DLCI) 7981da177e4SLinus Torvalds sha = dev->broadcast; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds /* 8011da177e4SLinus Torvalds * Process entry. The idea here is we want to send a reply if it is a 8021da177e4SLinus Torvalds * request for us or if it is a request for someone else that we hold 8031da177e4SLinus Torvalds * a proxy for. We want to add an entry to our cache if it is a reply 8041da177e4SLinus Torvalds * to us or if it is a request for our address. 8051da177e4SLinus Torvalds * (The assumption for this last is that if someone is requesting our 8061da177e4SLinus Torvalds * address, they are probably intending to talk to us, so it saves time 8071da177e4SLinus Torvalds * if we cache their address. Their address is also probably not in 8081da177e4SLinus Torvalds * our cache, since ours is not in their cache.) 8091da177e4SLinus Torvalds * 8101da177e4SLinus Torvalds * Putting this another way, we only care about replies if they are to 8111da177e4SLinus Torvalds * us, in which case we add them to the cache. For requests, we care 8121da177e4SLinus Torvalds * about those for us and those for our proxies. We reply to both, 8131da177e4SLinus Torvalds * and in the case of requests for us we add the requester to the arp 8141da177e4SLinus Torvalds * cache. 8151da177e4SLinus Torvalds */ 8161da177e4SLinus Torvalds 817f8a68e75SEric W. Biederman /* Special case: IPv4 duplicate address detection packet (RFC2131) */ 818f8a68e75SEric W. Biederman if (sip == 0) { 8191da177e4SLinus Torvalds if (arp->ar_op == htons(ARPOP_REQUEST) && 82049e8a279SDenis V. Lunev inet_addr_type(net, tip) == RTN_LOCAL && 8219bd85e32SDenis V. Lunev !arp_ignore(in_dev, sip, tip)) 822b4a9811cSJonas Danielsson arp_send(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, 823b4a9811cSJonas Danielsson dev->dev_addr, sha); 8241da177e4SLinus Torvalds goto out; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds if (arp->ar_op == htons(ARPOP_REQUEST) && 828c6cffba4SDavid S. Miller ip_route_input_noref(skb, tip, sip, 0, dev) == 0) { 8291da177e4SLinus Torvalds 830511c3f92SEric Dumazet rt = skb_rtable(skb); 8311da177e4SLinus Torvalds addr_type = rt->rt_type; 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds if (addr_type == RTN_LOCAL) { 834deffd777SChangli Gao int dont_send; 8351da177e4SLinus Torvalds 836deffd777SChangli Gao dont_send = arp_ignore(in_dev, sip, tip); 8371da177e4SLinus Torvalds if (!dont_send && IN_DEV_ARPFILTER(in_dev)) 838ae9c416dSChangli Gao dont_send = arp_filter(sip, tip, dev); 8398164f1b7SBen Greear if (!dont_send) { 8408164f1b7SBen Greear n = neigh_event_ns(&arp_tbl, sha, &sip, dev); 8418164f1b7SBen Greear if (n) { 842deffd777SChangli Gao arp_send(ARPOP_REPLY, ETH_P_ARP, sip, 843deffd777SChangli Gao dev, tip, sha, dev->dev_addr, 844deffd777SChangli Gao sha); 8451da177e4SLinus Torvalds neigh_release(n); 8461da177e4SLinus Torvalds } 8478164f1b7SBen Greear } 8481da177e4SLinus Torvalds goto out; 8491da177e4SLinus Torvalds } else if (IN_DEV_FORWARD(in_dev)) { 85065324144SJesper Dangaard Brouer if (addr_type == RTN_UNICAST && 85165324144SJesper Dangaard Brouer (arp_fwd_proxy(in_dev, dev, rt) || 85265324144SJesper Dangaard Brouer arp_fwd_pvlan(in_dev, dev, rt, sip, tip) || 85370620c46SThomas Graf (rt->dst.dev != dev && 85470620c46SThomas Graf pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))) { 8551da177e4SLinus Torvalds n = neigh_event_ns(&arp_tbl, sha, &sip, dev); 8561da177e4SLinus Torvalds if (n) 8571da177e4SLinus Torvalds neigh_release(n); 8581da177e4SLinus Torvalds 859a61bbcf2SPatrick McHardy if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED || 8601da177e4SLinus Torvalds skb->pkt_type == PACKET_HOST || 8611da177e4SLinus Torvalds in_dev->arp_parms->proxy_delay == 0) { 862deffd777SChangli Gao arp_send(ARPOP_REPLY, ETH_P_ARP, sip, 863deffd777SChangli Gao dev, tip, sha, dev->dev_addr, 864deffd777SChangli Gao sha); 8651da177e4SLinus Torvalds } else { 866deffd777SChangli Gao pneigh_enqueue(&arp_tbl, 867deffd777SChangli Gao in_dev->arp_parms, skb); 8681da177e4SLinus Torvalds return 0; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds goto out; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds /* Update our ARP tables */ 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds n = __neigh_lookup(&arp_tbl, &sip, dev, 0); 8781da177e4SLinus Torvalds 879124d37e9SNeil Horman if (IN_DEV_ARP_ACCEPT(in_dev)) { 8801da177e4SLinus Torvalds /* Unsolicited ARP is not accepted by default. 8811da177e4SLinus Torvalds It is possible, that this option should be enabled for some 8821da177e4SLinus Torvalds devices (strip is candidate) 8831da177e4SLinus Torvalds */ 8841da177e4SLinus Torvalds if (n == NULL && 8856d955180SOctavian Purdila (arp->ar_op == htons(ARPOP_REPLY) || 8866d955180SOctavian Purdila (arp->ar_op == htons(ARPOP_REQUEST) && tip == sip)) && 88749e8a279SDenis V. Lunev inet_addr_type(net, sip) == RTN_UNICAST) 8881b1ac759SJean Delvare n = __neigh_lookup(&arp_tbl, &sip, dev, 1); 889abd596a4SNeil Horman } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds if (n) { 8921da177e4SLinus Torvalds int state = NUD_REACHABLE; 8931da177e4SLinus Torvalds int override; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds /* If several different ARP replies follows back-to-back, 8961da177e4SLinus Torvalds use the FIRST one. It is possible, if several proxy 8971da177e4SLinus Torvalds agents are active. Taking the first reply prevents 8981da177e4SLinus Torvalds arp trashing and chooses the fastest router. 8991da177e4SLinus Torvalds */ 9001da177e4SLinus Torvalds override = time_after(jiffies, n->updated + n->parms->locktime); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* Broadcast replies and request packets 9031da177e4SLinus Torvalds do not assert neighbour reachability. 9041da177e4SLinus Torvalds */ 9051da177e4SLinus Torvalds if (arp->ar_op != htons(ARPOP_REPLY) || 9061da177e4SLinus Torvalds skb->pkt_type != PACKET_HOST) 9071da177e4SLinus Torvalds state = NUD_STALE; 908deffd777SChangli Gao neigh_update(n, sha, state, 909deffd777SChangli Gao override ? NEIGH_UPDATE_F_OVERRIDE : 0); 9101da177e4SLinus Torvalds neigh_release(n); 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds out: 914ead2ceb0SNeil Horman consume_skb(skb); 9151da177e4SLinus Torvalds return 0; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 918444fc8fcSHerbert Xu static void parp_redo(struct sk_buff *skb) 919444fc8fcSHerbert Xu { 920444fc8fcSHerbert Xu arp_process(skb); 921444fc8fcSHerbert Xu } 922444fc8fcSHerbert Xu 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds /* 9251da177e4SLinus Torvalds * Receive an arp request from the device layer. 9261da177e4SLinus Torvalds */ 9271da177e4SLinus Torvalds 9286c97e72aSAdrian Bunk static int arp_rcv(struct sk_buff *skb, struct net_device *dev, 9296c97e72aSAdrian Bunk struct packet_type *pt, struct net_device *orig_dev) 9301da177e4SLinus Torvalds { 931044453b3SEric Dumazet const struct arphdr *arp; 932044453b3SEric Dumazet 933044453b3SEric Dumazet if (dev->flags & IFF_NOARP || 934044453b3SEric Dumazet skb->pkt_type == PACKET_OTHERHOST || 935044453b3SEric Dumazet skb->pkt_type == PACKET_LOOPBACK) 936044453b3SEric Dumazet goto freeskb; 937044453b3SEric Dumazet 938044453b3SEric Dumazet skb = skb_share_check(skb, GFP_ATOMIC); 939044453b3SEric Dumazet if (!skb) 940044453b3SEric Dumazet goto out_of_mem; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* ARP header, plus 2 device addresses, plus 2 IP addresses. */ 943988b7050SPavel Emelyanov if (!pskb_may_pull(skb, arp_hdr_len(dev))) 9441da177e4SLinus Torvalds goto freeskb; 9451da177e4SLinus Torvalds 946d0a92be0SArnaldo Carvalho de Melo arp = arp_hdr(skb); 947044453b3SEric Dumazet if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4) 9481da177e4SLinus Torvalds goto freeskb; 9491da177e4SLinus Torvalds 950a61bbcf2SPatrick McHardy memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb)); 951a61bbcf2SPatrick McHardy 952fdc9314cSJan Engelhardt return NF_HOOK(NFPROTO_ARP, NF_ARP_IN, skb, dev, NULL, arp_process); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds freeskb: 9551da177e4SLinus Torvalds kfree_skb(skb); 9561da177e4SLinus Torvalds out_of_mem: 9571da177e4SLinus Torvalds return 0; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* 9611da177e4SLinus Torvalds * User level interface (ioctl) 9621da177e4SLinus Torvalds */ 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds /* 9651da177e4SLinus Torvalds * Set (create) an ARP cache entry. 9661da177e4SLinus Torvalds */ 9671da177e4SLinus Torvalds 96832e569b7SPavel Emelyanov static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on) 969f8b33fdfSPavel Emelyanov { 970f8b33fdfSPavel Emelyanov if (dev == NULL) { 971586f1211SPavel Emelyanov IPV4_DEVCONF_ALL(net, PROXY_ARP) = on; 972f8b33fdfSPavel Emelyanov return 0; 973f8b33fdfSPavel Emelyanov } 974c506653dSEric Dumazet if (__in_dev_get_rtnl(dev)) { 975c506653dSEric Dumazet IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on); 976f8b33fdfSPavel Emelyanov return 0; 977f8b33fdfSPavel Emelyanov } 978f8b33fdfSPavel Emelyanov return -ENXIO; 979f8b33fdfSPavel Emelyanov } 980f8b33fdfSPavel Emelyanov 98132e569b7SPavel Emelyanov static int arp_req_set_public(struct net *net, struct arpreq *r, 98232e569b7SPavel Emelyanov struct net_device *dev) 9831da177e4SLinus Torvalds { 984ed9bad06SAl Viro __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; 985ed9bad06SAl Viro __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr; 98643dc1701SPavel Emelyanov 987ed9bad06SAl Viro if (mask && mask != htonl(0xFFFFFFFF)) 9881da177e4SLinus Torvalds return -EINVAL; 9891da177e4SLinus Torvalds if (!dev && (r->arp_flags & ATF_COM)) { 990941666c2SEric Dumazet dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family, 99143dc1701SPavel Emelyanov r->arp_ha.sa_data); 9921da177e4SLinus Torvalds if (!dev) 9931da177e4SLinus Torvalds return -ENODEV; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds if (mask) { 9962db82b53SDenis V. Lunev if (pneigh_lookup(&arp_tbl, net, &ip, dev, 1) == NULL) 9971da177e4SLinus Torvalds return -ENOBUFS; 9981da177e4SLinus Torvalds return 0; 9991da177e4SLinus Torvalds } 1000f8b33fdfSPavel Emelyanov 100132e569b7SPavel Emelyanov return arp_req_set_proxy(net, dev, 1); 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 100432e569b7SPavel Emelyanov static int arp_req_set(struct net *net, struct arpreq *r, 100532e569b7SPavel Emelyanov struct net_device *dev) 100643dc1701SPavel Emelyanov { 100743dc1701SPavel Emelyanov __be32 ip; 100843dc1701SPavel Emelyanov struct neighbour *neigh; 100943dc1701SPavel Emelyanov int err; 101043dc1701SPavel Emelyanov 101143dc1701SPavel Emelyanov if (r->arp_flags & ATF_PUBL) 101232e569b7SPavel Emelyanov return arp_req_set_public(net, r, dev); 101343dc1701SPavel Emelyanov 101443dc1701SPavel Emelyanov ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; 10151da177e4SLinus Torvalds if (r->arp_flags & ATF_PERM) 10161da177e4SLinus Torvalds r->arp_flags |= ATF_COM; 10171da177e4SLinus Torvalds if (dev == NULL) { 101878fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0); 1019b23dd4feSDavid S. Miller 1020b23dd4feSDavid S. Miller if (IS_ERR(rt)) 1021b23dd4feSDavid S. Miller return PTR_ERR(rt); 1022d8d1f30bSChangli Gao dev = rt->dst.dev; 10231da177e4SLinus Torvalds ip_rt_put(rt); 10241da177e4SLinus Torvalds if (!dev) 10251da177e4SLinus Torvalds return -EINVAL; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds switch (dev->type) { 102840e4783eSIgor Maravic #if IS_ENABLED(CONFIG_FDDI) 10291da177e4SLinus Torvalds case ARPHRD_FDDI: 10301da177e4SLinus Torvalds /* 10311da177e4SLinus Torvalds * According to RFC 1390, FDDI devices should accept ARP 10321da177e4SLinus Torvalds * hardware types of 1 (Ethernet). However, to be more 10331da177e4SLinus Torvalds * robust, we'll accept hardware types of either 1 (Ethernet) 10341da177e4SLinus Torvalds * or 6 (IEEE 802.2). 10351da177e4SLinus Torvalds */ 10361da177e4SLinus Torvalds if (r->arp_ha.sa_family != ARPHRD_FDDI && 10371da177e4SLinus Torvalds r->arp_ha.sa_family != ARPHRD_ETHER && 10381da177e4SLinus Torvalds r->arp_ha.sa_family != ARPHRD_IEEE802) 10391da177e4SLinus Torvalds return -EINVAL; 10401da177e4SLinus Torvalds break; 10411da177e4SLinus Torvalds #endif 10421da177e4SLinus Torvalds default: 10431da177e4SLinus Torvalds if (r->arp_ha.sa_family != dev->type) 10441da177e4SLinus Torvalds return -EINVAL; 10451da177e4SLinus Torvalds break; 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev); 10491da177e4SLinus Torvalds err = PTR_ERR(neigh); 10501da177e4SLinus Torvalds if (!IS_ERR(neigh)) { 105195c96174SEric Dumazet unsigned int state = NUD_STALE; 10521da177e4SLinus Torvalds if (r->arp_flags & ATF_PERM) 10531da177e4SLinus Torvalds state = NUD_PERMANENT; 10541da177e4SLinus Torvalds err = neigh_update(neigh, (r->arp_flags & ATF_COM) ? 10551da177e4SLinus Torvalds r->arp_ha.sa_data : NULL, state, 10561da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | 10571da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 10581da177e4SLinus Torvalds neigh_release(neigh); 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds return err; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 106395c96174SEric Dumazet static unsigned int arp_state_to_flags(struct neighbour *neigh) 10641da177e4SLinus Torvalds { 10651da177e4SLinus Torvalds if (neigh->nud_state&NUD_PERMANENT) 1066deffd777SChangli Gao return ATF_PERM | ATF_COM; 10671da177e4SLinus Torvalds else if (neigh->nud_state&NUD_VALID) 1068deffd777SChangli Gao return ATF_COM; 1069deffd777SChangli Gao else 1070deffd777SChangli Gao return 0; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds /* 10741da177e4SLinus Torvalds * Get an ARP cache entry. 10751da177e4SLinus Torvalds */ 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds static int arp_req_get(struct arpreq *r, struct net_device *dev) 10781da177e4SLinus Torvalds { 1079ed9bad06SAl Viro __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr; 10801da177e4SLinus Torvalds struct neighbour *neigh; 10811da177e4SLinus Torvalds int err = -ENXIO; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds neigh = neigh_lookup(&arp_tbl, &ip, dev); 10841da177e4SLinus Torvalds if (neigh) { 10851da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 10861da177e4SLinus Torvalds memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len); 10871da177e4SLinus Torvalds r->arp_flags = arp_state_to_flags(neigh); 10881da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 10891da177e4SLinus Torvalds r->arp_ha.sa_family = dev->type; 10901da177e4SLinus Torvalds strlcpy(r->arp_dev, dev->name, sizeof(r->arp_dev)); 10911da177e4SLinus Torvalds neigh_release(neigh); 10921da177e4SLinus Torvalds err = 0; 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds return err; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 1097545ecdc3SMaxim Levitsky int arp_invalidate(struct net_device *dev, __be32 ip) 1098545ecdc3SMaxim Levitsky { 1099545ecdc3SMaxim Levitsky struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev); 1100545ecdc3SMaxim Levitsky int err = -ENXIO; 1101545ecdc3SMaxim Levitsky 1102545ecdc3SMaxim Levitsky if (neigh) { 1103545ecdc3SMaxim Levitsky if (neigh->nud_state & ~NUD_NOARP) 1104545ecdc3SMaxim Levitsky err = neigh_update(neigh, NULL, NUD_FAILED, 1105545ecdc3SMaxim Levitsky NEIGH_UPDATE_F_OVERRIDE| 1106545ecdc3SMaxim Levitsky NEIGH_UPDATE_F_ADMIN); 1107545ecdc3SMaxim Levitsky neigh_release(neigh); 1108545ecdc3SMaxim Levitsky } 1109545ecdc3SMaxim Levitsky 1110545ecdc3SMaxim Levitsky return err; 1111545ecdc3SMaxim Levitsky } 1112545ecdc3SMaxim Levitsky EXPORT_SYMBOL(arp_invalidate); 1113545ecdc3SMaxim Levitsky 111432e569b7SPavel Emelyanov static int arp_req_delete_public(struct net *net, struct arpreq *r, 111532e569b7SPavel Emelyanov struct net_device *dev) 11161da177e4SLinus Torvalds { 1117ed9bad06SAl Viro __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr; 111846479b43SPavel Emelyanov __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr; 11191da177e4SLinus Torvalds 1120ed9bad06SAl Viro if (mask == htonl(0xFFFFFFFF)) 11212db82b53SDenis V. Lunev return pneigh_delete(&arp_tbl, net, &ip, dev); 112246479b43SPavel Emelyanov 1123f8b33fdfSPavel Emelyanov if (mask) 11241da177e4SLinus Torvalds return -EINVAL; 1125f8b33fdfSPavel Emelyanov 112632e569b7SPavel Emelyanov return arp_req_set_proxy(net, dev, 0); 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 112932e569b7SPavel Emelyanov static int arp_req_delete(struct net *net, struct arpreq *r, 113032e569b7SPavel Emelyanov struct net_device *dev) 113146479b43SPavel Emelyanov { 113246479b43SPavel Emelyanov __be32 ip; 113346479b43SPavel Emelyanov 113446479b43SPavel Emelyanov if (r->arp_flags & ATF_PUBL) 113532e569b7SPavel Emelyanov return arp_req_delete_public(net, r, dev); 113646479b43SPavel Emelyanov 113746479b43SPavel Emelyanov ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; 11381da177e4SLinus Torvalds if (dev == NULL) { 113978fbfd8aSDavid S. Miller struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0); 1140b23dd4feSDavid S. Miller if (IS_ERR(rt)) 1141b23dd4feSDavid S. Miller return PTR_ERR(rt); 1142d8d1f30bSChangli Gao dev = rt->dst.dev; 11431da177e4SLinus Torvalds ip_rt_put(rt); 11441da177e4SLinus Torvalds if (!dev) 11451da177e4SLinus Torvalds return -EINVAL; 11461da177e4SLinus Torvalds } 1147545ecdc3SMaxim Levitsky return arp_invalidate(dev, ip); 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds /* 11511da177e4SLinus Torvalds * Handle an ARP layer I/O control request. 11521da177e4SLinus Torvalds */ 11531da177e4SLinus Torvalds 115432e569b7SPavel Emelyanov int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg) 11551da177e4SLinus Torvalds { 11561da177e4SLinus Torvalds int err; 11571da177e4SLinus Torvalds struct arpreq r; 11581da177e4SLinus Torvalds struct net_device *dev = NULL; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds switch (cmd) { 11611da177e4SLinus Torvalds case SIOCDARP: 11621da177e4SLinus Torvalds case SIOCSARP: 116352e804c6SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 11641da177e4SLinus Torvalds return -EPERM; 11651da177e4SLinus Torvalds case SIOCGARP: 11661da177e4SLinus Torvalds err = copy_from_user(&r, arg, sizeof(struct arpreq)); 11671da177e4SLinus Torvalds if (err) 11681da177e4SLinus Torvalds return -EFAULT; 11691da177e4SLinus Torvalds break; 11701da177e4SLinus Torvalds default: 11711da177e4SLinus Torvalds return -EINVAL; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds if (r.arp_pa.sa_family != AF_INET) 11751da177e4SLinus Torvalds return -EPFNOSUPPORT; 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds if (!(r.arp_flags & ATF_PUBL) && 11781da177e4SLinus Torvalds (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB))) 11791da177e4SLinus Torvalds return -EINVAL; 11801da177e4SLinus Torvalds if (!(r.arp_flags & ATF_NETMASK)) 11811da177e4SLinus Torvalds ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr = 11821da177e4SLinus Torvalds htonl(0xFFFFFFFFUL); 1183c506653dSEric Dumazet rtnl_lock(); 11841da177e4SLinus Torvalds if (r.arp_dev[0]) { 11851da177e4SLinus Torvalds err = -ENODEV; 1186c506653dSEric Dumazet dev = __dev_get_by_name(net, r.arp_dev); 1187deffd777SChangli Gao if (dev == NULL) 11881da177e4SLinus Torvalds goto out; 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds /* Mmmm... It is wrong... ARPHRD_NETROM==0 */ 11911da177e4SLinus Torvalds if (!r.arp_ha.sa_family) 11921da177e4SLinus Torvalds r.arp_ha.sa_family = dev->type; 11931da177e4SLinus Torvalds err = -EINVAL; 11941da177e4SLinus Torvalds if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type) 11951da177e4SLinus Torvalds goto out; 11961da177e4SLinus Torvalds } else if (cmd == SIOCGARP) { 11971da177e4SLinus Torvalds err = -ENODEV; 11981da177e4SLinus Torvalds goto out; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds switch (cmd) { 12021da177e4SLinus Torvalds case SIOCDARP: 120332e569b7SPavel Emelyanov err = arp_req_delete(net, &r, dev); 12041da177e4SLinus Torvalds break; 12051da177e4SLinus Torvalds case SIOCSARP: 120632e569b7SPavel Emelyanov err = arp_req_set(net, &r, dev); 12071da177e4SLinus Torvalds break; 12081da177e4SLinus Torvalds case SIOCGARP: 12091da177e4SLinus Torvalds err = arp_req_get(&r, dev); 12101da177e4SLinus Torvalds break; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds out: 1213c506653dSEric Dumazet rtnl_unlock(); 1214941666c2SEric Dumazet if (cmd == SIOCGARP && !err && copy_to_user(arg, &r, sizeof(r))) 1215941666c2SEric Dumazet err = -EFAULT; 12161da177e4SLinus Torvalds return err; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 1219deffd777SChangli Gao static int arp_netdev_event(struct notifier_block *this, unsigned long event, 1220deffd777SChangli Gao void *ptr) 12211da177e4SLinus Torvalds { 12221da177e4SLinus Torvalds struct net_device *dev = ptr; 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds switch (event) { 12251da177e4SLinus Torvalds case NETDEV_CHANGEADDR: 12261da177e4SLinus Torvalds neigh_changeaddr(&arp_tbl, dev); 1227bafa6d9dSNicolas Dichtel rt_cache_flush(dev_net(dev)); 12281da177e4SLinus Torvalds break; 12291da177e4SLinus Torvalds default: 12301da177e4SLinus Torvalds break; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds return NOTIFY_DONE; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds static struct notifier_block arp_netdev_notifier = { 12371da177e4SLinus Torvalds .notifier_call = arp_netdev_event, 12381da177e4SLinus Torvalds }; 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds /* Note, that it is not on notifier chain. 12411da177e4SLinus Torvalds It is necessary, that this routine was called after route cache will be 12421da177e4SLinus Torvalds flushed. 12431da177e4SLinus Torvalds */ 12441da177e4SLinus Torvalds void arp_ifdown(struct net_device *dev) 12451da177e4SLinus Torvalds { 12461da177e4SLinus Torvalds neigh_ifdown(&arp_tbl, dev); 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds /* 12511da177e4SLinus Torvalds * Called once on startup. 12521da177e4SLinus Torvalds */ 12531da177e4SLinus Torvalds 12547546dd97SStephen Hemminger static struct packet_type arp_packet_type __read_mostly = { 125509640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_ARP), 12561da177e4SLinus Torvalds .func = arp_rcv, 12571da177e4SLinus Torvalds }; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds static int arp_proc_init(void); 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds void __init arp_init(void) 12621da177e4SLinus Torvalds { 12631da177e4SLinus Torvalds neigh_table_init(&arp_tbl); 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds dev_add_pack(&arp_packet_type); 12661da177e4SLinus Torvalds arp_proc_init(); 12671da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 126854716e3bSEric W. Biederman neigh_sysctl_register(NULL, &arp_tbl.parms, "ipv4", NULL); 12691da177e4SLinus Torvalds #endif 12701da177e4SLinus Torvalds register_netdevice_notifier(&arp_netdev_notifier); 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 127440e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25) 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 12771da177e4SLinus Torvalds /* 12781da177e4SLinus Torvalds * ax25 -> ASCII conversion 12791da177e4SLinus Torvalds */ 12801da177e4SLinus Torvalds static char *ax2asc2(ax25_address *a, char *buf) 12811da177e4SLinus Torvalds { 12821da177e4SLinus Torvalds char c, *s; 12831da177e4SLinus Torvalds int n; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds for (n = 0, s = buf; n < 6; n++) { 12861da177e4SLinus Torvalds c = (a->ax25_call[n] >> 1) & 0x7F; 12871da177e4SLinus Torvalds 1288deffd777SChangli Gao if (c != ' ') 1289deffd777SChangli Gao *s++ = c; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds *s++ = '-'; 1293deffd777SChangli Gao n = (a->ax25_call[6] >> 1) & 0x0F; 1294deffd777SChangli Gao if (n > 9) { 12951da177e4SLinus Torvalds *s++ = '1'; 12961da177e4SLinus Torvalds n -= 10; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds *s++ = n + '0'; 13001da177e4SLinus Torvalds *s++ = '\0'; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds if (*buf == '\0' || *buf == '-') 13031da177e4SLinus Torvalds return "*"; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds return buf; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds #endif /* CONFIG_AX25 */ 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds #define HBUFFERLEN 30 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds static void arp_format_neigh_entry(struct seq_file *seq, 13121da177e4SLinus Torvalds struct neighbour *n) 13131da177e4SLinus Torvalds { 13141da177e4SLinus Torvalds char hbuffer[HBUFFERLEN]; 13151da177e4SLinus Torvalds int k, j; 13161da177e4SLinus Torvalds char tbuf[16]; 13171da177e4SLinus Torvalds struct net_device *dev = n->dev; 13181da177e4SLinus Torvalds int hatype = dev->type; 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds read_lock(&n->lock); 13211da177e4SLinus Torvalds /* Convert hardware address to XX:XX:XX:XX ... form. */ 132240e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25) 13231da177e4SLinus Torvalds if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM) 13241da177e4SLinus Torvalds ax2asc2((ax25_address *)n->ha, hbuffer); 13251da177e4SLinus Torvalds else { 13261da177e4SLinus Torvalds #endif 13271da177e4SLinus Torvalds for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) { 132851f82a2bSDenis Cheng hbuffer[k++] = hex_asc_hi(n->ha[j]); 132951f82a2bSDenis Cheng hbuffer[k++] = hex_asc_lo(n->ha[j]); 13301da177e4SLinus Torvalds hbuffer[k++] = ':'; 13311da177e4SLinus Torvalds } 1332a3e8ee68Sroel kluin if (k != 0) 1333a3e8ee68Sroel kluin --k; 1334a3e8ee68Sroel kluin hbuffer[k] = 0; 133540e4783eSIgor Maravic #if IS_ENABLED(CONFIG_AX25) 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds #endif 1338673d57e7SHarvey Harrison sprintf(tbuf, "%pI4", n->primary_key); 13391da177e4SLinus Torvalds seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n", 13401da177e4SLinus Torvalds tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name); 13411da177e4SLinus Torvalds read_unlock(&n->lock); 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds static void arp_format_pneigh_entry(struct seq_file *seq, 13451da177e4SLinus Torvalds struct pneigh_entry *n) 13461da177e4SLinus Torvalds { 13471da177e4SLinus Torvalds struct net_device *dev = n->dev; 13481da177e4SLinus Torvalds int hatype = dev ? dev->type : 0; 13491da177e4SLinus Torvalds char tbuf[16]; 13501da177e4SLinus Torvalds 1351673d57e7SHarvey Harrison sprintf(tbuf, "%pI4", n->key); 13521da177e4SLinus Torvalds seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n", 13531da177e4SLinus Torvalds tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00", 13541da177e4SLinus Torvalds dev ? dev->name : "*"); 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds static int arp_seq_show(struct seq_file *seq, void *v) 13581da177e4SLinus Torvalds { 13591da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 13601da177e4SLinus Torvalds seq_puts(seq, "IP address HW type Flags " 13611da177e4SLinus Torvalds "HW address Mask Device\n"); 13621da177e4SLinus Torvalds } else { 13631da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds if (state->flags & NEIGH_SEQ_IS_PNEIGH) 13661da177e4SLinus Torvalds arp_format_pneigh_entry(seq, v); 13671da177e4SLinus Torvalds else 13681da177e4SLinus Torvalds arp_format_neigh_entry(seq, v); 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds return 0; 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds static void *arp_seq_start(struct seq_file *seq, loff_t *pos) 13751da177e4SLinus Torvalds { 13761da177e4SLinus Torvalds /* Don't want to confuse "arp -a" w/ magic entries, 13771da177e4SLinus Torvalds * so we tell the generic iterator to skip NUD_NOARP. 13781da177e4SLinus Torvalds */ 13791da177e4SLinus Torvalds return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP); 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */ 13831da177e4SLinus Torvalds 1384f690808eSStephen Hemminger static const struct seq_operations arp_seq_ops = { 13851da177e4SLinus Torvalds .start = arp_seq_start, 13861da177e4SLinus Torvalds .next = neigh_seq_next, 13871da177e4SLinus Torvalds .stop = neigh_seq_stop, 13881da177e4SLinus Torvalds .show = arp_seq_show, 13891da177e4SLinus Torvalds }; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds static int arp_seq_open(struct inode *inode, struct file *file) 13921da177e4SLinus Torvalds { 1393426b5303SEric W. Biederman return seq_open_net(inode, file, &arp_seq_ops, 1394cf7732e4SPavel Emelyanov sizeof(struct neigh_seq_state)); 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 13979a32144eSArjan van de Ven static const struct file_operations arp_seq_fops = { 13981da177e4SLinus Torvalds .owner = THIS_MODULE, 13991da177e4SLinus Torvalds .open = arp_seq_open, 14001da177e4SLinus Torvalds .read = seq_read, 14011da177e4SLinus Torvalds .llseek = seq_lseek, 1402426b5303SEric W. Biederman .release = seq_release_net, 14031da177e4SLinus Torvalds }; 14041da177e4SLinus Torvalds 1405ffc31d3dSDenis V. Lunev 1406ffc31d3dSDenis V. Lunev static int __net_init arp_net_init(struct net *net) 14071da177e4SLinus Torvalds { 1408d4beaa66SGao feng if (!proc_create("arp", S_IRUGO, net->proc_net, &arp_seq_fops)) 14091da177e4SLinus Torvalds return -ENOMEM; 14101da177e4SLinus Torvalds return 0; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 1413ffc31d3dSDenis V. Lunev static void __net_exit arp_net_exit(struct net *net) 1414ffc31d3dSDenis V. Lunev { 1415*ece31ffdSGao feng remove_proc_entry("arp", net->proc_net); 1416ffc31d3dSDenis V. Lunev } 1417ffc31d3dSDenis V. Lunev 1418ffc31d3dSDenis V. Lunev static struct pernet_operations arp_net_ops = { 1419ffc31d3dSDenis V. Lunev .init = arp_net_init, 1420ffc31d3dSDenis V. Lunev .exit = arp_net_exit, 1421ffc31d3dSDenis V. Lunev }; 1422ffc31d3dSDenis V. Lunev 1423ffc31d3dSDenis V. Lunev static int __init arp_proc_init(void) 1424ffc31d3dSDenis V. Lunev { 1425ffc31d3dSDenis V. Lunev return register_pernet_subsys(&arp_net_ops); 1426ffc31d3dSDenis V. Lunev } 1427ffc31d3dSDenis V. Lunev 14281da177e4SLinus Torvalds #else /* CONFIG_PROC_FS */ 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds static int __init arp_proc_init(void) 14311da177e4SLinus Torvalds { 14321da177e4SLinus Torvalds return 0; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 1436