arp.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. RT_SCOPE_UNIVERSE);
  419. if (IS_ERR(rt))
  420. return 1;
  421. if (rt->dst.dev != dev) {
  422. __NET_INC_STATS(net, LINUX_MIB_ARPFILTER);
  423. flag = 1;
  424. }
  425. ip_rt_put(rt);
  426. return flag;
  427. }
  428. /*
  429. * Check if we can use proxy ARP for this path
  430. */
  431. static inline int arp_fwd_proxy(struct in_device *in_dev,
  432. struct net_device *dev, struct rtable *rt)
  433. {
  434. struct in_device *out_dev;
  435. int imi, omi = -1;
  436. if (rt->dst.dev == dev)
  437. return 0;
  438. if (!IN_DEV_PROXY_ARP(in_dev))
  439. return 0;
  440. imi = IN_DEV_MEDIUM_ID(in_dev);
  441. if (imi == 0)
  442. return 1;
  443. if (imi == -1)
  444. return 0;
  445. /* place to check for proxy_arp for routes */
  446. out_dev = __in_dev_get_rcu(rt->dst.dev);
  447. if (out_dev)
  448. omi = IN_DEV_MEDIUM_ID(out_dev);
  449. return omi != imi && omi != -1;
  450. }
  451. /*
  452. * Check for RFC3069 proxy arp private VLAN (allow to send back to same dev)
  453. *
  454. * RFC3069 supports proxy arp replies back to the same interface. This
  455. * is done to support (ethernet) switch features, like RFC 3069, where
  456. * the individual ports are not allowed to communicate with each
  457. * other, BUT they are allowed to talk to the upstream router. As
  458. * described in RFC 3069, it is possible to allow these hosts to
  459. * communicate through the upstream router, by proxy_arp'ing.
  460. *
  461. * RFC 3069: "VLAN Aggregation for Efficient IP Address Allocation"
  462. *
  463. * This technology is known by different names:
  464. * In RFC 3069 it is called VLAN Aggregation.
  465. * Cisco and Allied Telesyn call it Private VLAN.
  466. * Hewlett-Packard call it Source-Port filtering or port-isolation.
  467. * Ericsson call it MAC-Forced Forwarding (RFC Draft).
  468. *
  469. */
  470. static inline int arp_fwd_pvlan(struct in_device *in_dev,
  471. struct net_device *dev, struct rtable *rt,
  472. __be32 sip, __be32 tip)
  473. {
  474. /* Private VLAN is only concerned about the same ethernet segment */
  475. if (rt->dst.dev != dev)
  476. return 0;
  477. /* Don't reply on self probes (often done by windowz boxes)*/
  478. if (sip == tip)
  479. return 0;
  480. if (IN_DEV_PROXY_ARP_PVLAN(in_dev))
  481. return 1;
  482. else
  483. return 0;
  484. }
  485. /*
  486. * Interface to link layer: send routine and receive handler.
  487. */
  488. /*
  489. * Create an arp packet. If dest_hw is not set, we create a broadcast
  490. * message.
  491. */
  492. struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
  493. struct net_device *dev, __be32 src_ip,
  494. const unsigned char *dest_hw,
  495. const unsigned char *src_hw,
  496. const unsigned char *target_hw)
  497. {
  498. struct sk_buff *skb;
  499. struct arphdr *arp;
  500. unsigned char *arp_ptr;
  501. int hlen = LL_RESERVED_SPACE(dev);
  502. int tlen = dev->needed_tailroom;
  503. /*
  504. * Allocate a buffer
  505. */
  506. skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC);
  507. if (!skb)
  508. return NULL;
  509. skb_reserve(skb, hlen);
  510. skb_reset_network_header(skb);
  511. arp = skb_put(skb, arp_hdr_len(dev));
  512. skb->dev = dev;
  513. skb->protocol = htons(ETH_P_ARP);
  514. if (!src_hw)
  515. src_hw = dev->dev_addr;
  516. if (!dest_hw)
  517. dest_hw = dev->broadcast;
  518. /*
  519. * Fill the device header for the ARP frame
  520. */
  521. if (dev_hard_header(skb, dev, ptype, dest_hw, src_hw, skb->len) < 0)
  522. goto out;
  523. /*
  524. * Fill out the arp protocol part.
  525. *
  526. * The arp hardware type should match the device type, except for FDDI,
  527. * which (according to RFC 1390) should always equal 1 (Ethernet).
  528. */
  529. /*
  530. * Exceptions everywhere. AX.25 uses the AX.25 PID value not the
  531. * DIX code for the protocol. Make these device structure fields.
  532. */
  533. switch (dev->type) {
  534. default:
  535. arp->ar_hrd = htons(dev->type);
  536. arp->ar_pro = htons(ETH_P_IP);
  537. break;
  538. #if IS_ENABLED(CONFIG_AX25)
  539. case ARPHRD_AX25:
  540. arp->ar_hrd = htons(ARPHRD_AX25);
  541. arp->ar_pro = htons(AX25_P_IP);
  542. break;
  543. #if IS_ENABLED(CONFIG_NETROM)
  544. case ARPHRD_NETROM:
  545. arp->ar_hrd = htons(ARPHRD_NETROM);
  546. arp->ar_pro = htons(AX25_P_IP);
  547. break;
  548. #endif
  549. #endif
  550. #if IS_ENABLED(CONFIG_FDDI)
  551. case ARPHRD_FDDI:
  552. arp->ar_hrd = htons(ARPHRD_ETHER);
  553. arp->ar_pro = htons(ETH_P_IP);
  554. break;
  555. #endif
  556. }
  557. arp->ar_hln = dev->addr_len;
  558. arp->ar_pln = 4;
  559. arp->ar_op = htons(type);
  560. arp_ptr = (unsigned char *)(arp + 1);
  561. memcpy(arp_ptr, src_hw, dev->addr_len);
  562. arp_ptr += dev->addr_len;
  563. memcpy(arp_ptr, &src_ip, 4);
  564. arp_ptr += 4;
  565. switch (dev->type) {
  566. #if IS_ENABLED(CONFIG_FIREWIRE_NET)
  567. case ARPHRD_IEEE1394:
  568. break;
  569. #endif
  570. default:
  571. if (target_hw)
  572. memcpy(arp_ptr, target_hw, dev->addr_len);
  573. else
  574. memset(arp_ptr, 0, dev->addr_len);
  575. arp_ptr += dev->addr_len;
  576. }
  577. memcpy(arp_ptr, &dest_ip, 4);
  578. return skb;
  579. out:
  580. kfree_skb(skb);
  581. return NULL;
  582. }
  583. EXPORT_SYMBOL(arp_create);
  584. static int arp_xmit_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  585. {
  586. return dev_queue_xmit(skb);
  587. }
  588. /*
  589. * Send an arp packet.
  590. */
  591. void arp_xmit(struct sk_buff *skb)
  592. {
  593. rcu_read_lock();
  594. /* Send it off, maybe filter it using firewalling first. */
  595. NF_HOOK(NFPROTO_ARP, NF_ARP_OUT,
  596. dev_net_rcu(skb->dev), NULL, skb, NULL, skb->dev,
  597. arp_xmit_finish);
  598. rcu_read_unlock();
  599. }
  600. EXPORT_SYMBOL(arp_xmit);
  601. static bool arp_is_garp(struct net *net, struct net_device *dev,
  602. int *addr_type, __be16 ar_op,
  603. __be32 sip, __be32 tip,
  604. unsigned char *sha, unsigned char *tha)
  605. {
  606. bool is_garp = tip == sip;
  607. /* Gratuitous ARP _replies_ also require target hwaddr to be
  608. * the same as source.
  609. */
  610. if (is_garp && ar_op == htons(ARPOP_REPLY))
  611. is_garp =
  612. /* IPv4 over IEEE 1394 doesn't provide target
  613. * hardware address field in its ARP payload.
  614. */
  615. tha &&
  616. !memcmp(tha, sha, dev->addr_len);
  617. if (is_garp) {
  618. *addr_type = inet_addr_type_dev_table(net, dev, sip);
  619. if (*addr_type != RTN_UNICAST)
  620. is_garp = false;
  621. }
  622. return is_garp;
  623. }
  624. /*
  625. * Process an arp request.
  626. */
  627. static int arp_process(struct net *net, struct sock *sk, struct sk_buff *skb)
  628. {
  629. struct net_device *dev = skb->dev;
  630. struct in_device *in_dev = __in_dev_get_rcu(dev);
  631. struct arphdr *arp;
  632. unsigned char *arp_ptr;
  633. struct rtable *rt;
  634. unsigned char *sha;
  635. unsigned char *tha = NULL;
  636. __be32 sip, tip;
  637. u16 dev_type = dev->type;
  638. int addr_type;
  639. struct neighbour *n;
  640. struct dst_entry *reply_dst = NULL;
  641. bool is_garp = false;
  642. /* arp_rcv below verifies the ARP header and verifies the device
  643. * is ARP'able.
  644. */
  645. if (!in_dev)
  646. goto out_free_skb;
  647. arp = arp_hdr(skb);
  648. switch (dev_type) {
  649. default:
  650. if (arp->ar_pro != htons(ETH_P_IP) ||
  651. htons(dev_type) != arp->ar_hrd)
  652. goto out_free_skb;
  653. break;
  654. case ARPHRD_ETHER:
  655. case ARPHRD_FDDI:
  656. case ARPHRD_IEEE802:
  657. /*
  658. * ETHERNET, and Fibre Channel (which are IEEE 802
  659. * devices, according to RFC 2625) devices will accept ARP
  660. * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
  661. * This is the case also of FDDI, where the RFC 1390 says that
  662. * FDDI devices should accept ARP hardware of (1) Ethernet,
  663. * however, to be more robust, we'll accept both 1 (Ethernet)
  664. * or 6 (IEEE 802.2)
  665. */
  666. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  667. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  668. arp->ar_pro != htons(ETH_P_IP))
  669. goto out_free_skb;
  670. break;
  671. case ARPHRD_AX25:
  672. if (arp->ar_pro != htons(AX25_P_IP) ||
  673. arp->ar_hrd != htons(ARPHRD_AX25))
  674. goto out_free_skb;
  675. break;
  676. case ARPHRD_NETROM:
  677. if (arp->ar_pro != htons(AX25_P_IP) ||
  678. arp->ar_hrd != htons(ARPHRD_NETROM))
  679. goto out_free_skb;
  680. break;
  681. }
  682. /* Understand only these message types */
  683. if (arp->ar_op != htons(ARPOP_REPLY) &&
  684. arp->ar_op != htons(ARPOP_REQUEST))
  685. goto out_free_skb;
  686. /*
  687. * Extract fields
  688. */
  689. arp_ptr = (unsigned char *)(arp + 1);
  690. sha = arp_ptr;
  691. arp_ptr += dev->addr_len;
  692. memcpy(&sip, arp_ptr, 4);
  693. arp_ptr += 4;
  694. switch (dev_type) {
  695. #if IS_ENABLED(CONFIG_FIREWIRE_NET)
  696. case ARPHRD_IEEE1394:
  697. break;
  698. #endif
  699. default:
  700. tha = arp_ptr;
  701. arp_ptr += dev->addr_len;
  702. }
  703. memcpy(&tip, arp_ptr, 4);
  704. /*
  705. * Check for bad requests for 127.x.x.x and requests for multicast
  706. * addresses. If this is one such, delete it.
  707. */
  708. if (ipv4_is_multicast(tip) ||
  709. (!IN_DEV_ROUTE_LOCALNET(in_dev) && ipv4_is_loopback(tip)))
  710. goto out_free_skb;
  711. /*
  712. * For some 802.11 wireless deployments (and possibly other networks),
  713. * there will be an ARP proxy and gratuitous ARP frames are attacks
  714. * and thus should not be accepted.
  715. */
  716. if (sip == tip && IN_DEV_ORCONF(in_dev, DROP_GRATUITOUS_ARP))
  717. goto out_free_skb;
  718. /*
  719. * Special case: We must set Frame Relay source Q.922 address
  720. */
  721. if (dev_type == ARPHRD_DLCI)
  722. sha = dev->broadcast;
  723. /*
  724. * Process entry. The idea here is we want to send a reply if it is a
  725. * request for us or if it is a request for someone else that we hold
  726. * a proxy for. We want to add an entry to our cache if it is a reply
  727. * to us or if it is a request for our address.
  728. * (The assumption for this last is that if someone is requesting our
  729. * address, they are probably intending to talk to us, so it saves time
  730. * if we cache their address. Their address is also probably not in
  731. * our cache, since ours is not in their cache.)
  732. *
  733. * Putting this another way, we only care about replies if they are to
  734. * us, in which case we add them to the cache. For requests, we care
  735. * about those for us and those for our proxies. We reply to both,
  736. * and in the case of requests for us we add the requester to the arp
  737. * cache.
  738. */
  739. if (arp->ar_op == htons(ARPOP_REQUEST) && skb_metadata_dst(skb))
  740. reply_dst = (struct dst_entry *)
  741. iptunnel_metadata_reply(skb_metadata_dst(skb),
  742. GFP_ATOMIC);
  743. /* Special case: IPv4 duplicate address detection packet (RFC2131) */
  744. if (sip == 0) {
  745. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  746. inet_addr_type_dev_table(net, dev, tip) == RTN_LOCAL &&
  747. !arp_ignore(in_dev, sip, tip))
  748. arp_send_dst(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip,
  749. sha, dev->dev_addr, sha, reply_dst);
  750. goto out_consume_skb;
  751. }
  752. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  753. ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
  754. rt = skb_rtable(skb);
  755. addr_type = rt->rt_type;
  756. if (addr_type == RTN_LOCAL) {
  757. int dont_send;
  758. dont_send = arp_ignore(in_dev, sip, tip);
  759. if (!dont_send && IN_DEV_ARPFILTER(in_dev))
  760. dont_send = arp_filter(sip, tip, dev);
  761. if (!dont_send) {
  762. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  763. if (n) {
  764. arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
  765. sip, dev, tip, sha,
  766. dev->dev_addr, sha,
  767. reply_dst);
  768. neigh_release(n);
  769. }
  770. }
  771. goto out_consume_skb;
  772. } else if (IN_DEV_FORWARD(in_dev)) {
  773. if (addr_type == RTN_UNICAST &&
  774. (arp_fwd_proxy(in_dev, dev, rt) ||
  775. arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
  776. (rt->dst.dev != dev &&
  777. pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))) {
  778. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  779. if (n)
  780. neigh_release(n);
  781. if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
  782. skb->pkt_type == PACKET_HOST ||
  783. NEIGH_VAR(in_dev->arp_parms, PROXY_DELAY) == 0) {
  784. arp_send_dst(ARPOP_REPLY, ETH_P_ARP,
  785. sip, dev, tip, sha,
  786. dev->dev_addr, sha,
  787. reply_dst);
  788. } else {
  789. pneigh_enqueue(&arp_tbl,
  790. in_dev->arp_parms, skb);
  791. goto out_free_dst;
  792. }
  793. goto out_consume_skb;
  794. }
  795. }
  796. }
  797. /* Update our ARP tables */
  798. n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
  799. addr_type = -1;
  800. if (n || arp_accept(in_dev, sip)) {
  801. is_garp = arp_is_garp(net, dev, &addr_type, arp->ar_op,
  802. sip, tip, sha, tha);
  803. }
  804. if (arp_accept(in_dev, sip)) {
  805. /* Unsolicited ARP is not accepted by default.
  806. It is possible, that this option should be enabled for some
  807. devices (strip is candidate)
  808. */
  809. if (!n &&
  810. (is_garp ||
  811. (arp->ar_op == htons(ARPOP_REPLY) &&
  812. (addr_type == RTN_UNICAST ||
  813. (addr_type < 0 &&
  814. /* postpone calculation to as late as possible */
  815. inet_addr_type_dev_table(net, dev, sip) ==
  816. RTN_UNICAST)))))
  817. n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
  818. }
  819. if (n) {
  820. int state = NUD_REACHABLE;
  821. int override;
  822. /* If several different ARP replies follows back-to-back,
  823. use the FIRST one. It is possible, if several proxy
  824. agents are active. Taking the first reply prevents
  825. arp trashing and chooses the fastest router.
  826. */
  827. override = time_after(jiffies,
  828. n->updated +
  829. NEIGH_VAR(n->parms, LOCKTIME)) ||
  830. is_garp;
  831. /* Broadcast replies and request packets
  832. do not assert neighbour reachability.
  833. */
  834. if (arp->ar_op != htons(ARPOP_REPLY) ||
  835. skb->pkt_type != PACKET_HOST)
  836. state = NUD_STALE;
  837. neigh_update(n, sha, state,
  838. override ? NEIGH_UPDATE_F_OVERRIDE : 0, 0);
  839. neigh_release(n);
  840. }
  841. out_consume_skb:
  842. consume_skb(skb);
  843. out_free_dst:
  844. dst_release(reply_dst);
  845. return NET_RX_SUCCESS;
  846. out_free_skb:
  847. kfree_skb(skb);
  848. return NET_RX_DROP;
  849. }
  850. static void parp_redo(struct sk_buff *skb)
  851. {
  852. arp_process(dev_net(skb->dev), NULL, skb);
  853. }
  854. static int arp_is_multicast(const void *pkey)
  855. {
  856. return ipv4_is_multicast(*((__be32 *)pkey));
  857. }
  858. /*
  859. * Receive an arp request from the device layer.
  860. */
  861. static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
  862. struct packet_type *pt, struct net_device *orig_dev)
  863. {
  864. const struct arphdr *arp;
  865. /* do not tweak dropwatch on an ARP we will ignore */
  866. if (dev->flags & IFF_NOARP ||
  867. skb->pkt_type == PACKET_OTHERHOST ||
  868. skb->pkt_type == PACKET_LOOPBACK)
  869. goto consumeskb;
  870. skb = skb_share_check(skb, GFP_ATOMIC);
  871. if (!skb)
  872. goto out_of_mem;
  873. /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
  874. if (!pskb_may_pull(skb, arp_hdr_len(dev)))
  875. goto freeskb;
  876. arp = arp_hdr(skb);
  877. if (arp->ar_hln != dev->addr_len || arp->ar_pln != 4)
  878. goto freeskb;
  879. memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
  880. return NF_HOOK(NFPROTO_ARP, NF_ARP_IN,
  881. dev_net(dev), NULL, skb, dev, NULL,
  882. arp_process);
  883. consumeskb:
  884. consume_skb(skb);
  885. return NET_RX_SUCCESS;
  886. freeskb:
  887. kfree_skb(skb);
  888. out_of_mem:
  889. return NET_RX_DROP;
  890. }
  891. /*
  892. * User level interface (ioctl)
  893. */
  894. static struct net_device *arp_req_dev_by_name(struct net *net, struct arpreq *r,
  895. bool getarp)
  896. {
  897. struct net_device *dev;
  898. if (getarp)
  899. dev = dev_get_by_name_rcu(net, r->arp_dev);
  900. else
  901. dev = __dev_get_by_name(net, r->arp_dev);
  902. if (!dev)
  903. return ERR_PTR(-ENODEV);
  904. /* Mmmm... It is wrong... ARPHRD_NETROM == 0 */
  905. if (!r->arp_ha.sa_family)
  906. r->arp_ha.sa_family = dev->type;
  907. if ((r->arp_flags & ATF_COM) && r->arp_ha.sa_family != dev->type)
  908. return ERR_PTR(-EINVAL);
  909. return dev;
  910. }
  911. static struct net_device *arp_req_dev(struct net *net, struct arpreq *r)
  912. {
  913. struct net_device *dev;
  914. struct rtable *rt;
  915. __be32 ip;
  916. if (r->arp_dev[0])
  917. return arp_req_dev_by_name(net, r, false);
  918. if (r->arp_flags & ATF_PUBL)
  919. return NULL;
  920. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  921. rt = ip_route_output(net, ip, 0, 0, 0, RT_SCOPE_LINK);
  922. if (IS_ERR(rt))
  923. return ERR_CAST(rt);
  924. dev = rt->dst.dev;
  925. ip_rt_put(rt);
  926. if (!dev)
  927. return ERR_PTR(-EINVAL);
  928. return dev;
  929. }
  930. /*
  931. * Set (create) an ARP cache entry.
  932. */
  933. static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
  934. {
  935. if (!dev) {
  936. IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
  937. return 0;
  938. }
  939. if (__in_dev_get_rtnl(dev)) {
  940. IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
  941. return 0;
  942. }
  943. return -ENXIO;
  944. }
  945. static int arp_req_set_public(struct net *net, struct arpreq *r,
  946. struct net_device *dev)
  947. {
  948. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  949. if (!dev && (r->arp_flags & ATF_COM)) {
  950. dev = dev_getbyhwaddr(net, r->arp_ha.sa_family,
  951. r->arp_ha.sa_data);
  952. if (!dev)
  953. return -ENODEV;
  954. }
  955. if (mask) {
  956. __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  957. if (!pneigh_lookup(&arp_tbl, net, &ip, dev, 1))
  958. return -ENOBUFS;
  959. return 0;
  960. }
  961. return arp_req_set_proxy(net, dev, 1);
  962. }
  963. static int arp_req_set(struct net *net, struct arpreq *r)
  964. {
  965. struct neighbour *neigh;
  966. struct net_device *dev;
  967. __be32 ip;
  968. int err;
  969. dev = arp_req_dev(net, r);
  970. if (IS_ERR(dev))
  971. return PTR_ERR(dev);
  972. if (r->arp_flags & ATF_PUBL)
  973. return arp_req_set_public(net, r, dev);
  974. switch (dev->type) {
  975. #if IS_ENABLED(CONFIG_FDDI)
  976. case ARPHRD_FDDI:
  977. /*
  978. * According to RFC 1390, FDDI devices should accept ARP
  979. * hardware types of 1 (Ethernet). However, to be more
  980. * robust, we'll accept hardware types of either 1 (Ethernet)
  981. * or 6 (IEEE 802.2).
  982. */
  983. if (r->arp_ha.sa_family != ARPHRD_FDDI &&
  984. r->arp_ha.sa_family != ARPHRD_ETHER &&
  985. r->arp_ha.sa_family != ARPHRD_IEEE802)
  986. return -EINVAL;
  987. break;
  988. #endif
  989. default:
  990. if (r->arp_ha.sa_family != dev->type)
  991. return -EINVAL;
  992. break;
  993. }
  994. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  995. neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
  996. err = PTR_ERR(neigh);
  997. if (!IS_ERR(neigh)) {
  998. unsigned int state = NUD_STALE;
  999. if (r->arp_flags & ATF_PERM) {
  1000. r->arp_flags |= ATF_COM;
  1001. state = NUD_PERMANENT;
  1002. }
  1003. err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
  1004. r->arp_ha.sa_data : NULL, state,
  1005. NEIGH_UPDATE_F_OVERRIDE |
  1006. NEIGH_UPDATE_F_ADMIN, 0);
  1007. neigh_release(neigh);
  1008. }
  1009. return err;
  1010. }
  1011. static unsigned int arp_state_to_flags(struct neighbour *neigh)
  1012. {
  1013. if (neigh->nud_state&NUD_PERMANENT)
  1014. return ATF_PERM | ATF_COM;
  1015. else if (neigh->nud_state&NUD_VALID)
  1016. return ATF_COM;
  1017. else
  1018. return 0;
  1019. }
  1020. /*
  1021. * Get an ARP cache entry.
  1022. */
  1023. static int arp_req_get(struct net *net, struct arpreq *r)
  1024. {
  1025. __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
  1026. struct neighbour *neigh;
  1027. struct net_device *dev;
  1028. if (!r->arp_dev[0])
  1029. return -ENODEV;
  1030. dev = arp_req_dev_by_name(net, r, true);
  1031. if (IS_ERR(dev))
  1032. return PTR_ERR(dev);
  1033. neigh = neigh_lookup(&arp_tbl, &ip, dev);
  1034. if (!neigh)
  1035. return -ENXIO;
  1036. if (READ_ONCE(neigh->nud_state) & NUD_NOARP) {
  1037. neigh_release(neigh);
  1038. return -ENXIO;
  1039. }
  1040. read_lock_bh(&neigh->lock);
  1041. memcpy(r->arp_ha.sa_data, neigh->ha,
  1042. min(dev->addr_len, sizeof(r->arp_ha.sa_data_min)));
  1043. r->arp_flags = arp_state_to_flags(neigh);
  1044. read_unlock_bh(&neigh->lock);
  1045. neigh_release(neigh);
  1046. r->arp_ha.sa_family = dev->type;
  1047. netdev_copy_name(dev, r->arp_dev);
  1048. return 0;
  1049. }
  1050. int arp_invalidate(struct net_device *dev, __be32 ip, bool force)
  1051. {
  1052. struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
  1053. int err = -ENXIO;
  1054. struct neigh_table *tbl = &arp_tbl;
  1055. if (neigh) {
  1056. if ((READ_ONCE(neigh->nud_state) & NUD_VALID) && !force) {
  1057. neigh_release(neigh);
  1058. return 0;
  1059. }
  1060. if (READ_ONCE(neigh->nud_state) & ~NUD_NOARP)
  1061. err = neigh_update(neigh, NULL, NUD_FAILED,
  1062. NEIGH_UPDATE_F_OVERRIDE|
  1063. NEIGH_UPDATE_F_ADMIN, 0);
  1064. write_lock_bh(&tbl->lock);
  1065. neigh_release(neigh);
  1066. neigh_remove_one(neigh, tbl);
  1067. write_unlock_bh(&tbl->lock);
  1068. }
  1069. return err;
  1070. }
  1071. static int arp_req_delete_public(struct net *net, struct arpreq *r,
  1072. struct net_device *dev)
  1073. {
  1074. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  1075. if (mask) {
  1076. __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  1077. return pneigh_delete(&arp_tbl, net, &ip, dev);
  1078. }
  1079. return arp_req_set_proxy(net, dev, 0);
  1080. }
  1081. static int arp_req_delete(struct net *net, struct arpreq *r)
  1082. {
  1083. struct net_device *dev;
  1084. __be32 ip;
  1085. dev = arp_req_dev(net, r);
  1086. if (IS_ERR(dev))
  1087. return PTR_ERR(dev);
  1088. if (r->arp_flags & ATF_PUBL)
  1089. return arp_req_delete_public(net, r, dev);
  1090. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  1091. return arp_invalidate(dev, ip, true);
  1092. }
  1093. /*
  1094. * Handle an ARP layer I/O control request.
  1095. */
  1096. int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  1097. {
  1098. struct arpreq r;
  1099. __be32 *netmask;
  1100. int err;
  1101. switch (cmd) {
  1102. case SIOCDARP:
  1103. case SIOCSARP:
  1104. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1105. return -EPERM;
  1106. fallthrough;
  1107. case SIOCGARP:
  1108. err = copy_from_user(&r, arg, sizeof(struct arpreq));
  1109. if (err)
  1110. return -EFAULT;
  1111. break;
  1112. default:
  1113. return -EINVAL;
  1114. }
  1115. if (r.arp_pa.sa_family != AF_INET)
  1116. return -EPFNOSUPPORT;
  1117. if (!(r.arp_flags & ATF_PUBL) &&
  1118. (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
  1119. return -EINVAL;
  1120. netmask = &((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr;
  1121. if (!(r.arp_flags & ATF_NETMASK))
  1122. *netmask = htonl(0xFFFFFFFFUL);
  1123. else if (*netmask && *netmask != htonl(0xFFFFFFFFUL))
  1124. return -EINVAL;
  1125. switch (cmd) {
  1126. case SIOCDARP:
  1127. rtnl_lock();
  1128. err = arp_req_delete(net, &r);
  1129. rtnl_unlock();
  1130. break;
  1131. case SIOCSARP:
  1132. rtnl_lock();
  1133. err = arp_req_set(net, &r);
  1134. rtnl_unlock();
  1135. break;
  1136. case SIOCGARP:
  1137. rcu_read_lock();
  1138. err = arp_req_get(net, &r);
  1139. rcu_read_unlock();
  1140. if (!err && copy_to_user(arg, &r, sizeof(r)))
  1141. err = -EFAULT;
  1142. break;
  1143. }
  1144. return err;
  1145. }
  1146. static int arp_netdev_event(struct notifier_block *this, unsigned long event,
  1147. void *ptr)
  1148. {
  1149. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1150. struct netdev_notifier_change_info *change_info;
  1151. struct in_device *in_dev;
  1152. bool evict_nocarrier;
  1153. switch (event) {
  1154. case NETDEV_CHANGEADDR:
  1155. neigh_changeaddr(&arp_tbl, dev);
  1156. rt_cache_flush(dev_net(dev));
  1157. break;
  1158. case NETDEV_CHANGE:
  1159. change_info = ptr;
  1160. if (change_info->flags_changed & IFF_NOARP)
  1161. neigh_changeaddr(&arp_tbl, dev);
  1162. in_dev = __in_dev_get_rtnl(dev);
  1163. if (!in_dev)
  1164. evict_nocarrier = true;
  1165. else
  1166. evict_nocarrier = IN_DEV_ARP_EVICT_NOCARRIER(in_dev);
  1167. if (evict_nocarrier && !netif_carrier_ok(dev))
  1168. neigh_carrier_down(&arp_tbl, dev);
  1169. break;
  1170. default:
  1171. break;
  1172. }
  1173. return NOTIFY_DONE;
  1174. }
  1175. static struct notifier_block arp_netdev_notifier = {
  1176. .notifier_call = arp_netdev_event,
  1177. };
  1178. /* Note, that it is not on notifier chain.
  1179. It is necessary, that this routine was called after route cache will be
  1180. flushed.
  1181. */
  1182. void arp_ifdown(struct net_device *dev)
  1183. {
  1184. neigh_ifdown(&arp_tbl, dev);
  1185. }
  1186. /*
  1187. * Called once on startup.
  1188. */
  1189. static struct packet_type arp_packet_type __read_mostly = {
  1190. .type = cpu_to_be16(ETH_P_ARP),
  1191. .func = arp_rcv,
  1192. };
  1193. #ifdef CONFIG_PROC_FS
  1194. #if IS_ENABLED(CONFIG_AX25)
  1195. /*
  1196. * ax25 -> ASCII conversion
  1197. */
  1198. static void ax2asc2(ax25_address *a, char *buf)
  1199. {
  1200. char c, *s;
  1201. int n;
  1202. for (n = 0, s = buf; n < 6; n++) {
  1203. c = (a->ax25_call[n] >> 1) & 0x7F;
  1204. if (c != ' ')
  1205. *s++ = c;
  1206. }
  1207. *s++ = '-';
  1208. n = (a->ax25_call[6] >> 1) & 0x0F;
  1209. if (n > 9) {
  1210. *s++ = '1';
  1211. n -= 10;
  1212. }
  1213. *s++ = n + '0';
  1214. *s++ = '\0';
  1215. if (*buf == '\0' || *buf == '-') {
  1216. buf[0] = '*';
  1217. buf[1] = '\0';
  1218. }
  1219. }
  1220. #endif /* CONFIG_AX25 */
  1221. #define HBUFFERLEN 30
  1222. static void arp_format_neigh_entry(struct seq_file *seq,
  1223. struct neighbour *n)
  1224. {
  1225. char hbuffer[HBUFFERLEN];
  1226. int k, j;
  1227. char tbuf[16];
  1228. struct net_device *dev = n->dev;
  1229. int hatype = dev->type;
  1230. read_lock(&n->lock);
  1231. /* Convert hardware address to XX:XX:XX:XX ... form. */
  1232. #if IS_ENABLED(CONFIG_AX25)
  1233. if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
  1234. ax2asc2((ax25_address *)n->ha, hbuffer);
  1235. else {
  1236. #endif
  1237. for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
  1238. hbuffer[k++] = hex_asc_hi(n->ha[j]);
  1239. hbuffer[k++] = hex_asc_lo(n->ha[j]);
  1240. hbuffer[k++] = ':';
  1241. }
  1242. if (k != 0)
  1243. --k;
  1244. hbuffer[k] = 0;
  1245. #if IS_ENABLED(CONFIG_AX25)
  1246. }
  1247. #endif
  1248. sprintf(tbuf, "%pI4", n->primary_key);
  1249. seq_printf(seq, "%-16s 0x%-10x0x%-10x%-17s * %s\n",
  1250. tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
  1251. read_unlock(&n->lock);
  1252. }
  1253. static void arp_format_pneigh_entry(struct seq_file *seq,
  1254. struct pneigh_entry *n)
  1255. {
  1256. struct net_device *dev = n->dev;
  1257. int hatype = dev ? dev->type : 0;
  1258. char tbuf[16];
  1259. sprintf(tbuf, "%pI4", n->key);
  1260. seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
  1261. tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
  1262. dev ? dev->name : "*");
  1263. }
  1264. static int arp_seq_show(struct seq_file *seq, void *v)
  1265. {
  1266. if (v == SEQ_START_TOKEN) {
  1267. seq_puts(seq, "IP address HW type Flags "
  1268. "HW address Mask Device\n");
  1269. } else {
  1270. struct neigh_seq_state *state = seq->private;
  1271. if (state->flags & NEIGH_SEQ_IS_PNEIGH)
  1272. arp_format_pneigh_entry(seq, v);
  1273. else
  1274. arp_format_neigh_entry(seq, v);
  1275. }
  1276. return 0;
  1277. }
  1278. static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
  1279. {
  1280. /* Don't want to confuse "arp -a" w/ magic entries,
  1281. * so we tell the generic iterator to skip NUD_NOARP.
  1282. */
  1283. return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
  1284. }
  1285. static const struct seq_operations arp_seq_ops = {
  1286. .start = arp_seq_start,
  1287. .next = neigh_seq_next,
  1288. .stop = neigh_seq_stop,
  1289. .show = arp_seq_show,
  1290. };
  1291. #endif /* CONFIG_PROC_FS */
  1292. static int __net_init arp_net_init(struct net *net)
  1293. {
  1294. if (!proc_create_net("arp", 0444, net->proc_net, &arp_seq_ops,
  1295. sizeof(struct neigh_seq_state)))
  1296. return -ENOMEM;
  1297. return 0;
  1298. }
  1299. static void __net_exit arp_net_exit(struct net *net)
  1300. {
  1301. remove_proc_entry("arp", net->proc_net);
  1302. }
  1303. static struct pernet_operations arp_net_ops = {
  1304. .init = arp_net_init,
  1305. .exit = arp_net_exit,
  1306. };
  1307. void __init arp_init(void)
  1308. {
  1309. neigh_table_init(NEIGH_ARP_TABLE, &arp_tbl);
  1310. dev_add_pack(&arp_packet_type);
  1311. register_pernet_subsys(&arp_net_ops);
  1312. #ifdef CONFIG_SYSCTL
  1313. neigh_sysctl_register(NULL, &arp_tbl.parms, NULL);
  1314. #endif
  1315. register_netdevice_notifier(&arp_netdev_notifier);
  1316. }