arp.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* linux/net/ipv4/arp.c
  3. *
  4. * Copyright (C) 1994 by Florian La Roche
  5. *
  6. * This module implements the Address Resolution Protocol ARP (RFC 826),
  7. * which is used to convert IP addresses (or in the future maybe other
  8. * high-level addresses) into a low-level hardware address (like an Ethernet
  9. * address).
  10. *
  11. * Fixes:
  12. * Alan Cox : Removed the Ethernet assumptions in
  13. * Florian's code
  14. * Alan Cox : Fixed some small errors in the ARP
  15. * logic
  16. * Alan Cox : Allow >4K in /proc
  17. * Alan Cox : Make ARP add its own protocol entry
  18. * Ross Martin : Rewrote arp_rcv() and arp_get_info()
  19. * Stephen Henson : Add AX25 support to arp_get_info()
  20. * Alan Cox : Drop data when a device is downed.
  21. * Alan Cox : Use init_timer().
  22. * Alan Cox : Double lock fixes.
  23. * Martin Seine : Move the arphdr structure
  24. * to if_arp.h for compatibility.
  25. * with BSD based programs.
  26. * Andrew Tridgell : Added ARP netmask code and
  27. * re-arranged proxy handling.
  28. * Alan Cox : Changed to use notifiers.
  29. * Niibe Yutaka : Reply for this device or proxies only.
  30. * Alan Cox : Don't proxy across hardware types!
  31. * Jonathan Naylor : Added support for NET/ROM.
  32. * Mike Shaver : RFC1122 checks.
  33. * Jonathan Naylor : Only lookup the hardware address for
  34. * the correct hardware type.
  35. * Germano Caronni : Assorted subtle races.
  36. * Craig Schlenter : Don't modify permanent entry
  37. * during arp_rcv.
  38. * Russ Nelson : Tidied up a few bits.
  39. * Alexey Kuznetsov: Major changes to caching and behaviour,
  40. * eg intelligent arp probing and
  41. * generation
  42. * of host down events.
  43. * Alan Cox : Missing unlock in device events.
  44. * Eckes : ARP ioctl control errors.
  45. * Alexey Kuznetsov: Arp free fix.
  46. * Manuel Rodriguez: Gratuitous ARP.
  47. * Jonathan Layes : Added arpd support through kerneld
  48. * message queue (960314)
  49. * Mike Shaver : /proc/sys/net/ipv4/arp_* support
  50. * Mike McLagan : Routing by source
  51. * Stuart Cheshire : Metricom and grat arp fixes
  52. * *** FOR 2.1 clean this up ***
  53. * Lawrence V. Stefani: (08/12/96) Added FDDI support.
  54. * Alan Cox : Took the AP1000 nasty FDDI hack and
  55. * folded into the mainstream FDDI code.
  56. * Ack spit, Linus how did you allow that
  57. * one in...
  58. * Jes Sorensen : Make FDDI work again in 2.1.x and
  59. * clean up the APFDDI & gen. FDDI bits.
  60. * Alexey Kuznetsov: new arp state machine;
  61. * now it is in net/core/neighbour.c.
  62. * Krzysztof Halasa: Added Frame Relay ARP support.
  63. * Arnaldo C. Melo : convert /proc/net/arp to seq_file
  64. * Shmulik Hen: Split arp_send to arp_create and
  65. * arp_xmit so intermediate drivers like
  66. * bonding can change the skb before
  67. * sending (e.g. insert 8021q tag).
  68. * Harald Welte : convert to make use of jenkins hash
  69. * Jesper D. Brouer: Proxy ARP PVLAN RFC 3069 support.
  70. */
  71. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  72. #include <linux/module.h>
  73. #include <linux/types.h>
  74. #include <linux/string.h>
  75. #include <linux/kernel.h>
  76. #include <linux/capability.h>
  77. #include <linux/socket.h>
  78. #include <linux/sockios.h>
  79. #include <linux/errno.h>
  80. #include <linux/hex.h>
  81. #include <linux/in.h>
  82. #include <linux/mm.h>
  83. #include <linux/inet.h>
  84. #include <linux/inetdevice.h>
  85. #include <linux/netdevice.h>
  86. #include <linux/etherdevice.h>
  87. #include <linux/fddidevice.h>
  88. #include <linux/if_arp.h>
  89. #include <linux/skbuff.h>
  90. #include <linux/proc_fs.h>
  91. #include <linux/seq_file.h>
  92. #include <linux/stat.h>
  93. #include <linux/init.h>
  94. #include <linux/net.h>
  95. #include <linux/rcupdate.h>
  96. #include <linux/slab.h>
  97. #ifdef CONFIG_SYSCTL
  98. #include <linux/sysctl.h>
  99. #endif
  100. #include <net/net_namespace.h>
  101. #include <net/ip.h>
  102. #include <net/icmp.h>
  103. #include <net/route.h>
  104. #include <net/protocol.h>
  105. #include <net/tcp.h>
  106. #include <net/sock.h>
  107. #include <net/arp.h>
  108. #include <net/ax25.h>
  109. #include <net/netrom.h>
  110. #include <net/dst_metadata.h>
  111. #include <net/ip_tunnels.h>
  112. #include <linux/uaccess.h>
  113. #include <linux/netfilter_arp.h>
  114. /*
  115. * Interface to generic neighbour cache.
  116. */
  117. static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd);
  118. static bool arp_key_eq(const struct neighbour *n, const void *pkey);
  119. static int arp_constructor(struct neighbour *neigh);
  120. static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
  121. static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
  122. static void parp_redo(struct sk_buff *skb);
  123. static int arp_is_multicast(const void *pkey);
  124. static const struct neigh_ops arp_generic_ops = {
  125. .family = AF_INET,
  126. .solicit = arp_solicit,
  127. .error_report = arp_error_report,
  128. .output = neigh_resolve_output,
  129. .connected_output = neigh_connected_output,
  130. };
  131. static const struct neigh_ops arp_hh_ops = {
  132. .family = AF_INET,
  133. .solicit = arp_solicit,
  134. .error_report = arp_error_report,
  135. .output = neigh_resolve_output,
  136. .connected_output = neigh_resolve_output,
  137. };
  138. static const struct neigh_ops arp_direct_ops = {
  139. .family = AF_INET,
  140. .output = neigh_direct_output,
  141. .connected_output = neigh_direct_output,
  142. };
  143. struct neigh_table arp_tbl = {
  144. .family = AF_INET,
  145. .key_len = 4,
  146. .protocol = cpu_to_be16(ETH_P_IP),
  147. .hash = arp_hash,
  148. .key_eq = arp_key_eq,
  149. .constructor = arp_constructor,
  150. .proxy_redo = parp_redo,
  151. .is_multicast = arp_is_multicast,
  152. .id = "arp_cache",
  153. .parms = {
  154. .tbl = &arp_tbl,
  155. .reachable_time = 30 * HZ,
  156. .data = {
  157. [NEIGH_VAR_MCAST_PROBES] = 3,
  158. [NEIGH_VAR_UCAST_PROBES] = 3,
  159. [NEIGH_VAR_RETRANS_TIME] = 1 * HZ,
  160. [NEIGH_VAR_BASE_REACHABLE_TIME] = 30 * HZ,
  161. [NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
  162. [NEIGH_VAR_INTERVAL_PROBE_TIME_MS] = 5 * HZ,
  163. [NEIGH_VAR_GC_STALETIME] = 60 * HZ,
  164. [NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_DEFAULT,
  165. [NEIGH_VAR_PROXY_QLEN] = 64,
  166. [NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
  167. [NEIGH_VAR_PROXY_DELAY] = (8 * HZ) / 10,
  168. [NEIGH_VAR_LOCKTIME] = 1 * HZ,
  169. },
  170. },
  171. .gc_interval = 30 * HZ,
  172. .gc_thresh1 = 128,
  173. .gc_thresh2 = 512,
  174. .gc_thresh3 = 1024,
  175. };
  176. EXPORT_SYMBOL(arp_tbl);
  177. int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir)
  178. {
  179. switch (dev->type) {
  180. case ARPHRD_ETHER:
  181. case ARPHRD_FDDI:
  182. case ARPHRD_IEEE802:
  183. ip_eth_mc_map(addr, haddr);
  184. return 0;
  185. case ARPHRD_INFINIBAND:
  186. ip_ib_mc_map(addr, dev->broadcast, haddr);
  187. return 0;
  188. case ARPHRD_IPGRE:
  189. ip_ipgre_mc_map(addr, dev->broadcast, haddr);
  190. return 0;
  191. default:
  192. if (dir) {
  193. memcpy(haddr, dev->broadcast, dev->addr_len);
  194. return 0;
  195. }
  196. }
  197. return -EINVAL;
  198. }
  199. static u32 arp_hash(const void *pkey,
  200. const struct net_device *dev,
  201. __u32 *hash_rnd)
  202. {
  203. return arp_hashfn(pkey, dev, hash_rnd);
  204. }
  205. static bool arp_key_eq(const struct neighbour *neigh, const void *pkey)
  206. {
  207. return neigh_key_eq32(neigh, pkey);
  208. }
  209. static int arp_constructor(struct neighbour *neigh)
  210. {
  211. __be32 addr;
  212. struct net_device *dev = neigh->dev;
  213. struct in_device *in_dev;
  214. struct neigh_parms *parms;
  215. u32 inaddr_any = INADDR_ANY;
  216. if (dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
  217. memcpy(neigh->primary_key, &inaddr_any, arp_tbl.key_len);
  218. addr = *(__be32 *)neigh->primary_key;
  219. rcu_read_lock();
  220. in_dev = __in_dev_get_rcu(dev);
  221. if (!in_dev) {
  222. rcu_read_unlock();
  223. return -EINVAL;
  224. }
  225. neigh->type = inet_addr_type_dev_table(dev_net(dev), dev, addr);
  226. parms = in_dev->arp_parms;
  227. __neigh_parms_put(neigh->parms);
  228. neigh->parms = neigh_parms_clone(parms);
  229. rcu_read_unlock();
  230. if (!dev->header_ops) {
  231. neigh->nud_state = NUD_NOARP;
  232. neigh->ops = &arp_direct_ops;
  233. neigh->output = neigh_direct_output;
  234. } else {
  235. /* Good devices (checked by reading texts, but only Ethernet is
  236. tested)
  237. ARPHRD_ETHER: (ethernet, apfddi)
  238. ARPHRD_FDDI: (fddi)
  239. ARPHRD_IEEE802: (tr)
  240. ARPHRD_METRICOM: (strip)
  241. ARPHRD_ARCNET:
  242. etc. etc. etc.
  243. ARPHRD_IPDDP will also work, if author repairs it.
  244. I did not it, because this driver does not work even
  245. in old paradigm.
  246. */
  247. if (neigh->type == RTN_MULTICAST) {
  248. neigh->nud_state = NUD_NOARP;
  249. arp_mc_map(addr, neigh->ha, dev, 1);
  250. } else if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) {
  251. neigh->nud_state = NUD_NOARP;
  252. memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
  253. } else if (neigh->type == RTN_BROADCAST ||
  254. (dev->flags & IFF_POINTOPOINT)) {
  255. neigh->nud_state = NUD_NOARP;
  256. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  257. }
  258. if (dev->header_ops->cache)
  259. neigh->ops = &arp_hh_ops;
  260. else
  261. neigh->ops = &arp_generic_ops;
  262. if (neigh->nud_state & NUD_VALID)
  263. neigh->output = neigh->ops->connected_output;
  264. else
  265. neigh->output = neigh->ops->output;
  266. }
  267. return 0;
  268. }
  269. static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
  270. {
  271. dst_link_failure(skb);
  272. kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_FAILED);
  273. }
  274. /* Create and send an arp packet. */
  275. static void arp_send_dst(int type, int ptype, __be32 dest_ip,
  276. struct net_device *dev, __be32 src_ip,
  277. const unsigned char *dest_hw,
  278. const unsigned char *src_hw,
  279. const unsigned char *target_hw,
  280. struct dst_entry *dst)
  281. {
  282. struct sk_buff *skb;
  283. /* arp on this interface. */
  284. if (dev->flags & IFF_NOARP)
  285. return;
  286. skb = arp_create(type, ptype, dest_ip, dev, src_ip,
  287. dest_hw, src_hw, target_hw);
  288. if (!skb)
  289. return;
  290. skb_dst_set(skb, dst_clone(dst));
  291. arp_xmit(skb);
  292. }
  293. void arp_send(int type, int ptype, __be32 dest_ip,
  294. struct net_device *dev, __be32 src_ip,
  295. const unsigned char *dest_hw, const unsigned char *src_hw,
  296. const unsigned char *target_hw)
  297. {
  298. arp_send_dst(type, ptype, dest_ip, dev, src_ip, dest_hw, src_hw,
  299. target_hw, NULL);
  300. }
  301. EXPORT_SYMBOL(arp_send);
  302. static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
  303. {
  304. __be32 saddr = 0;
  305. u8 dst_ha[MAX_ADDR_LEN], *dst_hw = NULL;
  306. struct net_device *dev = neigh->dev;
  307. __be32 target = *(__be32 *)neigh->primary_key;
  308. int probes = atomic_read(&neigh->probes);
  309. struct in_device *in_dev;
  310. struct dst_entry *dst = NULL;
  311. rcu_read_lock();
  312. in_dev = __in_dev_get_rcu(dev);
  313. if (!in_dev) {
  314. rcu_read_unlock();
  315. return;
  316. }
  317. switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
  318. default:
  319. case 0: /* By default announce any local IP */
  320. if (skb && inet_addr_type_dev_table(dev_net(dev), dev,
  321. ip_hdr(skb)->saddr) == RTN_LOCAL)
  322. saddr = ip_hdr(skb)->saddr;
  323. break;
  324. case 1: /* Restrict announcements of saddr in same subnet */
  325. if (!skb)
  326. break;
  327. saddr = ip_hdr(skb)->saddr;
  328. if (inet_addr_type_dev_table(dev_net(dev), dev,
  329. saddr) == RTN_LOCAL) {
  330. /* saddr should be known to target */
  331. if (inet_addr_onlink(in_dev, target, saddr))
  332. break;
  333. }
  334. saddr = 0;
  335. break;
  336. case 2: /* Avoid secondary IPs, get a primary/preferred one */
  337. break;
  338. }
  339. rcu_read_unlock();
  340. if (!saddr)
  341. saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
  342. probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
  343. if (probes < 0) {
  344. if (!(READ_ONCE(neigh->nud_state) & NUD_VALID))
  345. pr_debug("trying to ucast probe in NUD_INVALID\n");
  346. neigh_ha_snapshot(dst_ha, neigh, dev);
  347. dst_hw = dst_ha;
  348. } else {
  349. probes -= NEIGH_VAR(neigh->parms, APP_PROBES);
  350. if (probes < 0) {
  351. neigh_app_ns(neigh);
  352. return;
  353. }
  354. }
  355. if (skb && !(dev->priv_flags & IFF_XMIT_DST_RELEASE))
  356. dst = skb_dst(skb);
  357. arp_send_dst(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
  358. dst_hw, dev->dev_addr, NULL, dst);
  359. }
  360. static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip)
  361. {
  362. struct net *net = dev_net(in_dev->dev);
  363. int scope;
  364. switch (IN_DEV_ARP_IGNORE(in_dev)) {
  365. case 0: /* Reply, the tip is already validated */
  366. return 0;
  367. case 1: /* Reply only if tip is configured on the incoming interface */
  368. sip = 0;
  369. scope = RT_SCOPE_HOST;
  370. break;
  371. case 2: /*
  372. * Reply only if tip is configured on the incoming interface
  373. * and is in same subnet as sip
  374. */
  375. scope = RT_SCOPE_HOST;
  376. break;
  377. case 3: /* Do not reply for scope host addresses */
  378. sip = 0;
  379. scope = RT_SCOPE_LINK;
  380. in_dev = NULL;
  381. break;
  382. case 4: /* Reserved */
  383. case 5:
  384. case 6:
  385. case 7:
  386. return 0;
  387. case 8: /* Do not reply */
  388. return 1;
  389. default:
  390. return 0;
  391. }
  392. return !inet_confirm_addr(net, in_dev, sip, tip, scope);
  393. }
  394. static int arp_accept(struct in_device *in_dev, __be32 sip)
  395. {
  396. struct net *net = dev_net(in_dev->dev);
  397. int scope = RT_SCOPE_LINK;
  398. switch (IN_DEV_ARP_ACCEPT(in_dev)) {
  399. case 0: /* Don't create new entries from garp */
  400. return 0;
  401. case 1: /* Create new entries from garp */
  402. return 1;
  403. case 2: /* Create a neighbor in the arp table only if sip
  404. * is in the same subnet as an address configured
  405. * on the interface that received the garp message
  406. */
  407. return !!inet_confirm_addr(net, in_dev, sip, 0, scope);
  408. default:
  409. return 0;
  410. }
  411. }
  412. static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev)
  413. {
  414. struct rtable *rt;
  415. int flag = 0;
  416. /*unsigned long now; */
  417. struct net *net = dev_net(dev);
  418. rt = ip_route_output(net, sip, tip, 0, l3mdev_master_ifindex_rcu(dev),
  419. RT_SCOPE_UNIVERSE);
  420. if (IS_ERR(rt))
  421. return 1;
  422. if (rt->dst.dev != dev) {
  423. __NET_INC_STATS(net, LINUX_MIB_ARPFILTER);
  424. flag = 1;
  425. }
  426. ip_rt_put(rt);
  427. return flag;
  428. }
  429. /*
  430. * Check if we can use proxy ARP for this path
  431. */
  432. static inline int arp_fwd_proxy(struct in_device *in_dev,
  433. struct net_device *dev, struct rtable *rt)
  434. {
  435. struct in_device *out_dev;
  436. int imi, omi = -1;
  437. if (rt->dst.dev == dev)
  438. return 0;
  439. if (!IN_DEV_PROXY_ARP(in_dev))
  440. return 0;
  441. imi = IN_DEV_MEDIUM_ID(in_dev);
  442. if (imi == 0)
  443. return 1;
  444. if (imi == -1)
  445. return 0;
  446. /* place to check for proxy_arp for routes */
  447. out_dev = __in_dev_get_rcu(rt->dst.dev);
  448. if (out_dev)
  449. omi = IN_DEV_MEDIUM_ID(out_dev);
  450. return omi != imi && omi != -1;
  451. }
  452. /*
  453. * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev)
  454. *
  455. * RFC3069 supports proxy arp replies back to the same interface. This
  456. * is done to support (ethernet) switch features, like RFC 3069, where
  457. * the individual ports are not allowed to communicate with each
  458. * other, BUT they are allowed to talk to the upstream router. As
  459. * described in RFC 3069, it is possible to allow these hosts to
  460. * communicate through the upstream router, by proxy_arp'ing.
  461. *
  462. * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation"
  463. *
  464. * This technology is known by different names:
  465. * In RFC 3069 it is called VLAN Aggregation.
  466. * Cisco and Allied Telesyn call it Private VLAN.
  467. * Hewlett-Packard call it Source-Port filtering or port-isolation.
  468. * Ericsson call it MAC-Forced Forwarding (RFC Draft).
  469. *
  470. */
  471. static inline int arp_fwd_pvlan(struct in_device *in_dev,
  472. struct net_device *dev, struct rtable *rt,
  473. __be32 sip, __be32 tip)
  474. {
  475. /* Private VLAN is only concerned about the same ethernet segment */
  476. if (rt->dst.dev != dev)
  477. return 0;
  478. /* Don't reply on self probes (often done by windowz boxes)*/
  479. if (sip == tip)
  480. return 0;
  481. if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
  482. return 1;
  483. else
  484. return 0;
  485. }
  486. /*
  487. * Interface to link layer: send routine and receive handler.
  488. */
  489. /*
  490. * Create an arp packet. If dest_hw is not set, we create a broadcast
  491. * message.
  492. */
  493. struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
  494. struct net_device *dev, __be32 src_ip,
  495. const unsigned char *dest_hw,
  496. const unsigned char *src_hw,
  497. const unsigned char *target_hw)
  498. {
  499. struct sk_buff *skb;
  500. struct arphdr *arp;
  501. unsigned char *arp_ptr;
  502. int hlen = LL_RESERVED_SPACE(dev);
  503. int tlen = dev->needed_tailroom;
  504. /*
  505. * Allocate a buffer
  506. */
  507. skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC);
  508. if (!skb)
  509. return NULL;
  510. skb_reserve(skb, hlen);
  511. skb_reset_network_header(skb);
  512. skb_put(skb, arp_hdr_len(dev));
  513. skb->dev = dev;
  514. skb->protocol = htons(ETH_P_ARP);
  515. if (!src_hw)
  516. src_hw = dev->dev_addr;
  517. if (!dest_hw)
  518. dest_hw = dev->broadcast;
  519. /* Fill the device header for the ARP frame.
  520. * Note: skb->head can be changed.
  521. */
  522. if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
  523. goto out;
  524. arp = arp_hdr(skb);
  525. /*
  526. * Fill out the arp protocol part.
  527. *
  528. * The arp hardware type should match the device type, except for FDDI,
  529. * which (according to RFC 1390) should always equal 1 (Ethernet).
  530. */
  531. /*
  532. * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
  533. * DIX code for the protocol. Make these device structure fields.
  534. */
  535. switch (dev->type) {
  536. default:
  537. arp->ar_hrd = htons(dev->type);
  538. arp->ar_pro = htons(ETH_P_IP);
  539. break;
  540. #if IS_ENABLED(CONFIG_AX25)
  541. case ARPHRD_AX25:
  542. arp->ar_hrd = htons(ARPHRD_AX25);
  543. arp->ar_pro = htons(AX25_P_IP);
  544. break;
  545. #if IS_ENABLED(CONFIG_NETROM)
  546. case ARPHRD_NETROM:
  547. arp->ar_hrd = htons(ARPHRD_NETROM);
  548. arp->ar_pro = htons(AX25_P_IP);
  549. break;
  550. #endif
  551. #endif
  552. #if IS_ENABLED(CONFIG_FDDI)
  553. case ARPHRD_FDDI:
  554. arp->ar_hrd = htons(ARPHRD_ETHER);
  555. arp->ar_pro = htons(ETH_P_IP);
  556. break;
  557. #endif
  558. }
  559. arp->ar_hln = dev->addr_len;
  560. arp->ar_pln = 4;
  561. arp->ar_op = htons(type);
  562. arp_ptr = (unsigned char *)(arp + 1);
  563. memcpy(arp_ptr, src_hw, dev->addr_len);
  564. arp_ptr += dev->addr_len;
  565. memcpy(arp_ptr, &src_ip, 4);
  566. arp_ptr += 4;
  567. switch (dev->type) {
  568. #if IS_ENABLED(CONFIG_FIREWIRE_NET)
  569. case ARPHRD_IEEE1394:
  570. break;
  571. #endif
  572. default:
  573. if (target_hw)
  574. memcpy(arp_ptr, target_hw, dev->addr_len);
  575. else
  576. memset(arp_ptr, 0, dev->addr_len);
  577. arp_ptr += dev->addr_len;
  578. }
  579. memcpy(arp_ptr, &dest_ip, 4);
  580. return skb;
  581. out:
  582. kfree_skb(skb);
  583. return NULL;
  584. }
  585. EXPORT_SYMBOL(arp_create);
  586. static int arp_xmit_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  587. {
  588. return dev_queue_xmit(skb);
  589. }
  590. /*
  591. * Send an arp packet.
  592. */
  593. void arp_xmit(struct sk_buff *skb)
  594. {
  595. rcu_read_lock();
  596. /* Send it off, maybe filter it using firewalling first. */
  597. NF_HOOK(NFPROTO_ARP, NF_ARP_OUT,
  598. dev_net_rcu(skb->dev), NULL, skb, NULL, skb->dev,
  599. arp_xmit_finish);
  600. rcu_read_unlock();
  601. }
  602. EXPORT_SYMBOL(arp_xmit);
  603. static bool arp_is_garp(struct net *net, struct net_device *dev,
  604. int *addr_type, __be16 ar_op,
  605. __be32 sip, __be32 tip,
  606. unsigned char *sha, unsigned char *tha)
  607. {
  608. bool is_garp = tip == sip;
  609. /* Gratuitous ARP _replies_ also require target hwaddr to be
  610. * the same as source.
  611. */
  612. if (is_garp && ar_op == htons(ARPOP_REPLY))
  613. is_garp =
  614. /* IPv4 over IEEE 1394 doesn't provide target
  615. * hardware address field in its ARP payload.
  616. */
  617. tha &&
  618. !memcmp(tha, sha, dev->addr_len);
  619. if (is_garp) {
  620. *addr_type = inet_addr_type_dev_table(net, dev, sip);
  621. if (*addr_type != RTN_UNICAST)
  622. is_garp = false;
  623. }
  624. return is_garp;
  625. }
  626. /*
  627. * Process an arp request.
  628. */
  629. static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
  630. {
  631. struct net_device *dev = skb->dev;
  632. struct in_device *in_dev = __in_dev_get_rcu(dev);
  633. struct arphdr *arp;
  634. unsigned char *arp_ptr;
  635. struct rtable *rt;
  636. unsigned char *sha;
  637. unsigned char *tha = NULL;
  638. __be32 sip, tip;
  639. u16 dev_type = dev->type;
  640. int addr_type;
  641. struct neighbour *n;
  642. struct dst_entry *reply_dst = NULL;
  643. bool is_garp = false;
  644. /* arp_rcv below verifies the ARP header and verifies the device
  645. * is ARP'able.
  646. */
  647. if (!in_dev)
  648. goto out_free_skb;
  649. arp = arp_hdr(skb);
  650. switch (dev_type) {
  651. default:
  652. if (arp->ar_pro != htons(ETH_P_IP) ||
  653. htons(dev_type) != arp->ar_hrd)
  654. goto out_free_skb;
  655. break;
  656. case ARPHRD_ETHER:
  657. case ARPHRD_FDDI:
  658. case ARPHRD_IEEE802:
  659. /*
  660. * ETHERNET, and Fibre Channel (which are IEEE 802
  661. * devices, according to RFC 2625) devices will accept ARP
  662. * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
  663. * This is the case also of FDDI, where the RFC 1390 says that
  664. * FDDI devices should accept ARP hardware of (1) Ethernet,
  665. * however, to be more robust, we'll accept both 1 (Ethernet)
  666. * or 6 (IEEE 802.2)
  667. */
  668. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  669. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  670. arp->ar_pro != htons(ETH_P_IP))
  671. goto out_free_skb;
  672. break;
  673. case ARPHRD_AX25:
  674. if (arp->ar_pro != htons(AX25_P_IP) ||
  675. arp->ar_hrd != htons(ARPHRD_AX25))
  676. goto out_free_skb;
  677. break;
  678. case ARPHRD_NETROM:
  679. if (arp->ar_pro != htons(AX25_P_IP) ||
  680. arp->ar_hrd != htons(ARPHRD_NETROM))
  681. goto out_free_skb;
  682. break;
  683. }
  684. /* Understand only these message types */
  685. if (arp->ar_op != htons(ARPOP_REPLY) &&
  686. arp->ar_op != htons(ARPOP_REQUEST))
  687. goto out_free_skb;
  688. /*
  689. * Extract fields
  690. */
  691. arp_ptr = (unsigned char *)(arp + 1);
  692. sha = arp_ptr;
  693. arp_ptr += dev->addr_len;
  694. memcpy(&sip, arp_ptr, 4);
  695. arp_ptr += 4;
  696. switch (dev_type) {
  697. #if IS_ENABLED(CONFIG_FIREWIRE_NET)
  698. case ARPHRD_IEEE1394:
  699. break;
  700. #endif
  701. default:
  702. tha = arp_ptr;
  703. arp_ptr += dev->addr_len;
  704. }
  705. memcpy(&tip, arp_ptr, 4);
  706. /*
  707. * Check for bad requests for 127.x.x.x and requests for multicast
  708. * addresses. If this is one such, delete it.
  709. */
  710. if (ipv4_is_multicast(tip) ||
  711. (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip)))
  712. goto out_free_skb;
  713. /*
  714. * For some 802.11 wireless deployments (and possibly other networks),
  715. * there will be an ARP proxy and gratuitous ARP frames are attacks
  716. * and thus should not be accepted.
  717. */
  718. if (sip == tip && IN_DEV_ORCONF(in_dev, DROP_GRATUITOUS_ARP))
  719. goto out_free_skb;
  720. /*
  721. * Special case: We must set Frame Relay source Q.922 address
  722. */
  723. if (dev_type == ARPHRD_DLCI)
  724. sha = dev->broadcast;
  725. /*
  726. * Process entry. The idea here is we want to send a reply if it is a
  727. * request for us or if it is a request for someone else that we hold
  728. * a proxy for. We want to add an entry to our cache if it is a reply
  729. * to us or if it is a request for our address.
  730. * (The assumption for this last is that if someone is requesting our
  731. * address, they are probably intending to talk to us, so it saves time
  732. * if we cache their address. Their address is also probably not in
  733. * our cache, since ours is not in their cache.)
  734. *
  735. * Putting this another way, we only care about replies if they are to
  736. * us, in which case we add them to the cache. For requests, we care
  737. * about those for us and those for our proxies. We reply to both,
  738. * and in the case of requests for us we add the requester to the arp
  739. * cache.
  740. */
  741. if (arp->ar_op == htons(ARPOP_REQUEST) && skb_metadata_dst(skb))
  742. reply_dst = (struct dst_entry *)
  743. iptunnel_metadata_reply(skb_metadata_dst(skb),
  744. GFP_ATOMIC);
  745. /* Special case: IPv4 duplicate address detection packet (RFC2131) */
  746. if (sip == 0) {
  747. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  748. inet_addr_type_dev_table(net, dev, tip) == RTN_LOCAL &&
  749. !arp_ignore(in_dev, sip, tip))
  750. arp_send_dst(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip,
  751. sha, dev->dev_addr, sha, reply_dst);
  752. goto out_consume_skb;
  753. }
  754. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  755. ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
  756. rt = skb_rtable(skb);
  757. addr_type = rt->rt_type;
  758. if (addr_type == RTN_LOCAL) {
  759. int dont_send;
  760. dont_send = arp_ignore(in_dev, sip, tip);
  761. if (!dont_send && IN_DEV_ARPFILTER(in_dev))
  762. dont_send = arp_filter(sip, tip, dev);
  763. if (!dont_send) {
  764. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  765. if (n) {
  766. arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
  767. sip, dev, tip, sha,
  768. dev->dev_addr, sha,
  769. reply_dst);
  770. neigh_release(n);
  771. }
  772. }
  773. goto out_consume_skb;
  774. } else if (IN_DEV_FORWARD(in_dev)) {
  775. if (addr_type == RTN_UNICAST &&
  776. (arp_fwd_proxy(in_dev, dev, rt) ||
  777. arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
  778. (rt->dst.dev != dev &&
  779. pneigh_lookup(&arp_tbl, net, &tip, dev)))) {
  780. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  781. if (n)
  782. neigh_release(n);
  783. if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
  784. skb->pkt_type == PACKET_HOST ||
  785. NEIGH_VAR(in_dev->arp_parms, PROXY_DELAY) == 0) {
  786. arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
  787. sip, dev, tip, sha,
  788. dev->dev_addr, sha,
  789. reply_dst);
  790. } else {
  791. pneigh_enqueue(&arp_tbl,
  792. in_dev->arp_parms, skb);
  793. goto out_free_dst;
  794. }
  795. goto out_consume_skb;
  796. }
  797. }
  798. }
  799. /* Update our ARP tables */
  800. n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
  801. addr_type = -1;
  802. if (n || arp_accept(in_dev, sip)) {
  803. is_garp = arp_is_garp(net, dev, &addr_type, arp->ar_op,
  804. sip, tip, sha, tha);
  805. }
  806. if (arp_accept(in_dev, sip)) {
  807. /* Unsolicited ARP is not accepted by default.
  808. It is possible, that this option should be enabled for some
  809. devices (strip is candidate)
  810. */
  811. if (!n &&
  812. (is_garp ||
  813. (arp->ar_op == htons(ARPOP_REPLY) &&
  814. (addr_type == RTN_UNICAST ||
  815. (addr_type < 0 &&
  816. /* postpone calculation to as late as possible */
  817. inet_addr_type_dev_table(net, dev, sip) ==
  818. RTN_UNICAST)))))
  819. n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
  820. }
  821. if (n) {
  822. int state = NUD_REACHABLE;
  823. int override;
  824. /* If several different ARP replies follows back-to-back,
  825. use the FIRST one. It is possible, if several proxy
  826. agents are active. Taking the first reply prevents
  827. arp trashing and chooses the fastest router.
  828. */
  829. override = time_after(jiffies,
  830. n->updated +
  831. NEIGH_VAR(n->parms, LOCKTIME)) ||
  832. is_garp;
  833. /* Broadcast replies and request packets
  834. do not assert neighbour reachability.
  835. */
  836. if (arp->ar_op != htons(ARPOP_REPLY) ||
  837. skb->pkt_type != PACKET_HOST)
  838. state = NUD_STALE;
  839. neigh_update(n, sha, state,
  840. override ? NEIGH_UPDATE_F_OVERRIDE : 0, 0);
  841. neigh_release(n);
  842. }
  843. out_consume_skb:
  844. consume_skb(skb);
  845. out_free_dst:
  846. dst_release(reply_dst);
  847. return NET_RX_SUCCESS;
  848. out_free_skb:
  849. kfree_skb(skb);
  850. return NET_RX_DROP;
  851. }
  852. static void parp_redo(struct sk_buff *skb)
  853. {
  854. arp_process(dev_net(skb->dev), NULL, skb);
  855. }
  856. static int arp_is_multicast(const void *pkey)
  857. {
  858. return ipv4_is_multicast(*((__be32 *)pkey));
  859. }
  860. /*
  861. * Receive an arp request from the device layer.
  862. */
  863. static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
  864. struct packet_type *pt, struct net_device *orig_dev)
  865. {
  866. enum skb_drop_reason drop_reason;
  867. const struct arphdr *arp;
  868. /* do not tweak dropwatch on an ARP we will ignore */
  869. if (dev->flags & IFF_NOARP ||
  870. skb->pkt_type == PACKET_OTHERHOST ||
  871. skb->pkt_type == PACKET_LOOPBACK)
  872. goto consumeskb;
  873. skb = skb_share_check(skb, GFP_ATOMIC);
  874. if (!skb)
  875. goto out_of_mem;
  876. /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
  877. drop_reason = pskb_may_pull_reason(skb, arp_hdr_len(dev));
  878. if (drop_reason != SKB_NOT_DROPPED_YET)
  879. goto freeskb;
  880. arp = arp_hdr(skb);
  881. if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4) {
  882. drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
  883. goto freeskb;
  884. }
  885. memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
  886. return NF_HOOK(NFPROTO_ARP, NF_ARP_IN,
  887. dev_net(dev), NULL, skb, dev, NULL,
  888. arp_process);
  889. consumeskb:
  890. consume_skb(skb);
  891. return NET_RX_SUCCESS;
  892. freeskb:
  893. kfree_skb_reason(skb, drop_reason);
  894. out_of_mem:
  895. return NET_RX_DROP;
  896. }
  897. /*
  898. * User level interface (ioctl)
  899. */
  900. static struct net_device *arp_req_dev_by_name(struct net *net, struct arpreq *r,
  901. bool getarp)
  902. {
  903. struct net_device *dev;
  904. if (getarp)
  905. dev = dev_get_by_name_rcu(net, r->arp_dev);
  906. else
  907. dev = __dev_get_by_name(net, r->arp_dev);
  908. if (!dev)
  909. return ERR_PTR(-ENODEV);
  910. /* Mmmm... It is wrong... ARPHRD_NETROM == 0 */
  911. if (!r->arp_ha.sa_family)
  912. r->arp_ha.sa_family = dev->type;
  913. if ((r->arp_flags & ATF_COM) && r->arp_ha.sa_family != dev->type)
  914. return ERR_PTR(-EINVAL);
  915. return dev;
  916. }
  917. static struct net_device *arp_req_dev(struct net *net, struct arpreq *r)
  918. {
  919. struct net_device *dev;
  920. struct rtable *rt;
  921. __be32 ip;
  922. if (r->arp_dev[0])
  923. return arp_req_dev_by_name(net, r, false);
  924. if (r->arp_flags & ATF_PUBL)
  925. return NULL;
  926. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  927. rt = ip_route_output(net, ip, 0, 0, 0, RT_SCOPE_LINK);
  928. if (IS_ERR(rt))
  929. return ERR_CAST(rt);
  930. dev = rt->dst.dev;
  931. ip_rt_put(rt);
  932. if (!dev)
  933. return ERR_PTR(-EINVAL);
  934. return dev;
  935. }
  936. /*
  937. * Set (create) an ARP cache entry.
  938. */
  939. static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
  940. {
  941. if (!dev) {
  942. IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
  943. return 0;
  944. }
  945. if (__in_dev_get_rtnl_net(dev)) {
  946. IN_DEV_CONF_SET(__in_dev_get_rtnl_net(dev), PROXY_ARP, on);
  947. return 0;
  948. }
  949. return -ENXIO;
  950. }
  951. static int arp_req_set_public(struct net *net, struct arpreq *r,
  952. struct net_device *dev)
  953. {
  954. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  955. if (!dev && (r->arp_flags & ATF_COM)) {
  956. dev = dev_getbyhwaddr(net, r->arp_ha.sa_family,
  957. r->arp_ha.sa_data);
  958. if (!dev)
  959. return -ENODEV;
  960. }
  961. if (mask) {
  962. __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  963. return pneigh_create(&arp_tbl, net, &ip, dev, 0, 0, false);
  964. }
  965. return arp_req_set_proxy(net, dev, 1);
  966. }
  967. static int arp_req_set(struct net *net, struct arpreq *r)
  968. {
  969. struct neighbour *neigh;
  970. struct net_device *dev;
  971. __be32 ip;
  972. int err;
  973. dev = arp_req_dev(net, r);
  974. if (IS_ERR(dev))
  975. return PTR_ERR(dev);
  976. if (r->arp_flags & ATF_PUBL)
  977. return arp_req_set_public(net, r, dev);
  978. switch (dev->type) {
  979. #if IS_ENABLED(CONFIG_FDDI)
  980. case ARPHRD_FDDI:
  981. /*
  982. * According to RFC 1390, FDDI devices should accept ARP
  983. * hardware types of 1 (Ethernet). However, to be more
  984. * robust, we'll accept hardware types of either 1 (Ethernet)
  985. * or 6 (IEEE 802.2).
  986. */
  987. if (r->arp_ha.sa_family != ARPHRD_FDDI &&
  988. r->arp_ha.sa_family != ARPHRD_ETHER &&
  989. r->arp_ha.sa_family != ARPHRD_IEEE802)
  990. return -EINVAL;
  991. break;
  992. #endif
  993. default:
  994. if (r->arp_ha.sa_family != dev->type)
  995. return -EINVAL;
  996. break;
  997. }
  998. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  999. neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
  1000. err = PTR_ERR(neigh);
  1001. if (!IS_ERR(neigh)) {
  1002. unsigned int state = NUD_STALE;
  1003. if (r->arp_flags & ATF_PERM) {
  1004. r->arp_flags |= ATF_COM;
  1005. state = NUD_PERMANENT;
  1006. }
  1007. err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
  1008. r->arp_ha.sa_data : NULL, state,
  1009. NEIGH_UPDATE_F_OVERRIDE |
  1010. NEIGH_UPDATE_F_ADMIN, 0);
  1011. neigh_release(neigh);
  1012. }
  1013. return err;
  1014. }
  1015. static unsigned int arp_state_to_flags(struct neighbour *neigh)
  1016. {
  1017. if (neigh->nud_state&NUD_PERMANENT)
  1018. return ATF_PERM | ATF_COM;
  1019. else if (neigh->nud_state&NUD_VALID)
  1020. return ATF_COM;
  1021. else
  1022. return 0;
  1023. }
  1024. /*
  1025. * Get an ARP cache entry.
  1026. */
  1027. static int arp_req_get(struct net *net, struct arpreq *r)
  1028. {
  1029. __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
  1030. struct neighbour *neigh;
  1031. struct net_device *dev;
  1032. if (!r->arp_dev[0])
  1033. return -ENODEV;
  1034. dev = arp_req_dev_by_name(net, r, true);
  1035. if (IS_ERR(dev))
  1036. return PTR_ERR(dev);
  1037. neigh = neigh_lookup(&arp_tbl, &ip, dev);
  1038. if (!neigh)
  1039. return -ENXIO;
  1040. if (READ_ONCE(neigh->nud_state) & NUD_NOARP) {
  1041. neigh_release(neigh);
  1042. return -ENXIO;
  1043. }
  1044. read_lock_bh(&neigh->lock);
  1045. memcpy(r->arp_ha.sa_data, neigh->ha,
  1046. min(dev->addr_len, sizeof(r->arp_ha.sa_data)));
  1047. r->arp_flags = arp_state_to_flags(neigh);
  1048. read_unlock_bh(&neigh->lock);
  1049. neigh_release(neigh);
  1050. r->arp_ha.sa_family = dev->type;
  1051. netdev_copy_name(dev, r->arp_dev);
  1052. return 0;
  1053. }
  1054. int arp_invalidate(struct net_device *dev, __be32 ip, bool force)
  1055. {
  1056. struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
  1057. int err = -ENXIO;
  1058. struct neigh_table *tbl = &arp_tbl;
  1059. if (neigh) {
  1060. if ((READ_ONCE(neigh->nud_state) & NUD_VALID) && !force) {
  1061. neigh_release(neigh);
  1062. return 0;
  1063. }
  1064. if (READ_ONCE(neigh->nud_state) & ~NUD_NOARP)
  1065. err = neigh_update(neigh, NULL, NUD_FAILED,
  1066. NEIGH_UPDATE_F_OVERRIDE|
  1067. NEIGH_UPDATE_F_ADMIN, 0);
  1068. spin_lock_bh(&tbl->lock);
  1069. neigh_release(neigh);
  1070. neigh_remove_one(neigh);
  1071. spin_unlock_bh(&tbl->lock);
  1072. }
  1073. return err;
  1074. }
  1075. static int arp_req_delete_public(struct net *net, struct arpreq *r,
  1076. struct net_device *dev)
  1077. {
  1078. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  1079. if (mask) {
  1080. __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  1081. return pneigh_delete(&arp_tbl, net, &ip, dev);
  1082. }
  1083. return arp_req_set_proxy(net, dev, 0);
  1084. }
  1085. static int arp_req_delete(struct net *net, struct arpreq *r)
  1086. {
  1087. struct net_device *dev;
  1088. __be32 ip;
  1089. dev = arp_req_dev(net, r);
  1090. if (IS_ERR(dev))
  1091. return PTR_ERR(dev);
  1092. if (r->arp_flags & ATF_PUBL)
  1093. return arp_req_delete_public(net, r, dev);
  1094. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  1095. return arp_invalidate(dev, ip, true);
  1096. }
  1097. /*
  1098. * Handle an ARP layer I/O control request.
  1099. */
  1100. int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  1101. {
  1102. struct arpreq r;
  1103. __be32 *netmask;
  1104. int err;
  1105. switch (cmd) {
  1106. case SIOCDARP:
  1107. case SIOCSARP:
  1108. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1109. return -EPERM;
  1110. fallthrough;
  1111. case SIOCGARP:
  1112. err = copy_from_user(&r, arg, sizeof(struct arpreq));
  1113. if (err)
  1114. return -EFAULT;
  1115. break;
  1116. default:
  1117. return -EINVAL;
  1118. }
  1119. if (r.arp_pa.sa_family != AF_INET)
  1120. return -EPFNOSUPPORT;
  1121. if (!(r.arp_flags & ATF_PUBL) &&
  1122. (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
  1123. return -EINVAL;
  1124. netmask = &((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr;
  1125. if (!(r.arp_flags & ATF_NETMASK))
  1126. *netmask = htonl(0xFFFFFFFFUL);
  1127. else if (*netmask && *netmask != htonl(0xFFFFFFFFUL))
  1128. return -EINVAL;
  1129. switch (cmd) {
  1130. case SIOCDARP:
  1131. rtnl_net_lock(net);
  1132. err = arp_req_delete(net, &r);
  1133. rtnl_net_unlock(net);
  1134. break;
  1135. case SIOCSARP:
  1136. rtnl_net_lock(net);
  1137. err = arp_req_set(net, &r);
  1138. rtnl_net_unlock(net);
  1139. break;
  1140. case SIOCGARP:
  1141. rcu_read_lock();
  1142. err = arp_req_get(net, &r);
  1143. rcu_read_unlock();
  1144. if (!err && copy_to_user(arg, &r, sizeof(r)))
  1145. err = -EFAULT;
  1146. break;
  1147. }
  1148. return err;
  1149. }
  1150. static int arp_netdev_event(struct notifier_block *this, unsigned long event,
  1151. void *ptr)
  1152. {
  1153. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1154. struct netdev_notifier_change_info *change_info;
  1155. struct in_device *in_dev;
  1156. bool evict_nocarrier;
  1157. switch (event) {
  1158. case NETDEV_CHANGEADDR:
  1159. neigh_changeaddr(&arp_tbl, dev);
  1160. rt_cache_flush(dev_net(dev));
  1161. break;
  1162. case NETDEV_CHANGE:
  1163. change_info = ptr;
  1164. if (change_info->flags_changed & IFF_NOARP)
  1165. neigh_changeaddr(&arp_tbl, dev);
  1166. in_dev = __in_dev_get_rtnl(dev);
  1167. if (!in_dev)
  1168. evict_nocarrier = true;
  1169. else
  1170. evict_nocarrier = IN_DEV_ARP_EVICT_NOCARRIER(in_dev);
  1171. if (evict_nocarrier && !netif_carrier_ok(dev))
  1172. neigh_carrier_down(&arp_tbl, dev);
  1173. break;
  1174. default:
  1175. break;
  1176. }
  1177. return NOTIFY_DONE;
  1178. }
  1179. static struct notifier_block arp_netdev_notifier = {
  1180. .notifier_call = arp_netdev_event,
  1181. };
  1182. /* Note, that it is not on notifier chain.
  1183. It is necessary, that this routine was called after route cache will be
  1184. flushed.
  1185. */
  1186. void arp_ifdown(struct net_device *dev)
  1187. {
  1188. neigh_ifdown(&arp_tbl, dev);
  1189. }
  1190. /*
  1191. * Called once on startup.
  1192. */
  1193. static struct packet_type arp_packet_type __read_mostly = {
  1194. .type = cpu_to_be16(ETH_P_ARP),
  1195. .func = arp_rcv,
  1196. };
  1197. #ifdef CONFIG_PROC_FS
  1198. #if IS_ENABLED(CONFIG_AX25)
  1199. /*
  1200. * ax25 -> ASCII conversion
  1201. */
  1202. static void ax2asc2(ax25_address *a, char *buf)
  1203. {
  1204. char c, *s;
  1205. int n;
  1206. for (n = 0, s = buf; n < 6; n++) {
  1207. c = (a->ax25_call[n] >> 1) & 0x7F;
  1208. if (c != ' ')
  1209. *s++ = c;
  1210. }
  1211. *s++ = '-';
  1212. n = (a->ax25_call[6] >> 1) & 0x0F;
  1213. if (n > 9) {
  1214. *s++ = '1';
  1215. n -= 10;
  1216. }
  1217. *s++ = n + '0';
  1218. *s++ = '\0';
  1219. if (*buf == '\0' || *buf == '-') {
  1220. buf[0] = '*';
  1221. buf[1] = '\0';
  1222. }
  1223. }
  1224. #endif /* CONFIG_AX25 */
  1225. #define HBUFFERLEN 30
  1226. static void arp_format_neigh_entry(struct seq_file *seq,
  1227. struct neighbour *n)
  1228. {
  1229. char hbuffer[HBUFFERLEN];
  1230. int k, j;
  1231. char tbuf[16];
  1232. struct net_device *dev = n->dev;
  1233. int hatype = dev->type;
  1234. read_lock(&n->lock);
  1235. /* Convert hardware address to XX:XX:XX:XX ... form. */
  1236. #if IS_ENABLED(CONFIG_AX25)
  1237. if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
  1238. ax2asc2((ax25_address *)n->ha, hbuffer);
  1239. else {
  1240. #endif
  1241. for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
  1242. hbuffer[k++] = hex_asc_hi(n->ha[j]);
  1243. hbuffer[k++] = hex_asc_lo(n->ha[j]);
  1244. hbuffer[k++] = ':';
  1245. }
  1246. if (k != 0)
  1247. --k;
  1248. hbuffer[k] = 0;
  1249. #if IS_ENABLED(CONFIG_AX25)
  1250. }
  1251. #endif
  1252. sprintf(tbuf, "%pI4", n->primary_key);
  1253. seq_printf(seq, "%-16s 0x%-10x0x%-10x%-17s * %s\n",
  1254. tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
  1255. read_unlock(&n->lock);
  1256. }
  1257. static void arp_format_pneigh_entry(struct seq_file *seq,
  1258. struct pneigh_entry *n)
  1259. {
  1260. struct net_device *dev = n->dev;
  1261. int hatype = dev ? dev->type : 0;
  1262. char tbuf[16];
  1263. sprintf(tbuf, "%pI4", n->key);
  1264. seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
  1265. tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
  1266. dev ? dev->name : "*");
  1267. }
  1268. static int arp_seq_show(struct seq_file *seq, void *v)
  1269. {
  1270. if (v == SEQ_START_TOKEN) {
  1271. seq_puts(seq, "IP address HW type Flags "
  1272. "HW address Mask Device\n");
  1273. } else {
  1274. struct neigh_seq_state *state = seq->private;
  1275. if (state->flags & NEIGH_SEQ_IS_PNEIGH)
  1276. arp_format_pneigh_entry(seq, v);
  1277. else
  1278. arp_format_neigh_entry(seq, v);
  1279. }
  1280. return 0;
  1281. }
  1282. static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
  1283. {
  1284. /* Don't want to confuse "arp -a" w/ magic entries,
  1285. * so we tell the generic iterator to skip NUD_NOARP.
  1286. */
  1287. return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
  1288. }
  1289. static const struct seq_operations arp_seq_ops = {
  1290. .start = arp_seq_start,
  1291. .next = neigh_seq_next,
  1292. .stop = neigh_seq_stop,
  1293. .show = arp_seq_show,
  1294. };
  1295. #endif /* CONFIG_PROC_FS */
  1296. static int __net_init arp_net_init(struct net *net)
  1297. {
  1298. if (!proc_create_net("arp", 0444, net->proc_net, &arp_seq_ops,
  1299. sizeof(struct neigh_seq_state)))
  1300. return -ENOMEM;
  1301. return 0;
  1302. }
  1303. static void __net_exit arp_net_exit(struct net *net)
  1304. {
  1305. remove_proc_entry("arp", net->proc_net);
  1306. }
  1307. static struct pernet_operations arp_net_ops = {
  1308. .init = arp_net_init,
  1309. .exit = arp_net_exit,
  1310. };
  1311. void __init arp_init(void)
  1312. {
  1313. neigh_table_init(NEIGH_ARP_TABLE, &arp_tbl);
  1314. dev_add_pack(&arp_packet_type);
  1315. register_pernet_subsys(&arp_net_ops);
  1316. #ifdef CONFIG_SYSCTL
  1317. neigh_sysctl_register(NULL, &arp_tbl.parms, NULL);
  1318. #endif
  1319. register_netdevice_notifier(&arp_netdev_notifier);
  1320. }