arp.c 35 KB

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