arp.c 36 KB

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