arp.c 34 KB

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