arp.c 35 KB

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