arp.c 35 KB

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