arp.c 34 KB

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