arp.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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. struct neighbour *__arp_bind_neighbour(struct dst_entry *dst, __be32 nexthop)
  476. {
  477. struct net_device *dev = dst->dev;
  478. if (dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
  479. nexthop = 0;
  480. return __neigh_lookup_errno(
  481. #if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE)
  482. dev->type == ARPHRD_ATM ?
  483. clip_tbl_hook :
  484. #endif
  485. &arp_tbl, &nexthop, dev);
  486. }
  487. int arp_bind_neighbour(struct dst_entry *dst)
  488. {
  489. struct net_device *dev = dst->dev;
  490. struct neighbour *n = dst_get_neighbour(dst);
  491. if (dev == NULL)
  492. return -EINVAL;
  493. if (n == NULL) {
  494. n = __arp_bind_neighbour(dst, ((struct rtable *)dst)->rt_gateway);
  495. if (IS_ERR(n))
  496. return PTR_ERR(n);
  497. dst_set_neighbour(dst, n);
  498. }
  499. return 0;
  500. }
  501. /*
  502. * Check if we can use proxy ARP for this path
  503. */
  504. static inline int arp_fwd_proxy(struct in_device *in_dev,
  505. struct net_device *dev, struct rtable *rt)
  506. {
  507. struct in_device *out_dev;
  508. int imi, omi = -1;
  509. if (rt->dst.dev == dev)
  510. return 0;
  511. if (!IN_DEV_PROXY_ARP(in_dev))
  512. return 0;
  513. imi = IN_DEV_MEDIUM_ID(in_dev);
  514. if (imi == 0)
  515. return 1;
  516. if (imi == -1)
  517. return 0;
  518. /* place to check for proxy_arp for routes */
  519. out_dev = __in_dev_get_rcu(rt->dst.dev);
  520. if (out_dev)
  521. omi = IN_DEV_MEDIUM_ID(out_dev);
  522. return omi != imi && omi != -1;
  523. }
  524. /*
  525. * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev)
  526. *
  527. * RFC3069 supports proxy arp replies back to the same interface. This
  528. * is done to support (ethernet) switch features, like RFC 3069, where
  529. * the individual ports are not allowed to communicate with each
  530. * other, BUT they are allowed to talk to the upstream router. As
  531. * described in RFC 3069, it is possible to allow these hosts to
  532. * communicate through the upstream router, by proxy_arp'ing.
  533. *
  534. * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation"
  535. *
  536. * This technology is known by different names:
  537. * In RFC 3069 it is called VLAN Aggregation.
  538. * Cisco and Allied Telesyn call it Private VLAN.
  539. * Hewlett-Packard call it Source-Port filtering or port-isolation.
  540. * Ericsson call it MAC-Forced Forwarding (RFC Draft).
  541. *
  542. */
  543. static inline int arp_fwd_pvlan(struct in_device *in_dev,
  544. struct net_device *dev, struct rtable *rt,
  545. __be32 sip, __be32 tip)
  546. {
  547. /* Private VLAN is only concerned about the same ethernet segment */
  548. if (rt->dst.dev != dev)
  549. return 0;
  550. /* Don't reply on self probes (often done by windowz boxes)*/
  551. if (sip == tip)
  552. return 0;
  553. if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
  554. return 1;
  555. else
  556. return 0;
  557. }
  558. /*
  559. * Interface to link layer: send routine and receive handler.
  560. */
  561. /*
  562. * Create an arp packet. If (dest_hw == NULL), we create a broadcast
  563. * message.
  564. */
  565. struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
  566. struct net_device *dev, __be32 src_ip,
  567. const unsigned char *dest_hw,
  568. const unsigned char *src_hw,
  569. const unsigned char *target_hw)
  570. {
  571. struct sk_buff *skb;
  572. struct arphdr *arp;
  573. unsigned char *arp_ptr;
  574. /*
  575. * Allocate a buffer
  576. */
  577. skb = alloc_skb(arp_hdr_len(dev) + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
  578. if (skb == NULL)
  579. return NULL;
  580. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  581. skb_reset_network_header(skb);
  582. arp = (struct arphdr *) skb_put(skb, arp_hdr_len(dev));
  583. skb->dev = dev;
  584. skb->protocol = htons(ETH_P_ARP);
  585. if (src_hw == NULL)
  586. src_hw = dev->dev_addr;
  587. if (dest_hw == NULL)
  588. dest_hw = dev->broadcast;
  589. /*
  590. * Fill the device header for the ARP frame
  591. */
  592. if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
  593. goto out;
  594. /*
  595. * Fill out the arp protocol part.
  596. *
  597. * The arp hardware type should match the device type, except for FDDI,
  598. * which (according to RFC 1390) should always equal 1 (Ethernet).
  599. */
  600. /*
  601. * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
  602. * DIX code for the protocol. Make these device structure fields.
  603. */
  604. switch (dev->type) {
  605. default:
  606. arp->ar_hrd = htons(dev->type);
  607. arp->ar_pro = htons(ETH_P_IP);
  608. break;
  609. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  610. case ARPHRD_AX25:
  611. arp->ar_hrd = htons(ARPHRD_AX25);
  612. arp->ar_pro = htons(AX25_P_IP);
  613. break;
  614. #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
  615. case ARPHRD_NETROM:
  616. arp->ar_hrd = htons(ARPHRD_NETROM);
  617. arp->ar_pro = htons(AX25_P_IP);
  618. break;
  619. #endif
  620. #endif
  621. #if defined(CONFIG_FDDI) || defined(CONFIG_FDDI_MODULE)
  622. case ARPHRD_FDDI:
  623. arp->ar_hrd = htons(ARPHRD_ETHER);
  624. arp->ar_pro = htons(ETH_P_IP);
  625. break;
  626. #endif
  627. #if defined(CONFIG_TR) || defined(CONFIG_TR_MODULE)
  628. case ARPHRD_IEEE802_TR:
  629. arp->ar_hrd = htons(ARPHRD_IEEE802);
  630. arp->ar_pro = htons(ETH_P_IP);
  631. break;
  632. #endif
  633. }
  634. arp->ar_hln = dev->addr_len;
  635. arp->ar_pln = 4;
  636. arp->ar_op = htons(type);
  637. arp_ptr = (unsigned char *)(arp + 1);
  638. memcpy(arp_ptr, src_hw, dev->addr_len);
  639. arp_ptr += dev->addr_len;
  640. memcpy(arp_ptr, &src_ip, 4);
  641. arp_ptr += 4;
  642. if (target_hw != NULL)
  643. memcpy(arp_ptr, target_hw, dev->addr_len);
  644. else
  645. memset(arp_ptr, 0, dev->addr_len);
  646. arp_ptr += dev->addr_len;
  647. memcpy(arp_ptr, &dest_ip, 4);
  648. return skb;
  649. out:
  650. kfree_skb(skb);
  651. return NULL;
  652. }
  653. EXPORT_SYMBOL(arp_create);
  654. /*
  655. * Send an arp packet.
  656. */
  657. void arp_xmit(struct sk_buff *skb)
  658. {
  659. /* Send it off, maybe filter it using firewalling first. */
  660. NF_HOOK(NFPROTO_ARP, NF_ARP_OUT, skb, NULL, skb->dev, dev_queue_xmit);
  661. }
  662. EXPORT_SYMBOL(arp_xmit);
  663. /*
  664. * Create and send an arp packet.
  665. */
  666. void arp_send(int type, int ptype, __be32 dest_ip,
  667. struct net_device *dev, __be32 src_ip,
  668. const unsigned char *dest_hw, const unsigned char *src_hw,
  669. const unsigned char *target_hw)
  670. {
  671. struct sk_buff *skb;
  672. /*
  673. * No arp on this interface.
  674. */
  675. if (dev->flags&IFF_NOARP)
  676. return;
  677. skb = arp_create(type, ptype, dest_ip, dev, src_ip,
  678. dest_hw, src_hw, target_hw);
  679. if (skb == NULL)
  680. return;
  681. arp_xmit(skb);
  682. }
  683. EXPORT_SYMBOL(arp_send);
  684. /*
  685. * Process an arp request.
  686. */
  687. static int arp_process(struct sk_buff *skb)
  688. {
  689. struct net_device *dev = skb->dev;
  690. struct in_device *in_dev = __in_dev_get_rcu(dev);
  691. struct arphdr *arp;
  692. unsigned char *arp_ptr;
  693. struct rtable *rt;
  694. unsigned char *sha;
  695. __be32 sip, tip;
  696. u16 dev_type = dev->type;
  697. int addr_type;
  698. struct neighbour *n;
  699. struct net *net = dev_net(dev);
  700. /* arp_rcv below verifies the ARP header and verifies the device
  701. * is ARP'able.
  702. */
  703. if (in_dev == NULL)
  704. goto out;
  705. arp = arp_hdr(skb);
  706. switch (dev_type) {
  707. default:
  708. if (arp->ar_pro != htons(ETH_P_IP) ||
  709. htons(dev_type) != arp->ar_hrd)
  710. goto out;
  711. break;
  712. case ARPHRD_ETHER:
  713. case ARPHRD_IEEE802_TR:
  714. case ARPHRD_FDDI:
  715. case ARPHRD_IEEE802:
  716. /*
  717. * ETHERNET, Token Ring and Fibre Channel (which are IEEE 802
  718. * devices, according to RFC 2625) devices will accept ARP
  719. * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
  720. * This is the case also of FDDI, where the RFC 1390 says that
  721. * FDDI devices should accept ARP hardware of (1) Ethernet,
  722. * however, to be more robust, we'll accept both 1 (Ethernet)
  723. * or 6 (IEEE 802.2)
  724. */
  725. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  726. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  727. arp->ar_pro != htons(ETH_P_IP))
  728. goto out;
  729. break;
  730. case ARPHRD_AX25:
  731. if (arp->ar_pro != htons(AX25_P_IP) ||
  732. arp->ar_hrd != htons(ARPHRD_AX25))
  733. goto out;
  734. break;
  735. case ARPHRD_NETROM:
  736. if (arp->ar_pro != htons(AX25_P_IP) ||
  737. arp->ar_hrd != htons(ARPHRD_NETROM))
  738. goto out;
  739. break;
  740. }
  741. /* Understand only these message types */
  742. if (arp->ar_op != htons(ARPOP_REPLY) &&
  743. arp->ar_op != htons(ARPOP_REQUEST))
  744. goto out;
  745. /*
  746. * Extract fields
  747. */
  748. arp_ptr = (unsigned char *)(arp + 1);
  749. sha = arp_ptr;
  750. arp_ptr += dev->addr_len;
  751. memcpy(&sip, arp_ptr, 4);
  752. arp_ptr += 4;
  753. arp_ptr += dev->addr_len;
  754. memcpy(&tip, arp_ptr, 4);
  755. /*
  756. * Check for bad requests for 127.x.x.x and requests for multicast
  757. * addresses. If this is one such, delete it.
  758. */
  759. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  760. goto out;
  761. /*
  762. * Special case: We must set Frame Relay source Q.922 address
  763. */
  764. if (dev_type == ARPHRD_DLCI)
  765. sha = dev->broadcast;
  766. /*
  767. * Process entry. The idea here is we want to send a reply if it is a
  768. * request for us or if it is a request for someone else that we hold
  769. * a proxy for. We want to add an entry to our cache if it is a reply
  770. * to us or if it is a request for our address.
  771. * (The assumption for this last is that if someone is requesting our
  772. * address, they are probably intending to talk to us, so it saves time
  773. * if we cache their address. Their address is also probably not in
  774. * our cache, since ours is not in their cache.)
  775. *
  776. * Putting this another way, we only care about replies if they are to
  777. * us, in which case we add them to the cache. For requests, we care
  778. * about those for us and those for our proxies. We reply to both,
  779. * and in the case of requests for us we add the requester to the arp
  780. * cache.
  781. */
  782. /* Special case: IPv4 duplicate address detection packet (RFC2131) */
  783. if (sip == 0) {
  784. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  785. inet_addr_type(net, tip) == RTN_LOCAL &&
  786. !arp_ignore(in_dev, sip, tip))
  787. arp_send(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  788. dev->dev_addr, sha);
  789. goto out;
  790. }
  791. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  792. ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
  793. rt = skb_rtable(skb);
  794. addr_type = rt->rt_type;
  795. if (addr_type == RTN_LOCAL) {
  796. int dont_send;
  797. dont_send = arp_ignore(in_dev, sip, tip);
  798. if (!dont_send && IN_DEV_ARPFILTER(in_dev))
  799. dont_send = arp_filter(sip, tip, dev);
  800. if (!dont_send) {
  801. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  802. if (n) {
  803. arp_send(ARPOP_REPLY, ETH_P_ARP, sip,
  804. dev, tip, sha, dev->dev_addr,
  805. sha);
  806. neigh_release(n);
  807. }
  808. }
  809. goto out;
  810. } else if (IN_DEV_FORWARD(in_dev)) {
  811. if (addr_type == RTN_UNICAST &&
  812. (arp_fwd_proxy(in_dev, dev, rt) ||
  813. arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
  814. (rt->dst.dev != dev &&
  815. pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))) {
  816. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  817. if (n)
  818. neigh_release(n);
  819. if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
  820. skb->pkt_type == PACKET_HOST ||
  821. in_dev->arp_parms->proxy_delay == 0) {
  822. arp_send(ARPOP_REPLY, ETH_P_ARP, sip,
  823. dev, tip, sha, dev->dev_addr,
  824. sha);
  825. } else {
  826. pneigh_enqueue(&arp_tbl,
  827. in_dev->arp_parms, skb);
  828. return 0;
  829. }
  830. goto out;
  831. }
  832. }
  833. }
  834. /* Update our ARP tables */
  835. n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
  836. if (IPV4_DEVCONF_ALL(dev_net(dev), ARP_ACCEPT)) {
  837. /* Unsolicited ARP is not accepted by default.
  838. It is possible, that this option should be enabled for some
  839. devices (strip is candidate)
  840. */
  841. if (n == NULL &&
  842. (arp->ar_op == htons(ARPOP_REPLY) ||
  843. (arp->ar_op == htons(ARPOP_REQUEST) && tip == sip)) &&
  844. inet_addr_type(net, sip) == RTN_UNICAST)
  845. n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
  846. }
  847. if (n) {
  848. int state = NUD_REACHABLE;
  849. int override;
  850. /* If several different ARP replies follows back-to-back,
  851. use the FIRST one. It is possible, if several proxy
  852. agents are active. Taking the first reply prevents
  853. arp trashing and chooses the fastest router.
  854. */
  855. override = time_after(jiffies, n->updated + n->parms->locktime);
  856. /* Broadcast replies and request packets
  857. do not assert neighbour reachability.
  858. */
  859. if (arp->ar_op != htons(ARPOP_REPLY) ||
  860. skb->pkt_type != PACKET_HOST)
  861. state = NUD_STALE;
  862. neigh_update(n, sha, state,
  863. override ? NEIGH_UPDATE_F_OVERRIDE : 0);
  864. neigh_release(n);
  865. }
  866. out:
  867. consume_skb(skb);
  868. return 0;
  869. }
  870. static void parp_redo(struct sk_buff *skb)
  871. {
  872. arp_process(skb);
  873. }
  874. /*
  875. * Receive an arp request from the device layer.
  876. */
  877. static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
  878. struct packet_type *pt, struct net_device *orig_dev)
  879. {
  880. struct arphdr *arp;
  881. /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
  882. if (!pskb_may_pull(skb, arp_hdr_len(dev)))
  883. goto freeskb;
  884. arp = arp_hdr(skb);
  885. if (arp->ar_hln != dev->addr_len ||
  886. dev->flags & IFF_NOARP ||
  887. skb->pkt_type == PACKET_OTHERHOST ||
  888. skb->pkt_type == PACKET_LOOPBACK ||
  889. arp->ar_pln != 4)
  890. goto freeskb;
  891. skb = skb_share_check(skb, GFP_ATOMIC);
  892. if (skb == NULL)
  893. goto out_of_mem;
  894. memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
  895. return NF_HOOK(NFPROTO_ARP, NF_ARP_IN, skb, dev, NULL, arp_process);
  896. freeskb:
  897. kfree_skb(skb);
  898. out_of_mem:
  899. return 0;
  900. }
  901. /*
  902. * User level interface (ioctl)
  903. */
  904. /*
  905. * Set (create) an ARP cache entry.
  906. */
  907. static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
  908. {
  909. if (dev == NULL) {
  910. IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
  911. return 0;
  912. }
  913. if (__in_dev_get_rtnl(dev)) {
  914. IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
  915. return 0;
  916. }
  917. return -ENXIO;
  918. }
  919. static int arp_req_set_public(struct net *net, struct arpreq *r,
  920. struct net_device *dev)
  921. {
  922. __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  923. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  924. if (mask && mask != htonl(0xFFFFFFFF))
  925. return -EINVAL;
  926. if (!dev && (r->arp_flags & ATF_COM)) {
  927. dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
  928. r->arp_ha.sa_data);
  929. if (!dev)
  930. return -ENODEV;
  931. }
  932. if (mask) {
  933. if (pneigh_lookup(&arp_tbl, net, &ip, dev, 1) == NULL)
  934. return -ENOBUFS;
  935. return 0;
  936. }
  937. return arp_req_set_proxy(net, dev, 1);
  938. }
  939. static int arp_req_set(struct net *net, struct arpreq *r,
  940. struct net_device *dev)
  941. {
  942. __be32 ip;
  943. struct neighbour *neigh;
  944. int err;
  945. if (r->arp_flags & ATF_PUBL)
  946. return arp_req_set_public(net, r, dev);
  947. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  948. if (r->arp_flags & ATF_PERM)
  949. r->arp_flags |= ATF_COM;
  950. if (dev == NULL) {
  951. struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
  952. if (IS_ERR(rt))
  953. return PTR_ERR(rt);
  954. dev = rt->dst.dev;
  955. ip_rt_put(rt);
  956. if (!dev)
  957. return -EINVAL;
  958. }
  959. switch (dev->type) {
  960. #if defined(CONFIG_FDDI) || defined(CONFIG_FDDI_MODULE)
  961. case ARPHRD_FDDI:
  962. /*
  963. * According to RFC 1390, FDDI devices should accept ARP
  964. * hardware types of 1 (Ethernet). However, to be more
  965. * robust, we'll accept hardware types of either 1 (Ethernet)
  966. * or 6 (IEEE 802.2).
  967. */
  968. if (r->arp_ha.sa_family != ARPHRD_FDDI &&
  969. r->arp_ha.sa_family != ARPHRD_ETHER &&
  970. r->arp_ha.sa_family != ARPHRD_IEEE802)
  971. return -EINVAL;
  972. break;
  973. #endif
  974. default:
  975. if (r->arp_ha.sa_family != dev->type)
  976. return -EINVAL;
  977. break;
  978. }
  979. neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
  980. err = PTR_ERR(neigh);
  981. if (!IS_ERR(neigh)) {
  982. unsigned state = NUD_STALE;
  983. if (r->arp_flags & ATF_PERM)
  984. state = NUD_PERMANENT;
  985. err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
  986. r->arp_ha.sa_data : NULL, state,
  987. NEIGH_UPDATE_F_OVERRIDE |
  988. NEIGH_UPDATE_F_ADMIN);
  989. neigh_release(neigh);
  990. }
  991. return err;
  992. }
  993. static unsigned arp_state_to_flags(struct neighbour *neigh)
  994. {
  995. if (neigh->nud_state&NUD_PERMANENT)
  996. return ATF_PERM | ATF_COM;
  997. else if (neigh->nud_state&NUD_VALID)
  998. return ATF_COM;
  999. else
  1000. return 0;
  1001. }
  1002. /*
  1003. * Get an ARP cache entry.
  1004. */
  1005. static int arp_req_get(struct arpreq *r, struct net_device *dev)
  1006. {
  1007. __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
  1008. struct neighbour *neigh;
  1009. int err = -ENXIO;
  1010. neigh = neigh_lookup(&arp_tbl, &ip, dev);
  1011. if (neigh) {
  1012. read_lock_bh(&neigh->lock);
  1013. memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
  1014. r->arp_flags = arp_state_to_flags(neigh);
  1015. read_unlock_bh(&neigh->lock);
  1016. r->arp_ha.sa_family = dev->type;
  1017. strlcpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
  1018. neigh_release(neigh);
  1019. err = 0;
  1020. }
  1021. return err;
  1022. }
  1023. int arp_invalidate(struct net_device *dev, __be32 ip)
  1024. {
  1025. struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
  1026. int err = -ENXIO;
  1027. if (neigh) {
  1028. if (neigh->nud_state & ~NUD_NOARP)
  1029. err = neigh_update(neigh, NULL, NUD_FAILED,
  1030. NEIGH_UPDATE_F_OVERRIDE|
  1031. NEIGH_UPDATE_F_ADMIN);
  1032. neigh_release(neigh);
  1033. }
  1034. return err;
  1035. }
  1036. EXPORT_SYMBOL(arp_invalidate);
  1037. static int arp_req_delete_public(struct net *net, struct arpreq *r,
  1038. struct net_device *dev)
  1039. {
  1040. __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
  1041. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  1042. if (mask == htonl(0xFFFFFFFF))
  1043. return pneigh_delete(&arp_tbl, net, &ip, dev);
  1044. if (mask)
  1045. return -EINVAL;
  1046. return arp_req_set_proxy(net, dev, 0);
  1047. }
  1048. static int arp_req_delete(struct net *net, struct arpreq *r,
  1049. struct net_device *dev)
  1050. {
  1051. __be32 ip;
  1052. if (r->arp_flags & ATF_PUBL)
  1053. return arp_req_delete_public(net, r, dev);
  1054. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  1055. if (dev == NULL) {
  1056. struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
  1057. if (IS_ERR(rt))
  1058. return PTR_ERR(rt);
  1059. dev = rt->dst.dev;
  1060. ip_rt_put(rt);
  1061. if (!dev)
  1062. return -EINVAL;
  1063. }
  1064. return arp_invalidate(dev, ip);
  1065. }
  1066. /*
  1067. * Handle an ARP layer I/O control request.
  1068. */
  1069. int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  1070. {
  1071. int err;
  1072. struct arpreq r;
  1073. struct net_device *dev = NULL;
  1074. switch (cmd) {
  1075. case SIOCDARP:
  1076. case SIOCSARP:
  1077. if (!capable(CAP_NET_ADMIN))
  1078. return -EPERM;
  1079. case SIOCGARP:
  1080. err = copy_from_user(&r, arg, sizeof(struct arpreq));
  1081. if (err)
  1082. return -EFAULT;
  1083. break;
  1084. default:
  1085. return -EINVAL;
  1086. }
  1087. if (r.arp_pa.sa_family != AF_INET)
  1088. return -EPFNOSUPPORT;
  1089. if (!(r.arp_flags & ATF_PUBL) &&
  1090. (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
  1091. return -EINVAL;
  1092. if (!(r.arp_flags & ATF_NETMASK))
  1093. ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr =
  1094. htonl(0xFFFFFFFFUL);
  1095. rtnl_lock();
  1096. if (r.arp_dev[0]) {
  1097. err = -ENODEV;
  1098. dev = __dev_get_by_name(net, r.arp_dev);
  1099. if (dev == NULL)
  1100. goto out;
  1101. /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
  1102. if (!r.arp_ha.sa_family)
  1103. r.arp_ha.sa_family = dev->type;
  1104. err = -EINVAL;
  1105. if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
  1106. goto out;
  1107. } else if (cmd == SIOCGARP) {
  1108. err = -ENODEV;
  1109. goto out;
  1110. }
  1111. switch (cmd) {
  1112. case SIOCDARP:
  1113. err = arp_req_delete(net, &r, dev);
  1114. break;
  1115. case SIOCSARP:
  1116. err = arp_req_set(net, &r, dev);
  1117. break;
  1118. case SIOCGARP:
  1119. err = arp_req_get(&r, dev);
  1120. break;
  1121. }
  1122. out:
  1123. rtnl_unlock();
  1124. if (cmd == SIOCGARP && !err && copy_to_user(arg, &r, sizeof(r)))
  1125. err = -EFAULT;
  1126. return err;
  1127. }
  1128. static int arp_netdev_event(struct notifier_block *this, unsigned long event,
  1129. void *ptr)
  1130. {
  1131. struct net_device *dev = ptr;
  1132. switch (event) {
  1133. case NETDEV_CHANGEADDR:
  1134. neigh_changeaddr(&arp_tbl, dev);
  1135. rt_cache_flush(dev_net(dev), 0);
  1136. break;
  1137. default:
  1138. break;
  1139. }
  1140. return NOTIFY_DONE;
  1141. }
  1142. static struct notifier_block arp_netdev_notifier = {
  1143. .notifier_call = arp_netdev_event,
  1144. };
  1145. /* Note, that it is not on notifier chain.
  1146. It is necessary, that this routine was called after route cache will be
  1147. flushed.
  1148. */
  1149. void arp_ifdown(struct net_device *dev)
  1150. {
  1151. neigh_ifdown(&arp_tbl, dev);
  1152. }
  1153. /*
  1154. * Called once on startup.
  1155. */
  1156. static struct packet_type arp_packet_type __read_mostly = {
  1157. .type = cpu_to_be16(ETH_P_ARP),
  1158. .func = arp_rcv,
  1159. };
  1160. static int arp_proc_init(void);
  1161. void __init arp_init(void)
  1162. {
  1163. neigh_table_init(&arp_tbl);
  1164. dev_add_pack(&arp_packet_type);
  1165. arp_proc_init();
  1166. #ifdef CONFIG_SYSCTL
  1167. neigh_sysctl_register(NULL, &arp_tbl.parms, "ipv4", NULL);
  1168. #endif
  1169. register_netdevice_notifier(&arp_netdev_notifier);
  1170. }
  1171. #ifdef CONFIG_PROC_FS
  1172. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  1173. /* ------------------------------------------------------------------------ */
  1174. /*
  1175. * ax25 -> ASCII conversion
  1176. */
  1177. static char *ax2asc2(ax25_address *a, char *buf)
  1178. {
  1179. char c, *s;
  1180. int n;
  1181. for (n = 0, s = buf; n < 6; n++) {
  1182. c = (a->ax25_call[n] >> 1) & 0x7F;
  1183. if (c != ' ')
  1184. *s++ = c;
  1185. }
  1186. *s++ = '-';
  1187. n = (a->ax25_call[6] >> 1) & 0x0F;
  1188. if (n > 9) {
  1189. *s++ = '1';
  1190. n -= 10;
  1191. }
  1192. *s++ = n + '0';
  1193. *s++ = '\0';
  1194. if (*buf == '\0' || *buf == '-')
  1195. return "*";
  1196. return buf;
  1197. }
  1198. #endif /* CONFIG_AX25 */
  1199. #define HBUFFERLEN 30
  1200. static void arp_format_neigh_entry(struct seq_file *seq,
  1201. struct neighbour *n)
  1202. {
  1203. char hbuffer[HBUFFERLEN];
  1204. int k, j;
  1205. char tbuf[16];
  1206. struct net_device *dev = n->dev;
  1207. int hatype = dev->type;
  1208. read_lock(&n->lock);
  1209. /* Convert hardware address to XX:XX:XX:XX ... form. */
  1210. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  1211. if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
  1212. ax2asc2((ax25_address *)n->ha, hbuffer);
  1213. else {
  1214. #endif
  1215. for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
  1216. hbuffer[k++] = hex_asc_hi(n->ha[j]);
  1217. hbuffer[k++] = hex_asc_lo(n->ha[j]);
  1218. hbuffer[k++] = ':';
  1219. }
  1220. if (k != 0)
  1221. --k;
  1222. hbuffer[k] = 0;
  1223. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  1224. }
  1225. #endif
  1226. sprintf(tbuf, "%pI4", n->primary_key);
  1227. seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
  1228. tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
  1229. read_unlock(&n->lock);
  1230. }
  1231. static void arp_format_pneigh_entry(struct seq_file *seq,
  1232. struct pneigh_entry *n)
  1233. {
  1234. struct net_device *dev = n->dev;
  1235. int hatype = dev ? dev->type : 0;
  1236. char tbuf[16];
  1237. sprintf(tbuf, "%pI4", n->key);
  1238. seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
  1239. tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
  1240. dev ? dev->name : "*");
  1241. }
  1242. static int arp_seq_show(struct seq_file *seq, void *v)
  1243. {
  1244. if (v == SEQ_START_TOKEN) {
  1245. seq_puts(seq, "IP address HW type Flags "
  1246. "HW address Mask Device\n");
  1247. } else {
  1248. struct neigh_seq_state *state = seq->private;
  1249. if (state->flags & NEIGH_SEQ_IS_PNEIGH)
  1250. arp_format_pneigh_entry(seq, v);
  1251. else
  1252. arp_format_neigh_entry(seq, v);
  1253. }
  1254. return 0;
  1255. }
  1256. static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
  1257. {
  1258. /* Don't want to confuse "arp -a" w/ magic entries,
  1259. * so we tell the generic iterator to skip NUD_NOARP.
  1260. */
  1261. return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
  1262. }
  1263. /* ------------------------------------------------------------------------ */
  1264. static const struct seq_operations arp_seq_ops = {
  1265. .start = arp_seq_start,
  1266. .next = neigh_seq_next,
  1267. .stop = neigh_seq_stop,
  1268. .show = arp_seq_show,
  1269. };
  1270. static int arp_seq_open(struct inode *inode, struct file *file)
  1271. {
  1272. return seq_open_net(inode, file, &arp_seq_ops,
  1273. sizeof(struct neigh_seq_state));
  1274. }
  1275. static const struct file_operations arp_seq_fops = {
  1276. .owner = THIS_MODULE,
  1277. .open = arp_seq_open,
  1278. .read = seq_read,
  1279. .llseek = seq_lseek,
  1280. .release = seq_release_net,
  1281. };
  1282. static int __net_init arp_net_init(struct net *net)
  1283. {
  1284. if (!proc_net_fops_create(net, "arp", S_IRUGO, &arp_seq_fops))
  1285. return -ENOMEM;
  1286. return 0;
  1287. }
  1288. static void __net_exit arp_net_exit(struct net *net)
  1289. {
  1290. proc_net_remove(net, "arp");
  1291. }
  1292. static struct pernet_operations arp_net_ops = {
  1293. .init = arp_net_init,
  1294. .exit = arp_net_exit,
  1295. };
  1296. static int __init arp_proc_init(void)
  1297. {
  1298. return register_pernet_subsys(&arp_net_ops);
  1299. }
  1300. #else /* CONFIG_PROC_FS */
  1301. static int __init arp_proc_init(void)
  1302. {
  1303. return 0;
  1304. }
  1305. #endif /* CONFIG_PROC_FS */