arp.c 35 KB

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