arp.c 35 KB

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