arp.c 35 KB

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