arp.c 36 KB

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