arp.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. .base_reachable_time = 30 * HZ,
  159. .retrans_time = 1 * HZ,
  160. .gc_staletime = 60 * HZ,
  161. .reachable_time = 30 * HZ,
  162. .delay_probe_time = 5 * HZ,
  163. .queue_len_bytes = 64*1024,
  164. .ucast_probes = 3,
  165. .mcast_probes = 3,
  166. .anycast_delay = 1 * HZ,
  167. .proxy_delay = (8 * HZ) / 10,
  168. .proxy_qlen = 64,
  169. .locktime = 1 * HZ,
  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(*(u32 *)pkey, dev, *hash_rnd);
  204. }
  205. static int arp_constructor(struct neighbour *neigh)
  206. {
  207. __be32 addr = *(__be32 *)neigh->primary_key;
  208. struct net_device *dev = neigh->dev;
  209. struct in_device *in_dev;
  210. struct neigh_parms *parms;
  211. rcu_read_lock();
  212. in_dev = __in_dev_get_rcu(dev);
  213. if (in_dev == NULL) {
  214. rcu_read_unlock();
  215. return -EINVAL;
  216. }
  217. neigh->type = inet_addr_type(dev_net(dev), addr);
  218. parms = in_dev->arp_parms;
  219. __neigh_parms_put(neigh->parms);
  220. neigh->parms = neigh_parms_clone(parms);
  221. rcu_read_unlock();
  222. if (!dev->header_ops) {
  223. neigh->nud_state = NUD_NOARP;
  224. neigh->ops = &arp_direct_ops;
  225. neigh->output = neigh_direct_output;
  226. } else {
  227. /* Good devices (checked by reading texts, but only Ethernet is
  228. tested)
  229. ARPHRD_ETHER: (ethernet, apfddi)
  230. ARPHRD_FDDI: (fddi)
  231. ARPHRD_IEEE802: (tr)
  232. ARPHRD_METRICOM: (strip)
  233. ARPHRD_ARCNET:
  234. etc. etc. etc.
  235. ARPHRD_IPDDP will also work, if author repairs it.
  236. I did not it, because this driver does not work even
  237. in old paradigm.
  238. */
  239. #if 1
  240. /* So... these "amateur" devices are hopeless.
  241. The only thing, that I can say now:
  242. It is very sad that we need to keep ugly obsolete
  243. code to make them happy.
  244. They should be moved to more reasonable state, now
  245. they use rebuild_header INSTEAD OF hard_start_xmit!!!
  246. Besides that, they are sort of out of date
  247. (a lot of redundant clones/copies, useless in 2.1),
  248. I wonder why people believe that they work.
  249. */
  250. switch (dev->type) {
  251. default:
  252. break;
  253. case ARPHRD_ROSE:
  254. #if IS_ENABLED(CONFIG_AX25)
  255. case ARPHRD_AX25:
  256. #if IS_ENABLED(CONFIG_NETROM)
  257. case ARPHRD_NETROM:
  258. #endif
  259. neigh->ops = &arp_broken_ops;
  260. neigh->output = neigh->ops->output;
  261. return 0;
  262. #else
  263. break;
  264. #endif
  265. }
  266. #endif
  267. if (neigh->type == RTN_MULTICAST) {
  268. neigh->nud_state = NUD_NOARP;
  269. arp_mc_map(addr, neigh->ha, dev, 1);
  270. } else if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) {
  271. neigh->nud_state = NUD_NOARP;
  272. memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
  273. } else if (neigh->type == RTN_BROADCAST ||
  274. (dev->flags & IFF_POINTOPOINT)) {
  275. neigh->nud_state = NUD_NOARP;
  276. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  277. }
  278. if (dev->header_ops->cache)
  279. neigh->ops = &arp_hh_ops;
  280. else
  281. neigh->ops = &arp_generic_ops;
  282. if (neigh->nud_state & NUD_VALID)
  283. neigh->output = neigh->ops->connected_output;
  284. else
  285. neigh->output = neigh->ops->output;
  286. }
  287. return 0;
  288. }
  289. static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
  290. {
  291. dst_link_failure(skb);
  292. kfree_skb(skb);
  293. }
  294. static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
  295. {
  296. __be32 saddr = 0;
  297. u8 *dst_ha = NULL;
  298. struct net_device *dev = neigh->dev;
  299. __be32 target = *(__be32 *)neigh->primary_key;
  300. int probes = atomic_read(&neigh->probes);
  301. struct in_device *in_dev;
  302. rcu_read_lock();
  303. in_dev = __in_dev_get_rcu(dev);
  304. if (!in_dev) {
  305. rcu_read_unlock();
  306. return;
  307. }
  308. switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
  309. default:
  310. case 0: /* By default announce any local IP */
  311. if (skb && inet_addr_type(dev_net(dev),
  312. ip_hdr(skb)->saddr) == RTN_LOCAL)
  313. saddr = ip_hdr(skb)->saddr;
  314. break;
  315. case 1: /* Restrict announcements of saddr in same subnet */
  316. if (!skb)
  317. break;
  318. saddr = ip_hdr(skb)->saddr;
  319. if (inet_addr_type(dev_net(dev), saddr) == RTN_LOCAL) {
  320. /* saddr should be known to target */
  321. if (inet_addr_onlink(in_dev, target, saddr))
  322. break;
  323. }
  324. saddr = 0;
  325. break;
  326. case 2: /* Avoid secondary IPs, get a primary/preferred one */
  327. break;
  328. }
  329. rcu_read_unlock();
  330. if (!saddr)
  331. saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
  332. probes -= neigh->parms->ucast_probes;
  333. if (probes < 0) {
  334. if (!(neigh->nud_state & NUD_VALID))
  335. pr_debug("trying to ucast probe in NUD_INVALID\n");
  336. dst_ha = neigh->ha;
  337. read_lock_bh(&neigh->lock);
  338. } else {
  339. probes -= neigh->parms->app_probes;
  340. if (probes < 0) {
  341. #ifdef CONFIG_ARPD
  342. neigh_app_ns(neigh);
  343. #endif
  344. return;
  345. }
  346. }
  347. arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
  348. dst_ha, dev->dev_addr, NULL);
  349. if (dst_ha)
  350. read_unlock_bh(&neigh->lock);
  351. }
  352. static int arp_ignore(struct in_device *in_dev, __be32 sip, __be32 tip)
  353. {
  354. int scope;
  355. switch (IN_DEV_ARP_IGNORE(in_dev)) {
  356. case 0: /* Reply, the tip is already validated */
  357. return 0;
  358. case 1: /* Reply only if tip is configured on the incoming interface */
  359. sip = 0;
  360. scope = RT_SCOPE_HOST;
  361. break;
  362. case 2: /*
  363. * Reply only if tip is configured on the incoming interface
  364. * and is in same subnet as sip
  365. */
  366. scope = RT_SCOPE_HOST;
  367. break;
  368. case 3: /* Do not reply for scope host addresses */
  369. sip = 0;
  370. scope = RT_SCOPE_LINK;
  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(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 = skb_rtable(skb)->rt_gateway;
  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. if (target_hw != NULL)
  593. memcpy(arp_ptr, target_hw, dev->addr_len);
  594. else
  595. memset(arp_ptr, 0, dev->addr_len);
  596. arp_ptr += dev->addr_len;
  597. memcpy(arp_ptr, &dest_ip, 4);
  598. return skb;
  599. out:
  600. kfree_skb(skb);
  601. return NULL;
  602. }
  603. EXPORT_SYMBOL(arp_create);
  604. /*
  605. * Send an arp packet.
  606. */
  607. void arp_xmit(struct sk_buff *skb)
  608. {
  609. /* Send it off, maybe filter it using firewalling first. */
  610. NF_HOOK(NFPROTO_ARP, NF_ARP_OUT, skb, NULL, skb->dev, dev_queue_xmit);
  611. }
  612. EXPORT_SYMBOL(arp_xmit);
  613. /*
  614. * Create and send an arp packet.
  615. */
  616. void arp_send(int type, int ptype, __be32 dest_ip,
  617. struct net_device *dev, __be32 src_ip,
  618. const unsigned char *dest_hw, const unsigned char *src_hw,
  619. const unsigned char *target_hw)
  620. {
  621. struct sk_buff *skb;
  622. /*
  623. * No arp on this interface.
  624. */
  625. if (dev->flags&IFF_NOARP)
  626. return;
  627. skb = arp_create(type, ptype, dest_ip, dev, src_ip,
  628. dest_hw, src_hw, target_hw);
  629. if (skb == NULL)
  630. return;
  631. arp_xmit(skb);
  632. }
  633. EXPORT_SYMBOL(arp_send);
  634. /*
  635. * Process an arp request.
  636. */
  637. static int arp_process(struct sk_buff *skb)
  638. {
  639. struct net_device *dev = skb->dev;
  640. struct in_device *in_dev = __in_dev_get_rcu(dev);
  641. struct arphdr *arp;
  642. unsigned char *arp_ptr;
  643. struct rtable *rt;
  644. unsigned char *sha;
  645. __be32 sip, tip;
  646. u16 dev_type = dev->type;
  647. int addr_type;
  648. struct neighbour *n;
  649. struct net *net = dev_net(dev);
  650. bool is_garp = false;
  651. /* arp_rcv below verifies the ARP header and verifies the device
  652. * is ARP'able.
  653. */
  654. if (in_dev == NULL)
  655. goto out;
  656. arp = arp_hdr(skb);
  657. switch (dev_type) {
  658. default:
  659. if (arp->ar_pro != htons(ETH_P_IP) ||
  660. htons(dev_type) != arp->ar_hrd)
  661. goto out;
  662. break;
  663. case ARPHRD_ETHER:
  664. case ARPHRD_FDDI:
  665. case ARPHRD_IEEE802:
  666. /*
  667. * ETHERNET, and Fibre Channel (which are IEEE 802
  668. * devices, according to RFC 2625) devices will accept ARP
  669. * hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).
  670. * This is the case also of FDDI, where the RFC 1390 says that
  671. * FDDI devices should accept ARP hardware of (1) Ethernet,
  672. * however, to be more robust, we'll accept both 1 (Ethernet)
  673. * or 6 (IEEE 802.2)
  674. */
  675. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  676. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  677. arp->ar_pro != htons(ETH_P_IP))
  678. goto out;
  679. break;
  680. case ARPHRD_AX25:
  681. if (arp->ar_pro != htons(AX25_P_IP) ||
  682. arp->ar_hrd != htons(ARPHRD_AX25))
  683. goto out;
  684. break;
  685. case ARPHRD_NETROM:
  686. if (arp->ar_pro != htons(AX25_P_IP) ||
  687. arp->ar_hrd != htons(ARPHRD_NETROM))
  688. goto out;
  689. break;
  690. }
  691. /* Understand only these message types */
  692. if (arp->ar_op != htons(ARPOP_REPLY) &&
  693. arp->ar_op != htons(ARPOP_REQUEST))
  694. goto out;
  695. /*
  696. * Extract fields
  697. */
  698. arp_ptr = (unsigned char *)(arp + 1);
  699. sha = arp_ptr;
  700. arp_ptr += dev->addr_len;
  701. memcpy(&sip, arp_ptr, 4);
  702. arp_ptr += 4;
  703. arp_ptr += dev->addr_len;
  704. memcpy(&tip, arp_ptr, 4);
  705. /*
  706. * Check for bad requests for 127.x.x.x and requests for multicast
  707. * addresses. If this is one such, delete it.
  708. */
  709. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  710. goto out;
  711. /*
  712. * For some 802.11 wireless deployments (and possibly other networks),
  713. * there will be an ARP proxy and gratuitous ARP frames are attacks
  714. * and thus should not be accepted.
  715. */
  716. if (sip == tip && IN_DEV_ORCONF(in_dev, DROP_GRATUITOUS_ARP))
  717. goto out;
  718. /*
  719. * Special case: We must set Frame Relay source Q.922 address
  720. */
  721. if (dev_type == ARPHRD_DLCI)
  722. sha = dev->broadcast;
  723. /*
  724. * Process entry. The idea here is we want to send a reply if it is a
  725. * request for us or if it is a request for someone else that we hold
  726. * a proxy for. We want to add an entry to our cache if it is a reply
  727. * to us or if it is a request for our address.
  728. * (The assumption for this last is that if someone is requesting our
  729. * address, they are probably intending to talk to us, so it saves time
  730. * if we cache their address. Their address is also probably not in
  731. * our cache, since ours is not in their cache.)
  732. *
  733. * Putting this another way, we only care about replies if they are to
  734. * us, in which case we add them to the cache. For requests, we care
  735. * about those for us and those for our proxies. We reply to both,
  736. * and in the case of requests for us we add the requester to the arp
  737. * cache.
  738. */
  739. /* Special case: IPv4 duplicate address detection packet (RFC2131) */
  740. if (sip == 0) {
  741. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  742. inet_addr_type(net, tip) == RTN_LOCAL &&
  743. !arp_ignore(in_dev, sip, tip))
  744. arp_send(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  745. dev->dev_addr, sha);
  746. goto out;
  747. }
  748. if (arp->ar_op == htons(ARPOP_REQUEST) &&
  749. ip_route_input_noref(skb, tip, sip, 0, dev) == 0) {
  750. rt = skb_rtable(skb);
  751. addr_type = rt->rt_type;
  752. if (addr_type == RTN_LOCAL) {
  753. int dont_send;
  754. dont_send = arp_ignore(in_dev, sip, tip);
  755. if (!dont_send && IN_DEV_ARPFILTER(in_dev))
  756. dont_send = arp_filter(sip, tip, dev);
  757. if (!dont_send) {
  758. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  759. if (n) {
  760. arp_send(ARPOP_REPLY, ETH_P_ARP, sip,
  761. dev, tip, sha, dev->dev_addr,
  762. sha);
  763. neigh_release(n);
  764. }
  765. }
  766. goto out;
  767. } else if (IN_DEV_FORWARD(in_dev)) {
  768. if (addr_type == RTN_UNICAST &&
  769. (arp_fwd_proxy(in_dev, dev, rt) ||
  770. arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||
  771. (rt->dst.dev != dev &&
  772. pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))) {
  773. n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
  774. if (n)
  775. neigh_release(n);
  776. if (NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED ||
  777. skb->pkt_type == PACKET_HOST ||
  778. in_dev->arp_parms->proxy_delay == 0) {
  779. arp_send(ARPOP_REPLY, ETH_P_ARP, sip,
  780. dev, tip, sha, dev->dev_addr,
  781. sha);
  782. } else {
  783. pneigh_enqueue(&arp_tbl,
  784. in_dev->arp_parms, skb);
  785. return 0;
  786. }
  787. goto out;
  788. }
  789. }
  790. }
  791. /* Update our ARP tables */
  792. n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
  793. if (IN_DEV_ARP_ACCEPT(in_dev)) {
  794. /* Unsolicited ARP is not accepted by default.
  795. It is possible, that this option should be enabled for some
  796. devices (strip is candidate)
  797. */
  798. is_garp = arp->ar_op == htons(ARPOP_REQUEST) && tip == sip &&
  799. inet_addr_type(net, sip) == RTN_UNICAST;
  800. if (n == NULL &&
  801. ((arp->ar_op == htons(ARPOP_REPLY) &&
  802. inet_addr_type(net, sip) == RTN_UNICAST) || is_garp))
  803. n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
  804. }
  805. if (n) {
  806. int state = NUD_REACHABLE;
  807. int override;
  808. /* If several different ARP replies follows back-to-back,
  809. use the FIRST one. It is possible, if several proxy
  810. agents are active. Taking the first reply prevents
  811. arp trashing and chooses the fastest router.
  812. */
  813. override = time_after(jiffies,
  814. n->updated +
  815. n->parms->locktime) ||
  816. is_garp;
  817. /* Broadcast replies and request packets
  818. do not assert neighbour reachability.
  819. */
  820. if (arp->ar_op != htons(ARPOP_REPLY) ||
  821. skb->pkt_type != PACKET_HOST)
  822. state = NUD_STALE;
  823. neigh_update(n, sha, state,
  824. override ? NEIGH_UPDATE_F_OVERRIDE : 0);
  825. neigh_release(n);
  826. }
  827. out:
  828. consume_skb(skb);
  829. return 0;
  830. }
  831. static void parp_redo(struct sk_buff *skb)
  832. {
  833. arp_process(skb);
  834. }
  835. /*
  836. * Receive an arp request from the device layer.
  837. */
  838. static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
  839. struct packet_type *pt, struct net_device *orig_dev)
  840. {
  841. struct arphdr *arp;
  842. /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
  843. if (!pskb_may_pull(skb, arp_hdr_len(dev)))
  844. goto freeskb;
  845. arp = arp_hdr(skb);
  846. if (arp->ar_hln != dev->addr_len ||
  847. dev->flags & IFF_NOARP ||
  848. skb->pkt_type == PACKET_OTHERHOST ||
  849. skb->pkt_type == PACKET_LOOPBACK ||
  850. arp->ar_pln != 4)
  851. goto freeskb;
  852. skb = skb_share_check(skb, GFP_ATOMIC);
  853. if (skb == NULL)
  854. goto out_of_mem;
  855. memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
  856. return NF_HOOK(NFPROTO_ARP, NF_ARP_IN, skb, dev, NULL, arp_process);
  857. freeskb:
  858. kfree_skb(skb);
  859. out_of_mem:
  860. return 0;
  861. }
  862. /*
  863. * User level interface (ioctl)
  864. */
  865. /*
  866. * Set (create) an ARP cache entry.
  867. */
  868. static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
  869. {
  870. if (dev == NULL) {
  871. IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
  872. return 0;
  873. }
  874. if (__in_dev_get_rtnl(dev)) {
  875. IN_DEV_CONF_SET(__in_dev_get_rtnl(dev), PROXY_ARP, on);
  876. return 0;
  877. }
  878. return -ENXIO;
  879. }
  880. static int arp_req_set_public(struct net *net, struct arpreq *r,
  881. struct net_device *dev)
  882. {
  883. __be32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  884. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  885. if (mask && mask != htonl(0xFFFFFFFF))
  886. return -EINVAL;
  887. if (!dev && (r->arp_flags & ATF_COM)) {
  888. dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
  889. r->arp_ha.sa_data);
  890. if (!dev)
  891. return -ENODEV;
  892. }
  893. if (mask) {
  894. if (pneigh_lookup(&arp_tbl, net, &ip, dev, 1) == NULL)
  895. return -ENOBUFS;
  896. return 0;
  897. }
  898. return arp_req_set_proxy(net, dev, 1);
  899. }
  900. static int arp_req_set(struct net *net, struct arpreq *r,
  901. struct net_device *dev)
  902. {
  903. __be32 ip;
  904. struct neighbour *neigh;
  905. int err;
  906. if (r->arp_flags & ATF_PUBL)
  907. return arp_req_set_public(net, r, dev);
  908. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  909. if (r->arp_flags & ATF_PERM)
  910. r->arp_flags |= ATF_COM;
  911. if (dev == NULL) {
  912. struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
  913. if (IS_ERR(rt))
  914. return PTR_ERR(rt);
  915. dev = rt->dst.dev;
  916. ip_rt_put(rt);
  917. if (!dev)
  918. return -EINVAL;
  919. }
  920. switch (dev->type) {
  921. #if IS_ENABLED(CONFIG_FDDI)
  922. case ARPHRD_FDDI:
  923. /*
  924. * According to RFC 1390, FDDI devices should accept ARP
  925. * hardware types of 1 (Ethernet). However, to be more
  926. * robust, we'll accept hardware types of either 1 (Ethernet)
  927. * or 6 (IEEE 802.2).
  928. */
  929. if (r->arp_ha.sa_family != ARPHRD_FDDI &&
  930. r->arp_ha.sa_family != ARPHRD_ETHER &&
  931. r->arp_ha.sa_family != ARPHRD_IEEE802)
  932. return -EINVAL;
  933. break;
  934. #endif
  935. default:
  936. if (r->arp_ha.sa_family != dev->type)
  937. return -EINVAL;
  938. break;
  939. }
  940. neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
  941. err = PTR_ERR(neigh);
  942. if (!IS_ERR(neigh)) {
  943. unsigned int state = NUD_STALE;
  944. if (r->arp_flags & ATF_PERM)
  945. state = NUD_PERMANENT;
  946. err = neigh_update(neigh, (r->arp_flags & ATF_COM) ?
  947. r->arp_ha.sa_data : NULL, state,
  948. NEIGH_UPDATE_F_OVERRIDE |
  949. NEIGH_UPDATE_F_ADMIN);
  950. neigh_release(neigh);
  951. }
  952. return err;
  953. }
  954. static unsigned int arp_state_to_flags(struct neighbour *neigh)
  955. {
  956. if (neigh->nud_state&NUD_PERMANENT)
  957. return ATF_PERM | ATF_COM;
  958. else if (neigh->nud_state&NUD_VALID)
  959. return ATF_COM;
  960. else
  961. return 0;
  962. }
  963. /*
  964. * Get an ARP cache entry.
  965. */
  966. static int arp_req_get(struct arpreq *r, struct net_device *dev)
  967. {
  968. __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
  969. struct neighbour *neigh;
  970. int err = -ENXIO;
  971. neigh = neigh_lookup(&arp_tbl, &ip, dev);
  972. if (neigh) {
  973. read_lock_bh(&neigh->lock);
  974. memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
  975. r->arp_flags = arp_state_to_flags(neigh);
  976. read_unlock_bh(&neigh->lock);
  977. r->arp_ha.sa_family = dev->type;
  978. strlcpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
  979. neigh_release(neigh);
  980. err = 0;
  981. }
  982. return err;
  983. }
  984. int arp_invalidate(struct net_device *dev, __be32 ip)
  985. {
  986. struct neighbour *neigh = neigh_lookup(&arp_tbl, &ip, dev);
  987. int err = -ENXIO;
  988. if (neigh) {
  989. if (neigh->nud_state & ~NUD_NOARP)
  990. err = neigh_update(neigh, NULL, NUD_FAILED,
  991. NEIGH_UPDATE_F_OVERRIDE|
  992. NEIGH_UPDATE_F_ADMIN);
  993. neigh_release(neigh);
  994. }
  995. return err;
  996. }
  997. EXPORT_SYMBOL(arp_invalidate);
  998. static int arp_req_delete_public(struct net *net, struct arpreq *r,
  999. struct net_device *dev)
  1000. {
  1001. __be32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
  1002. __be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
  1003. if (mask == htonl(0xFFFFFFFF))
  1004. return pneigh_delete(&arp_tbl, net, &ip, dev);
  1005. if (mask)
  1006. return -EINVAL;
  1007. return arp_req_set_proxy(net, dev, 0);
  1008. }
  1009. static int arp_req_delete(struct net *net, struct arpreq *r,
  1010. struct net_device *dev)
  1011. {
  1012. __be32 ip;
  1013. if (r->arp_flags & ATF_PUBL)
  1014. return arp_req_delete_public(net, r, dev);
  1015. ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
  1016. if (dev == NULL) {
  1017. struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
  1018. if (IS_ERR(rt))
  1019. return PTR_ERR(rt);
  1020. dev = rt->dst.dev;
  1021. ip_rt_put(rt);
  1022. if (!dev)
  1023. return -EINVAL;
  1024. }
  1025. return arp_invalidate(dev, ip);
  1026. }
  1027. /*
  1028. * Handle an ARP layer I/O control request.
  1029. */
  1030. int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  1031. {
  1032. int err;
  1033. struct arpreq r;
  1034. struct net_device *dev = NULL;
  1035. switch (cmd) {
  1036. case SIOCDARP:
  1037. case SIOCSARP:
  1038. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1039. return -EPERM;
  1040. case SIOCGARP:
  1041. err = copy_from_user(&r, arg, sizeof(struct arpreq));
  1042. if (err)
  1043. return -EFAULT;
  1044. break;
  1045. default:
  1046. return -EINVAL;
  1047. }
  1048. if (r.arp_pa.sa_family != AF_INET)
  1049. return -EPFNOSUPPORT;
  1050. if (!(r.arp_flags & ATF_PUBL) &&
  1051. (r.arp_flags & (ATF_NETMASK | ATF_DONTPUB)))
  1052. return -EINVAL;
  1053. if (!(r.arp_flags & ATF_NETMASK))
  1054. ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr =
  1055. htonl(0xFFFFFFFFUL);
  1056. rtnl_lock();
  1057. if (r.arp_dev[0]) {
  1058. err = -ENODEV;
  1059. dev = __dev_get_by_name(net, r.arp_dev);
  1060. if (dev == NULL)
  1061. goto out;
  1062. /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
  1063. if (!r.arp_ha.sa_family)
  1064. r.arp_ha.sa_family = dev->type;
  1065. err = -EINVAL;
  1066. if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
  1067. goto out;
  1068. } else if (cmd == SIOCGARP) {
  1069. err = -ENODEV;
  1070. goto out;
  1071. }
  1072. switch (cmd) {
  1073. case SIOCDARP:
  1074. err = arp_req_delete(net, &r, dev);
  1075. break;
  1076. case SIOCSARP:
  1077. err = arp_req_set(net, &r, dev);
  1078. break;
  1079. case SIOCGARP:
  1080. err = arp_req_get(&r, dev);
  1081. break;
  1082. }
  1083. out:
  1084. rtnl_unlock();
  1085. if (cmd == SIOCGARP && !err && copy_to_user(arg, &r, sizeof(r)))
  1086. err = -EFAULT;
  1087. return err;
  1088. }
  1089. static int arp_netdev_event(struct notifier_block *this, unsigned long event,
  1090. void *ptr)
  1091. {
  1092. struct net_device *dev = ptr;
  1093. switch (event) {
  1094. case NETDEV_CHANGEADDR:
  1095. neigh_changeaddr(&arp_tbl, dev);
  1096. rt_cache_flush(dev_net(dev), 0);
  1097. break;
  1098. default:
  1099. break;
  1100. }
  1101. return NOTIFY_DONE;
  1102. }
  1103. static struct notifier_block arp_netdev_notifier = {
  1104. .notifier_call = arp_netdev_event,
  1105. };
  1106. /* Note, that it is not on notifier chain.
  1107. It is necessary, that this routine was called after route cache will be
  1108. flushed.
  1109. */
  1110. void arp_ifdown(struct net_device *dev)
  1111. {
  1112. neigh_ifdown(&arp_tbl, dev);
  1113. }
  1114. /*
  1115. * Called once on startup.
  1116. */
  1117. static struct packet_type arp_packet_type __read_mostly = {
  1118. .type = cpu_to_be16(ETH_P_ARP),
  1119. .func = arp_rcv,
  1120. };
  1121. static int arp_proc_init(void);
  1122. void __init arp_init(void)
  1123. {
  1124. neigh_table_init(&arp_tbl);
  1125. dev_add_pack(&arp_packet_type);
  1126. arp_proc_init();
  1127. #ifdef CONFIG_SYSCTL
  1128. neigh_sysctl_register(NULL, &arp_tbl.parms, "ipv4", NULL);
  1129. #endif
  1130. register_netdevice_notifier(&arp_netdev_notifier);
  1131. }
  1132. #ifdef CONFIG_PROC_FS
  1133. #if IS_ENABLED(CONFIG_AX25)
  1134. /* ------------------------------------------------------------------------ */
  1135. /*
  1136. * ax25 -> ASCII conversion
  1137. */
  1138. static char *ax2asc2(ax25_address *a, char *buf)
  1139. {
  1140. char c, *s;
  1141. int n;
  1142. for (n = 0, s = buf; n < 6; n++) {
  1143. c = (a->ax25_call[n] >> 1) & 0x7F;
  1144. if (c != ' ')
  1145. *s++ = c;
  1146. }
  1147. *s++ = '-';
  1148. n = (a->ax25_call[6] >> 1) & 0x0F;
  1149. if (n > 9) {
  1150. *s++ = '1';
  1151. n -= 10;
  1152. }
  1153. *s++ = n + '0';
  1154. *s++ = '\0';
  1155. if (*buf == '\0' || *buf == '-')
  1156. return "*";
  1157. return buf;
  1158. }
  1159. #endif /* CONFIG_AX25 */
  1160. #define HBUFFERLEN 30
  1161. static void arp_format_neigh_entry(struct seq_file *seq,
  1162. struct neighbour *n)
  1163. {
  1164. char hbuffer[HBUFFERLEN];
  1165. int k, j;
  1166. char tbuf[16];
  1167. struct net_device *dev = n->dev;
  1168. int hatype = dev->type;
  1169. read_lock(&n->lock);
  1170. /* Convert hardware address to XX:XX:XX:XX ... form. */
  1171. #if IS_ENABLED(CONFIG_AX25)
  1172. if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
  1173. ax2asc2((ax25_address *)n->ha, hbuffer);
  1174. else {
  1175. #endif
  1176. for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < dev->addr_len; j++) {
  1177. hbuffer[k++] = hex_asc_hi(n->ha[j]);
  1178. hbuffer[k++] = hex_asc_lo(n->ha[j]);
  1179. hbuffer[k++] = ':';
  1180. }
  1181. if (k != 0)
  1182. --k;
  1183. hbuffer[k] = 0;
  1184. #if IS_ENABLED(CONFIG_AX25)
  1185. }
  1186. #endif
  1187. sprintf(tbuf, "%pI4", n->primary_key);
  1188. seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
  1189. tbuf, hatype, arp_state_to_flags(n), hbuffer, dev->name);
  1190. read_unlock(&n->lock);
  1191. }
  1192. static void arp_format_pneigh_entry(struct seq_file *seq,
  1193. struct pneigh_entry *n)
  1194. {
  1195. struct net_device *dev = n->dev;
  1196. int hatype = dev ? dev->type : 0;
  1197. char tbuf[16];
  1198. sprintf(tbuf, "%pI4", n->key);
  1199. seq_printf(seq, "%-16s 0x%-10x0x%-10x%s * %s\n",
  1200. tbuf, hatype, ATF_PUBL | ATF_PERM, "00:00:00:00:00:00",
  1201. dev ? dev->name : "*");
  1202. }
  1203. static int arp_seq_show(struct seq_file *seq, void *v)
  1204. {
  1205. if (v == SEQ_START_TOKEN) {
  1206. seq_puts(seq, "IP address HW type Flags "
  1207. "HW address Mask Device\n");
  1208. } else {
  1209. struct neigh_seq_state *state = seq->private;
  1210. if (state->flags & NEIGH_SEQ_IS_PNEIGH)
  1211. arp_format_pneigh_entry(seq, v);
  1212. else
  1213. arp_format_neigh_entry(seq, v);
  1214. }
  1215. return 0;
  1216. }
  1217. static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
  1218. {
  1219. /* Don't want to confuse "arp -a" w/ magic entries,
  1220. * so we tell the generic iterator to skip NUD_NOARP.
  1221. */
  1222. return neigh_seq_start(seq, pos, &arp_tbl, NEIGH_SEQ_SKIP_NOARP);
  1223. }
  1224. /* ------------------------------------------------------------------------ */
  1225. static const struct seq_operations arp_seq_ops = {
  1226. .start = arp_seq_start,
  1227. .next = neigh_seq_next,
  1228. .stop = neigh_seq_stop,
  1229. .show = arp_seq_show,
  1230. };
  1231. static int arp_seq_open(struct inode *inode, struct file *file)
  1232. {
  1233. return seq_open_net(inode, file, &arp_seq_ops,
  1234. sizeof(struct neigh_seq_state));
  1235. }
  1236. static const struct file_operations arp_seq_fops = {
  1237. .owner = THIS_MODULE,
  1238. .open = arp_seq_open,
  1239. .read = seq_read,
  1240. .llseek = seq_lseek,
  1241. .release = seq_release_net,
  1242. };
  1243. static int __net_init arp_net_init(struct net *net)
  1244. {
  1245. if (!proc_net_fops_create(net, "arp", S_IRUGO, &arp_seq_fops))
  1246. return -ENOMEM;
  1247. return 0;
  1248. }
  1249. static void __net_exit arp_net_exit(struct net *net)
  1250. {
  1251. proc_net_remove(net, "arp");
  1252. }
  1253. static struct pernet_operations arp_net_ops = {
  1254. .init = arp_net_init,
  1255. .exit = arp_net_exit,
  1256. };
  1257. static int __init arp_proc_init(void)
  1258. {
  1259. return register_pernet_subsys(&arp_net_ops);
  1260. }
  1261. #else /* CONFIG_PROC_FS */
  1262. static int __init arp_proc_init(void)
  1263. {
  1264. return 0;
  1265. }
  1266. #endif /* CONFIG_PROC_FS */