arp.c 36 KB

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