arp.c 36 KB

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