arp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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] = 64 * 1024,
  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 = *(__be32 *)neigh->primary_key;
  212. struct net_device *dev = neigh->dev;
  213. struct in_device *in_dev;
  214. struct neigh_parms *parms;
  215. rcu_read_lock();
  216. in_dev = __in_dev_get_rcu(dev);
  217. if (!in_dev) {
  218. rcu_read_unlock();
  219. return -EINVAL;
  220. }
  221. neigh->type = inet_addr_type_dev_table(dev_net(dev), dev, addr);
  222. parms = in_dev->arp_parms;
  223. __neigh_parms_put(neigh->parms);
  224. neigh->parms = neigh_parms_clone(parms);
  225. rcu_read_unlock();
  226. if (!dev->header_ops) {
  227. neigh->nud_state = NUD_NOARP;
  228. neigh->ops = &arp_direct_ops;
  229. neigh->output = neigh_direct_output;
  230. } else {
  231. /* Good devices (checked by reading texts, but only Ethernet is
  232. tested)
  233. ARPHRD_ETHER: (ethernet, apfddi)
  234. ARPHRD_FDDI: (fddi)
  235. ARPHRD_IEEE802: (tr)
  236. ARPHRD_METRICOM: (strip)
  237. ARPHRD_ARCNET:
  238. etc. etc. etc.
  239. ARPHRD_IPDDP will also work, if author repairs it.
  240. I did not it, because this driver does not work even
  241. in old paradigm.
  242. */
  243. if (neigh->type == RTN_MULTICAST) {
  244. neigh->nud_state = NUD_NOARP;
  245. arp_mc_map(addr, neigh->ha, dev, 1);
  246. } else if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) {
  247. neigh->nud_state = NUD_NOARP;
  248. memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
  249. } else if (neigh->type == RTN_BROADCAST ||
  250. (dev->flags & IFF_POINTOPOINT)) {
  251. neigh->nud_state = NUD_NOARP;
  252. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  253. }
  254. if (dev->header_ops->cache)
  255. neigh->ops = &arp_hh_ops;
  256. else
  257. neigh->ops = &arp_generic_ops;
  258. if (neigh->nud_state & NUD_VALID)
  259. neigh->output = neigh->ops->connected_output;
  260. else
  261. neigh->output = neigh->ops->output;
  262. }
  263. return 0;
  264. }
  265. static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
  266. {
  267. dst_link_failure(skb);
  268. kfree_skb(skb);
  269. }
  270. /* Create and send an arp packet. */
  271. static void arp_send_dst(int type, int ptype, __be32 dest_ip,
  272. struct net_device *dev, __be32 src_ip,
  273. const unsigned char *dest_hw,
  274. const unsigned char *src_hw,
  275. const unsigned char *target_hw,
  276. struct dst_entry *dst)
  277. {
  278. struct sk_buff *skb;
  279. /* arp on this interface. */
  280. if (dev->flags & IFF_NOARP)
  281. return;
  282. skb = arp_create(type, ptype, dest_ip, dev, src_ip,
  283. dest_hw, src_hw, target_hw);
  284. if (!skb)
  285. return;
  286. skb_dst_set(skb, dst_clone(dst));
  287. arp_xmit(skb);
  288. }
  289. void arp_send(int type, int ptype, __be32 dest_ip,
  290. struct net_device *dev, __be32 src_ip,
  291. const unsigned char *dest_hw, const unsigned char *src_hw,
  292. const unsigned char *target_hw)
  293. {
  294. arp_send_dst(type, ptype, dest_ip, dev, src_ip, dest_hw, src_hw,
  295. target_hw, NULL);
  296. }
  297. EXPORT_SYMBOL(arp_send);
  298. static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
  299. {
  300. __be32 saddr = 0;
  301. u8 dst_ha[MAX_ADDR_LEN], *dst_hw = NULL;
  302. struct net_device *dev = neigh->dev;
  303. __be32 target = *(__be32 *)neigh->primary_key;
  304. int probes = atomic_read(&neigh->probes);
  305. struct in_device *in_dev;
  306. struct dst_entry *dst = NULL;
  307. rcu_read_lock();
  308. in_dev = __in_dev_get_rcu(dev);
  309. if (!in_dev) {
  310. rcu_read_unlock();
  311. return;
  312. }
  313. switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
  314. default:
  315. case 0: /* By default announce any local IP */
  316. if (skb && inet_addr_type_dev_table(dev_net(dev), dev,
  317. ip_hdr(skb)->saddr) == RTN_LOCAL)
  318. saddr = ip_hdr(skb)->saddr;
  319. break;
  320. case 1: /* Restrict announcements of saddr in same subnet */
  321. if (!skb)
  322. break;
  323. saddr = ip_hdr(skb)->saddr;
  324. if (inet_addr_type_dev_table(dev_net(dev), dev,
  325. saddr) == RTN_LOCAL) {
  326. /* saddr should be known to target */
  327. if (inet_addr_onlink(in_dev, target, saddr))
  328. break;
  329. }
  330. saddr = 0;
  331. break;
  332. case 2: /* Avoid secondary IPs, get a primary/preferred one */
  333. break;
  334. }
  335. rcu_read_unlock();
  336. if (!saddr)
  337. saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
  338. probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
  339. if (probes < 0) {
  340. if (!(neigh->nud_state & NUD_VALID))
  341. pr_debug("trying to ucast probe in NUD_INVALID\n");
  342. neigh_ha_snapshot(dst_ha, neigh, dev);
  343. dst_hw = dst_ha;
  344. } else {
  345. probes -= NEIGH_VAR(neigh->parms, APP_PROBES);
  346. if (probes < 0) {
  347. neigh_app_ns(neigh);
  348. return;
  349. }
  350. }
  351. if (skb && !(dev->priv_flags & IFF_XMIT_DST_RELEASE))
  352. dst = skb_dst(skb);
  353. arp_send_dst(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
  354. dst_hw, dev->dev_addr, NULL, dst);
  355. }
  356. static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip)
  357. {
  358. struct net *net = dev_net(in_dev->dev);
  359. int scope;
  360. switch (IN_DEV_ARP_IGNORE(in_dev)) {
  361. case 0: /* Reply, the tip is already validated */
  362. return 0;
  363. case 1: /* Reply only if tip is configured on the incoming interface */
  364. sip = 0;
  365. scope = RT_SCOPE_HOST;
  366. break;
  367. case 2: /*
  368. * Reply only if tip is configured on the incoming interface
  369. * and is in same subnet as sip
  370. */
  371. scope = RT_SCOPE_HOST;
  372. break;
  373. case 3: /* Do not reply for scope host addresses */
  374. sip = 0;
  375. scope = RT_SCOPE_LINK;
  376. in_dev = NULL;
  377. break;
  378. case 4: /* Reserved */
  379. case 5:
  380. case 6:
  381. case 7:
  382. return 0;
  383. case 8: /* Do not reply */
  384. return 1;
  385. default:
  386. return 0;
  387. }
  388. return !inet_confirm_addr(net, in_dev, sip, tip, scope);
  389. }
  390. static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev)
  391. {
  392. struct rtable *rt;
  393. int flag = 0;
  394. /*unsigned long now; */
  395. struct net *net = dev_net(dev);
  396. rt = ip_route_output(net, sip, tip, 0, 0);
  397. if (IS_ERR(rt))
  398. return 1;
  399. if (rt->dst.dev != dev) {
  400. __NET_INC_STATS(net, LINUX_MIB_ARPFILTER);
  401. flag = 1;
  402. }
  403. ip_rt_put(rt);
  404. return flag;
  405. }
  406. /*
  407. * Check if we can use proxy ARP for this path
  408. */
  409. static inline int arp_fwd_proxy(struct in_device *in_dev,
  410. struct net_device *dev, struct rtable *rt)
  411. {
  412. struct in_device *out_dev;
  413. int imi, omi = -1;
  414. if (rt->dst.dev == dev)
  415. return 0;
  416. if (!IN_DEV_PROXY_ARP(in_dev))
  417. return 0;
  418. imi = IN_DEV_MEDIUM_ID(in_dev);
  419. if (imi == 0)
  420. return 1;
  421. if (imi == -1)
  422. return 0;
  423. /* place to check for proxy_arp for routes */
  424. out_dev = __in_dev_get_rcu(rt->dst.dev);
  425. if (out_dev)
  426. omi = IN_DEV_MEDIUM_ID(out_dev);
  427. return omi != imi && omi != -1;
  428. }
  429. /*
  430. * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev)
  431. *
  432. * RFC3069 supports proxy arp replies back to the same interface. This
  433. * is done to support (ethernet) switch features, like RFC 3069, where
  434. * the individual ports are not allowed to communicate with each
  435. * other, BUT they are allowed to talk to the upstream router. As
  436. * described in RFC 3069, it is possible to allow these hosts to
  437. * communicate through the upstream router, by proxy_arp'ing.
  438. *
  439. * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation"
  440. *
  441. * This technology is known by different names:
  442. * In RFC 3069 it is called VLAN Aggregation.
  443. * Cisco and Allied Telesyn call it Private VLAN.
  444. * Hewlett-Packard call it Source-Port filtering or port-isolation.
  445. * Ericsson call it MAC-Forced Forwarding (RFC Draft).
  446. *
  447. */
  448. static inline int arp_fwd_pvlan(struct in_device *in_dev,
  449. struct net_device *dev, struct rtable *rt,
  450. __be32 sip, __be32 tip)
  451. {
  452. /* Private VLAN is only concerned about the same ethernet segment */
  453. if (rt->dst.dev != dev)
  454. return 0;
  455. /* Don't reply on self probes (often done by windowz boxes)*/
  456. if (sip == tip)
  457. return 0;
  458. if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
  459. return 1;
  460. else
  461. return 0;
  462. }
  463. /*
  464. * Interface to link layer: send routine and receive handler.
  465. */
  466. /*
  467. * Create an arp packet. If dest_hw is not set, we create a broadcast
  468. * message.
  469. */
  470. struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
  471. struct net_device *dev, __be32 src_ip,
  472. const unsigned char *dest_hw,
  473. const unsigned char *src_hw,
  474. const unsigned char *target_hw)
  475. {
  476. struct sk_buff *skb;
  477. struct arphdr *arp;
  478. unsigned char *arp_ptr;
  479. int hlen = LL_RESERVED_SPACE(dev);
  480. int tlen = dev->needed_tailroom;
  481. /*
  482. * Allocate a buffer
  483. */
  484. skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC);
  485. if (!skb)
  486. return NULL;
  487. skb_reserve(skb, hlen);
  488. skb_reset_network_header(skb);
  489. arp = skb_put(skb, arp_hdr_len(dev));
  490. skb->dev = dev;
  491. skb->protocol = htons(ETH_P_ARP);
  492. if (!src_hw)
  493. src_hw = dev->dev_addr;
  494. if (!dest_hw)
  495. dest_hw = dev->broadcast;
  496. /*
  497. * Fill the device header for the ARP frame
  498. */
  499. if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
  500. goto out;
  501. /*
  502. * Fill out the arp protocol part.
  503. *
  504. * The arp hardware type should match the device type, except for FDDI,
  505. * which (according to RFC 1390) should always equal 1 (Ethernet).
  506. */
  507. /*
  508. * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
  509. * DIX code for the protocol. Make these device structure fields.
  510. */
  511. switch (dev->type) {
  512. default:
  513. arp->ar_hrd = htons(dev->type);
  514. arp->ar_pro = htons(ETH_P_IP);
  515. break;
  516. #if IS_ENABLED(CONFIG_AX25)
  517. case ARPHRD_AX25:
  518. arp->ar_hrd = htons(ARPHRD_AX25);
  519. arp->ar_pro = htons(AX25_P_IP);
  520. break;
  521. #if IS_ENABLED(CONFIG_NETROM)
  522. case ARPHRD_NETROM:
  523. arp->ar_hrd = htons(ARPHRD_NETROM);
  524. arp->ar_pro = htons(AX25_P_IP);
  525. break;
  526. #endif
  527. #endif
  528. #if IS_ENABLED(CONFIG_FDDI)
  529. case ARPHRD_FDDI:
  530. arp->ar_hrd = htons(ARPHRD_ETHER);
  531. arp->ar_pro = htons(ETH_P_IP);
  532. break;
  533. #endif
  534. }
  535. arp->ar_hln = dev->addr_len;
  536. arp->ar_pln = 4;
  537. arp->ar_op = htons(type);
  538. arp_ptr = (unsigned char *)(arp + 1);
  539. memcpy(arp_ptr, src_hw, dev->addr_len);
  540. arp_ptr += dev->addr_len;
  541. memcpy(arp_ptr, &src_ip, 4);
  542. arp_ptr += 4;
  543. switch (dev->type) {
  544. #if IS_ENABLED(CONFIG_FIREWIRE_NET)
  545. case ARPHRD_IEEE1394:
  546. break;
  547. #endif
  548. default:
  549. if (target_hw)
  550. memcpy(arp_ptr, target_hw, dev->addr_len);
  551. else
  552. memset(arp_ptr, 0, dev->addr_len);
  553. arp_ptr += dev->addr_len;
  554. }
  555. memcpy(arp_ptr, &dest_ip, 4);
  556. return skb;
  557. out:
  558. kfree_skb(skb);
  559. return NULL;
  560. }
  561. EXPORT_SYMBOL(arp_create);
  562. static int arp_xmit_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  563. {
  564. return dev_queue_xmit(skb);
  565. }
  566. /*
  567. * Send an arp packet.
  568. */
  569. void arp_xmit(struct sk_buff *skb)
  570. {
  571. /* Send it off, maybe filter it using firewalling first. */
  572. NF_HOOK(NFPROTO_ARP, NF_ARP_OUT,
  573. dev_net(skb->dev), NULL, skb, NULL, skb->dev,
  574. arp_xmit_finish);
  575. }
  576. EXPORT_SYMBOL(arp_xmit);
  577. static bool arp_is_garp(struct net *net, struct net_device *dev,
  578. int *addr_type, __be16 ar_op,
  579. __be32 sip, __be32 tip,
  580. unsigned char *sha, unsigned char *tha)
  581. {
  582. bool is_garp = tip == sip;
  583. /* Gratuitous ARP _replies_ also require target hwaddr to be
  584. * the same as source.
  585. */
  586. if (is_garp && ar_op == htons(ARPOP_REPLY))
  587. is_garp =
  588. /* IPv4 over IEEE 1394 doesn't provide target
  589. * hardware address field in its ARP payload.
  590. */
  591. tha &&
  592. !memcmp(tha, sha, dev->addr_len);
  593. if (is_garp) {
  594. *addr_type = inet_addr_type_dev_table(net, dev, sip);
  595. if (*addr_type != RTN_UNICAST)
  596. is_garp = false;
  597. }
  598. return is_garp;
  599. }
  600. /*
  601. * Process an arp request.
  602. */
  603. static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
  604. {
  605. struct net_device *dev = skb->dev;
  606. struct in_device *in_dev = __in_dev_get_rcu(dev);
  607. struct arphdr *arp;
  608. unsigned char *arp_ptr;
  609. struct rtable *rt;
  610. unsigned char *sha;
  611. unsigned char *tha = NULL;
  612. __be32 sip, tip;
  613. u16 dev_type = dev->type;
  614. int addr_type;
  615. struct neighbour *n;
  616. struct dst_entry *reply_dst = NULL;
  617. bool is_garp = false;
  618. /* arp_rcv below verifies the ARP header and verifies the device
  619. * is ARP'able.
  620. */
  621. if (!in_dev)
  622. goto out_free_skb;
  623. arp = arp_hdr(skb);
  624. switch (dev_type) {
  625. default:
  626. if (arp->ar_pro != htons(ETH_P_IP) ||
  627. htons(dev_type) != arp->ar_hrd)
  628. goto out_free_skb;
  629. break;
  630. case ARPHRD_ETHER:
  631. case ARPHRD_FDDI:
  632. case ARPHRD_IEEE802:
  633. /*
  634. * ETHERNET, and Fibre Channel (which are IEEE 802
  635. * devices, according to RFC 2625) devices will accept ARP
  636. * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
  637. * This is the case also of FDDI, where the RFC 1390 says that
  638. * FDDI devices should accept ARP hardware of (1) Ethernet,
  639. * however, to be more robust, we'll accept both 1 (Ethernet)
  640. * or 6 (IEEE 802.2)
  641. */
  642. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  643. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  644. arp->ar_pro != htons(ETH_P_IP))
  645. goto out_free_skb;
  646. break;
  647. case ARPHRD_AX25:
  648. if (arp->ar_pro != htons(AX25_P_IP) ||
  649. arp->ar_hrd != htons(ARPHRD_AX25))
  650. goto out_free_skb;
  651. break;
  652. case ARPHRD_NETROM:
  653. if (arp->ar_pro != htons(AX25_P_IP) ||
  654. arp->ar_hrd != htons(ARPHRD_NETROM))
  655. goto out_free_skb;
  656. break;
  657. }
  658. /* Understand only these message types */
  659. if (arp->ar_op != htons(ARPOP_REPLY) &&
  660. arp->ar_op != htons(ARPOP_REQUEST))
  661. goto out_free_skb;
  662. /*
  663. * Extract fields
  664. */
  665. arp_ptr = (unsigned char *)(arp + 1);
  666. sha = arp_ptr;
  667. arp_ptr += dev->addr_len;
  668. memcpy(&sip, arp_ptr, 4);
  669. arp_ptr += 4;
  670. switch (dev_type) {
  671. #if IS_ENABLED(CONFIG_FIREWIRE_NET)
  672. case ARPHRD_IEEE1394:
  673. break;
  674. #endif
  675. default:
  676. tha = arp_ptr;
  677. arp_ptr += dev->addr_len;
  678. }
  679. memcpy(&tip, arp_ptr, 4);
  680. /*
  681. * Check for bad requests for 127.x.x.x and requests for multicast
  682. * addresses. If this is one such, delete it.
  683. */
  684. if (ipv4_is_multicast(tip) ||
  685. (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip)))
  686. goto out_free_skb;
  687. /*
  688. * For some 802.11 wireless deployments (and possibly other networks),
  689. * there will be an ARP proxy and gratuitous ARP frames are attacks
  690. * and thus should not be accepted.
  691. */
  692. if (sip == tip && IN_DEV_ORCONF(in_dev, DROP_GRATUITOUS_ARP))
  693. goto out_free_skb;
  694. /*
  695. * Special case: We must set Frame Relay source Q.922 address
  696. */
  697. if (dev_type == ARPHRD_DLCI)
  698. sha = dev->broadcast;
  699. /*
  700. * Process entry. The idea here is we want to send a reply if it is a
  701. * request for us or if it is a request for someone else that we hold
  702. * a proxy for. We want to add an entry to our cache if it is a reply
  703. * to us or if it is a request for our address.
  704. * (The assumption for this last is that if someone is requesting our
  705. * address, they are probably intending to talk to us, so it saves time
  706. * if we cache their address. Their address is also probably not in
  707. * our cache, since ours is not in their cache.)
  708. *
  709. * Putting this another way, we only care about replies if they are to
  710. * us, in which case we add them to the cache. For requests, we care
  711. * about those for us and those for our proxies. We reply to both,
  712. * and in the case of requests for us we add the requester to the arp
  713. * cache.
  714. */
  715. if (arp->ar_op == htons(ARPOP_REQUEST) && skb_metadata_dst(skb))
  716. reply_dst = (struct dst_entry *)
  717. iptunnel_metadata_reply(skb_metadata_dst(skb),
  718. GFP_ATOMIC);
  719. /* Special case: IPv4 duplicate address detection packet (RFC2131) */
  720. if (sip == 0) {
  721. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  722. inet_addr_type_dev_table(net, dev, tip) == RTN_LOCAL &&
  723. !arp_ignore(in_dev, sip, tip))
  724. arp_send_dst(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip,
  725. sha, dev->dev_addr, sha, reply_dst);
  726. goto out_consume_skb;
  727. }
  728. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  729. ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
  730. rt = skb_rtable(skb);
  731. addr_type = rt->rt_type;
  732. if (addr_type == RTN_LOCAL) {
  733. int dont_send;
  734. dont_send = arp_ignore(in_dev, sip, tip);
  735. if (!dont_send && IN_DEV_ARPFILTER(in_dev))
  736. dont_send = arp_filter(sip, tip, dev);
  737. if (!dont_send) {
  738. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  739. if (n) {
  740. arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
  741. sip, dev, tip, sha,
  742. dev->dev_addr, sha,
  743. reply_dst);
  744. neigh_release(n);
  745. }
  746. }
  747. goto out_consume_skb;
  748. } else if (IN_DEV_FORWARD(in_dev)) {
  749. if (addr_type == RTN_UNICAST &&
  750. (arp_fwd_proxy(in_dev, dev, rt) ||
  751. arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
  752. (rt->dst.dev != dev &&
  753. pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))) {
  754. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  755. if (n)
  756. neigh_release(n);
  757. if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
  758. skb->pkt_type == PACKET_HOST ||
  759. NEIGH_VAR(in_dev->arp_parms, PROXY_DELAY) == 0) {
  760. arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
  761. sip, dev, tip, sha,
  762. dev->dev_addr, sha,
  763. reply_dst);
  764. } else {
  765. pneigh_enqueue(&arp_tbl,
  766. in_dev->arp_parms, skb);
  767. goto out_free_dst;
  768. }
  769. goto out_consume_skb;
  770. }
  771. }
  772. }
  773. /* Update our ARP tables */
  774. n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
  775. addr_type = -1;
  776. if (n || IN_DEV_ARP_ACCEPT(in_dev)) {
  777. is_garp = arp_is_garp(net, dev, &addr_type, arp->ar_op,
  778. sip, tip, sha, tha);
  779. }
  780. if (IN_DEV_ARP_ACCEPT(in_dev)) {
  781. /* Unsolicited ARP is not accepted by default.
  782. It is possible, that this option should be enabled for some
  783. devices (strip is candidate)
  784. */
  785. if (!n &&
  786. (is_garp ||
  787. (arp->ar_op == htons(ARPOP_REPLY) &&
  788. (addr_type == RTN_UNICAST ||
  789. (addr_type < 0 &&
  790. /* postpone calculation to as late as possible */
  791. inet_addr_type_dev_table(net, dev, sip) ==
  792. RTN_UNICAST)))))
  793. n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
  794. }
  795. if (n) {
  796. int state = NUD_REACHABLE;
  797. int override;
  798. /* If several different ARP replies follows back-to-back,
  799. use the FIRST one. It is possible, if several proxy
  800. agents are active. Taking the first reply prevents
  801. arp trashing and chooses the fastest router.
  802. */
  803. override = time_after(jiffies,
  804. n->updated +
  805. NEIGH_VAR(n->parms, LOCKTIME)) ||
  806. is_garp;
  807. /* Broadcast replies and request packets
  808. do not assert neighbour reachability.
  809. */
  810. if (arp->ar_op != htons(ARPOP_REPLY) ||
  811. skb->pkt_type != PACKET_HOST)
  812. state = NUD_STALE;
  813. neigh_update(n, sha, state,
  814. override ? NEIGH_UPDATE_F_OVERRIDE : 0, 0);
  815. neigh_release(n);
  816. }
  817. out_consume_skb:
  818. consume_skb(skb);
  819. out_free_dst:
  820. dst_release(reply_dst);
  821. return NET_RX_SUCCESS;
  822. out_free_skb:
  823. kfree_skb(skb);
  824. return NET_RX_DROP;
  825. }
  826. static void parp_redo(struct sk_buff *skb)
  827. {
  828. arp_process(dev_net(skb->dev), NULL, skb);
  829. }
  830. /*
  831. * Receive an arp request from the device layer.
  832. */
  833. static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
  834. struct packet_type *pt, struct net_device *orig_dev)
  835. {
  836. const struct arphdr *arp;
  837. /* do not tweak dropwatch on an ARP we will ignore */
  838. if (dev->flags & IFF_NOARP ||
  839. skb->pkt_type == PACKET_OTHERHOST ||
  840. skb->pkt_type == PACKET_LOOPBACK)
  841. goto consumeskb;
  842. skb = skb_share_check(skb, GFP_ATOMIC);
  843. if (!skb)
  844. goto out_of_mem;
  845. /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
  846. if (!pskb_may_pull(skb, arp_hdr_len(dev)))
  847. goto freeskb;
  848. arp = arp_hdr(skb);
  849. if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4)
  850. goto freeskb;
  851. memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
  852. return NF_HOOK(NFPROTO_ARP, NF_ARP_IN,
  853. dev_net(dev), NULL, skb, dev, NULL,
  854. arp_process);
  855. consumeskb:
  856. consume_skb(skb);
  857. return NET_RX_SUCCESS;
  858. freeskb:
  859. kfree_skb(skb);
  860. out_of_mem:
  861. return NET_RX_DROP;
  862. }
  863. /*
  864. * User level interface (ioctl)
  865. */
  866. /*
  867. * Set (create) an ARP cache entry.
  868. */
  869. static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
  870. {
  871. if (!dev) {
  872. IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
  873. return 0;
  874. }
  875. if (__in_dev_get_rtnl(dev)) {
  876. IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
  877. return 0;
  878. }
  879. return -ENXIO;
  880. }
  881. static int arp_req_set_public(struct net *net, struct arpreq *r,
  882. struct net_device *dev)
  883. {
  884. __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  885. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  886. if (mask && mask != htonl(0xFFFFFFFF))
  887. return -EINVAL;
  888. if (!dev && (r->arp_flags & ATF_COM)) {
  889. dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
  890. r->arp_ha.sa_data);
  891. if (!dev)
  892. return -ENODEV;
  893. }
  894. if (mask) {
  895. if (!pneigh_lookup(&arp_tbl, net, &ip, dev, 1))
  896. return -ENOBUFS;
  897. return 0;
  898. }
  899. return arp_req_set_proxy(net, dev, 1);
  900. }
  901. static int arp_req_set(struct net *net, struct arpreq *r,
  902. struct net_device *dev)
  903. {
  904. __be32 ip;
  905. struct neighbour *neigh;
  906. int err;
  907. if (r->arp_flags & ATF_PUBL)
  908. return arp_req_set_public(net, r, dev);
  909. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  910. if (r->arp_flags & ATF_PERM)
  911. r->arp_flags |= ATF_COM;
  912. if (!dev) {
  913. struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
  914. if (IS_ERR(rt))
  915. return PTR_ERR(rt);
  916. dev = rt->dst.dev;
  917. ip_rt_put(rt);
  918. if (!dev)
  919. return -EINVAL;
  920. }
  921. switch (dev->type) {
  922. #if IS_ENABLED(CONFIG_FDDI)
  923. case ARPHRD_FDDI:
  924. /*
  925. * According to RFC 1390, FDDI devices should accept ARP
  926. * hardware types of 1 (Ethernet). However, to be more
  927. * robust, we'll accept hardware types of either 1 (Ethernet)
  928. * or 6 (IEEE 802.2).
  929. */
  930. if (r->arp_ha.sa_family != ARPHRD_FDDI &&
  931. r->arp_ha.sa_family != ARPHRD_ETHER &&
  932. r->arp_ha.sa_family != ARPHRD_IEEE802)
  933. return -EINVAL;
  934. break;
  935. #endif
  936. default:
  937. if (r->arp_ha.sa_family != dev->type)
  938. return -EINVAL;
  939. break;
  940. }
  941. neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
  942. err = PTR_ERR(neigh);
  943. if (!IS_ERR(neigh)) {
  944. unsigned int state = NUD_STALE;
  945. if (r->arp_flags & ATF_PERM)
  946. state = NUD_PERMANENT;
  947. err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
  948. r->arp_ha.sa_data : NULL, state,
  949. NEIGH_UPDATE_F_OVERRIDE |
  950. NEIGH_UPDATE_F_ADMIN, 0);
  951. neigh_release(neigh);
  952. }
  953. return err;
  954. }
  955. static unsigned int arp_state_to_flags(struct neighbour *neigh)
  956. {
  957. if (neigh->nud_state&NUD_PERMANENT)
  958. return ATF_PERM | ATF_COM;
  959. else if (neigh->nud_state&NUD_VALID)
  960. return ATF_COM;
  961. else
  962. return 0;
  963. }
  964. /*
  965. * Get an ARP cache entry.
  966. */
  967. static int arp_req_get(struct arpreq *r, struct net_device *dev)
  968. {
  969. __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
  970. struct neighbour *neigh;
  971. int err = -ENXIO;
  972. neigh = neigh_lookup(&arp_tbl, &ip, dev);
  973. if (neigh) {
  974. if (!(neigh->nud_state & NUD_NOARP)) {
  975. read_lock_bh(&neigh->lock);
  976. memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
  977. r->arp_flags = arp_state_to_flags(neigh);
  978. read_unlock_bh(&neigh->lock);
  979. r->arp_ha.sa_family = dev->type;
  980. strlcpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
  981. err = 0;
  982. }
  983. neigh_release(neigh);
  984. }
  985. return err;
  986. }
  987. static int arp_invalidate(struct net_device *dev, __be32 ip)
  988. {
  989. struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
  990. int err = -ENXIO;
  991. struct neigh_table *tbl = &arp_tbl;
  992. if (neigh) {
  993. if (neigh->nud_state & ~NUD_NOARP)
  994. err = neigh_update(neigh, NULL, NUD_FAILED,
  995. NEIGH_UPDATE_F_OVERRIDE|
  996. NEIGH_UPDATE_F_ADMIN, 0);
  997. write_lock_bh(&tbl->lock);
  998. neigh_release(neigh);
  999. neigh_remove_one(neigh, tbl);
  1000. write_unlock_bh(&tbl->lock);
  1001. }
  1002. return err;
  1003. }
  1004. static int arp_req_delete_public(struct net *net, struct arpreq *r,
  1005. struct net_device *dev)
  1006. {
  1007. __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
  1008. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  1009. if (mask == htonl(0xFFFFFFFF))
  1010. return pneigh_delete(&arp_tbl, net, &ip, dev);
  1011. if (mask)
  1012. return -EINVAL;
  1013. return arp_req_set_proxy(net, dev, 0);
  1014. }
  1015. static int arp_req_delete(struct net *net, struct arpreq *r,
  1016. struct net_device *dev)
  1017. {
  1018. __be32 ip;
  1019. if (r->arp_flags & ATF_PUBL)
  1020. return arp_req_delete_public(net, r, dev);
  1021. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  1022. if (!dev) {
  1023. struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
  1024. if (IS_ERR(rt))
  1025. return PTR_ERR(rt);
  1026. dev = rt->dst.dev;
  1027. ip_rt_put(rt);
  1028. if (!dev)
  1029. return -EINVAL;
  1030. }
  1031. return arp_invalidate(dev, ip);
  1032. }
  1033. /*
  1034. * Handle an ARP layer I/O control request.
  1035. */
  1036. int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  1037. {
  1038. int err;
  1039. struct arpreq r;
  1040. struct net_device *dev = NULL;
  1041. switch (cmd) {
  1042. case SIOCDARP:
  1043. case SIOCSARP:
  1044. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1045. return -EPERM;
  1046. case SIOCGARP:
  1047. err = copy_from_user(&r, arg, sizeof(struct arpreq));
  1048. if (err)
  1049. return -EFAULT;
  1050. break;
  1051. default:
  1052. return -EINVAL;
  1053. }
  1054. if (r.arp_pa.sa_family != AF_INET)
  1055. return -EPFNOSUPPORT;
  1056. if (!(r.arp_flags & ATF_PUBL) &&
  1057. (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
  1058. return -EINVAL;
  1059. if (!(r.arp_flags & ATF_NETMASK))
  1060. ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr =
  1061. htonl(0xFFFFFFFFUL);
  1062. rtnl_lock();
  1063. if (r.arp_dev[0]) {
  1064. err = -ENODEV;
  1065. dev = __dev_get_by_name(net, r.arp_dev);
  1066. if (!dev)
  1067. goto out;
  1068. /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
  1069. if (!r.arp_ha.sa_family)
  1070. r.arp_ha.sa_family = dev->type;
  1071. err = -EINVAL;
  1072. if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
  1073. goto out;
  1074. } else if (cmd == SIOCGARP) {
  1075. err = -ENODEV;
  1076. goto out;
  1077. }
  1078. switch (cmd) {
  1079. case SIOCDARP:
  1080. err = arp_req_delete(net, &r, dev);
  1081. break;
  1082. case SIOCSARP:
  1083. err = arp_req_set(net, &r, dev);
  1084. break;
  1085. case SIOCGARP:
  1086. err = arp_req_get(&r, dev);
  1087. break;
  1088. }
  1089. out:
  1090. rtnl_unlock();
  1091. if (cmd == SIOCGARP && !err && copy_to_user(arg, &r, sizeof(r)))
  1092. err = -EFAULT;
  1093. return err;
  1094. }
  1095. static int arp_netdev_event(struct notifier_block *this, unsigned long event,
  1096. void *ptr)
  1097. {
  1098. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1099. struct netdev_notifier_change_info *change_info;
  1100. switch (event) {
  1101. case NETDEV_CHANGEADDR:
  1102. neigh_changeaddr(&arp_tbl, dev);
  1103. rt_cache_flush(dev_net(dev));
  1104. break;
  1105. case NETDEV_CHANGE:
  1106. change_info = ptr;
  1107. if (change_info->flags_changed & IFF_NOARP)
  1108. neigh_changeaddr(&arp_tbl, dev);
  1109. break;
  1110. default:
  1111. break;
  1112. }
  1113. return NOTIFY_DONE;
  1114. }
  1115. static struct notifier_block arp_netdev_notifier = {
  1116. .notifier_call = arp_netdev_event,
  1117. };
  1118. /* Note, that it is not on notifier chain.
  1119. It is necessary, that this routine was called after route cache will be
  1120. flushed.
  1121. */
  1122. void arp_ifdown(struct net_device *dev)
  1123. {
  1124. neigh_ifdown(&arp_tbl, dev);
  1125. }
  1126. /*
  1127. * Called once on startup.
  1128. */
  1129. static struct packet_type arp_packet_type __read_mostly = {
  1130. .type = cpu_to_be16(ETH_P_ARP),
  1131. .func = arp_rcv,
  1132. };
  1133. static int arp_proc_init(void);
  1134. void __init arp_init(void)
  1135. {
  1136. neigh_table_init(NEIGH_ARP_TABLE, &arp_tbl);
  1137. dev_add_pack(&arp_packet_type);
  1138. arp_proc_init();
  1139. #ifdef CONFIG_SYSCTL
  1140. neigh_sysctl_register(NULL, &arp_tbl.parms, NULL);
  1141. #endif
  1142. register_netdevice_notifier(&arp_netdev_notifier);
  1143. }
  1144. #ifdef CONFIG_PROC_FS
  1145. #if IS_ENABLED(CONFIG_AX25)
  1146. /* ------------------------------------------------------------------------ */
  1147. /*
  1148. * ax25 -> ASCII conversion
  1149. */
  1150. static void ax2asc2(ax25_address *a, char *buf)
  1151. {
  1152. char c, *s;
  1153. int n;
  1154. for (n = 0, s = buf; n < 6; n++) {
  1155. c = (a->ax25_call[n] >> 1) & 0x7F;
  1156. if (c != ' ')
  1157. *s++ = c;
  1158. }
  1159. *s++ = '-';
  1160. n = (a->ax25_call[6] >> 1) & 0x0F;
  1161. if (n > 9) {
  1162. *s++ = '1';
  1163. n -= 10;
  1164. }
  1165. *s++ = n + '0';
  1166. *s++ = '\0';
  1167. if (*buf == '\0' || *buf == '-') {
  1168. buf[0] = '*';
  1169. buf[1] = '\0';
  1170. }
  1171. }
  1172. #endif /* CONFIG_AX25 */
  1173. #define HBUFFERLEN 30
  1174. static void arp_format_neigh_entry(struct seq_file *seq,
  1175. struct neighbour *n)
  1176. {
  1177. char hbuffer[HBUFFERLEN];
  1178. int k, j;
  1179. char tbuf[16];
  1180. struct net_device *dev = n->dev;
  1181. int hatype = dev->type;
  1182. read_lock(&n->lock);
  1183. /* Convert hardware address to XX:XX:XX:XX ... form. */
  1184. #if IS_ENABLED(CONFIG_AX25)
  1185. if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
  1186. ax2asc2((ax25_address *)n->ha, hbuffer);
  1187. else {
  1188. #endif
  1189. for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
  1190. hbuffer[k++] = hex_asc_hi(n->ha[j]);
  1191. hbuffer[k++] = hex_asc_lo(n->ha[j]);
  1192. hbuffer[k++] = ':';
  1193. }
  1194. if (k != 0)
  1195. --k;
  1196. hbuffer[k] = 0;
  1197. #if IS_ENABLED(CONFIG_AX25)
  1198. }
  1199. #endif
  1200. sprintf(tbuf, "%pI4", n->primary_key);
  1201. seq_printf(seq, "%-16s 0x%-10x0x%-10x%-17s * %s\n",
  1202. tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
  1203. read_unlock(&n->lock);
  1204. }
  1205. static void arp_format_pneigh_entry(struct seq_file *seq,
  1206. struct pneigh_entry *n)
  1207. {
  1208. struct net_device *dev = n->dev;
  1209. int hatype = dev ? dev->type : 0;
  1210. char tbuf[16];
  1211. sprintf(tbuf, "%pI4", n->key);
  1212. seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
  1213. tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
  1214. dev ? dev->name : "*");
  1215. }
  1216. static int arp_seq_show(struct seq_file *seq, void *v)
  1217. {
  1218. if (v == SEQ_START_TOKEN) {
  1219. seq_puts(seq, "IP address HW type Flags "
  1220. "HW address Mask Device\n");
  1221. } else {
  1222. struct neigh_seq_state *state = seq->private;
  1223. if (state->flags & NEIGH_SEQ_IS_PNEIGH)
  1224. arp_format_pneigh_entry(seq, v);
  1225. else
  1226. arp_format_neigh_entry(seq, v);
  1227. }
  1228. return 0;
  1229. }
  1230. static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
  1231. {
  1232. /* Don't want to confuse "arp -a" w/ magic entries,
  1233. * so we tell the generic iterator to skip NUD_NOARP.
  1234. */
  1235. return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
  1236. }
  1237. /* ------------------------------------------------------------------------ */
  1238. static const struct seq_operations arp_seq_ops = {
  1239. .start = arp_seq_start,
  1240. .next = neigh_seq_next,
  1241. .stop = neigh_seq_stop,
  1242. .show = arp_seq_show,
  1243. };
  1244. static int arp_seq_open(struct inode *inode, struct file *file)
  1245. {
  1246. return seq_open_net(inode, file, &arp_seq_ops,
  1247. sizeof(struct neigh_seq_state));
  1248. }
  1249. static const struct file_operations arp_seq_fops = {
  1250. .owner = THIS_MODULE,
  1251. .open = arp_seq_open,
  1252. .read = seq_read,
  1253. .llseek = seq_lseek,
  1254. .release = seq_release_net,
  1255. };
  1256. static int __net_init arp_net_init(struct net *net)
  1257. {
  1258. if (!proc_create("arp", S_IRUGO, net->proc_net, &arp_seq_fops))
  1259. return -ENOMEM;
  1260. return 0;
  1261. }
  1262. static void __net_exit arp_net_exit(struct net *net)
  1263. {
  1264. remove_proc_entry("arp", net->proc_net);
  1265. }
  1266. static struct pernet_operations arp_net_ops = {
  1267. .init = arp_net_init,
  1268. .exit = arp_net_exit,
  1269. };
  1270. static int __init arp_proc_init(void)
  1271. {
  1272. return register_pernet_subsys(&arp_net_ops);
  1273. }
  1274. #else /* CONFIG_PROC_FS */
  1275. static int __init arp_proc_init(void)
  1276. {
  1277. return 0;
  1278. }
  1279. #endif /* CONFIG_PROC_FS */