arp.c 35 KB

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