arp.c 35 KB

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