arp.c 36 KB

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