arp.c 34 KB

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