arp.c 36 KB

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