arp.c 34 KB

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