arp.c 35 KB

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