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