arp.c 34 KB

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