arp.c 33 KB

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