arp.c 35 KB

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