arp.c 34 KB

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