arp.c 34 KB

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