arp.c 36 KB

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